• 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

81.4
/src/analysis/assumptions_analysis.cpp
1
#include "sdfg/analysis/assumptions_analysis.h"
2

3
#include <utility>
4
#include <vector>
5

6
#include "sdfg/analysis/scope_analysis.h"
7
#include "sdfg/analysis/users.h"
8
#include "sdfg/symbolic/assumptions.h"
9
#include "sdfg/symbolic/extreme_values.h"
10
#include "sdfg/symbolic/maps.h"
11
#include "sdfg/symbolic/polynomials.h"
12

13
namespace sdfg {
14
namespace analysis {
15

16
symbolic::Expression AssumptionsAnalysis::cnf_to_upper_bound(const symbolic::CNF& cnf,
127✔
17
                                                             const symbolic::Symbol& indvar) {
18
    std::vector<symbolic::Expression> candidates;
127✔
19

20
    for (const auto& clause : cnf) {
269✔
21
        for (const auto& literal : clause) {
285✔
22
            // Comparison: indvar < expr
23
            if (SymEngine::is_a<SymEngine::StrictLessThan>(*literal)) {
143✔
24
                auto lt = SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(literal);
132✔
25
                if (symbolic::eq(lt->get_arg1(), indvar) &&
264✔
26
                    !symbolic::uses(lt->get_arg2(), indvar)) {
132✔
27
                    auto ub = symbolic::sub(lt->get_arg2(), symbolic::one());
132✔
28
                    candidates.push_back(ub);
132✔
29
                }
132✔
30
            }
132✔
31
            // Comparison: indvar <= expr
32
            else if (SymEngine::is_a<SymEngine::LessThan>(*literal)) {
11✔
33
                auto le = SymEngine::rcp_static_cast<const SymEngine::LessThan>(literal);
6✔
34
                if (symbolic::eq(le->get_arg1(), indvar) &&
11✔
35
                    !symbolic::uses(le->get_arg2(), indvar)) {
5✔
36
                    candidates.push_back(le->get_arg2());
5✔
37
                }
5✔
38
            }
6✔
39
            // Comparison: indvar == expr
40
            else if (SymEngine::is_a<SymEngine::Equality>(*literal)) {
5✔
NEW
41
                auto eq = SymEngine::rcp_static_cast<const SymEngine::Equality>(literal);
×
NEW
42
                if (symbolic::eq(eq->get_arg1(), indvar) &&
×
NEW
43
                    !symbolic::uses(eq->get_arg2(), indvar)) {
×
NEW
44
                    candidates.push_back(eq->get_arg2());
×
NEW
45
                }
×
NEW
46
            }
×
47
        }
48
    }
49

50
    if (candidates.empty()) {
127✔
51
        return SymEngine::null;
4✔
52
    }
53

54
    // Return the smallest upper bound across all candidate constraints
55
    symbolic::Expression result = candidates[0];
123✔
56
    for (size_t i = 1; i < candidates.size(); ++i) {
137✔
57
        result = symbolic::min(result, candidates[i]);
14✔
58
    }
14✔
59

60
    return result;
123✔
61
}
250✔
62

63
AssumptionsAnalysis::AssumptionsAnalysis(StructuredSDFG& sdfg)
292✔
64
    : Analysis(sdfg) {
146✔
65

66
      };
146✔
67

68
void AssumptionsAnalysis::visit_block(structured_control_flow::Block* block,
206✔
69
                                      analysis::AnalysisManager& analysis_manager) {
70
    return;
206✔
71
};
72

73
void AssumptionsAnalysis::visit_sequence(structured_control_flow::Sequence* sequence,
327✔
74
                                         analysis::AnalysisManager& analysis_manager) {
75
    return;
327✔
76
};
77

78
void AssumptionsAnalysis::visit_if_else(structured_control_flow::IfElse* if_else,
27✔
79
                                        analysis::AnalysisManager& analysis_manager) {
80
    auto& users = analysis_manager.get<analysis::Users>();
27✔
81
    for (size_t i = 0; i < if_else->size(); i++) {
73✔
82
        auto& scope = if_else->at(i).first;
46✔
83
        auto condition = if_else->at(i).second;
46✔
84
        auto symbols = symbolic::atoms(condition);
46✔
85

86
        // Assumption: symbols are read-only in scope
87
        bool read_only = true;
46✔
88
        analysis::UsersView scope_users(users, scope);
46✔
89
        for (auto& sym : symbols) {
73✔
90
            if (scope_users.writes(sym->get_name()).size() > 0) {
39✔
91
                read_only = false;
12✔
92
                break;
12✔
93
            }
94
        }
95
        if (!read_only) {
46✔
96
            continue;
12✔
97
        }
98

99
        try {
100
            auto cnf = symbolic::conjunctive_normal_form(condition);
34✔
101

102
            // Assumption: no or conditions
103
            bool has_complex_clauses = false;
34✔
104
            for (auto& clause : cnf) {
69✔
105
                if (clause.size() > 1) {
35✔
106
                    has_complex_clauses = true;
×
107
                    break;
×
108
                }
109
            }
110
            if (has_complex_clauses) {
34✔
111
                continue;
×
112
            }
113

114
            for (auto& sym : symbols) {
57✔
115
                symbolic::Expression ub = symbolic::infty(1);
23✔
116
                symbolic::Expression lb = symbolic::infty(-1);
23✔
117
                for (auto& clause : cnf) {
39✔
118
                    auto& literal = clause[0];
26✔
119
                    // Literal does not use symbol
120
                    if (!symbolic::uses(literal, sym)) {
26✔
121
                        continue;
2✔
122
                    }
123

124
                    if (SymEngine::is_a<SymEngine::Equality>(*literal)) {
24✔
125
                        auto eq = SymEngine::rcp_dynamic_cast<const SymEngine::Equality>(literal);
10✔
126
                        auto lhs = eq->get_args()[0];
10✔
127
                        auto rhs = eq->get_args()[1];
10✔
128
                        if (SymEngine::eq(*lhs, *sym) && !symbolic::uses(rhs, sym)) {
10✔
129
                            ub = rhs;
1✔
130
                            lb = rhs;
1✔
131
                            break;
1✔
132
                        } else if (SymEngine::eq(*rhs, *sym) && !symbolic::uses(lhs, sym)) {
9✔
133
                            ub = lhs;
9✔
134
                            lb = lhs;
9✔
135
                            break;
9✔
136
                        }
137
                    } else if (SymEngine::is_a<SymEngine::StrictLessThan>(*literal)) {
24✔
138
                        auto lt =
139
                            SymEngine::rcp_dynamic_cast<const SymEngine::StrictLessThan>(literal);
8✔
140
                        auto lhs = lt->get_args()[0];
8✔
141
                        auto rhs = lt->get_args()[1];
8✔
142
                        if (SymEngine::eq(*lhs, *sym) && !symbolic::uses(rhs, sym)) {
8✔
143
                            if (symbolic::eq(ub, symbolic::infty(1))) {
5✔
144
                                ub = rhs;
5✔
145
                            } else {
5✔
146
                                ub = symbolic::min(ub, rhs);
×
147
                            }
148
                        } else if (SymEngine::eq(*rhs, *sym) && !symbolic::uses(lhs, sym)) {
8✔
149
                            if (symbolic::eq(lb, symbolic::infty(-1))) {
3✔
150
                                lb = lhs;
3✔
151
                            } else {
3✔
152
                                lb = symbolic::max(lb, lhs);
×
153
                            }
154
                        }
3✔
155
                    } else if (SymEngine::is_a<SymEngine::LessThan>(*literal)) {
14✔
156
                        auto lt = SymEngine::rcp_dynamic_cast<const SymEngine::LessThan>(literal);
1✔
157
                        auto lhs = lt->get_args()[0];
1✔
158
                        auto rhs = lt->get_args()[1];
1✔
159
                        if (SymEngine::eq(*lhs, *sym) && !symbolic::uses(rhs, sym)) {
1✔
160
                            if (symbolic::eq(ub, symbolic::infty(1))) {
×
161
                                ub = rhs;
×
162
                            } else {
×
163
                                ub = symbolic::min(ub, rhs);
×
164
                            }
165
                        } else if (SymEngine::eq(*rhs, *sym) && !symbolic::uses(lhs, sym)) {
1✔
166
                            if (symbolic::eq(lb, symbolic::infty(-1))) {
1✔
167
                                lb = lhs;
1✔
168
                            } else {
1✔
169
                                lb = symbolic::max(lb, lhs);
×
170
                            }
171
                        }
1✔
172
                    }
1✔
173
                }
174

175
                // Failed to infer anything
176
                if (symbolic::eq(ub, symbolic::infty(1)) && symbolic::eq(lb, symbolic::infty(-1))) {
31✔
177
                    continue;
5✔
178
                }
179

180
                if (this->assumptions_.find(&scope) == this->assumptions_.end()) {
18✔
181
                    this->assumptions_.insert({&scope, symbolic::Assumptions()});
14✔
182
                }
14✔
183
                auto& scope_assumptions = this->assumptions_[&scope];
18✔
184
                if (scope_assumptions.find(sym) == scope_assumptions.end()) {
18✔
185
                    scope_assumptions.insert({sym, symbolic::Assumption(sym)});
18✔
186
                }
18✔
187

188
                if (!symbolic::eq(ub, symbolic::infty(1))) {
18✔
189
                    scope_assumptions[sym].upper_bound(ub);
15✔
190
                }
15✔
191
                if (!symbolic::eq(lb, symbolic::infty(-1))) {
18✔
192
                    scope_assumptions[sym].lower_bound(lb);
14✔
193
                }
14✔
194
            }
23✔
195
        } catch (const symbolic::CNFException& e) {
34✔
196
            continue;
197
        }
×
198
    }
46✔
199
};
27✔
200

201
void AssumptionsAnalysis::visit_while(structured_control_flow::While* while_loop,
7✔
202
                                      analysis::AnalysisManager& analysis_manager) {
203
    return;
7✔
204
};
205

206
void AssumptionsAnalysis::visit_for(structured_control_flow::For* for_loop,
128✔
207
                                    analysis::AnalysisManager& analysis_manager) {
208
    auto indvar = for_loop->indvar();
128✔
209
    auto update = for_loop->update();
128✔
210

211
    // Add new assumptions
212
    auto& body = for_loop->root();
128✔
213
    if (this->assumptions_.find(&body) == this->assumptions_.end()) {
128✔
214
        this->assumptions_.insert({&body, symbolic::Assumptions()});
128✔
215
    }
128✔
216
    auto& body_assumptions = this->assumptions_[&body];
128✔
217
    if (body_assumptions.find(indvar) == body_assumptions.end()) {
128✔
218
        body_assumptions.insert({indvar, symbolic::Assumption(indvar)});
128✔
219
    }
128✔
220

221
    // Assumption 1: indvar moves according to update
222
    body_assumptions[indvar].map(update);
128✔
223

224
    // Prove that update is monotonic -> assume bounds
225
    auto& assums = this->get(*for_loop);
128✔
226
    if (!symbolic::is_monotonic(update, indvar, assums)) {
128✔
227
        return;
1✔
228
    }
229

230
    // Assumption 2: init is lower bound
231
    body_assumptions[indvar].lower_bound(for_loop->init());
127✔
232
    try {
233
        auto cnf = symbolic::conjunctive_normal_form(for_loop->condition());
127✔
234
        auto ub = cnf_to_upper_bound(cnf, indvar);
127✔
235
        if (ub == SymEngine::null) {
127✔
236
            return;
4✔
237
        }
238
        // Assumption 3: ub is upper bound
239
        body_assumptions[indvar].upper_bound(ub);
123✔
240
    } catch (const symbolic::CNFException& e) {
127✔
241
        return;
242
    }
×
243
}
128✔
244

245
void AssumptionsAnalysis::visit_map(structured_control_flow::Map* map,
×
246
                                    analysis::AnalysisManager& analysis_manager) {
247
    auto indvar = map->indvar();
×
248

249
    auto& body = map->root();
×
250
    if (this->assumptions_.find(&body) == this->assumptions_.end()) {
×
251
        this->assumptions_.insert({&body, symbolic::Assumptions()});
×
252
    }
×
253
    auto& body_assumptions = this->assumptions_[&body];
×
254
    if (body_assumptions.find(indvar) == body_assumptions.end()) {
×
255
        body_assumptions.insert({indvar, symbolic::Assumption(indvar)});
×
256
    }
×
257
    body_assumptions[indvar].lower_bound(symbolic::zero());
×
258
    body_assumptions[indvar].upper_bound(symbolic::sub(map->num_iterations(), symbolic::one()));
×
NEW
259
    body_assumptions[indvar].map(map->update());
×
UNCOV
260
};
×
261

262
void AssumptionsAnalysis::traverse(structured_control_flow::Sequence& root,
146✔
263
                                   analysis::AnalysisManager& analysis_manager) {
264
    std::list<structured_control_flow::ControlFlowNode*> queue = {&root};
146✔
265
    while (!queue.empty()) {
849✔
266
        auto current = queue.front();
703✔
267
        queue.pop_front();
703✔
268

269
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
703✔
270
            this->visit_block(block_stmt, analysis_manager);
206✔
271
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
703✔
272
            this->visit_sequence(sequence_stmt, analysis_manager);
327✔
273
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
703✔
274
                queue.push_back(&sequence_stmt->at(i).first);
376✔
275
            }
376✔
276
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
497✔
277
            this->visit_if_else(if_else_stmt, analysis_manager);
27✔
278
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
73✔
279
                queue.push_back(&if_else_stmt->at(i).first);
46✔
280
            }
46✔
281
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
170✔
282
            this->visit_while(while_stmt, analysis_manager);
7✔
283
            queue.push_back(&while_stmt->root());
7✔
284
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
143✔
285
            this->visit_for(for_stmt, analysis_manager);
128✔
286
            queue.push_back(&for_stmt->root());
128✔
287
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
136✔
288
            this->visit_map(map_stmt, analysis_manager);
×
289
            queue.push_back(&map_stmt->root());
×
290
        }
×
291
    }
292
};
146✔
293

294
void AssumptionsAnalysis::run(analysis::AnalysisManager& analysis_manager) {
146✔
295
    this->assumptions_.clear();
146✔
296

297
    // Add sdfg assumptions
298
    this->assumptions_.insert({&sdfg_.root(), symbolic::Assumptions()});
146✔
299

300
    // Add additional assumptions
301
    for (auto& entry : this->additional_assumptions_) {
146✔
302
        this->assumptions_[&sdfg_.root()][entry.first] = entry.second;
×
303
    }
304

305
    // Forward propagate for each node
306
    this->traverse(sdfg_.root(), analysis_manager);
146✔
307
};
146✔
308

309
const symbolic::Assumptions AssumptionsAnalysis::get(structured_control_flow::ControlFlowNode& node,
443✔
310
                                                     bool include_trivial_bounds) {
311
    // Compute assumptions on the fly
312

313
    // Node-level assumptions
314
    symbolic::Assumptions assums;
443✔
315
    if (this->assumptions_.find(&node) != this->assumptions_.end()) {
443✔
316
        for (auto& entry : this->assumptions_[&node]) {
127✔
317
            assums.insert({entry.first, entry.second});
63✔
318
        }
319
    }
64✔
320

321
    AnalysisManager manager(this->sdfg_);
443✔
322
    auto& scope_analysis = manager.get<ScopeAnalysis>();
443✔
323

324
    auto scope = scope_analysis.parent_scope(&node);
443✔
325
    while (scope != nullptr) {
1,474✔
326
        // Don't overwrite lower scopes' assumptions
327
        if (this->assumptions_.find(scope) != this->assumptions_.end()) {
1,031✔
328
            for (auto& entry : this->assumptions_[scope]) {
956✔
329
                if (assums.find(entry.first) == assums.end()) {
259✔
330
                    assums.insert({entry.first, entry.second});
259✔
331
                }
259✔
332
            }
333
        }
697✔
334
        scope = scope_analysis.parent_scope(scope);
1,031✔
335
    }
336

337
    if (include_trivial_bounds) {
443✔
338
        for (auto& entry : sdfg_.assumptions()) {
1,269✔
339
            if (assums.find(entry.first) == assums.end()) {
960✔
340
                assums.insert({entry.first, entry.second});
697✔
341
            }
697✔
342
        }
343
    }
309✔
344

345
    return assums;
443✔
346
};
443✔
347

NEW
348
void AssumptionsAnalysis::add(symbolic::Assumptions& assums,
×
349
                              structured_control_flow::ControlFlowNode& node) {
NEW
350
    if (this->assumptions_.find(&node) == this->assumptions_.end()) {
×
NEW
351
        return;
×
352
    }
353

NEW
354
    for (auto& entry : this->assumptions_[&node]) {
×
NEW
355
        if (assums.find(entry.first) == assums.end()) {
×
NEW
356
            assums.insert({entry.first, entry.second});
×
NEW
357
        } else {
×
NEW
358
            assums[entry.first] = entry.second;
×
359
        }
360
    }
NEW
361
}
×
362

363
}  // namespace analysis
364
}  // 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