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

daisytuner / sdfglib / 15274150764

27 May 2025 11:33AM UTC coverage: 58.452% (+0.2%) from 58.296%
15274150764

push

github

web-flow
Merge pull request #38 from daisytuner/multi-exit-loops

declares multi-exit loops as unstructured control flow

0 of 1 new or added line in 1 file covered. (0.0%)

2 existing lines in 1 file now uncovered.

7912 of 13536 relevant lines covered (58.45%)

96.33 hits per line

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

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

3
#include "sdfg/codegen/language_extensions/cpp_language_extension.h"
4
#include "sdfg/data_flow/library_node.h"
5
#include "sdfg/types/utils.h"
6

7
using namespace sdfg::control_flow;
8
using namespace sdfg::structured_control_flow;
9

10
namespace sdfg {
11
namespace builder {
12

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

26
        nodes.insert(curr);
3✔
27
        if (curr == &end) {
3✔
28
            continue;
1✔
29
        }
30

31
        for (auto& iedge : sdfg.in_edges(*curr)) {
4✔
32
            queue.push_back(&iedge.src());
2✔
33
        }
34
    }
35

36
    return nodes;
1✔
37
};
1✔
38

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

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

54
    return false;
×
55
}
6✔
56

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

68
    return pdom;
3✔
69
}
3✔
70

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

76
    auto pdom_tree = sdfg.post_dominator_tree();
4✔
77

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

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

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

99
    auto in_edges = sdfg.in_edges(*current);
8✔
100

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

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

132
        for (auto& edge : breaks) {
1✔
133
            exit_edges.insert(edge);
×
134
        }
135

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

148
        // 3. Add while loop
149
        While& loop = this->add_while(scope, {}, dbg_info);
1✔
150

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

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

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

177
        if (visited.find(curr) != visited.end()) {
15✔
178
            throw UnstructuredControlFlowException();
×
179
        }
180
        visited.insert(curr);
15✔
181

182
        auto out_edges = sdfg.out_edges(*curr);
15✔
183
        auto out_degree = sdfg.out_degree(*curr);
15✔
184

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

192
        // Case 2: Transition
193
        if (out_degree == 1) {
11✔
194
            auto& oedge = *out_edges.begin();
8✔
195
            if (!oedge.is_unconditional()) {
8✔
196
                throw InvalidSDFGException(
×
197
                    "Degenerated structured control flow: Non-deterministic transition");
×
198
            }
199
            this->add_block(scope, curr->dataflow(), oedge.assignments(), curr->debug_info());
8✔
200

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

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

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

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

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

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

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

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

285
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
305✔
286
    : FunctionBuilder(), structured_sdfg_(std::move(sdfg)) {};
305✔
287

288
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name)
443✔
289
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name)) {};
443✔
290

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

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

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

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

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

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

317
    this->traverse(sdfg);
4✔
318
};
4✔
319

320
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
3,035✔
321

322
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
420✔
323
    return std::move(this->structured_sdfg_);
420✔
324
};
325

326
Sequence& StructuredSDFGBuilder::add_sequence(Sequence& parent,
34✔
327
                                              const sdfg::symbolic::Assignments& assignments,
328
                                              const DebugInfo& debug_info) {
329
    parent.children_.push_back(
34✔
330
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
34✔
331
    this->element_counter_++;
34✔
332
    parent.transitions_.push_back(std::unique_ptr<Transition>(
34✔
333
        new Transition(this->element_counter_, debug_info, assignments)));
34✔
334
    this->element_counter_++;
34✔
335

336
    return static_cast<Sequence&>(*parent.children_.back().get());
34✔
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(
4✔
354
        parent.children_.begin() + index,
4✔
355
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
4✔
356
    this->element_counter_++;
4✔
357
    parent.transitions_.insert(
4✔
358
        parent.transitions_.begin() + index,
4✔
359
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
4✔
360
    this->element_counter_++;
4✔
361
    auto new_entry = parent.at(index);
4✔
362
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
4✔
363

364
    return {new_block, new_entry.second};
4✔
365
};
×
366

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

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

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

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

396
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
453✔
397
                                        const sdfg::symbolic::Assignments& assignments,
398
                                        const DebugInfo& debug_info) {
399
    parent.children_.push_back(
453✔
400
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info)));
453✔
401
    this->element_counter_++;
453✔
402
    parent.transitions_.push_back(std::unique_ptr<Transition>(
453✔
403
        new Transition(this->element_counter_, debug_info, assignments)));
453✔
404
    this->element_counter_++;
453✔
405
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
453✔
406
    (*new_block.dataflow_).parent_ = &new_block;
453✔
407

408
    return new_block;
453✔
409
};
×
410

411
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
28✔
412
                                        const data_flow::DataFlowGraph& data_flow_graph,
413
                                        const sdfg::symbolic::Assignments& assignments,
414
                                        const DebugInfo& debug_info) {
415
    parent.children_.push_back(
28✔
416
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info, data_flow_graph)));
28✔
417
    this->element_counter_++;
28✔
418
    parent.transitions_.push_back(std::unique_ptr<Transition>(
28✔
419
        new Transition(this->element_counter_, debug_info, assignments)));
28✔
420
    this->element_counter_++;
28✔
421
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
28✔
422
    (*new_block.dataflow_).parent_ = &new_block;
28✔
423

424
    return new_block;
28✔
425
};
×
426

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

439
    parent.children_.insert(parent.children_.begin() + index,
12✔
440
                            std::unique_ptr<Block>(new Block(this->element_counter_, debug_info)));
12✔
441
    this->element_counter_++;
12✔
442
    parent.transitions_.insert(
12✔
443
        parent.transitions_.begin() + index,
12✔
444
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
12✔
445
    this->element_counter_++;
12✔
446
    auto new_entry = parent.at(index);
12✔
447
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
12✔
448
    (*new_block.dataflow_).parent_ = &new_block;
12✔
449

450
    return {new_block, new_entry.second};
12✔
451
};
×
452

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

466
    parent.children_.insert(
×
467
        parent.children_.begin() + index,
×
468
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info, data_flow_graph)));
×
469
    this->element_counter_++;
×
470
    parent.transitions_.insert(
×
471
        parent.transitions_.begin() + index,
×
472
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
×
473
    this->element_counter_++;
×
474
    auto new_entry = parent.at(index);
×
475
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
476
    (*new_block.dataflow_).parent_ = &new_block;
×
477

478
    return {new_block, new_entry.second};
×
479
};
×
480

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

494
    parent.children_.insert(parent.children_.begin() + index + 1,
2✔
495
                            std::unique_ptr<Block>(new Block(this->element_counter_, debug_info)));
2✔
496
    this->element_counter_++;
2✔
497
    parent.transitions_.insert(
2✔
498
        parent.transitions_.begin() + index + 1,
2✔
499
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
2✔
500
    this->element_counter_++;
2✔
501
    auto new_entry = parent.at(index + 1);
2✔
502
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
2✔
503
    (*new_block.dataflow_).parent_ = &new_block;
2✔
504

505
    return {new_block, new_entry.second};
2✔
506
};
×
507

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

520
    parent.children_.insert(
×
521
        parent.children_.begin() + index + 1,
×
522
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info, data_flow_graph)));
×
523
    this->element_counter_++;
×
524
    parent.transitions_.insert(
×
525
        parent.transitions_.begin() + index + 1,
×
526
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
×
527
    this->element_counter_++;
×
528
    auto new_entry = parent.at(index + 1);
×
529
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
530
    (*new_block.dataflow_).parent_ = &new_block;
×
531

532
    return {new_block, new_entry.second};
×
533
};
×
534

535
For& StructuredSDFGBuilder::add_for(Sequence& parent, const symbolic::Symbol& indvar,
107✔
536
                                    const symbolic::Condition& condition,
537
                                    const symbolic::Expression& init,
538
                                    const symbolic::Expression& update,
539
                                    const sdfg::symbolic::Assignments& assignments,
540
                                    const DebugInfo& debug_info) {
541
    parent.children_.push_back(std::unique_ptr<For>(
214✔
542
        new For(this->element_counter_, debug_info, indvar, init, update, condition)));
107✔
543
    this->element_counter_ = this->element_counter_ + 2;
107✔
544
    parent.transitions_.push_back(std::unique_ptr<Transition>(
107✔
545
        new Transition(this->element_counter_, debug_info, assignments)));
107✔
546
    this->element_counter_++;
107✔
547

548
    return static_cast<For&>(*parent.children_.back().get());
107✔
549
};
×
550

551
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_before(
4✔
552
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
553
    const symbolic::Condition& condition, const symbolic::Expression& init,
554
    const symbolic::Expression& update, const DebugInfo& debug_info) {
555
    // Insert block before current block
556
    int index = -1;
4✔
557
    for (size_t i = 0; i < parent.children_.size(); i++) {
4✔
558
        if (parent.children_.at(i).get() == &block) {
4✔
559
            index = i;
4✔
560
            break;
4✔
561
        }
562
    }
×
563
    assert(index > -1);
4✔
564

565
    parent.children_.insert(parent.children_.begin() + index,
8✔
566
                            std::unique_ptr<For>(new For(this->element_counter_, debug_info, indvar,
8✔
567
                                                         init, update, condition)));
4✔
568
    this->element_counter_ = this->element_counter_ + 2;
4✔
569
    parent.transitions_.insert(
4✔
570
        parent.transitions_.begin() + index,
4✔
571
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
4✔
572
    this->element_counter_++;
4✔
573
    auto new_entry = parent.at(index);
4✔
574
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
4✔
575

576
    return {new_block, new_entry.second};
4✔
577
};
×
578

579
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_after(
2✔
580
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
581
    const symbolic::Condition& condition, const symbolic::Expression& init,
582
    const symbolic::Expression& update, const DebugInfo& debug_info) {
583
    // Insert block before current block
584
    int index = -1;
2✔
585
    for (size_t i = 0; i < parent.children_.size(); i++) {
3✔
586
        if (parent.children_.at(i).get() == &block) {
3✔
587
            index = i;
2✔
588
            break;
2✔
589
        }
590
    }
1✔
591
    assert(index > -1);
2✔
592

593
    parent.children_.insert(parent.children_.begin() + index + 1,
4✔
594
                            std::unique_ptr<For>(new For(this->element_counter_, debug_info, indvar,
4✔
595
                                                         init, update, condition)));
2✔
596
    this->element_counter_ = this->element_counter_ + 2;
2✔
597
    parent.transitions_.insert(
2✔
598
        parent.transitions_.begin() + index + 1,
2✔
599
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
2✔
600
    this->element_counter_++;
2✔
601
    auto new_entry = parent.at(index + 1);
2✔
602
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
2✔
603

604
    return {new_block, new_entry.second};
2✔
605
};
×
606

607
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent, const DebugInfo& debug_info) {
34✔
608
    return this->add_if_else(parent, symbolic::Assignments{}, debug_info);
34✔
609
};
×
610

611
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent,
35✔
612
                                           const sdfg::symbolic::Assignments& assignments,
613
                                           const DebugInfo& debug_info) {
614
    parent.children_.push_back(
35✔
615
        std::unique_ptr<IfElse>(new IfElse(this->element_counter_, debug_info)));
35✔
616
    this->element_counter_++;
35✔
617
    parent.transitions_.push_back(std::unique_ptr<Transition>(
35✔
618
        new Transition(this->element_counter_, debug_info, assignments)));
35✔
619
    this->element_counter_++;
35✔
620
    return static_cast<IfElse&>(*parent.children_.back().get());
35✔
621
};
×
622

623
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::add_if_else_before(
1✔
624
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
625
    // Insert block before current block
626
    int index = -1;
1✔
627
    for (size_t i = 0; i < parent.children_.size(); i++) {
1✔
628
        if (parent.children_.at(i).get() == &block) {
1✔
629
            index = i;
1✔
630
            break;
1✔
631
        }
632
    }
×
633
    assert(index > -1);
1✔
634

635
    parent.children_.insert(
1✔
636
        parent.children_.begin() + index,
1✔
637
        std::unique_ptr<IfElse>(new IfElse(this->element_counter_, debug_info)));
1✔
638
    this->element_counter_++;
1✔
639
    parent.transitions_.insert(
1✔
640
        parent.transitions_.begin() + index,
1✔
641
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
1✔
642
    this->element_counter_++;
1✔
643
    auto new_entry = parent.at(index);
1✔
644
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
1✔
645

646
    return {new_block, new_entry.second};
1✔
647
};
×
648

649
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond,
61✔
650
                                          const DebugInfo& debug_info) {
651
    scope.cases_.push_back(
61✔
652
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
61✔
653
    this->element_counter_++;
61✔
654
    scope.conditions_.push_back(cond);
61✔
655
    return *scope.cases_.back();
61✔
656
};
×
657

658
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t i, const DebugInfo& debug_info) {
1✔
659
    scope.cases_.erase(scope.cases_.begin() + i);
1✔
660
    scope.conditions_.erase(scope.conditions_.begin() + i);
1✔
661
};
1✔
662

663
While& StructuredSDFGBuilder::add_while(Sequence& parent,
36✔
664
                                        const sdfg::symbolic::Assignments& assignments,
665
                                        const DebugInfo& debug_info) {
666
    parent.children_.push_back(
36✔
667
        std::unique_ptr<While>(new While(this->element_counter_, debug_info)));
36✔
668
    this->element_counter_ = this->element_counter_ + 2;
36✔
669
    parent.transitions_.push_back(std::unique_ptr<Transition>(
36✔
670
        new Transition(this->element_counter_, debug_info, assignments)));
36✔
671
    this->element_counter_++;
36✔
672
    return static_cast<While&>(*parent.children_.back().get());
36✔
673
};
×
674

675
Kernel& StructuredSDFGBuilder::add_kernel(
16✔
676
    Sequence& parent, const std::string& suffix, const DebugInfo& debug_info,
677
    const symbolic::Expression& gridDim_x_init, const symbolic::Expression& gridDim_y_init,
678
    const symbolic::Expression& gridDim_z_init, const symbolic::Expression& blockDim_x_init,
679
    const symbolic::Expression& blockDim_y_init, const symbolic::Expression& blockDim_z_init,
680
    const symbolic::Expression& blockIdx_x_init, const symbolic::Expression& blockIdx_y_init,
681
    const symbolic::Expression& blockIdx_z_init, const symbolic::Expression& threadIdx_x_init,
682
    const symbolic::Expression& threadIdx_y_init, const symbolic::Expression& threadIdx_z_init) {
683
    parent.children_.push_back(std::unique_ptr<Kernel>(new Kernel(
32✔
684
        this->element_counter_, debug_info, suffix, gridDim_x_init, gridDim_y_init, gridDim_z_init,
16✔
685
        blockDim_x_init, blockDim_y_init, blockDim_z_init, blockIdx_x_init, blockIdx_y_init,
16✔
686
        blockIdx_z_init, threadIdx_x_init, threadIdx_y_init, threadIdx_z_init)));
16✔
687
    this->element_counter_ = this->element_counter_ + 2;
16✔
688
    parent.transitions_.push_back(
16✔
689
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
16✔
690
    this->element_counter_++;
16✔
691
    return static_cast<Kernel&>(*parent.children_.back().get());
16✔
692
};
×
693

694
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent, const DebugInfo& debug_info) {
14✔
695
    return this->add_continue(parent, symbolic::Assignments{}, debug_info);
14✔
696
};
×
697

698
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent,
15✔
699
                                              const sdfg::symbolic::Assignments& assignments,
700
                                              const DebugInfo& debug_info) {
701
    parent.children_.push_back(
15✔
702
        std::unique_ptr<Continue>(new Continue(this->element_counter_, debug_info)));
15✔
703
    this->element_counter_++;
15✔
704
    parent.transitions_.push_back(std::unique_ptr<Transition>(
15✔
705
        new Transition(this->element_counter_, debug_info, assignments)));
15✔
706
    this->element_counter_++;
15✔
707
    return static_cast<Continue&>(*parent.children_.back().get());
15✔
708
};
×
709

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

714
Break& StructuredSDFGBuilder::add_break(Sequence& parent,
16✔
715
                                        const sdfg::symbolic::Assignments& assignments,
716
                                        const DebugInfo& debug_info) {
717
    parent.children_.push_back(
16✔
718
        std::unique_ptr<Break>(new Break(this->element_counter_, debug_info)));
16✔
719
    this->element_counter_++;
16✔
720
    parent.transitions_.push_back(std::unique_ptr<Transition>(
16✔
721
        new Transition(this->element_counter_, debug_info, assignments)));
16✔
722
    this->element_counter_++;
16✔
723
    return static_cast<Break&>(*parent.children_.back().get());
16✔
724
};
×
725

726
Return& StructuredSDFGBuilder::add_return(Sequence& parent,
14✔
727
                                          const sdfg::symbolic::Assignments& assignments,
728
                                          const DebugInfo& debug_info) {
729
    parent.children_.push_back(
14✔
730
        std::unique_ptr<Return>(new Return(this->element_counter_, debug_info)));
14✔
731
    this->element_counter_++;
14✔
732
    parent.transitions_.push_back(std::unique_ptr<Transition>(
14✔
733
        new Transition(this->element_counter_, debug_info, assignments)));
14✔
734
    this->element_counter_++;
14✔
735
    return static_cast<Return&>(*parent.children_.back().get());
14✔
736
};
×
737

738
For& StructuredSDFGBuilder::convert_while(Sequence& parent, While& loop,
×
739
                                          const symbolic::Symbol& indvar,
740
                                          const symbolic::Condition& condition,
741
                                          const symbolic::Expression& init,
742
                                          const symbolic::Expression& update) {
743
    // Insert for loop
744
    size_t index = 0;
×
745
    for (auto& entry : parent.children_) {
×
746
        if (entry.get() == &loop) {
×
747
            break;
×
748
        }
749
        index++;
×
750
    }
751
    auto iter = parent.children_.begin() + index;
×
752
    auto& new_iter = *parent.children_.insert(
×
753
        iter + 1, std::unique_ptr<For>(new For(this->element_counter_, loop.debug_info(), indvar,
×
754
                                               init, update, condition)));
×
755
    this->element_counter_ = this->element_counter_ + 2;
×
756
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
757
    this->insert_children(for_loop.root(), loop.root(), 0);
×
758

759
    // Remove while loop
760
    parent.children_.erase(parent.children_.begin() + index);
×
761

762
    return for_loop;
×
763
};
×
764

765
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
2✔
766
    parent.children_.clear();
2✔
767
    parent.transitions_.clear();
2✔
768
};
2✔
769

770
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
2✔
771
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
2✔
772
    while (!queue.empty()) {
2✔
773
        auto current = queue.front();
2✔
774
        queue.pop_front();
2✔
775

776
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
2✔
777
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
2✔
778
                if (&sequence_stmt->at(i).first == &node) {
2✔
779
                    return *sequence_stmt;
2✔
780
                }
781
                queue.push_back(&sequence_stmt->at(i).first);
×
782
            }
×
783
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
784
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
785
                queue.push_back(&if_else_stmt->at(i).first);
×
786
            }
×
787
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
788
            queue.push_back(&while_stmt->root());
×
789
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
×
790
            queue.push_back(&for_stmt->root());
×
791
        } else if (auto kern_stmt = dynamic_cast<const structured_control_flow::Kernel*>(current)) {
×
792
            queue.push_back(&kern_stmt->root());
×
793
        }
×
794
    }
795

796
    return this->structured_sdfg_->root();
×
797
};
2✔
798

799
Kernel& StructuredSDFGBuilder::convert_into_kernel() {
5✔
800
    auto old_root = std::move(this->structured_sdfg_->root_);
5✔
801
    this->structured_sdfg_->root_ =
5✔
802
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, old_root->debug_info()));
5✔
803
    this->element_counter_++;
5✔
804
    auto& new_root = this->structured_sdfg_->root();
5✔
805
    auto& kernel = this->add_kernel(new_root, this->function().name());
5✔
806

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

809
    types::Scalar gridDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
810
    add_container(kernel.gridDim_x()->get_name(), gridDim_x);
5✔
811
    types::Scalar gridDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
812
    add_container(kernel.gridDim_y()->get_name(), gridDim_y);
5✔
813
    types::Scalar gridDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
814
    add_container(kernel.gridDim_z()->get_name(), gridDim_z);
5✔
815
    types::Scalar blockDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
816
    add_container(kernel.blockDim_x()->get_name(), blockDim_x);
5✔
817
    types::Scalar blockDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
818
    add_container(kernel.blockDim_y()->get_name(), blockDim_y);
5✔
819
    types::Scalar blockDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
820
    add_container(kernel.blockDim_z()->get_name(), blockDim_z);
5✔
821
    types::Scalar blockIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
822
    add_container(kernel.blockIdx_x()->get_name(), blockIdx_x);
5✔
823
    types::Scalar blockIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
824
    add_container(kernel.blockIdx_y()->get_name(), blockIdx_y);
5✔
825
    types::Scalar blockIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
826
    add_container(kernel.blockIdx_z()->get_name(), blockIdx_z);
5✔
827
    types::Scalar threadIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
828
    add_container(kernel.threadIdx_x()->get_name(), threadIdx_x);
5✔
829
    types::Scalar threadIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
830
    add_container(kernel.threadIdx_y()->get_name(), threadIdx_y);
5✔
831
    types::Scalar threadIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
5✔
832
    add_container(kernel.threadIdx_z()->get_name(), threadIdx_z);
5✔
833

834
    kernel.root().replace(symbolic::symbol("gridDim.x"), kernel.gridDim_x());
5✔
835
    kernel.root().replace(symbolic::symbol("gridDim.y"), kernel.gridDim_y());
5✔
836
    kernel.root().replace(symbolic::symbol("gridDim.z"), kernel.gridDim_z());
5✔
837

838
    kernel.root().replace(symbolic::symbol("blockDim.x"), kernel.blockDim_x());
5✔
839
    kernel.root().replace(symbolic::symbol("blockDim.y"), kernel.blockDim_y());
5✔
840
    kernel.root().replace(symbolic::symbol("blockDim.z"), kernel.blockDim_z());
5✔
841

842
    kernel.root().replace(symbolic::symbol("blockIdx.x"), kernel.blockIdx_x());
5✔
843
    kernel.root().replace(symbolic::symbol("blockIdx.y"), kernel.blockIdx_y());
5✔
844
    kernel.root().replace(symbolic::symbol("blockIdx.z"), kernel.blockIdx_z());
5✔
845

846
    kernel.root().replace(symbolic::symbol("threadIdx.x"), kernel.threadIdx_x());
5✔
847
    kernel.root().replace(symbolic::symbol("threadIdx.y"), kernel.threadIdx_y());
5✔
848
    kernel.root().replace(symbolic::symbol("threadIdx.z"), kernel.threadIdx_z());
5✔
849

850
    return kernel;
5✔
851
};
5✔
852

853
/***** Section: Dataflow Graph *****/
854

855
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
533✔
856
                                                         const std::string& data,
857
                                                         const DebugInfo& debug_info) {
858
    // Check: Data exists
859
    if (!this->subject().exists(data)) {
533✔
860
        throw InvalidSDFGException("Data does not exist in SDFG: " + data);
×
861
    }
862

863
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
533✔
864
    auto res = block.dataflow_->nodes_.insert(
1,066✔
865
        {vertex, std::unique_ptr<data_flow::AccessNode>(new data_flow::AccessNode(
533✔
866
                     this->element_counter_, debug_info, vertex, block.dataflow(), data))});
533✔
867
    this->element_counter_++;
533✔
868
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
533✔
869
};
×
870

871
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
343✔
872
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
873
    const std::pair<std::string, sdfg::types::Scalar>& output,
874
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
875
    const DebugInfo& debug_info) {
876
    // Check: Duplicate inputs
877
    std::unordered_set<std::string> input_names;
343✔
878
    for (auto& input : inputs) {
790✔
879
        if (input_names.find(input.first) != input_names.end()) {
447✔
880
            throw InvalidSDFGException("Input " + input.first + " already exists in SDFG");
×
881
        }
882
        input_names.insert(input.first);
447✔
883
    }
884

885
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
343✔
886
    auto res = block.dataflow_->nodes_.insert(
686✔
887
        {vertex, std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
686✔
888
                     this->element_counter_, debug_info, vertex, block.dataflow(), code, output,
343✔
889
                     inputs, symbolic::__true__()))});
343✔
890
    this->element_counter_++;
343✔
891
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
343✔
892
};
343✔
893

894
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
544✔
895
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
896
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
897
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
898
    auto& function_ = this->function();
544✔
899

900
    // Check - Case 1: Access Node -> Access Node
901
    // - src_conn or dst_conn must be refs. The other must be void.
902
    // - The side of the memlet that is void, is dereferenced.
903
    // - The dst type must always be a pointer after potential dereferencing.
904
    // - The src type can be any type after dereferecing (&dereferenced_src_type).
905
    if (dynamic_cast<data_flow::AccessNode*>(&src) && dynamic_cast<data_flow::AccessNode*>(&dst)) {
544✔
906
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
3✔
907
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
3✔
908
        if (src_conn == "refs") {
3✔
909
            if (dst_conn != "void") {
1✔
910
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
911
            }
912

913
            auto& dst_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
1✔
914
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
1✔
915
                throw InvalidSDFGException("dst type must be a pointer");
×
916
            }
917

918
            auto& src_type = function_.type(src_node.data());
1✔
919
            if (!dynamic_cast<const types::Pointer*>(&src_type)) {
1✔
920
                throw InvalidSDFGException("src type must be a pointer");
×
921
            }
922
        } else if (src_conn == "void") {
3✔
923
            if (dst_conn != "refs") {
2✔
924
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
925
            }
926

927
            if (symbolic::is_pointer(symbolic::symbol(src_node.data()))) {
2✔
928
                throw InvalidSDFGException("src_conn is void: src cannot be a raw pointer");
×
929
            }
930

931
            // Trivially correct but checks inference
932
            auto& src_type = types::infer_type(function_, function_.type(src_node.data()), subset);
2✔
933
            types::Pointer ref_type(src_type);
2✔
934
            if (!dynamic_cast<const types::Pointer*>(&ref_type)) {
935
                throw InvalidSDFGException("src type must be a pointer");
936
            }
937

938
            auto& dst_type = function_.type(dst_node.data());
2✔
939
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
2✔
940
                throw InvalidSDFGException("dst type must be a pointer");
×
941
            }
942
        } else {
2✔
943
            throw InvalidSDFGException("Invalid src connector: " + src_conn);
×
944
        }
945
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
742✔
946
               dynamic_cast<data_flow::Tasklet*>(&dst)) {
198✔
947
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
198✔
948
        auto& dst_node = dynamic_cast<data_flow::Tasklet&>(dst);
198✔
949
        if (src_conn != "void") {
198✔
950
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
951
        }
952
        bool found = false;
198✔
953
        for (auto& input : dst_node.inputs()) {
252✔
954
            if (input.first == dst_conn) {
252✔
955
                found = true;
198✔
956
                break;
198✔
957
            }
958
        }
959
        if (!found) {
198✔
960
            throw InvalidSDFGException("dst_conn not found in tasklet: " + dst_conn);
×
961
        }
962
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
198✔
963
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
198✔
964
            throw InvalidSDFGException("Tasklets inputs must be scalars");
×
965
        }
966
    } else if (dynamic_cast<data_flow::Tasklet*>(&src) &&
884✔
967
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
343✔
968
        auto& src_node = dynamic_cast<data_flow::Tasklet&>(src);
343✔
969
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
343✔
970
        if (src_conn != src_node.outputs()[0].first) {
343✔
971
            throw InvalidSDFGException("src_conn must match tasklet output name");
×
972
        }
973
        if (dst_conn != "void") {
343✔
974
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
975
        }
976

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

999
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
×
1000
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
×
1001
            throw InvalidSDFGException("Library node inputs must be scalars");
×
1002
        }
1003
    } else if (dynamic_cast<data_flow::LibraryNode*>(&src) &&
×
1004
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
×
1005
        auto& src_node = dynamic_cast<data_flow::LibraryNode&>(src);
×
1006
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
×
1007
        if (dst_conn != "void") {
×
1008
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
1009
        }
1010
        bool found = false;
×
1011
        for (auto& output : src_node.outputs()) {
×
1012
            if (output.first == src_conn) {
×
1013
                found = true;
×
1014
                break;
×
1015
            }
1016
        }
1017
        if (!found) {
×
1018
            throw InvalidSDFGException("src_conn not found in library node: " + src_conn);
×
1019
        }
1020

1021
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
1022
        if (!dynamic_cast<const types::Pointer*>(&element_type)) {
×
1023
            throw InvalidSDFGException("Access node must be a pointer");
×
1024
        }
1025
    } else {
×
1026
        throw InvalidSDFGException("Invalid src or dst node type");
×
1027
    }
1028

1029
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
544✔
1030
    auto res = block.dataflow_->edges_.insert(
1,088✔
1031
        {edge.first, std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
544✔
1032
                         this->element_counter_, debug_info, edge.first, block.dataflow(), src,
544✔
1033
                         src_conn, dst, dst_conn, subset))});
544✔
1034
    this->element_counter_++;
544✔
1035
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
544✔
1036
};
×
1037

1038
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
9✔
1039
    structured_control_flow::Block& block, const data_flow::LibraryNodeType& call,
1040
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& outputs,
1041
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
1042
    const bool has_side_effect, const DebugInfo& debug_info) {
1043
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
9✔
1044
    auto res = block.dataflow_->nodes_.insert(
18✔
1045
        {vertex, std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
9✔
1046
                     this->element_counter_, debug_info, vertex, block.dataflow(), outputs, inputs,
9✔
1047
                     call, has_side_effect))});
9✔
1048
    this->element_counter_++;
9✔
1049
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
9✔
1050
}
×
1051

1052
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
1053
                                          const data_flow::Memlet& edge) {
1054
    auto& graph = block.dataflow();
×
1055
    auto e = edge.edge();
×
1056
    boost::remove_edge(e, graph.graph_);
×
1057
    graph.edges_.erase(e);
×
1058
};
×
1059

1060
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
1061
                                        const data_flow::DataFlowNode& node) {
1062
    auto& graph = block.dataflow();
×
1063
    auto v = node.vertex();
×
1064
    boost::remove_vertex(v, graph.graph_);
×
1065
    graph.nodes_.erase(v);
×
1066
};
×
1067

1068
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
1069
                                       const data_flow::Tasklet& node) {
1070
    auto& graph = block.dataflow();
8✔
1071

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

1074
    // Delete incoming
1075
    std::list<const data_flow::Memlet*> iedges;
8✔
1076
    for (auto& iedge : graph.in_edges(node)) {
15✔
1077
        iedges.push_back(&iedge);
7✔
1078
    }
1079
    for (auto iedge : iedges) {
15✔
1080
        auto& src = iedge->src();
7✔
1081
        to_delete.insert(&src);
7✔
1082

1083
        auto edge = iedge->edge();
7✔
1084
        graph.edges_.erase(edge);
7✔
1085
        boost::remove_edge(edge, graph.graph_);
7✔
1086
    }
1087

1088
    // Delete outgoing
1089
    std::list<const data_flow::Memlet*> oedges;
8✔
1090
    for (auto& oedge : graph.out_edges(node)) {
16✔
1091
        oedges.push_back(&oedge);
8✔
1092
    }
1093
    for (auto oedge : oedges) {
16✔
1094
        auto& dst = oedge->dst();
8✔
1095
        to_delete.insert(&dst);
8✔
1096

1097
        auto edge = oedge->edge();
8✔
1098
        graph.edges_.erase(edge);
8✔
1099
        boost::remove_edge(edge, graph.graph_);
8✔
1100
    }
1101

1102
    // Delete nodes
1103
    for (auto obsolete_node : to_delete) {
31✔
1104
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1105
            auto vertex = obsolete_node->vertex();
23✔
1106
            graph.nodes_.erase(vertex);
23✔
1107
            boost::remove_vertex(vertex, graph.graph_);
23✔
1108
        }
23✔
1109
    }
1110
};
8✔
1111

1112
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
1113
                                       const data_flow::AccessNode& node) {
1114
    auto& graph = block.dataflow();
1✔
1115
    if (graph.out_degree(node) != 0) {
1✔
1116
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1117
    }
1118

1119
    std::list<const data_flow::Memlet*> tmp;
1✔
1120
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
1121
    while (!queue.empty()) {
3✔
1122
        auto current = queue.front();
2✔
1123
        queue.pop_front();
2✔
1124
        if (current != &node) {
2✔
1125
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
1126
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1127
                    continue;
×
1128
                }
1129
            }
×
1130
        }
1✔
1131

1132
        tmp.clear();
2✔
1133
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1134
            tmp.push_back(&iedge);
1✔
1135
        }
1136
        for (auto iedge : tmp) {
3✔
1137
            auto& src = iedge->src();
1✔
1138
            queue.push_back(&src);
1✔
1139

1140
            auto edge = iedge->edge();
1✔
1141
            graph.edges_.erase(edge);
1✔
1142
            boost::remove_edge(edge, graph.graph_);
1✔
1143
        }
1144

1145
        auto vertex = current->vertex();
2✔
1146
        graph.nodes_.erase(vertex);
2✔
1147
        boost::remove_vertex(vertex, graph.graph_);
2✔
1148
    }
1149
};
1✔
1150

1151
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
×
1152
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
1153
    auto& sdfg = this->subject();
×
1154

1155
    codegen::CPPLanguageExtension language_extension;
×
1156

1157
    // Base cases
1158
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
×
1159
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
×
1160

1161
        // Determine type
1162
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
×
1163
        if (symbolic::is_nvptx(sym)) {
×
1164
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
1165
        } else {
×
1166
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
×
1167
        }
1168

1169
        // Add new container for intermediate result
1170
        auto tmp = this->find_new_name();
×
1171
        this->add_container(tmp, sym_type);
×
1172

1173
        // Create dataflow graph
1174
        auto& input_node = this->add_access(parent, sym->get_name());
×
1175
        auto& output_node = this->add_access(parent, tmp);
×
1176
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
×
1177
                                          {"_out", sym_type}, {{"_in", sym_type}});
×
1178
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {});
×
1179
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1180

1181
        return output_node;
×
1182
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
×
1183
        auto tmp = this->find_new_name();
×
1184
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
1185

1186
        auto& output_node = this->add_access(parent, tmp);
×
1187
        auto& tasklet = this->add_tasklet(
×
1188
            parent, data_flow::TaskletCode::assign,
×
1189
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
1190
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
1191
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1192
        return output_node;
×
1193
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
1194
        auto tmp = this->find_new_name();
×
1195
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1196

1197
        auto& output_node = this->add_access(parent, tmp);
×
1198
        auto& tasklet = this->add_tasklet(
×
1199
            parent, data_flow::TaskletCode::assign,
×
1200
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1201
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1202
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1203
        return output_node;
×
1204
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1205
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
1206
        if (or_expr->get_container().size() != 2) {
×
1207
            throw InvalidSDFGException(
×
1208
                "StructuredSDFGBuilder: Or expression must have exactly two arguments");
×
1209
        }
1210

1211
        std::vector<data_flow::AccessNode*> input_nodes;
×
1212
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1213
        for (auto& arg : or_expr->get_container()) {
×
1214
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1215
            input_nodes.push_back(&input_node);
×
1216
            input_types.push_back(
×
1217
                {"_in" + std::to_string(input_types.size() + 1),
×
1218
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1219
        }
1220

1221
        // Add new container for intermediate result
1222
        auto tmp = this->find_new_name();
×
1223
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1224

1225
        auto& output_node = this->add_access(parent, tmp);
×
1226
        auto& tasklet =
×
1227
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1228
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1229
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1230
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1231
                             {});
×
1232
        }
×
1233
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1234
        return output_node;
×
1235
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1236
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
1237
        if (and_expr->get_container().size() != 2) {
×
1238
            throw InvalidSDFGException(
×
1239
                "StructuredSDFGBuilder: And expression must have exactly two arguments");
×
1240
        }
1241

1242
        std::vector<data_flow::AccessNode*> input_nodes;
×
1243
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1244
        for (auto& arg : and_expr->get_container()) {
×
1245
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1246
            input_nodes.push_back(&input_node);
×
1247
            input_types.push_back(
×
1248
                {"_in" + std::to_string(input_types.size() + 1),
×
1249
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1250
        }
1251

1252
        // Add new container for intermediate result
1253
        auto tmp = this->find_new_name();
×
1254
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1255

1256
        auto& output_node = this->add_access(parent, tmp);
×
1257
        auto& tasklet =
×
1258
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1259
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1260
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1261
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1262
                             {});
×
1263
        }
×
1264
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {});
×
1265
        return output_node;
×
1266
    } else {
×
1267
        throw std::runtime_error("Unsupported expression type");
×
1268
    }
1269
};
×
1270

1271
}  // namespace builder
1272
}  // 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