[PATCH] D73490: [LV] Remove nondeterminacy by changing LoopVectorizationLegality::Reductions from DenseMap to MapVector

Wei Mi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 27 08:47:23 PST 2020


wmi created this revision.
wmi added a reviewer: davidxl.
Herald added a subscriber: rkruppe.
Herald added a project: LLVM.

The iteration order of LoopVectorizationLegality::Reductions matters for the final code generation, so we better use MapVector instead of DenseMap for it to remove the nondeterminacy. reduction-order.ll in the patch is an example reduced from the case we saw. In the output of opt command, the order of the select instructions in the vector.body block keeps changing from run to run currently.


Repository:
  rL LLVM

https://reviews.llvm.org/D73490

Files:
  llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
  llvm/test/Transforms/LoopVectorize/reduction-order.ll


Index: llvm/test/Transforms/LoopVectorize/reduction-order.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/LoopVectorize/reduction-order.ll
@@ -0,0 +1,48 @@
+; RUN: opt -loop-vectorize -S < %s 2>&1 | FileCheck %s
+; RUN: opt -passes='loop-vectorize' -S < %s 2>&1 | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-grtev4-linux-gnu"
+
+; Make sure the selects generated from reduction are always emitted
+; in deterministic order.
+; CHECK-LABEL: @foo(
+; CHECK: vector.body:
+; CHECK: %[[VAR1:.*]] = add <4 x i32>
+; CHECK-NEXT:  = add <4 x i32>
+; CHECK-NEXT:  = add <4 x i32>
+; CHECK-NEXT:  = add <4 x i32>
+; CHECK-NEXT: %[[VAR2:.*]] = add <4 x i32>
+; CHECK: select <4 x i1> {{.*}}, <4 x i32> %[[VAR2]], <4 x i32>
+; CHECK-NEXT: select <4 x i1> {{.*}}, <4 x i32> %[[VAR1]], <4 x i32>
+; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
+;
+define internal i64 @foo(i32* %t0) !prof !1 {
+t16:
+  br label %t20
+
+t17:                                               ; preds = %t20
+  %t18 = phi i32 [ %t24, %t20 ]
+  %t19 = phi i32 [ %t28, %t20 ]
+  br label %t31
+
+t20:                                               ; preds = %t20, %t16
+  %t21 = phi i64 [ 0, %t16 ], [ %t29, %t20 ]
+  %t22 = phi i32 [ undef, %t16 ], [ %t28, %t20 ]
+  %t23 = phi i32 [ 0, %t16 ], [ %t24, %t20 ]
+  %t24 = add i32 undef, %t23
+  %t25 = add i32 %t22, undef
+  %t26 = add i32 %t25, undef
+  %t27 = add i32 %t26, undef
+  %t28 = add i32 %t27, undef
+  %t29 = add nuw nsw i64 %t21, 1
+  %t30 = icmp eq i64 %t29, undef
+  br i1 %t30, label %t17, label %t20, !prof !2
+
+t31:
+  ret i64 undef
+}
+
+!0 = !{!"clang version google3-trunk (fe5f233a938f5bc31c458c39cca54d7dcc2667ef)"}
+!1 = !{!"function_entry_count", i64 801}
+!2 = !{!"branch_weights", i32 746, i32 1}
Index: llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
===================================================================
--- llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -208,7 +208,7 @@
 
   /// ReductionList contains the reduction descriptors for all
   /// of the reductions that were found in the loop.
-  using ReductionList = DenseMap<PHINode *, RecurrenceDescriptor>;
+  using ReductionList = MapVector<PHINode *, RecurrenceDescriptor>;
 
   /// InductionList saves induction variables and maps them to the
   /// induction descriptor.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73490.240608.patch
Type: text/x-patch
Size: 2565 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200127/61a03c07/attachment.bin>


More information about the llvm-commits mailing list