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

daisytuner / sdfglib / 15798071638

21 Jun 2025 05:35PM UTC coverage: 63.84% (-0.2%) from 64.022%
15798071638

Pull #97

github

web-flow
Merge bc9208a8e into b1dccd749
Pull Request #97: adds constant assumptions to handle complex symbolic expressions

35 of 46 new or added lines in 6 files covered. (76.09%)

26 existing lines in 4 files now uncovered.

8054 of 12616 relevant lines covered (63.84%)

152.19 hits per line

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

81.01
/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,
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
    if (body_assumptions.find(indvar) == body_assumptions.end()) {
131✔
219
        body_assumptions.insert({indvar, symbolic::Assumption(indvar)});
131✔
220
    }
131✔
221

222
    // Assumption 1: indvar moves according to update
223
    body_assumptions[indvar].constant(true);
131✔
224

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

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

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

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

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

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

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

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

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

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

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

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

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

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

338
    if (include_trivial_bounds) {
557✔
339
        for (auto& entry : sdfg_.assumptions()) {
1,698✔
340
            if (assums.find(entry.first) == assums.end()) {
1,278✔
341
                assums.insert({entry.first, entry.second});
876✔
342
            }
876✔
343
        }
344
    }
420✔
345

346
    return assums;
557✔
347
};
557✔
348

349
const symbolic::Assumptions AssumptionsAnalysis::get(structured_control_flow::ControlFlowNode& from,
98✔
350
                                                     structured_control_flow::ControlFlowNode& to,
351
                                                     bool include_trivial_bounds) {
352
    auto assums_from = this->get(from, include_trivial_bounds);
98✔
353
    auto assums_to = this->get(to, include_trivial_bounds);
98✔
354

355
    // Add lower scope assumptions to outer
356
    // ignore constants assumption
357
    for (auto& entry : assums_from) {
372✔
358
        if (assums_to.find(entry.first) == assums_to.end()) {
274✔
NEW
359
            auto assums_safe = assums_to;
×
NEW
360
            assums_safe.at(entry.first).constant(false);
×
NEW
361
            assums_to.insert({entry.first, assums_safe.at(entry.first)});
×
NEW
362
        } else {
×
363
            auto assums_safe = assums_to;
274✔
364
            assums_safe.at(entry.first).constant(entry.second.constant());
274✔
365
            assums_to[entry.first] = assums_safe.at(entry.first);
274✔
366
        }
274✔
367
    }
368

369
    return assums_to;
98✔
370
}
98✔
371

UNCOV
372
void AssumptionsAnalysis::add(symbolic::Assumptions& assums,
×
373
                              structured_control_flow::ControlFlowNode& node) {
374
    if (this->assumptions_.find(&node) == this->assumptions_.end()) {
×
375
        return;
×
376
    }
377

378
    for (auto& entry : this->assumptions_[&node]) {
×
379
        if (assums.find(entry.first) == assums.end()) {
×
380
            assums.insert({entry.first, entry.second});
×
381
        } else {
×
382
            assums[entry.first] = entry.second;
×
383
        }
384
    }
385
}
×
386

387
}  // namespace analysis
388
}  // 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