Skip to content

[clang][bytecode] Implement fixed point negation #110237

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
Sep 27, 2024
Merged

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/110237.diff

5 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+2-2)
  • (modified) clang/lib/AST/ByteCode/FixedPoint.h (+13-3)
  • (modified) clang/lib/AST/ByteCode/Opcodes.td (+1-1)
  • (modified) clang/lib/AST/ByteCode/PrimType.h (+6-6)
  • (modified) clang/test/AST/ByteCode/fixed-point.cpp (+2)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index aac3fd384130d7..78ba1a7eec6620 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -724,9 +724,9 @@ bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) {
   assert(E->getType()->isFixedPointType());
   assert(classifyPrim(E) == PT_FixedPoint);
 
-  // FIXME: Semantics.
+  auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
   APInt Value = E->getValue();
-  return this->emitConstFixedPoint(Value, E);
+  return this->emitConstFixedPoint(FixedPoint(Value, Sem), E);
 }
 
 template <class Emitter>
diff --git a/clang/lib/AST/ByteCode/FixedPoint.h b/clang/lib/AST/ByteCode/FixedPoint.h
index 5c4043f060ec56..fba793cd59e7e1 100644
--- a/clang/lib/AST/ByteCode/FixedPoint.h
+++ b/clang/lib/AST/ByteCode/FixedPoint.h
@@ -17,16 +17,16 @@ namespace clang {
 namespace interp {
 
 using APInt = llvm::APInt;
+using APSInt = llvm::APSInt;
 
 /// Wrapper around fixed point types.
 class FixedPoint final {
 private:
   llvm::APFixedPoint V;
+  FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}
 
 public:
-  FixedPoint(APInt V)
-      : V(V,
-          llvm::FixedPointSemantics(V.getBitWidth(), 0, false, false, false)) {}
+  FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {}
   // This needs to be default-constructible so llvm::endian::read works.
   FixedPoint()
       : V(APInt(0, 0ULL, false),
@@ -42,12 +42,22 @@ 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(); }
+
+  unsigned bitWidth() const { return V.getWidth(); }
+  bool isSigned() const { return V.isSigned(); }
 
   ComparisonCategoryResult compare(const FixedPoint &Other) const {
     if (Other.V == V)
       return ComparisonCategoryResult::Equal;
     return ComparisonCategoryResult::Unordered;
   }
+
+  static bool neg(const FixedPoint &A, FixedPoint *R) {
+    bool Overflow = false;
+    *R = FixedPoint(A.V.negate(&Overflow));
+    return Overflow;
+  }
 };
 
 inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 84c5a1d1ab4c0d..5fdafd1bf81984 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -106,7 +106,7 @@ def PtrTypeClass : TypeClass {
 }
 
 def NonPtrTypeClass : TypeClass {
-  let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float]);
+  let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float], [FixedPoint]);
 }
 
 def AllTypeClass : TypeClass {
diff --git a/clang/lib/AST/ByteCode/PrimType.h b/clang/lib/AST/ByteCode/PrimType.h
index 23ca8027599cd5..59c04c4673d936 100644
--- a/clang/lib/AST/ByteCode/PrimType.h
+++ b/clang/lib/AST/ByteCode/PrimType.h
@@ -43,11 +43,11 @@ enum PrimType : unsigned {
   PT_IntAP = 8,
   PT_IntAPS = 9,
   PT_Bool = 10,
-  PT_Float = 11,
-  PT_Ptr = 12,
-  PT_FnPtr = 13,
-  PT_MemberPtr = 14,
-  PT_FixedPoint = 15,
+  PT_FixedPoint = 11,
+  PT_Float = 12,
+  PT_Ptr = 13,
+  PT_FnPtr = 14,
+  PT_MemberPtr = 15,
 };
 
 inline constexpr bool isPtrType(PrimType T) {
@@ -71,7 +71,7 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
   return OS;
 }
 
-constexpr bool isIntegralType(PrimType T) { return T <= PT_Bool; }
+constexpr bool isIntegralType(PrimType T) { return T <= PT_FixedPoint; }
 
 /// Mapping from primitive types to their representation.
 template <PrimType T> struct PrimConv;
diff --git a/clang/test/AST/ByteCode/fixed-point.cpp b/clang/test/AST/ByteCode/fixed-point.cpp
index 24595ed96c166d..42ebdf64e1a9fe 100644
--- a/clang/test/AST/ByteCode/fixed-point.cpp
+++ b/clang/test/AST/ByteCode/fixed-point.cpp
@@ -7,3 +7,5 @@ static_assert((bool)0.0k); // both-error {{static assertion failed}}
 
 static_assert(1.0k == 1.0k);
 static_assert(1.0k != 1.0k); // both-error {{failed due to requirement '1.0k != 1.0k'}}
+static_assert(-12.0k == -(-(-12.0k)));
+

@tbaederr tbaederr merged commit 581c015 into llvm:main Sep 27, 2024
9 of 11 checks passed
Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Sep 27, 2024
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants