https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/244116

>From 1c927e66ef51ffb7fdfa0f257c2192aa4f97c8a6 Mon Sep 17 00:00:00 2001
From: skc7 <[email protected]>
Date: Thu, 19 Nov 2026 12:18:03 +0530
Subject: [PATCH 1/2] [CIR] Lower va_arg for AMDGPU in CallConvLowering

---
 .../Transforms/CIRABIClassifierAMDGPU.cpp     |   9 +-
 .../TargetLowering/CIRABIRewriteContext.cpp   |  72 ++++++++--
 .../TargetLowering/CIRABIRewriteContext.h     |   2 +-
 .../Transforms/abi-lowering/amdgpu-vaarg.cir  | 130 ++++++++++++++++++
 clang/test/CodeGen/AMDGPU/abi-va-arg.c        |  89 ++++++++++++
 5 files changed, 283 insertions(+), 19 deletions(-)
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/amdgpu-vaarg.cir
 create mode 100644 clang/test/CodeGen/AMDGPU/abi-va-arg.c

diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp 
b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierAMDGPU.cpp
index c63191bd1eaac5..6d4b3a56f594a8 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 f286fa85ec28eb..19f082a60b78e4 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 671be6c185a83c..675826042ca073 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 00000000000000..c3d5e6fa9415f6
--- /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 00000000000000..984ca85e926a35
--- /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)

>From 37f11a56b6d265805a9c3847404d764bca2ea420 Mon Sep 17 00:00:00 2001
From: skc7 <[email protected]>
Date: Fri, 20 Nov 2026 12:35:38 +0530
Subject: [PATCH 2/2] use rewriteVAArgAMDGPU

---
 .../Transforms/CallConvLoweringPass.cpp       |  2 +-
 .../TargetLowering/CIRABIRewriteContext.cpp   | 52 +++++++++++--------
 .../TargetLowering/CIRABIRewriteContext.h     | 29 +++++++++--
 3 files changed, 55 insertions(+), 28 deletions(-)

diff --git a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp 
b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
index 51c172758a5232..65b0cfdec67ca8 100644
--- a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
@@ -208,7 +208,7 @@ void CallConvLoweringPass::runOnOperation() {
       !loweringOpts || loweringOpts.getEnableNoundefAttrs();
 
   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 19f082a60b78e4..53b7e6d65e2b40 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -2778,13 +2778,6 @@ 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,
@@ -2830,9 +2823,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
@@ -2840,23 +2830,41 @@ 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();
   }
 
-  // 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);
+  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");
diff --git 
a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h 
b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.h
index 675826042ca073..3d85c5d66ed312 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, or into a read from a pointer va_list's slots.
+  /// 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.

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

Reply via email to