Author: adams381
Date: 2025-12-18T15:53:04-08:00
New Revision: c2f33b5a28f82b723db51469aa4681cefea23c09

URL: 
https://github.com/llvm/llvm-project/commit/c2f33b5a28f82b723db51469aa4681cefea23c09
DIFF: 
https://github.com/llvm/llvm-project/commit/c2f33b5a28f82b723db51469aa4681cefea23c09.diff

LOG: [CIR] Implement AggExprEmitter::VisitVAArgExpr (#172551)

This PR implements support for aggregate va_arg expressions in CIR
codegen.

## Changes

- **CIRGenBuiltin.cpp**: Modified `emitVAArg` to return a pointer type
for aggregate types. For aggregate types, `va_arg` returns a pointer to
the aggregate rather than the aggregate value itself.

- **CIRGenExprAggregate.cpp**: Implemented
`AggExprEmitter::VisitVAArgExpr` to handle aggregate va_arg expressions
by:
  - Getting the va_arg pointer from `emitVAArg()`
  - Creating an `Address` from the pointer with proper alignment
  - Creating an `LValue` from the `Address`
- Copying the aggregate value to the destination using
`emitFinalDestCopy()`

- **Test**: Added comprehensive test `var-arg-aggregate.c` with CIR,
LLVM, and OGCG checks to verify the implementation matches original
codegen behavior.

## Testing

All tests pass:
- `check-clang-cir-codegen`: 180/181 passed (99.45%)
- `check-clang-cir`: 423/424 passed (99.76%)

Added: 
    clang/test/CIR/CodeGen/var-arg-aggregate.c

Modified: 
    clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
index 367c56f07f734..3eef284405ad0 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
@@ -375,7 +375,22 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
   }
 
   void VisitVAArgExpr(VAArgExpr *e) {
-    cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitVAArgExpr");
+    // emitVAArg returns an aggregate value (not a pointer) at the CIR level.
+    // ABI-specific pointer handling will be done later in LoweringPrepare.
+    mlir::Value vaArgValue = cgf.emitVAArg(e);
+
+    // Create a temporary alloca to hold the aggregate value.
+    mlir::Location loc = cgf.getLoc(e->getSourceRange());
+    Address tmpAddr = cgf.createMemTemp(e->getType(), loc, "vaarg.tmp");
+
+    // Store the va_arg result into the temporary.
+    cgf.emitAggregateStore(vaArgValue, tmpAddr);
+
+    // Create an LValue from the temporary address.
+    LValue tmpLValue = cgf.makeAddrLValue(tmpAddr, e->getType());
+
+    // Copy the aggregate value from temporary to destination.
+    emitFinalDestCopy(e->getType(), tmpLValue);
   }
 
   void VisitCXXThrowExpr(const CXXThrowExpr *e) {

diff  --git a/clang/test/CIR/CodeGen/var-arg-aggregate.c 
b/clang/test/CIR/CodeGen/var-arg-aggregate.c
new file mode 100644
index 0000000000000..5897bc41a116b
--- /dev/null
+++ b/clang/test/CIR/CodeGen/var-arg-aggregate.c
@@ -0,0 +1,51 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct Bar {
+  float f1;
+  float f2;
+  unsigned u;
+};
+
+struct Bar varargs_aggregate(int count, ...) {
+  __builtin_va_list args;
+  __builtin_va_start(args, count);
+  struct Bar res = __builtin_va_arg(args, struct Bar);
+  __builtin_va_end(args);
+  return res;
+}
+
+// CIR-LABEL: cir.func {{.*}} @varargs_aggregate(
+// CIR:   %[[COUNT_ADDR:.+]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["count", 
init]
+// CIR:   %[[RET_ADDR:.+]] = cir.alloca !rec_Bar, !cir.ptr<!rec_Bar>, 
["__retval", init]
+// CIR:   %[[VAAREA:.+]] = cir.alloca !cir.array<!rec___va_list_tag x 1>, 
!cir.ptr<!cir.array<!rec___va_list_tag x 1>>, ["args"]
+// CIR:   %[[TMP_ADDR:.+]] = cir.alloca !rec_Bar, !cir.ptr<!rec_Bar>, 
["vaarg.tmp"]
+// CIR:   cir.store %arg0, %[[COUNT_ADDR]] : !s32i, !cir.ptr<!s32i>
+// CIR:   %[[VA_PTR0:.+]] = cir.cast array_to_ptrdecay %[[VAAREA]] : 
!cir.ptr<!cir.array<!rec___va_list_tag x 1>> -> !cir.ptr<!rec___va_list_tag>
+// CIR:   %[[COUNT_VAL:.+]] = cir.load{{.*}} %[[COUNT_ADDR]] : 
!cir.ptr<!s32i>, !s32i
+// CIR:   cir.va_start %[[VA_PTR0]] %[[COUNT_VAL]] : 
!cir.ptr<!rec___va_list_tag>, !s32i
+// CIR:   %[[VA_PTR1:.+]] = cir.cast array_to_ptrdecay %[[VAAREA]] : 
!cir.ptr<!cir.array<!rec___va_list_tag x 1>> -> !cir.ptr<!rec___va_list_tag>
+// CIR:   %[[VA_ARG:.+]] = cir.va_arg %[[VA_PTR1]] : 
(!cir.ptr<!rec___va_list_tag>) -> !rec_Bar
+// CIR:   cir.store{{.*}} %[[VA_ARG]], %[[TMP_ADDR]] : !rec_Bar, 
!cir.ptr<!rec_Bar>
+// CIR:   cir.copy %[[TMP_ADDR]] to %[[RET_ADDR]] : !cir.ptr<!rec_Bar>
+// CIR:   %[[VA_PTR2:.+]] = cir.cast array_to_ptrdecay %[[VAAREA]] : 
!cir.ptr<!cir.array<!rec___va_list_tag x 1>> -> !cir.ptr<!rec___va_list_tag>
+// CIR:   cir.va_end %[[VA_PTR2]] : !cir.ptr<!rec___va_list_tag>
+// CIR:   %[[RETVAL:.+]] = cir.load{{.*}} %[[RET_ADDR]] : !cir.ptr<!rec_Bar>, 
!rec_Bar
+// CIR:   cir.return %[[RETVAL]] : !rec_Bar
+
+// LLVM-LABEL: define dso_local %struct.Bar @varargs_aggregate(
+// LLVM:   call void @llvm.va_start.p0(ptr %{{.*}})
+// LLVM:   %[[VA_PTR1:.+]] = getelementptr %struct.__va_list_tag, ptr %{{.*}}, 
i32 0
+// LLVM:   %[[VA_ARG:.+]] = va_arg ptr %[[VA_PTR1]], %struct.Bar
+// LLVM:   store %struct.Bar %[[VA_ARG]], ptr %{{.*}}
+// LLVM:   call void @llvm.memcpy.p0.p0.i32(ptr %{{.*}}, ptr %{{.*}}, i32 12, 
i1 false)
+
+// OGCG-LABEL: define dso_local { <2 x float>, i32 } @varargs_aggregate
+// OGCG:   call void @llvm.va_start.p0(ptr %{{.*}})
+// OGCG:   %[[VAARG_ADDR:.+]] = phi ptr [ %{{.*}}, %vaarg.in_reg ], [ %{{.*}}, 
%vaarg.in_mem ]
+// OGCG:   call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 
%[[VAARG_ADDR]], i64 12, i1 false)
+


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

Reply via email to