[PATCH] D114768: [ADT][Support] Remove zero-width assertions
Schuyler Eldridge via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 29 22:37:53 PST 2021
seldridge created this revision.
Herald added a subscriber: dexonsmith.
seldridge published this revision for review.
seldridge added a comment.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This diff removes restrictions around zero/sign extension of zero-width `APInt`s. Specifically, there is a restriction introduced in https://reviews.llvm.org/D109555 that disallows zero extension of zero-width `APInt`s. Additionally, there are some old asserts inside `MathExtras`, added in https://reviews.llvm.org/D22442, which assert on sign extension of any zero-width number (which are used pervasively inside `APInt` code to implement its sign extension). This is attempting to better enable the CIRCT project to rely on `APInt` to represent zero-width wires without introducing lots of additional guards to work around `APInt` methods which assert when used. More information on what specifically this is attempting to solve for CIRCT is here: https://github.com/llvm/circt/issues/2255
Remove assertion that disallows zero-extending a zero-width APInt. This
check is too restrictive and makes it difficult to use APInt to model
zero-width things, e.g., zero-width wires in the CIRCT project.
Remove assertions inside MathExtras that disallow zero or sign extending
zero-width things. Define behavior for this such that zero or sign
extending a zero-width thing returns zero. This behavior is added to
better support zero-width APInts which use MathExtras internally and it
is undesirable to duplicate MathExtras just to support this in APInt.
Signed-off-by: Schuyler Eldridge <schuyler.eldridge at sifive.com>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D114768
Files:
llvm/include/llvm/ADT/APInt.h
llvm/include/llvm/Support/MathExtras.h
Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -757,34 +757,38 @@
}
/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
-/// Requires 0 < B <= 32.
+/// Requires 0 <= B <= 32. Sign extension of a zero-width value returns zero.
template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
- static_assert(B > 0, "Bit width can't be 0.");
static_assert(B <= 32, "Bit width out of range.");
+ if (!B)
+ return 0;
return int32_t(X << (32 - B)) >> (32 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
-/// Requires 0 < B <= 32.
+/// Requires 0 <= B <= 32. Sign extension of a zero-width value returns zero.
inline int32_t SignExtend32(uint32_t X, unsigned B) {
- assert(B > 0 && "Bit width can't be 0.");
assert(B <= 32 && "Bit width out of range.");
+ if (!B)
+ return 0;
return int32_t(X << (32 - B)) >> (32 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
-/// Requires 0 < B <= 64.
+/// Requires 0 <= B <= 64. Sign extension of a zero-width value returns zero.
template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
- static_assert(B > 0, "Bit width can't be 0.");
static_assert(B <= 64, "Bit width out of range.");
+ if (!B)
+ return 0;
return int64_t(x << (64 - B)) >> (64 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
-/// Requires 0 < B <= 64.
+/// Requires 0 <= B <= 64. Sign extension of a zero-width value returns zero.
inline int64_t SignExtend64(uint64_t X, unsigned B) {
- assert(B > 0 && "Bit width can't be 0.");
assert(B <= 64 && "Bit width out of range.");
+ if (!B)
+ return 0;
return int64_t(X << (64 - B)) >> (64 - B);
}
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -1458,10 +1458,8 @@
/// uint64_t. The bitwidth must be <= 64 or the value must fit within a
/// uint64_t. Otherwise an assertion will result.
uint64_t getZExtValue() const {
- if (isSingleWord()) {
- assert(BitWidth && "zero width values not allowed");
+ if (isSingleWord())
return U.VAL;
- }
assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
return U.pVal[0];
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114768.390569.patch
Type: text/x-patch
Size: 2515 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211130/11f898de/attachment.bin>
More information about the llvm-commits
mailing list