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

daisytuner / docc / 27330290888

11 Jun 2026 07:12AM UTC coverage: 61.123% (+0.02%) from 61.108%
27330290888

push

github

web-flow
Structured ControlFlowNodes now keep a reference to their parent, no longer need ScopeAnalysis (#749)

* updated all current users of ScopeAnalysis to use the integrated parents instead
 + a few unit tests for parent relationship
 * refactored StructuredSDFGBuilder to reuse the same code to add ControlFlowNodes to the SDFG
 * generified mechanism for ControlFlowNode constructors to create further inner nodes (ex: Sequence inside Map) with element id reservation.
 - removed access to ScopeAnalysis from python bindings
 + access to parent reference in python bindings

177 of 225 new or added lines in 59 files covered. (78.67%)

12 existing lines in 6 files now uncovered.

35879 of 58700 relevant lines covered (61.12%)

750.7 hits per line

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

73.91
/sdfg/src/builder/structured_sdfg_builder.cpp
1
#include "sdfg/builder/structured_sdfg_builder.h"
2

3
#include <cstddef>
4

5
#include "sdfg/data_flow/library_node.h"
6
#include "sdfg/structured_control_flow/map.h"
7
#include "sdfg/structured_control_flow/sequence.h"
8
#include "sdfg/structured_control_flow/structured_loop.h"
9
#include "sdfg/types/utils.h"
10

11
#define TRAVERSE_CUTOFF 30
100✔
12

13
using namespace sdfg::control_flow;
14
using namespace sdfg::structured_control_flow;
15

16
namespace sdfg {
17
namespace builder {
18

19
std::unordered_set<const control_flow::State*> StructuredSDFGBuilder::
20
    determine_loop_nodes(SDFG& sdfg, const control_flow::State& start, const control_flow::State& end) const {
13✔
21
    std::unordered_set<const control_flow::State*> nodes;
13✔
22
    std::unordered_set<const control_flow::State*> visited;
13✔
23
    std::list<const control_flow::State*> queue = {&start};
13✔
24
    while (!queue.empty()) {
56✔
25
        auto curr = queue.front();
43✔
26
        queue.pop_front();
43✔
27
        if (visited.find(curr) != visited.end()) {
43✔
28
            continue;
1✔
29
        }
1✔
30
        visited.insert(curr);
42✔
31

32
        nodes.insert(curr);
42✔
33
        if (curr == &end) {
42✔
34
            continue;
13✔
35
        }
13✔
36

37
        for (auto& iedge : sdfg.in_edges(*curr)) {
30✔
38
            queue.push_back(&iedge.src());
30✔
39
        }
30✔
40
    }
29✔
41

42
    // Iteratively expand nodes to reduce frontier size
43
    auto dom_tree = sdfg.dominator_tree();
13✔
44
    auto dominates = [&](const control_flow::State* a, const control_flow::State* b) {
13✔
45
        const control_flow::State* curr = b;
12✔
46
        while (curr != nullptr) {
42✔
47
            if (curr == a) return true;
42✔
48
            if (dom_tree.find(curr) == dom_tree.end()) break;
30✔
49
            curr = dom_tree.at(curr);
30✔
50
        }
30✔
51
        return false;
×
52
    };
12✔
53

54
    // Identify header exits
55
    std::unordered_set<const control_flow::State*> stop_nodes;
13✔
56
    for (auto& edge : sdfg.out_edges(end)) {
24✔
57
        if (nodes.find(&edge.dst()) == nodes.end()) {
24✔
58
            stop_nodes.insert(&edge.dst());
11✔
59
        }
11✔
60
    }
24✔
61

62
    // If no header exits, check latch exits (Do-While)
63
    if (stop_nodes.empty()) {
13✔
64
        for (auto& edge : sdfg.out_edges(start)) {
4✔
65
            if (nodes.find(&edge.dst()) == nodes.end()) {
4✔
66
                stop_nodes.insert(&edge.dst());
2✔
67
            }
2✔
68
        }
4✔
69
    }
2✔
70

71
    // If still no exits, check any natural loop exit (e.g. infinite loop with break)
72
    if (stop_nodes.empty()) {
13✔
73
        for (auto node : nodes) {
×
74
            for (auto& edge : sdfg.out_edges(*node)) {
×
75
                if (nodes.find(&edge.dst()) == nodes.end()) {
×
76
                    stop_nodes.insert(&edge.dst());
×
77
                }
×
78
            }
×
79
        }
×
80
    }
×
81

82
    while (true) {
21✔
83
        std::unordered_set<const control_flow::State*> frontier;
21✔
84
        for (auto node : nodes) {
85✔
85
            for (auto& edge : sdfg.out_edges(*node)) {
139✔
86
                if (nodes.find(&edge.dst()) == nodes.end()) {
139✔
87
                    frontier.insert(&edge.dst());
40✔
88
                }
40✔
89
            }
139✔
90
        }
85✔
91

92
        bool changed = false;
21✔
93
        for (auto f : frontier) {
33✔
94
            // If f is a stop node, do not include it
95
            if (stop_nodes.find(f) != stop_nodes.end()) {
33✔
96
                continue;
21✔
97
            }
21✔
98
            // If f is dominated by the header, it belongs to the loop body (extended)
99
            if (dominates(&end, f)) {
12✔
100
                nodes.insert(f);
12✔
101
                changed = true;
12✔
102
            }
12✔
103
        }
12✔
104

105
        if (!changed) break;
21✔
106
    }
21✔
107

108
    return nodes;
13✔
109
};
13✔
110

111
bool post_dominates(
112
    const State* pdom,
113
    const State* node,
114
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree
115
) {
21✔
116
    if (pdom == node) {
21✔
117
        return true;
12✔
118
    }
12✔
119

120
    auto current = pdom_tree.at(node);
9✔
121
    while (current != nullptr) {
9✔
122
        if (current == pdom) {
3✔
123
            return true;
3✔
124
        }
3✔
125
        current = pdom_tree.at(current);
×
126
    }
×
127

128
    return false;
6✔
129
}
9✔
130

131

132
void StructuredSDFGBuilder::traverse(SDFG& sdfg) {
16✔
133
    // Start of SDFGS
134
    Sequence& root = *structured_sdfg_->root_;
16✔
135
    const State* start_state = &sdfg.start_state();
16✔
136

137
    auto pdom_tree = sdfg.post_dominator_tree();
16✔
138

139
    std::unordered_set<const InterstateEdge*> breaks;
16✔
140
    std::unordered_set<const InterstateEdge*> continues;
16✔
141
    for (auto& edge : sdfg.back_edges()) {
16✔
142
        continues.insert(edge);
13✔
143
    }
13✔
144

145
    this->current_traverse_loop_ = nullptr;
16✔
146
    std::unordered_set<const control_flow::State*> visited;
16✔
147
    this->structure_region(sdfg, root, start_state, nullptr, continues, breaks, pdom_tree, visited);
16✔
148
};
16✔
149

150
void StructuredSDFGBuilder::structure_region(
151
    SDFG& sdfg,
152
    Sequence& scope,
153
    const State* entry,
154
    const State* exit,
155
    const std::unordered_set<const InterstateEdge*>& continues,
156
    const std::unordered_set<const InterstateEdge*>& breaks,
157
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree,
158
    std::unordered_set<const control_flow::State*>& visited,
159
    bool is_loop_body
160
) {
64✔
161
    const State* current = entry;
64✔
162
    while (current != exit) {
138✔
163
        if (current == nullptr) {
100✔
164
            break;
×
165
        }
×
166

167

168
        // Cutoff
169
        if (this->function().element_counter_ > sdfg.states().size() * TRAVERSE_CUTOFF) {
100✔
170
            throw UnstructuredControlFlowException();
×
171
        }
×
172

173
        if (visited.find(current) != visited.end()) {
100✔
174
            throw UnstructuredControlFlowException();
×
175
        }
×
176
        visited.insert(current);
100✔
177

178
        // Loop detection
179
        bool is_loop_header = false;
100✔
180
        if (!is_loop_body || current != entry) {
100✔
181
            for (auto& iedge : sdfg.in_edges(*current)) {
100✔
182
                if (continues.find(&iedge) != continues.end()) {
100✔
183
                    is_loop_header = true;
9✔
184
                    break;
9✔
185
                }
9✔
186
            }
100✔
187
        }
91✔
188

189
        if (is_loop_header) {
100✔
190
            // 1. Determine nodes of loop body
191
            std::unordered_set<const InterstateEdge*> loop_edges;
9✔
192
            for (auto& iedge : sdfg.in_edges(*current)) {
22✔
193
                if (continues.find(&iedge) != continues.end()) {
22✔
194
                    loop_edges.insert(&iedge);
13✔
195
                }
13✔
196
            }
22✔
197

198
            std::unordered_set<const control_flow::State*> body;
9✔
199
            for (auto back_edge : loop_edges) {
13✔
200
                auto loop_nodes = this->determine_loop_nodes(sdfg, back_edge->src(), back_edge->dst());
13✔
201
                body.insert(loop_nodes.begin(), loop_nodes.end());
13✔
202
            }
13✔
203

204
            // 2. Determine exit states and exit edges
205
            std::unordered_set<const control_flow::State*> exit_states;
9✔
206
            std::unordered_set<const control_flow::InterstateEdge*> exit_edges;
9✔
207
            for (auto node : body) {
34✔
208
                for (auto& edge : sdfg.out_edges(*node)) {
52✔
209
                    if (body.find(&edge.dst()) == body.end()) {
52✔
210
                        if (continues.find(&edge) != continues.end()) {
12✔
211
                            continue;
×
212
                        }
×
213
                        exit_edges.insert(&edge);
12✔
214
                        exit_states.insert(&edge.dst());
12✔
215
                    }
12✔
216
                }
52✔
217
            }
34✔
218

219
            if (exit_states.size() > 1) {
9✔
220
                std::unordered_set<const control_flow::State*> non_return_exits;
×
221
                for (auto s : exit_states) {
×
222
                    if (dynamic_cast<const control_flow::ReturnState*>(s)) {
×
223
                        continue;
×
224
                    }
×
225
                    if (sdfg.out_degree(*s) > 0) {
×
226
                        non_return_exits.insert(s);
×
227
                    }
×
228
                }
×
229
                if (non_return_exits.size() == 1) {
×
230
                    exit_states = non_return_exits;
×
231
                }
×
232
            }
×
233

234
            if (exit_states.size() != 1) {
9✔
235
                throw UnstructuredControlFlowException();
×
236
            }
×
237
            const control_flow::State* exit_state = *exit_states.begin();
9✔
238

239
            for (auto& edge : breaks) {
9✔
240
                exit_edges.insert(edge);
1✔
241
            }
1✔
242

243
            // Collect debug information
244
            DebugInfo dbg_info = current->debug_info();
9✔
245
            for (auto& edge : sdfg.in_edges(*current)) {
22✔
246
                dbg_info = DebugInfo::merge(dbg_info, edge.debug_info());
22✔
247
            }
22✔
248
            for (auto node : body) {
34✔
249
                dbg_info = DebugInfo::merge(dbg_info, node->debug_info());
34✔
250
            }
34✔
251
            for (auto edge : exit_edges) {
13✔
252
                dbg_info = DebugInfo::merge(dbg_info, edge->debug_info());
13✔
253
            }
13✔
254

255
            // 3. Add while loop
256
            While& loop = this->add_while(scope, {}, dbg_info);
9✔
257
            auto last_loop_ = this->current_traverse_loop_;
9✔
258
            this->current_traverse_loop_ = &loop;
9✔
259

260
            std::unordered_set<const control_flow::State*> loop_visited(visited);
9✔
261
            loop_visited.erase(current);
9✔
262

263
            this->structure_region(
9✔
264
                sdfg, loop.root(), current, exit_state, continues, exit_edges, pdom_tree, loop_visited, true
9✔
265
            );
9✔
266
            this->current_traverse_loop_ = last_loop_;
9✔
267

268
            current = exit_state;
9✔
269
            continue;
9✔
270
        }
9✔
271

272
        auto out_edges = sdfg.out_edges(*current);
91✔
273
        auto out_degree = sdfg.out_degree(*current);
91✔
274

275
        // Case 1: Sink node
276
        if (out_degree == 0) {
91✔
277
            if (!std::ranges::empty(current->dataflow().nodes())) {
17✔
278
                this->add_block(scope, current->dataflow(), {}, current->debug_info());
×
279
            }
×
280

281
            auto return_state = dynamic_cast<const control_flow::ReturnState*>(current);
17✔
282
            assert(return_state != nullptr);
17✔
283
            if (return_state->is_data()) {
17✔
284
                this->add_return(scope, return_state->data(), {}, return_state->debug_info());
17✔
285
            } else if (return_state->is_constant()) {
17✔
286
                this->add_constant_return(
×
287
                    scope, return_state->data(), return_state->type(), {}, return_state->debug_info()
×
288
                );
×
289
            } else {
×
290
                assert(false && "Unknown return state type");
×
291
            }
×
292

293
            break;
17✔
294
        }
17✔
295

296
        // Case 2: Transition
297
        if (out_degree == 1) {
74✔
298
            auto& oedge = *out_edges.begin();
45✔
299
            if (!oedge.is_unconditional()) {
45✔
300
                throw UnstructuredControlFlowException();
×
301
            }
×
302

303
            if (!std::ranges::empty(current->dataflow().nodes()) || !oedge.assignments().empty()) {
45✔
304
                this->add_block(scope, current->dataflow(), oedge.assignments(), current->debug_info());
10✔
305
            }
10✔
306

307
            if (continues.find(&oedge) != continues.end()) {
45✔
308
                if (this->current_traverse_loop_ == nullptr) {
7✔
309
                    throw UnstructuredControlFlowException();
×
310
                }
×
311
                this->add_continue(scope, {}, oedge.debug_info());
7✔
312
                break;
7✔
313
            } else if (breaks.find(&oedge) != breaks.end()) {
38✔
314
                if (this->current_traverse_loop_ == nullptr) {
1✔
315
                    throw UnstructuredControlFlowException();
×
316
                }
×
317
                this->add_break(scope, {}, oedge.debug_info());
1✔
318
                break;
1✔
319
            } else {
37✔
320
                current = &oedge.dst();
37✔
321
            }
37✔
322
            continue;
37✔
323
        }
45✔
324

325
        // Case 3: Branches
326
        if (out_degree > 1) {
29✔
327
            if (!std::ranges::empty(current->dataflow().nodes())) {
29✔
328
                this->add_block(scope, current->dataflow(), {}, current->debug_info());
×
329
            }
×
330

331
            // Determine Merge Point
332
            const State* merge = nullptr;
29✔
333
            if (pdom_tree.find(current) != pdom_tree.end()) {
29✔
334
                merge = pdom_tree.at(current);
29✔
335
            }
29✔
336

337

338
            // If merge is beyond exit, clamp to exit
339
            if (exit != nullptr && merge != nullptr) {
29✔
340
                if (post_dominates(merge, exit, pdom_tree)) {
21✔
341
                    merge = exit;
15✔
342
                }
15✔
343
            }
21✔
344

345
            if (merge != nullptr && visited.find(merge) != visited.end()) {
29✔
346
                merge = exit;
4✔
347
            }
4✔
348

349
            if (merge == nullptr && exit != nullptr) {
29✔
350
                merge = exit;
×
351
            }
×
352

353
            auto& if_else = this->add_if_else(scope, {}, current->debug_info());
29✔
354
            for (auto& out_edge : out_edges) {
57✔
355
                auto& branch = this->add_case(if_else, out_edge.condition(), out_edge.debug_info());
57✔
356
                if (!out_edge.assignments().empty()) {
57✔
357
                    this->add_block(branch, out_edge.assignments(), out_edge.debug_info());
5✔
358
                }
5✔
359
                if (continues.find(&out_edge) != continues.end()) {
57✔
360
                    if (this->current_traverse_loop_ == nullptr) {
7✔
361
                        throw UnstructuredControlFlowException();
1✔
362
                    }
1✔
363
                    this->add_continue(branch, {}, out_edge.debug_info());
6✔
364
                } else if (breaks.find(&out_edge) != breaks.end()) {
50✔
365
                    if (this->current_traverse_loop_ == nullptr) {
11✔
366
                        throw UnstructuredControlFlowException();
×
367
                    }
×
368
                    this->add_break(branch, {}, out_edge.debug_info());
11✔
369
                } else {
39✔
370
                    std::unordered_set<const control_flow::State*> branch_visited(visited);
39✔
371
                    this->structure_region(
39✔
372
                        sdfg, branch, &out_edge.dst(), merge, continues, breaks, pdom_tree, branch_visited
39✔
373
                    );
39✔
374
                }
39✔
375
            }
57✔
376

377
            current = merge;
28✔
378
            continue;
28✔
379
        }
29✔
380
    }
29✔
381
}
64✔
382

383
Function& StructuredSDFGBuilder::function() const { return static_cast<Function&>(*this->structured_sdfg_); };
52,247✔
384

385
StructuredSDFGBuilder::StructuredSDFGBuilder(StructuredSDFG& sdfg)
386
    : FunctionBuilder(), structured_sdfg_(&sdfg, owned(false)) {};
×
387

388
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
389
    : FunctionBuilder(), structured_sdfg_(sdfg.release(), owned(true)) {};
292✔
390

391
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
392
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type), owned(true)) {};
1,718✔
393

394
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type, const types::IType& return_type)
395
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type, return_type), owned(true)) {};
38✔
396

397
StructuredSDFGBuilder::StructuredSDFGBuilder(SDFG& sdfg)
398
    : FunctionBuilder(),
16✔
399
      structured_sdfg_(new StructuredSDFG(sdfg.name(), sdfg.type(), sdfg.return_type()), owned(true)) {
16✔
400
    for (auto& entry : sdfg.structures_) {
16✔
401
        this->structured_sdfg_->structures_.insert({entry.first, entry.second->clone()});
×
402
    }
×
403

404
    for (auto& entry : sdfg.containers_) {
27✔
405
        this->structured_sdfg_->containers_.insert({entry.first, entry.second->clone()});
27✔
406
    }
27✔
407

408
    for (auto& arg : sdfg.arguments_) {
16✔
409
        this->structured_sdfg_->arguments_.push_back(arg);
2✔
410
    }
2✔
411

412
    for (auto& ext : sdfg.externals_) {
16✔
413
        this->structured_sdfg_->externals_.push_back(ext);
1✔
414
        this->structured_sdfg_->externals_linkage_types_[ext] = sdfg.linkage_type(ext);
1✔
415
    }
1✔
416

417
    for (auto& entry : sdfg.assumptions_) {
25✔
418
        this->structured_sdfg_->assumptions_.insert({entry.first, entry.second});
25✔
419
    }
25✔
420

421
    for (auto& entry : sdfg.metadata_) {
16✔
422
        this->structured_sdfg_->metadata_[entry.first] = entry.second;
2✔
423
    }
2✔
424

425
    this->traverse(sdfg);
16✔
426
};
16✔
427

428
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
6,063✔
429

430
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
550✔
431
#ifndef NDEBUG
550✔
432
    this->structured_sdfg_->validate();
550✔
433
#endif
550✔
434

435
    if (!structured_sdfg_.get_deleter().should_delete_) {
550✔
436
        throw InvalidSDFGException("StructuredSDFGBuilder: Cannot move a non-owned SDFG");
×
437
    }
×
438

439
    return std::move(std::unique_ptr<StructuredSDFG>(structured_sdfg_.release()));
550✔
440
};
550✔
441

442
void StructuredSDFGBuilder::rename_container(const std::string& old_name, const std::string& new_name) const {
×
443
    FunctionBuilder::rename_container(old_name, new_name);
×
444

445
    this->structured_sdfg_->root_->replace(symbolic::symbol(old_name), symbolic::symbol(new_name));
×
446
};
×
447

448
Element* StructuredSDFGBuilder::find_element_by_id(const size_t& element_id) const {
57✔
449
    auto& sdfg = this->subject();
57✔
450
    std::list<Element*> queue = {&sdfg.root()};
57✔
451
    while (!queue.empty()) {
233✔
452
        auto current = queue.front();
233✔
453
        queue.pop_front();
233✔
454

455
        if (current->element_id() == element_id) {
233✔
456
            return current;
57✔
457
        }
57✔
458

459
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
176✔
460
            auto& dataflow = block_stmt->dataflow();
9✔
461
            for (auto& node : dataflow.nodes()) {
37✔
462
                queue.push_back(&node);
37✔
463
            }
37✔
464
            for (auto& edge : dataflow.edges()) {
28✔
465
                queue.push_back(&edge);
28✔
466
            }
28✔
467
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
167✔
468
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
176✔
469
                queue.push_back(&sequence_stmt->at(i).first);
90✔
470
                queue.push_back(&sequence_stmt->at(i).second);
90✔
471
            }
90✔
472
        } else if (dynamic_cast<structured_control_flow::Return*>(current)) {
86✔
473
            // Do nothing
474
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
81✔
475
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
476
                queue.push_back(&if_else_stmt->at(i).first);
×
477
            }
×
478
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
81✔
479
            queue.push_back(&for_stmt->root());
5✔
480
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
76✔
481
            queue.push_back(&while_stmt->root());
×
482
        } else if (dynamic_cast<structured_control_flow::Continue*>(current)) {
76✔
483
            // Do nothing
484
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
76✔
485
            // Do nothing
486
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
76✔
487
            queue.push_back(&map_stmt->root());
27✔
488
        }
27✔
489
    }
176✔
490

491
    return nullptr;
×
492
};
57✔
493

494
Sequence& StructuredSDFGBuilder::
495
    add_sequence(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
28✔
496
    return insert_node_internal<Sequence>(parent, INSERT_AT_END, assignments, debug_info).first;
28✔
497
}
28✔
498

499
Sequence& StructuredSDFGBuilder::add_sequence_before(
500
    Sequence& parent,
501
    ControlFlowNode& child,
502
    const sdfg::control_flow::Assignments& assignments,
503
    const DebugInfo& debug_info
504
) {
587✔
505
    int index = parent.index(child);
587✔
506
    if (index == -1) {
587✔
507
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
508
    }
×
509

510
    return insert_node_internal<Sequence>(parent, index, assignments, debug_info).first;
587✔
511
}
587✔
512

513
Sequence& StructuredSDFGBuilder::add_sequence_after(
514
    Sequence& parent,
515
    ControlFlowNode& child,
516
    const sdfg::control_flow::Assignments& assignments,
517
    const DebugInfo& debug_info
518
) {
×
519
    int index = parent.index(child);
×
520
    if (index == -1) {
×
521
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
522
    }
×
523

NEW
524
    return insert_node_internal<Sequence>(parent, index + 1, assignments, debug_info).first;
×
NEW
525
}
×
526

527
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::
528
    add_sequence_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
529
    int index = parent.index(child);
×
530
    if (index == -1) {
×
531
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
532
    }
×
533

NEW
534
    return insert_node_internal<Sequence>(parent, index, {}, debug_info);
×
NEW
535
}
×
536

537
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t index) {
836✔
538
    parent.children_.erase(parent.children_.begin() + index);
836✔
539
    parent.transitions_.erase(parent.transitions_.begin() + index);
836✔
540
};
836✔
541

542
void StructuredSDFGBuilder::remove_children(Sequence& parent) {
×
543
    parent.children_.clear();
×
544
    parent.transitions_.clear();
×
545
};
×
546

547
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target) {
116✔
548
    size_t target_index = target.size();
116✔
549
    if (&source == &target) {
116✔
550
        target_index--;
×
551
    }
×
552
    this->move_child(source, source_index, target, target_index);
116✔
553
};
116✔
554

555
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target, size_t target_index) {
145✔
556
    auto node_ptr = std::move(source.children_.at(source_index));
145✔
557
    auto trans_ptr = std::move(source.transitions_.at(source_index));
145✔
558
    source.children_.erase(source.children_.begin() + source_index);
145✔
559
    source.transitions_.erase(source.transitions_.begin() + source_index);
145✔
560

561
    node_ptr->parent_ = &target;
145✔
562
    trans_ptr->parent_ = &target;
145✔
563
    target.children_.insert(target.children_.begin() + target_index, std::move(node_ptr));
145✔
564
    target.transitions_.insert(target.transitions_.begin() + target_index, std::move(trans_ptr));
145✔
565
};
145✔
566

567
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target) {
210✔
568
    this->move_children(source, target, target.size());
210✔
569
};
210✔
570

571
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target, size_t target_index) {
253✔
572
    target.children_.insert(
253✔
573
        target.children_.begin() + target_index,
253✔
574
        std::make_move_iterator(source.children_.begin()),
253✔
575
        std::make_move_iterator(source.children_.end())
253✔
576
    );
253✔
577
    target.transitions_.insert(
253✔
578
        target.transitions_.begin() + target_index,
253✔
579
        std::make_move_iterator(source.transitions_.begin()),
253✔
580
        std::make_move_iterator(source.transitions_.end())
253✔
581
    );
253✔
582
    for (auto& child : target.children_) {
512✔
583
        child->parent_ = &target;
512✔
584
    }
512✔
585
    for (auto& trans : target.transitions_) {
512✔
586
        trans->parent_ = &target;
512✔
587
    }
512✔
588
    source.children_.clear();
253✔
589
    source.transitions_.clear();
253✔
590
};
253✔
591

592
Sequence& StructuredSDFGBuilder::hoist_root() {
×
593
    auto current_root = std::move(this->structured_sdfg_->root_);
×
594

595
    this->structured_sdfg_->root_ =
×
NEW
596
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), current_root->debug_info(), nullptr));
×
597

NEW
598
    current_root->parent_ = this->structured_sdfg_->root_.get();
×
599
    this->structured_sdfg_->root_->children_.push_back(std::move(current_root));
×
600
    this->structured_sdfg_->root_->transitions_.push_back(std::unique_ptr<Transition>(
×
601
        new Transition(this->new_element_id(), current_root->debug_info(), *this->structured_sdfg_->root_)
×
602
    ));
×
603
    return *this->structured_sdfg_->root_;
×
604
};
×
605

606
Block& StructuredSDFGBuilder::
607
    add_block(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
3,065✔
608
    return insert_block_internal(parent, INSERT_AT_END, nullptr, assignments, debug_info).first;
3,065✔
609
}
3,065✔
610

611
Block& StructuredSDFGBuilder::add_block(
612
    Sequence& parent,
613
    const data_flow::DataFlowGraph& data_flow_graph,
614
    const sdfg::control_flow::Assignments& assignments,
615
    const DebugInfo& debug_info
616
) {
19✔
617
    return insert_block_internal(parent, INSERT_AT_END, &data_flow_graph, assignments, debug_info).first;
19✔
618
}
19✔
619

620
std::pair<Block&, Transition&> StructuredSDFGBuilder::insert_block_internal(
621
    Sequence& parent,
622
    int32_t insert_idx,
623
    const data_flow::DataFlowGraph* import_from,
624
    const sdfg::control_flow::Assignments& assignments,
625
    const DebugInfo& debug_info
626
) {
3,399✔
627
    auto new_pair = insert_node_internal<Block>(parent, insert_idx, assignments, debug_info);
3,399✔
628

629
    if (import_from) {
3,399✔
630
        this->add_dataflow(*import_from, new_pair.first);
19✔
631
    }
19✔
632

633
    return new_pair;
3,399✔
634
}
3,399✔
635

636
Block& StructuredSDFGBuilder::add_block_before(
637
    Sequence& parent,
638
    ControlFlowNode& child,
639
    const sdfg::control_flow::Assignments& assignments,
640
    const DebugInfo& debug_info
641
) {
157✔
642
    int index = parent.index(child);
157✔
643
    if (index == -1) {
157✔
644
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
645
    }
×
646

647
    return insert_block_internal(parent, index, nullptr, assignments, debug_info).first;
157✔
648
};
157✔
649

650
Block& StructuredSDFGBuilder::add_block_before(
651
    Sequence& parent,
652
    ControlFlowNode& child,
653
    data_flow::DataFlowGraph& data_flow_graph,
654
    const sdfg::control_flow::Assignments& assignments,
655
    const DebugInfo& debug_info
656
) {
×
657
    int index = parent.index(child);
×
658
    if (index == -1) {
×
659
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
660
    }
×
661

NEW
662
    return insert_block_internal(parent, index, &data_flow_graph, assignments, debug_info).first;
×
663
};
×
664

665
Block& StructuredSDFGBuilder::add_block_after(
666
    Sequence& parent,
667
    ControlFlowNode& child,
668
    const sdfg::control_flow::Assignments& assignments,
669
    const DebugInfo& debug_info
670
) {
158✔
671
    int index = parent.index(child);
158✔
672
    if (index == -1) {
158✔
673
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
674
    }
×
675

676
    return insert_block_internal(parent, index + 1, nullptr, assignments, debug_info).first;
158✔
677
}
158✔
678

679
Block& StructuredSDFGBuilder::add_block_after(
680
    Sequence& parent,
681
    ControlFlowNode& child,
682
    data_flow::DataFlowGraph& data_flow_graph,
683
    const sdfg::control_flow::Assignments& assignments,
684
    const DebugInfo& debug_info
685
) {
×
686
    int index = parent.index(child);
×
687
    if (index == -1) {
×
688
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
689
    }
×
690

NEW
691
    return insert_block_internal(parent, index + 1, &data_flow_graph, assignments, debug_info).first;
×
NEW
692
}
×
693

694
std::pair<Block&, Transition&> StructuredSDFGBuilder::
695
    add_block_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
696
    int index = parent.index(child);
×
697
    if (index == -1) {
×
698
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
699
    }
×
700

NEW
701
    return insert_block_internal(parent, index, nullptr, {}, debug_info);
×
NEW
702
}
×
703

704
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
705
    Sequence& parent, ControlFlowNode& child, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
706
) {
×
707
    int index = parent.index(child);
×
708
    if (index == -1) {
×
709
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
710
    }
×
711

NEW
712
    return insert_block_internal(parent, index, &data_flow_graph, {}, debug_info);
×
NEW
713
}
×
714

715
std::pair<Block&, Transition&> StructuredSDFGBuilder::
716
    add_block_after(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
717
    int index = parent.index(child);
×
718
    if (index == -1) {
×
719
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
720
    }
×
721

NEW
722
    return insert_block_internal(parent, index + 1, nullptr, {}, debug_info);
×
NEW
723
}
×
724

725
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_after(
726
    Sequence& parent, ControlFlowNode& child, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
727
) {
×
728
    int index = parent.index(child);
×
729
    if (index == -1) {
×
730
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
731
    }
×
732

NEW
733
    return insert_block_internal(parent, index + 1, &data_flow_graph, {}, debug_info);
×
NEW
734
}
×
735

736
IfElse& StructuredSDFGBuilder::
737
    add_if_else(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
121✔
738
    return insert_node_internal<IfElse>(parent, INSERT_AT_END, assignments, debug_info).first;
121✔
739
}
121✔
740

741
IfElse& StructuredSDFGBuilder::add_if_else_before(
742
    Sequence& parent,
743
    ControlFlowNode& child,
744
    const sdfg::control_flow::Assignments& assignments,
745
    const DebugInfo& debug_info
746
) {
10✔
747
    int index = parent.index(child);
10✔
748
    if (index == -1) {
10✔
749
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
750
    }
×
751

752
    return insert_node_internal<IfElse>(parent, index, assignments, debug_info).first;
10✔
753
}
10✔
754

755
IfElse& StructuredSDFGBuilder::add_if_else_after(
756
    Sequence& parent,
757
    ControlFlowNode& child,
758
    const sdfg::control_flow::Assignments& assignments,
759
    const DebugInfo& debug_info
760
) {
×
761
    int index = parent.index(child);
×
762
    if (index == -1) {
×
763
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
764
    }
×
765

NEW
766
    return insert_node_internal<IfElse>(parent, index + 1, assignments, debug_info).first;
×
NEW
767
}
×
768

769
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::
770
    add_if_else_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
771
    int index = parent.index(child);
×
772
    if (index == -1) {
×
773
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
774
    }
×
775

NEW
776
    return insert_node_internal<IfElse>(parent, INSERT_AT_END, {}, debug_info);
×
777
};
×
778

779
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond, const DebugInfo& debug_info) {
228✔
780
    scope.cases_.push_back(std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info, &scope)));
228✔
781

782
    scope.conditions_.push_back(cond);
228✔
783
    return *scope.cases_.back();
228✔
784
};
228✔
785

786
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t index, const DebugInfo& debug_info) {
×
787
    scope.cases_.erase(scope.cases_.begin() + index);
×
788
    scope.conditions_.erase(scope.conditions_.begin() + index);
×
789
};
×
790

791
While& StructuredSDFGBuilder::
792
    add_while(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
53✔
793
    return insert_node_internal<While>(parent, INSERT_AT_END, assignments, debug_info).first;
53✔
794
};
53✔
795

796
For& StructuredSDFGBuilder::add_for(
797
    Sequence& parent,
798
    const symbolic::Symbol indvar,
799
    const symbolic::Condition condition,
800
    const symbolic::Expression init,
801
    const symbolic::Expression update,
802
    const sdfg::control_flow::Assignments& assignments,
803
    const DebugInfo& debug_info
804
) {
681✔
805
    return insert_node_internal<For>(parent, INSERT_AT_END, assignments, debug_info, indvar, init, update, condition)
681✔
806
        .first;
681✔
807
}
681✔
808

809
For& StructuredSDFGBuilder::add_for_before(
810
    Sequence& parent,
811
    ControlFlowNode& child,
812
    const symbolic::Symbol indvar,
813
    const symbolic::Condition condition,
814
    const symbolic::Expression init,
815
    const symbolic::Expression update,
816
    const sdfg::control_flow::Assignments& assignments,
817
    const DebugInfo& debug_info
818
) {
39✔
819
    int index = parent.index(child);
39✔
820
    if (index == -1) {
39✔
821
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
822
    }
×
823

824
    return insert_node_internal<For>(parent, index, assignments, debug_info, indvar, init, update, condition).first;
39✔
825
}
39✔
826

827
For& StructuredSDFGBuilder::add_for_after(
828
    Sequence& parent,
829
    ControlFlowNode& child,
830
    const symbolic::Symbol indvar,
831
    const symbolic::Condition condition,
832
    const symbolic::Expression init,
833
    const symbolic::Expression update,
834
    const sdfg::control_flow::Assignments& assignments,
835
    const DebugInfo& debug_info
836
) {
62✔
837
    int index = parent.index(child);
62✔
838
    if (index == -1) {
62✔
839
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
840
    }
×
841

842
    return insert_node_internal<For>(parent, index + 1, assignments, debug_info, indvar, init, update, condition).first;
62✔
843
}
62✔
844

845
Map& StructuredSDFGBuilder::add_map(
846
    Sequence& parent,
847
    const symbolic::Symbol indvar,
848
    const symbolic::Condition condition,
849
    const symbolic::Expression init,
850
    const symbolic::Expression update,
851
    const ScheduleType& schedule_type,
852
    const sdfg::control_flow::Assignments& assignments,
853
    const DebugInfo& debug_info
854
) {
1,974✔
855
    return insert_node_internal<
1,974✔
856
               Map>(parent, INSERT_AT_END, assignments, debug_info, indvar, init, update, condition, schedule_type)
1,974✔
857
        .first;
1,974✔
858
}
1,974✔
859

860
Map& StructuredSDFGBuilder::add_map_before(
861
    Sequence& parent,
862
    ControlFlowNode& child,
863
    const symbolic::Symbol indvar,
864
    const symbolic::Condition condition,
865
    const symbolic::Expression init,
866
    const symbolic::Expression update,
867
    const ScheduleType& schedule_type,
868
    const sdfg::control_flow::Assignments& assignments,
869
    const DebugInfo& debug_info
870
) {
70✔
871
    int index = parent.index(child);
70✔
872
    if (index == -1) {
70✔
873
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
874
    }
×
875

876
    return insert_node_internal<
70✔
877
               Map>(parent, index, assignments, debug_info, indvar, init, update, condition, schedule_type)
70✔
878
        .first;
70✔
879
}
70✔
880

881
Map& StructuredSDFGBuilder::add_map_after(
882
    Sequence& parent,
883
    ControlFlowNode& child,
884
    const symbolic::Symbol indvar,
885
    const symbolic::Condition condition,
886
    const symbolic::Expression init,
887
    const symbolic::Expression update,
888
    const ScheduleType& schedule_type,
889
    const sdfg::control_flow::Assignments& assignments,
890
    const DebugInfo& debug_info
891
) {
91✔
892
    int index = parent.index(child);
91✔
893
    if (index == -1) {
91✔
894
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
895
    }
×
896

897
    return insert_node_internal<
91✔
898
               Map>(parent, index + 1, assignments, debug_info, indvar, init, update, condition, schedule_type)
91✔
899
        .first;
91✔
900
}
91✔
901

902
Continue& StructuredSDFGBuilder::
903
    add_continue(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
27✔
904
    return insert_node_internal<Continue>(parent, INSERT_AT_END, assignments, debug_info).first;
27✔
905
}
27✔
906

907
Break& StructuredSDFGBuilder::
908
    add_break(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
28✔
909
    return insert_node_internal<Break>(parent, INSERT_AT_END, assignments, debug_info).first;
28✔
910
}
28✔
911

912
Return& StructuredSDFGBuilder::add_return(
913
    Sequence& parent,
914
    const std::string& data,
915
    const sdfg::control_flow::Assignments& assignments,
916
    const DebugInfo& debug_info
917
) {
56✔
918
    return insert_node_internal<Return>(parent, INSERT_AT_END, assignments, debug_info, data).first;
56✔
919
}
56✔
920

921
Return& StructuredSDFGBuilder::add_constant_return(
922
    Sequence& parent,
923
    const std::string& data,
924
    const types::IType& type,
925
    const sdfg::control_flow::Assignments& assignments,
926
    const DebugInfo& debug_info
927
) {
×
NEW
928
    return insert_node_internal<Return>(parent, INSERT_AT_END, assignments, debug_info, data, type).first;
×
NEW
929
}
×
930

931
For& StructuredSDFGBuilder::convert_while(
932
    Sequence& parent,
933
    While& loop,
934
    const symbolic::Symbol indvar,
935
    const symbolic::Condition condition,
936
    const symbolic::Expression init,
937
    const symbolic::Expression update
938
) {
×
939
    int index = parent.index(loop);
×
940
    if (index == -1) {
×
941
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
942
    }
×
943

944
    auto iter = parent.children_.begin() + index;
×
945
    auto& new_iter = *parent.children_.insert(
×
946
        iter + 1,
×
NEW
947
        std::unique_ptr<For>(new For(
×
NEW
948
            this->new_element_id_batch(For::REQUIRED_ELEMENT_IDS),
×
NEW
949
            loop.debug_info(),
×
NEW
950
            &parent,
×
NEW
951
            indvar,
×
NEW
952
            init,
×
NEW
953
            update,
×
NEW
954
            condition
×
NEW
955
        ))
×
UNCOV
956
    );
×
957

UNCOV
958
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
959
    this->move_children(loop.root(), for_loop.root());
×
960

961
    // Remove while loop
962
    parent.children_.erase(parent.children_.begin() + index);
×
963

964
    return for_loop;
×
965
};
×
966

967
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
77✔
968
    int index = parent.index(loop);
77✔
969
    if (index == -1) {
77✔
970
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
971
    }
×
972

973
    auto iter = parent.children_.begin() + index;
77✔
974
    auto& new_iter = *parent.children_.insert(
77✔
975
        iter + 1,
77✔
976
        std::unique_ptr<Map>(new Map(
77✔
977
            this->new_element_id_batch(Map::REQUIRED_ELEMENT_IDS),
77✔
978
            loop.debug_info(),
77✔
979
            &parent,
77✔
980
            loop.indvar(),
77✔
981
            loop.init(),
77✔
982
            loop.update(),
77✔
983
            loop.condition(),
77✔
984
            ScheduleType_Sequential::create()
77✔
985
        ))
77✔
986
    );
77✔
987

988
    auto& map = dynamic_cast<Map&>(*new_iter);
77✔
989
    this->move_children(loop.root(), map.root());
77✔
990

991
    // Remove for loop
992
    parent.children_.erase(parent.children_.begin() + index);
77✔
993

994
    return map;
77✔
995
};
77✔
996

997
void StructuredSDFGBuilder::update_if_else_condition(IfElse& if_else, size_t index, const symbolic::Condition condition) {
×
998
    if (index >= if_else.conditions_.size()) {
×
999
        throw InvalidSDFGException("StructuredSDFGBuilder: Index out of range");
×
1000
    }
×
1001
    if_else.conditions_.at(index) = condition;
×
1002
};
×
1003

1004
void StructuredSDFGBuilder::update_loop(
1005
    StructuredLoop& loop,
1006
    const symbolic::Symbol indvar,
1007
    const symbolic::Condition condition,
1008
    const symbolic::Expression init,
1009
    const symbolic::Expression update
1010
) {
115✔
1011
    loop.indvar_ = indvar;
115✔
1012
    loop.condition_ = condition;
115✔
1013
    loop.init_ = init;
115✔
1014
    loop.update_ = update;
115✔
1015
};
115✔
1016

1017
void StructuredSDFGBuilder::update_schedule_type(Map& map, const ScheduleType& schedule_type) {
18✔
1018
    map.schedule_type_ = schedule_type;
18✔
1019
}
18✔
1020

1021
/***** Section: Dataflow Graph *****/
1022

1023
data_flow::AccessNode& StructuredSDFGBuilder::
1024
    add_access(structured_control_flow::Block& block, const std::string& data, const DebugInfo& debug_info) {
6,707✔
1025
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
6,707✔
1026
    auto res = block.dataflow_->nodes_.insert(
6,707✔
1027
        {vertex,
6,707✔
1028
         std::unique_ptr<data_flow::AccessNode>(
6,707✔
1029
             new data_flow::AccessNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data)
6,707✔
1030
         )}
6,707✔
1031
    );
6,707✔
1032

1033
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
6,707✔
1034
};
6,707✔
1035

1036
data_flow::ConstantNode& StructuredSDFGBuilder::add_constant(
1037
    structured_control_flow::Block& block, const std::string& data, const types::IType& type, const DebugInfo& debug_info
1038
) {
401✔
1039
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
401✔
1040
    auto res = block.dataflow_->nodes_.insert(
401✔
1041
        {vertex,
401✔
1042
         std::unique_ptr<data_flow::ConstantNode>(
401✔
1043
             new data_flow::ConstantNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data, type)
401✔
1044
         )}
401✔
1045
    );
401✔
1046

1047
    return dynamic_cast<data_flow::ConstantNode&>(*(res.first->second));
401✔
1048
};
401✔
1049

1050

1051
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
1052
    structured_control_flow::Block& block,
1053
    const data_flow::TaskletCode code,
1054
    const std::string& output,
1055
    const std::vector<std::string>& inputs,
1056
    const DebugInfo& debug_info
1057
) {
1,482✔
1058
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
1,482✔
1059
    auto res = block.dataflow_->nodes_.insert(
1,482✔
1060
        {vertex,
1,482✔
1061
         std::unique_ptr<data_flow::Tasklet>(
1,482✔
1062
             new data_flow::Tasklet(this->new_element_id(), debug_info, vertex, block.dataflow(), code, output, inputs)
1,482✔
1063
         )}
1,482✔
1064
    );
1,482✔
1065

1066
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
1,482✔
1067
};
1,482✔
1068

1069
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
1070
    structured_control_flow::Block& block,
1071
    data_flow::DataFlowNode& src,
1072
    const std::string& src_conn,
1073
    data_flow::DataFlowNode& dst,
1074
    const std::string& dst_conn,
1075
    const data_flow::Subset& subset,
1076
    const types::IType& base_type,
1077
    const DebugInfo& debug_info
1078
) {
7,351✔
1079
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
7,351✔
1080
    auto res = block.dataflow_->edges_.insert(
7,351✔
1081
        {edge.first,
7,351✔
1082
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
7,351✔
1083
             this->new_element_id(), debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset, base_type
7,351✔
1084
         ))}
7,351✔
1085
    );
7,351✔
1086

1087
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
7,351✔
1088
#ifndef NDEBUG
7,351✔
1089
    memlet.validate(*this->structured_sdfg_);
7,351✔
1090
#endif
7,351✔
1091

1092
    return memlet;
7,351✔
1093
};
7,351✔
1094

1095
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1096
    structured_control_flow::Block& block,
1097
    data_flow::AccessNode& src,
1098
    data_flow::CodeNode& dst,
1099
    const std::string& dst_conn,
1100
    const data_flow::Subset& subset,
1101
    const types::IType& base_type,
1102
    const DebugInfo& debug_info
1103
) {
3,896✔
1104
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, base_type, debug_info);
3,896✔
1105
};
3,896✔
1106

1107
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1108
    structured_control_flow::Block& block,
1109
    data_flow::CodeNode& src,
1110
    const std::string& src_conn,
1111
    data_flow::AccessNode& dst,
1112
    const data_flow::Subset& subset,
1113
    const types::IType& base_type,
1114
    const DebugInfo& debug_info
1115
) {
1,307✔
1116
    return this->add_memlet(block, src, src_conn, dst, "void", subset, base_type, debug_info);
1,307✔
1117
};
1,307✔
1118

1119
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1120
    structured_control_flow::Block& block,
1121
    data_flow::AccessNode& src,
1122
    data_flow::Tasklet& dst,
1123
    const std::string& dst_conn,
1124
    const data_flow::Subset& subset,
1125
    const DebugInfo& debug_info
1126
) {
992✔
1127
    const types::IType* src_type = nullptr;
992✔
1128
    if (auto cnode = dynamic_cast<data_flow::ConstantNode*>(&src)) {
992✔
1129
        src_type = &cnode->type();
226✔
1130
    } else {
766✔
1131
        src_type = &this->structured_sdfg_->type(src.data());
766✔
1132
    }
766✔
1133
    auto base_type = types::infer_type(*this->structured_sdfg_, *src_type, subset);
992✔
1134
    if (base_type->type_id() != types::TypeID::Scalar) {
992✔
1135
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1136
    }
×
1137
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, *src_type, debug_info);
992✔
1138
};
992✔
1139

1140
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1141
    structured_control_flow::Block& block,
1142
    data_flow::Tasklet& src,
1143
    const std::string& src_conn,
1144
    data_flow::AccessNode& dst,
1145
    const data_flow::Subset& subset,
1146
    const DebugInfo& debug_info
1147
) {
617✔
1148
    auto& dst_type = this->structured_sdfg_->type(dst.data());
617✔
1149
    auto base_type = types::infer_type(*this->structured_sdfg_, dst_type, subset);
617✔
1150
    if (base_type->type_id() != types::TypeID::Scalar) {
617✔
1151
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1152
    }
×
1153
    return this->add_memlet(block, src, src_conn, dst, "void", subset, dst_type, debug_info);
617✔
1154
};
617✔
1155

1156
data_flow::Memlet& StructuredSDFGBuilder::add_reference_memlet(
1157
    structured_control_flow::Block& block,
1158
    data_flow::AccessNode& src,
1159
    data_flow::AccessNode& dst,
1160
    const data_flow::Subset& subset,
1161
    const types::IType& base_type,
1162
    const DebugInfo& debug_info
1163
) {
120✔
1164
    return this->add_memlet(block, src, "void", dst, "ref", subset, base_type, debug_info);
120✔
1165
};
120✔
1166

1167
data_flow::Memlet& StructuredSDFGBuilder::add_dereference_memlet(
1168
    structured_control_flow::Block& block,
1169
    data_flow::AccessNode& src,
1170
    data_flow::AccessNode& dst,
1171
    bool derefs_src,
1172
    const types::IType& base_type,
1173
    const DebugInfo& debug_info
1174
) {
23✔
1175
    if (derefs_src) {
23✔
1176
        return this->add_memlet(block, src, "void", dst, "deref", {symbolic::zero()}, base_type, debug_info);
14✔
1177
    } else {
14✔
1178
        return this->add_memlet(block, src, "deref", dst, "void", {symbolic::zero()}, base_type, debug_info);
9✔
1179
    }
9✔
1180
};
23✔
1181

1182
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block, const data_flow::Memlet& edge) {
223✔
1183
    auto& graph = block.dataflow();
223✔
1184
    auto e = edge.edge();
223✔
1185
    boost::remove_edge(e, graph.graph_);
223✔
1186
    graph.edges_.erase(e);
223✔
1187
};
223✔
1188

1189
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
190✔
1190
    auto& graph = block.dataflow();
190✔
1191
    auto v = node.vertex();
190✔
1192
    boost::remove_vertex(v, graph.graph_);
190✔
1193
    graph.nodes_.erase(v);
190✔
1194
};
190✔
1195

1196
void StructuredSDFGBuilder::clear_code_node_legacy(structured_control_flow::Block& block, const data_flow::CodeNode& node) {
592✔
1197
    auto& graph = block.dataflow();
592✔
1198

1199
    std::unordered_set<const data_flow::DataFlowNode*> to_delete = {&node};
592✔
1200

1201
    // Delete incoming
1202
    std::list<const data_flow::Memlet*> iedges;
592✔
1203
    for (auto& iedge : graph.in_edges(node)) {
1,458✔
1204
        iedges.push_back(&iedge);
1,458✔
1205
    }
1,458✔
1206
    for (auto iedge : iedges) {
1,458✔
1207
        auto& src = iedge->src();
1,458✔
1208
        to_delete.insert(&src);
1,458✔
1209

1210
        auto edge = iedge->edge();
1,458✔
1211
        graph.edges_.erase(edge);
1,458✔
1212
        boost::remove_edge(edge, graph.graph_);
1,458✔
1213
    }
1,458✔
1214

1215
    // Delete outgoing
1216
    std::list<const data_flow::Memlet*> oedges;
592✔
1217
    for (auto& oedge : graph.out_edges(node)) {
592✔
1218
        oedges.push_back(&oedge);
39✔
1219
    }
39✔
1220
    for (auto oedge : oedges) {
592✔
1221
        auto& dst = oedge->dst();
39✔
1222
        to_delete.insert(&dst);
39✔
1223

1224
        auto edge = oedge->edge();
39✔
1225
        graph.edges_.erase(edge);
39✔
1226
        boost::remove_edge(edge, graph.graph_);
39✔
1227
    }
39✔
1228

1229
    // Delete nodes
1230
    for (auto obsolete_node : to_delete) {
2,089✔
1231
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
2,089✔
1232
            auto vertex = obsolete_node->vertex();
2,089✔
1233
            graph.nodes_.erase(vertex);
2,089✔
1234
            boost::remove_vertex(vertex, graph.graph_);
2,089✔
1235
        }
2,089✔
1236
    }
2,089✔
1237
}
592✔
1238

1239
void StructuredSDFGBuilder::
1240
    clear_access_node_legacy(structured_control_flow::Block& block, const data_flow::AccessNode& node) {
3✔
1241
    auto& graph = block.dataflow();
3✔
1242

1243
    std::list<const data_flow::Memlet*> tmp;
3✔
1244
    std::list<const data_flow::DataFlowNode*> queue = {&node};
3✔
1245
    while (!queue.empty()) {
9✔
1246
        auto current = queue.front();
6✔
1247
        queue.pop_front();
6✔
1248
        if (current != &node) {
6✔
1249
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
3✔
1250
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1251
                    continue;
×
1252
                }
×
1253
            }
×
1254
        }
3✔
1255

1256
        tmp.clear();
6✔
1257
        for (auto& iedge : graph.in_edges(*current)) {
6✔
1258
            tmp.push_back(&iedge);
3✔
1259
        }
3✔
1260
        for (auto iedge : tmp) {
6✔
1261
            auto& src = iedge->src();
3✔
1262
            queue.push_back(&src);
3✔
1263

1264
            auto edge = iedge->edge();
3✔
1265
            graph.edges_.erase(edge);
3✔
1266
            boost::remove_edge(edge, graph.graph_);
3✔
1267
        }
3✔
1268

1269
        if (current != &node || graph.out_degree(*current) == 0) {
6✔
1270
            auto vertex = current->vertex();
6✔
1271
            graph.nodes_.erase(vertex);
6✔
1272
            boost::remove_vertex(vertex, graph.graph_);
6✔
1273
        }
6✔
1274
    }
6✔
1275
}
3✔
1276

1277
int StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
13✔
1278
    return clear_node(block, node, {&node});
13✔
1279
}
13✔
1280

1281
int StructuredSDFGBuilder::clear_node(
1282
    structured_control_flow::Block& block,
1283
    const data_flow::DataFlowNode& node,
1284
    const std::unordered_set<const data_flow::DataFlowNode*>& ignore_side_effects
1285
) {
13✔
1286
    auto& graph = block.dataflow();
13✔
1287

1288
    std::list<const data_flow::Memlet*> tmp;
13✔
1289
    std::list<const data_flow::DataFlowNode*> queue = {&node};
13✔
1290
    std::unordered_set<const data_flow::DataFlowNode*> remove_once_set;
13✔
1291
    std::unordered_set<const data_flow::DataFlowNode*> force_remove_set;
13✔
1292
    int removed_nodes = 0;
13✔
1293

1294
    do {
36✔
1295
        auto current = queue.front();
36✔
1296
        queue.pop_front();
36✔
1297

1298
        if (!remove_once_set.contains(current)) {
36✔
1299
            bool no_more_consumers = graph.out_degree(*current) == 0; // cannot remove nodes still in use
35✔
1300

1301
            auto* access_node = dynamic_cast<const data_flow::AccessNode*>(current);
35✔
1302

1303
            bool ignore_side_effects_on_current = ignore_side_effects.contains(current);
35✔
1304
            bool force_remove = force_remove_set.contains(current);
35✔
1305

1306
            // we can remove nodes without out-edges & side effects.
1307
            if ((no_more_consumers && !current->side_effect()) ||
35✔
1308
                ((ignore_side_effects_on_current || force_remove) && (no_more_consumers || access_node))) {
35✔
1309
                // Or for access-nodes on the ignore list, we can remove the write side (will not remove the node, only
1310
                // inputs) For any other node on the ignore list, we can ignore if it has side effects or not
1311
                tmp.clear();
28✔
1312
                for (auto& iedge : graph.in_edges(*current)) {
28✔
1313
                    tmp.push_back(&iedge);
25✔
1314
                }
25✔
1315
                bool no_in_edges = true;
28✔
1316
                for (auto iedge : tmp) {
28✔
1317
                    auto& src = iedge->src();
25✔
1318

1319
                    auto edge_rem = src.can_remove_out_edge(graph, iedge);
25✔
1320
                    if (edge_rem != data_flow::EdgeRemoveOption::NotRemovable) {
25✔
1321
                        auto src_conn = iedge->src_conn();
23✔
1322
                        queue.push_back(&src);
23✔
1323
                        auto edge = iedge->edge();
23✔
1324
                        graph.edges_.erase(edge);
23✔
1325
                        boost::remove_edge(edge, graph.graph_);
23✔
1326
                        if (edge_rem == data_flow::EdgeRemoveOption::RequiresUpdate) {
23✔
1327
                            const_cast<data_flow::DataFlowNode&>(src).update_edge_removed(src_conn);
×
1328
                        } else if (edge_rem == data_flow::EdgeRemoveOption::RemoveNodeAfter) {
23✔
1329
                            force_remove_set.insert(&src);
7✔
1330
                        }
7✔
1331
                    } else {
23✔
1332
                        no_in_edges = false;
2✔
1333
                    }
2✔
1334
                }
25✔
1335

1336
                if (no_more_consumers && no_in_edges) {
28✔
1337
                    remove_once_set.insert(current);
25✔
1338
                    auto vertex = current->vertex();
25✔
1339
                    graph.nodes_.erase(vertex);
25✔
1340
                    boost::remove_vertex(vertex, graph.graph_);
25✔
1341
                    ++removed_nodes;
25✔
1342
                } else if (!no_more_consumers && force_remove) {
25✔
1343
                    throw std::runtime_error(
×
1344
                        "Not yet supported: RemoveNodeAfter with more than 1 edge: #" +
×
1345
                        std::to_string(current->element_id())
×
1346
                    );
×
1347
                }
×
1348
            }
28✔
1349
        }
35✔
1350
    } while (!queue.empty());
36✔
1351

1352
    return removed_nodes;
13✔
1353
}
13✔
1354

1355
int StructuredSDFGBuilder::clear_ptr_borrow_edge(Block& block, const data_flow::Memlet& edge) {
1✔
1356
    auto& graph = block.dataflow();
1✔
1357
    auto& src = edge.src();
1✔
1358
    auto& dst = edge.dst();
1✔
1359

1360
    auto edge_removal = dst.can_remove_in_edge(graph, &edge);
1✔
1361
    int removed = 0;
1✔
1362
    if (edge_removal == data_flow::EdgeRemoveOption::Trivially) {
1✔
1363
        remove_memlet(block, edge);
×
1364
        ++removed;
×
1365
    } else if (edge_removal == data_flow::EdgeRemoveOption::RemoveNodeAfter) {
1✔
1366
        removed = 1 + clear_node(block, dst);
1✔
1367
    } else if (edge_removal == data_flow::EdgeRemoveOption::RequiresUpdate) {
1✔
1368
        remove_memlet(block, edge);
×
1369
        const_cast<data_flow::DataFlowNode&>(dst).update_edge_removed(edge.dst_conn());
×
1370
    } else if (edge_removal == data_flow::EdgeRemoveOption::NotRemovable) {
×
1371
        return 0;
×
1372
    }
×
1373

1374
    return removed;
1✔
1375
}
1✔
1376

1377
void StructuredSDFGBuilder::add_dataflow(const data_flow::DataFlowGraph& from, Block& to) {
19✔
1378
    auto& to_dataflow = to.dataflow();
19✔
1379

1380
    std::unordered_map<graph::Vertex, graph::Vertex> node_mapping;
19✔
1381
    for (auto& entry : from.nodes_) {
21✔
1382
        auto vertex = boost::add_vertex(to_dataflow.graph_);
21✔
1383
        to_dataflow.nodes_.insert({vertex, entry.second->clone(this->new_element_id(), vertex, to_dataflow)});
21✔
1384
        node_mapping.insert({entry.first, vertex});
21✔
1385
    }
21✔
1386

1387
    for (auto& entry : from.edges_) {
19✔
1388
        auto src = node_mapping[entry.second->src().vertex()];
14✔
1389
        auto dst = node_mapping[entry.second->dst().vertex()];
14✔
1390

1391
        auto edge = boost::add_edge(src, dst, to_dataflow.graph_);
14✔
1392

1393
        to_dataflow.edges_.insert(
14✔
1394
            {edge.first,
14✔
1395
             entry.second->clone(
14✔
1396
                 this->new_element_id(), edge.first, to_dataflow, *to_dataflow.nodes_[src], *to_dataflow.nodes_[dst]
14✔
1397
             )}
14✔
1398
        );
14✔
1399
    }
14✔
1400
};
19✔
1401

1402
void StructuredSDFGBuilder::merge_siblings(data_flow::AccessNode& source_node) {
21✔
1403
    auto& user_graph = source_node.get_parent();
21✔
1404
    auto* block = dynamic_cast<structured_control_flow::Block*>(user_graph.get_parent());
21✔
1405
    if (!block) {
21✔
1406
        throw InvalidSDFGException("Parent of user graph must be a block!");
×
1407
    }
×
1408

1409
    // Merge access nodes if they access the same container on a code node
1410
    for (auto& oedge : user_graph.out_edges(source_node)) {
21✔
1411
        if (auto* code_node = dynamic_cast<data_flow::CodeNode*>(&oedge.dst())) {
15✔
1412
            std::unordered_set<data_flow::Memlet*> iedges;
8✔
1413
            for (auto& iedge : user_graph.in_edges(*code_node)) {
10✔
1414
                iedges.insert(&iedge);
10✔
1415
            }
10✔
1416
            for (auto* iedge : iedges) {
10✔
1417
                if (dynamic_cast<data_flow::ConstantNode*>(&iedge->src())) {
10✔
1418
                    continue;
×
1419
                }
×
1420
                auto* access_node = static_cast<data_flow::AccessNode*>(&iedge->src());
10✔
1421
                if (access_node == &source_node || access_node->data() != source_node.data()) {
10✔
1422
                    continue;
9✔
1423
                }
9✔
1424
                this->add_memlet(
1✔
1425
                    *block,
1✔
1426
                    source_node,
1✔
1427
                    iedge->src_conn(),
1✔
1428
                    *code_node,
1✔
1429
                    iedge->dst_conn(),
1✔
1430
                    iedge->subset(),
1✔
1431
                    iedge->base_type(),
1✔
1432
                    iedge->debug_info()
1✔
1433
                );
1✔
1434
                this->remove_memlet(*block, *iedge);
1✔
1435
                source_node.set_debug_info(DebugInfo::merge(source_node.debug_info(), access_node->debug_info()));
1✔
1436
            }
1✔
1437
        }
8✔
1438
    }
15✔
1439

1440
    // Also merge "output" access nodes if they access the same container on a library node
1441
    for (auto& iedge : user_graph.in_edges(source_node)) {
21✔
1442
        if (auto* libnode = dynamic_cast<data_flow::LibraryNode*>(&iedge.src())) {
7✔
1443
            std::unordered_set<data_flow::Memlet*> oedges;
×
1444
            for (auto& oedge : user_graph.out_edges(*libnode)) {
×
1445
                oedges.insert(&oedge);
×
1446
            }
×
1447
            for (auto* oedge : oedges) {
×
1448
                auto* access_node = static_cast<data_flow::AccessNode*>(&oedge->dst());
×
1449
                if (access_node == &source_node || access_node->data() != source_node.data()) {
×
1450
                    continue;
×
1451
                }
×
1452
                this->add_memlet(
×
1453
                    *block,
×
1454
                    *libnode,
×
1455
                    oedge->src_conn(),
×
1456
                    source_node,
×
1457
                    oedge->dst_conn(),
×
1458
                    oedge->subset(),
×
1459
                    oedge->base_type(),
×
1460
                    oedge->debug_info()
×
1461
                );
×
1462
                this->remove_memlet(*block, *oedge);
×
1463
                source_node.set_debug_info(DebugInfo::merge(source_node.debug_info(), access_node->debug_info()));
×
1464
            }
×
1465
        }
×
1466
    }
7✔
1467
}
21✔
1468

1469
} // namespace builder
1470
} // 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