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

daisytuner / sdfglib / 21026558861

15 Jan 2026 09:38AM UTC coverage: 62.594% (+0.3%) from 62.264%
21026558861

Pull #446

github

web-flow
Merge 7b9320de6 into eaabb9b4d
Pull Request #446: Fix arg capturing

265 of 396 new or added lines in 7 files covered. (66.92%)

11 existing lines in 4 files now uncovered.

15902 of 25405 relevant lines covered (62.59%)

96.77 hits per line

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

73.41
/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) {
3,765✔
26

27
      };
3,765✔
28

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

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

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

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

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

58
    this->subsets_cached_ = true;
185✔
59
    return this->subsets_;
185✔
60
};
367✔
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) {
717✔
72

73
      };
717✔
74

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

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

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

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

89
        if (auto access_node = dynamic_cast<data_flow::AccessNode*>(node)) {
674✔
90
            if (!symbolic::is_pointer(symbolic::symbol(access_node->data()))) {
463✔
91
                if (dataflow.in_degree(*node) > 0) {
463✔
92
                    Use use = Use::WRITE;
244✔
93

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

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

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

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

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

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

149
        for (auto& oedge : dataflow.out_edges(*node)) {
674✔
150
            std::unordered_set<std::string> used;
440✔
151
            for (auto dim : oedge.subset()) {
440✔
152
                for (auto atom : symbolic::atoms(dim)) {
322✔
153
                    if (used.find(atom->get_name()) != used.end()) {
197✔
154
                        continue;
×
155
                    }
×
156
                    used.insert(atom->get_name());
197✔
157

158
                    auto v = boost::add_vertex(this->graph_);
197✔
159
                    this->add_user(std::make_unique<User>(v, atom->get_name(), &oedge, Use::READ));
197✔
160
                    if (last != boost::graph_traits<graph::Graph>::null_vertex()) {
197✔
161
                        boost::add_edge(last, v, this->graph_);
183✔
162
                    } else {
183✔
163
                        first = v;
14✔
164
                    }
14✔
165
                    last = v;
197✔
166
                }
197✔
167
            }
322✔
168
        }
440✔
169
    }
674✔
170

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

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

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

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

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

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

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

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

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

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

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

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

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

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

244
        if (current == boost::graph_traits<graph::Graph>::null_vertex()) {
473✔
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_);
473✔
250
        this->users_.insert({t, std::make_unique<User>(t, "", sequence_stmt, Use::NOP)});
473✔
251
        boost::add_edge(current, t, this->graph_);
473✔
252
        this->exits_.insert({sequence_stmt, this->users_.at(t).get()});
473✔
253

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

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

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

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

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

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

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

296
        // Forward edge: Potentially missing else case
297
        if (!if_else_stmt->is_complete()) {
30✔
298
            if (t == boost::graph_traits<graph::Graph>::null_vertex()) {
12✔
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_);
12✔
304
        }
12✔
305

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

396
        return {s, t};
145✔
397
    } else if (auto cont_stmt = dynamic_cast<structured_control_flow::Continue*>(&node)) {
145✔
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)) {
7✔
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()) {
2✔
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 {
2✔
419
            auto v = boost::add_vertex(this->graph_);
2✔
420
            this->add_user(std::make_unique<User>(v, ret_stmt->data(), ret_stmt, Use::READ));
2✔
421
            this->entries_.insert({ret_stmt, this->users_.at(v).get()});
2✔
422
            this->exits_.insert({ret_stmt, this->users_.at(v).get()});
2✔
423
            return {v, boost::graph_traits<graph::Graph>::null_vertex()};
2✔
424
        }
2✔
425
    }
2✔
426

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

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

433
      };
266✔
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) {
266✔
441
    users_.clear();
266✔
442
    graph_.clear();
266✔
443
    source_ = nullptr;
266✔
444
    sink_ = nullptr;
266✔
445
    this->entries_.clear();
266✔
446
    this->exits_.clear();
266✔
447
    this->users_lookup_.clear();
266✔
448

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

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

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

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

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

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

503
    // Collect sub structures
504
    for (auto& entry : this->users_) {
3,760✔
505
        auto container = entry.second->container();
3,760✔
506
        if (entry.second->use() == Use::NOP) {
3,760✔
507
            continue;
2,134✔
508
        }
2,134✔
509
        if (container == "") {
1,626✔
510
            continue;
×
511
        }
×
512

513
        switch (entry.second->use()) {
1,626✔
514
            case Use::READ: {
913✔
515
                this->reads_[container].push_back(entry.second.get());
913✔
516
                break;
913✔
517
            }
×
518
            case Use::WRITE: {
645✔
519
                this->writes_[container].push_back(entry.second.get());
645✔
520
                break;
645✔
521
            }
×
522
            case Use::VIEW: {
32✔
523
                this->views_[container].push_back(entry.second.get());
32✔
524
                break;
32✔
525
            }
×
526
            case Use::MOVE: {
36✔
527
                this->moves_[container].push_back(entry.second.get());
36✔
528
                break;
36✔
529
            }
×
530
            default:
×
531
                break;
×
532
        }
1,626✔
533
    }
1,626✔
534
};
266✔
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 {
301✔
549
    std::list<User*> us;
301✔
550
    for (auto& entry : this->users_) {
5,683✔
551
        if (entry.second->container() != container) {
5,683✔
552
            continue;
5,150✔
553
        }
5,150✔
554
        if (entry.second->use() == Use::NOP) {
533✔
555
            continue;
×
556
        }
×
557
        us.push_back(entry.second.get());
533✔
558
    }
533✔
559

560
    return us;
301✔
561
};
301✔
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 {
×
566
    std::list<User*> us;
×
567
    for (auto& entry : this->users_) {
×
568
        if (entry.second->use() != Use::WRITE) {
×
569
            continue;
×
570
        }
×
571
        us.push_back(entry.second.get());
×
572
    }
×
573

574
    return us;
×
575
};
×
576

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

579
size_t Users::num_writes(const std::string& container) const { return this->writes(container).size(); };
3✔
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); };
165✔
594

595
size_t Users::num_reads(const std::string& container) const { return this->reads(container).size(); };
26✔
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); };
33✔
610

611
size_t Users::num_views(const std::string& container) const { return this->views(container).size(); };
27✔
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); };
84✔
626

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

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

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

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

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

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

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

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

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

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

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

703
    return uses;
×
704
};
×
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) {
379✔
738
    this->entry_ = users.entries_.at(&node);
379✔
739
    this->exit_ = users.exits_.at(&node);
379✔
740

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

748
        // Stop conditions
749
        if (current == this->entry_) {
4,461✔
750
            continue;
379✔
751
        }
379✔
752

753
        if (visited.find(current) != visited.end()) {
4,082✔
754
            continue;
443✔
755
        }
443✔
756
        visited.insert(current);
3,639✔
757

758
        this->sub_users_.insert(current);
3,639✔
759

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

771
std::vector<User*> UsersView::uses() const {
124✔
772
    std::vector<User*> us;
124✔
773
    for (auto& user : this->sub_users_) {
1,301✔
774
        if (user->use() == Use::NOP) {
1,301✔
775
            continue;
520✔
776
        }
520✔
777
        us.push_back(user);
781✔
778
    }
781✔
779

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

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

795
    return us;
279✔
796
};
279✔
797

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

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

810
std::vector<User*> UsersView::writes(const std::string& container) const {
459✔
811
    std::vector<User*> us;
459✔
812
    for (auto& user : this->sub_users_) {
7,166✔
813
        if (user->container() != container) {
7,166✔
814
            continue;
5,137✔
815
        }
5,137✔
816
        if (user->use() != Use::WRITE) {
2,029✔
817
            continue;
1,352✔
818
        }
1,352✔
819
        us.push_back(user);
677✔
820
    }
677✔
821

822
    return us;
459✔
823
};
459✔
824

825
std::vector<User*> UsersView::reads() const {
124✔
826
    std::vector<User*> us;
124✔
827
    for (auto& user : this->sub_users_) {
1,168✔
828
        if (user->use() != Use::READ) {
1,168✔
829
            continue;
681✔
830
        }
681✔
831
        us.push_back(user);
487✔
832
    }
487✔
833

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

837
std::vector<User*> UsersView::reads(const std::string& container) const {
253✔
838
    std::vector<User*> us;
253✔
839
    for (auto& user : this->sub_users_) {
3,385✔
840
        if (user->container() != container) {
3,385✔
841
            continue;
2,824✔
842
        }
2,824✔
843
        if (user->use() != Use::READ) {
561✔
844
            continue;
120✔
845
        }
120✔
846
        us.push_back(user);
441✔
847
    }
441✔
848

849
    return us;
253✔
850
};
253✔
851

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

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

864
std::vector<User*> UsersView::views(const std::string& container) const {
52✔
865
    std::vector<User*> us;
52✔
866
    for (auto& user : this->sub_users_) {
623✔
867
        if (user->container() != container) {
623✔
868
            continue;
546✔
869
        }
546✔
870
        if (user->use() != Use::VIEW) {
77✔
871
            continue;
74✔
872
        }
74✔
873
        us.push_back(user);
3✔
874
    }
3✔
875

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

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

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

891
std::vector<User*> UsersView::moves(const std::string& container) const {
52✔
892
    std::vector<User*> us;
52✔
893
    for (auto& user : this->sub_users_) {
623✔
894
        if (user->container() != container) {
623✔
895
            continue;
546✔
896
        }
546✔
897
        if (user->use() != Use::MOVE) {
77✔
898
            continue;
74✔
899
        }
74✔
900
        us.push_back(user);
3✔
901
    }
3✔
902

903
    return us;
52✔
904
};
52✔
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) {
6✔
947
    assert(this->sub_users_.find(&user) != this->sub_users_.end());
6✔
948

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

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

964
        if (current == exit_) {
18✔
965
            continue;
6✔
966
        }
6✔
967

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

977
    return uses;
6✔
978
};
6✔
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) {
863✔
991
    UserProps key{container, element, use, is_init, is_condition, is_update};
863✔
992
    return this->users_lookup_.at(key);
863✔
993
}
863✔
994

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

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

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

1023
std::unordered_set<std::string> Users::locals(structured_control_flow::ControlFlowNode& node) {
21✔
1024
    auto& sdfg = this->sdfg_;
21✔
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;
21✔
1030
    UsersView view(*this, node);
21✔
1031
    for (auto& user : view.uses()) {
57✔
1032
        if (!sdfg.is_transient(user->container())) {
57✔
1033
            continue;
23✔
1034
        }
23✔
1035
        if (view.uses(user->container()).size() == this->uses(user->container()).size()) {
34✔
1036
            locals.insert(user->container());
12✔
1037
        }
12✔
1038
    }
34✔
1039

1040
    return locals;
21✔
1041
};
21✔
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