Skip to content

Commit f60671f

Browse files
committed
[LV] Remove nondeterminacy by changing LoopVectorizationLegality::Reductions
from DenseMap to MapVector 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. Differential Revision: https://reviews.llvm.org/D73490
1 parent 949294f commit f60671f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class LoopVectorizationLegality {
208208

209209
/// ReductionList contains the reduction descriptors for all
210210
/// of the reductions that were found in the loop.
211-
using ReductionList = DenseMap<PHINode *, RecurrenceDescriptor>;
211+
using ReductionList = MapVector<PHINode *, RecurrenceDescriptor>;
212212

213213
/// InductionList saves induction variables and maps them to the
214214
/// induction descriptor.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
; RUN: opt -loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S < %s 2>&1 | FileCheck %s
2+
; RUN: opt -passes='loop-vectorize' -force-vector-width=4 -force-vector-interleave=1 -S < %s 2>&1 | FileCheck %s
3+
4+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5+
6+
; Make sure the selects generated from reduction are always emitted
7+
; in deterministic order.
8+
; CHECK-LABEL: @foo(
9+
; CHECK: vector.body:
10+
; CHECK: %[[VAR1:.*]] = add <4 x i32> <i32 3, i32 3, i32 3, i32 3>, %vec.phi1
11+
; CHECK-NEXT: %[[VAR2:.*]] = add <4 x i32> %vec.phi, <i32 5, i32 5, i32 5, i32 5>
12+
; CHECK-NEXT: icmp ule <4 x i64>
13+
; CHECK-NEXT: select <4 x i1> {{.*}}, <4 x i32> %[[VAR2]], <4 x i32>
14+
; CHECK-NEXT: select <4 x i1> {{.*}}, <4 x i32> %[[VAR1]], <4 x i32>
15+
; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
16+
;
17+
define internal i64 @foo(i32* %t0) !prof !1 {
18+
t16:
19+
br label %t20
20+
21+
t17: ; preds = %t20
22+
%t18 = phi i32 [ %t24, %t20 ]
23+
%t19 = phi i32 [ %t28, %t20 ]
24+
br label %t31
25+
26+
t20: ; preds = %t20, %t16
27+
%t21 = phi i64 [ 0, %t16 ], [ %t29, %t20 ]
28+
%t22 = phi i32 [ 0, %t16 ], [ %t28, %t20 ]
29+
%t23 = phi i32 [ 0, %t16 ], [ %t24, %t20 ]
30+
%t24 = add i32 3, %t23
31+
%t28 = add i32 %t22, 5
32+
%t29 = add nuw nsw i64 %t21, 1
33+
%t30 = icmp eq i64 %t29, undef
34+
br i1 %t30, label %t17, label %t20, !prof !2
35+
36+
t31:
37+
ret i64 undef
38+
}
39+
40+
!1 = !{!"function_entry_count", i64 801}
41+
!2 = !{!"branch_weights", i32 746, i32 1}

0 commit comments

Comments
 (0)