[clang] a9a2d25 - [CIR] Implement LValue InitListExpr and FunctionalCastExpr lowering (#192298)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 16 06:20:50 PDT 2026
Author: Erich Keane
Date: 2026-04-16T13:20:44Z
New Revision: a9a2d25db9c011c15a6557bc308ea1856efcb058
URL: https://github.com/llvm/llvm-project/commit/a9a2d25db9c011c15a6557bc308ea1856efcb058
DIFF: https://github.com/llvm/llvm-project/commit/a9a2d25db9c011c15a6557bc308ea1856efcb058.diff
LOG: [CIR] Implement LValue InitListExpr and FunctionalCastExpr lowering (#192298)
First, this fixes the InitListExpr lowering. This copies the same
implementation from classic-codegen, and adds some tests (this is
otherwise untested in classic codegen?).
Second, while I was writing tests for the above, I accidented into the
FunctionalCastExpr, which just calls emitCastLValue, so this fixes that
as well.
Added:
clang/test/CIR/CodeGen/init-list-lvalue.cpp
Modified:
clang/lib/CIR/CodeGen/CIRGenFunction.cpp
clang/lib/CIR/CodeGen/CIRGenFunction.h
Removed:
################################################################################
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 329c8b05a5f77..5003c16296cc1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -1015,6 +1015,16 @@ clang::QualType CIRGenFunction::buildFunctionArgList(clang::GlobalDecl gd,
return retTy;
}
+LValue CIRGenFunction::emitInitListLValue(const InitListExpr *e) {
+ // Initializing an aggregate temporary in C++11: T{...}.
+ if (!e->isGLValue())
+ return emitAggExprToLValue(e);
+
+ // An lvalue initializer list must be initializing a reference.
+ assert(e->isTransparent() && "non-transparent glvalue init list");
+ return emitLValue(e->getInit(0));
+}
+
static std::variant<LValue, RValue>
emitPseudoObjectExpr(CIRGenFunction &cgf, const PseudoObjectExpr *e,
bool forLValue, AggValueSlot slot) {
@@ -1168,7 +1178,8 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
case Expr::CXXDynamicCastExprClass:
case Expr::CXXReinterpretCastExprClass:
case Expr::CXXConstCastExprClass:
- // TODO(cir): The above list is missing CXXFunctionalCastExprClass,
+ case Expr::CXXFunctionalCastExprClass:
+ // TODO(cir): The above list is missing
// CXXAddrSpaceCastExprClass, and ObjCBridgedCastExprClass.
return emitCastLValue(cast<CastExpr>(e));
case Expr::MaterializeTemporaryExprClass:
@@ -1179,6 +1190,8 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
return emitLValue(cast<ChooseExpr>(e)->getChosenSubExpr());
case Expr::SubstNonTypeTemplateParmExprClass:
return emitLValue(cast<SubstNonTypeTemplateParmExpr>(e)->getReplacement());
+ case Expr::InitListExprClass:
+ return emitInitListLValue(cast<InitListExpr>(e));
case Expr::PseudoObjectExprClass:
return emitPseudoObjectLValue(cast<PseudoObjectExpr>(e));
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index 7f91cba115f04..e1ae2745e88ff 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -1519,6 +1519,7 @@ class CIRGenFunction : public CIRGenTypeCache {
mlir::Value emitArrayLength(const clang::ArrayType *arrayType,
QualType &baseType, Address &addr);
LValue emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e);
+ LValue emitInitListLValue(const InitListExpr *e);
LValue emitExtVectorElementExpr(const ExtVectorElementExpr *e);
diff --git a/clang/test/CIR/CodeGen/init-list-lvalue.cpp b/clang/test/CIR/CodeGen/init-list-lvalue.cpp
new file mode 100644
index 0000000000000..fd4df4558318b
--- /dev/null
+++ b/clang/test/CIR/CodeGen/init-list-lvalue.cpp
@@ -0,0 +1,74 @@
+// RUN: %clang_cc1 -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 -triple x86_64-unknown-linux-gnu -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 -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
+
+struct Struct {
+ int i;
+ const char *c;
+};
+
+void test1(int i) {
+ // CIR: cir.func {{.*}}@_Z5test1i(%[[I_ARG:.*]]: {{.*}})
+ // LLVM: define {{.*}}void @_Z5test1i(i32 {{.*}}%[[I_ARG:.*]])
+ int &refI = {i};
+ // CIR: %[[I_ALLOCA:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
+ // CIR: %[[REFI_ALLOCA:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["refI", init, const]
+ // CIR: cir.store %[[I_ARG]], %[[I_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+ // CIR: cir.store {{.*}}%[[I_ALLOCA]], %[[REFI_ALLOCA]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+ // LLVM: %[[I_ALLOCA:.*]] = alloca i32
+ // LLVM: %[[REFI_ALLOCA:.*]] = alloca ptr
+ // LLVM: store i32 %[[I_ARG]], ptr %[[I_ALLOCA]]
+ // LLVM: store ptr %[[I_ALLOCA]], ptr %[[REFI_ALLOCA]]
+}
+
+void test2() {
+ // CIR-LABEL: cir.func {{.*}}@_Z5test2v()
+ // LLVM-LABEL: define {{.*}}void @_Z5test2v()
+ Struct s {1, "asdf"};
+ Struct &refS = {s};
+ // CIR: %[[S_ALLOCA:.*]] = cir.alloca !rec_Struct, !cir.ptr<!rec_Struct>, ["s", init]
+ // CIR: %[[REFS_ALLOCA:.*]] = cir.alloca !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>, ["refS", init, const]
+ // CIR: %[[GET_S_INIT:.*]] = cir.get_global @__const._Z5test2v.s : !cir.ptr<!rec_Struct>
+ // CIR: cir.copy %[[GET_S_INIT]] to %[[S_ALLOCA]] : !cir.ptr<!rec_Struct> loc(#loc33)
+ // CIR: cir.store {{.*}}%[[S_ALLOCA]], %[[REFS_ALLOCA]] : !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>
+ // LLVM: %[[S_ALLOCA:.*]] = alloca %struct.Struct
+ // LLVM: %[[REFS_ALLOCA:.*]] = alloca ptr
+ // LLVM: call void @llvm.memcpy.p0.p0.i64(ptr {{.*}}%[[S_ALLOCA]], ptr {{.*}}@__const._Z5test2v.s, i64 16, i1 false)
+ // LLVM: store ptr %[[S_ALLOCA]], ptr %[[REFS_ALLOCA]]
+}
+
+// Note: In addition to testing init-list-lvalue, this also tests
+// FunctionalCastExpr.
+void test3(Struct &s) {
+ // CIR: cir.func {{.*}}@_Z5test3R6Struct(%[[S_ARG:.*]]: !cir.ptr<!rec_Struct> {{.*}})
+ // LLVM: define dso_local void @_Z5test3R6Struct(ptr{{.*}}%[[S_ARG:.*]])
+ using refSTy = Struct &;
+ Struct &refS = refSTy{s};
+ // CIR: %[[S_ALLOCA:.*]] = cir.alloca !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>, ["s", init, const]
+ // CIR: %[[REFS_ALLOCA:.*]] = cir.alloca !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>, ["refS", init, const]
+ // CIR: cir.store %[[S_ARG]], %[[S_ALLOCA]] : !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>
+ // CIR: %[[S_LOAD:.*]] = cir.load %[[S_ALLOCA]] : !cir.ptr<!cir.ptr<!rec_Struct>>, !cir.ptr<!rec_Struct>
+ // CIR: cir.store {{.*}}%[[S_LOAD]], %[[REFS_ALLOCA]] : !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>
+ // LLVM: %[[S_ALLOCA:.*]] = alloca ptr
+ // LLVM: %[[REFS_ALLOCA:.*]] = alloca ptr
+ // LLVM: store ptr %[[S_ARG]], ptr %[[S_ALLOCA]]
+ // LLVM: %[[S_LOAD:.*]] = load ptr, ptr %[[S_ALLOCA]]
+ // LLVM: store ptr %[[S_LOAD]], ptr %[[REFS_ALLOCA]]
+}
+
+void test4() {
+ // CIR-LABEL: cir.func {{.*}}@_Z5test4v()
+ // LLVM-LABEL: define {{.*}}void @_Z5test4v()
+ Struct s;
+ auto& [sb1, sb2] {s};
+
+ // CIR: %[[S_ALLOCA:.*]] = cir.alloca !rec_Struct, !cir.ptr<!rec_Struct>, ["s"] {alignment = 8 : i64}
+ // CIR: %[[SB_ALLOCA:.*]] = cir.alloca !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>, ["", init, const]
+ // CIR: cir.store {{.*}}%[[S_ALLOCA]], %[[SB_ALLOCA]] : !cir.ptr<!rec_Struct>, !cir.ptr<!cir.ptr<!rec_Struct>>
+ // LLVM: %[[S_ALLOCA:.*]] = alloca %struct.Struct
+ // LLVM: %[[SB_ALLOCA:.*]] = alloca ptr
+ // LLVM: store ptr %[[S_ALLOCA]], ptr %[[SB_ALLOCA]]
+}
More information about the cfe-commits
mailing list