Skip to content

Commit

Permalink
[clang][bytecode] Don't memcpy() FixedPoint values (#123599)
Browse files Browse the repository at this point in the history
llvm::FixedPoint is not trivially copyable.
  • Loading branch information
tbaederr authored Jan 20, 2025
1 parent a733c1f commit b5c9cba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
emitSerialized(Code, Val, Success);
}

template <>
void emit(Program &P, std::vector<std::byte> &Code, const FixedPoint &Val,
bool &Success) {
emitSerialized(Code, Val, Success);
}

template <typename... Tys>
bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args,
const SourceInfo &SI) {
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/ByteCode/Disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ inline IntegralAP<true> ReadArg<IntegralAP<true>>(Program &P, CodePtr &OpPC) {
return I;
}

template <> inline FixedPoint ReadArg<FixedPoint>(Program &P, CodePtr &OpPC) {
FixedPoint I = FixedPoint::deserialize(*OpPC);
OpPC += align(I.bytesToSerialize());
return I;
}

LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }

LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
Expand Down
26 changes: 26 additions & 0 deletions clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ class FixedPoint final {
return ComparisonCategoryResult::Greater;
}

size_t bytesToSerialize() const {
return sizeof(uint32_t) + (V.getValue().getBitWidth() / CHAR_BIT);
}

void serialize(std::byte *Buff) const {
// Semantics followed by APInt.
uint32_t SemI = V.getSemantics().toOpaqueInt();
std::memcpy(Buff, &SemI, sizeof(SemI));

llvm::APInt API = V.getValue();
llvm::StoreIntToMemory(API, (uint8_t *)(Buff + sizeof(SemI)),
bitWidth() / 8);
}

static FixedPoint deserialize(const std::byte *Buff) {
auto Sem = llvm::FixedPointSemantics::getFromOpaqueInt(
*reinterpret_cast<const uint32_t *>(Buff));
unsigned BitWidth = Sem.getWidth();
APInt I(BitWidth, 0ull, !Sem.isSigned());
llvm::LoadIntFromMemory(
I, reinterpret_cast<const uint8_t *>(Buff + sizeof(uint32_t)),
BitWidth / CHAR_BIT);

return FixedPoint(I, Sem);
}

static bool neg(const FixedPoint &A, FixedPoint *R) {
bool Overflow = false;
*R = FixedPoint(A.V.negate(&Overflow));
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3070,6 +3070,13 @@ inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
return I;
}

template <>
inline FixedPoint ReadArg<FixedPoint>(InterpState &S, CodePtr &OpPC) {
FixedPoint FP = FixedPoint::deserialize(*OpPC);
OpPC += align(FP.bytesToSerialize());
return FP;
}

} // namespace interp
} // namespace clang

Expand Down

0 comments on commit b5c9cba

Please sign in to comment.