[PATCH] D27397: [LAA] Prevent invalid IR for loop-invariant bound in loop body
Keno Fischer via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 4 16:27:47 PST 2016
loladiro created this revision.
loladiro added reviewers: hfinkel, anemet.
loladiro added a subscriber: llvm-commits.
Herald added a subscriber: mzolotukhin.
If LAA expands a bound that is loop invariant, but not hoisted out
of the loop body, it used to use that value anyway, causing a
non-domination error, because the memcheck block is of course not
dominated by the scalar loop body. Detect this situation and expand
the SCEV expression instead.
Fixes PR31251
Please note, I don't know this part of the code base at all, so while
I patched up the symptoms, there may be something more fundamental at
play here that I'm oblivious to.
https://reviews.llvm.org/D27397
Files:
lib/Analysis/LoopAccessAnalysis.cpp
test/Analysis/LoopAccessAnalysis/loop-invariant-bound.ll
Index: test/Analysis/LoopAccessAnalysis/loop-invariant-bound.ll
===================================================================
--- /dev/null
+++ test/Analysis/LoopAccessAnalysis/loop-invariant-bound.ll
@@ -0,0 +1,39 @@
+; RUN: opt -loop-vectorize -S < %s | FileCheck %s
+; Checks that when introducing memcheck, LAA does not accidentally introduce non-dominating instructions
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+%Dual.212 = type { %Dual.213, %Partials.215 }
+%Dual.213 = type { double, %Partials.214 }
+%Partials.214 = type { [2 x double] }
+%Partials.215 = type { [2 x %Dual.213] }
+
+; CHECK: vector.memcheck
+
+; Function Attrs: sspreq
+define void @"julia_axpy!_65480"(%Dual.212*) #0 {
+top:
+ br label %if24
+
+if24: ; preds = %if24, %top
+ %"#temp#1.sroa.3.02" = phi i64 [ undef, %top ], [ %2, %if24 ]
+ %"#temp#1.sroa.0.01" = phi i64 [ undef, %top ], [ %1, %if24 ]
+ %1 = add i64 %"#temp#1.sroa.0.01", 1
+ %2 = add i64 %"#temp#1.sroa.3.02", 1
+ ; This pointer is loop invariant. LAA used to re-use it from memcheck, even though it didn't dominate.
+ %v2.sroa.0.0..sroa_cast = bitcast %Dual.212* %0 to i64*
+ %v2.sroa.0.0.copyload = load i64, i64* %v2.sroa.0.0..sroa_cast, align 1
+ %3 = add i64 %"#temp#1.sroa.0.01", -1
+ %4 = getelementptr inbounds %Dual.212, %Dual.212* undef, i64 %3, i32 1, i32 0, i64 0, i32 1, i32 0, i64 0
+ %5 = bitcast double* %4 to i64*
+ store i64 undef, i64* %5, align 8
+ %notlhs27 = icmp eq i64 %2, undef
+ %notrhs28 = icmp eq i64 %1, undef
+ %6 = or i1 %notrhs28, %notlhs27
+ br i1 %6, label %L41.L335_crit_edge, label %if24
+
+L41.L335_crit_edge: ; preds = %if24
+ ret void
+}
+
+attributes #0 = { sspreq "no-frame-pointer-elim"="true" "target-cpu"="knl" }
Index: lib/Analysis/LoopAccessAnalysis.cpp
===================================================================
--- lib/Analysis/LoopAccessAnalysis.cpp
+++ lib/Analysis/LoopAccessAnalysis.cpp
@@ -1870,18 +1870,24 @@
Value *Ptr = PtrRtChecking.Pointers[CG->Members[0]].PointerValue;
const SCEV *Sc = SE->getSCEV(Ptr);
+ unsigned AS = Ptr->getType()->getPointerAddressSpace();
+ LLVMContext &Ctx = Loc->getContext();
+
+ // Use this type for pointer arithmetic.
+ Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS);
+
if (SE->isLoopInvariant(Sc, TheLoop)) {
DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" << *Ptr
<< "\n");
- return {Ptr, Ptr};
+ // Ptr could be in the loop body. If so, expand a new one at the correct
+ // location.
+ Instruction *Inst = dyn_cast<Instruction>(Ptr);
+ Value *NewPtr = (Inst && TheLoop->contains(Inst))
+ ? Exp.expandCodeFor(Sc, PtrArithTy, Loc)
+ : Ptr;
+ return {NewPtr, NewPtr};
} else {
- unsigned AS = Ptr->getType()->getPointerAddressSpace();
- LLVMContext &Ctx = Loc->getContext();
-
- // Use this type for pointer arithmetic.
- Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS);
Value *Start = nullptr, *End = nullptr;
-
DEBUG(dbgs() << "LAA: Adding RT check for range:\n");
Start = Exp.expandCodeFor(CG->Low, PtrArithTy, Loc);
End = Exp.expandCodeFor(CG->High, PtrArithTy, Loc);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27397.80220.patch
Type: text/x-patch
Size: 3329 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161205/d01573a6/attachment.bin>
More information about the llvm-commits
mailing list