llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clangir Author: Andy Kaylor (andykaylor) <details> <summary>Changes</summary> This change refactors the CIR calling convention lowering va_arg handling to allow targets to create their own subclass of the CIRABIRewriteContext, moves the target-specific parts of va_arg handling into a subclass for x86-64 and adds an AArch64 subclass. Windows and Darwin targets, which both require special handling, are not yet implemented. Assisted-by: Cursor / claude-opus-5.5 --- Patch is 64.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/244729.diff 10 Files Affected: - (modified) clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h (+7-1) - (modified) clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAArch64.cpp (+28-17) - (modified) clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp (+47-31) - (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp (+79-32) - (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h (+61-10) - (added) clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContextAArch64.cpp (+308) - (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt (+1) - (added) clang/test/CIR/CodeGen/aarch64-be-var-arg.c (+115) - (added) clang/test/CIR/CodeGen/aarch64-soft-float-var-arg.c (+49) - (added) clang/test/CIR/CodeGen/aarch64-var-arg.c (+269) ``````````diff diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h index b125ca743f0bd..4ab006a80a756 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h +++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h @@ -181,8 +181,14 @@ createSPIRVABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp); /// The AArch64 classifier, covering AAPCS64 and DarwinPCS. The ABI variant, /// endianness, data model, and source language come from \p modOp's /// attributes. +/// The AArch64 options used by the classifier. +llvm::abi::AArch64ABIOptions getAArch64ABIOptions(mlir::ModuleOp modOp); + +/// The AArch64 classifier, covering AAPCS64 and DarwinPCS, which classifies +/// with \p opts. std::unique_ptr<CIRABIClassifier> -createAArch64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp); +createAArch64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp, + const llvm::abi::AArch64ABIOptions &opts); /// Map a CIR calling convention to the llvm::CallingConv the ABI classifier /// uses. Conventions the classifier does not distinguish fall back to C. diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAArch64.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAArch64.cpp index 9499b619c7653..cab7215a24bff 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAArch64.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAArch64.cpp @@ -22,13 +22,7 @@ using namespace mlir; using namespace mlir::abi; using namespace cir; -namespace { - -/// The AArch64 options the library classifies with, derived from \p modOp's -/// attributes the way classic CodeGen derives them from the target and the -/// language options. A module with no triple is classified as AAPCS64 -/// little-endian, and one with no source language as C. -llvm::abi::AArch64ABIOptions getAArch64ABIOptions(mlir::ModuleOp modOp) { +llvm::abi::AArch64ABIOptions cir::getAArch64ABIOptions(mlir::ModuleOp modOp) { llvm::Triple triple("aarch64-unknown-linux-gnu"); if (auto tripleAttr = modOp->getAttrOfType<mlir::StringAttr>( cir::CIRDialect::getTripleAttrName())) @@ -75,20 +69,35 @@ llvm::abi::AArch64ABIOptions getAArch64ABIOptions(mlir::ModuleOp modOp) { return opts; } +namespace { + class AArch64ABIClassifier final : public CIRABIClassifierCommon { public: - AArch64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp) - : CIRABIClassifierCommon(dl, modOp), + AArch64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp, + const llvm::abi::AArch64ABIOptions &opts) + : CIRABIClassifierCommon(dl, modOp), abiKind(opts.Kind), targetInfo(llvm::abi::createAArch64TargetInfo( - typeMapper.getTypeBuilder(), getAArch64ABIOptions(modOp))) {} + typeMapper.getTypeBuilder(), opts)) {} - /// The AArch64 `va_list` and the expansion of `cir.va_arg` through it are - /// not implemented yet. + /// Classify the type fetched by \p vaArg as an unnamed argument with a full + /// register budget. mlir::FailureOr<ArgClassification> classifyVAArg(cir::VAArgOp vaArg) override { - vaArg->emitOpError() - << "not yet implemented in CallConvLowering for AArch64"; - return mlir::failure(); + switch (abiKind) { + case llvm::abi::AArch64ABIKind::DarwinPCS: + vaArg->emitOpError() + << "not yet implemented in CallConvLowering for Darwin AArch64"; + return mlir::failure(); + case llvm::abi::AArch64ABIKind::Win64: + vaArg->emitOpError() + << "not yet implemented in CallConvLowering for Windows AArch64"; + return mlir::failure(); + case llvm::abi::AArch64ABIKind::AAPCS: + case llvm::abi::AArch64ABIKind::AAPCSSoft: + break; + } + return classifyVarArgType(vaArg.getType(), *targetInfo, + [&]() { return vaArg->emitOpError(); }); } protected: @@ -99,6 +108,7 @@ class AArch64ABIClassifier final : public CIRABIClassifierCommon { } private: + llvm::abi::AArch64ABIKind abiKind; std::unique_ptr<llvm::abi::TargetInfo> targetInfo; }; @@ -106,6 +116,7 @@ class AArch64ABIClassifier final : public CIRABIClassifierCommon { std::unique_ptr<CIRABIClassifier> cir::createAArch64ABIClassifier(const mlir::DataLayout &dl, - mlir::ModuleOp modOp) { - return std::make_unique<AArch64ABIClassifier>(dl, modOp); + mlir::ModuleOp modOp, + const llvm::abi::AArch64ABIOptions &opts) { + return std::make_unique<AArch64ABIClassifier>(dl, modOp, opts); } diff --git a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp index 0a0b6e6ddad32..ae33be0883b0b 100644 --- a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp @@ -123,6 +123,13 @@ struct CallConvLoweringPass void runOnOperation() override; + /// Build the classifier and the rewrite context the pass options select for + /// \p modOp. Exactly one of `target` and `classificationAttr` must be set. + std::pair<std::unique_ptr<CIRABIClassifier>, + std::unique_ptr<CIRABIRewriteContext>> + createClassifierAndRewriteContext(const DataLayout &dl, ModuleOp modOp, + bool enableNoundefAttrs) const; + /// The x86_64 flags whose value depends on the target and the requested ABI /// compatibility version. Carried outside the pass options because the /// struct has no command-line parser, so a cir-opt run gets the library @@ -132,27 +139,40 @@ struct CallConvLoweringPass } // namespace -/// Build the classifier the pass options select. Exactly one of \p target -/// and \p classificationAttrName is set, which runOnOperation checks before -/// calling. -static std::unique_ptr<CIRABIClassifier> -createABIClassifier(cir::CallConvTarget target, - StringRef classificationAttrName, const DataLayout &dl, - ModuleOp modOp, const ABIClassifierOptions &options) { - if (!classificationAttrName.empty()) - return createInjectedABIClassifier(dl, classificationAttrName); +std::pair<std::unique_ptr<CIRABIClassifier>, + std::unique_ptr<CIRABIRewriteContext>> +CallConvLoweringPass::createClassifierAndRewriteContext( + const DataLayout &dl, ModuleOp modOp, bool enableNoundefAttrs) const { + auto baseRewriteCtx = [&] { + return std::make_unique<CIRABIRewriteContext>(modOp, dl, + enableNoundefAttrs); + }; + if (!classificationAttr.empty()) + return {createInjectedABIClassifier(dl, classificationAttr), + baseRewriteCtx()}; switch (target) { case cir::CallConvTarget::Test: - return createTestTargetABIClassifier(dl); - case cir::CallConvTarget::X86_64: - return createX86_64ABIClassifier(dl, modOp, options); + return {createTestTargetABIClassifier(dl), baseRewriteCtx()}; + case cir::CallConvTarget::X86_64: { + ABIClassifierOptions options; + options.x86AvxAbiLevel = x86AvxAbiLevel; + options.allowsX86TargetAttrAvx = allowsX86TargetAttrAvx; + options.x86AbiCompat = x86AbiCompat; + return {createX86_64ABIClassifier(dl, modOp, options), + createX86_64ABIRewriteContext(modOp, dl, enableNoundefAttrs)}; + } case cir::CallConvTarget::AMDGPU: - return createAMDGPUABIClassifier(dl, modOp); + return {createAMDGPUABIClassifier(dl, modOp), baseRewriteCtx()}; case cir::CallConvTarget::SPIRV: - return createSPIRVABIClassifier(dl, modOp); - case cir::CallConvTarget::AArch64: - return createAArch64ABIClassifier(dl, modOp); + return {createSPIRVABIClassifier(dl, modOp), baseRewriteCtx()}; + case cir::CallConvTarget::AArch64: { + llvm::abi::AArch64ABIOptions opts = getAArch64ABIOptions(modOp); + bool softFloat = opts.Kind == llvm::abi::AArch64ABIKind::AAPCSSoft; + return {createAArch64ABIClassifier(dl, modOp, opts), + createAArch64ABIRewriteContext(modOp, dl, enableNoundefAttrs, + softFloat)}; + } case cir::CallConvTarget::None: break; } @@ -220,22 +240,18 @@ void CallConvLoweringPass::runOnOperation() { } DataLayout dl(moduleOp); - CIRABIRewriteContext rewriteCtx(moduleOp, dl, enableNoundefAttrs); + std::unique_ptr<cir::CIRABIClassifier> classifier; + std::unique_ptr<CIRABIRewriteContext> rewriteCtx; + std::tie(classifier, rewriteCtx) = + createClassifierAndRewriteContext(dl, moduleOp, enableNoundefAttrs); // A non-byval indirect parameter's slot outlives the rewrite that retypes // the parameter, so that a call forwarding the parameter can still recognize // it. Draining on scope exit collapses those slots whichever way this // function returns. llvm::scope_exit drainParamSlots( - [&] { rewriteCtx.finalizeParameterSlots(); }); + [&] { rewriteCtx->finalizeParameterSlots(); }); SymbolTable symbolTable(moduleOp); - cir::ABIClassifierOptions classifierOptions; - classifierOptions.x86AvxAbiLevel = x86AvxAbiLevel; - classifierOptions.allowsX86TargetAttrAvx = allowsX86TargetAttrAvx; - classifierOptions.x86AbiCompat = x86AbiCompat; - std::unique_ptr<cir::CIRABIClassifier> classifier = createABIClassifier( - target, classificationAttr, dl, moduleOp, classifierOptions); - // Classify every cir.func up front. No IR mutation happens here, so // later walks can consult any function's classification regardless of // visitation order. @@ -342,7 +358,7 @@ void CallConvLoweringPass::runOnOperation() { // makes the forwarding decision independent of the order the callee and its // caller were declared in. for (auto &kv : classifications) { - if (failed(rewriteCtx.prepareNonByvalParameters(kv.first, kv.second))) { + if (failed(rewriteCtx->prepareNonByvalParameters(kv.first, kv.second))) { signalPassFailure(); return; } @@ -373,7 +389,7 @@ void CallConvLoweringPass::runOnOperation() { for (auto &kv : classifications) { cir::FuncOp func = kv.first; const FunctionClassification &fc = kv.second; - if (failed(rewriteCtx.rewriteFunctionDefinition(func, fc, builder))) { + if (failed(rewriteCtx->rewriteFunctionDefinition(func, fc, builder))) { signalPassFailure(); return; } @@ -388,13 +404,13 @@ void CallConvLoweringPass::runOnOperation() { } if (hasIndirectSlot(*callFc)) widenForArgMemory(cast<cir::CIRCallOpInterface>(callOp)); - if (failed(rewriteCtx.rewriteCallSite(callOp, *callFc, builder))) { + if (failed(rewriteCtx->rewriteCallSite(callOp, *callFc, builder))) { signalPassFailure(); return; } } for (cir::GetGlobalOp addrOp : addressTakers.lookup(func)) - rewriteCtx.rewriteFunctionAddress(addrOp, func, builder); + rewriteCtx->rewriteFunctionAddress(addrOp, func, builder); } // Rewrite indirect call sites. The callee is opaque, so classify from the @@ -423,7 +439,7 @@ void CallConvLoweringPass::runOnOperation() { } if (hasIndirectSlot(*fc)) widenForArgMemory(c); - if (failed(rewriteCtx.rewriteCallSite(c.getOperation(), *fc, builder))) { + if (failed(rewriteCtx->rewriteCallSite(c.getOperation(), *fc, builder))) { signalPassFailure(); return; } @@ -442,7 +458,7 @@ void CallConvLoweringPass::runOnOperation() { signalPassFailure(); return; } - if (failed(rewriteCtx.rewriteVAArg(v.getOperation(), *ac, builder))) { + if (failed(rewriteCtx->rewriteVAArg(v.getOperation(), *ac, builder))) { signalPassFailure(); return; } diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp index b41e5da0d3da2..d8e2e3b39a446 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp @@ -2579,20 +2579,29 @@ struct RegisterCursor { mlir::Value inRegs; }; -mlir::LogicalResult reportVAArgNYI(cir::VAArgOp op, llvm::StringRef what) { - op->emitOpError() << "va_arg of " << what - << " not yet implemented in CallConvLowering"; - return mlir::failure(); -} +class X86_64ABIRewriteContext final : public CIRABIRewriteContext { +public: + using CIRABIRewriteContext::CIRABIRewriteContext; + +protected: + mlir::LogicalResult expandVAArg(cir::VAArgOp op, const ArgClassification &ac, + mlir::OpBuilder &opBuilder) override; + +private: + static mlir::LogicalResult + classifyRegisterPair(cir::VAArgOp op, const ArgClassification &ac, + std::array<bool, 2> &pairIsSse, bool &isRegPair); + mlir::Value buildOverflowAddrAndAdvance(CIRBaseBuilderTy &b, + const VAArgFetch &f) const; +}; /// Sets \p isRegPair when a two-register argument is a coerced pair, one /// register per member, rather than a single wide scalar, and records in /// \p pairIsSse which element is SSE class. Fails on a coercion that is not /// two eightbytes. -mlir::LogicalResult classifyRegisterPair(cir::VAArgOp op, - const ArgClassification &ac, - std::array<bool, 2> &pairIsSse, - bool &isRegPair) { +mlir::LogicalResult X86_64ABIRewriteContext::classifyRegisterPair( + cir::VAArgOp op, const ArgClassification &ac, + std::array<bool, 2> &pairIsSse, bool &isRegPair) { auto pairTy = mlir::dyn_cast<cir::RecordType>(ac.coercedType); if (!pairTy) return mlir::success(); @@ -2615,28 +2624,10 @@ uint64_t argumentAreaAlign(mlir::Type ty, mlir::ModuleOp modOp) { return cir::CIRDataLayout(modOp).getTypeLanguageAlignment(ty); } -mlir::Value roundPointerUpToAlignment(CIRBaseBuilderTy &b, mlir::Location loc, - mlir::Value bytePtr, uint64_t align, - const mlir::DataLayout &dl) { - assert(mlir::cast<cir::PointerType>(bytePtr.getType()).getPointee() == - b.getUIntNTy(8) && - "the bump strides in bytes, so the pointee must be u8"); - assert(llvm::isPowerOf2_64(align) && - "mask rounding needs a power-of-two alignment"); - mlir::Value bumped = - b.createPtrStride(loc, bytePtr, b.getSignedInt(loc, align - 1, 32)); - std::optional<uint64_t> indexWidth = - dl.getTypeIndexBitwidth(bytePtr.getType()); - assert(indexWidth && "a pointer in the argument area has an index width"); - mlir::Value mask = b.getSignedInt(loc, -static_cast<int64_t>(align), - static_cast<unsigned>(*indexWidth)); - return cir::PtrMaskOp::create(b, loc, bytePtr.getType(), bumped, mask); -} - /// Reads the argument's address out of the overflow area, which also advances /// the cursor past the argument. -mlir::Value buildOverflowAddrAndAdvance(CIRBaseBuilderTy &b, - const VAArgFetch &f) { +mlir::Value X86_64ABIRewriteContext::buildOverflowAddrAndAdvance( + CIRBaseBuilderTy &b, const VAArgFetch &f) const { cir::IntType byteTy = b.getUIntNTy(8); mlir::Value overflowP = b.createGetMember( f.loc, b.getPointerTo(f.vaFields[2]), f.valist, "overflow_arg_area", 2); @@ -2645,7 +2636,7 @@ mlir::Value buildOverflowAddrAndAdvance(CIRBaseBuilderTy &b, uint64_t tyAlign = argumentAreaAlign(f.resultTy, f.module); if (tyAlign > 8) - bytePtr = roundPointerUpToAlignment(b, f.loc, bytePtr, tyAlign, f.dl); + bytePtr = roundPointerUpToAlignment(b, f.loc, bytePtr, tyAlign); uint64_t stride = llvm::alignTo(f.dl.getTypeAllocSize(f.resultTy).getFixedValue(), 8); @@ -2811,7 +2802,6 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, CIRBaseBuilderTy builder(opBuilder); mlir::Location loc = op.getLoc(); mlir::Type resultTy = op.getType(); - mlir::Value valist = op.getArgList(); // An ignored type travels in no register and no stack slot, so the fetch // reads nothing and must leave the cursor unchanged. The value holds no @@ -2823,6 +2813,15 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, op->erase(); return mlir::success(); } + return expandVAArg(op, ac, opBuilder); +} + +mlir::LogicalResult X86_64ABIRewriteContext::expandVAArg( + cir::VAArgOp op, const ArgClassification &ac, mlir::OpBuilder &opBuilder) { + CIRBaseBuilderTy builder(opBuilder); + mlir::Location loc = op.getLoc(); + mlir::Type resultTy = op.getType(); + mlir::Value valist = op.getArgList(); if (ac.kind == ArgKind::Indirect && !ac.byVal) return reportVAArgNYI(op, "a non-trivially-copyable type"); @@ -2940,7 +2939,7 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, if (mlir::isa<cir::RecordType>(resultTy)) { SmallPtrSet<mlir::Operation *, 1> noExceptions; if (failed(rewireRecordUses(builder, loc, op.getResult(), resultAddr, - resultAlign, dl, noExceptions))) + resultAlign, noExceptions))) return mlir::failure(); } else { op.getResult().replaceAllUsesWith( @@ -2949,3 +2948,51 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, op->erase(); return mlir::success(); } + +mlir::LogicalResult CIRABIRewriteContext::expandVAArg(cir::VAArgOp op, + const ArgClassification &, + mlir::OpBuilder &) { + op->emitOpError() + << "va_arg not yet implemented in CallConvLowering for this target"; + return mlir::failure(); +} + +mlir::LogicalResult CIRABIRewriteContext::rewireRecordUses( + mlir::OpBuilder &builder, mlir::Location loc, mlir::Value val, + mlir::Value addr, uint64_t addrAlign, + const SmallPtrSetImpl<mlir::Operation *> &exceptions) const { + return ::rewireRecordUses(builder, loc, val, addr, addrAlign, dl, exceptions); +} + +mlir::LogicalResult CIRABIRewriteContext::reportVAArgNYI(cir::VAArgOp op, + llvm::StringRef what) { + op->emitOpError() << "va_arg of " << what + << " not yet implemented in CallConvLowering"; + return mlir::failure(); +} + +mlir::Value CIRABIRewriteContext::roundPointerUpToAlignment( + CIRBaseBuilderTy &b, mlir::Location loc, mlir::Value bytePtr, + uint64_t align) const { + assert(mlir::cast<cir::PointerType>(bytePtr.getType()).getPointee() == + b.getUIntNTy(8) && + "the bump strides in bytes, so the pointee must be u8"); + assert(llvm::isPowerOf2_64(align) && + "mask rounding needs a power-of-two alignment"); + mlir::Value bumped = + b.createPtrStride(loc, bytePtr, b.getSignedInt(loc, align - 1, 32)); + std::optional<uint64_t> indexWidth = + dl.getTypeIndexBitwidth(bytePtr.getType()); + assert(indexWidth && "a pointer in the argument area has an index width"); + mlir::Value mask = b.getSignedInt(loc, -static_cast<int64_t>(align), + static_cast<unsigned>(*indexWidth)); + return cir::PtrMaskOp::create(b, loc, bytePtr.getType(), bumped, mask); +} + +std::unique_ptr<CIRABIRewriteContext> +cir::createX86_64ABIRewriteContext(mlir::ModuleOp module, + const mlir::DataLayout &dl, + bool enableNoundefAttrs) { + return std::make_unique<X86_64ABIRewriteContext>(module, dl, + enableNoundefAttrs); +} diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h index 671be6c185a83..31a425ee26e0c 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h @@ -25,8 +25,10 @@ #include "mlir/IR/BuiltinOps.h" #include "mlir/Interfaces/DataLayoutInterfaces.h" #include "clang/CIR/Dialect/Analysis/CIRAliasAnalysis.h" +#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" #include... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/244729 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
