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

daisytuner / sdfglib / 15827874660

23 Jun 2025 03:03PM UTC coverage: 64.193% (+0.4%) from 63.824%
15827874660

push

github

web-flow
Merge pull request #100 from daisytuner/transformations

new definition of map and adapts transformations

148 of 194 new or added lines in 13 files covered. (76.29%)

45 existing lines in 4 files now uncovered.

8193 of 12763 relevant lines covered (64.19%)

136.04 hits per line

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

68.61
/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::determine_loop_nodes(
1✔
19
    const SDFG& sdfg, const control_flow::State& start, const control_flow::State& end) const {
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, const State* node,
46
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree) {
47
    if (pdom == node) {
6✔
48
        return true;
1✔
49
    }
50

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

59
    return false;
×
60
}
6✔
61

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

73
    return pdom;
3✔
74
}
3✔
75

76
void StructuredSDFGBuilder::traverse(const SDFG& sdfg) {
4✔
77
    // Start of SDFGS
78
    Sequence& root = *structured_sdfg_->root_;
4✔
79
    const State* start_state = &sdfg.start_state();
4✔
80

81
    auto pdom_tree = sdfg.post_dominator_tree();
4✔
82

83
    std::unordered_set<const InterstateEdge*> breaks;
4✔
84
    std::unordered_set<const InterstateEdge*> continues;
4✔
85
    for (auto& edge : sdfg.back_edges()) {
5✔
86
        continues.insert(edge);
1✔
87
    }
88

89
    std::unordered_set<const control_flow::State*> visited;
4✔
90
    this->traverse_with_loop_detection(sdfg, root, start_state, nullptr, continues, breaks,
4✔
91
                                       pdom_tree, visited);
92
};
4✔
93

94
void StructuredSDFGBuilder::traverse_with_loop_detection(
9✔
95
    const SDFG& sdfg, Sequence& scope, const State* current, const State* end,
96
    const std::unordered_set<const InterstateEdge*>& continues,
97
    const std::unordered_set<const InterstateEdge*>& breaks,
98
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree,
99
    std::unordered_set<const control_flow::State*>& visited) {
100
    if (current == end) {
9✔
101
        return;
1✔
102
    }
103

104
    auto in_edges = sdfg.in_edges(*current);
8✔
105

106
    // Loop detection
107
    std::unordered_set<const InterstateEdge*> loop_edges;
8✔
108
    for (auto& iedge : in_edges) {
13✔
109
        if (continues.find(&iedge) != continues.end()) {
5✔
110
            loop_edges.insert(&iedge);
1✔
111
        }
1✔
112
    }
113
    if (!loop_edges.empty()) {
8✔
114
        // 1. Determine nodes of loop body
115
        std::unordered_set<const control_flow::State*> body;
1✔
116
        for (auto back_edge : loop_edges) {
2✔
117
            auto loop_nodes = this->determine_loop_nodes(sdfg, back_edge->src(), back_edge->dst());
1✔
118
            body.insert(loop_nodes.begin(), loop_nodes.end());
1✔
119
        }
1✔
120

121
        // 2. Determine exit states and exit edges
122
        std::unordered_set<const control_flow::State*> exit_states;
1✔
123
        std::unordered_set<const control_flow::InterstateEdge*> exit_edges;
1✔
124
        for (auto node : body) {
4✔
125
            for (auto& edge : sdfg.out_edges(*node)) {
7✔
126
                if (body.find(&edge.dst()) == body.end()) {
4✔
127
                    exit_edges.insert(&edge);
1✔
128
                    exit_states.insert(&edge.dst());
1✔
129
                }
1✔
130
            }
131
        }
132
        if (exit_states.size() != 1) {
1✔
133
            throw UnstructuredControlFlowException();
×
134
        }
135
        const control_flow::State* exit_state = *exit_states.begin();
1✔
136

137
        for (auto& edge : breaks) {
1✔
138
            exit_edges.insert(edge);
×
139
        }
140

141
        // Collect debug information (could be removed when this is computed dynamically)
142
        DebugInfo dbg_info = current->debug_info();
1✔
143
        for (auto& edge : in_edges) {
3✔
144
            dbg_info = DebugInfo::merge(dbg_info, edge.debug_info());
2✔
145
        }
146
        for (auto node : body) {
4✔
147
            dbg_info = DebugInfo::merge(dbg_info, node->debug_info());
3✔
148
        }
149
        for (auto edge : exit_edges) {
2✔
150
            dbg_info = DebugInfo::merge(dbg_info, edge->debug_info());
1✔
151
        }
152

153
        // 3. Add while loop
154
        While& loop = this->add_while(scope, {}, dbg_info);
1✔
155

156
        std::unordered_set<const control_flow::State*> loop_visited(visited);
1✔
157
        this->traverse_without_loop_detection(sdfg, loop.root(), current, exit_state, continues,
1✔
158
                                              exit_edges, pdom_tree, loop_visited);
1✔
159

160
        this->traverse_with_loop_detection(sdfg, scope, exit_state, end, continues, breaks,
2✔
161
                                           pdom_tree, visited);
1✔
162
    } else {
1✔
163
        this->traverse_without_loop_detection(sdfg, scope, current, end, continues, breaks,
14✔
164
                                              pdom_tree, visited);
7✔
165
    }
166
};
9✔
167

168
void StructuredSDFGBuilder::traverse_without_loop_detection(
8✔
169
    const SDFG& sdfg, Sequence& scope, const State* current, const State* end,
170
    const std::unordered_set<const InterstateEdge*>& continues,
171
    const std::unordered_set<const InterstateEdge*>& breaks,
172
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree,
173
    std::unordered_set<const control_flow::State*>& visited) {
174
    std::list<const State*> queue = {current};
8✔
175
    while (!queue.empty()) {
26✔
176
        auto curr = queue.front();
18✔
177
        queue.pop_front();
18✔
178
        if (curr == end) {
18✔
179
            continue;
3✔
180
        }
181

182
        if (visited.find(curr) != visited.end()) {
15✔
183
            throw UnstructuredControlFlowException();
×
184
        }
185
        visited.insert(curr);
15✔
186

187
        auto out_edges = sdfg.out_edges(*curr);
15✔
188
        auto out_degree = sdfg.out_degree(*curr);
15✔
189

190
        // Case 1: Sink node
191
        if (out_degree == 0) {
15✔
192
            this->add_block(scope, curr->dataflow(), {}, curr->debug_info());
4✔
193
            this->add_return(scope, {}, curr->debug_info());
4✔
194
            continue;
4✔
195
        }
196

197
        // Case 2: Transition
198
        if (out_degree == 1) {
11✔
199
            auto& oedge = *out_edges.begin();
8✔
200
            if (!oedge.is_unconditional()) {
8✔
201
                throw UnstructuredControlFlowException();
×
202
            }
203
            this->add_block(scope, curr->dataflow(), oedge.assignments(), curr->debug_info());
8✔
204

205
            if (continues.find(&oedge) != continues.end()) {
8✔
206
                this->add_continue(scope, oedge.debug_info());
×
207
            } else if (breaks.find(&oedge) != breaks.end()) {
8✔
208
                this->add_break(scope, oedge.debug_info());
×
209
            } else {
×
210
                bool starts_loop = false;
8✔
211
                for (auto& iedge : sdfg.in_edges(oedge.dst())) {
19✔
212
                    if (continues.find(&iedge) != continues.end()) {
11✔
213
                        starts_loop = true;
×
214
                        break;
×
215
                    }
216
                }
217
                if (!starts_loop) {
8✔
218
                    queue.push_back(&oedge.dst());
8✔
219
                } else {
8✔
220
                    this->traverse_with_loop_detection(sdfg, scope, &oedge.dst(), end, continues,
×
221
                                                       breaks, pdom_tree, visited);
×
222
                }
223
            }
224
            continue;
8✔
225
        }
226

227
        // Case 3: Branches
228
        if (out_degree > 1) {
3✔
229
            this->add_block(scope, curr->dataflow(), {}, curr->debug_info());
3✔
230

231
            std::vector<const InterstateEdge*> out_edges_vec;
3✔
232
            for (auto& edge : out_edges) {
9✔
233
                out_edges_vec.push_back(&edge);
6✔
234
            }
235

236
            // Best-effort approach: Find end of if-else
237
            // If not found, the branches may repeat paths yielding a large SDFG
238
            const control_flow::State* local_end =
3✔
239
                this->find_end_of_if_else(sdfg, curr, out_edges_vec, pdom_tree);
3✔
240
            if (local_end == nullptr) {
3✔
241
                local_end = end;
×
242
            }
×
243

244
            auto& if_else = this->add_if_else(scope, curr->debug_info());
3✔
245
            for (size_t i = 0; i < out_degree; i++) {
9✔
246
                auto& out_edge = out_edges_vec[i];
6✔
247

248
                auto& branch =
6✔
249
                    this->add_case(if_else, out_edge->condition(), out_edge->debug_info());
6✔
250
                if (!out_edge->assignments().empty()) {
6✔
251
                    this->add_block(branch, out_edge->assignments(), out_edge->debug_info());
2✔
252
                }
2✔
253
                if (continues.find(out_edge) != continues.end()) {
6✔
254
                    this->add_continue(branch, out_edge->debug_info());
1✔
255
                } else if (breaks.find(out_edge) != breaks.end()) {
6✔
256
                    this->add_break(branch, out_edge->debug_info());
1✔
257
                } else {
1✔
258
                    std::unordered_set<const control_flow::State*> branch_visited(visited);
4✔
259
                    this->traverse_with_loop_detection(sdfg, branch, &out_edge->dst(), local_end,
4✔
260
                                                       continues, breaks, pdom_tree,
4✔
261
                                                       branch_visited);
262
                }
4✔
263
            }
6✔
264

265
            if (local_end != end) {
3✔
266
                bool starts_loop = false;
2✔
267
                for (auto& iedge : sdfg.in_edges(*local_end)) {
6✔
268
                    if (continues.find(&iedge) != continues.end()) {
4✔
269
                        starts_loop = true;
×
270
                        break;
×
271
                    }
272
                }
273
                if (!starts_loop) {
2✔
274
                    queue.push_back(local_end);
2✔
275
                } else {
2✔
276
                    this->traverse_with_loop_detection(sdfg, scope, local_end, end, continues,
×
277
                                                       breaks, pdom_tree, visited);
×
278
                }
279
            }
2✔
280
            continue;
281
        }
3✔
282
    }
283
}
8✔
284

285
Function& StructuredSDFGBuilder::function() const {
5,930✔
286
    return static_cast<Function&>(*this->structured_sdfg_);
5,930✔
287
};
288

289
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
63✔
290
    : FunctionBuilder(), structured_sdfg_(std::move(sdfg)) {};
63✔
291

292
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
486✔
293
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type)) {};
486✔
294

295
StructuredSDFGBuilder::StructuredSDFGBuilder(const SDFG& sdfg)
4✔
296
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(sdfg.name(), sdfg.type())) {
4✔
297
    for (auto& entry : sdfg.structures_) {
4✔
298
        this->structured_sdfg_->structures_.insert({entry.first, entry.second->clone()});
×
299
    }
300

301
    for (auto& entry : sdfg.containers_) {
11✔
302
        this->structured_sdfg_->containers_.insert({entry.first, entry.second->clone()});
7✔
303
    }
304

305
    for (auto& arg : sdfg.arguments_) {
6✔
306
        this->structured_sdfg_->arguments_.push_back(arg);
2✔
307
    }
308

309
    for (auto& ext : sdfg.externals_) {
5✔
310
        this->structured_sdfg_->externals_.push_back(ext);
1✔
311
    }
312

313
    for (auto& entry : sdfg.assumptions_) {
9✔
314
        this->structured_sdfg_->assumptions_.insert({entry.first, entry.second});
5✔
315
    }
316

317
    for (auto& entry : sdfg.metadata_) {
6✔
318
        this->structured_sdfg_->metadata_[entry.first] = entry.second;
2✔
319
    }
320

321
    this->traverse(sdfg);
4✔
322
};
4✔
323

324
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
1,396✔
325

326
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
382✔
327
    return std::move(this->structured_sdfg_);
382✔
328
};
329

330
Element* StructuredSDFGBuilder::find_element_by_id(const size_t& element_id) const {
8✔
331
    auto& sdfg = this->subject();
8✔
332
    std::list<Element*> queue = {&sdfg.root()};
8✔
333
    while (!queue.empty()) {
30✔
334
        auto current = queue.front();
30✔
335
        queue.pop_front();
30✔
336

337
        if (current->element_id() == element_id) {
30✔
338
            return current;
8✔
339
        }
340

341
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
22✔
342
            auto& dataflow = block_stmt->dataflow();
×
343
            for (auto& node : dataflow.nodes()) {
×
344
                queue.push_back(&node);
×
345
            }
346
            for (auto& edge : dataflow.edges()) {
×
347
                queue.push_back(&edge);
×
348
            }
349
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
22✔
350
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
24✔
351
                queue.push_back(&sequence_stmt->at(i).first);
12✔
352
                queue.push_back(&sequence_stmt->at(i).second);
12✔
353
            }
12✔
354
        } else if (dynamic_cast<structured_control_flow::Return*>(current)) {
22✔
355
            // Do nothing
356
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
10✔
357
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
358
                queue.push_back(&if_else_stmt->at(i).first);
×
359
            }
×
360
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
10✔
UNCOV
361
            queue.push_back(&for_stmt->root());
×
362
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
10✔
363
            queue.push_back(&while_stmt->root());
×
364
        } else if (dynamic_cast<structured_control_flow::Continue*>(current)) {
10✔
365
            // Do nothing
366
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
10✔
367
            // Do nothing
368
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
10✔
369
            queue.push_back(&map_stmt->root());
5✔
370
        }
5✔
371
    }
372

373
    return nullptr;
×
374
};
8✔
375

376
Sequence& StructuredSDFGBuilder::add_sequence(Sequence& parent,
63✔
377
                                              const sdfg::control_flow::Assignments& assignments,
378
                                              const DebugInfo& debug_info) {
379
    parent.children_.push_back(
126✔
380
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
63✔
381

382
    parent.transitions_.push_back(std::unique_ptr<Transition>(
126✔
383
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
63✔
384

385
    return static_cast<Sequence&>(*parent.children_.back().get());
63✔
386
};
×
387

388
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::add_sequence_before(
16✔
389
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
390
    // Insert block before current block
391
    int index = -1;
16✔
392
    for (size_t i = 0; i < parent.children_.size(); i++) {
24✔
393
        if (parent.children_.at(i).get() == &block) {
24✔
394
            index = i;
16✔
395
            break;
16✔
396
        }
397
    }
8✔
398
    if (index == -1) {
16✔
399
        throw InvalidSDFGException("StructuredSDFGBuilder: Block not found");
×
400
    }
401

402
    parent.children_.insert(
32✔
403
        parent.children_.begin() + index,
16✔
404
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
16✔
405

406
    parent.transitions_.insert(
32✔
407
        parent.transitions_.begin() + index,
16✔
408
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
16✔
409

410
    auto new_entry = parent.at(index);
16✔
411
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
16✔
412

413
    return {new_block, new_entry.second};
16✔
414
};
×
415

416
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t i) {
25✔
417
    parent.children_.erase(parent.children_.begin() + i);
25✔
418
    parent.transitions_.erase(parent.transitions_.begin() + i);
25✔
419
};
25✔
420

421
void StructuredSDFGBuilder::remove_child(Sequence& parent, ControlFlowNode& child) {
28✔
422
    int index = -1;
28✔
423
    for (size_t i = 0; i < parent.children_.size(); i++) {
48✔
424
        if (parent.children_.at(i).get() == &child) {
48✔
425
            index = i;
28✔
426
            break;
28✔
427
        }
428
    }
20✔
429

430
    parent.children_.erase(parent.children_.begin() + index);
28✔
431
    parent.transitions_.erase(parent.transitions_.begin() + index);
28✔
432
};
28✔
433

434
void StructuredSDFGBuilder::insert_children(Sequence& parent, Sequence& other, size_t i) {
36✔
435
    parent.children_.insert(parent.children_.begin() + i,
72✔
436
                            std::make_move_iterator(other.children_.begin()),
36✔
437
                            std::make_move_iterator(other.children_.end()));
36✔
438
    parent.transitions_.insert(parent.transitions_.begin() + i,
72✔
439
                               std::make_move_iterator(other.transitions_.begin()),
36✔
440
                               std::make_move_iterator(other.transitions_.end()));
36✔
441
    other.children_.clear();
36✔
442
    other.transitions_.clear();
36✔
443
};
36✔
444

445
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
472✔
446
                                        const sdfg::control_flow::Assignments& assignments,
447
                                        const DebugInfo& debug_info) {
448
    parent.children_.push_back(
944✔
449
        std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
472✔
450

451
    parent.transitions_.push_back(std::unique_ptr<Transition>(
944✔
452
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
472✔
453

454
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
472✔
455
    (*new_block.dataflow_).parent_ = &new_block;
472✔
456

457
    return new_block;
472✔
458
};
×
459

460
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
35✔
461
                                        const data_flow::DataFlowGraph& data_flow_graph,
462
                                        const sdfg::control_flow::Assignments& assignments,
463
                                        const DebugInfo& debug_info) {
464
    parent.children_.push_back(
70✔
465
        std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
35✔
466

467
    parent.transitions_.push_back(std::unique_ptr<Transition>(
70✔
468
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
35✔
469

470
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
35✔
471
    (*new_block.dataflow_).parent_ = &new_block;
35✔
472

473
    this->add_dataflow(data_flow_graph, new_block);
35✔
474

475
    return new_block;
35✔
476
};
×
477

478
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
10✔
479
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
480
    // Insert block before current block
481
    int index = -1;
10✔
482
    for (size_t i = 0; i < parent.children_.size(); i++) {
10✔
483
        if (parent.children_.at(i).get() == &block) {
10✔
484
            index = i;
10✔
485
            break;
10✔
486
        }
487
    }
×
488
    assert(index > -1);
10✔
489

490
    parent.children_.insert(parent.children_.begin() + index,
20✔
491
                            std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
10✔
492

493
    parent.transitions_.insert(
20✔
494
        parent.transitions_.begin() + index,
10✔
495
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
10✔
496

497
    auto new_entry = parent.at(index);
10✔
498
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
10✔
499
    (*new_block.dataflow_).parent_ = &new_block;
10✔
500

501
    return {new_block, new_entry.second};
10✔
502
};
×
503

504
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
×
505
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph,
506
    const DebugInfo& debug_info) {
507
    // Insert block before current block
508
    int index = -1;
×
509
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
510
        if (parent.children_.at(i).get() == &block) {
×
511
            index = i;
×
512
            break;
×
513
        }
514
    }
×
515
    assert(index > -1);
×
516

517
    parent.children_.insert(parent.children_.begin() + index,
×
518
                            std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
519

520
    parent.transitions_.insert(
×
521
        parent.transitions_.begin() + index,
×
522
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
×
523

524
    auto new_entry = parent.at(index);
×
525
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
526
    (*new_block.dataflow_).parent_ = &new_block;
×
527

528
    this->add_dataflow(data_flow_graph, new_block);
×
529

530
    return {new_block, new_entry.second};
×
531
};
×
532

533
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_after(Sequence& parent,
2✔
534
                                                                      ControlFlowNode& block,
535
                                                                      const DebugInfo& debug_info) {
536
    // Insert block before current block
537
    int index = -1;
2✔
538
    for (size_t i = 0; i < parent.children_.size(); i++) {
3✔
539
        if (parent.children_.at(i).get() == &block) {
3✔
540
            index = i;
2✔
541
            break;
2✔
542
        }
543
    }
1✔
544
    assert(index > -1);
2✔
545

546
    parent.children_.insert(parent.children_.begin() + index + 1,
4✔
547
                            std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
2✔
548

549
    parent.transitions_.insert(
4✔
550
        parent.transitions_.begin() + index + 1,
2✔
551
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
2✔
552

553
    auto new_entry = parent.at(index + 1);
2✔
554
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
2✔
555
    (*new_block.dataflow_).parent_ = &new_block;
2✔
556

557
    return {new_block, new_entry.second};
2✔
558
};
×
559

560
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_after(
×
561
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph,
562
    const DebugInfo& debug_info) {
563
    int index = -1;
×
564
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
565
        if (parent.children_.at(i).get() == &block) {
×
566
            index = i;
×
567
            break;
×
568
        }
569
    }
×
570
    assert(index > -1);
×
571

572
    parent.children_.insert(parent.children_.begin() + index + 1,
×
573
                            std::unique_ptr<Block>(new Block(this->new_element_id(), debug_info)));
×
574

575
    parent.transitions_.insert(
×
576
        parent.transitions_.begin() + index + 1,
×
577
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
×
578

579
    auto new_entry = parent.at(index + 1);
×
580
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
581
    (*new_block.dataflow_).parent_ = &new_block;
×
582

583
    this->add_dataflow(data_flow_graph, new_block);
×
584

585
    return {new_block, new_entry.second};
×
586
};
×
587

588
For& StructuredSDFGBuilder::add_for(Sequence& parent, const symbolic::Symbol& indvar,
131✔
589
                                    const symbolic::Condition& condition,
590
                                    const symbolic::Expression& init,
591
                                    const symbolic::Expression& update,
592
                                    const sdfg::control_flow::Assignments& assignments,
593
                                    const DebugInfo& debug_info) {
594
    parent.children_.push_back(std::unique_ptr<For>(
262✔
595
        new For(this->new_element_id(), debug_info, indvar, init, update, condition)));
131✔
596

597
    // Increment element id for body node
598
    this->new_element_id();
131✔
599

600
    parent.transitions_.push_back(std::unique_ptr<Transition>(
262✔
601
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
131✔
602

603
    return static_cast<For&>(*parent.children_.back().get());
131✔
604
};
×
605

606
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_before(
10✔
607
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
608
    const symbolic::Condition& condition, const symbolic::Expression& init,
609
    const symbolic::Expression& update, const DebugInfo& debug_info) {
610
    // Insert block before current block
611
    int index = -1;
10✔
612
    for (size_t i = 0; i < parent.children_.size(); i++) {
10✔
613
        if (parent.children_.at(i).get() == &block) {
10✔
614
            index = i;
10✔
615
            break;
10✔
616
        }
617
    }
×
618
    assert(index > -1);
10✔
619

620
    parent.children_.insert(parent.children_.begin() + index,
20✔
621
                            std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar,
20✔
622
                                                         init, update, condition)));
10✔
623

624
    // Increment element id for body node
625
    this->new_element_id();
10✔
626

627
    parent.transitions_.insert(
20✔
628
        parent.transitions_.begin() + index,
10✔
629
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
10✔
630

631
    auto new_entry = parent.at(index);
10✔
632
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
10✔
633

634
    return {new_block, new_entry.second};
10✔
635
};
×
636

637
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_after(
6✔
638
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
639
    const symbolic::Condition& condition, const symbolic::Expression& init,
640
    const symbolic::Expression& update, const DebugInfo& debug_info) {
641
    // Insert block before current block
642
    int index = -1;
6✔
643
    for (size_t i = 0; i < parent.children_.size(); i++) {
11✔
644
        if (parent.children_.at(i).get() == &block) {
11✔
645
            index = i;
6✔
646
            break;
6✔
647
        }
648
    }
5✔
649
    assert(index > -1);
6✔
650

651
    parent.children_.insert(parent.children_.begin() + index + 1,
12✔
652
                            std::unique_ptr<For>(new For(this->new_element_id(), debug_info, indvar,
12✔
653
                                                         init, update, condition)));
6✔
654

655
    // Increment element id for body node
656
    this->new_element_id();
6✔
657

658
    parent.transitions_.insert(
12✔
659
        parent.transitions_.begin() + index + 1,
6✔
660
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
6✔
661

662
    auto new_entry = parent.at(index + 1);
6✔
663
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
6✔
664

665
    return {new_block, new_entry.second};
6✔
666
};
×
667

668
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent, const DebugInfo& debug_info) {
37✔
669
    return this->add_if_else(parent, control_flow::Assignments{}, debug_info);
37✔
670
};
×
671

672
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent,
42✔
673
                                           const sdfg::control_flow::Assignments& assignments,
674
                                           const DebugInfo& debug_info) {
675
    parent.children_.push_back(
84✔
676
        std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
42✔
677

678
    parent.transitions_.push_back(std::unique_ptr<Transition>(
84✔
679
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
42✔
680

681
    return static_cast<IfElse&>(*parent.children_.back().get());
42✔
682
};
×
683

684
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::add_if_else_before(
1✔
685
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
686
    // Insert block before current block
687
    int index = -1;
1✔
688
    for (size_t i = 0; i < parent.children_.size(); i++) {
1✔
689
        if (parent.children_.at(i).get() == &block) {
1✔
690
            index = i;
1✔
691
            break;
1✔
692
        }
693
    }
×
694
    assert(index > -1);
1✔
695

696
    parent.children_.insert(
2✔
697
        parent.children_.begin() + index,
1✔
698
        std::unique_ptr<IfElse>(new IfElse(this->new_element_id(), debug_info)));
1✔
699

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

704
    auto new_entry = parent.at(index);
1✔
705
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
1✔
706

707
    return {new_block, new_entry.second};
1✔
708
};
×
709

710
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond,
72✔
711
                                          const DebugInfo& debug_info) {
712
    scope.cases_.push_back(
144✔
713
        std::unique_ptr<Sequence>(new Sequence(this->new_element_id(), debug_info)));
72✔
714

715
    scope.conditions_.push_back(cond);
72✔
716
    return *scope.cases_.back();
72✔
717
};
×
718

719
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t i, const DebugInfo& debug_info) {
4✔
720
    scope.cases_.erase(scope.cases_.begin() + i);
4✔
721
    scope.conditions_.erase(scope.conditions_.begin() + i);
4✔
722
};
4✔
723

724
While& StructuredSDFGBuilder::add_while(Sequence& parent,
31✔
725
                                        const sdfg::control_flow::Assignments& assignments,
726
                                        const DebugInfo& debug_info) {
727
    parent.children_.push_back(
62✔
728
        std::unique_ptr<While>(new While(this->new_element_id(), debug_info)));
31✔
729

730
    // Increment element id for body node
731
    this->new_element_id();
31✔
732

733
    parent.transitions_.push_back(std::unique_ptr<Transition>(
62✔
734
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
31✔
735

736
    return static_cast<While&>(*parent.children_.back().get());
31✔
737
};
×
738

739
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent, const DebugInfo& debug_info) {
11✔
740
    return this->add_continue(parent, control_flow::Assignments{}, debug_info);
11✔
741
};
×
742

743
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent,
13✔
744
                                              const sdfg::control_flow::Assignments& assignments,
745
                                              const DebugInfo& debug_info) {
746
    // Check if continue is in a loop
747
    analysis::AnalysisManager analysis_manager(this->subject());
13✔
748
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
13✔
749
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
13✔
750
    bool in_loop = false;
13✔
751
    while (current_scope != nullptr) {
21✔
752
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
21✔
753
            in_loop = true;
13✔
754
            break;
13✔
755
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
8✔
756
            throw UnstructuredControlFlowException();
×
757
        }
758
        current_scope = scope_tree_analysis.parent_scope(current_scope);
8✔
759
    }
760
    if (!in_loop) {
13✔
761
        throw UnstructuredControlFlowException();
×
762
    }
763

764
    parent.children_.push_back(
26✔
765
        std::unique_ptr<Continue>(new Continue(this->new_element_id(), debug_info)));
13✔
766

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

770
    return static_cast<Continue&>(*parent.children_.back().get());
13✔
771
};
13✔
772

773
Break& StructuredSDFGBuilder::add_break(Sequence& parent, const DebugInfo& debug_info) {
12✔
774
    return this->add_break(parent, control_flow::Assignments{}, debug_info);
12✔
775
};
×
776

777
Break& StructuredSDFGBuilder::add_break(Sequence& parent,
14✔
778
                                        const sdfg::control_flow::Assignments& assignments,
779
                                        const DebugInfo& debug_info) {
780
    // Check if break is in a loop
781
    analysis::AnalysisManager analysis_manager(this->subject());
14✔
782
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
14✔
783
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
14✔
784
    bool in_loop = false;
14✔
785
    while (current_scope != nullptr) {
22✔
786
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
22✔
787
            in_loop = true;
14✔
788
            break;
14✔
789
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
8✔
790
            throw UnstructuredControlFlowException();
×
791
        }
792
        current_scope = scope_tree_analysis.parent_scope(current_scope);
8✔
793
    }
794
    if (!in_loop) {
14✔
795
        throw UnstructuredControlFlowException();
×
796
    }
797

798
    parent.children_.push_back(
28✔
799
        std::unique_ptr<Break>(new Break(this->new_element_id(), debug_info)));
14✔
800

801
    parent.transitions_.push_back(std::unique_ptr<Transition>(
28✔
802
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
14✔
803

804
    return static_cast<Break&>(*parent.children_.back().get());
14✔
805
};
14✔
806

807
Return& StructuredSDFGBuilder::add_return(Sequence& parent,
13✔
808
                                          const sdfg::control_flow::Assignments& assignments,
809
                                          const DebugInfo& debug_info) {
810
    parent.children_.push_back(
26✔
811
        std::unique_ptr<Return>(new Return(this->new_element_id(), debug_info)));
13✔
812

813
    parent.transitions_.push_back(std::unique_ptr<Transition>(
26✔
814
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
13✔
815

816
    return static_cast<Return&>(*parent.children_.back().get());
13✔
817
};
×
818

819
Map& StructuredSDFGBuilder::add_map(Sequence& parent, const symbolic::Symbol& indvar,
32✔
820
                                    const symbolic::Condition& condition,
821
                                    const symbolic::Expression& init,
822
                                    const symbolic::Expression& update,
823
                                    const ScheduleType& schedule_type,
824
                                    const sdfg::control_flow::Assignments& assignments,
825
                                    const DebugInfo& debug_info) {
826
    parent.children_.push_back(std::unique_ptr<Map>(new Map(
64✔
827
        this->new_element_id(), debug_info, indvar, init, update, condition, schedule_type)));
32✔
828

829
    // Increment element id for body node
830
    this->new_element_id();
32✔
831

832
    parent.transitions_.push_back(std::unique_ptr<Transition>(
64✔
833
        new Transition(this->new_element_id(), debug_info, parent, assignments)));
32✔
834

835
    return static_cast<Map&>(*parent.children_.back().get());
32✔
836
};
×
837

838
std::pair<Map&, Transition&> StructuredSDFGBuilder::add_map_before(
4✔
839
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
840
    const symbolic::Condition& condition, const symbolic::Expression& init,
841
    const symbolic::Expression& update, const ScheduleType& schedule_type,
842
    const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
843
    // Insert block before current block
844
    int index = -1;
4✔
845
    for (size_t i = 0; i < parent.children_.size(); i++) {
4✔
846
        if (parent.children_.at(i).get() == &block) {
4✔
847
            index = i;
4✔
848
            break;
4✔
849
        }
NEW
850
    }
×
851
    assert(index > -1);
4✔
852

853
    parent.children_.insert(parent.children_.begin() + index,
8✔
854
                            std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar,
8✔
855
                                                         init, update, condition, schedule_type)));
4✔
856

857
    // Increment element id for body node
858
    this->new_element_id();
4✔
859

860
    parent.transitions_.insert(
8✔
861
        parent.transitions_.begin() + index,
4✔
862
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
4✔
863

864
    auto new_entry = parent.at(index);
4✔
865
    auto& new_block = dynamic_cast<structured_control_flow::Map&>(new_entry.first);
4✔
866

867
    return {new_block, new_entry.second};
4✔
NEW
868
};
×
869

870
std::pair<Map&, Transition&> StructuredSDFGBuilder::add_map_after(
8✔
871
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
872
    const symbolic::Condition& condition, const symbolic::Expression& init,
873
    const symbolic::Expression& update, const ScheduleType& schedule_type,
874
    const sdfg::control_flow::Assignments& assignments, const DebugInfo& debug_info) {
875
    // Insert block before current block
876
    int index = -1;
8✔
877
    for (size_t i = 0; i < parent.children_.size(); i++) {
8✔
878
        if (parent.children_.at(i).get() == &block) {
8✔
879
            index = i;
8✔
880
            break;
8✔
881
        }
NEW
882
    }
×
883
    assert(index > -1);
8✔
884

885
    parent.children_.insert(parent.children_.begin() + index + 1,
16✔
886
                            std::unique_ptr<Map>(new Map(this->new_element_id(), debug_info, indvar,
16✔
887
                                                         init, update, condition, schedule_type)));
8✔
888

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

892
    parent.transitions_.insert(
16✔
893
        parent.transitions_.begin() + index + 1,
8✔
894
        std::unique_ptr<Transition>(new Transition(this->new_element_id(), debug_info, parent)));
8✔
895

896
    auto new_entry = parent.at(index + 1);
8✔
897
    auto& new_block = dynamic_cast<structured_control_flow::Map&>(new_entry.first);
8✔
898

899
    return {new_block, new_entry.second};
8✔
NEW
900
};
×
901

UNCOV
902
For& StructuredSDFGBuilder::convert_while(Sequence& parent, While& loop,
×
903
                                          const symbolic::Symbol& indvar,
904
                                          const symbolic::Condition& condition,
905
                                          const symbolic::Expression& init,
906
                                          const symbolic::Expression& update) {
907
    // Insert for loop
908
    size_t index = 0;
×
909
    for (auto& entry : parent.children_) {
×
910
        if (entry.get() == &loop) {
×
911
            break;
×
912
        }
913
        index++;
×
914
    }
915
    auto iter = parent.children_.begin() + index;
×
916
    auto& new_iter = *parent.children_.insert(
×
917
        iter + 1, std::unique_ptr<For>(new For(this->new_element_id(), loop.debug_info(), indvar,
×
918
                                               init, update, condition)));
×
919

920
    // Increment element id for body node
921
    this->new_element_id();
×
922

923
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
924
    this->insert_children(for_loop.root(), loop.root(), 0);
×
925

926
    // Remove while loop
927
    parent.children_.erase(parent.children_.begin() + index);
×
928

929
    return for_loop;
×
930
};
×
931

932
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop) {
8✔
933
    // Insert for loop
934
    size_t index = 0;
8✔
935
    for (auto& entry : parent.children_) {
8✔
936
        if (entry.get() == &loop) {
8✔
937
            break;
8✔
938
        }
939
        index++;
×
940
    }
941
    auto iter = parent.children_.begin() + index;
8✔
942
    auto& new_iter = *parent.children_.insert(
16✔
943
        iter + 1, std::unique_ptr<Map>(new Map(this->new_element_id(), loop.debug_info(),
16✔
944
                                               loop.indvar(), loop.init(), loop.update(),
8✔
945
                                               loop.condition(), ScheduleType_Sequential)));
8✔
946

947
    // Increment element id for body node
948
    this->new_element_id();
8✔
949

950
    auto& map = dynamic_cast<Map&>(*new_iter);
8✔
951
    this->insert_children(map.root(), loop.root(), 0);
8✔
952

953
    // Remove for loop
954
    parent.children_.erase(parent.children_.begin() + index);
8✔
955

956
    return map;
8✔
957
};
×
958

959
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
8✔
960
    parent.children_.clear();
8✔
961
    parent.transitions_.clear();
8✔
962
};
8✔
963

964
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
2✔
965
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
2✔
966
    while (!queue.empty()) {
2✔
967
        auto current = queue.front();
2✔
968
        queue.pop_front();
2✔
969

970
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
2✔
971
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
2✔
972
                if (&sequence_stmt->at(i).first == &node) {
2✔
973
                    return *sequence_stmt;
2✔
974
                }
UNCOV
975
                queue.push_back(&sequence_stmt->at(i).first);
×
UNCOV
976
            }
×
UNCOV
977
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
978
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
979
                queue.push_back(&if_else_stmt->at(i).first);
×
980
            }
×
UNCOV
981
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
982
            queue.push_back(&while_stmt->root());
×
UNCOV
983
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
×
UNCOV
984
            queue.push_back(&for_stmt->root());
×
UNCOV
985
        }
×
986
    }
987

988
    return this->structured_sdfg_->root();
×
989
};
2✔
990

991
/***** Section: Dataflow Graph *****/
992

993
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
540✔
994
                                                         const std::string& data,
995
                                                         const DebugInfo& debug_info) {
996
    // Check: Data exists
997
    if (!this->subject().exists(data)) {
540✔
998
        throw InvalidSDFGException("Data does not exist in SDFG: " + data);
×
999
    }
1000

1001
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
540✔
1002
    auto res = block.dataflow_->nodes_.insert(
1,080✔
1003
        {vertex, std::unique_ptr<data_flow::AccessNode>(new data_flow::AccessNode(
1,080✔
1004
                     this->new_element_id(), debug_info, vertex, block.dataflow(), data))});
540✔
1005

1006
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
540✔
1007
};
×
1008

1009
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
352✔
1010
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
1011
    const std::pair<std::string, sdfg::types::Scalar>& output,
1012
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
1013
    const DebugInfo& debug_info) {
1014
    // Check: Duplicate inputs
1015
    std::unordered_set<std::string> input_names;
352✔
1016
    for (auto& input : inputs) {
814✔
1017
        if (!input.first.starts_with("_in")) {
462✔
1018
            continue;
267✔
1019
        }
1020
        if (input_names.find(input.first) != input_names.end()) {
195✔
1021
            throw InvalidSDFGException("Input " + input.first + " already exists in SDFG");
×
1022
        }
1023
        input_names.insert(input.first);
195✔
1024
    }
1025

1026
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
352✔
1027
    auto res = block.dataflow_->nodes_.insert(
704✔
1028
        {vertex, std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
704✔
1029
                     this->new_element_id(), debug_info, vertex, block.dataflow(), code, output,
352✔
1030
                     inputs, symbolic::__true__()))});
352✔
1031

1032
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
352✔
1033
};
352✔
1034

1035
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
548✔
1036
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
1037
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
1038
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
1039
    auto& function_ = this->function();
548✔
1040

1041
    // Check - Case 1: Access Node -> Access Node
1042
    // - src_conn or dst_conn must be refs. The other must be void.
1043
    // - The side of the memlet that is void, is dereferenced.
1044
    // - The dst type must always be a pointer after potential dereferencing.
1045
    // - The src type can be any type after dereferecing (&dereferenced_src_type).
1046
    if (dynamic_cast<data_flow::AccessNode*>(&src) && dynamic_cast<data_flow::AccessNode*>(&dst)) {
548✔
1047
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
1✔
1048
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
1✔
1049
        if (src_conn == "refs") {
1✔
1050
            if (dst_conn != "void") {
×
1051
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
1052
            }
1053

1054
            auto& dst_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
1055
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
×
1056
                throw InvalidSDFGException("dst type must be a pointer");
×
1057
            }
1058

1059
            auto& src_type = function_.type(src_node.data());
×
1060
            if (!dynamic_cast<const types::Pointer*>(&src_type)) {
×
1061
                throw InvalidSDFGException("src type must be a pointer");
×
1062
            }
1063
        } else if (src_conn == "void") {
1✔
1064
            if (dst_conn != "refs") {
1✔
1065
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
1066
            }
1067

1068
            if (symbolic::is_pointer(symbolic::symbol(src_node.data()))) {
1✔
1069
                throw InvalidSDFGException("src_conn is void: src cannot be a raw pointer");
×
1070
            }
1071

1072
            // Trivially correct but checks inference
1073
            auto& src_type = types::infer_type(function_, function_.type(src_node.data()), subset);
1✔
1074
            types::Pointer ref_type(src_type);
1✔
1075
            if (!dynamic_cast<const types::Pointer*>(&ref_type)) {
1076
                throw InvalidSDFGException("src type must be a pointer");
1077
            }
1078

1079
            auto& dst_type = function_.type(dst_node.data());
1✔
1080
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
1✔
1081
                throw InvalidSDFGException("dst type must be a pointer");
×
1082
            }
1083
        } else {
1✔
1084
            throw InvalidSDFGException("Invalid src connector: " + src_conn);
×
1085
        }
1086
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
743✔
1087
               dynamic_cast<data_flow::Tasklet*>(&dst)) {
195✔
1088
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
195✔
1089
        auto& dst_node = dynamic_cast<data_flow::Tasklet&>(dst);
195✔
1090
        if (src_conn != "void") {
195✔
1091
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
1092
        }
1093
        bool found = false;
195✔
1094
        for (auto& input : dst_node.inputs()) {
257✔
1095
            if (input.first == dst_conn) {
257✔
1096
                found = true;
195✔
1097
                break;
195✔
1098
            }
1099
        }
1100
        if (!found) {
195✔
1101
            throw InvalidSDFGException("dst_conn not found in tasklet: " + dst_conn);
×
1102
        }
1103
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
195✔
1104
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
195✔
1105
            throw InvalidSDFGException("Tasklets inputs must be scalars");
×
1106
        }
1107
    } else if (dynamic_cast<data_flow::Tasklet*>(&src) &&
899✔
1108
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
352✔
1109
        auto& src_node = dynamic_cast<data_flow::Tasklet&>(src);
352✔
1110
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
352✔
1111
        if (src_conn != src_node.output().first) {
352✔
1112
            throw InvalidSDFGException("src_conn must match tasklet output name");
×
1113
        }
1114
        if (dst_conn != "void") {
352✔
1115
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
1116
        }
1117

1118
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
352✔
1119
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
352✔
1120
            throw InvalidSDFGException("Tasklet output must be a scalar");
×
1121
        }
1122
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
352✔
1123
               dynamic_cast<data_flow::LibraryNode*>(&dst)) {
×
1124
        auto& dst_node = dynamic_cast<data_flow::LibraryNode&>(dst);
×
1125
        if (src_conn != "void") {
×
1126
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
1127
        }
1128
        bool found = false;
×
1129
        for (auto& input : dst_node.inputs()) {
×
1130
            if (input == dst_conn) {
×
1131
                found = true;
×
1132
                break;
×
1133
            }
1134
        }
1135
        if (!found) {
×
1136
            throw InvalidSDFGException("dst_conn not found in library node: " + dst_conn);
×
1137
        }
1138
    } else if (dynamic_cast<data_flow::LibraryNode*>(&src) &&
×
1139
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
×
1140
        auto& src_node = dynamic_cast<data_flow::LibraryNode&>(src);
×
1141
        if (dst_conn != "void") {
×
1142
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
1143
        }
1144
        bool found = false;
×
1145
        for (auto& output : src_node.outputs()) {
×
1146
            if (output == src_conn) {
×
1147
                found = true;
×
1148
                break;
×
1149
            }
1150
        }
1151
        if (!found) {
×
1152
            throw InvalidSDFGException("src_conn not found in library node: " + src_conn);
×
1153
        }
1154
    } else {
×
1155
        throw InvalidSDFGException("Invalid src or dst node type");
×
1156
    }
1157

1158
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
548✔
1159
    auto res = block.dataflow_->edges_.insert(
1,096✔
1160
        {edge.first, std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
1,096✔
1161
                         this->new_element_id(), debug_info, edge.first, block.dataflow(), src,
548✔
1162
                         src_conn, dst, dst_conn, subset))});
548✔
1163

1164
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
548✔
1165
};
×
1166

1167
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
1168
                                          const data_flow::Memlet& edge) {
1169
    auto& graph = block.dataflow();
×
1170
    auto e = edge.edge();
×
1171
    boost::remove_edge(e, graph.graph_);
×
1172
    graph.edges_.erase(e);
×
1173
};
×
1174

1175
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
1176
                                        const data_flow::DataFlowNode& node) {
1177
    auto& graph = block.dataflow();
×
1178
    auto v = node.vertex();
×
1179
    boost::remove_vertex(v, graph.graph_);
×
1180
    graph.nodes_.erase(v);
×
1181
};
×
1182

1183
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
1184
                                       const data_flow::Tasklet& node) {
1185
    auto& graph = block.dataflow();
8✔
1186

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

1189
    // Delete incoming
1190
    std::list<const data_flow::Memlet*> iedges;
8✔
1191
    for (auto& iedge : graph.in_edges(node)) {
15✔
1192
        iedges.push_back(&iedge);
7✔
1193
    }
1194
    for (auto iedge : iedges) {
15✔
1195
        auto& src = iedge->src();
7✔
1196
        to_delete.insert(&src);
7✔
1197

1198
        auto edge = iedge->edge();
7✔
1199
        graph.edges_.erase(edge);
7✔
1200
        boost::remove_edge(edge, graph.graph_);
7✔
1201
    }
1202

1203
    // Delete outgoing
1204
    std::list<const data_flow::Memlet*> oedges;
8✔
1205
    for (auto& oedge : graph.out_edges(node)) {
16✔
1206
        oedges.push_back(&oedge);
8✔
1207
    }
1208
    for (auto oedge : oedges) {
16✔
1209
        auto& dst = oedge->dst();
8✔
1210
        to_delete.insert(&dst);
8✔
1211

1212
        auto edge = oedge->edge();
8✔
1213
        graph.edges_.erase(edge);
8✔
1214
        boost::remove_edge(edge, graph.graph_);
8✔
1215
    }
1216

1217
    // Delete nodes
1218
    for (auto obsolete_node : to_delete) {
31✔
1219
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1220
            auto vertex = obsolete_node->vertex();
23✔
1221
            graph.nodes_.erase(vertex);
23✔
1222
            boost::remove_vertex(vertex, graph.graph_);
23✔
1223
        }
23✔
1224
    }
1225
};
8✔
1226

1227
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
1228
                                       const data_flow::AccessNode& node) {
1229
    auto& graph = block.dataflow();
1✔
1230
    if (graph.out_degree(node) != 0) {
1✔
1231
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1232
    }
1233

1234
    std::list<const data_flow::Memlet*> tmp;
1✔
1235
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
1236
    while (!queue.empty()) {
3✔
1237
        auto current = queue.front();
2✔
1238
        queue.pop_front();
2✔
1239
        if (current != &node) {
2✔
1240
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
1241
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1242
                    continue;
×
1243
                }
1244
            }
×
1245
        }
1✔
1246

1247
        tmp.clear();
2✔
1248
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1249
            tmp.push_back(&iedge);
1✔
1250
        }
1251
        for (auto iedge : tmp) {
3✔
1252
            auto& src = iedge->src();
1✔
1253
            queue.push_back(&src);
1✔
1254

1255
            auto edge = iedge->edge();
1✔
1256
            graph.edges_.erase(edge);
1✔
1257
            boost::remove_edge(edge, graph.graph_);
1✔
1258
        }
1259

1260
        auto vertex = current->vertex();
2✔
1261
        graph.nodes_.erase(vertex);
2✔
1262
        boost::remove_vertex(vertex, graph.graph_);
2✔
1263
    }
1264
};
1✔
1265

1266
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
×
1267
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
1268
    auto& sdfg = this->subject();
×
1269

1270
    codegen::CPPLanguageExtension language_extension;
×
1271

1272
    // Base cases
1273
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
×
1274
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
×
1275

1276
        // Determine type
1277
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
×
1278
        if (symbolic::is_nv(sym)) {
×
1279
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
1280
        } else {
×
1281
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
×
1282
        }
1283

1284
        // Add new container for intermediate result
1285
        auto tmp = this->find_new_name();
×
1286
        this->add_container(tmp, sym_type);
×
1287

1288
        // Create dataflow graph
1289
        auto& input_node = this->add_access(parent, sym->get_name());
×
1290
        auto& output_node = this->add_access(parent, tmp);
×
1291
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
×
1292
                                          {"_out", sym_type}, {{"_in", sym_type}});
×
1293
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {});
×
1294
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1295

1296
        return output_node;
×
1297
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
×
1298
        auto tmp = this->find_new_name();
×
1299
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
1300

1301
        auto& output_node = this->add_access(parent, tmp);
×
1302
        auto& tasklet = this->add_tasklet(
×
1303
            parent, data_flow::TaskletCode::assign,
×
1304
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
1305
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
1306
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1307
        return output_node;
×
1308
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
1309
        auto tmp = this->find_new_name();
×
1310
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1311

1312
        auto& output_node = this->add_access(parent, tmp);
×
1313
        auto& tasklet = this->add_tasklet(
×
1314
            parent, data_flow::TaskletCode::assign,
×
1315
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1316
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1317
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1318
        return output_node;
×
1319
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1320
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
1321
        if (or_expr->get_container().size() != 2) {
×
1322
            throw InvalidSDFGException(
×
1323
                "StructuredSDFGBuilder: Or expression must have exactly two arguments");
×
1324
        }
1325

1326
        std::vector<data_flow::AccessNode*> input_nodes;
×
1327
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1328
        for (auto& arg : or_expr->get_container()) {
×
1329
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1330
            input_nodes.push_back(&input_node);
×
1331
            input_types.push_back(
×
1332
                {"_in" + std::to_string(input_types.size() + 1),
×
1333
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1334
        }
1335

1336
        // Add new container for intermediate result
1337
        auto tmp = this->find_new_name();
×
1338
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1339

1340
        auto& output_node = this->add_access(parent, tmp);
×
1341
        auto& tasklet =
×
1342
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1343
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1344
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1345
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1346
                             {});
×
1347
        }
×
1348
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1349
        return output_node;
×
1350
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1351
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
1352
        if (and_expr->get_container().size() != 2) {
×
1353
            throw InvalidSDFGException(
×
1354
                "StructuredSDFGBuilder: And expression must have exactly two arguments");
×
1355
        }
1356

1357
        std::vector<data_flow::AccessNode*> input_nodes;
×
1358
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1359
        for (auto& arg : and_expr->get_container()) {
×
1360
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1361
            input_nodes.push_back(&input_node);
×
1362
            input_types.push_back(
×
1363
                {"_in" + std::to_string(input_types.size() + 1),
×
1364
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1365
        }
1366

1367
        // Add new container for intermediate result
1368
        auto tmp = this->find_new_name();
×
1369
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1370

1371
        auto& output_node = this->add_access(parent, tmp);
×
1372
        auto& tasklet =
×
1373
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1374
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1375
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1376
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1377
                             {});
×
1378
        }
×
1379
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1380
        return output_node;
×
1381
    } else {
×
1382
        throw std::runtime_error("Unsupported expression type");
×
1383
    }
1384
};
×
1385

1386
void StructuredSDFGBuilder::add_dataflow(const data_flow::DataFlowGraph& from, Block& to) {
35✔
1387
    auto& to_dataflow = to.dataflow();
35✔
1388

1389
    std::unordered_map<graph::Vertex, graph::Vertex> node_mapping;
35✔
1390
    for (auto& entry : from.nodes_) {
84✔
1391
        auto vertex = boost::add_vertex(to_dataflow.graph_);
49✔
1392
        to_dataflow.nodes_.insert(
98✔
1393
            {vertex, entry.second->clone(this->new_element_id(), vertex, to_dataflow)});
49✔
1394
        node_mapping.insert({entry.first, vertex});
49✔
1395
    }
1396

1397
    for (auto& entry : from.edges_) {
67✔
1398
        auto src = node_mapping[entry.second->src().vertex()];
32✔
1399
        auto dst = node_mapping[entry.second->dst().vertex()];
32✔
1400

1401
        auto edge = boost::add_edge(src, dst, to_dataflow.graph_);
32✔
1402

1403
        to_dataflow.edges_.insert(
64✔
1404
            {edge.first, entry.second->clone(this->new_element_id(), edge.first, to_dataflow,
64✔
1405
                                             *to_dataflow.nodes_[src], *to_dataflow.nodes_[dst])});
32✔
1406
    }
1407
};
35✔
1408

1409
}  // namespace builder
1410
}  // 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