[clang] [CIR] Support file-scope compound literals in emitCompoundLiteralLValue (PR #248358)
Aman Maurya via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 2 23:51:26 PST 2026
https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/248358
Add `CIRGenModule::getAddrOfConstantCompoundLiteral` and handle `e->isFileScope()` in `CIRGenFunction::emitCompoundLiteralLValue`, with CIR/LLVM/OGCG tests in `clang/test/CIR/CodeGen/compound_literal.cpp`.
Testing:
- `llvm-lit -v clang/test/CIR/CodeGen/compound_literal.cpp clang/test/CIR/CodeGen/compound_literal.c` (PASS 2/2)
Assisted-by: Antigravity (Gemini 4 Hydrogen)
>From 1666f17eaf480535ac8dce8aaaf6f40e8ba445d7 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Thu, 3 Dec 2026 13:20:33 +0530
Subject: [PATCH] [CIR] Support file-scope compound literals in
emitCompoundLiteralLValue
---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 7 +++--
clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp | 19 +++++++++----
clang/lib/CIR/CodeGen/CIRGenModule.h | 1 +
clang/test/CIR/CodeGen/compound_literal.cpp | 30 ++++++++++++++++++++
4 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index d2412dbca9565b..9ac79a709f4cab 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2433,8 +2433,11 @@ CIRGenFunction::getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e) {
LValue CIRGenFunction::emitCompoundLiteralLValue(const CompoundLiteralExpr *e) {
if (e->isFileScope()) {
- cgm.errorNYI(e->getSourceRange(), "emitCompoundLiteralLValue: FileScope");
- return {};
+ cir::GlobalOp globalOp = cgm.getAddrOfConstantCompoundLiteral(e);
+ mlir::Value addr = builder.createGetGlobal(globalOp);
+ CharUnits align = CharUnits::fromQuantity(globalOp.getAlignment().value());
+ return makeAddrLValue(Address(addr, align), e->getType(),
+ AlignmentSource::Decl);
}
if (e->getType()->isVariablyModifiedType())
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
index b2b124d938040f..027345a8f101ae 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
@@ -1023,15 +1023,14 @@ ConstantLValue ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *e) {
return {};
}
-static cir::GlobalViewAttr
+static cir::GlobalOp
tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
const CompoundLiteralExpr *e) {
CIRGenModule &cgm = emitter.cgm;
- CIRGenBuilderTy &builder = cgm.getBuilder();
CharUnits align = cgm.getASTContext().getTypeAlignInChars(e->getType());
if (cir::GlobalOp addr = cgm.getAddrOfConstantCompoundLiteralIfEmitted(e))
- return builder.getGlobalViewAttr(addr);
+ return addr;
assert(!cir::MissingFeatures::addressSpace());
mlir::Attribute c =
@@ -1057,14 +1056,24 @@ tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
emitter.finalize(gv);
cgm.setAddrOfConstantCompoundLiteral(e, gv);
- return builder.getGlobalViewAttr(gv);
+ return gv;
}
ConstantLValue
ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *e) {
ConstantEmitter compoundLiteralEmitter(cgm, emitter.cgf);
compoundLiteralEmitter.setInConstantContext(emitter.isInConstantContext());
- return tryEmitGlobalCompoundLiteral(compoundLiteralEmitter, e);
+ if (cir::GlobalOp gv =
+ tryEmitGlobalCompoundLiteral(compoundLiteralEmitter, e))
+ return cgm.getBuilder().getGlobalViewAttr(gv);
+ return {};
+}
+
+cir::GlobalOp
+CIRGenModule::getAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *e) {
+ assert(e->isFileScope() && "not a file-scope compound literal expr");
+ ConstantEmitter emitter(*this);
+ return tryEmitGlobalCompoundLiteral(emitter, e);
}
ConstantLValue
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h
index 639f67444687d5..f1030622f4ff8e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.h
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -484,6 +484,7 @@ class CIRGenModule : public CIRGenTypeCache {
[[maybe_unused]] bool ok = emittedCompoundLiterals.insert({e, gv}).second;
assert(ok && "compound literal global already emitted");
}
+ cir::GlobalOp getAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *e);
/// Return a constant array for the given string.
mlir::Attribute getConstantArrayFromStringLiteral(const StringLiteral *e);
diff --git a/clang/test/CIR/CodeGen/compound_literal.cpp b/clang/test/CIR/CodeGen/compound_literal.cpp
index 31d753ddd25e90..5709975b37dab2 100644
--- a/clang/test/CIR/CodeGen/compound_literal.cpp
+++ b/clang/test/CIR/CodeGen/compound_literal.cpp
@@ -13,6 +13,26 @@ int foo() {
// CIR-DAG: cir.global "private" constant cir_private unnamed_addr @[[FOO4_P:.*]] = #cir.const_record<{#cir.int<5> : !s32i, #cir.int<10> : !s32i}> : !rec_Point
// LLVM-DAG: @[[FOO4_P:.*]] = private unnamed_addr constant %struct.Point { i32 5, i32 10 }
+// CIR-LABEL: cir.func internal private @__cxx_global_var_init()
+// CIR: %[[Y:.*]] = cir.get_global @y : !cir.ptr<!s32i>
+// CIR: %[[CL:.*]] = cir.get_global @".compoundliteral" : !cir.ptr<!cir.array<!s32i x 3>>
+// CIR: %[[G:.*]] = cir.get_global @g : !cir.ptr<!s32i>
+// CIR: %[[IDX:.*]] = cir.load align(4) %[[G]] : !cir.ptr<!s32i>, !s32i
+// CIR: %[[IDX64:.*]] = cir.cast integral %[[IDX]] : !s32i -> !s64i
+// CIR: %[[ELEM:.*]] = cir.get_element inbounds %[[CL]][%[[IDX64]] : !s64i] : !cir.ptr<!cir.array<!s32i x 3>> -> !cir.ptr<!s32i>
+// CIR: %[[VAL:.*]] = cir.load align(4) %[[ELEM]] : !cir.ptr<!s32i>, !s32i
+// CIR: cir.store align(4) %[[VAL]], %[[Y]] : !s32i, !cir.ptr<!s32i>
+// CIR-DAG: cir.global "private" internal @".compoundliteral" = #cir.const_array<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i]> : !cir.array<!s32i x 3> align(4)
+
+// LLVM-DAG: @.compoundliteral = internal global [3 x i32] [i32 1, i32 2, i32 3], align 4
+// OGCG-DAG: @.compoundliteral = internal global [3 x i32] [i32 1, i32 2, i32 3], align 4
+// LLVM-LABEL: define internal void @__cxx_global_var_init()
+// LLVM: %[[IDX:.*]] = load i32, ptr @g, align 4
+// LLVM: %[[IDX64:.*]] = sext i32 %[[IDX]] to i64
+// LLVM: %[[ELEM:.*]] = getelementptr inbounds [3 x i32], ptr @.compoundliteral, i64 0, i64 %[[IDX64]]
+// LLVM: %[[VAL:.*]] = load i32, ptr %[[ELEM]], align 4
+// LLVM: store i32 %[[VAL]], ptr @y, align 4
+
// CIR: %[[RET:.*]] = cir.alloca "" {{.*}} return_slot : !cir.ptr<!s32i>
// CIR: %[[INIT:.*]] = cir.alloca "e" {{.*}} init : !cir.ptr<!s32i>
// CIR: %[[COMPOUND:.*]] = cir.alloca ".compoundliteral" {{.*}} init : !cir.ptr<!s32i>
@@ -117,3 +137,13 @@ void foo4() {
// OGCG-LABEL: @_Z4foo4v
// OGCG: %[[P:.*]] = alloca %struct.Point
// OGCG: call void @llvm.memcpy{{.*}}(ptr{{.*}} %[[P]], ptr{{.*}} @__const._Z4foo4v.p
+
+int g = 0;
+int y = ((int[]){1, 2, 3})[g];
+
+// OGCG-LABEL: define internal void @__cxx_global_var_init()
+// OGCG: %[[IDX:.*]] = load i32, ptr @g, align 4
+// OGCG: %[[IDX64:.*]] = sext i32 %[[IDX]] to i64
+// OGCG: %[[ELEM:.*]] = getelementptr inbounds [3 x i32], ptr @.compoundliteral, i64 0, i64 %[[IDX64]]
+// OGCG: %[[VAL:.*]] = load i32, ptr %[[ELEM]], align 4
+// OGCG: store i32 %[[VAL]], ptr @y, align 4
More information about the cfe-commits
mailing list