Skip to content

Commit ce29c64

Browse files
committed
[Evaluator] Refactor evaluator's GraphViz output to the destructor of the evaluator. NFC
This allows us to output dependency-graph while using the compiler in other tools, like swift-ide-test, sourcekitd-test, etc.
1 parent 6ffe644 commit ce29c64

File tree

8 files changed

+23
-25
lines changed

8 files changed

+23
-25
lines changed

include/swift/AST/Evaluator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class Evaluator {
238238
/// diagnostics through the given diagnostics engine.
239239
Evaluator(DiagnosticEngine &diags, CycleDiagnosticKind shouldDiagnoseCycles);
240240

241+
/// Emit GraphViz output visualizing the request graph.
242+
void emitRequestEvaluatorGraphViz(llvm::StringRef graphVizPath);
243+
241244
/// Set the unified stats reporter through which evaluated-request
242245
/// statistics will be recorded.
243246
void setStatsReporter(UnifiedStatsReporter *stats) { this->stats = stats; }

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ namespace swift {
176176
CycleDiagnosticKind EvaluatorCycleDiagnostics =
177177
CycleDiagnosticKind::NoDiagnose;
178178

179+
/// \brief The path to which we should emit GraphViz output for the complete
180+
/// request-evaluator graph.
181+
std::string RequestEvaluatorGraphVizPath;
182+
179183
/// \brief The upper bound, in bytes, of temporary data that can be
180184
/// allocated by the constraint solver.
181185
unsigned SolverMemoryThreshold = 512 * 1024 * 1024;

include/swift/Frontend/FrontendOptions.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ class FrontendOptions {
7272
/// The path to which we should store indexing data, if any.
7373
std::string IndexStorePath;
7474

75-
/// The path to which we should emit GraphViz output for the complete
76-
/// request-evaluator graph.
77-
std::string RequestEvaluatorGraphVizPath;
78-
7975
/// Emit index data for imported serialized swift system modules.
8076
bool IndexSystemModules = false;
8177

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,11 @@ ASTContext::ASTContext(LangOptions &langOpts, SearchPathOptions &SearchPathOpts,
518518
}
519519

520520
ASTContext::~ASTContext() {
521+
// Emit evaluator dependency graph if requested.
522+
auto graphPath = LangOpts.RequestEvaluatorGraphVizPath;
523+
if (!graphPath.empty()) {
524+
evaluator.emitRequestEvaluatorGraphViz(graphPath);
525+
}
521526
getImpl().~Implementation();
522527
}
523528

lib/AST/Evaluator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Basic/Range.h"
1919
#include "llvm/ADT/StringExtras.h"
2020
#include "llvm/Support/Debug.h"
21+
#include "llvm/Support/FileSystem.h"
2122

2223
using namespace swift;
2324

@@ -62,6 +63,12 @@ Evaluator::Evaluator(DiagnosticEngine &diags,
6263
CycleDiagnosticKind shouldDiagnoseCycles)
6364
: diags(diags), shouldDiagnoseCycles(shouldDiagnoseCycles) { }
6465

66+
void Evaluator::emitRequestEvaluatorGraphViz(llvm::StringRef graphVizPath) {
67+
std::error_code error;
68+
llvm::raw_fd_ostream out(graphVizPath, error, llvm::sys::fs::F_Text);
69+
printDependenciesGraphviz(out);
70+
}
71+
6572
bool Evaluator::checkDependency(const AnyRequest &request) {
6673
// If there is an active request, record it's dependency on this request.
6774
if (!activeRequests.empty())

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ bool ArgsToFrontendOptionsConverter::convert(
6060
if (const Arg *A = Args.getLastArg(OPT_index_store_path)) {
6161
Opts.IndexStorePath = A->getValue();
6262
}
63-
if (const Arg *A = Args.getLastArg(OPT_output_request_graphviz)) {
64-
Opts.RequestEvaluatorGraphVizPath = A->getValue();
65-
}
6663

6764
Opts.IndexSystemModules |= Args.hasArg(OPT_index_system_modules);
6865

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
259259
Opts.EvaluatorCycleDiagnostics = CycleDiagnosticKind::DebugDiagnose;
260260
}
261261

262+
if (const Arg *A = Args.getLastArg(OPT_output_request_graphviz)) {
263+
Opts.RequestEvaluatorGraphVizPath = A->getValue();
264+
}
265+
262266
if (const Arg *A = Args.getLastArg(OPT_solver_memory_threshold)) {
263267
unsigned threshold;
264268
if (StringRef(A->getValue()).getAsInteger(10, threshold)) {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,6 @@ emitIndexData(CompilerInvocation &Invocation, CompilerInstance &Instance) {
868868
return hadEmitIndexDataError;
869869
}
870870

871-
/// Emits the request-evaluator graph to the given file in GraphViz format.
872-
void emitRequestEvaluatorGraphViz(ASTContext &ctx, StringRef graphVizPath) {
873-
std::error_code error;
874-
llvm::raw_fd_ostream out(graphVizPath, error, llvm::sys::fs::F_Text);
875-
ctx.evaluator.printDependenciesGraphviz(out);
876-
}
877-
878871
static bool performCompileStepsPostSILGen(
879872
CompilerInstance &Instance, CompilerInvocation &Invocation,
880873
std::unique_ptr<SILModule> SM, bool astGuaranteedToCorrespondToSIL,
@@ -917,17 +910,6 @@ static bool performCompile(CompilerInstance &Instance,
917910
Instance.performSema();
918911
}
919912

920-
SWIFT_DEFER {
921-
// Emit request-evaluator graph via GraphViz, if requested.
922-
if (!Invocation.getFrontendOptions().RequestEvaluatorGraphVizPath.empty() &&
923-
Instance.hasASTContext()) {
924-
ASTContext &ctx = Instance.getASTContext();
925-
emitRequestEvaluatorGraphViz(
926-
ctx,
927-
Invocation.getFrontendOptions().RequestEvaluatorGraphVizPath);
928-
}
929-
};
930-
931913
ASTContext &Context = Instance.getASTContext();
932914
if (Action == FrontendOptions::ActionType::Parse)
933915
return Context.hadError();

0 commit comments

Comments
 (0)