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

daisytuner / sdfglib / 15744620952

18 Jun 2025 10:07PM UTC coverage: 64.591% (+0.005%) from 64.586%
15744620952

push

github

web-flow
Merge pull request #86 from daisytuner/add-optimizer

Add optimizer functionality

122 of 201 new or added lines in 9 files covered. (60.7%)

2 existing lines in 1 file now uncovered.

8141 of 12604 relevant lines covered (64.59%)

153.84 hits per line

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

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

3
#include "sdfg/analysis/loop_analysis.h"
4
#include "sdfg/analysis/scope_analysis.h"
5
#include "sdfg/builder/structured_sdfg_builder.h"
6
#include "sdfg/deepcopy/structured_sdfg_deep_copy.h"
7
#include "sdfg/passes/structured_control_flow/dead_cfg_elimination.h"
8
#include "sdfg/passes/structured_control_flow/sequence_fusion.h"
9
#include "sdfg/structured_control_flow/structured_loop.h"
10

11
namespace sdfg {
12
namespace transformations {
13

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

17
std::string LoopTiling::name() const { return "LoopTiling"; };
5✔
18

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

29
void LoopTiling::apply(builder::StructuredSDFGBuilder& builder,
8✔
30
                       analysis::AnalysisManager& analysis_manager) {
31
    auto& sdfg = builder.subject();
8✔
32

33
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
8✔
34
    auto parent =
8✔
35
        static_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(&loop_));
8✔
36

37
    auto indvar = loop_.indvar();
8✔
38
    auto outer_indvar_str = builder.find_new_name(indvar->get_name() + "_tile");
8✔
39
    builder.add_container(outer_indvar_str, sdfg.type(loop_.indvar()->get_name()));
8✔
40
    auto outer_indvar = symbolic::symbol(outer_indvar_str);
8✔
41

42
    auto tile_size = symbolic::integer(this->tile_size_);
8✔
43

44
    auto condition = symbolic::subs(loop_.condition(), indvar, outer_indvar);
8✔
45
    auto update = symbolic::add(outer_indvar, tile_size);
8✔
46

47
    auto& outer_loop =
8✔
48
        builder.add_for_before(*parent, loop_, outer_indvar, condition, loop_.init(), update).first;
8✔
49

50
    auto& outer_body = outer_loop.root();
8✔
51

52
    auto inner_indvar = indvar;
8✔
53
    auto inner_init = outer_indvar;
8✔
54
    auto inner_condition_tile = symbolic::Lt(inner_indvar, symbolic::add(outer_indvar, tile_size));
8✔
55
    auto inner_condition_base = symbolic::subs(loop_.condition(), outer_indvar, inner_indvar);
8✔
56
    auto inner_condition = symbolic::And(inner_condition_tile, inner_condition_base);
8✔
57
    auto inner_update = symbolic::add(inner_indvar, symbolic::integer(1));
8✔
58

59
    // Add new loop with original body
60
    auto& tmp_root = builder.add_sequence_before(*parent, loop_).first;
8✔
61
    auto& inner_loop =
8✔
62
        builder.add_for(tmp_root, inner_indvar, inner_condition, inner_init, inner_update);
8✔
63

64
    deepcopy::StructuredSDFGDeepCopy copies(builder, inner_loop.root(), loop_.root());
8✔
65
    copies.copy();
8✔
66

67
    builder.clear_sequence(outer_body);
8✔
68

69
    deepcopy::StructuredSDFGDeepCopy copies2(builder, outer_body, tmp_root);
8✔
70
    copies2.copy();
8✔
71

72
    builder.remove_child(*parent, tmp_root);
8✔
73
    builder.remove_child(*parent, loop_);
8✔
74

75
    analysis_manager.invalidate_all();
8✔
76

77
    passes::SequenceFusion sf_pass;
8✔
78
    passes::DeadCFGElimination dce_pass;
8✔
79
    bool applies = false;
8✔
80
    do {
8✔
81
        applies = false;
16✔
82
        applies |= dce_pass.run(builder, analysis_manager);
16✔
83
        applies |= sf_pass.run(builder, analysis_manager);
16✔
84
    } while (applies);
16✔
85
};
8✔
86

87
void LoopTiling::to_json(nlohmann::json& j) const {
4✔
88
    j["transformation_type"] = this->name();
4✔
89
    j["loop_element_id"] = loop_.element_id();
4✔
90
    j["tile_size"] = tile_size_;
4✔
91
};
4✔
92

93
LoopTiling LoopTiling::from_json(builder::StructuredSDFGBuilder& builder,
3✔
94
                                 const nlohmann::json& desc) {
95
    auto loop_id = desc["loop_element_id"].get<size_t>();
3✔
96
    size_t tile_size = desc["tile_size"].get<size_t>();
3✔
97
    auto element = builder.find_element_by_id(loop_id);
3✔
98
    if (!element) {
3✔
NEW
99
        throw InvalidTransformationDescriptionException("Element with ID " +
×
NEW
100
                                                        std::to_string(loop_id) + " not found.");
×
101
    }
102
    auto loop = dynamic_cast<structured_control_flow::For*>(element);
3✔
103

104
    return LoopTiling(*loop, tile_size);
3✔
NEW
105
};
×
106

107
}  // namespace transformations
108
}  // 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