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

daisytuner / sdfglib / 20770413849

06 Jan 2026 10:50PM UTC coverage: 62.168% (+21.4%) from 40.764%
20770413849

push

github

web-flow
Merge pull request #433 from daisytuner/clang-coverage

updates clang coverage flags

14988 of 24109 relevant lines covered (62.17%)

88.57 hits per line

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

73.74
/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
    for (auto& container : this->sdfg_.arguments()) {
1✔
12
        args.push_back(language_extension_.declaration(container, this->sdfg_.type(container)));
×
13
    }
×
14

15
    std::stringstream arglist;
1✔
16
    arglist << sdfg::helpers::join(args, ", ");
1✔
17
    std::string arglist_str = arglist.str();
1✔
18
    if (arglist_str.empty()) {
1✔
19
        arglist_str = "void";
1✔
20
    }
1✔
21

22
    return "extern " + this->language_extension_.declaration("", sdfg_.return_type()) + this->externals_prefix_ +
1✔
23
           sdfg_.name() + "(" + arglist_str + ")";
1✔
24
};
1✔
25

26
void CCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
×
27
    std::string name = sdfg_.name();
×
28
    std::string arg_capture_path = sdfg_.metadata().at("arg_capture_path");
×
29

30
    ofs_source << "static void* __capture_ctx;" << std::endl;
×
31
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
×
32
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\", \"" << arg_capture_path << "\");"
×
33
               << std::endl;
×
34
    ofs_source << "}" << std::endl;
×
35
    ofs_source << std::endl;
×
36
}
×
37

38
void CCodeGenerator::dispatch_includes() {
10✔
39
    this->includes_stream_ << "#include <math.h>" << std::endl;
10✔
40
    this->includes_stream_ << "#include <alloca.h>" << std::endl;
10✔
41
    this->includes_stream_ << "#include <malloc.h>" << std::endl;
10✔
42
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
10✔
43
    this->includes_stream_ << "#include <stdio.h>" << std::endl;
10✔
44
    this->includes_stream_ << "#include <stdlib.h>" << std::endl;
10✔
45
    this->includes_stream_ << "#include <string.h>" << std::endl;
10✔
46
    this->includes_stream_ << "#include <cblas.h>" << std::endl;
10✔
47
    this->includes_stream_ << "#include <stdint.h>" << std::endl;
10✔
48
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
10✔
49
};
10✔
50

51
void CCodeGenerator::dispatch_structures() {
10✔
52
    // Forward declarations
53
    for (auto& structure : sdfg_.structures()) {
10✔
54
        this->classes_stream_ << "typedef struct " << structure << " " << structure << ";" << std::endl;
3✔
55
    }
3✔
56

57
    // Generate topology-sorted structure definitions
58
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
10✔
59
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
10✔
60
    std::vector<std::string> names;
10✔
61
    for (auto& structure : sdfg_.structures()) {
10✔
62
        names.push_back(structure);
3✔
63
    }
3✔
64
    structures_graph graph(names.size());
10✔
65

66
    for (auto& structure : names) {
10✔
67
        auto& definition = sdfg_.structure(structure);
3✔
68
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
69
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
70
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
71
                auto array_type = static_cast<const types::Array*>(member_type);
×
72
                member_type = &array_type->element_type();
×
73
            }
×
74

75
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
3✔
76
                boost::add_edge(
1✔
77
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
78
                    std::find(names.begin(), names.end(), structure) - names.begin(),
1✔
79
                    graph
1✔
80
                );
1✔
81
            }
1✔
82
        }
3✔
83
    }
3✔
84

85
    std::list<Vertex> order;
10✔
86
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
10✔
87
    boost::topological_sort(
10✔
88
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
10✔
89
    );
10✔
90
    order.reverse();
10✔
91

92
    for (auto& structure_index : order) {
10✔
93
        std::string structure = names.at(structure_index);
3✔
94
        auto& definition = sdfg_.structure(structure);
3✔
95
        this->classes_stream_ << "typedef struct ";
3✔
96
        if (definition.is_packed()) {
3✔
97
            this->classes_stream_ << "__attribute__((packed)) ";
×
98
        }
×
99
        this->classes_stream_ << structure << std::endl;
3✔
100
        this->classes_stream_ << "{\n";
3✔
101

102
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
103
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
104
            if (auto pointer_type = dynamic_cast<const sdfg::types::Pointer*>(&member_type)) {
3✔
105
                if (pointer_type->has_pointee_type() &&
×
106
                    dynamic_cast<const sdfg::types::Structure*>(&pointer_type->pointee_type())) {
×
107
                    this->classes_stream_ << "struct ";
×
108
                }
×
109
            }
×
110
            this->classes_stream_
3✔
111
                << language_extension_.declaration("member_" + std::to_string(i), member_type, false, true);
3✔
112
            this->classes_stream_ << ";" << std::endl;
3✔
113
        }
3✔
114

115
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
116
    }
3✔
117
};
10✔
118

119
void CCodeGenerator::dispatch_globals() {
10✔
120
    // Declare globals
121
    for (auto& container : sdfg_.externals()) {
10✔
122
        // Function declarations
123
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
124
            // Declare function
125
            this->globals_stream_ << "extern ";
×
126
            this->globals_stream_
×
127
                << language_extension_.declaration(this->externals_prefix_ + container, sdfg_.type(container)) << ";"
×
128
                << std::endl;
×
129
            continue;
×
130
        }
×
131

132
        // Other types must be pointers
133
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
134
        assert(type.has_pointee_type() && "Externals must have a pointee type");
1✔
135
        auto& base_type = type.pointee_type();
1✔
136

137
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
138
            this->globals_stream_ << "extern "
1✔
139
                                  << language_extension_.declaration(this->externals_prefix_ + container, base_type)
1✔
140
                                  << ";" << std::endl;
1✔
141
        } else {
1✔
142
            this->globals_stream_ << "static "
×
143
                                  << language_extension_.declaration(this->externals_prefix_ + container, base_type);
×
144
            if (!type.initializer().empty()) {
×
145
                this->globals_stream_ << " = " << type.initializer();
×
146
            }
×
147
            this->globals_stream_ << ";" << std::endl;
×
148
        }
×
149
    }
1✔
150
};
10✔
151

152
void CCodeGenerator::dispatch_schedule() {
10✔
153
    // Allocate variables
154
    for (auto& container : sdfg_.containers()) {
10✔
155
        if (sdfg_.is_external(container)) {
8✔
156
            continue;
1✔
157
        }
1✔
158
        auto& type = sdfg_.type(container);
7✔
159

160
        // Declare transient
161
        if (sdfg_.is_transient(container)) {
7✔
162
            std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
3✔
163
            if (!val.empty()) {
3✔
164
                this->main_stream_ << val;
3✔
165
                this->main_stream_ << ";" << std::endl;
3✔
166
            }
3✔
167
        }
3✔
168

169
        if (type.storage_type().allocation() == types::StorageType::AllocationType::Managed) {
7✔
170
            assert(
4✔
171
                !type.storage_type().allocation_size().is_null() &&
4✔
172
                "Managed allocations must have a valid allocation size"
4✔
173
            );
4✔
174
            if (type.storage_type().is_cpu_stack()) {
4✔
175
                this->main_stream_ << container << " = ";
×
176
                this->main_stream_
×
177
                    << "alloca(" << this->language_extension_.expression(type.storage_type().allocation_size()) << ")";
×
178
                this->main_stream_ << ";" << std::endl;
×
179
            } else if (type.storage_type().is_cpu_heap()) {
4✔
180
                this->main_stream_ << container << " = ";
4✔
181
                this->main_stream_ << this->externals_prefix_ << "malloc("
4✔
182
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
4✔
183
                                   << ")";
4✔
184
                this->main_stream_ << ";" << std::endl;
4✔
185
            } else if (type.storage_type().is_nv_generic()) {
4✔
186
                this->main_stream_ << "cudaSetDevice(0);" << std::endl;
×
187
                this->main_stream_ << "cudaMalloc(&" << container << ", "
×
188
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
×
189
                                   << ");" << std::endl;
×
190
            }
×
191
        }
4✔
192
    }
7✔
193

194
    auto dispatcher = create_dispatcher(
10✔
195
        language_extension_, sdfg_, analysis_manager_, sdfg_.root(), instrumentation_plan_, arg_capture_plan_
10✔
196
    );
10✔
197
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, *this->library_snippet_factory_);
10✔
198

199
    if (sdfg_.root().size() == 0 ||
10✔
200
        !dynamic_cast<const structured_control_flow::Return*>(&(sdfg_.root().at(sdfg_.root().size() - 1)).first)) {
10✔
201
        // Free heap allocations
202
        for (auto& container : sdfg_.containers()) {
10✔
203
            if (sdfg_.is_external(container)) {
8✔
204
                continue;
1✔
205
            }
1✔
206
            auto& type = sdfg_.type(container);
7✔
207

208
            // Free if needed
209
            if (type.storage_type().deallocation() == types::StorageType::AllocationType::Managed) {
7✔
210
                if (type.storage_type().is_cpu_heap()) {
3✔
211
                    this->main_stream_ << this->externals_prefix_ << "free(" << container << ");" << std::endl;
3✔
212
                } else if (type.storage_type().is_nv_generic()) {
3✔
213
                    this->main_stream_ << "cudaSetDevice(0);" << std::endl;
×
214
                    this->main_stream_ << "cudaFree(" << container << ");" << std::endl;
×
215
                }
×
216
            }
3✔
217
        }
7✔
218
    }
10✔
219
};
10✔
220

221
} // namespace codegen
222
} // 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