diff --git a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h --- a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h +++ b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h @@ -16,6 +16,7 @@ #include "llvm/ADT/EquivalenceClasses.h" #include "llvm/Analysis/LoopAnalysisManager.h" +#include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/Pass.h" @@ -26,7 +27,6 @@ class DataLayout; class Loop; class LoopAccessInfo; -class OptimizationRemarkEmitter; class raw_ostream; class SCEV; class SCEVUnionPredicate; @@ -173,7 +173,8 @@ : PSE(PSE), InnermostLoop(L), AccessIdx(0), MaxSafeDepDistBytes(0), MaxSafeVectorWidthInBits(-1U), FoundNonConstantDistanceDependence(false), - Status(VectorizationSafetyStatus::Safe), RecordDependences(true) {} + Status(VectorizationSafetyStatus::Safe), RecordDependences(true), + UnsafeDependence(nullptr) {} /// Register the location (instructions are given increasing numbers) /// of a write access. @@ -248,6 +249,10 @@ SmallVector getInstructionsForAccess(Value *Ptr, bool isWrite) const; + std::shared_ptr getUnsafeDependence() const { + return UnsafeDependence; + } + private: /// A wrapper around ScalarEvolution, used to add runtime SCEV checks, and /// applies dynamic knowledge to simplify SCEV expressions and convert them @@ -294,6 +299,10 @@ /// RecordDependences is true. SmallVector Dependences; + /// First Unsafe memory dependence collected during the analysis. + /// Used for generating optimization remarks. + std::shared_ptr UnsafeDependence; + /// Check whether there is a plausible dependence between the two /// accesses. /// diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp --- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp +++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp @@ -28,7 +28,6 @@ #include "llvm/Analysis/LoopAnalysisManager.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/MemoryLocation.h" -#include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/Analysis/TargetLibraryInfo.h" @@ -45,6 +44,7 @@ #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Operator.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/Type.h" @@ -520,7 +520,7 @@ MemoryDepChecker::DepCandidates &DA, PredicatedScalarEvolution &PSE) : TheLoop(TheLoop), AST(*AA), LI(LI), DepCands(DA), - IsRTCheckAnalysisNeeded(false), PSE(PSE) {} + IsRTCheckAnalysisNeeded(false), PSE(PSE), UncomputablePtr(nullptr) {} /// Register a load and whether it is only read from. void addLoad(MemoryLocation &Loc, bool IsReadOnly) { @@ -583,9 +583,15 @@ MemAccessInfoList &getDependenciesToCheck() { return CheckDeps; } + std::shared_ptr getUncomputablePtr() { return UncomputablePtr; } + private: typedef SetVector PtrAccessSet; + /// First uncomputable/unbounded pointer found while analyzing loop. + /// Used when emitting unknown array bounds remark + std::shared_ptr UncomputablePtr; + /// Go over all memory access and check whether runtime pointer checks /// are needed and build sets of dependency check candidates. void processMemAccesses(); @@ -798,6 +804,8 @@ TheLoop, RunningDepId, ASId, ShouldCheckWrap, /*Assume=*/true)) { CanDoAliasSetRT = false; + if (!UncomputablePtr) + UncomputablePtr = std::make_shared(Access.getPointer()); break; } } @@ -1756,6 +1764,12 @@ isDependent(*A.first, A.second, *B.first, B.second, Strides); mergeInStatus(Dependence::isSafeForVectorization(Type)); + // Collect first unsafe dependence for emitting remarks + if (!UnsafeDependence && Dependence::isSafeForVectorization(Type) != + VectorizationSafetyStatus::Safe) + UnsafeDependence = std::make_shared( + Dependence(A.second, B.second, Type)); + // Gather dependences unless we accumulated MaxDependences // dependences. In that case return as soon as we find the first // unsafe dependence. This puts a limit on this quadratic @@ -1842,6 +1856,19 @@ return true; } +static DebugLoc getDebugLocFromInstruction(Instruction *I) { + DebugLoc Loc; + if (I) { + Loc = I->getDebugLoc(); + + if (auto *DD = dyn_cast(isa(I) + ? cast(I)->getRawDest() + : getPointerOperand(I))) + Loc = DD->getDebugLoc(); + } + return Loc; +} + void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI, const TargetLibraryInfo *TLI, DominatorTree *DT) { @@ -2075,10 +2102,17 @@ bool CanDoRTIfNeeded = Accesses.canCheckPtrAtRT(*PtrRtChecking, PSE->getSE(), TheLoop, SymbolicStrides); if (!CanDoRTIfNeeded) { - recordAnalysis("CantIdentifyArrayBounds") << "cannot identify array bounds"; LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because we can't find " << "the array bounds.\n"); CanVecMem = false; + + if (auto Ptr = Accesses.getUncomputablePtr()) { + if (auto *I = dyn_cast(*Ptr)) { + recordAnalysis("UnknownArrayBounds", I) + << "cannot identify array bounds"; + } + } + return; } @@ -2133,12 +2167,43 @@ << (PtrRtChecking->Need ? "" : " don't") << " need runtime memory checks.\n"); else { - recordAnalysis("UnsafeMemDep") + LLVM_DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n"); + + auto Dep = *getDepChecker().getUnsafeDependence(); + // Emit detailed remarks for each unsafe dependence + DebugLoc SourceLoc = getDebugLocFromInstruction(Dep.getSource(*this)); + + OptimizationRemarkAnalysis &R = + recordAnalysis("UnsafeDep", Dep.getDestination(*this)) << "unsafe dependent memory operations in loop. Use " "#pragma loop distribute(enable) to allow loop distribution " "to attempt to isolate the offending operations into a separate " "loop"; - LLVM_DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n"); + + switch (Dep.Type) { + case MemoryDepChecker::Dependence::NoDep: + case MemoryDepChecker::Dependence::Forward: + case MemoryDepChecker::Dependence::BackwardVectorizable: + llvm_unreachable("Unexpected dependency"); + case MemoryDepChecker::Dependence::Backward: + R << "\nBackward loop carried data dependence."; + break; + case MemoryDepChecker::Dependence::ForwardButPreventsForwarding: + R << "\nForward loop carried data dependence that prevents " + "store-to-load forwarding."; + break; + case MemoryDepChecker::Dependence:: + BackwardVectorizableButPreventsForwarding: + R << "\nBackward loop carried data dependence that prevents " + "store-to-load forwarding."; + break; + case MemoryDepChecker::Dependence::Unknown: + R << "\nUnknown data dependence."; + break; + } + if (SourceLoc) + R << " Memory location is the same as accessed at " + << ore::NV("Location", SourceLoc); } } diff --git a/llvm/test/Analysis/LoopAccessAnalysis/pointer-phis.ll b/llvm/test/Analysis/LoopAccessAnalysis/pointer-phis.ll --- a/llvm/test/Analysis/LoopAccessAnalysis/pointer-phis.ll +++ b/llvm/test/Analysis/LoopAccessAnalysis/pointer-phis.ll @@ -127,6 +127,7 @@ ; CHECK-LABEL: 'load_with_pointer_phi_outside_loop' ; CHECK-NEXT: loop.header: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %v8 = load double, double* %ptr, align 8 -> @@ -164,6 +165,7 @@ ; CHECK-LABEL: 'store_with_pointer_phi_outside_loop' ; CHECK-NEXT: loop.header: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop. +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %v8 = load double, double* %arrayidx, align 8 -> @@ -201,6 +203,7 @@ ; CHECK-LABEL: 'store_with_pointer_phi_incoming_phi' ; CHECK-NEXT: loop.header: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %v8 = load double, double* %arrayidx, align 8 -> @@ -277,6 +280,7 @@ ; CHECK-LABEL: 'store_with_pointer_phi_incoming_phi_irreducible_cycle' ; CHECK-NEXT: loop.header: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %v8 = load double, double* %arrayidx, align 8 -> @@ -348,6 +352,7 @@ ; CHECK-LABEL: 'store_with_pointer_phi_outside_loop_select' ; CHECK-NEXT: loop.header: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop. +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %v8 = load double, double* %arrayidx, align 8 -> @@ -413,6 +418,7 @@ ; CHECK-LABEL: Loop access info in function 'phi_load_store_memdep_check': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %lv3 = load i16, i16* %c.sink, align 2 -> diff --git a/llvm/test/Analysis/LoopAccessAnalysis/pointer-with-unknown-bounds.ll b/llvm/test/Analysis/LoopAccessAnalysis/pointer-with-unknown-bounds.ll --- a/llvm/test/Analysis/LoopAccessAnalysis/pointer-with-unknown-bounds.ll +++ b/llvm/test/Analysis/LoopAccessAnalysis/pointer-with-unknown-bounds.ll @@ -13,6 +13,7 @@ ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop ; CHECK-NOT: Report: cannot identify array bounds +; CHECK-NEXT: Unknown data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Unknown: ; CHECK-NEXT: %loadA = load i16, i16* %arrayidxA, align 2 -> diff --git a/llvm/test/Analysis/LoopAccessAnalysis/stride-access-dependence.ll b/llvm/test/Analysis/LoopAccessAnalysis/stride-access-dependence.ll --- a/llvm/test/Analysis/LoopAccessAnalysis/stride-access-dependence.ll +++ b/llvm/test/Analysis/LoopAccessAnalysis/stride-access-dependence.ll @@ -118,6 +118,7 @@ ; CHECK: function 'unsafe_Read_Write': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: %0 = load i32, i32* %arrayidx, align 4 -> @@ -157,6 +158,7 @@ ; CHECK: function 'unsafe_Write_Read': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: store i32 %0, i32* %arrayidx, align 4 -> @@ -193,6 +195,7 @@ ; CHECK: function 'unsafe_Write_Write': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: store i32 %0, i32* %arrayidx, align 4 -> @@ -346,6 +349,7 @@ ; CHECK: function 'vectorizable_unscaled_Read_Write': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence that prevents store-to-load forwarding. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: BackwardVectorizableButPreventsForwarding: ; CHECK-NEXT: %2 = load i32, i32* %arrayidx, align 4 -> @@ -425,6 +429,7 @@ ; CHECK: function 'unsafe_unscaled_Read_Write': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: %2 = load i32, i32* %arrayidx, align 4 -> @@ -455,6 +460,7 @@ ; CHECK: function 'unsafe_unscaled_Read_Write2': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: %2 = load i32, i32* %arrayidx, align 4 -> @@ -505,6 +511,7 @@ ; CHECK: function 'interleaved_stores': ; CHECK-NEXT: for.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: store i32 %4, i32* %arrayidx5, align 4 -> diff --git a/llvm/test/Analysis/LoopAccessAnalysis/underlying-objects-2.ll b/llvm/test/Analysis/LoopAccessAnalysis/underlying-objects-2.ll --- a/llvm/test/Analysis/LoopAccessAnalysis/underlying-objects-2.ll +++ b/llvm/test/Analysis/LoopAccessAnalysis/underlying-objects-2.ll @@ -39,6 +39,7 @@ ; CHECK-LABEL: function 'f' ; CHECK: for_j.body: ; CHECK-NEXT: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: %loadB = load i8, i8* %gepB, align 1 -> diff --git a/llvm/test/Analysis/LoopAccessAnalysis/unsafe-and-rt-checks.ll b/llvm/test/Analysis/LoopAccessAnalysis/unsafe-and-rt-checks.ll --- a/llvm/test/Analysis/LoopAccessAnalysis/unsafe-and-rt-checks.ll +++ b/llvm/test/Analysis/LoopAccessAnalysis/unsafe-and-rt-checks.ll @@ -8,6 +8,7 @@ target triple = "x86_64-apple-macosx10.10.0" ; CHECK: Report: unsafe dependent memory operations in loop +; CHECK-NEXT: Backward loop carried data dependence. ; CHECK-NEXT: Dependences: ; CHECK-NEXT: Backward: ; CHECK-NEXT: %loadA = load i16, i16* %arrayidxA, align 2 -> diff --git a/llvm/test/Transforms/LoopVectorize/X86/vectorization-remarks-missed.ll b/llvm/test/Transforms/LoopVectorize/X86/vectorization-remarks-missed.ll --- a/llvm/test/Transforms/LoopVectorize/X86/vectorization-remarks-missed.ll +++ b/llvm/test/Transforms/LoopVectorize/X86/vectorization-remarks-missed.ll @@ -75,7 +75,7 @@ ; YAML-NEXT: ... ; YAML-NEXT: --- !Analysis ; YAML-NEXT: Pass: '' -; YAML-NEXT: Name: CantIdentifyArrayBounds +; YAML-NEXT: Name: UnknownArrayBounds ; YAML-NEXT: DebugLoc: { File: source.cpp, Line: 19, Column: 5 } ; YAML-NEXT: Function: _Z17test_array_boundsPiS_i ; YAML-NEXT: Args: diff --git a/llvm/test/Transforms/LoopVectorize/diag-with-hotness-info-2.ll b/llvm/test/Transforms/LoopVectorize/diag-with-hotness-info-2.ll --- a/llvm/test/Transforms/LoopVectorize/diag-with-hotness-info-2.ll +++ b/llvm/test/Transforms/LoopVectorize/diag-with-hotness-info-2.ll @@ -22,9 +22,12 @@ ; 19 } ; 20 } -; CHECK: remark: /tmp/s.c:2:3: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop (hotness: 300) -; CHECK: remark: /tmp/s.c:9:3: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop (hotness: 5000) -; CHECK: remark: /tmp/s.c:16:3: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop{{$}} +; CHECK: remark: /tmp/s.c:3:14: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Backward loop carried data dependence. Memory location is the same as accessed at /tmp/s.c:3:16 (hotness: 300) +; CHECK: remark: /tmp/s.c:10:14: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Backward loop carried data dependence. Memory location is the same as accessed at /tmp/s.c:10:16 (hotness: 5000) +; CHECK: remark: /tmp/s.c:17:14: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Backward loop carried data dependence. Memory location is the same as accessed at /tmp/s.c:17:16{{$}} ; ModuleID = '/tmp/s.c' source_filename = "/tmp/s.c" diff --git a/llvm/test/Transforms/LoopVectorize/memory-dep-remarks.ll b/llvm/test/Transforms/LoopVectorize/memory-dep-remarks.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/LoopVectorize/memory-dep-remarks.ll @@ -0,0 +1,378 @@ +; RUN: opt -passes='loop(require),function(loop-vectorize)' -disable-output -pass-remarks-analysis=loop-vectorize < %s 2>&1 | FileCheck %s +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" + +; // Loop has an array element B[i] (%arrayidx in IR) being used as index to +; // another array (A), and since the value of B[i] is unknown, +; // the bound for array A is unknown. +; void test_unknown_bounds(int* A, int* B, int n) { +; for(int i = 0; i < n ; ++i) +; A[i] = A[B[i]] + 1; +; } + +; CHECK: remark: source.c:4:16: loop not vectorized: cannot identify array bounds + +define void @test_unknown_bounds(i32* nocapture %A, i32* nocapture readonly %B, i64 %n) !dbg !13 { +entry: + %cmp10 = icmp sgt i64 %n, 0 + br i1 %cmp10, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] + %arrayidx = getelementptr inbounds i32, i32* %B, i64 %indvars.iv + %0 = load i32, i32* %arrayidx, align 4 + %idxprom1 = sext i32 %0 to i64, !dbg !35 + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %idxprom1, !dbg !35 + %1 = load i32, i32* %arrayidx2, align 4, !dbg !35 + %add = add nsw i32 %1, 1 + %arrayidx4 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv + store i32 %add, i32* %arrayidx4, align 4 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body, !dbg !28 +} + +; // a) Dependence::NoDep +; // Loop containing only reads (here of the array A) does not hinder vectorization +; void test_nodep(int n, int* A, int* B) { +; for(int i = 1; i < n ; ++i) { +; B[i] = A[i-1] + A[i+2]; +; } +; } + +; CHECK-NOT: remark: source.c:{{0-9]+}}:{{[0-9]+}}: + +define void @test_nodep(i64 %n, i32* nocapture readonly %A, i32* nocapture %B) !dbg !44 { +entry: + %cmp12 = icmp sgt i64 %n, 1 + br i1 %cmp12, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ] + %0 = add nsw i64 %indvars.iv, -1 + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %0, !dbg !61 + %1 = load i32, i32* %arrayidx, align 4, !dbg !61 + %2 = add nuw nsw i64 %indvars.iv, 2 + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %2, !dbg !63 + %3 = load i32, i32* %arrayidx2, align 4, !dbg !63 + %add3 = add nsw i32 %3, %1 + %arrayidx5 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv + store i32 %add3, i32* %arrayidx5, align 4 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +} + + +; // b) Dependence::Forward +; // Loop gets vectorized since it contains only a forward +; // dependency between A[i-2] and A[i] +; void test_forward(int n, int* A, int* B) { +; for(int i=1; i < n; ++i) { +; A[i] = 10; +; B[i] = A[i-2]; +; } +; } + +; CHECK-NOT: remark: source.c:{{0-9]+}}:{{[0-9]+}}: +define dso_local void @test_forward(i64 %n, i32* nocapture %A, i32* nocapture %B) !dbg !70 { +entry: + %cmp11 = icmp sgt i64 %n, 1 + br i1 %cmp11, label %for.body, label %for.cond.cleanup, !dbg !81 + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ] + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv, !dbg !83 + store i32 10, i32* %arrayidx, align 4 + %0 = add nsw i64 %indvars.iv, -2 + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %0, !dbg !87 + %1 = load i32, i32* %arrayidx2, align 4, !dbg !87 + %arrayidx4 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv, !dbg !88 + store i32 %1, i32* %arrayidx4, align 4, !dbg !89 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body, !dbg !81 +} + + +; // c) Dependence::BackwardVectorizable +; // Loop gets vectorized since it contains a backward dependency +; // between A[i] and A[i-4], but the dependency distance (4) is +; // greater than the minimum possible VF (2 in this case) +; void test_backwardVectorizable(int n, int* A, int* B) { +; for(int i=4; i < n; ++i) { +; A[i] = A[i-4] + 1; +; } +; } + +; CHECK-NOT: remark: source.c:{{0-9]+}}:{{[0-9]+}}: + +define dso_local void @test_backwardVectorizable(i64 %n, i32* nocapture %A, i32* nocapture readnone %B) !dbg !93 { +entry: + %cmp8 = icmp sgt i64 %n, 4 + br i1 %cmp8, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 4, %entry ], [ %indvars.iv.next, %for.body ] + %0 = add nsw i64 %indvars.iv, -4, !dbg !106 + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %0, !dbg !108 + %1 = load i32, i32* %arrayidx, align 4, !dbg !108 + %add = add nsw i32 %1, 1 + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv, !dbg !110 + store i32 %add, i32* %arrayidx2, align 4, !dbg !111 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +} + +; // d) Dependence::Backward +; // Loop does not get vectorized since it contains a backward +; // dependency between A[i] and A[i+3]. +; void test_backward_dep(int n, double *A) { +; for (int i = 1; i <= n - 3; i += 3) { +; A[i] = A[i-1]; +; A[i+1] = A[i+3]; +; } +; } + +; CHECK: remark: source.c:48:14: loop not vectorized: unsafe dependent memory operations in loop. +; CHECK-NEXT: Backward loop carried data dependence. Memory location is the same as accessed at source.c:47:5 + +define void @test_backward_dep(i32 %n, double* nocapture %A) { +entry: + %cmp.not19 = icmp slt i32 %n, 4 + br i1 %cmp.not19, label %for.cond.cleanup, label %for.body.preheader + +for.body.preheader: ; preds = %entry + %sub = add nsw i32 %n, -3 + %0 = zext i32 %sub to i64 + br label %for.body + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %for.body.preheader, %for.body + %indvars.iv = phi i64 [ 1, %for.body.preheader ], [ %indvars.iv.next, %for.body ] + %1 = add nsw i64 %indvars.iv, -1 + %arrayidx = getelementptr inbounds double, double* %A, i64 %1 + %2 = load double, double* %arrayidx, align 8 + %arrayidx3 = getelementptr inbounds double, double* %A, i64 %indvars.iv, !dbg !157 + store double %2, double* %arrayidx3, align 8 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 3 + %arrayidx5 = getelementptr inbounds double, double* %A, i64 %indvars.iv.next, !dbg !160 + %3 = load double, double* %arrayidx5, align 8, !dbg !160 + %4 = add nuw nsw i64 %indvars.iv, 1 + %arrayidx8 = getelementptr inbounds double, double* %A, i64 %4 + store double %3, double* %arrayidx8, align 8 + %cmp.not = icmp ugt i64 %indvars.iv.next, %0 + br i1 %cmp.not, label %for.cond.cleanup, label %for.body +} + +; // e) Dependence::ForwardButPreventsForwarding +; // Loop does not get vectorized despite only having a forward +; // dependency between A[i] and A[i-3]. +; // This is because the store-to-load forwarding distance (here 3) +; // needs to be a multiple of vector factor otherwise the +; // store (A[5:6] in i=5) and load (A[4:5],A[6:7] in i=7,9) are unaligned. +; void test_forwardPreventsForwarding_dep(int* A, int* B, int n) { +; for(int i=3; i < n; ++i) { +; A[i] = 10; +; B[i] = A[i-3]; +; } +; } + +; CHECK: remark: source.c:61:12: loop not vectorized: unsafe dependent memory operations in loop. +; CHECK-NEXT: Forward loop carried data dependence that prevents store-to-load forwarding. Memory location is the same as accessed at source.c:60:5 + +define void @test_forwardPreventsForwarding_dep(i32* nocapture %A, i32* nocapture %B, i64 %n) !dbg !166 { +entry: + %cmp11 = icmp sgt i64 %n, 3 + br i1 %cmp11, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 3, %entry ], [ %indvars.iv.next, %for.body ] + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv, !dbg !179 + store i32 10, i32* %arrayidx, align 4 + %0 = add nsw i64 %indvars.iv, -3 + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %0, !dbg !183 + %1 = load i32, i32* %arrayidx2, align 4, !dbg !183 + %arrayidx4 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv + store i32 %1, i32* %arrayidx4, align 4 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +} + +; // f) Dependence::BackwardVectorizableButPreventsForwarding +; // Loop does not get vectorized despite having a backward +; // but vectorizable dependency between A[i] and A[i-15]. +; // +; // This is because the store-to-load forwarding distance (here 15) +; // needs to be a multiple of vector factor otherwise +; // store (A[16:17] in i=16) and load (A[15:16], A[17:18] in i=30,32) are unaligned. +; void test_backwardVectorizableButPreventsForwarding(int* A, int n) { +; for(int i=15; i < n; ++i) { +; A[i] = A[i-2] + A[i-15]; +; } +; } + +; CHECK: remark: source.c:74:5: loop not vectorized: unsafe dependent memory operations in loop. +; CHECK: Backward loop carried data dependence that prevents store-to-load forwarding. Memory location is the same as accessed at source.c:74:21 + +define void @test_backwardVectorizableButPreventsForwarding(i32* nocapture %A, i64 %n) !dbg !189 { +entry: + %cmp13 = icmp sgt i64 %n, 15 + br i1 %cmp13, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 15, %entry ], [ %indvars.iv.next, %for.body ] + %0 = add nsw i64 %indvars.iv, -2 + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %0 + %1 = load i32, i32* %arrayidx, align 4 + %2 = add nsw i64 %indvars.iv, -15 + %arrayidx3 = getelementptr inbounds i32, i32* %A, i64 %2, !dbg !207 + %3 = load i32, i32* %arrayidx3, align 4 + %add = add nsw i32 %3, %1 + %arrayidx5 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv, !dbg !209 + store i32 %add, i32* %arrayidx5, align 4, !dbg !209 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +} + +; // g) Dependence::Unknown +; // Different stride lengths +; void test_unknown_dep(int* A, int n) { +; for(int i=0; i < n; ++i) { +; A[(i+1)*4] = 10; +; A[i] = 100; +; } +; } + +; CHECK: remark: source.c:83:7: loop not vectorized: unsafe dependent memory operations in loop. +; CHECK: Unknown data dependence. Memory location is the same as accessed at source.c:82:7 + +define void @test_unknown_dep(i32* nocapture %A, i64 %n) !dbg !214 { +entry: + %cmp8 = icmp sgt i64 %n, 0 + br i1 %cmp8, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + ret void + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %0 = shl nsw i64 %indvars.iv.next, 2 + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %0, !dbg !229 + store i32 10, i32* %arrayidx, align 4 + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv, !dbg !231 + store i32 100, i32* %arrayidx2, align 4, !dbg !231 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!4} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 14.0.0 (https://github.com/llvm/llvm-project.git 54f0f826c5c7d0ff16c230b259cb6aad33e18d97)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "source.c", directory: "") +!2 = !{} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!13 = distinct !DISubprogram(name: "test_unknown_bounds", scope: !1, file: !1, line: 2, type: !14, scopeLine: 2, unit: !0, retainedNodes: !18) +!14 = !DISubroutineType(types: !15) +!15 = !{null, !16, !16, !17} +!16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !17, size: 64) +!17 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!18 = !{} +!23 = distinct !DILexicalBlock(scope: !13, file: !1, line: 3, column: 5) +!27 = distinct !DILexicalBlock(scope: !23, file: !1, line: 3, column: 5) +!28 = !DILocation(line: 3, column: 5, scope: !23) +!35 = !DILocation(line: 4, column: 16, scope: !27) +!44 = distinct !DISubprogram(name: "test_nodep", scope: !1, file: !1, line: 14, type: !45, scopeLine: 14, unit: !0, retainedNodes: !47) +!45 = !DISubroutineType(types: !46) +!46 = !{null, !17, !16, !16} +!47 = !{} +!52 = distinct !DILexicalBlock(scope: !44, file: !1, line: 15, column: 3) +!56 = distinct !DILexicalBlock(scope: !52, file: !1, line: 15, column: 3) +!60 = distinct !DILexicalBlock(scope: !56, file: !1, line: 15, column: 31) +!61 = !DILocation(line: 16, column: 12, scope: !60) +!63 = !DILocation(line: 16, column: 21, scope: !60) +!70 = distinct !DISubprogram(name: "test_forward", scope: !1, file: !1, line: 24, type: !45, scopeLine: 24, unit: !0, retainedNodes: !71) +!71 = !{} +!77 = distinct !DILexicalBlock(scope: !70, file: !1, line: 25, column: 3) +!80 = distinct !DILexicalBlock(scope: !77, file: !1, line: 25, column: 3) +!81 = !DILocation(line: 25, column: 3, scope: !77) +!83 = !DILocation(line: 26, column: 5, scope: !84) +!84 = distinct !DILexicalBlock(scope: !80, file: !1, line: 25, column: 28) +!87 = !DILocation(line: 27, column: 12, scope: !84) +!88 = !DILocation(line: 27, column: 5, scope: !84) +!89 = !DILocation(line: 27, column: 10, scope: !84) +!93 = distinct !DISubprogram(name: "test_backwardVectorizable", scope: !1, file: !1, line: 36, type: !45, scopeLine: 36, unit: !0, retainedNodes: !94) +!94 = !{} +!99 = distinct !DILexicalBlock(scope: !93, file: !1, line: 37, column: 3) +!103 = distinct !DILexicalBlock(scope: !99, file: !1, line: 37, column: 3) +!106 = !DILocation(line: 38, column: 15, scope: !107) +!107 = distinct !DILexicalBlock(scope: !103, file: !1, line: 37, column: 28) +!108 = !DILocation(line: 38, column: 12, scope: !107) +!110 = !DILocation(line: 38, column: 5, scope: !107) +!111 = !DILocation(line: 38, column: 10, scope: !107) +!136 = distinct !DISubprogram(name: "test_backward_dep", scope: !1, file: !1, line: 45, type: !137, scopeLine: 45, unit: !0, retainedNodes: !141) +!137 = !DISubroutineType(types: !138) +!138 = !{null, !17, !139} +!139 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !140, size: 64) +!140 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float) +!141 = !{} +!145 = distinct !DILexicalBlock(scope: !136, file: !1, line: 46, column: 3) +!149 = distinct !DILexicalBlock(scope: !145, file: !1, line: 46, column: 3) +!153 = distinct !DILexicalBlock(scope: !149, file: !1, line: 46, column: 39) +!157 = !DILocation(line: 47, column: 5, scope: !153) +!160 = !DILocation(line: 48, column: 14, scope: !153) +!166 = distinct !DISubprogram(name: "test_forwardPreventsForwarding_dep", scope: !1, file: !1, line: 58, type: !14, scopeLine: 58, unit: !0, retainedNodes: !167) +!167 = !{} +!172 = distinct !DILexicalBlock(scope: !166, file: !1, line: 59, column: 3) +!175 = !DILocation(line: 59, column: 18, scope: !176) +!176 = distinct !DILexicalBlock(scope: !172, file: !1, line: 59, column: 3) +!177 = !DILocation(line: 59, column: 3, scope: !172) +!178 = !DILocation(line: 63, column: 1, scope: !166) +!179 = !DILocation(line: 60, column: 5, scope: !180) +!180 = distinct !DILexicalBlock(scope: !176, file: !1, line: 59, column: 28) +!181 = !DILocation(line: 60, column: 10, scope: !180) +!182 = !DILocation(line: 61, column: 15, scope: !180) +!183 = !DILocation(line: 61, column: 12, scope: !180) +!184 = !DILocation(line: 61, column: 5, scope: !180) +!185 = !DILocation(line: 61, column: 10, scope: !180) +!186 = !DILocation(line: 59, column: 23, scope: !176) +!189 = distinct !DISubprogram(name: "test_backwardVectorizableButPreventsForwarding", scope: !1, file: !1, line: 72, type: !190, scopeLine: 72, unit: !0, retainedNodes: !192) +!190 = !DISubroutineType(types: !191) +!191 = !{null, !16, !17} +!192 = !{} +!196 = distinct !DILexicalBlock(scope: !189, file: !1, line: 73, column: 3) +!200 = distinct !DILexicalBlock(scope: !196, file: !1, line: 73, column: 3) +!204 = distinct !DILexicalBlock(scope: !200, file: !1, line: 73, column: 29) +!207 = !DILocation(line: 74, column: 21, scope: !204) +!209 = !DILocation(line: 74, column: 5, scope: !204) +!214 = distinct !DISubprogram(name: "test_unknown_dep", scope: !1, file: !1, line: 80, type: !190, scopeLine: 80, unit: !0, retainedNodes: !215) +!215 = !{} +!219 = distinct !DILexicalBlock(scope: !214, file: !1, line: 81, column: 3) +!223 = distinct !DILexicalBlock(scope: !219, file: !1, line: 81, column: 3) +!227 = distinct !DILexicalBlock(scope: !223, file: !1, line: 81, column: 28) +!229 = !DILocation(line: 82, column: 7, scope: !227) +!231 = !DILocation(line: 83, column: 7, scope: !227) diff --git a/llvm/test/Transforms/LoopVectorize/unsafe-dep-remark.ll b/llvm/test/Transforms/LoopVectorize/unsafe-dep-remark.ll --- a/llvm/test/Transforms/LoopVectorize/unsafe-dep-remark.ll +++ b/llvm/test/Transforms/LoopVectorize/unsafe-dep-remark.ll @@ -11,7 +11,7 @@ ; 5 } ; 6 } -; CHECK: remark: /tmp/kk.c:2:3: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK: remark: /tmp/kk.c:3:14: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop define void @success(i8* nocapture %A, i8* nocapture readonly %B, i8* nocapture %C, i8* nocapture readonly %D, i8* nocapture readonly %E, i32 %N) !dbg !6 { entry: