-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now that we have it, use it.
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesNow that we have it, use it. Full diff: https://github.com/llvm/llvm-project/pull/123522.diff 2 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 7afae97f308ad5..78f936ec9f8958 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -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))
@@ -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:
@@ -1522,28 +1519,29 @@ 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;
}
@@ -1551,13 +1549,10 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
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;
};
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 93a91976a31bf1..58c0256c7d7df8 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -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;
@@ -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);
@@ -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);
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Now that we have it, use it.