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

daisytuner / sdfglib / 18896739436

28 Oct 2025 03:45PM UTC coverage: 62.202% (-0.1%) from 62.303%
18896739436

push

github

web-flow
Merge pull request #311 from daisytuner/storage-types

Storage types improvements

15 of 48 new or added lines in 3 files covered. (31.25%)

11 existing lines in 2 files now uncovered.

10119 of 16268 relevant lines covered (62.2%)

101.66 hits per line

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

73.94
/src/codegen/code_generators/c_code_generator.cpp
1
#include "sdfg/codegen/code_generators/c_code_generator.h"
2

3
#include "sdfg/codegen/dispatchers/node_dispatcher_registry.h"
4

5
namespace sdfg {
6
namespace codegen {
7

8
std::string CCodeGenerator::function_definition() {
1✔
9
    /********** Arglist **********/
10
    std::vector<std::string> args;
1✔
11
    if (this->sdfg_.name() == "main" && this->sdfg_.arguments().size() != 0) {
1✔
12
        auto& arg1 = this->sdfg_.arguments().at(0);
×
13
        sdfg::types::Scalar arg1_type(sdfg::types::PrimitiveType::Int32);
×
14

15
        auto& arg2 = this->sdfg_.arguments().at(1);
×
16
        sdfg::types::Scalar arg2_base_type(sdfg::types::PrimitiveType::UInt8);
×
17
        sdfg::types::Pointer arg2_ptr_type(arg2_base_type);
×
18
        sdfg::types::Pointer arg2_ptr_ptr_type(static_cast<const sdfg::types::IType&>(arg2_ptr_type));
×
19

20
        args.push_back(language_extension_.declaration(arg1, arg1_type));
×
21
        args.push_back(language_extension_.declaration(arg2, arg2_ptr_ptr_type));
×
22
    } else {
×
23
        for (auto& container : this->sdfg_.arguments()) {
1✔
24
            args.push_back(language_extension_.declaration(container, this->sdfg_.type(container)));
×
25
        }
26
    }
27

28
    std::stringstream arglist;
1✔
29
    arglist << sdfg::helpers::join(args, ", ");
1✔
30
    std::string arglist_str = arglist.str();
1✔
31
    if (arglist_str.empty()) {
1✔
32
        arglist_str = "void";
1✔
33
    }
1✔
34

35
    return "extern " + this->language_extension_.declaration("", sdfg_.return_type()) + sdfg_.name() + "(" +
2✔
36
           arglist_str + ")";
1✔
37
};
1✔
38

39
void CCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
1✔
40
    std::string name = sdfg_.name();
1✔
41
    std::string arg_capture_path = sdfg_.metadata().at("arg_capture_path");
1✔
42

43
    ofs_source << "static void* __capture_ctx;" << std::endl;
1✔
44
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
1✔
45
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\", \"" << arg_capture_path << "\");"
1✔
46
               << std::endl;
1✔
47
    ofs_source << "}" << std::endl;
1✔
48
    ofs_source << std::endl;
1✔
49
}
1✔
50

51
void CCodeGenerator::dispatch_includes() {
10✔
52
    this->includes_stream_ << "#include <math.h>" << std::endl;
10✔
53
    this->includes_stream_ << "#include <alloca.h>" << std::endl;
10✔
54
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
10✔
55
    this->includes_stream_ << "#include <stdio.h>" << std::endl;
10✔
56
    this->includes_stream_ << "#include <stdlib.h>" << std::endl;
10✔
57
    this->includes_stream_ << "#include <string.h>" << std::endl;
10✔
58
    this->includes_stream_ << "#include <cblas.h>" << std::endl;
10✔
59
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
10✔
60
};
10✔
61

62
void CCodeGenerator::dispatch_structures() {
10✔
63
    // Forward declarations
64
    for (auto& structure : sdfg_.structures()) {
13✔
65
        this->classes_stream_ << "typedef struct " << structure << " " << structure << ";" << std::endl;
3✔
66
    }
67

68
    // Generate topology-sorted structure definitions
69
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
70
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
71
    std::vector<std::string> names;
10✔
72
    for (auto& structure : sdfg_.structures()) {
13✔
73
        names.push_back(structure);
3✔
74
    }
75
    structures_graph graph(names.size());
10✔
76

77
    for (auto& structure : names) {
13✔
78
        auto& definition = sdfg_.structure(structure);
3✔
79
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
80
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
81
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
82
                auto array_type = static_cast<const types::Array*>(member_type);
×
83
                member_type = &array_type->element_type();
×
84
            }
85

86
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
3✔
87
                boost::add_edge(
1✔
88
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
89
                    std::find(names.begin(), names.end(), structure) - names.begin(),
1✔
90
                    graph
91
                );
92
            }
1✔
93
        }
3✔
94
    }
95

96
    std::list<Vertex> order;
10✔
97
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
10✔
98
    boost::topological_sort(
10✔
99
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
10✔
100
    );
101
    order.reverse();
10✔
102

103
    for (auto& structure_index : order) {
13✔
104
        std::string structure = names.at(structure_index);
3✔
105
        auto& definition = sdfg_.structure(structure);
3✔
106
        this->classes_stream_ << "typedef struct ";
3✔
107
        if (definition.is_packed()) {
3✔
108
            this->classes_stream_ << "__attribute__((packed)) ";
×
109
        }
×
110
        this->classes_stream_ << structure << std::endl;
3✔
111
        this->classes_stream_ << "{\n";
3✔
112

113
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
114
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
115
            if (auto pointer_type = dynamic_cast<const sdfg::types::Pointer*>(&member_type)) {
3✔
116
                if (pointer_type->has_pointee_type() &&
×
117
                    dynamic_cast<const sdfg::types::Structure*>(&pointer_type->pointee_type())) {
×
118
                    this->classes_stream_ << "struct ";
×
119
                }
×
120
            }
×
121
            this->classes_stream_
6✔
122
                << language_extension_.declaration("member_" + std::to_string(i), member_type, false, true);
3✔
123
            this->classes_stream_ << ";" << std::endl;
3✔
124
        }
3✔
125

126
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
127
    }
3✔
128
};
10✔
129

130
void CCodeGenerator::dispatch_globals() {
10✔
131
    // Declare globals
132
    for (auto& container : sdfg_.externals()) {
11✔
133
        // Function declarations
134
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
135
            // Declare function
136
            this->globals_stream_ << "extern ";
×
137
            this->globals_stream_
×
138
                << language_extension_.declaration(this->externals_prefix_ + container, sdfg_.type(container)) << ";"
×
139
                << std::endl;
×
140
            continue;
×
141
        }
142

143
        // Other types must be pointers
144
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
145
        assert(type.has_pointee_type() && "Externals must have a pointee type");
2✔
146
        auto& base_type = type.pointee_type();
1✔
147

148
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
149
            this->globals_stream_ << "extern "
2✔
150
                                  << language_extension_.declaration(this->externals_prefix_ + container, base_type)
1✔
151
                                  << ";" << std::endl;
1✔
152
        } else {
1✔
153
            this->globals_stream_ << "static "
×
154
                                  << language_extension_.declaration(this->externals_prefix_ + container, base_type);
×
155
            if (!type.initializer().empty()) {
×
156
                this->globals_stream_ << " = " << type.initializer();
×
157
            }
×
158
            this->globals_stream_ << ";" << std::endl;
×
159
        }
160
    }
161
};
10✔
162

163
void CCodeGenerator::dispatch_schedule() {
10✔
164
    // Allocate variables
165
    for (auto& container : sdfg_.containers()) {
18✔
166
        if (sdfg_.is_external(container)) {
8✔
167
            continue;
1✔
168
        }
169
        auto& type = sdfg_.type(container);
7✔
170

171
        // Declare transient
172
        if (sdfg_.is_transient(container)) {
7✔
173
            std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
3✔
174
            if (!val.empty()) {
3✔
175
                this->main_stream_ << val;
3✔
176
                this->main_stream_ << ";" << std::endl;
3✔
177
            }
3✔
178
        }
3✔
179

180
        if (type.storage_type().allocation() == types::StorageType::AllocationType::Managed) {
7✔
181
            assert(
8✔
182
                !type.storage_type().allocation_size().is_null() &&
183
                "Managed allocations must have a valid allocation size"
184
            );
185
            if (type.storage_type().is_cpu_stack()) {
4✔
186
                this->main_stream_ << container << " = ";
×
187
                this->main_stream_
×
188
                    << "alloca(" << this->language_extension_.expression(type.storage_type().allocation_size()) << ")";
×
189
                this->main_stream_ << ";" << std::endl;
×
190
            } else if (type.storage_type().is_cpu_heap()) {
4✔
191
                this->main_stream_ << container << " = ";
4✔
192
                this->main_stream_ << this->externals_prefix_ << "malloc("
8✔
193
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
4✔
194
                                   << ")";
4✔
195
                this->main_stream_ << ";" << std::endl;
4✔
196
            } else if (type.storage_type().is_nv_generic()) {
4✔
NEW
197
                this->main_stream_ << "cudaSetDevice(0);" << std::endl;
×
NEW
198
                this->main_stream_ << "cudaMalloc(&" << container << ", "
×
NEW
199
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
×
NEW
200
                                   << ");" << std::endl;
×
UNCOV
201
            }
×
202
        }
4✔
203
    }
204

205
    auto dispatcher =
206
        create_dispatcher(language_extension_, sdfg_, analysis_manager_, sdfg_.root(), instrumentation_plan_);
10✔
207
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
10✔
208

209
    if (sdfg_.root().size() == 0 ||
10✔
NEW
210
        !dynamic_cast<const structured_control_flow::Return*>(&(sdfg_.root().at(sdfg_.root().size() - 1)).first)) {
×
211
        // Free heap allocations
212
        for (auto& container : sdfg_.containers()) {
18✔
213
            if (sdfg_.is_external(container)) {
8✔
214
                continue;
1✔
215
            }
216
            auto& type = sdfg_.type(container);
7✔
217

218
            // Free if needed
219
            if (type.storage_type().deallocation() == types::StorageType::AllocationType::Managed) {
7✔
220
                if (type.storage_type().is_cpu_heap()) {
3✔
221
                    this->main_stream_ << this->externals_prefix_ << "free(" << container << ");" << std::endl;
3✔
222
                } else if (type.storage_type().is_nv_generic()) {
3✔
NEW
223
                    this->main_stream_ << "cudaSetDevice(0);" << std::endl;
×
NEW
224
                    this->main_stream_ << "cudaFree(" << container << ");" << std::endl;
×
NEW
225
                }
×
226
            }
3✔
227
        }
228
    }
10✔
229
};
10✔
230

231
} // namespace codegen
232
} // 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