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

daisytuner / sdfglib / 15494289007

06 Jun 2025 03:36PM UTC coverage: 57.304% (-0.4%) from 57.704%
15494289007

push

github

web-flow
Merge pull request #60 from daisytuner/kernels

removes kernel node in favor of function types

78 of 99 new or added lines in 11 files covered. (78.79%)

91 existing lines in 14 files now uncovered.

7583 of 13233 relevant lines covered (57.3%)

116.04 hits per line

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

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

3
#include "sdfg/analysis/scope_tree_analysis.h"
4
#include "sdfg/codegen/language_extensions/cpp_language_extension.h"
5
#include "sdfg/data_flow/library_node.h"
6
#include "sdfg/structured_control_flow/map.h"
7
#include "sdfg/structured_control_flow/sequence.h"
8
#include "sdfg/types/utils.h"
9

10
using namespace sdfg::control_flow;
11
using namespace sdfg::structured_control_flow;
12

13
namespace sdfg {
14
namespace builder {
15

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

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

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

39
    return nodes;
1✔
40
};
1✔
41

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

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

57
    return false;
×
58
}
6✔
59

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

71
    return pdom;
3✔
72
}
3✔
73

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

79
    auto pdom_tree = sdfg.post_dominator_tree();
4✔
80

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

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

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

102
    auto in_edges = sdfg.in_edges(*current);
8✔
103

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

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

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

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

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

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

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

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

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

185
        auto out_edges = sdfg.out_edges(*curr);
15✔
186
        auto out_degree = sdfg.out_degree(*curr);
15✔
187

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

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

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

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

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

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

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

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

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

283
Function& StructuredSDFGBuilder::function() const {
2,050✔
284
    return static_cast<Function&>(*this->structured_sdfg_);
2,050✔
285
};
286

287
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
302✔
288
    : FunctionBuilder(), structured_sdfg_(std::move(sdfg)) {};
302✔
289

290
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
442✔
291
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type)) {};
442✔
292

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

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

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

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

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

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

319
    this->traverse(sdfg);
4✔
320
};
4✔
321

322
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
3,015✔
323

324
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
419✔
325
    return std::move(this->structured_sdfg_);
419✔
326
};
327

328
Sequence& StructuredSDFGBuilder::add_sequence(Sequence& parent,
36✔
329
                                              const sdfg::symbolic::Assignments& assignments,
330
                                              const DebugInfo& debug_info) {
331
    parent.children_.push_back(std::unique_ptr<Sequence>(new Sequence(debug_info)));
36✔
332

333
    parent.transitions_.push_back(
36✔
334
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
36✔
335

336
    return static_cast<Sequence&>(*parent.children_.back().get());
36✔
337
};
×
338

339
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::add_sequence_before(
4✔
340
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
341
    // Insert block before current block
342
    int index = -1;
4✔
343
    for (size_t i = 0; i < parent.children_.size(); i++) {
4✔
344
        if (parent.children_.at(i).get() == &block) {
4✔
345
            index = i;
4✔
346
            break;
4✔
347
        }
348
    }
×
349
    if (index == -1) {
4✔
350
        throw InvalidSDFGException("StructuredSDFGBuilder: Block not found");
×
351
    }
352

353
    parent.children_.insert(parent.children_.begin() + index,
4✔
354
                            std::unique_ptr<Sequence>(new Sequence(debug_info)));
4✔
355

356
    parent.transitions_.insert(parent.transitions_.begin() + index,
4✔
357
                               std::unique_ptr<Transition>(new Transition(debug_info)));
4✔
358

359
    auto new_entry = parent.at(index);
4✔
360
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
4✔
361

362
    return {new_block, new_entry.second};
4✔
363
};
×
364

365
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t i) {
7✔
366
    parent.children_.erase(parent.children_.begin() + i);
7✔
367
    parent.transitions_.erase(parent.transitions_.begin() + i);
7✔
368
};
7✔
369

370
void StructuredSDFGBuilder::remove_child(Sequence& parent, ControlFlowNode& child) {
3✔
371
    int index = -1;
3✔
372
    for (size_t i = 0; i < parent.children_.size(); i++) {
3✔
373
        if (parent.children_.at(i).get() == &child) {
3✔
374
            index = i;
3✔
375
            break;
3✔
376
        }
377
    }
×
378

379
    parent.children_.erase(parent.children_.begin() + index);
3✔
380
    parent.transitions_.erase(parent.transitions_.begin() + index);
3✔
381
};
3✔
382

383
void StructuredSDFGBuilder::insert_children(Sequence& parent, Sequence& other, size_t i) {
8✔
384
    parent.children_.insert(parent.children_.begin() + i,
16✔
385
                            std::make_move_iterator(other.children_.begin()),
8✔
386
                            std::make_move_iterator(other.children_.end()));
8✔
387
    parent.transitions_.insert(parent.transitions_.begin() + i,
16✔
388
                               std::make_move_iterator(other.transitions_.begin()),
8✔
389
                               std::make_move_iterator(other.transitions_.end()));
8✔
390
    other.children_.clear();
8✔
391
    other.transitions_.clear();
8✔
392
};
8✔
393

394
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
446✔
395
                                        const sdfg::symbolic::Assignments& assignments,
396
                                        const DebugInfo& debug_info) {
397
    parent.children_.push_back(std::unique_ptr<Block>(new Block(debug_info)));
446✔
398

399
    parent.transitions_.push_back(
446✔
400
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
446✔
401

402
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
446✔
403
    (*new_block.dataflow_).parent_ = &new_block;
446✔
404

405
    return new_block;
446✔
406
};
×
407

408
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
28✔
409
                                        const data_flow::DataFlowGraph& data_flow_graph,
410
                                        const sdfg::symbolic::Assignments& assignments,
411
                                        const DebugInfo& debug_info) {
412
    parent.children_.push_back(std::unique_ptr<Block>(new Block(debug_info, data_flow_graph)));
28✔
413

414
    parent.transitions_.push_back(
28✔
415
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
28✔
416

417
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
28✔
418
    (*new_block.dataflow_).parent_ = &new_block;
28✔
419

420
    return new_block;
28✔
421
};
×
422

423
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
12✔
424
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
425
    // Insert block before current block
426
    int index = -1;
12✔
427
    for (size_t i = 0; i < parent.children_.size(); i++) {
13✔
428
        if (parent.children_.at(i).get() == &block) {
13✔
429
            index = i;
12✔
430
            break;
12✔
431
        }
432
    }
1✔
433
    assert(index > -1);
12✔
434

435
    parent.children_.insert(parent.children_.begin() + index,
12✔
436
                            std::unique_ptr<Block>(new Block(debug_info)));
12✔
437

438
    parent.transitions_.insert(parent.transitions_.begin() + index,
12✔
439
                               std::unique_ptr<Transition>(new Transition(debug_info)));
12✔
440

441
    auto new_entry = parent.at(index);
12✔
442
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
12✔
443
    (*new_block.dataflow_).parent_ = &new_block;
12✔
444

445
    return {new_block, new_entry.second};
12✔
446
};
×
447

448
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
×
449
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph,
450
    const DebugInfo& debug_info) {
451
    // Insert block before current block
452
    int index = -1;
×
453
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
454
        if (parent.children_.at(i).get() == &block) {
×
455
            index = i;
×
456
            break;
×
457
        }
458
    }
×
459
    assert(index > -1);
×
460

461
    parent.children_.insert(parent.children_.begin() + index,
×
462
                            std::unique_ptr<Block>(new Block(debug_info, data_flow_graph)));
×
463

464
    parent.transitions_.insert(parent.transitions_.begin() + index,
×
465
                               std::unique_ptr<Transition>(new Transition(debug_info)));
×
466

467
    auto new_entry = parent.at(index);
×
468
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
469
    (*new_block.dataflow_).parent_ = &new_block;
×
470

471
    return {new_block, new_entry.second};
×
472
};
×
473

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

487
    parent.children_.insert(parent.children_.begin() + index + 1,
6✔
488
                            std::unique_ptr<Block>(new Block(debug_info)));
6✔
489

490
    parent.transitions_.insert(parent.transitions_.begin() + index + 1,
6✔
491
                               std::unique_ptr<Transition>(new Transition(debug_info)));
6✔
492

493
    auto new_entry = parent.at(index + 1);
6✔
494
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
6✔
495
    (*new_block.dataflow_).parent_ = &new_block;
6✔
496

497
    return {new_block, new_entry.second};
6✔
498
};
×
499

500
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_after(
×
501
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph,
502
    const DebugInfo& debug_info) {
503
    int index = -1;
×
504
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
505
        if (parent.children_.at(i).get() == &block) {
×
506
            index = i;
×
507
            break;
×
508
        }
509
    }
×
510
    assert(index > -1);
×
511

512
    parent.children_.insert(parent.children_.begin() + index + 1,
×
513
                            std::unique_ptr<Block>(new Block(debug_info, data_flow_graph)));
×
514

515
    parent.transitions_.insert(parent.transitions_.begin() + index + 1,
×
516
                               std::unique_ptr<Transition>(new Transition(debug_info)));
×
517

518
    auto new_entry = parent.at(index + 1);
×
519
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
520
    (*new_block.dataflow_).parent_ = &new_block;
×
521

522
    return {new_block, new_entry.second};
×
523
};
×
524

525
For& StructuredSDFGBuilder::add_for(Sequence& parent, const symbolic::Symbol& indvar,
112✔
526
                                    const symbolic::Condition& condition,
527
                                    const symbolic::Expression& init,
528
                                    const symbolic::Expression& update,
529
                                    const sdfg::symbolic::Assignments& assignments,
530
                                    const DebugInfo& debug_info) {
531
    parent.children_.push_back(
224✔
532
        std::unique_ptr<For>(new For(debug_info, indvar, init, update, condition)));
112✔
533

534
    parent.transitions_.push_back(
112✔
535
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
112✔
536

537
    return static_cast<For&>(*parent.children_.back().get());
112✔
538
};
×
539

540
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_before(
4✔
541
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
542
    const symbolic::Condition& condition, const symbolic::Expression& init,
543
    const symbolic::Expression& update, const DebugInfo& debug_info) {
544
    // Insert block before current block
545
    int index = -1;
4✔
546
    for (size_t i = 0; i < parent.children_.size(); i++) {
4✔
547
        if (parent.children_.at(i).get() == &block) {
4✔
548
            index = i;
4✔
549
            break;
4✔
550
        }
551
    }
×
552
    assert(index > -1);
4✔
553

554
    parent.children_.insert(
8✔
555
        parent.children_.begin() + index,
4✔
556
        std::unique_ptr<For>(new For(debug_info, indvar, init, update, condition)));
4✔
557

558
    parent.transitions_.insert(parent.transitions_.begin() + index,
4✔
559
                               std::unique_ptr<Transition>(new Transition(debug_info)));
4✔
560

561
    auto new_entry = parent.at(index);
4✔
562
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
4✔
563

564
    return {new_block, new_entry.second};
4✔
565
};
×
566

567
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_after(
2✔
568
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
569
    const symbolic::Condition& condition, const symbolic::Expression& init,
570
    const symbolic::Expression& update, const DebugInfo& debug_info) {
571
    // Insert block before current block
572
    int index = -1;
2✔
573
    for (size_t i = 0; i < parent.children_.size(); i++) {
3✔
574
        if (parent.children_.at(i).get() == &block) {
3✔
575
            index = i;
2✔
576
            break;
2✔
577
        }
578
    }
1✔
579
    assert(index > -1);
2✔
580

581
    parent.children_.insert(
4✔
582
        parent.children_.begin() + index + 1,
2✔
583
        std::unique_ptr<For>(new For(debug_info, indvar, init, update, condition)));
2✔
584

585
    parent.transitions_.insert(parent.transitions_.begin() + index + 1,
2✔
586
                               std::unique_ptr<Transition>(new Transition(debug_info)));
2✔
587

588
    auto new_entry = parent.at(index + 1);
2✔
589
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
2✔
590

591
    return {new_block, new_entry.second};
2✔
592
};
×
593

594
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent, const DebugInfo& debug_info) {
33✔
595
    return this->add_if_else(parent, symbolic::Assignments{}, debug_info);
33✔
596
};
×
597

598
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent,
35✔
599
                                           const sdfg::symbolic::Assignments& assignments,
600
                                           const DebugInfo& debug_info) {
601
    parent.children_.push_back(std::unique_ptr<IfElse>(new IfElse(debug_info)));
35✔
602

603
    parent.transitions_.push_back(
35✔
604
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
35✔
605

606
    return static_cast<IfElse&>(*parent.children_.back().get());
35✔
607
};
×
608

609
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::add_if_else_before(
1✔
610
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
611
    // Insert block before current block
612
    int index = -1;
1✔
613
    for (size_t i = 0; i < parent.children_.size(); i++) {
1✔
614
        if (parent.children_.at(i).get() == &block) {
1✔
615
            index = i;
1✔
616
            break;
1✔
617
        }
618
    }
×
619
    assert(index > -1);
1✔
620

621
    parent.children_.insert(parent.children_.begin() + index,
1✔
622
                            std::unique_ptr<IfElse>(new IfElse(debug_info)));
1✔
623

624
    parent.transitions_.insert(parent.transitions_.begin() + index,
1✔
625
                               std::unique_ptr<Transition>(new Transition(debug_info)));
1✔
626

627
    auto new_entry = parent.at(index);
1✔
628
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
1✔
629

630
    return {new_block, new_entry.second};
1✔
631
};
×
632

633
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond,
61✔
634
                                          const DebugInfo& debug_info) {
635
    scope.cases_.push_back(std::unique_ptr<Sequence>(new Sequence(debug_info)));
61✔
636

637
    scope.conditions_.push_back(cond);
61✔
638
    return *scope.cases_.back();
61✔
639
};
×
640

641
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t i, const DebugInfo& debug_info) {
1✔
642
    scope.cases_.erase(scope.cases_.begin() + i);
1✔
643
    scope.conditions_.erase(scope.conditions_.begin() + i);
1✔
644
};
1✔
645

646
While& StructuredSDFGBuilder::add_while(Sequence& parent,
36✔
647
                                        const sdfg::symbolic::Assignments& assignments,
648
                                        const DebugInfo& debug_info) {
649
    parent.children_.push_back(std::unique_ptr<While>(new While(debug_info)));
36✔
650

651
    parent.transitions_.push_back(
36✔
652
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
36✔
653

654
    return static_cast<While&>(*parent.children_.back().get());
36✔
655
};
×
656

657
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent, const DebugInfo& debug_info) {
13✔
658
    return this->add_continue(parent, symbolic::Assignments{}, debug_info);
13✔
659
};
×
660

661
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent,
15✔
662
                                              const sdfg::symbolic::Assignments& assignments,
663
                                              const DebugInfo& debug_info) {
664
    // Check if continue is in a loop
665
    analysis::AnalysisManager analysis_manager(this->subject());
15✔
666
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeTreeAnalysis>();
15✔
667
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
15✔
668
    bool in_loop = false;
15✔
669
    while (current_scope != nullptr) {
27✔
670
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
27✔
671
            in_loop = true;
15✔
672
            break;
15✔
673
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
12✔
674
            throw UnstructuredControlFlowException();
×
675
        }
676
        current_scope = scope_tree_analysis.parent_scope(current_scope);
12✔
677
    }
678
    if (!in_loop) {
15✔
679
        throw UnstructuredControlFlowException();
×
680
    }
681

682
    parent.children_.push_back(std::unique_ptr<Continue>(new Continue(debug_info)));
15✔
683

684
    parent.transitions_.push_back(
30✔
685
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
15✔
686

687
    return static_cast<Continue&>(*parent.children_.back().get());
15✔
688
};
15✔
689

690
Break& StructuredSDFGBuilder::add_break(Sequence& parent, const DebugInfo& debug_info) {
14✔
691
    return this->add_break(parent, symbolic::Assignments{}, debug_info);
14✔
692
};
×
693

694
Break& StructuredSDFGBuilder::add_break(Sequence& parent,
16✔
695
                                        const sdfg::symbolic::Assignments& assignments,
696
                                        const DebugInfo& debug_info) {
697
    // Check if break is in a loop
698
    analysis::AnalysisManager analysis_manager(this->subject());
16✔
699
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeTreeAnalysis>();
16✔
700
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
16✔
701
    bool in_loop = false;
16✔
702
    while (current_scope != nullptr) {
28✔
703
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
28✔
704
            in_loop = true;
16✔
705
            break;
16✔
706
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
12✔
707
            throw UnstructuredControlFlowException();
×
708
        }
709
        current_scope = scope_tree_analysis.parent_scope(current_scope);
12✔
710
    }
711
    if (!in_loop) {
16✔
712
        throw UnstructuredControlFlowException();
×
713
    }
714

715
    parent.children_.push_back(std::unique_ptr<Break>(new Break(debug_info)));
16✔
716

717
    parent.transitions_.push_back(
32✔
718
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
16✔
719

720
    return static_cast<Break&>(*parent.children_.back().get());
16✔
721
};
16✔
722

723
Return& StructuredSDFGBuilder::add_return(Sequence& parent,
14✔
724
                                          const sdfg::symbolic::Assignments& assignments,
725
                                          const DebugInfo& debug_info) {
726
    parent.children_.push_back(std::unique_ptr<Return>(new Return(debug_info)));
14✔
727

728
    parent.transitions_.push_back(
14✔
729
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
14✔
730

731
    return static_cast<Return&>(*parent.children_.back().get());
14✔
732
};
×
733

734
Map& StructuredSDFGBuilder::add_map(Sequence& parent, const symbolic::Symbol& indvar,
9✔
735
                                    const symbolic::Expression& num_iterations,
736
                                    const sdfg::symbolic::Assignments& assignments,
737
                                    const DebugInfo& debug_info) {
738
    parent.children_.push_back(std::unique_ptr<Map>(new Map(debug_info, indvar, num_iterations)));
9✔
739

740
    parent.transitions_.push_back(
9✔
741
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
9✔
742

743
    return static_cast<Map&>(*parent.children_.back().get());
9✔
744
};
×
745

746
For& StructuredSDFGBuilder::convert_while(Sequence& parent, While& loop,
×
747
                                          const symbolic::Symbol& indvar,
748
                                          const symbolic::Condition& condition,
749
                                          const symbolic::Expression& init,
750
                                          const symbolic::Expression& update) {
751
    // Insert for loop
752
    size_t index = 0;
×
753
    for (auto& entry : parent.children_) {
×
754
        if (entry.get() == &loop) {
×
755
            break;
×
756
        }
757
        index++;
×
758
    }
759
    auto iter = parent.children_.begin() + index;
×
760
    auto& new_iter = *parent.children_.insert(
×
761
        iter + 1,
×
762
        std::unique_ptr<For>(new For(loop.debug_info(), indvar, init, update, condition)));
×
763

764
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
765
    this->insert_children(for_loop.root(), loop.root(), 0);
×
766

767
    // Remove while loop
768
    parent.children_.erase(parent.children_.begin() + index);
×
769

770
    return for_loop;
×
771
};
×
772

773
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop,
4✔
774
                                        const symbolic::Expression& num_iterations) {
775
    // Insert for loop
776
    size_t index = 0;
4✔
777
    for (auto& entry : parent.children_) {
4✔
778
        if (entry.get() == &loop) {
4✔
779
            break;
4✔
780
        }
781
        index++;
×
782
    }
783
    auto iter = parent.children_.begin() + index;
4✔
784
    auto& new_iter = *parent.children_.insert(
8✔
785
        iter + 1, std::unique_ptr<Map>(new Map(loop.debug_info(), loop.indvar(), num_iterations)));
4✔
786

787
    auto& map = dynamic_cast<Map&>(*new_iter);
4✔
788
    this->insert_children(map.root(), loop.root(), 0);
4✔
789

790
    // Remove for loop
791
    parent.children_.erase(parent.children_.begin() + index);
4✔
792

793
    return map;
4✔
794
};
×
795

796
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
2✔
797
    parent.children_.clear();
2✔
798
    parent.transitions_.clear();
2✔
799
};
2✔
800

801
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
6✔
802
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
6✔
803
    while (!queue.empty()) {
6✔
804
        auto current = queue.front();
6✔
805
        queue.pop_front();
6✔
806

807
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
6✔
808
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
6✔
809
                if (&sequence_stmt->at(i).first == &node) {
6✔
810
                    return *sequence_stmt;
6✔
811
                }
812
                queue.push_back(&sequence_stmt->at(i).first);
×
813
            }
×
814
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
815
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
816
                queue.push_back(&if_else_stmt->at(i).first);
×
817
            }
×
818
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
819
            queue.push_back(&while_stmt->root());
×
820
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
×
821
            queue.push_back(&for_stmt->root());
×
822
        }
×
823
    }
824

825
    return this->structured_sdfg_->root();
×
826
};
6✔
827

828
/***** Section: Dataflow Graph *****/
829

830
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
537✔
831
                                                         const std::string& data,
832
                                                         const DebugInfo& debug_info) {
833
    // Check: Data exists
834
    if (!this->subject().exists(data)) {
537✔
835
        throw InvalidSDFGException("Data does not exist in SDFG: " + data);
×
836
    }
837

838
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
537✔
839
    auto res = block.dataflow_->nodes_.insert(
1,074✔
840
        {vertex, std::unique_ptr<data_flow::AccessNode>(
537✔
841
                     new data_flow::AccessNode(debug_info, vertex, block.dataflow(), data))});
537✔
842

843
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
537✔
844
};
×
845

846
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
344✔
847
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
848
    const std::pair<std::string, sdfg::types::Scalar>& output,
849
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
850
    const DebugInfo& debug_info) {
851
    // Check: Duplicate inputs
852
    std::unordered_set<std::string> input_names;
344✔
853
    for (auto& input : inputs) {
792✔
854
        if (!input.first.starts_with("_in")) {
448✔
855
            continue;
245✔
856
        }
857
        if (input_names.find(input.first) != input_names.end()) {
203✔
858
            throw InvalidSDFGException("Input " + input.first + " already exists in SDFG");
×
859
        }
860
        input_names.insert(input.first);
203✔
861
    }
862

863
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
344✔
864
    auto res = block.dataflow_->nodes_.insert(
688✔
865
        {vertex,
344✔
866
         std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
688✔
867
             debug_info, vertex, block.dataflow(), code, output, inputs, symbolic::__true__()))});
344✔
868

869
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
344✔
870
};
344✔
871

872
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
549✔
873
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
874
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
875
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
876
    auto& function_ = this->function();
549✔
877

878
    // Check - Case 1: Access Node -> Access Node
879
    // - src_conn or dst_conn must be refs. The other must be void.
880
    // - The side of the memlet that is void, is dereferenced.
881
    // - The dst type must always be a pointer after potential dereferencing.
882
    // - The src type can be any type after dereferecing (&dereferenced_src_type).
883
    if (dynamic_cast<data_flow::AccessNode*>(&src) && dynamic_cast<data_flow::AccessNode*>(&dst)) {
549✔
884
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
2✔
885
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
2✔
886
        if (src_conn == "refs") {
2✔
887
            if (dst_conn != "void") {
×
888
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
889
            }
890

891
            auto& dst_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
892
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
×
893
                throw InvalidSDFGException("dst type must be a pointer");
×
894
            }
895

896
            auto& src_type = function_.type(src_node.data());
×
897
            if (!dynamic_cast<const types::Pointer*>(&src_type)) {
×
898
                throw InvalidSDFGException("src type must be a pointer");
×
899
            }
900
        } else if (src_conn == "void") {
2✔
901
            if (dst_conn != "refs") {
2✔
902
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
903
            }
904

905
            if (symbolic::is_pointer(symbolic::symbol(src_node.data()))) {
2✔
906
                throw InvalidSDFGException("src_conn is void: src cannot be a raw pointer");
×
907
            }
908

909
            // Trivially correct but checks inference
910
            auto& src_type = types::infer_type(function_, function_.type(src_node.data()), subset);
2✔
911
            types::Pointer ref_type(src_type);
2✔
912
            if (!dynamic_cast<const types::Pointer*>(&ref_type)) {
913
                throw InvalidSDFGException("src type must be a pointer");
914
            }
915

916
            auto& dst_type = function_.type(dst_node.data());
2✔
917
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
2✔
918
                throw InvalidSDFGException("dst type must be a pointer");
×
919
            }
920
        } else {
2✔
921
            throw InvalidSDFGException("Invalid src connector: " + src_conn);
×
922
        }
923
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
752✔
924
               dynamic_cast<data_flow::Tasklet*>(&dst)) {
203✔
925
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
203✔
926
        auto& dst_node = dynamic_cast<data_flow::Tasklet&>(dst);
203✔
927
        if (src_conn != "void") {
203✔
928
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
929
        }
930
        bool found = false;
203✔
931
        for (auto& input : dst_node.inputs()) {
257✔
932
            if (input.first == dst_conn) {
257✔
933
                found = true;
203✔
934
                break;
203✔
935
            }
936
        }
937
        if (!found) {
203✔
938
            throw InvalidSDFGException("dst_conn not found in tasklet: " + dst_conn);
×
939
        }
940
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
203✔
941
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
203✔
942
            throw InvalidSDFGException("Tasklets inputs must be scalars");
×
943
        }
944
    } else if (dynamic_cast<data_flow::Tasklet*>(&src) &&
891✔
945
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
344✔
946
        auto& src_node = dynamic_cast<data_flow::Tasklet&>(src);
344✔
947
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
344✔
948
        if (src_conn != src_node.outputs()[0].first) {
344✔
949
            throw InvalidSDFGException("src_conn must match tasklet output name");
×
950
        }
951
        if (dst_conn != "void") {
344✔
952
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
953
        }
954

955
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
344✔
956
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
344✔
957
            throw InvalidSDFGException("Tasklet output must be a scalar");
×
958
        }
959
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
344✔
960
               dynamic_cast<data_flow::LibraryNode*>(&dst)) {
×
961
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
×
962
        auto& dst_node = dynamic_cast<data_flow::LibraryNode&>(dst);
×
963
        if (src_conn != "void") {
×
964
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
965
        }
966
        bool found = false;
×
967
        for (auto& input : dst_node.inputs()) {
×
968
            if (input.first == dst_conn) {
×
969
                found = true;
×
970
                break;
×
971
            }
972
        }
973
        if (!found) {
×
974
            throw InvalidSDFGException("dst_conn not found in library node: " + dst_conn);
×
975
        }
976

977
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
×
978
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
×
979
            throw InvalidSDFGException("Library node inputs must be scalars");
×
980
        }
981
    } else if (dynamic_cast<data_flow::LibraryNode*>(&src) &&
×
982
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
×
983
        auto& src_node = dynamic_cast<data_flow::LibraryNode&>(src);
×
984
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
×
985
        if (dst_conn != "void") {
×
986
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
987
        }
988
        bool found = false;
×
989
        for (auto& output : src_node.outputs()) {
×
990
            if (output.first == src_conn) {
×
991
                found = true;
×
992
                break;
×
993
            }
994
        }
995
        if (!found) {
×
996
            throw InvalidSDFGException("src_conn not found in library node: " + src_conn);
×
997
        }
998

999
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
1000
        if (!dynamic_cast<const types::Pointer*>(&element_type)) {
×
1001
            throw InvalidSDFGException("Access node must be a pointer");
×
1002
        }
1003
    } else {
×
1004
        throw InvalidSDFGException("Invalid src or dst node type");
×
1005
    }
1006

1007
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
549✔
1008
    auto res = block.dataflow_->edges_.insert(
1,098✔
1009
        {edge.first,
1,098✔
1010
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
549✔
1011
             debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset))});
549✔
1012

1013
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
549✔
1014
};
×
1015

1016
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
8✔
1017
    structured_control_flow::Block& block, const data_flow::LibraryNodeType& call,
1018
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& outputs,
1019
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
1020
    const bool has_side_effect, const DebugInfo& debug_info) {
1021
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
8✔
1022
    auto res = block.dataflow_->nodes_.insert(
16✔
1023
        {vertex,
8✔
1024
         std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
8✔
1025
             debug_info, vertex, block.dataflow(), outputs, inputs, call, has_side_effect))});
8✔
1026

1027
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
8✔
1028
}
×
1029

1030
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
1031
                                          const data_flow::Memlet& edge) {
1032
    auto& graph = block.dataflow();
×
1033
    auto e = edge.edge();
×
1034
    boost::remove_edge(e, graph.graph_);
×
1035
    graph.edges_.erase(e);
×
1036
};
×
1037

1038
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
1039
                                        const data_flow::DataFlowNode& node) {
1040
    auto& graph = block.dataflow();
×
1041
    auto v = node.vertex();
×
1042
    boost::remove_vertex(v, graph.graph_);
×
1043
    graph.nodes_.erase(v);
×
1044
};
×
1045

1046
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
1047
                                       const data_flow::Tasklet& node) {
1048
    auto& graph = block.dataflow();
8✔
1049

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

1052
    // Delete incoming
1053
    std::list<const data_flow::Memlet*> iedges;
8✔
1054
    for (auto& iedge : graph.in_edges(node)) {
15✔
1055
        iedges.push_back(&iedge);
7✔
1056
    }
1057
    for (auto iedge : iedges) {
15✔
1058
        auto& src = iedge->src();
7✔
1059
        to_delete.insert(&src);
7✔
1060

1061
        auto edge = iedge->edge();
7✔
1062
        graph.edges_.erase(edge);
7✔
1063
        boost::remove_edge(edge, graph.graph_);
7✔
1064
    }
1065

1066
    // Delete outgoing
1067
    std::list<const data_flow::Memlet*> oedges;
8✔
1068
    for (auto& oedge : graph.out_edges(node)) {
16✔
1069
        oedges.push_back(&oedge);
8✔
1070
    }
1071
    for (auto oedge : oedges) {
16✔
1072
        auto& dst = oedge->dst();
8✔
1073
        to_delete.insert(&dst);
8✔
1074

1075
        auto edge = oedge->edge();
8✔
1076
        graph.edges_.erase(edge);
8✔
1077
        boost::remove_edge(edge, graph.graph_);
8✔
1078
    }
1079

1080
    // Delete nodes
1081
    for (auto obsolete_node : to_delete) {
31✔
1082
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1083
            auto vertex = obsolete_node->vertex();
23✔
1084
            graph.nodes_.erase(vertex);
23✔
1085
            boost::remove_vertex(vertex, graph.graph_);
23✔
1086
        }
23✔
1087
    }
1088
};
8✔
1089

1090
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
1091
                                       const data_flow::AccessNode& node) {
1092
    auto& graph = block.dataflow();
1✔
1093
    if (graph.out_degree(node) != 0) {
1✔
1094
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1095
    }
1096

1097
    std::list<const data_flow::Memlet*> tmp;
1✔
1098
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
1099
    while (!queue.empty()) {
3✔
1100
        auto current = queue.front();
2✔
1101
        queue.pop_front();
2✔
1102
        if (current != &node) {
2✔
1103
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
1104
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1105
                    continue;
×
1106
                }
1107
            }
×
1108
        }
1✔
1109

1110
        tmp.clear();
2✔
1111
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1112
            tmp.push_back(&iedge);
1✔
1113
        }
1114
        for (auto iedge : tmp) {
3✔
1115
            auto& src = iedge->src();
1✔
1116
            queue.push_back(&src);
1✔
1117

1118
            auto edge = iedge->edge();
1✔
1119
            graph.edges_.erase(edge);
1✔
1120
            boost::remove_edge(edge, graph.graph_);
1✔
1121
        }
1122

1123
        auto vertex = current->vertex();
2✔
1124
        graph.nodes_.erase(vertex);
2✔
1125
        boost::remove_vertex(vertex, graph.graph_);
2✔
1126
    }
1127
};
1✔
1128

1129
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
×
1130
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
1131
    auto& sdfg = this->subject();
×
1132

1133
    codegen::CPPLanguageExtension language_extension;
×
1134

1135
    // Base cases
1136
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
×
1137
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
×
1138

1139
        // Determine type
1140
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
×
NEW
1141
        if (symbolic::is_nv(sym)) {
×
1142
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
1143
        } else {
×
1144
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
×
1145
        }
1146

1147
        // Add new container for intermediate result
1148
        auto tmp = this->find_new_name();
×
1149
        this->add_container(tmp, sym_type);
×
1150

1151
        // Create dataflow graph
1152
        auto& input_node = this->add_access(parent, sym->get_name());
×
1153
        auto& output_node = this->add_access(parent, tmp);
×
1154
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
×
1155
                                          {"_out", sym_type}, {{"_in", sym_type}});
×
1156
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {});
×
1157
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1158

1159
        return output_node;
×
1160
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
×
1161
        auto tmp = this->find_new_name();
×
1162
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
1163

1164
        auto& output_node = this->add_access(parent, tmp);
×
1165
        auto& tasklet = this->add_tasklet(
×
1166
            parent, data_flow::TaskletCode::assign,
×
1167
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
1168
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
1169
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1170
        return output_node;
×
1171
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
1172
        auto tmp = this->find_new_name();
×
1173
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1174

1175
        auto& output_node = this->add_access(parent, tmp);
×
1176
        auto& tasklet = this->add_tasklet(
×
1177
            parent, data_flow::TaskletCode::assign,
×
1178
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1179
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1180
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1181
        return output_node;
×
1182
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1183
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
1184
        if (or_expr->get_container().size() != 2) {
×
1185
            throw InvalidSDFGException(
×
1186
                "StructuredSDFGBuilder: Or expression must have exactly two arguments");
×
1187
        }
1188

1189
        std::vector<data_flow::AccessNode*> input_nodes;
×
1190
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1191
        for (auto& arg : or_expr->get_container()) {
×
1192
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1193
            input_nodes.push_back(&input_node);
×
1194
            input_types.push_back(
×
1195
                {"_in" + std::to_string(input_types.size() + 1),
×
1196
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1197
        }
1198

1199
        // Add new container for intermediate result
1200
        auto tmp = this->find_new_name();
×
1201
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1202

1203
        auto& output_node = this->add_access(parent, tmp);
×
1204
        auto& tasklet =
×
1205
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1206
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1207
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1208
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1209
                             {});
×
1210
        }
×
1211
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1212
        return output_node;
×
1213
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1214
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
1215
        if (and_expr->get_container().size() != 2) {
×
1216
            throw InvalidSDFGException(
×
1217
                "StructuredSDFGBuilder: And expression must have exactly two arguments");
×
1218
        }
1219

1220
        std::vector<data_flow::AccessNode*> input_nodes;
×
1221
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1222
        for (auto& arg : and_expr->get_container()) {
×
1223
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1224
            input_nodes.push_back(&input_node);
×
1225
            input_types.push_back(
×
1226
                {"_in" + std::to_string(input_types.size() + 1),
×
1227
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1228
        }
1229

1230
        // Add new container for intermediate result
1231
        auto tmp = this->find_new_name();
×
1232
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1233

1234
        auto& output_node = this->add_access(parent, tmp);
×
1235
        auto& tasklet =
×
1236
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1237
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1238
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1239
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1240
                             {});
×
1241
        }
×
1242
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1243
        return output_node;
×
1244
    } else {
×
1245
        throw std::runtime_error("Unsupported expression type");
×
1246
    }
1247
};
×
1248

1249
}  // namespace builder
1250
}  // 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