Skip to content

[clang][Interp] IntegralAP zero-init #68081

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
Oct 19, 2023
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
4 changes: 2 additions & 2 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1665,9 +1665,9 @@ bool ByteCodeExprGen<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
case PT_Uint64:
return this->emitZeroUint64(E);
case PT_IntAP:
return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
case PT_IntAPS:
assert(false);
return false;
return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
case PT_Ptr:
return this->emitNullPtr(E);
case PT_FnPtr:
Expand Down
11 changes: 6 additions & 5 deletions clang/lib/AST/Interp/IntegralAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ template <bool Signed> class IntegralAP final {

template <typename T> static IntegralAP from(T Value, unsigned NumBits = 0) {
assert(NumBits > 0);
APSInt Copy = APSInt(APInt(NumBits, static_cast<int64_t>(Value), Signed), !Signed);
APSInt Copy =
APSInt(APInt(NumBits, static_cast<uint64_t>(Value), Signed), !Signed);

return IntegralAP<Signed>(Copy);
}
Expand All @@ -97,16 +98,16 @@ template <bool Signed> class IntegralAP final {
template <unsigned Bits, bool InputSigned>
static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
APSInt Copy =
APSInt(APInt(BitWidth, static_cast<int64_t>(I), InputSigned), !Signed);
APSInt(APInt(BitWidth, static_cast<uint64_t>(I), InputSigned), !Signed);
Copy.setIsSigned(Signed);

assert(Copy.isSigned() == Signed);
return IntegralAP<Signed>(Copy);
}

static IntegralAP zero() {
assert(false);
return IntegralAP(0);
static IntegralAP zero(int32_t BitWidth) {
APSInt V = APSInt(APInt(BitWidth, 0LL, Signed), !Signed);
return IntegralAP(V);
}

constexpr unsigned bitWidth() const { return V.getBitWidth(); }
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,16 @@ bool Zero(InterpState &S, CodePtr OpPC) {
return true;
}

static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
S.Stk.push<IntegralAP<false>>(IntegralAP<false>::zero(BitWidth));
return true;
}

static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
S.Stk.push<IntegralAP<true>>(IntegralAP<true>::zero(BitWidth));
return true;
}

template <PrimType Name, class T = typename PrimConv<Name>::T>
inline bool Null(InterpState &S, CodePtr OpPC) {
S.Stk.push<T>();
Expand Down
15 changes: 14 additions & 1 deletion clang/lib/AST/Interp/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def IntegerTypeClass : TypeClass {
Uint32, Sint64, Uint64, IntAP, IntAPS];
}

def FixedSizeIntegralTypeClass : TypeClass {
let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
Uint32, Sint64, Uint64, Bool];
}

def NumberTypeClass : TypeClass {
let Types = !listconcat(IntegerTypeClass.Types, [Float]);
}
Expand Down Expand Up @@ -243,10 +248,18 @@ def ConstBool : ConstOpcode<Bool, ArgBool>;

// [] -> [Integer]
def Zero : Opcode {
let Types = [AluTypeClass];
let Types = [FixedSizeIntegralTypeClass];
let HasGroup = 1;
}

def ZeroIntAP : Opcode {
let Args = [ArgUint32];
}

def ZeroIntAPS : Opcode {
let Args = [ArgUint32];
}

// [] -> [Pointer]
def Null : Opcode {
let Types = [PtrTypeClass];
Expand Down
15 changes: 15 additions & 0 deletions clang/test/AST/Interp/intap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ constexpr MaxBitInt A_ = 0;
constexpr MaxBitInt B_ = A_ + 1;
static_assert(B_ == 1, "");

constexpr MaxBitInt BitIntZero{};
static_assert(BitIntZero == 0, "");
constexpr unsigned _BitInt(128) UBitIntZero{};
static_assert(UBitIntZero == 0, "");

constexpr _BitInt(2) BitIntZero2{};
static_assert(BitIntZero2 == 0, "");
constexpr unsigned _BitInt(1) UBitIntZero1{};
static_assert(UBitIntZero1 == 0, "");


#ifdef __SIZEOF_INT128__
namespace i128 {
Expand Down Expand Up @@ -49,6 +59,11 @@ namespace i128 {
constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
static_assert(AllOnes == UINT128_MAX, "");

constexpr uint128_t i128Zero{};
static_assert(i128Zero == 0, "");
constexpr uint128_t ui128Zero{};
static_assert(ui128Zero == 0, "");

#if __cplusplus >= 201402L
template <typename T>
constexpr T CastFrom(__int128_t A) {
Expand Down