Skip to content

Add forced precomputation and verification of analysis #31274

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
Apr 24, 2020
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
11 changes: 11 additions & 0 deletions include/swift/SILOptimizer/Analysis/Analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class SILAnalysis : public DeleteNotificationHandler {
/// specific verification will do so.
virtual void verify(SILFunction *F) const { verify(); }

virtual void forcePrecompute(SILFunction *F) {}

/// Perform a potentially more expensive verification of the state of this
/// analysis.
///
Expand Down Expand Up @@ -234,6 +236,15 @@ class FunctionAnalysisBase : public SILAnalysis {
return it.second.get();
}

virtual void forcePrecompute(SILFunction *f) override {
// Check that the analysis can handle this function.
verifyFunction(f);

auto &it = storage.FindAndConstruct(f);
if (!it.second)
it.second = newFunctionAnalysis(f);
}

/// Invalidate all information in this analysis.
virtual void invalidate() override {
deleteAllAnalysisProviders();
Expand Down
7 changes: 7 additions & 0 deletions include/swift/SILOptimizer/PassManager/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ class SILPassManager {
}
}

/// Precompute all analyses.
void forcePrecomputeAnalyses(SILFunction *F) {
for (auto *A : Analyses) {
A->forcePrecompute(F);
}
}

/// Verify all analyses, limiting the verification to just this one function
/// if possible.
///
Expand Down
22 changes: 22 additions & 0 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ llvm::cl::list<std::string>
llvm::cl::desc("Verify the module/analyses after we run "
"a pass from this list"));

llvm::cl::list<std::string> SILForceVerifyAroundPass(
"sil-verify-force-analysis-around-pass",
llvm::cl::desc("For the given passes, precompute analyses before the pass "
"and verify analyses after the pass"));

llvm::cl::opt<bool> SILVerifyWithoutInvalidation(
"sil-verify-without-invalidation", llvm::cl::init(false),
llvm::cl::desc("Verify after passes even if the pass has not invalidated"));
Expand All @@ -111,6 +116,11 @@ llvm::cl::opt<bool> SILDisableSkippingPasses(
"sil-disable-skipping-passes", llvm::cl::init(false),
llvm::cl::desc("Do not skip passes even if nothing was changed"));

llvm::cl::opt<bool> SILForceVerifyAll(
"sil-verify-force-analysis", llvm::cl::init(false),
llvm::cl::desc("For all passes, precompute analyses before the pass and "
"verify analyses after the pass"));

static llvm::ManagedStatic<std::vector<unsigned>> DebugPassNumbers;

namespace {
Expand Down Expand Up @@ -421,7 +431,19 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
Mod->registerDeleteNotificationHandler(SFT);
if (breakBeforeRunning(F->getName(), SFT))
LLVM_BUILTIN_DEBUGTRAP;
if (SILForceVerifyAll ||
SILForceVerifyAroundPass.end() !=
std::find_if(SILForceVerifyAroundPass.begin(),
SILForceVerifyAroundPass.end(), MatchFun)) {
forcePrecomputeAnalyses(F);
}
SFT->run();
if (SILForceVerifyAll ||
SILForceVerifyAroundPass.end() !=
std::find_if(SILForceVerifyAroundPass.begin(),
SILForceVerifyAroundPass.end(), MatchFun)) {
verifyAnalyses(F);
}
assert(analysesUnlocked() && "Expected all analyses to be unlocked!");
Mod->removeDeleteNotificationHandler(SFT);

Expand Down
1 change: 1 addition & 0 deletions test/SILOptimizer/sil_combine_concrete_existential.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend -O -emit-sil -sil-verify-all -Xllvm -sil-disable-pass=function-signature-opts %s | %FileCheck %s
// %target-swift-frontend -O -emit-sil -Xllvm -sil-verify-force-analysis-around-pass=devirtualizer -Xllvm -sil-disable-pass=function-signature-opts %s | %FileCheck %s

//===----------------------------------------------------------------------===//
// testReturnSelf: Call to a protocol extension method with
Expand Down