[clang] [clang] Do not use atomicrmw for compound assignment to a bool enumeration (PR #245856)

Sendao Yan via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 19:02:33 PST 2026


https://github.com/sendaoYan created https://github.com/llvm/llvm-project/pull/245856

Fixes #245854.

`EmitCompoundAssignLValue()` allows `atomicrmw` for integer-like types that are
not `_Bool`, so an enumeration with a fixed bool underlying type is updated as a
plain integer and never renormalized to the bool representation:

```c
    _Atomic enum E : bool { F, T } b;
    b += 2;   /* gives 0 at -O2, expected 1 (both are 1 at -O0) */
```

The IR is `atomicrmw add i8 2`, which leaves the object representation as 2.
`b |= 2` is affected in the same way.

Use `hasBoolConversionSemantics()`, like the other sites changed in #244494, so
such objects take the cmpxchg path and the result goes back through
`EmitScalarConversion()`.

Tests: new case in `clang/test/CodeGen/enum-bool.c` checking that no
`atomicrmw` is emitted and `cmpxchg` is used instead.

>From 97342d55ca5927b22e0a318dd13b8a630bab1fd8 Mon Sep 17 00:00:00 2001
From: Sendao Yan <yansendao at 126.com>
Date: Wed, 25 Nov 2026 10:23:34 +0800
Subject: [PATCH] [clang] Do not use atomicrmw for compound assignment to a
 bool enumeration

CanEmitAtomicRMW in EmitCompoundAssignLValue() excludes _Bool but not an
enumeration with a fixed bool underlying type, so

  _Atomic enum E : bool { F, T } b;
  b += 2;

was lowered to atomicrmw add i8 2 and skipped the renormalization to the
bool representation; at -O2 this produced 0 instead of 1.

Use hasBoolConversionSemantics() so such enumerations take the cmpxchg
path, exactly like _Bool.
---
 clang/lib/CodeGen/CGExprScalar.cpp | 3 ++-
 clang/test/CodeGen/enum-bool.c     | 5 +++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff79..2036ec10b6e72c 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4126,7 +4126,8 @@ LValue ScalarExprEmitter::EmitCompoundAssignLValue(
           llvm::isPowerOf2_64(StoreBits);
     } else {
       CanEmitAtomicRMW =
-          !AtomicValueTy->isBooleanType() && AtomicValueTy->isIntegerType() &&
+          !hasBoolConversionSemantics(AtomicValueTy) &&
+          AtomicValueTy->isIntegerType() &&
           ResultTy->isIntegerType() &&
           !(AtomicValueTy->isUnsignedIntegerType() &&
             CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
index e98ecce22ee8df..ea0ca6581706fd 100644
--- a/clang/test/CodeGen/enum-bool.c
+++ b/clang/test/CodeGen/enum-bool.c
@@ -90,3 +90,8 @@ void o(_Atomic(enum E) *p) { ++*p; }
 // CHECK-LABEL: define{{.*}} void @o(
 // CHECK-NOT: atomicrmw add
 // CHECK: store atomic i8 1, ptr %{{.*}} seq_cst
+
+void q(_Atomic(enum E) *p) { *p += 2; }
+// CHECK-LABEL: define{{.*}} void @q(
+// CHECK-NOT: atomicrmw
+// CHECK: cmpxchg



More information about the cfe-commits mailing list