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

daisytuner / sdfglib / 15744620952

18 Jun 2025 10:07PM UTC coverage: 64.591% (+0.005%) from 64.586%
15744620952

push

github

web-flow
Merge pull request #86 from daisytuner/add-optimizer

Add optimizer functionality

122 of 201 new or added lines in 9 files covered. (60.7%)

2 existing lines in 1 file now uncovered.

8141 of 12604 relevant lines covered (64.59%)

153.84 hits per line

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

54.73
/src/transformations/loop_slicing.cpp
1
#include "sdfg/transformations/loop_slicing.h"
2

3
#include "sdfg/analysis/data_parallelism_analysis.h"
4
#include "sdfg/analysis/loop_analysis.h"
5
#include "sdfg/analysis/scope_analysis.h"
6
#include "sdfg/deepcopy/structured_sdfg_deep_copy.h"
7
#include "sdfg/structured_control_flow/structured_loop.h"
8

9
namespace sdfg {
10
namespace transformations {
11

12
enum class LoopSlicingType { Init, Bound, Split_Lt, Split_Le };
13

14
LoopSlicing::LoopSlicing(structured_control_flow::StructuredLoop& loop)
4✔
15
    : loop_(loop) {
4✔
16

17
      };
4✔
18

19
std::string LoopSlicing::name() const { return "LoopSlicing"; };
2✔
20

21
bool LoopSlicing::can_be_applied(builder::StructuredSDFGBuilder& builder,
4✔
22
                                 analysis::AnalysisManager& analysis_manager) {
23
    auto& sdfg = builder.subject();
4✔
24

25
    auto& loop_analysis = analysis_manager.get<analysis::LoopAnalysis>();
4✔
26
    if (!loop_analysis.is_contiguous(&loop_)) {
4✔
27
        return false;
×
28
    }
29

30
    // Collect moving symbols
31
    std::unordered_set<std::string> moving_symbols;
4✔
32
    auto& all_users = analysis_manager.get<analysis::Users>();
4✔
33
    auto& body = loop_.root();
4✔
34
    analysis::UsersView users(all_users, body);
4✔
35
    for (auto& entry : users.writes()) {
4✔
36
        auto& type = sdfg.type(entry->container());
×
37
        if (!dynamic_cast<const types::Scalar*>(&type)) {
×
38
            continue;
×
39
        }
40
        if (!types::is_integer(type.primitive_type())) {
×
41
            continue;
×
42
        }
43
        moving_symbols.insert(entry->container());
×
44
    }
45

46
    // Check if loop is sliced by if-elses
47
    auto indvar = loop_.indvar();
4✔
48
    for (size_t i = 0; i < body.size(); i++) {
4✔
49
        auto child = body.at(i);
4✔
50
        if (auto if_else = dynamic_cast<structured_control_flow::IfElse*>(&child.first)) {
4✔
51
            if (child.second.assignments().size() > 0) {
4✔
52
                return false;
×
53
            }
54
            if (if_else->size() != 2) {
4✔
55
                return false;
×
56
            }
57

58
            // Validate condition
59
            auto branch_1 = if_else->at(0);
4✔
60
            auto condition_1 = branch_1.second;
4✔
61
            if (!symbolic::uses(condition_1, indvar)) {
4✔
62
                return false;
×
63
            }
64
            auto condition_2 = if_else->at(1).second;
4✔
65
            if (!symbolic::eq(condition_1, condition_2->logical_not())) {
4✔
66
                return false;
×
67
            }
68
            for (auto& atom : symbolic::atoms(condition_1)) {
8✔
69
                auto sym = SymEngine::rcp_static_cast<const SymEngine::Symbol>(atom);
4✔
70
                if (moving_symbols.find(sym->get_name()) != moving_symbols.end()) {
4✔
71
                    return false;
×
72
                }
73
            }
4✔
74
            auto bound = analysis::DataParallelismAnalysis::bound(loop_);
4✔
75
            if (bound == SymEngine::null) {
4✔
76
                return false;
×
77
            }
78

79
            // Case: indvar == init
80
            if (symbolic::eq(condition_1, symbolic::Eq(indvar, loop_.init()))) {
4✔
81
                return true;
4✔
82
            }
83

84
            // Case: indvar == bound - 1
85
            if (symbolic::eq(condition_1,
×
86
                             symbolic::Eq(indvar, symbolic::sub(bound, symbolic::one())))) {
×
87
                return true;
×
88
            }
89

90
            // Case: indvar < new_bound
91
            if (SymEngine::is_a<SymEngine::StrictLessThan>(*condition_1)) {
×
92
                return true;
×
93
            }
94

95
            // Case: indvar <= new_bound
96
            if (SymEngine::is_a<SymEngine::LessThan>(*condition_1)) {
×
97
                return true;
×
98
            }
99

100
            return false;
×
101
        }
4✔
102
    }
×
103

104
    return false;
×
105
};
4✔
106

107
void LoopSlicing::apply(builder::StructuredSDFGBuilder& builder,
4✔
108
                        analysis::AnalysisManager& analysis_manager) {
109
    auto& sdfg = builder.subject();
4✔
110

111
    auto& body = loop_.root();
4✔
112
    auto indvar = loop_.indvar();
4✔
113

114
    // Collect loop locals
115
    auto& users = analysis_manager.get<analysis::Users>();
4✔
116
    auto locals = users.locals(body);
4✔
117

118
    // Find the if-else that slices the loop
119
    structured_control_flow::IfElse* if_else = nullptr;
4✔
120
    size_t if_else_index = 0;
4✔
121
    for (size_t i = 0; i < body.size(); i++) {
4✔
122
        auto child = body.at(i);
4✔
123
        auto if_else_ = dynamic_cast<structured_control_flow::IfElse*>(&child.first);
4✔
124
        if (if_else_) {
4✔
125
            if_else_index = i;
4✔
126
            if_else = if_else_;
4✔
127
            break;
4✔
128
        }
129
    }
×
130
    if (if_else == nullptr) {
4✔
131
        throw InvalidSDFGException("LoopSlicing: Expected IfElse");
×
132
    }
133

134
    auto branch_1 = if_else->at(0);
4✔
135
    auto condition_1 = branch_1.second;
4✔
136
    auto bound = analysis::DataParallelismAnalysis::bound(loop_);
4✔
137

138
    LoopSlicingType slice_type = LoopSlicingType::Init;
4✔
139
    if (symbolic::eq(condition_1, symbolic::Eq(indvar, loop_.init()))) {
4✔
140
        slice_type = LoopSlicingType::Init;
4✔
141
    } else if (symbolic::eq(condition_1,
4✔
142
                            symbolic::Eq(indvar, symbolic::sub(bound, symbolic::one())))) {
×
143
        slice_type = LoopSlicingType::Bound;
×
144
    } else if (SymEngine::is_a<SymEngine::StrictLessThan>(*condition_1)) {
×
145
        slice_type = LoopSlicingType::Split_Lt;
×
146
    } else if (SymEngine::is_a<SymEngine::LessThan>(*condition_1)) {
×
147
        slice_type = LoopSlicingType::Split_Le;
×
148
    }
×
149

150
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
4✔
151
    auto parent =
4✔
152
        static_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(&loop_));
4✔
153

154
    // Slice loop
155
    auto indvar_slice_str = builder.find_new_name(indvar->get_name());
4✔
156
    builder.add_container(indvar_slice_str, sdfg.type(indvar->get_name()));
4✔
157
    auto indvar_slice = SymEngine::symbol(indvar_slice_str);
4✔
158
    structured_control_flow::For* loop_slice = nullptr;
4✔
159
    structured_control_flow::For* loop_slice_2 = nullptr;
4✔
160
    switch (slice_type) {
4✔
161
        case LoopSlicingType::Init: {
162
            auto init_slice = loop_.init();
4✔
163
            auto condition_slice =
164
                symbolic::Lt(indvar_slice, symbolic::add(loop_.init(), symbolic::one()));
4✔
165
            auto increment_slice = symbolic::add(indvar_slice, symbolic::one());
4✔
166
            loop_slice = &builder
8✔
167
                              .add_for_before(*parent, loop_, indvar_slice, condition_slice,
4✔
168
                                              init_slice, increment_slice)
169
                              .first;
4✔
170
            loop_slice_2 =
4✔
171
                &builder
4✔
172
                     .add_for_after(*parent, loop_, loop_.indvar(), loop_.condition(),
8✔
173
                                    symbolic::add(loop_.init(), symbolic::one()), loop_.update())
4✔
174
                     .first;
4✔
175
            break;
176
        }
4✔
177
        case LoopSlicingType::Bound: {
178
            auto init_slice = symbolic::sub(bound, symbolic::one());
×
179
            auto condition_slice = symbolic::subs(loop_.condition(), loop_.indvar(), indvar_slice);
×
180
            auto increment_slice = symbolic::add(indvar_slice, symbolic::one());
×
181
            loop_slice = &builder
×
NEW
182
                              .add_for_after(*parent, loop_, indvar_slice, condition_slice,
×
183
                                             init_slice, increment_slice)
184
                              .first;
×
185
            loop_slice_2 = &builder
×
186
                                .add_for_before(
×
NEW
187
                                    *parent, loop_, loop_.indvar(),
×
188
                                    symbolic::Lt(loop_.indvar(),
×
189
                                                 symbolic::sub(loop_.condition(), symbolic::one())),
×
190
                                    loop_.init(), loop_.update())
×
191
                                .first;
×
192
            break;
193
        }
×
194
        case LoopSlicingType::Split_Lt: {
195
            auto init_slice = loop_.init();
×
196
            auto condition_slice =
197
                symbolic::And(symbolic::subs(condition_1, indvar, indvar_slice),
×
198
                              symbolic::subs(loop_.condition(), indvar, indvar_slice));
×
199
            auto increment_slice = symbolic::add(indvar_slice, symbolic::one());
×
200
            loop_slice = &builder
×
NEW
201
                              .add_for_before(*parent, loop_, indvar_slice, condition_slice,
×
202
                                              init_slice, increment_slice)
203
                              .first;
×
204

205
            auto condition_bound =
206
                SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(condition_1);
×
207
            auto condition_bound_args = condition_bound->get_args();
×
208
            auto condition_bound_args_bound = condition_bound_args.at(0);
×
209
            if (symbolic::eq(condition_bound_args_bound, loop_.indvar())) {
×
210
                condition_bound_args_bound = condition_bound_args.at(1);
×
211
            }
×
212

213
            loop_slice_2 = &builder
×
NEW
214
                                .add_for_after(*parent, loop_, loop_.indvar(), loop_.condition(),
×
215
                                               condition_bound_args_bound, loop_.update())
×
216
                                .first;
×
217
            break;
218
        }
×
219
        case LoopSlicingType::Split_Le: {
220
            auto init_slice = loop_.init();
×
221
            auto condition_slice =
222
                symbolic::And(symbolic::subs(condition_1, indvar, indvar_slice),
×
223
                              symbolic::subs(loop_.condition(), indvar, indvar_slice));
×
224
            auto increment_slice = symbolic::add(indvar_slice, symbolic::one());
×
225
            loop_slice = &builder
×
NEW
226
                              .add_for_before(*parent, loop_, indvar_slice, condition_slice,
×
227
                                              init_slice, increment_slice)
228
                              .first;
×
229

230
            auto condition_bound =
231
                SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(condition_1);
×
232
            auto condition_bound_args = condition_bound->get_args();
×
233
            auto condition_bound_args_bound = condition_bound_args.at(0);
×
234
            if (symbolic::eq(condition_bound_args_bound, loop_.indvar())) {
×
235
                condition_bound_args_bound = condition_bound_args.at(1);
×
236
            }
×
237

238
            loop_slice_2 =
×
239
                &builder
×
NEW
240
                     .add_for_after(*parent, loop_, loop_.indvar(), loop_.condition(),
×
241
                                    symbolic::add(condition_bound_args_bound, symbolic::one()),
×
242
                                    loop_.update())
×
243
                     .first;
×
244
            break;
245
        }
×
246
    }
247

248
    // Move loop locals to the new loop slice
249
    auto& body_slice = loop_slice->root();
4✔
250

251
    deepcopy::StructuredSDFGDeepCopy deep_copy(builder, body_slice, body);
4✔
252
    deep_copy.copy();
4✔
253
    auto& body_body_slice =
4✔
254
        dynamic_cast<structured_control_flow::Sequence&>(body_slice.at(0).first);
4✔
255

256
    auto& if_else_slice =
4✔
257
        dynamic_cast<structured_control_flow::IfElse&>(body_body_slice.at(if_else_index).first);
4✔
258
    auto& slice = builder.add_sequence_before(body_body_slice, if_else_slice).first;
4✔
259

260
    deepcopy::StructuredSDFGDeepCopy deep_copy_slice(builder, slice, if_else_slice.at(0).first);
4✔
261
    deep_copy_slice.copy();
4✔
262

263
    builder.remove_child(body_body_slice, if_else_index + 1);
4✔
264

265
    body_body_slice.replace(indvar, indvar_slice);
4✔
266

267
    // Update remaining loop
268
    builder.remove_case(*if_else, 0);
4✔
269

270
    auto& else_slice = builder.add_sequence_before(body, *if_else).first;
4✔
271
    deepcopy::StructuredSDFGDeepCopy deep_copy_else(builder, else_slice, if_else->at(0).first);
4✔
272
    deep_copy_else.copy();
4✔
273

274
    builder.remove_child(body, if_else_index + 1);
4✔
275

276
    // Rename all loop-local variables to break artificial dependencies
277
    for (auto& local : locals) {
4✔
278
        auto new_local = builder.find_new_name(local);
×
279
        builder.add_container(new_local, sdfg.type(local));
×
280
        loop_slice->root().replace(symbolic::symbol(local), symbolic::symbol(new_local));
×
281
    }
×
282

283
    // Move loop locals to the new loop
284
    builder.insert_children(loop_slice_2->root(), loop_.root(), 0);
4✔
285
    builder.remove_child(*parent, loop_);
4✔
286

287
    analysis_manager.invalidate_all();
4✔
288
};
4✔
289

290
void LoopSlicing::to_json(nlohmann::json& j) const {
2✔
291
    j["transformation_type"] = this->name();
2✔
292
    j["loop_element_id"] = loop_.element_id();
2✔
293
};
2✔
294

295
LoopSlicing LoopSlicing::from_json(builder::StructuredSDFGBuilder& builder,
1✔
296
                                   const nlohmann::json& desc) {
297
    auto loop_id = desc["loop_element_id"].get<size_t>();
1✔
298
    auto element = builder.find_element_by_id(loop_id);
1✔
299
    if (!element) {
1✔
NEW
300
        throw InvalidTransformationDescriptionException("Element with ID " +
×
NEW
301
                                                        std::to_string(loop_id) + " not found.");
×
302
    }
303
    auto loop = dynamic_cast<structured_control_flow::For*>(element);
1✔
304

305
    return LoopSlicing(*loop);
1✔
NEW
306
};
×
307

308
}  // namespace transformations
309
}  // 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