https://github.com/mmha created https://github.com/llvm/llvm-project/pull/245035

ClangIR gave `s->bf op= x` the untruncated result of the operation, not the 
value that the bit-field holds after the assignment. For `unsigned b : 1` 
holding 1, `(b |= 7)` evaluated to 7 instead of 1.

The code stored through the bit-field but dropped the truncated value that 
`emitStoreThroughBitfieldLValue` returns. Plain assignment and classic codegen 
use that value. Use it for compound assignment too.

This covers compound assignments computed in a scalar type. With a complex 
operand, ClangIR still fails earlier, on another path; that is an older, 
separate problem.

Found by the Plum Hall C validation suite, whose `|=` and `^=` tests failed at 
run time with `-fclangir`.

Assisted-By: Claude Opus 5.5 + GPT-6.2 Sol as reviewer

>From 466e0ce7cad0aa045a1b0c76e27fd4392054041f Mon Sep 17 00:00:00 2001
From: Morris Hafner <[email protected]>
Date: Wed, 18 Nov 2026 03:30:27 +0000
Subject: [PATCH] [CIR] Fix the value of a compound assignment to a bit-field

ClangIR gave `s->bf op= x` the untruncated result of the operation, not
the value that the bit-field holds after the assignment. For
`unsigned b : 1` holding 1, `(b |= 7)` evaluated to 7 instead of 1.

C11 6.5.16p3 (N1570): an assignment expression has the value of the
left operand after the assignment.

The code stored through the bit-field but dropped the truncated value
that emitStoreThroughBitfieldLValue returns. Plain assignment and
classic codegen use that value. Use it for compound assignment too.

This covers compound assignments computed in a scalar type. With a
complex operand, ClangIR still fails earlier, on another path; that is
an older, separate problem.

Found by the Plum Hall C validation suite, whose |= and ^= tests failed
at run time with -fclangir.
---
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp |  2 +-
 clang/test/CIR/CodeGen/bitfields.c         | 31 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 40bdd49b5d70a7..c49459c5020fbe 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1790,7 +1790,7 @@ LValue ScalarExprEmitter::emitCompoundAssignLValue(
   // 'An assignment expression has the value of the left operand after the
   // assignment...'.
   if (lhsLV.isBitField())
-    cgf.emitStoreThroughBitfieldLValue(RValue::get(result), lhsLV);
+    result = cgf.emitStoreThroughBitfieldLValue(RValue::get(result), lhsLV);
   else
     cgf.emitStoreThroughLValue(RValue::get(result), lhsLV);
 
diff --git a/clang/test/CIR/CodeGen/bitfields.c 
b/clang/test/CIR/CodeGen/bitfields.c
index 69766178c46132..3d5f685e373ba0 100644
--- a/clang/test/CIR/CodeGen/bitfields.c
+++ b/clang/test/CIR/CodeGen/bitfields.c
@@ -374,3 +374,34 @@ void binOp(S* s) {
 // OGCG:   [[TMP12:%.*]] = shl i64 [[TMP8]], 62
 // OGCG:   [[TMP13:%.*]] = ashr i64 [[TMP12]], 62
 // OGCG:   [[TMP14:%.*]] = trunc i64 [[TMP13]] to i32
+
+int binOpValue(S* s) {
+  return s->d |= 42;
+}
+
+// CIR: cir.func {{.*@binOpValue}}
+// CIR:   [[RETVAL:%.*]] = cir.alloca "" align(4) return_slot : !cir.ptr<!s32i>
+// CIR:   [[OR:%.*]] = cir.or {{.*}} : !s32i
+// CIR:   [[NEW:%.*]] = cir.set_bitfield align(4) (#bfi_d, {{.*}} : 
!cir.ptr<!u64i>, [[OR]] : !s32i) -> !s32i
+// CIR:   cir.store [[NEW]], [[RETVAL]] : !s32i, !cir.ptr<!s32i>
+
+// LLVM: define {{.*@binOpValue}}
+// LLVM:   [[RETVAL:%.*]] = alloca i32, align 4
+// LLVM:   [[OR:%.*]] = or i32 {{.*}}, 42
+// LLVM:   [[EXT:%.*]] = zext i32 [[OR]] to i64
+// LLVM:   [[BITS:%.*]] = and i64 [[EXT]], 3
+// LLVM:   store i64 {{.*}}, ptr {{.*}}, align 4
+// LLVM:   [[SHL:%.*]] = shl i64 [[BITS]], 62
+// LLVM:   [[ASHR:%.*]] = ashr i64 [[SHL]], 62
+// LLVM:   [[NEW:%.*]] = trunc i64 [[ASHR]] to i32
+// LLVM:   store i32 [[NEW]], ptr [[RETVAL]], align 4
+
+// OGCG: define {{.*@binOpValue}}
+// OGCG:   [[OR:%.*]] = or i32 {{.*}}, 42
+// OGCG:   [[EXT:%.*]] = zext i32 [[OR]] to i64
+// OGCG:   [[BITS:%.*]] = and i64 [[EXT]], 3
+// OGCG:   store i64 {{.*}}, ptr {{.*}}, align 4
+// OGCG:   [[SHL:%.*]] = shl i64 [[BITS]], 62
+// OGCG:   [[ASHR:%.*]] = ashr i64 [[SHL]], 62
+// OGCG:   [[NEW:%.*]] = trunc i64 [[ASHR]] to i32
+// OGCG:   ret i32 [[NEW]]

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to