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

daisytuner / sdfglib / 20792180504

07 Jan 2026 06:28PM UTC coverage: 62.339% (+0.2%) from 62.168%
20792180504

Pull #436

github

web-flow
Merge 6e411abb2 into acd6225ac
Pull Request #436: Add comprehensive tests and Doxygen documentation for SDFG transformations

14987 of 24041 relevant lines covered (62.34%)

88.99 hits per line

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

83.16
/src/transformations/loop_tiling.cpp
1
#include "sdfg/transformations/loop_tiling.h"
2

3
#include "sdfg/analysis/assumptions_analysis.h"
4
#include "sdfg/analysis/loop_analysis.h"
5
#include "sdfg/analysis/scope_analysis.h"
6
#include "sdfg/builder/structured_sdfg_builder.h"
7
#include "sdfg/structured_control_flow/structured_loop.h"
8
#include "sdfg/symbolic/symbolic.h"
9

10
namespace sdfg {
11
namespace transformations {
12

13
LoopTiling::LoopTiling(structured_control_flow::StructuredLoop& loop, size_t tile_size)
14
    : loop_(loop), tile_size_(tile_size) {};
17✔
15

16
std::string LoopTiling::name() const { return "LoopTiling"; };
9✔
17

18
bool LoopTiling::can_be_applied(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
15✔
19
    if (this->tile_size_ <= 1) {
15✔
20
        return false;
4✔
21
    }
4✔
22
    // Criterion contiguous loop
23
    auto& assumptions_analysis = analysis_manager.get<analysis::AssumptionsAnalysis>();
11✔
24
    return analysis::LoopAnalysis::is_contiguous(&loop_, assumptions_analysis);
11✔
25
};
15✔
26

27
void LoopTiling::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
11✔
28
    auto& sdfg = builder.subject();
11✔
29

30
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
11✔
31
    auto parent = static_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(&loop_));
11✔
32
    size_t index = parent->index(loop_);
11✔
33
    auto& transition = parent->at(index).second;
11✔
34

35
    auto indvar = loop_.indvar();
11✔
36

37
    // Step 1: Define new outer loop
38
    auto outer_indvar_str = builder.find_new_name(indvar->get_name() + "_tile");
11✔
39
    builder.add_container(outer_indvar_str, sdfg.type(loop_.indvar()->get_name()));
11✔
40

41
    auto outer_indvar = symbolic::symbol(outer_indvar_str);
11✔
42
    auto outer_condition = symbolic::subs(loop_.condition(), indvar, outer_indvar);
11✔
43
    auto outer_update = symbolic::add(outer_indvar, symbolic::integer(this->tile_size_));
11✔
44

45
    structured_control_flow::StructuredLoop* outer_loop = nullptr;
11✔
46
    if (auto map = dynamic_cast<structured_control_flow::Map*>(&loop_)) {
11✔
47
        outer_loop = &builder.add_map_before(
6✔
48
            *parent,
6✔
49
            loop_,
6✔
50
            outer_indvar,
6✔
51
            outer_condition,
6✔
52
            loop_.init(),
6✔
53
            outer_update,
6✔
54
            map->schedule_type(),
6✔
55
            transition.assignments(),
6✔
56
            loop_.debug_info()
6✔
57
        );
6✔
58
    } else {
6✔
59
        outer_loop = &builder.add_for_before(
5✔
60
            *parent,
5✔
61
            loop_,
5✔
62
            outer_indvar,
5✔
63
            outer_condition,
5✔
64
            loop_.init(),
5✔
65
            outer_update,
5✔
66
            transition.assignments(),
5✔
67
            loop_.debug_info()
5✔
68
        );
5✔
69
    }
5✔
70

71
    // Step 2: Redefine inner loop
72
    auto inner_indvar = indvar;
11✔
73
    auto inner_init = outer_indvar;
11✔
74
    auto inner_condition_tile =
11✔
75
        symbolic::Lt(inner_indvar, symbolic::add(outer_indvar, symbolic::integer(this->tile_size_)));
11✔
76

77
    symbolic::Condition inner_condition = symbolic::And(inner_condition_tile, loop_.condition());
11✔
78

79
    auto inner_update = symbolic::add(inner_indvar, symbolic::integer(1));
11✔
80
    builder.update_loop(loop_, inner_indvar, inner_condition, inner_init, inner_update);
11✔
81

82
    // Step 3: Move loop into tiling loop
83
    transition.assignments().clear();
11✔
84
    builder.move_child(*parent, index + 1, outer_loop->root());
11✔
85

86
    analysis_manager.invalidate_all();
11✔
87
    applied_ = true;
11✔
88
    inner_loop_ = &loop_;
11✔
89
    outer_loop_ = outer_loop;
11✔
90
};
11✔
91

92
void LoopTiling::to_json(nlohmann::json& j) const {
5✔
93
    std::string loop_type;
5✔
94
    if (dynamic_cast<structured_control_flow::For*>(&loop_)) {
5✔
95
        loop_type = "for";
3✔
96
    } else if (dynamic_cast<structured_control_flow::Map*>(&loop_)) {
3✔
97
        loop_type = "map";
2✔
98
    } else {
2✔
99
        throw InvalidSDFGException("Unsupported loop type for serialization of loop: " + loop_.indvar()->get_name());
×
100
    }
×
101

102
    j["transformation_type"] = this->name();
5✔
103
    j["subgraph"] = {{"0", {{"element_id", this->loop_.element_id()}, {"type", loop_type}}}};
5✔
104
    j["parameters"] = {{"tile_size", tile_size_}};
5✔
105
};
5✔
106

107
LoopTiling LoopTiling::from_json(builder::StructuredSDFGBuilder& builder, const nlohmann::json& desc) {
8✔
108
    auto loop_id = desc["subgraph"]["0"]["element_id"].get<size_t>();
8✔
109
    size_t tile_size = desc["parameters"]["tile_size"].get<size_t>();
8✔
110
    auto element = builder.find_element_by_id(loop_id);
8✔
111
    if (!element) {
8✔
112
        throw InvalidTransformationDescriptionException("Element with ID " + std::to_string(loop_id) + " not found.");
×
113
    }
×
114
    auto loop = dynamic_cast<structured_control_flow::StructuredLoop*>(element);
8✔
115

116
    return LoopTiling(*loop, tile_size);
8✔
117
};
8✔
118

119
structured_control_flow::StructuredLoop* LoopTiling::inner_loop() {
×
120
    if (!applied_) {
×
121
        throw InvalidSDFGException("Accessing tiled loop before their creation.");
×
122
    }
×
123

124
    return inner_loop_;
×
125
}
×
126

127
structured_control_flow::StructuredLoop* LoopTiling::outer_loop() {
×
128
    if (!applied_) {
×
129
        throw InvalidSDFGException("Accessing tiled loop before their creation.");
×
130
    }
×
131

132
    return outer_loop_;
×
133
}
×
134

135
} // namespace transformations
136
} // 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