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

daisytuner / sdfglib / 15075964231

16 May 2025 07:28PM UTC coverage: 63.623% (+0.1%) from 63.496%
15075964231

push

github

web-flow
Merge pull request #16 from daisytuner/segfaults

Enable Wall, Werror and Wpedantic

98 of 120 new or added lines in 39 files covered. (81.67%)

4 existing lines in 4 files now uncovered.

8633 of 13569 relevant lines covered (63.62%)

483.97 hits per line

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

75.0
/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
    this->traverse_with_loop_detection(sdfg, root, start_state, nullptr, continues, breaks,
4✔
84
                                       pdom_tree);
85
};
4✔
86

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

96
    auto in_edges = sdfg.in_edges(*current);
15✔
97

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

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

128
        for (auto& edge : breaks) {
1✔
129
            exit_edges.insert(edge);
×
130
        }
131

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

144
        // 3. Add while loop
145
        While& loop = this->add_while(scope, {}, dbg_info);
1✔
146
        this->traverse_without_loop_detection(sdfg, loop.root(), current, exit_state, continues,
1✔
147
                                              exit_edges, pdom_tree);
1✔
148

149
        this->traverse_with_loop_detection(sdfg, scope, exit_state, end, continues, breaks,
2✔
150
                                           pdom_tree);
1✔
151
    } else {
1✔
152
        this->traverse_without_loop_detection(sdfg, scope, current, end, continues, breaks,
28✔
153
                                              pdom_tree);
14✔
154
    }
155
};
19✔
156

157
void StructuredSDFGBuilder::traverse_without_loop_detection(
15✔
158
    const SDFG& sdfg, Sequence& scope, const State* current, const State* end,
159
    const std::unordered_set<const InterstateEdge*>& continues,
160
    const std::unordered_set<const InterstateEdge*>& breaks,
161
    const std::unordered_map<const control_flow::State*, const control_flow::State*>& pdom_tree) {
162
    if (current == end) {
15✔
163
        return;
×
164
    }
165

166
    auto out_edges = sdfg.out_edges(*current);
15✔
167
    auto out_degree = sdfg.out_degree(*current);
15✔
168

169
    // Case 1: Sink node
170
    if (out_degree == 0) {
15✔
171
        this->add_block(scope, current->dataflow(), {}, current->debug_info());
4✔
172
        this->add_return(scope, {}, current->debug_info());
4✔
173
        return;
4✔
174
    }
175

176
    // Case 2: Transition
177
    if (out_degree == 1) {
11✔
178
        auto& oedge = *out_edges.begin();
8✔
179
        assert(oedge.is_unconditional() &&
16✔
180
               "Degenerated structured control flow: Non-deterministic transition");
181
        this->add_block(scope, current->dataflow(), oedge.assignments(), current->debug_info());
8✔
182

183
        if (continues.find(&oedge) != continues.end()) {
8✔
184
            this->add_continue(scope, oedge.debug_info());
×
185
        } else if (breaks.find(&oedge) != breaks.end()) {
8✔
186
            this->add_break(scope, oedge.debug_info());
×
187
        } else {
×
188
            this->traverse_with_loop_detection(sdfg, scope, &oedge.dst(), end, continues, breaks,
16✔
189
                                               pdom_tree);
8✔
190
        }
191
        return;
8✔
192
    }
193

194
    // Case 3: Branches
195
    if (out_degree > 1) {
3✔
196
        this->add_block(scope, current->dataflow(), {}, current->debug_info());
3✔
197

198
        std::vector<const InterstateEdge*> out_edges_vec;
3✔
199
        for (auto& edge : out_edges) {
9✔
200
            out_edges_vec.push_back(&edge);
6✔
201
        }
202

203
        // Best-effort approach: Find end of if-else
204
        // If not found, the branches may repeat paths yielding a large SDFG
205
        const control_flow::State* local_end =
3✔
206
            this->find_end_of_if_else(sdfg, current, out_edges_vec, pdom_tree);
3✔
207
        if (local_end == nullptr) {
3✔
208
            local_end = end;
×
209
        }
×
210

211
        auto& if_else = this->add_if_else(scope, current->debug_info());
3✔
212
        for (size_t i = 0; i < out_degree; i++) {
9✔
213
            auto& out_edge = out_edges_vec[i];
6✔
214

215
            auto& branch = this->add_case(if_else, out_edge->condition(), out_edge->debug_info());
6✔
216
            if (!out_edge->assignments().empty()) {
6✔
217
                this->add_block(branch, out_edge->assignments(), out_edge->debug_info());
2✔
218
            }
2✔
219
            if (continues.find(out_edge) != continues.end()) {
6✔
220
                this->add_continue(branch, out_edge->debug_info());
1✔
221
            } else if (breaks.find(out_edge) != breaks.end()) {
6✔
222
                this->add_break(branch, out_edge->debug_info());
1✔
223
            } else {
1✔
224
                this->traverse_with_loop_detection(sdfg, branch, &out_edge->dst(), local_end,
4✔
225
                                                   continues, breaks, pdom_tree);
4✔
226
            }
227
        }
6✔
228

229
        if (local_end != end) {
3✔
230
            this->traverse_with_loop_detection(sdfg, scope, local_end, end, continues, breaks,
4✔
231
                                               pdom_tree);
2✔
232
        }
2✔
233

234
        return;
235
    }
3✔
236
}
15✔
237

238
Function& StructuredSDFGBuilder::function() const {
2,688✔
239
    return static_cast<Function&>(*this->structured_sdfg_);
2,688✔
240
};
241

242
StructuredSDFGBuilder::StructuredSDFGBuilder(std::unique_ptr<StructuredSDFG>& sdfg)
352✔
243
    : FunctionBuilder(), structured_sdfg_(std::move(sdfg)) {};
352✔
244

245
StructuredSDFGBuilder::StructuredSDFGBuilder(const std::string& name)
499✔
246
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(name)) {};
499✔
247

248
StructuredSDFGBuilder::StructuredSDFGBuilder(const SDFG& sdfg)
4✔
249
    : FunctionBuilder(), structured_sdfg_(new StructuredSDFG(sdfg.name())) {
4✔
250
    for (auto& entry : sdfg.structures_) {
4✔
251
        this->structured_sdfg_->structures_.insert({entry.first, entry.second->clone()});
×
252
    }
253

254
    for (auto& entry : sdfg.containers_) {
11✔
255
        this->structured_sdfg_->containers_.insert({entry.first, entry.second->clone()});
7✔
256
    }
257

258
    for (auto& arg : sdfg.arguments_) {
6✔
259
        this->structured_sdfg_->arguments_.push_back(arg);
2✔
260
    }
261

262
    for (auto& ext : sdfg.externals_) {
5✔
263
        this->structured_sdfg_->externals_.push_back(ext);
1✔
264
    }
265

266
    for (auto& entry : sdfg.assumptions_) {
9✔
267
        this->structured_sdfg_->assumptions_.insert({entry.first, entry.second});
5✔
268
    }
269

270
    this->traverse(sdfg);
4✔
271
};
4✔
272

273
StructuredSDFG& StructuredSDFGBuilder::subject() const { return *this->structured_sdfg_; };
2,888✔
274

275
std::unique_ptr<StructuredSDFG> StructuredSDFGBuilder::move() {
469✔
276
    return std::move(this->structured_sdfg_);
469✔
277
};
278

279
Sequence& StructuredSDFGBuilder::add_sequence(Sequence& parent,
39✔
280
                                              const sdfg::symbolic::Assignments& assignments,
281
                                              const DebugInfo& debug_info) {
282
    parent.children_.push_back(
39✔
283
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
39✔
284
    this->element_counter_++;
39✔
285
    parent.transitions_.push_back(std::unique_ptr<Transition>(
39✔
286
        new Transition(this->element_counter_, debug_info, assignments)));
39✔
287
    this->element_counter_++;
39✔
288

289
    return static_cast<Sequence&>(*parent.children_.back().get());
39✔
290
};
×
291

292
std::pair<Sequence&, Transition&> StructuredSDFGBuilder::add_sequence_before(
7✔
293
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
294
    // Insert block before current block
295
    int index = -1;
7✔
296
    for (size_t i = 0; i < parent.children_.size(); i++) {
7✔
297
        if (parent.children_.at(i).get() == &block) {
7✔
298
            index = i;
7✔
299
            break;
7✔
300
        }
301
    }
×
302
    assert(index > -1);
7✔
303

304
    parent.children_.insert(
7✔
305
        parent.children_.begin() + index,
7✔
306
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
7✔
307
    this->element_counter_++;
7✔
308
    parent.transitions_.insert(
7✔
309
        parent.transitions_.begin() + index,
7✔
310
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
7✔
311
    this->element_counter_++;
7✔
312
    auto new_entry = parent.at(index);
7✔
313
    auto& new_block = dynamic_cast<structured_control_flow::Sequence&>(new_entry.first);
7✔
314

315
    return {new_block, new_entry.second};
7✔
316
};
×
317

318
void StructuredSDFGBuilder::remove_child(Sequence& parent, size_t i) {
9✔
319
    parent.children_.erase(parent.children_.begin() + i);
9✔
320
    parent.transitions_.erase(parent.transitions_.begin() + i);
9✔
321
};
9✔
322

323
void StructuredSDFGBuilder::remove_child(Sequence& parent, ControlFlowNode& child) {
50✔
324
    int index = -1;
50✔
325
    for (size_t i = 0; i < parent.children_.size(); i++) {
51✔
326
        if (parent.children_.at(i).get() == &child) {
51✔
327
            index = i;
50✔
328
            break;
50✔
329
        }
330
    }
1✔
331

332
    parent.children_.erase(parent.children_.begin() + index);
50✔
333
    parent.transitions_.erase(parent.transitions_.begin() + index);
50✔
334
};
50✔
335

336
void StructuredSDFGBuilder::insert_children(Sequence& parent, Sequence& other, size_t i) {
10✔
337
    parent.children_.insert(parent.children_.begin() + i,
20✔
338
                            std::make_move_iterator(other.children_.begin()),
10✔
339
                            std::make_move_iterator(other.children_.end()));
10✔
340
    parent.transitions_.insert(parent.transitions_.begin() + i,
20✔
341
                               std::make_move_iterator(other.transitions_.begin()),
10✔
342
                               std::make_move_iterator(other.transitions_.end()));
10✔
343
    other.children_.clear();
10✔
344
    other.transitions_.clear();
10✔
345
};
10✔
346

347
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
648✔
348
                                        const sdfg::symbolic::Assignments& assignments,
349
                                        const DebugInfo& debug_info) {
350
    parent.children_.push_back(
648✔
351
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info)));
648✔
352
    this->element_counter_++;
648✔
353
    parent.transitions_.push_back(std::unique_ptr<Transition>(
648✔
354
        new Transition(this->element_counter_, debug_info, assignments)));
648✔
355
    this->element_counter_++;
648✔
356
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
648✔
357
    (*new_block.dataflow_).parent_ = &new_block;
648✔
358

359
    return new_block;
648✔
360
};
×
361

362
Block& StructuredSDFGBuilder::add_block(Sequence& parent,
82✔
363
                                        const data_flow::DataFlowGraph& data_flow_graph,
364
                                        const sdfg::symbolic::Assignments& assignments,
365
                                        const DebugInfo& debug_info) {
366
    parent.children_.push_back(
82✔
367
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info, data_flow_graph)));
82✔
368
    this->element_counter_++;
82✔
369
    parent.transitions_.push_back(std::unique_ptr<Transition>(
82✔
370
        new Transition(this->element_counter_, debug_info, assignments)));
82✔
371
    this->element_counter_++;
82✔
372
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(*parent.children_.back().get());
82✔
373
    (*new_block.dataflow_).parent_ = &new_block;
82✔
374

375
    return new_block;
82✔
376
};
×
377

378
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
12✔
379
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
380
    // Insert block before current block
381
    int index = -1;
12✔
382
    for (size_t i = 0; i < parent.children_.size(); i++) {
13✔
383
        if (parent.children_.at(i).get() == &block) {
13✔
384
            index = i;
12✔
385
            break;
12✔
386
        }
387
    }
1✔
388
    assert(index > -1);
12✔
389

390
    parent.children_.insert(parent.children_.begin() + index,
12✔
391
                            std::unique_ptr<Block>(new Block(this->element_counter_, debug_info)));
12✔
392
    this->element_counter_++;
12✔
393
    parent.transitions_.insert(
12✔
394
        parent.transitions_.begin() + index,
12✔
395
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
12✔
396
    this->element_counter_++;
12✔
397
    auto new_entry = parent.at(index);
12✔
398
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
12✔
399
    (*new_block.dataflow_).parent_ = &new_block;
12✔
400

401
    return {new_block, new_entry.second};
12✔
402
};
×
403

404
std::pair<Block&, Transition&> StructuredSDFGBuilder::add_block_before(
×
405
    Sequence& parent, ControlFlowNode& block, data_flow::DataFlowGraph& data_flow_graph,
406
    const DebugInfo& debug_info) {
407
    // Insert block before current block
408
    int index = -1;
×
409
    for (size_t i = 0; i < parent.children_.size(); i++) {
×
410
        if (parent.children_.at(i).get() == &block) {
×
411
            index = i;
×
412
            break;
×
413
        }
414
    }
×
415
    assert(index > -1);
×
416

417
    parent.children_.insert(
×
418
        parent.children_.begin() + index,
×
419
        std::unique_ptr<Block>(new Block(this->element_counter_, debug_info, data_flow_graph)));
×
420
    this->element_counter_++;
×
421
    parent.transitions_.insert(
×
422
        parent.transitions_.begin() + index,
×
423
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
×
424
    this->element_counter_++;
×
425
    auto new_entry = parent.at(index);
×
426
    auto& new_block = dynamic_cast<structured_control_flow::Block&>(new_entry.first);
×
NEW
427
    (*new_block.dataflow_).parent_ = &new_block;
×
428

429
    return {new_block, new_entry.second};
×
430
};
×
431

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

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

456
    return {new_block, new_entry.second};
2✔
457
};
×
458

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

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

483
    return {new_block, new_entry.second};
×
484
};
×
485

486
For& StructuredSDFGBuilder::add_for(Sequence& parent, const symbolic::Symbol& indvar,
278✔
487
                                    const symbolic::Condition& condition,
488
                                    const symbolic::Expression& init,
489
                                    const symbolic::Expression& update,
490
                                    const sdfg::symbolic::Assignments& assignments,
491
                                    const DebugInfo& debug_info) {
492
    parent.children_.push_back(std::unique_ptr<For>(
556✔
493
        new For(this->element_counter_, debug_info, indvar, init, update, condition)));
278✔
494
    this->element_counter_ = this->element_counter_ + 2;
278✔
495
    parent.transitions_.push_back(std::unique_ptr<Transition>(
278✔
496
        new Transition(this->element_counter_, debug_info, assignments)));
278✔
497
    this->element_counter_++;
278✔
498

499
    return static_cast<For&>(*parent.children_.back().get());
278✔
500
};
×
501

502
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_before(
51✔
503
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
504
    const symbolic::Condition& condition, const symbolic::Expression& init,
505
    const symbolic::Expression& update, const DebugInfo& debug_info) {
506
    // Insert block before current block
507
    int index = -1;
51✔
508
    for (size_t i = 0; i < parent.children_.size(); i++) {
73✔
509
        if (parent.children_.at(i).get() == &block) {
73✔
510
            index = i;
51✔
511
            break;
51✔
512
        }
513
    }
22✔
514
    assert(index > -1);
51✔
515

516
    parent.children_.insert(parent.children_.begin() + index,
102✔
517
                            std::unique_ptr<For>(new For(this->element_counter_, debug_info, indvar,
102✔
518
                                                         init, update, condition)));
51✔
519
    this->element_counter_ = this->element_counter_ + 2;
51✔
520
    parent.transitions_.insert(
51✔
521
        parent.transitions_.begin() + index,
51✔
522
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
51✔
523
    this->element_counter_++;
51✔
524
    auto new_entry = parent.at(index);
51✔
525
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
51✔
526

527
    return {new_block, new_entry.second};
51✔
528
};
×
529

530
std::pair<For&, Transition&> StructuredSDFGBuilder::add_for_after(
2✔
531
    Sequence& parent, ControlFlowNode& block, const symbolic::Symbol& indvar,
532
    const symbolic::Condition& condition, const symbolic::Expression& init,
533
    const symbolic::Expression& update, const DebugInfo& debug_info) {
534
    // Insert block before current block
535
    int index = -1;
2✔
536
    for (size_t i = 0; i < parent.children_.size(); i++) {
3✔
537
        if (parent.children_.at(i).get() == &block) {
3✔
538
            index = i;
2✔
539
            break;
2✔
540
        }
541
    }
1✔
542
    assert(index > -1);
2✔
543

544
    parent.children_.insert(parent.children_.begin() + index + 1,
4✔
545
                            std::unique_ptr<For>(new For(this->element_counter_, debug_info, indvar,
4✔
546
                                                         init, update, condition)));
2✔
547
    this->element_counter_ = this->element_counter_ + 2;
2✔
548
    parent.transitions_.insert(
2✔
549
        parent.transitions_.begin() + index + 1,
2✔
550
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
2✔
551
    this->element_counter_++;
2✔
552
    auto new_entry = parent.at(index + 1);
2✔
553
    auto& new_block = dynamic_cast<structured_control_flow::For&>(new_entry.first);
2✔
554

555
    return {new_block, new_entry.second};
2✔
556
};
×
557

558
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent, const DebugInfo& debug_info) {
41✔
559
    return this->add_if_else(parent, symbolic::Assignments{}, debug_info);
41✔
560
};
×
561

562
IfElse& StructuredSDFGBuilder::add_if_else(Sequence& parent,
43✔
563
                                           const sdfg::symbolic::Assignments& assignments,
564
                                           const DebugInfo& debug_info) {
565
    parent.children_.push_back(
43✔
566
        std::unique_ptr<IfElse>(new IfElse(this->element_counter_, debug_info)));
43✔
567
    this->element_counter_++;
43✔
568
    parent.transitions_.push_back(std::unique_ptr<Transition>(
43✔
569
        new Transition(this->element_counter_, debug_info, assignments)));
43✔
570
    this->element_counter_++;
43✔
571
    return static_cast<IfElse&>(*parent.children_.back().get());
43✔
572
};
×
573

574
std::pair<IfElse&, Transition&> StructuredSDFGBuilder::add_if_else_before(
2✔
575
    Sequence& parent, ControlFlowNode& block, const DebugInfo& debug_info) {
576
    // Insert block before current block
577
    int index = -1;
2✔
578
    for (size_t i = 0; i < parent.children_.size(); i++) {
2✔
579
        if (parent.children_.at(i).get() == &block) {
2✔
580
            index = i;
2✔
581
            break;
2✔
582
        }
583
    }
×
584
    assert(index > -1);
2✔
585

586
    parent.children_.insert(
2✔
587
        parent.children_.begin() + index,
2✔
588
        std::unique_ptr<IfElse>(new IfElse(this->element_counter_, debug_info)));
2✔
589
    this->element_counter_++;
2✔
590
    parent.transitions_.insert(
2✔
591
        parent.transitions_.begin() + index,
2✔
592
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
2✔
593
    this->element_counter_++;
2✔
594
    auto new_entry = parent.at(index);
2✔
595
    auto& new_block = dynamic_cast<structured_control_flow::IfElse&>(new_entry.first);
2✔
596

597
    return {new_block, new_entry.second};
2✔
598
};
×
599

600
Sequence& StructuredSDFGBuilder::add_case(IfElse& scope, const sdfg::symbolic::Condition cond,
73✔
601
                                          const DebugInfo& debug_info) {
602
    scope.cases_.push_back(
73✔
603
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, debug_info)));
73✔
604
    this->element_counter_++;
73✔
605
    scope.conditions_.push_back(cond);
73✔
606
    return *scope.cases_.back();
73✔
607
};
×
608

609
void StructuredSDFGBuilder::remove_case(IfElse& scope, size_t i, const DebugInfo& debug_info) {
3✔
610
    scope.cases_.erase(scope.cases_.begin() + i);
3✔
611
    scope.conditions_.erase(scope.conditions_.begin() + i);
3✔
612
};
3✔
613

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

626
Kernel& StructuredSDFGBuilder::add_kernel(
18✔
627
    Sequence& parent, const std::string& suffix, const DebugInfo& debug_info,
628
    const symbolic::Expression& gridDim_x_init, const symbolic::Expression& gridDim_y_init,
629
    const symbolic::Expression& gridDim_z_init, const symbolic::Expression& blockDim_x_init,
630
    const symbolic::Expression& blockDim_y_init, const symbolic::Expression& blockDim_z_init,
631
    const symbolic::Expression& blockIdx_x_init, const symbolic::Expression& blockIdx_y_init,
632
    const symbolic::Expression& blockIdx_z_init, const symbolic::Expression& threadIdx_x_init,
633
    const symbolic::Expression& threadIdx_y_init, const symbolic::Expression& threadIdx_z_init) {
634
    parent.children_.push_back(std::unique_ptr<Kernel>(new Kernel(
36✔
635
        this->element_counter_, debug_info, suffix, gridDim_x_init, gridDim_y_init, gridDim_z_init,
18✔
636
        blockDim_x_init, blockDim_y_init, blockDim_z_init, blockIdx_x_init, blockIdx_y_init,
18✔
637
        blockIdx_z_init, threadIdx_x_init, threadIdx_y_init, threadIdx_z_init)));
18✔
638
    this->element_counter_ = this->element_counter_ + 2;
18✔
639
    parent.transitions_.push_back(
18✔
640
        std::unique_ptr<Transition>(new Transition(this->element_counter_, debug_info)));
18✔
641
    this->element_counter_++;
18✔
642
    return static_cast<Kernel&>(*parent.children_.back().get());
18✔
643
};
×
644

645
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent, const DebugInfo& debug_info) {
14✔
646
    return this->add_continue(parent, symbolic::Assignments{}, debug_info);
14✔
647
};
×
648

649
Continue& StructuredSDFGBuilder::add_continue(Sequence& parent,
15✔
650
                                              const sdfg::symbolic::Assignments& assignments,
651
                                              const DebugInfo& debug_info) {
652
    parent.children_.push_back(
15✔
653
        std::unique_ptr<Continue>(new Continue(this->element_counter_, debug_info)));
15✔
654
    this->element_counter_++;
15✔
655
    parent.transitions_.push_back(std::unique_ptr<Transition>(
15✔
656
        new Transition(this->element_counter_, debug_info, assignments)));
15✔
657
    this->element_counter_++;
15✔
658
    return static_cast<Continue&>(*parent.children_.back().get());
15✔
659
};
×
660

661
Break& StructuredSDFGBuilder::add_break(Sequence& parent, const DebugInfo& debug_info) {
15✔
662
    return this->add_break(parent, symbolic::Assignments{}, debug_info);
15✔
663
};
×
664

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

677
Return& StructuredSDFGBuilder::add_return(Sequence& parent,
14✔
678
                                          const sdfg::symbolic::Assignments& assignments,
679
                                          const DebugInfo& debug_info) {
680
    parent.children_.push_back(
14✔
681
        std::unique_ptr<Return>(new Return(this->element_counter_, debug_info)));
14✔
682
    this->element_counter_++;
14✔
683
    parent.transitions_.push_back(std::unique_ptr<Transition>(
14✔
684
        new Transition(this->element_counter_, debug_info, assignments)));
14✔
685
    this->element_counter_++;
14✔
686
    return static_cast<Return&>(*parent.children_.back().get());
14✔
687
};
×
688

689
For& StructuredSDFGBuilder::convert_while(Sequence& parent, While& loop,
×
690
                                          const symbolic::Symbol& indvar,
691
                                          const symbolic::Condition& condition,
692
                                          const symbolic::Expression& init,
693
                                          const symbolic::Expression& update) {
694
    // Insert for loop
695
    size_t index = 0;
×
696
    for (auto& entry : parent.children_) {
×
697
        if (entry.get() == &loop) {
×
698
            break;
×
699
        }
700
        index++;
×
701
    }
702
    auto iter = parent.children_.begin() + index;
×
703
    auto& new_iter = *parent.children_.insert(
×
704
        iter + 1, std::unique_ptr<For>(new For(this->element_counter_, loop.debug_info(), indvar,
×
705
                                               init, update, condition)));
×
706
    this->element_counter_ = this->element_counter_ + 2;
×
707
    auto& for_loop = dynamic_cast<For&>(*new_iter);
×
708
    this->insert_children(for_loop.root(), loop.root(), 0);
×
709

710
    // Remove while loop
711
    parent.children_.erase(parent.children_.begin() + index);
×
712

713
    return for_loop;
×
714
};
×
715

716
void StructuredSDFGBuilder::clear_sequence(Sequence& parent) {
2✔
717
    parent.children_.clear();
2✔
718
    parent.transitions_.clear();
2✔
719
};
2✔
720

721
Sequence& StructuredSDFGBuilder::parent(const ControlFlowNode& node) {
98✔
722
    std::list<structured_control_flow::ControlFlowNode*> queue = {&this->structured_sdfg_->root()};
98✔
723
    while (!queue.empty()) {
210✔
724
        auto current = queue.front();
210✔
725
        queue.pop_front();
210✔
726

727
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
210✔
728
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
286✔
729
                if (&sequence_stmt->at(i).first == &node) {
233✔
730
                    return *sequence_stmt;
98✔
731
                }
732
                queue.push_back(&sequence_stmt->at(i).first);
135✔
733
            }
135✔
734
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
112✔
735
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
736
                queue.push_back(&if_else_stmt->at(i).first);
×
737
            }
×
738
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
59✔
739
            queue.push_back(&while_stmt->root());
×
740
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
59✔
741
            queue.push_back(&for_stmt->root());
59✔
742
        } else if (auto kern_stmt = dynamic_cast<const structured_control_flow::Kernel*>(current)) {
59✔
743
            queue.push_back(&kern_stmt->root());
×
744
        }
×
745
    }
746

747
    return this->structured_sdfg_->root();
×
748
};
98✔
749

750
Kernel& StructuredSDFGBuilder::convert_into_kernel() {
6✔
751
    auto old_root = std::move(this->structured_sdfg_->root_);
6✔
752
    this->structured_sdfg_->root_ =
6✔
753
        std::unique_ptr<Sequence>(new Sequence(this->element_counter_, old_root->debug_info()));
6✔
754
    this->element_counter_++;
6✔
755
    auto& new_root = this->structured_sdfg_->root();
6✔
756
    auto& kernel = this->add_kernel(new_root, this->function().name());
6✔
757

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

760
    types::Scalar gridDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
761
    add_container(kernel.gridDim_x()->get_name(), gridDim_x);
6✔
762
    types::Scalar gridDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
763
    add_container(kernel.gridDim_y()->get_name(), gridDim_y);
6✔
764
    types::Scalar gridDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
765
    add_container(kernel.gridDim_z()->get_name(), gridDim_z);
6✔
766
    types::Scalar blockDim_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
767
    add_container(kernel.blockDim_x()->get_name(), blockDim_x);
6✔
768
    types::Scalar blockDim_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
769
    add_container(kernel.blockDim_y()->get_name(), blockDim_y);
6✔
770
    types::Scalar blockDim_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
771
    add_container(kernel.blockDim_z()->get_name(), blockDim_z);
6✔
772
    types::Scalar blockIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
773
    add_container(kernel.blockIdx_x()->get_name(), blockIdx_x);
6✔
774
    types::Scalar blockIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
775
    add_container(kernel.blockIdx_y()->get_name(), blockIdx_y);
6✔
776
    types::Scalar blockIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
777
    add_container(kernel.blockIdx_z()->get_name(), blockIdx_z);
6✔
778
    types::Scalar threadIdx_x(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
779
    add_container(kernel.threadIdx_x()->get_name(), threadIdx_x);
6✔
780
    types::Scalar threadIdx_y(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
781
    add_container(kernel.threadIdx_y()->get_name(), threadIdx_y);
6✔
782
    types::Scalar threadIdx_z(types::PrimitiveType::Int32, types::DeviceLocation::nvptx, 0);
6✔
783
    add_container(kernel.threadIdx_z()->get_name(), threadIdx_z);
6✔
784

785
    kernel.root().replace(symbolic::symbol("gridDim.x"), kernel.gridDim_x());
6✔
786
    kernel.root().replace(symbolic::symbol("gridDim.y"), kernel.gridDim_y());
6✔
787
    kernel.root().replace(symbolic::symbol("gridDim.z"), kernel.gridDim_z());
6✔
788

789
    kernel.root().replace(symbolic::symbol("blockDim.x"), kernel.blockDim_x());
6✔
790
    kernel.root().replace(symbolic::symbol("blockDim.y"), kernel.blockDim_y());
6✔
791
    kernel.root().replace(symbolic::symbol("blockDim.z"), kernel.blockDim_z());
6✔
792

793
    kernel.root().replace(symbolic::symbol("blockIdx.x"), kernel.blockIdx_x());
6✔
794
    kernel.root().replace(symbolic::symbol("blockIdx.y"), kernel.blockIdx_y());
6✔
795
    kernel.root().replace(symbolic::symbol("blockIdx.z"), kernel.blockIdx_z());
6✔
796

797
    kernel.root().replace(symbolic::symbol("threadIdx.x"), kernel.threadIdx_x());
6✔
798
    kernel.root().replace(symbolic::symbol("threadIdx.y"), kernel.threadIdx_y());
6✔
799
    kernel.root().replace(symbolic::symbol("threadIdx.z"), kernel.threadIdx_z());
6✔
800

801
    return kernel;
6✔
802
};
6✔
803

804
/***** Section: Dataflow Graph *****/
805

806
data_flow::AccessNode& StructuredSDFGBuilder::add_access(structured_control_flow::Block& block,
1,039✔
807
                                                         const std::string& data,
808
                                                         const DebugInfo& debug_info) {
809
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
1,039✔
810
    auto res = block.dataflow_->nodes_.insert(
2,078✔
811
        {vertex, std::unique_ptr<data_flow::AccessNode>(new data_flow::AccessNode(
1,039✔
812
                     this->element_counter_, debug_info, vertex, block.dataflow(), data))});
1,039✔
813
    this->element_counter_++;
1,039✔
814
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
1,039✔
815
};
×
816

817
data_flow::Tasklet& StructuredSDFGBuilder::add_tasklet(
550✔
818
    structured_control_flow::Block& block, const data_flow::TaskletCode code,
819
    const std::pair<std::string, sdfg::types::Scalar>& output,
820
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
821
    const DebugInfo& debug_info) {
822
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
550✔
823
    auto res = block.dataflow_->nodes_.insert(
1,100✔
824
        {vertex, std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
1,100✔
825
                     this->element_counter_, debug_info, vertex, block.dataflow(), code, output,
550✔
826
                     inputs, symbolic::__true__()))});
550✔
827
    this->element_counter_++;
550✔
828
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
550✔
829
};
×
830

831
data_flow::Memlet& StructuredSDFGBuilder::add_memlet(
1,090✔
832
    structured_control_flow::Block& block, data_flow::DataFlowNode& src,
833
    const std::string& src_conn, data_flow::DataFlowNode& dst, const std::string& dst_conn,
834
    const data_flow::Subset& subset, const DebugInfo& debug_info) {
835
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, block.dataflow_->graph_);
1,090✔
836
    auto res = block.dataflow_->edges_.insert(
2,180✔
837
        {edge.first, std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
1,090✔
838
                         this->element_counter_, debug_info, edge.first, block.dataflow(), src,
1,090✔
839
                         src_conn, dst, dst_conn, subset))});
1,090✔
840
    this->element_counter_++;
1,090✔
841
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
1,090✔
842
};
×
843

844
data_flow::LibraryNode& StructuredSDFGBuilder::add_library_node(
10✔
845
    structured_control_flow::Block& block, const data_flow::LibraryNodeType& call,
846
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& outputs,
847
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
848
    const bool has_side_effect, const DebugInfo& debug_info) {
849
    auto vertex = boost::add_vertex(block.dataflow_->graph_);
10✔
850
    auto res = block.dataflow_->nodes_.insert(
20✔
851
        {vertex, std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
10✔
852
                     this->element_counter_, debug_info, vertex, block.dataflow(), outputs, inputs,
10✔
853
                     call, has_side_effect))});
10✔
854
    this->element_counter_++;
10✔
855
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
10✔
856
}
×
857

858
void StructuredSDFGBuilder::remove_memlet(structured_control_flow::Block& block,
×
859
                                          const data_flow::Memlet& edge) {
860
    auto& graph = block.dataflow();
×
861
    auto e = edge.edge();
×
862
    boost::remove_edge(e, graph.graph_);
×
863
    graph.edges_.erase(e);
×
864
};
×
865

866
void StructuredSDFGBuilder::remove_node(structured_control_flow::Block& block,
×
867
                                        const data_flow::DataFlowNode& node) {
868
    auto& graph = block.dataflow();
×
869
    auto v = node.vertex();
×
870
    boost::remove_vertex(v, graph.graph_);
×
871
    graph.nodes_.erase(v);
×
872
};
×
873

874
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
8✔
875
                                       const data_flow::Tasklet& node) {
876
    auto& graph = block.dataflow();
8✔
877

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

880
    // Delete incoming
881
    std::list<const data_flow::Memlet*> iedges;
8✔
882
    for (auto& iedge : graph.in_edges(node)) {
15✔
883
        iedges.push_back(&iedge);
7✔
884
    }
885
    for (auto iedge : iedges) {
15✔
886
        auto& src = iedge->src();
7✔
887
        to_delete.insert(&src);
7✔
888

889
        auto edge = iedge->edge();
7✔
890
        graph.edges_.erase(edge);
7✔
891
        boost::remove_edge(edge, graph.graph_);
7✔
892
    }
893

894
    // Delete outgoing
895
    std::list<const data_flow::Memlet*> oedges;
8✔
896
    for (auto& oedge : graph.out_edges(node)) {
16✔
897
        oedges.push_back(&oedge);
8✔
898
    }
899
    for (auto oedge : oedges) {
16✔
900
        auto& dst = oedge->dst();
8✔
901
        to_delete.insert(&dst);
8✔
902

903
        auto edge = oedge->edge();
8✔
904
        graph.edges_.erase(edge);
8✔
905
        boost::remove_edge(edge, graph.graph_);
8✔
906
    }
907

908
    // Delete nodes
909
    for (auto obsolete_node : to_delete) {
31✔
910
        if (graph.in_degree(*obsolete_node) == 0 && graph.out_degree(*obsolete_node) == 0) {
23✔
911
            auto vertex = obsolete_node->vertex();
23✔
912
            graph.nodes_.erase(vertex);
23✔
913
            boost::remove_vertex(vertex, graph.graph_);
23✔
914
        }
23✔
915
    }
916
};
8✔
917

918
void StructuredSDFGBuilder::clear_node(structured_control_flow::Block& block,
1✔
919
                                       const data_flow::AccessNode& node) {
920
    auto& graph = block.dataflow();
1✔
921
    assert(graph.out_degree(node) == 0);
1✔
922

923
    std::list<const data_flow::Memlet*> tmp;
1✔
924
    std::list<const data_flow::DataFlowNode*> queue = {&node};
1✔
925
    while (!queue.empty()) {
3✔
926
        auto current = queue.front();
2✔
927
        queue.pop_front();
2✔
928
        if (current != &node) {
2✔
929
            if (dynamic_cast<const data_flow::AccessNode*>(current)) {
1✔
930
                if (graph.in_degree(*current) > 0 || graph.out_degree(*current) > 0) {
×
931
                    continue;
×
932
                }
933
            }
×
934
        }
1✔
935

936
        tmp.clear();
2✔
937
        for (auto& iedge : graph.in_edges(*current)) {
3✔
938
            tmp.push_back(&iedge);
1✔
939
        }
940
        for (auto iedge : tmp) {
3✔
941
            auto& src = iedge->src();
1✔
942
            queue.push_back(&src);
1✔
943

944
            auto edge = iedge->edge();
1✔
945
            graph.edges_.erase(edge);
1✔
946
            boost::remove_edge(edge, graph.graph_);
1✔
947
        }
948

949
        auto vertex = current->vertex();
2✔
950
        graph.nodes_.erase(vertex);
2✔
951
        boost::remove_vertex(vertex, graph.graph_);
2✔
952
    }
953
};
1✔
954

955
data_flow::AccessNode& StructuredSDFGBuilder::symbolic_expression_to_dataflow(
1✔
956
    structured_control_flow::Block& parent, const symbolic::Expression& expr) {
957
    auto& sdfg = this->subject();
1✔
958

959
    codegen::CPPLanguageExtension language_extension;
1✔
960

961
    // Base cases
962
    if (SymEngine::is_a<SymEngine::Symbol>(*expr)) {
1✔
963
        auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(expr);
1✔
964

965
        // Determine type
966
        types::Scalar sym_type = types::Scalar(types::PrimitiveType::Void);
1✔
967
        if (symbolic::is_nvptx(sym)) {
1✔
968
            sym_type = types::Scalar(types::PrimitiveType::Int32);
×
969
        } else {
×
970
            sym_type = static_cast<const types::Scalar&>(sdfg.type(sym->get_name()));
1✔
971
        }
972

973
        // Add new container for intermediate result
974
        auto tmp = this->find_new_name();
1✔
975
        this->add_container(tmp, sym_type);
1✔
976

977
        // Create dataflow graph
978
        auto& input_node = this->add_access(parent, sym->get_name());
1✔
979
        auto& output_node = this->add_access(parent, tmp);
1✔
980
        auto& tasklet = this->add_tasklet(parent, data_flow::TaskletCode::assign,
2✔
981
                                          {"_out", sym_type}, {{"_in", sym_type}});
1✔
982
        this->add_memlet(parent, input_node, "void", tasklet, "_in", {symbolic::integer(0)});
1✔
983
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
1✔
984

985
        return output_node;
1✔
986
    } else if (SymEngine::is_a<SymEngine::Integer>(*expr)) {
1✔
987
        auto tmp = this->find_new_name();
×
988
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Int64));
×
989

990
        auto& output_node = this->add_access(parent, tmp);
×
991
        auto& tasklet = this->add_tasklet(
×
992
            parent, data_flow::TaskletCode::assign,
×
993
            {"_out", types::Scalar(types::PrimitiveType::Int64)},
×
994
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Int64)}});
×
995
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
996
        return output_node;
×
997
    } else if (SymEngine::is_a<SymEngine::BooleanAtom>(*expr)) {
×
998
        auto tmp = this->find_new_name();
×
999
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1000

1001
        auto& output_node = this->add_access(parent, tmp);
×
1002
        auto& tasklet = this->add_tasklet(
×
1003
            parent, data_flow::TaskletCode::assign,
×
1004
            {"_out", types::Scalar(types::PrimitiveType::Bool)},
×
1005
            {{language_extension.expression(expr), types::Scalar(types::PrimitiveType::Bool)}});
×
1006
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1007
        return output_node;
×
1008
    } else if (SymEngine::is_a<SymEngine::Or>(*expr)) {
×
1009
        auto or_expr = SymEngine::rcp_static_cast<const SymEngine::Or>(expr);
×
1010
        assert(or_expr->get_container().size() == 2);
×
1011

1012
        std::vector<data_flow::AccessNode*> input_nodes;
×
1013
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1014
        for (auto& arg : or_expr->get_container()) {
×
1015
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1016
            input_nodes.push_back(&input_node);
×
1017
            input_types.push_back(
×
1018
                {"_in" + std::to_string(input_types.size() + 1),
×
1019
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1020
        }
1021

1022
        // Add new container for intermediate result
1023
        auto tmp = this->find_new_name();
×
1024
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1025

1026
        auto& output_node = this->add_access(parent, tmp);
×
1027
        auto& tasklet =
×
1028
            this->add_tasklet(parent, data_flow::TaskletCode::logical_or,
×
1029
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1030
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1031
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1032
                             {symbolic::integer(0)});
×
1033
        }
×
1034
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1035
        return output_node;
×
1036
    } else if (SymEngine::is_a<SymEngine::And>(*expr)) {
×
1037
        auto and_expr = SymEngine::rcp_static_cast<const SymEngine::And>(expr);
×
1038
        assert(and_expr->get_container().size() == 2);
×
1039

1040
        std::vector<data_flow::AccessNode*> input_nodes;
×
1041
        std::vector<std::pair<std::string, types::Scalar>> input_types;
×
1042
        for (auto& arg : and_expr->get_container()) {
×
1043
            auto& input_node = symbolic_expression_to_dataflow(parent, arg);
×
1044
            input_nodes.push_back(&input_node);
×
1045
            input_types.push_back(
×
1046
                {"_in" + std::to_string(input_types.size() + 1),
×
1047
                 static_cast<const types::Scalar&>(sdfg.type(input_node.data()))});
×
1048
        }
1049

1050
        // Add new container for intermediate result
1051
        auto tmp = this->find_new_name();
×
1052
        this->add_container(tmp, types::Scalar(types::PrimitiveType::Bool));
×
1053

1054
        auto& output_node = this->add_access(parent, tmp);
×
1055
        auto& tasklet =
×
1056
            this->add_tasklet(parent, data_flow::TaskletCode::logical_and,
×
1057
                              {"_out", types::Scalar(types::PrimitiveType::Bool)}, input_types);
×
1058
        for (size_t i = 0; i < input_nodes.size(); i++) {
×
1059
            this->add_memlet(parent, *input_nodes.at(i), "void", tasklet, input_types.at(i).first,
×
1060
                             {symbolic::integer(0)});
×
1061
        }
×
1062
        this->add_memlet(parent, tasklet, "_out", output_node, "void", {symbolic::integer(0)});
×
1063
        return output_node;
×
1064
    } else {
×
1065
        throw std::runtime_error("Unsupported expression type");
×
1066
    }
1067
};
1✔
1068

1069
}  // namespace builder
1070
}  // 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