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

daisytuner / docc / 27477701050

13 Jun 2026 08:07PM UTC coverage: 61.376% (+0.1%) from 61.274%
27477701050

Pull #759

github

web-flow
Merge 4d53cf6a6 into 9754c0592
Pull Request #759: Removes cleanup passes from transformations

3 of 5 new or added lines in 3 files covered. (60.0%)

2 existing lines in 1 file now uncovered.

36296 of 59137 relevant lines covered (61.38%)

1126.78 hits per line

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

81.69
/opt/src/transformations/in_local_storage.cpp
1
#include "sdfg/transformations/in_local_storage.h"
2

3
#include <cstddef>
4
#include <functional>
5
#include <string>
6

7
#include "sdfg/analysis/memory_layout_analysis.h"
8
#include "sdfg/analysis/scope_analysis.h"
9
#include "sdfg/analysis/users.h"
10
#include "sdfg/builder/structured_sdfg_builder.h"
11
#include "sdfg/data_flow/access_node.h"
12
#include "sdfg/data_flow/library_nodes/barrier_local_node.h"
13
#include "sdfg/data_flow/memlet.h"
14
#include "sdfg/structured_control_flow/if_else.h"
15
#include "sdfg/structured_control_flow/sequence.h"
16
#include "sdfg/structured_control_flow/structured_loop.h"
17
#include "sdfg/symbolic/symbolic.h"
18
#include "sdfg/targets/gpu/gpu_schedule_type.h"
19
#include "sdfg/types/array.h"
20
#include "sdfg/types/pointer.h"
21
#include "sdfg/types/scalar.h"
22

23
namespace sdfg {
24
namespace transformations {
25

26
InLocalStorage::InLocalStorage(
27
    structured_control_flow::StructuredLoop& loop,
28
    const data_flow::AccessNode& access_node,
29
    const types::StorageType& storage_type
30
)
31
    : loop_(loop), access_node_(access_node), container_(access_node.data()), storage_type_(storage_type) {}
33✔
32

33
std::string InLocalStorage::name() const { return "InLocalStorage"; }
7✔
34

35
bool InLocalStorage::can_be_applied(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
33✔
36
    auto& sdfg = builder.subject();
33✔
37
    auto& body = this->loop_.root();
33✔
38

39
    tile_info_ = TileInfo{};
33✔
40

41
    // Criterion: Container must exist and is pointer
42
    if (!sdfg.exists(this->container_)) {
33✔
43
        return false;
×
44
    }
×
45
    auto& type = sdfg.type(this->container_);
33✔
46
    if (type.type_id() != types::TypeID::Pointer) {
33✔
UNCOV
47
        return false;
×
UNCOV
48
    }
×
49

50
    // Criterion: Container must be used in the loop body
51
    auto& users = analysis_manager.get<analysis::Users>();
33✔
52
    analysis::UsersView body_users(users, body);
33✔
53
    if (body_users.uses(this->container_).empty()) {
33✔
54
        return false;
2✔
55
    }
2✔
56

57
    // Criterion: Container must be read-only within the loop (no writes)
58
    if (!body_users.writes(this->container_).empty()) {
31✔
59
        return false;
1✔
60
    }
1✔
61

62
    // Use MemoryLayoutAnalysis tile group API
63
    // Find a representative memlet from the access node to identify its group.
64
    auto& mla = analysis_manager.get<analysis::MemoryLayoutAnalysis>();
30✔
65
    const analysis::MemoryTileGroup* group = nullptr;
30✔
66
    auto& dfg = access_node_.get_parent();
30✔
67
    for (auto& memlet : dfg.out_edges(access_node_)) {
30✔
68
        auto* candidate = mla.tile_group_for(loop_, memlet);
30✔
69
        if (!candidate) {
30✔
70
            continue;
×
71
        }
×
72

73
        auto extents = candidate->tile.extents_approx();
30✔
74
        if (extents.empty()) {
30✔
75
            continue;
×
76
        }
×
77

78
        // Reject candidates with any unbounded-dependent extent (returned as null).
79
        bool has_null = false;
30✔
80
        for (auto& ext : extents) {
52✔
81
            if (ext.is_null()) {
52✔
82
                has_null = true;
×
83
                break;
×
84
            }
×
85
        }
52✔
86
        if (has_null) {
30✔
87
            continue;
×
88
        }
×
89

90
        // GPU path: accept first valid group (substitution happens later)
91
        if (storage_type_.is_nv_shared()) {
30✔
92
            group = candidate;
5✔
93
            break;
5✔
94
        }
5✔
95

96
        // CPU path: require provably integer extents
97
        bool all_integer = true;
25✔
98
        for (auto& ext : extents) {
43✔
99
            if (!SymEngine::is_a<SymEngine::Integer>(*ext)) {
43✔
100
                all_integer = false;
×
101
                break;
×
102
            }
×
103
        }
43✔
104
        if (all_integer) {
25✔
105
            group = candidate;
25✔
106
            break;
25✔
107
        }
25✔
108
    }
25✔
109
    if (!group) {
30✔
110
        return false;
×
111
    }
×
112

113
    auto& tile = group->tile;
30✔
114
    auto extents = tile.extents_approx();
30✔
115

116
    // Store group memlets for use in apply()
117
    group_memlets_.clear();
30✔
118
    group_memlets_.insert(group->memlets.begin(), group->memlets.end());
30✔
119

120
    // Store tile info (before substitution, bases/strides stay symbolic)
121
    tile_info_.dimensions = extents;
30✔
122
    tile_info_.bases = tile.min_subset;
30✔
123
    tile_info_.strides = std::vector<symbolic::Expression>(tile.layout.strides().begin(), tile.layout.strides().end());
30✔
124
    tile_info_.offset = tile.layout.offset();
30✔
125

126
    // GPU shared memory: resolve symbolic extents using GPU block sizes and
127
    // require at least one cooperative dimension
128
    if (storage_type_.is_nv_shared()) {
30✔
129
        auto ancestors = ControlFlowNode::parent_chain(loop_);
5✔
130

131
        // Build substitution map: symbolic GPU map bounds → integer block sizes
132
        // E.g., Map condition "i < N" with block_size=32 → N=32
133
        for (auto* node : ancestors) {
23✔
134
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
23✔
135
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
9✔
136
                    continue;
×
137
                }
×
138
                auto block_size = gpu::gpu_block_size(ancestor_map->schedule_type());
9✔
139
                // Extract symbolic bound from condition: Lt(indvar, BOUND)
140
                auto condition = ancestor_map->condition();
9✔
141
                if (SymEngine::is_a<SymEngine::StrictLessThan>(*condition)) {
9✔
142
                    auto stl = SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(condition);
9✔
143
                    auto rhs = stl->get_args()[1];
9✔
144
                    auto iter_count = symbolic::sub(rhs, ancestor_map->init());
9✔
145
                    if (!SymEngine::is_a<SymEngine::Integer>(*iter_count)) {
9✔
146
                        // Symbolic bound — substitute with block size in extents and bases
147
                        for (auto& ext : tile_info_.dimensions) {
16✔
148
                            ext = symbolic::simplify(symbolic::subs(ext, iter_count, block_size));
16✔
149
                        }
16✔
150
                        for (auto& base : tile_info_.bases) {
16✔
151
                            base = symbolic::simplify(symbolic::subs(base, iter_count, block_size));
16✔
152
                        }
16✔
153
                    }
9✔
154
                }
9✔
155
            }
9✔
156
        }
23✔
157

158
        // Also resolve the loop's own bound if symbolic and matches a block size
159
        // E.g., For k = 0..K where K is a parameter — check if K can be resolved
160
        // from any GPU ancestor map
161
        // (Already handled above: if K appears as a GPU map bound, it's substituted)
162

163
        // Criterion: All extents must now be provably integer
164
        for (auto& ext : tile_info_.dimensions) {
9✔
165
            if (!SymEngine::is_a<SymEngine::Integer>(*ext)) {
9✔
166
                return false;
2✔
167
            }
2✔
168
        }
9✔
169

170
        // Criterion: At least one cooperative dimension
171
        bool has_cooperative_dim = false;
3✔
172
        for (auto* node : ancestors) {
6✔
173
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
6✔
174
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
3✔
175
                    continue;
×
176
                }
×
177
                // A GPU dim is cooperative if its indvar does NOT appear in any tile base
178
                bool appears_in_bases = false;
3✔
179
                for (auto& base : tile_info_.bases) {
5✔
180
                    if (symbolic::uses(base, ancestor_map->indvar())) {
5✔
181
                        appears_in_bases = true;
×
182
                        break;
×
183
                    }
×
184
                }
5✔
185
                if (!appears_in_bases) {
3✔
186
                    has_cooperative_dim = true;
3✔
187
                    break;
3✔
188
                }
3✔
189
            }
3✔
190
        }
6✔
191
        if (!has_cooperative_dim) {
3✔
192
            return false;
×
193
        }
×
194
    }
3✔
195

196
    return true;
28✔
197
}
30✔
198

199
void InLocalStorage::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
21✔
200
    auto& sdfg = builder.subject();
21✔
201

202
    auto parent_node = loop_.get_parent();
21✔
203
    auto parent = dynamic_cast<structured_control_flow::Sequence*>(parent_node);
21✔
204
    if (!parent) {
21✔
205
        throw InvalidSDFGException("InLocalStorage: Parent of loop must be a Sequence!");
×
206
    }
×
207

208
    // We replace all relevant memlets with flat local indices
209
    // Thus, we now use a flat pointer to index into container
210
    // Remark: sdfg.type may return an opaque pointer, so use
211
    //         memlet instead
212
    auto* memlet = *group_memlets_.begin();
21✔
213
    types::Scalar scalar_type(memlet->base_type().primitive_type());
21✔
214
    types::Pointer pointer_type(scalar_type);
21✔
215

216
    // Create local buffer name
217
    local_name_ = builder.find_new_name("__daisy_in_local_storage_" + this->container_);
21✔
218

219
    // Collect varying dimensions (extent > 1) and compute buffer layout
220
    std::vector<size_t> varying_dims;
21✔
221
    std::vector<symbolic::Expression> dim_sizes;
21✔
222
    for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
58✔
223
        auto& dim_size = tile_info_.dimensions.at(d);
37✔
224
        if (!symbolic::eq(dim_size, symbolic::integer(1))) {
37✔
225
            varying_dims.push_back(d);
28✔
226
            dim_sizes.push_back(dim_size);
28✔
227
        }
28✔
228
    }
37✔
229

230
    // Compute total buffer size
231
    symbolic::Expression total_size = symbolic::integer(1);
21✔
232
    for (auto& ds : dim_sizes) {
28✔
233
        total_size = symbolic::mul(total_size, ds);
28✔
234
    }
28✔
235

236
    // Helper: build linearized local index from per-dimension symbolic expressions
237
    auto linearize_exprs = [&](const std::vector<symbolic::Expression>& indices) -> symbolic::Expression {
43✔
238
        symbolic::Expression linear_idx = symbolic::integer(0);
43✔
239
        symbolic::Expression stride = symbolic::integer(1);
43✔
240
        for (int i = indices.size() - 1; i >= 0; i--) {
104✔
241
            linear_idx = symbolic::add(linear_idx, symbolic::mul(indices[i], stride));
61✔
242
            stride = symbolic::mul(stride, dim_sizes[i]);
61✔
243
        }
61✔
244
        return linear_idx;
43✔
245
    };
43✔
246

247
    // Helper: build linearized local index from per-dimension indvars (symbols)
248
    auto linearize = [&](const std::vector<symbolic::Symbol>& indvars) -> symbolic::Expression {
21✔
249
        std::vector<symbolic::Expression> exprs(indvars.begin(), indvars.end());
18✔
250
        return linearize_exprs(exprs);
18✔
251
    };
18✔
252

253
    // Helper: build source subset (base[d] + copy_indvar[d]) for original container
254
    auto build_original_subset = [&](const std::vector<symbolic::Expression>& copy_indices) -> data_flow::Subset {
21✔
255
        std::vector<symbolic::Expression> full_indices;
21✔
256
        size_t var_idx = 0;
21✔
257
        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
58✔
258
            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
37✔
259
                full_indices.push_back(symbolic::add(tile_info_.bases.at(d), copy_indices.at(var_idx++)));
28✔
260
            } else {
28✔
261
                full_indices.push_back(tile_info_.bases.at(d));
9✔
262
            }
9✔
263
        }
37✔
264

265
        symbolic::Expression linear = tile_info_.offset;
21✔
266
        for (size_t d = 0; d < full_indices.size(); d++) {
58✔
267
            linear = symbolic::add(linear, symbolic::mul(tile_info_.strides.at(d), full_indices.at(d)));
37✔
268
        }
37✔
269
        return {linear};
21✔
270
    };
21✔
271

272
    // ==================================================================
273
    // Branch: GPU cooperative path vs CPU sequential path
274
    // ==================================================================
275
    if (storage_type_.is_nv_shared()) {
21✔
276
        // ============================================================
277
        // GPU COOPERATIVE PATH
278
        // ============================================================
279
        auto ancestors = ControlFlowNode::parent_chain(loop_);
3✔
280

281
        // Collect cooperative GPU dimensions (indvar not in tile bases)
282
        struct CoopDim {
3✔
283
            symbolic::Symbol indvar;
3✔
284
            symbolic::Integer block_size;
3✔
285
            gpu::GPUDimension dimension;
3✔
286
        };
3✔
287
        std::vector<CoopDim> coop_dims;
3✔
288

289
        for (auto* node : ancestors) {
15✔
290
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
15✔
291
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
6✔
292
                    continue;
×
293
                }
×
294
                bool appears_in_bases = false;
6✔
295
                for (auto& base : tile_info_.bases) {
8✔
296
                    if (symbolic::uses(base, ancestor_map->indvar())) {
8✔
297
                        appears_in_bases = true;
2✔
298
                        break;
2✔
299
                    }
2✔
300
                }
8✔
301
                if (!appears_in_bases) {
6✔
302
                    coop_dims.push_back(
4✔
303
                        {ancestor_map->indvar(),
4✔
304
                         gpu::gpu_block_size(ancestor_map->schedule_type()),
4✔
305
                         gpu::gpu_dimension(ancestor_map->schedule_type())}
4✔
306
                    );
4✔
307
                }
4✔
308
            }
6✔
309
        }
15✔
310

311
        // Compute total cooperative thread count
312
        symbolic::Expression total_coop_threads = symbolic::integer(1);
3✔
313
        for (auto& cd : coop_dims) {
4✔
314
            total_coop_threads = symbolic::mul(total_coop_threads, cd.block_size);
4✔
315
        }
4✔
316

317
        // Create the local buffer with NV_Shared storage
318
        types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
3✔
319
        builder.add_container(local_name_, buffer_type);
3✔
320

321
        // Emit: barrier → guarded cooperative copy → barrier → loop
322
        // 1. Barrier before copy
323
        auto& barrier_block1 = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
3✔
324
        builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block1, {});
3✔
325

326
        // 2. Cooperative copy with if_else guard
327
        // Flatten cooperative thread index: coop_flat = sum(indvar[i] * product(block_size[j] for j>i))
328
        symbolic::Expression coop_flat = symbolic::integer(0);
3✔
329
        symbolic::Expression coop_stride = symbolic::integer(1);
3✔
330
        for (int i = coop_dims.size() - 1; i >= 0; i--) {
7✔
331
            coop_flat = symbolic::add(coop_flat, symbolic::mul(coop_dims[i].indvar, coop_stride));
4✔
332
            coop_stride = symbolic::mul(coop_stride, coop_dims[i].block_size);
4✔
333
        }
4✔
334

335
        // Each thread loads elements strided by total_coop_threads
336
        // Thread t loads elements: t, t + total_threads, t + 2*total_threads, ...
337
        // We emit a loop: for (idx = coop_flat; idx < total_size; idx += total_coop_threads)
338
        auto idx_name = builder.find_new_name("__daisy_ils_coop_" + this->container_);
3✔
339
        types::Scalar idx_type(types::PrimitiveType::UInt64);
3✔
340
        builder.add_container(idx_name, idx_type);
3✔
341
        auto idx_var = symbolic::symbol(idx_name);
3✔
342

343
        auto copy_init = coop_flat;
3✔
344
        auto copy_condition = symbolic::Lt(idx_var, total_size);
3✔
345
        auto copy_update = symbolic::add(idx_var, total_coop_threads);
3✔
346

347
        auto& copy_loop = builder.add_map_before(
3✔
348
            *parent,
3✔
349
            loop_,
3✔
350
            idx_var,
3✔
351
            copy_condition,
3✔
352
            copy_init,
3✔
353
            copy_update,
3✔
354
            structured_control_flow::ScheduleType_Sequential::create(),
3✔
355
            {},
3✔
356
            loop_.debug_info()
3✔
357
        );
3✔
358

359
        // Decompose flat idx back into per-dimension indices for source subset
360
        // idx maps to varying_dims in row-major order
361
        auto& copy_scope = copy_loop.root();
3✔
362
        auto& copy_block = builder.add_block(copy_scope);
3✔
363
        auto& copy_src = builder.add_access(copy_block, this->container_);
3✔
364
        auto& copy_dst = builder.add_access(copy_block, local_name_);
3✔
365
        auto& copy_tasklet = builder.add_tasklet(copy_block, data_flow::TaskletCode::assign, "_out", {"_in"});
3✔
366

367
        // Decompose idx_var into per-dim indices
368
        std::vector<symbolic::Expression> copy_indices;
3✔
369
        symbolic::Expression remainder = idx_var;
3✔
370
        for (size_t i = 0; i < dim_sizes.size(); i++) {
6✔
371
            if (i < dim_sizes.size() - 1) {
3✔
372
                // integer division: idx / (product of remaining dims)
373
                symbolic::Expression divisor = symbolic::integer(1);
×
374
                for (size_t j = i + 1; j < dim_sizes.size(); j++) {
×
375
                    divisor = symbolic::mul(divisor, dim_sizes[j]);
×
376
                }
×
377
                auto quotient = symbolic::div(remainder, divisor);
×
378
                copy_indices.push_back(quotient);
×
379
                remainder = symbolic::mod(remainder, divisor);
×
380
            } else {
3✔
381
                copy_indices.push_back(remainder);
3✔
382
            }
3✔
383
        }
3✔
384

385
        auto copy_src_subset = build_original_subset(copy_indices);
3✔
386
        data_flow::Subset copy_dst_subset = {idx_var};
3✔
387

388
        builder.add_computational_memlet(copy_block, copy_src, copy_tasklet, "_in", copy_src_subset, pointer_type);
3✔
389
        builder.add_computational_memlet(copy_block, copy_tasklet, "_out", copy_dst, copy_dst_subset, buffer_type);
3✔
390

391
        // 3. Barrier after copy
392
        auto& barrier_block2 = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
3✔
393
        builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block2, {});
3✔
394
    } else {
18✔
395
        // ============================================================
396
        // CPU SEQUENTIAL PATH
397
        // ============================================================
398
        // Create the local buffer with specified storage type
399
        types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
18✔
400
        builder.add_container(local_name_, buffer_type);
18✔
401

402
        std::vector<symbolic::Symbol> copy_indvars;
18✔
403
        structured_control_flow::Sequence* copy_scope =
18✔
404
            &builder.add_sequence_before(*parent, loop_, {}, loop_.debug_info());
18✔
405
        for (size_t i = 0; i < varying_dims.size(); i++) {
43✔
406
            size_t d = varying_dims[i];
25✔
407
            auto indvar_name = builder.find_new_name("__daisy_ils_" + this->container_ + "_d" + std::to_string(d));
25✔
408
            types::Scalar indvar_type(types::PrimitiveType::UInt64);
25✔
409
            builder.add_container(indvar_name, indvar_type);
25✔
410
            auto indvar = symbolic::symbol(indvar_name);
25✔
411
            copy_indvars.push_back(indvar);
25✔
412

413
            auto init = symbolic::integer(0);
25✔
414
            auto condition = symbolic::Lt(indvar, dim_sizes[i]);
25✔
415
            auto update = symbolic::add(indvar, symbolic::integer(1));
25✔
416

417
            auto& copy_loop = builder.add_map(
25✔
418
                *copy_scope,
25✔
419
                indvar,
25✔
420
                condition,
25✔
421
                init,
25✔
422
                update,
25✔
423
                structured_control_flow::ScheduleType_Sequential::create(),
25✔
424
                {},
25✔
425
                loop_.debug_info()
25✔
426
            );
25✔
427
            copy_scope = &copy_loop.root();
25✔
428
        }
25✔
429

430
        // Create copy block
431
        auto& copy_block = builder.add_block(*copy_scope);
18✔
432
        auto& copy_src = builder.add_access(copy_block, this->container_);
18✔
433
        auto& copy_dst = builder.add_access(copy_block, local_name_);
18✔
434
        auto& copy_tasklet = builder.add_tasklet(copy_block, data_flow::TaskletCode::assign, "_out", {"_in"});
18✔
435

436
        std::vector<symbolic::Expression> copy_exprs(copy_indvars.begin(), copy_indvars.end());
18✔
437
        auto copy_src_subset = build_original_subset(copy_exprs);
18✔
438
        data_flow::Subset copy_dst_subset = {linearize(copy_indvars)};
18✔
439

440
        builder.add_computational_memlet(copy_block, copy_src, copy_tasklet, "_in", copy_src_subset, pointer_type);
18✔
441
        types::Array buffer_type_ref(storage_type_, 0, {}, scalar_type, total_size);
18✔
442
        builder.add_computational_memlet(copy_block, copy_tasklet, "_out", copy_dst, copy_dst_subset, buffer_type_ref);
18✔
443
    }
18✔
444

445
    // ==================================================================
446
    // Update accesses in the main loop to use the local buffer
447
    // ==================================================================
448
    types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
21✔
449
    auto& mla = analysis_manager.get<analysis::MemoryLayoutAnalysis>();
21✔
450

451
    // Recursive helper to traverse all blocks in the loop body
452
    std::function<void(structured_control_flow::ControlFlowNode&)> rewrite_accesses;
21✔
453
    rewrite_accesses = [&](structured_control_flow::ControlFlowNode& node) {
81✔
454
        if (auto* block = dynamic_cast<structured_control_flow::Block*>(&node)) {
81✔
455
            auto& dfg = block->dataflow();
32✔
456

457
            // Collect access nodes to process (avoid iterator invalidation)
458
            std::vector<data_flow::AccessNode*> access_nodes;
32✔
459
            for (auto* access_node : dfg.data_nodes()) {
91✔
460
                if (access_node->data() == this->container_) {
91✔
461
                    access_nodes.push_back(access_node);
26✔
462
                }
26✔
463
            }
91✔
464

465
            for (auto* access : access_nodes) {
32✔
466
                // Classify memlets: group vs non-group
467
                struct MemletRewrite {
26✔
468
                    data_flow::Memlet* memlet;
26✔
469
                    data_flow::Subset local_subset;
26✔
470
                    bool is_outgoing;
26✔
471
                };
26✔
472
                std::vector<MemletRewrite> group_rewrites;
26✔
473
                bool all_in_group = true;
26✔
474

475
                for (auto& memlet : dfg.out_edges(*access)) {
27✔
476
                    if (group_memlets_.count(&memlet) == 0) {
27✔
477
                        all_in_group = false;
2✔
478
                        continue;
2✔
479
                    }
2✔
480
                    auto* acc = mla.access(memlet);
25✔
481
                    if (acc && acc->subset.size() == tile_info_.dimensions.size()) {
25✔
482
                        std::vector<symbolic::Expression> local_indices;
25✔
483
                        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
70✔
484
                            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
45✔
485
                                local_indices.push_back(symbolic::sub(acc->subset.at(d), tile_info_.bases.at(d)));
36✔
486
                            }
36✔
487
                        }
45✔
488
                        symbolic::Expression linear_idx = linearize_exprs(local_indices);
25✔
489
                        group_rewrites.push_back({&memlet, {linear_idx}, true});
25✔
490
                    }
25✔
491
                }
25✔
492
                for (auto& memlet : dfg.in_edges(*access)) {
26✔
493
                    if (group_memlets_.count(&memlet) == 0) {
×
494
                        all_in_group = false;
×
495
                        continue;
×
496
                    }
×
497
                    auto* acc = mla.access(memlet);
×
498
                    if (acc && acc->subset.size() == tile_info_.dimensions.size()) {
×
499
                        std::vector<symbolic::Expression> local_indices;
×
500
                        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
×
501
                            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
×
502
                                local_indices.push_back(symbolic::sub(acc->subset.at(d), tile_info_.bases.at(d)));
×
503
                            }
×
504
                        }
×
505
                        symbolic::Expression linear_idx = linearize_exprs(local_indices);
×
506
                        group_rewrites.push_back({&memlet, {linear_idx}, false});
×
507
                    }
×
508
                }
×
509

510
                if (group_rewrites.empty()) continue;
26✔
511

512
                if (all_in_group) {
25✔
513
                    // Simple case: all memlets in group → rewrite in-place and rename
514
                    for (auto& rw : group_rewrites) {
24✔
515
                        rw.memlet->set_subset(rw.local_subset);
24✔
516
                        rw.memlet->set_base_type(buffer_type);
24✔
517
                    }
24✔
518
                    access->data(local_name_);
24✔
519
                } else {
24✔
520
                    // Mixed case: split — create new local access node, redirect group memlets
521
                    auto& local_access = builder.add_access(*block, local_name_);
1✔
522
                    for (auto& rw : group_rewrites) {
1✔
523
                        if (rw.is_outgoing) {
1✔
524
                            // outgoing: access→tasklet  →  local_access→tasklet
525
                            auto& dst_node = rw.memlet->dst();
1✔
526
                            auto dst_conn = rw.memlet->dst_conn();
1✔
527
                            builder.remove_memlet(*block, *rw.memlet);
1✔
528
                            builder.add_memlet(
1✔
529
                                *block, local_access, "void", dst_node, dst_conn, rw.local_subset, buffer_type, {}
1✔
530
                            );
1✔
531
                        } else {
1✔
532
                            // incoming: tasklet→access  →  tasklet→local_access
533
                            auto& src_node = rw.memlet->src();
×
534
                            auto src_conn = rw.memlet->src_conn();
×
535
                            builder.remove_memlet(*block, *rw.memlet);
×
536
                            builder.add_memlet(
×
537
                                *block, src_node, src_conn, local_access, "void", rw.local_subset, buffer_type, {}
×
538
                            );
×
539
                        }
×
540
                    }
1✔
541
                }
1✔
542
            }
25✔
543
        } else if (auto* seq = dynamic_cast<structured_control_flow::Sequence*>(&node)) {
49✔
544
            for (size_t i = 0; i < seq->size(); i++) {
81✔
545
                rewrite_accesses(seq->at(i).first);
46✔
546
            }
46✔
547
        } else if (auto* loop = dynamic_cast<structured_control_flow::StructuredLoop*>(&node)) {
35✔
548
            rewrite_accesses(loop->root());
14✔
549
        } else if (auto* if_else = dynamic_cast<structured_control_flow::IfElse*>(&node)) {
14✔
550
            for (size_t i = 0; i < if_else->size(); i++) {
×
551
                rewrite_accesses(if_else->at(i).first);
×
552
            }
×
553
        }
×
554
    };
81✔
555
    rewrite_accesses(loop_.root());
21✔
556
}
21✔
557

558
void InLocalStorage::to_json(nlohmann::json& j) const {
6✔
559
    std::string loop_type;
6✔
560
    if (dynamic_cast<structured_control_flow::For*>(&loop_)) {
6✔
561
        loop_type = "for";
6✔
562
    } else if (dynamic_cast<structured_control_flow::Map*>(&loop_)) {
6✔
563
        loop_type = "map";
×
564
    } else {
×
565
        throw std::runtime_error("Unsupported loop type for serialization of loop: " + loop_.indvar()->get_name());
×
566
    }
×
567
    j["subgraph"] = {
6✔
568
        {"0", {{"element_id", this->loop_.element_id()}, {"type", loop_type}}},
6✔
569
        {"1", {{"element_id", this->access_node_.element_id()}, {"type", "access_node"}}}
6✔
570
    };
6✔
571
    j["transformation_type"] = this->name();
6✔
572
    j["container"] = container_;
6✔
573
}
6✔
574

575
InLocalStorage InLocalStorage::from_json(builder::StructuredSDFGBuilder& builder, const nlohmann::json& desc) {
1✔
576
    auto loop_id = desc["subgraph"]["0"]["element_id"].get<size_t>();
1✔
577
    auto element = builder.find_element_by_id(loop_id);
1✔
578
    if (!element) {
1✔
579
        throw InvalidTransformationDescriptionException("Element with ID " + std::to_string(loop_id) + " not found.");
×
580
    }
×
581
    auto loop = dynamic_cast<structured_control_flow::StructuredLoop*>(element);
1✔
582
    if (!loop) {
1✔
583
        throw InvalidTransformationDescriptionException(
×
584
            "Element with ID " + std::to_string(loop_id) + " is not a structured loop."
×
585
        );
×
586
    }
×
587

588
    auto access_node = dynamic_cast<
1✔
589
        data_flow::AccessNode*>(builder.find_element_by_id(desc.at("subgraph").at("1").at("element_id").get<size_t>()));
1✔
590
    if (!access_node) {
1✔
591
        throw InvalidTransformationDescriptionException(
×
592
            "Access node with ID " + std::to_string(desc.at("subgraph").at("1").at("element_id").get<size_t>()) +
×
593
            " not found."
×
594
        );
×
595
    }
×
596

597
    return InLocalStorage(*loop, *access_node);
1✔
598
}
1✔
599

600
} // namespace transformations
601
} // 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