[llvm] c166000 - [SLP]Reorder counters for same values, if the root node is reordered.

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 3 07:55:23 PDT 2023


Author: Alexey Bataev
Date: 2023-04-03T07:52:49-07:00
New Revision: c1660006b2809a17e9e33034252306a703fdb720

URL: https://github.com/llvm/llvm-project/commit/c1660006b2809a17e9e33034252306a703fdb720
DIFF: https://github.com/llvm/llvm-project/commit/c1660006b2809a17e9e33034252306a703fdb720.diff

LOG: [SLP]Reorder counters for same values, if the root node is reordered.

The counters for the repeated scalars are ordered in the natural order,
but the original scalars might be reordered during SLP graph reordering
and this order can be dropped. Need to use the scalars after the
reordering, not the original ones, to emit correct code for same value
counters.

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    llvm/test/Transforms/SLPVectorizer/X86/same-scalars-reordered-in-reduction.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 570a8efe78a74..7f3e6abe4c829 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -1165,6 +1165,12 @@ class BoUpSLP {
            !VectorizableTree.front()->UserTreeIndices.empty();
   }
 
+  /// Return the scalars of the root node.
+  ArrayRef<Value *> getRootNodeScalars() const {
+    assert(!VectorizableTree.empty() && "No graph to get the first node from");
+    return VectorizableTree.front()->Scalars;
+  }
+
   /// Builds external uses of the vectorized scalars, i.e. the list of
   /// vectorized scalars to be extracted, their lanes and their scalar users. \p
   /// ExternallyUsedValues contains additional list of external uses to handle
@@ -13355,8 +13361,9 @@ class HorizontalReduction {
 
         // Emit code to correctly handle reused reduced values, if required.
         if (OptReusedScalars && !SameScaleFactor) {
-          VectorizedRoot = emitReusedOps(VectorizedRoot, Builder, VL,
-                                         SameValuesCounter, TrackedToOrig);
+          VectorizedRoot =
+              emitReusedOps(VectorizedRoot, Builder, V.getRootNodeScalars(),
+                            SameValuesCounter, TrackedToOrig);
         }
 
         Value *ReducedSubTree =

diff  --git a/llvm/test/Transforms/SLPVectorizer/X86/same-scalars-reordered-in-reduction.ll b/llvm/test/Transforms/SLPVectorizer/X86/same-scalars-reordered-in-reduction.ll
index 01379ae7ebb14..bb00f753ddb1f 100644
--- a/llvm/test/Transforms/SLPVectorizer/X86/same-scalars-reordered-in-reduction.ll
+++ b/llvm/test/Transforms/SLPVectorizer/X86/same-scalars-reordered-in-reduction.ll
@@ -7,7 +7,7 @@ define i32 @test() {
 ; CHECK-NEXT:    [[SQ:%.*]] = alloca [64 x i32], i32 0, align 16
 ; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr inbounds [64 x i32], ptr [[SQ]], i64 0, i64 1
 ; CHECK-NEXT:    [[TMP1:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4
-; CHECK-NEXT:    [[TMP2:%.*]] = mul <4 x i32> [[TMP1]], <i32 3, i32 2, i32 2, i32 1>
+; CHECK-NEXT:    [[TMP2:%.*]] = mul <4 x i32> [[TMP1]], <i32 2, i32 3, i32 2, i32 1>
 ; CHECK-NEXT:    [[TMP3:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[TMP2]])
 ; CHECK-NEXT:    ret i32 [[TMP3]]
 ;


        


More information about the llvm-commits mailing list