[clang] [CIR] Implement 'referenced-by-subobject' static-local lowering (PR #194070)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 24 17:00:03 PDT 2026
================
@@ -1008,10 +1008,20 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) {
auto iter = localDeclMap.find(vd);
if (iter != localDeclMap.end()) {
addr = iter->second;
- } else {
+
+ } else if (vd->isStaticLocal()) {
// Otherwise, it might be static local we haven't emitted yet for some
// reason; most likely, because it's in an outer function.
- cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: static local");
+ cir::GlobalOp var =
+ cgm.getOrCreateStaticVarDecl(*vd, cgm.getCIRLinkageVarDefinition(vd));
+ mlir::Value getGlobVal = builder.createGetGlobal(var);
+ auto getGlob = getGlobVal.getDefiningOp<cir::GetGlobalOp>();
+ getGlob.setStaticLocal(true);
----------------
andykaylor wrote:
This isn't quite right because the `static_local` attribute on GetGlobalOp isn't as general as it sounds like it would be. Per the definition (and this is enforced by the verifier) it means "this global is a function-local static variable that requires guarded initialization." So, if we get here with a static local that doesn't require a guard variable, we get a verifier error.
This example demonstrates the verifier error with this PR:
```
int referenced_inside_const() {
static int static_local = 42; // no guard
auto lam = []() { return static_local; }; // setStaticLocal(true) -> verifier error
return lam();
}
```
https://github.com/llvm/llvm-project/pull/194070
More information about the cfe-commits
mailing list