Skip to content

Commit b0ff473

Browse files
authored
[LLVM] Change ModulePass::skipModule to take a const reference (#146168)
Change `ModulePass::skipModule` to take const Module reference. Additionally, make `OptPassGate::shouldRunPass` const as well as for most implementations it's a const query. For `OptBisect`, make `LastBisectNum` mutable so it could be updated in `shouldRunPass`. Additional minor cleanup: Change all StringRef arguments to simple StringRef (no const or reference), change `OptBisect::Disabled` to constexpr.
1 parent 67e73ba commit b0ff473

File tree

8 files changed

+20
-21
lines changed

8 files changed

+20
-21
lines changed

llvm/include/llvm/IR/OptBisect.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class OptPassGate {
2828

2929
/// IRDescription is a textual description of the IR unit the pass is running
3030
/// over.
31-
virtual bool shouldRunPass(const StringRef PassName,
32-
StringRef IRDescription) {
31+
virtual bool shouldRunPass(StringRef PassName,
32+
StringRef IRDescription) const {
3333
return true;
3434
}
3535

@@ -62,8 +62,8 @@ class LLVM_ABI OptBisect : public OptPassGate {
6262
/// Most passes should not call this routine directly. Instead, it is called
6363
/// through helper routines provided by the base classes of the pass. For
6464
/// instance, function passes should call FunctionPass::skipFunction().
65-
bool shouldRunPass(const StringRef PassName,
66-
StringRef IRDescription) override;
65+
bool shouldRunPass(StringRef PassName,
66+
StringRef IRDescription) const override;
6767

6868
/// isEnabled() should return true before calling shouldRunPass().
6969
bool isEnabled() const override { return BisectLimit != Disabled; }
@@ -75,11 +75,11 @@ class LLVM_ABI OptBisect : public OptPassGate {
7575
LastBisectNum = 0;
7676
}
7777

78-
static const int Disabled = std::numeric_limits<int>::max();
78+
static constexpr int Disabled = std::numeric_limits<int>::max();
7979

8080
private:
8181
int BisectLimit = Disabled;
82-
int LastBisectNum = 0;
82+
mutable int LastBisectNum = 0;
8383
};
8484

8585
/// Singleton instance of the OptBisect class, so multiple pass managers don't

llvm/include/llvm/Pass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class LLVM_ABI ModulePass : public Pass {
271271
protected:
272272
/// Optional passes call this function to check whether the pass should be
273273
/// skipped. This is the case when optimization bisect is over the limit.
274-
bool skipModule(Module &M) const;
274+
bool skipModule(const Module &M) const;
275275
};
276276

277277
//===----------------------------------------------------------------------===//

llvm/lib/Analysis/LoopPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ bool LoopPass::skipLoop(const Loop *L) const {
373373
if (!F)
374374
return false;
375375
// Check the opt bisect limit.
376-
OptPassGate &Gate = F->getContext().getOptPassGate();
376+
const OptPassGate &Gate = F->getContext().getOptPassGate();
377377
if (Gate.isEnabled() &&
378378
!Gate.shouldRunPass(this->getPassName(), getDescription(*L)))
379379
return true;

llvm/lib/Analysis/RegionPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static std::string getDescription(const Region &R) {
282282

283283
bool RegionPass::skipRegion(Region &R) const {
284284
Function &F = *R.getEntry()->getParent();
285-
OptPassGate &Gate = F.getContext().getOptPassGate();
285+
const OptPassGate &Gate = F.getContext().getOptPassGate();
286286
if (Gate.isEnabled() &&
287287
!Gate.shouldRunPass(this->getPassName(), getDescription(R)))
288288
return true;

llvm/lib/IR/OptBisect.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ static cl::opt<bool> OptBisectVerbose(
3737
cl::desc("Show verbose output when opt-bisect-limit is set"), cl::Hidden,
3838
cl::init(true), cl::Optional);
3939

40-
static void printPassMessage(const StringRef &Name, int PassNum,
41-
StringRef TargetDesc, bool Running) {
40+
static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc,
41+
bool Running) {
4242
StringRef Status = Running ? "" : "NOT ";
43-
errs() << "BISECT: " << Status << "running pass "
44-
<< "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
43+
errs() << "BISECT: " << Status << "running pass (" << PassNum << ") " << Name
44+
<< " on " << TargetDesc << '\n';
4545
}
4646

47-
bool OptBisect::shouldRunPass(const StringRef PassName,
48-
StringRef IRDescription) {
47+
bool OptBisect::shouldRunPass(StringRef PassName,
48+
StringRef IRDescription) const {
4949
assert(isEnabled());
5050

5151
int CurBisectNum = ++LastBisectNum;
@@ -55,6 +55,4 @@ bool OptBisect::shouldRunPass(const StringRef PassName,
5555
return ShouldRun;
5656
}
5757

58-
const int OptBisect::Disabled;
59-
6058
OptPassGate &llvm::getGlobalPassGate() { return getOptBisector(); }

llvm/lib/IR/Pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ static std::string getDescription(const Module &M) {
6060
return "module (" + M.getName().str() + ")";
6161
}
6262

63-
bool ModulePass::skipModule(Module &M) const {
64-
OptPassGate &Gate = M.getContext().getOptPassGate();
63+
bool ModulePass::skipModule(const Module &M) const {
64+
const OptPassGate &Gate = M.getContext().getOptPassGate();
6565
return Gate.isEnabled() &&
6666
!Gate.shouldRunPass(this->getPassName(), getDescription(M));
6767
}

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ bool OptPassGateInstrumentation::shouldRun(StringRef PassName, Any IR) {
10741074

10751075
void OptPassGateInstrumentation::registerCallbacks(
10761076
PassInstrumentationCallbacks &PIC) {
1077-
OptPassGate &PassGate = Context.getOptPassGate();
1077+
const OptPassGate &PassGate = Context.getOptPassGate();
10781078
if (!PassGate.isEnabled())
10791079
return;
10801080

llvm/unittests/IR/LegacyPassManagerTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ namespace llvm {
359359
struct CustomOptPassGate : public OptPassGate {
360360
bool Skip;
361361
CustomOptPassGate(bool Skip) : Skip(Skip) { }
362-
bool shouldRunPass(const StringRef PassName, StringRef IRDescription) override {
362+
bool shouldRunPass(StringRef PassName,
363+
StringRef IRDescription) const override {
363364
return !Skip;
364365
}
365366
bool isEnabled() const override { return true; }

0 commit comments

Comments
 (0)