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

daisytuner / docc / 28131092262

24 Jun 2026 09:34PM UTC coverage: 61.733% (+0.2%) from 61.582%
28131092262

Pull #802

github

web-flow
Merge 8407649e8 into 0a5f3e8d4
Pull Request #802: adds reduce as new structured loop type

604 of 780 new or added lines in 24 files covered. (77.44%)

4 existing lines in 3 files now uncovered.

38041 of 61622 relevant lines covered (61.73%)

1001.29 hits per line

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

74.49
/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_); };
55,685✔
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)) {};
283✔
390

391
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
392
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type), owned(true)) {};
1,842✔
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,879✔
429

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

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

439
    return std::move(std::unique_ptr<StructuredSDFG>(structured_sdfg_.release()));
548✔
440
};
548✔
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 {
69✔
449
    auto& sdfg = this->subject();
69✔
450
    std::list<Element*> queue = {&sdfg.root()};
69✔
451
    while (!queue.empty()) {
297✔
452
        auto current = queue.front();
294✔
453
        queue.pop_front();
294✔
454

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

459
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
228✔
460
            auto& dataflow = block_stmt->dataflow();
13✔
461
            for (auto& node : dataflow.nodes()) {
44✔
462
                queue.push_back(&node);
44✔
463
            }
44✔
464
            for (auto& edge : dataflow.edges()) {
31✔
465
                queue.push_back(&edge);
31✔
466
            }
31✔
467
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
215✔
468
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
217✔
469
                queue.push_back(&sequence_stmt->at(i).first);
109✔
470
                queue.push_back(&sequence_stmt->at(i).second);
109✔
471
            }
109✔
472
        } else if (dynamic_cast<structured_control_flow::Return*>(current)) {
108✔
473
            // Do nothing
474
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
107✔
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)) {
107✔
479
            queue.push_back(&for_stmt->root());
7✔
480
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
100✔
481
            queue.push_back(&while_stmt->root());
×
482
        } else if (dynamic_cast<structured_control_flow::Continue*>(current)) {
100✔
483
            // Do nothing
484
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
100✔
485
            // Do nothing
486
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
100✔
487
            queue.push_back(&map_stmt->root());
35✔
488
        } else if (auto reduce_stmt = dynamic_cast<structured_control_flow::Reduce*>(current)) {
65✔
NEW
489
            queue.push_back(&reduce_stmt->root());
×
UNCOV
490
        }
×
491
    }
228✔
492

493
    return nullptr;
3✔
494
};
69✔
495

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

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

512
    return insert_node_internal<Sequence>(parent, index, assignments, debug_info).first;
614✔
513
}
614✔
514

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

526
    return insert_node_internal<Sequence>(parent, index + 1, assignments, debug_info).first;
24✔
527
}
24✔
528

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

536
    return insert_node_internal<Sequence>(parent, index, {}, debug_info);
×
537
}
×
538

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

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

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

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

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

569
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target) {
231✔
570
    this->move_children(source, target, target.size());
231✔
571
};
231✔
572

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

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

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

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

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

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

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

631
    if (import_from) {
3,597✔
632
        this->add_dataflow(*import_from, new_pair.first);
19✔
633
    }
19✔
634

635
    return new_pair;
3,597✔
636
}
3,597✔
637

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

649
    return insert_block_internal(parent, index, nullptr, assignments, debug_info).first;
187✔
650
};
187✔
651

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

664
    return insert_block_internal(parent, index, &data_flow_graph, assignments, debug_info).first;
×
665
};
×
666

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

678
    return insert_block_internal(parent, index + 1, nullptr, assignments, debug_info).first;
176✔
679
}
176✔
680

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

693
    return insert_block_internal(parent, index + 1, &data_flow_graph, assignments, debug_info).first;
×
694
}
×
695

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

703
    return insert_block_internal(parent, index, nullptr, {}, debug_info);
×
704
}
×
705

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

714
    return insert_block_internal(parent, index, &data_flow_graph, {}, debug_info);
×
715
}
×
716

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

724
    return insert_block_internal(parent, index + 1, nullptr, {}, debug_info);
×
725
}
×
726

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

735
    return insert_block_internal(parent, index + 1, &data_flow_graph, {}, debug_info);
×
736
}
×
737

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

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

754
    return insert_node_internal<IfElse>(parent, index, assignments, debug_info).first;
18✔
755
}
18✔
756

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

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

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

778
    return insert_node_internal<IfElse>(parent, INSERT_AT_END, {}, debug_info);
×
779
};
×
780

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

784
    scope.conditions_.push_back(cond);
259✔
785
    return *scope.cases_.back();
259✔
786
};
259✔
787

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

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

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

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

826
    return insert_node_internal<For>(parent, index, assignments, debug_info, indvar, init, update, condition).first;
40✔
827
}
40✔
828

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

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

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

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

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

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

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

904
Reduce& StructuredSDFGBuilder::add_reduce(
905
    Sequence& parent,
906
    const symbolic::Symbol indvar,
907
    const symbolic::Condition condition,
908
    const symbolic::Expression init,
909
    const symbolic::Expression update,
910
    const std::vector<structured_control_flow::ReductionInfo>& reductions,
911
    const ScheduleType& schedule_type,
912
    const sdfg::control_flow::Assignments& assignments,
913
    const DebugInfo& debug_info
914
) {
14✔
915
    return insert_node_internal<Reduce>(
14✔
916
               parent, INSERT_AT_END, assignments, debug_info, indvar, init, update, condition, reductions, schedule_type
14✔
917
    )
14✔
918
        .first;
14✔
919
}
14✔
920

921
Reduce& StructuredSDFGBuilder::add_reduce_before(
922
    Sequence& parent,
923
    ControlFlowNode& child,
924
    const symbolic::Symbol indvar,
925
    const symbolic::Condition condition,
926
    const symbolic::Expression init,
927
    const symbolic::Expression update,
928
    const std::vector<structured_control_flow::ReductionInfo>& reductions,
929
    const ScheduleType& schedule_type,
930
    const sdfg::control_flow::Assignments& assignments,
931
    const DebugInfo& debug_info
NEW
932
) {
×
NEW
933
    int index = parent.index(child);
×
NEW
934
    if (index == -1) {
×
NEW
935
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
NEW
936
    }
×
937

NEW
938
    return insert_node_internal<
×
NEW
939
               Reduce>(parent, index, assignments, debug_info, indvar, init, update, condition, reductions, schedule_type)
×
NEW
940
        .first;
×
NEW
941
}
×
942

943
Reduce& StructuredSDFGBuilder::add_reduce_after(
944
    Sequence& parent,
945
    ControlFlowNode& child,
946
    const symbolic::Symbol indvar,
947
    const symbolic::Condition condition,
948
    const symbolic::Expression init,
949
    const symbolic::Expression update,
950
    const std::vector<structured_control_flow::ReductionInfo>& reductions,
951
    const ScheduleType& schedule_type,
952
    const sdfg::control_flow::Assignments& assignments,
953
    const DebugInfo& debug_info
NEW
954
) {
×
NEW
955
    int index = parent.index(child);
×
NEW
956
    if (index == -1) {
×
NEW
957
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
NEW
958
    }
×
959

NEW
960
    return insert_node_internal<Reduce>(
×
NEW
961
               parent, index + 1, assignments, debug_info, indvar, init, update, condition, reductions, schedule_type
×
NEW
962
    )
×
NEW
963
        .first;
×
NEW
964
}
×
965

966
Continue& StructuredSDFGBuilder::
967
    add_continue(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
27✔
968
    return insert_node_internal<Continue>(parent, INSERT_AT_END, assignments, debug_info).first;
27✔
969
}
27✔
970

971
Break& StructuredSDFGBuilder::
972
    add_break(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
28✔
973
    return insert_node_internal<Break>(parent, INSERT_AT_END, assignments, debug_info).first;
28✔
974
}
28✔
975

976
Return& StructuredSDFGBuilder::add_return(
977
    Sequence& parent,
978
    const std::string& data,
979
    const sdfg::control_flow::Assignments& assignments,
980
    const DebugInfo& debug_info
981
) {
56✔
982
    return insert_node_internal<Return>(parent, INSERT_AT_END, assignments, debug_info, data).first;
56✔
983
}
56✔
984

985
Return& StructuredSDFGBuilder::add_constant_return(
986
    Sequence& parent,
987
    const std::string& data,
988
    const types::IType& type,
989
    const sdfg::control_flow::Assignments& assignments,
990
    const DebugInfo& debug_info
991
) {
×
992
    return insert_node_internal<Return>(parent, INSERT_AT_END, assignments, debug_info, data, type).first;
×
993
}
×
994

995
For& StructuredSDFGBuilder::convert_while(
996
    Sequence& parent,
997
    While& loop,
998
    const symbolic::Symbol indvar,
999
    const symbolic::Condition condition,
1000
    const symbolic::Expression init,
1001
    const symbolic::Expression update
1002
) {
×
1003
    int index = parent.index(loop);
×
1004
    if (index == -1) {
×
1005
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1006
    }
×
1007

1008
    auto iter = parent.children_.begin() + index;
×
1009
    auto& new_iter = *parent.children_.insert(
×
1010
        iter + 1,
×
1011
        std::unique_ptr<For>(new For(
×
1012
            this->new_element_id_batch(For::REQUIRED_ELEMENT_IDS),
×
1013
            loop.debug_info(),
×
1014
            &parent,
×
1015
            indvar,
×
1016
            init,
×
1017
            update,
×
1018
            condition
×
1019
        ))
×
1020
    );
×
1021

1022
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
1023
    this->move_children(loop.root(), for_loop.root());
×
1024

1025
    // Remove while loop
1026
    parent.children_.erase(parent.children_.begin() + index);
×
1027

1028
    return for_loop;
×
1029
};
×
1030

1031
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
77✔
1032
    int index = parent.index(loop);
77✔
1033
    if (index == -1) {
77✔
1034
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1035
    }
×
1036

1037
    auto iter = parent.children_.begin() + index;
77✔
1038
    auto& new_iter = *parent.children_.insert(
77✔
1039
        iter + 1,
77✔
1040
        std::unique_ptr<Map>(new Map(
77✔
1041
            this->new_element_id_batch(Map::REQUIRED_ELEMENT_IDS),
77✔
1042
            loop.debug_info(),
77✔
1043
            &parent,
77✔
1044
            loop.indvar(),
77✔
1045
            loop.init(),
77✔
1046
            loop.update(),
77✔
1047
            loop.condition(),
77✔
1048
            ScheduleType_Sequential::create()
77✔
1049
        ))
77✔
1050
    );
77✔
1051

1052
    auto& map = dynamic_cast<Map&>(*new_iter);
77✔
1053
    this->move_children(loop.root(), map.root());
77✔
1054

1055
    // Remove for loop
1056
    parent.children_.erase(parent.children_.begin() + index);
77✔
1057

1058
    return map;
77✔
1059
};
77✔
1060

1061
Reduce& StructuredSDFGBuilder::convert_for_to_reduce(
1062
    Sequence& parent, For& loop, const std::vector<structured_control_flow::ReductionInfo>& reductions
1063
) {
10✔
1064
    int index = parent.index(loop);
10✔
1065
    if (index == -1) {
10✔
NEW
1066
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
NEW
1067
    }
×
1068

1069
    auto iter = parent.children_.begin() + index;
10✔
1070
    auto& new_iter = *parent.children_.insert(
10✔
1071
        iter + 1,
10✔
1072
        std::unique_ptr<Reduce>(new Reduce(
10✔
1073
            this->new_element_id_batch(Reduce::REQUIRED_ELEMENT_IDS),
10✔
1074
            loop.debug_info(),
10✔
1075
            &parent,
10✔
1076
            loop.indvar(),
10✔
1077
            loop.init(),
10✔
1078
            loop.update(),
10✔
1079
            loop.condition(),
10✔
1080
            reductions,
10✔
1081
            ScheduleType_Sequential::create()
10✔
1082
        ))
10✔
1083
    );
10✔
1084

1085
    auto& reduce = dynamic_cast<Reduce&>(*new_iter);
10✔
1086
    this->move_children(loop.root(), reduce.root());
10✔
1087

1088
    // Remove for loop
1089
    parent.children_.erase(parent.children_.begin() + index);
10✔
1090

1091
    return reduce;
10✔
1092
};
10✔
1093

1094
void StructuredSDFGBuilder::update_if_else_condition(IfElse& if_else, size_t index, const symbolic::Condition condition) {
×
1095
    if (index >= if_else.conditions_.size()) {
×
1096
        throw InvalidSDFGException("StructuredSDFGBuilder: Index out of range");
×
1097
    }
×
1098
    if_else.conditions_.at(index) = condition;
×
1099
};
×
1100

1101
void StructuredSDFGBuilder::update_loop(
1102
    StructuredLoop& loop,
1103
    const symbolic::Symbol indvar,
1104
    const symbolic::Condition condition,
1105
    const symbolic::Expression init,
1106
    const symbolic::Expression update
1107
) {
119✔
1108
    loop.indvar_ = indvar;
119✔
1109
    loop.condition_ = condition;
119✔
1110
    loop.init_ = init;
119✔
1111
    loop.update_ = update;
119✔
1112
};
119✔
1113

1114
void StructuredSDFGBuilder::update_schedule_type(StructuredLoop& loop, const ScheduleType& schedule_type) {
36✔
1115
    loop.schedule_type_ = schedule_type;
36✔
1116
}
36✔
1117

1118
/***** Section: Dataflow Graph *****/
1119

1120
data_flow::AccessNode& StructuredSDFGBuilder::
1121
    add_access(structured_control_flow::Block& block, const std::string& data, const DebugInfo& debug_info) {
7,111✔
1122
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
7,111✔
1123
    auto res = block.dataflow_->nodes_.insert(
7,111✔
1124
        {vertex,
7,111✔
1125
         std::unique_ptr<data_flow::AccessNode>(
7,111✔
1126
             new data_flow::AccessNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data)
7,111✔
1127
         )}
7,111✔
1128
    );
7,111✔
1129

1130
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
7,111✔
1131
};
7,111✔
1132

1133
data_flow::ConstantNode& StructuredSDFGBuilder::add_constant(
1134
    structured_control_flow::Block& block, const std::string& data, const types::IType& type, const DebugInfo& debug_info
1135
) {
422✔
1136
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
422✔
1137
    auto res = block.dataflow_->nodes_.insert(
422✔
1138
        {vertex,
422✔
1139
         std::unique_ptr<data_flow::ConstantNode>(
422✔
1140
             new data_flow::ConstantNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data, type)
422✔
1141
         )}
422✔
1142
    );
422✔
1143

1144
    return dynamic_cast<data_flow::ConstantNode&>(*(res.first->second));
422✔
1145
};
422✔
1146

1147

1148
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
1149
    structured_control_flow::Block& block,
1150
    const data_flow::TaskletCode code,
1151
    const std::string& output,
1152
    const std::vector<std::string>& inputs,
1153
    const DebugInfo& debug_info
1154
) {
1,592✔
1155
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
1,592✔
1156
    auto res = block.dataflow_->nodes_.insert(
1,592✔
1157
        {vertex,
1,592✔
1158
         std::unique_ptr<data_flow::Tasklet>(
1,592✔
1159
             new data_flow::Tasklet(this->new_element_id(), debug_info, vertex, block.dataflow(), code, output, inputs)
1,592✔
1160
         )}
1,592✔
1161
    );
1,592✔
1162

1163
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
1,592✔
1164
};
1,592✔
1165

1166
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
1167
    structured_control_flow::Block& block,
1168
    data_flow::DataFlowNode& src,
1169
    const std::string& src_conn,
1170
    data_flow::DataFlowNode& dst,
1171
    const std::string& dst_conn,
1172
    const data_flow::Subset& subset,
1173
    const types::IType& base_type,
1174
    const DebugInfo& debug_info
1175
) {
7,796✔
1176
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
7,796✔
1177
    auto res = block.dataflow_->edges_.insert(
7,796✔
1178
        {edge.first,
7,796✔
1179
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
7,796✔
1180
             this->new_element_id(), debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset, base_type
7,796✔
1181
         ))}
7,796✔
1182
    );
7,796✔
1183

1184
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
7,796✔
1185
#ifndef NDEBUG
7,796✔
1186
    memlet.validate(*this->structured_sdfg_);
7,796✔
1187
#endif
7,796✔
1188

1189
    return memlet;
7,796✔
1190
};
7,796✔
1191

1192
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1193
    structured_control_flow::Block& block,
1194
    data_flow::AccessNode& src,
1195
    data_flow::CodeNode& dst,
1196
    const std::string& dst_conn,
1197
    const data_flow::Subset& subset,
1198
    const types::IType& base_type,
1199
    const DebugInfo& debug_info
1200
) {
4,153✔
1201
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, base_type, debug_info);
4,153✔
1202
};
4,153✔
1203

1204
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1205
    structured_control_flow::Block& block,
1206
    data_flow::CodeNode& src,
1207
    const std::string& src_conn,
1208
    data_flow::AccessNode& dst,
1209
    const data_flow::Subset& subset,
1210
    const types::IType& base_type,
1211
    const DebugInfo& debug_info
1212
) {
1,429✔
1213
    return this->add_memlet(block, src, src_conn, dst, "void", subset, base_type, debug_info);
1,429✔
1214
};
1,429✔
1215

1216
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1217
    structured_control_flow::Block& block,
1218
    data_flow::AccessNode& src,
1219
    data_flow::Tasklet& dst,
1220
    const std::string& dst_conn,
1221
    const data_flow::Subset& subset,
1222
    const DebugInfo& debug_info
1223
) {
1,018✔
1224
    const types::IType* src_type = nullptr;
1,018✔
1225
    if (auto cnode = dynamic_cast<data_flow::ConstantNode*>(&src)) {
1,018✔
1226
        src_type = &cnode->type();
235✔
1227
    } else {
783✔
1228
        src_type = &this->structured_sdfg_->type(src.data());
783✔
1229
    }
783✔
1230
    auto base_type = types::infer_type(*this->structured_sdfg_, *src_type, subset);
1,018✔
1231
    if (base_type->type_id() != types::TypeID::Scalar) {
1,018✔
1232
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1233
    }
×
1234
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, *src_type, debug_info);
1,018✔
1235
};
1,018✔
1236

1237
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1238
    structured_control_flow::Block& block,
1239
    data_flow::Tasklet& src,
1240
    const std::string& src_conn,
1241
    data_flow::AccessNode& dst,
1242
    const data_flow::Subset& subset,
1243
    const DebugInfo& debug_info
1244
) {
635✔
1245
    auto& dst_type = this->structured_sdfg_->type(dst.data());
635✔
1246
    auto base_type = types::infer_type(*this->structured_sdfg_, dst_type, subset);
635✔
1247
    if (base_type->type_id() != types::TypeID::Scalar) {
635✔
1248
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1249
    }
×
1250
    return this->add_memlet(block, src, src_conn, dst, "void", subset, dst_type, debug_info);
635✔
1251
};
635✔
1252

1253
data_flow::Memlet& StructuredSDFGBuilder::add_reference_memlet(
1254
    structured_control_flow::Block& block,
1255
    data_flow::AccessNode& src,
1256
    data_flow::AccessNode& dst,
1257
    const data_flow::Subset& subset,
1258
    const types::IType& base_type,
1259
    const DebugInfo& debug_info
1260
) {
120✔
1261
    return this->add_memlet(block, src, "void", dst, "ref", subset, base_type, debug_info);
120✔
1262
};
120✔
1263

1264
data_flow::Memlet& StructuredSDFGBuilder::add_dereference_memlet(
1265
    structured_control_flow::Block& block,
1266
    data_flow::AccessNode& src,
1267
    data_flow::AccessNode& dst,
1268
    bool derefs_src,
1269
    const types::IType& base_type,
1270
    const DebugInfo& debug_info
1271
) {
23✔
1272
    if (derefs_src) {
23✔
1273
        return this->add_memlet(block, src, "void", dst, "deref", {symbolic::zero()}, base_type, debug_info);
14✔
1274
    } else {
14✔
1275
        return this->add_memlet(block, src, "deref", dst, "void", {symbolic::zero()}, base_type, debug_info);
9✔
1276
    }
9✔
1277
};
23✔
1278

1279
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block, const data_flow::Memlet& edge) {
228✔
1280
    auto& graph = block.dataflow();
228✔
1281
    auto e = edge.edge();
228✔
1282
    boost::remove_edge(e, graph.graph_);
228✔
1283
    graph.edges_.erase(e);
228✔
1284
};
228✔
1285

1286
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
192✔
1287
    auto& graph = block.dataflow();
192✔
1288
    auto v = node.vertex();
192✔
1289
    boost::remove_vertex(v, graph.graph_);
192✔
1290
    graph.nodes_.erase(v);
192✔
1291
};
192✔
1292

1293
void StructuredSDFGBuilder::clear_code_node_legacy(structured_control_flow::Block& block, const data_flow::CodeNode& node) {
602✔
1294
    auto& graph = block.dataflow();
602✔
1295

1296
    std::unordered_set<const data_flow::DataFlowNode*> to_delete = {&node};
602✔
1297

1298
    // Delete incoming
1299
    std::list<const data_flow::Memlet*> iedges;
602✔
1300
    for (auto& iedge : graph.in_edges(node)) {
1,492✔
1301
        iedges.push_back(&iedge);
1,492✔
1302
    }
1,492✔
1303
    for (auto iedge : iedges) {
1,492✔
1304
        auto& src = iedge->src();
1,492✔
1305
        to_delete.insert(&src);
1,492✔
1306

1307
        auto edge = iedge->edge();
1,492✔
1308
        graph.edges_.erase(edge);
1,492✔
1309
        boost::remove_edge(edge, graph.graph_);
1,492✔
1310
    }
1,492✔
1311

1312
    // Delete outgoing
1313
    std::list<const data_flow::Memlet*> oedges;
602✔
1314
    for (auto& oedge : graph.out_edges(node)) {
602✔
1315
        oedges.push_back(&oedge);
39✔
1316
    }
39✔
1317
    for (auto oedge : oedges) {
602✔
1318
        auto& dst = oedge->dst();
39✔
1319
        to_delete.insert(&dst);
39✔
1320

1321
        auto edge = oedge->edge();
39✔
1322
        graph.edges_.erase(edge);
39✔
1323
        boost::remove_edge(edge, graph.graph_);
39✔
1324
    }
39✔
1325

1326
    // Delete nodes
1327
    for (auto obsolete_node : to_delete) {
2,133✔
1328
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
2,133✔
1329
            auto vertex = obsolete_node->vertex();
2,133✔
1330
            graph.nodes_.erase(vertex);
2,133✔
1331
            boost::remove_vertex(vertex, graph.graph_);
2,133✔
1332
        }
2,133✔
1333
    }
2,133✔
1334
}
602✔
1335

1336
void StructuredSDFGBuilder::
1337
    clear_access_node_legacy(structured_control_flow::Block& block, const data_flow::AccessNode& node) {
3✔
1338
    auto& graph = block.dataflow();
3✔
1339

1340
    std::list<const data_flow::Memlet*> tmp;
3✔
1341
    std::list<const data_flow::DataFlowNode*> queue = {&node};
3✔
1342
    while (!queue.empty()) {
9✔
1343
        auto current = queue.front();
6✔
1344
        queue.pop_front();
6✔
1345
        if (current != &node) {
6✔
1346
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
3✔
1347
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1348
                    continue;
×
1349
                }
×
1350
            }
×
1351
        }
3✔
1352

1353
        tmp.clear();
6✔
1354
        for (auto& iedge : graph.in_edges(*current)) {
6✔
1355
            tmp.push_back(&iedge);
3✔
1356
        }
3✔
1357
        for (auto iedge : tmp) {
6✔
1358
            auto& src = iedge->src();
3✔
1359
            queue.push_back(&src);
3✔
1360

1361
            auto edge = iedge->edge();
3✔
1362
            graph.edges_.erase(edge);
3✔
1363
            boost::remove_edge(edge, graph.graph_);
3✔
1364
        }
3✔
1365

1366
        if (current != &node || graph.out_degree(*current) == 0) {
6✔
1367
            auto vertex = current->vertex();
6✔
1368
            graph.nodes_.erase(vertex);
6✔
1369
            boost::remove_vertex(vertex, graph.graph_);
6✔
1370
        }
6✔
1371
    }
6✔
1372
}
3✔
1373

1374
int StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
13✔
1375
    return clear_node(block, node, {&node});
13✔
1376
}
13✔
1377

1378
int StructuredSDFGBuilder::clear_node(
1379
    structured_control_flow::Block& block,
1380
    const data_flow::DataFlowNode& node,
1381
    const std::unordered_set<const data_flow::DataFlowNode*>& ignore_side_effects
1382
) {
13✔
1383
    auto& graph = block.dataflow();
13✔
1384

1385
    std::list<const data_flow::Memlet*> tmp;
13✔
1386
    std::list<const data_flow::DataFlowNode*> queue = {&node};
13✔
1387
    std::unordered_set<const data_flow::DataFlowNode*> remove_once_set;
13✔
1388
    std::unordered_set<const data_flow::DataFlowNode*> force_remove_set;
13✔
1389
    int removed_nodes = 0;
13✔
1390

1391
    do {
36✔
1392
        auto current = queue.front();
36✔
1393
        queue.pop_front();
36✔
1394

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

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

1400
            bool ignore_side_effects_on_current = ignore_side_effects.contains(current);
35✔
1401
            bool force_remove = force_remove_set.contains(current);
35✔
1402

1403
            // we can remove nodes without out-edges & side effects.
1404
            if ((no_more_consumers && !current->side_effect()) ||
35✔
1405
                ((ignore_side_effects_on_current || force_remove) && (no_more_consumers || access_node))) {
35✔
1406
                // Or for access-nodes on the ignore list, we can remove the write side (will not remove the node, only
1407
                // inputs) For any other node on the ignore list, we can ignore if it has side effects or not
1408
                tmp.clear();
28✔
1409
                for (auto& iedge : graph.in_edges(*current)) {
28✔
1410
                    tmp.push_back(&iedge);
25✔
1411
                }
25✔
1412
                bool no_in_edges = true;
28✔
1413
                for (auto iedge : tmp) {
28✔
1414
                    auto& src = iedge->src();
25✔
1415

1416
                    auto edge_rem = src.can_remove_out_edge(graph, iedge);
25✔
1417
                    if (edge_rem != data_flow::EdgeRemoveOption::NotRemovable) {
25✔
1418
                        auto src_conn = iedge->src_conn();
23✔
1419
                        queue.push_back(&src);
23✔
1420
                        auto edge = iedge->edge();
23✔
1421
                        graph.edges_.erase(edge);
23✔
1422
                        boost::remove_edge(edge, graph.graph_);
23✔
1423
                        if (edge_rem == data_flow::EdgeRemoveOption::RequiresUpdate) {
23✔
1424
                            const_cast<data_flow::DataFlowNode&>(src).update_edge_removed(src_conn);
×
1425
                        } else if (edge_rem == data_flow::EdgeRemoveOption::RemoveNodeAfter) {
23✔
1426
                            force_remove_set.insert(&src);
7✔
1427
                        }
7✔
1428
                    } else {
23✔
1429
                        no_in_edges = false;
2✔
1430
                    }
2✔
1431
                }
25✔
1432

1433
                if (no_more_consumers && no_in_edges) {
28✔
1434
                    remove_once_set.insert(current);
25✔
1435
                    auto vertex = current->vertex();
25✔
1436
                    graph.nodes_.erase(vertex);
25✔
1437
                    boost::remove_vertex(vertex, graph.graph_);
25✔
1438
                    ++removed_nodes;
25✔
1439
                } else if (!no_more_consumers && force_remove) {
25✔
1440
                    throw std::runtime_error(
×
1441
                        "Not yet supported: RemoveNodeAfter with more than 1 edge: #" +
×
1442
                        std::to_string(current->element_id())
×
1443
                    );
×
1444
                }
×
1445
            }
28✔
1446
        }
35✔
1447
    } while (!queue.empty());
36✔
1448

1449
    return removed_nodes;
13✔
1450
}
13✔
1451

1452
int StructuredSDFGBuilder::clear_ptr_borrow_edge(Block& block, const data_flow::Memlet& edge) {
1✔
1453
    auto& graph = block.dataflow();
1✔
1454
    auto& src = edge.src();
1✔
1455
    auto& dst = edge.dst();
1✔
1456

1457
    auto edge_removal = dst.can_remove_in_edge(graph, &edge);
1✔
1458
    int removed = 0;
1✔
1459
    if (edge_removal == data_flow::EdgeRemoveOption::Trivially) {
1✔
1460
        remove_memlet(block, edge);
×
1461
        ++removed;
×
1462
    } else if (edge_removal == data_flow::EdgeRemoveOption::RemoveNodeAfter) {
1✔
1463
        removed = 1 + clear_node(block, dst);
1✔
1464
    } else if (edge_removal == data_flow::EdgeRemoveOption::RequiresUpdate) {
1✔
1465
        remove_memlet(block, edge);
×
1466
        const_cast<data_flow::DataFlowNode&>(dst).update_edge_removed(edge.dst_conn());
×
1467
    } else if (edge_removal == data_flow::EdgeRemoveOption::NotRemovable) {
×
1468
        return 0;
×
1469
    }
×
1470

1471
    return removed;
1✔
1472
}
1✔
1473

1474
void StructuredSDFGBuilder::add_dataflow(const data_flow::DataFlowGraph& from, Block& to) {
19✔
1475
    auto& to_dataflow = to.dataflow();
19✔
1476

1477
    std::unordered_map<graph::Vertex, graph::Vertex> node_mapping;
19✔
1478
    for (auto& entry : from.nodes_) {
21✔
1479
        auto vertex = boost::add_vertex(to_dataflow.graph_);
21✔
1480
        to_dataflow.nodes_.insert({vertex, entry.second->clone(this->new_element_id(), vertex, to_dataflow)});
21✔
1481
        node_mapping.insert({entry.first, vertex});
21✔
1482
    }
21✔
1483

1484
    for (auto& entry : from.edges_) {
19✔
1485
        auto src = node_mapping[entry.second->src().vertex()];
14✔
1486
        auto dst = node_mapping[entry.second->dst().vertex()];
14✔
1487

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

1490
        to_dataflow.edges_.insert(
14✔
1491
            {edge.first,
14✔
1492
             entry.second->clone(
14✔
1493
                 this->new_element_id(), edge.first, to_dataflow, *to_dataflow.nodes_[src], *to_dataflow.nodes_[dst]
14✔
1494
             )}
14✔
1495
        );
14✔
1496
    }
14✔
1497
};
19✔
1498

1499
void StructuredSDFGBuilder::merge_siblings(data_flow::AccessNode& source_node) {
21✔
1500
    auto& user_graph = source_node.get_parent();
21✔
1501
    auto* block = dynamic_cast<structured_control_flow::Block*>(user_graph.get_parent());
21✔
1502
    if (!block) {
21✔
1503
        throw InvalidSDFGException("Parent of user graph must be a block!");
×
1504
    }
×
1505

1506
    // Merge access nodes if they access the same container on a code node
1507
    for (auto& oedge : user_graph.out_edges(source_node)) {
21✔
1508
        if (auto* code_node = dynamic_cast<data_flow::CodeNode*>(&oedge.dst())) {
15✔
1509
            std::unordered_set<data_flow::Memlet*> iedges;
8✔
1510
            for (auto& iedge : user_graph.in_edges(*code_node)) {
10✔
1511
                iedges.insert(&iedge);
10✔
1512
            }
10✔
1513
            for (auto* iedge : iedges) {
10✔
1514
                if (dynamic_cast<data_flow::ConstantNode*>(&iedge->src())) {
10✔
1515
                    continue;
×
1516
                }
×
1517
                auto* access_node = static_cast<data_flow::AccessNode*>(&iedge->src());
10✔
1518
                if (access_node == &source_node || access_node->data() != source_node.data()) {
10✔
1519
                    continue;
9✔
1520
                }
9✔
1521
                this->add_memlet(
1✔
1522
                    *block,
1✔
1523
                    source_node,
1✔
1524
                    iedge->src_conn(),
1✔
1525
                    *code_node,
1✔
1526
                    iedge->dst_conn(),
1✔
1527
                    iedge->subset(),
1✔
1528
                    iedge->base_type(),
1✔
1529
                    iedge->debug_info()
1✔
1530
                );
1✔
1531
                this->remove_memlet(*block, *iedge);
1✔
1532
                source_node.set_debug_info(DebugInfo::merge(source_node.debug_info(), access_node->debug_info()));
1✔
1533
            }
1✔
1534
        }
8✔
1535
    }
15✔
1536

1537
    // Also merge "output" access nodes if they access the same container on a library node
1538
    for (auto& iedge : user_graph.in_edges(source_node)) {
21✔
1539
        if (auto* libnode = dynamic_cast<data_flow::LibraryNode*>(&iedge.src())) {
7✔
1540
            std::unordered_set<data_flow::Memlet*> oedges;
×
1541
            for (auto& oedge : user_graph.out_edges(*libnode)) {
×
1542
                oedges.insert(&oedge);
×
1543
            }
×
1544
            for (auto* oedge : oedges) {
×
1545
                auto* access_node = static_cast<data_flow::AccessNode*>(&oedge->dst());
×
1546
                if (access_node == &source_node || access_node->data() != source_node.data()) {
×
1547
                    continue;
×
1548
                }
×
1549
                this->add_memlet(
×
1550
                    *block,
×
1551
                    *libnode,
×
1552
                    oedge->src_conn(),
×
1553
                    source_node,
×
1554
                    oedge->dst_conn(),
×
1555
                    oedge->subset(),
×
1556
                    oedge->base_type(),
×
1557
                    oedge->debug_info()
×
1558
                );
×
1559
                this->remove_memlet(*block, *oedge);
×
1560
                source_node.set_debug_info(DebugInfo::merge(source_node.debug_info(), access_node->debug_info()));
×
1561
            }
×
1562
        }
×
1563
    }
7✔
1564
}
21✔
1565

1566
void StructuredSDFGBuilder::merge_sinks(structured_control_flow::Block& block) {
4✔
1567
    auto& graph = block.dataflow();
4✔
1568

1569
    // Collect sink access nodes (modifying the graph during iteration is not safe)
1570
    std::vector<data_flow::AccessNode*> sink_access_nodes;
4✔
1571
    for (auto node : graph.sinks()) {
7✔
1572
        if (auto* access_node = dynamic_cast<data_flow::AccessNode*>(node)) {
7✔
1573
            sink_access_nodes.push_back(access_node);
7✔
1574
        }
7✔
1575
    }
7✔
1576

1577
    // Merge sink access nodes that refer to the same container
1578
    std::unordered_map<std::string, data_flow::AccessNode*> representatives;
4✔
1579
    for (auto* access_node : sink_access_nodes) {
7✔
1580
        auto it = representatives.find(access_node->data());
7✔
1581
        if (it == representatives.end()) {
7✔
1582
            representatives.insert({access_node->data(), access_node});
6✔
1583
            continue;
6✔
1584
        }
6✔
1585

1586
        auto& rep = *it->second;
1✔
1587

1588
        // Redirect all incoming memlets of this node onto the representative
1589
        std::unordered_set<data_flow::Memlet*> iedges;
1✔
1590
        for (auto& iedge : graph.in_edges(*access_node)) {
1✔
1591
            iedges.insert(&iedge);
1✔
1592
        }
1✔
1593
        for (auto* iedge : iedges) {
1✔
1594
            this->add_memlet(
1✔
1595
                block,
1✔
1596
                iedge->src(),
1✔
1597
                iedge->src_conn(),
1✔
1598
                rep,
1✔
1599
                iedge->dst_conn(),
1✔
1600
                iedge->subset(),
1✔
1601
                iedge->base_type(),
1✔
1602
                iedge->debug_info()
1✔
1603
            );
1✔
1604
            this->remove_memlet(block, *iedge);
1✔
1605
        }
1✔
1606

1607
        rep.set_debug_info(DebugInfo::merge(rep.debug_info(), access_node->debug_info()));
1✔
1608

1609
        // The node is now isolated and can be removed
1610
        this->remove_node(block, *access_node);
1✔
1611
    }
1✔
1612
}
4✔
1613

1614
} // namespace builder
1615
} // 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