[clang] 62d473b - [CIR] Implement 1 more global-ref variable fixup (#183403)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 26 07:01:45 PST 2026
Author: Erich Keane
Date: 2026-02-26T07:01:40-08:00
New Revision: 62d473bd01192157493e5bc96e4a8d299ce2594d
URL: https://github.com/llvm/llvm-project/commit/62d473bd01192157493e5bc96e4a8d299ce2594d
DIFF: https://github.com/llvm/llvm-project/commit/62d473bd01192157493e5bc96e4a8d299ce2594d.diff
LOG: [CIR] Implement 1 more global-ref variable fixup (#183403)
This is related to #182608, and I discovered this doing something else.
This patch adds 1 missing call/removes 1 NYI to get references to a
global reference that weren't caught elsewhere to correctly codegen.
Added:
Modified:
clang/lib/CIR/CodeGen/CIRGenExpr.cpp
clang/test/CIR/CodeGenCXX/global-refs.cpp
Removed:
################################################################################
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index f8ad143977ff1..b92f7e6b424b2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -305,8 +305,8 @@ static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
Address addr(v, realVarTy, alignment);
LValue lv;
if (vd->getType()->isReferenceType())
- cgf.cgm.errorNYI(e->getSourceRange(),
- "emitGlobalVarDeclLValue: reference type");
+ lv = cgf.emitLoadOfReferenceLValue(addr, cgf.getLoc(e->getSourceRange()),
+ vd->getType(), AlignmentSource::Decl);
else
lv = cgf.makeAddrLValue(addr, t, AlignmentSource::Decl);
assert(!cir::MissingFeatures::setObjCGCLValueClass());
diff --git a/clang/test/CIR/CodeGenCXX/global-refs.cpp b/clang/test/CIR/CodeGenCXX/global-refs.cpp
index cf9070191bcb7..8749bb3019224 100644
--- a/clang/test/CIR/CodeGenCXX/global-refs.cpp
+++ b/clang/test/CIR/CodeGenCXX/global-refs.cpp
@@ -30,7 +30,6 @@ const int &constGlobalIntRef = 5;
// CIR: cir.global constant external @constGlobalIntRef = #cir.global_view<@_ZGR17constGlobalIntRef_> : !cir.ptr<!s32i> {alignment = 8 : i64}
// LLVM: @_ZGR17constGlobalIntRef_ = {{.*}}global i32 5, align 4
// LLVM: @constGlobalIntRef = constant ptr @_ZGR17constGlobalIntRef_, align 8
-// CIR-BEFORE-LLP: FAIL
DefCtor defCtor{};
// CIR: cir.global external @defCtor = #cir.undef : !rec_DefCtor {alignment = 1 : i64}
@@ -121,10 +120,25 @@ WithCtorDtor withCtorDtor{};
// CIR-AFTER-NEXT: }
// LLVM: @withCtorDtor = global %struct.WithCtorDtor zeroinitializer, align 1
+
WithCtorDtor &withCtorDtorRef = withCtorDtor;
// CIR: cir.global constant external @withCtorDtorRef = #cir.global_view<@withCtorDtor> : !cir.ptr<!rec_WithCtorDtor> {alignment = 8 : i64}
// LLVM: @withCtorDtorRef = constant ptr @withCtorDtor, align 8
+extern WithCtor &ExternRef;
+// CIR: cir.global "private" constant external @ExternRef : !cir.ptr<!rec_WithCtor>
+// LLVM: @ExternRef = external constant ptr
+
+void use() {
+ // CIR-LABEL: cir.func{{.*}}use
+
+ WithCtor &local = ExternRef;
+ // CIR-NEXT: %[[LOCAL:.*]] = cir.alloca !cir.ptr<!rec_WithCtor>, !cir.ptr<!cir.ptr<!rec_WithCtor>>, ["local", init, const]
+ // CIR-NEXT: %[[EXT_REF:.*]] = cir.get_global @ExternRef : !cir.ptr<!cir.ptr<!rec_WithCtor>>
+ // CIR-NEXT: %[[EXT_REF_LOAD:.*]] = cir.load %[[EXT_REF]] : !cir.ptr<!cir.ptr<!rec_WithCtor>>, !cir.ptr<!rec_WithCtor>
+ // CIR-NEXT: cir.store{{.*}}%[[EXT_REF_LOAD]], %[[LOCAL]]
+}
+
// LLVM: define internal void @__cxx_global_var_init{{.*}}()
// LLVM: call void @_ZN8WithCtorC1Ev(ptr {{.*}}@withCtor)
// LLVM-NEXT: ret void
@@ -144,6 +158,11 @@ WithCtorDtor &withCtorDtorRef = withCtorDtor;
// LLVM-NEXT: call {{.*}}@__cxa_atexit(ptr {{.*}}@_ZN12WithCtorDtorD1Ev, ptr {{.*}}@withCtorDtor, ptr {{.*}}@__dso_handle)
// LLVM-NEXT: ret void
+// LLVM-LABEL: define{{.*}}use
+// LLVM: %[[LOCAL:.*]] = alloca ptr
+// LLVM-NEXT: %[[EXT_REF:.*]] = load ptr, ptr @ExternRef
+// LLVM-NEXT: store ptr %[[EXT_REF]], ptr %[[LOCAL]]
+
// TODO(cir): Once we get destructors for temporaries done, we should test them
// here, same as the 'const-WithCtor' examples, except with the 'withCtorDtor'
// versions.
More information about the cfe-commits
mailing list