[PATCH] D136256: [InstCombine] Fix assert condition in `foldSelectShuffleOfSelectShuffle`

Nabeel Omer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 19 07:18:44 PDT 2022


n-omer created this revision.
n-omer added reviewers: RKSimon, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: All.
n-omer requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The `assert()` is making an assumption that the resulting shuffle mask will always select elements from both vectors, this is untrue in the case of two shuffles being folded if the former shuffle has a mask with `undef` elements in it. In such a case folding the shuffles might result in a mask which only selects from one of the vectors because the other elements (in the mask) are `undef`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136256

Files:
  llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
  llvm/test/Transforms/InstCombine/foldSelectShuffleOfSelectShuffle.ll


Index: llvm/test/Transforms/InstCombine/foldSelectShuffleOfSelectShuffle.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/foldSelectShuffleOfSelectShuffle.ll
@@ -0,0 +1,13 @@
+; RUN: opt -passes=instcombine %s -S -o - | FileCheck %s
+
+; CHECK: shufflevector <4 x float> %1, <4 x float> poison, <4 x i32> <i32 0, i32 undef, i32 2, i32 3>
+
+ at g = dso_local local_unnamed_addr global [4 x float] zeroinitializer, align 16
+ at h = dso_local local_unnamed_addr global [4 x float] zeroinitializer, align 16
+define <4 x float> @square() local_unnamed_addr #0 {
+  %1 = load <4 x float>, ptr @g, align 16
+  %2 = load <4 x float>, ptr @h, align 16
+  %3 = shufflevector <4 x float> %1, <4 x float> %2, <4 x i32> <i32 0, i32 5, i32 undef, i32 undef>
+  %4 = shufflevector <4 x float> %3, <4 x float> %1, <4 x i32> <i32 0, i32 undef, i32 6, i32 7>
+  ret <4 x float> %4
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -2004,7 +2004,8 @@
   for (unsigned i = 0; i != NumElts; ++i)
     NewMask[i] = Mask[i] < (signed)NumElts ? Mask[i] : Mask1[i];
 
-  assert(ShuffleVectorInst::isSelectMask(NewMask) && "Unexpected shuffle mask");
+  // A select mask with undef elements might look like an identity mask.
+  assert((ShuffleVectorInst::isSelectMask(NewMask) || ShuffleVectorInst::isIdentityMask(NewMask)) && "Unexpected shuffle mask");
   return new ShuffleVectorInst(X, Y, NewMask);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136256.468906.patch
Type: text/x-patch
Size: 1662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221019/c7288162/attachment.bin>


More information about the llvm-commits mailing list