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

daisytuner / sdfglib / 15796425724

21 Jun 2025 02:00PM UTC coverage: 64.059% (-0.5%) from 64.591%
15796425724

push

github

web-flow
Merge pull request #95 from daisytuner/data-dependencies

Extends Data Dependency Analysis

993 of 1197 new or added lines in 19 files covered. (82.96%)

55 existing lines in 5 files now uncovered.

8058 of 12579 relevant lines covered (64.06%)

150.28 hits per line

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

70.64
/src/analysis/loop_analysis.cpp
1
#include "sdfg/analysis/loop_analysis.h"
2

3
#include "sdfg/analysis/assumptions_analysis.h"
4
#include "sdfg/structured_control_flow/structured_loop.h"
5
#include "sdfg/symbolic/conjunctive_normal_form.h"
6
#include "sdfg/symbolic/maps.h"
7

8
namespace sdfg {
9
namespace analysis {
10

11
LoopAnalysis::LoopAnalysis(StructuredSDFG& sdfg) : Analysis(sdfg) {}
37✔
12

13
void LoopAnalysis::run(structured_control_flow::ControlFlowNode& scope,
98✔
14
                       structured_control_flow::ControlFlowNode* parent_loop) {
15
    std::list<structured_control_flow::ControlFlowNode*> queue = {&scope};
98✔
16
    while (!queue.empty()) {
317✔
17
        auto current = queue.front();
219✔
18
        queue.pop_front();
219✔
19

20
        // Loop detected
21
        if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
219✔
22
            this->loops_.insert(while_stmt);
×
23
            this->loop_tree_[while_stmt] = parent_loop;
×
24
        } else if (auto loop_stmt =
219✔
25
                       dynamic_cast<structured_control_flow::StructuredLoop*>(current)) {
219✔
26
            this->loops_.insert(loop_stmt);
61✔
27
            this->loop_tree_[loop_stmt] = parent_loop;
61✔
28
        }
61✔
29

30
        if (dynamic_cast<structured_control_flow::Block*>(current)) {
219✔
31
            continue;
60✔
32
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
159✔
33
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
219✔
34
                queue.push_back(&sequence_stmt->at(i).first);
121✔
35
            }
121✔
36
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
159✔
UNCOV
37
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
×
UNCOV
38
                queue.push_back(&if_else_stmt->at(i).first);
×
UNCOV
39
            }
×
40
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
61✔
41
            this->run(while_stmt->root(), while_stmt);
×
42
        } else if (auto for_stmt =
61✔
43
                       dynamic_cast<structured_control_flow::StructuredLoop*>(current)) {
61✔
44
            this->run(for_stmt->root(), for_stmt);
61✔
45
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
61✔
46
            continue;
×
47
        } else if (dynamic_cast<structured_control_flow::Continue*>(current)) {
×
48
            continue;
×
49
        } else if (dynamic_cast<structured_control_flow::Return*>(current)) {
×
50
            continue;
×
51
        } else {
52
            throw std::runtime_error("Unsupported control flow node type");
×
53
        }
54
    }
55
}
98✔
56

57
void LoopAnalysis::run(AnalysisManager& analysis_manager) {
37✔
58
    this->loops_.clear();
37✔
59
    this->loop_tree_.clear();
37✔
60
    this->run(this->sdfg_.root(), nullptr);
37✔
61
}
37✔
62

63
const std::unordered_set<structured_control_flow::ControlFlowNode*> LoopAnalysis::loops() const {
39✔
64
    return this->loops_;
39✔
65
}
66

67
bool LoopAnalysis::is_monotonic(structured_control_flow::StructuredLoop* loop,
98✔
68
                                AssumptionsAnalysis& assumptions_analysis) {
69
    auto assums = assumptions_analysis.get(*loop, true);
98✔
70

71
    return symbolic::is_monotonic(loop->update(), loop->indvar(), assums);
98✔
72
}
98✔
73

74
bool LoopAnalysis::is_contiguous(structured_control_flow::StructuredLoop* loop,
35✔
75
                                 AssumptionsAnalysis& assumptions_analysis) {
76
    auto assums = assumptions_analysis.get(*loop, true);
35✔
77

78
    return symbolic::is_contiguous(loop->update(), loop->indvar(), assums);
35✔
79
}
35✔
80

81
symbolic::Expression LoopAnalysis::canonical_bound(structured_control_flow::StructuredLoop* loop,
12✔
82
                                                   AssumptionsAnalysis& assumptions_analysis) {
83
    if (!LoopAnalysis::is_contiguous(loop, assumptions_analysis)) {
12✔
UNCOV
84
        return SymEngine::null;
×
85
    }
86

87
    symbolic::CNF cnf;
12✔
88
    try {
89
        cnf = symbolic::conjunctive_normal_form(loop->condition());
12✔
90
    } catch (const std::runtime_error& e) {
12✔
91
        return SymEngine::null;
×
92
    }
×
93

94
    bool has_complex_clauses = false;
12✔
95
    for (auto& clause : cnf) {
28✔
96
        if (clause.size() > 1) {
16✔
97
            has_complex_clauses = true;
×
98
            break;
×
99
        }
100
    }
101
    if (has_complex_clauses) {
12✔
102
        return SymEngine::null;
×
103
    }
104

105
    auto indvar = loop->indvar();
12✔
106
    symbolic::Expression bound = SymEngine::null;
12✔
107
    for (auto& clause : cnf) {
27✔
108
        for (auto& literal : clause) {
31✔
109
            if (SymEngine::is_a<SymEngine::StrictLessThan>(*literal)) {
16✔
110
                auto lt = SymEngine::rcp_dynamic_cast<const SymEngine::StrictLessThan>(literal);
11✔
111
                auto lhs = lt->get_args()[0];
11✔
112
                auto rhs = lt->get_args()[1];
11✔
113
                if (SymEngine::eq(*lhs, *indvar)) {
11✔
114
                    if (bound == SymEngine::null) {
11✔
115
                        bound = rhs;
8✔
116
                    } else {
8✔
117
                        bound = symbolic::min(bound, rhs);
3✔
118
                    }
119
                } else {
11✔
120
                    return SymEngine::null;
×
121
                }
122
            } else if (SymEngine::is_a<SymEngine::LessThan>(*literal)) {
16✔
123
                auto le = SymEngine::rcp_dynamic_cast<const SymEngine::LessThan>(literal);
5✔
124
                auto lhs = le->get_args()[0];
5✔
125
                auto rhs = le->get_args()[1];
5✔
126
                if (SymEngine::eq(*lhs, *indvar)) {
5✔
127
                    if (bound == SymEngine::null) {
4✔
128
                        bound = symbolic::add(rhs, symbolic::one());
3✔
129
                    } else {
3✔
130
                        bound = symbolic::min(bound, symbolic::add(rhs, symbolic::one()));
1✔
131
                    }
132
                } else {
4✔
133
                    return SymEngine::null;
1✔
134
                }
135
            } else {
5✔
136
                return SymEngine::null;
×
137
            }
138
        }
139
    }
140

141
    return bound;
11✔
142
}
12✔
143

144
const std::unordered_map<structured_control_flow::ControlFlowNode*,
145
                         structured_control_flow::ControlFlowNode*>&
146
LoopAnalysis::loop_tree() const {
×
147
    return this->loop_tree_;
×
148
}
149

150
structured_control_flow::ControlFlowNode* LoopAnalysis::parent_loop(
×
151
    structured_control_flow::ControlFlowNode* loop) const {
152
    return this->loop_tree_.at(loop);
×
153
}
154

155
const std::vector<structured_control_flow::ControlFlowNode*> LoopAnalysis::outermost_loops() const {
×
156
    std::vector<structured_control_flow::ControlFlowNode*> outermost_loops_;
×
157
    for (const auto& [loop, parent] : this->loop_tree_) {
×
158
        if (parent == nullptr) {
×
159
            outermost_loops_.push_back(loop);
×
160
        }
×
161
    }
162
    return outermost_loops_;
×
163
}
×
164

165
}  // namespace analysis
166
}  // 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