[clang] [CIR] Add support for TypeTraitExpr with bool result (PR #171687)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 10 13:17:03 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Amr Hesham (AmrDeveloper)
<details>
<summary>Changes</summary>
Add support for the TypeTraitExpr with a boolean result
---
Full diff: https://github.com/llvm/llvm-project/pull/171687.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+5-1)
- (modified) clang/test/CIR/CodeGen/cxx-traits.cpp (+39)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/171687
More information about the cfe-commits
mailing list