Skip to content
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

[APInt] Fix getAllOnes() with zero width #112227

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Oct 14, 2024

This makes sure that APInt::getAllOnes() keeps working after the APInt constructor assertions are enabled.

I'm relaxing the requirement for the signed case to either an all zeros or all ones integer. This is basically saying that we can interpret the zero-width integer as either positive or negative.

This makes sure that APInt::getAllOnes() keeps working after
the APInt constructor assertions are enabled.

I'm relaxing the requirement for the signed case to either
an all zeros or all ones integer.
@nikic nikic requested a review from dtcxzyw October 14, 2024 15:54
@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2024

@llvm/pr-subscribers-llvm-adt

Author: Nikita Popov (nikic)

Changes

This makes sure that APInt::getAllOnes() keeps working after the APInt constructor assertions are enabled.

I'm relaxing the requirement for the signed case to either an all zeros or all ones integer. This is basically saying that we can interpret the zero-width integer as either positive or negative.


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/APInt.h (+14-7)
  • (modified) llvm/unittests/ADT/APIntTest.cpp (+1)
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index a42dae8887392d..63a138527b32e1 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -112,14 +112,21 @@ class [[nodiscard]] APInt {
         bool implicitTrunc = true)
       : BitWidth(numBits) {
     if (!implicitTrunc) {
-      if (BitWidth == 0) {
-        assert(val == 0 && "Value must be zero for 0-bit APInt");
-      } else if (isSigned) {
-        assert(llvm::isIntN(BitWidth, val) &&
-               "Value is not an N-bit signed value");
+      if (isSigned) {
+        if (BitWidth == 0) {
+          assert((val == 0 || val == uint64_t(-1)) &&
+                 "Value must be 0 or -1 for signed 0-bit APInt");
+        } else {
+          assert(llvm::isIntN(BitWidth, val) &&
+                 "Value is not an N-bit signed value");
+        }
       } else {
-        assert(llvm::isUIntN(BitWidth, val) &&
-               "Value is not an N-bit unsigned value");
+        if (BitWidth == 0) {
+          assert(val == 0 && "Value must be zero for unsigned 0-bit APInt");
+        } else {
+          assert(llvm::isUIntN(BitWidth, val) &&
+                 "Value is not an N-bit unsigned value");
+        }
       }
     }
     if (isSingleWord()) {
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 7a9ac5562e7722..4d5553fcbd1e3f 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -3423,6 +3423,7 @@ TEST(APIntTest, ZeroWidth) {
   EXPECT_EQ(0U, ZW.getBitWidth());
   EXPECT_EQ(0U, APInt(0, ArrayRef<uint64_t>({0, 1, 2})).getBitWidth());
   EXPECT_EQ(0U, APInt(0, "0", 10).getBitWidth());
+  EXPECT_EQ(0U, APInt::getAllOnes(0).getBitWidth());
 
   // Default constructor is single bit wide.
   EXPECT_EQ(1U, APInt().getBitWidth());

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG.

@nikic nikic merged commit c180da9 into llvm:main Oct 15, 2024
10 checks passed
@nikic nikic deleted the apint-allones-zerobw-fix branch October 15, 2024 07:33
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
This makes sure that APInt::getAllOnes() keeps working after the APInt
constructor assertions are enabled.

I'm relaxing the requirement for the signed case to either an all zeros
or all ones integer. This is basically saying that we can interpret the
zero-width integer as either positive or negative.
bricknerb pushed a commit to bricknerb/llvm-project that referenced this pull request Oct 17, 2024
This makes sure that APInt::getAllOnes() keeps working after the APInt
constructor assertions are enabled.

I'm relaxing the requirement for the signed case to either an all zeros
or all ones integer. This is basically saying that we can interpret the
zero-width integer as either positive or negative.
EricWF pushed a commit to efcs/llvm-project that referenced this pull request Oct 22, 2024
This makes sure that APInt::getAllOnes() keeps working after the APInt
constructor assertions are enabled.

I'm relaxing the requirement for the signed case to either an all zeros
or all ones integer. This is basically saying that we can interpret the
zero-width integer as either positive or negative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants