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

daisytuner / docc / 23087278095

14 Mar 2026 11:44AM UTC coverage: 63.927% (+0.3%) from 63.617%
23087278095

push

github

web-flow
Merge pull request #568 from daisytuner/dead-data-elimination

Working on memory ownership & escape analysis

475 of 637 new or added lines in 28 files covered. (74.57%)

6 existing lines in 3 files now uncovered.

26010 of 40687 relevant lines covered (63.93%)

402.05 hits per line

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

86.59
/sdfg/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_]+");
18

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

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

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

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

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

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

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

93
            if (nonexistent_conn) {
54✔
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
54✔
98
                auto& dstVar = dynamic_cast<data_flow::AccessNode const&>(iedge.dst()).data();
27✔
99
                bool subsetOnDst = false;
27✔
100
                if (srcIsDeref && dstIsVoid) { // Pure Store by Memlet definition (Dereference Memlet Store)
27✔
101
                    auto& subset = iedge.subset();
×
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
27✔
109
                    this->stream_ << dstVar; // use access node name instead of connector-name
27✔
110
                    subsetOnDst = true;
27✔
111
                } else {
27✔
112
                    this->stream_ << dstVar; // use access node name instead of connector-name
×
113
                }
×
114
                if (subsetOnDst) {
27✔
115
                    this->visualizeSubset(sdfg, iedge.subset(), &iedge.base_type());
27✔
116
                }
27✔
117
            } else { // dst is a tasklet/library node
27✔
118
                this->stream_ << dst_conn;
27✔
119
            }
27✔
120

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

123
            if (srcIsVoid || srcIsDeref) { // subset applies to src, could be computational, reference or dereference
54✔
124
                                           // memlet
125
                auto& srcVar = dynamic_cast<data_flow::AccessNode const&>(src).data();
27✔
126
                bool subsetOnSrc = false;
27✔
127
                if (srcIsVoid && dstIsRef) { // reference memlet / address-of / get-element-ptr equivalent
27✔
128
                    this->stream_ << "&";
×
129
                    subsetOnSrc = true;
×
130
                } else if (srcIsVoid && dstIsDeref) { // Dereference memlet / load from address
27✔
131
                    this->stream_ << "*";
×
132
                    auto& subset = iedge.subset();
×
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) {
27✔
138
                    subsetOnSrc = true;
27✔
139
                }
27✔
140
                this->stream_ << srcVar;
27✔
141
                if (subsetOnSrc) {
27✔
142
                    this->visualizeSubset(sdfg, iedge.subset(), &iedge.base_type());
27✔
143
                }
27✔
144
            } else {
27✔
145
                this->stream_ << src_conn;
27✔
146
            }
27✔
147
            this->stream_ << "   \"];" << std::endl;
54✔
148
        }
54✔
149

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

166
void DotVisualizer::visualizeSequence(const StructuredSDFG& sdfg, const structured_control_flow::Sequence& sequence) {
40✔
167
    std::string last_comp_name_tmp, last_comp_name_cluster_tmp;
40✔
168
    for (size_t i = 0; i < sequence.size(); ++i) {
89✔
169
        std::pair<const structured_control_flow::ControlFlowNode&, const structured_control_flow::Transition&> child =
49✔
170
            sequence.at(i);
49✔
171
        this->visualizeNode(sdfg, child.first);
49✔
172
        if ((i > 0) && !last_comp_name_tmp.empty() && !this->last_comp_name_.empty() &&
49✔
173
            last_comp_name_tmp != this->last_comp_name_) {
49✔
174
            this->stream_ << last_comp_name_tmp << " -> " << this->last_comp_name_ << " [";
9✔
175
            if (!last_comp_name_cluster_tmp.empty()) this->stream_ << "ltail=\"" << last_comp_name_cluster_tmp << "\",";
9✔
176
            if (!this->last_comp_name_cluster_.empty())
9✔
177
                this->stream_ << "lhead=\"" << this->last_comp_name_cluster_ << "\",";
6✔
178
            this->stream_ << "minlen=3]"
9✔
179
                          << ";" << std::endl;
9✔
180
        }
9✔
181
        last_comp_name_tmp = this->last_comp_name_;
49✔
182
        last_comp_name_cluster_tmp = this->last_comp_name_cluster_;
49✔
183
    }
49✔
184
}
40✔
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=\""
6✔
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) {
2✔
235
    auto id = escapeDotId(return_node.element_id(), "return_");
2✔
236
    this->stream_ << id << " [shape=cds,label=\" return " << return_node.data() << "\"];" << std::endl;
2✔
237
    this->last_comp_name_ = id;
2✔
238
    this->last_comp_name_cluster_.clear();
2✔
239
}
2✔
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() {
19✔
269
    this->stream_.clear();
19✔
270
    this->stream_ << "digraph " << escapeDotId(this->sdfg_.name()) << " {" << std::endl;
19✔
271
    this->stream_.setIndent(4);
19✔
272
    this->stream_ << "graph [compound=true];" << std::endl;
19✔
273
    this->stream_ << "subgraph cluster_" << escapeDotId(this->sdfg_.name()) << " {" << std::endl;
19✔
274
    this->stream_.setIndent(8);
19✔
275
    this->stream_ << "node [style=filled,fillcolor=white];" << std::endl
19✔
276
                  << "style=filled;color=lightblue;label=\"\";" << std::endl;
19✔
277
    this->visualizeSequence(this->sdfg_, this->sdfg_.root());
19✔
278
    this->stream_.setIndent(4);
19✔
279
    this->stream_ << "}" << std::endl;
19✔
280
    this->stream_.setIndent(0);
19✔
281
    this->stream_ << "}" << std::endl;
19✔
282
}
19✔
283

NEW
284
void DotVisualizer::writeToFile(const StructuredSDFG& sdfg, const std::filesystem::path& file) {
×
NEW
285
    writeToFile(sdfg, &file);
×
NEW
286
}
×
287

288
void DotVisualizer::writeToFile(const StructuredSDFG& sdfg, const std::filesystem::path* file) {
1✔
289
    DotVisualizer viz(sdfg);
1✔
290
    viz.visualize();
1✔
291

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

294
    auto parent_path = fileName.parent_path();
1✔
295
    if (!parent_path.empty()) {
1✔
296
        std::filesystem::create_directories(fileName.parent_path());
×
297
    }
×
298

299
    std::ofstream dotOutput(fileName, std::ofstream::out);
1✔
300
    if (!dotOutput.is_open()) {
1✔
301
        std::cerr << "Could not open file " << fileName << " for writing DOT output." << std::endl;
×
302
    }
×
303

304
    dotOutput << viz.getStream().str();
1✔
305
    dotOutput.close();
1✔
306
}
1✔
307

308
} // namespace visualizer
309
} // 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