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

daisytuner / sdfglib / 16069945621

04 Jul 2025 08:56AM UTC coverage: 64.375% (-0.2%) from 64.606%
16069945621

push

github

web-flow
Merge pull request #137 from daisytuner/clang-format

runs clang-format on codebase

609 of 827 new or added lines in 63 files covered. (73.64%)

46 existing lines in 30 files now uncovered.

8578 of 13325 relevant lines covered (64.38%)

177.24 hits per line

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

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

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

10
namespace sdfg {
11
namespace transformations {
12

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

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

18
      };
4✔
19

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

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

25
    auto& assumptions_analysis = analysis_manager.get<analysis::AssumptionsAnalysis>();
4✔
26
    if (!analysis::LoopAnalysis::is_contiguous(&loop_, assumptions_analysis)) {
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
NEW
85
            if (symbolic::eq(condition_1, symbolic::Eq(indvar, symbolic::sub(bound, symbolic::one())))) {
×
86
                return true;
×
87
            }
88

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

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

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

103
    return false;
×
104
};
4✔
105

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

109
    auto& body = loop_.root();
4✔
110
    auto indvar = loop_.indvar();
4✔
111

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

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

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

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

147
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
4✔
148
    auto parent = static_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(&loop_));
4✔
149

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

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

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

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

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

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

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

257
    auto& if_else_slice = 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, const nlohmann::json& desc) {
1✔
296
    auto loop_id = desc["loop_element_id"].get<size_t>();
1✔
297
    auto element = builder.find_element_by_id(loop_id);
1✔
298
    if (!element) {
1✔
NEW
299
        throw InvalidTransformationDescriptionException("Element with ID " + std::to_string(loop_id) + " not found.");
×
300
    }
301
    auto loop = dynamic_cast<structured_control_flow::For*>(element);
1✔
302

303
    return LoopSlicing(*loop);
1✔
304
};
×
305

306
} // namespace transformations
307
} // 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

© 2025 Coveralls, Inc