Skip to content

[clang][bytecode][NFC] Switch BitcastBuffer to SmallVector #114677

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
Nov 3, 2024
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
43 changes: 20 additions & 23 deletions clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,33 @@ static void swapBytes(std::byte *M, size_t N) {
/// have indeterminate value.
/// All offsets are in bits.
struct BitcastBuffer {
llvm::BitVector Data;
size_t SizeInBits = 0;
llvm::SmallVector<std::byte> Data;

BitcastBuffer() = default;

size_t size() const { return Data.size(); }
size_t size() const { return SizeInBits; }

const std::byte *data() const {
unsigned NBytes = Data.size() / 8;
unsigned BitVectorWordSize = sizeof(uintptr_t);
bool FullWord = (NBytes % BitVectorWordSize == 0);

// llvm::BitVector uses 64-bit fields internally, so when we have
// fewer bytes than that, we need to compensate for that on
// big endian hosts.
unsigned DataPlus;
if (llvm::sys::IsBigEndianHost)
DataPlus = BitVectorWordSize - (NBytes % BitVectorWordSize);
else
DataPlus = 0;

return reinterpret_cast<const std::byte *>(Data.getData().data()) +
(FullWord ? 0 : DataPlus);
}
const std::byte *data() const { return Data.data(); }

bool allInitialized() const {
// FIXME: Implement.
return true;
}

bool atByteBoundary() const { return (Data.size() * 8) == SizeInBits; }

void pushBit(bool Value) {
if (atByteBoundary())
Data.push_back(std::byte{0});

if (Value)
Data.back() |= (std::byte{1} << (SizeInBits % 8));
++SizeInBits;
}

void pushData(const std::byte *data, size_t BitOffset, size_t BitWidth,
bool BigEndianTarget) {
Data.reserve(BitOffset + BitWidth);

bool OnlyFullBytes = BitWidth % 8 == 0;
unsigned NBytes = BitWidth / 8;

Expand All @@ -125,7 +119,7 @@ struct BitcastBuffer {
std::byte B =
BigEndianTarget ? data[NBytes - OnlyFullBytes - I] : data[I];
for (unsigned X = 0; X != 8; ++X) {
Data.push_back(bitof(B, X));
pushBit(bitof(B, X));
++BitsHandled;
}
}
Expand All @@ -137,7 +131,7 @@ struct BitcastBuffer {
assert((BitWidth - BitsHandled) < 8);
std::byte B = BigEndianTarget ? data[0] : data[NBytes];
for (size_t I = 0, E = (BitWidth - BitsHandled); I != E; ++I) {
Data.push_back(bitof(B, I));
pushBit(bitof(B, I));
++BitsHandled;
}

Expand Down Expand Up @@ -363,5 +357,8 @@ bool clang::interp::DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
HasIndeterminateBits = !Buffer.allInitialized();
std::memcpy(Buff, Buffer.data(), BuffSize);

if (llvm::sys::IsBigEndianHost)
swapBytes(Buff, BuffSize);

return Success;
}
11 changes: 11 additions & 0 deletions clang/test/AST/ByteCode/builtin-bit-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ constexpr bool operator==(const struct bits<N, T, P>& lhs, const struct bits<N,
#ifdef __SIZEOF_INT128__
static_assert(check_round_trip<__int128_t>((__int128_t)34));
static_assert(check_round_trip<__int128_t>((__int128_t)-34));

constexpr unsigned char OneBit[] = {
0x1, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
};
constexpr __int128_t One = 1;
constexpr __int128_t Expected = One << 120;
static_assert(__builtin_bit_cast(__int128_t, OneBit) == (LITTLE_END ? 1 : Expected));

#endif


Expand Down
Loading