Skip to content

Commit 1714b11

Browse files
authored
[clang][bytcode] Convert Fixed Point values to target semantics... (#110411)
... after a binary operation. The Result of the operation is in the common semantics of RHS and LHS, so we need to convert that to the semantics of the BinaryOperator expression.
1 parent c2a37e4 commit 1714b11

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,25 +1502,41 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15021502
assert(LHS->getType()->isFixedPointType() ||
15031503
RHS->getType()->isFixedPointType());
15041504

1505+
auto LHSSema = Ctx.getASTContext().getFixedPointSemantics(LHS->getType());
1506+
auto RHSSema = Ctx.getASTContext().getFixedPointSemantics(RHS->getType());
1507+
15051508
if (!this->visit(LHS))
15061509
return false;
15071510
if (!LHS->getType()->isFixedPointType()) {
1508-
auto Sem = Ctx.getASTContext().getFixedPointSemantics(LHS->getType());
15091511
uint32_t I;
1510-
std::memcpy(&I, &Sem, sizeof(Sem));
1512+
std::memcpy(&I, &LHSSema, sizeof(llvm::FixedPointSemantics));
15111513
if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), I, E))
15121514
return false;
15131515
}
1516+
15141517
if (!this->visit(RHS))
15151518
return false;
15161519
if (!RHS->getType()->isFixedPointType()) {
1517-
auto Sem = Ctx.getASTContext().getFixedPointSemantics(RHS->getType());
15181520
uint32_t I;
1519-
std::memcpy(&I, &Sem, sizeof(Sem));
1521+
std::memcpy(&I, &RHSSema, sizeof(llvm::FixedPointSemantics));
15201522
if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), I, E))
15211523
return false;
15221524
}
15231525

1526+
// Convert the result to the target semantics.
1527+
auto ConvertResult = [&](bool R) -> bool {
1528+
if (!R)
1529+
return false;
1530+
auto ResultSema = Ctx.getASTContext().getFixedPointSemantics(E->getType());
1531+
auto CommonSema = LHSSema.getCommonSemantics(RHSSema);
1532+
if (ResultSema != CommonSema) {
1533+
uint32_t I;
1534+
std::memcpy(&I, &ResultSema, sizeof(ResultSema));
1535+
return this->emitCastFixedPoint(I, E);
1536+
}
1537+
return true;
1538+
};
1539+
15241540
switch (E->getOpcode()) {
15251541
case BO_EQ:
15261542
return this->emitEQFixedPoint(E);
@@ -1537,7 +1553,7 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15371553
return this->emitGEFixedPoint(E);
15381554
#endif
15391555
case BO_Add:
1540-
return this->emitAddFixedPoint(E);
1556+
return ConvertResult(this->emitAddFixedPoint(E));
15411557

15421558
default:
15431559
return this->emitInvalid(E);

clang/lib/AST/ByteCode/Interp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,8 @@ inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
21772177
E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
21782178
<< Result.toDiagnosticString(S.getASTContext()) << E->getType();
21792179
}
2180-
S.CCEDiag(E, diag::note_constexpr_overflow) << Result << E->getType();
2180+
S.CCEDiag(E, diag::note_constexpr_overflow)
2181+
<< Result.toDiagnosticString(S.getASTContext()) << E->getType();
21812182
if (!S.noteUndefinedBehavior())
21822183
return false;
21832184
}

clang/test/AST/ByteCode/fixed-point.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ namespace BinOps {
4242
static_assert(1 + A == 14.0k);
4343
static_assert((A + A) == 26);
4444

45-
/// FIXME: Conversion between fixed point semantics.
46-
static_assert(A + 100000 == 14.0k); // expected-error {{static assertion failed}} \
47-
// ref-error {{is not an integral constant expression}} \
48-
// ref-note {{is outside the range of representable values}}
45+
static_assert(A + 100000 == 14.0k); // both-error {{is not an integral constant expression}} \
46+
// both-note {{is outside the range of representable values}}
4947
}
5048

5149
namespace FixedPointCasts {

0 commit comments

Comments
 (0)