MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
dot.cpp
Go to the documentation of this file.
1#include "pbqp/dot.h"
2
3#include <cassert>
4
5#include <print>
6
7namespace pbqp {
8
9namespace {
10
11constexpr const char* kChosen = R"( bgcolor="palegreen")";
12
13void print_cell(std::ostream& out, Cost cost, bool chosen) {
14 const char* const attr = chosen ? kChosen : "";
15 if (cost.is_inf())
16 std::print(out, "<td{}>&#8734;</td>", attr);
17 else
18 std::print(out, "<td{}>{}</td>", attr, cost.value());
19}
20
21} // namespace
22
23void write_dot(std::ostream& out, const Graph& graph, fe::View<OptionId> choices) {
24 const bool highlight = !choices.empty();
25 assert((!highlight || choices.size() >= graph.num_nodes()) && "write_dot: choices must cover every node id");
26
27 std::println(out, "graph pbqp {{");
28 std::println(out, " node [shape=plain];");
29 std::println(out, " edge [fontsize=10];");
30
31 for (const auto n : graph.nodes()) {
32 const auto costs = graph.node_costs(n);
33 std::print(
34 out,
35 R"( n{} [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td colspan="{}"><b>n{}</b></td></tr><tr>)",
36 n, costs.size(), n);
37 for (OptionId i = 0; i < costs.size(); ++i)
38 print_cell(out, costs[i], highlight && choices[n] == i);
39 std::println(out, "</tr></table>>];");
40 }
41
42 for (const auto e : graph.edges()) {
43 const auto [src, dst] = graph.edge(e);
44 const MatrixView matrix = graph.edge_matrix(e);
45 std::print(
46 out,
47 R"( n{} -- n{} [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td colspan="{}">n{} &#215; n{}</td></tr>)",
48 src, dst, matrix.cols(), src, dst);
49 for (OptionId i = 0; i < matrix.rows(); ++i) {
50 std::print(out, "<tr>");
51 for (OptionId j = 0; j < matrix.cols(); ++j)
52 print_cell(out, matrix[i, j], highlight && choices[src] == i && choices[dst] == j);
53 std::print(out, "</tr>");
54 }
55 std::println(out, "</table>>];");
56 }
57
58 std::println(out, "}}");
59}
60
61} // namespace pbqp
constexpr size_t rows() const
Definition graph.h:88
constexpr size_t cols() const
Definition graph.h:89
A saturating cost; Cost::inf forbids an option or option pair.
Definition graph.h:31
A PBQP problem graph.
Definition graph.h:252
Ends edge(EdgeId e) const
Definition graph.h:303
size_t num_nodes() const
Definition graph.h:312
MatrixView edge_matrix(EdgeId e) const
Row-major with the src option as the row.
Definition graph.h:287
fe::View< Cost > node_costs(NodeId n) const
Definition graph.h:284
auto nodes() const
All node ids.
Definition graph.h:309
auto edges() const
All edge ids.
Definition graph.h:310
u32 cost(const App *app)
The cost of the instruction app: its latency plus one, so of two equally fast covers the shorter wins...
Definition insn.h:24
Definition dot.h:9
size_t OptionId
Index of an option within a node's cost vector.
Definition graph.h:27
BasicMatrix< const Cost > MatrixView
Definition graph.h:186
void write_dot(std::ostream &out, const Graph &graph, fe::View< OptionId > choices={})
Write graph in Graphviz DOT format with cost vectors and matrices as HTML-like labels.
Definition dot.cpp:23