Skip to content

Commit a461869

Browse files
authored
[SandboxIR][Pass] Implement Analyses class (#113962)
The Analyses class provides a way to pass around commonly used Analyses to SandboxIR passes throught `runOnFunction()` and `runOnRegion()` functions.
1 parent 1ceccbb commit a461869

File tree

12 files changed

+56
-33
lines changed

12 files changed

+56
-33
lines changed

llvm/include/llvm/SandboxIR/Pass.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@
1212
#include "llvm/Support/ErrorHandling.h"
1313
#include "llvm/Support/raw_ostream.h"
1414

15-
namespace llvm::sandboxir {
15+
namespace llvm {
16+
17+
class ScalarEvolution;
18+
19+
namespace sandboxir {
1620

1721
class Function;
1822
class Region;
1923

24+
class Analyses {
25+
ScalarEvolution *SE = nullptr;
26+
27+
Analyses() = default;
28+
29+
public:
30+
Analyses(ScalarEvolution &SE) : SE(&SE) {}
31+
32+
public:
33+
ScalarEvolution &getScalarEvolution() const { return *SE; }
34+
/// For use by unit tests.
35+
static Analyses emptyForTesting() { return Analyses(); }
36+
};
37+
2038
/// The base class of a Sandbox IR Pass.
2139
class Pass {
2240
protected:
@@ -52,7 +70,7 @@ class FunctionPass : public Pass {
5270
/// \p Name can't contain any spaces or start with '-'.
5371
FunctionPass(StringRef Name) : Pass(Name) {}
5472
/// \Returns true if it modifies \p F.
55-
virtual bool runOnFunction(Function &F) = 0;
73+
virtual bool runOnFunction(Function &F, const Analyses &A) = 0;
5674
};
5775

5876
/// A pass that runs on a sandbox::Region.
@@ -61,9 +79,10 @@ class RegionPass : public Pass {
6179
/// \p Name can't contain any spaces or start with '-'.
6280
RegionPass(StringRef Name) : Pass(Name) {}
6381
/// \Returns true if it modifies \p R.
64-
virtual bool runOnRegion(Region &R) = 0;
82+
virtual bool runOnRegion(Region &R, const Analyses &A) = 0;
6583
};
6684

67-
} // namespace llvm::sandboxir
85+
} // namespace sandboxir
86+
} // namespace llvm
6887

6988
#endif // LLVM_SANDBOXIR_PASS_H

llvm/include/llvm/SandboxIR/PassManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class FunctionPassManager final
208208
FunctionPassManager(StringRef Name, StringRef Pipeline,
209209
CreatePassFunc CreatePass)
210210
: PassManager(Name, Pipeline, CreatePass) {}
211-
bool runOnFunction(Function &F) final;
211+
bool runOnFunction(Function &F, const Analyses &A) final;
212212
};
213213

214214
class RegionPassManager final : public PassManager<RegionPass, RegionPass> {
@@ -217,7 +217,7 @@ class RegionPassManager final : public PassManager<RegionPass, RegionPass> {
217217
RegionPassManager(StringRef Name, StringRef Pipeline,
218218
CreatePassFunc CreatePass)
219219
: PassManager(Name, Pipeline, CreatePass) {}
220-
bool runOnRegion(Region &R) final;
220+
bool runOnRegion(Region &R, const Analyses &A) final;
221221
};
222222

223223
} // namespace llvm::sandboxir

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class BottomUpVec final : public FunctionPass {
3333

3434
public:
3535
BottomUpVec(StringRef Pipeline);
36-
bool runOnFunction(Function &F) final;
36+
bool runOnFunction(Function &F, const Analyses &A) final;
3737
void printPipeline(raw_ostream &OS) const final {
3838
OS << getName() << "\n";
3939
RPM.printPipeline(OS);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Region;
1111
class NullPass final : public RegionPass {
1212
public:
1313
NullPass() : RegionPass("null") {}
14-
bool runOnRegion(Region &R) final { return false; }
14+
bool runOnRegion(Region &R, const Analyses &A) final { return false; }
1515
};
1616

1717
} // namespace llvm::sandboxir

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace llvm::sandboxir {
1212
class PrintInstructionCount final : public RegionPass {
1313
public:
1414
PrintInstructionCount() : RegionPass("null") {}
15-
bool runOnRegion(Region &R) final {
15+
bool runOnRegion(Region &R, const Analyses &A) final {
1616
outs() << "InstructionCount: " << std::distance(R.begin(), R.end()) << "\n";
1717
return false;
1818
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RegionsFromMetadata final : public FunctionPass {
2626

2727
public:
2828
RegionsFromMetadata(StringRef Pipeline);
29-
bool runOnFunction(Function &F) final;
29+
bool runOnFunction(Function &F, const Analyses &A) final;
3030
void printPipeline(raw_ostream &OS) const final {
3131
OS << getName() << "\n";
3232
RPM.printPipeline(OS);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <memory>
1212

13+
#include "llvm/Analysis/ScalarEvolution.h"
1314
#include "llvm/IR/PassManager.h"
1415
#include "llvm/SandboxIR/PassManager.h"
1516

@@ -19,6 +20,7 @@ class TargetTransformInfo;
1920

2021
class SandboxVectorizerPass : public PassInfoMixin<SandboxVectorizerPass> {
2122
TargetTransformInfo *TTI = nullptr;
23+
ScalarEvolution *SE = nullptr;
2224

2325
// A pipeline of SandboxIR function passes run by the vectorizer.
2426
sandboxir::FunctionPassManager FPM;

llvm/lib/SandboxIR/PassManager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
namespace llvm::sandboxir {
1212

13-
bool FunctionPassManager::runOnFunction(Function &F) {
13+
bool FunctionPassManager::runOnFunction(Function &F, const Analyses &A) {
1414
bool Change = false;
1515
for (auto &Pass : Passes) {
16-
Change |= Pass->runOnFunction(F);
16+
Change |= Pass->runOnFunction(F, A);
1717
// TODO: run the verifier.
1818
}
1919
// TODO: Check ChangeAll against hashes before/after.
2020
return Change;
2121
}
2222

23-
bool RegionPassManager::runOnRegion(Region &R) {
23+
bool RegionPassManager::runOnRegion(Region &R, const Analyses &A) {
2424
bool Change = false;
2525
for (auto &Pass : Passes) {
26-
Change |= Pass->runOnRegion(R);
26+
Change |= Pass->runOnRegion(R, A);
2727
// TODO: run the verifier.
2828
}
2929
// TODO: Check ChangeAll against hashes before/after.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl) {
5959

6060
void BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) { vectorizeRec(Bndl); }
6161

62-
bool BottomUpVec::runOnFunction(Function &F) {
62+
bool BottomUpVec::runOnFunction(Function &F, const Analyses &A) {
6363
Change = false;
6464
// TODO: Start from innermost BBs first
6565
for (auto &BB : F) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ RegionsFromMetadata::RegionsFromMetadata(StringRef Pipeline)
1717
: FunctionPass("regions-from-metadata"),
1818
RPM("rpm", Pipeline, SandboxVectorizerPassBuilder::createRegionPass) {}
1919

20-
bool RegionsFromMetadata::runOnFunction(Function &F) {
20+
bool RegionsFromMetadata::runOnFunction(Function &F, const Analyses &A) {
2121
SmallVector<std::unique_ptr<sandboxir::Region>> Regions =
2222
sandboxir::Region::createRegionsFromMD(F);
2323
for (auto &R : Regions) {
24-
RPM.runOnRegion(*R);
24+
RPM.runOnRegion(*R, A);
2525
}
2626
return false;
2727
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ SandboxVectorizerPass::~SandboxVectorizerPass() = default;
5151
PreservedAnalyses SandboxVectorizerPass::run(Function &F,
5252
FunctionAnalysisManager &AM) {
5353
TTI = &AM.getResult<TargetIRAnalysis>(F);
54+
SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
5455

5556
bool Changed = runImpl(F);
5657
if (!Changed)
@@ -82,5 +83,6 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
8283
// Create SandboxIR for LLVMF and run BottomUpVec on it.
8384
sandboxir::Context Ctx(LLVMF.getContext());
8485
sandboxir::Function &F = *Ctx.createFunction(&LLVMF);
85-
return FPM.runOnFunction(F);
86+
sandboxir::Analyses A(*SE);
87+
return FPM.runOnFunction(F, A);
8688
}

llvm/unittests/SandboxIR/PassTest.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ define void @foo() {
4646

4747
public:
4848
TestPass(unsigned &BBCnt) : FunctionPass("test-pass"), BBCnt(BBCnt) {}
49-
bool runOnFunction(Function &F) final {
49+
bool runOnFunction(Function &F, const Analyses &A) final {
5050
for ([[maybe_unused]] auto &BB : F)
5151
++BBCnt;
5252
return false;
@@ -59,7 +59,7 @@ define void @foo() {
5959
// Check classof().
6060
EXPECT_TRUE(llvm::isa<FunctionPass>(TPass));
6161
// Check runOnFunction();
62-
TPass.runOnFunction(*F);
62+
TPass.runOnFunction(*F, Analyses::emptyForTesting());
6363
EXPECT_EQ(BBCnt, 1u);
6464
#ifndef NDEBUG
6565
{
@@ -80,7 +80,7 @@ define void @foo() {
8080
class TestNamePass final : public FunctionPass {
8181
public:
8282
TestNamePass(llvm::StringRef Name) : FunctionPass(Name) {}
83-
bool runOnFunction(Function &F) { return false; }
83+
bool runOnFunction(Function &F, const Analyses &A) { return false; }
8484
};
8585
EXPECT_DEATH(TestNamePass("white space"), ".*whitespace.*");
8686
EXPECT_DEATH(TestNamePass("-dash"), ".*start with.*");
@@ -106,7 +106,7 @@ define i8 @foo(i8 %v0, i8 %v1) {
106106
public:
107107
TestPass(unsigned &InstCount)
108108
: RegionPass("test-pass"), InstCount(InstCount) {}
109-
bool runOnRegion(Region &R) final {
109+
bool runOnRegion(Region &R, const Analyses &A) final {
110110
for ([[maybe_unused]] auto &Inst : R) {
111111
++InstCount;
112112
}
@@ -121,7 +121,7 @@ define i8 @foo(i8 %v0, i8 %v1) {
121121
llvm::SmallVector<std::unique_ptr<Region>> Regions =
122122
Region::createRegionsFromMD(*F);
123123
ASSERT_EQ(Regions.size(), 1u);
124-
TPass.runOnRegion(*Regions[0]);
124+
TPass.runOnRegion(*Regions[0], Analyses::emptyForTesting());
125125
EXPECT_EQ(InstCount, 2u);
126126
#ifndef NDEBUG
127127
{
@@ -142,7 +142,7 @@ define i8 @foo(i8 %v0, i8 %v1) {
142142
class TestNamePass final : public RegionPass {
143143
public:
144144
TestNamePass(llvm::StringRef Name) : RegionPass(Name) {}
145-
bool runOnRegion(Region &F) { return false; }
145+
bool runOnRegion(Region &F, const Analyses &A) { return false; }
146146
};
147147
EXPECT_DEATH(TestNamePass("white space"), ".*whitespace.*");
148148
EXPECT_DEATH(TestNamePass("-dash"), ".*start with.*");
@@ -161,7 +161,7 @@ define void @foo() {
161161

162162
public:
163163
TestPass1(unsigned &BBCnt) : FunctionPass("test-pass1"), BBCnt(BBCnt) {}
164-
bool runOnFunction(Function &F) final {
164+
bool runOnFunction(Function &F, const Analyses &A) final {
165165
for ([[maybe_unused]] auto &BB : F)
166166
++BBCnt;
167167
return false;
@@ -172,7 +172,7 @@ define void @foo() {
172172

173173
public:
174174
TestPass2(unsigned &BBCnt) : FunctionPass("test-pass2"), BBCnt(BBCnt) {}
175-
bool runOnFunction(Function &F) final {
175+
bool runOnFunction(Function &F, const Analyses &A) final {
176176
for ([[maybe_unused]] auto &BB : F)
177177
++BBCnt;
178178
return false;
@@ -185,7 +185,7 @@ define void @foo() {
185185
FPM.addPass(std::make_unique<TestPass1>(BBCnt1));
186186
FPM.addPass(std::make_unique<TestPass2>(BBCnt2));
187187
// Check runOnFunction().
188-
FPM.runOnFunction(*F);
188+
FPM.runOnFunction(*F, Analyses::emptyForTesting());
189189
EXPECT_EQ(BBCnt1, 1u);
190190
EXPECT_EQ(BBCnt2, 1u);
191191
#ifndef NDEBUG
@@ -216,7 +216,7 @@ define i8 @foo(i8 %v0, i8 %v1) {
216216
public:
217217
TestPass1(unsigned &InstCount)
218218
: RegionPass("test-pass1"), InstCount(InstCount) {}
219-
bool runOnRegion(Region &R) final {
219+
bool runOnRegion(Region &R, const Analyses &A) final {
220220
for ([[maybe_unused]] auto &Inst : R)
221221
++InstCount;
222222
return false;
@@ -228,7 +228,7 @@ define i8 @foo(i8 %v0, i8 %v1) {
228228
public:
229229
TestPass2(unsigned &InstCount)
230230
: RegionPass("test-pass2"), InstCount(InstCount) {}
231-
bool runOnRegion(Region &R) final {
231+
bool runOnRegion(Region &R, const Analyses &A) final {
232232
for ([[maybe_unused]] auto &Inst : R)
233233
++InstCount;
234234
return false;
@@ -244,7 +244,7 @@ define i8 @foo(i8 %v0, i8 %v1) {
244244
llvm::SmallVector<std::unique_ptr<Region>> Regions =
245245
Region::createRegionsFromMD(*F);
246246
ASSERT_EQ(Regions.size(), 1u);
247-
RPM.runOnRegion(*Regions[0]);
247+
RPM.runOnRegion(*Regions[0], Analyses::emptyForTesting());
248248
EXPECT_EQ(InstCount1, 2u);
249249
EXPECT_EQ(InstCount2, 2u);
250250
#ifndef NDEBUG
@@ -270,7 +270,7 @@ define void @f() {
270270
public:
271271
FooPass(std::string &Str, llvm::StringRef Args)
272272
: FunctionPass("foo-pass"), Str(Str), Args(Args.str()) {}
273-
bool runOnFunction(Function &F) final {
273+
bool runOnFunction(Function &F, const Analyses &A) final {
274274
Str += "foo<" + Args + ">";
275275
return false;
276276
}
@@ -282,7 +282,7 @@ define void @f() {
282282
public:
283283
BarPass(std::string &Str, llvm::StringRef Args)
284284
: FunctionPass("bar-pass"), Str(Str), Args(Args.str()) {}
285-
bool runOnFunction(Function &F) final {
285+
bool runOnFunction(Function &F, const Analyses &A) final {
286286
Str += "bar<" + Args + ">";
287287
return false;
288288
}
@@ -302,7 +302,7 @@ define void @f() {
302302
FunctionPassManager FPM("test-fpm");
303303
FPM.setPassPipeline("foo<abc>,bar<nested1<nested2<nested3>>>,foo",
304304
CreatePass);
305-
FPM.runOnFunction(*F);
305+
FPM.runOnFunction(*F, Analyses::emptyForTesting());
306306
EXPECT_EQ(Str, "foo<abc>bar<nested1<nested2<nested3>>>foo<>");
307307

308308
// A second call to setPassPipeline will trigger an assertion in debug mode.

0 commit comments

Comments
 (0)