[PATCH] D140418: [APSInt] Fix bug in APSInt in issue 59513
Peter Rong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 20 10:37:47 PST 2022
Peter created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Peter requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Also provide a tryExtValue() API like APInt did in D139683 <https://reviews.llvm.org/D139683>
Fix a use case in MIParser that used APSInt wrong and was revealed because of this patch.
This patch is almost identical to D140059 <https://reviews.llvm.org/D140059>, that one got reverted due to the previous MIParser use case.
getExtValue() used to provide a 64bit representation regardless of the sign.
After this patch, only 63 bits are allowed for unsigned value since the return value is int64_t.
This change of semantics caused some unit testing w/ large constants to fail.
We modify MIParser to refelect the intended behavior using `try` API introduced in D139683 <https://reviews.llvm.org/D139683>.
It's also a show case how `try` API can be used conveniently where user don't need to worry about
the detailed condition.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D140418
Files:
llvm/include/llvm/ADT/APSInt.h
llvm/lib/CodeGen/MIRParser/MIParser.cpp
llvm/unittests/ADT/APSIntTest.cpp
Index: llvm/unittests/ADT/APSIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APSIntTest.cpp
+++ llvm/unittests/ADT/APSIntTest.cpp
@@ -62,6 +62,13 @@
EXPECT_EQ(UINT64_C(0) - 7, APSInt::getUnsigned(-7).getZExtValue());
}
+TEST(APSIntTest, isRepresentableByInt64) {
+ ASSERT_TRUE(APSInt(APInt(3, 7), true).isRepresentableByInt64());
+ ASSERT_TRUE(APSInt(APInt(128, 7), true).isRepresentableByInt64());
+ ASSERT_TRUE(APSInt(APInt(128, 7), false).isRepresentableByInt64());
+ ASSERT_TRUE(APSInt(APInt(64, -1), false).isRepresentableByInt64());
+ ASSERT_FALSE(APSInt(APInt(64, (uint64_t)-1), true).isRepresentableByInt64());
+}
TEST(APSIntTest, getExtValue) {
EXPECT_TRUE(APSInt(APInt(3, 7), true).isUnsigned());
EXPECT_TRUE(APSInt(APInt(3, 7), false).isSigned());
@@ -76,6 +83,16 @@
EXPECT_EQ(9, APSInt(APInt(4, -7), true).getExtValue());
EXPECT_EQ(-7, APSInt(APInt(4, -7), false).getExtValue());
}
+TEST(APSIntTest, tryExtValue) {
+ ASSERT_EQ(-7, APSInt(APInt(64, -7), false).tryExtValue().value_or(42));
+ ASSERT_EQ(42, APSInt(APInt(128, -7), false).tryExtValue().value_or(42));
+ ASSERT_EQ(-1,
+ APSInt(APInt::getAllOnes(128), false).tryExtValue().value_or(42));
+ ASSERT_EQ(42, APSInt(APInt(64, -7), true).tryExtValue().value_or(42));
+ ASSERT_EQ(1, APSInt(APInt(128, 1), true).tryExtValue().value_or(42));
+ ASSERT_EQ(42,
+ APSInt(APInt::getAllOnes(128), true).tryExtValue().value_or(42));
+}
TEST(APSIntTest, compareValues) {
auto U = [](uint64_t V) { return APSInt::getUnsigned(V); };
Index: llvm/lib/CodeGen/MIRParser/MIParser.cpp
===================================================================
--- llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -1778,9 +1778,12 @@
bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
assert(Token.is(MIToken::IntegerLiteral));
const APSInt &Int = Token.integerValue();
- if (Int.getMinSignedBits() > 64)
+ if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
+ Dest = MachineOperand::CreateImm(*SImm);
+ else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
+ Dest = MachineOperand::CreateImm(*UImm);
+ else
return error("integer literal is too large to be an immediate operand");
- Dest = MachineOperand::CreateImm(Int.getExtValue());
lex();
return false;
}
Index: llvm/include/llvm/ADT/APSInt.h
===================================================================
--- llvm/include/llvm/ADT/APSInt.h
+++ llvm/include/llvm/ADT/APSInt.h
@@ -85,12 +85,26 @@
}
using APInt::toString;
+ /// If this int is representable using an int64_t.
+ bool isRepresentableByInt64() const {
+ // For unsigned values with 64 active bits, they technically fit into a
+ // int64_t, but the user may get negative numbers and has to manually cast
+ // them to unsigned. Let's not bet the user has the sanity to do that and
+ // not give them a vague value at the first place.
+ return isSigned() ? isSignedIntN(64) : isIntN(63);
+ }
+
/// Get the correctly-extended \c int64_t value.
int64_t getExtValue() const {
- assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
+ assert(isRepresentableByInt64() && "Too many bits for int64_t");
return isSigned() ? getSExtValue() : getZExtValue();
}
+ std::optional<int64_t> tryExtValue() const {
+ return isRepresentableByInt64() ? std::optional<int64_t>(getExtValue())
+ : std::nullopt;
+ }
+
APSInt trunc(uint32_t width) const {
return APSInt(APInt::trunc(width), IsUnsigned);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140418.484317.patch
Type: text/x-patch
Size: 3688 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221220/6477c3bf/attachment.bin>
More information about the llvm-commits
mailing list