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

daisytuner / docc / 27704607727

17 Jun 2026 04:38PM UTC coverage: 61.63% (+0.1%) from 61.533%
27704607727

Pull #764

github

web-flow
Merge 52a992b2c into 5b779fb0a
Pull Request #764: simplifies GPU transformations

255 of 329 new or added lines in 6 files covered. (77.51%)

5 existing lines in 2 files now uncovered.

36652 of 59471 relevant lines covered (61.63%)

1139.64 hits per line

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

69.49
/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/type_analysis.h"
7
#include "sdfg/data_flow/access_node.h"
8
#include "sdfg/structured_control_flow/block.h"
9
#include "sdfg/structured_control_flow/if_else.h"
10
#include "sdfg/structured_control_flow/map.h"
11
#include "sdfg/symbolic/symbolic.h"
12

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

19
namespace sdfg {
20
namespace transformations {
21

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

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

30
    bool visit() override { return visit_internal(map_.root()); }
20✔
31

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

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

45

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

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

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

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

78
    // Note: arbitrary `init` and `stride` are permitted on the kernel-boundary
79
    // Map. The CUDA/ROCm dispatchers emit
80
    //   `<map.indvar> = init + thread_flat_id * stride`,
81
    // and `num_iterations()` already accounts for both when computing the grid
82
    // geometry.
83
    if (map_.num_iterations().is_null()) {
20✔
NEW
84
        if (report_) report_->transform_impossible(this, "cannot determine num iterations");
×
NEW
85
        DEBUG_PRINTLN("Cannot apply transform: cannot determine number of iterations for map");
×
86
        return false;
×
87
    }
×
88

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

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

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

112
    if (report_) report_->transform_possible(this);
20✔
113
    return true;
20✔
114
}
20✔
115

116
void OffloadTransform::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
18✔
117
    // Schedule
118
    builder.update_schedule_type(this->map_, transformed_schedule_type());
18✔
119

120
    auto& sdfg = builder.subject();
18✔
121

122
    // Identify arguments and locals
123
    auto& arguments_analysis = analysis_manager.get<analysis::ArgumentsAnalysis>();
18✔
124

125
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->map_);
18✔
126
    auto& locals = arguments_analysis.locals(analysis_manager, this->map_);
18✔
127

128
    // Infer subsets for arguments
129
    auto& argument_sizes = arguments_analysis.argument_sizes(analysis_manager, this->map_, allow_dynamic_sizes_);
18✔
130

131
    auto parent_scope = static_cast<structured_control_flow::Sequence*>(this->map_.get_parent());
18✔
132

133
    std::string container_prefix = copy_prefix() + std::to_string(parent_scope->element_id()) + "_";
18✔
134

135
    // Allocate arguments and locals
136
    allocate_locals_on_device_stack(builder, analysis_manager, locals);
18✔
137
    handle_device_setup_and_teardown(builder, arguments, argument_sizes, container_prefix);
18✔
138

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

150
    update_map_containers(arguments, container_prefix);
18✔
151

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

167
    if (report_) report_->transform_applied(this);
18✔
168
}
18✔
169

170
void OffloadTransform::handle_device_setup_and_teardown(
171
    builder::StructuredSDFGBuilder& builder,
172

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

184
        auto arg_size = argument_sizes.at(argument);
10✔
185

186
        add_device_buffer(builder, argument, argument_device, arg_size);
10✔
187
    }
10✔
188
}
18✔
189

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

200
} // namespace transformations
201
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc