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

daisytuner / sdfglib / 15238257521

25 May 2025 01:14PM UTC coverage: 60.342% (-0.1%) from 60.473%
15238257521

push

github

web-flow
Merge pull request #31 from daisytuner/exception-handling

Exception handling

18 of 60 new or added lines in 17 files covered. (30.0%)

1 existing line in 1 file now uncovered.

8052 of 13344 relevant lines covered (60.34%)

102.27 hits per line

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

70.41
/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

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

9
namespace sdfg {
10
namespace builder {
11

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

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

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

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

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

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

53
    return false;
×
54
}
6✔
55

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

67
    return pdom;
3✔
68
}
3✔
69

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

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

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

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

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

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

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

115
        // 2. Determine exit states and exit edges
116
        std::unordered_set<const control_flow::State*> exit_states;
1✔
117
        std::unordered_set<const control_flow::InterstateEdge*> exit_edges;
1✔
118
        for (auto node : body) {
4✔
119
            for (auto& edge : sdfg.out_edges(*node)) {
7✔
120
                if (body.find(&edge.dst()) == body.end()) {
4✔
121
                    exit_edges.insert(&edge);
1✔
122
                    exit_states.insert(&edge.dst());
1✔
123
                }
1✔
124
            }
125
        }
126
        if (exit_states.size() != 1) {
1✔
NEW
127
            throw InvalidSDFGException(
×
NEW
128
                "Degenerated structured control flow: Loop body must have exactly one exit state");
×
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✔
NEW
196
                throw InvalidSDFGException(
×
NEW
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 {
1,706✔
282
    return static_cast<Function&>(*this->structured_sdfg_);
1,706✔
283
};
284

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

288
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name)
460✔
289
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name)) {};
460✔
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_; };
2,532✔
321

322
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
427✔
323
    return std::move(this->structured_sdfg_);
427✔
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✔
NEW
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) {
10✔
386
    parent.children_.insert(parent.children_.begin() + i,
20✔
387
                            std::make_move_iterator(other.children_.begin()),
10✔
388
                            std::make_move_iterator(other.children_.end()));
10✔
389
    parent.transitions_.insert(parent.transitions_.begin() + i,
20✔
390
                               std::make_move_iterator(other.transitions_.begin()),
10✔
391
                               std::make_move_iterator(other.transitions_.end()));
10✔
392
    other.children_.clear();
10✔
393
    other.transitions_.clear();
10✔
394
};
10✔
395

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

408
    return new_block;
472✔
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,
115✔
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>(
230✔
542
        new For(this->element_counter_, debug_info, indvar, init, update, condition)));
115✔
543
    this->element_counter_ = this->element_counter_ + 2;
115✔
544
    parent.transitions_.push_back(std::unique_ptr<Transition>(
115✔
545
        new Transition(this->element_counter_, debug_info, assignments)));
115✔
546
    this->element_counter_++;
115✔
547

548
    return static_cast<For&>(*parent.children_.back().get());
115✔
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) {
35✔
608
    return this->add_if_else(parent, symbolic::Assignments{}, debug_info);
35✔
609
};
×
610

611
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent,
36✔
612
                                           const sdfg::symbolic::Assignments& assignments,
613
                                           const DebugInfo& debug_info) {
614
    parent.children_.push_back(
36✔
615
        std::unique_ptr<IfElse>(new IfElse(this->element_counter_, debug_info)));
36✔
616
    this->element_counter_++;
36✔
617
    parent.transitions_.push_back(std::unique_ptr<Transition>(
36✔
618
        new Transition(this->element_counter_, debug_info, assignments)));
36✔
619
    this->element_counter_++;
36✔
620
    return static_cast<IfElse&>(*parent.children_.back().get());
36✔
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,
63✔
650
                                          const DebugInfo& debug_info) {
651
    scope.cases_.push_back(
63✔
652
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
63✔
653
    this->element_counter_++;
63✔
654
    scope.conditions_.push_back(cond);
63✔
655
    return *scope.cases_.back();
63✔
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,
38✔
664
                                        const sdfg::symbolic::Assignments& assignments,
665
                                        const DebugInfo& debug_info) {
666
    parent.children_.push_back(
38✔
667
        std::unique_ptr<While>(new While(this->element_counter_, debug_info)));
38✔
668
    this->element_counter_ = this->element_counter_ + 2;
38✔
669
    parent.transitions_.push_back(std::unique_ptr<Transition>(
38✔
670
        new Transition(this->element_counter_, debug_info, assignments)));
38✔
671
    this->element_counter_++;
38✔
672
    return static_cast<While&>(*parent.children_.back().get());
38✔
673
};
×
674

675
Kernel& StructuredSDFGBuilder::add_kernel(
18✔
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(
36✔
684
        this->element_counter_, debug_info, suffix, gridDim_x_init, gridDim_y_init, gridDim_z_init,
18✔
685
        blockDim_x_init, blockDim_y_init, blockDim_z_init, blockIdx_x_init, blockIdx_y_init,
18✔
686
        blockIdx_z_init, threadIdx_x_init, threadIdx_y_init, threadIdx_z_init)));
18✔
687
    this->element_counter_ = this->element_counter_ + 2;
18✔
688
    parent.transitions_.push_back(
18✔
689
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
18✔
690
    this->element_counter_++;
18✔
691
    return static_cast<Kernel&>(*parent.children_.back().get());
18✔
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) {
7✔
771
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
7✔
772
    while (!queue.empty()) {
7✔
773
        auto current = queue.front();
7✔
774
        queue.pop_front();
7✔
775

776
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
7✔
777
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
7✔
778
                if (&sequence_stmt->at(i).first == &node) {
7✔
779
                    return *sequence_stmt;
7✔
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
};
7✔
798

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

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

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

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

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

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

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

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

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

855
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
571✔
856
                                                         const std::string& data,
857
                                                         const DebugInfo& debug_info) {
858
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
571✔
859
    auto res = block.dataflow_->nodes_.insert(
1,142✔
860
        {vertex, std::unique_ptr<data_flow::AccessNode>(new data_flow::AccessNode(
571✔
861
                     this->element_counter_, debug_info, vertex, block.dataflow(), data))});
571✔
862
    this->element_counter_++;
571✔
863
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
571✔
864
};
×
865

866
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
361✔
867
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
868
    const std::pair<std::string, sdfg::types::Scalar>& output,
869
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
870
    const DebugInfo& debug_info) {
871
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
361✔
872
    auto res = block.dataflow_->nodes_.insert(
722✔
873
        {vertex, std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
722✔
874
                     this->element_counter_, debug_info, vertex, block.dataflow(), code, output,
361✔
875
                     inputs, symbolic::__true__()))});
361✔
876
    this->element_counter_++;
361✔
877
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
361✔
878
};
×
879

880
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
581✔
881
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
882
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
883
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
884
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
581✔
885
    auto res = block.dataflow_->edges_.insert(
1,162✔
886
        {edge.first, std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
581✔
887
                         this->element_counter_, debug_info, edge.first, block.dataflow(), src,
581✔
888
                         src_conn, dst, dst_conn, subset))});
581✔
889
    this->element_counter_++;
581✔
890
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
581✔
891
};
×
892

893
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
10✔
894
    structured_control_flow::Block& block, const data_flow::LibraryNodeType& call,
895
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& outputs,
896
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
897
    const bool has_side_effect, const DebugInfo& debug_info) {
898
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
10✔
899
    auto res = block.dataflow_->nodes_.insert(
20✔
900
        {vertex, std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
10✔
901
                     this->element_counter_, debug_info, vertex, block.dataflow(), outputs, inputs,
10✔
902
                     call, has_side_effect))});
10✔
903
    this->element_counter_++;
10✔
904
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
10✔
905
}
×
906

907
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
908
                                          const data_flow::Memlet& edge) {
909
    auto& graph = block.dataflow();
×
910
    auto e = edge.edge();
×
911
    boost::remove_edge(e, graph.graph_);
×
912
    graph.edges_.erase(e);
×
913
};
×
914

915
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
916
                                        const data_flow::DataFlowNode& node) {
917
    auto& graph = block.dataflow();
×
918
    auto v = node.vertex();
×
919
    boost::remove_vertex(v, graph.graph_);
×
920
    graph.nodes_.erase(v);
×
921
};
×
922

923
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
924
                                       const data_flow::Tasklet& node) {
925
    auto& graph = block.dataflow();
8✔
926

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

929
    // Delete incoming
930
    std::list<const data_flow::Memlet*> iedges;
8✔
931
    for (auto& iedge : graph.in_edges(node)) {
15✔
932
        iedges.push_back(&iedge);
7✔
933
    }
934
    for (auto iedge : iedges) {
15✔
935
        auto& src = iedge->src();
7✔
936
        to_delete.insert(&src);
7✔
937

938
        auto edge = iedge->edge();
7✔
939
        graph.edges_.erase(edge);
7✔
940
        boost::remove_edge(edge, graph.graph_);
7✔
941
    }
942

943
    // Delete outgoing
944
    std::list<const data_flow::Memlet*> oedges;
8✔
945
    for (auto& oedge : graph.out_edges(node)) {
16✔
946
        oedges.push_back(&oedge);
8✔
947
    }
948
    for (auto oedge : oedges) {
16✔
949
        auto& dst = oedge->dst();
8✔
950
        to_delete.insert(&dst);
8✔
951

952
        auto edge = oedge->edge();
8✔
953
        graph.edges_.erase(edge);
8✔
954
        boost::remove_edge(edge, graph.graph_);
8✔
955
    }
956

957
    // Delete nodes
958
    for (auto obsolete_node : to_delete) {
31✔
959
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
960
            auto vertex = obsolete_node->vertex();
23✔
961
            graph.nodes_.erase(vertex);
23✔
962
            boost::remove_vertex(vertex, graph.graph_);
23✔
963
        }
23✔
964
    }
965
};
8✔
966

967
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
968
                                       const data_flow::AccessNode& node) {
969
    auto& graph = block.dataflow();
1✔
970
    if (graph.out_degree(node) != 0) {
1✔
NEW
971
        throw InvalidSDFGException("StructuredSDFGBuilder: Access node has outgoing edges");
×
972
    }
973

974
    std::list<const data_flow::Memlet*> tmp;
1✔
975
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
976
    while (!queue.empty()) {
3✔
977
        auto current = queue.front();
2✔
978
        queue.pop_front();
2✔
979
        if (current != &node) {
2✔
980
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
981
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
982
                    continue;
×
983
                }
984
            }
×
985
        }
1✔
986

987
        tmp.clear();
2✔
988
        for (auto& iedge : graph.in_edges(*current)) {
3✔
989
            tmp.push_back(&iedge);
1✔
990
        }
991
        for (auto iedge : tmp) {
3✔
992
            auto& src = iedge->src();
1✔
993
            queue.push_back(&src);
1✔
994

995
            auto edge = iedge->edge();
1✔
996
            graph.edges_.erase(edge);
1✔
997
            boost::remove_edge(edge, graph.graph_);
1✔
998
        }
999

1000
        auto vertex = current->vertex();
2✔
1001
        graph.nodes_.erase(vertex);
2✔
1002
        boost::remove_vertex(vertex, graph.graph_);
2✔
1003
    }
1004
};
1✔
1005

1006
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
×
1007
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
1008
    auto& sdfg = this->subject();
×
1009

1010
    codegen::CPPLanguageExtension language_extension;
×
1011

1012
    // Base cases
1013
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
×
1014
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
×
1015

1016
        // Determine type
1017
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
×
1018
        if (symbolic::is_nvptx(sym)) {
×
1019
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
1020
        } else {
×
1021
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
×
1022
        }
1023

1024
        // Add new container for intermediate result
1025
        auto tmp = this->find_new_name();
×
1026
        this->add_container(tmp, sym_type);
×
1027

1028
        // Create dataflow graph
1029
        auto& input_node = this->add_access(parent, sym->get_name());
×
1030
        auto& output_node = this->add_access(parent, tmp);
×
1031
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
×
1032
                                          {"_out", sym_type}, {{"_in", sym_type}});
×
1033
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {symbolic::integer(0)});
×
1034
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1035

1036
        return output_node;
×
1037
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
×
1038
        auto tmp = this->find_new_name();
×
1039
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
1040

1041
        auto& output_node = this->add_access(parent, tmp);
×
1042
        auto& tasklet = this->add_tasklet(
×
1043
            parent, data_flow::TaskletCode::assign,
×
1044
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
1045
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
1046
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1047
        return output_node;
×
1048
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
1049
        auto tmp = this->find_new_name();
×
1050
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1051

1052
        auto& output_node = this->add_access(parent, tmp);
×
1053
        auto& tasklet = this->add_tasklet(
×
1054
            parent, data_flow::TaskletCode::assign,
×
1055
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1056
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1057
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1058
        return output_node;
×
1059
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1060
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
NEW
1061
        if (or_expr->get_container().size() != 2) {
×
NEW
1062
            throw InvalidSDFGException(
×
NEW
1063
                "StructuredSDFGBuilder: Or expression must have exactly two arguments");
×
1064
        }
1065

1066
        std::vector<data_flow::AccessNode*> input_nodes;
×
1067
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1068
        for (auto& arg : or_expr->get_container()) {
×
1069
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1070
            input_nodes.push_back(&input_node);
×
1071
            input_types.push_back(
×
1072
                {"_in" + std::to_string(input_types.size() + 1),
×
1073
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1074
        }
1075

1076
        // Add new container for intermediate result
1077
        auto tmp = this->find_new_name();
×
1078
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1079

1080
        auto& output_node = this->add_access(parent, tmp);
×
1081
        auto& tasklet =
×
1082
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1083
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1084
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1085
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1086
                             {symbolic::integer(0)});
×
1087
        }
×
1088
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1089
        return output_node;
×
1090
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1091
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
NEW
1092
        if (and_expr->get_container().size() != 2) {
×
NEW
1093
            throw InvalidSDFGException(
×
NEW
1094
                "StructuredSDFGBuilder: And expression must have exactly two arguments");
×
1095
        }
1096

1097
        std::vector<data_flow::AccessNode*> input_nodes;
×
1098
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1099
        for (auto& arg : and_expr->get_container()) {
×
1100
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1101
            input_nodes.push_back(&input_node);
×
1102
            input_types.push_back(
×
1103
                {"_in" + std::to_string(input_types.size() + 1),
×
1104
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1105
        }
1106

1107
        // Add new container for intermediate result
1108
        auto tmp = this->find_new_name();
×
1109
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1110

1111
        auto& output_node = this->add_access(parent, tmp);
×
1112
        auto& tasklet =
×
1113
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1114
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1115
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1116
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1117
                             {symbolic::integer(0)});
×
1118
        }
×
1119
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1120
        return output_node;
×
1121
    } else {
×
1122
        throw std::runtime_error("Unsupported expression type");
×
1123
    }
1124
};
×
1125

1126
}  // namespace builder
1127
}  // 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