[clang] [CIR] Support VAArgExpr in CIRGenFunction::emitLValue (PR #248350)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 03:44:16 PST 2026
================
@@ -517,3 +514,48 @@ long varargs_packed_overaligned(int count, ...) {
// LLVM-LABEL: define dso_local i64 @varargs_packed_overaligned(i32 noundef %{{.*}}, ...)
// LLVM: %[[ALIGNED:.+]] = call ptr @llvm.ptrmask.p0.i64(ptr %{{.+}}, i64 -32)
// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 32 %{{.+}}, ptr align 32 %[[ALIGNED]], i64 32, i1 false)
+
+long varargs_aggregate_lvalue(int count, ...) {
+ __builtin_va_list args;
+ __builtin_va_start(args, count);
+ long res = __builtin_va_arg(args, struct LongPair).a;
+ __builtin_va_end(args);
+ return res;
+}
+
+// CIR-LABEL: cir.func {{.*}} @varargs_aggregate_lvalue(
+// CIR: %[[REF_TMP:.+]] = cir.alloca "ref.tmp0" align(8) : !cir.ptr<!rec_LongPair>
+// CIR: cir.copy %{{.+}} align(8) to %[[REF_TMP]] align(8) : !cir.ptr<!rec_LongPair>
+// CIR: %[[MEM:.+]] = cir.get_member %[[REF_TMP]][0] {name = "a"} : !cir.ptr<!rec_LongPair> -> !cir.ptr<!s64i>
+// CIR: %{{.+}} = cir.load align(8) %[[MEM]] : !cir.ptr<!s64i>, !s64i
+
+// LLVM-LABEL: define dso_local i64 @varargs_aggregate_lvalue(i32 noundef %{{.*}}, ...)
+// LLVM: %[[REF_TMP:.+]] = alloca %struct.LongPair
+// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[REF_TMP]], ptr align 8 %{{.+}}, i64 16, i1 false)
+// LLVM: %[[MEM:.+]] = getelementptr inbounds nuw %struct.LongPair, ptr %[[REF_TMP]], i32 0, i32 0
+// LLVM: %{{.+}} = load i64, ptr %[[MEM]], align 8
+
+typedef int IntArr[4];
+
+int varargs_array_lvalue(int count, ...) {
+ __builtin_va_list args;
+ __builtin_va_start(args, count);
+ int res = __builtin_va_arg(args, IntArr)[0];
+ __builtin_va_end(args);
+ return res;
+}
+
+// CIR-LABEL: cir.func {{.*}} @varargs_array_lvalue(
+// CIR: %[[AGG_TMP:.+]] = cir.alloca "tmp" align(4) : !cir.ptr<!cir.array<!s32i x 4>>
+// CIR: %[[VAL:.+]] = cir.load align(4) %{{.+}} : !cir.ptr<!cir.array<!s32i x 4>>, !cir.array<!s32i x 4>
+// CIR: cir.store align(4) %[[VAL]], %[[AGG_TMP]] : !cir.array<!s32i x 4>, !cir.ptr<!cir.array<!s32i x 4>>
+// CIR: %[[ELEM:.+]] = cir.get_element inbounds %[[AGG_TMP]][%{{.+}} : !s64i] : !cir.ptr<!cir.array<!s32i x 4>> -> !cir.ptr<!s32i>
+// CIR: %{{.+}} = cir.load align(4) %[[ELEM]] : !cir.ptr<!s32i>, !s32i
+
+// LLVM-LABEL: define dso_local i32 @varargs_array_lvalue(i32 noundef %{{.*}}, ...)
+// LLVM: %[[AGG_TMP:.+]] = alloca [4 x i32], align 4
+// LLVMCIR: %[[VAL:.+]] = load [4 x i32], ptr %{{.+}}, align 4
----------------
xlauko wrote:
Classic codegen copies the array with `memcpy` (line 559). CIR loads the whole `[4 x i32]` and stores it, because `replaceVAArgResult` in `CIRABIRewriteContext.cpp` only redirects uses to the slot for `cir::RecordType`. Could arrays take the same path so the LLVMCIR and OGCG lines merge into one LLVM check?
https://github.com/llvm/llvm-project/pull/248350
More information about the cfe-commits
mailing list