[clang] [CIR] Add support for TypeTraitExpr with bool result (PR #171687)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 10 12:00:05 PST 2025
https://github.com/AmrDeveloper created https://github.com/llvm/llvm-project/pull/171687
Add support for the TypeTraitExpr with a boolean result
>From 4446c7a80e85d0285386e9dddf1605375d4a6c0d Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Wed, 10 Dec 2025 20:37:22 +0100
Subject: [PATCH] [CIR] Add support for TypeTraitExpr with bool result
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 6 +++-
clang/test/CIR/CodeGen/cxx-traits.cpp | 39 ++++++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 3887433e5e181..392d4a1ee25e5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -840,7 +840,11 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
return {};
}
mlir::Value VisitTypeTraitExpr(const TypeTraitExpr *e) {
- cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: type trait");
+ mlir::Location loc = cgf.getLoc(e->getExprLoc());
+ if (e->isStoredAsBoolean())
+ return builder.getBool(e->getBoolValue(), loc);
+ cgf.cgm.errorNYI(e->getSourceRange(),
+ "ScalarExprEmitter: TypeTraitExpr stored as int");
return {};
}
mlir::Value
diff --git a/clang/test/CIR/CodeGen/cxx-traits.cpp b/clang/test/CIR/CodeGen/cxx-traits.cpp
index cf44f775d0842..ddbdfaeccae2c 100644
--- a/clang/test/CIR/CodeGen/cxx-traits.cpp
+++ b/clang/test/CIR/CodeGen/cxx-traits.cpp
@@ -18,3 +18,42 @@ void expression_trait_expr() {
// OGCG: %[[A_ADDR:.*]] = alloca i8, align 1
// OGCG: store i8 0, ptr %[[A_ADDR]], align 1
+
+void type_trait_expr() {
+ enum E {};
+ bool a = __is_enum(E);
+ bool b = __is_same(int, float);
+ bool c = __is_constructible(int, int, int, int);
+ bool d = __is_array(int);
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["a", init]
+// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b", init]
+// CIR: %[[C_ADDR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["c", init]
+// CIR: %[[D_ADDR:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["d", init]
+// CIR: %[[CONST_TRUE:.*]] = cir.const #true
+// CIR: cir.store {{.*}} %[[CONST_TRUE]], %[[A_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CIR: %[[CONST_FALSE:.*]] = cir.const #false
+// CIR: cir.store {{.*}} %[[CONST_FALSE]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CIR: %[[CONST_FALSE:.*]] = cir.const #false
+// CIR: cir.store {{.*}} %[[CONST_FALSE]], %[[C_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CIR: %[[CONST_FALSE:.*]] = cir.const #false
+// CIR: cir.store {{.*}} %[[CONST_FALSE]], %[[D_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+
+// LLVM: %[[A_ADDR:.*]] = alloca i8, i64 1, align 1
+// LLVM: %[[B_ADDR:.*]] = alloca i8, i64 1, align 1
+// LLVM: %[[C_ADDR:.*]] = alloca i8, i64 1, align 1
+// LLVM: %[[D_ADDR:.*]] = alloca i8, i64 1, align 1
+// LLVM: store i8 1, ptr %[[A_ADDR]], align 1
+// LLVM: store i8 0, ptr %[[B_ADDR]], align 1
+// LLVM: store i8 0, ptr %[[C_ADDR]], align 1
+// LLVM: store i8 0, ptr %[[D_ADDR]], align 1
+
+// OGCG: %[[A_ADDR:.*]] = alloca i8, align 1
+// OGCG: %[[B_ADDR:.*]] = alloca i8, align 1
+// OGCG: %[[C_ADDR:.*]] = alloca i8, align 1
+// OGCG: %[[D_ADDR:.*]] = alloca i8, align 1
+// OGCG: store i8 1, ptr %[[A_ADDR]], align 1
+// OGCG: store i8 0, ptr %[[B_ADDR]], align 1
+// OGCG: store i8 0, ptr %[[C_ADDR]], align 1
+// OGCG: store i8 0, ptr %[[D_ADDR]], align 1
More information about the cfe-commits
mailing list