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

daisytuner / docc / 25206942913

01 May 2026 07:44AM UTC coverage: 64.532%. First build
25206942913

push

github

web-flow
Merge pull request #694 from daisytuner/local-storage-type

adds StorageType to InLocalStorage and OutLocalStorage

438 of 484 new or added lines in 2 files covered. (90.5%)

31254 of 48432 relevant lines covered (64.53%)

684.19 hits per line

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

86.79
/opt/src/transformations/out_local_storage.cpp
1
#include "sdfg/transformations/out_local_storage.h"
2

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

6
#include "sdfg/analysis/memory_layout_analysis.h"
7
#include "sdfg/analysis/scope_analysis.h"
8
#include "sdfg/analysis/users.h"
9
#include "sdfg/builder/structured_sdfg_builder.h"
10
#include "sdfg/data_flow/access_node.h"
11
#include "sdfg/data_flow/library_nodes/barrier_local_node.h"
12
#include "sdfg/data_flow/memlet.h"
13
#include "sdfg/passes/structured_control_flow/dead_cfg_elimination.h"
14
#include "sdfg/passes/structured_control_flow/sequence_fusion.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
OutLocalStorage::OutLocalStorage(
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) {};
25✔
32

33
std::string OutLocalStorage::name() const { return "OutLocalStorage"; };
5✔
34

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

39
    tile_info_ = TileInfo{};
23✔
40

41
    // Criterion: Container must exist
42
    if (!sdfg.exists(this->container_)) {
23✔
43
        return false;
×
44
    }
×
45

46
    auto& type = sdfg.type(this->container_);
23✔
47

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

55
    // Criterion: Container must have writes (this is OutLocalStorage, not InLocalStorage)
56
    if (body_users.writes(this->container_).empty()) {
21✔
57
        return false;
1✔
58
    }
1✔
59

60
    // Determine if container is also read (read-write vs write-only)
61
    tile_info_.has_read = !body_users.reads(this->container_).empty();
20✔
62

63
    // Handle scalar containers: no tile needed, dimensions stay empty
64
    if (type.type_id() == types::TypeID::Scalar) {
20✔
65
        return true;
1✔
66
    }
1✔
67

68
    // For Array/Pointer types: use MemoryLayoutAnalysis tile API
69
    if (type.type_id() != types::TypeID::Pointer && type.type_id() != types::TypeID::Array) {
19✔
70
        return false;
×
71
    }
×
72

73
    auto& mla = analysis_manager.get<analysis::MemoryLayoutAnalysis>();
19✔
74
    auto* tile = mla.tile(loop_, this->container_);
19✔
75
    if (!tile) {
19✔
76
        return false;
1✔
77
    }
1✔
78

79
    // Get overapproximated extents (integer upper bounds)
80
    auto extents = tile->extents_approx();
18✔
81
    if (extents.empty()) {
18✔
82
        return false;
×
83
    }
×
84

85
    // Store tile info (before substitution, bases/strides stay symbolic)
86
    tile_info_.dimensions = extents;
18✔
87
    tile_info_.bases = tile->min_subset;
18✔
88
    tile_info_.strides =
18✔
89
        std::vector<symbolic::Expression>(tile->layout.strides().begin(), tile->layout.strides().end());
18✔
90
    tile_info_.offset = tile->layout.offset();
18✔
91

92
    // GPU shared memory: resolve symbolic extents using GPU block sizes and
93
    // require at least one cooperative dimension
94
    if (storage_type_.is_nv_shared()) {
18✔
95
        auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
6✔
96
        auto ancestors = scope_analysis.ancestor_scopes(&loop_);
6✔
97

98
        // Build substitution map: symbolic GPU map bounds → integer block sizes
99
        for (auto* node : ancestors) {
26✔
100
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
26✔
101
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
10✔
NEW
102
                    continue;
×
NEW
103
                }
×
104
                auto block_size = gpu::gpu_block_size(ancestor_map->schedule_type());
10✔
105
                // Extract symbolic bound from condition: Lt(indvar, BOUND)
106
                auto condition = ancestor_map->condition();
10✔
107
                if (SymEngine::is_a<SymEngine::StrictLessThan>(*condition)) {
10✔
108
                    auto stl = SymEngine::rcp_static_cast<const SymEngine::StrictLessThan>(condition);
10✔
109
                    auto rhs = stl->get_args()[1];
10✔
110
                    auto iter_count = symbolic::sub(rhs, ancestor_map->init());
10✔
111
                    if (!SymEngine::is_a<SymEngine::Integer>(*iter_count)) {
10✔
112
                        // Symbolic bound — substitute with block size in extents and bases
113
                        for (auto& ext : tile_info_.dimensions) {
17✔
114
                            ext = symbolic::simplify(symbolic::subs(ext, iter_count, block_size));
17✔
115
                        }
17✔
116
                        for (auto& base : tile_info_.bases) {
17✔
117
                            base = symbolic::simplify(symbolic::subs(base, iter_count, block_size));
17✔
118
                        }
17✔
119
                    }
10✔
120
                }
10✔
121
            }
10✔
122
        }
26✔
123

124
        // Criterion: All extents must now be provably integer
125
        for (auto& ext : tile_info_.dimensions) {
10✔
126
            if (!SymEngine::is_a<SymEngine::Integer>(*ext)) {
10✔
127
                return false;
2✔
128
            }
2✔
129
        }
10✔
130

131
        // Criterion: At least one cooperative dimension
132
        bool has_cooperative_dim = false;
4✔
133
        for (auto* node : ancestors) {
12✔
134
            if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
12✔
135
                if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
6✔
NEW
136
                    continue;
×
NEW
137
                }
×
138
                bool appears_in_bases = false;
6✔
139
                for (auto& base : tile_info_.bases) {
9✔
140
                    if (symbolic::uses(base, ancestor_map->indvar())) {
9✔
141
                        appears_in_bases = true;
2✔
142
                        break;
2✔
143
                    }
2✔
144
                }
9✔
145
                if (!appears_in_bases) {
6✔
146
                    has_cooperative_dim = true;
4✔
147
                    break;
4✔
148
                }
4✔
149
            }
6✔
150
        }
12✔
151
        if (!has_cooperative_dim) {
4✔
NEW
152
            return false;
×
NEW
153
        }
×
154
    } else {
12✔
155
        // CPU path: All extents must be provably integer
156
        for (auto& ext : tile_info_.dimensions) {
17✔
157
            if (!SymEngine::is_a<SymEngine::Integer>(*ext)) {
17✔
NEW
158
                return false;
×
NEW
159
            }
×
160
        }
17✔
161
    }
12✔
162

163
    return true;
16✔
164
}
18✔
165

166
void OutLocalStorage::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
15✔
167
    auto& sdfg = builder.subject();
15✔
168
    auto& users = analysis_manager.get<analysis::Users>();
15✔
169
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
15✔
170

171
    auto parent_node = scope_analysis.parent_scope(&loop_);
15✔
172
    auto parent = dynamic_cast<structured_control_flow::Sequence*>(parent_node);
15✔
173
    if (!parent) {
15✔
174
        throw InvalidSDFGException("OutLocalStorage: Parent of loop must be a Sequence!");
×
175
    }
×
176

177
    // Get type information
178
    auto& type = sdfg.type(this->container_);
15✔
179
    types::Scalar scalar_type(type.primitive_type());
15✔
180

181
    // Create local buffer name
182
    local_name_ = "__daisy_out_local_storage_" + this->container_;
15✔
183

184
    // ========================================================================
185
    // SCALAR PATH: tile_info_.dimensions is empty
186
    // ========================================================================
187
    if (tile_info_.dimensions.empty()) {
15✔
188
        // Create scalar local buffer
189
        builder.add_container(local_name_, scalar_type);
1✔
190

191
        // Get the access subset from the first user (all scalar, so empty subset)
192
        analysis::UsersView body_users(users, loop_.root());
1✔
193
        auto accesses = body_users.uses(this->container_);
1✔
194
        auto first_access = accesses.at(0);
1✔
195
        auto first_subset = first_access->subsets().at(0);
1✔
196

197
        // Init block (copy from container to local) - before loop
198
        if (tile_info_.has_read) {
1✔
199
            auto& init_block = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
1✔
200
            auto& init_src = builder.add_access(init_block, this->container_);
1✔
201
            auto& init_dst = builder.add_access(init_block, local_name_);
1✔
202
            auto& init_tasklet = builder.add_tasklet(init_block, data_flow::TaskletCode::assign, "_out", {"_in"});
1✔
203
            builder.add_computational_memlet(init_block, init_src, init_tasklet, "_in", first_subset, type);
1✔
204
            builder.add_computational_memlet(init_block, init_tasklet, "_out", init_dst, {}, scalar_type);
1✔
205
        }
1✔
206

207
        // Writeback block (copy from local to container) - after loop
208
        {
1✔
209
            auto& wb_block = builder.add_block_after(*parent, loop_, {}, loop_.debug_info());
1✔
210
            auto& wb_src = builder.add_access(wb_block, local_name_);
1✔
211
            auto& wb_dst = builder.add_access(wb_block, this->container_);
1✔
212
            auto& wb_tasklet = builder.add_tasklet(wb_block, data_flow::TaskletCode::assign, "_out", {"_in"});
1✔
213
            builder.add_computational_memlet(wb_block, wb_src, wb_tasklet, "_in", {}, scalar_type);
1✔
214
            builder.add_computational_memlet(wb_block, wb_tasklet, "_out", wb_dst, first_subset, type);
1✔
215
        }
1✔
216

217
        // Rewrite body accesses to use scalar local
218
        for (auto* user : body_users.uses(this->container_)) {
2✔
219
            auto element = user->element();
2✔
220
            if (auto access = dynamic_cast<data_flow::AccessNode*>(element)) {
2✔
221
                for (auto& iedge : access->get_parent().in_edges(*access)) {
2✔
222
                    auto memlet = &iedge;
1✔
223
                    memlet->set_subset({});
1✔
224
                    memlet->set_base_type(scalar_type);
1✔
225
                }
1✔
226
                for (auto& oedge : access->get_parent().out_edges(*access)) {
2✔
227
                    auto memlet = &oedge;
1✔
228
                    memlet->set_subset({});
1✔
229
                    memlet->set_base_type(scalar_type);
1✔
230
                }
1✔
231
            }
2✔
232
        }
2✔
233

234
        // Replace container name in the loop body
235
        loop_.replace(symbolic::symbol(this->container_), symbolic::symbol(local_name_));
1✔
236
    }
1✔
237
    // ========================================================================
238
    // ARRAY PATH: tile_info_.dimensions is non-empty
239
    // ========================================================================
240
    else {
14✔
241
        // Collect varying dimensions (extent > 1) and compute buffer layout
242
        std::vector<size_t> varying_dims;
14✔
243
        std::vector<symbolic::Expression> dim_sizes;
14✔
244
        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
35✔
245
            auto& dim_size = tile_info_.dimensions.at(d);
21✔
246
            if (!symbolic::eq(dim_size, symbolic::integer(1))) {
21✔
247
                varying_dims.push_back(d);
16✔
248
                dim_sizes.push_back(dim_size);
16✔
249
            }
16✔
250
        }
21✔
251

252
        // Compute total buffer size
253
        symbolic::Expression total_size = symbolic::integer(1);
14✔
254
        for (auto& ds : dim_sizes) {
16✔
255
            total_size = symbolic::mul(total_size, ds);
16✔
256
        }
16✔
257

258
        // Create the local buffer with specified storage type
259
        types::Array buffer_type(storage_type_, 0, {}, scalar_type, total_size);
14✔
260
        builder.add_container(local_name_, buffer_type);
14✔
261

262
        // Helper: build linearized local index from per-dimension expressions
263
        auto linearize_exprs = [&](const std::vector<symbolic::Expression>& indices) -> symbolic::Expression {
17✔
264
            symbolic::Expression linear_idx = symbolic::integer(0);
17✔
265
            symbolic::Expression stride = symbolic::integer(1);
17✔
266
            for (int i = indices.size() - 1; i >= 0; i--) {
38✔
267
                linear_idx = symbolic::add(linear_idx, symbolic::mul(indices[i], stride));
21✔
268
                stride = symbolic::mul(stride, dim_sizes[i]);
21✔
269
            }
21✔
270
            return linear_idx;
17✔
271
        };
17✔
272

273
        // Helper: build linearized local index from per-dimension indvars (symbols)
274
        auto linearize = [&](const std::vector<symbolic::Symbol>& indvars) -> symbolic::Expression {
17✔
275
            std::vector<symbolic::Expression> exprs(indvars.begin(), indvars.end());
17✔
276
            return linearize_exprs(exprs);
17✔
277
        };
17✔
278

279
        // Helper: build source subset (base[d] + copy_indvar[d]) for original container
280
        bool is_pointer = (type.type_id() == types::TypeID::Pointer);
14✔
281
        auto build_original_subset = [&](const std::vector<symbolic::Expression>& copy_indices) -> data_flow::Subset {
22✔
282
            std::vector<symbolic::Expression> full_indices;
22✔
283
            size_t var_idx = 0;
22✔
284
            for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
55✔
285
                if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
33✔
286
                    full_indices.push_back(symbolic::add(tile_info_.bases.at(d), copy_indices.at(var_idx++)));
26✔
287
                } else {
26✔
288
                    full_indices.push_back(tile_info_.bases.at(d));
7✔
289
                }
7✔
290
            }
33✔
291

292
            if (is_pointer) {
22✔
293
                symbolic::Expression linear = tile_info_.offset;
22✔
294
                for (size_t d = 0; d < full_indices.size(); d++) {
55✔
295
                    linear = symbolic::add(linear, symbolic::mul(tile_info_.strides.at(d), full_indices.at(d)));
33✔
296
                }
33✔
297
                return {linear};
22✔
298
            } else {
22✔
299
                return data_flow::Subset(full_indices.begin(), full_indices.end());
×
300
            }
×
301
        };
22✔
302

303
        if (storage_type_.is_nv_shared()) {
14✔
304
            // ============================================================
305
            // GPU COOPERATIVE PATH
306
            // ============================================================
307
            auto ancestors = scope_analysis.ancestor_scopes(&loop_);
4✔
308

309
            // Collect cooperative GPU dimensions
310
            struct CoopDim {
4✔
311
                symbolic::Symbol indvar;
4✔
312
                symbolic::Integer block_size;
4✔
313
                gpu::GPUDimension dimension;
4✔
314
            };
4✔
315
            std::vector<CoopDim> coop_dims;
4✔
316

317
            for (auto* node : ancestors) {
20✔
318
                if (auto* ancestor_map = dynamic_cast<structured_control_flow::Map*>(node)) {
20✔
319
                    if (!gpu::is_gpu_schedule(ancestor_map->schedule_type())) {
8✔
NEW
320
                        continue;
×
NEW
321
                    }
×
322
                    bool appears_in_bases = false;
8✔
323
                    for (auto& base : tile_info_.bases) {
11✔
324
                        if (symbolic::uses(base, ancestor_map->indvar())) {
11✔
325
                            appears_in_bases = true;
3✔
326
                            break;
3✔
327
                        }
3✔
328
                    }
11✔
329
                    if (!appears_in_bases) {
8✔
330
                        coop_dims.push_back(
5✔
331
                            {ancestor_map->indvar(),
5✔
332
                             gpu::gpu_block_size(ancestor_map->schedule_type()),
5✔
333
                             gpu::gpu_dimension(ancestor_map->schedule_type())}
5✔
334
                        );
5✔
335
                    }
5✔
336
                }
8✔
337
            }
20✔
338

339
            // Compute total cooperative thread count
340
            symbolic::Expression total_coop_threads = symbolic::integer(1);
4✔
341
            for (auto& cd : coop_dims) {
5✔
342
                total_coop_threads = symbolic::mul(total_coop_threads, cd.block_size);
5✔
343
            }
5✔
344

345
            // Flatten cooperative thread index
346
            symbolic::Expression coop_flat = symbolic::integer(0);
4✔
347
            symbolic::Expression coop_stride = symbolic::integer(1);
4✔
348
            for (int i = coop_dims.size() - 1; i >= 0; i--) {
9✔
349
                coop_flat = symbolic::add(coop_flat, symbolic::mul(coop_dims[i].indvar, coop_stride));
5✔
350
                coop_stride = symbolic::mul(coop_stride, coop_dims[i].block_size);
5✔
351
            }
5✔
352

353
            // INIT: barrier → cooperative copy-in → barrier (if has_read)
354
            if (tile_info_.has_read) {
4✔
355
                // Barrier before init
356
                auto& barrier_block1 = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
1✔
357
                builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block1, {});
1✔
358

359
                // Cooperative copy-in loop
360
                auto idx_name = "__daisy_ols_coop_init_" + this->container_;
1✔
361
                types::Scalar idx_type(types::PrimitiveType::UInt64);
1✔
362
                builder.add_container(idx_name, idx_type);
1✔
363
                auto idx_var = symbolic::symbol(idx_name);
1✔
364

365
                auto& init_loop = builder.add_map_before(
1✔
366
                    *parent,
1✔
367
                    loop_,
1✔
368
                    idx_var,
1✔
369
                    symbolic::Lt(idx_var, total_size),
1✔
370
                    coop_flat,
1✔
371
                    symbolic::add(idx_var, total_coop_threads),
1✔
372
                    structured_control_flow::ScheduleType_Sequential::create(),
1✔
373
                    {},
1✔
374
                    loop_.debug_info()
1✔
375
                );
1✔
376

377
                auto& init_block = builder.add_block(init_loop.root());
1✔
378
                auto& init_src = builder.add_access(init_block, this->container_);
1✔
379
                auto& init_dst = builder.add_access(init_block, local_name_);
1✔
380
                auto& init_tasklet = builder.add_tasklet(init_block, data_flow::TaskletCode::assign, "_out", {"_in"});
1✔
381

382
                // Decompose idx_var into per-dim indices
383
                std::vector<symbolic::Expression> init_indices;
1✔
384
                symbolic::Expression remainder = idx_var;
1✔
385
                for (size_t i = 0; i < dim_sizes.size(); i++) {
2✔
386
                    if (i < dim_sizes.size() - 1) {
1✔
NEW
387
                        symbolic::Expression divisor = symbolic::integer(1);
×
NEW
388
                        for (size_t j = i + 1; j < dim_sizes.size(); j++) {
×
NEW
389
                            divisor = symbolic::mul(divisor, dim_sizes[j]);
×
NEW
390
                        }
×
NEW
391
                        init_indices.push_back(symbolic::div(remainder, divisor));
×
NEW
392
                        remainder = symbolic::mod(remainder, divisor);
×
393
                    } else {
1✔
394
                        init_indices.push_back(remainder);
1✔
395
                    }
1✔
396
                }
1✔
397

398
                auto init_src_subset = build_original_subset(init_indices);
1✔
399
                builder.add_computational_memlet(init_block, init_src, init_tasklet, "_in", init_src_subset, type);
1✔
400
                builder.add_computational_memlet(init_block, init_tasklet, "_out", init_dst, {idx_var}, buffer_type);
1✔
401

402
                // Barrier after init
403
                auto& barrier_block2 = builder.add_block_before(*parent, loop_, {}, loop_.debug_info());
1✔
404
                builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block2, {});
1✔
405
            }
1✔
406

407
            // WRITEBACK: barrier → cooperative copy-out → barrier
408
            {
4✔
409
                // Barrier before writeback
410
                auto& barrier_block3 = builder.add_block_after(*parent, loop_, {}, loop_.debug_info());
4✔
411
                builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block3, {});
4✔
412

413
                // Cooperative writeback loop
414
                auto idx_name = "__daisy_ols_coop_wb_" + this->container_;
4✔
415
                types::Scalar idx_type(types::PrimitiveType::UInt64);
4✔
416
                builder.add_container(idx_name, idx_type);
4✔
417
                auto idx_var = symbolic::symbol(idx_name);
4✔
418

419
                auto& wb_loop = builder.add_map_after(
4✔
420
                    *parent,
4✔
421
                    loop_,
4✔
422
                    idx_var,
4✔
423
                    symbolic::Lt(idx_var, total_size),
4✔
424
                    coop_flat,
4✔
425
                    symbolic::add(idx_var, total_coop_threads),
4✔
426
                    structured_control_flow::ScheduleType_Sequential::create(),
4✔
427
                    {},
4✔
428
                    loop_.debug_info()
4✔
429
                );
4✔
430

431
                auto& wb_block = builder.add_block(wb_loop.root());
4✔
432
                auto& wb_src = builder.add_access(wb_block, local_name_);
4✔
433
                auto& wb_dst = builder.add_access(wb_block, this->container_);
4✔
434
                auto& wb_tasklet = builder.add_tasklet(wb_block, data_flow::TaskletCode::assign, "_out", {"_in"});
4✔
435

436
                // Decompose idx_var into per-dim indices
437
                std::vector<symbolic::Expression> wb_indices;
4✔
438
                symbolic::Expression remainder = idx_var;
4✔
439
                for (size_t i = 0; i < dim_sizes.size(); i++) {
8✔
440
                    if (i < dim_sizes.size() - 1) {
4✔
NEW
441
                        symbolic::Expression divisor = symbolic::integer(1);
×
NEW
442
                        for (size_t j = i + 1; j < dim_sizes.size(); j++) {
×
NEW
443
                            divisor = symbolic::mul(divisor, dim_sizes[j]);
×
NEW
444
                        }
×
NEW
445
                        wb_indices.push_back(symbolic::div(remainder, divisor));
×
NEW
446
                        remainder = symbolic::mod(remainder, divisor);
×
447
                    } else {
4✔
448
                        wb_indices.push_back(remainder);
4✔
449
                    }
4✔
450
                }
4✔
451

452
                auto wb_dst_subset = build_original_subset(wb_indices);
4✔
453
                builder.add_computational_memlet(wb_block, wb_src, wb_tasklet, "_in", {idx_var}, buffer_type);
4✔
454
                builder.add_computational_memlet(wb_block, wb_tasklet, "_out", wb_dst, wb_dst_subset, type);
4✔
455

456
                // Barrier after writeback
457
                auto& barrier_block4 = builder.add_block_after(*parent, loop_, {}, loop_.debug_info());
4✔
458
                builder.add_library_node<data_flow::BarrierLocalNode>(barrier_block4, {});
4✔
459
            }
4✔
460
        } else {
10✔
461
            // ============================================================
462
            // CPU SEQUENTIAL PATH
463
            // ============================================================
464
            if (tile_info_.has_read) {
10✔
465
                std::vector<symbolic::Symbol> init_indvars;
7✔
466
                structured_control_flow::Sequence* init_scope = parent;
7✔
467
                bool first_init_loop = true;
7✔
468

469
                for (size_t i = 0; i < varying_dims.size(); i++) {
16✔
470
                    size_t d = varying_dims[i];
9✔
471
                    auto indvar_name = "__daisy_ols_init_" + this->container_ + "_d" + std::to_string(d);
9✔
472
                    types::Scalar indvar_type(types::PrimitiveType::UInt64);
9✔
473
                    builder.add_container(indvar_name, indvar_type);
9✔
474
                    auto indvar = symbolic::symbol(indvar_name);
9✔
475
                    init_indvars.push_back(indvar);
9✔
476

477
                    auto init = symbolic::integer(0);
9✔
478
                    auto condition = symbolic::Lt(indvar, dim_sizes[i]);
9✔
479
                    auto update = symbolic::add(indvar, symbolic::integer(1));
9✔
480

481
                    if (first_init_loop) {
9✔
482
                        auto& init_loop = builder.add_map_before(
6✔
483
                            *init_scope,
6✔
484
                            loop_,
6✔
485
                            indvar,
6✔
486
                            condition,
6✔
487
                            init,
6✔
488
                            update,
6✔
489
                            structured_control_flow::ScheduleType_Sequential::create(),
6✔
490
                            {},
6✔
491
                            loop_.debug_info()
6✔
492
                        );
6✔
493
                        init_scope = &init_loop.root();
6✔
494
                        first_init_loop = false;
6✔
495
                    } else {
6✔
496
                        auto& init_loop = builder.add_map(
3✔
497
                            *init_scope,
3✔
498
                            indvar,
3✔
499
                            condition,
3✔
500
                            init,
3✔
501
                            update,
3✔
502
                            structured_control_flow::ScheduleType_Sequential::create(),
3✔
503
                            {},
3✔
504
                            loop_.debug_info()
3✔
505
                        );
3✔
506
                        init_scope = &init_loop.root();
3✔
507
                    }
3✔
508
                }
9✔
509

510
                // Create init copy block
511
                auto& init_block = builder.add_block(*init_scope);
7✔
512
                auto& init_src = builder.add_access(init_block, this->container_);
7✔
513
                auto& init_dst = builder.add_access(init_block, local_name_);
7✔
514
                auto& init_tasklet = builder.add_tasklet(init_block, data_flow::TaskletCode::assign, "_out", {"_in"});
7✔
515

516
                std::vector<symbolic::Expression> init_exprs(init_indvars.begin(), init_indvars.end());
7✔
517
                auto init_src_subset = build_original_subset(init_exprs);
7✔
518
                data_flow::Subset init_dst_subset = {linearize(init_indvars)};
7✔
519

520
                builder.add_computational_memlet(init_block, init_src, init_tasklet, "_in", init_src_subset, type);
7✔
521
                builder
7✔
522
                    .add_computational_memlet(init_block, init_tasklet, "_out", init_dst, init_dst_subset, buffer_type);
7✔
523
            }
7✔
524

525
            // Writeback Maps
526
            {
10✔
527
                std::vector<symbolic::Symbol> wb_indvars;
10✔
528
                structured_control_flow::Sequence* wb_scope = parent;
10✔
529
                bool first_wb_loop = true;
10✔
530

531
                for (size_t i = 0; i < varying_dims.size(); i++) {
22✔
532
                    size_t d = varying_dims[i];
12✔
533
                    auto indvar_name = "__daisy_ols_wb_" + this->container_ + "_d" + std::to_string(d);
12✔
534
                    types::Scalar indvar_type(types::PrimitiveType::UInt64);
12✔
535
                    builder.add_container(indvar_name, indvar_type);
12✔
536
                    auto indvar = symbolic::symbol(indvar_name);
12✔
537
                    wb_indvars.push_back(indvar);
12✔
538

539
                    auto init = symbolic::integer(0);
12✔
540
                    auto condition = symbolic::Lt(indvar, dim_sizes[i]);
12✔
541
                    auto update = symbolic::add(indvar, symbolic::integer(1));
12✔
542

543
                    if (first_wb_loop) {
12✔
544
                        auto& wb_loop = builder.add_map_after(
9✔
545
                            *wb_scope,
9✔
546
                            loop_,
9✔
547
                            indvar,
9✔
548
                            condition,
9✔
549
                            init,
9✔
550
                            update,
9✔
551
                            structured_control_flow::ScheduleType_Sequential::create(),
9✔
552
                            {},
9✔
553
                            loop_.debug_info()
9✔
554
                        );
9✔
555
                        wb_scope = &wb_loop.root();
9✔
556
                        first_wb_loop = false;
9✔
557
                    } else {
9✔
558
                        auto& wb_loop = builder.add_map(
3✔
559
                            *wb_scope,
3✔
560
                            indvar,
3✔
561
                            condition,
3✔
562
                            init,
3✔
563
                            update,
3✔
564
                            structured_control_flow::ScheduleType_Sequential::create(),
3✔
565
                            {},
3✔
566
                            loop_.debug_info()
3✔
567
                        );
3✔
568
                        wb_scope = &wb_loop.root();
3✔
569
                    }
3✔
570
                }
12✔
571

572
                // Create writeback copy block
573
                auto& wb_block = builder.add_block(*wb_scope);
10✔
574
                auto& wb_src = builder.add_access(wb_block, local_name_);
10✔
575
                auto& wb_dst = builder.add_access(wb_block, this->container_);
10✔
576
                auto& wb_tasklet = builder.add_tasklet(wb_block, data_flow::TaskletCode::assign, "_out", {"_in"});
10✔
577

578
                std::vector<symbolic::Expression> wb_exprs(wb_indvars.begin(), wb_indvars.end());
10✔
579
                data_flow::Subset wb_src_subset = {linearize(wb_indvars)};
10✔
580
                auto wb_dst_subset = build_original_subset(wb_exprs);
10✔
581

582
                builder.add_computational_memlet(wb_block, wb_src, wb_tasklet, "_in", wb_src_subset, buffer_type);
10✔
583
                builder.add_computational_memlet(wb_block, wb_tasklet, "_out", wb_dst, wb_dst_subset, type);
10✔
584
            }
10✔
585
        }
10✔
586

587
        // ==================================================================
588
        // Update accesses in the main loop to use the local buffer
589
        // ==================================================================
590
        analysis::UsersView body_users(users, loop_.root());
14✔
591
        auto& mla = analysis_manager.get<analysis::MemoryLayoutAnalysis>();
14✔
592

593
        for (auto* user : body_users.uses(this->container_)) {
22✔
594
            auto element = user->element();
22✔
595
            if (auto memlet = dynamic_cast<data_flow::Memlet*>(element)) {
22✔
596
                auto* access = mla.access(*memlet);
×
597
                if (access && access->subset.size() == tile_info_.dimensions.size()) {
×
598
                    std::vector<symbolic::Expression> local_indices;
×
599
                    for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
×
600
                        if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
×
601
                            local_indices.push_back(symbolic::sub(access->subset.at(d), tile_info_.bases.at(d)));
×
602
                        }
×
603
                    }
×
604

NEW
605
                    symbolic::Expression linear_idx = linearize_exprs(local_indices);
×
606
                    memlet->set_subset({linear_idx});
×
607
                    memlet->set_base_type(buffer_type);
×
608
                } else {
×
609
                    auto& old_subset = memlet->subset();
×
610
                    if (old_subset.size() == tile_info_.dimensions.size()) {
×
611
                        std::vector<symbolic::Expression> local_indices;
×
612
                        for (size_t d = 0; d < tile_info_.dimensions.size(); d++) {
×
613
                            if (!symbolic::eq(tile_info_.dimensions.at(d), symbolic::integer(1))) {
×
614
                                local_indices.push_back(symbolic::sub(old_subset.at(d), tile_info_.bases.at(d)));
×
615
                            }
×
616
                        }
×
617

NEW
618
                        symbolic::Expression linear_idx = linearize_exprs(local_indices);
×
619
                        memlet->set_subset({linear_idx});
×
620
                        memlet->set_base_type(buffer_type);
×
621
                    }
×
622
                }
×
623
            }
×
624
        }
22✔
625

626
        // Replace container name in the loop body
627
        loop_.replace(symbolic::symbol(this->container_), symbolic::symbol(local_name_));
14✔
628
    }
14✔
629

630
    // Cleanup
631
    analysis_manager.invalidate_all();
15✔
632

633
    passes::SequenceFusion sf_pass;
15✔
634
    passes::DeadCFGElimination dce_pass;
15✔
635
    bool applies = false;
15✔
636
    do {
15✔
637
        applies = false;
15✔
638
        applies |= dce_pass.run(builder, analysis_manager);
15✔
639
        applies |= sf_pass.run(builder, analysis_manager);
15✔
640
    } while (applies);
15✔
641
};
15✔
642

643
void OutLocalStorage::to_json(nlohmann::json& j) const {
3✔
644
    std::string loop_type;
3✔
645
    if (dynamic_cast<structured_control_flow::For*>(&loop_)) {
3✔
646
        loop_type = "for";
2✔
647
    } else if (dynamic_cast<structured_control_flow::Map*>(&loop_)) {
2✔
648
        loop_type = "map";
1✔
649
    } else {
1✔
650
        throw std::runtime_error("Unsupported loop type for serialization of loop: " + loop_.indvar()->get_name());
×
651
    }
×
652
    j["subgraph"] = {
3✔
653
        {"0", {{"element_id", this->loop_.element_id()}, {"type", loop_type}}},
3✔
654
        {"1", {{"element_id", this->access_node_.element_id()}, {"type", "access_node"}}}
3✔
655
    };
3✔
656
    j["transformation_type"] = this->name();
3✔
657
};
3✔
658

659
OutLocalStorage OutLocalStorage::from_json(builder::StructuredSDFGBuilder& builder, const nlohmann::json& desc) {
1✔
660
    auto loop_id = desc["subgraph"]["0"]["element_id"].get<size_t>();
1✔
661
    auto element = builder.find_element_by_id(loop_id);
1✔
662
    if (!element) {
1✔
663
        throw InvalidTransformationDescriptionException("Element with ID " + std::to_string(loop_id) + " not found.");
×
664
    }
×
665
    auto loop = dynamic_cast<structured_control_flow::StructuredLoop*>(element);
1✔
666

667
    auto access_node = dynamic_cast<
1✔
668
        data_flow::AccessNode*>(builder.find_element_by_id(desc.at("subgraph").at("1").at("element_id").get<size_t>()));
1✔
669
    if (!access_node) {
1✔
670
        throw InvalidTransformationDescriptionException(
×
671
            "Access node with ID " + std::to_string(desc.at("subgraph").at("1").at("element_id").get<size_t>()) +
×
672
            " not found."
×
673
        );
×
674
    }
×
675

676
    return OutLocalStorage(*loop, *access_node);
1✔
677
};
1✔
678

679
} // namespace transformations
680
} // 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