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

daisytuner / sdfglib / 16779684622

06 Aug 2025 02:21PM UTC coverage: 64.3% (-1.0%) from 65.266%
16779684622

push

github

web-flow
Merge pull request #172 from daisytuner/opaque-pointers

Opaque pointers, typed memlets, untyped tasklet connectors

330 of 462 new or added lines in 38 files covered. (71.43%)

382 existing lines in 30 files now uncovered.

8865 of 13787 relevant lines covered (64.3%)

116.73 hits per line

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

76.86
/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/types/utils.h"
11

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

15
namespace sdfg {
16
namespace builder {
17

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

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

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

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

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

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

61
    return false;
×
62
}
6✔
63

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

78
    return pdom;
3✔
79
}
3✔
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

294
Function& StructuredSDFGBuilder::function() const { return static_cast<Function&>(*this->structured_sdfg_); };
4,963✔
295

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

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

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

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

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

316
    for (auto& ext : sdfg.externals_) {
5✔
317
        this->structured_sdfg_->externals_.push_back(ext);
1✔
318
    }
319

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

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

328
    this->traverse(sdfg);
4✔
329
};
4✔
330

331
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
883✔
332

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

338
    return std::move(this->structured_sdfg_);
406✔
339
};
340

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

348
        if (current->element_id() == element_id) {
3✔
349
            return current;
2✔
350
        }
351

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

384
    return nullptr;
×
385
};
2✔
386

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

391
    parent.transitions_
60✔
392
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
30✔
393
        );
394

395
    return static_cast<Sequence&>(*parent.children_.back().get());
30✔
396
};
×
397

398
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::
399
    add_sequence_before(Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
9✔
400
    // Insert block before current block
401
    int index = -1;
9✔
402
    for (size_t i = 0; i < parent.children_.size(); i++) {
9✔
403
        if (parent.children_.at(i).get() == &block) {
9✔
404
            index = i;
9✔
405
            break;
9✔
406
        }
407
    }
×
408
    if (index == -1) {
9✔
409
        throw InvalidSDFGException("StructuredSDFGBuilder: Block not found");
×
410
    }
411

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

416
    parent.transitions_.insert(
18✔
417
        parent.transitions_.begin() + index,
9✔
418
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
9✔
419
    );
420

421
    auto new_entry = parent.at(index);
9✔
422
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
9✔
423

424
    return {new_block, new_entry.second};
9✔
425
};
×
426

427
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t i) {
5✔
428
    parent.children_.erase(parent.children_.begin() + i);
5✔
429
    parent.transitions_.erase(parent.transitions_.begin() + i);
5✔
430
};
5✔
431

432
void StructuredSDFGBuilder::remove_child(Sequence& parent, ControlFlowNode& child) {
10✔
433
    int index = -1;
10✔
434
    for (size_t i = 0; i < parent.children_.size(); i++) {
18✔
435
        if (parent.children_.at(i).get() == &child) {
18✔
436
            index = i;
10✔
437
            break;
10✔
438
        }
439
    }
8✔
440

441
    parent.children_.erase(parent.children_.begin() + index);
10✔
442
    parent.transitions_.erase(parent.transitions_.begin() + index);
10✔
443
};
10✔
444

445
void StructuredSDFGBuilder::insert_children(Sequence& parent, Sequence& other, size_t i) {
11✔
446
    parent.children_.insert(
22✔
447
        parent.children_.begin() + i,
11✔
448
        std::make_move_iterator(other.children_.begin()),
11✔
449
        std::make_move_iterator(other.children_.end())
11✔
450
    );
451
    parent.transitions_.insert(
22✔
452
        parent.transitions_.begin() + i,
11✔
453
        std::make_move_iterator(other.transitions_.begin()),
11✔
454
        std::make_move_iterator(other.transitions_.end())
11✔
455
    );
456
    for (auto& trans : parent.transitions_) {
23✔
457
        trans->parent_ = &parent;
12✔
458
    }
459
    other.children_.clear();
11✔
460
    other.transitions_.clear();
11✔
461
};
11✔
462

463
void StructuredSDFGBuilder::insert(ControlFlowNode& node, Sequence& source, Sequence& target, const DebugInfo& debug_info) {
1✔
464
    // Insert node into target sequence
465
    int index = -1;
1✔
466
    for (size_t i = 0; i < source.children_.size(); i++) {
2✔
467
        if (source.children_.at(i).get() == &node) {
2✔
468
            index = i;
1✔
469
            break;
1✔
470
        }
471
    }
1✔
472
    if (index == -1) {
1✔
473
        throw InvalidSDFGException("StructuredSDFGBuilder: Node not found in source sequence");
×
474
    }
475

476
    auto node_ptr = std::move(source.children_.at(index));
1✔
477
    source.children_.erase(source.children_.begin() + index);
1✔
478
    source.transitions_.erase(source.transitions_.begin() + index);
1✔
479

480
    target.children_.push_back(std::move(node_ptr));
1✔
481
    target.transitions_.push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, target)
1✔
482
    ));
483
};
1✔
484

485
Block& StructuredSDFGBuilder::
486
    add_block(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
467✔
487
    parent.children_.push_back(std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
467✔
488

489
    parent.transitions_
934✔
490
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
467✔
491
        );
492

493
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
467✔
494
    (*new_block.dataflow_).parent_ = &new_block;
467✔
495

496
    return new_block;
467✔
497
};
×
498

499
Block& StructuredSDFGBuilder::add_block(
18✔
500
    Sequence& parent,
501
    const data_flow::DataFlowGraph& data_flow_graph,
502
    const sdfg::control_flow::Assignments& assignments,
503
    const DebugInfo& debug_info
504
) {
505
    parent.children_.push_back(std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
18✔
506

507
    parent.transitions_
36✔
508
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
18✔
509
        );
510

511
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
18✔
512
    (*new_block.dataflow_).parent_ = &new_block;
18✔
513

514
    this->add_dataflow(data_flow_graph, new_block);
18✔
515

516
    return new_block;
18✔
517
};
×
518

519
std::pair<Block&, Transition&> StructuredSDFGBuilder::
520
    add_block_before(Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
11✔
521
    // Insert block before current block
522
    int index = -1;
11✔
523
    for (size_t i = 0; i < parent.children_.size(); i++) {
11✔
524
        if (parent.children_.at(i).get() == &block) {
11✔
525
            index = i;
11✔
526
            break;
11✔
527
        }
528
    }
×
529
    assert(index > -1);
11✔
530

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

534
    parent.transitions_.insert(
22✔
535
        parent.transitions_.begin() + index,
11✔
536
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
11✔
537
    );
538

539
    auto new_entry = parent.at(index);
11✔
540
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
11✔
541
    (*new_block.dataflow_).parent_ = &new_block;
11✔
542

543
    return {new_block, new_entry.second};
11✔
544
};
×
545

546
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
×
547
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
548
) {
549
    // Insert block before current block
550
    int index = -1;
×
551
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
552
        if (parent.children_.at(i).get() == &block) {
×
553
            index = i;
×
554
            break;
×
555
        }
556
    }
×
557
    assert(index > -1);
×
558

559
    parent.children_
×
560
        .insert(parent.children_.begin() + index, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
561

562
    parent.transitions_.insert(
×
563
        parent.transitions_.begin() + index,
×
564
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
565
    );
566

567
    auto new_entry = parent.at(index);
×
568
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
569
    (*new_block.dataflow_).parent_ = &new_block;
×
570

571
    this->add_dataflow(data_flow_graph, new_block);
×
572

573
    return {new_block, new_entry.second};
×
574
};
×
575

576
std::pair<Block&, Transition&> StructuredSDFGBuilder::
577
    add_block_after(Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
3✔
578
    // Insert block before current block
579
    int index = -1;
3✔
580
    for (size_t i = 0; i < parent.children_.size(); i++) {
5✔
581
        if (parent.children_.at(i).get() == &block) {
5✔
582
            index = i;
3✔
583
            break;
3✔
584
        }
585
    }
2✔
586
    assert(index > -1);
3✔
587

588
    parent.children_.insert(
6✔
589
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
3✔
590
    );
591

592
    parent.transitions_.insert(
6✔
593
        parent.transitions_.begin() + index + 1,
3✔
594
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
3✔
595
    );
596

597
    auto new_entry = parent.at(index + 1);
3✔
598
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
3✔
599
    (*new_block.dataflow_).parent_ = &new_block;
3✔
600

601
    return {new_block, new_entry.second};
3✔
602
};
×
603

604
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_after(
×
605
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph, const DebugInfo& debug_info
606
) {
607
    int index = -1;
×
608
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
609
        if (parent.children_.at(i).get() == &block) {
×
610
            index = i;
×
611
            break;
×
612
        }
613
    }
×
614
    assert(index > -1);
×
615

616
    parent.children_.insert(
×
617
        parent.children_.begin() + index + 1, std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info))
×
618
    );
619

620
    parent.transitions_.insert(
×
621
        parent.transitions_.begin() + index + 1,
×
622
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
623
    );
624

625
    auto new_entry = parent.at(index + 1);
×
626
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
627
    (*new_block.dataflow_).parent_ = &new_block;
×
628

629
    this->add_dataflow(data_flow_graph, new_block);
×
630

631
    return {new_block, new_entry.second};
×
632
};
×
633

634
For& StructuredSDFGBuilder::add_for(
109✔
635
    Sequence& parent,
636
    const symbolic::Symbol& indvar,
637
    const symbolic::Condition& condition,
638
    const symbolic::Expression& init,
639
    const symbolic::Expression& update,
640
    const sdfg::control_flow::Assignments& assignments,
641
    const DebugInfo& debug_info
642
) {
643
    parent.children_
218✔
644
        .push_back(std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition)));
109✔
645

646
    // Increment element id for body node
647
    this->new_element_id();
109✔
648

649
    parent.transitions_
218✔
650
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
109✔
651
        );
652

653
    return static_cast<For&>(*parent.children_.back().get());
109✔
654
};
×
655

656
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_before(
4✔
657
    Sequence& parent,
658
    ControlFlowNode& block,
659
    const symbolic::Symbol& indvar,
660
    const symbolic::Condition& condition,
661
    const symbolic::Expression& init,
662
    const symbolic::Expression& update,
663
    const DebugInfo& debug_info
664
) {
665
    // Insert block before current block
666
    int index = -1;
4✔
667
    for (size_t i = 0; i < parent.children_.size(); i++) {
4✔
668
        if (parent.children_.at(i).get() == &block) {
4✔
669
            index = i;
4✔
670
            break;
4✔
671
        }
672
    }
×
673
    assert(index > -1);
4✔
674

675
    parent.children_.insert(
8✔
676
        parent.children_.begin() + index,
4✔
677
        std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition))
4✔
678
    );
679

680
    // Increment element id for body node
681
    this->new_element_id();
4✔
682

683
    parent.transitions_.insert(
8✔
684
        parent.transitions_.begin() + index,
4✔
685
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
4✔
686
    );
687

688
    auto new_entry = parent.at(index);
4✔
689
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
4✔
690

691
    return {new_block, new_entry.second};
4✔
692
};
×
693

694
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_after(
3✔
695
    Sequence& parent,
696
    ControlFlowNode& block,
697
    const symbolic::Symbol& indvar,
698
    const symbolic::Condition& condition,
699
    const symbolic::Expression& init,
700
    const symbolic::Expression& update,
701
    const DebugInfo& debug_info
702
) {
703
    // Insert block before current block
704
    int index = -1;
3✔
705
    for (size_t i = 0; i < parent.children_.size(); i++) {
5✔
706
        if (parent.children_.at(i).get() == &block) {
5✔
707
            index = i;
3✔
708
            break;
3✔
709
        }
710
    }
2✔
711
    assert(index > -1);
3✔
712

713
    parent.children_.insert(
6✔
714
        parent.children_.begin() + index + 1,
3✔
715
        std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar, init, update, condition))
3✔
716
    );
717

718
    // Increment element id for body node
719
    this->new_element_id();
3✔
720

721
    parent.transitions_.insert(
6✔
722
        parent.transitions_.begin() + index + 1,
3✔
723
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
3✔
724
    );
725

726
    auto new_entry = parent.at(index + 1);
3✔
727
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
3✔
728

729
    return {new_block, new_entry.second};
3✔
730
};
×
731

732
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent, const DebugInfo& debug_info) {
36✔
733
    return this->add_if_else(parent, control_flow::Assignments{}, debug_info);
36✔
734
};
×
735

736
IfElse& StructuredSDFGBuilder::
737
    add_if_else(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
38✔
738
    parent.children_.push_back(std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
38✔
739

740
    parent.transitions_
76✔
741
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
38✔
742
        );
743

744
    return static_cast<IfElse&>(*parent.children_.back().get());
38✔
745
};
×
746

747
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::
748
    add_if_else_before(Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
1✔
749
    // Insert block before current block
750
    int index = -1;
1✔
751
    for (size_t i = 0; i < parent.children_.size(); i++) {
1✔
752
        if (parent.children_.at(i).get() == &block) {
1✔
753
            index = i;
1✔
754
            break;
1✔
755
        }
756
    }
×
757
    assert(index > -1);
1✔
758

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

762
    parent.transitions_.insert(
2✔
763
        parent.transitions_.begin() + index,
1✔
764
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
1✔
765
    );
766

767
    auto new_entry = parent.at(index);
1✔
768
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
1✔
769

770
    return {new_block, new_entry.second};
1✔
771
};
×
772

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

776
    scope.conditions_.push_back(cond);
64✔
777
    return *scope.cases_.back();
64✔
778
};
×
779

780
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t i, const DebugInfo& debug_info) {
1✔
781
    scope.cases_.erase(scope.cases_.begin() + i);
1✔
782
    scope.conditions_.erase(scope.conditions_.begin() + i);
1✔
783
};
1✔
784

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

789
    // Increment element id for body node
790
    this->new_element_id();
33✔
791

792
    parent.transitions_
66✔
793
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
33✔
794
        );
795

796
    return static_cast<While&>(*parent.children_.back().get());
33✔
797
};
×
798

799
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent, const DebugInfo& debug_info) {
12✔
800
    return this->add_continue(parent, control_flow::Assignments{}, debug_info);
12✔
801
};
×
802

803
Continue& StructuredSDFGBuilder::
804
    add_continue(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
14✔
805
    // Check if continue is in a loop
806
    analysis::AnalysisManager analysis_manager(this->subject());
14✔
807
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
14✔
808
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
14✔
809
    bool in_loop = false;
14✔
810
    while (current_scope != nullptr) {
24✔
811
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
24✔
812
            in_loop = true;
14✔
813
            break;
14✔
814
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
10✔
815
            throw UnstructuredControlFlowException();
×
816
        }
817
        current_scope = scope_tree_analysis.parent_scope(current_scope);
10✔
818
    }
819
    if (!in_loop) {
14✔
820
        throw UnstructuredControlFlowException();
×
821
    }
822

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

825
    parent.transitions_
28✔
826
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
14✔
827
        );
828

829
    return static_cast<Continue&>(*parent.children_.back().get());
14✔
830
};
14✔
831

832
Break& StructuredSDFGBuilder::add_break(Sequence& parent, const DebugInfo& debug_info) {
13✔
833
    return this->add_break(parent, control_flow::Assignments{}, debug_info);
13✔
834
};
×
835

836
Break& StructuredSDFGBuilder::
837
    add_break(Sequence& parent, const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
15✔
838
    // Check if break is in a loop
839
    analysis::AnalysisManager analysis_manager(this->subject());
15✔
840
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
15✔
841
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
15✔
842
    bool in_loop = false;
15✔
843
    while (current_scope != nullptr) {
25✔
844
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
25✔
845
            in_loop = true;
15✔
846
            break;
15✔
847
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
10✔
848
            throw UnstructuredControlFlowException();
×
849
        }
850
        current_scope = scope_tree_analysis.parent_scope(current_scope);
10✔
851
    }
852
    if (!in_loop) {
15✔
853
        throw UnstructuredControlFlowException();
×
854
    }
855

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

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

862
    return static_cast<Break&>(*parent.children_.back().get());
15✔
863
};
15✔
864

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

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

873
    return static_cast<Return&>(*parent.children_.back().get());
14✔
874
};
×
875

876
Map& StructuredSDFGBuilder::add_map(
36✔
877
    Sequence& parent,
878
    const symbolic::Symbol& indvar,
879
    const symbolic::Condition& condition,
880
    const symbolic::Expression& init,
881
    const symbolic::Expression& update,
882
    const ScheduleType& schedule_type,
883
    const sdfg::control_flow::Assignments& assignments,
884
    const DebugInfo& debug_info
885
) {
886
    parent.children_
72✔
887
        .push_back(std::unique_ptr<
36✔
888
                   Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)));
36✔
889

890
    // Increment element id for body node
891
    this->new_element_id();
36✔
892

893
    parent.transitions_
72✔
894
        .push_back(std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent, assignments))
36✔
895
        );
896

897
    return static_cast<Map&>(*parent.children_.back().get());
36✔
898
};
×
899

UNCOV
900
std::pair<Map&, Transition&> StructuredSDFGBuilder::add_map_before(
×
901
    Sequence& parent,
902
    ControlFlowNode& block,
903
    const symbolic::Symbol& indvar,
904
    const symbolic::Condition& condition,
905
    const symbolic::Expression& init,
906
    const symbolic::Expression& update,
907
    const ScheduleType& schedule_type,
908
    const sdfg::control_flow::Assignments& assignments,
909
    const DebugInfo& debug_info
910
) {
911
    // Insert block before current block
UNCOV
912
    int index = -1;
×
UNCOV
913
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
UNCOV
914
        if (parent.children_.at(i).get() == &block) {
×
UNCOV
915
            index = i;
×
UNCOV
916
            break;
×
917
        }
918
    }
×
UNCOV
919
    assert(index > -1);
×
920

UNCOV
921
    parent.children_.insert(
×
UNCOV
922
        parent.children_.begin() + index,
×
UNCOV
923
        std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)
×
924
        )
925
    );
926

927
    // Increment element id for body node
UNCOV
928
    this->new_element_id();
×
929

UNCOV
930
    parent.transitions_.insert(
×
UNCOV
931
        parent.transitions_.begin() + index,
×
UNCOV
932
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
×
933
    );
934

UNCOV
935
    auto new_entry = parent.at(index);
×
UNCOV
936
    auto& new_block = dynamic_cast<structured_control_flow::Map&>(new_entry.first);
×
937

UNCOV
938
    return {new_block, new_entry.second};
×
939
};
×
940

941
std::pair<Map&, Transition&> StructuredSDFGBuilder::add_map_after(
2✔
942
    Sequence& parent,
943
    ControlFlowNode& block,
944
    const symbolic::Symbol& indvar,
945
    const symbolic::Condition& condition,
946
    const symbolic::Expression& init,
947
    const symbolic::Expression& update,
948
    const ScheduleType& schedule_type,
949
    const sdfg::control_flow::Assignments& assignments,
950
    const DebugInfo& debug_info
951
) {
952
    // Insert block before current block
953
    int index = -1;
2✔
954
    for (size_t i = 0; i < parent.children_.size(); i++) {
2✔
955
        if (parent.children_.at(i).get() == &block) {
2✔
956
            index = i;
2✔
957
            break;
2✔
958
        }
959
    }
×
960
    assert(index > -1);
2✔
961

962
    parent.children_.insert(
4✔
963
        parent.children_.begin() + index + 1,
2✔
964
        std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)
2✔
965
        )
966
    );
967

968
    // Increment element id for body node
969
    this->new_element_id();
2✔
970

971
    parent.transitions_.insert(
4✔
972
        parent.transitions_.begin() + index + 1,
2✔
973
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent))
2✔
974
    );
975

976
    auto new_entry = parent.at(index + 1);
2✔
977
    auto& new_block = dynamic_cast<structured_control_flow::Map&>(new_entry.first);
2✔
978

979
    return {new_block, new_entry.second};
2✔
980
};
×
981

982
For& StructuredSDFGBuilder::convert_while(
×
983
    Sequence& parent,
984
    While& loop,
985
    const symbolic::Symbol& indvar,
986
    const symbolic::Condition& condition,
987
    const symbolic::Expression& init,
988
    const symbolic::Expression& update
989
) {
990
    // Insert for loop
991
    size_t index = 0;
×
992
    for (auto& entry : parent.children_) {
×
993
        if (entry.get() == &loop) {
×
994
            break;
×
995
        }
996
        index++;
×
997
    }
998
    auto iter = parent.children_.begin() + index;
×
999
    auto& new_iter = *parent.children_.insert(
×
1000
        iter + 1,
×
1001
        std::unique_ptr<For>(new For(this->new_element_id(), loop.debug_info(), indvar, init, update, condition))
×
1002
    );
1003

1004
    // Increment element id for body node
1005
    this->new_element_id();
×
1006

1007
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
1008
    this->insert_children(for_loop.root(), loop.root(), 0);
×
1009

1010
    // Remove while loop
1011
    parent.children_.erase(parent.children_.begin() + index);
×
1012

1013
    return for_loop;
×
1014
};
×
1015

1016
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
8✔
1017
    // Insert for loop
1018
    size_t index = 0;
8✔
1019
    for (auto& entry : parent.children_) {
8✔
1020
        if (entry.get() == &loop) {
8✔
1021
            break;
8✔
1022
        }
1023
        index++;
×
1024
    }
1025
    auto iter = parent.children_.begin() + index;
8✔
1026
    auto& new_iter = *parent.children_.insert(
16✔
1027
        iter + 1,
8✔
1028
        std::unique_ptr<Map>(new Map(
16✔
1029
            this->new_element_id(),
8✔
1030
            loop.debug_info(),
8✔
1031
            loop.indvar(),
8✔
1032
            loop.init(),
8✔
1033
            loop.update(),
8✔
1034
            loop.condition(),
8✔
1035
            ScheduleType_Sequential
1036
        ))
1037
    );
1038

1039
    // Increment element id for body node
1040
    this->new_element_id();
8✔
1041

1042
    auto& map = dynamic_cast<Map&>(*new_iter);
8✔
1043
    this->insert_children(map.root(), loop.root(), 0);
8✔
1044

1045
    // Remove for loop
1046
    parent.children_.erase(parent.children_.begin() + index);
8✔
1047

1048
    return map;
8✔
1049
};
×
1050

1051
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
×
1052
    parent.children_.clear();
×
1053
    parent.transitions_.clear();
×
1054
};
×
1055

1056
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
2✔
1057
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
2✔
1058
    while (!queue.empty()) {
2✔
1059
        auto current = queue.front();
2✔
1060
        queue.pop_front();
2✔
1061

1062
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
2✔
1063
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
2✔
1064
                if (&sequence_stmt->at(i).first == &node) {
2✔
1065
                    return *sequence_stmt;
2✔
1066
                }
1067
                queue.push_back(&sequence_stmt->at(i).first);
×
1068
            }
×
1069
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
1070
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
1071
                queue.push_back(&if_else_stmt->at(i).first);
×
1072
            }
×
1073
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
1074
            queue.push_back(&while_stmt->root());
×
1075
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
×
1076
            queue.push_back(&for_stmt->root());
×
1077
        }
×
1078
    }
1079

1080
    return this->structured_sdfg_->root();
×
1081
};
2✔
1082

1083
/***** Section: Dataflow Graph *****/
1084

1085
data_flow::AccessNode& StructuredSDFGBuilder::
1086
    add_access(structured_control_flow::Block& block, const std::string& data, const DebugInfo& debug_info) {
542✔
1087
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
542✔
1088
    auto res = block.dataflow_->nodes_.insert(
1,084✔
1089
        {vertex,
542✔
1090
         std::unique_ptr<data_flow::AccessNode>(
542✔
1091
             new data_flow::AccessNode(this->new_element_id(), debug_info, vertex, block.dataflow(), data)
542✔
1092
         )}
1093
    );
1094

1095
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
542✔
1096
};
×
1097

1098
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
331✔
1099
    structured_control_flow::Block& block,
1100
    const data_flow::TaskletCode code,
1101
    const std::string& output,
1102
    const std::vector<std::string>& inputs,
1103
    const DebugInfo& debug_info
1104
) {
1105
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
331✔
1106
    auto res = block.dataflow_->nodes_.insert(
662✔
1107
        {vertex,
331✔
1108
         std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
662✔
1109
             this->new_element_id(), debug_info, vertex, block.dataflow(), code, output, inputs, symbolic::__true__()
331✔
1110
         ))}
1111
    );
1112

1113
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
331✔
1114
};
×
1115

1116
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
499✔
1117
    structured_control_flow::Block& block,
1118
    data_flow::DataFlowNode& src,
1119
    const std::string& src_conn,
1120
    data_flow::DataFlowNode& dst,
1121
    const std::string& dst_conn,
1122
    const data_flow::Subset& subset,
1123
    const types::IType& base_type,
1124
    const DebugInfo& debug_info
1125
) {
1126
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
499✔
1127
    auto res = block.dataflow_->edges_.insert(
998✔
1128
        {edge.first,
998✔
1129
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
998✔
1130
             this->new_element_id(), debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset, base_type
499✔
1131
         ))}
1132
    );
1133

1134
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
499✔
1135
#ifndef NDEBUG
1136
    memlet.validate(*this->structured_sdfg_);
499✔
1137
#endif
1138

1139
    return memlet;
499✔
1140
};
×
1141

1142
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
41✔
1143
    structured_control_flow::Block& block,
1144
    data_flow::DataFlowNode& src,
1145
    const std::string& src_conn,
1146
    data_flow::DataFlowNode& dst,
1147
    const std::string& dst_conn,
1148
    const data_flow::Subset& begin_subset,
1149
    const data_flow::Subset& end_subset,
1150
    const types::IType& base_type,
1151
    const DebugInfo& debug_info
1152
) {
1153
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
41✔
1154
    auto res = block.dataflow_->edges_.insert(
82✔
1155
        {edge.first,
82✔
1156
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
82✔
1157
             this->new_element_id(),
41✔
1158
             debug_info,
41✔
1159
             edge.first,
41✔
1160
             block.dataflow(),
41✔
1161
             src,
41✔
1162
             src_conn,
41✔
1163
             dst,
41✔
1164
             dst_conn,
41✔
1165
             begin_subset,
41✔
1166
             end_subset,
41✔
1167
             base_type
41✔
1168
         ))}
1169
    );
1170

1171
    auto& memlet = dynamic_cast<data_flow::Memlet&>(*(res.first->second));
41✔
1172
#ifndef NDEBUG
1173
    memlet.validate(*this->structured_sdfg_);
41✔
1174
#endif
1175

1176
    return memlet;
41✔
1177
};
×
1178

1179
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
84✔
1180
    structured_control_flow::Block& block,
1181
    data_flow::AccessNode& src,
1182
    data_flow::Tasklet& dst,
1183
    const std::string& dst_conn,
1184
    const data_flow::Subset& subset,
1185
    const types::IType& base_type,
1186
    const DebugInfo& debug_info
1187
) {
1188
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, base_type, debug_info);
84✔
1189
};
×
1190

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

1203
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
78✔
1204
    structured_control_flow::Block& block,
1205
    data_flow::AccessNode& src,
1206
    data_flow::Tasklet& dst,
1207
    const std::string& dst_conn,
1208
    const data_flow::Subset& subset,
1209
    const DebugInfo& debug_info
1210
) {
1211
    auto& src_type = this->structured_sdfg_->type(src.data());
78✔
1212
    auto& base_type = types::infer_type(*this->structured_sdfg_, src_type, subset);
78✔
1213
    if (base_type.type_id() != types::TypeID::Scalar) {
78✔
NEW
1214
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1215
    }
1216
    return this->add_memlet(block, src, "void", dst, dst_conn, subset, src_type, debug_info);
78✔
NEW
1217
};
×
1218

1219
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
252✔
1220
    structured_control_flow::Block& block,
1221
    data_flow::Tasklet& src,
1222
    const std::string& src_conn,
1223
    data_flow::AccessNode& dst,
1224
    const data_flow::Subset& subset,
1225
    const DebugInfo& debug_info
1226
) {
1227
    auto& dst_type = this->structured_sdfg_->type(dst.data());
252✔
1228
    auto& base_type = types::infer_type(*this->structured_sdfg_, dst_type, subset);
252✔
1229
    if (base_type.type_id() != types::TypeID::Scalar) {
252✔
NEW
1230
        throw InvalidSDFGException("Computational memlet must have a scalar type");
×
1231
    }
1232
    return this->add_memlet(block, src, src_conn, dst, "void", subset, dst_type, debug_info);
252✔
UNCOV
1233
};
×
1234

1235
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
14✔
1236
    structured_control_flow::Block& block,
1237
    data_flow::AccessNode& src,
1238
    data_flow::LibraryNode& dst,
1239
    const std::string& dst_conn,
1240
    const data_flow::Subset& begin_subset,
1241
    const data_flow::Subset& end_subset,
1242
    const types::IType& base_type,
1243
    const DebugInfo& debug_info
1244
) {
1245
    return this->add_memlet(block, src, "void", dst, dst_conn, begin_subset, end_subset, base_type, debug_info);
14✔
1246
};
×
1247

1248
data_flow::Memlet& StructuredSDFGBuilder::add_computational_memlet(
9✔
1249
    structured_control_flow::Block& block,
1250
    data_flow::LibraryNode& src,
1251
    const std::string& src_conn,
1252
    data_flow::AccessNode& dst,
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
    return this->add_memlet(block, src, src_conn, dst, "void", begin_subset, end_subset, base_type, debug_info);
9✔
1259
};
×
1260

1261
data_flow::Memlet& StructuredSDFGBuilder::add_reference_memlet(
6✔
1262
    structured_control_flow::Block& block,
1263
    data_flow::AccessNode& src,
1264
    data_flow::AccessNode& dst,
1265
    const data_flow::Subset& subset,
1266
    const types::IType& base_type,
1267
    const DebugInfo& debug_info
1268
) {
1269
    return this->add_memlet(block, src, "void", dst, "ref", subset, base_type, debug_info);
6✔
1270
};
×
1271

1272
data_flow::Memlet& StructuredSDFGBuilder::add_dereference_memlet(
5✔
1273
    structured_control_flow::Block& block,
1274
    data_flow::AccessNode& src,
1275
    data_flow::AccessNode& dst,
1276
    bool derefs_src,
1277
    const types::IType& base_type,
1278
    const DebugInfo& debug_info
1279
) {
1280
    if (derefs_src) {
5✔
1281
        return this->add_memlet(block, src, "void", dst, "deref", {symbolic::zero()}, base_type, debug_info);
2✔
1282
    } else {
1283
        return this->add_memlet(block, src, "deref", dst, "void", {symbolic::zero()}, base_type, debug_info);
3✔
1284
    }
1285
};
5✔
1286

1287
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block, const data_flow::Memlet& edge) {
19✔
1288
    auto& graph = block.dataflow();
19✔
1289
    auto e = edge.edge();
19✔
1290
    boost::remove_edge(e, graph.graph_);
19✔
1291
    graph.edges_.erase(e);
19✔
1292
};
19✔
1293

1294
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block, const data_flow::DataFlowNode& node) {
18✔
1295
    auto& graph = block.dataflow();
18✔
1296
    auto v = node.vertex();
18✔
1297
    boost::remove_vertex(v, graph.graph_);
18✔
1298
    graph.nodes_.erase(v);
18✔
1299
};
18✔
1300

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

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

1306
    // Delete incoming
1307
    std::list<const data_flow::Memlet*> iedges;
8✔
1308
    for (auto& iedge : graph.in_edges(node)) {
15✔
1309
        iedges.push_back(&iedge);
7✔
1310
    }
1311
    for (auto iedge : iedges) {
15✔
1312
        auto& src = iedge->src();
7✔
1313
        to_delete.insert(&src);
7✔
1314

1315
        auto edge = iedge->edge();
7✔
1316
        graph.edges_.erase(edge);
7✔
1317
        boost::remove_edge(edge, graph.graph_);
7✔
1318
    }
1319

1320
    // Delete outgoing
1321
    std::list<const data_flow::Memlet*> oedges;
8✔
1322
    for (auto& oedge : graph.out_edges(node)) {
16✔
1323
        oedges.push_back(&oedge);
8✔
1324
    }
1325
    for (auto oedge : oedges) {
16✔
1326
        auto& dst = oedge->dst();
8✔
1327
        to_delete.insert(&dst);
8✔
1328

1329
        auto edge = oedge->edge();
8✔
1330
        graph.edges_.erase(edge);
8✔
1331
        boost::remove_edge(edge, graph.graph_);
8✔
1332
    }
1333

1334
    // Delete nodes
1335
    for (auto obsolete_node : to_delete) {
31✔
1336
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1337
            auto vertex = obsolete_node->vertex();
23✔
1338
            graph.nodes_.erase(vertex);
23✔
1339
            boost::remove_vertex(vertex, graph.graph_);
23✔
1340
        }
23✔
1341
    }
1342
};
8✔
1343

1344
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block, const data_flow::AccessNode& node) {
1✔
1345
    auto& graph = block.dataflow();
1✔
1346
    if (graph.out_degree(node) != 0) {
1✔
1347
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1348
    }
1349

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

1363
        tmp.clear();
2✔
1364
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1365
            tmp.push_back(&iedge);
1✔
1366
        }
1367
        for (auto iedge : tmp) {
3✔
1368
            auto& src = iedge->src();
1✔
1369
            queue.push_back(&src);
1✔
1370

1371
            auto edge = iedge->edge();
1✔
1372
            graph.edges_.erase(edge);
1✔
1373
            boost::remove_edge(edge, graph.graph_);
1✔
1374
        }
1375

1376
        auto vertex = current->vertex();
2✔
1377
        graph.nodes_.erase(vertex);
2✔
1378
        boost::remove_vertex(vertex, graph.graph_);
2✔
1379
    }
1380
};
1✔
1381

1382
void StructuredSDFGBuilder::add_dataflow(const data_flow::DataFlowGraph& from, Block& to) {
18✔
1383
    auto& to_dataflow = to.dataflow();
18✔
1384

1385
    std::unordered_map<graph::Vertex, graph::Vertex> node_mapping;
18✔
1386
    for (auto& entry : from.nodes_) {
19✔
1387
        auto vertex = boost::add_vertex(to_dataflow.graph_);
1✔
1388
        to_dataflow.nodes_.insert({vertex, entry.second->clone(this->new_element_id(), vertex, to_dataflow)});
1✔
1389
        node_mapping.insert({entry.first, vertex});
1✔
1390
    }
1391

1392
    for (auto& entry : from.edges_) {
18✔
1393
        auto src = node_mapping[entry.second->src().vertex()];
×
1394
        auto dst = node_mapping[entry.second->dst().vertex()];
×
1395

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

1398
        to_dataflow.edges_.insert(
×
1399
            {edge.first,
×
1400
             entry.second->clone(
×
1401
                 this->new_element_id(), edge.first, to_dataflow, *to_dataflow.nodes_[src], *to_dataflow.nodes_[dst]
×
1402
             )}
1403
        );
1404
    }
1405
};
18✔
1406

1407
} // namespace builder
1408
} // 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