Skip to content
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

Fix errors in WhyDgml and account for cycles #106928

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 27 additions & 17 deletions src/coreclr/tools/aot/WhyDgml/WhyDgml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
//
// It works best if FirstMarkLogStrategy is used. It might hit cycles if full
// marking strategy is used, so better not try that. That's untested.
//
//
// Given the name of the DGML file and a name of the node in the DGML graph,
// it prints the path from the node to the roots.

class Program
namespace WhyDgml;

public class Program
{
static void Main(string[] args)
public static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage:");
Console.Write(Assembly.GetExecutingAssembly().ManifestModule.Name);
Console.Write(Assembly.GetExecutingAssembly().GetName().Name);
Console.WriteLine(" dgmlfile.xml node_name");
return;
}
Expand Down Expand Up @@ -56,31 +58,39 @@ static void Main(string[] args)
}

string goal = args[1];
if (!nameToNode.ContainsKey(goal))
if (!nameToNode.TryGetValue(goal, out Node value))
Console.WriteLine($"No such node: '{goal}'.");
else
Dump(nameToNode[goal]);
Dump(value);
}

static void Dump(Node current, int indent = 0, string reason = "")
private static void Dump(Node current, int indent = 0, string reason = "", HashSet<Node> visited = null)
{
visited ??= new HashSet<Node>();

if (!visited.Add(current))
{
Console.WriteLine($"{new string(' ', indent)}({reason}) {current.Name} (cycle)");
return;
}

Console.WriteLine($"{new string(' ', indent)}({reason}) {current.Name}");

foreach (var edge in current.Edges)
{
Dump(edge.Node, indent + 2, edge.Label);
Dump(edge.Node, indent + 2, edge.Label, visited);
}
}
}

class Node
{
public readonly string Name;
public readonly List<(Node Node, string Label)> Edges;

public Node(string name)
private sealed class Node
{
Name = name;
Edges = new List<(Node, string)>();
public readonly string Name;
public readonly List<(Node Node, string Label)> Edges;

public Node(string name)
{
Name = name;
Edges = new List<(Node, string)>();
}
}
}