[llvm] Fix overflow issues associated with shufflevector for landing PR #229662 (PR #230410)
Sushant Gokhale via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 19:09:22 PDT 2026
https://github.com/sushgokh updated https://github.com/llvm/llvm-project/pull/230410
>From 6ed7e63ddb389de7c2fa9fabee0ccba5b395abb7 Mon Sep 17 00:00:00 2001
From: sgokhale <sgokhale at nvidia.com>
Date: Fri, 9 Oct 2026 01:13:37 -0700
Subject: [PATCH 1/2] Fix overflow issues associated with shufflevector for
landing PR #229662
Consider the motivating cases here: https://godbolt.org/z/7rTsxT4sG
I will provide some context why shufflevector APIs need to be modified.
PR #229662 detects adjacent loads %L1/%L2, feeding as shuffle operands, and converts 2 loads into single load(say %L). Shufflevector also needs to be modified so that 0th operand is %L and 1st operand is poison. `ShuffleVectorInst::isValidOperands` API checks if the index mask for the new shuffle is in range of concat(%L, poison).
Computing range of concat i.e. 2 * sizeof(%L) creates signed overflow.
For the same case, while computing the new shuffle cost, `isSingleSourceMaskImpl` API also computes 2 * sizeof(%L) and oveflows.
This patch fixes these issues.
Note that such overflow issues, asssociated with shufflevector, are present elsewhere but we wont fix it here.
---
llvm/lib/IR/Instructions.cpp | 4 ++--
llvm/test/Verifier/shufflevector-mask.ll | 14 ++++++++++++++
2 files changed, 16 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/Verifier/shufflevector-mask.ll
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index fa91cb79a63f9..3c9c79b4f06b1 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -1841,7 +1841,7 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
int V1Size =
cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue();
for (int Elem : Mask)
- if (Elem != PoisonMaskElem && Elem >= V1Size * 2)
+ if (Elem != PoisonMaskElem && Elem >= (int64_t)V1Size * 2)
return false;
if (isa<ScalableVectorType>(V1->getType()))
@@ -1962,7 +1962,7 @@ static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
for (int I : Mask) {
if (I == -1)
continue;
- assert(I >= 0 && I < (NumOpElts * 2) &&
+ assert(I >= 0 && I < ((int64_t)NumOpElts * 2) &&
"Out-of-bounds shuffle mask element");
UsesLHS |= (I < NumOpElts);
UsesRHS |= (I >= NumOpElts);
diff --git a/llvm/test/Verifier/shufflevector-mask.ll b/llvm/test/Verifier/shufflevector-mask.ll
new file mode 100644
index 0000000000000..31a56173fc36e
--- /dev/null
+++ b/llvm/test/Verifier/shufflevector-mask.ll
@@ -0,0 +1,14 @@
+; RUN: opt -passes=verify -disable-output %s
+
+; concat(V1, V2) = (2 * 1073741824) overflows int range but the mask indices are still within range
+define <2 x i8> @shuffle_v1073741824i8_index_zero_first(<1073741824 x i8> %a, <1073741824 x i8> %b) {
+ %s = shufflevector <1073741824 x i8> %a, <1073741824 x i8> %b, <2 x i32> <i32 0, i32 1>
+ ret <2 x i8> %s
+}
+
+; concat(V1, V2) = (2 * 1073741824) overflows int range and 0th and last element of the concatenated vector is referred.
+define <2 x i8> @shuffle_v1073741824i8_index_zero_last(<1073741824 x i8> %a, <1073741824 x i8> %b) {
+ %s = shufflevector <1073741824 x i8> %a, <1073741824 x i8> %b, <2 x i32> <i32 0, i32 2147483647>
+ ret <2 x i8> %s
+}
+
>From a18fd2a48888dddb83fa3f434e967a586053744a Mon Sep 17 00:00:00 2001
From: sgokhale <sgokhale at nvidia.com>
Date: Sat, 10 Oct 2026 19:07:43 -0700
Subject: [PATCH 2/2] address review comments
---
llvm/lib/IR/Instructions.cpp | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 3c9c79b4f06b1..2517a3ba79cf6 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -1837,18 +1837,13 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
return false;
- // Make sure the mask elements make sense.
- int V1Size =
- cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue();
- for (int Elem : Mask)
- if (Elem != PoisonMaskElem && Elem >= (int64_t)V1Size * 2)
- return false;
-
if (isa<ScalableVectorType>(V1->getType()))
- if ((Mask[0] != 0 && Mask[0] != PoisonMaskElem) || !all_equal(Mask))
- return false;
+ return (Mask[0] == 0 || Mask[0] == PoisonMaskElem) && all_equal(Mask);
- return true;
+ // Make sure the mask elements make sense.
+ int64_t V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
+ auto [MinIt, MaxIt] = std::minmax_element(Mask.begin(), Mask.end());
+ return *MinIt >= PoisonMaskElem && *MaxIt < V1Size * 2;
}
bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
@@ -1955,14 +1950,14 @@ Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask,
return ConstantVector::get(MaskConst);
}
-static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
+static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int64_t NumOpElts) {
assert(!Mask.empty() && "Shuffle mask must contain elements");
bool UsesLHS = false;
bool UsesRHS = false;
for (int I : Mask) {
if (I == -1)
continue;
- assert(I >= 0 && I < ((int64_t)NumOpElts * 2) &&
+ assert(I >= 0 && I < (NumOpElts * 2) &&
"Out-of-bounds shuffle mask element");
UsesLHS |= (I < NumOpElts);
UsesRHS |= (I >= NumOpElts);
More information about the llvm-commits
mailing list