Ensure KnownBits passed when calculating from range md has right size (#132985)

KnownBits passed to computeKnownBitsFromRangeMetadata must have the same
bit width as the range metadata bit width. Otherwise the calculated
results will be incorrect.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
This commit is contained in:
LU-JOHN 2025-04-02 22:17:14 -05:00 committed by GitHub
parent dcc2182bce
commit 6a46c6c865
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -430,6 +430,10 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
ConstantInt *Upper =
mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
ConstantRange Range(Lower->getValue(), Upper->getValue());
// BitWidth must equal the Ranges BitWidth for the correct number of high
// bits to be set.
assert(BitWidth == Range.getBitWidth() &&
"Known bit width must match range bit width!");
// The first CommonPrefixBits of all values in Range are equal.
unsigned CommonPrefixBits =

View File

@ -9176,6 +9176,12 @@ SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
"Cannot use an ext load to change the number of vector elements!");
}
assert((!MMO->getRanges() ||
(mdconst::extract<ConstantInt>(MMO->getRanges()->getOperand(0))
->getBitWidth() == MemVT.getScalarSizeInBits() &&
MemVT.isInteger())) &&
"Range metadata and load type must match!");
bool Indexed = AM != ISD::UNINDEXED;
assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");