• 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

77.24
/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
#include "sdfg/codegen/instrumentation/instrumentation_plan.h"
5

6
namespace sdfg {
7
namespace codegen {
8

9
std::string CPPCodeGenerator::function_definition() {
1✔
10
    /********** Arglist **********/
11
    std::vector<std::string> args;
1✔
12
    for (auto& container : sdfg_.arguments()) {
1✔
13
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
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()) + sdfg_.name() + "(" +
2✔
23
           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 << "\");" << 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
    const std::unordered_set<std::string> reserved_symbols = {
4✔
115
        "alloca",
4✔
116
        "calloc",
4✔
117
        "free",
4✔
118
        "malloc",
4✔
119
        "memcpy",
4✔
120
        "memmove",
4✔
121
        "memset",
4✔
122
        "stderr",
4✔
123
        "stdin",
4✔
124
        "stdout"
4✔
125
    };
126
    for (auto& container : sdfg_.externals()) {
5✔
127
        // Reserved symbols
128
        if (reserved_symbols.find(container) != reserved_symbols.end()) {
1✔
129
            continue;
×
130
        }
131

132
        // Function declarations
133
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
NEW
134
            this->globals_stream_ << "extern \"C\" ";
×
NEW
135
            this->globals_stream_ << language_extension_.declaration(container, sdfg_.type(container)) << ";"
×
NEW
136
                        << std::endl;
×
NEW
137
            continue;
×
138
        }
139

140
        // Other types must be pointers
141
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
142
        assert(type.has_pointee_type() && "Externals must have a pointee type");
2✔
143
        auto& base_type = type.pointee_type();
1✔
144

145
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
146
            this->globals_stream_ << "extern " << language_extension_.declaration(container, base_type) << ";"
2✔
147
                                  << std::endl;
1✔
148
        } else {
1✔
149
            this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
150
            if (!type.initializer().empty()) {
×
151
                this->globals_stream_ << " = " << type.initializer();
×
152
            }
×
153
            this->globals_stream_ << ";" << std::endl;
×
154
        }
155
    }
156
};
4✔
157

158
void CPPCodeGenerator::dispatch_schedule() {
4✔
159
    // Declare transient containers
160
    for (auto& container : sdfg_.containers()) {
5✔
161
        if (!sdfg_.is_transient(container)) {
1✔
162
            continue;
1✔
163
        }
164

165
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
166
        if (!val.empty()) {
×
167
            this->main_stream_ << val;
×
168
            this->main_stream_ << ";" << std::endl;
×
169
        }
×
170
    }
×
171

172
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
4✔
173
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
4✔
174
};
4✔
175

176
} // namespace codegen
177
} // 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