Skip to content

Commit 61453a0

Browse files
committed
[SandboxVec] Add BottomUpVec test flag to build regions from metadata.
This allows us to write lit tests for region passes where regions are specified by metadata in textual IR. See added test file for an example.
1 parent c04b640 commit 61453a0

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
2+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
3+
4+
#include "llvm/SandboxIR/Pass.h"
5+
6+
namespace llvm::sandboxir {
7+
8+
class Region;
9+
10+
/// A Region pass that prints the instruction count for the region to stdout.
11+
/// Used to test -sbvec-passes while we don't have any actual optimization
12+
/// passes.
13+
class PrintInstructionCountPass final : public RegionPass {
14+
public:
15+
PrintInstructionCountPass() : RegionPass("null") {}
16+
bool runOnRegion(Region &R) final {
17+
outs() << "InstructionCount: " << std::distance(R.begin(), R.end()) << "\n";
18+
return false;
19+
}
20+
};
21+
22+
} // namespace llvm::sandboxir
23+
24+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@
1010
#include "llvm/ADT/SmallVector.h"
1111
#include "llvm/SandboxIR/Function.h"
1212
#include "llvm/SandboxIR/Instruction.h"
13+
#include "llvm/SandboxIR/Region.h"
1314
#include "llvm/Support/CommandLine.h"
1415
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
16+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h"
1517

1618
namespace llvm::sandboxir {
1719

1820
static cl::opt<bool>
1921
PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
2022
cl::desc("Prints the pass pipeline and returns."));
2123

24+
static cl::opt<bool> UseRegionsFromMetadata(
25+
"sbvec-use-regions-from-metadata", cl::init(false), cl::Hidden,
26+
cl::desc("Skips bottom-up vectorization, builds regions from metadata "
27+
"already present in the IR and runs the region pass pipeline."));
28+
2229
/// A magic string for the default pass pipeline.
2330
static const char *DefaultPipelineMagicStr = "*";
2431

@@ -86,6 +93,14 @@ bool BottomUpVec::runOnFunction(Function &F) {
8693
RPM.printPipeline(outs());
8794
return false;
8895
}
96+
if (UseRegionsFromMetadata) {
97+
SmallVector<std::unique_ptr<Region>> Regions =
98+
Region::createRegionsFromMD(F);
99+
for (auto &R : Regions) {
100+
RPM.runOnRegion(*R);
101+
}
102+
return false;
103+
}
89104

90105
Change = false;
91106
// TODO: Start from innermost BBs first

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
#endif
1919

2020
REGION_PASS("null", NullPass())
21+
REGION_PASS("print-instruction-count", PrintInstructionCountPass())
2122

2223
#undef REGION_PASS
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: opt -disable-output --passes=sandbox-vectorizer \
2+
; RUN: -sbvec-passes=print-instruction-count \
3+
; RUN: -sbvec-use-regions-from-metadata %s | FileCheck %s
4+
5+
define i8 @foo(i8 %v0, i8 %v1) {
6+
%t0 = add i8 %v0, 1, !sandboxvec !0
7+
%t1 = add i8 %t0, %v1, !sandboxvec !1
8+
%t2 = add i8 %t1, %v1, !sandboxvec !1
9+
ret i8 %t2
10+
}
11+
12+
!0 = distinct !{!"sandboxregion"}
13+
!1 = distinct !{!"sandboxregion"}
14+
15+
; CHECK: InstructionCount: 1
16+
; CHECK: InstructionCount: 2

0 commit comments

Comments
 (0)