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

daisytuner / sdfglib / 20764569418

06 Jan 2026 10:50PM UTC coverage: 62.168% (+21.4%) from 40.764%
20764569418

push

github

web-flow
Merge pull request #433 from daisytuner/clang-coverage

updates clang coverage flags

14988 of 24109 relevant lines covered (62.17%)

88.57 hits per line

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

74.14
/src/analysis/flop_analysis.cpp
1
#include "sdfg/analysis/flop_analysis.h"
2
#include <cassert>
3
#include <cstddef>
4
#include <string>
5
#include <unordered_map>
6
#include <unordered_set>
7
#include <vector>
8
#include "sdfg/analysis/analysis.h"
9
#include "sdfg/analysis/assumptions_analysis.h"
10
#include "sdfg/analysis/loop_analysis.h"
11
#include "sdfg/analysis/scope_analysis.h"
12
#include "sdfg/analysis/users.h"
13
#include "sdfg/data_flow/tasklet.h"
14
#include "sdfg/exceptions.h"
15
#include "sdfg/structured_control_flow/block.h"
16
#include "sdfg/structured_control_flow/control_flow_node.h"
17
#include "sdfg/structured_control_flow/if_else.h"
18
#include "sdfg/structured_control_flow/return.h"
19
#include "sdfg/structured_control_flow/sequence.h"
20
#include "sdfg/structured_control_flow/structured_loop.h"
21
#include "sdfg/structured_control_flow/while.h"
22
#include "sdfg/structured_sdfg.h"
23
#include "sdfg/symbolic/assumptions.h"
24
#include "sdfg/symbolic/polynomials.h"
25
#include "sdfg/symbolic/symbolic.h"
26
#include "symengine/functions.h"
27
#include "symengine/symengine_rcp.h"
28
#include "symengine/visitor.h"
29

30
namespace sdfg {
31
namespace analysis {
32

33
/// An expression is a parameter expression if all its symbols are parameters
34
bool FlopAnalysis::is_parameter_expression(const symbolic::SymbolSet& parameters, const symbolic::Expression& expr) {
26✔
35
    if (expr.is_null()) {
26✔
36
        return false;
×
37
    }
×
38
    for (auto& sym : symbolic::atoms(expr)) {
26✔
39
        if (!parameters.contains(sym)) {
22✔
40
            return false;
4✔
41
        }
4✔
42
    }
22✔
43
    return true;
22✔
44
}
26✔
45

46
symbolic::ExpressionSet FlopAnalysis::
47
    choose_bounds(const symbolic::SymbolSet& parameters, const symbolic::ExpressionSet& bounds) {
4✔
48
    symbolic::ExpressionSet result;
4✔
49
    for (auto& bound : bounds) {
4✔
50
        if (symbolic::eq(bound, SymEngine::NegInf) || symbolic::eq(bound, SymEngine::Inf)) {
4✔
51
            // Skip infinities
52
            continue;
×
53
        } else if (SymEngine::is_a<SymEngine::Integer>(*bound)) {
4✔
54
            // Collect integers
55
            result.insert(bound);
×
56
        } else if (!symbolic::has_dynamic_sizeof(bound) && this->is_parameter_expression(parameters, bound)) {
4✔
57
            // Collect parameter expressions if they do not contain dynamic_sizeof
58
            result.insert(bound);
4✔
59
        }
4✔
60
    }
4✔
61
    if (result.empty()) {
4✔
62
        // Fallback if no integers or parameter expressions were found
63
        return bounds;
×
64
    } else {
4✔
65
        return result;
4✔
66
    }
4✔
67
}
4✔
68

69
symbolic::Expression FlopAnalysis::replace_loop_indices(
70
    const symbolic::SymbolSet& parameters, const symbolic::Expression expr, symbolic::Assumptions& assumptions
71
) {
43✔
72
    symbolic::Expression result = expr;
43✔
73
    auto atoms = symbolic::atoms(result);
43✔
74
    for (auto sym : atoms) {
43✔
75
        if (!assumptions.contains(sym)) continue;
18✔
76
        symbolic::Assumption assumption = assumptions.at(sym);
18✔
77
        if (!assumption.constant() || assumption.map().is_null()) continue;
18✔
78
        symbolic::Expression ub, lb;
6✔
79
        if (assumption.tight_upper_bound().is_null()) {
6✔
80
            auto bounds = this->choose_bounds(parameters, assumption.upper_bounds());
×
81
            if (bounds.empty()) {
×
82
                ub = assumption.upper_bound();
×
83
            } else {
×
84
                ub = SymEngine::min(std::vector<symbolic::Expression>(bounds.begin(), bounds.end()));
×
85
            }
×
86
        } else {
6✔
87
            ub = assumption.tight_upper_bound();
6✔
88
        }
6✔
89
        if (assumption.tight_lower_bound().is_null()) {
6✔
90
            auto bounds = this->choose_bounds(parameters, assumption.lower_bounds());
×
91
            if (bounds.empty()) {
×
92
                lb = assumption.lower_bound();
×
93
            } else {
×
94
                lb = SymEngine::max(std::vector<symbolic::Expression>(bounds.begin(), bounds.end()));
×
95
            }
×
96
        } else {
6✔
97
            lb = assumption.tight_lower_bound();
6✔
98
        }
6✔
99
        result = symbolic::subs(result, sym, symbolic::div(symbolic::sub(ub, lb), symbolic::integer(2)));
6✔
100
        this->precise_ = false;
6✔
101
    }
6✔
102
    return result;
43✔
103
}
43✔
104

105
symbolic::SymbolSet FlopAnalysis::
106
    get_scope_parameters(const structured_control_flow::ControlFlowNode& scope, AnalysisManager& analysis_manager) {
19✔
107
    auto& users_analysis = analysis_manager.get<Users>();
19✔
108
    analysis::UsersView users_view(users_analysis, scope);
19✔
109

110
    std::unordered_set<std::string> all_uses, illegal_uses;
19✔
111
    for (auto* user : users_view.uses()) {
144✔
112
        if (!sdfg_.exists(user->container())) {
144✔
113
            continue;
×
114
        }
×
115

116
        Use not_allowed;
144✔
117
        switch (this->sdfg_.type(user->container()).type_id()) {
144✔
118
            case types::TypeID::Scalar:
144✔
119
                not_allowed = Use::WRITE;
144✔
120
                break;
144✔
121
            case types::TypeID::Pointer:
×
122
            case types::TypeID::Array:
×
123
                not_allowed = Use::MOVE;
×
124
                break;
×
125
            default:
×
126
                continue;
×
127
        }
144✔
128
        all_uses.insert(user->container());
144✔
129
        if (user->use() == not_allowed) {
144✔
130
            illegal_uses.insert(user->container());
48✔
131
        }
48✔
132
    }
144✔
133

134
    symbolic::SymbolSet parameters;
19✔
135
    for (auto& container : all_uses) {
99✔
136
        if (!illegal_uses.contains(container)) {
99✔
137
            parameters.insert(symbolic::symbol(container));
65✔
138
        }
65✔
139
    }
99✔
140

141
    return parameters;
19✔
142
}
19✔
143

144
symbolic::Expression FlopAnalysis::visit(structured_control_flow::ControlFlowNode& node, AnalysisManager& analysis_manager) {
17✔
145
    if (auto sequence = dynamic_cast<structured_control_flow::Sequence*>(&node)) {
17✔
146
        return this->visit_sequence(*sequence, analysis_manager);
×
147
    } else if (auto block = dynamic_cast<structured_control_flow::Block*>(&node)) {
17✔
148
        return this->visit_block(*block, analysis_manager);
9✔
149
    } else if (auto structured_loop = dynamic_cast<structured_control_flow::StructuredLoop*>(&node)) {
9✔
150
        return this->visit_structured_loop(*structured_loop, analysis_manager);
5✔
151
    } else if (auto if_else = dynamic_cast<structured_control_flow::IfElse*>(&node)) {
5✔
152
        return this->visit_if_else(*if_else, analysis_manager);
1✔
153
    } else if (auto while_loop = dynamic_cast<structured_control_flow::While*>(&node)) {
2✔
154
        return this->visit_while(*while_loop, analysis_manager);
1✔
155
    } else if (dynamic_cast<structured_control_flow::Return*>(&node) ||
1✔
156
               dynamic_cast<structured_control_flow::Break*>(&node) ||
1✔
157
               dynamic_cast<structured_control_flow::Continue*>(&node)) {
1✔
158
        this->flops_[&node] = symbolic::zero();
1✔
159
        return symbolic::zero();
1✔
160
    } else {
1✔
161
        this->flops_[&node] = SymEngine::null;
×
162
        this->precise_ = false;
×
163
        return SymEngine::null;
×
164
    }
×
165
}
17✔
166

167
symbolic::Expression FlopAnalysis::
168
    visit_sequence(structured_control_flow::Sequence& sequence, AnalysisManager& analysis_manager) {
16✔
169
    symbolic::Expression result = symbolic::zero();
16✔
170
    bool is_null = false;
16✔
171

172
    for (size_t i = 0; i < sequence.size(); i++) {
33✔
173
        symbolic::Expression child = this->visit(sequence.at(i).first, analysis_manager);
17✔
174
        if (child.is_null()) {
17✔
175
            is_null = true;
1✔
176
        }
1✔
177
        if (!is_null) {
17✔
178
            result = symbolic::add(result, child);
16✔
179
        }
16✔
180
    }
17✔
181

182
    if (is_null) {
16✔
183
        this->flops_[&sequence] = SymEngine::null;
1✔
184
        this->precise_ = false;
1✔
185
        return SymEngine::null;
1✔
186
    }
1✔
187
    this->flops_[&sequence] = result;
15✔
188
    return result;
15✔
189
}
16✔
190

191
symbolic::Expression FlopAnalysis::visit_block(structured_control_flow::Block& block, AnalysisManager& analysis_manager) {
9✔
192
    auto& dfg = block.dataflow();
9✔
193

194
    symbolic::Expression tasklets_result = symbolic::zero();
9✔
195
    for (auto tasklet : dfg.tasklets()) {
9✔
196
        if (tasklet->code() == data_flow::TaskletCode::fp_fma) {
9✔
197
            tasklets_result = symbolic::add(tasklets_result, symbolic::integer(2));
3✔
198
        } else if (data_flow::is_floating_point(tasklet->code())) {
6✔
199
            tasklets_result = symbolic::add(tasklets_result, symbolic::one());
6✔
200
        }
6✔
201
    }
9✔
202

203
    symbolic::Expression libnodes_result = symbolic::zero();
9✔
204
    for (auto libnode : dfg.library_nodes()) {
9✔
205
        symbolic::Expression tmp = libnode->flop();
1✔
206
        if (tmp.is_null()) {
1✔
207
            this->precise_ = false;
×
208
            return SymEngine::null;
×
209
        }
×
210
        libnodes_result = symbolic::add(libnodes_result, tmp);
1✔
211
    }
1✔
212

213
    // Determine scope parameters
214
    symbolic::SymbolSet parameters = this->get_scope_parameters(block, analysis_manager);
9✔
215

216
    // Filter the loop index variables in libnodes_result, and replace them by (upper_bound - lower_bound) / 2
217
    auto& assumptions_analysis = analysis_manager.get<AssumptionsAnalysis>();
9✔
218
    auto block_assumptions = assumptions_analysis.get(block);
9✔
219
    libnodes_result = this->replace_loop_indices(parameters, libnodes_result, block_assumptions);
9✔
220

221
    symbolic::Expression result = symbolic::add(tasklets_result, libnodes_result);
9✔
222
    this->flops_[&block] = result;
9✔
223
    return result;
9✔
224
}
9✔
225

226
symbolic::Expression FlopAnalysis::
227
    visit_structured_loop(structured_control_flow::StructuredLoop& loop, AnalysisManager& analysis_manager) {
5✔
228
    symbolic::Expression child = this->visit_sequence(loop.root(), analysis_manager);
5✔
229
    if (child.is_null()) {
5✔
230
        this->precise_ = false;
×
231
        return SymEngine::null;
×
232
    }
×
233

234
    auto& scope_analysis = analysis_manager.get<ScopeAnalysis>();
5✔
235
    structured_control_flow::ControlFlowNode* parent = scope_analysis.parent_scope(&loop);
5✔
236
    if (!parent) {
5✔
237
        throw InvalidSDFGException("FlopAnalysis: Could not find parent scope of structured loop");
×
238
    }
×
239
    this->flops_[&loop] = this->visit_structured_loop_with_scope(loop, analysis_manager, loop, child);
5✔
240
    return this->visit_structured_loop_with_scope(loop, analysis_manager, *parent, child);
5✔
241
}
5✔
242

243
symbolic::Expression FlopAnalysis::visit_structured_loop_with_scope(
244
    structured_control_flow::StructuredLoop& loop,
245
    AnalysisManager& analysis_manager,
246
    structured_control_flow::ControlFlowNode& scope,
247
    symbolic::Expression child_expr
248
) {
10✔
249
    // Determine scope parameters
250
    symbolic::SymbolSet parameters = this->get_scope_parameters(scope, analysis_manager);
10✔
251

252
    // Require existance of assumptions for the loop indvar
253
    auto indvar = loop.indvar();
10✔
254
    auto& assumptions_analysis = analysis_manager.get<AssumptionsAnalysis>();
10✔
255
    auto loop_assumptions = assumptions_analysis.get(loop.root());
10✔
256
    if (!loop_assumptions.contains(indvar)) {
10✔
257
        this->precise_ = false;
×
258
        return SymEngine::null;
×
259
    }
×
260
    bool done;
10✔
261

262
    // Determine initial value of loop
263
    symbolic::Expression init = SymEngine::null;
10✔
264
    done = false;
10✔
265
    if (!loop_assumptions[indvar].tight_lower_bound().is_null()) {
10✔
266
        init = this->replace_loop_indices(parameters, loop_assumptions[indvar].tight_lower_bound(), loop_assumptions);
10✔
267
        done = this->is_parameter_expression(parameters, init);
10✔
268
    }
10✔
269
    if (!done && !symbolic::eq(loop_assumptions[indvar].lower_bound(), SymEngine::NegInf)) {
10✔
270
        auto bounds = this->choose_bounds(parameters, loop_assumptions[indvar].lower_bounds());
2✔
271
        if (!bounds.empty()) {
2✔
272
            init = this->replace_loop_indices(
2✔
273
                parameters,
2✔
274
                SymEngine::max(std::vector<symbolic::Expression>(bounds.begin(), bounds.end())),
2✔
275
                loop_assumptions
2✔
276
            );
2✔
277
            this->precise_ = false;
2✔
278
            done = this->is_parameter_expression(parameters, init);
2✔
279
        }
2✔
280
    }
2✔
281
    if (!done) {
10✔
282
        init = this->replace_loop_indices(parameters, loop.init(), loop_assumptions);
2✔
283
        this->precise_ = false;
2✔
284
    }
2✔
285
    if (init.is_null()) {
10✔
286
        this->precise_ = false;
×
287
        return SymEngine::null;
×
288
    }
×
289

290
    // Determine bound of loop
291
    symbolic::Expression bound;
10✔
292
    done = false;
10✔
293
    if (!loop_assumptions[indvar].tight_upper_bound().is_null()) {
10✔
294
        bound = this->replace_loop_indices(parameters, loop_assumptions[indvar].tight_upper_bound(), loop_assumptions);
8✔
295
        done = this->is_parameter_expression(parameters, bound);
8✔
296
    }
8✔
297
    if (!done && !symbolic::eq(loop_assumptions[indvar].upper_bound(), SymEngine::Inf)) {
10✔
298
        auto bounds = this->choose_bounds(parameters, loop_assumptions[indvar].upper_bounds());
2✔
299
        if (!bounds.empty()) {
2✔
300
            bound = this->replace_loop_indices(
2✔
301
                parameters,
2✔
302
                SymEngine::min(std::vector<symbolic::Expression>(bounds.begin(), bounds.end())),
2✔
303
                loop_assumptions
2✔
304
            );
2✔
305
            this->precise_ = false;
2✔
306
            done = this->is_parameter_expression(parameters, bound);
2✔
307
        }
2✔
308
    }
2✔
309
    if (!done) {
10✔
310
        auto canonical_bound = LoopAnalysis::canonical_bound(&loop, assumptions_analysis);
×
311
        if (!canonical_bound.is_null()) {
×
312
            bound =
×
313
                this->replace_loop_indices(parameters, symbolic::sub(canonical_bound, symbolic::one()), loop_assumptions);
×
314
            this->precise_ = false;
×
315
        }
×
316
    }
×
317
    if (bound.is_null()) {
10✔
318
        this->precise_ = false;
×
319
        return SymEngine::null;
×
320
    }
×
321

322
    // Determine stride of loop
323
    symbolic::SymbolVec symbols = {indvar};
10✔
324
    auto update_polynomial = symbolic::polynomial(loop.update(), symbols);
10✔
325
    if (update_polynomial.is_null()) {
10✔
326
        this->precise_ = false;
×
327
        return SymEngine::null;
×
328
    }
×
329
    auto update_coeffs = symbolic::affine_coefficients(update_polynomial, symbols);
10✔
330

331
    // For now, only allow polynomial of the form: 1 * indvar + n
332
    assert(update_coeffs.contains(indvar) && symbolic::eq(update_coeffs[indvar], symbolic::one()));
10✔
333
    symbolic::Expression stride =
10✔
334
        this->replace_loop_indices(parameters, update_coeffs[symbolic::symbol("__daisy_constant__")], loop_assumptions);
10✔
335

336
    return symbolic::mul(symbolic::div(symbolic::add(symbolic::sub(bound, init), symbolic::one()), stride), child_expr);
10✔
337
}
10✔
338

339
symbolic::Expression FlopAnalysis::
340
    visit_if_else(structured_control_flow::IfElse& if_else, AnalysisManager& analysis_manager) {
1✔
341
    if (if_else.size() == 0) {
1✔
342
        this->flops_[&if_else] = symbolic::zero();
×
343
        return symbolic::zero();
×
344
    }
×
345

346
    std::vector<symbolic::Expression> sub_flops;
1✔
347
    bool is_null = false;
1✔
348

349
    for (size_t i = 0; i < if_else.size(); i++) {
3✔
350
        symbolic::Expression child = this->visit_sequence(if_else.at(i).first, analysis_manager);
2✔
351
        if (child.is_null()) {
2✔
352
            is_null = true;
×
353
        }
×
354
        if (!is_null) {
2✔
355
            sub_flops.push_back(child);
2✔
356
        }
2✔
357
    }
2✔
358

359
    this->precise_ = false;
1✔
360
    if (is_null) {
1✔
361
        this->flops_[&if_else] = SymEngine::null;
×
362
        return SymEngine::null;
×
363
    }
×
364
    symbolic::Expression result = SymEngine::max(sub_flops);
1✔
365
    this->flops_[&if_else] = result;
1✔
366
    return result;
1✔
367
}
1✔
368

369
symbolic::Expression FlopAnalysis::visit_while(structured_control_flow::While& loop, AnalysisManager& analysis_manager) {
1✔
370
    this->visit_sequence(loop.root(), analysis_manager);
1✔
371
    this->flops_[&loop] = SymEngine::null;
1✔
372
    this->precise_ = false;
1✔
373
    // Return null because there is now good way to simply estimate the FLOPs of a while loop
374
    return SymEngine::null;
1✔
375
}
1✔
376

377
void FlopAnalysis::run(AnalysisManager& analysis_manager) {
8✔
378
    this->flops_.clear();
8✔
379
    this->precise_ = true;
8✔
380

381
    auto& assumptions_analysis = analysis_manager.get<AssumptionsAnalysis>();
8✔
382

383
    this->visit_sequence(this->sdfg_.root(), analysis_manager);
8✔
384
}
8✔
385

386
FlopAnalysis::FlopAnalysis(StructuredSDFG& sdfg) : Analysis(sdfg) {}
8✔
387

388
bool FlopAnalysis::contains(const structured_control_flow::ControlFlowNode* node) {
31✔
389
    return this->flops_.contains(node);
31✔
390
}
31✔
391

392
symbolic::Expression FlopAnalysis::get(const structured_control_flow::ControlFlowNode* node) {
31✔
393
    auto it = this->flops_.find(node);
31✔
394
    if (it != this->flops_.end()) {
31✔
395
        return it->second;
31✔
396
    } else {
31✔
397
        return SymEngine::null;
×
398
    }
×
399
}
31✔
400

401
std::unordered_map<const structured_control_flow::ControlFlowNode*, symbolic::Expression> FlopAnalysis::get() {
×
402
    return this->flops_;
×
403
}
×
404

405
bool FlopAnalysis::precise() { return this->precise_; }
8✔
406

407
symbolic::Expression FlopAnalysis::get_if_valid_for_codegen(const symbolic::Expression flops) {
×
408
    if (!flops.is_null()) {
×
409
        for (auto& atom : SymEngine::atoms<SymEngine::Basic>(*flops)) { // TODO this excludes anything including
×
410
                                                                        // Infinity, as long as its not filtered out at
411
                                                                        // the beginning of flop analysis (untested,
412
                                                                        // whether parsing can handle incomplete traces
413
                                                                        // (we emit null, for such)
414
            if (SymEngine::is_a<SymEngine::Infty>(*atom)) {
×
415
                return SymEngine::null;
×
416
            }
×
417
        }
×
418
        if (!symbolic::has_dynamic_sizeof(flops)) {
×
419
            return flops;
×
420
        }
×
421
    }
×
422
    return SymEngine::null;
×
423
}
×
424

425
} // namespace analysis
426
} // 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