[clang] [clang][Interp] IntegralAP zero-init (PR #68081)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 12 23:21:01 PDT 2023
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/68081
>From a7f0b32783bd8e9ce4d74d98e5a45351d3b38d24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 3 Oct 2023 11:05:27 +0200
Subject: [PATCH] [clang][Interp] IntegralAP zero-init
---
clang/lib/AST/Interp/ByteCodeExprGen.cpp | 4 ++--
clang/lib/AST/Interp/IntegralAP.h | 8 ++++----
clang/lib/AST/Interp/Interp.h | 10 ++++++++++
clang/lib/AST/Interp/Opcodes.td | 15 ++++++++++++++-
clang/test/AST/Interp/intap.cpp | 15 +++++++++++++++
5 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 2b745d6a1509868..c0a8d4f1b208ee9 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1669,9 +1669,9 @@ bool ByteCodeExprGen<Emitter>::visitZeroInitializer(QualType QT,
case PT_Uint64:
return this->emitZeroUint64(E);
case PT_IntAP:
+ return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
case PT_IntAPS:
- assert(false);
- return false;
+ return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
case PT_Ptr:
return this->emitNullPtr(E);
case PT_FnPtr:
diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index f9a33bbcd7bd7fa..c624845ce6b8b83 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -103,12 +103,12 @@ template <bool Signed> class IntegralAP final {
}
static IntegralAP from(const Boolean &B) {
assert(false);
- return IntegralAP::zero();
+ return IntegralAP::zero(1);
}
- static IntegralAP zero() {
- assert(false);
- return IntegralAP(0);
+ static IntegralAP zero(int32_t BitWidth) {
+ APSInt V = APSInt(APInt(BitWidth, 0LL, Signed), !Signed);
+ return IntegralAP(V);
}
constexpr unsigned bitWidth() const { return V.getBitWidth(); }
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 1ad3b8bfc7711d3..fa7e137ca56c05e 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1685,6 +1685,16 @@ bool Zero(InterpState &S, CodePtr OpPC) {
return true;
}
+static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
+ S.Stk.push<IntegralAP<false>>(IntegralAP<false>::zero(BitWidth));
+ return true;
+}
+
+static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
+ S.Stk.push<IntegralAP<true>>(IntegralAP<true>::zero(BitWidth));
+ return true;
+}
+
template <PrimType Name, class T = typename PrimConv<Name>::T>
inline bool Null(InterpState &S, CodePtr OpPC) {
S.Stk.push<T>();
diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td
index 9d390fed152417f..e1e7e5e2efbb059 100644
--- a/clang/lib/AST/Interp/Opcodes.td
+++ b/clang/lib/AST/Interp/Opcodes.td
@@ -72,6 +72,11 @@ def IntegerTypeClass : TypeClass {
Uint32, Sint64, Uint64, IntAP, IntAPS];
}
+def FixedSizeIntegralTypeClass : TypeClass {
+ let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
+ Uint32, Sint64, Uint64, Bool];
+}
+
def NumberTypeClass : TypeClass {
let Types = !listconcat(IntegerTypeClass.Types, [Float]);
}
@@ -243,10 +248,18 @@ def ConstBool : ConstOpcode<Bool, ArgBool>;
// [] -> [Integer]
def Zero : Opcode {
- let Types = [AluTypeClass];
+ let Types = [FixedSizeIntegralTypeClass];
let HasGroup = 1;
}
+def ZeroIntAP : Opcode {
+ let Args = [ArgUint32];
+}
+
+def ZeroIntAPS : Opcode {
+ let Args = [ArgUint32];
+}
+
// [] -> [Pointer]
def Null : Opcode {
let Types = [PtrTypeClass];
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index 8fe65a69a4fee8d..65c02f801ecf817 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -17,6 +17,16 @@ constexpr MaxBitInt A_ = 0;
constexpr MaxBitInt B_ = A_ + 1;
static_assert(B_ == 1, "");
+constexpr MaxBitInt BitIntZero{};
+static_assert(BitIntZero == 0, "");
+constexpr unsigned _BitInt(128) UBitIntZero{};
+static_assert(UBitIntZero == 0, "");
+
+constexpr _BitInt(2) BitIntZero2{};
+static_assert(BitIntZero2 == 0, "");
+constexpr unsigned _BitInt(1) UBitIntZero1{};
+static_assert(UBitIntZero1 == 0, "");
+
#ifdef __SIZEOF_INT128__
namespace i128 {
@@ -46,6 +56,11 @@ namespace i128 {
constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
static_assert(AllOnes == UINT128_MAX, "");
+ constexpr uint128_t i128Zero{};
+ static_assert(i128Zero == 0, "");
+ constexpr uint128_t ui128Zero{};
+ static_assert(ui128Zero == 0, "");
+
#if __cplusplus >= 201402L
template <typename T>
constexpr T CastFrom(__int128_t A) {
More information about the cfe-commits
mailing list