llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clangir @llvm/pr-subscribers-clang Author: Andy Kaylor (andykaylor) <details> <summary>Changes</summary> This adds support to CIR for calling functions through pointer to method pointers with the Itanium ABI for x86_64 targets. The ARM-specific handling of method pointers is not-yet implemented. --- Patch is 30.67 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/176063.diff 13 Files Affected: - (modified) clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h (+30) - (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+54) - (modified) clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td (+8) - (modified) clang/include/clang/CIR/MissingFeatures.h (+6) - (modified) clang/lib/CIR/CodeGen/CIRGenCall.h (+2) - (modified) clang/lib/CIR/CodeGen/CIRGenExpr.cpp (+2-5) - (modified) clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp (+44) - (modified) clang/lib/CIR/CodeGen/CIRGenFunction.h (+3) - (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+47) - (modified) clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp (+11-1) - (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h (+8) - (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp (+152-2) - (modified) clang/test/CIR/CodeGen/pointer-to-member-func.cpp (+84) ``````````diff diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index eadf3dd6ee0f0..2aaae86240cf2 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -710,6 +710,36 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value = {}) { return cir::YieldOp::create(*this, loc, value); } + + struct GetMethodResults { + mlir::Value callee; + mlir::Value adjustedThis; + }; + + GetMethodResults createGetMethod(mlir::Location loc, mlir::Value method, + mlir::Value objectPtr) { + // Build the callee function type. + auto methodFuncTy = + mlir::cast<cir::MethodType>(method.getType()).getMemberFuncTy(); + auto methodFuncInputTypes = methodFuncTy.getInputs(); + + auto objectPtrTy = mlir::cast<cir::PointerType>(objectPtr.getType()); + mlir::Type adjustedThisTy = getVoidPtrTy(objectPtrTy.getAddrSpace()); + + llvm::SmallVector<mlir::Type> calleeFuncInputTypes{adjustedThisTy}; + calleeFuncInputTypes.insert(calleeFuncInputTypes.end(), + methodFuncInputTypes.begin(), + methodFuncInputTypes.end()); + cir::FuncType calleeFuncTy = + methodFuncTy.clone(calleeFuncInputTypes, methodFuncTy.getReturnType()); + // TODO(cir): consider the address space of the callee. + assert(!cir::MissingFeatures::addressSpace()); + cir::PointerType calleeTy = getPointerTo(calleeFuncTy); + + auto op = cir::GetMethodOp::create(*this, loc, calleeTy, adjustedThisTy, + method, objectPtr); + return {op.getCallee(), op.getAdjustedThis()}; + } }; } // namespace cir diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index ceb9899a00ac4..07950d6e42f63 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -3805,6 +3805,60 @@ def CIR_GetRuntimeMemberOp : CIR_Op<"get_runtime_member"> { let hasLLVMLowering = false; } +//===----------------------------------------------------------------------===// +// GetMethodOp +//===----------------------------------------------------------------------===// + +def CIR_GetMethodOp : CIR_Op<"get_method"> { + let summary = "Resolve a method to a function pointer as callee"; + let description = [{ + The `cir.get_method` operation takes a pointer to method (!cir.method) and + a pointer to a class object (!cir.ptr<!cir.record>>) as input, and + yields a function pointer that points to the actual function corresponding + to the input method. The operation also applies any necessary adjustments to + the input object pointer for calling the method and yields the adjusted + pointer. + + This operation is generated when calling a method through a pointer-to- + member-function in C++: + + ```cpp + // Foo *object; + // int arg; + // void (Foo::*method)(int); + + (object->*method)(arg); + ``` + + The code above will generate CIR similar to: + + ```mlir + %callee, %this = cir.get_method %method, %object + cir.call %callee(%this, %arg) + ``` + + The method type must match the callee type. That is: + - The return type of the method must match the return type of the callee. + - The first parameter of the callee must have type `!cir.ptr<!cir.void>`. + - Types of other parameters of the callee must match the parameters of the + method. + }]; + + let arguments = (ins CIR_MethodType:$method, CIR_PtrToRecordType:$object); + let results = (outs CIR_PtrToFunc:$callee, CIR_VoidPtrType:$adjusted_this); + + let assemblyFormat = [{ + $method `,` $object + `:` `(` qualified(type($method)) `,` qualified(type($object)) `)` + `->` `(` qualified(type($callee)) `,` qualified(type($adjusted_this)) `)` + attr-dict + }]; + + let hasVerifier = 1; + let hasLLVMLowering = false; + let hasCXXABILowering = true; +} + //===----------------------------------------------------------------------===// // VecCreate //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td index 3b2ec5276a677..1a5bae13c96df 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td @@ -191,6 +191,12 @@ def CIR_AnyComplexOrIntOrBoolOrFloatType def CIR_AnyRecordType : CIR_TypeBase<"::cir::RecordType", "record type">; +//===----------------------------------------------------------------------===// +// Function Type predicates +//===----------------------------------------------------------------------===// + +def CIR_AnyFuncType : CIR_TypeBase<"::cir::FuncType", "function type">; + //===----------------------------------------------------------------------===// // Array Type predicates //===----------------------------------------------------------------------===// @@ -253,6 +259,8 @@ def CIR_PtrToComplexType : CIR_PtrToType<CIR_AnyComplexType>; def CIR_PtrToRecordType : CIR_PtrToType<CIR_AnyRecordType>; +def CIR_PtrToFunc : CIR_PtrToType<CIR_AnyFuncType>; + def CIR_PtrToArray : CIR_PtrToType<CIR_AnyArrayType>; //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h index 9336fafea04cd..70b92757a4eee 100644 --- a/clang/include/clang/CIR/MissingFeatures.h +++ b/clang/include/clang/CIR/MissingFeatures.h @@ -194,6 +194,11 @@ struct MissingFeatures { static bool lowerModuleCodeGenOpts() { return false; } static bool lowerModuleLangOpts() { return false; } + // Extra checks for lowerGetMethod in ItaniumCXXABI + static bool emitCFICheck() { return false; } + static bool emitVFEInfo() { return false; } + static bool emitWPDInfo() { return false; } + // Misc static bool aarch64SIMDIntrinsics() { return false; } static bool aarch64SMEIntrinsics() { return false; } @@ -211,6 +216,7 @@ struct MissingFeatures { static bool aggValueSlotVolatile() { return false; } static bool alignCXXRecordDecl() { return false; } static bool allocToken() { return false; } + static bool appleArm64CXXABI() { return false; } static bool appleKext() { return false; } static bool armComputeVolatileBitfields() { return false; } static bool asmGoto() { return false; } diff --git a/clang/lib/CIR/CodeGen/CIRGenCall.h b/clang/lib/CIR/CodeGen/CIRGenCall.h index 55b3d9765c5c5..347bd4a7c8266 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCall.h +++ b/clang/lib/CIR/CodeGen/CIRGenCall.h @@ -33,6 +33,8 @@ class CIRGenCalleeInfo { CIRGenCalleeInfo(const clang::FunctionProtoType *calleeProtoTy, clang::GlobalDecl calleeDecl) : calleeProtoTy(calleeProtoTy), calleeDecl(calleeDecl) {} + CIRGenCalleeInfo(const clang::FunctionProtoType *calleeProtoTy) + : calleeProtoTy(calleeProtoTy) {} CIRGenCalleeInfo(clang::GlobalDecl calleeDecl) : calleeProtoTy(nullptr), calleeDecl(calleeDecl) {} diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index cd13498e3702f..ca6357e2ba138 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -2303,11 +2303,8 @@ RValue CIRGenFunction::emitCXXMemberCallExpr(const CXXMemberCallExpr *ce, ReturnValueSlot returnValue) { const Expr *callee = ce->getCallee()->IgnoreParens(); - if (isa<BinaryOperator>(callee)) { - cgm.errorNYI(ce->getSourceRange(), - "emitCXXMemberCallExpr: C++ binary operator"); - return RValue::get(nullptr); - } + if (isa<BinaryOperator>(callee)) + return emitCXXMemberPointerCallExpr(ce, returnValue); const auto *me = cast<MemberExpr>(callee); const auto *md = cast<CXXMethodDecl>(me->getMemberDecl()); diff --git a/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp b/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp index eb894c2fb30ee..98cf75f0d69e0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp @@ -75,6 +75,50 @@ static MemberCallInfo commonBuildCXXMemberOrOperatorCall( return {required, prefixSize}; } +RValue +CIRGenFunction::emitCXXMemberPointerCallExpr(const CXXMemberCallExpr *ce, + ReturnValueSlot returnValue) { + const BinaryOperator *bo = + cast<BinaryOperator>(ce->getCallee()->IgnoreParens()); + const Expr *baseExpr = bo->getLHS(); + const Expr *memFnExpr = bo->getRHS(); + + const auto *mpt = memFnExpr->getType()->castAs<MemberPointerType>(); + const auto *fpt = mpt->getPointeeType()->castAs<FunctionProtoType>(); + + // Emit the 'this' pointer. + Address thisAddr = Address::invalid(); + if (bo->getOpcode() == BO_PtrMemI) + thisAddr = emitPointerWithAlignment(baseExpr); + else + thisAddr = emitLValue(baseExpr).getAddress(); + + assert(!cir::MissingFeatures::emitTypeCheck()); + + // Get the member function pointer. + mlir::Value memFnPtr = emitScalarExpr(memFnExpr); + + // Resolve the member function pointer to the actual callee and adjust the + // "this" pointer for call. + mlir::Location loc = getLoc(ce->getExprLoc()); + auto [/*mlir::Value*/ calleePtr, /*mlir::Value*/ adjustedThis] = + builder.createGetMethod(loc, memFnPtr, thisAddr.getPointer()); + + // Prepare the call arguments. + CallArgList argsList; + argsList.add(RValue::get(adjustedThis), getContext().VoidPtrTy); + emitCallArgs(argsList, fpt, ce->arguments()); + + RequiredArgs required = RequiredArgs::getFromProtoWithExtraSlots(fpt, 1); + + // Build the call. + CIRGenCallee callee(fpt, calleePtr.getDefiningOp()); + assert(!cir::MissingFeatures::opCallMustTail()); + return emitCall(cgm.getTypes().arrangeCXXMethodCall(argsList, fpt, required, + /*PrefixSize=*/0), + callee, returnValue, argsList, nullptr, loc); +} + RValue CIRGenFunction::emitCXXMemberOrOperatorMemberCallExpr( const CallExpr *ce, const CXXMethodDecl *md, ReturnValueSlot returnValue, bool hasQualifier, NestedNameSpecifier qualifier, bool isArrow, diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index 5fe1d9a4f2b76..6b47bc9975fcf 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -1553,6 +1553,9 @@ class CIRGenFunction : public CIRGenTypeCache { clang::NestedNameSpecifier qualifier, bool isArrow, const clang::Expr *base); + RValue emitCXXMemberPointerCallExpr(const CXXMemberCallExpr *ce, + ReturnValueSlot returnValue); + mlir::Value emitCXXNewExpr(const CXXNewExpr *e); void emitNewArrayInitializer(const CXXNewExpr *e, QualType elementType, diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 6c4607abb40e7..1dec653e421c4 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2666,6 +2666,53 @@ LogicalResult cir::GetRuntimeMemberOp::verify() { return mlir::success(); } +//===----------------------------------------------------------------------===// +// GetMethodOp Definitions +//===----------------------------------------------------------------------===// + +LogicalResult cir::GetMethodOp::verify() { + cir::MethodType methodTy = getMethod().getType(); + + // Assume objectTy is !cir.ptr<!T> + cir::PointerType objectPtrTy = getObject().getType(); + mlir::Type objectTy = objectPtrTy.getPointee(); + + if (methodTy.getClassTy() != objectTy) + return emitError() << "method class type and object type do not match"; + + // Assume methodFuncTy is !cir.func<!Ret (!Args)> + auto calleeTy = mlir::cast<cir::FuncType>(getCallee().getType().getPointee()); + cir::FuncType methodFuncTy = methodTy.getMemberFuncTy(); + + // We verify at here that calleeTy is !cir.func<!Ret (!cir.ptr<!void>, !Args)> + // Note that the first parameter type of the callee is !cir.ptr<!void> instead + // of !cir.ptr<!T> because the "this" pointer may be adjusted before calling + // the callee. + + if (methodFuncTy.getReturnType() != calleeTy.getReturnType()) + return emitError() + << "method return type and callee return type do not match"; + + llvm::ArrayRef<mlir::Type> calleeArgsTy = calleeTy.getInputs(); + llvm::ArrayRef<mlir::Type> methodFuncArgsTy = methodFuncTy.getInputs(); + + if (calleeArgsTy.empty()) + return emitError() << "callee parameter list lacks receiver object ptr"; + + auto calleeThisArgPtrTy = mlir::dyn_cast<cir::PointerType>(calleeArgsTy[0]); + if (!calleeThisArgPtrTy || + !mlir::isa<cir::VoidType>(calleeThisArgPtrTy.getPointee())) { + return emitError() + << "the first parameter of callee must be a void pointer"; + } + + if (calleeArgsTy.slice(1) != methodFuncArgsTy) + return emitError() + << "callee parameters and method parameters do not match"; + + return mlir::success(); +} + //===----------------------------------------------------------------------===// // GetMemberOp Definitions //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp index 145f8574893f4..469dddbb118e3 100644 --- a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp @@ -59,7 +59,7 @@ class CIRGenericCXXABILoweringPattern : public mlir::ConversionPattern { // Do not match on operations that have dedicated ABI lowering rewrite rules if (llvm::isa<cir::AllocaOp, cir::BaseDataMemberOp, cir::ConstantOp, cir::CmpOp, cir::DerivedDataMemberOp, cir::FuncOp, - cir::GetRuntimeMemberOp, cir::GlobalOp>(op)) + cir::GetMethodOp, cir::GetRuntimeMemberOp, cir::GlobalOp>(op)) return mlir::failure(); const mlir::TypeConverter *typeConverter = getTypeConverter(); @@ -252,6 +252,16 @@ mlir::LogicalResult CIRDerivedDataMemberOpABILowering::matchAndRewrite( return mlir::success(); } +mlir::LogicalResult CIRGetMethodOpABILowering::matchAndRewrite( + cir::GetMethodOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + mlir::Value loweredResults[2]; + lowerModule->getCXXABI().lowerGetMethod( + op, loweredResults, adaptor.getMethod(), adaptor.getObject(), rewriter); + rewriter.replaceOp(op, loweredResults); + return mlir::success(); +} + mlir::LogicalResult CIRGetRuntimeMemberOpABILowering::matchAndRewrite( cir::GetRuntimeMemberOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h index 0dedfa7221f5f..69d8b682ab4c5 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h @@ -66,6 +66,14 @@ class CIRCXXABI { mlir::Value loweredAddr, mlir::Value loweredMember, mlir::OpBuilder &builder) const = 0; + /// Lower the given cir.get_method op to a sequence of more "primitive" CIR + /// operations that act on the ABI types. The lowered result values will be + /// stored in the given loweredResults array. + virtual void + lowerGetMethod(cir::GetMethodOp op, mlir::Value (&loweredResults)[2], + mlir::Value loweredMethod, mlir::Value loweredObjectPtr, + mlir::ConversionPatternRewriter &rewriter) const = 0; + /// Lower the given cir.base_data_member op to a sequence of more "primitive" /// CIR operations that act on the ABI types. virtual mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op, diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp index d944fa3294684..d581e18b5e37a 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp @@ -30,8 +30,12 @@ namespace cir { namespace { class LowerItaniumCXXABI : public CIRCXXABI { +protected: + bool useARMMethodPtrABI; + public: - LowerItaniumCXXABI(LowerModule &lm) : CIRCXXABI(lm) {} + LowerItaniumCXXABI(LowerModule &lm, bool useARMMethodPtrABI = false) + : CIRCXXABI(lm), useARMMethodPtrABI(useARMMethodPtrABI) {} /// Lower the given data member pointer type to its ABI type. The returned /// type is also a CIR type. @@ -56,6 +60,10 @@ class LowerItaniumCXXABI : public CIRCXXABI { mlir::Value loweredAddr, mlir::Value loweredMember, mlir::OpBuilder &builder) const override; + void lowerGetMethod(cir::GetMethodOp op, mlir::Value (&loweredResults)[2], + mlir::Value loweredMethod, mlir::Value loweredObjectPtr, + mlir::ConversionPatternRewriter &rewriter) const override; + mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder) const override; @@ -72,7 +80,26 @@ class LowerItaniumCXXABI : public CIRCXXABI { } // namespace std::unique_ptr<CIRCXXABI> createItaniumCXXABI(LowerModule &lm) { - return std::make_unique<LowerItaniumCXXABI>(lm); + switch (lm.getCXXABIKind()) { + // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't + // include the other 32-bit ARM oddities: constructor/destructor return values + // and array cookies. + case clang::TargetCXXABI::GenericAArch64: + case clang::TargetCXXABI::AppleARM64: + // TODO: this isn't quite right, clang uses AppleARM64CXXABI which inherits + // from ARMCXXABI. We'll have to follow suit. + assert(!cir::MissingFeatures::appleArm64CXXABI()); + return std::make_unique<LowerItaniumCXXABI>(lm, + /*useARMMethodPtrABI=*/true); + + case clang::TargetCXXABI::GenericItanium: + return std::make_unique<LowerItaniumCXXABI>(lm); + + case clang::TargetCXXABI::Microsoft: + llvm_unreachable("Microsoft ABI is not Itanium-based"); + default: + llvm_unreachable("Other Itanium ABI?"); + } } static cir::IntType getPtrDiffCIRTy(LowerModule &lm) { @@ -197,6 +224,129 @@ mlir::Operation *LowerItaniumCXXABI::lowerGetRuntimeMember( cir::CastKind::bitcast, memberBytesPtr); } +void LowerItaniumCXXABI::lowerGetMethod( + cir::GetMethodOp op, mlir::Value (&loweredResults)[2], + mlir::Value loweredMethod, mlir::Value loweredObjectPtr, + mlir::ConversionPatternRewriter &rewriter) const { + // In the Itanium and ARM ABIs, method pointers have the form: + // struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; + // + // In the Itanium ABI: + // - method pointers are virtual if (memptr.ptr & 1) is nonzero + // - the this-adjustment is (memptr.adj) + // - the virtual offset is (memptr.ptr - 1) + // + // In the ARM ABI: + // - method pointers are virtual if (memptr.adj & 1) is nonzero + // - the this-adjustment is (memptr.adj >> 1) + // - the virtual offset is (memptr.ptr) + // ARM uses 'adj' for the virtual flag because Thumb functions + // may be only single-byte aligned. + // + // If the member is virtual, the adjusted 'this' pointer points + // to a vtable pointer from which the virtual offset is applied. + // + // If the member is non-virtual, memptr.ptr is the address of + // the function to call. + + mlir::Value &callee = loweredResults[0]; + mlir::Value &adjustedThis = loweredResults[1]; + mlir::Type calleePtrTy = op.getCallee().getType(); + + cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lm); + mlir::Value ptrdiffOne = cir::ConstantOp::create( + rewriter, op.getLoc(), cir::IntAttr::get(ptrdiffCIRTy, 1)); + + mlir::Value adj = cir::ExtractMemberOp::creat... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/176063 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
