Skip to content

Commit 76c6a8b

Browse files
authored
[LAA] Improve the output remark for LoopVectorize (#65832)
Don't report 'Use #pragma loop distribute(enable) to allow loop distribution' when we already add #pragma clang loop distribute(enable) Fixes #64637
1 parent 64c9dbb commit 76c6a8b

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,12 +2505,24 @@ void LoopAccessInfo::emitUnsafeDependenceRemark() {
25052505
LLVM_DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n");
25062506

25072507
// Emit remark for first unsafe dependence
2508+
bool HasForcedDistribution = false;
2509+
std::optional<const MDOperand *> Value =
2510+
findStringMetadataForLoop(TheLoop, "llvm.loop.distribute.enable");
2511+
if (Value) {
2512+
const MDOperand *Op = *Value;
2513+
assert(Op && mdconst::hasa<ConstantInt>(*Op) && "invalid metadata");
2514+
HasForcedDistribution = mdconst::extract<ConstantInt>(*Op)->getZExtValue();
2515+
}
2516+
2517+
const std::string Info =
2518+
HasForcedDistribution
2519+
? "unsafe dependent memory operations in loop."
2520+
: "unsafe dependent memory operations in loop. Use "
2521+
"#pragma loop distribute(enable) to allow loop distribution "
2522+
"to attempt to isolate the offending operations into a separate "
2523+
"loop";
25082524
OptimizationRemarkAnalysis &R =
2509-
recordAnalysis("UnsafeDep", Dep.getDestination(*this))
2510-
<< "unsafe dependent memory operations in loop. Use "
2511-
"#pragma loop distribute(enable) to allow loop distribution "
2512-
"to attempt to isolate the offending operations into a separate "
2513-
"loop";
2525+
recordAnalysis("UnsafeDep", Dep.getDestination(*this)) << Info;
25142526

25152527
switch (Dep.Type) {
25162528
case MemoryDepChecker::Dependence::NoDep:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; RUN: opt -S -passes='print<access-info>' -pass-remarks-analysis=loop-vectorize -disable-output < %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
2+
3+
; Test that LoopVectorize don't report 'Use #pragma loop distribute(enable) to allow loop distribution'
4+
; when we already add #pragma clang loop distribute(enable).
5+
;
6+
; Testcase derived from the following C:
7+
;
8+
; #define M 100
9+
; void foo (int *restrict y, int *restrict x, int *restrict indices, int n)
10+
; {
11+
; int k = 3;
12+
; #pragma clang loop distribute(enable)
13+
; for (int i = 0; i < n; i++) {
14+
; y[i + k * M] = y[i + k* M] + 1;
15+
; y[i + k * (M+1)] = indices[i] + 2;
16+
; }
17+
; }
18+
19+
define void @foo(ptr noalias nocapture noundef %y, ptr noalias nocapture noundef readnone %x, ptr noalias nocapture noundef readonly %indices, i32 noundef %n) {
20+
; ANALYSIS: Report: unsafe dependent memory operations in loop.
21+
; ANALYSIS: Backward loop carried data dependence that prevents store-to-load forwarding.
22+
entry:
23+
%cmp22 = icmp sgt i32 %n, 0
24+
br i1 %cmp22, label %for.body.preheader, label %for.cond.cleanup
25+
26+
for.body.preheader: ; preds = %entry
27+
%wide.trip.count = zext i32 %n to i64
28+
br label %for.body
29+
30+
for.cond.cleanup.loopexit: ; preds = %for.body
31+
br label %for.cond.cleanup
32+
33+
for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
34+
ret void
35+
36+
for.body: ; preds = %for.body, %for.body.preheader
37+
%indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
38+
%0 = add nuw nsw i64 %indvars.iv, 300
39+
%arrayidx = getelementptr inbounds i32, ptr %y, i64 %0
40+
%1 = load i32, ptr %arrayidx, align 4
41+
%add1 = add nsw i32 %1, 1
42+
store i32 %add1, ptr %arrayidx, align 4
43+
%arrayidx7 = getelementptr inbounds i32, ptr %indices, i64 %indvars.iv
44+
%2 = load i32, ptr %arrayidx7, align 4
45+
%add8 = add nsw i32 %2, 2
46+
%3 = add nuw nsw i64 %indvars.iv, 303
47+
%arrayidx12 = getelementptr inbounds i32, ptr %y, i64 %3
48+
store i32 %add8, ptr %arrayidx12, align 4
49+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
50+
%exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
51+
br i1 %exitcond.not, label %for.cond.cleanup.loopexit, label %for.body, !llvm.loop !0
52+
}
53+
54+
!0 = distinct !{!0, !1}
55+
!1 = !{!"llvm.loop.distribute.enable", i1 true}

0 commit comments

Comments
 (0)