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

daisytuner / docc / 29438562417

15 Jul 2026 05:56PM UTC coverage: 63.155% (-0.02%) from 63.174%
29438562417

Pull #853

github

web-flow
Merge c9df0ad93 into 74b3dd197
Pull Request #853: Make LoopSchedulers available from DoccTarget

22 of 43 new or added lines in 4 files covered. (51.16%)

6 existing lines in 1 file now uncovered.

40628 of 64331 relevant lines covered (63.15%)

980.85 hits per line

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

70.0
/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::Map& map, bool allow_dynamic_sizes)
24
    : map_(map), allow_dynamic_sizes_(allow_dynamic_sizes) {}
38✔
25

26

27
bool OffloadTransform::can_be_applied(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
20✔
28
    auto& sdfg = builder.subject();
20✔
29

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

32
    if (!arguments_analysis.inferred_types(analysis_manager, this->map_)) {
20✔
33
        if (report_) report_->transform_impossible(this, "unranged args");
×
34
        DEBUG_PRINTLN("Cannot apply transform: argument types not inferred");
×
35
        return false;
×
36
    }
×
37
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->map_);
20✔
38

39
    // Criterion: arg Data Types must be continuous
40
    for (auto& [argument, meta] : arguments) {
41✔
41
        auto base_type = analysis::TypeAnalysis(sdfg, &map_, analysis_manager).get_outer_type(argument);
41✔
42
        if (base_type == nullptr) {
41✔
43
            if (report_) report_->transform_impossible(this, "cannot infer type");
×
44
            DEBUG_PRINTLN("Cannot apply transform: argument type cannot be inferred");
×
45
            return false;
×
46
        }
×
47
        if (!types::is_contiguous_type(*base_type, sdfg)) {
41✔
48
            if (report_) report_->transform_impossible(this, "type is not contiguous");
×
49
            DEBUG_PRINTLN("Cannot apply transform: argument type is not contiguous");
×
50
            return false;
×
51
        }
×
52
        if (meta.is_scalar && meta.is_output) {
41✔
53
            if (report_) report_->transform_impossible(this, "scalar output");
×
54
            DEBUG_PRINTLN("Cannot apply transform: map writes to scalar argument");
×
55
            return false;
×
56
        }
×
57
    }
41✔
58

59
    // Note: arbitrary `init` and `stride` are permitted on the kernel-boundary
60
    // Map. The CUDA/ROCm dispatchers emit
61
    //   `<map.indvar> = init + thread_flat_id * stride`,
62
    // and `num_iterations()` already accounts for both when computing the grid
63
    // geometry.
64
    if (map_.num_iterations().is_null()) {
20✔
65
        if (report_) report_->transform_impossible(this, "cannot determine num iterations");
×
66
        DEBUG_PRINTLN("Cannot apply transform: cannot determine number of iterations for map");
×
67
        return false;
×
68
    }
×
69

70
    // Criterion: Map cannot write to scalar arguments
71
    for (auto& [argument, meta] : arguments) {
41✔
72
        if (meta.is_scalar && meta.is_output) {
41✔
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
    }
41✔
78

79
    if (!arguments_analysis.argument_size_known(analysis_manager, this->map_, allow_dynamic_sizes_)) {
20✔
80
        if (report_) report_->transform_impossible(this, "args not understood");
×
81
        DEBUG_PRINTLN("Cannot apply transform: argument sizes not known");
×
82
        return false;
×
83
    }
×
84

85
    // Criterion: Map cannot contain function calls with side effects (e.g. library nodes that write to memory)
86
    SideEffectFinder side_effect_finder(sdfg, analysis_manager, this->map_);
20✔
87
    if (side_effect_finder.visit()) {
20✔
88
        if (report_) report_->transform_impossible(this, "side effects");
×
89
        DEBUG_PRINTLN("Cannot apply transform: map contains library nodes with side effects");
×
90
        return false;
×
91
    }
×
92

93
    if (report_) report_->transform_possible(this);
20✔
94
    return true;
20✔
95
}
20✔
96

97
void OffloadTransform::apply(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
18✔
98
    // Schedule
99
    builder.update_schedule_type(this->map_, transformed_schedule_type());
18✔
100

101
    auto& sdfg = builder.subject();
18✔
102

103
    // Identify arguments and locals
104
    auto& arguments_analysis = analysis_manager.get<analysis::ArgumentsAnalysis>();
18✔
105

106
    auto& arguments = arguments_analysis.arguments(analysis_manager, this->map_);
18✔
107
    auto& locals = arguments_analysis.locals(analysis_manager, this->map_);
18✔
108

109
    // Infer subsets for arguments
110
    auto& argument_sizes = arguments_analysis.argument_sizes(analysis_manager, this->map_, allow_dynamic_sizes_);
18✔
111

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

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

123
    // Allocate arguments and locals
124
    allocate_locals_on_device_stack(builder, analysis_manager, locals);
18✔
125
    handle_device_setup_and_teardown(builder, arguments, argument_sizes, container_prefix);
18✔
126

127
    // Copy-in arguments to device memory & allocation
128
    for (auto& [argument, meta] : arguments) {
37✔
129
        if (!meta.is_ptr) {
37✔
130
            continue;
18✔
131
        }
18✔
132
        auto argument_device = container_prefix + argument;
19✔
133
        auto& new_block = builder.add_block_before(*parent_scope, this->map_, {}, this->map_.debug_info());
19✔
134
        auto& size = argument_sizes.at(argument);
19✔
135
        copy_to_device_with_allocation(builder, argument, argument_device, size, SymEngine::null, new_block);
19✔
136
    }
19✔
137

138
    update_map_containers(arguments, container_prefix);
18✔
139

140
    // Copy-out arguments to host memory & free
141
    for (auto& [argument, meta] : arguments) {
37✔
142
        if (!meta.is_ptr) {
37✔
143
            continue;
18✔
144
        }
18✔
145
        auto argument_device = container_prefix + argument;
19✔
146
        auto& new_block = builder.add_block_after(*parent_scope, this->map_, {}, this->map_.debug_info());
19✔
147
        auto& size = argument_sizes.at(argument);
19✔
148
        if (!skip_unneeded_d2h_ || meta.is_output) {
19✔
149
            copy_from_device_with_free(builder, new_block, argument, argument_device, size, SymEngine::null);
17✔
150
        } else {
17✔
151
            deallocate_device_arg(builder, new_block, argument_device, size, SymEngine::null);
2✔
152
        }
2✔
153
    }
19✔
154

155
    if (report_) report_->transform_applied(this);
18✔
156
}
18✔
157

158
void OffloadTransform::handle_device_setup_and_teardown(
159
    builder::StructuredSDFGBuilder& builder,
160

161
    const std::map<std::string, analysis::RegionArgument>& arguments,
162
    const std::unordered_map<std::string, symbolic::Expression>& argument_sizes,
163
    std::string prefix
164
) {
18✔
165
    // Add managed buffers for pointer arguments
166
    for (auto& [argument, meta] : arguments) {
37✔
167
        if (!meta.is_ptr || builder.subject().exists(prefix + argument)) {
37✔
168
            continue;
18✔
169
        }
18✔
170
        auto argument_device = prefix + argument;
19✔
171

172
        auto arg_size = argument_sizes.at(argument);
19✔
173

174
        add_device_buffer(builder, argument, argument_device, arg_size);
19✔
175
    }
19✔
176
}
18✔
177

178
void OffloadTransform::
179
    update_map_containers(const std::map<std::string, analysis::RegionArgument>& arguments, std::string prefix) {
18✔
180
    for (auto& [argument, meta] : arguments) {
37✔
181
        if (meta.is_ptr) {
37✔
182
            auto argument_device = prefix + argument;
19✔
183
            this->map_.replace(symbolic::symbol(argument), symbolic::symbol(argument_device));
19✔
184
        }
19✔
185
    }
37✔
186
}
18✔
187

188
bool ::sdfg::transformations::SideEffectFinder::accept(structured_control_flow::Block& node) {
21✔
189
    for (const auto& lib_node : node.dataflow().library_nodes()) {
21✔
NEW
190
        if (lib_node->side_effect()) {
×
NEW
191
            return true;
×
NEW
192
        }
×
NEW
193
    }
×
194
    return false;
21✔
195
}
21✔
196

197
bool ::sdfg::transformations::SideEffectFinder::visit() {
20✔
198
    return visitor::ImmutableStructuredSDFGVisitor::visit_internal(map_.StructuredLoop::root());
20✔
199
}
20✔
200

201
::sdfg::transformations::SideEffectFinder::SideEffectFinder(
202
    StructuredSDFG& sdfg, analysis::AnalysisManager& analysis_manager, structured_control_flow::Map& map
203
)
204
    : visitor::ImmutableStructuredSDFGVisitor(sdfg, analysis_manager), map_(map) {}
20✔
205

206
} // namespace transformations
207
} // 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