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

daisytuner / docc / 25459771247

06 May 2026 08:37PM UTC coverage: 65.659% (+0.4%) from 65.223%
25459771247

push

github

web-flow
Merge pull request #704 from daisytuner/lcd-analysis

Refactors LoopCarriedDependencyAnalysis into standalone analysis

731 of 828 new or added lines in 13 files covered. (88.29%)

42 existing lines in 5 files now uncovered.

32175 of 49003 relevant lines covered (65.66%)

12934.86 hits per line

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

71.36
/opt/src/transformations/loop_interchange.cpp
1
#include "sdfg/transformations/loop_interchange.h"
2

3
#include <isl/ctx.h>
4
#include <isl/map.h>
5
#include <isl/options.h>
6
#include <isl/set.h>
7

8
#include "sdfg/analysis/data_dependency_analysis.h"
9
#include "sdfg/analysis/loop_carried_dependency_analysis.h"
10
#include "sdfg/analysis/scope_analysis.h"
11
#include "sdfg/exceptions.h"
12
#include "sdfg/structured_control_flow/for.h"
13
#include "sdfg/structured_control_flow/structured_loop.h"
14
#include "sdfg/symbolic/polynomials.h"
15

16
namespace sdfg {
17
namespace transformations {
18

19
/// Check that a 2D delta set is lex-non-negative in the post-interchange order.
20
/// `new_outer_dim` is the index (0 or 1) of the dimension that becomes the
21
/// new outer loop after interchange.
22
/// Returns false (unsafe) if any delta vector is lex-negative in the new order.
23
static bool is_interchange_legal_2d(const std::string& deltas_str, int new_outer_dim) {
3✔
24
    if (deltas_str.empty()) {
3✔
25
        return false;
×
26
    }
×
27

28
    isl_ctx* ctx = isl_ctx_alloc();
3✔
29
    isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
3✔
30

31
    isl_set* deltas = isl_set_read_from_str(ctx, deltas_str.c_str());
3✔
32
    if (!deltas) {
3✔
33
        isl_ctx_free(ctx);
×
34
        return false;
×
35
    }
×
36

37
    int n_dims = isl_set_dim(deltas, isl_dim_set);
3✔
38
    if (n_dims != 2) {
3✔
39
        isl_set_free(deltas);
×
40
        isl_ctx_free(ctx);
×
41
        return false;
×
42
    }
×
43

44
    // Build lex-negative constraint in post-interchange order.
45
    // If new_outer is dim0: lex-neg = { [x, y] : x < 0 or (x = 0 and y < 0) }
46
    // If new_outer is dim1: lex-neg = { [x, y] : y < 0 or (y = 0 and x < 0) }
47
    const char* lex_neg_str = (new_outer_dim == 0) ? "{ [x, y] : x < 0 or (x = 0 and y < 0) }"
3✔
48
                                                   : "{ [x, y] : y < 0 or (y = 0 and x < 0) }";
3✔
49

50
    isl_set* lex_neg = isl_set_read_from_str(ctx, lex_neg_str);
3✔
51
    isl_set* violation = isl_set_intersect(deltas, lex_neg);
3✔
52
    bool legal = isl_set_is_empty(violation);
3✔
53
    isl_set_free(violation);
3✔
54
    isl_ctx_free(ctx);
3✔
55

56
    return legal;
3✔
57
}
3✔
58

59
/// Check that a 1D delta set {[d]} has no negative values.
60
/// After interchange the inner loop becomes the outer, so we need d >= 0.
61
static bool is_interchange_legal_1d(const std::string& deltas_str) {
8✔
62
    if (deltas_str.empty()) {
8✔
63
        return false;
×
64
    }
×
65

66
    isl_ctx* ctx = isl_ctx_alloc();
8✔
67
    isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
8✔
68

69
    isl_set* deltas = isl_set_read_from_str(ctx, deltas_str.c_str());
8✔
70
    if (!deltas) {
8✔
71
        isl_ctx_free(ctx);
×
72
        return false;
×
73
    }
×
74

75
    int n_dims = isl_set_dim(deltas, isl_dim_set);
8✔
76
    if (n_dims != 1) {
8✔
77
        isl_set_free(deltas);
×
78
        isl_ctx_free(ctx);
×
79
        return false;
×
80
    }
×
81

82
    isl_set* neg = isl_set_read_from_str(ctx, "{ [x] : x < 0 }");
8✔
83
    isl_set* violation = isl_set_intersect(deltas, neg);
8✔
84
    bool legal = isl_set_is_empty(violation);
8✔
85
    isl_set_free(violation);
8✔
86
    isl_ctx_free(ctx);
8✔
87

88
    return legal;
8✔
89
}
8✔
90

91
/// Extract the upper bound from a condition of the form `indvar [+ offset] < expr`,
92
/// or `And(indvar < expr1, indvar < expr2, ...)`.
93
/// Returns the equivalent RHS such that `indvar < result` (using min for conjunctions).
94
/// Returns SymEngine::null if the condition is not extractable.
95
static symbolic::Expression
96
extract_strict_upper_bound(const symbolic::Condition& condition, const symbolic::Symbol& indvar) {
33✔
97
    if (SymEngine::is_a<SymEngine::StrictLessThan>(*condition)) {
33✔
98
        auto lt = SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(condition);
33✔
99
        auto lhs = lt->get_arg1();
33✔
100
        auto rhs = lt->get_arg2();
33✔
101
        if (symbolic::eq(lhs, indvar)) {
33✔
102
            return rhs;
23✔
103
        }
23✔
104
        // Handle: Lt(indvar + offset, bound) → indvar < bound - offset
105
        if (symbolic::uses(lhs, indvar->get_name()) && !symbolic::uses(rhs, indvar->get_name())) {
10✔
106
            auto offset = symbolic::sub(lhs, indvar);
10✔
107
            if (!symbolic::uses(offset, indvar->get_name())) {
10✔
108
                return symbolic::sub(rhs, offset);
10✔
109
            }
10✔
110
        }
10✔
111
    }
10✔
112
    // Handle: And(cond1, cond2, ...) → min of extracted bounds
113
    if (SymEngine::is_a<SymEngine::And>(*condition)) {
×
114
        auto conj = SymEngine::rcp_static_cast<const SymEngine::And>(condition);
×
115
        symbolic::Expression result = SymEngine::null;
×
116
        for (auto& arg : conj->get_container()) {
×
117
            auto bound = extract_strict_upper_bound(SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(arg), indvar);
×
118
            if (bound == SymEngine::null) return SymEngine::null;
×
119
            if (result == SymEngine::null) {
×
120
                result = bound;
×
121
            } else {
×
122
                result = symbolic::min(result, bound);
×
123
            }
×
124
        }
×
125
        return result;
×
126
    }
×
127
    return SymEngine::null;
×
128
}
×
129

130
/// Decompose `expr` as `coefficient * sym + constant` where coefficient is a
131
/// positive integer.  Returns the (coefficient, constant) pair on success, or
132
/// (null, null) when the expression is not affine in `sym` or the coefficient
133
/// is not a positive integer.
134
struct AffineDecomp {
135
    symbolic::Expression coefficient = SymEngine::null;
136
    symbolic::Expression constant = SymEngine::null;
137
    explicit operator bool() const { return coefficient != SymEngine::null; }
21✔
138
};
139

140
static AffineDecomp check_affine(const symbolic::Expression& expr, const symbolic::Symbol& sym) {
33✔
141
    symbolic::SymbolVec syms = {sym};
33✔
142
    auto poly = symbolic::polynomial(expr, syms);
33✔
143
    if (poly == SymEngine::null) return {};
33✔
144
    auto coeffs = symbolic::affine_coefficients(poly, syms);
33✔
145
    if (coeffs.empty()) return {};
33✔
146
    auto coeff = coeffs[sym];
33✔
147
    // Coefficient must be a positive integer
148
    if (!SymEngine::is_a<SymEngine::Integer>(*coeff)) return {};
33✔
149
    if (SymEngine::down_cast<const SymEngine::Integer&>(*coeff).as_int() <= 0) return {};
33✔
150
    return {coeff, coeffs[symbolic::symbol("__daisy_constant__")]};
27✔
151
}
33✔
152

153
LoopInterchange::LoopInterchange(
154
    structured_control_flow::StructuredLoop& outer_loop, structured_control_flow::StructuredLoop& inner_loop
155
)
156
    : outer_loop_(outer_loop), inner_loop_(inner_loop) {
199✔
157

158
      };
199✔
159

160
std::string LoopInterchange::name() const { return "LoopInterchange"; };
29✔
161

162
bool LoopInterchange::can_be_applied(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
184✔
163
    auto& outer_indvar = this->outer_loop_.indvar();
184✔
164

165
    // Check if inner bounds depend on outer loop
166
    auto inner_loop_init = this->inner_loop_.init();
184✔
167
    auto inner_loop_condition = this->inner_loop_.condition();
184✔
168
    auto inner_loop_update = this->inner_loop_.update();
184✔
169

170
    // Inner update must never depend on outer
171
    if (symbolic::uses(inner_loop_update, outer_indvar->get_name())) {
184✔
172
        return false;
×
173
    }
×
174

175
    bool inner_depends_on_outer = symbolic::uses(inner_loop_init, outer_indvar->get_name()) ||
184✔
176
                                  symbolic::uses(inner_loop_condition, outer_indvar->get_name());
184✔
177

178
    if (inner_depends_on_outer) {
184✔
179
        // Fourier-Motzkin elimination: only For-For
180
        if (dynamic_cast<structured_control_flow::Map*>(&outer_loop_) ||
36✔
181
            dynamic_cast<structured_control_flow::Map*>(&inner_loop_)) {
36✔
182
            return false;
24✔
183
        }
24✔
184
        // Outer loop must have unit step
185
        if (!symbolic::eq(outer_loop_.update(), symbolic::add(outer_loop_.indvar(), symbolic::integer(1)))) {
12✔
186
            return false;
×
187
        }
×
188
        // Inner loop must have a positive integer step
189
        auto inner_stride = symbolic::sub(inner_loop_.update(), inner_loop_.indvar());
12✔
190
        if (!SymEngine::is_a<SymEngine::Integer>(*inner_stride) ||
12✔
191
            SymEngine::down_cast<const SymEngine::Integer&>(*inner_stride).as_int() <= 0) {
12✔
192
            return false;
×
193
        }
×
194
        // Outer condition must be extractable as indvar < bound
195
        auto outer_bound = extract_strict_upper_bound(outer_loop_.condition(), outer_loop_.indvar());
12✔
196
        if (outer_bound == SymEngine::null) {
12✔
197
            return false;
×
198
        }
×
199
        // Inner init must be affine in outer indvar with positive integer coeff
200
        auto init_decomp = check_affine(inner_loop_init, outer_indvar);
12✔
201
        if (!init_decomp) {
12✔
202
            return false;
3✔
203
        }
3✔
204
        // Inner bound must be affine in outer indvar with positive integer coeff
205
        auto inner_bound = extract_strict_upper_bound(inner_loop_.condition(), inner_loop_.indvar());
9✔
206
        if (inner_bound == SymEngine::null) {
9✔
207
            return false;
×
208
        }
×
209
        auto bound_decomp = check_affine(inner_bound, outer_indvar);
9✔
210
        if (!bound_decomp) {
9✔
211
            return false;
3✔
212
        }
3✔
213
        // Both must have the same coefficient (ensures rectangular projection)
214
        if (!symbolic::eq(init_decomp.coefficient, bound_decomp.coefficient)) {
6✔
215
            return false;
×
216
        }
×
217
    }
6✔
218

219
    // Criterion: Outer loop must not have any outer blocks
220
    if (outer_loop_.root().size() > 1) {
154✔
221
        return false;
23✔
222
    }
23✔
223
    if (outer_loop_.root().at(0).second.assignments().size() > 0) {
131✔
224
        return false;
×
225
    }
×
226
    if (&outer_loop_.root().at(0).first != &inner_loop_) {
131✔
227
        return false;
×
228
    }
×
229

230
    // Criterion: Any of both loops is a map
231
    if (dynamic_cast<structured_control_flow::Map*>(&outer_loop_) ||
131✔
232
        dynamic_cast<structured_control_flow::Map*>(&inner_loop_)) {
131✔
233
        return true;
71✔
234
    }
71✔
235

236
    auto& users_analysis = analysis_manager.get<analysis::Users>();
60✔
237
    analysis::UsersView body_users(users_analysis, inner_loop_.root());
60✔
238
    if (!body_users.views().empty() || !body_users.moves().empty()) {
60✔
239
        // Views and moves may have complex semantics that we don't handle yet
240
        return false;
×
241
    }
×
242

243
    // For-For: check legality using dependence delta sets
244
    auto& lcd = analysis_manager.get<analysis::LoopCarriedDependencyAnalysis>();
60✔
245

246
    if (!lcd.available(outer_loop_) || !lcd.available(inner_loop_)) {
60✔
NEW
247
        return false;
×
NEW
248
    }
×
249

250
    std::string outer_indvar_name = outer_loop_.indvar()->get_name();
60✔
251
    std::string inner_indvar_name = inner_loop_.indvar()->get_name();
60✔
252

253
    // Check outer loop dependencies (2D delta sets: [d_outer, d_inner])
254
    auto& outer_deps = lcd.dependencies(outer_loop_);
60✔
255
    for (auto& dep : outer_deps) {
247✔
256
        // Skip dependencies on loop induction variables — structurally safe
257
        if (dep.first == outer_indvar_name || dep.first == inner_indvar_name) {
247✔
258
            continue;
60✔
259
        }
60✔
260
        auto& deltas = dep.second.deltas;
187✔
261
        if (deltas.empty) {
187✔
262
            continue;
×
263
        }
×
264
        if (deltas.dimensions.empty()) {
187✔
265
            // No loop dimensions — purely intra-iteration, safe for interchange
266
            continue;
174✔
267
        }
174✔
268
        if (deltas.deltas_str.empty()) {
13✔
269
            // Dependence exists but no isl info — conservative reject
270
            return false;
×
271
        }
×
272
        if (deltas.dimensions.size() == 2) {
13✔
273
            // Determine which dimension becomes the new outer (= current inner indvar)
274
            int new_outer_dim = -1;
6✔
275
            for (int d = 0; d < 2; d++) {
14✔
276
                if (deltas.dimensions[d] == inner_indvar_name) {
11✔
277
                    new_outer_dim = d;
3✔
278
                    break;
3✔
279
                }
3✔
280
            }
11✔
281
            if (new_outer_dim < 0) {
6✔
282
                // Inner indvar not found in dimensions — the dependency is between
283
                // nested loop iterations that don't involve the loops being interchanged.
284
                // This is safe because the nested loop order is preserved after interchange.
285
                continue;
3✔
286
            }
3✔
287
            if (!is_interchange_legal_2d(deltas.deltas_str, new_outer_dim)) {
3✔
288
                return false;
1✔
289
            }
1✔
290
        } else if (deltas.dimensions.size() == 1) {
7✔
291
            // Only outer dimension — after interchange becomes inner, always safe
292
        } else {
6✔
293
            // Multi-dimensional delta set (>2): check if outer/inner indvars are involved
294
            bool has_outer = false, has_inner = false;
1✔
295
            for (auto& dim : deltas.dimensions) {
4✔
296
                if (dim == outer_indvar_name) has_outer = true;
4✔
297
                if (dim == inner_indvar_name) has_inner = true;
4✔
298
            }
4✔
299
            if (!has_outer && !has_inner) {
1✔
300
                // Dependency is entirely on nested loop variables — safe for interchange
301
                continue;
1✔
302
            }
1✔
303
            if (!has_inner) {
×
304
                // Only outer indvar involved — after interchange becomes inner, always safe
305
                continue;
×
306
            }
×
307
            // Inner indvar is involved in multi-D delta set — use ISL to check legality
308
            // Find the inner dimension index and check non-negativity
309
            int inner_dim = -1;
×
310
            for (size_t d = 0; d < deltas.dimensions.size(); d++) {
×
311
                if (deltas.dimensions[d] == inner_indvar_name) {
×
312
                    inner_dim = static_cast<int>(d);
×
313
                    break;
×
314
                }
×
315
            }
×
316
            // Project out all other dimensions and check 1D legality on inner_dim
317
            isl_ctx* ctx = isl_ctx_alloc();
×
318
            isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
×
319
            isl_set* delta_set = isl_set_read_from_str(ctx, deltas.deltas_str.c_str());
×
320
            if (delta_set) {
×
321
                int n_dims = isl_set_dim(delta_set, isl_dim_set);
×
322
                // Project out all dims except inner_dim
323
                // First project out dims after inner_dim
324
                if (inner_dim + 1 < n_dims) {
×
325
                    delta_set = isl_set_project_out(delta_set, isl_dim_set, inner_dim + 1, n_dims - inner_dim - 1);
×
326
                }
×
327
                // Then project out dims before inner_dim
328
                if (inner_dim > 0) {
×
329
                    delta_set = isl_set_project_out(delta_set, isl_dim_set, 0, inner_dim);
×
330
                }
×
331
                // Now it's 1D — check non-negativity
332
                isl_set* neg = isl_set_read_from_str(ctx, "{ [x] : x < 0 }");
×
333
                isl_set* violation = isl_set_intersect(delta_set, neg);
×
334
                bool legal = isl_set_is_empty(violation);
×
335
                isl_set_free(violation);
×
336
                isl_ctx_free(ctx);
×
337
                if (!legal) {
×
338
                    return false;
×
339
                }
×
340
            } else {
×
341
                isl_ctx_free(ctx);
×
342
                return false;
×
343
            }
×
344
        }
×
345
    }
13✔
346

347
    // Check inner loop dependencies (1D delta sets: [d_inner])
348
    auto& inner_deps = lcd.dependencies(inner_loop_);
59✔
349
    for (auto& dep : inner_deps) {
198✔
350
        if (dep.first == outer_indvar_name || dep.first == inner_indvar_name) {
198✔
351
            continue;
×
352
        }
×
353
        auto& deltas = dep.second.deltas;
198✔
354
        if (deltas.empty) {
198✔
355
            continue;
×
356
        }
×
357
        if (deltas.dimensions.empty()) {
198✔
358
            continue;
183✔
359
        }
183✔
360
        if (deltas.deltas_str.empty()) {
15✔
361
            return false;
×
362
        }
×
363
        if (deltas.dimensions.size() == 1) {
15✔
364
            if (!is_interchange_legal_1d(deltas.deltas_str)) {
8✔
365
                return false;
×
366
            }
×
367
        } else if (deltas.dimensions.size() >= 1) {
8✔
368
            // Multi-dimensional delta set from nested loops inside the inner loop.
369
            // Find the dimension corresponding to the inner loop indvar.
370
            int inner_dim = -1;
7✔
371
            for (size_t d = 0; d < deltas.dimensions.size(); d++) {
21✔
372
                if (deltas.dimensions[d] == inner_indvar_name) {
19✔
373
                    inner_dim = static_cast<int>(d);
5✔
374
                    break;
5✔
375
                }
5✔
376
            }
19✔
377
            if (inner_dim < 0) {
7✔
378
                // Inner indvar not found in dimensions — safe (dependency is on nested loops only)
379
                continue;
2✔
380
            }
2✔
381
            // For interchange, only the inner indvar dimension matters (it becomes outer).
382
            // The other dimensions represent nested loops which stay nested.
383
            // Project to 1D by checking only the inner indvar dimension.
384
            // After interchange, we need: delta_inner >= 0 for lex-positive order.
385
            // Since we use < constraint now, we only get forward (positive) deltas.
386
            //
387
            // For the case where other dimensions are all 0, this is effectively
388
            // a 1D dependency. For multi-D cases where inner_dim is found,
389
            // we need to verify that dimension is non-negative.
390
            if (deltas.dimensions.size() >= 2 && inner_dim >= 0) {
5✔
391
                // The inner dimension must not have negative deltas.
392
                // With < constraint, we should only have positive deltas.
393
                // Use is_interchange_legal_1d to check just the inner dimension.
394
                // Since we can't easily project in ISL here, we accept if no
395
                // explicit negative constraint on inner_dim is visible.
396
                // The < constraint should ensure only positive deltas exist.
397
                continue; // Safe with forward-only deltas
5✔
398
            } else if (inner_dim < 0) {
5✔
399
                // Inner indvar not found — safe, nested loop dependency
400
                continue;
×
401
            } else {
×
402
                // Fallback for unexpected cases
403
                return false;
×
404
            }
×
405
        }
5✔
406
    }
15✔
407

408
    return true;
59✔
409
};
59✔
410

411
void LoopInterchange::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
42✔
412
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
42✔
413
    auto& outer_scope = static_cast<structured_control_flow::Sequence&>(*scope_analysis.parent_scope(&outer_loop_));
42✔
414
    auto& inner_scope = outer_loop_.root();
42✔
415

416
    int index = outer_scope.index(this->outer_loop_);
42✔
417
    auto& outer_transition = outer_scope.at(index).second;
42✔
418

419
    // Add new outer and inner loops
420
    structured_control_flow::StructuredLoop* new_outer_loop = nullptr;
42✔
421
    structured_control_flow::StructuredLoop* new_inner_loop = nullptr;
42✔
422

423
    auto* inner_map = dynamic_cast<structured_control_flow::Map*>(&inner_loop_);
42✔
424
    auto* outer_map = dynamic_cast<structured_control_flow::Map*>(&outer_loop_);
42✔
425

426
    bool dependent = !inner_map && !outer_map &&
42✔
427
                     (symbolic::uses(inner_loop_.init(), outer_loop_.indvar()->get_name()) ||
42✔
428
                      symbolic::uses(inner_loop_.condition(), outer_loop_.indvar()->get_name()));
25✔
429

430
    if (dependent) {
42✔
431
        // Fourier-Motzkin elimination: compute projected and inverted bounds
432
        auto outer_indvar = outer_loop_.indvar();
6✔
433
        auto inner_indvar = inner_loop_.indvar();
6✔
434
        auto outer_init_expr = outer_loop_.init();
6✔
435
        auto outer_bound = extract_strict_upper_bound(outer_loop_.condition(), outer_indvar);
6✔
436
        auto outer_max = symbolic::sub(outer_bound, symbolic::integer(1));
6✔
437

438
        auto inner_init_expr = inner_loop_.init();
6✔
439
        auto inner_bound_expr = extract_strict_upper_bound(inner_loop_.condition(), inner_indvar);
6✔
440

441
        // Project inner bounds for new outer loop:
442
        //   new_init = inner_init(outer_var = outer_init)
443
        //   new_bound = inner_bound(outer_var = outer_max)
444
        auto new_outer_init = symbolic::subs(inner_init_expr, outer_indvar, outer_init_expr);
6✔
445
        auto new_outer_bound = symbolic::subs(inner_bound_expr, outer_indvar, outer_max);
6✔
446
        auto new_outer_cond = symbolic::Lt(inner_indvar, new_outer_bound);
6✔
447

448
        // Invert inner bounds for new inner loop (FM elimination):
449
        //   inner_init  = α*outer_var + b  =>  from y >= α*x + b:  x <= (y-b)/α
450
        //   inner_bound = α*outer_var + d  =>  from y <  α*x + d:  x >  (y-d)/α
451
        // Integer rounding: x < floor((y-b)/α) + 1 and x >= floor((y-d)/α) + 1
452
        auto init_decomp = check_affine(inner_init_expr, outer_indvar);
6✔
453
        auto bound_decomp = check_affine(inner_bound_expr, outer_indvar);
6✔
454
        auto alpha = init_decomp.coefficient; // == bound_decomp.coefficient
6✔
455
        auto b = init_decomp.constant;
6✔
456
        auto d = bound_decomp.constant;
6✔
457

458
        symbolic::Expression lower_from_cond, upper_from_init;
6✔
459
        if (symbolic::eq(alpha, symbolic::integer(1))) {
6✔
460
            // Unit coefficient — avoid introducing idiv(x,1)
461
            lower_from_cond = symbolic::add(symbolic::sub(inner_indvar, d), symbolic::integer(1));
×
462
            upper_from_init = symbolic::add(symbolic::sub(inner_indvar, b), symbolic::integer(1));
×
463
        } else {
6✔
464
            // General: floor((y - const) / α) + 1
465
            lower_from_cond = symbolic::add(symbolic::div(symbolic::sub(inner_indvar, d), alpha), symbolic::integer(1));
6✔
466
            upper_from_init = symbolic::add(symbolic::div(symbolic::sub(inner_indvar, b), alpha), symbolic::integer(1));
6✔
467
        }
6✔
468
        auto new_inner_init = symbolic::max(outer_init_expr, lower_from_cond);
6✔
469
        auto new_inner_bound = symbolic::min(outer_bound, upper_from_init);
6✔
470
        auto new_inner_cond = symbolic::Lt(outer_indvar, new_inner_bound);
6✔
471

472
        new_outer_loop = &builder.add_for_after(
6✔
473
            outer_scope,
6✔
474
            this->outer_loop_,
6✔
475
            inner_indvar,
6✔
476
            new_outer_cond,
6✔
477
            new_outer_init,
6✔
478
            this->inner_loop_.update(),
6✔
479
            outer_transition.assignments(),
6✔
480
            this->inner_loop_.debug_info()
6✔
481
        );
6✔
482

483
        new_inner_loop = &builder.add_for_after(
6✔
484
            inner_scope,
6✔
485
            this->inner_loop_,
6✔
486
            outer_indvar,
6✔
487
            new_inner_cond,
6✔
488
            new_inner_init,
6✔
489
            this->outer_loop_.update(),
6✔
490
            {},
6✔
491
            this->outer_loop_.debug_info()
6✔
492
        );
6✔
493
    } else {
36✔
494
        // Standard case: just swap loop headers
495
        if (inner_map) {
36✔
496
            new_outer_loop = &builder.add_map_after(
11✔
497
                outer_scope,
11✔
498
                this->outer_loop_,
11✔
499
                inner_map->indvar(),
11✔
500
                inner_map->condition(),
11✔
501
                inner_map->init(),
11✔
502
                inner_map->update(),
11✔
503
                inner_map->schedule_type(),
11✔
504
                outer_transition.assignments(),
11✔
505
                this->inner_loop_.debug_info()
11✔
506
            );
11✔
507
        } else {
25✔
508
            new_outer_loop = &builder.add_for_after(
25✔
509
                outer_scope,
25✔
510
                this->outer_loop_,
25✔
511
                this->inner_loop_.indvar(),
25✔
512
                this->inner_loop_.condition(),
25✔
513
                this->inner_loop_.init(),
25✔
514
                this->inner_loop_.update(),
25✔
515
                outer_transition.assignments(),
25✔
516
                this->inner_loop_.debug_info()
25✔
517
            );
25✔
518
        }
25✔
519

520
        if (outer_map) {
36✔
521
            new_inner_loop = &builder.add_map_after(
15✔
522
                inner_scope,
15✔
523
                this->inner_loop_,
15✔
524
                outer_map->indvar(),
15✔
525
                outer_map->condition(),
15✔
526
                outer_map->init(),
15✔
527
                outer_map->update(),
15✔
528
                outer_map->schedule_type(),
15✔
529
                {},
15✔
530
                this->outer_loop_.debug_info()
15✔
531
            );
15✔
532
        } else {
21✔
533
            new_inner_loop = &builder.add_for_after(
21✔
534
                inner_scope,
21✔
535
                this->inner_loop_,
21✔
536
                this->outer_loop_.indvar(),
21✔
537
                this->outer_loop_.condition(),
21✔
538
                this->outer_loop_.init(),
21✔
539
                this->outer_loop_.update(),
21✔
540
                {},
21✔
541
                this->outer_loop_.debug_info()
21✔
542
            );
21✔
543
        }
21✔
544
    }
36✔
545

546
    // Insert inner loop body into new inner loop
547
    builder.move_children(this->inner_loop_.root(), new_inner_loop->root());
42✔
548

549
    // Insert outer loop body into new outer loop
550
    builder.move_children(this->outer_loop_.root(), new_outer_loop->root());
42✔
551

552
    // Remove old loops
553
    builder.remove_child(new_outer_loop->root(), 0);
42✔
554
    builder.remove_child(outer_scope, index);
42✔
555

556
    analysis_manager.invalidate_all();
42✔
557
    applied_ = true;
42✔
558
    new_outer_loop_ = new_outer_loop;
42✔
559
    new_inner_loop_ = new_inner_loop;
42✔
560
};
42✔
561

562
void LoopInterchange::to_json(nlohmann::json& j) const {
26✔
563
    std::vector<std::string> loop_types;
26✔
564
    for (auto* loop : {&(this->outer_loop_), &(this->inner_loop_)}) {
52✔
565
        if (dynamic_cast<structured_control_flow::For*>(loop)) {
52✔
566
            loop_types.push_back("for");
40✔
567
        } else if (dynamic_cast<structured_control_flow::Map*>(loop)) {
40✔
568
            loop_types.push_back("map");
12✔
569
        } else {
12✔
570
            throw InvalidSDFGException("Unsupported loop type for serialization of loop: " + loop->indvar()->get_name());
×
571
        }
×
572
    }
52✔
573
    j["transformation_type"] = this->name();
26✔
574
    j["subgraph"] = {
26✔
575
        {"0", {{"element_id", this->outer_loop_.element_id()}, {"type", loop_types[0]}}},
26✔
576
        {"1", {{"element_id", this->inner_loop_.element_id()}, {"type", loop_types[1]}}}
26✔
577
    };
26✔
578
};
26✔
579

580
LoopInterchange LoopInterchange::from_json(builder::StructuredSDFGBuilder& builder, const nlohmann::json& desc) {
4✔
581
    auto outer_loop_id = desc["subgraph"]["0"]["element_id"].get<size_t>();
4✔
582
    auto inner_loop_id = desc["subgraph"]["1"]["element_id"].get<size_t>();
4✔
583
    auto outer_element = builder.find_element_by_id(outer_loop_id);
4✔
584
    auto inner_element = builder.find_element_by_id(inner_loop_id);
4✔
585
    if (outer_element == nullptr) {
4✔
586
        throw InvalidSDFGException("Element with ID " + std::to_string(outer_loop_id) + " not found.");
×
587
    }
×
588
    if (inner_element == nullptr) {
4✔
589
        throw InvalidSDFGException("Element with ID " + std::to_string(inner_loop_id) + " not found.");
×
590
    }
×
591
    auto outer_loop = dynamic_cast<structured_control_flow::StructuredLoop*>(outer_element);
4✔
592
    if (outer_loop == nullptr) {
4✔
593
        throw InvalidSDFGException("Element with ID " + std::to_string(outer_loop_id) + " is not a StructuredLoop.");
×
594
    }
×
595
    auto inner_loop = dynamic_cast<structured_control_flow::StructuredLoop*>(inner_element);
4✔
596
    if (inner_loop == nullptr) {
4✔
597
        throw InvalidSDFGException("Element with ID " + std::to_string(inner_loop_id) + " is not a StructuredLoop.");
×
598
    }
×
599

600
    return LoopInterchange(*outer_loop, *inner_loop);
4✔
601
};
4✔
602

603
structured_control_flow::StructuredLoop* LoopInterchange::new_outer_loop() const {
×
604
    if (!applied_) {
×
605
        throw InvalidSDFGException("Transformation has not been applied yet.");
×
606
    }
×
607
    return new_outer_loop_;
×
608
};
×
609

610
structured_control_flow::StructuredLoop* LoopInterchange::new_inner_loop() const {
×
611
    if (!applied_) {
×
612
        throw InvalidSDFGException("Transformation has not been applied yet.");
×
613
    }
×
614
    return new_inner_loop_;
×
615
};
×
616

617
} // namespace transformations
618
} // 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