-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[CIR] Implement part of ConstantExpr support for ScalarExpr #173009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AmrDeveloper
wants to merge
1
commit into
llvm:main
Choose a base branch
from
AmrDeveloper:cir_scalar_constant_expr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+28
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clangir Author: Amr Hesham (AmrDeveloper) ChangesImplement part of the ConstantExpr support for the ScalarExpr Full diff: https://github.com/llvm/llvm-project/pull/173009.diff 2 Files Affected:
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index dbd67b367ec4c..cc4048b5aa021 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -141,6 +141,23 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
}
mlir::Value VisitConstantExpr(ConstantExpr *e) {
+ // A constant expression of type 'void' generates no code and produces no
+ // value.
+ if (e->getType()->isVoidType()) {
+ return {};
+ }
+
+ if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e)) {
+ if (e->isGLValue()) {
+ cgf.cgm.errorNYI(e->getSourceRange(),
+ "ScalarExprEmitter: constant expr GL Value");
+ return {};
+ }
+
+ return builder.getConstant(cgf.getLoc(e->getSourceRange()),
+ mlir::cast<mlir::TypedAttr>(result));
+ }
+
cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: constant expr");
return {};
}
diff --git a/clang/test/CIR/CodeGen/constant-expr.cpp b/clang/test/CIR/CodeGen/constant-expr.cpp
new file mode 100644
index 0000000000000..e3325782b6fa3
--- /dev/null
+++ b/clang/test/CIR/CodeGen/constant-expr.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct StructWithConstEval {
+ consteval int consteval_ret_int() { return 1; }
+ consteval void consteval_ret_void() {}
+};
+
+void calling_consteval_methods() {
+ StructWithConstEval a;
+ int b = a.consteval_ret_int();
+ a.consteval_ret_void();
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca !rec_StructWithConstEval, !cir.ptr<!rec_StructWithConstEval>, ["a"]
+// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
+// CIR: %[[CONST_1:.*]] = cir.const #cir.int<1> : !s32i
+// CIR: cir.store {{.*}} %[[CONST_1]], %[[B_ADDR]] : !s32i, !cir.ptr<!s32i>
+
+// LLVM: %[[A_ADDR:.*]] = alloca %struct.StructWithConstEval, i64 1, align 1
+// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
+// LLVM: store i32 1, ptr %[[B_ADDR]], align 4
+
+// OGCG: %[[A_ADDR:.*]] = alloca %struct.StructWithConstEval, align 1
+// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
+// OGCG: store i32 1, ptr %[[B_ADDR]], align 4
|
Member
|
@llvm/pr-subscribers-clang Author: Amr Hesham (AmrDeveloper) ChangesImplement part of the ConstantExpr support for the ScalarExpr Full diff: https://github.com/llvm/llvm-project/pull/173009.diff 2 Files Affected:
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index dbd67b367ec4c..cc4048b5aa021 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -141,6 +141,23 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
}
mlir::Value VisitConstantExpr(ConstantExpr *e) {
+ // A constant expression of type 'void' generates no code and produces no
+ // value.
+ if (e->getType()->isVoidType()) {
+ return {};
+ }
+
+ if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e)) {
+ if (e->isGLValue()) {
+ cgf.cgm.errorNYI(e->getSourceRange(),
+ "ScalarExprEmitter: constant expr GL Value");
+ return {};
+ }
+
+ return builder.getConstant(cgf.getLoc(e->getSourceRange()),
+ mlir::cast<mlir::TypedAttr>(result));
+ }
+
cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: constant expr");
return {};
}
diff --git a/clang/test/CIR/CodeGen/constant-expr.cpp b/clang/test/CIR/CodeGen/constant-expr.cpp
new file mode 100644
index 0000000000000..e3325782b6fa3
--- /dev/null
+++ b/clang/test/CIR/CodeGen/constant-expr.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct StructWithConstEval {
+ consteval int consteval_ret_int() { return 1; }
+ consteval void consteval_ret_void() {}
+};
+
+void calling_consteval_methods() {
+ StructWithConstEval a;
+ int b = a.consteval_ret_int();
+ a.consteval_ret_void();
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca !rec_StructWithConstEval, !cir.ptr<!rec_StructWithConstEval>, ["a"]
+// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
+// CIR: %[[CONST_1:.*]] = cir.const #cir.int<1> : !s32i
+// CIR: cir.store {{.*}} %[[CONST_1]], %[[B_ADDR]] : !s32i, !cir.ptr<!s32i>
+
+// LLVM: %[[A_ADDR:.*]] = alloca %struct.StructWithConstEval, i64 1, align 1
+// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
+// LLVM: store i32 1, ptr %[[B_ADDR]], align 4
+
+// OGCG: %[[A_ADDR:.*]] = alloca %struct.StructWithConstEval, align 1
+// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
+// OGCG: store i32 1, ptr %[[B_ADDR]], align 4
|
ojhunt
reviewed
Dec 21, 2025
60d23bf to
622af6e
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implement part of the ConstantExpr support for the ScalarExpr