Skip to content

Commit a72357f

Browse files
TiborGYGYTfhahn
authored andcommitted
[LV] Emit better debug and opt-report messages when vectorization is disallowed in the LoopVectorizer (llvm#158513)
While looking into fixing llvm#158499, I found some other cases where the messages emitted could be improved. This PR improves both the messages printed to the debug output and the missed-optimization messages in cases where: - loop vectorization is explicitly disabled - loop vectorization is implicitly disabled by disabling all loop transformations - loop vectorization is set to happen only where explicitly enabled A branch that should currently be unreachable is also added. If the related logic ever breaks (eg. due to changes to getForce() or the ForceKind enum) this should alert devs and users. New test cases are also added to verify that the correct messages (and only them) are outputted. --------- Co-authored-by: GYT <[email protected]> Co-authored-by: Florian Hahn <[email protected]>
1 parent 84aa119 commit a72357f

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ class LoopVectorizeHints {
196196

197197
/// Interface to emit optimization remarks.
198198
OptimizationRemarkEmitter &ORE;
199+
200+
/// Reports a condition where loop vectorization is disallowed: prints
201+
/// \p DebugMsg for debugging purposes along with the corresponding
202+
/// optimization remark \p RemarkName, with \p RemarkMsg as the user-facing
203+
/// message. The loop \p L is used for the location of the remark.
204+
void reportDisallowedVectorization(const StringRef DebugMsg,
205+
const StringRef RemarkName,
206+
const StringRef RemarkMsg,
207+
const Loop *L) const;
199208
};
200209

201210
/// This holds vectorization requirements that must be verified late in

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,37 @@ void LoopVectorizeHints::setAlreadyVectorized() {
179179
IsVectorized.Value = 1;
180180
}
181181

182+
void LoopVectorizeHints::reportDisallowedVectorization(
183+
const StringRef DebugMsg, const StringRef RemarkName,
184+
const StringRef RemarkMsg, const Loop *L) const {
185+
LLVM_DEBUG(dbgs() << "LV: Not vectorizing: " << DebugMsg << ".\n");
186+
ORE.emit(OptimizationRemarkMissed(LV_NAME, RemarkName, L->getStartLoc(),
187+
L->getHeader())
188+
<< "loop not vectorized: " << RemarkMsg);
189+
}
190+
182191
bool LoopVectorizeHints::allowVectorization(
183192
Function *F, Loop *L, bool VectorizeOnlyWhenForced) const {
184193
if (getForce() == LoopVectorizeHints::FK_Disabled) {
185-
LLVM_DEBUG(dbgs() << "LV: Not vectorizing: #pragma vectorize disable.\n");
186-
emitRemarkWithHints();
194+
if (Force.Value == LoopVectorizeHints::FK_Disabled) {
195+
reportDisallowedVectorization("#pragma vectorize disable",
196+
"MissedExplicitlyDisabled",
197+
"vectorization is explicitly disabled", L);
198+
} else if (hasDisableAllTransformsHint(L)) {
199+
reportDisallowedVectorization("loop hasDisableAllTransformsHint",
200+
"MissedTransformsDisabled",
201+
"loop transformations are disabled", L);
202+
} else {
203+
llvm_unreachable("loop vect disabled for an unknown reason");
204+
}
187205
return false;
188206
}
189207

190208
if (VectorizeOnlyWhenForced && getForce() != LoopVectorizeHints::FK_Enabled) {
191-
LLVM_DEBUG(dbgs() << "LV: Not vectorizing: No #pragma vectorize enable.\n");
192-
emitRemarkWithHints();
209+
reportDisallowedVectorization(
210+
"VectorizeOnlyWhenForced is set, and no #pragma vectorize enable",
211+
"MissedForceOnly", "only vectorizing loops that explicitly request it",
212+
L);
193213
return false;
194214
}
195215

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
; REQUIRES: asserts
2+
3+
; TEST 1
4+
; Checks that we emit only the correct debug messages and
5+
; optimization remark when the loop vectorizer is disabled by loop metadata.
6+
; RUN: opt -S -passes=loop-vectorize -pass-remarks=loop-vectorize \
7+
; RUN: -pass-remarks-missed=loop-vectorize \
8+
; RUN: -pass-remarks-analysis=loop-vectorize -debug \
9+
; RUN: < %s 2>&1 | FileCheck --check-prefixes=METADATA,ALL %s
10+
; TEST 2
11+
; Checks that we emit only the correct debug messages and
12+
; optimization remark when the loop is not vectorized due to the
13+
; vectorize-forced-only pass option being set.
14+
; Strip metadata for FORCEDONLY run, keep it for METADATA run
15+
; RUN: sed 's/,[[:space:]]*!llvm\.loop[[:space:]]*!0//' %s | \
16+
; RUN: opt -S -passes='loop-vectorize<vectorize-forced-only>' \
17+
; RUN: -pass-remarks=loop-vectorize \
18+
; RUN: -pass-remarks-missed=loop-vectorize \
19+
; RUN: -pass-remarks-analysis=loop-vectorize -debug \
20+
; RUN: 2>&1 | FileCheck --check-prefixes=FORCEDONLY,ALL %s
21+
; TEST 3
22+
; Checks that we emit only the correct debug messages and
23+
; optimization remark when the loop vectorizer is disabled by loop metadata
24+
; that requests no loop transformations.
25+
; RUN: opt -S -passes=loop-vectorize -pass-remarks=loop-vectorize \
26+
; RUN: -pass-remarks-missed=loop-vectorize \
27+
; RUN: -pass-remarks-analysis=loop-vectorize -debug \
28+
; RUN: -force-vector-interleave=1 -force-vector-width=2 \
29+
; RUN: < %s 2>&1 | FileCheck --check-prefix=ALL %s
30+
31+
; ALL-LABEL: 'disabled_loop_vectorization' from <stdin>
32+
; ALL-NOT: LV: We can vectorize this loop
33+
; ALL-NOT: LV: Not vectorizing: loop hasDisableAllTransformsHint
34+
; ALL-NOT: LV: Not vectorizing: Disabled/already vectorized
35+
; ALL-NOT: LV: Not vectorizing: Cannot prove legality
36+
;
37+
; METADATA-NOT: LV: Not vectorizing: VectorizeOnlyWhenForced is set
38+
; METADATA: LV: Loop hints: force=disabled
39+
; METADATA: LV: Not vectorizing: #pragma vectorize disable.
40+
; METADATA: remark:
41+
; METADATA-SAME: loop not vectorized: vectorization is explicitly disabled
42+
;
43+
; FORCEDONLY-NOT: LV: Not vectorizing: #pragma vectorize disable
44+
; FORCEDONLY: LV: Loop hints: force=?
45+
; FORCEDONLY: LV: Not vectorizing: VectorizeOnlyWhenForced is set, and no #pragma vectorize enable
46+
; FORCEDONLY: remark:
47+
; FORCEDONLY-SAME: loop not vectorized: only vectorizing loops that explicitly request it
48+
;
49+
; ALL: LV: Loop hints prevent vectorization
50+
define void @disabled_loop_vectorization(ptr %src) {
51+
entry:
52+
br label %loop
53+
54+
loop:
55+
%iv = phi i64 [ 0, %entry ], [ %inc, %loop ]
56+
%arrayidx = getelementptr inbounds nuw double, ptr %src, i64 %iv
57+
store double 0.0, ptr %arrayidx, align 8
58+
%inc = add nuw nsw i64 %iv, 1
59+
%exitcond.not = icmp eq i64 %inc, 15
60+
br i1 %exitcond.not, label %exit, label %loop, !llvm.loop !0
61+
62+
exit:
63+
ret void
64+
}
65+
!0 = distinct !{!0, !1}
66+
!1 = !{!"llvm.loop.vectorize.enable", i1 false}
67+
68+
; ALL-LABEL: 'disable_nonforced' from <stdin>
69+
; ALL-NOT: LV: We can vectorize this loop
70+
; ALL-NOT: LV: Not vectorizing: #pragma vectorize disable.
71+
; ALL-NOT: LV: Not vectorizing: VectorizeOnlyWhenForced is set
72+
; ALL-NOT: LV: Not vectorizing: Disabled/already vectorized
73+
; ALL-NOT: LV: Not vectorizing: Cannot prove legality
74+
; ALL: LV: Loop hints: force=disabled
75+
; ALL: LV: Not vectorizing: loop hasDisableAllTransformsHint.
76+
; ALL: remark:
77+
; ALL-SAME: loop not vectorized: loop transformations are disabled
78+
; ALL: LV: Loop hints prevent vectorization
79+
define void @disable_nonforced(ptr nocapture %a, i32 %n) {
80+
entry:
81+
br label %loop
82+
83+
loop:
84+
%iv = phi i32 [ %iv.next, %loop ], [ 0, %entry ]
85+
%arrayidx = getelementptr inbounds i32, ptr %a, i32 %iv
86+
store i32 %iv, ptr %arrayidx, align 4
87+
%iv.next = add i32 %iv, 1
88+
%exitcond = icmp eq i32 %iv.next, %n
89+
br i1 %exitcond, label %end, label %loop, !llvm.loop !2
90+
91+
end:
92+
ret void
93+
}
94+
!2 = !{!2, !{!"llvm.loop.disable_nonforced"}}

0 commit comments

Comments
 (0)