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

daisytuner / docc / 27472437694

13 Jun 2026 04:29PM UTC coverage: 61.275% (+0.001%) from 61.274%
27472437694

Pull #759

github

web-flow
Merge 8a5ce0d62 into db7d71ecc
Pull Request #759: Removes cleanup passes from transformations

0 of 1 new or added line in 1 file covered. (0.0%)

60 existing lines in 2 files now uncovered.

36231 of 59129 relevant lines covered (61.27%)

1121.77 hits per line

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

79.02
/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) {}
31✔
32

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

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

39
    tile_info_ = TileInfo{};
31✔
40

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

50
    // Use MemoryLayoutAnalysis tile group API
51
    // Find a representative memlet from the access node to identify its group.
52
    auto& mla = analysis_manager.get<analysis::MemoryLayoutAnalysis>();
31✔
53
    const analysis::MemoryTileGroup* group = nullptr;
31✔
54
    auto& dfg = access_node_.get_parent();
31✔
55
    for (auto& memlet : dfg.out_edges(access_node_)) {
31✔
56
        auto* candidate = mla.tile_group_for(loop_, memlet);
29✔
57
        if (!candidate) {
29✔
UNCOV
58
            continue;
×
UNCOV
59
        }
×
60

61
        auto extents = candidate->tile.extents_approx();
29✔
62
        if (extents.empty()) {
29✔
UNCOV
63
            continue;
×
NEW
64
        }
×
65

66
        // Reject candidates with any unbounded-dependent extent (returned as null).
67
        bool has_null = false;
29✔
68
        for (auto& ext : extents) {
50✔
69
            if (ext.is_null()) {
50✔
70
                has_null = true;
×
71
                break;
×
UNCOV
72
            }
×
73
        }
50✔
74
        if (has_null) {
29✔
75
            continue;
×
76
        }
×
77

78
        // GPU path: accept first valid group (substitution happens later)
79
        if (storage_type_.is_nv_shared()) {
29✔
80
            group = candidate;
5✔
81
            break;
5✔
82
        }
5✔
83

84
        // CPU path: require provably integer extents
85
        bool all_integer = true;
24✔
86
        for (auto& ext : extents) {
41✔
87
            if (!SymEngine::is_a<SymEngine::Integer>(*ext)) {
41✔
88
                all_integer = false;
×
UNCOV
89
                break;
×
UNCOV
90
            }
×
91
        }
41✔
92
        if (all_integer) {
24✔
93
            group = candidate;
24✔
94
            break;
24✔
95
        }
24✔
96
    }
24✔
97
    if (!group) {
31✔
98
        return false;
2✔
99
    }
2✔
100

101
    auto& tile = group->tile;
29✔
102
    auto extents = tile.extents_approx();
29✔
103

104
    // Store group memlets for use in apply()
105
    group_memlets_.clear();
29✔
106
    group_memlets_.insert(group->memlets.begin(), group->memlets.end());
29✔
107

108
    // Store tile info (before substitution, bases/strides stay symbolic)
109
    tile_info_.dimensions = extents;
29✔
110
    tile_info_.bases = tile.min_subset;
29✔
111
    tile_info_.strides = std::vector<symbolic::Expression>(tile.layout.strides().begin(), tile.layout.strides().end());
29✔
112
    tile_info_.offset = tile.layout.offset();
29✔
113

114
    // GPU shared memory: resolve symbolic extents using GPU block sizes and
115
    // require at least one cooperative dimension
116
    if (storage_type_.is_nv_shared()) {
29✔
117
        auto ancestors = ControlFlowNode::parent_chain(loop_);
5✔
118

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

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

151
        // Criterion: All extents must now be provably integer
152
        for (auto& ext : tile_info_.dimensions) {
9✔
153
            if (!SymEngine::is_a<SymEngine::Integer>(*ext)) {
9✔
154
                return false;
2✔
155
            }
2✔
156
        }
9✔
157

158
        // Criterion: At least one cooperative dimension
159
        bool has_cooperative_dim = false;
3✔
160
        for (auto* node : ancestors) {
6✔
161
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
6✔
162
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
3✔
UNCOV
163
                    continue;
×
UNCOV
164
                }
×
165
                // A GPU dim is cooperative if its indvar does NOT appear in any tile base
166
                bool appears_in_bases = false;
3✔
167
                for (auto& base : tile_info_.bases) {
5✔
168
                    if (symbolic::uses(base, ancestor_map->indvar())) {
5✔
UNCOV
169
                        appears_in_bases = true;
×
UNCOV
170
                        break;
×
UNCOV
171
                    }
×
172
                }
5✔
173
                if (!appears_in_bases) {
3✔
174
                    has_cooperative_dim = true;
3✔
175
                    break;
3✔
176
                }
3✔
177
            }
3✔
178
        }
6✔
179
        if (!has_cooperative_dim) {
3✔
UNCOV
180
            return false;
×
181
        }
×
182
    }
3✔
183

184
    return true;
27✔
185
}
29✔
186

187
void InLocalStorage::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
20✔
188
    auto& sdfg = builder.subject();
20✔
189

190
    auto parent_node = loop_.get_parent();
20✔
191
    auto parent = dynamic_cast<structured_control_flow::Sequence*>(parent_node);
20✔
192
    if (!parent) {
20✔
193
        throw InvalidSDFGException("InLocalStorage: Parent of loop must be a Sequence!");
×
UNCOV
194
    }
×
195

196
    // We replace all relevant memlets with flat local indices
197
    // Thus, we now use a flat pointer to index into container
198
    // Remark: sdfg.type may return an opaque pointer, so use
199
    //         memlet instead
200
    auto* memlet = *group_memlets_.begin();
20✔
201
    types::Scalar scalar_type(memlet->base_type().primitive_type());
20✔
202
    types::Pointer pointer_type(scalar_type);
20✔
203

204
    // Create local buffer name
205
    local_name_ = builder.find_new_name("__daisy_in_local_storage_" + this->container_);
20✔
206

207
    // Collect varying dimensions (extent > 1) and compute buffer layout
208
    std::vector<size_t> varying_dims;
20✔
209
    std::vector<symbolic::Expression> dim_sizes;
20✔
210
    for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
55✔
211
        auto& dim_size = tile_info_.dimensions.at(d);
35✔
212
        if (!symbolic::eq(dim_size, symbolic::integer(1))) {
35✔
213
            varying_dims.push_back(d);
27✔
214
            dim_sizes.push_back(dim_size);
27✔
215
        }
27✔
216
    }
35✔
217

218
    // Compute total buffer size
219
    symbolic::Expression total_size = symbolic::integer(1);
20✔
220
    for (auto& ds : dim_sizes) {
27✔
221
        total_size = symbolic::mul(total_size, ds);
27✔
222
    }
27✔
223

224
    // Helper: build linearized local index from per-dimension symbolic expressions
225
    auto linearize_exprs = [&](const std::vector<symbolic::Expression>& indices) -> symbolic::Expression {
41✔
226
        symbolic::Expression linear_idx = symbolic::integer(0);
41✔
227
        symbolic::Expression stride = symbolic::integer(1);
41✔
228
        for (int i = indices.size() - 1; i >= 0; i--) {
100✔
229
            linear_idx = symbolic::add(linear_idx, symbolic::mul(indices[i], stride));
59✔
230
            stride = symbolic::mul(stride, dim_sizes[i]);
59✔
231
        }
59✔
232
        return linear_idx;
41✔
233
    };
41✔
234

235
    // Helper: build linearized local index from per-dimension indvars (symbols)
236
    auto linearize = [&](const std::vector<symbolic::Symbol>& indvars) -> symbolic::Expression {
20✔
237
        std::vector<symbolic::Expression> exprs(indvars.begin(), indvars.end());
17✔
238
        return linearize_exprs(exprs);
17✔
239
    };
17✔
240

241
    // Helper: build source subset (base[d] + copy_indvar[d]) for original container
242
    auto build_original_subset = [&](const std::vector<symbolic::Expression>& copy_indices) -> data_flow::Subset {
20✔
243
        std::vector<symbolic::Expression> full_indices;
20✔
244
        size_t var_idx = 0;
20✔
245
        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
55✔
246
            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
35✔
247
                full_indices.push_back(symbolic::add(tile_info_.bases.at(d), copy_indices.at(var_idx++)));
27✔
248
            } else {
27✔
249
                full_indices.push_back(tile_info_.bases.at(d));
8✔
250
            }
8✔
251
        }
35✔
252

253
        symbolic::Expression linear = tile_info_.offset;
20✔
254
        for (size_t d = 0; d < full_indices.size(); d++) {
55✔
255
            linear = symbolic::add(linear, symbolic::mul(tile_info_.strides.at(d), full_indices.at(d)));
35✔
256
        }
35✔
257
        return {linear};
20✔
258
    };
20✔
259

260
    // ==================================================================
261
    // Branch: GPU cooperative path vs CPU sequential path
262
    // ==================================================================
263
    if (storage_type_.is_nv_shared()) {
20✔
264
        // ============================================================
265
        // GPU COOPERATIVE PATH
266
        // ============================================================
267
        auto ancestors = ControlFlowNode::parent_chain(loop_);
3✔
268

269
        // Collect cooperative GPU dimensions (indvar not in tile bases)
270
        struct CoopDim {
3✔
271
            symbolic::Symbol indvar;
3✔
272
            symbolic::Integer block_size;
3✔
273
            gpu::GPUDimension dimension;
3✔
274
        };
3✔
275
        std::vector<CoopDim> coop_dims;
3✔
276

277
        for (auto* node : ancestors) {
15✔
278
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
15✔
279
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
6✔
UNCOV
280
                    continue;
×
UNCOV
281
                }
×
282
                bool appears_in_bases = false;
6✔
283
                for (auto& base : tile_info_.bases) {
8✔
284
                    if (symbolic::uses(base, ancestor_map->indvar())) {
8✔
285
                        appears_in_bases = true;
2✔
286
                        break;
2✔
287
                    }
2✔
288
                }
8✔
289
                if (!appears_in_bases) {
6✔
290
                    coop_dims.push_back(
4✔
291
                        {ancestor_map->indvar(),
4✔
292
                         gpu::gpu_block_size(ancestor_map->schedule_type()),
4✔
293
                         gpu::gpu_dimension(ancestor_map->schedule_type())}
4✔
294
                    );
4✔
295
                }
4✔
296
            }
6✔
297
        }
15✔
298

299
        // Compute total cooperative thread count
300
        symbolic::Expression total_coop_threads = symbolic::integer(1);
3✔
301
        for (auto& cd : coop_dims) {
4✔
302
            total_coop_threads = symbolic::mul(total_coop_threads, cd.block_size);
4✔
303
        }
4✔
304

305
        // Create the local buffer with NV_Shared storage
306
        types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
3✔
307
        builder.add_container(local_name_, buffer_type);
3✔
308

309
        // Emit: barrier → guarded cooperative copy → barrier → loop
310
        // 1. Barrier before copy
311
        auto& barrier_block1 = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
3✔
312
        builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block1, {});
3✔
313

314
        // 2. Cooperative copy with if_else guard
315
        // Flatten cooperative thread index: coop_flat = sum(indvar[i] * product(block_size[j] for j>i))
316
        symbolic::Expression coop_flat = symbolic::integer(0);
3✔
317
        symbolic::Expression coop_stride = symbolic::integer(1);
3✔
318
        for (int i = coop_dims.size() - 1; i >= 0; i--) {
7✔
319
            coop_flat = symbolic::add(coop_flat, symbolic::mul(coop_dims[i].indvar, coop_stride));
4✔
320
            coop_stride = symbolic::mul(coop_stride, coop_dims[i].block_size);
4✔
321
        }
4✔
322

323
        // Each thread loads elements strided by total_coop_threads
324
        // Thread t loads elements: t, t + total_threads, t + 2*total_threads, ...
325
        // We emit a loop: for (idx = coop_flat; idx < total_size; idx += total_coop_threads)
326
        auto idx_name = builder.find_new_name("__daisy_ils_coop_" + this->container_);
3✔
327
        types::Scalar idx_type(types::PrimitiveType::UInt64);
3✔
328
        builder.add_container(idx_name, idx_type);
3✔
329
        auto idx_var = symbolic::symbol(idx_name);
3✔
330

331
        auto copy_init = coop_flat;
3✔
332
        auto copy_condition = symbolic::Lt(idx_var, total_size);
3✔
333
        auto copy_update = symbolic::add(idx_var, total_coop_threads);
3✔
334

335
        auto& copy_loop = builder.add_map_before(
3✔
336
            *parent,
3✔
337
            loop_,
3✔
338
            idx_var,
3✔
339
            copy_condition,
3✔
340
            copy_init,
3✔
341
            copy_update,
3✔
342
            structured_control_flow::ScheduleType_Sequential::create(),
3✔
343
            {},
3✔
344
            loop_.debug_info()
3✔
345
        );
3✔
346

347
        // Decompose flat idx back into per-dimension indices for source subset
348
        // idx maps to varying_dims in row-major order
349
        auto& copy_scope = copy_loop.root();
3✔
350
        auto& copy_block = builder.add_block(copy_scope);
3✔
351
        auto& copy_src = builder.add_access(copy_block, this->container_);
3✔
352
        auto& copy_dst = builder.add_access(copy_block, local_name_);
3✔
353
        auto& copy_tasklet = builder.add_tasklet(copy_block, data_flow::TaskletCode::assign, "_out", {"_in"});
3✔
354

355
        // Decompose idx_var into per-dim indices
356
        std::vector<symbolic::Expression> copy_indices;
3✔
357
        symbolic::Expression remainder = idx_var;
3✔
358
        for (size_t i = 0; i < dim_sizes.size(); i++) {
6✔
359
            if (i < dim_sizes.size() - 1) {
3✔
360
                // integer division: idx / (product of remaining dims)
UNCOV
361
                symbolic::Expression divisor = symbolic::integer(1);
×
UNCOV
362
                for (size_t j = i + 1; j < dim_sizes.size(); j++) {
×
UNCOV
363
                    divisor = symbolic::mul(divisor, dim_sizes[j]);
×
UNCOV
364
                }
×
UNCOV
365
                auto quotient = symbolic::div(remainder, divisor);
×
UNCOV
366
                copy_indices.push_back(quotient);
×
UNCOV
367
                remainder = symbolic::mod(remainder, divisor);
×
368
            } else {
3✔
369
                copy_indices.push_back(remainder);
3✔
370
            }
3✔
371
        }
3✔
372

373
        auto copy_src_subset = build_original_subset(copy_indices);
3✔
374
        data_flow::Subset copy_dst_subset = {idx_var};
3✔
375

376
        builder.add_computational_memlet(copy_block, copy_src, copy_tasklet, "_in", copy_src_subset, pointer_type);
3✔
377
        builder.add_computational_memlet(copy_block, copy_tasklet, "_out", copy_dst, copy_dst_subset, buffer_type);
3✔
378

379
        // 3. Barrier after copy
380
        auto& barrier_block2 = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
3✔
381
        builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block2, {});
3✔
382
    } else {
17✔
383
        // ============================================================
384
        // CPU SEQUENTIAL PATH
385
        // ============================================================
386
        // Create the local buffer with specified storage type
387
        types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
17✔
388
        builder.add_container(local_name_, buffer_type);
17✔
389

390
        std::vector<symbolic::Symbol> copy_indvars;
17✔
391
        structured_control_flow::Sequence* copy_scope =
17✔
392
            &builder.add_sequence_before(*parent, loop_, {}, loop_.debug_info());
17✔
393
        for (size_t i = 0; i < varying_dims.size(); i++) {
41✔
394
            size_t d = varying_dims[i];
24✔
395
            auto indvar_name = builder.find_new_name("__daisy_ils_" + this->container_ + "_d" + std::to_string(d));
24✔
396
            types::Scalar indvar_type(types::PrimitiveType::UInt64);
24✔
397
            builder.add_container(indvar_name, indvar_type);
24✔
398
            auto indvar = symbolic::symbol(indvar_name);
24✔
399
            copy_indvars.push_back(indvar);
24✔
400

401
            auto init = symbolic::integer(0);
24✔
402
            auto condition = symbolic::Lt(indvar, dim_sizes[i]);
24✔
403
            auto update = symbolic::add(indvar, symbolic::integer(1));
24✔
404

405
            auto& copy_loop = builder.add_map(
24✔
406
                *copy_scope,
24✔
407
                indvar,
24✔
408
                condition,
24✔
409
                init,
24✔
410
                update,
24✔
411
                structured_control_flow::ScheduleType_Sequential::create(),
24✔
412
                {},
24✔
413
                loop_.debug_info()
24✔
414
            );
24✔
415
            copy_scope = &copy_loop.root();
24✔
416
        }
24✔
417

418
        // Create copy block
419
        auto& copy_block = builder.add_block(*copy_scope);
17✔
420
        auto& copy_src = builder.add_access(copy_block, this->container_);
17✔
421
        auto& copy_dst = builder.add_access(copy_block, local_name_);
17✔
422
        auto& copy_tasklet = builder.add_tasklet(copy_block, data_flow::TaskletCode::assign, "_out", {"_in"});
17✔
423

424
        std::vector<symbolic::Expression> copy_exprs(copy_indvars.begin(), copy_indvars.end());
17✔
425
        auto copy_src_subset = build_original_subset(copy_exprs);
17✔
426
        data_flow::Subset copy_dst_subset = {linearize(copy_indvars)};
17✔
427

428
        builder.add_computational_memlet(copy_block, copy_src, copy_tasklet, "_in", copy_src_subset, pointer_type);
17✔
429
        types::Array buffer_type_ref(storage_type_, 0, {}, scalar_type, total_size);
17✔
430
        builder.add_computational_memlet(copy_block, copy_tasklet, "_out", copy_dst, copy_dst_subset, buffer_type_ref);
17✔
431
    }
17✔
432

433
    // ==================================================================
434
    // Update accesses in the main loop to use the local buffer
435
    // ==================================================================
436
    types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
20✔
437
    auto& mla = analysis_manager.get<analysis::MemoryLayoutAnalysis>();
20✔
438

439
    // Recursive helper to traverse all blocks in the loop body
440
    std::function<void(structured_control_flow::ControlFlowNode&)> rewrite_accesses;
20✔
441
    rewrite_accesses = [&](structured_control_flow::ControlFlowNode& node) {
77✔
442
        if (auto* block = dynamic_cast<structured_control_flow::Block*>(&node)) {
77✔
443
            auto& dfg = block->dataflow();
29✔
444

445
            // Collect access nodes to process (avoid iterator invalidation)
446
            std::vector<data_flow::AccessNode*> access_nodes;
29✔
447
            for (auto* access_node : dfg.data_nodes()) {
81✔
448
                if (access_node->data() == this->container_) {
81✔
449
                    access_nodes.push_back(access_node);
26✔
450
                }
26✔
451
            }
81✔
452

453
            for (auto* access : access_nodes) {
29✔
454
                // Classify memlets: group vs non-group
455
                struct MemletRewrite {
26✔
456
                    data_flow::Memlet* memlet;
26✔
457
                    data_flow::Subset local_subset;
26✔
458
                    bool is_outgoing;
26✔
459
                };
26✔
460
                std::vector<MemletRewrite> group_rewrites;
26✔
461
                bool all_in_group = true;
26✔
462

463
                for (auto& memlet : dfg.out_edges(*access)) {
26✔
464
                    if (group_memlets_.count(&memlet) == 0) {
26✔
465
                        all_in_group = false;
2✔
466
                        continue;
2✔
467
                    }
2✔
468
                    auto* acc = mla.access(memlet);
24✔
469
                    if (acc && acc->subset.size() == tile_info_.dimensions.size()) {
24✔
470
                        std::vector<symbolic::Expression> local_indices;
24✔
471
                        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
67✔
472
                            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
43✔
473
                                local_indices.push_back(symbolic::sub(acc->subset.at(d), tile_info_.bases.at(d)));
35✔
474
                            }
35✔
475
                        }
43✔
476
                        symbolic::Expression linear_idx = linearize_exprs(local_indices);
24✔
477
                        group_rewrites.push_back({&memlet, {linear_idx}, true});
24✔
478
                    }
24✔
479
                }
24✔
480
                for (auto& memlet : dfg.in_edges(*access)) {
26✔
UNCOV
481
                    if (group_memlets_.count(&memlet) == 0) {
×
UNCOV
482
                        all_in_group = false;
×
UNCOV
483
                        continue;
×
UNCOV
484
                    }
×
UNCOV
485
                    auto* acc = mla.access(memlet);
×
UNCOV
486
                    if (acc && acc->subset.size() == tile_info_.dimensions.size()) {
×
UNCOV
487
                        std::vector<symbolic::Expression> local_indices;
×
UNCOV
488
                        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
×
UNCOV
489
                            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
×
UNCOV
490
                                local_indices.push_back(symbolic::sub(acc->subset.at(d), tile_info_.bases.at(d)));
×
UNCOV
491
                            }
×
UNCOV
492
                        }
×
493
                        symbolic::Expression linear_idx = linearize_exprs(local_indices);
×
494
                        group_rewrites.push_back({&memlet, {linear_idx}, false});
×
495
                    }
×
496
                }
×
497

498
                if (group_rewrites.empty()) continue;
26✔
499

500
                if (all_in_group) {
24✔
501
                    // Simple case: all memlets in group → rewrite in-place and rename
502
                    for (auto& rw : group_rewrites) {
24✔
503
                        rw.memlet->set_subset(rw.local_subset);
24✔
504
                        rw.memlet->set_base_type(buffer_type);
24✔
505
                    }
24✔
506
                    access->data(local_name_);
24✔
507
                } else {
24✔
508
                    // Mixed case: split — create new local access node, redirect group memlets
UNCOV
509
                    auto& local_access = builder.add_access(*block, local_name_);
×
UNCOV
510
                    for (auto& rw : group_rewrites) {
×
UNCOV
511
                        if (rw.is_outgoing) {
×
512
                            // outgoing: access→tasklet  →  local_access→tasklet
UNCOV
513
                            auto& dst_node = rw.memlet->dst();
×
UNCOV
514
                            auto dst_conn = rw.memlet->dst_conn();
×
UNCOV
515
                            builder.remove_memlet(*block, *rw.memlet);
×
UNCOV
516
                            builder.add_memlet(
×
UNCOV
517
                                *block, local_access, "void", dst_node, dst_conn, rw.local_subset, buffer_type, {}
×
UNCOV
518
                            );
×
UNCOV
519
                        } else {
×
520
                            // incoming: tasklet→access  →  tasklet→local_access
521
                            auto& src_node = rw.memlet->src();
×
522
                            auto src_conn = rw.memlet->src_conn();
×
523
                            builder.remove_memlet(*block, *rw.memlet);
×
UNCOV
524
                            builder.add_memlet(
×
525
                                *block, src_node, src_conn, local_access, "void", rw.local_subset, buffer_type, {}
×
526
                            );
×
527
                        }
×
528
                    }
×
529
                }
×
530
            }
24✔
531
        } else if (auto* seq = dynamic_cast<structured_control_flow::Sequence*>(&node)) {
48✔
532
            for (size_t i = 0; i < seq->size(); i++) {
77✔
533
                rewrite_accesses(seq->at(i).first);
43✔
534
            }
43✔
535
        } else if (auto* loop = dynamic_cast<structured_control_flow::StructuredLoop*>(&node)) {
34✔
536
            rewrite_accesses(loop->root());
14✔
537
        } else if (auto* if_else = dynamic_cast<structured_control_flow::IfElse*>(&node)) {
14✔
538
            for (size_t i = 0; i < if_else->size(); i++) {
×
539
                rewrite_accesses(if_else->at(i).first);
×
540
            }
×
541
        }
×
542
    };
77✔
543
    rewrite_accesses(loop_.root());
20✔
544
}
20✔
545

546
void InLocalStorage::to_json(nlohmann::json& j) const {
6✔
547
    std::string loop_type;
6✔
548
    if (dynamic_cast<structured_control_flow::For*>(&loop_)) {
6✔
549
        loop_type = "for";
6✔
550
    } else if (dynamic_cast<structured_control_flow::Map*>(&loop_)) {
6✔
551
        loop_type = "map";
×
552
    } else {
×
553
        throw std::runtime_error("Unsupported loop type for serialization of loop: " + loop_.indvar()->get_name());
×
UNCOV
554
    }
×
555
    j["subgraph"] = {
6✔
556
        {"0", {{"element_id", this->loop_.element_id()}, {"type", loop_type}}},
6✔
557
        {"1", {{"element_id", this->access_node_.element_id()}, {"type", "access_node"}}}
6✔
558
    };
6✔
559
    j["transformation_type"] = this->name();
6✔
560
    j["container"] = container_;
6✔
561
}
6✔
562

563
InLocalStorage InLocalStorage::from_json(builder::StructuredSDFGBuilder& builder, const nlohmann::json& desc) {
1✔
564
    auto loop_id = desc["subgraph"]["0"]["element_id"].get<size_t>();
1✔
565
    auto element = builder.find_element_by_id(loop_id);
1✔
566
    if (!element) {
1✔
UNCOV
567
        throw InvalidTransformationDescriptionException("Element with ID " + std::to_string(loop_id) + " not found.");
×
UNCOV
568
    }
×
569
    auto loop = dynamic_cast<structured_control_flow::StructuredLoop*>(element);
1✔
570
    if (!loop) {
1✔
UNCOV
571
        throw InvalidTransformationDescriptionException(
×
UNCOV
572
            "Element with ID " + std::to_string(loop_id) + " is not a structured loop."
×
UNCOV
573
        );
×
UNCOV
574
    }
×
575

576
    auto access_node = dynamic_cast<
1✔
577
        data_flow::AccessNode*>(builder.find_element_by_id(desc.at("subgraph").at("1").at("element_id").get<size_t>()));
1✔
578
    if (!access_node) {
1✔
579
        throw InvalidTransformationDescriptionException(
×
580
            "Access node with ID " + std::to_string(desc.at("subgraph").at("1").at("element_id").get<size_t>()) +
×
UNCOV
581
            " not found."
×
UNCOV
582
        );
×
583
    }
×
584

585
    return InLocalStorage(*loop, *access_node);
1✔
586
}
1✔
587

588
} // namespace transformations
589
} // 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