diff options
author | Nikita Popov <npopov@redhat.com> | 2024-09-02 09:48:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-02 09:48:54 +0200 |
commit | 30cc198c2d4ad784f18cc10a03d45a19145357af (patch) | |
tree | 04c64c8d8bf268218368efe07a6f5f8753595a98 /llvm/lib/Support/APInt.cpp | |
parent | e4e0dfb0c24c9bcd4bef835bd6a162967f097584 (diff) | |
download | llvm-30cc198c2d4ad784f18cc10a03d45a19145357af.zip llvm-30cc198c2d4ad784f18cc10a03d45a19145357af.tar.gz llvm-30cc198c2d4ad784f18cc10a03d45a19145357af.tar.bz2 |
[APInt] Add default-disabled assertion to APInt constructor (#106524)
If the uint64_t constructor is used, assert that the value is actually a
signed or unsigned N-bit integer depending on whether the isSigned flag
is set. Provide an implicitTrunc flag to restore the previous behavior,
where the argument is silently truncated instead.
In this commit, implicitTrunc is enabled by default, which means that
the new assertions are disabled and no actual change in behavior occurs.
The plan is to flip the default once all places violating the assertion
have been fixed. See #80309 for the scope of the necessary changes.
The primary motivation for this change is to avoid incorrectly specified
isSigned flags. A recurring problem we have is that people write
something like `APInt(BW, -1)` and this works perfectly fine -- until
the code path is hit with `BW > 64`. Most of our i128 specific
miscompilations are caused by variants of this issue.
The cost of the change is that we have to specify the correct isSigned
flag (and make sure there are no excess bits) for uses where BW is
always <= 64 as well.
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r-- | llvm/lib/Support/APInt.cpp | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index fe22e9b..78d5739 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -234,7 +234,8 @@ APInt& APInt::operator-=(uint64_t RHS) { APInt APInt::operator*(const APInt& RHS) const { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); if (isSingleWord()) - return APInt(BitWidth, U.VAL * RHS.U.VAL); + return APInt(BitWidth, U.VAL * RHS.U.VAL, /*isSigned=*/false, + /*implicitTrunc=*/true); APInt Result(getMemory(getNumWords()), getBitWidth()); tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords()); @@ -455,7 +456,8 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { "Illegal bit extraction"); if (isSingleWord()) - return APInt(numBits, U.VAL >> bitPosition); + return APInt(numBits, U.VAL >> bitPosition, /*isSigned=*/false, + /*implicitTrunc=*/true); unsigned loBit = whichBit(bitPosition); unsigned loWord = whichWord(bitPosition); @@ -463,7 +465,8 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const { // Single word result extracting bits from a single word source. if (loWord == hiWord) - return APInt(numBits, U.pVal[loWord] >> loBit); + return APInt(numBits, U.pVal[loWord] >> loBit, /*isSigned=*/false, + /*implicitTrunc=*/true); // Extracting bits that start on a source word boundary can be done // as a fast memory copy. @@ -907,7 +910,8 @@ APInt APInt::trunc(unsigned width) const { assert(width <= BitWidth && "Invalid APInt Truncate request"); if (width <= APINT_BITS_PER_WORD) - return APInt(width, getRawData()[0]); + return APInt(width, getRawData()[0], /*isSigned=*/false, + /*implicitTrunc=*/true); if (width == BitWidth) return *this; @@ -955,7 +959,7 @@ APInt APInt::sext(unsigned Width) const { assert(Width >= BitWidth && "Invalid APInt SignExtend request"); if (Width <= APINT_BITS_PER_WORD) - return APInt(Width, SignExtend64(U.VAL, BitWidth)); + return APInt(Width, SignExtend64(U.VAL, BitWidth), /*isSigned=*/true); if (Width == BitWidth) return *this; |