[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.
This commit is contained in:
Rahul Joshi
2025-06-30 07:23:04 -07:00
committed by GitHub
parent 67e73ba605
commit b0ff473340
8 changed files with 20 additions and 21 deletions

View File

@@ -28,8 +28,8 @@ public:
/// IRDescription is a textual description of the IR unit the pass is running
/// over.
virtual bool shouldRunPass(const StringRef PassName,
StringRef IRDescription) {
virtual bool shouldRunPass(StringRef PassName,
StringRef IRDescription) const {
return true;
}
@@ -62,8 +62,8 @@ public:
/// Most passes should not call this routine directly. Instead, it is called
/// through helper routines provided by the base classes of the pass. For
/// instance, function passes should call FunctionPass::skipFunction().
bool shouldRunPass(const StringRef PassName,
StringRef IRDescription) override;
bool shouldRunPass(StringRef PassName,
StringRef IRDescription) const override;
/// isEnabled() should return true before calling shouldRunPass().
bool isEnabled() const override { return BisectLimit != Disabled; }
@@ -75,11 +75,11 @@ public:
LastBisectNum = 0;
}
static const int Disabled = std::numeric_limits<int>::max();
static constexpr int Disabled = std::numeric_limits<int>::max();
private:
int BisectLimit = Disabled;
int LastBisectNum = 0;
mutable int LastBisectNum = 0;
};
/// Singleton instance of the OptBisect class, so multiple pass managers don't

View File

@@ -271,7 +271,7 @@ public:
protected:
/// Optional passes call this function to check whether the pass should be
/// skipped. This is the case when optimization bisect is over the limit.
bool skipModule(Module &M) const;
bool skipModule(const Module &M) const;
};
//===----------------------------------------------------------------------===//

View File

@@ -373,7 +373,7 @@ bool LoopPass::skipLoop(const Loop *L) const {
if (!F)
return false;
// Check the opt bisect limit.
OptPassGate &Gate = F->getContext().getOptPassGate();
const OptPassGate &Gate = F->getContext().getOptPassGate();
if (Gate.isEnabled() &&
!Gate.shouldRunPass(this->getPassName(), getDescription(*L)))
return true;

View File

@@ -282,7 +282,7 @@ static std::string getDescription(const Region &R) {
bool RegionPass::skipRegion(Region &R) const {
Function &F = *R.getEntry()->getParent();
OptPassGate &Gate = F.getContext().getOptPassGate();
const OptPassGate &Gate = F.getContext().getOptPassGate();
if (Gate.isEnabled() &&
!Gate.shouldRunPass(this->getPassName(), getDescription(R)))
return true;

View File

@@ -37,15 +37,15 @@ static cl::opt<bool> OptBisectVerbose(
cl::desc("Show verbose output when opt-bisect-limit is set"), cl::Hidden,
cl::init(true), cl::Optional);
static void printPassMessage(const StringRef &Name, int PassNum,
StringRef TargetDesc, bool Running) {
static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc,
bool Running) {
StringRef Status = Running ? "" : "NOT ";
errs() << "BISECT: " << Status << "running pass "
<< "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
errs() << "BISECT: " << Status << "running pass (" << PassNum << ") " << Name
<< " on " << TargetDesc << '\n';
}
bool OptBisect::shouldRunPass(const StringRef PassName,
StringRef IRDescription) {
bool OptBisect::shouldRunPass(StringRef PassName,
StringRef IRDescription) const {
assert(isEnabled());
int CurBisectNum = ++LastBisectNum;
@@ -55,6 +55,4 @@ bool OptBisect::shouldRunPass(const StringRef PassName,
return ShouldRun;
}
const int OptBisect::Disabled;
OptPassGate &llvm::getGlobalPassGate() { return getOptBisector(); }

View File

@@ -60,8 +60,8 @@ static std::string getDescription(const Module &M) {
return "module (" + M.getName().str() + ")";
}
bool ModulePass::skipModule(Module &M) const {
OptPassGate &Gate = M.getContext().getOptPassGate();
bool ModulePass::skipModule(const Module &M) const {
const OptPassGate &Gate = M.getContext().getOptPassGate();
return Gate.isEnabled() &&
!Gate.shouldRunPass(this->getPassName(), getDescription(M));
}

View File

@@ -1074,7 +1074,7 @@ bool OptPassGateInstrumentation::shouldRun(StringRef PassName, Any IR) {
void OptPassGateInstrumentation::registerCallbacks(
PassInstrumentationCallbacks &PIC) {
OptPassGate &PassGate = Context.getOptPassGate();
const OptPassGate &PassGate = Context.getOptPassGate();
if (!PassGate.isEnabled())
return;

View File

@@ -359,7 +359,8 @@ namespace llvm {
struct CustomOptPassGate : public OptPassGate {
bool Skip;
CustomOptPassGate(bool Skip) : Skip(Skip) { }
bool shouldRunPass(const StringRef PassName, StringRef IRDescription) override {
bool shouldRunPass(StringRef PassName,
StringRef IRDescription) const override {
return !Skip;
}
bool isEnabled() const override { return true; }