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

daisytuner / docc / 21919989349

11 Feb 2026 07:31PM UTC coverage: 66.315% (-0.08%) from 66.399%
21919989349

Pull #518

github

web-flow
Merge 0553f59bc into 632d0e1c4
Pull Request #518: Revert "Fix DeadDataElimination (#514)"

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

30 existing lines in 3 files now uncovered.

23488 of 35419 relevant lines covered (66.31%)

372.33 hits per line

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

78.46
/sdfg/src/analysis/users.cpp
1
#include "sdfg/analysis/users.h"
2

3
#include <cassert>
4
#include <list>
5
#include <memory>
6
#include <string>
7
#include <unordered_map>
8
#include <unordered_set>
9
#include <vector>
10

11
#include "sdfg/data_flow/memlet.h"
12
#include "sdfg/element.h"
13
#include "sdfg/graph/graph.h"
14
#include "sdfg/structured_control_flow/for.h"
15
#include "sdfg/structured_control_flow/map.h"
16
#include "sdfg/structured_control_flow/sequence.h"
17
#include "sdfg/structured_sdfg.h"
18
#include "sdfg/symbolic/sets.h"
19
#include "sdfg/symbolic/symbolic.h"
20

21
namespace sdfg {
22
namespace analysis {
23

24
User::User(graph::Vertex vertex, const std::string& container, Element* element, Use use)
25
    : vertex_(vertex), container_(container), use_(use), element_(element) {
13,473✔
26

27
      };
13,473✔
28

29
Use User::use() const { return this->use_; };
99,976✔
30

31
std::string& User::container() { return this->container_; };
582,402✔
32

33
Element* User::element() { return this->element_; };
19,743✔
34

35
const std::vector<data_flow::Subset>& User::subsets() const {
3,947✔
36
    if (this->subsets_cached_) {
3,947✔
37
        return this->subsets_;
3,149✔
38
    }
3,149✔
39

40
    if (this->container_ == "") {
798✔
41
        // No-op user
42
    } else if (auto access_node = dynamic_cast<data_flow::AccessNode*>(this->element_)) {
798✔
43
        auto& graph = access_node->get_parent();
600✔
44
        if (this->use_ == Use::READ || this->use_ == Use::VIEW) {
600✔
45
            for (auto& iedge : graph.out_edges(*access_node)) {
310✔
46
                this->subsets_.push_back(iedge.subset());
310✔
47
            }
310✔
48
        } else if (this->use_ == Use::WRITE || this->use_ == Use::MOVE) {
313✔
49
            for (auto& oedge : graph.in_edges(*access_node)) {
313✔
50
                this->subsets_.push_back(oedge.subset());
313✔
51
            }
313✔
52
        }
313✔
53
    } else {
600✔
54
        // Use of symbol
55
        this->subsets_.push_back({});
198✔
56
    }
198✔
57

58
    this->subsets_cached_ = true;
798✔
59
    return this->subsets_;
798✔
60
};
3,947✔
61

62
ForUser::ForUser(
63
    graph::Vertex vertex,
64
    const std::string& container,
65
    Element* element,
66
    Use use,
67
    bool is_init,
68
    bool is_condition,
69
    bool is_update
70
)
71
    : User(vertex, container, element, use), is_init_(is_init), is_condition_(is_condition), is_update_(is_update) {
3,408✔
72

73
      };
3,408✔
74

75
bool ForUser::is_init() const { return this->is_init_; };
3,412✔
76

77
bool ForUser::is_condition() const { return this->is_condition_; };
2,675✔
78

79
bool ForUser::is_update() const { return this->is_update_; };
1,403✔
80

81
std::pair<graph::Vertex, graph::Vertex> Users::traverse(data_flow::DataFlowGraph& dataflow) {
974✔
82
    graph::Vertex first = boost::graph_traits<graph::Graph>::null_vertex();
974✔
83
    graph::Vertex last = boost::graph_traits<graph::Graph>::null_vertex();
974✔
84
    for (auto node : dataflow.topological_sort()) {
2,754✔
85
        if (dynamic_cast<data_flow::ConstantNode*>(node) != nullptr) {
2,754✔
86
            continue;
110✔
87
        }
110✔
88

89
        if (auto access_node = dynamic_cast<data_flow::AccessNode*>(node)) {
2,644✔
90
            if (!symbolic::is_pointer(symbolic::symbol(access_node->data()))) {
1,840✔
91
                if (dataflow.in_degree(*node) > 0) {
1,840✔
92
                    Use use = Use::WRITE;
806✔
93

94
                    // Check if the pointer itself is moved (overwritten)
95
                    for (auto& iedge : dataflow.in_edges(*access_node)) {
806✔
96
                        if (iedge.type() == data_flow::MemletType::Reference ||
806✔
97
                            iedge.type() == data_flow::MemletType::Dereference_Src) {
806✔
98
                            use = Use::MOVE;
38✔
99
                            break;
38✔
100
                        }
38✔
101
                    }
806✔
102

103
                    auto v = boost::add_vertex(this->graph_);
806✔
104
                    this->add_user(std::make_unique<User>(v, access_node->data(), access_node, use));
806✔
105

106
                    if (last != boost::graph_traits<graph::Graph>::null_vertex()) {
806✔
107
                        boost::add_edge(last, v, this->graph_);
727✔
108
                    } else {
727✔
109
                        first = v;
79✔
110
                    }
79✔
111
                    last = v;
806✔
112
                }
806✔
113
                if (dataflow.out_degree(*access_node) > 0) {
1,840✔
114
                    Use use = Use::READ;
1,068✔
115

116
                    // Check if the pointer itself is viewed (aliased)
117
                    for (auto& oedge : dataflow.out_edges(*access_node)) {
1,099✔
118
                        if (oedge.type() == data_flow::MemletType::Reference ||
1,099✔
119
                            oedge.type() == data_flow::MemletType::Dereference_Dst) {
1,099✔
120
                            use = Use::VIEW;
33✔
121
                            break;
33✔
122
                        }
33✔
123
                    }
1,099✔
124

125
                    auto v = boost::add_vertex(this->graph_);
1,068✔
126
                    this->add_user(std::make_unique<User>(v, access_node->data(), access_node, use));
1,068✔
127

128
                    if (last != boost::graph_traits<graph::Graph>::null_vertex()) {
1,068✔
129
                        boost::add_edge(last, v, this->graph_);
457✔
130
                    } else {
611✔
131
                        first = v;
611✔
132
                    }
611✔
133
                    last = v;
1,068✔
134
                }
1,068✔
135
            }
1,840✔
136
        } else if (auto library_node = dynamic_cast<data_flow::LibraryNode*>(node)) {
1,840✔
137
            for (auto& symbol : library_node->symbols()) {
107✔
138
                auto v = boost::add_vertex(this->graph_);
18✔
139
                this->add_user(std::make_unique<User>(v, symbol->get_name(), library_node, Use::READ));
18✔
140
                if (last != boost::graph_traits<graph::Graph>::null_vertex()) {
18✔
141
                    boost::add_edge(last, v, this->graph_);
18✔
142
                } else {
18✔
UNCOV
143
                    first = v;
×
UNCOV
144
                }
×
145
                last = v;
18✔
146
            }
18✔
147
        }
107✔
148

149
        for (auto& oedge : dataflow.out_edges(*node)) {
2,644✔
150
            std::unordered_set<std::string> used;
1,863✔
151
            for (auto dim : oedge.subset()) {
1,996✔
152
                for (auto atom : symbolic::atoms(dim)) {
1,996✔
153
                    if (used.find(atom->get_name()) != used.end()) {
1,843✔
154
                        continue;
10✔
155
                    }
10✔
156
                    used.insert(atom->get_name());
1,833✔
157

158
                    auto v = boost::add_vertex(this->graph_);
1,833✔
159
                    this->add_user(std::make_unique<User>(v, atom->get_name(), &oedge, Use::READ));
1,833✔
160
                    if (last != boost::graph_traits<graph::Graph>::null_vertex()) {
1,833✔
161
                        boost::add_edge(last, v, this->graph_);
1,759✔
162
                    } else {
1,759✔
163
                        first = v;
74✔
164
                    }
74✔
165
                    last = v;
1,833✔
166
                }
1,833✔
167
            }
1,996✔
168
        }
1,863✔
169
    }
2,644✔
170

171
    return {first, last};
974✔
172
};
974✔
173

174
std::pair<graph::Vertex, graph::Vertex> Users::traverse(structured_control_flow::ControlFlowNode& node) {
3,033✔
175
    if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(&node)) {
3,033✔
176
        // NOP
177
        auto s = boost::add_vertex(this->graph_);
974✔
178
        this->users_.insert({s, std::make_unique<User>(s, "", block_stmt, Use::NOP)});
974✔
179
        this->entries_.insert({block_stmt, this->users_.at(s).get()});
974✔
180

181
        // NOP
182
        auto t = boost::add_vertex(this->graph_);
974✔
183
        this->users_.insert({t, std::make_unique<User>(t, "", block_stmt, Use::NOP)});
974✔
184
        this->exits_.insert({block_stmt, this->users_.at(t).get()});
974✔
185

186
        auto& dataflow = block_stmt->dataflow();
974✔
187
        auto subgraph = this->traverse(dataflow);
974✔
188

189
        // May be empty
190
        if (subgraph.first == boost::graph_traits<graph::Graph>::null_vertex()) {
974✔
191
            boost::add_edge(s, t, this->graph_);
210✔
192
            return {s, t};
210✔
193
        }
210✔
194

195
        boost::add_edge(s, subgraph.first, this->graph_);
764✔
196
        boost::add_edge(subgraph.second, t, this->graph_);
764✔
197

198
        return {s, t};
764✔
199
    } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(&node)) {
2,059✔
200
        auto s = boost::add_vertex(this->graph_);
1,275✔
201
        this->users_.insert({s, std::make_unique<User>(s, "", sequence_stmt, Use::NOP)});
1,275✔
202
        this->entries_.insert({sequence_stmt, this->users_.at(s).get()});
1,275✔
203

204
        graph::Vertex current = s;
1,275✔
205
        for (size_t i = 0; i < sequence_stmt->size(); i++) {
3,034✔
206
            auto child = sequence_stmt->at(i);
1,764✔
207

208
            auto subgraph = this->traverse(child.first);
1,764✔
209
            boost::add_edge(current, subgraph.first, this->graph_);
1,764✔
210
            // Return node
211
            if (subgraph.second == boost::graph_traits<graph::Graph>::null_vertex()) {
1,764✔
212
                break;
5✔
213
            }
5✔
214
            current = subgraph.second;
1,759✔
215

216
            std::unordered_set<std::string> used;
1,759✔
217
            for (auto& entry : child.second.assignments()) {
1,759✔
218
                for (auto atom : symbolic::atoms(entry.second)) {
152✔
219
                    if (symbolic::is_pointer(atom)) {
64✔
220
                        continue;
×
221
                    }
×
222
                    if (used.find(atom->get_name()) != used.end()) {
64✔
223
                        continue;
×
224
                    }
×
225
                    used.insert(atom->get_name());
64✔
226

227
                    auto v = boost::add_vertex(this->graph_);
64✔
228
                    this->add_user(std::make_unique<User>(v, atom->get_name(), &child.second, Use::READ));
64✔
229

230
                    boost::add_edge(current, v, this->graph_);
64✔
231
                    current = v;
64✔
232
                }
64✔
233
            }
152✔
234

235
            for (auto& entry : child.second.assignments()) {
1,759✔
236
                auto v = boost::add_vertex(this->graph_);
152✔
237
                this->add_user(std::make_unique<User>(v, entry.first->get_name(), &child.second, Use::WRITE));
152✔
238

239
                boost::add_edge(current, v, this->graph_);
152✔
240
                current = v;
152✔
241
            }
152✔
242
        }
1,759✔
243

244
        if (current == boost::graph_traits<graph::Graph>::null_vertex()) {
1,275✔
245
            this->exits_.insert({sequence_stmt, this->users_.at(s).get()});
×
246
            return {s, current};
×
247
        }
×
248

249
        auto t = boost::add_vertex(this->graph_);
1,275✔
250
        this->users_.insert({t, std::make_unique<User>(t, "", sequence_stmt, Use::NOP)});
1,275✔
251
        boost::add_edge(current, t, this->graph_);
1,275✔
252
        this->exits_.insert({sequence_stmt, this->users_.at(t).get()});
1,275✔
253

254
        return {s, t};
1,275✔
255
    } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(&node)) {
1,275✔
256
        // NOP
257
        auto s = boost::add_vertex(this->graph_);
56✔
258
        this->users_.insert({s, std::make_unique<User>(s, "", if_else_stmt, Use::NOP)});
56✔
259
        this->entries_.insert({if_else_stmt, this->users_.at(s).get()});
56✔
260

261
        graph::Vertex last = s;
56✔
262

263
        std::unordered_set<std::string> used;
56✔
264
        for (size_t i = 0; i < if_else_stmt->size(); i++) {
134✔
265
            auto condition = if_else_stmt->at(i).second;
78✔
266
            for (auto atom : symbolic::atoms(condition)) {
79✔
267
                if (used.find(atom->get_name()) != used.end()) {
79✔
268
                    continue;
16✔
269
                }
16✔
270
                used.insert(atom->get_name());
63✔
271

272
                auto v = boost::add_vertex(this->graph_);
63✔
273

274
                this->add_user(std::make_unique<User>(v, atom->get_name(), if_else_stmt, Use::READ));
63✔
275

276
                boost::add_edge(last, v, this->graph_);
63✔
277
                last = v;
63✔
278
            }
63✔
279
        }
78✔
280

281
        graph::Vertex t = boost::graph_traits<graph::Graph>::null_vertex();
56✔
282
        for (size_t i = 0; i < if_else_stmt->size(); i++) {
134✔
283
            auto branch = if_else_stmt->at(i);
78✔
284
            auto subgraph = this->traverse(branch.first);
78✔
285
            boost::add_edge(last, subgraph.first, this->graph_);
78✔
286
            if (subgraph.second != boost::graph_traits<graph::Graph>::null_vertex()) {
78✔
287
                if (t == boost::graph_traits<graph::Graph>::null_vertex()) {
78✔
288
                    t = boost::add_vertex(this->graph_);
56✔
289
                    this->users_.insert({t, std::make_unique<User>(t, "", if_else_stmt, Use::NOP)});
56✔
290
                    this->exits_.insert({if_else_stmt, this->users_.at(t).get()});
56✔
291
                }
56✔
292
                boost::add_edge(subgraph.second, t, this->graph_);
78✔
293
            }
78✔
294
        }
78✔
295

296
        // Forward edge: Potentially missing else case
297
        if (!if_else_stmt->is_complete()) {
56✔
298
            if (t == boost::graph_traits<graph::Graph>::null_vertex()) {
34✔
299
                t = boost::add_vertex(this->graph_);
×
300
                this->users_.insert({t, std::make_unique<User>(t, "", if_else_stmt, Use::NOP)});
×
301
                this->exits_.insert({if_else_stmt, this->users_.at(t).get()});
×
302
            }
×
303
            boost::add_edge(last, t, this->graph_);
34✔
304
        }
34✔
305

306
        return {s, t};
56✔
307
    } else if (auto loop_stmt = dynamic_cast<structured_control_flow::While*>(&node)) {
728✔
308
        // NOP
309
        auto s = boost::add_vertex(this->graph_);
15✔
310
        this->users_.insert({s, std::make_unique<User>(s, "", loop_stmt, Use::NOP)});
15✔
311
        this->entries_.insert({loop_stmt, this->users_.at(s).get()});
15✔
312

313
        auto subgraph = this->traverse(loop_stmt->root());
15✔
314

315
        // NOP
316
        auto t = boost::add_vertex(this->graph_);
15✔
317
        this->users_.insert({t, std::make_unique<User>(t, "", loop_stmt, Use::NOP)});
15✔
318
        this->exits_.insert({loop_stmt, this->users_.at(t).get()});
15✔
319

320
        boost::add_edge(s, subgraph.first, this->graph_);
15✔
321
        if (subgraph.second != boost::graph_traits<graph::Graph>::null_vertex()) {
15✔
322
            boost::add_edge(subgraph.second, t, this->graph_);
15✔
323
        }
15✔
324

325
        // Empty loop
326
        boost::add_edge(s, t, this->graph_);
15✔
327
        // Back edge
328
        boost::add_edge(t, s, this->graph_);
15✔
329

330
        return {s, t};
15✔
331
    } else if (auto for_stmt = dynamic_cast<structured_control_flow::StructuredLoop*>(&node)) {
713✔
332
        // NOP
333
        auto s = boost::add_vertex(this->graph_);
699✔
334
        this->users_.insert({s, std::make_unique<User>(s, "", for_stmt, Use::NOP)});
699✔
335
        auto last = s;
699✔
336
        this->entries_.insert({for_stmt, this->users_.at(s).get()});
699✔
337

338
        // NOP
339
        auto t = boost::add_vertex(this->graph_);
699✔
340
        this->users_.insert({t, std::make_unique<User>(t, "", for_stmt, Use::NOP)});
699✔
341
        this->exits_.insert({for_stmt, this->users_.at(t).get()});
699✔
342

343
        // Init
344
        for (auto atom : symbolic::atoms(for_stmt->init())) {
699✔
345
            auto v = boost::add_vertex(this->graph_);
37✔
346
            this->add_user(std::make_unique<ForUser>(v, atom->get_name(), for_stmt, Use::READ, true, false, false));
37✔
347
            boost::add_edge(last, v, this->graph_);
37✔
348
            last = v;
37✔
349
        }
37✔
350
        // Indvar
351
        auto v = boost::add_vertex(this->graph_);
699✔
352
        this->add_user(std::make_unique<
699✔
353
                       ForUser>(v, for_stmt->indvar()->get_name(), for_stmt, Use::WRITE, true, false, false));
699✔
354

355
        boost::add_edge(last, v, this->graph_);
699✔
356
        last = v;
699✔
357

358
        // Condition
359
        for (auto atom : symbolic::atoms(for_stmt->condition())) {
1,274✔
360
            auto v = boost::add_vertex(this->graph_);
1,274✔
361
            this->add_user(std::make_unique<ForUser>(v, atom->get_name(), for_stmt, Use::READ, false, true, false));
1,274✔
362

363
            boost::add_edge(last, v, this->graph_);
1,274✔
364
            boost::add_edge(v, t, this->graph_);
1,274✔
365
            last = v;
1,274✔
366
        }
1,274✔
367

368
        auto subgraph = this->traverse(for_stmt->root());
699✔
369
        boost::add_edge(last, subgraph.first, this->graph_);
699✔
370

371
        // Update
372
        auto end = subgraph.second;
699✔
373
        for (auto atom : symbolic::atoms(for_stmt->update())) {
699✔
374
            auto v = boost::add_vertex(this->graph_);
699✔
375
            this->add_user(std::make_unique<ForUser>(v, atom->get_name(), for_stmt, Use::READ, false, false, true));
699✔
376
            boost::add_edge(end, v, this->graph_);
699✔
377
            end = v;
699✔
378
        }
699✔
379

380
        auto update_v = boost::add_vertex(this->graph_);
699✔
381
        this->add_user(std::make_unique<
699✔
382
                       ForUser>(update_v, for_stmt->indvar()->get_name(), for_stmt, Use::WRITE, false, false, true));
699✔
383

384
        if (end != boost::graph_traits<graph::Graph>::null_vertex()) {
699✔
385
            boost::add_edge(end, update_v, this->graph_);
699✔
386
        }
699✔
387
        end = update_v;
699✔
388

389
        if (end != boost::graph_traits<graph::Graph>::null_vertex()) {
699✔
390
            boost::add_edge(end, t, this->graph_);
699✔
391
        }
699✔
392

393
        // Back edge
394
        boost::add_edge(t, last, this->graph_);
699✔
395

396
        return {s, t};
699✔
397
    } else if (auto cont_stmt = dynamic_cast<structured_control_flow::Continue*>(&node)) {
699✔
398
        // Approximated by general back edge in loop scope
399
        auto v = boost::add_vertex(this->graph_);
4✔
400
        this->users_.insert({v, std::make_unique<User>(v, "", cont_stmt, Use::NOP)});
4✔
401
        this->entries_.insert({cont_stmt, this->users_.at(v).get()});
4✔
402
        this->exits_.insert({cont_stmt, this->users_.at(v).get()});
4✔
403
        return {v, v};
4✔
404
    } else if (auto br_stmt = dynamic_cast<structured_control_flow::Break*>(&node)) {
10✔
405
        // Approximated by general back edge in loop scope
406
        auto v = boost::add_vertex(this->graph_);
5✔
407
        this->users_.insert({v, std::make_unique<User>(v, "", br_stmt, Use::NOP)});
5✔
408
        this->entries_.insert({br_stmt, this->users_.at(v).get()});
5✔
409
        this->exits_.insert({br_stmt, this->users_.at(v).get()});
5✔
410
        return {v, v};
5✔
411
    } else if (auto ret_stmt = dynamic_cast<structured_control_flow::Return*>(&node)) {
5✔
412
        if (!ret_stmt->is_data() || ret_stmt->data().empty()) {
5✔
413
            auto v = boost::add_vertex(this->graph_);
×
414
            this->users_.insert({v, std::make_unique<User>(v, "", ret_stmt, Use::NOP)});
×
415
            this->entries_.insert({ret_stmt, this->users_.at(v).get()});
×
416
            this->exits_.insert({ret_stmt, this->users_.at(v).get()});
×
417
            return {v, boost::graph_traits<graph::Graph>::null_vertex()};
×
418
        } else {
5✔
419
            auto v = boost::add_vertex(this->graph_);
5✔
420
            this->add_user(std::make_unique<User>(v, ret_stmt->data(), ret_stmt, Use::READ));
5✔
421
            this->entries_.insert({ret_stmt, this->users_.at(v).get()});
5✔
422
            this->exits_.insert({ret_stmt, this->users_.at(v).get()});
5✔
423
            return {v, boost::graph_traits<graph::Graph>::null_vertex()};
5✔
424
        }
5✔
425
    }
5✔
426

427
    throw std::invalid_argument("Invalid control flow node type");
×
428
};
3,033✔
429

430
Users::Users(StructuredSDFG& sdfg)
431
    : Analysis(sdfg), node_(sdfg.root()) {
477✔
432

433
      };
477✔
434

435
Users::Users(StructuredSDFG& sdfg, structured_control_flow::ControlFlowNode& node)
436
    : Analysis(sdfg), node_(node) {
×
437

438
      };
×
439

440
void Users::run(analysis::AnalysisManager& analysis_manager) {
477✔
441
    users_.clear();
477✔
442
    graph_.clear();
477✔
443
    source_ = nullptr;
477✔
444
    sink_ = nullptr;
477✔
445
    this->entries_.clear();
477✔
446
    this->exits_.clear();
477✔
447
    this->users_lookup_.clear();
477✔
448

449
    reads_.clear();
477✔
450
    writes_.clear();
477✔
451
    views_.clear();
477✔
452
    moves_.clear();
477✔
453

454
    this->traverse(node_);
477✔
455
    if (this->users_.empty()) {
477✔
456
        return;
×
457
    }
×
458

459
    // Require a single source
460
    for (auto& entry : this->users_) {
13,464✔
461
        if (boost::in_degree(entry.first, this->graph_) == 0) {
13,464✔
462
            if (this->source_ != nullptr) {
477✔
463
                throw InvalidSDFGException("Users Analysis: Non-unique source node");
×
464
            }
×
465
            this->source_ = entry.second.get();
477✔
466
        }
477✔
467
    }
13,464✔
468
    if (this->source_ == nullptr) {
477✔
469
        throw InvalidSDFGException("Users Analysis: No source node");
×
470
    }
×
471

472
    std::list<User*> sinks;
477✔
473
    for (auto& entry : this->users_) {
13,464✔
474
        if (boost::out_degree(entry.first, this->graph_) == 0) {
13,464✔
475
            sinks.push_back(entry.second.get());
482✔
476
        }
482✔
477
    }
13,464✔
478
    if (sinks.size() == 0) {
477✔
479
        throw InvalidSDFGException("Users Analysis: No sink node");
×
480
    }
×
481
    if (sinks.size() > 1) {
477✔
482
        // add artificial sink
483
        auto v = boost::add_vertex(this->graph_);
4✔
484
        auto sink = std::make_unique<User>(v, "", nullptr, Use::NOP);
4✔
485
        this->users_.insert({v, std::move(sink)});
4✔
486
        for (auto sink : sinks) {
9✔
487
            boost::add_edge(sink->vertex_, v, this->graph_);
9✔
488
        }
9✔
489
        sinks.clear();
4✔
490

491
        this->sink_ = this->users_.at(v).get();
4✔
492
    } else {
473✔
493
        this->sink_ = sinks.front();
473✔
494
    }
473✔
495

496
    for (auto& container : sdfg_.containers()) {
2,231✔
497
        this->reads_.insert({container, {}});
2,231✔
498
        this->writes_.insert({container, {}});
2,231✔
499
        this->views_.insert({container, {}});
2,231✔
500
        this->moves_.insert({container, {}});
2,231✔
501
    }
2,231✔
502

503
    // Collect sub structures
504
    for (auto& entry : this->users_) {
13,468✔
505
        auto container = entry.second->container();
13,468✔
506
        if (entry.second->use() == Use::NOP) {
13,468✔
507
            continue;
6,051✔
508
        }
6,051✔
509
        if (container == "") {
7,417✔
510
            continue;
×
511
        }
×
512

513
        switch (entry.second->use()) {
7,417✔
514
            case Use::READ: {
5,028✔
515
                this->reads_[container].push_back(entry.second.get());
5,028✔
516
                break;
5,028✔
517
            }
×
518
            case Use::WRITE: {
2,318✔
519
                this->writes_[container].push_back(entry.second.get());
2,318✔
520
                break;
2,318✔
521
            }
×
522
            case Use::VIEW: {
33✔
523
                this->views_[container].push_back(entry.second.get());
33✔
524
                break;
33✔
525
            }
×
526
            case Use::MOVE: {
38✔
527
                this->moves_[container].push_back(entry.second.get());
38✔
528
                break;
38✔
529
            }
×
530
            default:
×
531
                break;
×
532
        }
7,417✔
533
    }
7,417✔
534
};
477✔
535

536
std::list<User*> Users::uses() const {
×
537
    std::list<User*> us;
×
538
    for (auto& entry : this->users_) {
×
539
        if (entry.second->use() == Use::NOP) {
×
540
            continue;
×
541
        }
×
542
        us.push_back(entry.second.get());
×
543
    }
×
544

545
    return us;
×
546
};
×
547

548
std::list<User*> Users::uses(const std::string& container) const {
1,889✔
549
    std::list<User*> us;
1,889✔
550
    for (auto& entry : this->users_) {
127,642✔
551
        if (entry.second->container() != container) {
127,642✔
552
            continue;
117,582✔
553
        }
117,582✔
554
        if (entry.second->use() == Use::NOP) {
10,060✔
555
            continue;
×
556
        }
×
557
        us.push_back(entry.second.get());
10,060✔
558
    }
10,060✔
559

560
    return us;
1,889✔
561
};
1,889✔
562

563
size_t Users::num_uses(const std::string& container) const { return this->uses(container).size(); };
×
564

565
std::list<User*> Users::writes() const {
3✔
566
    std::list<User*> us;
3✔
567
    for (auto& entry : this->users_) {
29✔
568
        if (entry.second->use() != Use::WRITE) {
29✔
569
            continue;
24✔
570
        }
24✔
571
        us.push_back(entry.second.get());
5✔
572
    }
5✔
573

574
    return us;
3✔
575
};
3✔
576

577
const std::list<User*>& Users::writes(const std::string& container) const { return this->writes_.at(container); };
67✔
578

579
size_t Users::num_writes(const std::string& container) const { return this->writes(container).size(); };
7✔
580

581
std::list<User*> Users::reads() const {
×
582
    std::list<User*> us;
×
583
    for (auto& entry : this->users_) {
×
584
        if (entry.second->use() != Use::READ) {
×
585
            continue;
×
586
        }
×
587
        us.push_back(entry.second.get());
×
588
    }
×
589

590
    return us;
×
591
};
×
592

593
const std::list<User*>& Users::reads(const std::string& container) const { return this->reads_.at(container); };
2,114✔
594

595
size_t Users::num_reads(const std::string& container) const { return this->reads(container).size(); };
1,552✔
596

597
std::list<User*> Users::views() const {
×
598
    std::list<User*> us;
×
599
    for (auto& entry : this->users_) {
×
600
        if (entry.second->use() != Use::VIEW) {
×
601
            continue;
×
602
        }
×
603
        us.push_back(entry.second.get());
×
604
    }
×
605

606
    return us;
×
607
};
×
608

609
const std::list<User*>& Users::views(const std::string& container) const { return this->views_.at(container); };
462✔
610

611
size_t Users::num_views(const std::string& container) const { return this->views(container).size(); };
425✔
612

613
std::list<User*> Users::moves() const {
×
614
    std::list<User*> us;
×
615
    for (auto& entry : this->users_) {
×
616
        if (entry.second->use() != Use::MOVE) {
×
617
            continue;
×
618
        }
×
619
        us.push_back(entry.second.get());
×
620
    }
×
621

622
    return us;
×
623
};
×
624

625
const std::list<User*>& Users::moves(const std::string& container) const { return this->moves_.at(container); };
482✔
626

627
size_t Users::num_moves(const std::string& container) const { return this->moves(container).size(); };
425✔
628

629
structured_control_flow::ControlFlowNode* Users::scope(User* user) {
3,811✔
630
    if (auto data_node = dynamic_cast<data_flow::DataFlowNode*>(user->element())) {
3,811✔
631
        return static_cast<structured_control_flow::Block*>(data_node->get_parent().get_parent());
3,649✔
632
    } else if (auto memlet = dynamic_cast<data_flow::Memlet*>(user->element())) {
3,649✔
633
        return static_cast<structured_control_flow::Block*>(memlet->get_parent().get_parent());
50✔
634
    } else if (auto transition = dynamic_cast<structured_control_flow::Transition*>(user->element())) {
112✔
635
        return &transition->parent();
×
636
    } else {
112✔
637
        auto user_element = dynamic_cast<structured_control_flow::ControlFlowNode*>(user->element());
112✔
638
        assert(user_element != nullptr && "Users::scope: User element is not a ControlFlowNode");
112✔
639
        return user_element;
112✔
640
    }
112✔
641
}
3,811✔
642

643
const std::unordered_set<User*> Users::all_uses_between(User& user1, User& user2) {
11✔
644
    std::unordered_set<User*> uses;
11✔
645
    std::unordered_set<User*> visited;
11✔
646
    std::list<User*> queue = {&user2};
11✔
647
    while (!queue.empty()) {
62✔
648
        auto current = queue.front();
51✔
649
        queue.pop_front();
51✔
650

651
        // Stop conditions
652
        if (current == &user1) {
51✔
653
            continue;
11✔
654
        }
11✔
655

656
        if (visited.find(current) != visited.end()) {
40✔
657
            continue;
×
658
        }
×
659
        visited.insert(current);
40✔
660

661
        if (current != &user1 && current != &user2 && current->use() != Use::NOP) {
40✔
662
            uses.insert(current);
5✔
663
        }
5✔
664

665
        // Extend search
666
        // Backward search to utilize domination user1 over user
667
        auto [eb, ee] = boost::in_edges(current->vertex_, this->graph_);
40✔
668
        auto edges = std::ranges::subrange(eb, ee);
40✔
669
        for (auto edge : edges) {
40✔
670
            auto v = boost::source(edge, this->graph_);
40✔
671
            queue.push_back(this->users_.at(v).get());
40✔
672
        }
40✔
673
    }
40✔
674

675
    return uses;
11✔
676
};
11✔
677

678
const std::unordered_set<User*> Users::all_uses_after(User& user) {
34✔
679
    std::unordered_set<User*> uses;
34✔
680
    std::unordered_set<User*> visited;
34✔
681
    std::list<User*> queue = {&user};
34✔
682
    while (!queue.empty()) {
235✔
683
        auto current = queue.front();
201✔
684
        queue.pop_front();
201✔
685
        if (visited.find(current) != visited.end()) {
201✔
686
            continue;
4✔
687
        }
4✔
688
        visited.insert(current);
197✔
689

690
        if (current != &user && current->use() != Use::NOP) {
197✔
691
            uses.insert(current);
57✔
692
        }
57✔
693

694
        // Extend search
695
        auto [eb, ee] = boost::out_edges(current->vertex_, this->graph_);
197✔
696
        auto edges = std::ranges::subrange(eb, ee);
197✔
697
        for (auto edge : edges) {
197✔
698
            auto v = boost::target(edge, this->graph_);
167✔
699
            queue.push_back(this->users_.at(v).get());
167✔
700
        }
167✔
701
    }
197✔
702

703
    return uses;
34✔
704
};
34✔
705

706
const std::vector<std::string> Users::all_containers_in_order() {
×
707
    std::unordered_set<std::string> unique_containers;
×
708
    std::vector<std::string> containers;
×
709

710
    // BFS traversal
711
    std::unordered_set<User*> visited;
×
712
    std::list<User*> queue = {this->source_};
×
713
    while (!queue.empty()) {
×
714
        auto current = queue.front();
×
715
        queue.pop_front();
×
716
        if (visited.find(current) != visited.end()) {
×
717
            continue;
×
718
        }
×
719
        visited.insert(current);
×
720

721
        if ((current->container() != "" || current->use() != Use::NOP) &&
×
722
            unique_containers.find(current->container()) == unique_containers.end()) {
×
723
            unique_containers.insert(current->container());
×
724
            containers.push_back(current->container());
×
725
        }
×
726

727
        auto [eb, ee] = boost::out_edges(current->vertex_, this->graph_);
×
728
        auto edges = std::ranges::subrange(eb, ee);
×
729
        for (auto edge : edges) {
×
730
            auto v = boost::target(edge, this->graph_);
×
731
            queue.push_back(this->users_.at(v).get());
×
732
        }
×
733
    }
×
734
    return containers;
×
735
}
×
736

737
UsersView::UsersView(Users& users, const structured_control_flow::ControlFlowNode& node) : users_(users) {
1,529✔
738
    this->entry_ = users.entries_.at(&node);
1,529✔
739
    this->exit_ = users.exits_.at(&node);
1,529✔
740

741
    // Collect sub users
742
    std::unordered_set<User*> visited;
1,529✔
743
    std::list<User*> queue = {this->exit_};
1,529✔
744
    while (!queue.empty()) {
37,707✔
745
        auto current = queue.front();
36,178✔
746
        queue.pop_front();
36,178✔
747

748
        // Stop conditions
749
        if (current == this->entry_) {
36,178✔
750
            continue;
1,530✔
751
        }
1,530✔
752

753
        if (visited.find(current) != visited.end()) {
34,648✔
754
            continue;
4,045✔
755
        }
4,045✔
756
        visited.insert(current);
30,603✔
757

758
        this->sub_users_.insert(current);
30,603✔
759

760
        // Extend search
761
        // Backward search to utilize domination user1 over user
762
        auto [eb, ee] = boost::in_edges(current->vertex_, users.graph_);
30,603✔
763
        auto edges = std::ranges::subrange(eb, ee);
30,603✔
764
        for (auto edge : edges) {
34,649✔
765
            auto v = boost::source(edge, users.graph_);
34,649✔
766
            queue.push_back(users.users_.at(v).get());
34,649✔
767
        }
34,649✔
768
    }
30,603✔
769
};
1,529✔
770

771
std::vector<User*> UsersView::uses() const {
511✔
772
    std::vector<User*> us;
511✔
773
    for (auto& user : this->sub_users_) {
9,687✔
774
        if (user->use() == Use::NOP) {
9,687✔
775
            continue;
3,567✔
776
        }
3,567✔
777
        us.push_back(user);
6,120✔
778
    }
6,120✔
779

780
    return us;
511✔
781
};
511✔
782

783
std::vector<User*> UsersView::uses(const std::string& container) const {
2,802✔
784
    std::vector<User*> us;
2,802✔
785
    for (auto& user : this->sub_users_) {
83,631✔
786
        if (user->container() != container) {
83,631✔
787
            continue;
72,047✔
788
        }
72,047✔
789
        if (user->use() == Use::NOP) {
11,584✔
790
            continue;
×
791
        }
×
792
        us.push_back(user);
11,584✔
793
    }
11,584✔
794

795
    return us;
2,802✔
796
};
2,802✔
797

798
std::vector<User*> UsersView::writes() const {
72✔
799
    std::vector<User*> us;
72✔
800
    for (auto& user : this->sub_users_) {
2,753✔
801
        if (user->use() != Use::WRITE) {
2,753✔
802
            continue;
2,308✔
803
        }
2,308✔
804
        us.push_back(user);
445✔
805
    }
445✔
806

807
    return us;
72✔
808
};
72✔
809

810
std::vector<User*> UsersView::writes(const std::string& container) const {
2,500✔
811
    std::vector<User*> us;
2,500✔
812
    for (auto& user : this->sub_users_) {
58,841✔
813
        if (user->container() != container) {
58,841✔
814
            continue;
46,718✔
815
        }
46,718✔
816
        if (user->use() != Use::WRITE) {
12,123✔
817
            continue;
8,012✔
818
        }
8,012✔
819
        us.push_back(user);
4,111✔
820
    }
4,111✔
821

822
    return us;
2,500✔
823
};
2,500✔
824

825
std::vector<User*> UsersView::reads() const {
606✔
826
    std::vector<User*> us;
606✔
827
    for (auto& user : this->sub_users_) {
13,659✔
828
        if (user->use() != Use::READ) {
13,659✔
829
            continue;
6,640✔
830
        }
6,640✔
831
        us.push_back(user);
7,019✔
832
    }
7,019✔
833

834
    return us;
606✔
835
};
606✔
836

837
std::vector<User*> UsersView::reads(const std::string& container) const {
1,562✔
838
    std::vector<User*> us;
1,562✔
839
    for (auto& user : this->sub_users_) {
44,087✔
840
        if (user->container() != container) {
44,087✔
841
            continue;
38,809✔
842
        }
38,809✔
843
        if (user->use() != Use::READ) {
5,278✔
844
            continue;
856✔
845
        }
856✔
846
        us.push_back(user);
4,422✔
847
    }
4,422✔
848

849
    return us;
1,562✔
850
};
1,562✔
851

852
std::vector<User*> UsersView::views() const {
40✔
853
    std::vector<User*> us;
40✔
854
    for (auto& user : this->sub_users_) {
692✔
855
        if (user->use() != Use::VIEW) {
692✔
856
            continue;
692✔
857
        }
692✔
858
        us.push_back(user);
×
859
    }
×
860

861
    return us;
40✔
862
};
40✔
863

864
std::vector<User*> UsersView::views(const std::string& container) const {
171✔
865
    std::vector<User*> us;
171✔
866
    for (auto& user : this->sub_users_) {
2,876✔
867
        if (user->container() != container) {
2,876✔
868
            continue;
2,529✔
869
        }
2,529✔
870
        if (user->use() != Use::VIEW) {
347✔
871
            continue;
343✔
872
        }
343✔
873
        us.push_back(user);
4✔
874
    }
4✔
875

876
    return us;
171✔
877
};
171✔
878

879
std::vector<User*> UsersView::moves() const {
42✔
880
    std::vector<User*> us;
42✔
881
    for (auto& user : this->sub_users_) {
710✔
882
        if (user->use() != Use::MOVE) {
710✔
883
            continue;
708✔
884
        }
708✔
885
        us.push_back(user);
2✔
886
    }
2✔
887

888
    return us;
42✔
889
};
42✔
890

891
std::vector<User*> UsersView::moves(const std::string& container) const {
183✔
892
    std::vector<User*> us;
183✔
893
    for (auto& user : this->sub_users_) {
3,058✔
894
        if (user->container() != container) {
3,058✔
895
            continue;
2,681✔
896
        }
2,681✔
897
        if (user->use() != Use::MOVE) {
377✔
898
            continue;
372✔
899
        }
372✔
900
        us.push_back(user);
5✔
901
    }
5✔
902

903
    return us;
183✔
904
};
183✔
905

906
std::unordered_set<User*> UsersView::all_uses_between(User& user1, User& user2) {
×
907
    assert(this->sub_users_.find(&user1) != this->sub_users_.end());
×
908
    assert(this->sub_users_.find(&user2) != this->sub_users_.end());
×
909

910
    std::unordered_set<User*> uses;
×
911
    std::unordered_set<User*> visited;
×
912
    std::list<User*> queue = {&user1};
×
913
    while (!queue.empty()) {
×
914
        auto current = queue.front();
×
915
        queue.pop_front();
×
916
        if (visited.find(current) != visited.end()) {
×
917
            continue;
×
918
        }
×
919
        visited.insert(current);
×
920

921
        if (current != &user1 && current != &user2 && current->use() != Use::NOP) {
×
922
            uses.insert(current);
×
923
        }
×
924

925
        // Stop conditions
926
        if (current == exit_) {
×
927
            continue;
×
928
        }
×
929

930
        if (current == &user2) {
×
931
            continue;
×
932
        }
×
933

934
        // Extend search
935
        auto [eb, ee] = boost::out_edges(current->vertex_, this->users_.graph_);
×
936
        auto edges = std::ranges::subrange(eb, ee);
×
937
        for (auto edge : edges) {
×
938
            auto v = boost::target(edge, this->users_.graph_);
×
939
            queue.push_back(this->users_.users_.at(v).get());
×
940
        }
×
941
    }
×
942

943
    return uses;
×
944
};
×
945

946
std::unordered_set<User*> UsersView::all_uses_after(User& user) {
20✔
947
    assert(this->sub_users_.find(&user) != this->sub_users_.end());
20✔
948

949
    std::unordered_set<User*> uses;
20✔
950
    std::unordered_set<User*> visited;
20✔
951
    std::list<User*> queue = {&user};
20✔
952
    while (!queue.empty()) {
143✔
953
        auto current = queue.front();
123✔
954
        queue.pop_front();
123✔
955
        if (visited.find(current) != visited.end()) {
123✔
956
            continue;
×
957
        }
×
958
        visited.insert(current);
123✔
959

960
        if (current != &user && current->use() != Use::NOP) {
123✔
961
            uses.insert(current);
37✔
962
        }
37✔
963

964
        if (current == exit_) {
123✔
965
            continue;
20✔
966
        }
20✔
967

968
        // Extend search
969
        auto [eb, ee] = boost::out_edges(current->vertex_, this->users_.graph_);
103✔
970
        auto edges = std::ranges::subrange(eb, ee);
103✔
971
        for (auto edge : edges) {
103✔
972
            auto v = boost::target(edge, this->users_.graph_);
103✔
973
            queue.push_back(this->users_.users_.at(v).get());
103✔
974
        }
103✔
975
    }
103✔
976

977
    return uses;
20✔
978
};
20✔
979

980
bool Users::
981
    has_user(const std::string& container, Element* element, Use use, bool is_init, bool is_condition, bool is_update) {
208✔
982
    UserProps key{container, element, use, is_init, is_condition, is_update};
208✔
983
    if (this->users_lookup_.find(key) == this->users_lookup_.end()) {
208✔
984
        return false;
156✔
985
    }
156✔
986
    return true;
52✔
987
}
208✔
988

989
User* Users::
990
    get_user(const std::string& container, Element* element, Use use, bool is_init, bool is_condition, bool is_update) {
7,832✔
991
    UserProps key{container, element, use, is_init, is_condition, is_update};
7,832✔
992
    return this->users_lookup_.at(key);
7,832✔
993
}
7,832✔
994

995
void Users::add_user(std::unique_ptr<User> user) {
7,417✔
996
    auto vertex = user->vertex_;
7,417✔
997
    this->users_.insert({vertex, std::move(user)});
7,417✔
998

999
    auto user_ptr = this->users_.at(vertex).get();
7,417✔
1000
    bool is_init = false;
7,417✔
1001
    bool is_condition = false;
7,417✔
1002
    bool is_update = false;
7,417✔
1003
    if (auto for_user = dynamic_cast<ForUser*>(user_ptr)) {
7,417✔
1004
        auto for_loop = dynamic_cast<structured_control_flow::StructuredLoop*>(user_ptr->element());
3,408✔
1005
        if (for_loop == nullptr) {
3,408✔
1006
            throw std::invalid_argument("Invalid user type");
×
1007
        }
×
1008
        if (for_user->is_init()) {
3,408✔
1009
            is_init = true;
736✔
1010
        } else if (for_user->is_condition()) {
2,672✔
1011
            is_condition = true;
1,274✔
1012
        } else if (for_user->is_update()) {
1,398✔
1013
            is_update = true;
1,398✔
1014
        } else {
1,398✔
1015
            throw std::invalid_argument("Invalid user type");
×
1016
        }
×
1017
    }
3,408✔
1018

1019
    UserProps key{user_ptr->container(), user_ptr->element(), user_ptr->use(), is_init, is_condition, is_update};
7,417✔
1020
    this->users_lookup_.insert({key, user_ptr});
7,417✔
1021
}
7,417✔
1022

1023
std::unordered_set<std::string> Users::locals(structured_control_flow::ControlFlowNode& node) {
122✔
1024
    auto& sdfg = this->sdfg_;
122✔
1025

1026
    // Locals have no uses outside of the node
1027
    // We can check this by comparing the number of uses of the container in the view and the total
1028
    // number of uses of the container in the users map.
1029
    std::unordered_set<std::string> locals;
122✔
1030
    UsersView view(*this, node);
122✔
1031
    for (auto& user : view.uses()) {
1,621✔
1032
        if (!sdfg.is_transient(user->container())) {
1,621✔
1033
            continue;
468✔
1034
        }
468✔
1035
        if (view.uses(user->container()).size() == this->uses(user->container()).size()) {
1,153✔
1036
            locals.insert(user->container());
660✔
1037
        }
660✔
1038
    }
1,153✔
1039

1040
    return locals;
122✔
1041
};
122✔
1042

1043
const std::vector<std::string> UsersView::all_containers_in_order() {
×
1044
    std::unordered_set<std::string> unique_containers;
×
1045
    std::vector<std::string> containers;
×
1046

1047
    // BFS traversal
1048
    std::unordered_set<User*> visited;
×
1049
    std::list<User*> queue = {this->entry_};
×
1050
    while (!queue.empty()) {
×
1051
        auto current = queue.front();
×
1052
        queue.pop_front();
×
1053
        if (visited.find(current) != visited.end()) {
×
1054
            continue;
×
1055
        }
×
1056
        visited.insert(current);
×
1057

1058
        if ((current->container() != "" || current->use() != Use::NOP) &&
×
1059
            unique_containers.find(current->container()) == unique_containers.end()) {
×
1060
            unique_containers.insert(current->container());
×
1061
            containers.push_back(current->container());
×
1062
        }
×
1063

1064
        if (current == this->exit_) {
×
1065
            continue;
×
1066
        }
×
1067

1068
        auto [eb, ee] = boost::out_edges(current->vertex_, this->users_.graph_);
×
1069
        auto edges = std::ranges::subrange(eb, ee);
×
1070
        for (auto edge : edges) {
×
1071
            auto v = boost::target(edge, this->users_.graph_);
×
1072
            queue.push_back(this->users_.users_.at(v).get());
×
1073
        }
×
1074
    }
×
1075

1076
    return containers;
×
1077
}
×
1078

1079
} // namespace analysis
1080
} // 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