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

daisytuner / sdfglib / 18187030480

02 Oct 2025 08:00AM UTC coverage: 61.116% (+0.05%) from 61.068%
18187030480

push

github

web-flow
Merge pull request #258 from daisytuner/debug-print-macro

adds macro for debug prints

0 of 16 new or added lines in 5 files covered. (0.0%)

10 existing lines in 2 files now uncovered.

9569 of 15657 relevant lines covered (61.12%)

111.31 hits per line

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

64.23
/src/codegen/code_generators/cuda_code_generator.cpp
1
#include "sdfg/codegen/code_generators/cuda_code_generator.h"
2

3
#include "sdfg/codegen/dispatchers/node_dispatcher_registry.h"
4
#include "sdfg/codegen/instrumentation/instrumentation_plan.h"
5
#include "sdfg/helpers/helpers.h"
6

7
namespace sdfg {
8
namespace codegen {
9

10
CUDACodeGenerator::CUDACodeGenerator(
4✔
11
    StructuredSDFG& sdfg,
12
    InstrumentationPlan& instrumentation_plan,
13
    bool capture_args_results,
14
    const std::pair<std::filesystem::path, std::filesystem::path>* output_and_header_paths
15
)
16
    : CodeGenerator(sdfg, instrumentation_plan, capture_args_results, output_and_header_paths),
4✔
17
      language_extension_(sdfg.externals()) {
4✔
18
    if (sdfg.type() != FunctionType_NV_GLOBAL) {
4✔
19
        throw std::runtime_error("CUDACodeGenerator can only be used for GPU SDFGs");
×
20
    }
21
    if (capture_args_results) {
4✔
NEW
22
        DEBUG_PRINTLN("CUDACodeGenerator does not support capturing args/results!");
×
23
    }
×
24
};
4✔
25

26
bool CUDACodeGenerator::generate() {
3✔
27
    this->dispatch_includes();
3✔
28
    this->dispatch_structures();
3✔
29
    this->dispatch_globals();
3✔
30
    this->dispatch_schedule();
3✔
31
    return true;
3✔
32
};
33

34
std::string CUDACodeGenerator::function_definition() {
1✔
35
    /********** Arglist **********/
36
    std::vector<std::string> args;
1✔
37
    for (auto& container : sdfg_.arguments()) {
1✔
38
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
39
    }
40
    std::stringstream arglist;
1✔
41
    arglist << sdfg::helpers::join(args, ", ");
1✔
42

43
    return "extern \"C\" __global__ void " + sdfg_.name() + "(" + arglist.str() + ")";
1✔
44
};
1✔
45

46
bool CUDACodeGenerator::as_source(const std::filesystem::path& header_path, const std::filesystem::path& source_path) {
×
47
    std::ofstream ofs_header(header_path, std::ofstream::out);
×
48
    if (!ofs_header.is_open()) {
×
49
        return false;
×
50
    }
51

52
    std::ofstream ofs_source(source_path, std::ofstream::out);
×
53
    if (!ofs_source.is_open()) {
×
54
        return false;
×
55
    }
56

57
    ofs_header << "#pragma once" << std::endl;
×
58
    ofs_header << this->includes_stream_.str() << std::endl;
×
59
    ofs_header << this->classes_stream_.str() << std::endl;
×
60
    ofs_header.close();
×
61

62
    ofs_source << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
63

64
    ofs_source << this->globals_stream_.str() << std::endl;
×
65

66
    append_function_source(ofs_source);
×
67

68
    ofs_source.close();
×
69

70
    return true;
×
71
};
×
72

73
void CUDACodeGenerator::append_function_source(std::ofstream& ofs_source) {
×
74
    ofs_source << this->function_definition() << std::endl;
×
75
    ofs_source << "{" << std::endl;
×
76
    ofs_source << this->main_stream_.str() << std::endl;
×
77
    ofs_source << "}" << std::endl;
×
78
}
×
79

80
void CUDACodeGenerator::dispatch_includes() {
3✔
81
    this->includes_stream_ << "#define "
3✔
82
                           << "__DAISY_NVVM__" << std::endl;
3✔
83
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
3✔
84
};
3✔
85

86
void CUDACodeGenerator::dispatch_structures() {
3✔
87
    // Forward declarations
88
    for (auto& structure : sdfg_.structures()) {
6✔
89
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
3✔
90
    }
91

92
    // Generate topology-sorted structure definitions
93
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
94
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
95
    std::vector<std::string> names;
3✔
96
    for (auto& structure : sdfg_.structures()) {
6✔
97
        names.push_back(structure);
3✔
98
    }
99
    structures_graph graph(names.size());
3✔
100

101
    for (auto& structure : names) {
6✔
102
        auto& definition = sdfg_.structure(structure);
3✔
103
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
104
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
105
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
106
                auto array_type = static_cast<const types::Array*>(member_type);
×
107
                member_type = &array_type->element_type();
×
108
            }
109

110
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
3✔
111
                boost::add_edge(
1✔
112
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
113
                    std::find(names.begin(), names.end(), structure) - names.begin(),
1✔
114
                    graph
115
                );
116
            }
1✔
117
        }
3✔
118
    }
119

120
    std::list<Vertex> order;
3✔
121
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
3✔
122
    boost::topological_sort(
3✔
123
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
3✔
124
    );
125
    order.reverse();
3✔
126

127
    for (auto& structure_index : order) {
6✔
128
        std::string structure = names.at(structure_index);
3✔
129
        auto& definition = sdfg_.structure(structure);
3✔
130
        this->classes_stream_ << "struct ";
3✔
131
        if (definition.is_packed()) {
3✔
132
            this->classes_stream_ << "__attribute__((packed)) ";
×
133
        }
×
134
        this->classes_stream_ << structure << std::endl;
3✔
135
        this->classes_stream_ << "{\n";
3✔
136

137
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
138
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
139
            if (dynamic_cast<const sdfg::types::Structure*>(&member_type)) {
3✔
140
                this->classes_stream_ << "struct ";
1✔
141
            }
1✔
142
            this->classes_stream_
6✔
143
                << language_extension_.declaration("member_" + std::to_string(i), member_type, false, true);
3✔
144
            this->classes_stream_ << ";" << std::endl;
3✔
145
        }
3✔
146

147
        this->classes_stream_ << "};" << std::endl;
3✔
148
    }
3✔
149
};
3✔
150

151
void CUDACodeGenerator::dispatch_globals() {
3✔
152
    for (auto& container : sdfg_.externals()) {
4✔
153
        auto& type = sdfg_.type(container);
1✔
154
        if (type.storage_type() == types::StorageType_NV_Global) {
1✔
155
            auto& base_type = dynamic_cast<const types::Pointer&>(type).pointee_type();
1✔
156
            if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
157
                this->globals_stream_ << "extern " << language_extension_.declaration(container, base_type) << ";"
2✔
158
                                      << std::endl;
1✔
159
            } else {
1✔
160
                this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
161
                if (!type.initializer().empty()) {
×
162
                    this->globals_stream_ << " = " << type.initializer();
×
163
                }
×
164
                this->globals_stream_ << ";" << std::endl;
×
165
            }
166
        }
1✔
167
        if (type.storage_type() == types::StorageType_NV_Constant) {
1✔
168
            assert(type.initializer().empty());
×
169
            auto& base_type = dynamic_cast<const types::Pointer&>(type).pointee_type();
×
170
            this->globals_stream_ << "__constant__ " << language_extension_.declaration(container, base_type, true)
×
171
                                  << ";" << std::endl;
×
172
        }
×
173
    }
174
};
3✔
175

176
void CUDACodeGenerator::dispatch_schedule() {
3✔
177
    // Declare shared memory
178
    for (auto& container : sdfg_.externals()) {
4✔
179
        auto& type = sdfg_.type(container);
1✔
180
        if (type.storage_type() == types::StorageType_NV_Shared) {
1✔
181
            this->main_stream_ << language_extension_.declaration(container, sdfg_.type(container)) << ";" << std::endl;
×
182
        }
×
183
    }
184

185
    // Declare transient containers
186
    for (auto& container : sdfg_.containers()) {
4✔
187
        if (!sdfg_.is_transient(container)) {
1✔
188
            continue;
1✔
189
        }
190

191
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
192
        if (!val.empty()) {
×
193
            this->main_stream_ << val;
×
194
            this->main_stream_ << ";" << std::endl;
×
195
        }
×
196
    }
×
197

198
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
3✔
199
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
3✔
200
};
3✔
201

202
} // namespace codegen
203
} // 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