[clang] defead4 - [clang][bytecode] Implement fixed-point add (#110405)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 28 22:13:21 PDT 2024
Author: Timm Baeder
Date: 2024-09-29T07:13:17+02:00
New Revision: defead4d2dfc18fe423792005e99edc7b14edf2f
URL: https://github.com/llvm/llvm-project/commit/defead4d2dfc18fe423792005e99edc7b14edf2f
DIFF: https://github.com/llvm/llvm-project/commit/defead4d2dfc18fe423792005e99edc7b14edf2f.diff
LOG: [clang][bytecode] Implement fixed-point add (#110405)
Added:
Modified:
clang/lib/AST/ByteCode/Compiler.cpp
clang/lib/AST/ByteCode/FixedPoint.h
clang/lib/AST/ByteCode/Opcodes.td
clang/test/AST/ByteCode/fixed-point.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 77bfb3d8cd7f1a..ef058e8da44b34 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1528,6 +1528,9 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
case BO_GE:
return this->emitGEFixedPoint(E);
#endif
+ case BO_Add:
+ return this->emitAddFixedPoint(E);
+
default:
return this->emitInvalid(E);
}
diff --git a/clang/lib/AST/ByteCode/FixedPoint.h b/clang/lib/AST/ByteCode/FixedPoint.h
index fb8457a08e93cc..0a808e13eda4eb 100644
--- a/clang/lib/AST/ByteCode/FixedPoint.h
+++ b/clang/lib/AST/ByteCode/FixedPoint.h
@@ -47,15 +47,28 @@ class FixedPoint final {
void print(llvm::raw_ostream &OS) const { OS << V; }
APValue toAPValue(const ASTContext &) const { return APValue(V); }
- APSInt toAPSInt(unsigned BitWidth) const { return V.getValue(); }
+ APSInt toAPSInt(unsigned BitWidth = 0) const { return V.getValue(); }
unsigned bitWidth() const { return V.getWidth(); }
bool isSigned() const { return V.isSigned(); }
+ bool isZero() const { return V.getValue().isZero(); }
+ bool isNegative() const { return V.getValue().isNegative(); }
+ bool isPositive() const { return V.getValue().isNonNegative(); }
+ bool isMin() const {
+ return V.getValue() == APSInt::getMinValue(V.getSemantics().getWidth(),
+ !V.getSemantics().isSigned());
+ }
+
+ FixedPoint truncate(unsigned BitWidth) const { return *this; }
llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
return V.convertToFloat(*Sem);
}
+ std::string toDiagnosticString(const ASTContext &Ctx) const {
+ return V.toString();
+ }
+
ComparisonCategoryResult compare(const FixedPoint &Other) const {
if (Other.V == V)
return ComparisonCategoryResult::Equal;
@@ -67,6 +80,27 @@ class FixedPoint final {
*R = FixedPoint(A.V.negate(&Overflow));
return Overflow;
}
+
+ static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits,
+ FixedPoint *R) {
+ bool Overflow = false;
+ *R = FixedPoint(A.V.add(B.V, &Overflow));
+ return Overflow;
+ }
+ static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits,
+ FixedPoint *R) {
+ return true;
+ }
+ static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits,
+ FixedPoint *R) {
+ return true;
+ }
+ static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits,
+ FixedPoint *R) {
+ return true;
+ }
+ static bool increment(const FixedPoint &A, FixedPoint *R) { return true; }
+ static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; }
};
inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 4f11b927989004..ceea2accc22eff 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -98,7 +98,7 @@ def FloatTypeClass : TypeClass {
}
def AluTypeClass : TypeClass {
- let Types = !listconcat(IntegerTypeClass.Types, [Bool]);
+ let Types = !listconcat(IntegerTypeClass.Types, [Bool], [FixedPoint]);
}
def PtrTypeClass : TypeClass {
@@ -110,7 +110,7 @@ def NonPtrTypeClass : TypeClass {
}
def AllTypeClass : TypeClass {
- let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types, FloatTypeClass.Types, [FixedPoint]);
+ let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types, FloatTypeClass.Types);
}
def ComparableTypeClass : TypeClass {
diff --git a/clang/test/AST/ByteCode/fixed-point.cpp b/clang/test/AST/ByteCode/fixed-point.cpp
index 68137618b5db60..03324c79fc9cae 100644
--- a/clang/test/AST/ByteCode/fixed-point.cpp
+++ b/clang/test/AST/ByteCode/fixed-point.cpp
@@ -35,3 +35,15 @@ namespace FloatToFixedPointCast {
constexpr float sf2f = sf2;
static_assert(sf2f == 0.5);
}
+
+namespace BinOps {
+ constexpr _Accum A = 13;
+ static_assert(A + 1 == 14.0k);
+ static_assert(1 + A == 14.0k);
+ static_assert((A + A) == 26);
+
+ /// FIXME: Conversion between fixed point semantics.
+ static_assert(A + 100000 == 14.0k); // expected-error {{static assertion failed}} \
+ // ref-error {{is not an integral constant expression}} \
+ // ref-note {{is outside the range of representable values}}
+}
More information about the cfe-commits
mailing list