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

daisytuner / sdfglib / 15822095523

23 Jun 2025 10:43AM UTC coverage: 63.824% (-0.02%) from 63.84%
15822095523

push

github

web-flow
Merge pull request #99 from daisytuner/delinearization

adds support for delinearizing affine expressions

69 of 103 new or added lines in 8 files covered. (66.99%)

3 existing lines in 2 files now uncovered.

8084 of 12666 relevant lines covered (63.82%)

149.74 hits per line

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

80.22
/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/polynomials.h"
11
#include "sdfg/symbolic/series.h"
12

13
namespace sdfg {
14
namespace analysis {
15

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

20
    for (const auto& clause : cnf) {
275✔
21
        for (const auto& literal : clause) {
291✔
22
            // Comparison: indvar < expr
23
            if (SymEngine::is_a<SymEngine::StrictLessThan>(*literal)) {
146✔
24
                auto lt = SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(literal);
135✔
25
                if (symbolic::eq(lt->get_arg1(), indvar) &&
270✔
26
                    !symbolic::uses(lt->get_arg2(), indvar)) {
135✔
27
                    auto ub = symbolic::sub(lt->get_arg2(), symbolic::one());
135✔
28
                    candidates.push_back(ub);
135✔
29
                }
135✔
30
            }
135✔
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✔
41
                auto eq = SymEngine::rcp_static_cast<const SymEngine::Equality>(literal);
×
42
                if (symbolic::eq(eq->get_arg1(), indvar) &&
×
43
                    !symbolic::uses(eq->get_arg2(), indvar)) {
×
44
                    candidates.push_back(eq->get_arg2());
×
45
                }
×
46
            }
×
47
        }
48
    }
49

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

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

60
    return result;
126✔
61
}
256✔
62

63
AssumptionsAnalysis::AssumptionsAnalysis(StructuredSDFG& sdfg)
296✔
64
    : Analysis(sdfg) {
148✔
65

66
      };
148✔
67

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

73
void AssumptionsAnalysis::visit_sequence(structured_control_flow::Sequence* sequence,
332✔
74
                                         analysis::AnalysisManager& analysis_manager) {
75
    return;
332✔
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
                scope_assumptions[sym].constant(true);
18✔
189
                if (!symbolic::eq(ub, symbolic::infty(1))) {
18✔
190
                    scope_assumptions[sym].upper_bound(ub);
15✔
191
                }
15✔
192
                if (!symbolic::eq(lb, symbolic::infty(-1))) {
18✔
193
                    scope_assumptions[sym].lower_bound(lb);
14✔
194
                }
14✔
195
            }
23✔
196
        } catch (const symbolic::CNFException& e) {
34✔
197
            continue;
198
        }
×
199
    }
46✔
200
};
27✔
201

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

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

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

219
    // By definition: indvar and condition symbols are constant
220
    symbolic::SymbolSet syms = {indvar};
131✔
221
    for (auto& sym : symbolic::atoms(for_loop->condition())) {
397✔
222
        syms.insert(sym);
266✔
223
    }
224
    for (auto& sym : syms) {
397✔
225
        if (body_assumptions.find(sym) == body_assumptions.end()) {
266✔
226
            body_assumptions.insert({sym, symbolic::Assumption(sym)});
266✔
227
        }
266✔
228
        body_assumptions[sym].constant(true);
266✔
229
    }
230

231
    // Bounds of indvar
232

233
    // Prove that update is monotonic -> assume bounds
234
    auto& assums = this->get(*for_loop);
131✔
235
    if (!symbolic::series::is_monotonic(update, indvar, assums)) {
131✔
236
        return;
1✔
237
    }
238

239
    // Assumption 2: init is lower bound
240
    body_assumptions[indvar].lower_bound(for_loop->init());
130✔
241
    try {
242
        auto cnf = symbolic::conjunctive_normal_form(for_loop->condition());
130✔
243
        auto ub = cnf_to_upper_bound(cnf, indvar);
130✔
244
        if (ub == SymEngine::null) {
130✔
245
            return;
4✔
246
        }
247
        // Assumption 3: ub is upper bound
248
        body_assumptions[indvar].upper_bound(ub);
126✔
249

250
        // Assumption 4: any ub symbol is at least init
251
        for (auto& sym : symbolic::atoms(ub)) {
255✔
252
            body_assumptions[sym].lower_bound(symbolic::add(for_loop->init(), symbolic::one()));
129✔
253
        }
254
    } catch (const symbolic::CNFException& e) {
130✔
255
        return;
256
    }
×
257
}
131✔
258

259
void AssumptionsAnalysis::visit_map(structured_control_flow::Map* map,
×
260
                                    analysis::AnalysisManager& analysis_manager) {
261
    auto indvar = map->indvar();
×
262

263
    auto& body = map->root();
×
264
    if (this->assumptions_.find(&body) == this->assumptions_.end()) {
×
265
        this->assumptions_.insert({&body, symbolic::Assumptions()});
×
266
    }
×
267
    auto& body_assumptions = this->assumptions_[&body];
×
268

269
    // By definition: indvar and num_iterations symbols are constant
270

NEW
271
    symbolic::SymbolSet syms = {indvar};
×
NEW
272
    for (auto& sym : symbolic::atoms(map->num_iterations())) {
×
NEW
273
        syms.insert(sym);
×
274
    }
NEW
275
    for (auto& sym : syms) {
×
NEW
276
        if (body_assumptions.find(sym) == body_assumptions.end()) {
×
NEW
277
            body_assumptions.insert({sym, symbolic::Assumption(sym)});
×
NEW
278
        }
×
NEW
279
        body_assumptions[sym].constant(true);
×
280
    }
281

282
    // Bounds of indvar
283
    body_assumptions[indvar].lower_bound(symbolic::zero());
×
284
    body_assumptions[indvar].upper_bound(symbolic::sub(map->num_iterations(), symbolic::one()));
×
285
};
×
286

287
void AssumptionsAnalysis::traverse(structured_control_flow::Sequence& root,
148✔
288
                                   analysis::AnalysisManager& analysis_manager) {
289
    std::list<structured_control_flow::ControlFlowNode*> queue = {&root};
148✔
290
    while (!queue.empty()) {
861✔
291
        auto current = queue.front();
713✔
292
        queue.pop_front();
713✔
293

294
        if (auto block_stmt = dynamic_cast<structured_control_flow::Block*>(current)) {
713✔
295
            this->visit_block(block_stmt, analysis_manager);
208✔
296
        } else if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(current)) {
713✔
297
            this->visit_sequence(sequence_stmt, analysis_manager);
332✔
298
            for (size_t i = 0; i < sequence_stmt->size(); i++) {
713✔
299
                queue.push_back(&sequence_stmt->at(i).first);
381✔
300
            }
381✔
301
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(current)) {
505✔
302
            this->visit_if_else(if_else_stmt, analysis_manager);
27✔
303
            for (size_t i = 0; i < if_else_stmt->size(); i++) {
73✔
304
                queue.push_back(&if_else_stmt->at(i).first);
46✔
305
            }
46✔
306
        } else if (auto while_stmt = dynamic_cast<structured_control_flow::While*>(current)) {
173✔
307
            this->visit_while(while_stmt, analysis_manager);
7✔
308
            queue.push_back(&while_stmt->root());
7✔
309
        } else if (auto for_stmt = dynamic_cast<structured_control_flow::For*>(current)) {
146✔
310
            this->visit_for(for_stmt, analysis_manager);
131✔
311
            queue.push_back(&for_stmt->root());
131✔
312
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(current)) {
139✔
313
            this->visit_map(map_stmt, analysis_manager);
×
314
            queue.push_back(&map_stmt->root());
×
315
        }
×
316
    }
317
};
148✔
318

319
void AssumptionsAnalysis::run(analysis::AnalysisManager& analysis_manager) {
148✔
320
    this->assumptions_.clear();
148✔
321

322
    // Add sdfg assumptions
323
    this->assumptions_.insert({&sdfg_.root(), symbolic::Assumptions()});
148✔
324

325
    // Add additional assumptions
326
    for (auto& entry : this->additional_assumptions_) {
148✔
327
        this->assumptions_[&sdfg_.root()][entry.first] = entry.second;
×
328
    }
329

330
    // Forward propagate for each node
331
    this->traverse(sdfg_.root(), analysis_manager);
148✔
332
};
148✔
333

334
const symbolic::Assumptions AssumptionsAnalysis::get(structured_control_flow::ControlFlowNode& node,
557✔
335
                                                     bool include_trivial_bounds) {
336
    // Compute assumptions on the fly
337

338
    // Node-level assumptions
339
    symbolic::Assumptions assums;
557✔
340
    if (this->assumptions_.find(&node) != this->assumptions_.end()) {
557✔
341
        for (auto& entry : this->assumptions_[&node]) {
476✔
342
            assums.insert({entry.first, entry.second});
314✔
343
        }
344
    }
162✔
345

346
    AnalysisManager manager(this->sdfg_);
557✔
347
    auto& scope_analysis = manager.get<ScopeAnalysis>();
557✔
348

349
    auto scope = scope_analysis.parent_scope(&node);
557✔
350
    while (scope != nullptr) {
1,884✔
351
        // Don't overwrite lower scopes' assumptions
352
        if (this->assumptions_.find(scope) != this->assumptions_.end()) {
1,327✔
353
            for (auto& entry : this->assumptions_[scope]) {
1,467✔
354
                if (assums.find(entry.first) == assums.end()) {
614✔
355
                    assums.insert({entry.first, entry.second});
484✔
356
                }
484✔
357
            }
358
        }
853✔
359
        scope = scope_analysis.parent_scope(scope);
1,327✔
360
    }
361

362
    if (include_trivial_bounds) {
557✔
363
        for (auto& entry : sdfg_.assumptions()) {
1,698✔
364
            if (assums.find(entry.first) == assums.end()) {
1,278✔
365
                assums.insert({entry.first, entry.second});
583✔
366
            }
583✔
367
        }
368
    }
420✔
369

370
    return assums;
557✔
371
};
557✔
372

373
const symbolic::Assumptions AssumptionsAnalysis::get(structured_control_flow::ControlFlowNode& from,
98✔
374
                                                     structured_control_flow::ControlFlowNode& to,
375
                                                     bool include_trivial_bounds) {
376
    auto assums_from = this->get(from, include_trivial_bounds);
98✔
377
    auto assums_to = this->get(to, include_trivial_bounds);
98✔
378

379
    // Add lower scope assumptions to outer
380
    // ignore constants assumption
381
    for (auto& entry : assums_from) {
372✔
382
        if (assums_to.find(entry.first) == assums_to.end()) {
274✔
383
            auto assums_safe = assums_to;
×
384
            assums_safe.at(entry.first).constant(false);
×
385
            assums_to.insert({entry.first, assums_safe.at(entry.first)});
×
386
        } else {
×
387
            auto assums_safe = assums_to;
274✔
388
            assums_safe.at(entry.first).constant(entry.second.constant());
274✔
389
            assums_to[entry.first] = assums_safe.at(entry.first);
274✔
390
        }
274✔
391
    }
392

393
    return assums_to;
98✔
394
}
98✔
395

396
void AssumptionsAnalysis::add(symbolic::Assumptions& assums,
×
397
                              structured_control_flow::ControlFlowNode& node) {
398
    if (this->assumptions_.find(&node) == this->assumptions_.end()) {
×
399
        return;
×
400
    }
401

402
    for (auto& entry : this->assumptions_[&node]) {
×
403
        if (assums.find(entry.first) == assums.end()) {
×
404
            assums.insert({entry.first, entry.second});
×
405
        } else {
×
406
            assums[entry.first] = entry.second;
×
407
        }
408
    }
409
}
×
410

411
}  // namespace analysis
412
}  // 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