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

daisytuner / sdfglib / 18122167774

30 Sep 2025 07:25AM UTC coverage: 61.108% (-0.01%) from 61.119%
18122167774

push

github

web-flow
Merge pull request #253 from daisytuner/type-rec-hotfix

Hotfix: dim_idx larger than range dims in analyze_type_rec

10 of 41 new or added lines in 2 files covered. (24.39%)

2 existing lines in 1 file now uncovered.

9567 of 15656 relevant lines covered (61.11%)

111.31 hits per line

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

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

NEW
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 << "\");" << std::endl;
×
32
    ofs_source << "}" << std::endl;
×
33
    ofs_source << std::endl;
×
34
}
×
35

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

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

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

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

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

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

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

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

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

111
void CPPCodeGenerator::dispatch_globals() {
4✔
112
    // Declare globals
113
    const std::unordered_set<std::string> reserved_symbols = {"stderr", "stdin", "stdout"};
4✔
114
    for (auto& container : sdfg_.externals()) {
5✔
115
        // Function declarations
116
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
117
            continue;
×
118
        }
119
        // Reserved symbols
120
        if (reserved_symbols.find(container) != reserved_symbols.end()) {
1✔
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 " << language_extension_.declaration(container, base_type) << ";"
2✔
131
                                  << std::endl;
1✔
132
        } else {
1✔
133
            this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
134
            if (!type.initializer().empty()) {
×
135
                this->globals_stream_ << " = " << type.initializer();
×
136
            }
×
137
            this->globals_stream_ << ";" << std::endl;
×
138
        }
139
    }
140
};
4✔
141

142
void CPPCodeGenerator::dispatch_schedule() {
4✔
143
    // Declare transient containers
144
    for (auto& container : sdfg_.containers()) {
5✔
145
        if (!sdfg_.is_transient(container)) {
1✔
146
            continue;
1✔
147
        }
148

149
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
150
        if (!val.empty()) {
×
151
            this->main_stream_ << val;
×
152
            this->main_stream_ << ";" << std::endl;
×
153
        }
×
154
    }
×
155

156
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
4✔
157
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
4✔
158
};
4✔
159

160
} // namespace codegen
161
} // 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