[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
Sun Dec 6 06:11:19 PST 2026


https://github.com/sendaoYan updated https://github.com/llvm/llvm-project/pull/245930

>From 8ebac3d8eae43ad09113d3b96bdd183216ad7b9a 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 1/3] 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         | 25 ++++--------
 clang/test/CIR/CodeGen/enum-bool.c         | 46 ++++++++++++++++++++++
 5 files changed, 74 insertions(+), 26 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 7e8fbab357a4cf..7de2b1f2930132 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -2927,6 +2927,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 4e4c4a37ecff71..ded4b2fb9782a4 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2587,6 +2587,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();
@@ -2622,11 +2629,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 f56cb196360f5e..db1599aabdf2fa 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);
@@ -2943,7 +2942,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 c9f46686e3edcf..1c94b5a4d14093 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -172,15 +172,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) {
@@ -1649,7 +1640,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
@@ -1685,7 +1676,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);
@@ -1893,7 +1884,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);
@@ -3048,7 +3039,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_FixedPointToBoolean:
     assert(E->getType()->isFixedPointType() &&
            "Expected src type to be fixed point type");
-    assert(hasBoolConversionSemantics(DestTy) &&
+    assert(DestTy->hasBooleanConversionSemantics() &&
            "Expected dest type to be a boolean type");
     return EmitScalarConversion(Visit(E), E->getType(), DestTy,
                                 CE->getExprLoc());
@@ -3361,7 +3352,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())
@@ -3377,7 +3368,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() !=
@@ -3433,7 +3424,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.
@@ -4124,7 +4115,7 @@ LValue ScalarExprEmitter::EmitCompoundAssignLValue(
           llvm::isPowerOf2_64(StoreBits);
     } else {
       CanEmitAtomicRMW =
-          !hasBoolConversionSemantics(AtomicValueTy) &&
+          !AtomicValueTy->hasBooleanConversionSemantics() &&
           AtomicValueTy->isIntegerType() && ResultTy->isIntegerType() &&
           !(AtomicValueTy->isUnsignedIntegerType() &&
             CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
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

>From fa843a69965f2191cba43b14bc215afc638022ad Mon Sep 17 00:00:00 2001
From: Sendao Yan <yansendao at 126.com>
Date: Tue, 1 Dec 2026 22:43:29 +0800
Subject: [PATCH 2/3] Cover the direct fixed-point to bool enum conversion

The input from #245913 is a direct cast from _Accum to the enumeration,
which the test did not exercise yet; the compound-assignment case was
the only fixed-point path covered.
---
 clang/test/CIR/CodeGen/enum-bool.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/clang/test/CIR/CodeGen/enum-bool.c b/clang/test/CIR/CodeGen/enum-bool.c
index e754ad975d287a..b4d309a2bfca2e 100644
--- a/clang/test/CIR/CodeGen/enum-bool.c
+++ b/clang/test/CIR/CodeGen/enum-bool.c
@@ -44,3 +44,9 @@ enum E l(enum E e) { return ++e; }
 enum E m(enum E e) { e++; return e; }
 // CIR-LABEL: cir.func {{.*}} @m(
 // CIR:         cir.const #true
+
+enum E r(_Accum x) { return (enum E)x; }
+// CIR-LABEL: cir.func {{.*}} @r(
+// CIR:         cir.cast int_to_bool
+// LLVM-LABEL: define {{.*}} @r(
+// LLVM:         icmp ne i32

>From 75b4cdf8430d0cafc6447056a510e0b70cc98ff9 Mon Sep 17 00:00:00 2001
From: Sendao Yan <yansendao at 126.com>
Date: Wed, 2 Dec 2026 21:20:30 +0800
Subject: [PATCH 3/3] [clang][CIR] Address review comments

- Drop the redundant isComplete() check in
  Type::hasBooleanConversionSemantics(): a fixed enum always has an
  integer type.
- Use hasBooleanConversionSemantics() for the bool-conversion gates in
  ScalarExprEmitter::emitScalarConversion and
  CIRGenFunction::emitComplexToScalarConversion, matching classic CodeGen.
- In the CK_IntegralToPointer intermediate cast, select bool_to_int from the
  emitted value's type (mlir::isa<cir::BoolType>) instead of the AST type, so
  a bool-backed enum operand is not cast with cir.cast integral on !cir.bool.
- Emit an errorNYI instead of asserting on the integer atomic fast path
  for atomic decrement of a bool-like value, returning a well-typed false
  value so a caller that stores the result does not dereference a null
  mlir::Value; the proper lowering is in flight (#247673).
- Add the ordinary CodeGen run line to the enum-bool test, check the LLVM
  output for every case, and add the b, d, f, i, o and p cases.
- Add a -verify test covering the atomic decrement NYI diagnostic.
---
 clang/lib/AST/Type.cpp                     |  3 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 18 ++++++--
 clang/test/CIR/CodeGen/enum-bool-nyi.c     | 24 +++++++++++
 clang/test/CIR/CodeGen/enum-bool.c         | 49 +++++++++++++++++++++-
 4 files changed, 86 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/enum-bool-nyi.c

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index ded4b2fb9782a4..918c4babc6b60a 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2589,8 +2589,7 @@ bool Type::isArithmeticType() const {
 
 bool Type::hasBooleanConversionSemantics() const {
   if (const auto *ED = getAsEnumDecl())
-    return ED->isComplete() && ED->isFixed() &&
-           ED->getIntegerType()->isBooleanType();
+    return ED->isFixed() && ED->getIntegerType()->isBooleanType();
   return isBooleanType();
 }
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index db1599aabdf2fa..daef8bb7605aea 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -673,6 +673,16 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
                                   builder.getBoolTy());
       }
 
+      // Decrement of a bool-like value is not implemented yet.
+      // See https://github.com/llvm/llvm-project/issues/247673.
+      if (!isInc && valType->hasBooleanConversionSemantics()) {
+        cgf.cgm.errorNYI(e->getSourceRange(),
+                         "atomic decrement of a bool-like value");
+        // Return a well-typed value so that callers which store the result do
+        // not dereference a null mlir::Value after the diagnostic.
+        return builder.getFalse(loc);
+      }
+
       // 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.
@@ -1107,7 +1117,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
 
     // Handle conversions to bool first, they are special: comparisons against
     // 0.
-    if (dstType->isBooleanType())
+    if (dstType->hasBooleanConversionSemantics())
       return emitConversionToBool(src, srcType, cgf.getLoc(loc));
 
     mlir::Type mlirDstType = cgf.convertType(dstType);
@@ -2787,8 +2797,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
     // correct CIR conversion.
     mlir::Type middleTy = cgf.cgm.getDataLayout().getIntPtrType(destCIRTy);
     mlir::Value middleVal = builder.createCast(
-        subExpr->getType()->isBooleanType() ? cir::CastKind::bool_to_int
-                                            : cir::CastKind::integral,
+        mlir::isa<cir::BoolType>(src.getType()) ? cir::CastKind::bool_to_int
+                                                : cir::CastKind::integral,
         src, middleTy);
 
     if (cgf.cgm.getCodeGenOpts().StrictVTablePointers) {
@@ -3200,7 +3210,7 @@ mlir::Value CIRGenFunction::emitComplexToScalarConversion(mlir::Value src,
          "Invalid complex -> scalar conversion");
 
   QualType complexElemTy = srcTy->castAs<ComplexType>()->getElementType();
-  if (dstTy->isBooleanType()) {
+  if (dstTy->hasBooleanConversionSemantics()) {
     auto kind = complexElemTy->isFloatingType()
                     ? cir::CastKind::float_complex_to_bool
                     : cir::CastKind::int_complex_to_bool;
diff --git a/clang/test/CIR/CodeGen/enum-bool-nyi.c b/clang/test/CIR/CodeGen/enum-bool-nyi.c
new file mode 100644
index 00000000000000..78ba338b0911e1
--- /dev/null
+++ b/clang/test/CIR/CodeGen/enum-bool-nyi.c
@@ -0,0 +1,24 @@
+// Atomic decrement of a bool-like value is not lowered yet: it must report an
+// NYI diagnostic instead of building an atomic operation whose operand
+// constraint excludes !cir.bool. The proper lowering is tracked in
+// https://github.com/llvm/llvm-project/issues/247673.
+
+enum E : bool { Zero, One };
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -fclangir -emit-cir -DPRE %s -verify -o /dev/null
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -fclangir -emit-cir -DPOST %s -verify -o /dev/null
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -fclangir -emit-cir -DASSIGN %s -verify -o /dev/null
+
+#ifdef PRE
+void pre(_Atomic(enum E) *p) { --*p; } // expected-error {{atomic decrement of a bool-like value}}
+#endif
+
+#ifdef POST
+void post(_Atomic(_Bool) *p) { (*p)--; } // expected-error {{atomic decrement of a bool-like value}}
+#endif
+
+#ifdef ASSIGN
+// The rejected decrement's result still flows into a store; codegen must not
+// crash (dereference a null mlir::Value) after the diagnostic.
+void assign(_Atomic(enum E) *p) { enum E x; x = (*p)--; } // expected-error {{atomic decrement of a bool-like value}}
+#endif
diff --git a/clang/test/CIR/CodeGen/enum-bool.c b/clang/test/CIR/CodeGen/enum-bool.c
index b4d309a2bfca2e..92e6ae3b19e8d7 100644
--- a/clang/test/CIR/CodeGen/enum-bool.c
+++ b/clang/test/CIR/CodeGen/enum-bool.c
@@ -2,6 +2,8 @@
 // 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
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -ffixed-point -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t.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
@@ -15,12 +17,30 @@ enum E a(int x) { return (enum E)x; }
 // LLVM-LABEL: define {{.*}} @a(
 // LLVM:         icmp ne i32
 
+enum E b(int x) { return x; }
+// CIR-LABEL: cir.func {{.*}} @b(
+// CIR:         cir.cast int_to_bool
+// LLVM-LABEL: define {{.*}} @b(
+// 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 d(double x) { enum E e = x; return e; }
+// CIR-LABEL: cir.func {{.*}} @d(
+// CIR:         cir.cast float_to_bool
+// LLVM-LABEL: define {{.*}} @d(
+// LLVM:         fcmp une double
+
+enum E f(long long x) { enum E e; e = x; return e; }
+// CIR-LABEL: cir.func {{.*}} @f(
+// CIR:         cir.cast int_to_bool
+// LLVM-LABEL: define {{.*}} @f(
+// LLVM:         icmp ne i64
+
 enum E g(double _Complex x) { return (enum E)x; }
 // CIR-LABEL: cir.func {{.*}} @g(
 // CIR-COUNT-2: cir.cast float_to_bool
@@ -32,21 +52,46 @@ 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(
+// LLVM:         icmp ne i32
+
+enum E i(enum E e, double x) { e += x; return e; }
+// CIR-LABEL: cir.func {{.*}} @i(
+// CIR:         cir.cast float_to_bool
+// LLVM-LABEL: define {{.*}} @i(
+// LLVM:         fcmp une double
 
 enum E k(enum E e, _Accum x) { e += x; return e; }
 // CIR-LABEL: cir.func {{.*}} @k(
-// CIR:         cir.cast int_to_bool
+// CIR:         cir.cast int_to_bool %{{.*}} : !s32i -> !cir.bool
+// LLVM-LABEL: define {{.*}} @k(
+// LLVM:         icmp ne i32
 
 enum E l(enum E e) { return ++e; }
 // CIR-LABEL: cir.func {{.*}} @l(
 // CIR:         cir.const #true
+// LLVM-LABEL: define {{.*}} @l(
+// LLVM:         store i8 1
 
 enum E m(enum E e) { e++; return e; }
 // CIR-LABEL: cir.func {{.*}} @m(
 // CIR:         cir.const #true
+// LLVM-LABEL: define {{.*}} @m(
+// LLVM:         store i8 1
+
+void o(_Atomic(enum E) *p) { ++*p; }
+// CIR-LABEL: cir.func {{.*}} @o(
+// CIR:         cir.store {{.*}}atomic(seq_cst)
+// LLVM-LABEL: define {{.*}} void @o(
+// LLVM:         store atomic i8 1, ptr %{{.*}} seq_cst
+
+void *p(enum E e) { return (void *)e; }
+// CIR-LABEL: cir.func {{.*}} @p(
+// CIR:         cir.cast bool_to_int %{{.*}} : !cir.bool -> !u64i
+// LLVM-LABEL: define {{.*}} ptr @p(
+// LLVM:         zext i1
 
 enum E r(_Accum x) { return (enum E)x; }
 // CIR-LABEL: cir.func {{.*}} @r(
-// CIR:         cir.cast int_to_bool
+// CIR:         cir.cast int_to_bool %{{.*}} : !s32i -> !cir.bool
 // LLVM-LABEL: define {{.*}} @r(
 // LLVM:         icmp ne i32



More information about the cfe-commits mailing list