|
| 1 | +commit 99ca52276f9ee1386866d6dff6179cfa64824621 |
| 2 | +Author: Keno Fischer < [email protected]> |
| 3 | +Date: Mon Dec 5 21:25:03 2016 +0000 |
| 4 | + |
| 5 | + [LAA] Prevent invalid IR for loop-invariant bound in loop body |
| 6 | + |
| 7 | + Summary: |
| 8 | + If LAA expands a bound that is loop invariant, but not hoisted out |
| 9 | + of the loop body, it used to use that value anyway, causing a |
| 10 | + non-domination error, because the memcheck block is of course not |
| 11 | + dominated by the scalar loop body. Detect this situation and expand |
| 12 | + the SCEV expression instead. |
| 13 | + |
| 14 | + Fixes PR31251 |
| 15 | + |
| 16 | + Reviewers: anemet |
| 17 | + Subscribers: mzolotukhin, llvm-commits |
| 18 | + |
| 19 | + Differential Revision: https://reviews.llvm.org/D27397 |
| 20 | + |
| 21 | + git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288705 91177308-0d34-0410-b5e6-96231b3b80d8 |
| 22 | + |
| 23 | +diff --git a/lib/Analysis/LoopAccessAnalysis.cpp b/lib/Analysis/LoopAccessAnalysis.cpp |
| 24 | +index 01a2f46..2f3dca3 100644 |
| 25 | +--- a/lib/Analysis/LoopAccessAnalysis.cpp |
| 26 | ++++ b/lib/Analysis/LoopAccessAnalysis.cpp |
| 27 | +@@ -1870,18 +1870,24 @@ expandBounds(const RuntimePointerChecking::CheckingPtrGroup *CG, Loop *TheLoop, |
| 28 | + Value *Ptr = PtrRtChecking.Pointers[CG->Members[0]].PointerValue; |
| 29 | + const SCEV *Sc = SE->getSCEV(Ptr); |
| 30 | + |
| 31 | ++ unsigned AS = Ptr->getType()->getPointerAddressSpace(); |
| 32 | ++ LLVMContext &Ctx = Loc->getContext(); |
| 33 | ++ |
| 34 | ++ // Use this type for pointer arithmetic. |
| 35 | ++ Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS); |
| 36 | ++ |
| 37 | + if (SE->isLoopInvariant(Sc, TheLoop)) { |
| 38 | + DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" << *Ptr |
| 39 | + << "\n"); |
| 40 | +- return {Ptr, Ptr}; |
| 41 | ++ // Ptr could be in the loop body. If so, expand a new one at the correct |
| 42 | ++ // location. |
| 43 | ++ Instruction *Inst = dyn_cast<Instruction>(Ptr); |
| 44 | ++ Value *NewPtr = (Inst && TheLoop->contains(Inst)) |
| 45 | ++ ? Exp.expandCodeFor(Sc, PtrArithTy, Loc) |
| 46 | ++ : Ptr; |
| 47 | ++ return {NewPtr, NewPtr}; |
| 48 | + } else { |
| 49 | +- unsigned AS = Ptr->getType()->getPointerAddressSpace(); |
| 50 | +- LLVMContext &Ctx = Loc->getContext(); |
| 51 | +- |
| 52 | +- // Use this type for pointer arithmetic. |
| 53 | +- Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS); |
| 54 | + Value *Start = nullptr, *End = nullptr; |
| 55 | +- |
| 56 | + DEBUG(dbgs() << "LAA: Adding RT check for range:\n"); |
| 57 | + Start = Exp.expandCodeFor(CG->Low, PtrArithTy, Loc); |
| 58 | + End = Exp.expandCodeFor(CG->High, PtrArithTy, Loc); |
| 59 | +diff --git a/test/Transforms/LoopVersioning/loop-invariant-bound.ll b/test/Transforms/LoopVersioning/loop-invariant-bound.ll |
| 60 | +new file mode 100644 |
| 61 | +index 0000000..3411adb |
| 62 | +--- /dev/null |
| 63 | ++++ b/test/Transforms/LoopVersioning/loop-invariant-bound.ll |
| 64 | +@@ -0,0 +1,37 @@ |
| 65 | ++; RUN: opt -loop-versioning -S < %s | FileCheck %s |
| 66 | ++; Checks that when introducing check, we don't accidentally introduce non-dominating instructions |
| 67 | ++target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" |
| 68 | ++ |
| 69 | ++%Dual.212 = type { %Dual.213, %Partials.215 } |
| 70 | ++%Dual.213 = type { double, %Partials.214 } |
| 71 | ++%Partials.214 = type { [2 x double] } |
| 72 | ++%Partials.215 = type { [2 x %Dual.213] } |
| 73 | ++ |
| 74 | ++; Function Attrs: sspreq |
| 75 | ++define void @"julia_axpy!_65480"(%Dual.212*) { |
| 76 | ++top: |
| 77 | ++ br label %if24 |
| 78 | ++ |
| 79 | ++; CHECK-NOT: %bc = bitcast i64* %v2.sroa.0.0..sroa_cast |
| 80 | ++; CHECK: %bound0 |
| 81 | ++ |
| 82 | ++if24: ; preds = %if24, %top |
| 83 | ++ %"#temp#1.sroa.3.02" = phi i64 [ undef, %top ], [ %2, %if24 ] |
| 84 | ++ %"#temp#1.sroa.0.01" = phi i64 [ undef, %top ], [ %1, %if24 ] |
| 85 | ++ %1 = add i64 %"#temp#1.sroa.0.01", 1 |
| 86 | ++ %2 = add i64 %"#temp#1.sroa.3.02", 1 |
| 87 | ++ ; This pointer is loop invariant. LAA used to re-use it from memcheck, even though it didn't dominate. |
| 88 | ++ %v2.sroa.0.0..sroa_cast = bitcast %Dual.212* %0 to i64* |
| 89 | ++ %v2.sroa.0.0.copyload = load i64, i64* %v2.sroa.0.0..sroa_cast, align 1 |
| 90 | ++ %3 = add i64 %"#temp#1.sroa.0.01", -1 |
| 91 | ++ %4 = getelementptr inbounds %Dual.212, %Dual.212* undef, i64 %3, i32 1, i32 0, i64 0, i32 1, i32 0, i64 0 |
| 92 | ++ %5 = bitcast double* %4 to i64* |
| 93 | ++ store i64 undef, i64* %5, align 8 |
| 94 | ++ %notlhs27 = icmp eq i64 %2, undef |
| 95 | ++ %notrhs28 = icmp eq i64 %1, undef |
| 96 | ++ %6 = or i1 %notrhs28, %notlhs27 |
| 97 | ++ br i1 %6, label %L41.L335_crit_edge, label %if24 |
| 98 | ++ |
| 99 | ++L41.L335_crit_edge: ; preds = %if24 |
| 100 | ++ ret void |
| 101 | ++} |
0 commit comments