[clang] 6fd870b - [clang][bytecode] Implement zero-init for fixed point types (#110257)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 27 09:29:37 PDT 2024


Author: Timm Baeder
Date: 2024-09-27T18:29:34+02:00
New Revision: 6fd870bf3585db7d9b4af5e4b95bc731bbf9c0fa

URL: https://github.com/llvm/llvm-project/commit/6fd870bf3585db7d9b4af5e4b95bc731bbf9c0fa
DIFF: https://github.com/llvm/llvm-project/commit/6fd870bf3585db7d9b4af5e4b95bc731bbf9c0fa.diff

LOG: [clang][bytecode] Implement zero-init for fixed point types (#110257)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/lib/AST/ByteCode/FixedPoint.h
    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 78ba1a7eec6620..2520fe30547b23 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -3699,7 +3699,10 @@ bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
     return this->emitNullMemberPtr(nullptr, E);
   case PT_Float:
     return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
-  case PT_FixedPoint:
+  case PT_FixedPoint: {
+    auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
+    return this->emitConstFixedPoint(FixedPoint::Zero(Sem), E);
+  }
     llvm_unreachable("Implement");
   }
   llvm_unreachable("unknown primitive type");

diff  --git a/clang/lib/AST/ByteCode/FixedPoint.h b/clang/lib/AST/ByteCode/FixedPoint.h
index fba793cd59e7e1..9dd300c3dbf6df 100644
--- a/clang/lib/AST/ByteCode/FixedPoint.h
+++ b/clang/lib/AST/ByteCode/FixedPoint.h
@@ -32,6 +32,10 @@ class FixedPoint final {
       : V(APInt(0, 0ULL, false),
           llvm::FixedPointSemantics(0, 0, false, false, false)) {}
 
+  static FixedPoint Zero(llvm::FixedPointSemantics Sem) {
+    return FixedPoint(APInt(Sem.getWidth(), 0ULL, Sem.isSigned()), Sem);
+  }
+
   operator bool() const { return V.getBoolValue(); }
   template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
   explicit operator Ty() const {

diff  --git a/clang/test/AST/ByteCode/fixed-point.cpp b/clang/test/AST/ByteCode/fixed-point.cpp
index 42ebdf64e1a9fe..bf1bd41783448f 100644
--- a/clang/test/AST/ByteCode/fixed-point.cpp
+++ b/clang/test/AST/ByteCode/fixed-point.cpp
@@ -9,3 +9,6 @@ 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)));
 
+/// Zero-init.
+constexpr _Accum A{};
+static_assert(A == 0.0k);


        


More information about the cfe-commits mailing list