[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:18:11 PST 2026
https://github.com/sendaoYan updated https://github.com/llvm/llvm-project/pull/245856
>From 918f4013d072600ece27de3acfc3af48f5527f84 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