• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / sdfglib / 17559474002

08 Sep 2025 05:49PM UTC coverage: 61.347% (+2.2%) from 59.145%
17559474002

Pull #219

github

web-flow
Merge 2ae413ec1 into b8fdeb232
Pull Request #219: stdlib Library Nodes and ConstantNodes

424 of 1301 new or added lines in 74 files covered. (32.59%)

89 existing lines in 31 files now uncovered.

9318 of 15189 relevant lines covered (61.35%)

109.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.16
/src/visualizer/dot_visualizer.cpp
1
#include "sdfg/visualizer/dot_visualizer.h"
2

3
#include <cstddef>
4
#include <string>
5
#include <utility>
6

7
#include <regex>
8
#include "sdfg/data_flow/access_node.h"
9
#include "sdfg/data_flow/memlet.h"
10
#include "sdfg/structured_control_flow/control_flow_node.h"
11
#include "sdfg/structured_control_flow/sequence.h"
12
#include "sdfg/structured_sdfg.h"
13

14
namespace sdfg {
15
namespace visualizer {
16

17
static std::regex dotIdBadChars("[^a-zA-Z0-9_]+");
2✔
18

19
static std::string escapeDotId(size_t id, const std::string& prefix = "") { return prefix + std::to_string(id); }
825✔
20

21
static std::string escapeDotId(const std::string& id, const std::string& prefix = "") {
348✔
22
    return prefix + std::regex_replace(id, dotIdBadChars, "_");
348✔
23
}
×
24

25
void DotVisualizer::visualizeBlock(const StructuredSDFG& sdfg, const structured_control_flow::Block& block) {
184✔
26
    auto id = escapeDotId(block.element_id(), "block_");
184✔
27
    this->stream_ << "subgraph cluster_" << id << " {" << std::endl;
184✔
28
    this->stream_.setIndent(this->stream_.indent() + 4);
184✔
29
    this->stream_ << "style=filled;shape=box;fillcolor=white;color=black;label=\"\";" << std::endl;
184✔
30
    this->last_comp_name_cluster_ = "cluster_" + id;
184✔
31
    if (block.dataflow().nodes().empty()) {
184✔
32
        this->stream_ << id << " [shape=point,style=invis,label=\"\"];" << std::endl;
2✔
33
        this->stream_.setIndent(this->stream_.indent() - 4);
2✔
34
        this->stream_ << "}" << std::endl;
2✔
35
        this->last_comp_name_ = id;
2✔
36
        return;
2✔
37
    }
38
    this->last_comp_name_.clear();
182✔
39
    std::list<const data_flow::DataFlowNode*> nodes = block.dataflow().topological_sort();
182✔
40
    for (const data_flow::DataFlowNode* node : nodes) {
583✔
41
        std::vector<std::string> in_connectors;
401✔
42
        bool is_access_node = false;
401✔
43
        bool node_will_show_literal_connectors = false;
401✔
44
        auto nodeId = escapeDotId(node->element_id(), "n_");
401✔
45
        if (this->last_comp_name_.empty()) this->last_comp_name_ = nodeId;
401✔
46
        if (const data_flow::Tasklet* tasklet = dynamic_cast<const data_flow::Tasklet*>(node)) {
401✔
47
            this->stream_ << nodeId << " [shape=octagon,label=\"" << tasklet->output() << " = ";
186✔
48
            this->visualizeTasklet(*tasklet);
186✔
49
            this->stream_ << "\"];" << std::endl;
186✔
50

51
            in_connectors = tasklet->inputs();
186✔
52
            node_will_show_literal_connectors = true;
186✔
53
        } else if (const data_flow::ConstantNode* constant_node = dynamic_cast<const data_flow::ConstantNode*>(node)) {
401✔
54
            this->stream_ << nodeId << " [";
8✔
55
            this->stream_ << "penwidth=3.0,";
8✔
56
            if (sdfg.is_transient(constant_node->data())) this->stream_ << "style=\"dashed,filled\",";
8✔
57
            this->stream_ << "label=\"" << constant_node->data() << "\"];" << std::endl;
8✔
58
            is_access_node = true;
8✔
59
        } else if (const data_flow::AccessNode* access_node = dynamic_cast<const data_flow::AccessNode*>(node)) {
215✔
60
            this->stream_ << nodeId << " [";
207✔
61
            this->stream_ << "penwidth=3.0,";
207✔
62
            if (sdfg.is_transient(access_node->data())) this->stream_ << "style=\"dashed,filled\",";
207✔
63
            this->stream_ << "label=\"" << access_node->data() << "\"];" << std::endl;
207✔
64
            is_access_node = true;
207✔
65
        } else if (const data_flow::LibraryNode* libnode = dynamic_cast<const data_flow::LibraryNode*>(node)) {
207✔
UNCOV
66
            this->stream_ << nodeId << " [shape=doubleoctagon,label=\"" << libnode->toStr() << "\"];" << std::endl;
×
UNCOV
67
            in_connectors = libnode->inputs();
×
UNCOV
68
        }
×
69

70
        std::unordered_set<std::string> unused_connectors(in_connectors.begin(), in_connectors.end());
401✔
71
        for (const data_flow::Memlet& iedge : block.dataflow().in_edges(*node)) {
620✔
72
            auto& src = iedge.src();
219✔
73
            auto& dst_conn = iedge.dst_conn();
219✔
74
            bool nonexistent_conn = false;
219✔
75

76
            if (!is_access_node) {
219✔
77
                auto it = unused_connectors.find(dst_conn);
33✔
78
                if (it != unused_connectors.end()) {
33✔
79
                    unused_connectors.erase(it); // remove connector from in_connectors, so it is not used again
33✔
80
                } else {
33✔
81
                    nonexistent_conn = true;
×
82
                }
83
            }
33✔
84

85
            this->stream_ << escapeDotId(src.element_id(), "n_") << " -> " << nodeId << " [label=\"   ";
219✔
86
            bool dstIsVoid = dst_conn == "void";
219✔
87
            bool dstIsRef = dst_conn == "ref";
219✔
88
            bool dstIsDeref = dst_conn == "deref";
219✔
89
            auto& src_conn = iedge.src_conn();
219✔
90
            bool srcIsVoid = src_conn == "void";
219✔
91
            bool srcIsDeref = src_conn == "deref";
219✔
92

93
            if (nonexistent_conn) {
219✔
94
                this->stream_ << "!!"; // this should not happen, but if it does, we can still visualize the memlet
×
95
            }
×
96

97
            if (dstIsVoid || dstIsRef || dstIsDeref) { // subset applies to dst
219✔
98
                auto& dstVar = dynamic_cast<data_flow::AccessNode const&>(iedge.dst()).data();
186✔
99
                bool subsetOnDst = false;
186✔
100
                if (srcIsDeref && dstIsVoid) { // Pure Store by Memlet definition (Dereference Memlet Store)
186✔
NEW
101
                    auto& subset = iedge.subset();
×
NEW
102
                    if (subset.size() == 1 && symbolic::eq(subset[0], symbolic::integer(0))) {
×
103
                        this->stream_ << "*" << dstVar; // store to pointer without further address calc
×
104
                    } else { // fallback, this should not be allowed to happen
×
105
                        this->stream_ << dstVar; // use access node name instead of connector-name
×
106
                        subsetOnDst = true;
×
107
                    }
108
                } else if (dstIsVoid) { // computational memlet / output from tasklet / memory store
186✔
109
                    this->stream_ << dstVar; // use access node name instead of connector-name
186✔
110
                    subsetOnDst = true;
186✔
111
                } else {
186✔
112
                    this->stream_ << dstVar; // use access node name instead of connector-name
×
113
                }
114
                if (subsetOnDst) {
186✔
115
                    this->visualizeSubset(sdfg, iedge.subset(), &iedge.base_type());
186✔
116
                }
186✔
117
            } else { // dst is a tasklet/library node
186✔
118
                this->stream_ << dst_conn;
33✔
119
            }
120

121
            this->stream_ << " = ";
219✔
122

123
            if (srcIsVoid || srcIsDeref) { // subset applies to src, could be computational, reference or dereference
219✔
124
                                           // memlet
125
                auto& srcVar = dynamic_cast<data_flow::AccessNode const&>(src).data();
33✔
126
                bool subsetOnSrc = false;
33✔
127
                if (srcIsVoid && dstIsRef) { // reference memlet / address-of / get-element-ptr equivalent
33✔
128
                    this->stream_ << "&";
×
129
                    subsetOnSrc = true;
×
130
                } else if (srcIsVoid && dstIsDeref) { // Dereference memlet / load from address
33✔
131
                    this->stream_ << "*";
×
NEW
132
                    auto& subset = iedge.subset();
×
NEW
133
                    if (subset.size() != 1 && symbolic::eq(subset[0], symbolic::integer(0))) { // does not match memlet
×
134
                                                                                               // definition -> fallback
135
                        subsetOnSrc = true;
×
136
                    }
×
137
                } else if (srcIsVoid) {
33✔
138
                    subsetOnSrc = true;
33✔
139
                }
33✔
140
                this->stream_ << srcVar;
33✔
141
                if (subsetOnSrc) {
33✔
142
                    this->visualizeSubset(sdfg, iedge.subset(), &iedge.base_type());
33✔
143
                }
33✔
144
            } else {
33✔
145
                this->stream_ << src_conn;
186✔
146
            }
147
            this->stream_ << "   \"];" << std::endl;
219✔
148
        }
149

150
        if (!node_will_show_literal_connectors) {
401✔
151
            for (uint64_t i = 0; i < in_connectors.size(); ++i) {
215✔
UNCOV
152
                auto& in_conn = in_connectors[i];
×
UNCOV
153
                auto it = unused_connectors.find(in_conn);
×
UNCOV
154
                if (it != unused_connectors.end()) {
×
UNCOV
155
                    auto literal_id = escapeDotId(node->element_id(), "n_") + "_" + escapeDotId(i, "in");
×
UNCOV
156
                    this->stream_ << literal_id << " [style=\"dotted\", label=\"" << in_conn << "\"];" << std::endl;
×
UNCOV
157
                    this->stream_ << literal_id << " -> " << nodeId << " [label=\"" << i << "\"]" << ";" << std::endl;
×
UNCOV
158
                }
×
UNCOV
159
            }
×
160
        }
215✔
161
    }
401✔
162
    this->stream_.setIndent(this->stream_.indent() - 4);
182✔
163
    this->stream_ << "}" << std::endl;
182✔
164
}
184✔
165

166
void DotVisualizer::visualizeSequence(const StructuredSDFG& sdfg, const structured_control_flow::Sequence& sequence) {
196✔
167
    std::string last_comp_name_tmp, last_comp_name_cluster_tmp;
196✔
168
    for (size_t i = 0; i < sequence.size(); ++i) {
402✔
169
        std::pair<const structured_control_flow::ControlFlowNode&, const structured_control_flow::Transition&> child =
170
            sequence.at(i);
206✔
171
        this->visualizeNode(sdfg, child.first);
206✔
172
        if ((i > 0) && !last_comp_name_tmp.empty() && !this->last_comp_name_.empty() &&
206✔
173
            last_comp_name_tmp != this->last_comp_name_) {
10✔
174
            this->stream_ << last_comp_name_tmp << " -> " << this->last_comp_name_ << " [";
10✔
175
            if (!last_comp_name_cluster_tmp.empty()) this->stream_ << "ltail=\"" << last_comp_name_cluster_tmp << "\",";
10✔
176
            if (!this->last_comp_name_cluster_.empty())
10✔
177
                this->stream_ << "lhead=\"" << this->last_comp_name_cluster_ << "\",";
8✔
178
            this->stream_ << "minlen=3]"
10✔
179
                          << ";" << std::endl;
10✔
180
        }
10✔
181
        last_comp_name_tmp = this->last_comp_name_;
206✔
182
        last_comp_name_cluster_tmp = this->last_comp_name_cluster_;
206✔
183
    }
206✔
184
}
196✔
185

186
void DotVisualizer::visualizeIfElse(const StructuredSDFG& sdfg, const structured_control_flow::IfElse& if_else) {
3✔
187
    auto id = escapeDotId(if_else.element_id(), "if_");
3✔
188
    this->stream_ << "subgraph cluster_" << id << " {" << std::endl;
3✔
189
    this->stream_.setIndent(this->stream_.indent() + 4);
3✔
190
    this->stream_ << "style=filled;shape=box;fillcolor=white;color=black;label=\"if:\";" << std::endl
3✔
191
                  << id << " [shape=point,style=invis,label=\"\"];" << std::endl;
3✔
192
    for (size_t i = 0; i < if_else.size(); ++i) {
9✔
193
        this->stream_ << "subgraph cluster_" << id << "_" << std::to_string(i) << " {" << std::endl;
6✔
194
        this->stream_.setIndent(this->stream_.indent() + 4);
6✔
195
        this->stream_ << "style=filled;shape=box;fillcolor=white;color=black;label=\""
12✔
196
                      << this->expression(if_else.at(i).second->__str__()) << "\";" << std::endl;
6✔
197
        this->visualizeSequence(sdfg, if_else.at(i).first);
6✔
198
        this->stream_.setIndent(this->stream_.indent() - 4);
6✔
199
        this->stream_ << "}" << std::endl;
6✔
200
    }
6✔
201
    this->stream_.setIndent(this->stream_.indent() - 4);
3✔
202
    this->stream_ << "}" << std::endl;
3✔
203
    this->last_comp_name_ = id;
3✔
204
    this->last_comp_name_cluster_ = "cluster_" + id;
3✔
205
}
3✔
206

207
void DotVisualizer::visualizeWhile(const StructuredSDFG& sdfg, const structured_control_flow::While& while_loop) {
2✔
208
    auto id = escapeDotId(while_loop.element_id(), "while_");
2✔
209
    this->stream_ << "subgraph cluster_" << id << " {" << std::endl;
2✔
210
    this->stream_.setIndent(this->stream_.indent() + 4);
2✔
211
    this->stream_ << "style=filled;shape=box;fillcolor=white;color=black;label=\"while:\";" << std::endl
2✔
212
                  << id << " [shape=point,style=invis,label=\"\"];" << std::endl;
2✔
213
    this->visualizeSequence(sdfg, while_loop.root());
2✔
214
    this->stream_.setIndent(this->stream_.indent() - 4);
2✔
215
    this->stream_ << "}" << std::endl;
2✔
216
    this->last_comp_name_ = id;
2✔
217
    this->last_comp_name_cluster_ = "cluster_" + id;
2✔
218
}
2✔
219

220
void DotVisualizer::visualizeFor(const StructuredSDFG& sdfg, const structured_control_flow::For& loop) {
10✔
221
    auto id = escapeDotId(loop.element_id(), "for_");
10✔
222
    this->stream_ << "subgraph cluster_" << id << " {" << std::endl;
10✔
223
    this->stream_.setIndent(this->stream_.indent() + 4);
10✔
224
    this->stream_ << "style=filled;shape=box;fillcolor=white;color=black;label=\"for: ";
10✔
225
    this->visualizeForBounds(loop.indvar(), loop.init(), loop.condition(), loop.update());
10✔
226
    this->stream_ << "\";" << std::endl << id << " [shape=point,style=invis,label=\"\"];" << std::endl;
10✔
227
    this->visualizeSequence(sdfg, loop.root());
10✔
228
    this->stream_.setIndent(this->stream_.indent() - 4);
10✔
229
    this->stream_ << "}" << std::endl;
10✔
230
    this->last_comp_name_ = id;
10✔
231
    this->last_comp_name_cluster_ = "cluster_" + id;
10✔
232
}
10✔
233

234
void DotVisualizer::visualizeReturn(const StructuredSDFG& sdfg, const structured_control_flow::Return& return_node) {
1✔
235
    auto id = escapeDotId(return_node.element_id(), "return_");
1✔
236
    this->stream_ << id << " [shape=cds,label=\" return  \"];" << std::endl;
1✔
237
    this->last_comp_name_ = id;
1✔
238
    this->last_comp_name_cluster_.clear();
1✔
239
}
1✔
240
void DotVisualizer::visualizeBreak(const StructuredSDFG& sdfg, const structured_control_flow::Break& break_node) {
1✔
241
    auto id = escapeDotId(break_node.element_id(), "break_");
1✔
242
    this->stream_ << id << " [shape=cds,label=\" break  \"];" << std::endl;
1✔
243
    this->last_comp_name_ = id;
1✔
244
    this->last_comp_name_cluster_.clear();
1✔
245
}
1✔
246

247
void DotVisualizer::visualizeContinue(const StructuredSDFG& sdfg, const structured_control_flow::Continue& continue_node) {
1✔
248
    auto id = escapeDotId(continue_node.element_id(), "cont_");
1✔
249
    this->stream_ << id << " [shape=cds,label=\" continue  \"];" << std::endl;
1✔
250
    this->last_comp_name_ = id;
1✔
251
    this->last_comp_name_cluster_.clear();
1✔
252
}
1✔
253

254
void DotVisualizer::visualizeMap(const StructuredSDFG& sdfg, const structured_control_flow::Map& map_node) {
3✔
255
    auto id = escapeDotId(map_node.element_id(), "map_");
3✔
256
    this->stream_ << "subgraph cluster_" << id << " {" << std::endl;
3✔
257
    this->stream_.setIndent(this->stream_.indent() + 4);
3✔
258
    this->stream_ << "style=filled;shape=box;fillcolor=white;color=black;label=\"map: ";
3✔
259
    this->visualizeForBounds(map_node.indvar(), map_node.init(), map_node.condition(), map_node.update());
3✔
260
    this->stream_ << "\";" << std::endl << id << " [shape=point,style=invis,label=\"\"];" << std::endl;
3✔
261
    this->visualizeSequence(sdfg, map_node.root());
3✔
262
    this->stream_.setIndent(this->stream_.indent() - 4);
3✔
263
    this->stream_ << "}" << std::endl;
3✔
264
    this->last_comp_name_ = id;
3✔
265
    this->last_comp_name_cluster_ = "cluster_" + id;
3✔
266
}
3✔
267

268
void DotVisualizer::visualize() {
174✔
269
    this->stream_.clear();
174✔
270
    this->stream_ << "digraph " << escapeDotId(this->sdfg_.name()) << " {" << std::endl;
174✔
271
    this->stream_.setIndent(4);
174✔
272
    this->stream_ << "graph [compound=true];" << std::endl;
174✔
273
    this->stream_ << "subgraph cluster_" << escapeDotId(this->sdfg_.name()) << " {" << std::endl;
174✔
274
    this->stream_.setIndent(8);
174✔
275
    this->stream_ << "node [style=filled,fillcolor=white];" << std::endl
174✔
276
                  << "style=filled;color=lightblue;label=\"\";" << std::endl;
174✔
277
    this->visualizeSequence(this->sdfg_, this->sdfg_.root());
174✔
278
    this->stream_.setIndent(4);
174✔
279
    this->stream_ << "}" << std::endl;
174✔
280
    this->stream_.setIndent(0);
174✔
281
    this->stream_ << "}" << std::endl;
174✔
282
}
174✔
283

284
void DotVisualizer::writeToFile(const StructuredSDFG& sdfg, const std::filesystem::path* file) {
1✔
285
    DotVisualizer viz(sdfg);
1✔
286
    viz.visualize();
1✔
287

288
    std::filesystem::path fileName = file ? *file : std::filesystem::path(sdfg.name() + ".dot");
1✔
289

290
    std::ofstream dotOutput(fileName, std::ofstream::out);
1✔
291

292
    dotOutput << viz.getStream().str();
1✔
293
    dotOutput.close();
1✔
294
    std::cout << "Wrote graph to : " << fileName << std::endl;
1✔
295
}
1✔
296
} // namespace visualizer
297
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc