Skip to content

Evaluator: Improve -debug-cycles output #35849

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
Jun 25, 2021
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: 0 additions & 2 deletions docs/RequestEvaluator.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ All existing requests inherit the [`SimpleRequest`](https://github.com/apple/swi

Each request can issue other requests, also through the evaluator. The evaluator automatically tracks such dependencies (by keeping a stack of the currently-active request evaluations). This information is valuable for a few reasons. First, it can help with debugging both correctness and performance, allowing one to visualize the dependencies evaluated when compiling a program. The current protocol has both full-graph visualization (via GraphViz output) and dependencies-for-a-single-request visualization (via a tree-like dump). Second, it ensures that we can detect cyclic dependencies (e.g., a cyclic inheritance hierarchy) correctly, because they show up as cycles in the dependency graph, allowing for proper diagnostics (for the user) and recovery (in the compiler). Third, it can eventually be leveraged to enable better incremental compilation--providing the ability to discover what information affected a particular type-checking decision, and propagate the effects of a specific change through the dependency graph.

The complete dependency graph formed by processing a source file can be visualized by passing the frontend option `-output-request-graphviz <filename>`. The dependency graph will be emitted using the [GraphViz](https://www.graphviz.org) format.

The frontend option `-debug-cycles` will provide debugging output for any cycle detected while processing the given source files. For example, running the [`circular_inheritance.swift` test](https://github.com/apple/swift/blob/main/test/decl/class/circular_inheritance.swift) from the Swift repository using this flag, e.g.,

```
Expand Down
34 changes: 30 additions & 4 deletions lib/AST/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,37 @@ bool Evaluator::checkDependency(const ActiveRequest &request) {

void Evaluator::diagnoseCycle(const ActiveRequest &request) {
if (debugDumpCycles) {
llvm::errs() << "===CYCLE DETECTED===\n";
for (auto &req : activeRequests) {
simple_display(llvm::errs(), req);
llvm::errs() << "\n";
const auto printIndent = [](llvm::raw_ostream &OS, unsigned indent) {
OS.indent(indent);
OS << "`--";
};

unsigned indent = 1;
auto &OS = llvm::errs();

OS << "===CYCLE DETECTED===\n";
for (const auto &step : activeRequests) {
printIndent(OS, indent);
if (step == request) {
OS.changeColor(llvm::raw_ostream::GREEN);
simple_display(OS, step);
OS.resetColor();
} else {
simple_display(OS, step);
}
OS << "\n";
indent += 4;
}

printIndent(OS, indent);
OS.changeColor(llvm::raw_ostream::GREEN);
simple_display(OS, request);

OS.changeColor(llvm::raw_ostream::RED);
OS << " (cyclic dependency)";
OS.resetColor();

OS << "\n";
}

request.diagnoseCycle(diags);
Expand Down
12 changes: 12 additions & 0 deletions test/Frontend/debug-cycles.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

// RUN: not %target-swift-frontend -typecheck -debug-cycles %s 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace --color=false

class Outer2: Outer2.Inner {
class Inner {}
}
// CHECK:===CYCLE DETECTED===
// CHECK-NEXT: `--TypeCheckSourceFileRequest({{.*}})
// CHECK-NEXT: `--SuperclassDeclRequest({{.*}})
// CHECK-NEXT: `--InheritedDeclsReferencedRequest({{.*}})
// CHECK-NEXT: `--QualifiedLookupRequest({{.*}})
// CHECK-NEXT: `--SuperclassDeclRequest({{.*}}) (cyclic dependency)