[clang] [CIR] Implement 'weak-ref' lowering (PR #238538)
Bruno Cardoso Lopes via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 11:47:13 PST 2026
================
@@ -1194,8 +1196,17 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) {
!e->getLocation().isValid()) &&
"Should not use decl without marking it used!");
- if (nd->hasAttr<WeakRefAttr>())
- cgm.errorNYI(nd->getSourceRange(), "emitGlobal: WeakRefAttr");
+ // A weakref variable refers to its alias target. Weakref functions are
+ // handled by emitFunctionDeclPointer.
+ if (const auto *vd = dyn_cast<VarDecl>(nd);
+ vd && vd->hasAttr<WeakRefAttr>()) {
+ auto aliasee = mlir::cast<cir::GlobalOp>(cgm.getWeakRefReference(vd));
----------------
bcardosolopes wrote:
`getWeakRefReference` returns whatever already has the target's name, and that isn't always a `cir.global` of this type. Say `extern long tgt;` (or `void tgt(void);`) appears earlier in the TU, then `static int r __attribute__((weakref("tgt")));` and a use of `r`. OG doesn't care, it wraps the existing entry with the decl's type, but here I'd expect either this `cast` or the pointee assert in `Address` to fire. The function side has the same problem when a global already has the name. Odd code, but worth an errorNYI on the mismatch rather than an assert?
Also, the comment above still points at `emitFunctionDeclPointer`, which is `getRawFunctionPointer` now.
https://github.com/llvm/llvm-project/pull/238538
More information about the cfe-commits
mailing list