[clang] 2e9fd5c - [CIR] Fix the value of a compound assignment to a bit-field (#245035)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 18:46:23 PST 2026


Author: Morris Hafner
Date: 2026-11-24T03:46:17+01:00
New Revision: 2e9fd5c76b7f606b2a3a816a410a1753ee088fbe

URL: https://github.com/llvm/llvm-project/commit/2e9fd5c76b7f606b2a3a816a410a1753ee088fbe
DIFF: https://github.com/llvm/llvm-project/commit/2e9fd5c76b7f606b2a3a816a410a1753ee088fbe.diff

LOG: [CIR] Fix the value of a compound assignment to a bit-field (#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

Added: 
    

Modified: 
    clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
    clang/test/CIR/CodeGen/bitfields.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 40bdd49b5d70a..c49459c5020fb 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 69766178c4613..3d5f685e373ba 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]]


        


More information about the cfe-commits mailing list