[clang] 9099ddf - [CIR] Support ConstantExpr in CIRGenFunction::emitLValue (#237421)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 12:14:59 PST 2026
Author: Aman Maurya
Date: 2026-11-13T01:44:54+05:30
New Revision: 9099ddf1e0aae2397fb8a0aea85dc74e4ed4e698
URL: https://github.com/llvm/llvm-project/commit/9099ddf1e0aae2397fb8a0aea85dc74e4ed4e698
DIFF: https://github.com/llvm/llvm-project/commit/9099ddf1e0aae2397fb8a0aea85dc74e4ed4e698.diff
LOG: [CIR] Support ConstantExpr in CIRGenFunction::emitLValue (#237421)
### Description
This patch adds `ConstantExpr` handling to `CIRGenFunction::emitLValue`,
establishing parity with classic Clang CodeGen
(`clang/lib/CodeGen/CGExpr.cpp:1825-1830`) and completing AST visitor
coverage across CIR.
In ClangIR, `ConstantExpr` was already supported in `ScalarExprEmitter`
(`CIRGenExprScalar.cpp:147`) and `AggExprEmitter`
(`CIRGenExprAggregate.cpp:344`), but was stubbed with `errorNYI` in
`CIRGenFunction::emitLValue`:
```cpp
case Expr::ConstantExprClass:
getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: ConstantExpr");
return LValue();
```
Classic Clang unwraps and evaluates the subexpression when evaluating an
lvalue. This patch brings `CIRGenFunction::emitLValue` in line with
classic Clang by delegating to
`emitLValue(cast<ConstantExpr>(e)->getSubExpr())`.
Fixes #237403.
Added:
Modified:
clang/lib/CIR/CodeGen/CIRGenFunction.cpp
clang/test/CIR/CodeGenCXX/global-refs.cpp
Removed:
################################################################################
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 04216f2d386f2..5fac7bb17b3d0 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -14,6 +14,7 @@
#include "CIRGenCXXABI.h"
#include "CIRGenCall.h"
+#include "CIRGenConstantEmitter.h"
#include "CIRGenValue.h"
#include "mlir/IR/Location.h"
#include "clang/AST/Attr.h"
@@ -1249,9 +1250,16 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
return LValue();
case Expr::DeclRefExprClass:
return emitDeclRefLValue(cast<DeclRefExpr>(e));
- case Expr::ConstantExprClass:
- getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: ConstantExpr");
- return LValue();
+ case Expr::ConstantExprClass: {
+ const ConstantExpr *ce = cast<ConstantExpr>(e);
+ if (mlir::Attribute result =
+ ConstantEmitter(*this).tryEmitConstantExpr(ce)) {
+ mlir::Value val = builder.getConstant(
+ getLoc(e->getSourceRange()), mlir::cast<mlir::TypedAttr>(result));
+ return makeNaturalAlignPointeeAddrLValue(val, ce->getType());
+ }
+ return emitLValue(ce->getSubExpr());
+ }
case Expr::ParenExprClass:
return emitLValue(cast<ParenExpr>(e)->getSubExpr());
case Expr::GenericSelectionExprClass:
diff --git a/clang/test/CIR/CodeGenCXX/global-refs.cpp b/clang/test/CIR/CodeGenCXX/global-refs.cpp
index 67eee5ef18d39..e2e9d504b7c1b 100644
--- a/clang/test/CIR/CodeGenCXX/global-refs.cpp
+++ b/clang/test/CIR/CodeGenCXX/global-refs.cpp
@@ -1,8 +1,8 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> %t-before.cir
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> %t-before.cir
// RUN: FileCheck %s --input-file=%t-before.cir --check-prefixes=CIR,CIR-BEFORE
// RUN: FileCheck %s --input-file=%t.cir --check-prefixes=CIR,CIR-AFTER
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefixes=LLVM,LLVMCIR
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefixes=LLVM,OGCG
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefixes=LLVM,LLVMCIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefixes=LLVM,OGCG
struct DefCtor{};
struct WithCtor{
@@ -134,6 +134,10 @@ extern WithCtor &ExternRef;
// CIR: cir.global "private" constant external @ExternRef : !cir.ptr<!rec_WithCtor>
// LLVM: @ExternRef = external constant ptr
+static constexpr int val = 10;
+// CIR: cir.global "private" constant internal {{.*}}@_ZL3val = #cir.int<10> : !s32i align(4)
+// LLVM: @_ZL3val = internal constant i32 10, align 4
+
void use() {
// CIR-LABEL: cir.func{{.*}}use
@@ -168,6 +172,21 @@ void use() {
// LLVM-NEXT: %[[EXT_REF:.*]] = load ptr, ptr @ExternRef
// LLVM-NEXT: store ptr %[[EXT_REF]], ptr %[[LOCAL]]
+consteval const int &get_ref(const int &x) {
+ return x;
+}
+
+const int &test_return() {
+ // CIR-LABEL: cir.func {{.*}} @_Z11test_returnv
+ // CIR: %[[VAL_ADDR:.*]] = cir.const #cir.global_view<@_ZL3val> : !cir.ptr<!s32i>
+ // CIR: cir.store{{.*}} %[[VAL_ADDR]], %[[RET_ADDR:.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+ // LLVM-LABEL: define {{.*}} @_Z11test_returnv
+ // LLVMCIR: store ptr @_ZL3val
+ // LLVMCIR: ret ptr
+ // OGCG: ret ptr @_ZL3val
+ 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.
More information about the cfe-commits
mailing list