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

daisytuner / sdfglib / 18474094147

13 Oct 2025 05:52PM UTC coverage: 61.661% (+0.01%) from 61.651%
18474094147

push

github

web-flow
Merge pull request #273 from daisytuner/codegen-externals

updates codegen for externals

24 of 67 new or added lines in 3 files covered. (35.82%)

4 existing lines in 1 file now uncovered.

9103 of 14763 relevant lines covered (61.66%)

103.6 hits per line

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

79.53
/src/codegen/code_generators/c_code_generator.cpp
1
#include "sdfg/codegen/code_generators/c_code_generator.h"
2

3
#include "sdfg/analysis/analysis.h"
4
#include "sdfg/analysis/users.h"
5
#include "sdfg/codegen/dispatchers/node_dispatcher_registry.h"
6
#include "sdfg/codegen/instrumentation/capture_var_plan.h"
7
#include "sdfg/codegen/instrumentation/instrumentation_plan.h"
8

9
namespace sdfg {
10
namespace codegen {
11

12
std::string CCodeGenerator::function_definition() {
1✔
13
    /********** Arglist **********/
14
    std::vector<std::string> args;
1✔
15
    for (auto& container : sdfg_.arguments()) {
1✔
16
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
17
    }
18
    std::stringstream arglist;
1✔
19
    arglist << sdfg::helpers::join(args, ", ");
1✔
20
    std::string arglist_str = arglist.str();
1✔
21
    if (arglist_str.empty()) {
1✔
22
        arglist_str = "void";
1✔
23
    }
1✔
24

25
    return "extern " + this->language_extension_.declaration("", sdfg_.return_type()) + sdfg_.name() + "(" +
2✔
26
           arglist_str + ")";
1✔
27
};
1✔
28

29
void CCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
1✔
30
    std::string name = sdfg_.name();
1✔
31
    std::string arg_capture_path = sdfg_.metadata().at("arg_capture_path");
1✔
32

33
    ofs_source << "static void* __capture_ctx;" << std::endl;
1✔
34
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
1✔
35
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\", \"" << arg_capture_path << "\");" << std::endl;
1✔
36
    ofs_source << "}" << std::endl;
1✔
37
    ofs_source << std::endl;
1✔
38
}
1✔
39

40
void CCodeGenerator::dispatch_includes() {
3✔
41
    this->includes_stream_ << "#include <math.h>" << std::endl;
3✔
42
    this->includes_stream_ << "#include <alloca.h>" << std::endl;
3✔
43
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
3✔
44
    this->includes_stream_ << "#include <stdio.h>" << std::endl;
3✔
45
    this->includes_stream_ << "#include <stdlib.h>" << std::endl;
3✔
46
    this->includes_stream_ << "#include <string.h>" << std::endl;
3✔
47
    this->includes_stream_ << "#include <cblas.h>" << std::endl;
3✔
48
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
3✔
49
};
3✔
50

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

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

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

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

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

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

102
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
103
            auto& member_type = definition.member_type(symbolic::integer(i));
3✔
104
            if (auto pointer_type = dynamic_cast<const sdfg::types::Pointer*>(&member_type)) {
3✔
105
                if (pointer_type->has_pointee_type() &&
×
106
                    dynamic_cast<const sdfg::types::Structure*>(&pointer_type->pointee_type())) {
×
107
                    this->classes_stream_ << "struct ";
×
108
                }
×
109
            }
×
110
            this->classes_stream_
6✔
111
                << language_extension_.declaration("member_" + std::to_string(i), member_type, false, true);
3✔
112
            this->classes_stream_ << ";" << std::endl;
3✔
113
        }
3✔
114

115
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
116
    }
3✔
117
};
3✔
118

119
void CCodeGenerator::dispatch_globals() {
3✔
120
    // Declare globals
121
    const std::unordered_set<std::string> reserved_symbols = {
3✔
122
        "alloca",
3✔
123
        "calloc",
3✔
124
        "free",
3✔
125
        "malloc",
3✔
126
        "memcpy",
3✔
127
        "memmove",
3✔
128
        "memset",
3✔
129
        "stderr",
3✔
130
        "stdin",
3✔
131
        "stdout"
3✔
132
    };
133
    for (auto& container : sdfg_.externals()) {
4✔
134
        // Reserved symbols
135
        if (reserved_symbols.find(container) != reserved_symbols.end()) {
1✔
136
            continue;
×
137
        }
138

139
        // Function declarations
140
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
141
            // Declare function
NEW
142
            this->globals_stream_ << "extern ";
×
NEW
143
            this->globals_stream_ << language_extension_.declaration(container, sdfg_.type(container)) << ";"
×
NEW
144
                        << std::endl;
×
NEW
145
            continue;
×
146
        }
147

148
        // Other types must be pointers
149
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
150
        assert(type.has_pointee_type() && "Externals must have a pointee type");
2✔
151
        auto& base_type = type.pointee_type();
1✔
152

153
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
154
            this->globals_stream_ << "extern " << language_extension_.declaration(container, base_type) << ";"
2✔
155
                                  << std::endl;
1✔
156
        } else {
1✔
157
            this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
158
            if (!type.initializer().empty()) {
×
159
                this->globals_stream_ << " = " << type.initializer();
×
160
            }
×
161
            this->globals_stream_ << ";" << std::endl;
×
162
        }
163
    }
164
};
3✔
165

166
void CCodeGenerator::dispatch_schedule() {
3✔
167
    // Declare transient containers
168
    for (auto& container : sdfg_.containers()) {
4✔
169
        if (!sdfg_.is_transient(container)) {
1✔
170
            continue;
1✔
171
        }
172

173
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
174
        if (!val.empty()) {
×
175
            this->main_stream_ << val;
×
176
            this->main_stream_ << ";" << std::endl;
×
177
        }
×
178
    }
×
179

180
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
3✔
181
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
3✔
182
};
3✔
183

184
} // namespace codegen
185
} // 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