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

daisytuner / sdfglib / 16525628849

25 Jul 2025 03:25PM UTC coverage: 65.266% (-0.05%) from 65.32%
16525628849

push

github

web-flow
Merge pull request #162 from daisytuner/transfer-tuning

Transfer tuning

25 of 53 new or added lines in 6 files covered. (47.17%)

1 existing line in 1 file now uncovered.

8976 of 13753 relevant lines covered (65.27%)

125.53 hits per line

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

86.3
/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)
10✔
14
    : loop_(loop), tile_size_(tile_size) {};
10✔
15

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

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

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

30
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
7✔
31
    auto parent = static_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(&loop_));
7✔
32

33
    auto indvar = loop_.indvar();
7✔
34

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

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

43
    structured_control_flow::StructuredLoop* outer_loop = nullptr;
7✔
44
    if (auto map = dynamic_cast<structured_control_flow::Map*>(&loop_)) {
7✔
45
        outer_loop =
4✔
46
            &builder
4✔
47
                 .add_map_before(
4✔
48
                     *parent, loop_, outer_indvar, outer_condition, loop_.init(), outer_update, map->schedule_type()
4✔
49
                 )
50
                 .first;
4✔
51
    } else {
4✔
52
        outer_loop =
3✔
53
            &builder.add_for_before(*parent, loop_, outer_indvar, outer_condition, loop_.init(), outer_update).first;
3✔
54
    }
55

56
    // Step 2: Redefine inner loop
57
    auto inner_indvar = indvar;
7✔
58
    auto inner_init = outer_indvar;
7✔
59
    auto inner_condition_tile =
60
        symbolic::Lt(inner_indvar, symbolic::add(outer_indvar, symbolic::integer(this->tile_size_)));
7✔
61

62
    auto& assumptions_analysis = analysis_manager.get<analysis::AssumptionsAnalysis>();
7✔
63
    auto old_bound = analysis::LoopAnalysis::canonical_bound(&loop_, assumptions_analysis);
7✔
64

65
    symbolic::Condition inner_condition = inner_condition_tile;
7✔
66
    if (old_bound == SymEngine::null) {
7✔
67
        inner_condition = symbolic::And(inner_condition_tile, loop_.condition());
×
68
    } else if (SymEngine::is_a<SymEngine::Integer>(*old_bound)) {
7✔
69
        size_t old_bound_int = SymEngine::rcp_static_cast<const SymEngine::Integer>(old_bound)->as_uint();
×
70
        if ((old_bound_int % this->tile_size_) == 0) {
×
71
            inner_condition = inner_condition_tile;
×
72
        } else {
×
73
            inner_condition = symbolic::And(inner_condition_tile, loop_.condition());
×
74
        }
75
    } else {
×
76
        inner_condition = symbolic::And(inner_condition_tile, loop_.condition());
7✔
77
    }
78
    auto inner_update = symbolic::add(inner_indvar, symbolic::integer(1));
7✔
79
    loop_.update() = inner_update;
7✔
80
    loop_.condition() = inner_condition;
7✔
81
    loop_.init() = inner_init;
7✔
82

83
    // Step 3: Move inner loop body to outer loop body
84
    builder.insert(loop_, *parent, outer_loop->root(), loop_.debug_info());
7✔
85

86
    analysis_manager.invalidate_all();
7✔
87
};
7✔
88

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

99
    j["transformation_type"] = this->name();
4✔
100
    j["subgraph"] = {{"0", {{"element_id", this->loop_.element_id()}, {"type", loop_type}}}};
4✔
101
    j["tile_size"] = tile_size_;
4✔
102
};
4✔
103

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

113
    return LoopTiling(*loop, tile_size);
4✔
114
};
×
115

116
} // namespace transformations
117
} // 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