[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
Wed Nov 25 16:27:15 PST 2026
https://github.com/sendaoYan updated https://github.com/llvm/llvm-project/pull/245856
>From 228ea05f8a11732afb8002e5c4ae339cb9998b5e 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 | 4 ++--
clang/test/CodeGen/enum-bool.c | 5 +++++
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 768e01deb9ceb3..ad20d83ae29fcb 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4127,8 +4127,8 @@ LValue ScalarExprEmitter::EmitCompoundAssignLValue(
llvm::isPowerOf2_64(StoreBits);
} else {
CanEmitAtomicRMW =
- !AtomicValueTy->isBooleanType() && AtomicValueTy->isIntegerType() &&
- ResultTy->isIntegerType() &&
+ !hasBoolConversionSemantics(AtomicValueTy) &&
+ AtomicValueTy->isIntegerType() && ResultTy->isIntegerType() &&
!(AtomicValueTy->isUnsignedIntegerType() &&
CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
CGF.getLangOpts().getSignedOverflowBehavior() !=
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
index 872834d573f1bb..2405f378efff33 100644
--- a/clang/test/CodeGen/enum-bool.c
+++ b/clang/test/CodeGen/enum-bool.c
@@ -95,3 +95,8 @@ enum E r(_Accum x) { return (enum E)x; }
// CHECK-LABEL: define{{.*}} zeroext i1 @r(
// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
// CHECK: ret i1 %tobool
+
+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