Skip to content

[clang][bytecode] Support bitcasting into float fields #114825

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 5, 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
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2735,7 +2735,7 @@ bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
return this->visitInitializer(SubExpr);
return this->visitInitializer(SubExpr) && this->emitFinishInit(E);
}
}
return false;
Expand Down
22 changes: 19 additions & 3 deletions clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,35 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
/*ReturnOnUninit=*/false);

// Now read the values out of the buffer again and into ToPtr.
const ASTContext &ASTCtx = S.getASTContext();
size_t BitOffset = 0;
bool Success = enumeratePointerFields(
ToPtr, S.getContext(),
[&](const Pointer &P, PrimType T, size_t _) -> bool {
BITCAST_TYPE_SWITCH_FIXED_SIZE(T, {
T &Val = P.deref<T>();
if (T == PT_Float) {
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
const auto &Semantics = ASTCtx.getFloatTypeSemantics(P.getType());
unsigned NumBits = llvm::APFloatBase::getSizeInBits(Semantics);
assert(NumBits % 8 == 0);
assert(NumBits <= ASTCtx.toBits(ObjectReprChars));
std::byte *M = Buffer.getBytes(BitOffset);

if (llvm::sys::IsBigEndianHost)
swapBytes(M, NumBits / 8);

P.deref<Floating>() = Floating::bitcastFromMemory(M, Semantics);
P.initialize();
BitOffset += ASTCtx.toBits(ObjectReprChars);
return true;
}

BITCAST_TYPE_SWITCH_FIXED_SIZE(T, {
std::byte *M = Buffer.getBytes(BitOffset);

if (llvm::sys::IsBigEndianHost)
swapBytes(M, T::bitWidth() / 8);

Val = T::bitcastFromMemory(M, T::bitWidth());
P.deref<T>() = T::bitcastFromMemory(M, T::bitWidth());
P.initialize();
BitOffset += T::bitWidth();
});
Expand Down
13 changes: 10 additions & 3 deletions clang/test/AST/ByteCode/builtin-bit-cast-long-double.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct bytes {
unsigned char d[16];
};

// static_assert(round_trip<bytes>(ld), "");
static_assert(round_trip<bytes>(ld), "");

static_assert(round_trip<long double>(10.0L));

Expand Down Expand Up @@ -77,10 +77,17 @@ constexpr bytes ld539 = {
0x8, 0x40, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
};

constexpr long double fivehundredandthirtynine = 539.0;

static_assert(bit_cast<long double>(ld539) == fivehundredandthirtynine, "");

struct LD {
long double v;
};

constexpr LD ld2 = __builtin_bit_cast(LD, ld539.d);
constexpr long double five39 = __builtin_bit_cast(long double, ld539.d);
static_assert(ld2.v == five39);

#else
static_assert(round_trip<__int128_t>(34.0L));
#endif
Expand Down
19 changes: 19 additions & 0 deletions clang/test/AST/ByteCode/builtin-bit-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,22 @@ struct ref_mem {
// both-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
// both-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
constexpr intptr_t run_ref_mem = __builtin_bit_cast(intptr_t, ref_mem{global_int});




namespace test_complex {
constexpr _Complex unsigned test_int_complex = { 0x0C05FEFE, 0xCAFEBABE };
static_assert(round_trip<_Complex unsigned>(0xCAFEBABE0C05FEFEULL), "");
static_assert(bit_cast<unsigned long long>(test_int_complex) == (LITTLE_END
? 0xCAFEBABE0C05FEFE
: 0x0C05FEFECAFEBABE), "");
static_assert(sizeof(double) == 2 * sizeof(float));
struct TwoFloats { float A; float B; };
constexpr _Complex float test_float_complex = {1.0f, 2.0f};
constexpr TwoFloats TF = __builtin_bit_cast(TwoFloats, test_float_complex);
static_assert(TF.A == 1.0f && TF.B == 2.0f);

constexpr double D = __builtin_bit_cast(double, test_float_complex);
constexpr int M = __builtin_bit_cast(int, test_int_complex); // both-error {{__builtin_bit_cast source size does not equal destination size}}
}
Loading