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

daisytuner / docc / 27161566962

08 Jun 2026 07:25PM UTC coverage: 61.31% (+0.04%) from 61.275%
27161566962

Pull #741

github

web-flow
Merge 860fd1b7d into aacd50c09
Pull Request #741: replaces MemAccessRangeAnalysis with MemoryLayoutAnalysis

384 of 424 new or added lines in 11 files covered. (90.57%)

40 existing lines in 11 files now uncovered.

35643 of 58136 relevant lines covered (61.31%)

744.53 hits per line

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

68.07
/opt/src/transformations/offloading/offload_transform.cpp
1
#include "sdfg/transformations/offloading/offload_transform.h"
2

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

6
#include "sdfg/analysis/scope_analysis.h"
7
#include "sdfg/analysis/type_analysis.h"
8
#include "sdfg/data_flow/access_node.h"
9
#include "sdfg/structured_control_flow/block.h"
10
#include "sdfg/structured_control_flow/if_else.h"
11
#include "sdfg/structured_control_flow/map.h"
12
#include "sdfg/symbolic/symbolic.h"
13

14
#include "sdfg/data_flow/library_node.h"
15
#include "sdfg/optimization_report/pass_report_consumer.h"
16
#include "sdfg/types/utils.h"
17
#include "sdfg/visitor/immutable_structured_sdfg_visitor.h"
18
#include "symengine/symengine_rcp.h"
19

20
namespace sdfg {
21
namespace transformations {
22

23
class SideEffectFinder : public visitor::ImmutableStructuredSDFGVisitor {
24
private:
25
    structured_control_flow::Map& map_;
26

27
public:
28
    SideEffectFinder(StructuredSDFG& sdfg, analysis::AnalysisManager& analysis_manager, structured_control_flow::Map& map)
29
        : visitor::ImmutableStructuredSDFGVisitor(sdfg, analysis_manager), map_(map) {}
5✔
30

31
    bool visit() override { return visit_internal(map_.root()); }
5✔
32

33
    bool accept(structured_control_flow::Block& node) override {
4✔
34
        for (const auto& lib_node : node.dataflow().library_nodes()) {
4✔
35
            if (lib_node->side_effect()) {
×
36
                return true;
×
37
            }
×
38
        }
×
39
        return false;
4✔
40
    }
4✔
41
};
42

43
OffloadTransform::OffloadTransform(structured_control_flow::Map& map, bool allow_dynamic_sizes)
44
    : map_(map), allow_dynamic_sizes_(allow_dynamic_sizes) {}
11✔
45

46

47
bool OffloadTransform::can_be_applied(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
5✔
48
    auto& sdfg = builder.subject();
5✔
49

50
    auto& arguments_analysis = analysis_manager.get<analysis::ArgumentsAnalysis>();
5✔
51

52
    if (!arguments_analysis.inferred_types(analysis_manager, this->map_)) {
5✔
53
        if (report_) report_->transform_impossible(this, "unranged args");
×
54
        DEBUG_PRINTLN("Cannot apply transform: argument types not inferred");
×
55
        return false;
×
56
    }
×
57
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->map_);
5✔
58

59
    // Criterion: arg Data Types must be continuous
60
    for (auto& [argument, meta] : arguments) {
10✔
61
        auto base_type = analysis::TypeAnalysis(sdfg, &map_, analysis_manager).get_outer_type(argument);
10✔
62
        if (base_type == nullptr) {
10✔
63
            if (report_) report_->transform_impossible(this, "cannot infer type");
×
64
            DEBUG_PRINTLN("Cannot apply transform: argument type cannot be inferred");
×
65
            return false;
×
66
        }
×
67
        if (!types::is_contiguous_type(*base_type, sdfg)) {
10✔
68
            if (report_) report_->transform_impossible(this, "type is not contiguous");
×
69
            DEBUG_PRINTLN("Cannot apply transform: argument type is not contiguous");
×
70
            return false;
×
71
        }
×
72
        if (meta.is_scalar && meta.is_output) {
10✔
73
            if (report_) report_->transform_impossible(this, "scalar output");
×
74
            DEBUG_PRINTLN("Cannot apply transform: map writes to scalar argument");
×
75
            return false;
×
76
        }
×
77
    }
10✔
78

79
    // Criterion: Map must start at 0
80
    if (!symbolic::eq(this->map_.init(), symbolic::zero())) {
5✔
81
        if (report_) report_->transform_impossible(this, "non zero start");
×
82
        DEBUG_PRINTLN("Cannot apply transform: map does not start at zero");
×
83
        return false;
×
84
    }
×
85

86
    // Criterion: Map cannot write to scalar arguments
87
    for (auto& [argument, meta] : arguments) {
10✔
88
        if (meta.is_scalar && meta.is_output) {
10✔
89
            if (report_) report_->transform_impossible(this, "scalar output");
×
90
            DEBUG_PRINTLN("Cannot apply transform: map writes to scalar argument");
×
91
            return false;
×
92
        }
×
93
    }
10✔
94

95
    if (!arguments_analysis.argument_size_known(analysis_manager, this->map_, allow_dynamic_sizes_)) {
5✔
UNCOV
96
        if (report_) report_->transform_impossible(this, "args not understood");
×
97
        DEBUG_PRINTLN("Cannot apply transform: argument sizes not known");
×
98
        return false;
×
99
    }
×
100

101
    // Criterion: Map cannot contain function calls with side effects (e.g. library nodes that write to memory)
102
    SideEffectFinder side_effect_finder(sdfg, analysis_manager, this->map_);
5✔
103
    if (side_effect_finder.visit()) {
5✔
104
        if (report_) report_->transform_impossible(this, "side effects");
×
105
        DEBUG_PRINTLN("Cannot apply transform: map contains library nodes with side effects");
×
106
        return false;
×
107
    }
×
108

109
    if (report_) report_->transform_possible(this);
5✔
110
    return true;
5✔
111
}
5✔
112

113
void OffloadTransform::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
5✔
114
    // Schedule
115
    builder.update_schedule_type(this->map_, transformed_schedule_type());
5✔
116

117
    auto& sdfg = builder.subject();
5✔
118

119
    // Identify arguments and locals
120
    auto& arguments_analysis = analysis_manager.get<analysis::ArgumentsAnalysis>();
5✔
121

122
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->map_);
5✔
123
    auto& locals = arguments_analysis.locals(analysis_manager, this->map_);
5✔
124

125
    // Infer subsets for arguments
126
    auto& argument_sizes = arguments_analysis.argument_sizes(analysis_manager, this->map_, allow_dynamic_sizes_);
5✔
127

128
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
5✔
129
    auto parent_scope = static_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(&this->map_));
5✔
130

131
    std::string container_prefix = copy_prefix() + std::to_string(parent_scope->element_id()) + "_";
5✔
132

133
    // Allocate arguments and locals
134
    allocate_locals_on_device_stack(builder, analysis_manager, locals);
5✔
135
    handle_device_setup_and_teardown(builder, arguments, argument_sizes, container_prefix);
5✔
136

137
    // Copy-in arguments to device memory & allocation
138
    for (auto& [argument, meta] : arguments) {
10✔
139
        if (!meta.is_ptr) {
10✔
140
            continue;
6✔
141
        }
6✔
142
        auto argument_device = container_prefix + argument;
4✔
143
        auto& new_block = builder.add_block_before(*parent_scope, this->map_, {}, this->map_.debug_info());
4✔
144
        auto& size = argument_sizes.at(argument);
4✔
145
        copy_to_device_with_allocation(builder, argument, argument_device, size, SymEngine::null, new_block);
4✔
146
    }
4✔
147

148
    update_map_containers(arguments, container_prefix);
5✔
149

150
    // Copy-out arguments to host memory & free
151
    for (auto& [argument, meta] : arguments) {
10✔
152
        if (!meta.is_ptr) {
10✔
153
            continue;
6✔
154
        }
6✔
155
        auto argument_device = container_prefix + argument;
4✔
156
        auto& new_block = builder.add_block_after(*parent_scope, this->map_, {}, this->map_.debug_info());
4✔
157
        auto& size = argument_sizes.at(argument);
4✔
158
        if (!skip_unneeded_d2h_ || meta.is_output) {
4✔
159
            copy_from_device_with_free(builder, new_block, argument, argument_device, size, SymEngine::null);
4✔
160
        } else {
4✔
161
            deallocate_device_arg(builder, new_block, argument_device, size, SymEngine::null);
×
162
        }
×
163
    }
4✔
164

165
    if (report_) report_->transform_applied(this);
5✔
166
}
5✔
167

168
void OffloadTransform::handle_device_setup_and_teardown(
169
    builder::StructuredSDFGBuilder& builder,
170

171
    const std::map<std::string, analysis::RegionArgument>& arguments,
172
    const std::unordered_map<std::string, symbolic::Expression>& argument_sizes,
173
    std::string prefix
174
) {
5✔
175
    // Add managed buffers for pointer arguments
176
    for (auto& [argument, meta] : arguments) {
10✔
177
        if (!meta.is_ptr || builder.subject().exists(prefix + argument)) {
10✔
178
            continue;
7✔
179
        }
7✔
180
        auto argument_device = prefix + argument;
3✔
181

182
        auto arg_size = argument_sizes.at(argument);
3✔
183

184
        add_device_buffer(builder, argument, argument_device, arg_size);
3✔
185
    }
3✔
186
}
5✔
187

188
void OffloadTransform::
189
    update_map_containers(const std::map<std::string, analysis::RegionArgument>& arguments, std::string prefix) {
5✔
190
    for (auto& [argument, meta] : arguments) {
10✔
191
        if (meta.is_ptr) {
10✔
192
            auto argument_device = prefix + argument;
4✔
193
            this->map_.replace(symbolic::symbol(argument), symbolic::symbol(argument_device));
4✔
194
        }
4✔
195
    }
10✔
196
}
5✔
197

198
} // namespace transformations
199
} // 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