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

daisytuner / sdfglib / 17559474002

08 Sep 2025 05:49PM UTC coverage: 61.347% (+2.2%) from 59.145%
17559474002

Pull #219

github

web-flow
Merge 2ae413ec1 into b8fdeb232
Pull Request #219: stdlib Library Nodes and ConstantNodes

424 of 1301 new or added lines in 74 files covered. (32.59%)

89 existing lines in 31 files now uncovered.

9318 of 15189 relevant lines covered (61.35%)

109.36 hits per line

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

65.94
/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(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
    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
    if (pdom == nullptr) {
3✔
NEW
74
        return nullptr;
×
75
    }
76

77
    for (auto& edge : out_edges) {
9✔
78
        if (!post_dominates(pdom, &edge->dst(), pdom_tree)) {
6✔
79
            return nullptr;
×
80
        }
81
    }
82

83
    return pdom;
3✔
84
}
3✔
85

86
void StructuredSDFGBuilder::traverse(SDFG& sdfg) {
4✔
87
    // Start of SDFGS
88
    Sequence& root = *structured_sdfg_->root_;
4✔
89
    const State* start_state = &sdfg.start_state();
4✔
90

91
    auto pdom_tree = sdfg.post_dominator_tree();
4✔
92

93
    std::unordered_set<const InterstateEdge*> breaks;
4✔
94
    std::unordered_set<const InterstateEdge*> continues;
4✔
95
    for (auto& edge : sdfg.back_edges()) {
5✔
96
        continues.insert(edge);
1✔
97
    }
98

99
    std::unordered_set<const control_flow::State*> visited;
4✔
100
    this->traverse_with_loop_detection(sdfg, root, start_state, nullptr, continues, breaks, pdom_tree, visited);
4✔
101
};
4✔
102

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

117
    auto in_edges = sdfg.in_edges(*current);
8✔
118

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

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

150
        for (auto& edge : breaks) {
1✔
151
            exit_edges.insert(edge);
×
152
        }
153

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

166
        // 3. Add while loop
167
        While& loop = this->add_while(scope, {}, dbg_info);
1✔
168

169
        std::unordered_set<const control_flow::State*> loop_visited(visited);
1✔
170
        this->traverse_without_loop_detection(
1✔
171
            sdfg, loop.root(), current, exit_state, continues, exit_edges, pdom_tree, loop_visited
1✔
172
        );
173

174
        this->traverse_with_loop_detection(sdfg, scope, exit_state, end, continues, breaks, pdom_tree, visited);
1✔
175
    } else {
1✔
176
        this->traverse_without_loop_detection(sdfg, scope, current, end, continues, breaks, pdom_tree, visited);
7✔
177
    }
178
};
9✔
179

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

198
        if (visited.find(curr) != visited.end()) {
17✔
199
            throw UnstructuredControlFlowException();
×
200
        }
201
        visited.insert(curr);
17✔
202

203
        auto out_edges = sdfg.out_edges(*curr);
17✔
204
        auto out_degree = sdfg.out_degree(*curr);
17✔
205

206
        // Case 1: Sink node
207
        if (out_degree == 0) {
17✔
208
            this->add_block(scope, curr->dataflow(), {}, curr->debug_info());
4✔
209

210
            auto return_state = dynamic_cast<const control_flow::ReturnState*>(curr);
4✔
211
            assert(return_state != nullptr);
4✔
212
            this->add_return(scope, return_state->data(), return_state->unreachable(), {}, return_state->debug_info());
4✔
213
            continue;
4✔
214
        }
215

216
        // Case 2: Transition
217
        if (out_degree == 1) {
13✔
218
            auto& oedge = *out_edges.begin();
10✔
219
            if (!oedge.is_unconditional()) {
10✔
220
                throw UnstructuredControlFlowException();
×
221
            }
222
            this->add_block(scope, curr->dataflow(), oedge.assignments(), curr->debug_info());
10✔
223

224
            if (continues.find(&oedge) != continues.end()) {
10✔
225
                this->add_continue(scope, {}, oedge.debug_info());
×
226
            } else if (breaks.find(&oedge) != breaks.end()) {
10✔
227
                this->add_break(scope, {}, oedge.debug_info());
×
228
            } else {
×
229
                bool starts_loop = false;
10✔
230
                for (auto& iedge : sdfg.in_edges(oedge.dst())) {
23✔
231
                    if (continues.find(&iedge) != continues.end()) {
13✔
232
                        starts_loop = true;
×
233
                        break;
×
234
                    }
235
                }
236
                if (!starts_loop) {
10✔
237
                    queue.push_back(&oedge.dst());
10✔
238
                } else {
10✔
239
                    this->traverse_with_loop_detection(
×
240
                        sdfg, scope, &oedge.dst(), end, continues, breaks, pdom_tree, visited
×
241
                    );
242
                }
243
            }
244
            continue;
10✔
245
        }
246

247
        // Case 3: Branches
248
        if (out_degree > 1) {
3✔
249
            this->add_block(scope, curr->dataflow(), {}, curr->debug_info());
3✔
250

251
            std::vector<const InterstateEdge*> out_edges_vec;
3✔
252
            for (auto& edge : out_edges) {
9✔
253
                out_edges_vec.push_back(&edge);
6✔
254
            }
255

256
            // Best-effort approach: Find end of if-else
257
            // If not found, the branches may repeat paths yielding a large SDFG
258
            const control_flow::State* local_end = this->find_end_of_if_else(sdfg, curr, out_edges_vec, pdom_tree);
3✔
259
            if (local_end == nullptr) {
3✔
260
                local_end = end;
×
261
            }
×
262

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

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

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

302
Function& StructuredSDFGBuilder::function() const { return static_cast<Function&>(*this->structured_sdfg_); };
5,674✔
303

304
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
80✔
305
    : FunctionBuilder(), structured_sdfg_(std::move(sdfg)) {};
80✔
306

307
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
514✔
308
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type)) {};
514✔
309

310
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type, const types::IType& return_type)
5✔
311
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type, return_type)) {};
5✔
312

313
StructuredSDFGBuilder::StructuredSDFGBuilder(SDFG& sdfg)
4✔
314
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(sdfg.name(), sdfg.type(), sdfg.return_type())) {
4✔
315
    for (auto& entry : sdfg.structures_) {
4✔
316
        this->structured_sdfg_->structures_.insert({entry.first, entry.second->clone()});
×
317
    }
318

319
    for (auto& entry : sdfg.containers_) {
11✔
320
        this->structured_sdfg_->containers_.insert({entry.first, entry.second->clone()});
7✔
321
    }
322

323
    for (auto& arg : sdfg.arguments_) {
6✔
324
        this->structured_sdfg_->arguments_.push_back(arg);
2✔
325
    }
326

327
    for (auto& ext : sdfg.externals_) {
5✔
328
        this->structured_sdfg_->externals_.push_back(ext);
1✔
329
        this->structured_sdfg_->externals_linkage_types_[ext] = sdfg.linkage_type(ext);
1✔
330
    }
331

332
    for (auto& entry : sdfg.assumptions_) {
9✔
333
        this->structured_sdfg_->assumptions_.insert({entry.first, entry.second});
5✔
334
    }
335

336
    for (auto& entry : sdfg.metadata_) {
6✔
337
        this->structured_sdfg_->metadata_[entry.first] = entry.second;
2✔
338
    }
339

340
    this->traverse(sdfg);
4✔
341
};
4✔
342

343
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
1,471✔
344

345
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
260✔
346
#ifndef NDEBUG
347
    this->structured_sdfg_->validate();
260✔
348
#endif
349

350
    return std::move(this->structured_sdfg_);
260✔
351
};
352

353
Element* StructuredSDFGBuilder::find_element_by_id(const size_t& element_id) const {
13✔
354
    auto& sdfg = this->subject();
13✔
355
    std::list<Element*> queue = {&sdfg.root()};
13✔
356
    while (!queue.empty()) {
55✔
357
        auto current = queue.front();
55✔
358
        queue.pop_front();
55✔
359

360
        if (current->element_id() == element_id) {
55✔
361
            return current;
13✔
362
        }
363

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

396
    return nullptr;
×
397
};
13✔
398

399
Sequence& StructuredSDFGBuilder::
400
    add_sequence(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
27✔
401
    parent.children_.push_back(std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
27✔
402

403
    parent.transitions_
54✔
404
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
27✔
405
        );
406

407
    return static_cast<Sequence&>(*parent.children_.back().get());
27✔
408
};
×
409

410
Sequence& StructuredSDFGBuilder::add_sequence_before(
10✔
411
    Sequence& parent,
412
    ControlFlowNode& child,
413
    const sdfg::control_flow::Assignments& assignments,
414
    const DebugInfo& debug_info
415
) {
416
    int index = parent.index(child);
10✔
417
    if (index == -1) {
10✔
418
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
419
    }
420

421
    parent.children_.insert(
20✔
422
        parent.children_.begin() + index, std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info))
10✔
423
    );
424

425
    parent.transitions_.insert(
20✔
426
        parent.transitions_.begin() + index,
10✔
427
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
10✔
428
    );
429

430
    return static_cast<Sequence&>(*parent.children_.at(index).get());
10✔
431
};
×
432

433
Sequence& StructuredSDFGBuilder::add_sequence_after(
×
434
    Sequence& parent,
435
    ControlFlowNode& child,
436
    const sdfg::control_flow::Assignments& assignments,
437
    const DebugInfo& debug_info
438
) {
439
    int index = parent.index(child);
×
440
    if (index == -1) {
×
441
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
442
    }
443

444
    parent.children_.insert(
×
445
        parent.children_.begin() + index + 1,
×
446
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info))
×
447
    );
448

449
    parent.transitions_.insert(
×
450
        parent.transitions_.begin() + index + 1,
×
451
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
452
    );
453

454
    return static_cast<Sequence&>(*parent.children_.at(index + 1).get());
×
455
};
×
456

457
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::
458
    add_sequence_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
459
    int index = parent.index(child);
×
460
    if (index == -1) {
×
461
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
462
    }
463

464
    parent.children_.insert(
×
465
        parent.children_.begin() + index, std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info))
×
466
    );
467

468
    parent.transitions_.insert(
×
469
        parent.transitions_.begin() + index,
×
470
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
471
    );
472

473
    auto new_entry = parent.at(index);
×
474
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
×
475

476
    return {new_block, new_entry.second};
×
477
};
×
478

479
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t index) {
25✔
480
    parent.children_.erase(parent.children_.begin() + index);
25✔
481
    parent.transitions_.erase(parent.transitions_.begin() + index);
25✔
482
};
25✔
483

484
void StructuredSDFGBuilder::remove_children(Sequence& parent) {
×
485
    parent.children_.clear();
×
486
    parent.transitions_.clear();
×
487
};
×
488

489
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target) {
10✔
490
    this->move_child(source, source_index, target, target.size());
10✔
491
};
10✔
492

493
void StructuredSDFGBuilder::move_child(Sequence& source, size_t source_index, Sequence& target, size_t target_index) {
10✔
494
    auto node_ptr = std::move(source.children_.at(source_index));
10✔
495
    auto trans_ptr = std::move(source.transitions_.at(source_index));
10✔
496
    source.children_.erase(source.children_.begin() + source_index);
10✔
497
    source.transitions_.erase(source.transitions_.begin() + source_index);
10✔
498

499
    trans_ptr->parent_ = &target;
10✔
500
    target.children_.insert(target.children_.begin() + target_index, std::move(node_ptr));
10✔
501
    target.transitions_.insert(target.transitions_.begin() + target_index, std::move(trans_ptr));
10✔
502
};
10✔
503

504
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target) {
23✔
505
    this->move_children(source, target, target.size());
23✔
506
};
23✔
507

508
void StructuredSDFGBuilder::move_children(Sequence& source, Sequence& target, size_t target_index) {
23✔
509
    target.children_.insert(
46✔
510
        target.children_.begin() + target_index,
23✔
511
        std::make_move_iterator(source.children_.begin()),
23✔
512
        std::make_move_iterator(source.children_.end())
23✔
513
    );
514
    target.transitions_.insert(
46✔
515
        target.transitions_.begin() + target_index,
23✔
516
        std::make_move_iterator(source.transitions_.begin()),
23✔
517
        std::make_move_iterator(source.transitions_.end())
23✔
518
    );
519
    for (auto& trans : target.transitions_) {
52✔
520
        trans->parent_ = &target;
29✔
521
    }
522
    source.children_.clear();
23✔
523
    source.transitions_.clear();
23✔
524
};
23✔
525

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

530
    parent.transitions_
1,008✔
531
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
504✔
532
        );
533

534
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.back());
504✔
535
    (*new_block.dataflow_).parent_ = &new_block;
504✔
536

537
    return new_block;
504✔
538
};
×
539

540
Block& StructuredSDFGBuilder::add_block(
20✔
541
    Sequence& parent,
542
    const data_flow::DataFlowGraph& data_flow_graph,
543
    const sdfg::control_flow::Assignments& assignments,
544
    const DebugInfo& debug_info
545
) {
546
    parent.children_.push_back(std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
20✔
547

548
    parent.transitions_
40✔
549
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
20✔
550
        );
551

552
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.back());
20✔
553
    (*new_block.dataflow_).parent_ = &new_block;
20✔
554

555
    this->add_dataflow(data_flow_graph, new_block);
20✔
556

557
    return new_block;
20✔
558
};
×
559

560
Block& StructuredSDFGBuilder::add_block_before(
11✔
561
    Sequence& parent,
562
    ControlFlowNode& child,
563
    const sdfg::control_flow::Assignments& assignments,
564
    const DebugInfo& debug_info
565
) {
566
    int index = parent.index(child);
11✔
567
    if (index == -1) {
11✔
568
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
569
    }
570

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

574
    parent.transitions_.insert(
22✔
575
        parent.transitions_.begin() + index,
11✔
576
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
11✔
577
    );
578

579
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index));
11✔
580
    (*new_block.dataflow_).parent_ = &new_block;
11✔
581

582
    return new_block;
11✔
583
};
×
584

585
Block& StructuredSDFGBuilder::add_block_before(
×
586
    Sequence& parent,
587
    ControlFlowNode& child,
588
    data_flow::DataFlowGraph& data_flow_graph,
589
    const sdfg::control_flow::Assignments& assignments,
590
    const DebugInfo& debug_info
591
) {
592
    int index = parent.index(child);
×
593
    if (index == -1) {
×
594
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
595
    }
596

597
    parent.children_
×
598
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
599

600
    parent.transitions_.insert(
×
601
        parent.transitions_.begin() + index,
×
602
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
603
    );
604

605
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index));
×
606
    (*new_block.dataflow_).parent_ = &new_block;
×
607
    this->add_dataflow(data_flow_graph, new_block);
×
608

609
    return new_block;
×
610
};
×
611

612
Block& StructuredSDFGBuilder::add_block_after(
3✔
613
    Sequence& parent,
614
    ControlFlowNode& child,
615
    const sdfg::control_flow::Assignments& assignments,
616
    const DebugInfo& debug_info
617
) {
618
    int index = parent.index(child);
3✔
619
    if (index == -1) {
3✔
620
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
621
    }
622

623
    parent.children_.insert(
6✔
624
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
3✔
625
    );
626

627
    parent.transitions_.insert(
6✔
628
        parent.transitions_.begin() + index + 1,
3✔
629
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
3✔
630
    );
631

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

635
    return new_block;
3✔
636
};
×
637

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

650
    parent.children_.insert(
×
651
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
652
    );
653

654
    parent.transitions_.insert(
×
655
        parent.transitions_.begin() + index + 1,
×
656
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
657
    );
658

659
    auto& new_block = static_cast<structured_control_flow::Block&>(*parent.children_.at(index + 1));
×
660
    (*new_block.dataflow_).parent_ = &new_block;
×
661
    this->add_dataflow(data_flow_graph, new_block);
×
662

663
    return new_block;
×
664
};
×
665

666
std::pair<Block&, Transition&> StructuredSDFGBuilder::
667
    add_block_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
668
    int index = parent.index(child);
×
669
    if (index == -1) {
×
670
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
671
    }
672

673
    parent.children_
×
674
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
675

676
    parent.transitions_.insert(
×
677
        parent.transitions_.begin() + index,
×
678
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
679
    );
680

681
    auto new_entry = parent.at(index);
×
682
    auto& new_block = static_cast<structured_control_flow::Block&>(new_entry.first);
×
683
    (*new_block.dataflow_).parent_ = &new_block;
×
684

685
    return {new_block, new_entry.second};
×
686
};
×
687

688
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
×
689
    Sequence& parent, ControlFlowNode& child, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
690
) {
691
    int index = parent.index(child);
×
692
    if (index == -1) {
×
693
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
694
    }
695

696
    parent.children_
×
697
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
698

699
    parent.transitions_.insert(
×
700
        parent.transitions_.begin() + index,
×
701
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
702
    );
703

704
    auto new_entry = parent.at(index);
×
705
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
706
    (*new_block.dataflow_).parent_ = &new_block;
×
707

708
    this->add_dataflow(data_flow_graph, new_block);
×
709

710
    return {new_block, new_entry.second};
×
711
};
×
712

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

720
    parent.children_.insert(
×
721
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
722
    );
723

724
    parent.transitions_.insert(
×
725
        parent.transitions_.begin() + index + 1,
×
726
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
727
    );
728

729
    auto new_entry = parent.at(index + 1);
×
730
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
731
    (*new_block.dataflow_).parent_ = &new_block;
×
732

733
    return {new_block, new_entry.second};
×
734
};
×
735

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

744
    parent.children_.insert(
×
745
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
746
    );
747

748
    parent.transitions_.insert(
×
749
        parent.transitions_.begin() + index + 1,
×
750
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
751
    );
752

753
    auto new_entry = parent.at(index + 1);
×
754
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
755
    (*new_block.dataflow_).parent_ = &new_block;
×
756

757
    this->add_dataflow(data_flow_graph, new_block);
×
758

759
    return {new_block, new_entry.second};
×
760
};
×
761

762
IfElse& StructuredSDFGBuilder::
763
    add_if_else(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
41✔
764
    parent.children_.push_back(std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
41✔
765

766
    parent.transitions_
82✔
767
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
41✔
768
        );
769

770
    return static_cast<IfElse&>(*parent.children_.back().get());
41✔
771
};
×
772

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

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

787
    parent.transitions_.insert(
2✔
788
        parent.transitions_.begin() + index,
1✔
789
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
1✔
790
    );
791

792
    return static_cast<IfElse&>(*parent.children_.at(index));
1✔
793
};
×
794

795
IfElse& StructuredSDFGBuilder::add_if_else_after(
×
796
    Sequence& parent,
797
    ControlFlowNode& child,
798
    const sdfg::control_flow::Assignments& assignments,
799
    const DebugInfo& debug_info
800
) {
801
    int index = parent.index(child);
×
802
    if (index == -1) {
×
803
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
804
    }
805

806
    parent.children_.insert(
×
807
        parent.children_.begin() + index + 1, std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info))
×
808
    );
809

810
    parent.transitions_.insert(
×
811
        parent.transitions_.begin() + index + 1,
×
812
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
×
813
    );
814

815
    return static_cast<IfElse&>(*parent.children_.at(index + 1));
×
816
};
×
817

818
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::
819
    add_if_else_before(Sequence& parent, ControlFlowNode& child, const DebugInfo& debug_info) {
×
820
    int index = parent.index(child);
×
821
    if (index == -1) {
×
822
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
823
    }
824

825
    parent.children_
×
826
        .insert(parent.children_.begin() + index, std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
×
827

828
    parent.transitions_.insert(
×
829
        parent.transitions_.begin() + index,
×
830
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
831
    );
832

833
    auto new_entry = parent.at(index);
×
834
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
×
835

836
    return {new_block, new_entry.second};
×
837
};
×
838

839
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond, const DebugInfo& debug_info) {
66✔
840
    scope.cases_.push_back(std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
66✔
841

842
    scope.conditions_.push_back(cond);
66✔
843
    return *scope.cases_.back();
66✔
844
};
×
845

846
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t index, const DebugInfo& debug_info) {
×
847
    scope.cases_.erase(scope.cases_.begin() + index);
×
848
    scope.conditions_.erase(scope.conditions_.begin() + index);
×
849
};
×
850

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

855
    // Increment element id for body node
856
    this->new_element_id();
33✔
857

858
    parent.transitions_
66✔
859
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
33✔
860
        );
861

862
    return static_cast<While&>(*parent.children_.back().get());
33✔
863
};
×
864

865
For& StructuredSDFGBuilder::add_for(
119✔
866
    Sequence& parent,
867
    const symbolic::Symbol& indvar,
868
    const symbolic::Condition& condition,
869
    const symbolic::Expression& init,
870
    const symbolic::Expression& update,
871
    const sdfg::control_flow::Assignments& assignments,
872
    const DebugInfo& debug_info
873
) {
874
    parent.children_
238✔
875
        .push_back(std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition)));
119✔
876

877
    // Increment element id for body node
878
    this->new_element_id();
119✔
879

880
    parent.transitions_
238✔
881
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
119✔
882
        );
883

884
    return static_cast<For&>(*parent.children_.back().get());
119✔
885
};
×
886

887
For& StructuredSDFGBuilder::add_for_before(
6✔
888
    Sequence& parent,
889
    ControlFlowNode& child,
890
    const symbolic::Symbol& indvar,
891
    const symbolic::Condition& condition,
892
    const symbolic::Expression& init,
893
    const symbolic::Expression& update,
894
    const sdfg::control_flow::Assignments& assignments,
895
    const DebugInfo& debug_info
896
) {
897
    int index = parent.index(child);
6✔
898
    if (index == -1) {
6✔
899
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
900
    }
901

902
    parent.children_.insert(
12✔
903
        parent.children_.begin() + index,
6✔
904
        std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition))
6✔
905
    );
906

907
    // Increment element id for body node
908
    this->new_element_id();
6✔
909

910
    parent.transitions_.insert(
12✔
911
        parent.transitions_.begin() + index,
6✔
912
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
6✔
913
    );
914

915
    return static_cast<For&>(*parent.children_.at(index).get());
6✔
916
};
×
917

918
For& StructuredSDFGBuilder::add_for_after(
2✔
919
    Sequence& parent,
920
    ControlFlowNode& child,
921
    const symbolic::Symbol& indvar,
922
    const symbolic::Condition& condition,
923
    const symbolic::Expression& init,
924
    const symbolic::Expression& update,
925
    const sdfg::control_flow::Assignments& assignments,
926
    const DebugInfo& debug_info
927
) {
928
    int index = parent.index(child);
2✔
929
    if (index == -1) {
2✔
930
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
931
    }
932

933
    parent.children_.insert(
4✔
934
        parent.children_.begin() + index + 1,
2✔
935
        std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition))
2✔
936
    );
937

938
    // Increment element id for body node
939
    this->new_element_id();
2✔
940

941
    parent.transitions_.insert(
4✔
942
        parent.transitions_.begin() + index + 1,
2✔
943
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
2✔
944
    );
945

946
    return static_cast<For&>(*parent.children_.at(index + 1).get());
2✔
947
};
×
948

949
Map& StructuredSDFGBuilder::add_map(
51✔
950
    Sequence& parent,
951
    const symbolic::Symbol& indvar,
952
    const symbolic::Condition& condition,
953
    const symbolic::Expression& init,
954
    const symbolic::Expression& update,
955
    const ScheduleType& schedule_type,
956
    const sdfg::control_flow::Assignments& assignments,
957
    const DebugInfo& debug_info
958
) {
959
    parent.children_
102✔
960
        .push_back(std::unique_ptr<
51✔
961
                   Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)));
51✔
962

963
    // Increment element id for body node
964
    this->new_element_id();
51✔
965

966
    parent.transitions_
102✔
967
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
51✔
968
        );
969

970
    return static_cast<Map&>(*parent.children_.back().get());
51✔
971
};
×
972

973
Map& StructuredSDFGBuilder::add_map_before(
6✔
974
    Sequence& parent,
975
    ControlFlowNode& child,
976
    const symbolic::Symbol& indvar,
977
    const symbolic::Condition& condition,
978
    const symbolic::Expression& init,
979
    const symbolic::Expression& update,
980
    const ScheduleType& schedule_type,
981
    const sdfg::control_flow::Assignments& assignments,
982
    const DebugInfo& debug_info
983
) {
984
    int index = parent.index(child);
6✔
985
    if (index == -1) {
6✔
986
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
987
    }
988

989
    parent.children_.insert(
12✔
990
        parent.children_.begin() + index,
6✔
991
        std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)
6✔
992
        )
993
    );
994

995
    // Increment element id for body node
996
    this->new_element_id();
6✔
997

998
    parent.transitions_.insert(
12✔
999
        parent.transitions_.begin() + index,
6✔
1000
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
6✔
1001
    );
1002

1003
    return static_cast<Map&>(*parent.children_.at(index).get());
6✔
1004
};
×
1005

1006
Map& StructuredSDFGBuilder::add_map_after(
12✔
1007
    Sequence& parent,
1008
    ControlFlowNode& child,
1009
    const symbolic::Symbol& indvar,
1010
    const symbolic::Condition& condition,
1011
    const symbolic::Expression& init,
1012
    const symbolic::Expression& update,
1013
    const ScheduleType& schedule_type,
1014
    const sdfg::control_flow::Assignments& assignments,
1015
    const DebugInfo& debug_info
1016
) {
1017
    int index = parent.index(child);
12✔
1018
    if (index == -1) {
12✔
1019
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1020
    }
1021

1022
    parent.children_.insert(
24✔
1023
        parent.children_.begin() + index + 1,
12✔
1024
        std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)
12✔
1025
        )
1026
    );
1027

1028
    // Increment element id for body node
1029
    this->new_element_id();
12✔
1030

1031
    parent.transitions_.insert(
24✔
1032
        parent.transitions_.begin() + index + 1,
12✔
1033
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
12✔
1034
    );
1035

1036
    return static_cast<Map&>(*parent.children_.at(index + 1).get());
12✔
1037
};
×
1038

1039
Continue& StructuredSDFGBuilder::
1040
    add_continue(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
14✔
1041
    // Check if continue is in a loop
1042
    analysis::AnalysisManager analysis_manager(this->subject());
14✔
1043
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
14✔
1044
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
14✔
1045
    bool in_loop = false;
14✔
1046
    while (current_scope != nullptr) {
24✔
1047
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
24✔
1048
            in_loop = true;
14✔
1049
            break;
14✔
1050
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
10✔
1051
            throw UnstructuredControlFlowException();
×
1052
        }
1053
        current_scope = scope_tree_analysis.parent_scope(current_scope);
10✔
1054
    }
1055
    if (!in_loop) {
14✔
1056
        throw UnstructuredControlFlowException();
×
1057
    }
1058

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

1061
    parent.transitions_
28✔
1062
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
14✔
1063
        );
1064

1065
    return static_cast<Continue&>(*parent.children_.back().get());
14✔
1066
};
14✔
1067

1068
Break& StructuredSDFGBuilder::
1069
    add_break(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
15✔
1070
    // Check if break is in a loop
1071
    analysis::AnalysisManager analysis_manager(this->subject());
15✔
1072
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
15✔
1073
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
15✔
1074
    bool in_loop = false;
15✔
1075
    while (current_scope != nullptr) {
25✔
1076
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
25✔
1077
            in_loop = true;
15✔
1078
            break;
15✔
1079
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
10✔
1080
            throw UnstructuredControlFlowException();
×
1081
        }
1082
        current_scope = scope_tree_analysis.parent_scope(current_scope);
10✔
1083
    }
1084
    if (!in_loop) {
15✔
1085
        throw UnstructuredControlFlowException();
×
1086
    }
1087

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

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

1094
    return static_cast<Break&>(*parent.children_.back().get());
15✔
1095
};
15✔
1096

1097
Return& StructuredSDFGBuilder::add_return(
16✔
1098
    Sequence& parent,
1099
    const std::string& data,
1100
    bool unreachable,
1101
    const sdfg::control_flow::Assignments& assignments,
1102
    const DebugInfo& debug_info
1103
) {
1104
    parent.children_.push_back(std::unique_ptr<Return>(new Return(this->new_element_id(), debug_info, data, unreachable)
16✔
1105
    ));
1106

1107
    parent.transitions_
32✔
1108
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
16✔
1109
        );
1110

1111
    return static_cast<Return&>(*parent.children_.back().get());
16✔
1112
};
×
1113

1114
For& StructuredSDFGBuilder::convert_while(
×
1115
    Sequence& parent,
1116
    While& loop,
1117
    const symbolic::Symbol& indvar,
1118
    const symbolic::Condition& condition,
1119
    const symbolic::Expression& init,
1120
    const symbolic::Expression& update
1121
) {
1122
    int index = parent.index(loop);
×
1123
    if (index == -1) {
×
1124
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1125
    }
1126

1127
    auto iter = parent.children_.begin() + index;
×
1128
    auto& new_iter = *parent.children_.insert(
×
1129
        iter + 1,
×
1130
        std::unique_ptr<For>(new For(this->new_element_id(), loop.debug_info(), indvar, init, update, condition))
×
1131
    );
1132

1133
    // Increment element id for body node
1134
    this->new_element_id();
×
1135

1136
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
1137
    this->move_children(loop.root(), for_loop.root());
×
1138

1139
    // Remove while loop
1140
    parent.children_.erase(parent.children_.begin() + index);
×
1141

1142
    return for_loop;
×
1143
};
×
1144

1145
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
8✔
1146
    int index = parent.index(loop);
8✔
1147
    if (index == -1) {
8✔
1148
        throw InvalidSDFGException("StructuredSDFGBuilder: Child not found");
×
1149
    }
1150

1151
    auto iter = parent.children_.begin() + index;
8✔
1152
    auto& new_iter = *parent.children_.insert(
16✔
1153
        iter + 1,
8✔
1154
        std::unique_ptr<Map>(new Map(
16✔
1155
            this->new_element_id(),
8✔
1156
            loop.debug_info(),
8✔
1157
            loop.indvar(),
8✔
1158
            loop.init(),
8✔
1159
            loop.update(),
8✔
1160
            loop.condition(),
8✔
1161
            ScheduleType_Sequential::create()
8✔
1162
        ))
1163
    );
1164

1165
    // Increment element id for body node
1166
    this->new_element_id();
8✔
1167

1168
    auto& map = dynamic_cast<Map&>(*new_iter);
8✔
1169
    this->move_children(loop.root(), map.root());
8✔
1170

1171
    // Remove for loop
1172
    parent.children_.erase(parent.children_.begin() + index);
8✔
1173

1174
    return map;
8✔
1175
};
×
1176

1177
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
2✔
1178
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
2✔
1179
    while (!queue.empty()) {
2✔
1180
        auto current = queue.front();
2✔
1181
        queue.pop_front();
2✔
1182

1183
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
2✔
1184
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
2✔
1185
                if (&sequence_stmt->at(i).first == &node) {
2✔
1186
                    return *sequence_stmt;
2✔
1187
                }
1188
                queue.push_back(&sequence_stmt->at(i).first);
×
1189
            }
×
1190
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
1191
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
1192
                queue.push_back(&if_else_stmt->at(i).first);
×
1193
            }
×
1194
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
1195
            queue.push_back(&while_stmt->root());
×
1196
        } else if (auto loop_stmt = dynamic_cast<structured_control_flow::StructuredLoop*>(current)) {
×
1197
            queue.push_back(&loop_stmt->root());
×
1198
        }
×
1199
    }
1200

1201
    return this->structured_sdfg_->root();
×
1202
};
2✔
1203

1204
/***** Section: Dataflow Graph *****/
1205

1206
data_flow::AccessNode& StructuredSDFGBuilder::
1207
    add_access(structured_control_flow::Block& block, const std::string& data, const DebugInfo& debug_info) {
605✔
1208
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
605✔
1209
    auto res = block.dataflow_->nodes_.insert(
1,210✔
1210
        {vertex,
605✔
1211
         std::unique_ptr<data_flow::AccessNode>(
605✔
1212
             new data_flow::AccessNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data)
605✔
1213
         )}
1214
    );
1215

1216
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
605✔
1217
};
×
1218

1219
data_flow::ConstantNode& StructuredSDFGBuilder::add_constant(
52✔
1220
    structured_control_flow::Block& block, const std::string& data, const types::IType& type, const DebugInfo& debug_info
1221
) {
1222
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
52✔
1223
    auto res = block.dataflow_->nodes_.insert(
104✔
1224
        {vertex,
52✔
1225
         std::unique_ptr<data_flow::ConstantNode>(
52✔
1226
             new data_flow::ConstantNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data, type)
52✔
1227
         )}
1228
    );
1229

1230
    return dynamic_cast<data_flow::ConstantNode&>(*(res.first->second));
52✔
NEW
1231
};
×
1232

1233

1234
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
360✔
1235
    structured_control_flow::Block& block,
1236
    const data_flow::TaskletCode code,
1237
    const std::string& output,
1238
    const std::vector<std::string>& inputs,
1239
    const DebugInfo& debug_info
1240
) {
1241
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
360✔
1242
    auto res = block.dataflow_->nodes_.insert(
720✔
1243
        {vertex,
360✔
1244
         std::unique_ptr<data_flow::Tasklet>(
360✔
1245
             new data_flow::Tasklet(this->new_element_id(), debug_info, vertex, block.dataflow(), code, output, inputs)
360✔
1246
         )}
1247
    );
1248

1249
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
360✔
1250
};
×
1251

1252
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
652✔
1253
    structured_control_flow::Block& block,
1254
    data_flow::DataFlowNode& src,
1255
    const std::string& src_conn,
1256
    data_flow::DataFlowNode& dst,
1257
    const std::string& dst_conn,
1258
    const data_flow::Subset& subset,
1259
    const types::IType& base_type,
1260
    const DebugInfo& debug_info
1261
) {
1262
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
652✔
1263
    auto res = block.dataflow_->edges_.insert(
1,304✔
1264
        {edge.first,
1,304✔
1265
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
1,304✔
1266
             this->new_element_id(), debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset, base_type
652✔
1267
         ))}
1268
    );
1269

1270
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
652✔
1271
#ifndef NDEBUG
1272
    memlet.validate(*this->structured_sdfg_);
652✔
1273
#endif
1274

1275
    return memlet;
652✔
1276
};
×
1277

1278
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
101✔
1279
    structured_control_flow::Block& block,
1280
    data_flow::AccessNode& src,
1281
    data_flow::Tasklet& dst,
1282
    const std::string& dst_conn,
1283
    const data_flow::Subset& subset,
1284
    const types::IType& base_type,
1285
    const DebugInfo& debug_info
1286
) {
1287
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, base_type, debug_info);
101✔
1288
};
×
1289

1290
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
87✔
1291
    structured_control_flow::Block& block,
1292
    data_flow::Tasklet& src,
1293
    const std::string& src_conn,
1294
    data_flow::AccessNode& dst,
1295
    const data_flow::Subset& subset,
1296
    const types::IType& base_type,
1297
    const DebugInfo& debug_info
1298
) {
1299
    return this->add_memlet(block, src, src_conn, dst, "void", subset, base_type, debug_info);
87✔
1300
};
×
1301

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

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

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

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

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

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

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

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

1403
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::CodeNode& node) {
9✔
1404
    auto& graph = block.dataflow();
9✔
1405

1406
    std::unordered_set<const data_flow::DataFlowNode*> to_delete = {&node};
9✔
1407

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

1417
        auto edge = iedge->edge();
15✔
1418
        graph.edges_.erase(edge);
15✔
1419
        boost::remove_edge(edge, graph.graph_);
15✔
1420
    }
1421

1422
    // Delete outgoing
1423
    std::list<const data_flow::Memlet*> oedges;
9✔
1424
    for (auto& oedge : graph.out_edges(node)) {
18✔
1425
        oedges.push_back(&oedge);
9✔
1426
    }
1427
    for (auto oedge : oedges) {
18✔
1428
        auto& dst = oedge->dst();
9✔
1429
        to_delete.insert(&dst);
9✔
1430

1431
        auto edge = oedge->edge();
9✔
1432
        graph.edges_.erase(edge);
9✔
1433
        boost::remove_edge(edge, graph.graph_);
9✔
1434
    }
1435

1436
    // Delete nodes
1437
    for (auto obsolete_node : to_delete) {
42✔
1438
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
33✔
1439
            auto vertex = obsolete_node->vertex();
33✔
1440
            graph.nodes_.erase(vertex);
33✔
1441
            boost::remove_vertex(vertex, graph.graph_);
33✔
1442
        }
33✔
1443
    }
1444
};
9✔
1445

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

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

UNCOV
1465
        tmp.clear();
×
UNCOV
1466
        for (auto& iedge : graph.in_edges(*current)) {
×
UNCOV
1467
            tmp.push_back(&iedge);
×
1468
        }
UNCOV
1469
        for (auto iedge : tmp) {
×
UNCOV
1470
            auto& src = iedge->src();
×
UNCOV
1471
            queue.push_back(&src);
×
1472

UNCOV
1473
            auto edge = iedge->edge();
×
UNCOV
1474
            graph.edges_.erase(edge);
×
UNCOV
1475
            boost::remove_edge(edge, graph.graph_);
×
1476
        }
1477

UNCOV
1478
        auto vertex = current->vertex();
×
UNCOV
1479
        graph.nodes_.erase(vertex);
×
UNCOV
1480
        boost::remove_vertex(vertex, graph.graph_);
×
1481
    }
UNCOV
1482
};
×
1483

1484
void StructuredSDFGBuilder::add_dataflow(const data_flow::DataFlowGraph& from, Block& to) {
20✔
1485
    auto& to_dataflow = to.dataflow();
20✔
1486

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

1494
    for (auto& entry : from.edges_) {
20✔
1495
        auto src = node_mapping[entry.second->src().vertex()];
×
1496
        auto dst = node_mapping[entry.second->dst().vertex()];
×
1497

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

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

1509
} // namespace builder
1510
} // 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