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

daisytuner / sdfglib / 18741216368

23 Oct 2025 07:41AM UTC coverage: 61.48% (+0.01%) from 61.468%
18741216368

push

github

web-flow
Merge pull request #297 from daisytuner/allocation-management

uses better naming policy for managed storage types

24 of 46 new or added lines in 7 files covered. (52.17%)

14 existing lines in 1 file now uncovered.

9709 of 15792 relevant lines covered (61.48%)

100.96 hits per line

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

82.64
/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 : sdfg_.arguments()) {
1✔
12
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
13
    }
14
    std::stringstream arglist;
1✔
15
    arglist << sdfg::helpers::join(args, ", ");
1✔
16
    std::string arglist_str = arglist.str();
1✔
17
    if (arglist_str.empty()) {
1✔
18
        arglist_str = "void";
1✔
19
    }
1✔
20

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

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

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

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

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

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

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

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

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

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

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

112
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
113
    }
3✔
114
};
10✔
115

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

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

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

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

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

166
        if (type.storage_type().allocation() == types::StorageType::AllocationType::Managed) {
7✔
167
            assert(
8✔
168
                !type.storage_type().allocation_size().is_null() &&
169
                "Managed allocations must have a valid allocation size"
170
            );
171
            if (type.storage_type().is_cpu_stack()) {
4✔
NEW
172
                this->main_stream_ << container << " = ";
×
NEW
173
                this->main_stream_
×
NEW
174
                    << "alloca(" << this->language_extension_.expression(type.storage_type().allocation_size()) << ")";
×
NEW
175
                this->main_stream_ << ";" << std::endl;
×
176
            } else if (type.storage_type().is_cpu_heap()) {
4✔
177
                this->main_stream_ << container << " = ";
4✔
178
                this->main_stream_ << this->externals_prefix_ << "malloc("
8✔
179
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
4✔
180
                                   << ")";
4✔
181
                this->main_stream_ << ";" << std::endl;
4✔
182
            }
4✔
183
        }
4✔
184
    }
185

186
    auto dispatcher =
187
        create_dispatcher(language_extension_, sdfg_, analysis_manager_, sdfg_.root(), instrumentation_plan_);
10✔
188
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
10✔
189

190
    // Free heap allocations
191
    for (auto& container : sdfg_.containers()) {
18✔
192
        if (sdfg_.is_external(container)) {
8✔
193
            continue;
1✔
194
        }
195
        auto& type = sdfg_.type(container);
7✔
196

197
        // Free if needed
198
        if (type.storage_type().deallocation() == types::StorageType::AllocationType::Managed) {
7✔
199
            if (type.storage_type().is_cpu_heap()) {
3✔
200
                this->main_stream_ << this->externals_prefix_ << "free(" << container << ");" << std::endl;
3✔
201
            }
3✔
202
        }
3✔
203
    }
204
};
10✔
205

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