Skip to content

Commit 23ba0fd

Browse files
authored
[clang][bytecode] Fix assignInteger() with allocated primtypes (#145302)
1 parent 9881a50 commit 23ba0fd

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,21 @@ static void pushInteger(InterpState &S, T Val, QualType QT) {
9696
QT);
9797
}
9898

99-
static void assignInteger(const Pointer &Dest, PrimType ValueT,
99+
static void assignInteger(InterpState &S, const Pointer &Dest, PrimType ValueT,
100100
const APSInt &Value) {
101-
INT_TYPE_SWITCH_NO_BOOL(
102-
ValueT, { Dest.deref<T>() = T::from(static_cast<T>(Value)); });
101+
102+
if (ValueT == PT_IntAPS) {
103+
Dest.deref<IntegralAP<true>>() =
104+
S.allocAP<IntegralAP<true>>(Value.getBitWidth());
105+
Dest.deref<IntegralAP<true>>().copy(Value);
106+
} else if (ValueT == PT_IntAP) {
107+
Dest.deref<IntegralAP<false>>() =
108+
S.allocAP<IntegralAP<false>>(Value.getBitWidth());
109+
Dest.deref<IntegralAP<false>>().copy(Value);
110+
} else {
111+
INT_TYPE_SWITCH_NO_BOOL(
112+
ValueT, { Dest.deref<T>() = T::from(static_cast<T>(Value)); });
113+
}
103114
}
104115

105116
static QualType getElemType(const Pointer &P) {
@@ -849,7 +860,7 @@ static bool interp__builtin_overflowop(InterpState &S, CodePtr OpPC,
849860
}
850861

851862
// Write Result to ResultPtr and put Overflow on the stack.
852-
assignInteger(ResultPtr, ResultT, Result);
863+
assignInteger(S, ResultPtr, ResultT, Result);
853864
ResultPtr.initialize();
854865
assert(Call->getDirectCallee()->getReturnType()->isBooleanType());
855866
S.Stk.push<Boolean>(Overflow);
@@ -902,7 +913,7 @@ static bool interp__builtin_carryop(InterpState &S, CodePtr OpPC,
902913

903914
QualType CarryOutType = Call->getArg(3)->getType()->getPointeeType();
904915
PrimType CarryOutT = *S.getContext().classify(CarryOutType);
905-
assignInteger(CarryOutPtr, CarryOutT, CarryOut);
916+
assignInteger(S, CarryOutPtr, CarryOutT, CarryOut);
906917
CarryOutPtr.initialize();
907918

908919
assert(Call->getType() == Call->getArg(0)->getType());
@@ -1414,7 +1425,7 @@ static bool interp__builtin_ia32_addcarry_subborrow(InterpState &S,
14141425

14151426
QualType CarryOutType = Call->getArg(3)->getType()->getPointeeType();
14161427
PrimType CarryOutT = *S.getContext().classify(CarryOutType);
1417-
assignInteger(CarryOutPtr, CarryOutT, APSInt(Result, true));
1428+
assignInteger(S, CarryOutPtr, CarryOutT, APSInt(Result, true));
14181429

14191430
pushInteger(S, CarryOut, Call->getType());
14201431

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,4 +1739,18 @@ namespace WithinLifetime {
17391739
// both-warning {{expression result unused}}
17401740
}
17411741
}
1742+
1743+
#ifdef __SIZEOF_INT128__
1744+
namespace I128Mul {
1745+
constexpr int mul() {
1746+
__int128 A = 10;
1747+
__int128 B = 10;
1748+
__int128 R;
1749+
__builtin_mul_overflow(A, B, &R);
1750+
return 1;
1751+
}
1752+
static_assert(mul() == 1);
1753+
}
1754+
#endif
1755+
17421756
#endif

0 commit comments

Comments
 (0)