Author: Chaitanya Date: 2026-11-21T08:47:14+05:30 New Revision: 4b12a1f34e998766ef97789082d222a558afc08b
URL: https://github.com/llvm/llvm-project/commit/4b12a1f34e998766ef97789082d222a558afc08b DIFF: https://github.com/llvm/llvm-project/commit/4b12a1f34e998766ef97789082d222a558afc08b.diff LOG: [CIR] Lower va_arg for AMDGPU in CallConvLowering (#244116) Summary: AMDGPU classifier rejected every `cir.va_arg` as NYI. This PR expands `cir.va_arg` the same way classic `AMDGPUABIInfo::EmitVAArg` does. The `va_list` is a single pointer into one buffer of 4-byte slots, so each fetch: - loads the cursor, - moves it by the type's alloc size rounded up to 4, - reads the value at the old cursor with align 4. Relates to : https://github.com/llvm/llvm-project/issues/220471 Assisted by: claude opus 5.5 Added: clang/test/CIR/Transforms/abi-lowering/amdgpu-vaarg.cir clang/test/CodeGen/AMDGPU/abi-va-arg.c Modified: clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h Removed: ################################################################################ diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp index c63191bd1eaac..6d4b3a56f594a 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp @@ -26,11 +26,10 @@ class AMDGPUABIClassifier final : public CIRABIClassifierCommon { AMDGPUABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp) : CIRABIClassifierCommon(dl, modOp) {} - mlir::FailureOr<ArgClassification> - classifyVAArg(cir::VAArgOp vaArg) override { - vaArg->emitOpError() - << "not yet implemented in CallConvLowering for AMDGPU"; - return mlir::failure(); + /// Each variadic argument is read in place from its own 4-byte slot. + mlir::FailureOr<ArgClassification> classifyVAArg(cir::VAArgOp) override { + return ArgClassification::getDirect(/*coerced=*/nullptr, /*offset=*/0, + llvm::Align(4)); } protected: diff --git a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp index 0a0b6e6ddad32..2ccf798e5fa3f 100644 --- a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp @@ -220,7 +220,7 @@ void CallConvLoweringPass::runOnOperation() { } DataLayout dl(moduleOp); - CIRABIRewriteContext rewriteCtx(moduleOp, dl, enableNoundefAttrs); + CIRABIRewriteContext rewriteCtx(moduleOp, dl, target, 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 diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp index b41e5da0d3da2..a62bc9d3d88c6 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp @@ -2801,6 +2801,43 @@ void advanceRegisterCursors(CIRBaseBuilderTy &b, mlir::Location loc, } } +/// Advances the pointer cursor at \p valist past the argument's slots and +/// returns the argument's address, which is the old cursor. +mlir::Value buildSlotAddrAndAdvance(CIRBaseBuilderTy &b, mlir::Location loc, + mlir::Value valist, mlir::Type resultTy, + uint64_t slotSize, + const mlir::DataLayout &dl) { + mlir::Value cur = b.createLoad(loc, valist); + mlir::Value bytePtr = b.createPtrBitcast(cur, b.getUIntNTy(8)); + uint64_t stride = + llvm::alignTo(dl.getTypeAllocSize(resultTy).getFixedValue(), slotSize); + mlir::Value next = + b.createPtrStride(loc, bytePtr, b.getSignedInt(loc, stride, 32)); + b.createStore(loc, b.createBitcast(next, cur.getType()), valist); + return bytePtr; +} + +/// Replaces \p op's result with the value at \p addr, which is \p align +/// aligned, and erases \p op. +mlir::LogicalResult replaceVAArgResult(CIRBaseBuilderTy &b, cir::VAArgOp op, + mlir::Value addr, uint64_t align, + const mlir::DataLayout &dl) { + mlir::Location loc = op.getLoc(); + mlir::Type resultTy = op.getType(); + mlir::Value resultAddr = b.createPtrBitcast(addr, resultTy); + if (mlir::isa<cir::RecordType>(resultTy)) { + SmallPtrSet<mlir::Operation *, 1> noExceptions; + if (failed(rewireRecordUses(b, loc, op.getResult(), resultAddr, align, dl, + noExceptions))) + return mlir::failure(); + } else { + op.getResult().replaceAllUsesWith( + b.createAlignedLoad(loc, resultAddr, align)); + } + op->erase(); + return mlir::success(); +} + } // namespace mlir::LogicalResult @@ -2809,9 +2846,6 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, mlir::OpBuilder &opBuilder) { auto op = mlir::cast<cir::VAArgOp>(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 @@ -2819,11 +2853,42 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, if (ac.kind == ArgKind::Ignore) { builder.setInsertionPoint(op); op.getResult().replaceAllUsesWith( - createIgnoredValue(builder, loc, resultTy)); + createIgnoredValue(builder, op.getLoc(), op.getType())); op->erase(); return mlir::success(); } + switch (target) { + case cir::CallConvTarget::X86_64: + return rewriteVAArgX86_64(op, ac, builder); + case cir::CallConvTarget::AMDGPU: + return rewriteVAArgAMDGPU(op, ac, builder); + case cir::CallConvTarget::None: + case cir::CallConvTarget::Test: + case cir::CallConvTarget::SPIRV: + case cir::CallConvTarget::AArch64: + break; + } + return reportVAArgNYI(op, "a type on this target"); +} + +mlir::LogicalResult CIRABIRewriteContext::rewriteVAArgAMDGPU( + cir::VAArgOp op, const ArgClassification &ac, CIRBaseBuilderTy &builder) { + assert(ac.kind == ArgKind::Direct && !ac.coercedType && ac.directAlign && + "AMDGPU reads every variadic argument in place from its slots"); + builder.setInsertionPoint(op); + uint64_t slotSize = ac.directAlign->value(); + mlir::Value addr = buildSlotAddrAndAdvance( + builder, op.getLoc(), op.getArgList(), op.getType(), slotSize, dl); + return replaceVAArgResult(builder, op, addr, slotSize, dl); +} + +mlir::LogicalResult CIRABIRewriteContext::rewriteVAArgX86_64( + cir::VAArgOp op, const ArgClassification &ac, CIRBaseBuilderTy &builder) { + 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"); if (ac.kind == ArgKind::IndirectAliased) @@ -2935,17 +3000,6 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation *vaArgOp, } // Every path above places the result at least this well aligned. - mlir::Value resultAddr = builder.createPtrBitcast(addr, resultTy); - uint64_t resultAlign = argumentAreaAlign(resultTy, module); - if (mlir::isa<cir::RecordType>(resultTy)) { - SmallPtrSet<mlir::Operation *, 1> noExceptions; - if (failed(rewireRecordUses(builder, loc, op.getResult(), resultAddr, - resultAlign, dl, noExceptions))) - return mlir::failure(); - } else { - op.getResult().replaceAllUsesWith( - builder.createAlignedLoad(loc, resultAddr, resultAlign)); - } - op->erase(); - return mlir::success(); + return replaceVAArgResult(builder, op, addr, + argumentAreaAlign(resultTy, module), dl); } diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h index 671be6c185a83..3d85c5d66ed31 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h @@ -27,12 +27,15 @@ #include "clang/CIR/Dialect/Analysis/CIRAliasAnalysis.h" #include "clang/CIR/Dialect/IR/CIRDataLayout.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "clang/CIR/Dialect/Passes.h" #include "llvm/ADT/SmallVector.h" #include <cassert> #include <utility> namespace cir { +class CIRBaseBuilderTy; + /// CIR-specific implementation of mlir::abi::ABIRewriteContext. /// /// The driver pass (CallConvLoweringPass) computes a FunctionClassification @@ -44,9 +47,10 @@ namespace cir { 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) { + cir::CallConvTarget target, bool enableNoundefAttrs) + : module(module), dl(dl), target(target), + enableNoundefAttrs(enableNoundefAttrs), cirDataLayout(module), + aliasAnalysis(module) { cir::registerCIRAliasAnalyses(aliasAnalysis, module); } @@ -65,8 +69,8 @@ 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. + /// Expand a `cir.va_arg` with the va_arg rules of the target this context + /// lowers for. mlir::LogicalResult rewriteVAArg(mlir::Operation *vaArgOp, const mlir::abi::ArgClassification &ac, mlir::OpBuilder &builder) override; @@ -116,9 +120,24 @@ class CIRABIRewriteContext : public mlir::abi::ABIRewriteContext { mlir::StringRef getDialectNamespace() const override { return "cir"; } private: + /// Expand \p op into the x86-64 SysV register-save-area / overflow-area + /// sequence. + mlir::LogicalResult rewriteVAArgX86_64(cir::VAArgOp op, + const mlir::abi::ArgClassification &ac, + CIRBaseBuilderTy &builder); + + /// Expand \p op into a read from the 4-byte slots its pointer va_list walks. + mlir::LogicalResult rewriteVAArgAMDGPU(cir::VAArgOp op, + const mlir::abi::ArgClassification &ac, + CIRBaseBuilderTy &builder); + mlir::ModuleOp module; const mlir::DataLayout &dl; + /// The target whose rules drive the target-specific rewrites, such as + /// va_arg expansion. + cir::CallConvTarget target; + /// Whether extended and indirect arguments are marked llvm.noundef. Set /// from the enable_noundef_attrs field of the module's /// #cir.lowering_options. diff --git a/clang/test/CIR/Transforms/abi-lowering/amdgpu-vaarg.cir b/clang/test/CIR/Transforms/abi-lowering/amdgpu-vaarg.cir new file mode 100644 index 0000000000000..c3d5e6fa9415f --- /dev/null +++ b/clang/test/CIR/Transforms/abi-lowering/amdgpu-vaarg.cir @@ -0,0 +1,130 @@ +// RUN: cir-opt %s -cir-call-conv-lowering=target=amdgpu | FileCheck %s + +// AMDGPU reads each va_arg in place from 4-byte slots of one buffer. The +// cursor moves by the alloc size rounded up to 4 and is never realigned. + +!s8i = !cir.int<s, 8> +!s32i = !cir.int<s, 32> +!s64i = !cir.int<s, 64> +!rec_Char = !cir.struct<"Char" {data !s8i}> +!rec_Triple = !cir.struct<"Triple" {data !s32i, data !s32i, data !s32i}> +!rec_Big = !cir.struct<"Big" {data !cir.array<!s32i x 20>}> + +module attributes { + dlti.dl_spec = #dlti.dl_spec< + #dlti.dl_entry<i32, dense<32>: vector<2xi64>>, + #dlti.dl_entry<i64, dense<64>: vector<2xi64>>, + #dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>> +} { + + // An int fills one slot. The cursor moves by 4 and the int is read at the + // old cursor. + cir.func @va_int(%arg0: !cir.ptr<!cir.ptr<!s8i>>) -> !s32i requires_call_conv_lowering { + %0 = cir.va_arg %arg0 : (!cir.ptr<!cir.ptr<!s8i>>) -> !s32i + cir.return %0 : !s32i + } + + // CHECK-LABEL: cir.func @va_int( + // CHECK-SAME: %[[AP:.*]]: !cir.ptr<!cir.ptr<!s8i>>) -> !s32i { + // CHECK-NEXT: %[[CUR:.*]] = cir.load %[[AP]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> + // CHECK-NEXT: %[[CUR_U8:.*]] = cir.cast bitcast %[[CUR]] : !cir.ptr<!s8i> -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[STRIDE:.*]] = cir.const #cir.int<4> : !s32i + // CHECK-NEXT: %[[NEXT_U8:.*]] = cir.ptr_stride %[[CUR_U8]], %[[STRIDE]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[NEXT:.*]] = cir.cast bitcast %[[NEXT_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s8i> + // CHECK-NEXT: cir.store %[[NEXT]], %[[AP]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> + // CHECK-NEXT: %[[ARG_PTR:.*]] = cir.cast bitcast %[[CUR_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s32i> + // CHECK-NEXT: %[[ARG:.*]] = cir.load align(4) %[[ARG_PTR]] : !cir.ptr<!s32i>, !s32i + // CHECK-NEXT: cir.return %[[ARG]] : !s32i + + // A double fills two slots. The cursor moves by 8 but is not realigned, so + // the double is read with align 4, not its own 8. + cir.func @va_double(%arg0: !cir.ptr<!cir.ptr<!s8i>>) -> !cir.double requires_call_conv_lowering { + %0 = cir.va_arg %arg0 : (!cir.ptr<!cir.ptr<!s8i>>) -> !cir.double + cir.return %0 : !cir.double + } + + // CHECK-LABEL: cir.func @va_double( + // CHECK-SAME: %[[AP:.*]]: !cir.ptr<!cir.ptr<!s8i>>) -> !cir.double { + // CHECK: %[[CUR:.*]] = cir.load %[[AP]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> + // CHECK-NEXT: %[[CUR_U8:.*]] = cir.cast bitcast %[[CUR]] : !cir.ptr<!s8i> -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[STRIDE:.*]] = cir.const #cir.int<8> : !s32i + // CHECK-NEXT: %[[NEXT_U8:.*]] = cir.ptr_stride %[[CUR_U8]], %[[STRIDE]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[NEXT:.*]] = cir.cast bitcast %[[NEXT_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s8i> + // CHECK-NEXT: cir.store %[[NEXT]], %[[AP]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> + // CHECK-NEXT: %[[ARG_PTR:.*]] = cir.cast bitcast %[[CUR_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!cir.double> + // CHECK-NEXT: %[[ARG:.*]] = cir.load align(4) %[[ARG_PTR]] : !cir.ptr<!cir.double>, !cir.double + // CHECK-NEXT: cir.return %[[ARG]] : !cir.double + + cir.func @va_long(%arg0: !cir.ptr<!cir.ptr<!s8i>>) -> !s64i requires_call_conv_lowering { + %0 = cir.va_arg %arg0 : (!cir.ptr<!cir.ptr<!s8i>>) -> !s64i + cir.return %0 : !s64i + } + + // CHECK-LABEL: cir.func @va_long( + // CHECK-SAME: %[[AP:.*]]: !cir.ptr<!cir.ptr<!s8i>>) -> !s64i { + // CHECK: %[[CUR:.*]] = cir.load %[[AP]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> + // CHECK-NEXT: %[[CUR_U8:.*]] = cir.cast bitcast %[[CUR]] : !cir.ptr<!s8i> -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[STRIDE:.*]] = cir.const #cir.int<8> : !s32i + // CHECK-NEXT: %[[NEXT_U8:.*]] = cir.ptr_stride %[[CUR_U8]], %[[STRIDE]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[NEXT:.*]] = cir.cast bitcast %[[NEXT_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s8i> + // CHECK-NEXT: cir.store %[[NEXT]], %[[AP]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> + // CHECK-NEXT: %[[ARG_PTR:.*]] = cir.cast bitcast %[[CUR_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s64i> + // CHECK-NEXT: %[[ARG:.*]] = cir.load align(4) %[[ARG_PTR]] : !cir.ptr<!s64i>, !s64i + // CHECK-NEXT: cir.return %[[ARG]] : !s64i + + // A one-byte record still takes a whole slot. + cir.func @va_char_struct(%arg0: !cir.ptr<!cir.ptr<!s8i>>, %arg1: !cir.ptr<!rec_Char>) requires_call_conv_lowering { + %0 = cir.va_arg %arg0 : (!cir.ptr<!cir.ptr<!s8i>>) -> !rec_Char + cir.store %0, %arg1 : !rec_Char, !cir.ptr<!rec_Char> + cir.return + } + + // CHECK-LABEL: cir.func @va_char_struct( + // CHECK-SAME: %[[AP:[^:]*]]: !cir.ptr<!cir.ptr<!s8i>>, %[[OUT:.*]]: !cir.ptr<!rec_Char>) { + // CHECK: %[[CUR:.*]] = cir.load %[[AP]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> + // CHECK-NEXT: %[[CUR_U8:.*]] = cir.cast bitcast %[[CUR]] : !cir.ptr<!s8i> -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[STRIDE:.*]] = cir.const #cir.int<4> : !s32i + // CHECK-NEXT: %[[NEXT_U8:.*]] = cir.ptr_stride %[[CUR_U8]], %[[STRIDE]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[NEXT:.*]] = cir.cast bitcast %[[NEXT_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s8i> + // CHECK-NEXT: cir.store %[[NEXT]], %[[AP]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> + // CHECK-NEXT: %[[ARG_PTR:.*]] = cir.cast bitcast %[[CUR_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!rec_Char> + // CHECK-NEXT: cir.copy %[[ARG_PTR]] align(4) to %[[OUT]] align(1) : !cir.ptr<!rec_Char> + + // A 12-byte record fills three slots. The store of the fetched value becomes + // a copy straight from the old cursor. + cir.func @va_triple(%arg0: !cir.ptr<!cir.ptr<!s8i>>, %arg1: !cir.ptr<!rec_Triple>) requires_call_conv_lowering { + %0 = cir.va_arg %arg0 : (!cir.ptr<!cir.ptr<!s8i>>) -> !rec_Triple + cir.store %0, %arg1 : !rec_Triple, !cir.ptr<!rec_Triple> + cir.return + } + + // CHECK-LABEL: cir.func @va_triple( + // CHECK-SAME: %[[AP:[^:]*]]: !cir.ptr<!cir.ptr<!s8i>>, %[[OUT:.*]]: !cir.ptr<!rec_Triple>) { + // CHECK: %[[CUR:.*]] = cir.load %[[AP]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> + // CHECK-NEXT: %[[CUR_U8:.*]] = cir.cast bitcast %[[CUR]] : !cir.ptr<!s8i> -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[STRIDE:.*]] = cir.const #cir.int<12> : !s32i + // CHECK-NEXT: %[[NEXT_U8:.*]] = cir.ptr_stride %[[CUR_U8]], %[[STRIDE]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[NEXT:.*]] = cir.cast bitcast %[[NEXT_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s8i> + // CHECK-NEXT: cir.store %[[NEXT]], %[[AP]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> + // CHECK-NEXT: %[[ARG_PTR:.*]] = cir.cast bitcast %[[CUR_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!rec_Triple> + // CHECK-NEXT: cir.copy %[[ARG_PTR]] align(4) to %[[OUT]] align(4) : !cir.ptr<!rec_Triple> + + // A record past the register budget is still read in place, not through a + // pointer. + cir.func @va_big(%arg0: !cir.ptr<!cir.ptr<!s8i>>, %arg1: !cir.ptr<!rec_Big>) requires_call_conv_lowering { + %0 = cir.va_arg %arg0 : (!cir.ptr<!cir.ptr<!s8i>>) -> !rec_Big + cir.store %0, %arg1 : !rec_Big, !cir.ptr<!rec_Big> + cir.return + } + + // CHECK-LABEL: cir.func @va_big( + // CHECK-SAME: %[[AP:[^:]*]]: !cir.ptr<!cir.ptr<!s8i>>, %[[OUT:.*]]: !cir.ptr<!rec_Big>) { + // CHECK: %[[CUR:.*]] = cir.load %[[AP]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> + // CHECK-NEXT: %[[CUR_U8:.*]] = cir.cast bitcast %[[CUR]] : !cir.ptr<!s8i> -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[STRIDE:.*]] = cir.const #cir.int<80> : !s32i + // CHECK-NEXT: %[[NEXT_U8:.*]] = cir.ptr_stride %[[CUR_U8]], %[[STRIDE]] : (!cir.ptr<!u8i>, !s32i) -> !cir.ptr<!u8i> + // CHECK-NEXT: %[[NEXT:.*]] = cir.cast bitcast %[[NEXT_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!s8i> + // CHECK-NEXT: cir.store %[[NEXT]], %[[AP]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> + // CHECK-NEXT: %[[ARG_PTR:.*]] = cir.cast bitcast %[[CUR_U8]] : !cir.ptr<!u8i> -> !cir.ptr<!rec_Big> + // CHECK-NEXT: cir.copy %[[ARG_PTR]] align(4) to %[[OUT]] align(4) : !cir.ptr<!rec_Big> +} diff --git a/clang/test/CodeGen/AMDGPU/abi-va-arg.c b/clang/test/CodeGen/AMDGPU/abi-va-arg.c new file mode 100644 index 0000000000000..984ca85e926a3 --- /dev/null +++ b/clang/test/CodeGen/AMDGPU/abi-va-arg.c @@ -0,0 +1,89 @@ +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -emit-llvm %s -o - | FileCheck %s %} + +// Checks how AMDGPU functions read variadic arguments with va_arg. +// Each value is read in place from 4-byte slots of one buffer. The cursor +// moves by the alloc size rounded up to 4 and is never realigned. + +typedef struct { char c; } char_t; +typedef struct { int a, b, c; } triple_t; +typedef struct { int data[20]; } big_t; + +int va_int(int n, ...) { + __builtin_va_list ap; + __builtin_va_start(ap, n); + int v = __builtin_va_arg(ap, int); + __builtin_va_end(ap); + return v; +} + +// CHECK-LABEL: define {{.*}} i32 @va_int(i32 noundef %{{.*}}, ...) +// CHECK: call void @llvm.va_start.p0(ptr %[[AP:[^)]+]]) +// CHECK-NEXT: %[[CUR:.*]] = load ptr, ptr %[[AP]], align 8 +// CHECK-NEXT: %[[NEXT:.*]] = getelementptr {{.*}}i8, ptr %[[CUR]], i64 4 +// CHECK-NEXT: store ptr %[[NEXT]], ptr %[[AP]], align 8 +// CHECK-NEXT: %{{.*}} = load i32, ptr %[[CUR]], align 4 + +// A double takes two slots and is read at the slot's alignment, not its own 8. +double va_double(int n, ...) { + __builtin_va_list ap; + __builtin_va_start(ap, n); + double v = __builtin_va_arg(ap, double); + __builtin_va_end(ap); + return v; +} + +// CHECK-LABEL: define {{.*}} double @va_double(i32 noundef %{{.*}}, ...) +// CHECK: call void @llvm.va_start.p0(ptr %[[AP:[^)]+]]) +// CHECK-NEXT: %[[CUR:.*]] = load ptr, ptr %[[AP]], align 8 +// CHECK-NEXT: %[[NEXT:.*]] = getelementptr {{.*}}i8, ptr %[[CUR]], i64 8 +// CHECK-NEXT: store ptr %[[NEXT]], ptr %[[AP]], align 8 +// CHECK-NEXT: %{{.*}} = load double, ptr %[[CUR]], align 4 + +// A one-byte record still takes a whole slot. +char_t va_char_struct(int n, ...) { + __builtin_va_list ap; + __builtin_va_start(ap, n); + char_t v = __builtin_va_arg(ap, char_t); + __builtin_va_end(ap); + return v; +} + +// CHECK-LABEL: define {{.*}} i8 @va_char_struct(i32 noundef %{{.*}}, ...) +// CHECK: call void @llvm.va_start.p0(ptr %[[AP:[^)]+]]) +// CHECK-NEXT: %[[CUR:.*]] = load ptr, ptr %[[AP]], align 8 +// CHECK-NEXT: %[[NEXT:.*]] = getelementptr {{.*}}i8, ptr %[[CUR]], i64 4 +// CHECK-NEXT: store ptr %[[NEXT]], ptr %[[AP]], align 8 +// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %{{.*}}, ptr align 4 %[[CUR]], i64 1, i1 false) + +triple_t va_triple(int n, ...) { + __builtin_va_list ap; + __builtin_va_start(ap, n); + triple_t v = __builtin_va_arg(ap, triple_t); + __builtin_va_end(ap); + return v; +} + +// CHECK-LABEL: define {{.*}} %struct.triple_t @va_triple(i32 noundef %{{.*}}, ...) +// CHECK: call void @llvm.va_start.p0(ptr %[[AP:[^)]+]]) +// CHECK-NEXT: %[[CUR:.*]] = load ptr, ptr %[[AP]], align 8 +// CHECK-NEXT: %[[NEXT:.*]] = getelementptr {{.*}}i8, ptr %[[CUR]], i64 12 +// CHECK-NEXT: store ptr %[[NEXT]], ptr %[[AP]], align 8 +// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %[[CUR]], i64 12, i1 false) + +// A record past the register budget is still read in place, not through a +// pointer. +big_t va_big(int n, ...) { + __builtin_va_list ap; + __builtin_va_start(ap, n); + big_t v = __builtin_va_arg(ap, big_t); + __builtin_va_end(ap); + return v; +} + +// CHECK-LABEL: define {{.*}} void @va_big(ptr addrspace(5) dead_on_unwind noalias writable sret(%struct.big_t) align 4 %{{.*}}, i32 noundef %{{.*}}, ...) +// CHECK: call void @llvm.va_start.p0(ptr %[[AP:[^)]+]]) +// CHECK-NEXT: %[[CUR:.*]] = load ptr, ptr %[[AP]], align 8 +// CHECK-NEXT: %[[NEXT:.*]] = getelementptr {{.*}}i8, ptr %[[CUR]], i64 80 +// CHECK-NEXT: store ptr %[[NEXT]], ptr %[[AP]], align 8 +// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %[[CUR]], i64 80, i1 false) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
