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

daisytuner / sdfglib / 15142284085

20 May 2025 03:58PM UTC coverage: 60.528% (-0.02%) from 60.543%
15142284085

push

github

web-flow
Merge pull request #23 from daisytuner/loops

LoopTrees and Instrumentation Strategies

80 of 182 new or added lines in 17 files covered. (43.96%)

11 existing lines in 6 files now uncovered.

7928 of 13098 relevant lines covered (60.53%)

104.16 hits per line

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

59.75
/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

5
#include "sdfg/codegen/instrumentation/instrumentation.h"
6
#include "sdfg/codegen/instrumentation/outermost_loops_instrumentation.h"
7

8
namespace sdfg {
9
namespace codegen {
10

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

14
      };
5✔
15

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

UNCOV
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

NEW
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

NEW
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) this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
4✔
112

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

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

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

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

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

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

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

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

169
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
170
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
171
            if (auto pointer_type = dynamic_cast<const sdfg::types::Pointer*>(&member_type)) {
3✔
172
                if (dynamic_cast<const sdfg::types::Structure*>(
×
173
                        &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
                           << "&" << external_name;
×
208
        this->main_stream_ << ";" << std::endl;
×
209
    }
×
210

211
    for (size_t i = 0; i < schedule_.size(); i++) {
8✔
212
        auto& schedule = schedule_.schedule(i);
4✔
213
        auto condition = schedule_.condition(i);
4✔
214
        
215
        // Add instrumentation
216
        auto instrumentation = create_instrumentation(instrumentation_strategy_, schedule);
4✔
217

218
        if (i > 0) {
4✔
NEW
219
            this->main_stream_ << "else ";
×
NEW
220
        }
×
221

222
        this->main_stream_ << "if (" << language_extension_.expression(condition)
8✔
223
                               << ") {\n";
4✔
224

225
        auto& function_i = schedule.builder().subject();
4✔
226
        auto dispatcher = create_dispatcher(language_extension_, schedule,
4✔
227
                                            function_i.root(), *instrumentation);
4✔
228
        dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
4✔
229

230
        this->main_stream_ << "}\n";
4✔
231
    }
4✔
232
};
4✔
233

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