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
}

0 commit comments

Comments
 (0)