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

daisytuner / docc / 28218915263

25 Jun 2026 06:08PM UTC coverage: 61.743% (+0.1%) from 61.644%
28218915263

push

github

web-flow
adds reduce as new structured loop type (#802)

* adds Reduce loop to structured loops

* extends For2Map into a general detection of reduce and map

* makes serialization of reduce backward compatible

* counts reduce as fors in loop analysis

* renames serializer for structured loops

* updates verifier

* adds support for FMA

* updates loop report to count map, reduce, for separately

* updates verifier after merge

* removes non-existent api function in tests after merge

* updates transformations to handle reduce

* adds vectorize dispatcher for reduce

* updates xfail for incorrect output

* updates go fast

* updates llvm verifier

* removes torchaudio dependency

705 of 985 new or added lines in 34 files covered. (71.57%)

5 existing lines in 4 files now uncovered.

38837 of 62901 relevant lines covered (61.74%)

977.93 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

127
    return false;
6✔
128
}
9✔
129

130

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

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

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

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

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

166

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

292
            break;
17✔
293
        }
17✔
294

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

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

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

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

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

336

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

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

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

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

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

382
Function& StructuredSDFGBuilder::function() const { return static_cast<Function&>(*this->structured_sdfg_); };
56,474✔
383

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

458
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
286✔
459
            auto& dataflow = block_stmt->dataflow();
18✔
460
            for (auto& node : dataflow.nodes()) {
61✔
461
                queue.push_back(&node);
61✔
462
            }
61✔
463
            for (auto& edge : dataflow.edges()) {
43✔
464
                queue.push_back(&edge);
43✔
465
            }
43✔
466
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
268✔
467
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
235✔
468
                queue.push_back(&sequence_stmt->at(i).first);
120✔
469
                queue.push_back(&sequence_stmt->at(i).second);
120✔
470
            }
120✔
471
        } else if (dynamic_cast<structured_control_flow::Return*>(current)) {
153✔
472
            // Do nothing
473
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
153✔
474
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
475
                queue.push_back(&if_else_stmt->at(i).first);
×
476
            }
×
477
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
153✔
478
            queue.push_back(&for_stmt->root());
7✔
479
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
146✔
480
            queue.push_back(&while_stmt->root());
×
481
        } else if (dynamic_cast<structured_control_flow::Continue*>(current)) {
146✔
482
            // Do nothing
483
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
146✔
484
            // Do nothing
485
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
146✔
486
            queue.push_back(&map_stmt->root());
41✔
487
        } else if (auto reduce_stmt = dynamic_cast<structured_control_flow::Reduce*>(current)) {
105✔
NEW
488
            queue.push_back(&reduce_stmt->root());
×
UNCOV
489
        }
×
490
    }
286✔
491

492
    return nullptr;
4✔
493
};
70✔
494

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

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

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

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

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

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

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

538
void StructuredSDFGBuilder::remove_from_parent(ControlFlowNode& child) {
5✔
539
    auto* parent = dynamic_cast<structured_control_flow::Sequence*>(child.get_parent());
5✔
540
    if (parent == nullptr) {
5✔
541
        throw InvalidSDFGException(
×
542
            "StructuredSDFGBuilder: Child has no sequence parent: #" + std::to_string(child.element_id())
×
543
        );
×
544
    }
×
545
    auto idx = parent->index(child);
5✔
546
    if (idx < 0) {
5✔
547
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found in parent");
×
548
    }
×
549
    remove_child(*parent, idx);
5✔
550
}
5✔
551

552
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t index) {
888✔
553
    parent.children_.erase(parent.children_.begin() + index);
888✔
554
    parent.transitions_.erase(parent.transitions_.begin() + index);
888✔
555
};
888✔
556

557
void StructuredSDFGBuilder::remove_children(Sequence& parent) {
×
558
    parent.children_.clear();
×
559
    parent.transitions_.clear();
×
560
};
×
561

562
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target) {
128✔
563
    size_t target_index = target.size();
128✔
564
    if (&source == &target) {
128✔
565
        target_index--;
×
566
    }
×
567
    this->move_child(source, source_index, target, target_index);
128✔
568
};
128✔
569

570
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target, size_t target_index) {
157✔
571
    auto node_ptr = std::move(source.children_.at(source_index));
157✔
572
    auto trans_ptr = std::move(source.transitions_.at(source_index));
157✔
573
    source.children_.erase(source.children_.begin() + source_index);
157✔
574
    source.transitions_.erase(source.transitions_.begin() + source_index);
157✔
575

576
    node_ptr->parent_ = &target;
157✔
577
    trans_ptr->parent_ = &target;
157✔
578
    target.children_.insert(target.children_.begin() + target_index, std::move(node_ptr));
157✔
579
    target.transitions_.insert(target.transitions_.begin() + target_index, std::move(trans_ptr));
157✔
580
};
157✔
581

582
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target) {
249✔
583
    this->move_children(source, target, target.size());
249✔
584
};
249✔
585

586
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target, size_t target_index) {
316✔
587
    target.children_.insert(
316✔
588
        target.children_.begin() + target_index,
316✔
589
        std::make_move_iterator(source.children_.begin()),
316✔
590
        std::make_move_iterator(source.children_.end())
316✔
591
    );
316✔
592
    target.transitions_.insert(
316✔
593
        target.transitions_.begin() + target_index,
316✔
594
        std::make_move_iterator(source.transitions_.begin()),
316✔
595
        std::make_move_iterator(source.transitions_.end())
316✔
596
    );
316✔
597
    for (auto& child : target.children_) {
656✔
598
        child->parent_ = &target;
656✔
599
    }
656✔
600
    for (auto& trans : target.transitions_) {
656✔
601
        trans->parent_ = &target;
656✔
602
    }
656✔
603
    source.children_.clear();
316✔
604
    source.transitions_.clear();
316✔
605
};
316✔
606

607
Sequence& StructuredSDFGBuilder::hoist_root() {
×
608
    auto current_root = std::move(this->structured_sdfg_->root_);
×
609

610
    this->structured_sdfg_->root_ =
×
611
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), current_root->debug_info(), nullptr));
×
612

613
    current_root->parent_ = this->structured_sdfg_->root_.get();
×
614
    this->structured_sdfg_->root_->children_.push_back(std::move(current_root));
×
615
    this->structured_sdfg_->root_->transitions_.push_back(std::unique_ptr<Transition>(
×
616
        new Transition(this->new_element_id(), current_root->debug_info(), *this->structured_sdfg_->root_)
×
617
    ));
×
618
    return *this->structured_sdfg_->root_;
×
619
};
×
620

621
Block& StructuredSDFGBuilder::
622
    add_block(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
3,242✔
623
    return insert_block_internal(parent, INSERT_AT_END, nullptr, assignments, debug_info).first;
3,242✔
624
}
3,242✔
625

626
Block& StructuredSDFGBuilder::add_block(
627
    Sequence& parent,
628
    const data_flow::DataFlowGraph& data_flow_graph,
629
    const sdfg::control_flow::Assignments& assignments,
630
    const DebugInfo& debug_info
631
) {
19✔
632
    return insert_block_internal(parent, INSERT_AT_END, &data_flow_graph, assignments, debug_info).first;
19✔
633
}
19✔
634

635
std::pair<Block&, Transition&> StructuredSDFGBuilder::insert_block_internal(
636
    Sequence& parent,
637
    int32_t insert_idx,
638
    const data_flow::DataFlowGraph* import_from,
639
    const sdfg::control_flow::Assignments& assignments,
640
    const DebugInfo& debug_info
641
) {
3,624✔
642
    auto new_pair = insert_node_internal<Block>(parent, insert_idx, assignments, debug_info);
3,624✔
643

644
    if (import_from) {
3,624✔
645
        this->add_dataflow(*import_from, new_pair.first);
19✔
646
    }
19✔
647

648
    return new_pair;
3,624✔
649
}
3,624✔
650

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

662
    return insert_block_internal(parent, index, nullptr, assignments, debug_info).first;
187✔
663
};
187✔
664

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

677
    return insert_block_internal(parent, index, &data_flow_graph, assignments, debug_info).first;
×
678
};
×
679

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

691
    return insert_block_internal(parent, index + 1, nullptr, assignments, debug_info).first;
176✔
692
}
176✔
693

694
Block& StructuredSDFGBuilder::add_block_after(
695
    Sequence& parent,
696
    ControlFlowNode& child,
697
    data_flow::DataFlowGraph& data_flow_graph,
698
    const sdfg::control_flow::Assignments& assignments,
699
    const DebugInfo& debug_info
700
) {
×
701
    int index = parent.index(child);
×
702
    if (index == -1) {
×
703
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
704
    }
×
705

706
    return insert_block_internal(parent, index + 1, &data_flow_graph, assignments, debug_info).first;
×
707
}
×
708

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

716
    return insert_block_internal(parent, index, nullptr, {}, debug_info);
×
717
}
×
718

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

727
    return insert_block_internal(parent, index, &data_flow_graph, {}, debug_info);
×
728
}
×
729

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

737
    return insert_block_internal(parent, index + 1, nullptr, {}, debug_info);
×
738
}
×
739

740
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_after(
741
    Sequence& parent, ControlFlowNode& child, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
742
) {
×
743
    int index = parent.index(child);
×
744
    if (index == -1) {
×
745
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
746
    }
×
747

748
    return insert_block_internal(parent, index + 1, &data_flow_graph, {}, debug_info);
×
749
}
×
750

751
IfElse& StructuredSDFGBuilder::
752
    add_if_else(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
143✔
753
    return insert_node_internal<IfElse>(parent, INSERT_AT_END, assignments, debug_info).first;
143✔
754
}
143✔
755

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

767
    return insert_node_internal<IfElse>(parent, index, assignments, debug_info).first;
18✔
768
}
18✔
769

770
IfElse& StructuredSDFGBuilder::add_if_else_after(
771
    Sequence& parent,
772
    ControlFlowNode& child,
773
    const sdfg::control_flow::Assignments& assignments,
774
    const DebugInfo& debug_info
775
) {
×
776
    int index = parent.index(child);
×
777
    if (index == -1) {
×
778
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
779
    }
×
780

781
    return insert_node_internal<IfElse>(parent, index + 1, assignments, debug_info).first;
×
782
}
×
783

784
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::
785
    add_if_else_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
786
    int index = parent.index(child);
×
787
    if (index == -1) {
×
788
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
789
    }
×
790

791
    return insert_node_internal<IfElse>(parent, INSERT_AT_END, {}, debug_info);
×
792
};
×
793

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

797
    scope.conditions_.push_back(cond);
259✔
798
    return *scope.cases_.back();
259✔
799
};
259✔
800

801
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t index, const DebugInfo& debug_info) {
×
802
    scope.cases_.erase(scope.cases_.begin() + index);
×
803
    scope.conditions_.erase(scope.conditions_.begin() + index);
×
804
};
×
805

806
While& StructuredSDFGBuilder::
807
    add_while(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
53✔
808
    return insert_node_internal<While>(parent, INSERT_AT_END, assignments, debug_info).first;
53✔
809
};
53✔
810

811
For& StructuredSDFGBuilder::add_for(
812
    Sequence& parent,
813
    const symbolic::Symbol indvar,
814
    const symbolic::Condition condition,
815
    const symbolic::Expression init,
816
    const symbolic::Expression update,
817
    const sdfg::control_flow::Assignments& assignments,
818
    const DebugInfo& debug_info
819
) {
808✔
820
    return insert_node_internal<For>(parent, INSERT_AT_END, assignments, debug_info, indvar, init, update, condition)
808✔
821
        .first;
808✔
822
}
808✔
823

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

839
    return insert_node_internal<For>(parent, index, assignments, debug_info, indvar, init, update, condition).first;
40✔
840
}
40✔
841

842
For& StructuredSDFGBuilder::add_for_after(
843
    Sequence& parent,
844
    ControlFlowNode& child,
845
    const symbolic::Symbol indvar,
846
    const symbolic::Condition condition,
847
    const symbolic::Expression init,
848
    const symbolic::Expression update,
849
    const sdfg::control_flow::Assignments& assignments,
850
    const DebugInfo& debug_info
851
) {
62✔
852
    int index = parent.index(child);
62✔
853
    if (index == -1) {
62✔
854
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
855
    }
×
856

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

860
Map& StructuredSDFGBuilder::add_map(
861
    Sequence& parent,
862
    const symbolic::Symbol indvar,
863
    const symbolic::Condition condition,
864
    const symbolic::Expression init,
865
    const symbolic::Expression update,
866
    const ScheduleType& schedule_type,
867
    const sdfg::control_flow::Assignments& assignments,
868
    const DebugInfo& debug_info
869
) {
2,219✔
870
    return insert_node_internal<
2,219✔
871
               Map>(parent, INSERT_AT_END, assignments, debug_info, indvar, init, update, condition, schedule_type)
2,219✔
872
        .first;
2,219✔
873
}
2,219✔
874

875
Map& StructuredSDFGBuilder::add_map_before(
876
    Sequence& parent,
877
    ControlFlowNode& child,
878
    const symbolic::Symbol indvar,
879
    const symbolic::Condition condition,
880
    const symbolic::Expression init,
881
    const symbolic::Expression update,
882
    const ScheduleType& schedule_type,
883
    const sdfg::control_flow::Assignments& assignments,
884
    const DebugInfo& debug_info
885
) {
63✔
886
    int index = parent.index(child);
63✔
887
    if (index == -1) {
63✔
888
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
889
    }
×
890

891
    return insert_node_internal<
63✔
892
               Map>(parent, index, assignments, debug_info, indvar, init, update, condition, schedule_type)
63✔
893
        .first;
63✔
894
}
63✔
895

896
Map& StructuredSDFGBuilder::add_map_after(
897
    Sequence& parent,
898
    ControlFlowNode& child,
899
    const symbolic::Symbol indvar,
900
    const symbolic::Condition condition,
901
    const symbolic::Expression init,
902
    const symbolic::Expression update,
903
    const ScheduleType& schedule_type,
904
    const sdfg::control_flow::Assignments& assignments,
905
    const DebugInfo& debug_info
906
) {
85✔
907
    int index = parent.index(child);
85✔
908
    if (index == -1) {
85✔
909
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
910
    }
×
911

912
    return insert_node_internal<
85✔
913
               Map>(parent, index + 1, assignments, debug_info, indvar, init, update, condition, schedule_type)
85✔
914
        .first;
85✔
915
}
85✔
916

917
Reduce& StructuredSDFGBuilder::add_reduce(
918
    Sequence& parent,
919
    const symbolic::Symbol indvar,
920
    const symbolic::Condition condition,
921
    const symbolic::Expression init,
922
    const symbolic::Expression update,
923
    const std::vector<structured_control_flow::ReductionInfo>& reductions,
924
    const ScheduleType& schedule_type,
925
    const sdfg::control_flow::Assignments& assignments,
926
    const DebugInfo& debug_info
927
) {
14✔
928
    return insert_node_internal<Reduce>(
14✔
929
               parent, INSERT_AT_END, assignments, debug_info, indvar, init, update, condition, reductions, schedule_type
14✔
930
    )
14✔
931
        .first;
14✔
932
}
14✔
933

934
Reduce& StructuredSDFGBuilder::add_reduce_before(
935
    Sequence& parent,
936
    ControlFlowNode& child,
937
    const symbolic::Symbol indvar,
938
    const symbolic::Condition condition,
939
    const symbolic::Expression init,
940
    const symbolic::Expression update,
941
    const std::vector<structured_control_flow::ReductionInfo>& reductions,
942
    const ScheduleType& schedule_type,
943
    const sdfg::control_flow::Assignments& assignments,
944
    const DebugInfo& debug_info
NEW
945
) {
×
NEW
946
    int index = parent.index(child);
×
NEW
947
    if (index == -1) {
×
NEW
948
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
NEW
949
    }
×
950

NEW
951
    return insert_node_internal<
×
NEW
952
               Reduce>(parent, index, assignments, debug_info, indvar, init, update, condition, reductions, schedule_type)
×
NEW
953
        .first;
×
NEW
954
}
×
955

956
Reduce& StructuredSDFGBuilder::add_reduce_after(
957
    Sequence& parent,
958
    ControlFlowNode& child,
959
    const symbolic::Symbol indvar,
960
    const symbolic::Condition condition,
961
    const symbolic::Expression init,
962
    const symbolic::Expression update,
963
    const std::vector<structured_control_flow::ReductionInfo>& reductions,
964
    const ScheduleType& schedule_type,
965
    const sdfg::control_flow::Assignments& assignments,
966
    const DebugInfo& debug_info
967
) {
4✔
968
    int index = parent.index(child);
4✔
969
    if (index == -1) {
4✔
NEW
970
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
NEW
971
    }
×
972

973
    return insert_node_internal<Reduce>(
4✔
974
               parent, index + 1, assignments, debug_info, indvar, init, update, condition, reductions, schedule_type
4✔
975
    )
4✔
976
        .first;
4✔
977
}
4✔
978

979
Continue& StructuredSDFGBuilder::
980
    add_continue(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
27✔
981
    return insert_node_internal<Continue>(parent, INSERT_AT_END, assignments, debug_info).first;
27✔
982
}
27✔
983

984
Break& StructuredSDFGBuilder::
985
    add_break(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
28✔
986
    return insert_node_internal<Break>(parent, INSERT_AT_END, assignments, debug_info).first;
28✔
987
}
28✔
988

989
Return& StructuredSDFGBuilder::add_return(
990
    Sequence& parent,
991
    const std::string& data,
992
    const sdfg::control_flow::Assignments& assignments,
993
    const DebugInfo& debug_info
994
) {
56✔
995
    return insert_node_internal<Return>(parent, INSERT_AT_END, assignments, debug_info, data).first;
56✔
996
}
56✔
997

998
Return& StructuredSDFGBuilder::add_constant_return(
999
    Sequence& parent,
1000
    const std::string& data,
1001
    const types::IType& type,
1002
    const sdfg::control_flow::Assignments& assignments,
1003
    const DebugInfo& debug_info
1004
) {
×
1005
    return insert_node_internal<Return>(parent, INSERT_AT_END, assignments, debug_info, data, type).first;
×
1006
}
×
1007

1008
For& StructuredSDFGBuilder::convert_while(
1009
    Sequence& parent,
1010
    While& loop,
1011
    const symbolic::Symbol indvar,
1012
    const symbolic::Condition condition,
1013
    const symbolic::Expression init,
1014
    const symbolic::Expression update
1015
) {
×
1016
    int index = parent.index(loop);
×
1017
    if (index == -1) {
×
1018
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1019
    }
×
1020

1021
    auto iter = parent.children_.begin() + index;
×
1022
    auto& new_iter = *parent.children_.insert(
×
1023
        iter + 1,
×
1024
        std::unique_ptr<For>(new For(
×
1025
            this->new_element_id_batch(For::REQUIRED_ELEMENT_IDS),
×
1026
            loop.debug_info(),
×
1027
            &parent,
×
1028
            indvar,
×
1029
            init,
×
1030
            update,
×
1031
            condition
×
1032
        ))
×
1033
    );
×
1034

1035
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
1036
    this->move_children(loop.root(), for_loop.root());
×
1037

1038
    // Remove while loop
1039
    parent.children_.erase(parent.children_.begin() + index);
×
1040

1041
    return for_loop;
×
1042
};
×
1043

1044
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
77✔
1045
    int index = parent.index(loop);
77✔
1046
    if (index == -1) {
77✔
1047
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1048
    }
×
1049

1050
    auto iter = parent.children_.begin() + index;
77✔
1051
    auto& new_iter = *parent.children_.insert(
77✔
1052
        iter + 1,
77✔
1053
        std::unique_ptr<Map>(new Map(
77✔
1054
            this->new_element_id_batch(Map::REQUIRED_ELEMENT_IDS),
77✔
1055
            loop.debug_info(),
77✔
1056
            &parent,
77✔
1057
            loop.indvar(),
77✔
1058
            loop.init(),
77✔
1059
            loop.update(),
77✔
1060
            loop.condition(),
77✔
1061
            ScheduleType_Sequential::create()
77✔
1062
        ))
77✔
1063
    );
77✔
1064

1065
    auto& map = dynamic_cast<Map&>(*new_iter);
77✔
1066
    this->move_children(loop.root(), map.root());
77✔
1067

1068
    // Remove for loop
1069
    parent.children_.erase(parent.children_.begin() + index);
77✔
1070

1071
    return map;
77✔
1072
};
77✔
1073

1074
Reduce& StructuredSDFGBuilder::convert_for_to_reduce(
1075
    Sequence& parent, For& loop, const std::vector<structured_control_flow::ReductionInfo>& reductions
1076
) {
26✔
1077
    int index = parent.index(loop);
26✔
1078
    if (index == -1) {
26✔
NEW
1079
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
NEW
1080
    }
×
1081

1082
    auto iter = parent.children_.begin() + index;
26✔
1083
    auto& new_iter = *parent.children_.insert(
26✔
1084
        iter + 1,
26✔
1085
        std::unique_ptr<Reduce>(new Reduce(
26✔
1086
            this->new_element_id_batch(Reduce::REQUIRED_ELEMENT_IDS),
26✔
1087
            loop.debug_info(),
26✔
1088
            &parent,
26✔
1089
            loop.indvar(),
26✔
1090
            loop.init(),
26✔
1091
            loop.update(),
26✔
1092
            loop.condition(),
26✔
1093
            reductions,
26✔
1094
            ScheduleType_Sequential::create()
26✔
1095
        ))
26✔
1096
    );
26✔
1097

1098
    auto& reduce = dynamic_cast<Reduce&>(*new_iter);
26✔
1099
    this->move_children(loop.root(), reduce.root());
26✔
1100

1101
    // Remove for loop
1102
    parent.children_.erase(parent.children_.begin() + index);
26✔
1103

1104
    return reduce;
26✔
1105
};
26✔
1106

1107
void StructuredSDFGBuilder::update_if_else_condition(IfElse& if_else, size_t index, const symbolic::Condition condition) {
×
1108
    if (index >= if_else.conditions_.size()) {
×
1109
        throw InvalidSDFGException("StructuredSDFGBuilder: Index out of range");
×
1110
    }
×
1111
    if_else.conditions_.at(index) = condition;
×
1112
};
×
1113

1114
void StructuredSDFGBuilder::update_loop(
1115
    StructuredLoop& loop,
1116
    const symbolic::Symbol indvar,
1117
    const symbolic::Condition condition,
1118
    const symbolic::Expression init,
1119
    const symbolic::Expression update
1120
) {
119✔
1121
    loop.indvar_ = indvar;
119✔
1122
    loop.condition_ = condition;
119✔
1123
    loop.init_ = init;
119✔
1124
    loop.update_ = update;
119✔
1125
};
119✔
1126

1127
void StructuredSDFGBuilder::update_schedule_type(StructuredLoop& loop, const ScheduleType& schedule_type) {
36✔
1128
    loop.schedule_type_ = schedule_type;
36✔
1129
}
36✔
1130

1131
/***** Section: Dataflow Graph *****/
1132

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

1143
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
7,146✔
1144
};
7,146✔
1145

1146
data_flow::ConstantNode& StructuredSDFGBuilder::add_constant(
1147
    structured_control_flow::Block& block, const std::string& data, const types::IType& type, const DebugInfo& debug_info
1148
) {
429✔
1149
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
429✔
1150
    auto res = block.dataflow_->nodes_.insert(
429✔
1151
        {vertex,
429✔
1152
         std::unique_ptr<data_flow::ConstantNode>(
429✔
1153
             new data_flow::ConstantNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data, type)
429✔
1154
         )}
429✔
1155
    );
429✔
1156

1157
    return dynamic_cast<data_flow::ConstantNode&>(*(res.first->second));
429✔
1158
};
429✔
1159

1160

1161
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
1162
    structured_control_flow::Block& block,
1163
    const data_flow::TaskletCode code,
1164
    const std::string& output,
1165
    const std::vector<std::string>& inputs,
1166
    const DebugInfo& debug_info
1167
) {
1,610✔
1168
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
1,610✔
1169
    auto res = block.dataflow_->nodes_.insert(
1,610✔
1170
        {vertex,
1,610✔
1171
         std::unique_ptr<data_flow::Tasklet>(
1,610✔
1172
             new data_flow::Tasklet(this->new_element_id(), debug_info, vertex, block.dataflow(), code, output, inputs)
1,610✔
1173
         )}
1,610✔
1174
    );
1,610✔
1175

1176
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
1,610✔
1177
};
1,610✔
1178

1179
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
1180
    structured_control_flow::Block& block,
1181
    data_flow::DataFlowNode& src,
1182
    const std::string& src_conn,
1183
    data_flow::DataFlowNode& dst,
1184
    const std::string& dst_conn,
1185
    const data_flow::Subset& subset,
1186
    const types::IType& base_type,
1187
    const DebugInfo& debug_info
1188
) {
7,860✔
1189
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
7,860✔
1190
    auto res = block.dataflow_->edges_.insert(
7,860✔
1191
        {edge.first,
7,860✔
1192
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
7,860✔
1193
             this->new_element_id(), debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset, base_type
7,860✔
1194
         ))}
7,860✔
1195
    );
7,860✔
1196

1197
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
7,860✔
1198
#ifndef NDEBUG
7,860✔
1199
    memlet.validate(*this->structured_sdfg_);
7,860✔
1200
#endif
7,860✔
1201

1202
    return memlet;
7,860✔
1203
};
7,860✔
1204

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

1217
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1218
    structured_control_flow::Block& block,
1219
    data_flow::CodeNode& src,
1220
    const std::string& src_conn,
1221
    data_flow::AccessNode& dst,
1222
    const data_flow::Subset& subset,
1223
    const types::IType& base_type,
1224
    const DebugInfo& debug_info
1225
) {
1,450✔
1226
    return this->add_memlet(block, src, src_conn, dst, "void", subset, base_type, debug_info);
1,450✔
1227
};
1,450✔
1228

1229
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1230
    structured_control_flow::Block& block,
1231
    data_flow::AccessNode& src,
1232
    data_flow::Tasklet& dst,
1233
    const std::string& dst_conn,
1234
    const data_flow::Subset& subset,
1235
    const DebugInfo& debug_info
1236
) {
1,026✔
1237
    const types::IType* src_type = nullptr;
1,026✔
1238
    if (auto cnode = dynamic_cast<data_flow::ConstantNode*>(&src)) {
1,026✔
1239
        src_type = &cnode->type();
240✔
1240
    } else {
786✔
1241
        src_type = &this->structured_sdfg_->type(src.data());
786✔
1242
    }
786✔
1243
    auto base_type = types::infer_type(*this->structured_sdfg_, *src_type, subset);
1,026✔
1244
    if (base_type->type_id() != types::TypeID::Scalar) {
1,026✔
1245
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1246
    }
×
1247
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, *src_type, debug_info);
1,026✔
1248
};
1,026✔
1249

1250
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
1251
    structured_control_flow::Block& block,
1252
    data_flow::Tasklet& src,
1253
    const std::string& src_conn,
1254
    data_flow::AccessNode& dst,
1255
    const data_flow::Subset& subset,
1256
    const DebugInfo& debug_info
1257
) {
638✔
1258
    auto& dst_type = this->structured_sdfg_->type(dst.data());
638✔
1259
    auto base_type = types::infer_type(*this->structured_sdfg_, dst_type, subset);
638✔
1260
    if (base_type->type_id() != types::TypeID::Scalar) {
638✔
1261
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1262
    }
×
1263
    return this->add_memlet(block, src, src_conn, dst, "void", subset, dst_type, debug_info);
638✔
1264
};
638✔
1265

1266
data_flow::Memlet& StructuredSDFGBuilder::add_reference_memlet(
1267
    structured_control_flow::Block& block,
1268
    data_flow::AccessNode& src,
1269
    data_flow::AccessNode& dst,
1270
    const data_flow::Subset& subset,
1271
    const types::IType& base_type,
1272
    const DebugInfo& debug_info
1273
) {
120✔
1274
    return this->add_memlet(block, src, "void", dst, "ref", subset, base_type, debug_info);
120✔
1275
};
120✔
1276

1277
data_flow::Memlet& StructuredSDFGBuilder::add_dereference_memlet(
1278
    structured_control_flow::Block& block,
1279
    data_flow::AccessNode& src,
1280
    data_flow::AccessNode& dst,
1281
    bool derefs_src,
1282
    const types::IType& base_type,
1283
    const DebugInfo& debug_info
1284
) {
23✔
1285
    if (derefs_src) {
23✔
1286
        return this->add_memlet(block, src, "void", dst, "deref", {symbolic::zero()}, base_type, debug_info);
14✔
1287
    } else {
14✔
1288
        return this->add_memlet(block, src, "deref", dst, "void", {symbolic::zero()}, base_type, debug_info);
9✔
1289
    }
9✔
1290
};
23✔
1291

1292
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block, const data_flow::Memlet& edge) {
240✔
1293
    auto& graph = block.dataflow();
240✔
1294
    auto e = edge.edge();
240✔
1295
    boost::remove_edge(e, graph.graph_);
240✔
1296
    graph.edges_.erase(e);
240✔
1297
};
240✔
1298

1299
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
194✔
1300
    auto& graph = block.dataflow();
194✔
1301
    auto v = node.vertex();
194✔
1302
    boost::remove_vertex(v, graph.graph_);
194✔
1303
    graph.nodes_.erase(v);
194✔
1304
};
194✔
1305

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

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

1311
    // Delete incoming
1312
    std::list<const data_flow::Memlet*> iedges;
602✔
1313
    for (auto& iedge : graph.in_edges(node)) {
1,492✔
1314
        iedges.push_back(&iedge);
1,492✔
1315
    }
1,492✔
1316
    for (auto iedge : iedges) {
1,492✔
1317
        auto& src = iedge->src();
1,492✔
1318
        to_delete.insert(&src);
1,492✔
1319

1320
        auto edge = iedge->edge();
1,492✔
1321
        graph.edges_.erase(edge);
1,492✔
1322
        boost::remove_edge(edge, graph.graph_);
1,492✔
1323
    }
1,492✔
1324

1325
    // Delete outgoing
1326
    std::list<const data_flow::Memlet*> oedges;
602✔
1327
    for (auto& oedge : graph.out_edges(node)) {
602✔
1328
        oedges.push_back(&oedge);
39✔
1329
    }
39✔
1330
    for (auto oedge : oedges) {
602✔
1331
        auto& dst = oedge->dst();
39✔
1332
        to_delete.insert(&dst);
39✔
1333

1334
        auto edge = oedge->edge();
39✔
1335
        graph.edges_.erase(edge);
39✔
1336
        boost::remove_edge(edge, graph.graph_);
39✔
1337
    }
39✔
1338

1339
    // Delete nodes
1340
    for (auto obsolete_node : to_delete) {
2,133✔
1341
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
2,133✔
1342
            auto vertex = obsolete_node->vertex();
2,133✔
1343
            graph.nodes_.erase(vertex);
2,133✔
1344
            boost::remove_vertex(vertex, graph.graph_);
2,133✔
1345
        }
2,133✔
1346
    }
2,133✔
1347
}
602✔
1348

1349
void StructuredSDFGBuilder::
1350
    clear_access_node_legacy(structured_control_flow::Block& block, const data_flow::AccessNode& node) {
3✔
1351
    auto& graph = block.dataflow();
3✔
1352

1353
    std::list<const data_flow::Memlet*> tmp;
3✔
1354
    std::list<const data_flow::DataFlowNode*> queue = {&node};
3✔
1355
    while (!queue.empty()) {
9✔
1356
        auto current = queue.front();
6✔
1357
        queue.pop_front();
6✔
1358
        if (current != &node) {
6✔
1359
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
3✔
1360
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1361
                    continue;
×
1362
                }
×
1363
            }
×
1364
        }
3✔
1365

1366
        tmp.clear();
6✔
1367
        for (auto& iedge : graph.in_edges(*current)) {
6✔
1368
            tmp.push_back(&iedge);
3✔
1369
        }
3✔
1370
        for (auto iedge : tmp) {
6✔
1371
            auto& src = iedge->src();
3✔
1372
            queue.push_back(&src);
3✔
1373

1374
            auto edge = iedge->edge();
3✔
1375
            graph.edges_.erase(edge);
3✔
1376
            boost::remove_edge(edge, graph.graph_);
3✔
1377
        }
3✔
1378

1379
        if (current != &node || graph.out_degree(*current) == 0) {
6✔
1380
            auto vertex = current->vertex();
6✔
1381
            graph.nodes_.erase(vertex);
6✔
1382
            boost::remove_vertex(vertex, graph.graph_);
6✔
1383
        }
6✔
1384
    }
6✔
1385
}
3✔
1386

1387
int StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
13✔
1388
    return clear_node(block, node, {&node});
13✔
1389
}
13✔
1390

1391
int StructuredSDFGBuilder::clear_node(
1392
    structured_control_flow::Block& block,
1393
    const data_flow::DataFlowNode& node,
1394
    const std::unordered_set<const data_flow::DataFlowNode*>& ignore_side_effects
1395
) {
13✔
1396
    auto& graph = block.dataflow();
13✔
1397

1398
    std::list<const data_flow::Memlet*> tmp;
13✔
1399
    std::list<const data_flow::DataFlowNode*> queue = {&node};
13✔
1400
    std::unordered_set<const data_flow::DataFlowNode*> remove_once_set;
13✔
1401
    std::unordered_set<const data_flow::DataFlowNode*> force_remove_set;
13✔
1402
    int removed_nodes = 0;
13✔
1403

1404
    do {
36✔
1405
        auto current = queue.front();
36✔
1406
        queue.pop_front();
36✔
1407

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

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

1413
            bool ignore_side_effects_on_current = ignore_side_effects.contains(current);
35✔
1414
            bool force_remove = force_remove_set.contains(current);
35✔
1415

1416
            // we can remove nodes without out-edges & side effects.
1417
            if ((no_more_consumers && !current->side_effect()) ||
35✔
1418
                ((ignore_side_effects_on_current || force_remove) && (no_more_consumers || access_node))) {
35✔
1419
                // Or for access-nodes on the ignore list, we can remove the write side (will not remove the node, only
1420
                // inputs) For any other node on the ignore list, we can ignore if it has side effects or not
1421
                tmp.clear();
28✔
1422
                for (auto& iedge : graph.in_edges(*current)) {
28✔
1423
                    tmp.push_back(&iedge);
25✔
1424
                }
25✔
1425
                bool no_in_edges = true;
28✔
1426
                for (auto iedge : tmp) {
28✔
1427
                    auto& src = iedge->src();
25✔
1428

1429
                    auto edge_rem = src.can_remove_out_edge(graph, iedge);
25✔
1430
                    if (edge_rem != data_flow::EdgeRemoveOption::NotRemovable) {
25✔
1431
                        auto src_conn = iedge->src_conn();
23✔
1432
                        queue.push_back(&src);
23✔
1433
                        auto edge = iedge->edge();
23✔
1434
                        graph.edges_.erase(edge);
23✔
1435
                        boost::remove_edge(edge, graph.graph_);
23✔
1436
                        if (edge_rem == data_flow::EdgeRemoveOption::RequiresUpdate) {
23✔
1437
                            const_cast<data_flow::DataFlowNode&>(src).update_edge_removed(src_conn);
×
1438
                        } else if (edge_rem == data_flow::EdgeRemoveOption::RemoveNodeAfter) {
23✔
1439
                            force_remove_set.insert(&src);
7✔
1440
                        }
7✔
1441
                    } else {
23✔
1442
                        no_in_edges = false;
2✔
1443
                    }
2✔
1444
                }
25✔
1445

1446
                if (no_more_consumers && no_in_edges) {
28✔
1447
                    remove_once_set.insert(current);
25✔
1448
                    auto vertex = current->vertex();
25✔
1449
                    graph.nodes_.erase(vertex);
25✔
1450
                    boost::remove_vertex(vertex, graph.graph_);
25✔
1451
                    ++removed_nodes;
25✔
1452
                } else if (!no_more_consumers && force_remove) {
25✔
1453
                    throw std::runtime_error(
×
1454
                        "Not yet supported: RemoveNodeAfter with more than 1 edge: #" +
×
1455
                        std::to_string(current->element_id())
×
1456
                    );
×
1457
                }
×
1458
            }
28✔
1459
        }
35✔
1460
    } while (!queue.empty());
36✔
1461

1462
    return removed_nodes;
13✔
1463
}
13✔
1464

1465
int StructuredSDFGBuilder::clear_ptr_borrow_edge(Block& block, const data_flow::Memlet& edge) {
1✔
1466
    auto& graph = block.dataflow();
1✔
1467
    auto& src = edge.src();
1✔
1468
    auto& dst = edge.dst();
1✔
1469

1470
    auto edge_removal = dst.can_remove_in_edge(graph, &edge);
1✔
1471
    int removed = 0;
1✔
1472
    if (edge_removal == data_flow::EdgeRemoveOption::Trivially) {
1✔
1473
        remove_memlet(block, edge);
×
1474
        ++removed;
×
1475
    } else if (edge_removal == data_flow::EdgeRemoveOption::RemoveNodeAfter) {
1✔
1476
        removed = 1 + clear_node(block, dst);
1✔
1477
    } else if (edge_removal == data_flow::EdgeRemoveOption::RequiresUpdate) {
1✔
1478
        remove_memlet(block, edge);
×
1479
        const_cast<data_flow::DataFlowNode&>(dst).update_edge_removed(edge.dst_conn());
×
1480
    } else if (edge_removal == data_flow::EdgeRemoveOption::NotRemovable) {
×
1481
        return 0;
×
1482
    }
×
1483

1484
    return removed;
1✔
1485
}
1✔
1486

1487
void StructuredSDFGBuilder::add_dataflow(const data_flow::DataFlowGraph& from, Block& to) {
19✔
1488
    auto& to_dataflow = to.dataflow();
19✔
1489

1490
    std::unordered_map<graph::Vertex, graph::Vertex> node_mapping;
19✔
1491
    for (auto& entry : from.nodes_) {
21✔
1492
        auto vertex = boost::add_vertex(to_dataflow.graph_);
21✔
1493
        to_dataflow.nodes_.insert({vertex, entry.second->clone(this->new_element_id(), vertex, to_dataflow)});
21✔
1494
        node_mapping.insert({entry.first, vertex});
21✔
1495
    }
21✔
1496

1497
    for (auto& entry : from.edges_) {
19✔
1498
        auto src = node_mapping[entry.second->src().vertex()];
14✔
1499
        auto dst = node_mapping[entry.second->dst().vertex()];
14✔
1500

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

1503
        to_dataflow.edges_.insert(
14✔
1504
            {edge.first,
14✔
1505
             entry.second->clone(
14✔
1506
                 this->new_element_id(), edge.first, to_dataflow, *to_dataflow.nodes_[src], *to_dataflow.nodes_[dst]
14✔
1507
             )}
14✔
1508
        );
14✔
1509
    }
14✔
1510
};
19✔
1511

1512
void StructuredSDFGBuilder::merge_siblings(data_flow::AccessNode& source_node) {
21✔
1513
    auto& user_graph = source_node.get_parent();
21✔
1514
    auto* block = dynamic_cast<structured_control_flow::Block*>(user_graph.get_parent());
21✔
1515
    if (!block) {
21✔
1516
        throw InvalidSDFGException("Parent of user graph must be a block!");
×
1517
    }
×
1518

1519
    // Merge access nodes if they access the same container on a code node
1520
    for (auto& oedge : user_graph.out_edges(source_node)) {
21✔
1521
        if (auto* code_node = dynamic_cast<data_flow::CodeNode*>(&oedge.dst())) {
15✔
1522
            std::unordered_set<data_flow::Memlet*> iedges;
8✔
1523
            for (auto& iedge : user_graph.in_edges(*code_node)) {
10✔
1524
                iedges.insert(&iedge);
10✔
1525
            }
10✔
1526
            for (auto* iedge : iedges) {
10✔
1527
                if (dynamic_cast<data_flow::ConstantNode*>(&iedge->src())) {
10✔
1528
                    continue;
×
1529
                }
×
1530
                auto* access_node = static_cast<data_flow::AccessNode*>(&iedge->src());
10✔
1531
                if (access_node == &source_node || access_node->data() != source_node.data()) {
10✔
1532
                    continue;
9✔
1533
                }
9✔
1534
                this->add_memlet(
1✔
1535
                    *block,
1✔
1536
                    source_node,
1✔
1537
                    iedge->src_conn(),
1✔
1538
                    *code_node,
1✔
1539
                    iedge->dst_conn(),
1✔
1540
                    iedge->subset(),
1✔
1541
                    iedge->base_type(),
1✔
1542
                    iedge->debug_info()
1✔
1543
                );
1✔
1544
                this->remove_memlet(*block, *iedge);
1✔
1545
                source_node.set_debug_info(DebugInfo::merge(source_node.debug_info(), access_node->debug_info()));
1✔
1546
            }
1✔
1547
        }
8✔
1548
    }
15✔
1549

1550
    // Also merge "output" access nodes if they access the same container on a library node
1551
    for (auto& iedge : user_graph.in_edges(source_node)) {
21✔
1552
        if (auto* libnode = dynamic_cast<data_flow::LibraryNode*>(&iedge.src())) {
7✔
1553
            std::unordered_set<data_flow::Memlet*> oedges;
×
1554
            for (auto& oedge : user_graph.out_edges(*libnode)) {
×
1555
                oedges.insert(&oedge);
×
1556
            }
×
1557
            for (auto* oedge : oedges) {
×
1558
                auto* access_node = static_cast<data_flow::AccessNode*>(&oedge->dst());
×
1559
                if (access_node == &source_node || access_node->data() != source_node.data()) {
×
1560
                    continue;
×
1561
                }
×
1562
                this->add_memlet(
×
1563
                    *block,
×
1564
                    *libnode,
×
1565
                    oedge->src_conn(),
×
1566
                    source_node,
×
1567
                    oedge->dst_conn(),
×
1568
                    oedge->subset(),
×
1569
                    oedge->base_type(),
×
1570
                    oedge->debug_info()
×
1571
                );
×
1572
                this->remove_memlet(*block, *oedge);
×
1573
                source_node.set_debug_info(DebugInfo::merge(source_node.debug_info(), access_node->debug_info()));
×
1574
            }
×
1575
        }
×
1576
    }
7✔
1577
}
21✔
1578

1579
void StructuredSDFGBuilder::merge_sinks(structured_control_flow::Block& block) {
6✔
1580
    auto& graph = block.dataflow();
6✔
1581

1582
    // Collect sink access nodes (modifying the graph during iteration is not safe)
1583
    std::vector<data_flow::AccessNode*> sink_access_nodes;
6✔
1584
    for (auto node : graph.sinks()) {
9✔
1585
        if (auto* access_node = dynamic_cast<data_flow::AccessNode*>(node)) {
9✔
1586
            sink_access_nodes.push_back(access_node);
9✔
1587
        }
9✔
1588
    }
9✔
1589

1590
    // Merge sink access nodes that refer to the same container
1591
    std::unordered_map<std::string, data_flow::AccessNode*> representatives;
6✔
1592
    for (auto* access_node : sink_access_nodes) {
9✔
1593
        auto it = representatives.find(access_node->data());
9✔
1594
        if (it == representatives.end()) {
9✔
1595
            representatives.insert({access_node->data(), access_node});
8✔
1596
            continue;
8✔
1597
        }
8✔
1598

1599
        auto& rep = *it->second;
1✔
1600

1601
        // Redirect all incoming memlets of this node onto the representative
1602
        std::unordered_set<data_flow::Memlet*> iedges;
1✔
1603
        for (auto& iedge : graph.in_edges(*access_node)) {
1✔
1604
            iedges.insert(&iedge);
1✔
1605
        }
1✔
1606
        for (auto* iedge : iedges) {
1✔
1607
            this->add_memlet(
1✔
1608
                block,
1✔
1609
                iedge->src(),
1✔
1610
                iedge->src_conn(),
1✔
1611
                rep,
1✔
1612
                iedge->dst_conn(),
1✔
1613
                iedge->subset(),
1✔
1614
                iedge->base_type(),
1✔
1615
                iedge->debug_info()
1✔
1616
            );
1✔
1617
            this->remove_memlet(block, *iedge);
1✔
1618
        }
1✔
1619

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

1622
        // The node is now isolated and can be removed
1623
        this->remove_node(block, *access_node);
1✔
1624
    }
1✔
1625
}
6✔
1626

1627
} // namespace builder
1628
} // 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