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

daisytuner / sdfglib / 15781182351

20 Jun 2025 02:27PM UTC coverage: 62.978% (-1.6%) from 64.591%
15781182351

Pull #95

github

web-flow
Merge 3b6e445ac into 37b47b09d
Pull Request #95: Extends Data Dependency Analysis

393 of 500 new or added lines in 10 files covered. (78.6%)

114 existing lines in 7 files now uncovered.

7752 of 12309 relevant lines covered (62.98%)

137.91 hits per line

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

73.45
/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/series.h"
7

8
namespace sdfg {
9
namespace analysis {
10

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

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

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

30
        if (dynamic_cast<structured_control_flow::Block*>(current)) {
288✔
31
            continue;
66✔
32
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
222✔
33
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
288✔
34
                queue.push_back(&sequence_stmt->at(i).first);
149✔
35
            }
149✔
36
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
222✔
37
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
12✔
38
                queue.push_back(&if_else_stmt->at(i).first);
8✔
39
            }
8✔
40
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
83✔
41
            this->run(while_stmt->root(), while_stmt);
×
42
        } else if (auto for_stmt =
79✔
43
                       dynamic_cast<structured_control_flow::StructuredLoop*>(current)) {
79✔
44
            this->run(for_stmt->root(), for_stmt);
79✔
45
        } else if (dynamic_cast<structured_control_flow::Break*>(current)) {
79✔
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
}
131✔
56

57
void LoopAnalysis::run(AnalysisManager& analysis_manager) {
52✔
58
    this->loops_.clear();
52✔
59
    this->loop_tree_.clear();
52✔
60
    this->run(this->sdfg_.root(), nullptr);
52✔
61
}
52✔
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) const {
55✔
68
    AnalysisManager manager(this->sdfg_);
55✔
69
    auto& assums_analysis = manager.get<AssumptionsAnalysis>();
55✔
70
    auto assums = assums_analysis.get(*loop, true);
55✔
71

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

75
bool LoopAnalysis::is_contiguous(structured_control_flow::StructuredLoop* loop) const {
20✔
76
    AnalysisManager manager(this->sdfg_);
20✔
77
    auto& assums_analysis = manager.get<AssumptionsAnalysis>();
20✔
78
    auto assums = assums_analysis.get(*loop, true);
20✔
79

80
    return symbolic::is_contiguous(loop->update(), loop->indvar(), assums);
20✔
81
}
20✔
82

83
symbolic::Expression LoopAnalysis::canonical_bound(
3✔
84
    structured_control_flow::StructuredLoop* loop) const {
85
    if (!this->is_contiguous(loop)) {
3✔
86
        return SymEngine::null;
×
87
    }
88

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

96
    bool has_complex_clauses = false;
3✔
97
    for (auto& clause : cnf) {
8✔
98
        if (clause.size() > 1) {
5✔
99
            has_complex_clauses = true;
×
100
            break;
×
101
        }
102
    }
103
    if (has_complex_clauses) {
3✔
104
        return SymEngine::null;
×
105
    }
106

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

143
    return bound;
3✔
144
}
3✔
145

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

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

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

167
}  // namespace analysis
168
}  // 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