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

daisytuner / docc / 25289367548

03 May 2026 08:01PM UTC coverage: 64.317% (+0.1%) from 64.206%
25289367548

push

github

web-flow
Merge pull request #698 from daisytuner/tile-fusion-double-buffering

Tile fusion double buffering

306 of 453 new or added lines in 4 files covered. (67.55%)

3 existing lines in 2 files now uncovered.

31682 of 49259 relevant lines covered (64.32%)

1432.27 hits per line

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

71.69
/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/scope_analysis.h"
10
#include "sdfg/exceptions.h"
11
#include "sdfg/structured_control_flow/for.h"
12
#include "sdfg/structured_control_flow/structured_loop.h"
13
#include "sdfg/symbolic/polynomials.h"
14

15
namespace sdfg {
16
namespace transformations {
17

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

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

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

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

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

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

55
    return legal;
3✔
56
}
3✔
57

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

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

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

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

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

87
    return legal;
8✔
88
}
8✔
89

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

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

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

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

157
      };
199✔
158

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

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

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

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

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

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

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

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

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

242
    // For-For: check legality using dependence delta sets
243
    analysis::DataDependencyAnalysis dda(builder.subject(), true);
62✔
244
    dda.run(analysis_manager);
62✔
245

246
    std::string outer_indvar_name = outer_loop_.indvar()->get_name();
62✔
247
    std::string inner_indvar_name = inner_loop_.indvar()->get_name();
62✔
248

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

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

404
    return true;
61✔
405
};
61✔
406

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

412
    int index = outer_scope.index(this->outer_loop_);
42✔
413
    auto& outer_transition = outer_scope.at(index).second;
42✔
414

415
    // Add new outer and inner loops
416
    structured_control_flow::StructuredLoop* new_outer_loop = nullptr;
42✔
417
    structured_control_flow::StructuredLoop* new_inner_loop = nullptr;
42✔
418

419
    auto* inner_map = dynamic_cast<structured_control_flow::Map*>(&inner_loop_);
42✔
420
    auto* outer_map = dynamic_cast<structured_control_flow::Map*>(&outer_loop_);
42✔
421

422
    bool dependent = !inner_map && !outer_map &&
42✔
423
                     (symbolic::uses(inner_loop_.init(), outer_loop_.indvar()->get_name()) ||
42✔
424
                      symbolic::uses(inner_loop_.condition(), outer_loop_.indvar()->get_name()));
25✔
425

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

434
        auto inner_init_expr = inner_loop_.init();
6✔
435
        auto inner_bound_expr = extract_strict_upper_bound(inner_loop_.condition(), inner_indvar);
6✔
436

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

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

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

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

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

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

542
    // Insert inner loop body into new inner loop
543
    builder.move_children(this->inner_loop_.root(), new_inner_loop->root());
42✔
544

545
    // Insert outer loop body into new outer loop
546
    builder.move_children(this->outer_loop_.root(), new_outer_loop->root());
42✔
547

548
    // Remove old loops
549
    builder.remove_child(new_outer_loop->root(), 0);
42✔
550
    builder.remove_child(outer_scope, index);
42✔
551

552
    analysis_manager.invalidate_all();
42✔
553
    applied_ = true;
42✔
554
    new_outer_loop_ = new_outer_loop;
42✔
555
    new_inner_loop_ = new_inner_loop;
42✔
556
};
42✔
557

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

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

596
    return LoopInterchange(*outer_loop, *inner_loop);
4✔
597
};
4✔
598

599
structured_control_flow::StructuredLoop* LoopInterchange::new_outer_loop() const {
×
600
    if (!applied_) {
×
601
        throw InvalidSDFGException("Transformation has not been applied yet.");
×
602
    }
×
603
    return new_outer_loop_;
×
604
};
×
605

606
structured_control_flow::StructuredLoop* LoopInterchange::new_inner_loop() const {
×
607
    if (!applied_) {
×
608
        throw InvalidSDFGException("Transformation has not been applied yet.");
×
609
    }
×
610
    return new_inner_loop_;
×
611
};
×
612

613
} // namespace transformations
614
} // 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