llvm.org GIT mirror llvm / 435798e
Disable unrolling in the loop vectorizer when disabled in the pass manager When unrolling is disabled in the pass manager, the loop vectorizer should also not unroll loops. This will allow the -fno-unroll-loops option in Clang to behave as expected (even for vectorizable loops). The loop vectorizer's -force-vector-unroll option will (continue to) override the pass-manager setting (including -force-vector-unroll=0 to force use of the internal auto-selection logic). In order to test this, I added a flag to opt (-disable-loop-unrolling) to force disable unrolling through opt (the analog of -fno-unroll-loops in Clang). Also, this fixes a small bug in opt where the loop vectorizer was enabled only after the pass manager populated the queue of passes (the global_alias.ll test needed a slight update to the RUN line as a result of this fix). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189499 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 6 years ago
6 changed file(s) with 55 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
113113 //
114114 // LoopVectorize - Create a loop vectorization pass.
115115 //
116 Pass *createLoopVectorizePass();
116 Pass *createLoopVectorizePass(bool NoUnrolling = false);
117117
118118 //===----------------------------------------------------------------------===//
119119 //
195195 MPM.add(createLoopDeletionPass()); // Delete dead loops
196196
197197 if (!LateVectorize && LoopVectorize)
198 MPM.add(createLoopVectorizePass());
198 MPM.add(createLoopVectorizePass(DisableUnrollLoops));
199199
200200 if (!DisableUnrollLoops)
201201 MPM.add(createLoopUnrollPass()); // Unroll small loops
249249 // Add the various vectorization passes and relevant cleanup passes for
250250 // them since we are no longer in the middle of the main scalar pipeline.
251251 if (LoopVectorize) {
252 MPM.add(createLoopVectorizePass());
252 MPM.add(createLoopVectorizePass(DisableUnrollLoops));
253253
254254 if (!DisableUnrollLoops)
255255 MPM.add(createLoopUnrollPass()); // Unroll small loops
760760 /// Vectorization unroll factor.
761761 unsigned Unroll;
762762
763 LoopVectorizeHints(const Loop *L)
763 LoopVectorizeHints(const Loop *L, bool DisableUnrolling)
764764 : Width(VectorizationFactor)
765 , Unroll(VectorizationUnroll)
765 , Unroll(DisableUnrolling ? 1 : VectorizationUnroll)
766766 , LoopID(L->getLoopID()) {
767767 getHints(L);
768768 // The command line options override any loop metadata except for when
771771 Width = VectorizationFactor;
772772 if (VectorizationUnroll.getNumOccurrences() > 0)
773773 Unroll = VectorizationUnroll;
774
775 DEBUG(if (DisableUnrolling && Unroll == 1)
776 dbgs() << "LV: Unrolling disabled by the pass manager\n");
774777 }
775778
776779 /// Return the loop vectorizer metadata prefix.
877880 /// Pass identification, replacement for typeid
878881 static char ID;
879882
880 explicit LoopVectorize() : LoopPass(ID) {
883 explicit LoopVectorize(bool NoUnrolling = false)
884 : LoopPass(ID), DisableUnrolling(NoUnrolling) {
881885 initializeLoopVectorizePass(*PassRegistry::getPassRegistry());
882886 }
883887
887891 TargetTransformInfo *TTI;
888892 DominatorTree *DT;
889893 TargetLibraryInfo *TLI;
894 bool DisableUnrolling;
890895
891896 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
892897 // We only vectorize innermost loops.
908913 DEBUG(dbgs() << "LV: Checking a loop in \"" <<
909914 L->getHeader()->getParent()->getName() << "\"\n");
910915
911 LoopVectorizeHints Hints(L);
916 LoopVectorizeHints Hints(L, DisableUnrolling);
912917
913918 if (Hints.Width == 1 && Hints.Unroll == 1) {
914919 DEBUG(dbgs() << "LV: Not vectorizing.\n");
47854790 INITIALIZE_PASS_END(LoopVectorize, LV_NAME, lv_name, false, false)
47864791
47874792 namespace llvm {
4788 Pass *createLoopVectorizePass() {
4789 return new LoopVectorize();
4793 Pass *createLoopVectorizePass(bool NoUnrolling) {
4794 return new LoopVectorize(NoUnrolling);
47904795 }
47914796 }
47924797
0 ; RUN: opt < %s -O2 -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx -force-vector-width=4 -S | FileCheck %s
1 ; RUN: opt < %s -O2 -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx -force-vector-width=4 -disable-loop-unrolling -S | FileCheck %s -check-prefix=CHECK-NOUNRL
2
3 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
4 target triple = "x86_64-apple-macosx10.8.0"
5 ;CHECK-LABEL: @bar(
6 ;CHECK: store <4 x i32>
7 ;CHECK: store <4 x i32>
8 ;CHECK: ret
9 ;CHECK-NOUNRL-LABEL: @bar(
10 ;CHECK-NOUNRL: store <4 x i32>
11 ;CHECK-NOUNRL-NOT: store <4 x i32>
12 ;CHECK-NOUNRL: ret
13 define i32 @bar(i32* nocapture %A, i32 %n) nounwind uwtable ssp {
14 %1 = icmp sgt i32 %n, 0
15 br i1 %1, label %.lr.ph, label %._crit_edge
16
17 .lr.ph: ; preds = %0, %.lr.ph
18 %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
19 %2 = getelementptr inbounds i32* %A, i64 %indvars.iv
20 %3 = load i32* %2, align 4
21 %4 = add nsw i32 %3, 6
22 store i32 %4, i32* %2, align 4
23 %indvars.iv.next = add i64 %indvars.iv, 1
24 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
25 %exitcond = icmp eq i32 %lftr.wideiv, %n
26 br i1 %exitcond, label %._crit_edge, label %.lr.ph
27
28 ._crit_edge: ; preds = %.lr.ph, %0
29 ret i32 undef
30 }
None ; RUN: opt < %s -O3 -loop-vectorize -force-vector-unroll=1 -force-vector-width=4 -dce -instcombine -S | FileCheck %s
0 ; RUN: opt < %s -O1 -loop-vectorize -force-vector-unroll=1 -force-vector-width=4 -dce -instcombine -S | FileCheck %s
11
22 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
33
135135 cl::init(true));
136136
137137 static cl::opt
138 DisableLoopUnrolling("disable-loop-unrolling",
139 cl::desc("Disable loop unrolling in all relevant passes"),
140 cl::init(false));
141
142 static cl::opt
138143 DisableSimplifyLibCalls("disable-simplify-libcalls",
139144 cl::desc("Disable simplify-libcalls"));
140145
446451 Builder.Inliner = createAlwaysInlinerPass();
447452 }
448453 Builder.DisableUnitAtATime = !UnitAtATime;
449 Builder.DisableUnrollLoops = OptLevel == 0;
454 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
455 DisableLoopUnrolling : OptLevel == 0;
450456
457 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
458
451459 Builder.populateFunctionPassManager(FPM);
452460 Builder.populateModulePassManager(MPM);
453
454 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
455461 }
456462
457463 static void AddStandardCompilePasses(PassManagerBase &PM) {