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

daisytuner / sdfglib / 15049122732

15 May 2025 03:33PM UTC coverage: 62.241% (+4.7%) from 57.525%
15049122732

Pull #9

github

web-flow
Merge b96e33e0e into 9d3b1a2b3
Pull Request #9: Graphviz DOT Visualizer for SDFGs

520 of 542 new or added lines in 3 files covered. (95.94%)

782 existing lines in 68 files now uncovered.

8049 of 12932 relevant lines covered (62.24%)

504.09 hits per line

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

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

5
namespace sdfg {
6
namespace codegen {
7

8
CPPCodeGenerator::CPPCodeGenerator(ConditionalSchedule& schedule, bool instrumented)
12✔
9
    : CodeGenerator(schedule, instrumented){
6✔
10

11
      };
6✔
12

13
bool CPPCodeGenerator::generate() {
5✔
14
    this->dispatch_includes();
5✔
15
    this->dispatch_structures();
5✔
16
    this->dispatch_globals();
5✔
17
    this->dispatch_schedule();
5✔
18
    return true;
5✔
19
};
20

21
std::string CPPCodeGenerator::function_definition() {
1✔
22
    // Define SDFG as a function
23
    auto& function = this->schedule_.schedule(0).sdfg();
1✔
24

25
    /********** Arglist **********/
26
    std::vector<std::string> args;
1✔
27
    for (auto& container : function.arguments()) {
1✔
28
        args.push_back(language_extension_.declaration(container, function.type(container)));
×
29
    }
30
    std::stringstream arglist;
1✔
31
    arglist << sdfg::helpers::join(args, ", ");
1✔
32

33
    return "extern \"C\" void " + function.name() + "(" + arglist.str() + ")";
1✔
34
};
1✔
35

36
bool CPPCodeGenerator::as_source(const std::filesystem::path& header_path,
×
37
                                 const std::filesystem::path& source_path,
38
                                 const std::filesystem::path& library_path) {
39
    std::ofstream ofs_header(header_path, std::ofstream::out);
×
40
    if (!ofs_header.is_open()) {
×
41
        return false;
×
42
    }
43

44
    std::ofstream ofs_source(source_path, std::ofstream::out);
×
45
    if (!ofs_source.is_open()) {
×
46
        return false;
×
47
    }
48

49
    std::ofstream ofs_library(library_path, std::ofstream::out);
×
50
    if (!ofs_library.is_open()) {
×
51
        return false;
×
52
    }
53

54
    ofs_header << "#pragma once" << std::endl;
×
55
    ofs_header << this->includes_stream_.str() << std::endl;
×
56
    ofs_header << this->classes_stream_.str() << std::endl;
×
57
    ofs_header.close();
×
58

59
    ofs_source << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
60
    ofs_source << this->globals_stream_.str() << std::endl;
×
61
    ofs_source << this->function_definition() << std::endl;
×
62
    ofs_source << "{" << std::endl;
×
63

64
    if (instrumented_) {
×
65
        ofs_source << "__daisy_instrument_init();" << std::endl;
×
UNCOV
66
    }
×
67

68
    ofs_source << this->main_stream_.str() << std::endl;
×
69
    
70
    if (instrumented_) {
×
71
        ofs_source << "__daisy_instrument_finalize();" << std::endl;
×
UNCOV
72
    }
×
73
    
74
    ofs_source << "}" << std::endl;
×
75
    ofs_source.close();
×
76

77
    auto library_content = this->library_stream_.str();
×
78
    if (library_content.empty()) {
×
79
        ofs_library.close();
×
80
        return true;
×
81
    }
82

83
    ofs_library << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
84
    ofs_library << std::endl;
×
85

86
    ofs_library << "#undef HWY_TARGET_INCLUDE" << std::endl;
×
87
    ofs_library << "#define HWY_TARGET_INCLUDE " << library_path.filename() << std::endl;
×
88
    ofs_library << "#include <hwy/foreach_target.h>" << std::endl;
×
89
    ofs_library << "#include <hwy/highway.h>" << std::endl;
×
90
    ofs_library << "#include <hwy/contrib/math/math-inl.h>" << std::endl;
×
91
    ofs_library << std::endl;
×
92

93
    ofs_library << library_content << std::endl;
×
94
    ofs_library.close();
×
95

96
    return true;
×
97
};
×
98

99
void CPPCodeGenerator::dispatch_includes() {
5✔
100
    this->includes_stream_ << "#include <cmath>" << std::endl;
5✔
101
    if (instrumented_) this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
5✔
102
    this->includes_stream_ << "#define __daisy_min(a,b) ((a)<(b)?(a):(b))" << std::endl;
5✔
103
    this->includes_stream_ << "#define __daisy_max(a,b) ((a)>(b)?(a):(b))" << std::endl;
5✔
104
    this->includes_stream_ << "#define __daisy_fma(a,b,c) a * b + c" << std::endl;
5✔
105
};
5✔
106

107
void CPPCodeGenerator::dispatch_structures() {
5✔
108
    auto& function = this->schedule_.schedule(0).sdfg();
5✔
109

110
    // Forward declarations
111
    for (auto& structure : function.structures()) {
9✔
112
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
4✔
113
    }
114

115
    // Generate topology-sorted structure definitions
116
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
117
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
118
    std::vector<std::string> names;
5✔
119
    for (auto& structure : function.structures()) {
9✔
120
        names.push_back(structure);
4✔
121
    }
122
    structures_graph graph(names.size());
5✔
123

124
    for (auto& structure : names) {
9✔
125
        auto& definition = function.structure(structure);
4✔
126
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
127
            auto member_type = &definition.member_type(symbolic::integer(i));
4✔
128
            while (dynamic_cast<const types::Array*>(member_type)) {
4✔
129
                auto array_type = static_cast<const types::Array*>(member_type);
×
130
                member_type = &array_type->element_type();
×
131
            }
132

133
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
4✔
134
                boost::add_edge(
1✔
135
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
136
                    std::find(names.begin(), names.end(), structure) - names.begin(), graph);
1✔
137
            }
1✔
138
        }
4✔
139
    }
140

141
    std::list<Vertex> order;
5✔
142
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
5✔
143
    boost::topological_sort(graph, std::back_inserter(order),
10✔
144
                            boost::color_map(boost::make_assoc_property_map(vertex_colors)));
5✔
145
    order.reverse();
5✔
146

147
    for (auto& structure_index : order) {
9✔
148
        std::string structure = names.at(structure_index);
4✔
149
        auto& definition = function.structure(structure);
4✔
150
        this->classes_stream_ << "struct ";
4✔
151
        if (definition.is_packed()) {
4✔
152
            this->classes_stream_ << "__attribute__((packed)) ";
1✔
153
        }
1✔
154
        this->classes_stream_ << structure << std::endl;
4✔
155
        this->classes_stream_ << "{\n";
4✔
156

157
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
158
            auto& member_type = definition.member_type(symbolic::integer(i));
4✔
159
            if (dynamic_cast<const sdfg::types::Structure*>(&member_type)) {
4✔
160
                this->classes_stream_ << "struct ";
1✔
161
            }
1✔
162
            this->classes_stream_ << language_extension_.declaration("member_" + std::to_string(i),
4✔
163
                                                                     member_type);
4✔
164
            this->classes_stream_ << ";" << std::endl;
4✔
165
        }
4✔
166

167
        this->classes_stream_ << "};" << std::endl;
4✔
168
    }
4✔
169
};
5✔
170

171
void CPPCodeGenerator::dispatch_globals() {
5✔
172
    auto& function = this->schedule_.schedule(0).sdfg();
5✔
173
    for (auto& container : function.externals()) {
6✔
174
        this->globals_stream_ << "extern "
2✔
175
                              << language_extension_.declaration(container,
2✔
176
                                                                 function.type(container))
1✔
177
                              << ";" << std::endl;
1✔
178
    }
179
};
5✔
180

181
void CPPCodeGenerator::dispatch_schedule() {
5✔
182
    // Map external variables to internal variables
183
    auto& function = this->schedule_.schedule(0).sdfg();
5✔
184
    for (auto& container : function.containers()) {
6✔
185
        if (!function.is_internal(container)) {
1✔
186
            continue;
1✔
187
        }
188
        std::string external_name =
189
            container.substr(0, container.length() - external_suffix.length());
×
190
        this->main_stream_ << language_extension_.declaration(container, function.type(container));
×
191
        this->main_stream_ << " = "
×
192
                           << "&" << external_name;
×
193
        this->main_stream_ << ";" << std::endl;
×
194
    }
×
195

196
    for (size_t i = 0; i < schedule_.size(); i++) {
10✔
197
        if (i == 0) {
5✔
198
            this->main_stream_ << "if (" << language_extension_.expression(schedule_.condition(i))
10✔
199
                               << ") {\n";
5✔
200

201
            auto& function_i = schedule_.schedule(i).builder().subject();
5✔
202
            auto dispatcher = create_dispatcher(language_extension_, schedule_.schedule(i),
10✔
203
                                                function_i.root(), this->instrumented_);
5✔
204
            dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
5✔
205

206
            this->main_stream_ << "}\n";
5✔
207
        } else {
5✔
208
            this->main_stream_ << "else if ("
×
209
                               << language_extension_.expression(schedule_.condition(i)) << ") {\n";
×
210

211
            auto& function_i = schedule_.schedule(i).builder().subject();
×
212
            auto dispatcher = create_dispatcher(language_extension_, schedule_.schedule(i),
×
213
                                                function_i.root(), this->instrumented_);
×
214
            dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_stream_);
×
215

216
            this->main_stream_ << "}\n";
×
217
        }
×
218
    }
5✔
219
};
5✔
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