Skip to content

[clang][Interp] Add basic support for _BitInt #68069

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 2 commits into from
Oct 11, 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
12 changes: 12 additions & 0 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (!this->visit(SubExpr))
return false;

if (ToT == PT_IntAP)
return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
CE);
if (ToT == PT_IntAPS)
return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
CE);

return this->emitCastFloatingIntegral(*ToT, CE);
}

Expand Down Expand Up @@ -183,6 +190,11 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
return true;
}

if (ToT == PT_IntAP)
return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE);
if (ToT == PT_IntAPS)
return this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE);

return this->emitCast(*FromT, *ToT, CE);
}

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/Interp/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Context final {
unsigned getCharBit() const;
/// Return the floating-point semantics for T.
const llvm::fltSemantics &getFloatSemantics(QualType T) const;
/// Return the size of T in bits.
uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }

/// Classifies an expression.
std::optional<PrimType> classify(QualType T) const;
Expand Down
16 changes: 9 additions & 7 deletions clang/lib/AST/Interp/IntegralAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ template <bool Signed> class IntegralAP final {
APSInt V;

template <typename T> static T truncateCast(const APSInt &V) {
return std::is_signed_v<T> ? V.trunc(sizeof(T) * 8).getSExtValue()
: V.trunc(sizeof(T) * 8).getZExtValue();
constexpr unsigned BitSize = sizeof(T) * 8;
if (BitSize >= V.getBitWidth())
return std::is_signed_v<T> ? V.getSExtValue() : V.getZExtValue();

return std::is_signed_v<T> ? V.trunc(BitSize).getSExtValue()
: V.trunc(BitSize).getZExtValue();
}

public:
Expand Down Expand Up @@ -89,10 +93,9 @@ template <bool Signed> class IntegralAP final {
}

template <unsigned Bits, bool InputSigned>
static IntegralAP from(Integral<Bits, InputSigned> I) {
// FIXME: Take bits parameter.
static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
APSInt Copy =
APSInt(APInt(128, static_cast<int64_t>(I), InputSigned), !Signed);
APSInt(APInt(BitWidth, static_cast<int64_t>(I), InputSigned), !Signed);
Copy.setIsSigned(Signed);

assert(Copy.isSigned() == Signed);
Expand All @@ -108,8 +111,7 @@ template <bool Signed> class IntegralAP final {
return IntegralAP(0);
}

// FIXME: This can't be static if the bitwidth depends on V.
static constexpr unsigned bitWidth() { return 128; }
constexpr unsigned bitWidth() const { return V.getBitWidth(); }

APSInt toAPSInt(unsigned Bits = 0) const { return V; }
APValue toAPValue() const { return APValue(V); }
Expand Down
58 changes: 57 additions & 1 deletion clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,22 @@ inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
return true;
}

/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
/// to know what bitwidth the result should be.
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
S.Stk.push<IntegralAP<false>>(
IntegralAP<false>::from(S.Stk.pop<T>(), BitWidth));
return true;
}

template <PrimType Name, class T = typename PrimConv<Name>::T>
bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
S.Stk.push<IntegralAP<true>>(
IntegralAP<true>::from(S.Stk.pop<T>(), BitWidth));
return true;
}

template <PrimType Name, class T = typename PrimConv<Name>::T>
bool CastIntegralFloating(InterpState &S, CodePtr OpPC,
const llvm::fltSemantics *Sem,
Expand Down Expand Up @@ -1608,6 +1624,46 @@ bool CastFloatingIntegral(InterpState &S, CodePtr OpPC) {
}
}

static inline bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC,
uint32_t BitWidth) {
const Floating &F = S.Stk.pop<Floating>();

APSInt Result(BitWidth, /*IsUnsigned=*/true);
auto Status = F.convertToInteger(Result);

// Float-to-Integral overflow check.
if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite()) {
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();

S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
return S.noteUndefinedBehavior();
}

S.Stk.push<IntegralAP<true>>(IntegralAP<true>(Result));
return CheckFloatResult(S, OpPC, F, Status);
}

static inline bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC,
uint32_t BitWidth) {
const Floating &F = S.Stk.pop<Floating>();

APSInt Result(BitWidth, /*IsUnsigned=*/false);
auto Status = F.convertToInteger(Result);

// Float-to-Integral overflow check.
if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite()) {
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();

S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
return S.noteUndefinedBehavior();
}

S.Stk.push<IntegralAP<true>>(IntegralAP<true>(Result));
return CheckFloatResult(S, OpPC, F, Status);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CastFloatingIntegralAP(S) needs a helper function to get rid of the code duplication.

template <PrimType Name, class T = typename PrimConv<Name>::T>
bool CastPointerIntegral(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
Expand Down Expand Up @@ -1697,7 +1753,7 @@ inline bool Shl(InterpState &S, CodePtr OpPC) {

typename LT::AsUnsigned R;
LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
LT::AsUnsigned::from(RHS), Bits, &R);
LT::AsUnsigned::from(RHS, Bits), Bits, &R);
S.Stk.push<LT>(LT::from(R));
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/Interp/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ static APSInt peekToAPSInt(InterpStack &Stk, PrimType T, size_t Offset = 0) {
APSInt R;
INT_TYPE_SWITCH(T, {
T Val = Stk.peek<T>(Offset);
R = APSInt(APInt(T::bitWidth(), static_cast<uint64_t>(Val), T::isSigned()));
R = APSInt(
APInt(Val.bitWidth(), static_cast<uint64_t>(Val), T::isSigned()));
});

return R;
Expand Down
30 changes: 28 additions & 2 deletions clang/lib/AST/Interp/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def FromCastTypeClass : TypeClass {
}

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

def Cast: Opcode {
Expand All @@ -577,6 +577,22 @@ def CastFP : Opcode {
let Args = [ArgFltSemantics, ArgRoundingMode];
}

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

def CastAP : Opcode {
let Types = [AluTypeClass];
let Args = [ArgUint32];
let HasGroup = 1;
}

def CastAPS : Opcode {
let Types = [AluTypeClass];
let Args = [ArgUint32];
let HasGroup = 1;
}

// Cast an integer to a floating type
def CastIntegralFloating : Opcode {
let Types = [AluTypeClass];
Expand All @@ -586,11 +602,21 @@ def CastIntegralFloating : Opcode {

// Cast a floating to an integer type
def CastFloatingIntegral : Opcode {
let Types = [AluTypeClass];
let Types = [FixedSizeIntegralTypes];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type classes for these get kinda confusing... but I just can't come up with the correct syntax to define them inline.

let Args = [];
let HasGroup = 1;
}

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

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

def CastPointerIntegral : Opcode {
let Types = [AluTypeClass];
let Args = [];
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 @@ -3,6 +3,21 @@
// RUN: %clang_cc1 -std=c++11 -fms-extensions -verify=ref %s
// RUN: %clang_cc1 -std=c++20 -fms-extensions -verify=ref %s


using MaxBitInt = _BitInt(8388608);

constexpr _BitInt(2) A = 0;
constexpr _BitInt(2) B = A + 1;
constexpr _BitInt(2) C = B + 1; // expected-warning {{from 2 to -2}} \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since _BitInt can end up being pretty sizable (I think we removed the size limit?), can we test something like a 1024 sized one?

// ref-warning {{from 2 to -2}}
static_assert(C == -2, "");


constexpr MaxBitInt A_ = 0;
constexpr MaxBitInt B_ = A_ + 1;
static_assert(B_ == 1, "");


#ifdef __SIZEOF_INT128__
namespace i128 {
typedef __int128 int128_t;
Expand Down