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

daisytuner / sdfglib / 15455990921

05 Jun 2025 01:03AM UTC coverage: 57.883% (-0.4%) from 58.236%
15455990921

push

github

web-flow
Merge pull request #56 from daisytuner/allocations

removes allocation handling

15 of 36 new or added lines in 4 files covered. (41.67%)

27 existing lines in 4 files now uncovered.

8018 of 13852 relevant lines covered (57.88%)

107.95 hits per line

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

67.56
/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,201✔
284
    return static_cast<Function&>(*this->structured_sdfg_);
2,201✔
285
};
286

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

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

293
StructuredSDFGBuilder::StructuredSDFGBuilder(const SDFG& sdfg)
4✔
294
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(sdfg.name())) {
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,036✔
323

324
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
425✔
325
    return std::move(this->structured_sdfg_);
425✔
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) {
13✔
384
    parent.children_.insert(parent.children_.begin() + i,
26✔
385
                            std::make_move_iterator(other.children_.begin()),
13✔
386
                            std::make_move_iterator(other.children_.end()));
13✔
387
    parent.transitions_.insert(parent.transitions_.begin() + i,
26✔
388
                               std::make_move_iterator(other.transitions_.begin()),
13✔
389
                               std::make_move_iterator(other.transitions_.end()));
13✔
390
    other.children_.clear();
13✔
391
    other.transitions_.clear();
13✔
392
};
13✔
393

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

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

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

405
    return new_block;
453✔
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,
115✔
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(
230✔
532
        std::unique_ptr<For>(new For(debug_info, indvar, init, update, condition)));
115✔
533

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

537
    return static_cast<For&>(*parent.children_.back().get());
115✔
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
Kernel& StructuredSDFGBuilder::add_kernel(
16✔
658
    Sequence& parent, const std::string& suffix, const DebugInfo& debug_info,
659
    const symbolic::Expression& gridDim_x_init, const symbolic::Expression& gridDim_y_init,
660
    const symbolic::Expression& gridDim_z_init, const symbolic::Expression& blockDim_x_init,
661
    const symbolic::Expression& blockDim_y_init, const symbolic::Expression& blockDim_z_init,
662
    const symbolic::Expression& blockIdx_x_init, const symbolic::Expression& blockIdx_y_init,
663
    const symbolic::Expression& blockIdx_z_init, const symbolic::Expression& threadIdx_x_init,
664
    const symbolic::Expression& threadIdx_y_init, const symbolic::Expression& threadIdx_z_init) {
665
    parent.children_.push_back(std::unique_ptr<Kernel>(new Kernel(
32✔
666
        debug_info, suffix, gridDim_x_init, gridDim_y_init, gridDim_z_init, blockDim_x_init,
16✔
667
        blockDim_y_init, blockDim_z_init, blockIdx_x_init, blockIdx_y_init, blockIdx_z_init,
16✔
668
        threadIdx_x_init, threadIdx_y_init, threadIdx_z_init)));
16✔
669

670
    parent.transitions_.push_back(std::unique_ptr<Transition>(new Transition(debug_info)));
16✔
671

672
    return static_cast<Kernel&>(*parent.children_.back().get());
16✔
673
};
×
674

675
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent, const DebugInfo& debug_info) {
13✔
676
    return this->add_continue(parent, symbolic::Assignments{}, debug_info);
13✔
677
};
×
678

679
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent,
15✔
680
                                              const sdfg::symbolic::Assignments& assignments,
681
                                              const DebugInfo& debug_info) {
682
    // Check if continue is in a loop
683
    analysis::AnalysisManager analysis_manager(this->subject());
15✔
684
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeTreeAnalysis>();
15✔
685
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
15✔
686
    bool in_loop = false;
15✔
687
    while (current_scope != nullptr) {
27✔
688
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
27✔
689
            in_loop = true;
15✔
690
            break;
15✔
691
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
12✔
692
            throw UnstructuredControlFlowException();
×
693
        }
694
        current_scope = scope_tree_analysis.parent_scope(current_scope);
12✔
695
    }
696
    if (!in_loop) {
15✔
697
        throw UnstructuredControlFlowException();
×
698
    }
699

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

702
    parent.transitions_.push_back(
30✔
703
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
15✔
704

705
    return static_cast<Continue&>(*parent.children_.back().get());
15✔
706
};
15✔
707

708
Break& StructuredSDFGBuilder::add_break(Sequence& parent, const DebugInfo& debug_info) {
14✔
709
    return this->add_break(parent, symbolic::Assignments{}, debug_info);
14✔
710
};
×
711

712
Break& StructuredSDFGBuilder::add_break(Sequence& parent,
16✔
713
                                        const sdfg::symbolic::Assignments& assignments,
714
                                        const DebugInfo& debug_info) {
715
    // Check if break is in a loop
716
    analysis::AnalysisManager analysis_manager(this->subject());
16✔
717
    auto& scope_tree_analysis = analysis_manager.get<analysis::ScopeTreeAnalysis>();
16✔
718
    auto current_scope = scope_tree_analysis.parent_scope(&parent);
16✔
719
    bool in_loop = false;
16✔
720
    while (current_scope != nullptr) {
28✔
721
        if (dynamic_cast<structured_control_flow::While*>(current_scope)) {
28✔
722
            in_loop = true;
16✔
723
            break;
16✔
724
        } else if (dynamic_cast<structured_control_flow::For*>(current_scope)) {
12✔
725
            throw UnstructuredControlFlowException();
×
726
        }
727
        current_scope = scope_tree_analysis.parent_scope(current_scope);
12✔
728
    }
729
    if (!in_loop) {
16✔
730
        throw UnstructuredControlFlowException();
×
731
    }
732

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

735
    parent.transitions_.push_back(
32✔
736
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
16✔
737

738
    return static_cast<Break&>(*parent.children_.back().get());
16✔
739
};
16✔
740

741
Return& StructuredSDFGBuilder::add_return(Sequence& parent,
14✔
742
                                          const sdfg::symbolic::Assignments& assignments,
743
                                          const DebugInfo& debug_info) {
744
    parent.children_.push_back(std::unique_ptr<Return>(new Return(debug_info)));
14✔
745

746
    parent.transitions_.push_back(
14✔
747
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
14✔
748

749
    return static_cast<Return&>(*parent.children_.back().get());
14✔
750
};
×
751

752
Map& StructuredSDFGBuilder::add_map(Sequence& parent, const symbolic::Symbol& indvar,
9✔
753
                                    const symbolic::Expression& num_iterations,
754
                                    const sdfg::symbolic::Assignments& assignments,
755
                                    const DebugInfo& debug_info) {
756
    parent.children_.push_back(std::unique_ptr<Map>(new Map(debug_info, indvar, num_iterations)));
9✔
757

758
    parent.transitions_.push_back(
9✔
759
        std::unique_ptr<Transition>(new Transition(debug_info, assignments)));
9✔
760

761
    return static_cast<Map&>(*parent.children_.back().get());
9✔
762
};
×
763

764
For& StructuredSDFGBuilder::convert_while(Sequence& parent, While& loop,
×
765
                                          const symbolic::Symbol& indvar,
766
                                          const symbolic::Condition& condition,
767
                                          const symbolic::Expression& init,
768
                                          const symbolic::Expression& update) {
769
    // Insert for loop
770
    size_t index = 0;
×
771
    for (auto& entry : parent.children_) {
×
772
        if (entry.get() == &loop) {
×
773
            break;
×
774
        }
775
        index++;
×
776
    }
777
    auto iter = parent.children_.begin() + index;
×
778
    auto& new_iter = *parent.children_.insert(
×
779
        iter + 1,
×
780
        std::unique_ptr<For>(new For(loop.debug_info(), indvar, init, update, condition)));
×
781

782
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
783
    this->insert_children(for_loop.root(), loop.root(), 0);
×
784

785
    // Remove while loop
786
    parent.children_.erase(parent.children_.begin() + index);
×
787

788
    return for_loop;
×
789
};
×
790

791
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop,
4✔
792
                                        const symbolic::Expression& num_iterations) {
793
    // Insert for loop
794
    size_t index = 0;
4✔
795
    for (auto& entry : parent.children_) {
4✔
796
        if (entry.get() == &loop) {
4✔
797
            break;
4✔
798
        }
799
        index++;
×
800
    }
801
    auto iter = parent.children_.begin() + index;
4✔
802
    auto& new_iter = *parent.children_.insert(
8✔
803
        iter + 1, std::unique_ptr<Map>(new Map(loop.debug_info(), loop.indvar(), num_iterations)));
4✔
804

805
    auto& map = dynamic_cast<Map&>(*new_iter);
4✔
806
    this->insert_children(map.root(), loop.root(), 0);
4✔
807

808
    // Remove for loop
809
    parent.children_.erase(parent.children_.begin() + index);
4✔
810

811
    return map;
4✔
812
};
×
813

814
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
2✔
815
    parent.children_.clear();
2✔
816
    parent.transitions_.clear();
2✔
817
};
2✔
818

819
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
6✔
820
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
6✔
821
    while (!queue.empty()) {
6✔
822
        auto current = queue.front();
6✔
823
        queue.pop_front();
6✔
824

825
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
6✔
826
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
6✔
827
                if (&sequence_stmt->at(i).first == &node) {
6✔
828
                    return *sequence_stmt;
6✔
829
                }
830
                queue.push_back(&sequence_stmt->at(i).first);
×
831
            }
×
832
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
833
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
834
                queue.push_back(&if_else_stmt->at(i).first);
×
835
            }
×
836
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
837
            queue.push_back(&while_stmt->root());
×
838
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
×
839
            queue.push_back(&for_stmt->root());
×
840
        } else if (auto kern_stmt = dynamic_cast<const structured_control_flow::Kernel*>(current)) {
×
841
            queue.push_back(&kern_stmt->root());
×
842
        }
×
843
    }
844

845
    return this->structured_sdfg_->root();
×
846
};
6✔
847

848
Kernel& StructuredSDFGBuilder::convert_into_kernel() {
5✔
849
    auto old_root = std::move(this->structured_sdfg_->root_);
5✔
850
    this->structured_sdfg_->root_ = std::unique_ptr<Sequence>(new Sequence(old_root->debug_info()));
5✔
851

852
    auto& new_root = this->structured_sdfg_->root();
5✔
853
    auto& kernel = this->add_kernel(new_root, this->function().name());
5✔
854

855
    this->insert_children(kernel.root(), *old_root, 0);
5✔
856

857
    types::Scalar gridDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
858
    add_container(kernel.gridDim_x()->get_name(), gridDim_x);
5✔
859
    types::Scalar gridDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
860
    add_container(kernel.gridDim_y()->get_name(), gridDim_y);
5✔
861
    types::Scalar gridDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
862
    add_container(kernel.gridDim_z()->get_name(), gridDim_z);
5✔
863
    types::Scalar blockDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
864
    add_container(kernel.blockDim_x()->get_name(), blockDim_x);
5✔
865
    types::Scalar blockDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
866
    add_container(kernel.blockDim_y()->get_name(), blockDim_y);
5✔
867
    types::Scalar blockDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
868
    add_container(kernel.blockDim_z()->get_name(), blockDim_z);
5✔
869
    types::Scalar blockIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
870
    add_container(kernel.blockIdx_x()->get_name(), blockIdx_x);
5✔
871
    types::Scalar blockIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
872
    add_container(kernel.blockIdx_y()->get_name(), blockIdx_y);
5✔
873
    types::Scalar blockIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
874
    add_container(kernel.blockIdx_z()->get_name(), blockIdx_z);
5✔
875
    types::Scalar threadIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
876
    add_container(kernel.threadIdx_x()->get_name(), threadIdx_x);
5✔
877
    types::Scalar threadIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
878
    add_container(kernel.threadIdx_y()->get_name(), threadIdx_y);
5✔
879
    types::Scalar threadIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
880
    add_container(kernel.threadIdx_z()->get_name(), threadIdx_z);
5✔
881

882
    kernel.root().replace(symbolic::symbol("gridDim.x"), kernel.gridDim_x());
5✔
883
    kernel.root().replace(symbolic::symbol("gridDim.y"), kernel.gridDim_y());
5✔
884
    kernel.root().replace(symbolic::symbol("gridDim.z"), kernel.gridDim_z());
5✔
885

886
    kernel.root().replace(symbolic::symbol("blockDim.x"), kernel.blockDim_x());
5✔
887
    kernel.root().replace(symbolic::symbol("blockDim.y"), kernel.blockDim_y());
5✔
888
    kernel.root().replace(symbolic::symbol("blockDim.z"), kernel.blockDim_z());
5✔
889

890
    kernel.root().replace(symbolic::symbol("blockIdx.x"), kernel.blockIdx_x());
5✔
891
    kernel.root().replace(symbolic::symbol("blockIdx.y"), kernel.blockIdx_y());
5✔
892
    kernel.root().replace(symbolic::symbol("blockIdx.z"), kernel.blockIdx_z());
5✔
893

894
    kernel.root().replace(symbolic::symbol("threadIdx.x"), kernel.threadIdx_x());
5✔
895
    kernel.root().replace(symbolic::symbol("threadIdx.y"), kernel.threadIdx_y());
5✔
896
    kernel.root().replace(symbolic::symbol("threadIdx.z"), kernel.threadIdx_z());
5✔
897

898
    return kernel;
5✔
899
};
5✔
900

901
/***** Section: Dataflow Graph *****/
902

903
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
541✔
904
                                                         const std::string& data,
905
                                                         const DebugInfo& debug_info) {
906
    // Check: Data exists
907
    if (!this->subject().exists(data)) {
541✔
908
        throw InvalidSDFGException("Data does not exist in SDFG: " + data);
×
909
    }
910

911
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
541✔
912
    auto res = block.dataflow_->nodes_.insert(
1,082✔
913
        {vertex, std::unique_ptr<data_flow::AccessNode>(
541✔
914
                     new data_flow::AccessNode(debug_info, vertex, block.dataflow(), data))});
541✔
915

916
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
541✔
917
};
×
918

919
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
346✔
920
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
921
    const std::pair<std::string, sdfg::types::Scalar>& output,
922
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
923
    const DebugInfo& debug_info) {
924
    // Check: Duplicate inputs
925
    std::unordered_set<std::string> input_names;
346✔
926
    for (auto& input : inputs) {
796✔
927
        if (!input.first.starts_with("_in")) {
450✔
928
            continue;
245✔
929
        }
930
        if (input_names.find(input.first) != input_names.end()) {
205✔
931
            throw InvalidSDFGException("Input " + input.first + " already exists in SDFG");
×
932
        }
933
        input_names.insert(input.first);
205✔
934
    }
935

936
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
346✔
937
    auto res = block.dataflow_->nodes_.insert(
692✔
938
        {vertex,
346✔
939
         std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
692✔
940
             debug_info, vertex, block.dataflow(), code, output, inputs, symbolic::__true__()))});
346✔
941

942
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
346✔
943
};
346✔
944

945
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
553✔
946
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
947
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
948
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
949
    auto& function_ = this->function();
553✔
950

951
    // Check - Case 1: Access Node -> Access Node
952
    // - src_conn or dst_conn must be refs. The other must be void.
953
    // - The side of the memlet that is void, is dereferenced.
954
    // - The dst type must always be a pointer after potential dereferencing.
955
    // - The src type can be any type after dereferecing (&dereferenced_src_type).
956
    if (dynamic_cast<data_flow::AccessNode*>(&src) && dynamic_cast<data_flow::AccessNode*>(&dst)) {
553✔
957
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
2✔
958
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
2✔
959
        if (src_conn == "refs") {
2✔
UNCOV
960
            if (dst_conn != "void") {
×
961
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
962
            }
963

UNCOV
964
            auto& dst_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
UNCOV
965
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
×
966
                throw InvalidSDFGException("dst type must be a pointer");
×
967
            }
968

UNCOV
969
            auto& src_type = function_.type(src_node.data());
×
UNCOV
970
            if (!dynamic_cast<const types::Pointer*>(&src_type)) {
×
971
                throw InvalidSDFGException("src type must be a pointer");
×
972
            }
973
        } else if (src_conn == "void") {
2✔
974
            if (dst_conn != "refs") {
2✔
975
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
976
            }
977

978
            if (symbolic::is_pointer(symbolic::symbol(src_node.data()))) {
2✔
979
                throw InvalidSDFGException("src_conn is void: src cannot be a raw pointer");
×
980
            }
981

982
            // Trivially correct but checks inference
983
            auto& src_type = types::infer_type(function_, function_.type(src_node.data()), subset);
2✔
984
            types::Pointer ref_type(src_type);
2✔
985
            if (!dynamic_cast<const types::Pointer*>(&ref_type)) {
986
                throw InvalidSDFGException("src type must be a pointer");
987
            }
988

989
            auto& dst_type = function_.type(dst_node.data());
2✔
990
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
2✔
991
                throw InvalidSDFGException("dst type must be a pointer");
×
992
            }
993
        } else {
2✔
994
            throw InvalidSDFGException("Invalid src connector: " + src_conn);
×
995
        }
996
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
758✔
997
               dynamic_cast<data_flow::Tasklet*>(&dst)) {
205✔
998
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
205✔
999
        auto& dst_node = dynamic_cast<data_flow::Tasklet&>(dst);
205✔
1000
        if (src_conn != "void") {
205✔
1001
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
1002
        }
1003
        bool found = false;
205✔
1004
        for (auto& input : dst_node.inputs()) {
259✔
1005
            if (input.first == dst_conn) {
259✔
1006
                found = true;
205✔
1007
                break;
205✔
1008
            }
1009
        }
1010
        if (!found) {
205✔
1011
            throw InvalidSDFGException("dst_conn not found in tasklet: " + dst_conn);
×
1012
        }
1013
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
205✔
1014
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
205✔
1015
            throw InvalidSDFGException("Tasklets inputs must be scalars");
×
1016
        }
1017
    } else if (dynamic_cast<data_flow::Tasklet*>(&src) &&
897✔
1018
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
346✔
1019
        auto& src_node = dynamic_cast<data_flow::Tasklet&>(src);
346✔
1020
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
346✔
1021
        if (src_conn != src_node.outputs()[0].first) {
346✔
1022
            throw InvalidSDFGException("src_conn must match tasklet output name");
×
1023
        }
1024
        if (dst_conn != "void") {
346✔
1025
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
1026
        }
1027

1028
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
346✔
1029
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
346✔
1030
            throw InvalidSDFGException("Tasklet output must be a scalar");
×
1031
        }
1032
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
346✔
1033
               dynamic_cast<data_flow::LibraryNode*>(&dst)) {
×
1034
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
×
1035
        auto& dst_node = dynamic_cast<data_flow::LibraryNode&>(dst);
×
1036
        if (src_conn != "void") {
×
1037
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
1038
        }
1039
        bool found = false;
×
1040
        for (auto& input : dst_node.inputs()) {
×
1041
            if (input.first == dst_conn) {
×
1042
                found = true;
×
1043
                break;
×
1044
            }
1045
        }
1046
        if (!found) {
×
1047
            throw InvalidSDFGException("dst_conn not found in library node: " + dst_conn);
×
1048
        }
1049

1050
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
×
1051
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
×
1052
            throw InvalidSDFGException("Library node inputs must be scalars");
×
1053
        }
1054
    } else if (dynamic_cast<data_flow::LibraryNode*>(&src) &&
×
1055
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
×
1056
        auto& src_node = dynamic_cast<data_flow::LibraryNode&>(src);
×
1057
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
×
1058
        if (dst_conn != "void") {
×
1059
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
1060
        }
1061
        bool found = false;
×
1062
        for (auto& output : src_node.outputs()) {
×
1063
            if (output.first == src_conn) {
×
1064
                found = true;
×
1065
                break;
×
1066
            }
1067
        }
1068
        if (!found) {
×
1069
            throw InvalidSDFGException("src_conn not found in library node: " + src_conn);
×
1070
        }
1071

1072
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
1073
        if (!dynamic_cast<const types::Pointer*>(&element_type)) {
×
1074
            throw InvalidSDFGException("Access node must be a pointer");
×
1075
        }
1076
    } else {
×
1077
        throw InvalidSDFGException("Invalid src or dst node type");
×
1078
    }
1079

1080
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
553✔
1081
    auto res = block.dataflow_->edges_.insert(
1,106✔
1082
        {edge.first,
1,106✔
1083
         std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
553✔
1084
             debug_info, edge.first, block.dataflow(), src, src_conn, dst, dst_conn, subset))});
553✔
1085

1086
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
553✔
1087
};
×
1088

1089
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
9✔
1090
    structured_control_flow::Block& block, const data_flow::LibraryNodeType& call,
1091
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& outputs,
1092
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
1093
    const bool has_side_effect, const DebugInfo& debug_info) {
1094
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
9✔
1095
    auto res = block.dataflow_->nodes_.insert(
18✔
1096
        {vertex,
9✔
1097
         std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
9✔
1098
             debug_info, vertex, block.dataflow(), outputs, inputs, call, has_side_effect))});
9✔
1099

1100
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
9✔
1101
}
×
1102

1103
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
1104
                                          const data_flow::Memlet& edge) {
1105
    auto& graph = block.dataflow();
×
1106
    auto e = edge.edge();
×
1107
    boost::remove_edge(e, graph.graph_);
×
1108
    graph.edges_.erase(e);
×
1109
};
×
1110

1111
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
1112
                                        const data_flow::DataFlowNode& node) {
1113
    auto& graph = block.dataflow();
×
1114
    auto v = node.vertex();
×
1115
    boost::remove_vertex(v, graph.graph_);
×
1116
    graph.nodes_.erase(v);
×
1117
};
×
1118

1119
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
1120
                                       const data_flow::Tasklet& node) {
1121
    auto& graph = block.dataflow();
8✔
1122

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

1125
    // Delete incoming
1126
    std::list<const data_flow::Memlet*> iedges;
8✔
1127
    for (auto& iedge : graph.in_edges(node)) {
15✔
1128
        iedges.push_back(&iedge);
7✔
1129
    }
1130
    for (auto iedge : iedges) {
15✔
1131
        auto& src = iedge->src();
7✔
1132
        to_delete.insert(&src);
7✔
1133

1134
        auto edge = iedge->edge();
7✔
1135
        graph.edges_.erase(edge);
7✔
1136
        boost::remove_edge(edge, graph.graph_);
7✔
1137
    }
1138

1139
    // Delete outgoing
1140
    std::list<const data_flow::Memlet*> oedges;
8✔
1141
    for (auto& oedge : graph.out_edges(node)) {
16✔
1142
        oedges.push_back(&oedge);
8✔
1143
    }
1144
    for (auto oedge : oedges) {
16✔
1145
        auto& dst = oedge->dst();
8✔
1146
        to_delete.insert(&dst);
8✔
1147

1148
        auto edge = oedge->edge();
8✔
1149
        graph.edges_.erase(edge);
8✔
1150
        boost::remove_edge(edge, graph.graph_);
8✔
1151
    }
1152

1153
    // Delete nodes
1154
    for (auto obsolete_node : to_delete) {
31✔
1155
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1156
            auto vertex = obsolete_node->vertex();
23✔
1157
            graph.nodes_.erase(vertex);
23✔
1158
            boost::remove_vertex(vertex, graph.graph_);
23✔
1159
        }
23✔
1160
    }
1161
};
8✔
1162

1163
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
1164
                                       const data_flow::AccessNode& node) {
1165
    auto& graph = block.dataflow();
1✔
1166
    if (graph.out_degree(node) != 0) {
1✔
1167
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1168
    }
1169

1170
    std::list<const data_flow::Memlet*> tmp;
1✔
1171
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
1172
    while (!queue.empty()) {
3✔
1173
        auto current = queue.front();
2✔
1174
        queue.pop_front();
2✔
1175
        if (current != &node) {
2✔
1176
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
1177
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1178
                    continue;
×
1179
                }
1180
            }
×
1181
        }
1✔
1182

1183
        tmp.clear();
2✔
1184
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1185
            tmp.push_back(&iedge);
1✔
1186
        }
1187
        for (auto iedge : tmp) {
3✔
1188
            auto& src = iedge->src();
1✔
1189
            queue.push_back(&src);
1✔
1190

1191
            auto edge = iedge->edge();
1✔
1192
            graph.edges_.erase(edge);
1✔
1193
            boost::remove_edge(edge, graph.graph_);
1✔
1194
        }
1195

1196
        auto vertex = current->vertex();
2✔
1197
        graph.nodes_.erase(vertex);
2✔
1198
        boost::remove_vertex(vertex, graph.graph_);
2✔
1199
    }
1200
};
1✔
1201

1202
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
×
1203
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
1204
    auto& sdfg = this->subject();
×
1205

1206
    codegen::CPPLanguageExtension language_extension;
×
1207

1208
    // Base cases
1209
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
×
1210
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
×
1211

1212
        // Determine type
1213
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
×
1214
        if (symbolic::is_nvptx(sym)) {
×
1215
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
1216
        } else {
×
1217
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
×
1218
        }
1219

1220
        // Add new container for intermediate result
1221
        auto tmp = this->find_new_name();
×
1222
        this->add_container(tmp, sym_type);
×
1223

1224
        // Create dataflow graph
1225
        auto& input_node = this->add_access(parent, sym->get_name());
×
1226
        auto& output_node = this->add_access(parent, tmp);
×
1227
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
×
1228
                                          {"_out", sym_type}, {{"_in", sym_type}});
×
1229
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {});
×
1230
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1231

1232
        return output_node;
×
1233
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
×
1234
        auto tmp = this->find_new_name();
×
1235
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
1236

1237
        auto& output_node = this->add_access(parent, tmp);
×
1238
        auto& tasklet = this->add_tasklet(
×
1239
            parent, data_flow::TaskletCode::assign,
×
1240
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
1241
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
1242
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1243
        return output_node;
×
1244
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
1245
        auto tmp = this->find_new_name();
×
1246
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1247

1248
        auto& output_node = this->add_access(parent, tmp);
×
1249
        auto& tasklet = this->add_tasklet(
×
1250
            parent, data_flow::TaskletCode::assign,
×
1251
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1252
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1253
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1254
        return output_node;
×
1255
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1256
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
1257
        if (or_expr->get_container().size() != 2) {
×
1258
            throw InvalidSDFGException(
×
1259
                "StructuredSDFGBuilder: Or expression must have exactly two arguments");
×
1260
        }
1261

1262
        std::vector<data_flow::AccessNode*> input_nodes;
×
1263
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1264
        for (auto& arg : or_expr->get_container()) {
×
1265
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1266
            input_nodes.push_back(&input_node);
×
1267
            input_types.push_back(
×
1268
                {"_in" + std::to_string(input_types.size() + 1),
×
1269
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1270
        }
1271

1272
        // Add new container for intermediate result
1273
        auto tmp = this->find_new_name();
×
1274
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1275

1276
        auto& output_node = this->add_access(parent, tmp);
×
1277
        auto& tasklet =
×
1278
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1279
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1280
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1281
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1282
                             {});
×
1283
        }
×
1284
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1285
        return output_node;
×
1286
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1287
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
1288
        if (and_expr->get_container().size() != 2) {
×
1289
            throw InvalidSDFGException(
×
1290
                "StructuredSDFGBuilder: And expression must have exactly two arguments");
×
1291
        }
1292

1293
        std::vector<data_flow::AccessNode*> input_nodes;
×
1294
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1295
        for (auto& arg : and_expr->get_container()) {
×
1296
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1297
            input_nodes.push_back(&input_node);
×
1298
            input_types.push_back(
×
1299
                {"_in" + std::to_string(input_types.size() + 1),
×
1300
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1301
        }
1302

1303
        // Add new container for intermediate result
1304
        auto tmp = this->find_new_name();
×
1305
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1306

1307
        auto& output_node = this->add_access(parent, tmp);
×
1308
        auto& tasklet =
×
1309
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1310
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1311
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1312
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1313
                             {});
×
1314
        }
×
1315
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1316
        return output_node;
×
1317
    } else {
×
1318
        throw std::runtime_error("Unsupported expression type");
×
1319
    }
1320
};
×
1321

1322
}  // namespace builder
1323
}  // 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

© 2025 Coveralls, Inc