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

daisytuner / docc / 31009380971

05 Aug 2026 01:15PM UTC coverage: 65.103% (+0.1%) from 65.005%
31009380971

Pull #814

github

web-flow
Merge 268a080ae into 7d5b198bd
Pull Request #814: Adds GPU reduce dispatchers

409 of 663 new or added lines in 18 files covered. (61.69%)

145 existing lines in 6 files now uncovered.

46693 of 71722 relevant lines covered (65.1%)

713.1 hits per line

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

67.44
/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

23
OffloadTransform::OffloadTransform(structured_control_flow::StructuredLoop& loop, bool allow_dynamic_sizes)
24
    : loop_(loop), allow_dynamic_sizes_(allow_dynamic_sizes) {}
38✔
25

26

27
bool OffloadTransform::can_be_applied(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
20✔
28
    if (dynamic_cast<structured_control_flow::Map*>(&loop_) == nullptr &&
20✔
29
        dynamic_cast<structured_control_flow::Reduce*>(&loop_) == nullptr) {
20✔
NEW
30
        return false;
×
NEW
31
    }
×
32

33
    if (dynamic_cast<structured_control_flow::Reduce*>(&loop_)) {
20✔
NEW
34
        if (report_) report_->transform_impossible(this, "reduce");
×
NEW
35
        DEBUG_PRINTLN("Cannot apply transform: Reduce nodes are not offloaded yet");
×
NEW
36
        return false;
×
NEW
37
    }
×
38

39
    auto& sdfg = builder.subject();
20✔
40

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

43
    if (!arguments_analysis.inferred_types(analysis_manager, this->loop_)) {
20✔
44
        if (report_) report_->transform_impossible(this, "unranged args");
×
45
        DEBUG_PRINTLN("Cannot apply transform: argument types not inferred");
×
46
        return false;
×
47
    }
×
48
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->loop_);
20✔
49

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

70
    // Note: arbitrary `init` and `stride` are permitted on the kernel-boundary
71
    // Map. The CUDA/ROCm dispatchers emit
72
    //   `<map.indvar> = init + thread_flat_id * stride`,
73
    // and `num_iterations()` already accounts for both when computing the grid
74
    // geometry.
75
    if (loop_.num_iterations().is_null()) {
20✔
76
        if (report_) report_->transform_impossible(this, "cannot determine num iterations");
×
77
        DEBUG_PRINTLN("Cannot apply transform: cannot determine number of iterations for map");
×
78
        return false;
×
79
    }
×
80

81
    // Criterion: Map cannot write to scalar arguments
82
    for (auto& [argument, meta] : arguments) {
41✔
83
        if (meta.is_scalar && meta.is_output) {
41✔
84
            if (report_) report_->transform_impossible(this, "scalar output");
×
85
            DEBUG_PRINTLN("Cannot apply transform: map writes to scalar argument");
×
86
            return false;
×
87
        }
×
88
    }
41✔
89

90
    if (!arguments_analysis.argument_size_known(analysis_manager, this->loop_, allow_dynamic_sizes_)) {
20✔
91
        if (report_) report_->transform_impossible(this, "args not understood");
×
92
        DEBUG_PRINTLN("Cannot apply transform: argument sizes not known");
×
93
        return false;
×
94
    }
×
95

96
    // Criterion: Map cannot contain function calls with side effects (e.g. library nodes that write to memory)
97
    SideEffectFinder side_effect_finder(sdfg, analysis_manager, this->loop_);
20✔
98
    if (side_effect_finder.visit()) {
20✔
99
        if (report_) report_->transform_impossible(this, "side effects");
×
100
        DEBUG_PRINTLN("Cannot apply transform: map contains library nodes with side effects");
×
101
        return false;
×
102
    }
×
103

104
    if (report_) report_->transform_possible(this);
20✔
105
    return true;
20✔
106
}
20✔
107

108
void OffloadTransform::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
18✔
109
    // Schedule
110
    builder.update_schedule_type(this->loop_, transformed_schedule_type());
18✔
111

112
    auto& sdfg = builder.subject();
18✔
113

114
    // Identify arguments and locals
115
    auto& arguments_analysis = analysis_manager.get<analysis::ArgumentsAnalysis>();
18✔
116

117
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->loop_);
18✔
118
    auto& locals = arguments_analysis.locals(analysis_manager, this->loop_);
18✔
119

120
    // Infer subsets for arguments
121
    auto& argument_sizes = arguments_analysis.argument_sizes(analysis_manager, this->loop_, allow_dynamic_sizes_);
18✔
122

123
    auto parent_scope = static_cast<structured_control_flow::Sequence*>(this->loop_.get_parent());
18✔
124

125
    // Key the device-buffer names by THIS map's element id so every offloaded loop nest gets its own SSA device
126
    // buffer. Keying by the parent scope instead would make two maps under the same sequence that offload the same
127
    // host container resolve to one device name, allocated and freed once per map -- a single device name carrying
128
    // multiple alloc/free lifetimes. DataTransferMinimization's reuse reconciliation bails on exactly that
129
    // (a device buffer with more than one free cannot be proven double-free safe), which blocks the D2H->H2D
130
    // device aliasing and leaves a redundant host round-trip. One buffer per loop nest keeps each name single-alloc
131
    // single-free, so the reuse fires and DeadDataElimination can drop the now-userless staging container.
132
    std::string container_prefix = copy_prefix() + std::to_string(this->loop_.element_id()) + "_";
18✔
133

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

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

149
    update_loop_containers(arguments, container_prefix);
18✔
150

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

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

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

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

183
        auto arg_size = argument_sizes.at(argument);
19✔
184

185
        add_device_buffer(builder, argument, argument_device, arg_size);
19✔
186
    }
19✔
187
}
18✔
188

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

199
bool ::sdfg::transformations::SideEffectFinder::accept(structured_control_flow::Block& node) {
21✔
200
    for (const auto& lib_node : node.dataflow().library_nodes()) {
21✔
201
        if (lib_node->side_effect()) {
×
202
            return true;
×
203
        }
×
204
    }
×
205
    return false;
21✔
206
}
21✔
207

208
bool ::sdfg::transformations::SideEffectFinder::visit() {
20✔
209
    return visitor::ImmutableStructuredSDFGVisitor::visit_internal(loop_.StructuredLoop::root());
20✔
210
}
20✔
211

212
::sdfg::transformations::SideEffectFinder::SideEffectFinder(
213
    StructuredSDFG& sdfg, analysis::AnalysisManager& analysis_manager, structured_control_flow::StructuredLoop& loop
214
)
215
    : visitor::ImmutableStructuredSDFGVisitor(sdfg, analysis_manager), loop_(loop) {}
20✔
216

217
} // namespace transformations
218
} // 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