Skip to content

Commit 2fb1f03

Browse files
authored
[SandboxVec] Add region-from-bbs helper pass (#130153)
RegionFromBBs is a helper Sandbox IR function pass that builds a region for each BB. This helps test region passes in isolation without relying on other parts of the vectorizer, which is especially useful for stress-testing.
1 parent 6f77f53 commit 2fb1f03

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

llvm/include/llvm/SandboxIR/Region.h

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class Region {
120120
void remove(Instruction *I);
121121
friend class Context; // The callbacks need to call add() and remove().
122122
friend class RegionInternalsAttorney; // For unit tests.
123+
friend class RegionsFromBBs; // For add().
123124

124125
/// Set \p I as the \p Idx'th element in the auxiliary vector.
125126
/// NOTE: This is for internal use, it does not set the metadata.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===- RegionsFromBBs.h -----------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// A SandboxIR function pass that builds one region per BB and then runs a
10+
// pipeline of region passes on them. This is useful to test region passes in
11+
// isolation without relying on the output of other vectorizer components.
12+
//
13+
14+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_REGIONSFROMBBS_H
15+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_REGIONSFROMBBS_H
16+
17+
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/SandboxIR/Pass.h"
19+
#include "llvm/SandboxIR/PassManager.h"
20+
21+
namespace llvm::sandboxir {
22+
23+
class RegionsFromBBs final : public FunctionPass {
24+
// The PM containing the pipeline of region passes.
25+
RegionPassManager RPM;
26+
27+
public:
28+
RegionsFromBBs(StringRef Pipeline);
29+
bool runOnFunction(Function &F, const Analyses &A) final;
30+
void printPipeline(raw_ostream &OS) const final {
31+
OS << getName() << "\n";
32+
RPM.printPipeline(OS);
33+
}
34+
};
35+
36+
} // namespace llvm::sandboxir
37+
38+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_REGIONSFROMBBS_H

llvm/lib/Transforms/Vectorize/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_llvm_component_library(LLVMVectorize
88
SandboxVectorizer/Interval.cpp
99
SandboxVectorizer/Legality.cpp
1010
SandboxVectorizer/Passes/BottomUpVec.cpp
11+
SandboxVectorizer/Passes/RegionsFromBBs.cpp
1112
SandboxVectorizer/Passes/RegionsFromMetadata.cpp
1213
SandboxVectorizer/Passes/SeedCollection.cpp
1314
SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp

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

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ REGION_PASS("bottom-up-vec", ::llvm::sandboxir::BottomUpVec)
3131
#endif
3232

3333
FUNCTION_PASS_WITH_PARAMS("seed-collection", ::llvm::sandboxir::SeedCollection)
34+
FUNCTION_PASS_WITH_PARAMS("regions-from-bbs", ::llvm::sandboxir::RegionsFromBBs)
3435
FUNCTION_PASS_WITH_PARAMS("regions-from-metadata", ::llvm::sandboxir::RegionsFromMetadata)
3536

3637
#undef FUNCTION_PASS_WITH_PARAMS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- RegionsFromBBs.cpp - A helper to test RegionPasses -----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/RegionsFromBBs.h"
10+
#include "llvm/SandboxIR/Function.h"
11+
#include "llvm/SandboxIR/Region.h"
12+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
13+
14+
namespace llvm::sandboxir {
15+
16+
RegionsFromBBs::RegionsFromBBs(StringRef Pipeline)
17+
: FunctionPass("regions-from-bbs"),
18+
RPM("rpm", Pipeline, SandboxVectorizerPassBuilder::createRegionPass) {}
19+
20+
bool RegionsFromBBs::runOnFunction(Function &F, const Analyses &A) {
21+
SmallVector<std::unique_ptr<Region>, 16> Regions;
22+
// Create a region for each BB.
23+
for (BasicBlock &BB : F) {
24+
Regions.push_back(std::make_unique<Region>(F.getContext(), A.getTTI()));
25+
auto &RgnPtr = Regions.back();
26+
for (Instruction &I : BB)
27+
RgnPtr->add(&I);
28+
}
29+
// For each region run the region pass pipeline.
30+
for (auto &RgnPtr : Regions)
31+
RPM.runOnRegion(*RgnPtr, A);
32+
return false;
33+
}
34+
35+
} // namespace llvm::sandboxir

llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h"
44
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
55
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCount.h"
6+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/RegionsFromBBs.h"
67
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/RegionsFromMetadata.h"
78
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/SeedCollection.h"
89
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt --passes=sandbox-vectorizer -sbvec-passes='regions-from-bbs<null>' %s -S | FileCheck %s
3+
4+
define void @foo(i8 %v) {
5+
; CHECK-LABEL: define void @foo(
6+
; CHECK-SAME: i8 [[V:%.*]]) {
7+
; CHECK-NEXT: [[BBA:.*:]]
8+
; CHECK-NEXT: [[ADD0:%.*]] = add i8 [[V]], 0, !sandboxvec [[META0:![0-9]+]]
9+
; CHECK-NEXT: br label %[[BBB:.*]], !sandboxvec [[META0]]
10+
; CHECK: [[BBB]]:
11+
; CHECK-NEXT: [[ADD1:%.*]] = add i8 [[V]], 1, !sandboxvec [[META1:![0-9]+]]
12+
; CHECK-NEXT: br label %[[BBC:.*]], !sandboxvec [[META1]]
13+
; CHECK: [[BBC]]:
14+
; CHECK-NEXT: ret void, !sandboxvec [[META2:![0-9]+]]
15+
;
16+
bbA:
17+
%add0 = add i8 %v, 0
18+
br label %bbB
19+
20+
bbB:
21+
%add1 = add i8 %v, 1
22+
br label %bbC
23+
24+
bbC:
25+
ret void
26+
}
27+
;.
28+
; CHECK: [[META0]] = distinct !{!"sandboxregion"}
29+
; CHECK: [[META1]] = distinct !{!"sandboxregion"}
30+
; CHECK: [[META2]] = distinct !{!"sandboxregion"}
31+
;.

0 commit comments

Comments
 (0)