[PATCH] D150706: [LAA] Update MaxSafeDepDistBytes when non-unit stride
Michael Maitland via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 10 09:07:06 PDT 2023
michaelmaitland updated this revision to Diff 538682.
michaelmaitland added a comment.
Rebase once more. This patch is ready for review.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D150706/new/
https://reviews.llvm.org/D150706
Files:
llvm/lib/Analysis/LoopAccessAnalysis.cpp
llvm/test/Analysis/LoopAccessAnalysis/max_safe_dep_dist_non_unit_stride.ll
Index: llvm/test/Analysis/LoopAccessAnalysis/max_safe_dep_dist_non_unit_stride.ll
===================================================================
--- llvm/test/Analysis/LoopAccessAnalysis/max_safe_dep_dist_non_unit_stride.ll
+++ llvm/test/Analysis/LoopAccessAnalysis/max_safe_dep_dist_non_unit_stride.ll
@@ -10,7 +10,7 @@
define void @foo(i64 %len, ptr %a) {
; CHECK-LABEL: Loop access info in function 'foo':
; CHECK-NEXT: loop:
-; CHECK-NEXT: Memory dependences are safe with a maximum dependence distance of 24 bytes
+; CHECK-NEXT: Memory dependences are safe with a maximum dependence distance of 8 bytes
; CHECK-NEXT: Dependences:
; CHECK-NEXT: BackwardVectorizable:
; CHECK-NEXT: store i32 %0, ptr %arrayidx2, align 4 ->
Index: llvm/lib/Analysis/LoopAccessAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1987,14 +1987,42 @@
// B[i+2] = B[i] + 1;
// }
// }
- //
// This case is currently unsafe according to the max safe distance. If we
// analyze the two accesses on array B, the max safe dependence distance
// is 2. Then we analyze the accesses on array A, the minimum distance needed
// is 8, which is less than 2 and forbidden vectorization, But actually
// both A and B could be vectorized by 2 iterations.
- MaxSafeDepDistBytes =
- std::min(static_cast<uint64_t>(Distance), MaxSafeDepDistBytes);
+ //
+ // Distance must be reduced by a factor of the stride of the loop induction
+ // variable, otherwise it is possible that MaxSafeDepDistBytes is too
+ // large. For example,
+ // for (int k = 0; k < len; k+=3) {
+ // a[k] = a[k + 4];
+ // a[k+2] = a[k+6];
+ // }
+ // without accounting for loop stride has MaxSafeDepDist=24 when it it must be
+ // 8.
+ auto Bounds = InnermostLoop->getBounds(SE);
+ if (!Bounds) {
+ LLVM_DEBUG(dbgs() << "LAA: Could not determine bounds of loop induction "
+ "variable, so the MaxSafeDepDistBytes is unknown");
+ MaxSafeDepDistBytes = 0;
+ return Dependence::Unknown;
+ }
+ const SCEV *StepVal = SE.getSCEV(Bounds->getStepValue());
+ const SCEVConstant *StepValC = dyn_cast<SCEVConstant>(StepVal);
+ if (!StepValC) {
+ LLVM_DEBUG(dbgs() << "LAA: Could not determine step value of loop induction "
+ "variable, so the MaxSafeDepDistBytes is unknown");
+ MaxSafeDepDistBytes = 0;
+ return Dependence::Unknown;
+ }
+
+ const APInt &LoopIVStrideAP = StepValC->getAPInt().abs();
+ uint64_t LoopIVStride = LoopIVStrideAP.getZExtValue();
+
+ MaxSafeDepDistBytes = std::min(static_cast<uint64_t>(Distance / LoopIVStride),
+ MaxSafeDepDistBytes);
bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
if (IsTrueDataDependence && EnableForwardingConflictDetection &&
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150706.538682.patch
Type: text/x-patch
Size: 2956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230710/39d14c82/attachment.bin>
More information about the llvm-commits
mailing list