[clang] [CIR] Support SubstNonTypeTemplateParmExpr for aggregates (PR #223603)
Aman Maurya via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 14 23:00:27 PDT 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/223603
>From 765966be3835f54f04d3288f79a9a55128019334 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Tue, 15 Sep 2026 11:11:38 +0530
Subject: [PATCH 1/2] [CIR] Support SubstNonTypeTemplateParmExpr for aggregates
Visit the replacement expression for aggregate NTTPs, matching classic Clang codegen and existing CIR NTTP emitters. The gap was analyzed with LLM assistance.
Issue #223533
Assisted-by: Antigravity
---
clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp | 3 +-
.../CIR/CodeGen/non-type-template-param.cpp | 81 ++++++++++++-------
2 files changed, 55 insertions(+), 29 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
index afc32a3d24c42..2fb66232d806c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
@@ -402,8 +402,7 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
}
void VisitUnaryExtension(UnaryOperator *e) { Visit(e->getSubExpr()); }
void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *e) {
- cgf.cgm.errorNYI(e->getSourceRange(),
- "AggExprEmitter: VisitSubstNonTypeTemplateParmExpr");
+ Visit(e->getReplacement());
}
void VisitConstantExpr(ConstantExpr *e) {
ensureDest(cgf.getLoc(e->getSourceRange()), e->getType());
diff --git a/clang/test/CIR/CodeGen/non-type-template-param.cpp b/clang/test/CIR/CodeGen/non-type-template-param.cpp
index 9edb7c0ae53d7..4711c1dbf10e0 100644
--- a/clang/test/CIR/CodeGen/non-type-template-param.cpp
+++ b/clang/test/CIR/CodeGen/non-type-template-param.cpp
@@ -1,27 +1,54 @@
-// 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
-
-template <const int N>
-void template_foo() {
- int a = N + 5;
-}
-
-// CIR: %[[INIT:.*]] = cir.alloca "a" {{.*}} init : !cir.ptr<!s32i>
-// CIR: %[[CONST_1:.*]] = cir.const #cir.int<1> : !s32i
-// CIR: %[[CONST_2:.*]] = cir.const #cir.int<5> : !s32i
-// CIR: %[[ADD:.*]] = cir.add nsw %[[CONST_1]], %[[CONST_2]] : !s32i
-// CIR: cir.store{{.*}} %[[ADD]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
-
-// LLVM: %[[INIT:.*]] = alloca i32, align 4
-// LLVM: store i32 6, ptr %[[INIT]], align 4
-
-// OGCG: %[[INIT:.*]] = alloca i32, align 4
-// OGCG: store i32 6, ptr %[[INIT]], align 4
-
-void foo() {
- template_foo<1>();
-}
+// RUN: %clang_cc1 -std=c++20 -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 -std=c++20 -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 -std=c++20 -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
+
+template <const int N>
+void template_foo() {
+ int a = N + 5;
+}
+
+// CIR: %[[INIT:.*]] = cir.alloca "a" {{.*}} init : !cir.ptr<!s32i>
+// CIR: %[[CONST_1:.*]] = cir.const #cir.int<1> : !s32i
+// CIR: %[[CONST_2:.*]] = cir.const #cir.int<5> : !s32i
+// CIR: %[[ADD:.*]] = cir.add nsw %[[CONST_1]], %[[CONST_2]] : !s32i
+// CIR: cir.store{{.*}} %[[ADD]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
+
+// LLVM: %[[INIT:.*]] = alloca i32, align 4
+// LLVM: store i32 6, ptr %[[INIT]], align 4
+
+// OGCG: %[[INIT:.*]] = alloca i32, align 4
+// OGCG: store i32 6, ptr %[[INIT]], align 4
+
+void foo() {
+ template_foo<1>();
+}
+
+struct Point {
+ int x;
+ int y;
+};
+
+template <Point P>
+void template_agg() {
+ Point a = P;
+}
+
+// CIR: %[[A:.*]] = cir.alloca {{.*}}
+// CIR: %[[GLOBAL:.*]] = cir.get_global @{{.*}}
+// CIR: cir.copy %[[GLOBAL]] to %[[A]] : !cir.ptr<{{.*}}>
+// CIR: cir.return
+
+// LLVM: %[[A:.*]] = alloca %struct.Point
+// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align {{[0-9]+}} %[[A]], ptr align {{[0-9]+}} @{{.*}}, i64 8, i1 false)
+// LLVM: ret void
+
+// OGCG: %[[A:.*]] = alloca %struct.Point
+// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align {{[0-9]+}} %[[A]], ptr align {{[0-9]+}} @{{.*}}, i64 8, i1 false)
+// OGCG: ret void
+
+void bar() {
+ template_agg<Point{1, 2}>();
+}
>From 30ccdf3d79efc1e6d014525699af21860513bc12 Mon Sep 17 00:00:00 2001
From: Aman Maurya <amanmaurya9209 at gmail.com>
Date: Tue, 15 Sep 2026 11:30:18 +0530
Subject: [PATCH 2/2] Update clang/test/CIR/CodeGen/non-type-template-param.cpp
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
clang/test/CIR/CodeGen/non-type-template-param.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CIR/CodeGen/non-type-template-param.cpp b/clang/test/CIR/CodeGen/non-type-template-param.cpp
index 4711c1dbf10e0..97567cb3c14b0 100644
--- a/clang/test/CIR/CodeGen/non-type-template-param.cpp
+++ b/clang/test/CIR/CodeGen/non-type-template-param.cpp
@@ -3,7 +3,7 @@
// RUN: %clang_cc1 -std=c++20 -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 -std=c++20 -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
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
template <const int N>
void template_foo() {
More information about the cfe-commits
mailing list