Skip to content

[clang][bytecode][NFC] Use FixedPoint opaque int API #123522

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
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
51 changes: 23 additions & 28 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,20 +689,18 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (!this->visit(SubExpr))
return false;

auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
uint32_t I;
std::memcpy(&I, &Sem, sizeof(Sem));
return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), I,
CE);
auto Sem =
Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()),
Sem, CE);
}
case CK_FloatingToFixedPoint: {
if (!this->visit(SubExpr))
return false;

auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
uint32_t I;
std::memcpy(&I, &Sem, sizeof(Sem));
return this->emitCastFloatingFixedPoint(I, CE);
auto Sem =
Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
return this->emitCastFloatingFixedPoint(Sem, CE);
}
case CK_FixedPointToFloating: {
if (!this->visit(SubExpr))
Expand All @@ -718,10 +716,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
case CK_FixedPointCast: {
if (!this->visit(SubExpr))
return false;
auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
uint32_t I;
std::memcpy(&I, &Sem, sizeof(Sem));
return this->emitCastFixedPoint(I, CE);
auto Sem =
Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
return this->emitCastFixedPoint(Sem, CE);
}

case CK_ToVoid:
Expand Down Expand Up @@ -1522,42 +1519,40 @@ template <class Emitter>
bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
const ASTContext &ASTCtx = Ctx.getASTContext();

assert(LHS->getType()->isFixedPointType() ||
RHS->getType()->isFixedPointType());

auto LHSSema = Ctx.getASTContext().getFixedPointSemantics(LHS->getType());
auto RHSSema = Ctx.getASTContext().getFixedPointSemantics(RHS->getType());
auto LHSSema = ASTCtx.getFixedPointSemantics(LHS->getType());
auto LHSSemaInt = LHSSema.toOpaqueInt();
auto RHSSema = ASTCtx.getFixedPointSemantics(RHS->getType());
auto RHSSemaInt = RHSSema.toOpaqueInt();

if (!this->visit(LHS))
return false;
if (!LHS->getType()->isFixedPointType()) {
uint32_t I;
std::memcpy(&I, &LHSSema, sizeof(llvm::FixedPointSemantics));
if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), I, E))
if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()),
LHSSemaInt, E))
return false;
}

if (!this->visit(RHS))
return false;
if (!RHS->getType()->isFixedPointType()) {
uint32_t I;
std::memcpy(&I, &RHSSema, sizeof(llvm::FixedPointSemantics));
if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), I, E))
if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()),
RHSSemaInt, E))
return false;
}

// Convert the result to the target semantics.
auto ConvertResult = [&](bool R) -> bool {
if (!R)
return false;
auto ResultSema = Ctx.getASTContext().getFixedPointSemantics(E->getType());
auto CommonSema = LHSSema.getCommonSemantics(RHSSema);
if (ResultSema != CommonSema) {
uint32_t I;
std::memcpy(&I, &ResultSema, sizeof(ResultSema));
return this->emitCastFixedPoint(I, E);
}
auto ResultSema = ASTCtx.getFixedPointSemantics(E->getType()).toOpaqueInt();
auto CommonSema = LHSSema.getCommonSemantics(RHSSema).toOpaqueInt();
if (ResultSema != CommonSema)
return this->emitCastFixedPoint(ResultSema, E);
return true;
};

Expand Down
11 changes: 4 additions & 7 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2141,9 +2141,8 @@ inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
}

inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
FixedPointSemantics TargetSemantics(0, 0, false, false, false);
std::memcpy(&TargetSemantics, &FPS, sizeof(TargetSemantics));

FixedPointSemantics TargetSemantics =
FixedPointSemantics::getFromOpaqueInt(FPS);
const auto &Source = S.Stk.pop<FixedPoint>();

bool Overflow;
Expand Down Expand Up @@ -2271,8 +2270,7 @@ static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
uint32_t FPS) {
const T &Int = S.Stk.pop<T>();

FixedPointSemantics Sem(0, 0, false, false, false);
std::memcpy(&Sem, &FPS, sizeof(Sem));
FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);

bool Overflow;
FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
Expand All @@ -2288,8 +2286,7 @@ static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
uint32_t FPS) {
const auto &Float = S.Stk.pop<Floating>();

FixedPointSemantics Sem(0, 0, false, false, false);
std::memcpy(&Sem, &FPS, sizeof(Sem));
FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);

bool Overflow;
FixedPoint Result = FixedPoint::from(Float.getAPFloat(), Sem, &Overflow);
Expand Down
Loading