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

daisytuner / sdfglib / 17483304451

04 Sep 2025 07:45AM UTC coverage: 59.057% (+0.02%) from 59.036%
17483304451

push

github

web-flow
Fix Bug (#220)

12 of 16 new or added lines in 2 files covered. (75.0%)

2 existing lines in 1 file now uncovered.

9223 of 15617 relevant lines covered (59.06%)

116.37 hits per line

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

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

3
#include <cstddef>
4

5
#include "sdfg/analysis/scope_analysis.h"
6
#include "sdfg/codegen/language_extensions/cpp_language_extension.h"
7
#include "sdfg/data_flow/library_node.h"
8
#include "sdfg/structured_control_flow/map.h"
9
#include "sdfg/structured_control_flow/sequence.h"
10
#include "sdfg/structured_control_flow/structured_loop.h"
11
#include "sdfg/types/utils.h"
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(const SDFG& sdfg, const control_flow::State& start, const control_flow::State& end) const {
1✔
21
    std::unordered_set<const control_flow::State*> nodes;
1✔
22
    std::unordered_set<const control_flow::State*> visited;
1✔
23
    std::list<const control_flow::State*> queue = {&start};
1✔
24
    while (!queue.empty()) {
4✔
25
        auto curr = queue.front();
3✔
26
        queue.pop_front();
3✔
27
        if (visited.find(curr) != visited.end()) {
3✔
28
            continue;
×
29
        }
30
        visited.insert(curr);
3✔
31

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

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

42
    return nodes;
1✔
43
};
1✔
44

45
bool post_dominates(
6✔
46
    const State* pdom,
47
    const State* node,
48
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree
49
) {
50
    if (pdom == node) {
6✔
51
        return true;
1✔
52
    }
53

54
    auto current = pdom_tree.at(node);
5✔
55
    while (current != nullptr) {
9✔
56
        if (current == pdom) {
9✔
57
            return true;
5✔
58
        }
59
        current = pdom_tree.at(current);
4✔
60
    }
61

62
    return false;
×
63
}
6✔
64

65
const control_flow::State* StructuredSDFGBuilder::find_end_of_if_else(
3✔
66
    const SDFG& sdfg,
67
    const State* current,
68
    std::vector<const InterstateEdge*>& out_edges,
69
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree
70
) {
71
    // Best-effort approach: Check if post-dominator of current dominates all out edges
72
    auto pdom = pdom_tree.at(current);
3✔
73
    for (auto& edge : out_edges) {
9✔
74
        if (!post_dominates(pdom, &edge->dst(), pdom_tree)) {
6✔
75
            return nullptr;
×
76
        }
77
    }
78

79
    return pdom;
3✔
80
}
3✔
81

82
void StructuredSDFGBuilder::traverse(const SDFG& sdfg) {
4✔
83
    // Start of SDFGS
84
    Sequence& root = *structured_sdfg_->root_;
4✔
85
    const State* start_state = &sdfg.start_state();
4✔
86

87
    auto pdom_tree = sdfg.post_dominator_tree();
4✔
88

89
    std::unordered_set<const InterstateEdge*> breaks;
4✔
90
    std::unordered_set<const InterstateEdge*> continues;
4✔
91
    for (auto& edge : sdfg.back_edges()) {
5✔
92
        continues.insert(edge);
1✔
93
    }
94

95
    std::unordered_set<const control_flow::State*> visited;
4✔
96
    this->traverse_with_loop_detection(sdfg, root, start_state, nullptr, continues, breaks, pdom_tree, visited);
4✔
97
};
4✔
98

99
void StructuredSDFGBuilder::traverse_with_loop_detection(
9✔
100
    const SDFG& sdfg,
101
    Sequence& scope,
102
    const State* current,
103
    const State* end,
104
    const std::unordered_set<const InterstateEdge*>& continues,
105
    const std::unordered_set<const InterstateEdge*>& breaks,
106
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree,
107
    std::unordered_set<const control_flow::State*>& visited
108
) {
109
    if (current == end) {
9✔
110
        return;
1✔
111
    }
112

113
    auto in_edges = sdfg.in_edges(*current);
8✔
114

115
    // Loop detection
116
    std::unordered_set<const InterstateEdge*> loop_edges;
8✔
117
    for (auto& iedge : in_edges) {
13✔
118
        if (continues.find(&iedge) != continues.end()) {
5✔
119
            loop_edges.insert(&iedge);
1✔
120
        }
1✔
121
    }
122
    if (!loop_edges.empty()) {
8✔
123
        // 1. Determine nodes of loop body
124
        std::unordered_set<const control_flow::State*> body;
1✔
125
        for (auto back_edge : loop_edges) {
2✔
126
            auto loop_nodes = this->determine_loop_nodes(sdfg, back_edge->src(), back_edge->dst());
1✔
127
            body.insert(loop_nodes.begin(), loop_nodes.end());
1✔
128
        }
1✔
129

130
        // 2. Determine exit states and exit edges
131
        std::unordered_set<const control_flow::State*> exit_states;
1✔
132
        std::unordered_set<const control_flow::InterstateEdge*> exit_edges;
1✔
133
        for (auto node : body) {
4✔
134
            for (auto& edge : sdfg.out_edges(*node)) {
7✔
135
                if (body.find(&edge.dst()) == body.end()) {
4✔
136
                    exit_edges.insert(&edge);
1✔
137
                    exit_states.insert(&edge.dst());
1✔
138
                }
1✔
139
            }
140
        }
141
        if (exit_states.size() != 1) {
1✔
142
            throw UnstructuredControlFlowException();
×
143
        }
144
        const control_flow::State* exit_state = *exit_states.begin();
1✔
145

146
        for (auto& edge : breaks) {
1✔
147
            exit_edges.insert(edge);
×
148
        }
149

150
        // Collect debug information (could be removed when this is computed dynamically)
151
        DebugInfo dbg_info = current->debug_info();
1✔
152
        for (auto& edge : in_edges) {
3✔
153
            dbg_info = DebugInfo::merge(dbg_info, edge.debug_info());
2✔
154
        }
155
        for (auto node : body) {
4✔
156
            dbg_info = DebugInfo::merge(dbg_info, node->debug_info());
3✔
157
        }
158
        for (auto edge : exit_edges) {
2✔
159
            dbg_info = DebugInfo::merge(dbg_info, edge->debug_info());
1✔
160
        }
161

162
        // 3. Add while loop
163
        While& loop = this->add_while(scope, {}, dbg_info);
1✔
164

165
        std::unordered_set<const control_flow::State*> loop_visited(visited);
1✔
166
        this->traverse_without_loop_detection(
1✔
167
            sdfg, loop.root(), current, exit_state, continues, exit_edges, pdom_tree, loop_visited
1✔
168
        );
169

170
        this->traverse_with_loop_detection(sdfg, scope, exit_state, end, continues, breaks, pdom_tree, visited);
1✔
171
    } else {
1✔
172
        this->traverse_without_loop_detection(sdfg, scope, current, end, continues, breaks, pdom_tree, visited);
7✔
173
    }
174
};
9✔
175

176
void StructuredSDFGBuilder::traverse_without_loop_detection(
8✔
177
    const SDFG& sdfg,
178
    Sequence& scope,
179
    const State* current,
180
    const State* end,
181
    const std::unordered_set<const InterstateEdge*>& continues,
182
    const std::unordered_set<const InterstateEdge*>& breaks,
183
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree,
184
    std::unordered_set<const control_flow::State*>& visited
185
) {
186
    std::list<const State*> queue = {current};
8✔
187
    while (!queue.empty()) {
26✔
188
        auto curr = queue.front();
18✔
189
        queue.pop_front();
18✔
190
        if (curr == end) {
18✔
191
            continue;
3✔
192
        }
193

194
        if (visited.find(curr) != visited.end()) {
15✔
195
            throw UnstructuredControlFlowException();
×
196
        }
197
        visited.insert(curr);
15✔
198

199
        auto out_edges = sdfg.out_edges(*curr);
15✔
200
        auto out_degree = sdfg.out_degree(*curr);
15✔
201

202
        // Case 1: Sink node
203
        if (out_degree == 0) {
15✔
204
            this->add_block(scope, curr->dataflow(), {}, curr->debug_info());
4✔
205
            this->add_return(scope, {}, curr->debug_info());
4✔
206
            continue;
4✔
207
        }
208

209
        // Case 2: Transition
210
        if (out_degree == 1) {
11✔
211
            auto& oedge = *out_edges.begin();
8✔
212
            if (!oedge.is_unconditional()) {
8✔
213
                throw UnstructuredControlFlowException();
×
214
            }
215
            this->add_block(scope, curr->dataflow(), oedge.assignments(), curr->debug_info());
8✔
216

217
            if (continues.find(&oedge) != continues.end()) {
8✔
218
                this->add_continue(scope, {}, oedge.debug_info());
×
219
            } else if (breaks.find(&oedge) != breaks.end()) {
8✔
220
                this->add_break(scope, {}, oedge.debug_info());
×
221
            } else {
×
222
                bool starts_loop = false;
8✔
223
                for (auto& iedge : sdfg.in_edges(oedge.dst())) {
19✔
224
                    if (continues.find(&iedge) != continues.end()) {
11✔
225
                        starts_loop = true;
×
226
                        break;
×
227
                    }
228
                }
229
                if (!starts_loop) {
8✔
230
                    queue.push_back(&oedge.dst());
8✔
231
                } else {
8✔
232
                    this->traverse_with_loop_detection(
×
233
                        sdfg, scope, &oedge.dst(), end, continues, breaks, pdom_tree, visited
×
234
                    );
235
                }
236
            }
237
            continue;
8✔
238
        }
239

240
        // Case 3: Branches
241
        if (out_degree > 1) {
3✔
242
            this->add_block(scope, curr->dataflow(), {}, curr->debug_info());
3✔
243

244
            std::vector<const InterstateEdge*> out_edges_vec;
3✔
245
            for (auto& edge : out_edges) {
9✔
246
                out_edges_vec.push_back(&edge);
6✔
247
            }
248

249
            // Best-effort approach: Find end of if-else
250
            // If not found, the branches may repeat paths yielding a large SDFG
251
            const control_flow::State* local_end = this->find_end_of_if_else(sdfg, curr, out_edges_vec, pdom_tree);
3✔
252
            if (local_end == nullptr) {
3✔
253
                local_end = end;
×
254
            }
×
255

256
            auto& if_else = this->add_if_else(scope, {}, curr->debug_info());
3✔
257
            for (size_t i = 0; i < out_degree; i++) {
9✔
258
                auto& out_edge = out_edges_vec[i];
6✔
259

260
                auto& branch = this->add_case(if_else, out_edge->condition(), out_edge->debug_info());
6✔
261
                if (!out_edge->assignments().empty()) {
6✔
262
                    this->add_block(branch, out_edge->assignments(), out_edge->debug_info());
2✔
263
                }
2✔
264
                if (continues.find(out_edge) != continues.end()) {
6✔
265
                    this->add_continue(branch, {}, out_edge->debug_info());
1✔
266
                } else if (breaks.find(out_edge) != breaks.end()) {
6✔
267
                    this->add_break(branch, {}, out_edge->debug_info());
1✔
268
                } else {
1✔
269
                    std::unordered_set<const control_flow::State*> branch_visited(visited);
4✔
270
                    this->traverse_with_loop_detection(
4✔
271
                        sdfg, branch, &out_edge->dst(), local_end, continues, breaks, pdom_tree, branch_visited
4✔
272
                    );
273
                }
4✔
274
            }
6✔
275

276
            if (local_end != end) {
3✔
277
                bool starts_loop = false;
2✔
278
                for (auto& iedge : sdfg.in_edges(*local_end)) {
6✔
279
                    if (continues.find(&iedge) != continues.end()) {
4✔
280
                        starts_loop = true;
×
281
                        break;
×
282
                    }
283
                }
284
                if (!starts_loop) {
2✔
285
                    queue.push_back(local_end);
2✔
286
                } else {
2✔
287
                    this->traverse_with_loop_detection(sdfg, scope, local_end, end, continues, breaks, pdom_tree, visited);
×
288
                }
289
            }
2✔
290
            continue;
291
        }
3✔
292
    }
293
}
8✔
294

295
Function& StructuredSDFGBuilder::function() const { return static_cast<Function&>(*this->structured_sdfg_); };
5,573✔
296

297
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
79✔
298
    : FunctionBuilder(), structured_sdfg_(std::move(sdfg)) {};
79✔
299

300
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
518✔
301
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type)) {};
518✔
302

303
StructuredSDFGBuilder::StructuredSDFGBuilder(const SDFG& sdfg)
4✔
304
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(sdfg.name(), sdfg.type())) {
4✔
305
    for (auto& entry : sdfg.structures_) {
4✔
306
        this->structured_sdfg_->structures_.insert({entry.first, entry.second->clone()});
×
307
    }
308

309
    for (auto& entry : sdfg.containers_) {
11✔
310
        this->structured_sdfg_->containers_.insert({entry.first, entry.second->clone()});
7✔
311
    }
312

313
    for (auto& arg : sdfg.arguments_) {
6✔
314
        this->structured_sdfg_->arguments_.push_back(arg);
2✔
315
    }
316

317
    for (auto& ext : sdfg.externals_) {
5✔
318
        this->structured_sdfg_->externals_.push_back(ext);
1✔
319
        this->structured_sdfg_->externals_linkage_types_[ext] = sdfg.linkage_type(ext);
1✔
320
    }
321

322
    for (auto& entry : sdfg.assumptions_) {
9✔
323
        this->structured_sdfg_->assumptions_.insert({entry.first, entry.second});
5✔
324
    }
325

326
    for (auto& entry : sdfg.metadata_) {
6✔
327
        this->structured_sdfg_->metadata_[entry.first] = entry.second;
2✔
328
    }
329

330
    this->traverse(sdfg);
4✔
331
};
4✔
332

333
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
978✔
334

335
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
423✔
336
#ifndef NDEBUG
337
    this->structured_sdfg_->validate();
423✔
338
#endif
339

340
    return std::move(this->structured_sdfg_);
423✔
341
};
342

343
Element* StructuredSDFGBuilder::find_element_by_id(const size_t& element_id) const {
13✔
344
    auto& sdfg = this->subject();
13✔
345
    std::list<Element*> queue = {&sdfg.root()};
13✔
346
    while (!queue.empty()) {
55✔
347
        auto current = queue.front();
55✔
348
        queue.pop_front();
55✔
349

350
        if (current->element_id() == element_id) {
55✔
351
            return current;
13✔
352
        }
353

354
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
42✔
355
            auto& dataflow = block_stmt->dataflow();
×
356
            for (auto& node : dataflow.nodes()) {
×
357
                queue.push_back(&node);
×
358
            }
359
            for (auto& edge : dataflow.edges()) {
×
360
                queue.push_back(&edge);
×
361
            }
362
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
42✔
363
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
44✔
364
                queue.push_back(&sequence_stmt->at(i).first);
22✔
365
                queue.push_back(&sequence_stmt->at(i).second);
22✔
366
            }
22✔
367
        } else if (dynamic_cast<structured_control_flow::Return*>(current)) {
42✔
368
            // Do nothing
369
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
20✔
370
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
371
                queue.push_back(&if_else_stmt->at(i).first);
×
372
            }
×
373
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
20✔
374
            queue.push_back(&for_stmt->root());
×
375
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
20✔
376
            queue.push_back(&while_stmt->root());
×
377
        } else if (dynamic_cast<structured_control_flow::Continue*>(current)) {
20✔
378
            // Do nothing
379
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
20✔
380
            // Do nothing
381
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
20✔
382
            queue.push_back(&map_stmt->root());
10✔
383
        }
10✔
384
    }
385

386
    return nullptr;
×
387
};
13✔
388

389
Sequence& StructuredSDFGBuilder::
390
    add_sequence(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
27✔
391
    parent.children_.push_back(std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
27✔
392

393
    parent.transitions_
54✔
394
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
27✔
395
        );
396

397
    return static_cast<Sequence&>(*parent.children_.back().get());
27✔
398
};
×
399

400
Sequence& StructuredSDFGBuilder::add_sequence_before(
10✔
401
    Sequence& parent,
402
    ControlFlowNode& child,
403
    const sdfg::control_flow::Assignments& assignments,
404
    const DebugInfo& debug_info
405
) {
406
    int index = parent.index(child);
10✔
407
    if (index == -1) {
10✔
408
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
409
    }
410

411
    parent.children_.insert(
20✔
412
        parent.children_.begin() + index, std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info))
10✔
413
    );
414

415
    parent.transitions_.insert(
20✔
416
        parent.transitions_.begin() + index,
10✔
417
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
10✔
418
    );
419

420
    return static_cast<Sequence&>(*parent.children_.at(index).get());
10✔
421
};
×
422

423
Sequence& StructuredSDFGBuilder::add_sequence_after(
×
424
    Sequence& parent,
425
    ControlFlowNode& child,
426
    const sdfg::control_flow::Assignments& assignments,
427
    const DebugInfo& debug_info
428
) {
429
    int index = parent.index(child);
×
430
    if (index == -1) {
×
431
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
432
    }
433

434
    parent.children_.insert(
×
435
        parent.children_.begin() + index + 1,
×
436
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info))
×
437
    );
438

439
    parent.transitions_.insert(
×
440
        parent.transitions_.begin() + index + 1,
×
441
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
442
    );
443

444
    return static_cast<Sequence&>(*parent.children_.at(index + 1).get());
×
445
};
×
446

447
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::
448
    add_sequence_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
449
    int index = parent.index(child);
×
450
    if (index == -1) {
×
451
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
452
    }
453

454
    parent.children_.insert(
×
455
        parent.children_.begin() + index, std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info))
×
456
    );
457

458
    parent.transitions_.insert(
×
459
        parent.transitions_.begin() + index,
×
460
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
461
    );
462

463
    auto new_entry = parent.at(index);
×
464
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
×
465

466
    return {new_block, new_entry.second};
×
467
};
×
468

469
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t index) {
25✔
470
    parent.children_.erase(parent.children_.begin() + index);
25✔
471
    parent.transitions_.erase(parent.transitions_.begin() + index);
25✔
472
};
25✔
473

474
void StructuredSDFGBuilder::remove_children(Sequence& parent) {
×
475
    parent.children_.clear();
×
476
    parent.transitions_.clear();
×
477
};
×
478

479
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target) {
10✔
480
    this->move_child(source, source_index, target, target.size());
10✔
481
};
10✔
482

483
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target, size_t target_index) {
10✔
484
    auto node_ptr = std::move(source.children_.at(source_index));
10✔
485
    auto trans_ptr = std::move(source.transitions_.at(source_index));
10✔
486
    source.children_.erase(source.children_.begin() + source_index);
10✔
487
    source.transitions_.erase(source.transitions_.begin() + source_index);
10✔
488

489
    trans_ptr->parent_ = &target;
10✔
490
    target.children_.insert(target.children_.begin() + target_index, std::move(node_ptr));
10✔
491
    target.transitions_.insert(target.transitions_.begin() + target_index, std::move(trans_ptr));
10✔
492
};
10✔
493

494
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target) {
23✔
495
    this->move_children(source, target, target.size());
23✔
496
};
23✔
497

498
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target, size_t target_index) {
23✔
499
    target.children_.insert(
46✔
500
        target.children_.begin() + target_index,
23✔
501
        std::make_move_iterator(source.children_.begin()),
23✔
502
        std::make_move_iterator(source.children_.end())
23✔
503
    );
504
    target.transitions_.insert(
46✔
505
        target.transitions_.begin() + target_index,
23✔
506
        std::make_move_iterator(source.transitions_.begin()),
23✔
507
        std::make_move_iterator(source.transitions_.end())
23✔
508
    );
509
    for (auto& trans : target.transitions_) {
52✔
510
        trans->parent_ = &target;
29✔
511
    }
512
    source.children_.clear();
23✔
513
    source.transitions_.clear();
23✔
514
};
23✔
515

516
Block& StructuredSDFGBuilder::
517
    add_block(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
504✔
518
    parent.children_.push_back(std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
504✔
519

520
    parent.transitions_
1,008✔
521
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
504✔
522
        );
523

524
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.back());
504✔
525
    (*new_block.dataflow_).parent_ = &new_block;
504✔
526

527
    return new_block;
504✔
528
};
×
529

530
Block& StructuredSDFGBuilder::add_block(
18✔
531
    Sequence& parent,
532
    const data_flow::DataFlowGraph& data_flow_graph,
533
    const sdfg::control_flow::Assignments& assignments,
534
    const DebugInfo& debug_info
535
) {
536
    parent.children_.push_back(std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
18✔
537

538
    parent.transitions_
36✔
539
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
18✔
540
        );
541

542
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.back());
18✔
543
    (*new_block.dataflow_).parent_ = &new_block;
18✔
544

545
    this->add_dataflow(data_flow_graph, new_block);
18✔
546

547
    return new_block;
18✔
548
};
×
549

550
Block& StructuredSDFGBuilder::add_block_before(
11✔
551
    Sequence& parent,
552
    ControlFlowNode& child,
553
    const sdfg::control_flow::Assignments& assignments,
554
    const DebugInfo& debug_info
555
) {
556
    int index = parent.index(child);
11✔
557
    if (index == -1) {
11✔
558
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
559
    }
560

561
    parent.children_
22✔
562
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
11✔
563

564
    parent.transitions_.insert(
22✔
565
        parent.transitions_.begin() + index,
11✔
566
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
11✔
567
    );
568

569
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index));
11✔
570
    (*new_block.dataflow_).parent_ = &new_block;
11✔
571

572
    return new_block;
11✔
UNCOV
573
};
×
574

575
Block& StructuredSDFGBuilder::add_block_before(
×
576
    Sequence& parent,
577
    ControlFlowNode& child,
578
    data_flow::DataFlowGraph& data_flow_graph,
579
    const sdfg::control_flow::Assignments& assignments,
580
    const DebugInfo& debug_info
581
) {
582
    int index = parent.index(child);
×
583
    if (index == -1) {
×
584
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
585
    }
586

587
    parent.children_
×
588
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
589

590
    parent.transitions_.insert(
×
591
        parent.transitions_.begin() + index,
×
592
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
593
    );
594

NEW
595
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index));
×
596
    (*new_block.dataflow_).parent_ = &new_block;
×
597
    this->add_dataflow(data_flow_graph, new_block);
×
598

599
    return new_block;
×
600
};
×
601

602
Block& StructuredSDFGBuilder::add_block_after(
3✔
603
    Sequence& parent,
604
    ControlFlowNode& child,
605
    const sdfg::control_flow::Assignments& assignments,
606
    const DebugInfo& debug_info
607
) {
608
    int index = parent.index(child);
3✔
609
    if (index == -1) {
3✔
610
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
611
    }
612

613
    parent.children_.insert(
6✔
614
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
3✔
615
    );
616

617
    parent.transitions_.insert(
6✔
618
        parent.transitions_.begin() + index + 1,
3✔
619
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
3✔
620
    );
621

622
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index + 1));
3✔
623
    (*new_block.dataflow_).parent_ = &new_block;
3✔
624

625
    return new_block;
3✔
UNCOV
626
};
×
627

628
Block& StructuredSDFGBuilder::add_block_after(
×
629
    Sequence& parent,
630
    ControlFlowNode& child,
631
    data_flow::DataFlowGraph& data_flow_graph,
632
    const sdfg::control_flow::Assignments& assignments,
633
    const DebugInfo& debug_info
634
) {
635
    int index = parent.index(child);
×
636
    if (index == -1) {
×
637
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
638
    }
639

640
    parent.children_.insert(
×
641
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
642
    );
643

644
    parent.transitions_.insert(
×
645
        parent.transitions_.begin() + index + 1,
×
646
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
647
    );
648

NEW
649
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index + 1));
×
650
    (*new_block.dataflow_).parent_ = &new_block;
×
651
    this->add_dataflow(data_flow_graph, new_block);
×
652

653
    return new_block;
×
654
};
×
655

656
std::pair<Block&, Transition&> StructuredSDFGBuilder::
657
    add_block_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
658
    int index = parent.index(child);
×
659
    if (index == -1) {
×
660
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
661
    }
662

663
    parent.children_
×
664
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
665

666
    parent.transitions_.insert(
×
667
        parent.transitions_.begin() + index,
×
668
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
669
    );
670

671
    auto new_entry = parent.at(index);
×
NEW
672
    auto& new_block = static_cast<structured_control_flow::Block&>(new_entry.first);
×
673
    (*new_block.dataflow_).parent_ = &new_block;
×
674

675
    return {new_block, new_entry.second};
×
676
};
×
677

678
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
×
679
    Sequence& parent, ControlFlowNode& child, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
680
) {
681
    int index = parent.index(child);
×
682
    if (index == -1) {
×
683
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
684
    }
685

686
    parent.children_
×
687
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
688

689
    parent.transitions_.insert(
×
690
        parent.transitions_.begin() + index,
×
691
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
692
    );
693

694
    auto new_entry = parent.at(index);
×
695
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
696
    (*new_block.dataflow_).parent_ = &new_block;
×
697

698
    this->add_dataflow(data_flow_graph, new_block);
×
699

700
    return {new_block, new_entry.second};
×
701
};
×
702

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

710
    parent.children_.insert(
×
711
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
712
    );
713

714
    parent.transitions_.insert(
×
715
        parent.transitions_.begin() + index + 1,
×
716
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
717
    );
718

719
    auto new_entry = parent.at(index + 1);
×
720
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
721
    (*new_block.dataflow_).parent_ = &new_block;
×
722

723
    return {new_block, new_entry.second};
×
724
};
×
725

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

734
    parent.children_.insert(
×
735
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
736
    );
737

738
    parent.transitions_.insert(
×
739
        parent.transitions_.begin() + index + 1,
×
740
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
741
    );
742

743
    auto new_entry = parent.at(index + 1);
×
744
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
745
    (*new_block.dataflow_).parent_ = &new_block;
×
746

747
    this->add_dataflow(data_flow_graph, new_block);
×
748

749
    return {new_block, new_entry.second};
×
750
};
×
751

752
IfElse& StructuredSDFGBuilder::
753
    add_if_else(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
40✔
754
    parent.children_.push_back(std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
40✔
755

756
    parent.transitions_
80✔
757
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
40✔
758
        );
759

760
    return static_cast<IfElse&>(*parent.children_.back().get());
40✔
761
};
×
762

763
IfElse& StructuredSDFGBuilder::add_if_else_before(
1✔
764
    Sequence& parent,
765
    ControlFlowNode& child,
766
    const sdfg::control_flow::Assignments& assignments,
767
    const DebugInfo& debug_info
768
) {
769
    int index = parent.index(child);
1✔
770
    if (index == -1) {
1✔
771
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
772
    }
773

774
    parent.children_
2✔
775
        .insert(parent.children_.begin() + index, std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
1✔
776

777
    parent.transitions_.insert(
2✔
778
        parent.transitions_.begin() + index,
1✔
779
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
1✔
780
    );
781

782
    return static_cast<IfElse&>(*parent.children_.at(index));
1✔
783
};
×
784

785
IfElse& StructuredSDFGBuilder::add_if_else_after(
×
786
    Sequence& parent,
787
    ControlFlowNode& child,
788
    const sdfg::control_flow::Assignments& assignments,
789
    const DebugInfo& debug_info
790
) {
791
    int index = parent.index(child);
×
792
    if (index == -1) {
×
793
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
794
    }
795

796
    parent.children_.insert(
×
797
        parent.children_.begin() + index + 1, std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info))
×
798
    );
799

800
    parent.transitions_.insert(
×
801
        parent.transitions_.begin() + index + 1,
×
802
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
803
    );
804

805
    return static_cast<IfElse&>(*parent.children_.at(index + 1));
×
806
};
×
807

808
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::
809
    add_if_else_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
810
    int index = parent.index(child);
×
811
    if (index == -1) {
×
812
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
813
    }
814

815
    parent.children_
×
816
        .insert(parent.children_.begin() + index, std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
×
817

818
    parent.transitions_.insert(
×
819
        parent.transitions_.begin() + index,
×
820
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
821
    );
822

823
    auto new_entry = parent.at(index);
×
824
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
×
825

826
    return {new_block, new_entry.second};
×
827
};
×
828

829
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond, const DebugInfo& debug_info) {
64✔
830
    scope.cases_.push_back(std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
64✔
831

832
    scope.conditions_.push_back(cond);
64✔
833
    return *scope.cases_.back();
64✔
834
};
×
835

836
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t index, const DebugInfo& debug_info) {
×
837
    scope.cases_.erase(scope.cases_.begin() + index);
×
838
    scope.conditions_.erase(scope.conditions_.begin() + index);
×
839
};
×
840

841
While& StructuredSDFGBuilder::
842
    add_while(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
33✔
843
    parent.children_.push_back(std::unique_ptr<While>(new While(this->new_element_id(), debug_info)));
33✔
844

845
    // Increment element id for body node
846
    this->new_element_id();
33✔
847

848
    parent.transitions_
66✔
849
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
33✔
850
        );
851

852
    return static_cast<While&>(*parent.children_.back().get());
33✔
853
};
×
854

855
For& StructuredSDFGBuilder::add_for(
121✔
856
    Sequence& parent,
857
    const symbolic::Symbol& indvar,
858
    const symbolic::Condition& condition,
859
    const symbolic::Expression& init,
860
    const symbolic::Expression& update,
861
    const sdfg::control_flow::Assignments& assignments,
862
    const DebugInfo& debug_info
863
) {
864
    parent.children_
242✔
865
        .push_back(std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition)));
121✔
866

867
    // Increment element id for body node
868
    this->new_element_id();
121✔
869

870
    parent.transitions_
242✔
871
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
121✔
872
        );
873

874
    return static_cast<For&>(*parent.children_.back().get());
121✔
875
};
×
876

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

892
    parent.children_.insert(
12✔
893
        parent.children_.begin() + index,
6✔
894
        std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition))
6✔
895
    );
896

897
    // Increment element id for body node
898
    this->new_element_id();
6✔
899

900
    parent.transitions_.insert(
12✔
901
        parent.transitions_.begin() + index,
6✔
902
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
6✔
903
    );
904

905
    return static_cast<For&>(*parent.children_.at(index).get());
6✔
906
};
×
907

908
For& StructuredSDFGBuilder::add_for_after(
2✔
909
    Sequence& parent,
910
    ControlFlowNode& child,
911
    const symbolic::Symbol& indvar,
912
    const symbolic::Condition& condition,
913
    const symbolic::Expression& init,
914
    const symbolic::Expression& update,
915
    const sdfg::control_flow::Assignments& assignments,
916
    const DebugInfo& debug_info
917
) {
918
    int index = parent.index(child);
2✔
919
    if (index == -1) {
2✔
920
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
921
    }
922

923
    parent.children_.insert(
4✔
924
        parent.children_.begin() + index + 1,
2✔
925
        std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition))
2✔
926
    );
927

928
    // Increment element id for body node
929
    this->new_element_id();
2✔
930

931
    parent.transitions_.insert(
4✔
932
        parent.transitions_.begin() + index + 1,
2✔
933
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
2✔
934
    );
935

936
    return static_cast<For&>(*parent.children_.at(index + 1).get());
2✔
937
};
×
938

939
Map& StructuredSDFGBuilder::add_map(
51✔
940
    Sequence& parent,
941
    const symbolic::Symbol& indvar,
942
    const symbolic::Condition& condition,
943
    const symbolic::Expression& init,
944
    const symbolic::Expression& update,
945
    const ScheduleType& schedule_type,
946
    const sdfg::control_flow::Assignments& assignments,
947
    const DebugInfo& debug_info
948
) {
949
    parent.children_
102✔
950
        .push_back(std::unique_ptr<
51✔
951
                   Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)));
51✔
952

953
    // Increment element id for body node
954
    this->new_element_id();
51✔
955

956
    parent.transitions_
102✔
957
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
51✔
958
        );
959

960
    return static_cast<Map&>(*parent.children_.back().get());
51✔
961
};
×
962

963
Map& StructuredSDFGBuilder::add_map_before(
6✔
964
    Sequence& parent,
965
    ControlFlowNode& child,
966
    const symbolic::Symbol& indvar,
967
    const symbolic::Condition& condition,
968
    const symbolic::Expression& init,
969
    const symbolic::Expression& update,
970
    const ScheduleType& schedule_type,
971
    const sdfg::control_flow::Assignments& assignments,
972
    const DebugInfo& debug_info
973
) {
974
    int index = parent.index(child);
6✔
975
    if (index == -1) {
6✔
976
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
977
    }
978

979
    parent.children_.insert(
12✔
980
        parent.children_.begin() + index,
6✔
981
        std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)
6✔
982
        )
983
    );
984

985
    // Increment element id for body node
986
    this->new_element_id();
6✔
987

988
    parent.transitions_.insert(
12✔
989
        parent.transitions_.begin() + index,
6✔
990
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
6✔
991
    );
992

993
    return static_cast<Map&>(*parent.children_.at(index).get());
6✔
994
};
×
995

996
Map& StructuredSDFGBuilder::add_map_after(
12✔
997
    Sequence& parent,
998
    ControlFlowNode& child,
999
    const symbolic::Symbol& indvar,
1000
    const symbolic::Condition& condition,
1001
    const symbolic::Expression& init,
1002
    const symbolic::Expression& update,
1003
    const ScheduleType& schedule_type,
1004
    const sdfg::control_flow::Assignments& assignments,
1005
    const DebugInfo& debug_info
1006
) {
1007
    int index = parent.index(child);
12✔
1008
    if (index == -1) {
12✔
1009
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1010
    }
1011

1012
    parent.children_.insert(
24✔
1013
        parent.children_.begin() + index + 1,
12✔
1014
        std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)
12✔
1015
        )
1016
    );
1017

1018
    // Increment element id for body node
1019
    this->new_element_id();
12✔
1020

1021
    parent.transitions_.insert(
24✔
1022
        parent.transitions_.begin() + index + 1,
12✔
1023
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
12✔
1024
    );
1025

1026
    return static_cast<Map&>(*parent.children_.at(index + 1).get());
12✔
1027
};
×
1028

1029
Continue& StructuredSDFGBuilder::
1030
    add_continue(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
14✔
1031
    // Check if continue is in a loop
1032
    analysis::AnalysisManager analysis_manager(this->subject());
14✔
1033
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
14✔
1034
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
14✔
1035
    bool in_loop = false;
14✔
1036
    while (current_scope != nullptr) {
24✔
1037
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
24✔
1038
            in_loop = true;
14✔
1039
            break;
14✔
1040
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
10✔
1041
            throw UnstructuredControlFlowException();
×
1042
        }
1043
        current_scope = scope_tree_analysis.parent_scope(current_scope);
10✔
1044
    }
1045
    if (!in_loop) {
14✔
1046
        throw UnstructuredControlFlowException();
×
1047
    }
1048

1049
    parent.children_.push_back(std::unique_ptr<Continue>(new Continue(this->new_element_id(), debug_info)));
14✔
1050

1051
    parent.transitions_
28✔
1052
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
14✔
1053
        );
1054

1055
    return static_cast<Continue&>(*parent.children_.back().get());
14✔
1056
};
14✔
1057

1058
Break& StructuredSDFGBuilder::
1059
    add_break(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
15✔
1060
    // Check if break is in a loop
1061
    analysis::AnalysisManager analysis_manager(this->subject());
15✔
1062
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
15✔
1063
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
15✔
1064
    bool in_loop = false;
15✔
1065
    while (current_scope != nullptr) {
25✔
1066
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
25✔
1067
            in_loop = true;
15✔
1068
            break;
15✔
1069
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
10✔
1070
            throw UnstructuredControlFlowException();
×
1071
        }
1072
        current_scope = scope_tree_analysis.parent_scope(current_scope);
10✔
1073
    }
1074
    if (!in_loop) {
15✔
1075
        throw UnstructuredControlFlowException();
×
1076
    }
1077

1078
    parent.children_.push_back(std::unique_ptr<Break>(new Break(this->new_element_id(), debug_info)));
15✔
1079

1080
    parent.transitions_
30✔
1081
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
15✔
1082
        );
1083

1084
    return static_cast<Break&>(*parent.children_.back().get());
15✔
1085
};
15✔
1086

1087
Return& StructuredSDFGBuilder::
1088
    add_return(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
14✔
1089
    parent.children_.push_back(std::unique_ptr<Return>(new Return(this->new_element_id(), debug_info)));
14✔
1090

1091
    parent.transitions_
28✔
1092
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
14✔
1093
        );
1094

1095
    return static_cast<Return&>(*parent.children_.back().get());
14✔
1096
};
×
1097

1098
For& StructuredSDFGBuilder::convert_while(
×
1099
    Sequence& parent,
1100
    While& loop,
1101
    const symbolic::Symbol& indvar,
1102
    const symbolic::Condition& condition,
1103
    const symbolic::Expression& init,
1104
    const symbolic::Expression& update
1105
) {
1106
    int index = parent.index(loop);
×
1107
    if (index == -1) {
×
1108
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1109
    }
1110

1111
    auto iter = parent.children_.begin() + index;
×
1112
    auto& new_iter = *parent.children_.insert(
×
1113
        iter + 1,
×
1114
        std::unique_ptr<For>(new For(this->new_element_id(), loop.debug_info(), indvar, init, update, condition))
×
1115
    );
1116

1117
    // Increment element id for body node
1118
    this->new_element_id();
×
1119

1120
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
1121
    this->move_children(loop.root(), for_loop.root());
×
1122

1123
    // Remove while loop
1124
    parent.children_.erase(parent.children_.begin() + index);
×
1125

1126
    return for_loop;
×
1127
};
×
1128

1129
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
8✔
1130
    int index = parent.index(loop);
8✔
1131
    if (index == -1) {
8✔
1132
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1133
    }
1134

1135
    auto iter = parent.children_.begin() + index;
8✔
1136
    auto& new_iter = *parent.children_.insert(
16✔
1137
        iter + 1,
8✔
1138
        std::unique_ptr<Map>(new Map(
16✔
1139
            this->new_element_id(),
8✔
1140
            loop.debug_info(),
8✔
1141
            loop.indvar(),
8✔
1142
            loop.init(),
8✔
1143
            loop.update(),
8✔
1144
            loop.condition(),
8✔
1145
            ScheduleType_Sequential
1146
        ))
1147
    );
1148

1149
    // Increment element id for body node
1150
    this->new_element_id();
8✔
1151

1152
    auto& map = dynamic_cast<Map&>(*new_iter);
8✔
1153
    this->move_children(loop.root(), map.root());
8✔
1154

1155
    // Remove for loop
1156
    parent.children_.erase(parent.children_.begin() + index);
8✔
1157

1158
    return map;
8✔
1159
};
×
1160

1161
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
2✔
1162
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
2✔
1163
    while (!queue.empty()) {
2✔
1164
        auto current = queue.front();
2✔
1165
        queue.pop_front();
2✔
1166

1167
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
2✔
1168
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
2✔
1169
                if (&sequence_stmt->at(i).first == &node) {
2✔
1170
                    return *sequence_stmt;
2✔
1171
                }
1172
                queue.push_back(&sequence_stmt->at(i).first);
×
1173
            }
×
1174
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
1175
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
1176
                queue.push_back(&if_else_stmt->at(i).first);
×
1177
            }
×
1178
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
1179
            queue.push_back(&while_stmt->root());
×
1180
        } else if (auto loop_stmt = dynamic_cast<structured_control_flow::StructuredLoop*>(current)) {
×
1181
            queue.push_back(&loop_stmt->root());
×
1182
        }
×
1183
    }
1184

1185
    return this->structured_sdfg_->root();
×
1186
};
2✔
1187

1188
/***** Section: Dataflow Graph *****/
1189

1190
data_flow::AccessNode& StructuredSDFGBuilder::
1191
    add_access(structured_control_flow::Block& block, const std::string& data, const DebugInfo& debug_info) {
608✔
1192
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
608✔
1193
    auto res = block.dataflow_->nodes_.insert(
1,216✔
1194
        {vertex,
608✔
1195
         std::unique_ptr<data_flow::AccessNode>(
608✔
1196
             new data_flow::AccessNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data)
608✔
1197
         )}
1198
    );
1199

1200
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
608✔
1201
};
×
1202

1203
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
359✔
1204
    structured_control_flow::Block& block,
1205
    const data_flow::TaskletCode code,
1206
    const std::string& output,
1207
    const std::vector<std::string>& inputs,
1208
    const DebugInfo& debug_info
1209
) {
1210
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
359✔
1211
    auto res = block.dataflow_->nodes_.insert(
718✔
1212
        {vertex,
359✔
1213
         std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
718✔
1214
             this->new_element_id(), debug_info, vertex, block.dataflow(), code, output, inputs, symbolic::__true__()
359✔
1215
         ))}
1216
    );
1217

1218
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
359✔
1219
};
×
1220

1221
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
560✔
1222
    structured_control_flow::Block& block,
1223
    data_flow::DataFlowNode& src,
1224
    const std::string& src_conn,
1225
    data_flow::DataFlowNode& dst,
1226
    const std::string& dst_conn,
1227
    const data_flow::Subset& subset,
1228
    const types::IType& base_type,
1229
    const DebugInfo& debug_info
1230
) {
1231
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
560✔
1232
    auto res = block.dataflow_->edges_.insert(
1,120✔
1233
        {edge.first,
1,120✔
1234
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
1,120✔
1235
             this->new_element_id(), debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset, base_type
560✔
1236
         ))}
1237
    );
1238

1239
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
560✔
1240
#ifndef NDEBUG
1241
    memlet.validate(*this->structured_sdfg_);
560✔
1242
#endif
1243

1244
    return memlet;
560✔
1245
};
×
1246

1247
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
41✔
1248
    structured_control_flow::Block& block,
1249
    data_flow::DataFlowNode& src,
1250
    const std::string& src_conn,
1251
    data_flow::DataFlowNode& dst,
1252
    const std::string& dst_conn,
1253
    const data_flow::Subset& begin_subset,
1254
    const data_flow::Subset& end_subset,
1255
    const types::IType& base_type,
1256
    const DebugInfo& debug_info
1257
) {
1258
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
41✔
1259
    auto res = block.dataflow_->edges_.insert(
82✔
1260
        {edge.first,
82✔
1261
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
82✔
1262
             this->new_element_id(),
41✔
1263
             debug_info,
41✔
1264
             edge.first,
41✔
1265
             block.dataflow(),
41✔
1266
             src,
41✔
1267
             src_conn,
41✔
1268
             dst,
41✔
1269
             dst_conn,
41✔
1270
             begin_subset,
41✔
1271
             end_subset,
41✔
1272
             base_type
41✔
1273
         ))}
1274
    );
1275

1276
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
41✔
1277
#ifndef NDEBUG
1278
    memlet.validate(*this->structured_sdfg_);
41✔
1279
#endif
1280

1281
    return memlet;
41✔
1282
};
×
1283

1284
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
96✔
1285
    structured_control_flow::Block& block,
1286
    data_flow::AccessNode& src,
1287
    data_flow::Tasklet& dst,
1288
    const std::string& dst_conn,
1289
    const data_flow::Subset& subset,
1290
    const types::IType& base_type,
1291
    const DebugInfo& debug_info
1292
) {
1293
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, base_type, debug_info);
96✔
1294
};
×
1295

1296
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
87✔
1297
    structured_control_flow::Block& block,
1298
    data_flow::Tasklet& src,
1299
    const std::string& src_conn,
1300
    data_flow::AccessNode& dst,
1301
    const data_flow::Subset& subset,
1302
    const types::IType& base_type,
1303
    const DebugInfo& debug_info
1304
) {
1305
    return this->add_memlet(block, src, src_conn, dst, "void", subset, base_type, debug_info);
87✔
1306
};
×
1307

1308
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
91✔
1309
    structured_control_flow::Block& block,
1310
    data_flow::AccessNode& src,
1311
    data_flow::Tasklet& dst,
1312
    const std::string& dst_conn,
1313
    const data_flow::Subset& subset,
1314
    const DebugInfo& debug_info
1315
) {
1316
    auto& src_type = this->structured_sdfg_->type(src.data());
91✔
1317
    auto& base_type = types::infer_type(*this->structured_sdfg_, src_type, subset);
91✔
1318
    if (base_type.type_id() != types::TypeID::Scalar) {
91✔
1319
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1320
    }
1321
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, src_type, debug_info);
91✔
1322
};
×
1323

1324
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
267✔
1325
    structured_control_flow::Block& block,
1326
    data_flow::Tasklet& src,
1327
    const std::string& src_conn,
1328
    data_flow::AccessNode& dst,
1329
    const data_flow::Subset& subset,
1330
    const DebugInfo& debug_info
1331
) {
1332
    auto& dst_type = this->structured_sdfg_->type(dst.data());
267✔
1333
    auto& base_type = types::infer_type(*this->structured_sdfg_, dst_type, subset);
267✔
1334
    if (base_type.type_id() != types::TypeID::Scalar) {
267✔
1335
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1336
    }
1337
    return this->add_memlet(block, src, src_conn, dst, "void", subset, dst_type, debug_info);
267✔
1338
};
×
1339

1340
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
14✔
1341
    structured_control_flow::Block& block,
1342
    data_flow::AccessNode& src,
1343
    data_flow::LibraryNode& dst,
1344
    const std::string& dst_conn,
1345
    const data_flow::Subset& begin_subset,
1346
    const data_flow::Subset& end_subset,
1347
    const types::IType& base_type,
1348
    const DebugInfo& debug_info
1349
) {
1350
    return this->add_memlet(block, src, "void", dst, dst_conn, begin_subset, end_subset, base_type, debug_info);
14✔
1351
};
×
1352

1353
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
9✔
1354
    structured_control_flow::Block& block,
1355
    data_flow::LibraryNode& src,
1356
    const std::string& src_conn,
1357
    data_flow::AccessNode& dst,
1358
    const data_flow::Subset& begin_subset,
1359
    const data_flow::Subset& end_subset,
1360
    const types::IType& base_type,
1361
    const DebugInfo& debug_info
1362
) {
1363
    return this->add_memlet(block, src, src_conn, dst, "void", begin_subset, end_subset, base_type, debug_info);
9✔
1364
};
×
1365

1366
data_flow::Memlet& StructuredSDFGBuilder::add_reference_memlet(
8✔
1367
    structured_control_flow::Block& block,
1368
    data_flow::AccessNode& src,
1369
    data_flow::AccessNode& dst,
1370
    const data_flow::Subset& subset,
1371
    const types::IType& base_type,
1372
    const DebugInfo& debug_info
1373
) {
1374
    return this->add_memlet(block, src, "void", dst, "ref", subset, base_type, debug_info);
8✔
1375
};
×
1376

1377
data_flow::Memlet& StructuredSDFGBuilder::add_dereference_memlet(
11✔
1378
    structured_control_flow::Block& block,
1379
    data_flow::AccessNode& src,
1380
    data_flow::AccessNode& dst,
1381
    bool derefs_src,
1382
    const types::IType& base_type,
1383
    const DebugInfo& debug_info
1384
) {
1385
    if (derefs_src) {
11✔
1386
        return this->add_memlet(block, src, "void", dst, "deref", {symbolic::zero()}, base_type, debug_info);
6✔
1387
    } else {
1388
        return this->add_memlet(block, src, "deref", dst, "void", {symbolic::zero()}, base_type, debug_info);
5✔
1389
    }
1390
};
11✔
1391

1392
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block, const data_flow::Memlet& edge) {
19✔
1393
    auto& graph = block.dataflow();
19✔
1394
    auto e = edge.edge();
19✔
1395
    boost::remove_edge(e, graph.graph_);
19✔
1396
    graph.edges_.erase(e);
19✔
1397
};
19✔
1398

1399
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
18✔
1400
    auto& graph = block.dataflow();
18✔
1401
    auto v = node.vertex();
18✔
1402
    boost::remove_vertex(v, graph.graph_);
18✔
1403
    graph.nodes_.erase(v);
18✔
1404
};
18✔
1405

1406
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::CodeNode& node) {
8✔
1407
    auto& graph = block.dataflow();
8✔
1408

1409
    std::unordered_set<const data_flow::DataFlowNode*> to_delete = {&node};
8✔
1410

1411
    // Delete incoming
1412
    std::list<const data_flow::Memlet*> iedges;
8✔
1413
    for (auto& iedge : graph.in_edges(node)) {
15✔
1414
        iedges.push_back(&iedge);
7✔
1415
    }
1416
    for (auto iedge : iedges) {
15✔
1417
        auto& src = iedge->src();
7✔
1418
        to_delete.insert(&src);
7✔
1419

1420
        auto edge = iedge->edge();
7✔
1421
        graph.edges_.erase(edge);
7✔
1422
        boost::remove_edge(edge, graph.graph_);
7✔
1423
    }
1424

1425
    // Delete outgoing
1426
    std::list<const data_flow::Memlet*> oedges;
8✔
1427
    for (auto& oedge : graph.out_edges(node)) {
16✔
1428
        oedges.push_back(&oedge);
8✔
1429
    }
1430
    for (auto oedge : oedges) {
16✔
1431
        auto& dst = oedge->dst();
8✔
1432
        to_delete.insert(&dst);
8✔
1433

1434
        auto edge = oedge->edge();
8✔
1435
        graph.edges_.erase(edge);
8✔
1436
        boost::remove_edge(edge, graph.graph_);
8✔
1437
    }
1438

1439
    // Delete nodes
1440
    for (auto obsolete_node : to_delete) {
31✔
1441
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1442
            auto vertex = obsolete_node->vertex();
23✔
1443
            graph.nodes_.erase(vertex);
23✔
1444
            boost::remove_vertex(vertex, graph.graph_);
23✔
1445
        }
23✔
1446
    }
1447
};
8✔
1448

1449
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::AccessNode& node) {
1✔
1450
    auto& graph = block.dataflow();
1✔
1451
    if (graph.out_degree(node) != 0) {
1✔
1452
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1453
    }
1454

1455
    std::list<const data_flow::Memlet*> tmp;
1✔
1456
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
1457
    while (!queue.empty()) {
3✔
1458
        auto current = queue.front();
2✔
1459
        queue.pop_front();
2✔
1460
        if (current != &node) {
2✔
1461
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
1462
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1463
                    continue;
×
1464
                }
1465
            }
×
1466
        }
1✔
1467

1468
        tmp.clear();
2✔
1469
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1470
            tmp.push_back(&iedge);
1✔
1471
        }
1472
        for (auto iedge : tmp) {
3✔
1473
            auto& src = iedge->src();
1✔
1474
            queue.push_back(&src);
1✔
1475

1476
            auto edge = iedge->edge();
1✔
1477
            graph.edges_.erase(edge);
1✔
1478
            boost::remove_edge(edge, graph.graph_);
1✔
1479
        }
1480

1481
        auto vertex = current->vertex();
2✔
1482
        graph.nodes_.erase(vertex);
2✔
1483
        boost::remove_vertex(vertex, graph.graph_);
2✔
1484
    }
1485
};
1✔
1486

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

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

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

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

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

1512
} // namespace builder
1513
} // 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