[llvm] [InstCombine][NFC] Use uint64_t for TruncRatio to avoid overflow (PR #225150)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 21 10:48:10 PDT 2026
https://github.com/ahradwan2-public created https://github.com/llvm/llvm-project/pull/225150
## Summary
This changes `TruncRatio` from `unsigned` to `uint64_t` in `foldVecExtTruncToExtElt`
unsigned TruncRatio = SrcBits / DstBits;
`BitCastNumElts` is used to build the destination vector type with `TruncRatio` declared `unsigned`, this product is evaluated in 32 bits.
BitCastNumElts = VecElts.getKnownMinValue() * TruncRatio;
An overflow would wrap to a small value that silently satisfies the existing
assert(BitCastNumElts <= std::numeric_limits<uint32_t>::max() &&
"overflow 32-bits");
and construct a wrong-sized vector type. Declaring `TruncRatio` as `uint64_t` computes the true 64-bit product, so the assert is actually meaningful.
#### Assisted by: Cline
#### I don't have write access - please merge on my behalf once approved. Thanks.
>From ebc50e5dbba9d678a74f7a6be08580e36b7d6991 Mon Sep 17 00:00:00 2001
From: Ahmed Radwan <ahradwan at amd.com>
Date: Mon, 21 Sep 2026 11:32:45 -0400
Subject: [PATCH] [InstCombine][NFC] Use uint64_t for TruncRatio to avoid
32-bit overflow of BitCastNumElts in foldVecExtTruncToExtElt
---
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 97defc5e3ddd35..2784fd7893f702 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -754,7 +754,7 @@ static Instruction *foldVecExtTruncToExtElt(TruncInst &Trunc,
// A badly fit destination size would result in an invalid cast.
unsigned SrcBits = SrcType->getScalarSizeInBits();
unsigned DstBits = DstType->getScalarSizeInBits();
- unsigned TruncRatio = SrcBits / DstBits;
+ uint64_t TruncRatio = SrcBits / DstBits;
if ((SrcBits % DstBits) != 0)
return nullptr;
More information about the llvm-commits
mailing list