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

daisytuner / sdfglib / 20770413849

06 Jan 2026 10:50PM UTC coverage: 62.168% (+21.4%) from 40.764%
20770413849

push

github

web-flow
Merge pull request #433 from daisytuner/clang-coverage

updates clang coverage flags

14988 of 24109 relevant lines covered (62.17%)

88.57 hits per line

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

60.0
/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_registry.h"
4

5
namespace sdfg {
6
namespace codegen {
7

8
std::string CPPCodeGenerator::function_definition() {
1✔
9
    /********** Arglist **********/
10
    std::vector<std::string> args;
1✔
11
    for (auto& container : this->sdfg_.arguments()) {
1✔
12
        args.push_back(language_extension_.declaration(container, this->sdfg_.type(container)));
×
13
    }
×
14

15
    std::stringstream arglist;
1✔
16
    arglist << sdfg::helpers::join(args, ", ");
1✔
17
    std::string arglist_str = arglist.str();
1✔
18
    if (arglist_str.empty()) {
1✔
19
        arglist_str = "void";
1✔
20
    }
1✔
21

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

26
void CPPCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
×
27
    std::string name = sdfg_.name();
×
28
    std::string arg_capture_path = sdfg_.metadata().at("arg_capture_path");
×
29

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

38
void CPPCodeGenerator::dispatch_includes() {
4✔
39
    this->includes_stream_ << "#include <alloca.h>" << std::endl;
4✔
40
    this->includes_stream_ << "#include <malloc.h>" << std::endl;
4✔
41
    this->includes_stream_ << "#include <cmath>" << std::endl;
4✔
42
    this->includes_stream_ << "#include <cstdio>" << std::endl;
4✔
43
    this->includes_stream_ << "#include <cstdlib>" << std::endl;
4✔
44
    this->includes_stream_ << "#include <cstring>" << std::endl;
4✔
45
    this->includes_stream_ << "#include <cblas.h>" << std::endl;
4✔
46
    this->includes_stream_ << "#include <cstdint>" << std::endl;
4✔
47
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
4✔
48
};
4✔
49

50
void CPPCodeGenerator::dispatch_structures() {
4✔
51
    // Forward declarations
52
    for (auto& structure : sdfg_.structures()) {
4✔
53
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
4✔
54
    }
4✔
55

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

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

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

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

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

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

111
        this->classes_stream_ << "};" << std::endl;
4✔
112
    }
4✔
113
};
4✔
114

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

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

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

147
void CPPCodeGenerator::dispatch_schedule() {
4✔
148
    // Allocate variables
149
    for (auto& container : sdfg_.containers()) {
4✔
150
        if (sdfg_.is_external(container)) {
1✔
151
            continue;
1✔
152
        }
1✔
153
        auto& type = sdfg_.type(container);
×
154

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

164
        // Allocate if needed
165
        if (type.storage_type().allocation() == types::StorageType::AllocationType::Managed) {
×
166
            assert(
×
167
                !type.storage_type().allocation_size().is_null() &&
×
168
                "Managed allocations must have a valid allocation size"
×
169
            );
×
170
            if (type.storage_type().is_cpu_stack()) {
×
171
                this->main_stream_ << container << " = ";
×
172
                this->main_stream_
×
173
                    << "alloca(" << this->language_extension_.expression(type.storage_type().allocation_size()) << ")";
×
174
                this->main_stream_ << ";" << std::endl;
×
175
            } else if (type.storage_type().is_cpu_heap()) {
×
176
                this->main_stream_ << container << " = ";
×
177
                this->main_stream_ << this->externals_prefix_ << "malloc("
×
178
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
×
179
                                   << ")";
×
180
                this->main_stream_ << ";" << std::endl;
×
181
            } else if (type.storage_type().is_nv_generic()) {
×
182
                this->main_stream_ << "cudaSetDevice(0);" << std::endl;
×
183
                this->main_stream_ << "cudaMalloc(&" << container << ", "
×
184
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
×
185
                                   << ");" << std::endl;
×
186
            }
×
187
        }
×
188
    }
×
189

190
    auto dispatcher = create_dispatcher(
4✔
191
        language_extension_, sdfg_, analysis_manager_, sdfg_.root(), instrumentation_plan_, arg_capture_plan_
4✔
192
    );
4✔
193
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, *this->library_snippet_factory_);
4✔
194

195
    if (sdfg_.root().size() == 0 ||
4✔
196
        !dynamic_cast<const structured_control_flow::Return*>(&(sdfg_.root().at(sdfg_.root().size() - 1)).first)) {
4✔
197
        // Free heap allocations
198
        for (auto& container : sdfg_.containers()) {
4✔
199
            if (sdfg_.is_external(container)) {
1✔
200
                continue;
1✔
201
            }
1✔
202
            auto& type = sdfg_.type(container);
×
203

204
            // Free if needed
205
            if (type.storage_type().deallocation() == types::StorageType::AllocationType::Managed) {
×
206
                if (type.storage_type().is_cpu_heap()) {
×
207
                    this->main_stream_ << this->externals_prefix_ << "free(" << container << ");" << std::endl;
×
208
                } else if (type.storage_type().is_nv_generic()) {
×
209
                    this->main_stream_ << "cudaSetDevice(0);" << std::endl;
×
210
                    this->main_stream_ << "cudaFree(" << container << ");" << std::endl;
×
211
                }
×
212
            }
×
213
        }
×
214
    }
4✔
215
};
4✔
216

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