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

daisytuner / sdfglib / 15518226219

08 Jun 2025 12:03PM UTC coverage: 61.652% (-0.8%) from 62.447%
15518226219

push

github

web-flow
Merge pull request #65 from daisytuner/lib-nodes-plugins

Adds registry for library node dispatchers

41 of 77 new or added lines in 5 files covered. (53.25%)

124 existing lines in 10 files now uncovered.

7249 of 11758 relevant lines covered (61.65%)

125.57 hits per line

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

66.79
/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 {
1,961✔
284
    return static_cast<Function&>(*this->structured_sdfg_);
1,961✔
285
};
286

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

290
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name, FunctionType type)
434✔
291
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name, type)) {};
434✔
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_; };
1,234✔
323

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

405
    return new_block;
431✔
406
};
×
407

408
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
25✔
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)));
25✔
413

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

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

420
    return new_block;
25✔
421
};
×
422

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

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

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

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

445
    return {new_block, new_entry.second};
10✔
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,
100✔
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(
200✔
532
        std::unique_ptr<For>(new For(debug_info, indvar, init, update, condition)));
100✔
533

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

537
    return static_cast<For&>(*parent.children_.back().get());
100✔
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(
11✔
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;
11✔
573
    for (size_t i = 0; i < parent.children_.size(); i++) {
13✔
574
        if (parent.children_.at(i).get() == &block) {
13✔
575
            index = i;
11✔
576
            break;
11✔
577
        }
578
    }
2✔
579
    assert(index > -1);
11✔
580

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

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

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

591
    return {new_block, new_entry.second};
11✔
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 ScheduleType& schedule_type,
737
                                    const sdfg::symbolic::Assignments& assignments,
738
                                    const DebugInfo& debug_info) {
739
    parent.children_.push_back(
18✔
740
        std::unique_ptr<Map>(new Map(debug_info, indvar, num_iterations, schedule_type)));
9✔
741

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

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

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

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

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

772
    return for_loop;
×
773
};
×
774

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

790
    auto& map = dynamic_cast<Map&>(*new_iter);
4✔
791
    this->insert_children(map.root(), loop.root(), 0);
4✔
792

793
    // Remove for loop
794
    parent.children_.erase(parent.children_.begin() + index);
4✔
795

796
    return map;
4✔
797
};
×
798

799
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
1✔
800
    parent.children_.clear();
1✔
801
    parent.transitions_.clear();
1✔
802
};
1✔
803

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

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

828
    return this->structured_sdfg_->root();
×
829
};
22✔
830

831
/***** Section: Dataflow Graph *****/
832

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

841
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
516✔
842
    auto res = block.dataflow_->nodes_.insert(
1,032✔
843
        {vertex, std::unique_ptr<data_flow::AccessNode>(
516✔
844
                     new data_flow::AccessNode(debug_info, vertex, block.dataflow(), data))});
516✔
845

846
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
516✔
847
};
×
848

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

866
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
334✔
867
    auto res = block.dataflow_->nodes_.insert(
668✔
868
        {vertex,
334✔
869
         std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
668✔
870
             debug_info, vertex, block.dataflow(), code, output, inputs, symbolic::__true__()))});
334✔
871

872
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
334✔
873
};
334✔
874

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

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

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

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

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

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

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

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

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

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

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

1016
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
528✔
1017
};
×
1018

1019
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
3✔
1020
    structured_control_flow::Block& block, const data_flow::LibraryNodeCode code,
1021
    const std::vector<std::string>& outputs, const std::vector<std::string>& inputs,
1022
    const bool side_effect, const DebugInfo& debug_info) {
1023
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
3✔
1024
    auto res = block.dataflow_->nodes_.insert(
6✔
1025
        {vertex, std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
3✔
1026
                     debug_info, vertex, block.dataflow(), code, outputs, inputs, side_effect))});
3✔
1027

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1134
    codegen::CPPLanguageExtension language_extension;
×
1135

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

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

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

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

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

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

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

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

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

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

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

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

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

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