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

daisytuner / sdfglib / 18741216368

23 Oct 2025 07:41AM UTC coverage: 61.48% (+0.01%) from 61.468%
18741216368

push

github

web-flow
Merge pull request #297 from daisytuner/allocation-management

uses better naming policy for managed storage types

24 of 46 new or added lines in 7 files covered. (52.17%)

14 existing lines in 1 file now uncovered.

9709 of 15792 relevant lines covered (61.48%)

100.96 hits per line

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

62.14
/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 : sdfg_.arguments()) {
1✔
12
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
13
    }
14
    std::stringstream arglist;
1✔
15
    arglist << sdfg::helpers::join(args, ", ");
1✔
16
    std::string arglist_str = arglist.str();
1✔
17
    if (arglist_str.empty()) {
1✔
18
        arglist_str = "void";
1✔
19
    }
1✔
20

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

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

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

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

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

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

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

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

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

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

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

108
        this->classes_stream_ << "};" << std::endl;
4✔
109
    }
4✔
110
};
4✔
111

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

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

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

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

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

161
        // Allocate if needed
NEW
162
        if (type.storage_type().allocation() == types::StorageType::AllocationType::Managed) {
×
NEW
163
            assert(
×
164
                !type.storage_type().allocation_size().is_null() &&
165
                "Managed allocations must have a valid allocation size"
166
            );
NEW
167
            if (type.storage_type().is_cpu_stack()) {
×
NEW
168
                this->main_stream_ << container << " = ";
×
NEW
169
                this->main_stream_
×
NEW
170
                    << "alloca(" << this->language_extension_.expression(type.storage_type().allocation_size()) << ")";
×
NEW
171
                this->main_stream_ << ";" << std::endl;
×
NEW
172
            } else if (type.storage_type().is_cpu_heap()) {
×
NEW
173
                this->main_stream_ << container << " = ";
×
NEW
174
                this->main_stream_ << this->externals_prefix_ << "malloc("
×
NEW
175
                                   << this->language_extension_.expression(type.storage_type().allocation_size())
×
NEW
176
                                   << ")";
×
NEW
177
                this->main_stream_ << ";" << std::endl;
×
178
            }
×
179
        }
×
180
    }
181

182
    auto dispatcher =
183
        create_dispatcher(language_extension_, sdfg_, analysis_manager_, sdfg_.root(), instrumentation_plan_);
4✔
184
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
4✔
185

186
    // Free heap allocations
187
    for (auto& container : sdfg_.containers()) {
5✔
188
        if (sdfg_.is_external(container)) {
1✔
189
            continue;
1✔
190
        }
191
        auto& type = sdfg_.type(container);
×
192

193
        // Free if needed
NEW
194
        if (type.storage_type().deallocation() == types::StorageType::AllocationType::Managed) {
×
NEW
195
            if (type.storage_type().is_cpu_heap()) {
×
196
                this->main_stream_ << this->externals_prefix_ << "free(" << container << ");" << std::endl;
×
197
            }
×
198
        }
×
199
    }
200
};
4✔
201

202
} // namespace codegen
203
} // 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