Skip to content

Commit c752924

Browse files
authored
[SandboxVec][BottomUpVec] Add -sbvec-stop-bndl flag for debugging (#129132)
This patch adds a helper flag for bisection debugging. This flag force-stops vectorization after this many bundles have been considered for vectorization. Using -sbvec-stop-bndl=0 will not vectorize the code at all.
1 parent dd3c4fb commit c752924

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ enum class ResultReason {
9898
CantSchedule,
9999
Unimplemented,
100100
Infeasible,
101+
ForcePackForDebugging,
101102
};
102103

103104
#ifndef NDEBUG
@@ -142,6 +143,8 @@ struct ToStr {
142143
return "Unimplemented";
143144
case ResultReason::Infeasible:
144145
return "Infeasible";
146+
case ResultReason::ForcePackForDebugging:
147+
return "ForcePackForDebugging";
145148
}
146149
llvm_unreachable("Unknown ResultReason enum");
147150
}
@@ -347,6 +350,10 @@ class LegalityAnalysis {
347350
// TODO: Try to remove the SkipScheduling argument by refactoring the tests.
348351
const LegalityResult &canVectorize(ArrayRef<Value *> Bndl,
349352
bool SkipScheduling = false);
353+
/// \Returns a Pack with reason 'ForcePackForDebugging'.
354+
const LegalityResult &getForcedPackForDebugging() {
355+
return createLegalityResult<Pack>(ResultReason::ForcePackForDebugging);
356+
}
350357
void clear();
351358
};
352359

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class BottomUpVec final : public RegionPass {
8181
#endif // NDEBUG
8282
};
8383
ActionsVector Actions;
84+
/// Helper counter for debugging. It counts the bundles that we attempt to
85+
/// vectorize in vectorizeRec().
86+
unsigned DebugBndlCnt = 0;
87+
8488
/// Recursively try to vectorize \p Bndl and its operands. This populates the
8589
/// `Actions` vector.
8690
Action *vectorizeRec(ArrayRef<Value *> Bndl, ArrayRef<Value *> UserBndl,

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ static cl::opt<unsigned long>
3232
cl::desc("Vectorize if the invocation count is < than this. 0 "
3333
"disables vectorization."));
3434

35+
static constexpr const unsigned long StopBundleDisabled =
36+
std::numeric_limits<unsigned long>::max();
37+
static cl::opt<unsigned long>
38+
StopBundle("sbvec-stop-bndl", cl::init(StopBundleDisabled), cl::Hidden,
39+
cl::desc("Vectorize up to this many bundles."));
40+
3541
namespace sandboxir {
3642

3743
static SmallVector<Value *, 4> getOperand(ArrayRef<Value *> Bndl,
@@ -270,7 +276,10 @@ void BottomUpVec::collectPotentiallyDeadInstrs(ArrayRef<Value *> Bndl) {
270276

271277
Action *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
272278
ArrayRef<Value *> UserBndl, unsigned Depth) {
273-
const auto &LegalityRes = Legality->canVectorize(Bndl);
279+
bool StopForDebug =
280+
DebugBndlCnt++ >= StopBundle && StopBundle != StopBundleDisabled;
281+
const auto &LegalityRes = StopForDebug ? Legality->getForcedPackForDebugging()
282+
: Legality->canVectorize(Bndl);
274283
auto ActionPtr =
275284
std::make_unique<Action>(&LegalityRes, Bndl, UserBndl, Depth);
276285
SmallVector<Action *> Operands;
@@ -469,6 +478,7 @@ bool BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) {
469478
DeadInstrCandidates.clear();
470479
Legality->clear();
471480
Actions.clear();
481+
DebugBndlCnt = 0;
472482
vectorizeRec(Bndl, {}, /*Depth=*/0);
473483
emitVectors();
474484
tryEraseDeadInstrs();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=sandbox-vectorizer -sbvec-vec-reg-bits=1024 -sbvec-allow-non-pow2 -sbvec-passes="seed-collection<tr-save,bottom-up-vec,tr-accept>" -sbvec-stop-bndl=0 %s -S | FileCheck %s --check-prefix=STOP0
3+
; RUN: opt -passes=sandbox-vectorizer -sbvec-vec-reg-bits=1024 -sbvec-allow-non-pow2 -sbvec-passes="seed-collection<tr-save,bottom-up-vec,tr-accept>" -sbvec-stop-bndl=1 %s -S | FileCheck %s --check-prefix=STOP1
4+
; RUN: opt -passes=sandbox-vectorizer -sbvec-vec-reg-bits=1024 -sbvec-allow-non-pow2 -sbvec-passes="seed-collection<tr-save,bottom-up-vec,tr-accept>" -sbvec-stop-bndl=2 %s -S | FileCheck %s --check-prefix=STOP2
5+
; RUN: opt -passes=sandbox-vectorizer -sbvec-vec-reg-bits=1024 -sbvec-allow-non-pow2 -sbvec-passes="seed-collection<tr-save,bottom-up-vec,tr-accept>" -sbvec-stop-bndl=3 %s -S | FileCheck %s --check-prefix=STOP3
6+
; RUN: opt -passes=sandbox-vectorizer -sbvec-vec-reg-bits=1024 -sbvec-allow-non-pow2 -sbvec-passes="seed-collection<tr-save,bottom-up-vec,tr-accept>" %s -S | FileCheck %s --check-prefix=NOSTOP
7+
8+
define void @stop_bndl(ptr %ptr) {
9+
; STOP0-LABEL: define void @stop_bndl(
10+
; STOP0-SAME: ptr [[PTR:%.*]]) {
11+
; STOP0-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
12+
; STOP0-NEXT: [[PTR1:%.*]] = getelementptr float, ptr [[PTR]], i32 1
13+
; STOP0-NEXT: [[LD0:%.*]] = load float, ptr [[PTR0]], align 4
14+
; STOP0-NEXT: [[LD1:%.*]] = load float, ptr [[PTR1]], align 4
15+
; STOP0-NEXT: [[ADD0:%.*]] = fadd float [[LD0]], 0.000000e+00
16+
; STOP0-NEXT: [[ADD1:%.*]] = fadd float [[LD1]], 0.000000e+00
17+
; STOP0-NEXT: store float [[ADD0]], ptr [[PTR0]], align 4
18+
; STOP0-NEXT: store float [[ADD1]], ptr [[PTR1]], align 4
19+
; STOP0-NEXT: ret void
20+
;
21+
; STOP1-LABEL: define void @stop_bndl(
22+
; STOP1-SAME: ptr [[PTR:%.*]]) {
23+
; STOP1-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
24+
; STOP1-NEXT: [[PTR1:%.*]] = getelementptr float, ptr [[PTR]], i32 1
25+
; STOP1-NEXT: [[LD0:%.*]] = load float, ptr [[PTR0]], align 4
26+
; STOP1-NEXT: [[LD1:%.*]] = load float, ptr [[PTR1]], align 4
27+
; STOP1-NEXT: [[ADD0:%.*]] = fadd float [[LD0]], 0.000000e+00
28+
; STOP1-NEXT: [[ADD1:%.*]] = fadd float [[LD1]], 0.000000e+00
29+
; STOP1-NEXT: [[PACK:%.*]] = insertelement <2 x float> poison, float [[ADD0]], i32 0
30+
; STOP1-NEXT: [[PACK1:%.*]] = insertelement <2 x float> [[PACK]], float [[ADD1]], i32 1
31+
; STOP1-NEXT: store <2 x float> [[PACK1]], ptr [[PTR0]], align 4
32+
; STOP1-NEXT: ret void
33+
;
34+
; STOP2-LABEL: define void @stop_bndl(
35+
; STOP2-SAME: ptr [[PTR:%.*]]) {
36+
; STOP2-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
37+
; STOP2-NEXT: [[PTR1:%.*]] = getelementptr float, ptr [[PTR]], i32 1
38+
; STOP2-NEXT: [[LD0:%.*]] = load float, ptr [[PTR0]], align 4
39+
; STOP2-NEXT: [[LD1:%.*]] = load float, ptr [[PTR1]], align 4
40+
; STOP2-NEXT: [[PACK:%.*]] = insertelement <2 x float> poison, float [[LD0]], i32 0
41+
; STOP2-NEXT: [[PACK1:%.*]] = insertelement <2 x float> [[PACK]], float [[LD1]], i32 1
42+
; STOP2-NEXT: [[VEC:%.*]] = fadd <2 x float> [[PACK1]], zeroinitializer
43+
; STOP2-NEXT: store <2 x float> [[VEC]], ptr [[PTR0]], align 4
44+
; STOP2-NEXT: ret void
45+
;
46+
; STOP3-LABEL: define void @stop_bndl(
47+
; STOP3-SAME: ptr [[PTR:%.*]]) {
48+
; STOP3-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
49+
; STOP3-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
50+
; STOP3-NEXT: [[VEC:%.*]] = fadd <2 x float> [[VECL]], zeroinitializer
51+
; STOP3-NEXT: store <2 x float> [[VEC]], ptr [[PTR0]], align 4
52+
; STOP3-NEXT: ret void
53+
;
54+
; NOSTOP-LABEL: define void @stop_bndl(
55+
; NOSTOP-SAME: ptr [[PTR:%.*]]) {
56+
; NOSTOP-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
57+
; NOSTOP-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
58+
; NOSTOP-NEXT: [[VEC:%.*]] = fadd <2 x float> [[VECL]], zeroinitializer
59+
; NOSTOP-NEXT: store <2 x float> [[VEC]], ptr [[PTR0]], align 4
60+
; NOSTOP-NEXT: ret void
61+
;
62+
%ptr0 = getelementptr float, ptr %ptr, i32 0
63+
%ptr1 = getelementptr float, ptr %ptr, i32 1
64+
%ld0 = load float, ptr %ptr0
65+
%ld1 = load float, ptr %ptr1
66+
%add0 = fadd float %ld0, 0.0
67+
%add1 = fadd float %ld1, 0.0
68+
store float %add0, ptr %ptr0
69+
store float %add1, ptr %ptr1
70+
ret void
71+
}

0 commit comments

Comments
 (0)