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

daisytuner / sdfglib / 15238327410

25 May 2025 01:21PM UTC coverage: 60.381% (+0.04%) from 60.342%
15238327410

Pull #32

github

web-flow
Merge 979e5bb69 into 337a208bc
Pull Request #32: Test formatting

297 of 456 new or added lines in 52 files covered. (65.13%)

19 existing lines in 15 files now uncovered.

8268 of 13693 relevant lines covered (60.38%)

99.82 hits per line

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

70.95
/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/structured_control_flow/map.h"
6
#include "sdfg/structured_control_flow/sequence.h"
7

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

11
namespace sdfg {
12
namespace builder {
13

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

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

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

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

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

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

55
    return false;
×
56
}
6✔
57

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

69
    return pdom;
3✔
70
}
3✔
71

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

290
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name)
477✔
291
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name)) {};
477✔
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_; };
2,570✔
323

324
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
443✔
325
    return std::move(this->structured_sdfg_);
443✔
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(
36✔
332
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
36✔
333
    this->element_counter_++;
36✔
334
    parent.transitions_.push_back(std::unique_ptr<Transition>(
36✔
335
        new Transition(this->element_counter_, debug_info, assignments)));
36✔
336
    this->element_counter_++;
36✔
337

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

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

355
    parent.children_.insert(
4✔
356
        parent.children_.begin() + index,
4✔
357
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
4✔
358
    this->element_counter_++;
4✔
359
    parent.transitions_.insert(
4✔
360
        parent.transitions_.begin() + index,
4✔
361
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
4✔
362
    this->element_counter_++;
4✔
363
    auto new_entry = parent.at(index);
4✔
364
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
4✔
365

366
    return {new_block, new_entry.second};
4✔
367
};
×
368

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

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

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

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

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

410
    return new_block;
481✔
411
};
×
412

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

426
    return new_block;
28✔
427
};
×
428

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

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

452
    return {new_block, new_entry.second};
12✔
453
};
×
454

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

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

480
    return {new_block, new_entry.second};
×
481
};
×
482

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

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

507
    return {new_block, new_entry.second};
2✔
508
};
×
509

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

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

534
    return {new_block, new_entry.second};
×
535
};
×
536

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

550
    return static_cast<For&>(*parent.children_.back().get());
123✔
551
};
×
552

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

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

578
    return {new_block, new_entry.second};
4✔
579
};
×
580

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

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

606
    return {new_block, new_entry.second};
2✔
607
};
×
608

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

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

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

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

648
    return {new_block, new_entry.second};
1✔
649
};
×
650

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

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

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

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

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

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

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

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

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

740
Map& StructuredSDFGBuilder::add_map(Sequence& parent, const symbolic::Symbol& indvar,
9✔
741
                                    const symbolic::Expression& num_iterations,
742
                                    const sdfg::symbolic::Assignments& assignments,
743
                                    const DebugInfo& debug_info) {
744
    parent.children_.push_back(
18✔
745
        std::unique_ptr<Map>(new Map(this->element_counter_, debug_info, indvar, num_iterations)));
9✔
746
    this->element_counter_ = this->element_counter_ + 2;
9✔
747
    parent.transitions_.push_back(std::unique_ptr<Transition>(
9✔
748
        new Transition(this->element_counter_, debug_info, assignments)));
9✔
749
    this->element_counter_++;
9✔
750

751
    return static_cast<Map&>(*parent.children_.back().get());
9✔
NEW
752
};
×
753

UNCOV
754
For& StructuredSDFGBuilder::convert_while(Sequence& parent, While& loop,
×
755
                                          const symbolic::Symbol& indvar,
756
                                          const symbolic::Condition& condition,
757
                                          const symbolic::Expression& init,
758
                                          const symbolic::Expression& update) {
759
    // Insert for loop
760
    size_t index = 0;
×
761
    for (auto& entry : parent.children_) {
×
762
        if (entry.get() == &loop) {
×
763
            break;
×
764
        }
765
        index++;
×
766
    }
767
    auto iter = parent.children_.begin() + index;
×
768
    auto& new_iter = *parent.children_.insert(
×
769
        iter + 1, std::unique_ptr<For>(new For(this->element_counter_, loop.debug_info(), indvar,
×
770
                                               init, update, condition)));
×
771
    this->element_counter_ = this->element_counter_ + 2;
×
772
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
773
    this->insert_children(for_loop.root(), loop.root(), 0);
×
774

775
    // Remove while loop
776
    parent.children_.erase(parent.children_.begin() + index);
×
777

778
    return for_loop;
×
779
};
×
780

781
Map& StructuredSDFGBuilder::convert_for(Sequence& parent, For& loop,
4✔
782
                                        const symbolic::Expression& num_iterations) {
783
    // Insert for loop
784
    size_t index = 0;
4✔
785
    for (auto& entry : parent.children_) {
4✔
786
        if (entry.get() == &loop) {
4✔
787
            break;
4✔
788
        }
NEW
789
        index++;
×
790
    }
791
    auto iter = parent.children_.begin() + index;
4✔
792
    auto& new_iter = *parent.children_.insert(
8✔
793
        iter + 1, std::unique_ptr<Map>(new Map(this->element_counter_, loop.debug_info(),
8✔
794
                                               loop.indvar(), num_iterations)));
4✔
795
    this->element_counter_ = this->element_counter_ + 2;
4✔
796
    auto& map = dynamic_cast<Map&>(*new_iter);
4✔
797
    this->insert_children(map.root(), loop.root(), 0);
4✔
798

799
    // Remove for loop
800
    parent.children_.erase(parent.children_.begin() + index);
4✔
801

802
    return map;
4✔
NEW
803
};
×
804

805
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
2✔
806
    parent.children_.clear();
2✔
807
    parent.transitions_.clear();
2✔
808
};
2✔
809

810
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
7✔
811
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
7✔
812
    while (!queue.empty()) {
7✔
813
        auto current = queue.front();
7✔
814
        queue.pop_front();
7✔
815

816
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
7✔
817
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
7✔
818
                if (&sequence_stmt->at(i).first == &node) {
7✔
819
                    return *sequence_stmt;
7✔
820
                }
821
                queue.push_back(&sequence_stmt->at(i).first);
×
822
            }
×
823
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
×
824
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
825
                queue.push_back(&if_else_stmt->at(i).first);
×
826
            }
×
827
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
×
828
            queue.push_back(&while_stmt->root());
×
829
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
×
830
            queue.push_back(&for_stmt->root());
×
831
        } else if (auto kern_stmt = dynamic_cast<const structured_control_flow::Kernel*>(current)) {
×
832
            queue.push_back(&kern_stmt->root());
×
833
        }
×
834
    }
835

836
    return this->structured_sdfg_->root();
×
837
};
7✔
838

839
Kernel& StructuredSDFGBuilder::convert_into_kernel() {
6✔
840
    auto old_root = std::move(this->structured_sdfg_->root_);
6✔
841
    this->structured_sdfg_->root_ =
6✔
842
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, old_root->debug_info()));
6✔
843
    this->element_counter_++;
6✔
844
    auto& new_root = this->structured_sdfg_->root();
6✔
845
    auto& kernel = this->add_kernel(new_root, this->function().name());
6✔
846

847
    this->insert_children(kernel.root(), *old_root, 0);
6✔
848

849
    types::Scalar gridDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
850
    add_container(kernel.gridDim_x()->get_name(), gridDim_x);
6✔
851
    types::Scalar gridDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
852
    add_container(kernel.gridDim_y()->get_name(), gridDim_y);
6✔
853
    types::Scalar gridDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
854
    add_container(kernel.gridDim_z()->get_name(), gridDim_z);
6✔
855
    types::Scalar blockDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
856
    add_container(kernel.blockDim_x()->get_name(), blockDim_x);
6✔
857
    types::Scalar blockDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
858
    add_container(kernel.blockDim_y()->get_name(), blockDim_y);
6✔
859
    types::Scalar blockDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
860
    add_container(kernel.blockDim_z()->get_name(), blockDim_z);
6✔
861
    types::Scalar blockIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
862
    add_container(kernel.blockIdx_x()->get_name(), blockIdx_x);
6✔
863
    types::Scalar blockIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
864
    add_container(kernel.blockIdx_y()->get_name(), blockIdx_y);
6✔
865
    types::Scalar blockIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
866
    add_container(kernel.blockIdx_z()->get_name(), blockIdx_z);
6✔
867
    types::Scalar threadIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
868
    add_container(kernel.threadIdx_x()->get_name(), threadIdx_x);
6✔
869
    types::Scalar threadIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
870
    add_container(kernel.threadIdx_y()->get_name(), threadIdx_y);
6✔
871
    types::Scalar threadIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
872
    add_container(kernel.threadIdx_z()->get_name(), threadIdx_z);
6✔
873

874
    kernel.root().replace(symbolic::symbol("gridDim.x"), kernel.gridDim_x());
6✔
875
    kernel.root().replace(symbolic::symbol("gridDim.y"), kernel.gridDim_y());
6✔
876
    kernel.root().replace(symbolic::symbol("gridDim.z"), kernel.gridDim_z());
6✔
877

878
    kernel.root().replace(symbolic::symbol("blockDim.x"), kernel.blockDim_x());
6✔
879
    kernel.root().replace(symbolic::symbol("blockDim.y"), kernel.blockDim_y());
6✔
880
    kernel.root().replace(symbolic::symbol("blockDim.z"), kernel.blockDim_z());
6✔
881

882
    kernel.root().replace(symbolic::symbol("blockIdx.x"), kernel.blockIdx_x());
6✔
883
    kernel.root().replace(symbolic::symbol("blockIdx.y"), kernel.blockIdx_y());
6✔
884
    kernel.root().replace(symbolic::symbol("blockIdx.z"), kernel.blockIdx_z());
6✔
885

886
    kernel.root().replace(symbolic::symbol("threadIdx.x"), kernel.threadIdx_x());
6✔
887
    kernel.root().replace(symbolic::symbol("threadIdx.y"), kernel.threadIdx_y());
6✔
888
    kernel.root().replace(symbolic::symbol("threadIdx.z"), kernel.threadIdx_z());
6✔
889

890
    return kernel;
6✔
891
};
6✔
892

893
/***** Section: Dataflow Graph *****/
894

895
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
585✔
896
                                                         const std::string& data,
897
                                                         const DebugInfo& debug_info) {
898
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
585✔
899
    auto res = block.dataflow_->nodes_.insert(
1,170✔
900
        {vertex, std::unique_ptr<data_flow::AccessNode>(new data_flow::AccessNode(
585✔
901
                     this->element_counter_, debug_info, vertex, block.dataflow(), data))});
585✔
902
    this->element_counter_++;
585✔
903
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
585✔
904
};
×
905

906
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
368✔
907
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
908
    const std::pair<std::string, sdfg::types::Scalar>& output,
909
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
910
    const DebugInfo& debug_info) {
911
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
368✔
912
    auto res = block.dataflow_->nodes_.insert(
736✔
913
        {vertex, std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
736✔
914
                     this->element_counter_, debug_info, vertex, block.dataflow(), code, output,
368✔
915
                     inputs, symbolic::__true__()))});
368✔
916
    this->element_counter_++;
368✔
917
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
368✔
918
};
×
919

920
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
595✔
921
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
922
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
923
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
924
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
595✔
925
    auto res = block.dataflow_->edges_.insert(
1,190✔
926
        {edge.first, std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
595✔
927
                         this->element_counter_, debug_info, edge.first, block.dataflow(), src,
595✔
928
                         src_conn, dst, dst_conn, subset))});
595✔
929
    this->element_counter_++;
595✔
930
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
595✔
931
};
×
932

933
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
10✔
934
    structured_control_flow::Block& block, const data_flow::LibraryNodeType& call,
935
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& outputs,
936
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
937
    const bool has_side_effect, const DebugInfo& debug_info) {
938
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
10✔
939
    auto res = block.dataflow_->nodes_.insert(
20✔
940
        {vertex, std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
10✔
941
                     this->element_counter_, debug_info, vertex, block.dataflow(), outputs, inputs,
10✔
942
                     call, has_side_effect))});
10✔
943
    this->element_counter_++;
10✔
944
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
10✔
945
}
×
946

947
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
948
                                          const data_flow::Memlet& edge) {
949
    auto& graph = block.dataflow();
×
950
    auto e = edge.edge();
×
951
    boost::remove_edge(e, graph.graph_);
×
952
    graph.edges_.erase(e);
×
953
};
×
954

955
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
956
                                        const data_flow::DataFlowNode& node) {
957
    auto& graph = block.dataflow();
×
958
    auto v = node.vertex();
×
959
    boost::remove_vertex(v, graph.graph_);
×
960
    graph.nodes_.erase(v);
×
961
};
×
962

963
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
964
                                       const data_flow::Tasklet& node) {
965
    auto& graph = block.dataflow();
8✔
966

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

969
    // Delete incoming
970
    std::list<const data_flow::Memlet*> iedges;
8✔
971
    for (auto& iedge : graph.in_edges(node)) {
15✔
972
        iedges.push_back(&iedge);
7✔
973
    }
974
    for (auto iedge : iedges) {
15✔
975
        auto& src = iedge->src();
7✔
976
        to_delete.insert(&src);
7✔
977

978
        auto edge = iedge->edge();
7✔
979
        graph.edges_.erase(edge);
7✔
980
        boost::remove_edge(edge, graph.graph_);
7✔
981
    }
982

983
    // Delete outgoing
984
    std::list<const data_flow::Memlet*> oedges;
8✔
985
    for (auto& oedge : graph.out_edges(node)) {
16✔
986
        oedges.push_back(&oedge);
8✔
987
    }
988
    for (auto oedge : oedges) {
16✔
989
        auto& dst = oedge->dst();
8✔
990
        to_delete.insert(&dst);
8✔
991

992
        auto edge = oedge->edge();
8✔
993
        graph.edges_.erase(edge);
8✔
994
        boost::remove_edge(edge, graph.graph_);
8✔
995
    }
996

997
    // Delete nodes
998
    for (auto obsolete_node : to_delete) {
31✔
999
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
1000
            auto vertex = obsolete_node->vertex();
23✔
1001
            graph.nodes_.erase(vertex);
23✔
1002
            boost::remove_vertex(vertex, graph.graph_);
23✔
1003
        }
23✔
1004
    }
1005
};
8✔
1006

1007
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
1008
                                       const data_flow::AccessNode& node) {
1009
    auto& graph = block.dataflow();
1✔
1010
    if (graph.out_degree(node) != 0) {
1✔
1011
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
1012
    }
1013

1014
    std::list<const data_flow::Memlet*> tmp;
1✔
1015
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
1016
    while (!queue.empty()) {
3✔
1017
        auto current = queue.front();
2✔
1018
        queue.pop_front();
2✔
1019
        if (current != &node) {
2✔
1020
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
1021
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
1022
                    continue;
×
1023
                }
1024
            }
×
1025
        }
1✔
1026

1027
        tmp.clear();
2✔
1028
        for (auto& iedge : graph.in_edges(*current)) {
3✔
1029
            tmp.push_back(&iedge);
1✔
1030
        }
1031
        for (auto iedge : tmp) {
3✔
1032
            auto& src = iedge->src();
1✔
1033
            queue.push_back(&src);
1✔
1034

1035
            auto edge = iedge->edge();
1✔
1036
            graph.edges_.erase(edge);
1✔
1037
            boost::remove_edge(edge, graph.graph_);
1✔
1038
        }
1039

1040
        auto vertex = current->vertex();
2✔
1041
        graph.nodes_.erase(vertex);
2✔
1042
        boost::remove_vertex(vertex, graph.graph_);
2✔
1043
    }
1044
};
1✔
1045

1046
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
×
1047
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
1048
    auto& sdfg = this->subject();
×
1049

1050
    codegen::CPPLanguageExtension language_extension;
×
1051

1052
    // Base cases
1053
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
×
1054
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
×
1055

1056
        // Determine type
1057
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
×
1058
        if (symbolic::is_nvptx(sym)) {
×
1059
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
1060
        } else {
×
1061
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
×
1062
        }
1063

1064
        // Add new container for intermediate result
1065
        auto tmp = this->find_new_name();
×
1066
        this->add_container(tmp, sym_type);
×
1067

1068
        // Create dataflow graph
1069
        auto& input_node = this->add_access(parent, sym->get_name());
×
1070
        auto& output_node = this->add_access(parent, tmp);
×
1071
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
×
1072
                                          {"_out", sym_type}, {{"_in", sym_type}});
×
1073
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {symbolic::integer(0)});
×
1074
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1075

1076
        return output_node;
×
1077
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
×
1078
        auto tmp = this->find_new_name();
×
1079
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
1080

1081
        auto& output_node = this->add_access(parent, tmp);
×
1082
        auto& tasklet = this->add_tasklet(
×
1083
            parent, data_flow::TaskletCode::assign,
×
1084
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
1085
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
1086
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1087
        return output_node;
×
1088
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
1089
        auto tmp = this->find_new_name();
×
1090
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1091

1092
        auto& output_node = this->add_access(parent, tmp);
×
1093
        auto& tasklet = this->add_tasklet(
×
1094
            parent, data_flow::TaskletCode::assign,
×
1095
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1096
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1097
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1098
        return output_node;
×
1099
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1100
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
1101
        if (or_expr->get_container().size() != 2) {
×
1102
            throw InvalidSDFGException(
×
1103
                "StructuredSDFGBuilder: Or expression must have exactly two arguments");
×
1104
        }
1105

1106
        std::vector<data_flow::AccessNode*> input_nodes;
×
1107
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1108
        for (auto& arg : or_expr->get_container()) {
×
1109
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1110
            input_nodes.push_back(&input_node);
×
1111
            input_types.push_back(
×
1112
                {"_in" + std::to_string(input_types.size() + 1),
×
1113
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1114
        }
1115

1116
        // Add new container for intermediate result
1117
        auto tmp = this->find_new_name();
×
1118
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1119

1120
        auto& output_node = this->add_access(parent, tmp);
×
1121
        auto& tasklet =
×
1122
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1123
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1124
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1125
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1126
                             {symbolic::integer(0)});
×
1127
        }
×
1128
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1129
        return output_node;
×
1130
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1131
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
1132
        if (and_expr->get_container().size() != 2) {
×
1133
            throw InvalidSDFGException(
×
1134
                "StructuredSDFGBuilder: And expression must have exactly two arguments");
×
1135
        }
1136

1137
        std::vector<data_flow::AccessNode*> input_nodes;
×
1138
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1139
        for (auto& arg : and_expr->get_container()) {
×
1140
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1141
            input_nodes.push_back(&input_node);
×
1142
            input_types.push_back(
×
1143
                {"_in" + std::to_string(input_types.size() + 1),
×
1144
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1145
        }
1146

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

1151
        auto& output_node = this->add_access(parent, tmp);
×
1152
        auto& tasklet =
×
1153
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1154
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1155
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1156
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1157
                             {symbolic::integer(0)});
×
1158
        }
×
1159
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1160
        return output_node;
×
1161
    } else {
×
1162
        throw std::runtime_error("Unsupported expression type");
×
1163
    }
1164
};
×
1165

1166
}  // namespace builder
1167
}  // 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