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

daisytuner / sdfglib / 15455990921

05 Jun 2025 01:03AM UTC coverage: 57.883% (-0.4%) from 58.236%
15455990921

push

github

web-flow
Merge pull request #56 from daisytuner/allocations

removes allocation handling

15 of 36 new or added lines in 4 files covered. (41.67%)

27 existing lines in 4 files now uncovered.

8018 of 13852 relevant lines covered (57.88%)

107.95 hits per line

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

57.49
/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_factory.h"
4
#include "sdfg/codegen/instrumentation/instrumentation.h"
5
#include "sdfg/codegen/instrumentation/outermost_loops_instrumentation.h"
6

7
namespace sdfg {
8
namespace codegen {
9

10
CCodeGenerator::CCodeGenerator(ConditionalSchedule& schedule)
10✔
11
    : CodeGenerator(schedule, InstrumentationStrategy::NONE) {
5✔
12

13
      };
5✔
14

15
CCodeGenerator::CCodeGenerator(ConditionalSchedule& schedule,
×
16
                               InstrumentationStrategy instrumentation_strategy)
17
    : CodeGenerator(schedule, instrumentation_strategy) {
×
18

19
      };
×
20

21
bool CCodeGenerator::generate() {
4✔
22
    this->dispatch_includes();
4✔
23
    this->dispatch_structures();
4✔
24
    this->dispatch_globals();
4✔
25
    this->dispatch_schedule();
4✔
26
    return true;
4✔
27
};
28

29
std::string CCodeGenerator::function_definition() {
1✔
30
    // Define SDFG as a function
31
    auto& function = this->schedule_.schedule(0).sdfg();
1✔
32

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

41
    return "extern void " + function.name() + "(" + arglist.str() + ")";
1✔
42
};
1✔
43

44
bool CCodeGenerator::as_source(const std::filesystem::path& header_path,
×
45
                               const std::filesystem::path& source_path,
46
                               const std::filesystem::path& library_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
    std::ofstream ofs_library(library_path, std::ofstream::out);
×
58
    if (!ofs_library.is_open()) {
×
59
        return false;
×
60
    }
61

62
    ofs_header << "#pragma once" << std::endl;
×
63
    ofs_header << this->includes_stream_.str() << std::endl;
×
64
    ofs_header << this->classes_stream_.str() << std::endl;
×
65
    ofs_header.close();
×
66

67
    ofs_source << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
68
    ofs_source << this->globals_stream_.str() << std::endl;
×
69
    ofs_source << this->function_definition() << std::endl;
×
70
    ofs_source << "{" << std::endl;
×
71

72
    if (instrumentation_strategy_ != InstrumentationStrategy::NONE) {
×
73
        ofs_source << "__daisy_instrument_init();" << std::endl;
×
74
    }
×
75

76
    ofs_source << this->main_stream_.str() << std::endl;
×
77

78
    if (instrumentation_strategy_ != InstrumentationStrategy::NONE) {
×
79
        ofs_source << "__daisy_instrument_finalize();" << std::endl;
×
80
    }
×
81

82
    ofs_source << "}" << std::endl;
×
83
    ofs_source.close();
×
84

85
    auto library_content = this->library_stream_.str();
×
86
    if (library_content.empty()) {
×
87
        ofs_library.close();
×
88
        return true;
×
89
    }
90

91
    ofs_library << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
92
    ofs_library << std::endl;
×
93

94
    ofs_library << "#undef HWY_TARGET_INCLUDE" << std::endl;
×
95
    ofs_library << "#define HWY_TARGET_INCLUDE " << library_path.filename() << std::endl;
×
96
    ofs_library << "#include <hwy/foreach_target.h>" << std::endl;
×
97
    ofs_library << "#include <hwy/highway.h>" << std::endl;
×
98
    ofs_library << "#include <hwy/contrib/math/math-inl.h>" << std::endl;
×
99
    ofs_library << std::endl;
×
100

101
    ofs_library << library_content << std::endl;
×
102
    ofs_library.close();
×
103

104
    return true;
×
105
};
×
106

107
void CCodeGenerator::dispatch_includes() {
4✔
108
    this->includes_stream_ << "#include <math.h>" << std::endl;
4✔
109
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
4✔
110
    this->includes_stream_ << "#include <stdlib.h>" << std::endl;
4✔
111
    if (this->instrumentation_strategy_ != InstrumentationStrategy::NONE)
4✔
112
        this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
×
113

114
    this->includes_stream_ << "#define __daisy_min(a,b) ((a)<(b)?(a):(b))" << std::endl;
4✔
115
    this->includes_stream_ << "#define __daisy_max(a,b) ((a)>(b)?(a):(b))" << std::endl;
4✔
116
    this->includes_stream_ << "#define __daisy_fma(a,b,c) a * b + c" << std::endl;
4✔
117
};
4✔
118

119
void CCodeGenerator::dispatch_structures() {
4✔
120
    auto& function = this->schedule_.schedule(0).sdfg();
4✔
121

122
    // Forward declarations
123
    for (auto& structure : function.structures()) {
7✔
124
        this->classes_stream_ << "typedef struct " << structure << " " << structure << ";"
3✔
125
                              << std::endl;
3✔
126
    }
127

128
    // Generate topology-sorted structure definitions
129
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
130
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
131
    std::vector<std::string> names;
4✔
132
    for (auto& structure : function.structures()) {
7✔
133
        names.push_back(structure);
3✔
134
    }
135
    structures_graph graph(names.size());
4✔
136

137
    for (auto& structure : names) {
7✔
138
        auto& definition = function.structure(structure);
3✔
139
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
140
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
141
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
142
                auto array_type = static_cast<const types::Array*>(member_type);
×
143
                member_type = &array_type->element_type();
×
144
            }
145

146
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
3✔
147
                boost::add_edge(
1✔
148
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
149
                    std::find(names.begin(), names.end(), structure) - names.begin(), graph);
1✔
150
            }
1✔
151
        }
3✔
152
    }
153

154
    std::list<Vertex> order;
4✔
155
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
4✔
156
    boost::topological_sort(graph, std::back_inserter(order),
8✔
157
                            boost::color_map(boost::make_assoc_property_map(vertex_colors)));
4✔
158
    order.reverse();
4✔
159

160
    for (auto& structure_index : order) {
7✔
161
        std::string structure = names.at(structure_index);
3✔
162
        auto& definition = function.structure(structure);
3✔
163
        this->classes_stream_ << "typedef struct ";
3✔
164
        if (definition.is_packed()) {
3✔
165
            this->classes_stream_ << "__attribute__((packed)) ";
×
166
        }
×
167
        this->classes_stream_ << structure << std::endl;
3✔
168
        this->classes_stream_ << "{\n";
3✔
169

170
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
171
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
172
            if (auto pointer_type = dynamic_cast<const sdfg::types::Pointer*>(&member_type)) {
3✔
173
                if (dynamic_cast<const sdfg::types::Structure*>(&pointer_type->pointee_type())) {
×
174
                    this->classes_stream_ << "struct ";
×
175
                }
×
176
            }
×
177
            this->classes_stream_ << language_extension_.declaration("member_" + std::to_string(i),
3✔
178
                                                                     member_type);
3✔
179
            this->classes_stream_ << ";" << std::endl;
3✔
180
        }
3✔
181

182
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
183
    }
3✔
184
};
4✔
185

186
void CCodeGenerator::dispatch_globals() {
4✔
187
    auto& function = this->schedule_.schedule(0).sdfg();
4✔
188
    for (auto& container : function.externals()) {
5✔
189
        this->globals_stream_ << "extern "
2✔
190
                              << language_extension_.declaration(container,
2✔
191
                                                                 function.type(container))
1✔
192
                              << ";" << std::endl;
1✔
193
    }
194
};
4✔
195

196
void CCodeGenerator::dispatch_schedule() {
4✔
197
    // Map external variables to internal variables
198
    auto& function = this->schedule_.schedule(0).sdfg();
4✔
199
    for (auto& container : function.containers()) {
5✔
200
        if (!function.is_internal(container)) {
1✔
201
            continue;
1✔
202
        }
203
        std::string external_name =
204
            container.substr(0, container.length() - external_suffix.length());
×
205
        this->main_stream_ << language_extension_.declaration(container, function.type(container));
×
206
        this->main_stream_ << " = "
×
207
                           << language_extension_.type_cast("&" + external_name,
×
208
                                                            function.type(container));
×
209
        this->main_stream_ << ";" << std::endl;
×
210
    }
×
211

212
    // Declare transient containers
213
    for (auto& container : function.containers()) {
5✔
214
        if (!function.is_transient(container)) {
1✔
215
            continue;
1✔
216
        }
217

218
        std::string val =
NEW
219
            this->language_extension_.declaration(container, function.type(container));
×
NEW
220
        if (!val.empty()) {
×
NEW
221
            this->main_stream_ << val;
×
NEW
222
            this->main_stream_ << ";" << std::endl;
×
NEW
223
        }
×
NEW
224
    }
×
225

226
    for (size_t i = 0; i < schedule_.size(); i++) {
8✔
227
        auto& schedule = schedule_.schedule(i);
4✔
228
        auto condition = schedule_.condition(i);
4✔
229

230
        // Add instrumentation
231
        auto instrumentation = create_instrumentation(instrumentation_strategy_, schedule);
4✔
232

233
        if (i > 0) {
4✔
234
            this->main_stream_ << "else ";
×
235
        }
×
236

237
        this->main_stream_ << "if (" << language_extension_.expression(condition) << ") {\n";
4✔
238

239
        auto& function_i = schedule.builder().subject();
4✔
240
        auto dispatcher =
241
            create_dispatcher(language_extension_, schedule, function_i.root(), *instrumentation);
4✔
242
        dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
4✔
243

244
        this->main_stream_ << "}\n";
4✔
245
    }
4✔
246
};
4✔
247

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

© 2025 Coveralls, Inc