[clang] [clang][bytecode] Fix right shfits greater than bitwidth (PR #202851)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 9 22:01:18 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
We're not erroring in C, but we did compute the wrong result.
---
Full diff: https://github.com/llvm/llvm-project/pull/202851.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.h (+10-10)
- (modified) clang/test/AST/ByteCode/codegen.c (+3)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index c25531ecedd47..4c0a7040f46aa 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3245,16 +3245,16 @@ inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
return true;
}
- // Right shift.
- if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
- ComparisonCategoryResult::Greater) {
- R = LT::AsUnsigned::from(-1);
- } else {
- // Do the shift on potentially signed LT, then convert to unsigned type.
- LT A;
- LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
- R = LT::AsUnsigned::from(A);
- }
+ // Right shift.
+ if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
+ ComparisonCategoryResult::Greater) {
+ R = LT::AsUnsigned::from(0);
+ } else {
+ // Do the shift on potentially signed LT, then convert to unsigned type.
+ LT A;
+ LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
+ R = LT::AsUnsigned::from(A);
+ }
S.Stk.push<LT>(LT::from(R));
return true;
diff --git a/clang/test/AST/ByteCode/codegen.c b/clang/test/AST/ByteCode/codegen.c
index 3c6f17e2b8726..7f877bb4240fe 100644
--- a/clang/test/AST/ByteCode/codegen.c
+++ b/clang/test/AST/ByteCode/codegen.c
@@ -9,6 +9,9 @@ const intptr_t Z1 = (intptr_t)(((char*)-1LL) + 1);
const intptr_t Z2 = (intptr_t)(((char*)1LL) - 1);
// CHECK: @Z2 = constant i64 0
+const int Shift = 2 >> 32;
+// CHECK: @Shift = constant i32 0
+
struct A {
char num_fields;
};
``````````
</details>
https://github.com/llvm/llvm-project/pull/202851
More information about the cfe-commits
mailing list