[clang] [CIR] Support CXXScalarValueInitExpr in AggExprEmitter (PR #237422)

Aman Maurya via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 08:51:39 PST 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/237422

>From c54ef34af926be8e55063420f58d2cf4641c9e15 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 31 Oct 2026 04:00:54 +0530
Subject: [PATCH 1/2] [CIR] Support CXXScalarValueInitExpr in AggExprEmitter

Implement VisitCXXScalarValueInitExpr in AggExprEmitter by obtaining the destination AggValueSlot and null-initializing the lvalue.

Fixes #237404
---
 clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp |  7 ++++--
 .../CodeGenCXX/agg-cxx-scalar-value-init.cpp  | 25 +++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp

diff --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
index ac186a5b58a38b..da36fec890c80b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
@@ -653,8 +653,11 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
   }
 
   void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) {
-    cgf.cgm.errorNYI(e->getSourceRange(),
-                     "AggExprEmitter: VisitCXXScalarValueInitExpr");
+    QualType ty = e->getType();
+    mlir::Location loc = cgf.getLoc(e->getSourceRange());
+    AggValueSlot slot = ensureSlot(loc, ty);
+    emitNullInitializationToLValue(loc,
+                                   cgf.makeAddrLValue(slot.getAddress(), ty));
   }
   void VisitCXXTypeidExpr(CXXTypeidExpr *e) { emitAggLoadOfLValue(e); }
   void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *e) {
diff --git a/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp b/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp
new file mode 100644
index 00000000000000..3750988a44436f
--- /dev/null
+++ b/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm -disable-llvm-passes %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct S {
+  int a;
+};
+
+// CIR-LABEL: cir.func {{.*}} @_Z4testb
+// OGCG-LABEL: define {{.*}} @_Z4testb
+void test(bool b) {
+  // CIR: cir.if %{{.+}} {
+  // CIR:   %[[MEM:.*]] = cir.get_member %{{.*}}[0] {name = "a"} : !cir.ptr<!rec_S> -> !cir.ptr<!s32i>
+  // CIR:   %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR:   cir.store align(4) %[[ZERO]], %[[MEM]] : !s32i, !cir.ptr<!s32i>
+  // CIR: } else {
+  // CIR:   %[[MEM2:.*]] = cir.get_member %{{.*}}[0] {name = "a"} : !cir.ptr<!rec_S> -> !cir.ptr<!s32i>
+  // CIR:   %[[ZERO2:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR:   cir.store align(4) %[[ZERO2]], %[[MEM2]] : !s32i, !cir.ptr<!s32i>
+  // CIR: }
+
+  // OGCG: store i32 0, ptr %{{.*}}, align 4
+  S s = b ? S{int()} : S{int()};
+}

>From ef93aa0bccde319cfab7318c7d3aa5e129d21c73 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 11 Nov 2026 22:20:40 +0530
Subject: [PATCH 2/2] [CIR] Add atomic struct test case in
 agg-cxx-scalar-value-init.cpp

---
 .../CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp b/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp
index 3750988a44436f..07055d8502e4e4 100644
--- a/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp
+++ b/clang/test/CIR/CodeGenCXX/agg-cxx-scalar-value-init.cpp
@@ -23,3 +23,19 @@ void test(bool b) {
   // OGCG: store i32 0, ptr %{{.*}}, align 4
   S s = b ? S{int()} : S{int()};
 }
+
+struct Pair {
+  int a;
+  int b;
+};
+using AS = _Atomic(Pair);
+
+// CIR-LABEL: cir.func {{.*}} @_Z11test_atomicv
+// OGCG-LABEL: define {{.*}} @_Z11test_atomicv
+void test_atomic() {
+  // CIR: %[[ZERO:.*]] = cir.const #cir.zero : !rec_Pair
+  // CIR: cir.store %[[ZERO]], %{{.*}} : !rec_Pair, !cir.ptr<!rec_Pair>
+
+  // OGCG: call void @llvm.memset.p0.i64(ptr align 8 %{{.*}}, i8 0, i64 8, i1 false)
+  AS s = AS();
+}



More information about the cfe-commits mailing list