[clang] [CIR] Support ConstantExpr GLValues and subexpression fallback in ScalarExprEmitter (PR #242234)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 02:10:37 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clangir

Author: Aman Maurya (amanmaurya92)

<details>
<summary>Changes</summary>

### 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.


---
Full diff: https://github.com/llvm/llvm-project/pull/242234.diff


2 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+10-5) 
- (modified) clang/test/CIR/CodeGenCXX/global-refs.cpp (+14) 


``````````diff
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.

``````````

</details>


https://github.com/llvm/llvm-project/pull/242234


More information about the cfe-commits mailing list