Skip to content
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

[clang][bytecode] Don't memcpy() FixedPoint values #123599

Merged
merged 1 commit into from
Jan 20, 2025
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
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);
Endilll marked this conversation as resolved.
Show resolved Hide resolved
}

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)),
tbaederr marked this conversation as resolved.
Show resolved Hide resolved
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
Loading