aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/MathExtrasTest.cpp
diff options
context:
space:
mode:
authorThéo Degioanni <theo.degioanni.llvm.deluge062@simplelogin.fr>2024-04-22 20:54:22 +0200
committerGitHub <noreply@github.com>2024-04-22 20:54:22 +0200
commita54102a093190f3a29add5d9327e62f13fce896a (patch)
tree3bdd21904ed623829f7150601a489c8ba3c7744c /llvm/unittests/Support/MathExtrasTest.cpp
parent43c26bbc425dbd8caee311a0d5e4d90c8e71d0d8 (diff)
downloadllvm-a54102a093190f3a29add5d9327e62f13fce896a.zip
llvm-a54102a093190f3a29add5d9327e62f13fce896a.tar.gz
llvm-a54102a093190f3a29add5d9327e62f13fce896a.tar.bz2
[llvm] Add support for zero-width integers in MathExtras.h (#87193)
MLIR uses zero-width integers, but also re-uses integer logic from LLVM to avoid duplication. This creates issues when LLVM logic is used in MLIR on integers which can be zero-width. In order to avoid special-casing the bitwidth-related logic in MLIR, this PR adds support for zero-width integers in LLVM's MathExtras (and consequently APInt). While most of the logic in theory works the same way out of the box, because bitshifting right by the entire bitwidth in C++ is undefined behavior instead of being zero, some special cases had to be added. Fortunately, it seems like the performance penalty is small. In x86, this usually yields the addition of a predicated conditional move. I checked that no branch is inserted in Arm too. This happens to fix a crash in `arith.extsi` canonicalization in MLIR. I think a follow-up PR to add tests for i0 in arith would be beneficial.
Diffstat (limited to 'llvm/unittests/Support/MathExtrasTest.cpp')
-rw-r--r--llvm/unittests/Support/MathExtrasTest.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp
index 72c765d..2186558 100644
--- a/llvm/unittests/Support/MathExtrasTest.cpp
+++ b/llvm/unittests/Support/MathExtrasTest.cpp
@@ -41,6 +41,9 @@ TEST(MathExtras, onesMask) {
TEST(MathExtras, isIntN) {
EXPECT_TRUE(isIntN(16, 32767));
EXPECT_FALSE(isIntN(16, 32768));
+ EXPECT_TRUE(isUIntN(0, 0));
+ EXPECT_FALSE(isUIntN(0, 1));
+ EXPECT_FALSE(isUIntN(0, -1));
}
TEST(MathExtras, isUIntN) {
@@ -48,6 +51,8 @@ TEST(MathExtras, isUIntN) {
EXPECT_FALSE(isUIntN(16, 65536));
EXPECT_TRUE(isUIntN(1, 0));
EXPECT_TRUE(isUIntN(6, 63));
+ EXPECT_TRUE(isUIntN(0, 0));
+ EXPECT_FALSE(isUIntN(0, 1));
}
TEST(MathExtras, maxIntN) {
@@ -55,6 +60,7 @@ TEST(MathExtras, maxIntN) {
EXPECT_EQ(2147483647, maxIntN(32));
EXPECT_EQ(std::numeric_limits<int32_t>::max(), maxIntN(32));
EXPECT_EQ(std::numeric_limits<int64_t>::max(), maxIntN(64));
+ EXPECT_EQ(0, maxIntN(0));
}
TEST(MathExtras, minIntN) {
@@ -62,6 +68,7 @@ TEST(MathExtras, minIntN) {
EXPECT_EQ(-64LL, minIntN(7));
EXPECT_EQ(std::numeric_limits<int32_t>::min(), minIntN(32));
EXPECT_EQ(std::numeric_limits<int64_t>::min(), minIntN(64));
+ EXPECT_EQ(0, minIntN(0));
}
TEST(MathExtras, maxUIntN) {
@@ -70,6 +77,7 @@ TEST(MathExtras, maxUIntN) {
EXPECT_EQ(0xffffffffffffffffULL, maxUIntN(64));
EXPECT_EQ(1ULL, maxUIntN(1));
EXPECT_EQ(0x0fULL, maxUIntN(4));
+ EXPECT_EQ(0, maxUIntN(0));
}
TEST(MathExtras, reverseBits) {