[clang] [clang][CIR] Convert fixed-point values to bool enums like bool (PR #245930)
Sendao Yan via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 02:52:33 PST 2026
https://github.com/sendaoYan updated https://github.com/llvm/llvm-project/pull/245930
>From bf95ff5d7b83ea631afa347b69bf034876ff2da0 Mon Sep 17 00:00:00 2001
From: Sendao Yan <yansendao at 126.com>
Date: Wed, 25 Nov 2026 16:33:57 +0800
Subject: [PATCH] [clang][CIR] Treat enumerations with a bool underlying type
like bool
Since #244494, Type::getScalarTypeKind() returns STK_Bool for an
enumeration with a fixed bool underlying type, so PrepareScalarCast()
selects the boolean cast kinds. The CIR backend still tested
isBooleanType() directly, so for such an enumeration
- VisitCastExpr() asserted on CK_FixedPointToBoolean and crashed an
assertions build,
- emitScalarConversion() truncated a fixed-point value to one bit
instead of comparing it against zero,
- emitScalarPrePostIncDec() emitted cir.inc on a !cir.bool value for
++, which the verifier rejects.
Introduce Type::hasBooleanConversionSemantics() next to
hasBooleanRepresentation(), use it from getScalarTypeKind() and the CIR
scalar emitter, and replace the local helper in classic CodeGen with it
so there is a single helper.
Fixes https://github.com/llvm/llvm-project/issues/245913
---
clang/include/clang/AST/TypeBase.h | 5 +++
clang/lib/AST/Type.cpp | 12 ++++--
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 12 +++---
clang/lib/CodeGen/CGExprScalar.cpp | 21 +++-------
clang/test/CIR/CodeGen/enum-bool.c | 46 ++++++++++++++++++++++
5 files changed, 72 insertions(+), 24 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/enum-bool.c
diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index dc376530f1506a..fb3d9aa4a35fe8 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -2926,6 +2926,11 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
/// vector of booleans.
bool hasBooleanRepresentation() const;
+ /// Determine whether conversion to this type has the semantics of conversion
+ /// to bool: the type is bool, or an enumeration with a fixed underlying type
+ /// of bool (C23 6.7.3.3p16).
+ bool hasBooleanConversionSemantics() const;
+
// Type Checking Functions: Check to see if this type is structurally the
// specified type, ignoring typedefs and qualifiers, and return a pointer to
// the best type we can.
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 349b818e419b4f..ed99bb1eb11acb 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2571,6 +2571,13 @@ bool Type::isArithmeticType() const {
return isa<ComplexType>(CanonicalType) || isBitIntType();
}
+bool Type::hasBooleanConversionSemantics() const {
+ if (const auto *ED = getAsEnumDecl())
+ return ED->isComplete() && ED->isFixed() &&
+ ED->getIntegerType()->isBooleanType();
+ return isBooleanType();
+}
+
bool Type::hasBooleanRepresentation() const {
if (const auto *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isBooleanType();
@@ -2606,11 +2613,10 @@ Type::ScalarTypeKind Type::getScalarTypeKind() const {
} else if (isa<MemberPointerType>(T)) {
return STK_MemberPointer;
} else if (isa<EnumType>(T)) {
- const EnumDecl *ED = T->castAsEnumDecl();
- assert(ED->isComplete());
+ assert(T->castAsEnumDecl()->isComplete());
// C23 6.7.3.3p16: conversion to an enumerated type with a fixed underlying
// type has the same semantics as conversion to the underlying type.
- if (ED->isFixed() && ED->getIntegerType()->isBooleanType())
+ if (hasBooleanConversionSemantics())
return STK_Bool;
return STK_Integral;
} else if (const auto *CT = dyn_cast<ComplexType>(T)) {
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index c49459c5020fbe..a6057d0e491965 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -647,9 +647,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
bool isInc = e->isIncrementOp();
bool isPre = e->isPrefix();
- // Bools are always set-to-true, as decrement isn't legal on bools.
- if (valType->isBooleanType()) {
- assert(isInc);
+ // Increment of a bool-like value is always true.
+ if (isInc && valType->hasBooleanConversionSemantics()) {
// Atomic operations require an integer type; reinterpret the bool
// pointer as a pointer to its underlying storage integer type.
cir::IntType intTy = builder.getBoolMemoryTy();
@@ -742,7 +741,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
// -> bool = ((int)bool + 1 != 0)
// An interesting aspect of this is that increment is always true.
// Decrement does not have this property.
- if (e->isIncrementOp() && type->isBooleanType()) {
+ if (e->isIncrementOp() && type->hasBooleanConversionSemantics()) {
value = builder.getTrue(cgf.getLoc(e->getExprLoc()));
} else if (type->isIntegerType()) {
QualType promotedType;
@@ -1069,7 +1068,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
// integers, we do not want to follow any logic that assumes they should be
// treated as integers.
if (srcType->isFixedPointType()) {
- if (dstType->isBooleanType()) {
+ if (dstType->hasBooleanConversionSemantics()) {
cir::BoolType boolTy = builder.getBoolTy();
return cir::CastOp::create(builder, cgf.getLoc(loc), boolTy,
cir::CastKind::int_to_bool, src);
@@ -2942,7 +2941,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
case CK_FixedPointToBoolean:
assert(subExpr->getType()->isFixedPointType() &&
"Expected src type to be fixed point type");
- assert(destTy->isBooleanType() && "Expected dest type to be boolean type");
+ assert(destTy->hasBooleanConversionSemantics() &&
+ "Expected dest type to be boolean type");
return emitScalarConversion(Visit(subExpr), subExpr->getType(), destTy,
ce->getExprLoc());
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff79..8b6d266409fabd 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -176,15 +176,6 @@ static bool MustVisitNullValue(const Expr *E) {
return E->getType()->isNullPtrType();
}
-/// Return true if conversion to \p T has the semantics of conversion to bool:
-/// \p T is bool or an enumeration with a fixed underlying type of bool
-/// (C23 6.7.3.3p16).
-static bool hasBoolConversionSemantics(QualType T) {
- if (const auto *ED = T->getAsEnumDecl())
- return ED->isFixed() && ED->getIntegerType()->isBooleanType();
- return T->isBooleanType();
-}
-
/// If \p E is a widened promoted integer, get its base (unpromoted) type.
static std::optional<QualType> getUnwidenedIntegerType(const ASTContext &Ctx,
const Expr *E) {
@@ -1653,7 +1644,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
// TODO(leonardchan): When necessary, add another if statement checking for
// conversions to fixed point types from other types.
if (SrcType->isFixedPointType()) {
- if (hasBoolConversionSemantics(DstType))
+ if (DstType->hasBooleanConversionSemantics())
// It is important that we check this before checking if the dest type is
// an integer because booleans are technically integer types.
// We do not need to check the padding bit on unsigned types if unsigned
@@ -1689,7 +1680,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
llvm::Type *SrcTy = Src->getType();
// Handle conversions to bool first, they are special: comparisons against 0.
- if (hasBoolConversionSemantics(DstType))
+ if (DstType->hasBooleanConversionSemantics())
return EmitConversionToBool(Src, SrcType);
llvm::Type *DstTy = ConvertType(DstType);
@@ -1897,7 +1888,7 @@ Value *ScalarExprEmitter::EmitComplexToScalarConversion(
SrcTy = SrcTy->castAs<ComplexType>()->getElementType();
// Handle conversions to bool first, they are special: comparisons against 0.
- if (hasBoolConversionSemantics(DstTy)) {
+ if (DstTy->hasBooleanConversionSemantics()) {
// Complex != 0 -> (Real != 0) | (Imag != 0)
Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy, Loc);
Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy, Loc);
@@ -3363,7 +3354,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
type = atomicTy->getValueType();
- if (isInc && hasBoolConversionSemantics(type)) {
+ if (isInc && type->hasBooleanConversionSemantics()) {
llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type);
if (isPre) {
Builder.CreateStore(True, LV.getAddress(), LV.isVolatileQualified())
@@ -3379,7 +3370,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
// Special case for atomic increment / decrement on integers, emit
// atomicrmw instructions. We skip this if we want to be doing overflow
// checking, and fall into the slow path with the atomic cmpxchg loop.
- if (!hasBoolConversionSemantics(type) && type->isIntegerType() &&
+ if (!type->hasBooleanConversionSemantics() && type->isIntegerType() &&
!(type->isUnsignedIntegerType() &&
CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
CGF.getLangOpts().getSignedOverflowBehavior() !=
@@ -3435,7 +3426,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
// -> bool = ((int)bool + 1 != 0)
// An interesting aspect of this is that increment is always true.
// Decrement does not have this property.
- if (isInc && hasBoolConversionSemantics(type)) {
+ if (isInc && type->hasBooleanConversionSemantics()) {
value = Builder.getTrue();
// Most common case by far: integer increment.
diff --git a/clang/test/CIR/CodeGen/enum-bool.c b/clang/test/CIR/CodeGen/enum-bool.c
new file mode 100644
index 00000000000000..e754ad975d287a
--- /dev/null
+++ b/clang/test/CIR/CodeGen/enum-bool.c
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -ffixed-point -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -ffixed-point -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
+
+// C23 6.7.3.3p16: conversion to an enumeration with a fixed bool underlying
+// type has the semantics of conversion to bool. Mirrors the cases in
+// clang/test/CodeGen/enum-bool.c, restricted to what CIR can lower today.
+
+enum E : bool { Zero, One };
+
+enum E a(int x) { return (enum E)x; }
+// CIR-LABEL: cir.func {{.*}} @a(
+// CIR: cir.cast int_to_bool
+// LLVM-LABEL: define {{.*}} @a(
+// LLVM: icmp ne i32
+
+enum E c(double x) { return (enum E)x; }
+// CIR-LABEL: cir.func {{.*}} @c(
+// CIR: cir.cast float_to_bool
+// LLVM-LABEL: define {{.*}} @c(
+// LLVM: fcmp une double
+
+enum E g(double _Complex x) { return (enum E)x; }
+// CIR-LABEL: cir.func {{.*}} @g(
+// CIR-COUNT-2: cir.cast float_to_bool
+// CIR: cir.const #true
+// LLVM-LABEL: define {{.*}} @g(
+// LLVM-COUNT-2: fcmp une double
+
+enum E h(enum E e, int x) { e += x; return e; }
+// CIR-LABEL: cir.func {{.*}} @h(
+// CIR: cir.cast int_to_bool
+// LLVM-LABEL: define {{.*}} @h(
+
+enum E k(enum E e, _Accum x) { e += x; return e; }
+// CIR-LABEL: cir.func {{.*}} @k(
+// CIR: cir.cast int_to_bool
+
+enum E l(enum E e) { return ++e; }
+// CIR-LABEL: cir.func {{.*}} @l(
+// CIR: cir.const #true
+
+enum E m(enum E e) { e++; return e; }
+// CIR-LABEL: cir.func {{.*}} @m(
+// CIR: cir.const #true
More information about the cfe-commits
mailing list