Skip to content

[clang][bytecode] Fix shifts with an allocated RHS #145280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/IntegralAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ template <bool Signed> class IntegralAP final {
if (NumBits == 0)
NumBits = sizeof(T) * 8;
assert(NumBits > 0);
assert(APInt::getNumWords(NumBits) == 1);
APInt Copy = APInt(NumBits, static_cast<uint64_t>(Value), Signed);
assert(false);
return IntegralAP<Signed>(Copy);
}

Expand Down
63 changes: 37 additions & 26 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ inline bool RVOPtr(InterpState &S, CodePtr OpPC) {
template <class LT, class RT, ShiftDir Dir>
inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
LT *Result) {

static_assert(!needsAlloc<LT>());
const unsigned Bits = LHS.bitWidth();

// OpenCL 6.3j: shift values are effectively % word size of LHS.
Expand Down Expand Up @@ -2770,7 +2770,10 @@ inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
LT::AsUnsigned::from(RHS, Bits), Bits, &R);
}
} else {
S.Stk.push<LT>(LT::from(R));
return true;
}

// Right shift.
if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
ComparisonCategoryResult::Greater) {
Expand All @@ -2779,51 +2782,52 @@ inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
// Do the shift on potentially signed LT, then convert to unsigned type.
LT A;
LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
// LT::shiftRight(LHS, LT(RHSTemp), Bits, &A);
R = LT::AsUnsigned::from(A);
}
}

S.Stk.push<LT>(LT::from(R));
return true;
}

/// A version of DoShift that works on IntegralAP.
template <class LT, class RT, ShiftDir Dir>
inline bool DoShiftAP(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
LT *Result) {
const unsigned Bits = LHS.bitWidth();
const APSInt &LHSAP = LHS.toAPSInt();
APSInt RHSAP = RHS.toAPSInt();
inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
APSInt RHS, LT *Result) {
const unsigned Bits = LHS.getBitWidth();

// OpenCL 6.3j: shift values are effectively % word size of LHS.
if (S.getLangOpts().OpenCL)
RHSAP &= APSInt(llvm::APInt(RHSAP.getBitWidth(),
static_cast<uint64_t>(LHSAP.getBitWidth() - 1)),
RHSAP.isUnsigned());
RHS &=
APSInt(llvm::APInt(RHS.getBitWidth(), static_cast<uint64_t>(Bits - 1)),
RHS.isUnsigned());

if (RHS.isNegative()) {
// During constant-folding, a negative shift is an opposite shift. Such a
// shift is not a constant expression.
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS; //.toAPSInt();
if (!S.noteUndefinedBehavior())
return false;
RHS = -RHS;
return DoShiftAP<LT, RT,
Dir == ShiftDir::Left ? ShiftDir::Right : ShiftDir::Left>(
S, OpPC, LHS, RHS, Result);
S, OpPC, LHS, -RHS, Result);
}

if (!CheckShift<Dir>(S, OpPC, LHS, RHS, Bits))
if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
Bits))
return false;

unsigned SA = (unsigned)RHS.getLimitedValue(Bits - 1);
if constexpr (Dir == ShiftDir::Left) {
unsigned SA = (unsigned)RHSAP.getLimitedValue(LHS.bitWidth() - 1);
Result->copy(LHSAP << SA);
if constexpr (needsAlloc<LT>())
Result->copy(LHS << SA);
else
*Result = LT(LHS << SA);
} else {
unsigned SA = (unsigned)RHSAP.getLimitedValue(LHS.bitWidth() - 1);
Result->copy(LHSAP >> SA);
if constexpr (needsAlloc<LT>())
Result->copy(LHS >> SA);
else
*Result = LT(LHS >> SA);
}

S.Stk.push<LT>(*Result);
Expand All @@ -2837,9 +2841,12 @@ inline bool Shr(InterpState &S, CodePtr OpPC) {
auto RHS = S.Stk.pop<RT>();
auto LHS = S.Stk.pop<LT>();

if constexpr (needsAlloc<LT>()) {
LT Result = S.allocAP<LT>(LHS.bitWidth());
return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
LT Result;
if constexpr (needsAlloc<LT>())
Result = S.allocAP<LT>(LHS.bitWidth());
return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS.toAPSInt(),
RHS.toAPSInt(), &Result);
} else {
LT Result;
return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
Expand All @@ -2852,9 +2859,13 @@ inline bool Shl(InterpState &S, CodePtr OpPC) {
using RT = typename PrimConv<NameR>::T;
auto RHS = S.Stk.pop<RT>();
auto LHS = S.Stk.pop<LT>();
if constexpr (needsAlloc<LT>()) {
LT Result = S.allocAP<LT>(LHS.bitWidth());
return DoShiftAP<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);

if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
LT Result;
if constexpr (needsAlloc<LT>())
Result = S.allocAP<LT>(LHS.bitWidth());
return DoShiftAP<LT, RT, ShiftDir::Left>(S, OpPC, LHS.toAPSInt(),
RHS.toAPSInt(), &Result);
} else {
LT Result;
return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);
Expand Down
14 changes: 14 additions & 0 deletions clang/test/AST/ByteCode/intap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,18 @@ namespace IncDec {
#endif
}

#if __cplusplus >= 201402L
const __int128_t a = ( (__int128_t)1 << 64 );
const _BitInt(72) b = ( 1 << 72 ); // both-warning {{shift count >= width of type}}
constexpr int shifts() { // both-error {{never produces a constant expression}}
(void)(2 >> a); // both-warning {{shift count >= width of type}} \
// both-note {{shift count 18446744073709551616 >= width of type 'int' (32 bits)}}
(void)(2 >> b); // ref-warning {{shift count is negative}}
(void)(2 << a); // both-warning {{shift count >= width of type}}
(void)(2 << b); // ref-warning {{shift count is negative}}
return 1;
}
#endif


#endif
Loading