[llvm] [llvm] produce a more consistent estimates of bit width needed when parsing an integer (PR #205947)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 25 14:03:59 PDT 2026
================
@@ -522,16 +522,17 @@ bool StringRef::consumeInteger(unsigned Radix, APInt &Result) {
return false;
}
- // (Over-)estimate the required number of bits.
unsigned Log2Radix = 0;
while ((1U << Log2Radix) < Radix) Log2Radix++;
bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
- unsigned BitWidth = Log2Radix * Str.size();
- if (BitWidth < Result.getBitWidth())
- BitWidth = Result.getBitWidth(); // don't shrink the result
- else if (BitWidth > Result.getBitWidth())
+ // Initialize Result to a reasonable starting width (at least 64 bits),
+ // but do not shrink it if it was already larger.
+ unsigned BitWidth = std::max(64U, Result.getBitWidth());
+
+ if (Result.getBitWidth() < BitWidth) {
Result = Result.zext(BitWidth);
+ }
APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
----------------
efriedma-quic wrote:
There isn't really any reason for RadixAP and CharAP to exist; we have add/mul operators which take a uint64_t as the RHS. But I guess that can be a followup.
https://github.com/llvm/llvm-project/pull/205947
More information about the llvm-commits
mailing list