[clang] [clang][Interp] Implement inc/dec for IntegralAP (PR #69597)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 19 05:29:01 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
The tests are commented out and I can't merge this yet, since it creates memory leaks when the interpretation is aborted midway through.
This problem also exists with floating-point values:
```c++
constexpr long double a() {
long double A = __LDBL_MAX__;
throw;
return A;
}
```
We will allocate a `APFloat` into the stack frame and not call its destructor later.
---
Full diff: https://github.com/llvm/llvm-project/pull/69597.diff
2 Files Affected:
- (modified) clang/lib/AST/Interp/IntegralAP.h (+11-10)
- (modified) clang/test/AST/Interp/intap.cpp (+55-7)
``````````diff
diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index 45e5b49546270aa..2e68c59ee80d436 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -116,7 +116,9 @@ template <bool Signed> class IntegralAP final {
constexpr unsigned bitWidth() const { return V.getBitWidth(); }
- APSInt toAPSInt(unsigned Bits = 0) const { return APSInt(V, Signed); }
+ APSInt toAPSInt(unsigned Bits = 0) const {
+ return APSInt(V, Signed).extend(Bits);
+ }
APValue toAPValue() const { return APValue(APSInt(V, Signed)); }
bool isZero() const { return V.isZero(); }
@@ -167,17 +169,13 @@ template <bool Signed> class IntegralAP final {
}
static bool increment(IntegralAP A, IntegralAP *R) {
- // FIXME: Implement.
- assert(false);
- *R = IntegralAP(A.V - 1);
- return false;
+ IntegralAP<Signed> One(1, A.bitWidth());
+ return add(A, One, A.bitWidth() + 1, R);
}
static bool decrement(IntegralAP A, IntegralAP *R) {
- // FIXME: Implement.
- assert(false);
- *R = IntegralAP(A.V - 1);
- return false;
+ IntegralAP<Signed> One(1, A.bitWidth());
+ return sub(A, One, A.bitWidth() + 1, R);
}
static bool add(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
@@ -246,7 +244,10 @@ template <bool Signed> class IntegralAP final {
static void shiftRight(const IntegralAP A, const IntegralAP B,
unsigned OpBits, IntegralAP *R) {
- *R = IntegralAP(A.V.ashr(B.V.getZExtValue()));
+ if constexpr (Signed)
+ *R = IntegralAP(A.V.ashr(B.V.getZExtValue()));
+ else
+ *R = IntegralAP(A.V.lshr(B.V.getZExtValue()));
}
private:
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index 27fae1b904351ce..9d947a092bb7440 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -29,9 +29,17 @@ static_assert(UBitIntZero1 == 0, "");
#ifdef __SIZEOF_INT128__
+typedef __int128 int128_t;
+typedef unsigned __int128 uint128_t;
+static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
+static_assert(UINT128_MAX == -1, "");
+
+static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
+static_assert(INT128_MAX != 0, "");
+static const __int128_t INT128_MIN = -INT128_MAX - 1;
+
namespace i128 {
- typedef __int128 int128_t;
- typedef unsigned __int128 uint128_t;
+
constexpr int128_t I128_1 = 12;
static_assert(I128_1 == 12, "");
static_assert(I128_1 != 10, "");
@@ -40,12 +48,7 @@ namespace i128 {
// expected-note{{evaluates to}} \
// ref-note{{evaluates to}}
- static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
- static_assert(UINT128_MAX == -1, "");
- static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
- static_assert(INT128_MAX != 0, "");
- static const __int128_t INT128_MIN = -INT128_MAX - 1;
constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{outside the range}} \
// ref-error {{must be initialized by a constant expression}} \
@@ -118,4 +121,49 @@ namespace AddSubOffset {
static_assert(*P2 == 1,"");
}
+namespace IncDec {
+#if 0
+ constexpr int128_t maxPlus1(bool Pre) {
+ int128_t a = INT128_MAX;
+
+ if (Pre)
+ ++a; // ref-note {{value 170141183460469231731687303715884105728 is outside the range}} \
+ // expected-note {{value 170141183460469231731687303715884105728 is outside the range}}
+ else
+ a++;
+ return a;
+ }
+ static_assert(maxPlus1(true) == 0, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note in call to}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note in call to}}
+ static_assert(maxPlus1(false) == 0, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note in call to}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note in call to}}
+
+ constexpr int128_t inc1(bool Pre) {
+ int128_t A = 0;
+ if (Pre)
+ ++A;
+ else
+ A++;
+ return A;
+ }
+ static_assert(inc1(true) == 1, "");
+ static_assert(inc1(false) == 1, "");
+
+ constexpr int128_t dec1(bool Pre) {
+ int128_t A = 2;
+ if (Pre)
+ --A;
+ else
+ A--;
+ return A;
+ }
+ static_assert(dec1(true) == 1, "");
+ static_assert(dec1(false) == 1, "");
+#endif
+}
+
#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/69597
More information about the cfe-commits
mailing list