[clang] [clang] Do not use atomicrmw for compound assignment to a bool enumeration (PR #245856)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 19:03:09 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
Author: Sendao Yan (sendaoYan)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/245856.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2-1)
- (modified) clang/test/CodeGen/enum-bool.c (+5)
``````````diff
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff7..2036ec10b6e72 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 e98ecce22ee8d..ea0ca6581706f 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/245856
More information about the cfe-commits
mailing list