Skip to content

PassManager: add the -sil-pass-count-config-file for easier bisecting pass counts in large projects #70786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/SILOptimizer/PassManager/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ class SILPassManager {
static bool disablePassesForFunction(SILFunction *function);

private:
void parsePassCount(StringRef countsStr);

bool doPrintBefore(SILTransform *T, SILFunction *F);

bool doPrintAfter(SILTransform *T, SILFunction *F, bool PassChangedSIL);
Expand Down
59 changes: 48 additions & 11 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/ManagedStatic.h"

#include <fstream>

using namespace swift;

llvm::cl::opt<bool> SILPrintAll(
Expand All @@ -67,6 +69,20 @@ llvm::cl::opt<std::string> SILNumOptPassesToRun(
"sil-opt-pass-count", llvm::cl::init(""),
llvm::cl::desc("Stop optimizing after <N> passes or <N>.<M> passes/sub-passes"));

// Read pass counts for each module from a config file.
// Config file format:
// <module-name>:<pass-count>(.<sub-pass-count>)
//
// This is useful for bisecting passes in large projects:
// 1. create a config file from a full build log. E.g. with
// grep -e '-module-name' build.log | sed -e 's/.*-module-name \([^ ]*\) .*/\1:10000000/' | sort | uniq > config.txt
// 2. add the `-Xllvm -sil-pass-count-config-file config.txt` option to the project settings
// 3. bisect by modifying the counts in the config file
// 4. clean-rebuild after each bisecting step
llvm::cl::opt<std::string> SILPassCountConfigFile(
"sil-pass-count-config-file", llvm::cl::init(""),
llvm::cl::desc("Read optimization counts from file"));

llvm::cl::opt<unsigned> SILOptProfileRepeat(
"sil-opt-profile-repeat", llvm::cl::init(1),
llvm::cl::desc("repeat passes N times and report the run time"));
Expand Down Expand Up @@ -382,19 +398,25 @@ SILPassManager::SILPassManager(SILModule *M, bool isMandatory,
#include "swift/SILOptimizer/Analysis/Analysis.def"

if (!SILNumOptPassesToRun.empty()) {
StringRef countsStr = SILNumOptPassesToRun;
bool validFormat = true;
if (countsStr.consumeInteger(10, maxNumPassesToRun))
validFormat = false;
if (countsStr.startswith(".")) {
countsStr = countsStr.drop_front(1);
if (countsStr.consumeInteger(10, maxNumSubpassesToRun))
validFormat = false;
}
if (!validFormat || !countsStr.empty()) {
llvm::errs() << "error: wrong format of -sil-opt-pass-count option\n";
parsePassCount(SILNumOptPassesToRun);
} else if (!SILPassCountConfigFile.empty()) {
StringRef moduleName = M->getSwiftModule()->getName().str();
std::fstream fs(SILPassCountConfigFile);
if (!fs) {
llvm::errs() << "cannot open pass count config file\n";
exit(1);
}
std::string line;
while (std::getline(fs, line)) {
auto pair = StringRef(line).split(":");
StringRef modName = pair.first;
StringRef countsStr = pair.second;
if (modName == moduleName) {
parsePassCount(countsStr);
break;
}
}
fs.close();
}

for (SILAnalysis *A : Analyses) {
Expand All @@ -407,6 +429,21 @@ SILPassManager::SILPassManager(SILModule *M, bool isMandatory,
M->registerDeserializationNotificationHandler(std::move(handler));
}

void SILPassManager::parsePassCount(StringRef countsStr) {
bool validFormat = true;
if (countsStr.consumeInteger(10, maxNumPassesToRun))
validFormat = false;
if (countsStr.startswith(".")) {
countsStr = countsStr.drop_front(1);
if (countsStr.consumeInteger(10, maxNumSubpassesToRun))
validFormat = false;
}
if (!validFormat || !countsStr.empty()) {
llvm::errs() << "error: wrong format of -sil-opt-pass-count option\n";
exit(1);
}
}

bool SILPassManager::continueTransforming() {
if (isMandatory)
return true;
Expand Down