https://github.com/amanmaurya92 created
https://github.com/llvm/llvm-project/pull/242234
### Description
This patch adds support for `ConstantExpr` GLValues and subexpression fallback
in `ScalarExprEmitter::VisitConstantExpr`, establishing parity with classic
Clang CodeGen (`clang/lib/CodeGen/CGExprScalar.cpp:516-530`).
In ClangIR, evaluating a `ConstantExpr` GLValue in an RValue scalar context
(such as returning or loading a scalar from a `consteval` function returning a
reference: `int test_load() { return get_ref(val); }`) previously caused an
`errorNYI: ScalarExprEmitter: constant expr GL Value`, while a null result from
`tryEmitConstantExpr` failed with `errorNYI: ScalarExprEmitter: constant expr`
rather than delegating to the subexpression.
This change:
1. Creates the pointee LValue via `makeNaturalAlignPointeeAddrLValue` and emits
a scalar load via `emitLoadOfScalar`.
2. Handles cases where constant evaluation already converted the GLValue to an
rvalue (`e->hasAPValueResult() && !e->getAPValueResult().isLValue()`).
3. Falls back to evaluating the subexpression via `Visit(e->getSubExpr())` when
`ConstantEmitter::tryEmitConstantExpr` cannot produce a constant attribute.
4. Adds test coverage in `clang/test/CIR/CodeGenCXX/global-refs.cpp` validating
CIR, LLVM IR, and classic CodeGen parity.
Complements #237421.
>From 18c45a71dc74ed70011297cd8fafcc22cad3f700 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <[email protected]>
Date: Fri, 13 Nov 2026 13:36:12 +0530
Subject: [PATCH] [CIR] Support ConstantExpr GLValues and subexpression
fallback in ScalarExprEmitter
Handle GLValues in ScalarExprEmitter::VisitConstantExpr by emitting a
scalar load from the pointee address, and fall back to evaluating the
subexpression when constant evaluation cannot produce an attribute.
Assisted-by: Antigravity (Gemini 3.8 Flash)
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 15 ++++++++++-----
clang/test/CIR/CodeGenCXX/global-refs.cpp | 14 ++++++++++++++
2 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index ee885fe1574f1..e8c6d5f019cb3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -152,17 +152,22 @@ class ScalarExprEmitter : public
StmtVisitor<ScalarExprEmitter, mlir::Value> {
if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e)) {
if (e->isGLValue()) {
- cgf.cgm.errorNYI(e->getSourceRange(),
- "ScalarExprEmitter: constant expr GL Value");
- return {};
+ if (e->hasAPValueResult() && !e->getAPValueResult().isLValue())
+ return builder.getConstant(cgf.getLoc(e->getSourceRange()),
+ mlir::cast<mlir::TypedAttr>(result));
+ mlir::Value val =
+ builder.getConstant(cgf.getLoc(e->getSourceRange()),
+ mlir::cast<mlir::TypedAttr>(result));
+ return cgf.emitLoadOfScalar(
+ cgf.makeNaturalAlignPointeeAddrLValue(val, e->getType()),
+ e->getExprLoc());
}
return builder.getConstant(cgf.getLoc(e->getSourceRange()),
mlir::cast<mlir::TypedAttr>(result));
}
- cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: constant expr");
- return {};
+ return Visit(e->getSubExpr());
}
mlir::Value VisitPackIndexingExpr(PackIndexingExpr *e) {
diff --git a/clang/test/CIR/CodeGenCXX/global-refs.cpp
b/clang/test/CIR/CodeGenCXX/global-refs.cpp
index e2e9d504b7c1b..4b6306d54d90e 100644
--- a/clang/test/CIR/CodeGenCXX/global-refs.cpp
+++ b/clang/test/CIR/CodeGenCXX/global-refs.cpp
@@ -187,6 +187,20 @@ const int &test_return() {
return get_ref(val);
}
+int test_load() {
+ // CIR-LABEL: cir.func {{.*}} @_Z9test_loadv
+ // CIR: %[[VAL_ADDR:.*]] = cir.const #cir.global_view<@_ZL3val> :
!cir.ptr<!s32i>
+ // CIR: %[[LOAD:.*]] = cir.load align(4) %[[VAL_ADDR]] : !cir.ptr<!s32i>,
!s32i
+ // CIR: cir.store{{.*}} %[[LOAD]], %[[RET_ADDR:.*]] : !s32i, !cir.ptr<!s32i>
+ // LLVM-LABEL: define {{.*}} @_Z9test_loadv
+ // LLVMCIR: %[[VAL:.*]] = load i32, ptr @_ZL3val, align 4
+ // LLVMCIR: store i32 %[[VAL]], ptr %{{.*}}, align 4
+ // LLVMCIR: ret i32
+ // OGCG: %[[VAL:.*]] = load i32, ptr @_ZL3val, align 4
+ // OGCG: ret i32 %[[VAL]]
+ return get_ref(val);
+}
+
// 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.
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits