Skip to content

Commit 757642b

Browse files
author
Jenkins
committed
merge main into amd-staging
Change-Id: I6576fdc8e2f1d6fc91049bed99485a740065dbbb
2 parents 4e31ba9 + 76f2fa8 commit 757642b

File tree

13 files changed

+191
-97
lines changed

13 files changed

+191
-97
lines changed

clang/lib/AST/ByteCode/Boolean.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Boolean final {
3030
public:
3131
/// Zero-initializes a boolean.
3232
Boolean() : V(false) {}
33+
Boolean(const llvm::APSInt &I) : V(!I.isZero()) {}
3334
explicit Boolean(bool V) : V(V) {}
3435

3536
bool operator<(Boolean RHS) const { return V < RHS.V; }

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,11 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
697697
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
698698
return this->emitCastFixedPointFloating(TargetSemantics, CE);
699699
}
700+
case CK_FixedPointToIntegral: {
701+
if (!this->visit(SubExpr))
702+
return false;
703+
return this->emitCastFixedPointIntegral(classifyPrim(CE->getType()), CE);
704+
}
700705
case CK_FixedPointCast: {
701706
if (!this->visit(SubExpr))
702707
return false;
@@ -1542,7 +1547,6 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15421547
return this->emitEQFixedPoint(E);
15431548
case BO_NE:
15441549
return this->emitNEFixedPoint(E);
1545-
#if 0
15461550
case BO_LT:
15471551
return this->emitLTFixedPoint(E);
15481552
case BO_LE:
@@ -1551,9 +1555,14 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15511555
return this->emitGTFixedPoint(E);
15521556
case BO_GE:
15531557
return this->emitGEFixedPoint(E);
1554-
#endif
15551558
case BO_Add:
15561559
return ConvertResult(this->emitAddFixedPoint(E));
1560+
case BO_Sub:
1561+
return ConvertResult(this->emitSubFixedPoint(E));
1562+
case BO_Mul:
1563+
return ConvertResult(this->emitMulFixedPoint(E));
1564+
case BO_Div:
1565+
return ConvertResult(this->emitDivFixedPoint(E));
15571566

15581567
default:
15591568
return this->emitInvalid(E);
@@ -1562,6 +1571,25 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15621571
llvm_unreachable("unhandled binop opcode");
15631572
}
15641573

1574+
template <class Emitter>
1575+
bool Compiler<Emitter>::VisitFixedPointUnaryOperator(const UnaryOperator *E) {
1576+
const Expr *SubExpr = E->getSubExpr();
1577+
assert(SubExpr->getType()->isFixedPointType());
1578+
1579+
switch (E->getOpcode()) {
1580+
case UO_Plus:
1581+
return this->delegate(SubExpr);
1582+
case UO_Minus:
1583+
if (!this->visit(SubExpr))
1584+
return false;
1585+
return this->emitNegFixedPoint(E);
1586+
default:
1587+
return false;
1588+
}
1589+
1590+
llvm_unreachable("Unhandled unary opcode");
1591+
}
1592+
15651593
template <class Emitter>
15661594
bool Compiler<Emitter>::VisitImplicitValueInitExpr(
15671595
const ImplicitValueInitExpr *E) {
@@ -3805,7 +3833,7 @@ bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
38053833
return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
38063834
case PT_FixedPoint: {
38073835
auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
3808-
return this->emitConstFixedPoint(FixedPoint::Zero(Sem), E);
3836+
return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
38093837
}
38103838
llvm_unreachable("Implement");
38113839
}
@@ -5471,6 +5499,8 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
54715499
return this->VisitComplexUnaryOperator(E);
54725500
if (SubExpr->getType()->isVectorType())
54735501
return this->VisitVectorUnaryOperator(E);
5502+
if (SubExpr->getType()->isFixedPointType())
5503+
return this->VisitFixedPointUnaryOperator(E);
54745504
std::optional<PrimType> T = classify(SubExpr->getType());
54755505

54765506
switch (E->getOpcode()) {

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
133133
bool VisitComplexBinOp(const BinaryOperator *E);
134134
bool VisitVectorBinOp(const BinaryOperator *E);
135135
bool VisitFixedPointBinOp(const BinaryOperator *E);
136+
bool VisitFixedPointUnaryOperator(const UnaryOperator *E);
136137
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E);
137138
bool VisitCallExpr(const CallExpr *E);
138139
bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinID);

clang/lib/AST/ByteCode/FixedPoint.h

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ class FixedPoint final {
3333
: V(APInt(0, 0ULL, false),
3434
llvm::FixedPointSemantics(0, 0, false, false, false)) {}
3535

36-
static FixedPoint Zero(llvm::FixedPointSemantics Sem) {
36+
static FixedPoint zero(llvm::FixedPointSemantics Sem) {
3737
return FixedPoint(APInt(Sem.getWidth(), 0ULL, Sem.isSigned()), Sem);
3838
}
3939

40-
operator bool() const { return V.getBoolValue(); }
41-
template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
42-
explicit operator Ty() const {
43-
// FIXME
44-
return 0;
40+
static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem,
41+
bool *Overflow) {
42+
return FixedPoint(llvm::APFixedPoint::getFromIntValue(I, Sem, Overflow));
43+
}
44+
static FixedPoint from(const llvm::APFloat &I, llvm::FixedPointSemantics Sem,
45+
bool *Overflow) {
46+
return FixedPoint(llvm::APFixedPoint::getFromFloatValue(I, Sem, Overflow));
4547
}
4648

49+
operator bool() const { return V.getBoolValue(); }
4750
void print(llvm::raw_ostream &OS) const { OS << V; }
4851

4952
APValue toAPValue(const ASTContext &) const { return APValue(V); }
@@ -55,9 +58,9 @@ class FixedPoint final {
5558
bool isNegative() const { return V.getValue().isNegative(); }
5659
bool isPositive() const { return V.getValue().isNonNegative(); }
5760
bool isMin() const {
58-
return V.getValue() == APSInt::getMinValue(V.getSemantics().getWidth(),
59-
!V.getSemantics().isSigned());
61+
return V == llvm::APFixedPoint::getMin(V.getSemantics());
6062
}
63+
bool isMinusOne() const { return V.isSigned() && V.getValue() == -1; }
6164

6265
FixedPoint truncate(unsigned BitWidth) const { return *this; }
6366

@@ -70,14 +73,21 @@ class FixedPoint final {
7073
return V.convertToFloat(*Sem);
7174
}
7275

76+
llvm::APSInt toInt(unsigned BitWidth, bool Signed, bool *Overflow) const {
77+
return V.convertToInt(BitWidth, Signed, Overflow);
78+
}
79+
7380
std::string toDiagnosticString(const ASTContext &Ctx) const {
7481
return V.toString();
7582
}
7683

7784
ComparisonCategoryResult compare(const FixedPoint &Other) const {
78-
if (Other.V == V)
85+
int c = V.compare(Other.V);
86+
if (c == 0)
7987
return ComparisonCategoryResult::Equal;
80-
return ComparisonCategoryResult::Unordered;
88+
else if (c < 0)
89+
return ComparisonCategoryResult::Less;
90+
return ComparisonCategoryResult::Greater;
8191
}
8292

8393
static bool neg(const FixedPoint &A, FixedPoint *R) {
@@ -94,16 +104,40 @@ class FixedPoint final {
94104
}
95105
static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits,
96106
FixedPoint *R) {
97-
return true;
107+
bool Overflow = false;
108+
*R = FixedPoint(A.V.sub(B.V, &Overflow));
109+
return Overflow;
98110
}
99111
static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits,
100112
FixedPoint *R) {
101-
return true;
113+
bool Overflow = false;
114+
*R = FixedPoint(A.V.mul(B.V, &Overflow));
115+
return Overflow;
102116
}
103117
static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits,
104118
FixedPoint *R) {
119+
bool Overflow = false;
120+
*R = FixedPoint(A.V.div(B.V, &Overflow));
121+
return Overflow;
122+
}
123+
static bool rem(const FixedPoint A, const FixedPoint B, unsigned Bits,
124+
FixedPoint *R) {
125+
llvm_unreachable("Rem doesn't exist for fixed point values");
126+
return true;
127+
}
128+
static bool bitAnd(const FixedPoint A, const FixedPoint B, unsigned Bits,
129+
FixedPoint *R) {
105130
return true;
106131
}
132+
static bool bitOr(const FixedPoint A, const FixedPoint B, unsigned Bits,
133+
FixedPoint *R) {
134+
return true;
135+
}
136+
static bool bitXor(const FixedPoint A, const FixedPoint B, unsigned Bits,
137+
FixedPoint *R) {
138+
return true;
139+
}
140+
107141
static bool increment(const FixedPoint &A, FixedPoint *R) { return true; }
108142
static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; }
109143
};

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,19 @@ bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E) {
13931393
return false;
13941394
}
13951395

1396+
bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC,
1397+
const FixedPoint &FP) {
1398+
const Expr *E = S.Current->getExpr(OpPC);
1399+
if (S.checkingForUndefinedBehavior()) {
1400+
S.getASTContext().getDiagnostics().Report(
1401+
E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
1402+
<< FP.toDiagnosticString(S.getASTContext()) << E->getType();
1403+
}
1404+
S.CCEDiag(E, diag::note_constexpr_overflow)
1405+
<< FP.toDiagnosticString(S.getASTContext()) << E->getType();
1406+
return S.noteUndefinedBehavior();
1407+
}
1408+
13961409
bool Interpret(InterpState &S, APValue &Result) {
13971410
// The current stack frame when we started Interpret().
13981411
// This is being used by the ops to determine wheter

0 commit comments

Comments
 (0)