llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Morris Hafner (mmha)

<details>
<summary>Changes</summary>

ClangIR gave `s-&gt;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

---
Full diff: https://github.com/llvm/llvm-project/pull/245035.diff


2 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+1-1) 
- (modified) clang/test/CIR/CodeGen/bitfields.c (+31) 


``````````diff
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]]

``````````

</details>


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

Reply via email to