llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Chaitanya (skc7)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/244116.diff


5 Files Affected:

- (modified) clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp (+4-5) 
- (modified) 
clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp 
(+59-13) 
- (modified) 
clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h (+1-1) 
- (added) clang/test/CIR/Transforms/abi-lowering/amdgpu-vaarg.cir (+130) 
- (added) clang/test/CodeGen/AMDGPU/abi-va-arg.c (+89) 


``````````diff
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/TargetLowering/CIRABIRewriteContext.cpp 
b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index 97a70984a8486..48a75cccdeabe 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -2778,6 +2778,50 @@ void advanceRegisterCursors(CIRBaseBuilderTy &b, 
mlir::Location loc,
   }
 }
 
+/// Whether \p ac reads the value in place from a buffer of slots, each
+/// ac.directAlign in size and alignment.
+bool isSlotFetch(const ArgClassification &ac) {
+  return ac.kind == ArgKind::Direct && !ac.coercedType && ac.directAlign &&
+         !ac.neededIntRegs && !ac.neededSseRegs;
+}
+
+/// 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
@@ -2801,6 +2845,19 @@ CIRABIRewriteContext::rewriteVAArg(mlir::Operation 
*vaArgOp,
     return mlir::success();
   }
 
+  // A va_list that is a single pointer walks one buffer of fixed-size slots.
+  auto vaListTy = mlir::cast<cir::PointerType>(valist.getType()).getPointee();
+  if (mlir::isa<cir::PointerType>(vaListTy) && isSlotFetch(ac)) {
+    // A value smaller than its slot would sit right-adjusted on big-endian.
+    if (cirDataLayout.isBigEndian())
+      return reportVAArgNYI(op, "a slot on a big-endian target");
+    builder.setInsertionPoint(op);
+    uint64_t slotSize = ac.directAlign->value();
+    mlir::Value addr =
+        buildSlotAddrAndAdvance(builder, loc, valist, resultTy, slotSize, dl);
+    return replaceVAArgResult(builder, op, addr, slotSize, dl);
+  }
+
   if (ac.kind == ArgKind::Indirect && !ac.byVal)
     return reportVAArgNYI(op, "a non-trivially-copyable type");
   if (ac.kind == ArgKind::IndirectAliased)
@@ -2912,17 +2969,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..675826042ca07 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h
@@ -66,7 +66,7 @@ class CIRABIRewriteContext : public 
mlir::abi::ABIRewriteContext {
                   mlir::OpBuilder &builder) override;
 
   /// Expand a `cir.va_arg` into the x86-64 SysV register-save-area /
-  /// overflow-area sequence.
+  /// overflow-area sequence, or into a read from a pointer va_list's slots.
   mlir::LogicalResult rewriteVAArg(mlir::Operation *vaArgOp,
                                    const mlir::abi::ArgClassification &ac,
                                    mlir::OpBuilder &builder) override;
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)

``````````

</details>


https://github.com/llvm/llvm-project/pull/244116
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to