https://github.com/andykaylor created https://github.com/llvm/llvm-project/pull/244729
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 >From 7f9956ad9c5328e83b54b7966965f4dea7fe5ebc Mon Sep 17 00:00:00 2001 From: Andy Kaylor <[email protected]> Date: Thu, 12 Nov 2026 17:34:57 -0800 Subject: [PATCH] [CIR] Implement CIR va_arg lowering for AArch64 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 --- .../CIR/Dialect/Transforms/CIRABIClassifier.h | 8 +- .../Transforms/CIRABIClassifierAArch64.cpp | 45 ++- .../Transforms/CallConvLoweringPass.cpp | 78 +++-- .../TargetLowering/CIRABIRewriteContext.cpp | 111 +++++-- .../TargetLowering/CIRABIRewriteContext.h | 71 +++- .../CIRABIRewriteContextAArch64.cpp | 308 ++++++++++++++++++ .../Transforms/TargetLowering/CMakeLists.txt | 1 + clang/test/CIR/CodeGen/aarch64-be-var-arg.c | 115 +++++++ .../CIR/CodeGen/aarch64-soft-float-var-arg.c | 49 +++ clang/test/CIR/CodeGen/aarch64-var-arg.c | 269 +++++++++++++++ 10 files changed, 964 insertions(+), 91 deletions(-) create mode 100644 clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContextAArch64.cpp create mode 100644 clang/test/CIR/CodeGen/aarch64-be-var-arg.c create mode 100644 clang/test/CIR/CodeGen/aarch64-soft-float-var-arg.c create mode 100644 clang/test/CIR/CodeGen/aarch64-var-arg.c diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h index b125ca743f0bd2..4ab006a80a756a 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 9499b619c7653a..cab7215a24bffc 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 0a0b6e6ddad32d..ae33be0883b0bb 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 b41e5da0d3da24..d8e2e3b39a4460 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 671be6c185a83c..31a425ee26e0c7 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 "clang/CIR/Dialect/IR/CIRDataLayout.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include <cassert> #include <utility> @@ -39,14 +41,18 @@ namespace cir { /// for each cir.func / cir.call and dispatches to this class to perform the /// actual IR rewriting using cir dialect operations. /// +/// The rewriting of signatures, calls and returns is target-independent. The +/// expansion of `cir.va_arg` depends on the target's va_list, and a subclass +/// for each target that has one supplies it by overriding expandVAArg. +/// /// Holds a reference to the module's DataLayout for coercion alignment /// queries. The DataLayout must outlive the rewrite context. class CIRABIRewriteContext : public mlir::abi::ABIRewriteContext { public: CIRABIRewriteContext(mlir::ModuleOp module, const mlir::DataLayout &dl, bool enableNoundefAttrs) - : module(module), dl(dl), enableNoundefAttrs(enableNoundefAttrs), - cirDataLayout(module), aliasAnalysis(module) { + : module(module), dl(dl), cirDataLayout(module), + enableNoundefAttrs(enableNoundefAttrs), aliasAnalysis(module) { cir::registerCIRAliasAnalyses(aliasAnalysis, module); } @@ -65,11 +71,11 @@ class CIRABIRewriteContext : public mlir::abi::ABIRewriteContext { const mlir::abi::FunctionClassification &fc, mlir::OpBuilder &builder) override; - /// Expand a `cir.va_arg` into the x86-64 SysV register-save-area / - /// overflow-area sequence. + /// Replace a `cir.va_arg` of an ignored type with poison, leaving the + /// va_list unchanged. Every other `cir.va_arg` is passed to expandVAArg. mlir::LogicalResult rewriteVAArg(mlir::Operation *vaArgOp, const mlir::abi::ArgClassification &ac, - mlir::OpBuilder &builder) override; + mlir::OpBuilder &builder) final; /// Retype \p addrOp, which holds the address of \p funcOp, to the signature /// funcOp was rewritten to, and cast it back so existing uses keep the type @@ -115,19 +121,50 @@ class CIRABIRewriteContext : public mlir::abi::ABIRewriteContext { mlir::StringRef getDialectNamespace() const override { return "cir"; } -private: +protected: + /// Expand \p op, whose type is not ignored, into a read through the + /// target's va_list that also advances the va_list past the value. This + /// implementation reports that the target has no expansion. + virtual mlir::LogicalResult + expandVAArg(cir::VAArgOp op, const mlir::abi::ArgClassification &ac, + mlir::OpBuilder &builder); + + /// Rewire the uses of the record value \p val, whose bytes sit at \p addr, + /// which is aligned to \p addrAlign, so that each one finds the record in + /// memory. A store of \p val becomes a cir.copy from \p addr, and every + /// other use outside \p exceptions is pointed at one cir.load of \p addr + /// created at the builder's insertion point. Fails, after reporting it, on + /// a use that cannot read the record from memory, before anything is + /// rewired. + mlir::LogicalResult rewireRecordUses( + mlir::OpBuilder &builder, mlir::Location loc, mlir::Value val, + mlir::Value addr, uint64_t addrAlign, + const llvm::SmallPtrSetImpl<mlir::Operation *> &exceptions) const; + + /// Report on \p op that a va_arg of \p what is not implemented yet, and + /// fail. + static mlir::LogicalResult reportVAArgNYI(cir::VAArgOp op, + llvm::StringRef what); + + /// Round \p bytePtr, a pointer to u8, up to a multiple of \p align, which is + /// a power of two. + mlir::Value roundPointerUpToAlignment(CIRBaseBuilderTy &b, mlir::Location loc, + mlir::Value bytePtr, + uint64_t align) const; + mlir::ModuleOp module; const mlir::DataLayout &dl; + /// The CIR view of the module's data layout, which provides the store and + /// alloc sizes that \c dl does not. + cir::CIRDataLayout cirDataLayout; + +private: /// Whether extended and indirect arguments are marked llvm.noundef. Set /// from the enable_noundef_attrs field of the module's /// #cir.lowering_options. bool enableNoundefAttrs; - /// The CIR view of the module's data layout, which provides the store and - /// alloc sizes that \c dl does not. - cir::CIRDataLayout cirDataLayout; - /// Alias analysis over the module with the CIR implementations registered. /// rewriteCallSite asks it whether a byval operand's storage is written /// between its load and the call. @@ -146,6 +183,20 @@ class CIRABIRewriteContext : public mlir::abi::ABIRewriteContext { /// The signature the indirect call \p call reaches its callee through. cir::FuncType getIndirectCalleeType(cir::CIRCallOpInterface call); +/// A rewrite context that expands `cir.va_arg` through the x86-64 SysV +/// va_list. +std::unique_ptr<CIRABIRewriteContext> +createX86_64ABIRewriteContext(mlir::ModuleOp module, const mlir::DataLayout &dl, + bool enableNoundefAttrs); + +/// A rewrite context that expands `cir.va_arg` through the AAPCS64 va_list. +/// When \p softFloat is set, floating-point and vector values are read from +/// the general register save area. +std::unique_ptr<CIRABIRewriteContext> +createAArch64ABIRewriteContext(mlir::ModuleOp module, + const mlir::DataLayout &dl, + bool enableNoundefAttrs, bool softFloat); + } // namespace cir #endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRABIREWRITECONTEXT_H diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContextAArch64.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContextAArch64.cpp new file mode 100644 index 00000000000000..d31f50750f5eb9 --- /dev/null +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContextAArch64.cpp @@ -0,0 +1,308 @@ +//===- CIRABIRewriteContextAArch64.cpp - AArch64 ABI rewrite context -----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// The AArch64 rewrite context, which expands cir.va_arg through the AAPCS64 +// __stack / __gr_top / __vr_top / __gr_offs / __vr_offs va_list. +// +//===----------------------------------------------------------------------===// + +#include "CIRABIRewriteContext.h" +#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" +#include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "clang/CIR/Dialect/IR/CIRTypes.h" +#include "llvm/Support/Alignment.h" +#include "llvm/Support/MathExtras.h" +#include <algorithm> + +using namespace cir; +using namespace mlir; +using namespace mlir::abi; + +namespace { + +class AArch64ABIRewriteContext final : public CIRABIRewriteContext { +public: + AArch64ABIRewriteContext(mlir::ModuleOp module, const mlir::DataLayout &dl, + bool enableNoundefAttrs, bool softFloat) + : CIRABIRewriteContext(module, dl, enableNoundefAttrs), + softFloat(softFloat) {} + +protected: + mlir::LogicalResult expandVAArg(cir::VAArgOp op, const ArgClassification &ac, + mlir::OpBuilder &opBuilder) override; + +private: + /// Whether the module uses the soft-float ABI, which has no vector + /// registers, so floating-point and vector values are read from the general + /// register save area. + bool softFloat; +}; + +/// The alignment AAPCS64 places a variadic argument at. This is the +/// unadjusted alignment, which an `aligned` attribute on a record does not +/// raise. +uint64_t unadjustedArgAlign(mlir::Type ty, mlir::ModuleOp modOp, + const mlir::DataLayout &dl) { + if (auto recTy = mlir::dyn_cast<cir::RecordType>(ty)) + if (auto layout = cir::tryGetRecordLayout(modOp, recTy.getName())) + return layout.getUnadjustedAlign(); + return dl.getTypeABIAlignment(ty); +} + +/// Whether AAPCS64 treats \p ty as an aggregate rather than a scalar. +bool isAAPCS64Aggregate(mlir::Type ty) { + return mlir::isa<cir::RecordType, cir::ArrayType, cir::ComplexType>(ty); +} + +} // namespace + +mlir::LogicalResult AArch64ABIRewriteContext::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(); + + switch (ac.kind) { + case ArgKind::Direct: + case ArgKind::Extend: + if (ac.directOffset) + return reportVAArgNYI(op, "a type read at an offset into its register"); + break; + case ArgKind::Indirect: + if (ac.byVal) + return reportVAArgNYI(op, "a type passed byval"); + break; + case ArgKind::IndirectAliased: + return reportVAArgNYI(op, "a type passed byref"); + case ArgKind::Expand: + return reportVAArgNYI(op, "a type expanded into its fields"); + case ArgKind::Ignore: + llvm_unreachable("rewriteVAArg replaces an ignored va_arg"); + } + + auto vaListRecTy = mlir::dyn_cast<cir::RecordType>( + mlir::cast<cir::PointerType>(valist.getType()).getPointee()); + if (!vaListRecTy || vaListRecTy.getNumElements() != 5) { + return reportVAArgNYI(op, "a va_list that is not the five-field __stack / " + "__gr_top / __vr_top / __gr_offs / __vr_offs " + "cursor"); + } + llvm::ArrayRef<mlir::Type> vaFields = vaListRecTy.getMembers(); + + cir::IntType byteTy = builder.getUIntNTy(8); + bool isIndirect = ac.kind == ArgKind::Indirect; + bool isBigEndian = cirDataLayout.isBigEndian(); + bool isAggregate = isAAPCS64Aggregate(resultTy); + uint64_t tySize = dl.getTypeAllocSize(resultTy).getFixedValue(); + uint64_t tyAlign = unadjustedArgAlign(resultTy, module, dl); + + // An indirect argument is passed as a pointer to a copy. Otherwise the + // coerced type says which registers carry the value, and an array coercion + // spreads it over one register per element. + mlir::Type baseTy = resultTy; + if (isIndirect) + baseTy = builder.getPointerTo(byteTy); + else if (ac.coercedType) + baseTy = ac.coercedType; + unsigned numRegs = 1; + if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(baseTy)) { + baseTy = arrTy.getElementType(); + numRegs = arrTy.getSize(); + } + bool isFPR = + !softFloat && mlir::isa<cir::FPTypeInterface, cir::VectorType>(baseTy); + bool isHFA = isFPR && isAggregate; + + // Each vector register save slot is 16 bytes, whatever part of it the value + // fills. General register slots are 8 bytes and packed. + unsigned offsIdx = isFPR ? 4 : 3; + unsigned topIdx = isFPR ? 2 : 1; + llvm::StringRef offsName = isFPR ? "__vr_offs" : "__gr_offs"; + llvm::StringRef topName = isFPR ? "__vr_top" : "__gr_top"; + uint64_t regSize = + isFPR ? 16 * numRegs : llvm::alignTo(isIndirect ? 8 : tySize, 8); + uint64_t regSlotSize = isFPR ? 16 : 8; + + builder.setInsertionPoint(op); + + // The offset is negative, counting up to zero, while registers remain. + // A value aligned to more than 8 bytes starts at an even general register. + mlir::Value offsP = builder.createGetMember( + loc, builder.getPointerTo(vaFields[offsIdx]), valist, offsName, offsIdx); + llvm::Align offsAlign = + llvm::commonAlignment(llvm::Align(dl.getTypeABIAlignment(vaListRecTy)), + vaListRecTy.getElementOffset(dl, offsIdx)); + mlir::Value offs = + builder.createLoad(loc, offsP, /*isVolatile=*/false, offsAlign.value()); + mlir::Type offsTy = offs.getType(); + mlir::Value zero = builder.getConstantInt(loc, offsTy, 0); + mlir::Value usingStack = + builder.createCompare(loc, cir::CmpOpKind::ge, offs, zero); + mlir::Value regOffs = offs; + if (!isFPR && !isIndirect && tyAlign > 8) { + regOffs = builder.createAdd( + loc, offs, builder.getConstantInt(loc, offsTy, tyAlign - 1)); + regOffs = builder.createAnd( + loc, regOffs, + builder.getConstantInt(loc, offsTy, -static_cast<int64_t>(tyAlign))); + } + mlir::Value newOffs = builder.createAdd( + loc, regOffs, builder.getConstantInt(loc, offsTy, regSize)); + + // Once the offset is non-negative it is left unchanged. Otherwise it is + // advanced past this value even when the value does not fit, since an + // argument passed on the stack also uses up the remaining registers. + mlir::Value inRegs = + cir::TernaryOp::create( + builder, loc, usingStack, + /*trueBuilder=*/ + [&](mlir::OpBuilder &ob, mlir::Location l) { + CIRBaseBuilderTy b(ob); + cir::YieldOp::create(b, l, mlir::Value(b.getBool(false, l))); + }, + /*falseBuilder=*/ + [&](mlir::OpBuilder &ob, mlir::Location l) { + CIRBaseBuilderTy b(ob); + b.createStore(l, newOffs, offsP, /*isVolatile=*/false, + /*isNontemporal=*/false, + b.getAlignmentAttr(offsAlign)); + mlir::Value fits = + b.createCompare(l, cir::CmpOpKind::le, newOffs, zero); + cir::YieldOp::create(b, l, fits); + }) + .getResult(); + + // The members of an HFA in registers are 16 bytes apart, so they are copied + // into a temporary where they are contiguous. + bool copyHFA = isHFA && numRegs > 1; + mlir::Value hfaTemp; + uint64_t regAlign = regSlotSize; + if (copyHFA) { + regAlign = std::max<uint64_t>(tyAlign, dl.getTypeABIAlignment(baseTy)); + hfaTemp = builder.createAlloca( + loc, builder.getPointerTo(cir::ArrayType::get(baseTy, numRegs)), + "vaarg.hfa", clang::CharUnits::fromQuantity(regAlign)); + } + + // On a big-endian target a value smaller than its slot is at the high end + // of the slot. In a register slot this applies to scalars and to the + // members of an HFA. In a stack slot it applies only to scalars. + uint64_t regBEOffset = 0; + if (isBigEndian && copyHFA) { + uint64_t baseSize = dl.getTypeAllocSize(baseTy).getFixedValue(); + if (baseSize < 16) + regBEOffset = 16 - baseSize; + } else if (isBigEndian && !isIndirect && (isHFA || !isAggregate) && + tySize < regSlotSize) { + regBEOffset = regSlotSize - tySize; + regAlign = + llvm::commonAlignment(llvm::Align(regAlign), regBEOffset).value(); + } + uint64_t stackSlotSize = 8; + uint64_t stackAlign = std::max<uint64_t>(stackSlotSize, tyAlign); + uint64_t stackBEOffset = 0; + if (isBigEndian && !isAggregate && tySize < stackSlotSize) { + stackBEOffset = stackSlotSize - tySize; + stackAlign = + llvm::commonAlignment(llvm::Align(stackAlign), stackBEOffset).value(); + } + + mlir::Value addr = + cir::TernaryOp::create( + builder, loc, inRegs, + /*trueBuilder=*/ + [&](mlir::OpBuilder &ob, mlir::Location l) { + CIRBaseBuilderTy b(ob); + mlir::Value regTop = b.createLoad( + l, b.createGetMember(l, b.getPointerTo(vaFields[topIdx]), + valist, topName, topIdx)); + mlir::Value regAddr = b.createPtrStride( + l, b.createPtrBitcast(regTop, byteTy), regOffs); + if (copyHFA) { + mlir::Value elems = + b.createCast(l, cir::CastKind::array_to_ptrdecay, hfaTemp, + b.getPointerTo(baseTy)); + uint64_t baseSize = dl.getTypeAllocSize(baseTy).getFixedValue(); + for (unsigned i = 0; i < numRegs; ++i) { + uint64_t srcOffset = 16 * i + regBEOffset; + mlir::Value src = b.createPtrStride( + l, regAddr, b.getSignedInt(l, srcOffset, 64)); + mlir::Value elem = b.createAlignedLoad( + l, b.createPtrBitcast(src, baseTy), + llvm::commonAlignment(llvm::Align(16), srcOffset).value()); + mlir::Value dst = + b.createPtrStride(l, elems, b.getSignedInt(l, i, 64)); + b.createStore(l, elem, dst, /*isVolatile=*/false, + /*isNontemporal=*/false, + b.getAlignmentAttr(llvm::commonAlignment( + llvm::Align(regAlign), i * baseSize))); + } + regAddr = b.createPtrBitcast(hfaTemp, byteTy); + } else if (regBEOffset) { + regAddr = b.createPtrStride(l, regAddr, + b.getSignedInt(l, regBEOffset, 64)); + } + cir::YieldOp::create(b, l, regAddr); + }, + /*falseBuilder=*/ + [&](mlir::OpBuilder &ob, mlir::Location l) { + CIRBaseBuilderTy b(ob); + mlir::Value stackP = b.createGetMember( + l, b.getPointerTo(vaFields[0]), valist, "__stack", 0); + mlir::Value stack = + b.createPtrBitcast(b.createLoad(l, stackP), byteTy); + if (!isIndirect && tyAlign > 8) + stack = roundPointerUpToAlignment(b, l, stack, tyAlign); + uint64_t stackSize = isIndirect + ? stackSlotSize + : llvm::alignTo(tySize, stackSlotSize); + b.createStore( + l, + b.createPtrStride(l, stack, b.getSignedInt(l, stackSize, 64)), + stackP); + if (stackBEOffset) + stack = b.createPtrStride(l, stack, + b.getSignedInt(l, stackBEOffset, 64)); + cir::YieldOp::create(b, l, stack); + }) + .getResult(); + + uint64_t addrAlign = std::min(regAlign, stackAlign); + mlir::Value resultAddr; + uint64_t resultAlign; + if (isIndirect) { + mlir::Value ptrAddr = + builder.createPtrBitcast(addr, builder.getPointerTo(resultTy)); + resultAddr = builder.createAlignedLoad(loc, ptrAddr, addrAlign); + resultAlign = tyAlign; + } else { + resultAddr = builder.createPtrBitcast(addr, resultTy); + resultAlign = addrAlign; + } + + if (mlir::isa<cir::RecordType>(resultTy)) { + SmallPtrSet<mlir::Operation *, 1> noExceptions; + if (failed(rewireRecordUses(builder, loc, op.getResult(), resultAddr, + resultAlign, noExceptions))) + return mlir::failure(); + } else { + op.getResult().replaceAllUsesWith( + builder.createAlignedLoad(loc, resultAddr, resultAlign)); + } + op->erase(); + return mlir::success(); +} + +std::unique_ptr<CIRABIRewriteContext> +cir::createAArch64ABIRewriteContext(mlir::ModuleOp module, + const mlir::DataLayout &dl, + bool enableNoundefAttrs, bool softFloat) { + return std::make_unique<AArch64ABIRewriteContext>( + module, dl, enableNoundefAttrs, softFloat); +} diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt index b8799b4454fa61..3173711da12cf9 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt @@ -1,5 +1,6 @@ add_clang_library(MLIRCIRTargetLowering CIRABIRewriteContext.cpp + CIRABIRewriteContextAArch64.cpp CIRCXXABI.cpp LowerModule.cpp LowerItaniumCXXABI.cpp diff --git a/clang/test/CIR/CodeGen/aarch64-be-var-arg.c b/clang/test/CIR/CodeGen/aarch64-be-var-arg.c new file mode 100644 index 00000000000000..a0086a6a78a897 --- /dev/null +++ b/clang/test/CIR/CodeGen/aarch64-be-var-arg.c @@ -0,0 +1,115 @@ +// RUN: %clang_cc1 -triple aarch64_be-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVMCIR +// RUN: %clang_cc1 -triple aarch64_be-unknown-linux-gnu -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG + +// On a big-endian target a value smaller than its slot is at the high end of +// the slot. + +// An int is 4 bytes into its 8-byte general register slot and its 8-byte +// stack slot. +int va_int(__builtin_va_list args) { + return __builtin_va_arg(args, int); +} + +// LLVM-LABEL: define {{.*}} i32 @va_int( +// LLVM: getelementptr inbounds nuw %struct.__va_list, ptr %{{.*}}, i32 0, i32 3 +// LLVM: %[[GR_TOP:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[SLOT:.*]] = getelementptr i8, ptr %[[GR_TOP]], i64 %{{.*}} +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[SLOT]], i64 4 +// OGCG: %[[SLOT:.*]] = getelementptr inbounds i8, ptr %[[GR_TOP]], i32 %{{.*}} +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[SLOT]], i64 4 +// LLVM: %[[STACK_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %{{.*}}, i32 0, i32 0 +// LLVM: %[[STACK:.*]] = load ptr, ptr %[[STACK_P]], align 8 +// LLVM: %[[NEW_STACK:.*]] = getelementptr {{.*}}i8, ptr %[[STACK]], i64 8 +// LLVM: store ptr %[[NEW_STACK]], ptr %[[STACK_P]], align 8 +// LLVMCIR: %[[STACK_ADDR:.*]] = getelementptr i8, ptr %[[STACK]], i64 4 +// OGCG: %[[STACK_ADDR:.*]] = getelementptr inbounds i8, ptr %[[STACK]], i64 4 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK_ADDR]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK_ADDR]], %{{.*}} ] +// LLVM: %{{.*}} = load i32, ptr %[[ADDR]], align 4 + +// An 8-byte vector is 8 bytes into its 16-byte vector register slot and fills +// its stack slot. +typedef float V2F __attribute__((vector_size(8))); + +V2F va_v2f(__builtin_va_list args) { + return __builtin_va_arg(args, V2F); +} + +// LLVM-LABEL: define {{.*}} <2 x float> @va_v2f( +// LLVM: getelementptr inbounds nuw %struct.__va_list, ptr %{{.*}}, i32 0, i32 4 +// LLVM: %[[VR_TOP:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[SLOT:.*]] = getelementptr i8, ptr %[[VR_TOP]], i64 %{{.*}} +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[SLOT]], i64 8 +// OGCG: %[[SLOT:.*]] = getelementptr inbounds i8, ptr %[[VR_TOP]], i32 %{{.*}} +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[SLOT]], i64 8 +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 8 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: %{{.*}} = load <2 x float>, ptr %[[ADDR]], align 8 + +// Each member of an HFA is at the high end of its 16-byte vector register +// slot. On the stack the HFA is laid out as in memory. +struct HFA2 { + float a, b; +}; + +struct HFA2 va_hfa2(__builtin_va_list args) { + return __builtin_va_arg(args, struct HFA2); +} + +// LLVM-LABEL: define {{.*}} %struct.HFA2 @va_hfa2( +// LLVM: %[[VR_TOP:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[SLOT:.*]] = getelementptr i8, ptr %[[VR_TOP]], i64 %{{.*}} +// OGCG: %[[SLOT:.*]] = getelementptr inbounds i8, ptr %[[VR_TOP]], i32 %{{.*}} +// LLVMCIR: %[[SRC0:.*]] = getelementptr i8, ptr %[[SLOT]], i64 12 +// OGCG: %[[SRC0:.*]] = getelementptr inbounds i8, ptr %[[SLOT]], i64 12 +// LLVM: load float, ptr %[[SRC0]], align 4 +// LLVMCIR: %[[SRC1:.*]] = getelementptr i8, ptr %[[SLOT]], i64 28 +// OGCG: %[[SRC1:.*]] = getelementptr inbounds i8, ptr %[[SLOT]], i64 28 +// LLVM: load float, ptr %[[SRC1]], align 4 +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 8 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %{{.*}}, %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %{{.*}}, %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %[[ADDR]], i64 8, i1 false) + +// A single-member HFA is read in place from the high end of its register slot. +struct HFA1 { + float a; +}; + +struct HFA1 va_hfa1(__builtin_va_list args) { + return __builtin_va_arg(args, struct HFA1); +} + +// LLVM-LABEL: define {{.*}} %struct.HFA1 @va_hfa1( +// LLVM: %[[VR_TOP:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[SLOT:.*]] = getelementptr i8, ptr %[[VR_TOP]], i64 %{{.*}} +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[SLOT]], i64 12 +// OGCG: %[[SLOT:.*]] = getelementptr inbounds i8, ptr %[[VR_TOP]], i32 %{{.*}} +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[SLOT]], i64 12 +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %[[ADDR]], i64 4, i1 false) + +// A small record that is not an HFA is read from the start of its slot. +struct Small { + char c; +}; + +struct Small va_small(__builtin_va_list args) { + return __builtin_va_arg(args, struct Small); +} + +// LLVM-LABEL: define {{.*}} i64 @va_small( +// LLVM: %[[GR_TOP:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[GR_TOP]], i64 %{{.*}} +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[GR_TOP]], i32 %{{.*}} +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %{{.*}}, ptr align 8 %[[ADDR]], i64 1, i1 false) diff --git a/clang/test/CIR/CodeGen/aarch64-soft-float-var-arg.c b/clang/test/CIR/CodeGen/aarch64-soft-float-var-arg.c new file mode 100644 index 00000000000000..1df239e3ea6d19 --- /dev/null +++ b/clang/test/CIR/CodeGen/aarch64-soft-float-var-arg.c @@ -0,0 +1,49 @@ +// RUN: %clang_cc1 -triple aarch64-none-elf -target-abi aapcs-soft -target-feature -fp-armv8 -target-feature -neon -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVMCIR +// RUN: %clang_cc1 -triple aarch64-none-elf -target-abi aapcs-soft -target-feature -fp-armv8 -target-feature -neon -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG + +// Under the soft-float ABI there are no vector registers, so floating-point +// values are read through __gr_top and __gr_offs. +double va_double(__builtin_va_list args) { + return __builtin_va_arg(args, double); +} + +// LLVM-LABEL: define {{.*}} double @va_double( +// LLVM: %[[GR_OFFS_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS:.*]], i32 0, i32 3 +// LLVM: %[[GR_OFFS:.*]] = load i32, ptr %[[GR_OFFS_P]], align 8 +// LLVM: %[[NEW_OFFS:.*]] = add i32 %[[GR_OFFS]], 8 +// LLVM: store i32 %[[NEW_OFFS]], ptr %[[GR_OFFS_P]], align 8 +// LLVM: %[[GR_TOP_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS]], i32 0, i32 1 +// LLVM: %[[GR_TOP:.*]] = load ptr, ptr %[[GR_TOP_P]], align 8 +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[GR_TOP]], i64 %{{.*}} +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[GR_TOP]], i32 %[[GR_OFFS]] +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 8 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: %{{.*}} = load double, ptr %[[ADDR]], align 8 + +// A record of two doubles is not an HFA here. It is contiguous in two general +// registers. +struct TwoDoubles { + double a, b; +}; + +struct TwoDoubles va_two_doubles(__builtin_va_list args) { + return __builtin_va_arg(args, struct TwoDoubles); +} + +// LLVM-LABEL: define {{.*}} [2 x i64] @va_two_doubles( +// LLVM: %[[GR_OFFS_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS:.*]], i32 0, i32 3 +// LLVM: %[[GR_OFFS:.*]] = load i32, ptr %[[GR_OFFS_P]], align 8 +// LLVM: %[[NEW_OFFS:.*]] = add i32 %[[GR_OFFS]], 16 +// LLVM: store i32 %[[NEW_OFFS]], ptr %[[GR_OFFS_P]], align 8 +// LLVM: %[[GR_TOP:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[GR_TOP]], i64 %{{.*}} +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[GR_TOP]], i32 %[[GR_OFFS]] +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 16 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %{{.*}}, ptr align 8 %[[ADDR]], i64 16, i1 false) diff --git a/clang/test/CIR/CodeGen/aarch64-var-arg.c b/clang/test/CIR/CodeGen/aarch64-var-arg.c new file mode 100644 index 00000000000000..0a57b0fd0eddc2 --- /dev/null +++ b/clang/test/CIR/CodeGen/aarch64-var-arg.c @@ -0,0 +1,269 @@ +// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVMCIR +// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG + +// An int is read from the general register save area through __gr_top and +// __gr_offs, or from the stack through __stack. +int va_int(int count, ...) { + __builtin_va_list args; + __builtin_va_start(args, count); + int res = __builtin_va_arg(args, int); + __builtin_va_end(args); + return res; +} + +// CIR-LABEL: cir.func {{.*}} @va_int( +// CIR: %[[ARGS:.*]] = cir.alloca "args" align(8) : !cir.ptr<!rec___va_list> +// CIR: cir.va_start %[[ARGS]] +// CIR: %[[GR_OFFS_P:.*]] = cir.get_member %[[ARGS]][3] {name = "__gr_offs"} : !cir.ptr<!rec___va_list> -> !cir.ptr<!s32i> +// CIR: %[[GR_OFFS:.*]] = cir.load align(8) %[[GR_OFFS_P]] : !cir.ptr<!s32i>, !s32i +// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i +// CIR: %[[ON_STACK:.*]] = cir.cmp ge %[[GR_OFFS]], %[[ZERO]] : !s32i +// CIR: %[[REG_SIZE:.*]] = cir.const #cir.int<8> : !s32i +// CIR: %[[NEW_OFFS:.*]] = cir.add %[[GR_OFFS]], %[[REG_SIZE]] : !s32i +// CIR: %[[IN_REGS:.*]] = cir.ternary(%[[ON_STACK]], true { +// CIR: %[[FALSE:.*]] = cir.const #false +// CIR: cir.yield %[[FALSE]] : !cir.bool +// CIR: }, false { +// CIR: cir.store align(8) %[[NEW_OFFS]], %[[GR_OFFS_P]] : !s32i, !cir.ptr<!s32i> +// CIR: %[[FITS:.*]] = cir.cmp le %[[NEW_OFFS]], %[[ZERO]] : !s32i +// CIR: cir.yield %[[FITS]] : !cir.bool +// CIR: }) : (!cir.bool) -> !cir.bool +// CIR: %[[ADDR:.*]] = cir.ternary(%[[IN_REGS]], true { +// CIR: %[[GR_TOP_P:.*]] = cir.get_member %[[ARGS]][1] {name = "__gr_top"} : !cir.ptr<!rec___va_list> -> !cir.ptr<!cir.ptr<!void>> +// CIR: %[[GR_TOP:.*]] = cir.load %[[GR_TOP_P]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void> +// CIR: %[[GR_TOP_B:.*]] = cir.cast bitcast %[[GR_TOP]] : !cir.ptr<!void> -> !cir.ptr<!u8i> +// CIR: %[[REG_ADDR:.*]] = cir.ptr_stride %[[GR_TOP_B]], %[[GR_OFFS]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> +// CIR: cir.yield %[[REG_ADDR]] : !cir.ptr<!u8i> +// CIR: }, false { +// CIR: %[[STACK_P:.*]] = cir.get_member %[[ARGS]][0] {name = "__stack"} : !cir.ptr<!rec___va_list> -> !cir.ptr<!cir.ptr<!void>> +// CIR: %[[STACK:.*]] = cir.load %[[STACK_P]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void> +// CIR: %[[STACK_B:.*]] = cir.cast bitcast %[[STACK]] : !cir.ptr<!void> -> !cir.ptr<!u8i> +// CIR: %[[STACK_SIZE:.*]] = cir.const #cir.int<8> : !s64i +// CIR: %[[NEW_STACK:.*]] = cir.ptr_stride %[[STACK_B]], %[[STACK_SIZE]] : (!cir.ptr<!u8i>, !s64i) -> !cir.ptr<!u8i> +// CIR: %[[STACK_P_B:.*]] = cir.cast bitcast %[[STACK_P]] : !cir.ptr<!cir.ptr<!void>> -> !cir.ptr<!cir.ptr<!u8i>> +// CIR: cir.store %[[NEW_STACK]], %[[STACK_P_B]] : !cir.ptr<!u8i>, !cir.ptr<!cir.ptr<!u8i>> +// CIR: cir.yield %[[STACK_B]] : !cir.ptr<!u8i> +// CIR: }) : (!cir.bool) -> !cir.ptr<!u8i> +// CIR: %[[ADDR_I:.*]] = cir.cast bitcast %[[ADDR]] : !cir.ptr<!u8i> -> !cir.ptr<!s32i> +// CIR: %{{.*}} = cir.load align(8) %[[ADDR_I]] : !cir.ptr<!s32i>, !s32i + +// LLVM-LABEL: define {{.*}} i32 @va_int( +// LLVM: %[[GR_OFFS_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS:.*]], i32 0, i32 3 +// LLVM: %[[GR_OFFS:.*]] = load i32, ptr %[[GR_OFFS_P]], align 8 +// LLVM: %{{.*}} = icmp sge i32 %[[GR_OFFS]], 0 +// LLVM: %[[NEW_OFFS:.*]] = add i32 %[[GR_OFFS]], 8 +// LLVM: store i32 %[[NEW_OFFS]], ptr %[[GR_OFFS_P]], align 8 +// LLVM: %{{.*}} = icmp sle i32 %[[NEW_OFFS]], 0 +// LLVM: %[[GR_TOP_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS]], i32 0, i32 1 +// LLVM: %[[GR_TOP:.*]] = load ptr, ptr %[[GR_TOP_P]], align 8 +// LLVMCIR: %[[GR_OFFS_EXT:.*]] = sext i32 %[[GR_OFFS]] to i64 +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %[[GR_TOP]], i64 %[[GR_OFFS_EXT]] +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %[[GR_TOP]], i32 %[[GR_OFFS]] +// LLVM: %[[STACK_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS]], i32 0, i32 0 +// LLVM: %[[STACK:.*]] = load ptr, ptr %[[STACK_P]], align 8 +// LLVMCIR: %[[NEW_STACK:.*]] = getelementptr i8, ptr %[[STACK]], i64 8 +// OGCG: %[[NEW_STACK:.*]] = getelementptr inbounds i8, ptr %[[STACK]], i64 8 +// LLVM: store ptr %[[NEW_STACK]], ptr %[[STACK_P]], align 8 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: %{{.*}} = load i32, ptr %[[ADDR]], align 8 + +// A double is read from the vector register save area through __vr_top and +// __vr_offs, which advances by a 16-byte register. A stack slot is 8 bytes. +double va_double(int count, ...) { + __builtin_va_list args; + __builtin_va_start(args, count); + double res = __builtin_va_arg(args, double); + __builtin_va_end(args); + return res; +} + +// CIR-LABEL: cir.func {{.*}} @va_double( +// CIR: %[[VR_OFFS_P:.*]] = cir.get_member %{{.*}}[4] {name = "__vr_offs"} : !cir.ptr<!rec___va_list> -> !cir.ptr<!s32i> +// CIR: %[[VR_OFFS:.*]] = cir.load align(4) %[[VR_OFFS_P]] : !cir.ptr<!s32i>, !s32i +// CIR: %[[REG_SIZE:.*]] = cir.const #cir.int<16> : !s32i +// CIR: %{{.*}} = cir.add %[[VR_OFFS]], %[[REG_SIZE]] : !s32i +// CIR: cir.get_member %{{.*}}[2] {name = "__vr_top"} +// CIR: cir.get_member %{{.*}}[0] {name = "__stack"} +// CIR: %{{.*}} = cir.const #cir.int<8> : !s64i +// CIR: cir.load align(8) %{{.*}} : !cir.ptr<!cir.double>, !cir.double + +// LLVM-LABEL: define {{.*}} double @va_double( +// LLVM: %[[VR_OFFS_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS:.*]], i32 0, i32 4 +// LLVM: %[[VR_OFFS:.*]] = load i32, ptr %[[VR_OFFS_P]], align 4 +// LLVM: %[[NEW_OFFS:.*]] = add i32 %[[VR_OFFS]], 16 +// LLVM: store i32 %[[NEW_OFFS]], ptr %[[VR_OFFS_P]], align 4 +// LLVM: getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS]], i32 0, i32 2 +// LLVM: %[[STACK_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS]], i32 0, i32 0 +// LLVM: %[[STACK:.*]] = load ptr, ptr %[[STACK_P]], align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 8 +// LLVM: %[[ADDR:.*]] = phi ptr +// LLVM: %{{.*}} = load double, ptr %[[ADDR]], align 8 + +// The members of an HFA are 16 bytes apart in the vector register save area, +// so they are copied into a temporary where they are contiguous. +struct HFA { + float a, b, c; +}; + +struct HFA va_hfa(int count, ...) { + __builtin_va_list args; + __builtin_va_start(args, count); + struct HFA res = __builtin_va_arg(args, struct HFA); + __builtin_va_end(args); + return res; +} + +// CIR-LABEL: cir.func {{.*}} @va_hfa( +// CIR: %[[VR_OFFS_P:.*]] = cir.get_member %{{.*}}[4] {name = "__vr_offs"} +// CIR: %[[VR_OFFS:.*]] = cir.load align(4) %[[VR_OFFS_P]] : !cir.ptr<!s32i>, !s32i +// CIR: %[[REG_SIZE:.*]] = cir.const #cir.int<48> : !s32i +// CIR: %{{.*}} = cir.add %[[VR_OFFS]], %[[REG_SIZE]] : !s32i +// CIR: %[[HFA_TMP:.*]] = cir.alloca "vaarg.hfa" align(4) : !cir.ptr<!cir.array<!cir.float x 3>> +// CIR: %[[ADDR:.*]] = cir.ternary(%{{.*}}, true { +// CIR: %[[VR_TOP_P:.*]] = cir.get_member %{{.*}}[2] {name = "__vr_top"} +// CIR: %[[VR_TOP:.*]] = cir.load %[[VR_TOP_P]] +// CIR: %[[VR_TOP_B:.*]] = cir.cast bitcast %[[VR_TOP]] : !cir.ptr<!void> -> !cir.ptr<!u8i> +// CIR: %[[REG_ADDR:.*]] = cir.ptr_stride %[[VR_TOP_B]], %[[VR_OFFS]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> +// CIR: %[[ELEMS:.*]] = cir.cast array_to_ptrdecay %[[HFA_TMP]] : !cir.ptr<!cir.array<!cir.float x 3>> -> !cir.ptr<!cir.float> +// CIR: %[[OFF0:.*]] = cir.const #cir.int<0> : !s64i +// CIR: %[[SRC0:.*]] = cir.ptr_stride %[[REG_ADDR]], %[[OFF0]] +// CIR: %[[SRC0_F:.*]] = cir.cast bitcast %[[SRC0]] : !cir.ptr<!u8i> -> !cir.ptr<!cir.float> +// CIR: %[[ELEM0:.*]] = cir.load align(16) %[[SRC0_F]] : !cir.ptr<!cir.float>, !cir.float +// CIR: %[[IDX0:.*]] = cir.const #cir.int<0> : !s64i +// CIR: %[[DST0:.*]] = cir.ptr_stride %[[ELEMS]], %[[IDX0]] +// CIR: cir.store align(4) %[[ELEM0]], %[[DST0]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[OFF1:.*]] = cir.const #cir.int<16> : !s64i +// CIR: %[[SRC1:.*]] = cir.ptr_stride %[[REG_ADDR]], %[[OFF1]] +// CIR: %[[SRC1_F:.*]] = cir.cast bitcast %[[SRC1]] : !cir.ptr<!u8i> -> !cir.ptr<!cir.float> +// CIR: %[[ELEM1:.*]] = cir.load align(16) %[[SRC1_F]] : !cir.ptr<!cir.float>, !cir.float +// CIR: %[[IDX1:.*]] = cir.const #cir.int<1> : !s64i +// CIR: %[[DST1:.*]] = cir.ptr_stride %[[ELEMS]], %[[IDX1]] +// CIR: cir.store align(4) %[[ELEM1]], %[[DST1]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[OFF2:.*]] = cir.const #cir.int<32> : !s64i +// CIR: %[[SRC2:.*]] = cir.ptr_stride %[[REG_ADDR]], %[[OFF2]] +// CIR: %[[SRC2_F:.*]] = cir.cast bitcast %[[SRC2]] : !cir.ptr<!u8i> -> !cir.ptr<!cir.float> +// CIR: %[[ELEM2:.*]] = cir.load align(16) %[[SRC2_F]] : !cir.ptr<!cir.float>, !cir.float +// CIR: %[[IDX2:.*]] = cir.const #cir.int<2> : !s64i +// CIR: %[[DST2:.*]] = cir.ptr_stride %[[ELEMS]], %[[IDX2]] +// CIR: cir.store align(4) %[[ELEM2]], %[[DST2]] : !cir.float, !cir.ptr<!cir.float> +// CIR: %[[TMP_B:.*]] = cir.cast bitcast %[[HFA_TMP]] : !cir.ptr<!cir.array<!cir.float x 3>> -> !cir.ptr<!u8i> +// CIR: cir.yield %[[TMP_B]] : !cir.ptr<!u8i> +// CIR: }, false { +// CIR: cir.get_member %{{.*}}[0] {name = "__stack"} +// CIR: %{{.*}} = cir.const #cir.int<16> : !s64i +// CIR: }) : (!cir.bool) -> !cir.ptr<!u8i> +// CIR: %[[ADDR_R:.*]] = cir.cast bitcast %[[ADDR]] : !cir.ptr<!u8i> -> !cir.ptr<!rec_HFA> +// CIR: cir.copy %[[ADDR_R]] align(4) to %{{.*}} align(4) : !cir.ptr<!rec_HFA> + +// LLVM-LABEL: define {{.*}} %struct.HFA @va_hfa( +// LLVMCIR: %[[HFA_TMP:.*]] = alloca [3 x float], align 4 +// OGCG: %[[HFA_TMP:.*]] = alloca [3 x float], align 4 +// LLVM: %[[VR_OFFS_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS:.*]], i32 0, i32 4 +// LLVM: %[[VR_OFFS:.*]] = load i32, ptr %[[VR_OFFS_P]], align 4 +// LLVM: %[[NEW_OFFS:.*]] = add i32 %[[VR_OFFS]], 48 +// LLVM: store i32 %[[NEW_OFFS]], ptr %[[VR_OFFS_P]], align 4 +// LLVM: getelementptr inbounds nuw %struct.__va_list, ptr %[[ARGS]], i32 0, i32 2 +// LLVMCIR: getelementptr i8, ptr %[[REG_ADDR:[0-9]+]], i64 0 +// OGCG: getelementptr inbounds i8, ptr %[[REG_ADDR:[0-9]+]], i64 0 +// LLVM: %[[ELEM0:.*]] = load float, ptr %{{.*}}, align 16 +// LLVM: store float %[[ELEM0]], ptr %{{.*}}, align 4 +// LLVMCIR: getelementptr i8, ptr %[[REG_ADDR]], i64 16 +// OGCG: getelementptr inbounds i8, ptr %[[REG_ADDR]], i64 16 +// LLVM: %[[ELEM1:.*]] = load float, ptr %{{.*}}, align 16 +// LLVM: store float %[[ELEM1]], ptr %{{.*}}, align 4 +// LLVMCIR: getelementptr i8, ptr %[[REG_ADDR]], i64 32 +// OGCG: getelementptr inbounds i8, ptr %[[REG_ADDR]], i64 32 +// LLVM: %[[ELEM2:.*]] = load float, ptr %{{.*}}, align 16 +// LLVM: store float %[[ELEM2]], ptr %{{.*}}, align 4 +// LLVM: %[[STACK:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 16 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK]], %{{.*}} ], [ %[[HFA_TMP]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[HFA_TMP]], %{{.*}} ], [ %[[STACK]], %{{.*}} ] +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %[[ADDR]], i64 12, i1 false) + +// A 16-byte-aligned value starts at an even general register, so __gr_offs is +// rounded up before it is used. On the stack, __stack is rounded up. +struct Align16 { + __int128 a; +}; + +struct Align16 va_align16(int count, ...) { + __builtin_va_list args; + __builtin_va_start(args, count); + struct Align16 res = __builtin_va_arg(args, struct Align16); + __builtin_va_end(args); + return res; +} + +// CIR-LABEL: cir.func {{.*}} @va_align16( +// CIR: %[[GR_OFFS:.*]] = cir.load align(8) %{{.*}} : !cir.ptr<!s32i>, !s32i +// CIR: %[[BUMP:.*]] = cir.const #cir.int<15> : !s32i +// CIR: %[[BUMPED:.*]] = cir.add %[[GR_OFFS]], %[[BUMP]] : !s32i +// CIR: %[[MASK:.*]] = cir.const #cir.int<-16> : !s32i +// CIR: %[[ALIGNED:.*]] = cir.and %[[BUMPED]], %[[MASK]] : !s32i +// CIR: %[[REG_SIZE:.*]] = cir.const #cir.int<16> : !s32i +// CIR: %{{.*}} = cir.add %[[ALIGNED]], %[[REG_SIZE]] : !s32i +// CIR: cir.ternary +// CIR: cir.get_member %{{.*}}[1] {name = "__gr_top"} +// CIR: cir.ptr_stride %{{.*}}, %[[ALIGNED]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> +// CIR: cir.get_member %{{.*}}[0] {name = "__stack"} +// CIR: %[[STACK_MASK:.*]] = cir.const #cir.int<-16> : !s64i +// CIR: %[[STACK_ALIGNED:.*]] = cir.ptr_mask %{{.*}}, %[[STACK_MASK]] : (!cir.ptr<!u8i>, !s64i) -> !cir.ptr<!u8i> +// CIR: %[[STACK_SIZE:.*]] = cir.const #cir.int<16> : !s64i +// CIR: cir.ptr_stride %[[STACK_ALIGNED]], %[[STACK_SIZE]] +// CIR: cir.yield %[[STACK_ALIGNED]] : !cir.ptr<!u8i> +// CIR: cir.copy %{{.*}} align(8) to %{{.*}} align(16) : !cir.ptr<!rec_Align16> + +// LLVM-LABEL: define {{.*}} i128 @va_align16( +// LLVM: %[[GR_OFFS:.*]] = load i32, ptr %{{.*}}, align 8 +// LLVM: %[[BUMPED:.*]] = add i32 %[[GR_OFFS]], 15 +// LLVM: %[[ALIGNED:.*]] = and i32 %[[BUMPED]], -16 +// LLVM: %[[NEW_OFFS:.*]] = add i32 %[[ALIGNED]], 16 +// LLVM: store i32 %[[NEW_OFFS]], ptr %{{.*}}, align 8 +// LLVMCIR: %[[ALIGNED_EXT:.*]] = sext i32 %[[ALIGNED]] to i64 +// LLVMCIR: %[[REG_ADDR:.*]] = getelementptr i8, ptr %{{.*}}, i64 %[[ALIGNED_EXT]] +// OGCG: %[[REG_ADDR:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 %[[ALIGNED]] +// LLVM: %[[STACK_ALIGNED:.*]] = call ptr @llvm.ptrmask.p0.i64(ptr %{{.*}}, i64 -16) +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK_ALIGNED]], i64 16 +// LLVMCIR: %[[ADDR:.*]] = phi ptr [ %[[STACK_ALIGNED]], %{{.*}} ], [ %[[REG_ADDR]], %{{.*}} ] +// OGCG: %[[ADDR:.*]] = phi ptr [ %[[REG_ADDR]], %{{.*}} ], [ %[[STACK_ALIGNED]], %{{.*}} ] +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 16 %{{.*}}, ptr align 8 %[[ADDR]], i64 16, i1 false) + +// A record larger than 16 bytes is passed as a pointer to a copy, so the slot +// holds a pointer to the value. +struct Big { + long a[5]; +}; + +struct Big va_big(int count, ...) { + __builtin_va_list args; + __builtin_va_start(args, count); + struct Big res = __builtin_va_arg(args, struct Big); + __builtin_va_end(args); + return res; +} + +// CIR-LABEL: cir.func {{.*}} @va_big( +// CIR: cir.get_member %{{.*}}[3] {name = "__gr_offs"} +// CIR: %{{.*}} = cir.const #cir.int<8> : !s32i +// CIR: cir.get_member %{{.*}}[0] {name = "__stack"} +// CIR: %{{.*}} = cir.const #cir.int<8> : !s64i +// CIR: }) : (!cir.bool) -> !cir.ptr<!u8i> +// CIR: %[[ADDR_P:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!u8i> -> !cir.ptr<!cir.ptr<!rec_Big>> +// CIR: %[[VAL_P:.*]] = cir.load align(8) %[[ADDR_P]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big> +// CIR: cir.copy %[[VAL_P]] align(8) to %{{.*}} align(8) : !cir.ptr<!rec_Big> + +// LLVM-LABEL: define {{.*}} void @va_big( +// LLVM: %[[GR_OFFS:.*]] = load i32, ptr %{{.*}}, align 8 +// LLVM: add i32 %[[GR_OFFS]], 8 +// LLVM: %[[STACK_P:.*]] = getelementptr inbounds nuw %struct.__va_list, ptr %{{.*}}, i32 0, i32 0 +// LLVM: %[[STACK:.*]] = load ptr, ptr %[[STACK_P]], align 8 +// LLVM: getelementptr {{.*}}i8, ptr %[[STACK]], i64 8 +// LLVM: %[[ADDR:.*]] = phi ptr +// LLVM: %[[VAL_P:.*]] = load ptr, ptr %[[ADDR]], align 8 +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %{{.*}}, ptr align 8 %[[VAL_P]], i64 40, i1 false) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
