[clang] 93d0c61 - [Clang] Fix atomic boolean compound assignment (#33210) (#180200)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 6 07:46:24 PST 2026
Author: Kiran
Date: 2026-02-06T15:46:19Z
New Revision: 93d0c61d4d391d937e01e038081bdf9a2a08ba19
URL: https://github.com/llvm/llvm-project/commit/93d0c61d4d391d937e01e038081bdf9a2a08ba19
DIFF: https://github.com/llvm/llvm-project/commit/93d0c61d4d391d937e01e038081bdf9a2a08ba19.diff
LOG: [Clang] Fix atomic boolean compound assignment (#33210) (#180200)
This is my second attempt to merge the #33210 fix. My original patch
#178220 caused test failures on risc-v, as I committed a new test file
that was too target-specific. I reverted the PR (#180183) to avoid
causing disruption, as I did not know how long the fix would take.
I have now fixed the test so that it behaves correctly on risc-v. I have
also changed the run lines so it is tested on aarch64, amd64 and risc-v.
Added:
clang/test/CodeGen/compound-assign-atomic-bool.c
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/CodeGen/CGExprScalar.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 02e2034624e4a..613d87668be18 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -223,6 +223,9 @@ Improvements to Coverage Mapping
Bug Fixes in This Version
-------------------------
+
+- Fixed atomic boolean compound assignment; the conversion back to atomic bool would be miscompiled. (#GH33210)
+
- Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters are missing parentheses. (#GH175088)
- Fix lifetime extension of temporaries in for-range-initializers in templates. (#GH165182)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index d21e017bd2b56..1f9389660e127 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4035,9 +4035,14 @@ LValue ScalarExprEmitter::EmitCompoundAssignLValue(
if (LHSLV.isBitField()) {
Previous = Result;
Result = EmitScalarConversion(Result, PromotionTypeCR, LHSTy, Loc);
- } else
+ } else if (const auto *atomicTy = LHSTy->getAs<AtomicType>()) {
+ Result =
+ EmitScalarConversion(Result, PromotionTypeCR, atomicTy->getValueType(),
+ Loc, ScalarConversionOpts(CGF.SanOpts));
+ } else {
Result = EmitScalarConversion(Result, PromotionTypeCR, LHSTy, Loc,
ScalarConversionOpts(CGF.SanOpts));
+ }
if (atomicPHI) {
llvm::BasicBlock *curBlock = Builder.GetInsertBlock();
diff --git a/clang/test/CodeGen/compound-assign-atomic-bool.c b/clang/test/CodeGen/compound-assign-atomic-bool.c
new file mode 100644
index 0000000000000..fd99f09d640ad
--- /dev/null
+++ b/clang/test/CodeGen/compound-assign-atomic-bool.c
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple riscv64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+// When performing compound assignment on atomic_bool, ensure that we
+// correctly handle the conversion from integer to boolean, by comparing
+// with zero rather than truncating.
+
+// CHECK-LABEL: @compund_assign_add
+int compund_assign_add(void) {
+ _Atomic _Bool b;
+
+ b += 2;
+ // CHECK: add
+ // CHECK: icmp ne
+ // CHECK-NOT: trunc
+ // CHECK: {{cmpxchg|call.*__atomic_compare_exchange}}
+ return b;
+}
+
+// CHECK-LABEL: @compund_assign_minus
+int compund_assign_minus(void) {
+ _Atomic _Bool b;
+
+ b -= 2;
+ // CHECK: sub
+ // CHECK: icmp ne
+ // CHECK-NOT: trunc
+ // CHECK: {{cmpxchg|call.*__atomic_compare_exchange}}
+ return b;
+}
\ No newline at end of file
More information about the cfe-commits
mailing list