-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ModuleTrace] Emit import graph to ease debugging import issues. #33980
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
Draft
varungandhi-apple
wants to merge
3
commits into
swiftlang:main
Choose a base branch
from
varungandhi-apple:vg-module-trace-upgrade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1c644dd
[NFC] Make 'isOverlayOfClangModule' a static function.
varungandhi-apple 9799b1b
[AST] Fix latent bug in import filtering for ShadowedBySeparateOverlay.
varungandhi-apple d0ac0d4
[ModuleTrace] Emit import graph to ease debugging import issues.
varungandhi-apple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
//===--- GraphViz.h - Utilities for outputting graphs -----------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// Defines data structures for creating transient graphs in order to print | ||
/// them to GraphViz DOT format. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_BASIC_GRAPHVIZ_H | ||
#define SWIFT_BASIC_GRAPHVIZ_H | ||
|
||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
#include <iterator> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace swift { | ||
// MARK: - MultiGraph | ||
|
||
/// A multigraph data structure, with a set of edges connecting pairs of nodes. | ||
/// | ||
/// The fields are left public so that you can potentially customize printing | ||
/// directly, instead of relying on the existing customization points. | ||
/// | ||
/// \invariant { | ||
/// \code forall n1 n2. nodePairs[{n1, n2}] < edgeSets.size() \endcode | ||
/// } | ||
template <typename Node, typename Edge, typename EdgeSet> | ||
struct MultiGraph { | ||
/// List of nodes stored in the graph. | ||
llvm::DenseMap<std::pair<Node, Node>, size_t> nodePairs; | ||
|
||
/// List of edges stored in the graph. | ||
std::vector<EdgeSet> edgeSets; | ||
|
||
/// Three way comparison function for nodes used for deterministic output. | ||
llvm::function_ref<int (Node, Node)> compareNodes; | ||
|
||
/// Three way comparison function for edges used for deterministic output. | ||
llvm::function_ref<int (Edge, Edge)> compareEdges; | ||
|
||
/// Default attributes used for styling nodes. | ||
std::string defaultNodeAttrs = | ||
"shape = \"box\", style = \"rounded\", penwidth = \"2\""; | ||
|
||
/// Print the name of a node. | ||
llvm::function_ref<void (Node, llvm::raw_ostream &)> printNodeName; | ||
|
||
/// Print the attributes used to style a node, potentially null. | ||
llvm::function_ref<void (Node, llvm::raw_ostream &)> printNodeAttr; | ||
|
||
/// Print the label for the edge set between a pair of nodes, optionally | ||
/// customizing it based on the nodes themselves. | ||
llvm::function_ref< | ||
void (Node, Node, const SmallVectorImpl<Edge> &, | ||
llvm::raw_ostream &)> printEdgeSet; | ||
|
||
public: | ||
MultiGraph() = default; | ||
MultiGraph(const MultiGraph &) = delete; | ||
MultiGraph(MultiGraph &&) = default; | ||
|
||
/// Insert an edge to the set of edges between two nodes. | ||
void updateEdge(Node from, Node to, Edge edge) { | ||
auto it = nodePairs.find(std::pair<Node, Node>{from, to}); | ||
if (it != nodePairs.end()) { | ||
edgeSets[it->second].insert(edge); | ||
return; | ||
} | ||
nodePairs.insert({{from, to}, edgeSets.size()}); | ||
EdgeSet edgeSet{}; | ||
edgeSet.insert(edge); | ||
edgeSets.push_back(edgeSet); | ||
}; | ||
|
||
/// Output the graph in DOT format to \p os. | ||
void printAsGraphviz(llvm::raw_ostream &os, bool horizontal = false) { | ||
os << "digraph CustomGraph {\n"; | ||
os << " node [" << defaultNodeAttrs; os << "];\n"; | ||
if (horizontal) | ||
os << " rankdir = \"LR\";\n"; | ||
|
||
auto copyAndSort = [](const auto &set, auto &vec, auto cmp) { | ||
vec.reserve(set.size()); | ||
llvm::copy(set, std::back_inserter(vec)); | ||
llvm::sort(vec, [&](auto t1, auto t2) { return cmp(t1, t2) < 0;}); | ||
}; | ||
|
||
// Print the nodes first in case we have custom attributes for nodes. | ||
if (printNodeAttr) { | ||
llvm::DenseSet<Node> nodes{}; | ||
for (auto &entry: nodePairs) { | ||
nodes.insert(entry.first.first); | ||
nodes.insert(entry.first.second); | ||
} | ||
std::vector<Node> nodesVec{}; | ||
copyAndSort(nodes, nodesVec, compareNodes); | ||
for (auto node: nodes) { | ||
os << " "; | ||
printNodeName(node, os); | ||
printNodeAttr(node, os); | ||
os << ";\n"; | ||
} | ||
} | ||
std::vector<std::pair<std::pair<Node, Node>, size_t>> nodePairsVec{}; | ||
copyAndSort(nodePairs, nodePairsVec, | ||
[this](auto e1, auto e2) -> int { | ||
auto nodePair1 = e1.first; | ||
auto nodePair2 = e1.first; | ||
auto cmp1 = compareNodes(nodePair1.first, nodePair2.first); | ||
if (cmp1 < 0) return cmp1; | ||
return compareNodes(nodePair1.second, nodePair2.second); | ||
}); | ||
SmallVector<Edge, 5> scratchEdges; | ||
for (auto &entry: nodePairsVec) { | ||
auto from = entry.first.first; | ||
auto to = entry.first.second; | ||
auto edgeSetIndex = entry.second; | ||
os << " "; | ||
printNodeName(from, os); | ||
os << " -> "; | ||
printNodeName(to, os); | ||
if (printEdgeSet) { | ||
copyAndSort(edgeSets[edgeSetIndex], scratchEdges, compareEdges); | ||
os << " [label = \""; | ||
printEdgeSet(from, to, scratchEdges, os); | ||
os << "\"]"; | ||
} | ||
scratchEdges.clear(); | ||
os << ";\n"; | ||
} | ||
os << "}\n"; | ||
} | ||
}; | ||
|
||
} // end namespace swift | ||
|
||
#endif // SWIFT_BASIC_GRAPHVIZ_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This "fix" is incorrect. The
ShadowedBySeparateOverlay
filter is essentially a different axis altogether.Ideally, we'd be passing in two
OptionSet
s, not one IMO. If you pass innewFilter = {{@_exported}, {not shadowed}}
, you get EN. If you pass innewFilter = {{@_exported | private}, {shadowed}}
, you'd get ES and PS.Today, if you pass in
filter = {@_exported}
, you get EN whereas if you pass infilter = {shadowed}
, you get nothing. That's unnecessarily confusing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also,
@_spi
is a different axis, so the table in ^ is wrong.