• 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

59.88
/src/codegen/code_generators/cpp_code_generator.cpp
1
#include "sdfg/codegen/code_generators/cpp_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
CPPCodeGenerator::CPPCodeGenerator(ConditionalSchedule& schedule)
12✔
11
    : CodeGenerator(schedule, InstrumentationStrategy::NONE) {
6✔
12

13
      };
6✔
14

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

19
      };
×
20

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

29
std::string CPPCodeGenerator::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 \"C\" void " + function.name() + "(" + arglist.str() + ")";
1✔
42
};
1✔
43

44
bool CPPCodeGenerator::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 CPPCodeGenerator::dispatch_includes() {
5✔
108
    this->includes_stream_ << "#include <cmath>" << std::endl;
5✔
109
    if (this->instrumentation_strategy_ != InstrumentationStrategy::NONE)
5✔
110
        this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
×
111
    this->includes_stream_ << "#define __daisy_min(a,b) ((a)<(b)?(a):(b))" << std::endl;
5✔
112
    this->includes_stream_ << "#define __daisy_max(a,b) ((a)>(b)?(a):(b))" << std::endl;
5✔
113
    this->includes_stream_ << "#define __daisy_fma(a,b,c) a * b + c" << std::endl;
5✔
114
};
5✔
115

116
void CPPCodeGenerator::dispatch_structures() {
5✔
117
    auto& function = this->schedule_.schedule(0).sdfg();
5✔
118

119
    // Forward declarations
120
    for (auto& structure : function.structures()) {
9✔
121
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
4✔
122
    }
123

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

133
    for (auto& structure : names) {
9✔
134
        auto& definition = function.structure(structure);
4✔
135
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
136
            auto member_type = &definition.member_type(symbolic::integer(i));
4✔
137
            while (dynamic_cast<const types::Array*>(member_type)) {
4✔
138
                auto array_type = static_cast<const types::Array*>(member_type);
×
139
                member_type = &array_type->element_type();
×
140
            }
141

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

150
    std::list<Vertex> order;
5✔
151
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
5✔
152
    boost::topological_sort(graph, std::back_inserter(order),
10✔
153
                            boost::color_map(boost::make_assoc_property_map(vertex_colors)));
5✔
154
    order.reverse();
5✔
155

156
    for (auto& structure_index : order) {
9✔
157
        std::string structure = names.at(structure_index);
4✔
158
        auto& definition = function.structure(structure);
4✔
159
        this->classes_stream_ << "struct ";
4✔
160
        if (definition.is_packed()) {
4✔
161
            this->classes_stream_ << "__attribute__((packed)) ";
1✔
162
        }
1✔
163
        this->classes_stream_ << structure << std::endl;
4✔
164
        this->classes_stream_ << "{\n";
4✔
165

166
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
167
            auto& member_type = definition.member_type(symbolic::integer(i));
4✔
168
            if (dynamic_cast<const sdfg::types::Structure*>(&member_type)) {
4✔
169
                this->classes_stream_ << "struct ";
1✔
170
            }
1✔
171
            this->classes_stream_ << language_extension_.declaration("member_" + std::to_string(i),
4✔
172
                                                                     member_type);
4✔
173
            this->classes_stream_ << ";" << std::endl;
4✔
174
        }
4✔
175

176
        this->classes_stream_ << "};" << std::endl;
4✔
177
    }
4✔
178
};
5✔
179

180
void CPPCodeGenerator::dispatch_globals() {
5✔
181
    auto& function = this->schedule_.schedule(0).sdfg();
5✔
182
    for (auto& container : function.externals()) {
6✔
183
        this->globals_stream_ << "extern "
2✔
184
                              << language_extension_.declaration(container,
2✔
185
                                                                 function.type(container))
1✔
186
                              << ";" << std::endl;
1✔
187
    }
188
};
5✔
189

190
void CPPCodeGenerator::dispatch_schedule() {
5✔
191
    // Map external variables to internal variables
192
    auto& function = this->schedule_.schedule(0).sdfg();
5✔
193
    for (auto& container : function.containers()) {
6✔
194
        if (!function.is_internal(container)) {
1✔
195
            continue;
1✔
196
        }
197
        std::string external_name =
198
            container.substr(0, container.length() - external_suffix.length());
×
199
        this->main_stream_ << language_extension_.declaration(container, function.type(container));
×
200
        this->main_stream_ << " = "
×
201
                           << language_extension_.type_cast("&" + external_name,
×
202
                                                            function.type(container));
×
203
        this->main_stream_ << ";" << std::endl;
×
204
    }
×
205

206
    // Declare transient containers
207
    for (auto& container : function.containers()) {
6✔
208
        if (!function.is_transient(container)) {
1✔
209
            continue;
1✔
210
        }
211

212
        std::string val =
NEW
213
            this->language_extension_.declaration(container, function.type(container));
×
NEW
214
        if (!val.empty()) {
×
NEW
215
            this->main_stream_ << val;
×
NEW
216
            this->main_stream_ << ";" << std::endl;
×
NEW
217
        }
×
NEW
218
    }
×
219

220
    for (size_t i = 0; i < schedule_.size(); i++) {
10✔
221
        auto& schedule = schedule_.schedule(i);
5✔
222
        auto condition = schedule_.condition(i);
5✔
223

224
        // Add instrumentation
225
        auto instrumentation = create_instrumentation(instrumentation_strategy_, schedule);
5✔
226

227
        if (i > 0) {
5✔
228
            this->main_stream_ << "else ";
×
229
        }
×
230

231
        this->main_stream_ << "if (" << language_extension_.expression(condition) << ") {\n";
5✔
232

233
        auto& function_i = schedule.builder().subject();
5✔
234
        auto dispatcher =
235
            create_dispatcher(language_extension_, schedule, function_i.root(), *instrumentation);
5✔
236
        dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
5✔
237

238
        this->main_stream_ << "}\n";
5✔
239
    }
5✔
240
};
5✔
241

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