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

daisytuner / sdfglib / 16188914472

10 Jul 2025 07:30AM UTC coverage: 64.808% (+0.1%) from 64.705%
16188914472

push

github

web-flow
Merge pull request #138 from daisytuner/tenstorrent/trivial

lib_stream -> lib_snippet_factory in code generation

131 of 243 new or added lines in 18 files covered. (53.91%)

3 existing lines in 3 files now uncovered.

8545 of 13185 relevant lines covered (64.81%)

179.13 hits per line

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

76.29
/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.h"
5
#include "sdfg/codegen/instrumentation/outermost_loops_instrumentation.h"
6

7
namespace sdfg {
8
namespace codegen {
9

10
std::string CPPCodeGenerator::function_definition() {
1✔
11
    /********** Arglist **********/
12
    std::vector<std::string> args;
1✔
13
    for (auto& container : sdfg_.arguments()) {
1✔
14
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
15
    }
16
    std::stringstream arglist;
1✔
17
    arglist << sdfg::helpers::join(args, ", ");
1✔
18

19
    return "extern \"C\" void " + sdfg_.name() + "(" + arglist.str() + ")";
1✔
20
};
1✔
21

NEW
22
void CPPCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
×
NEW
23
    std::string name = sdfg_.name();
×
24

NEW
25
    ofs_source << "static void* __capture_ctx;" << std::endl;
×
NEW
26
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
×
NEW
27
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\");" << std::endl;
×
28
    ofs_source << "}" << std::endl;
×
NEW
29
    ofs_source << std::endl;
×
NEW
30
}
×
31

32
void CPPCodeGenerator::dispatch_includes() {
5✔
33
    this->includes_stream_ << "#include <cmath>" << std::endl;
5✔
34
    if (this->instrumentation_strategy_ != InstrumentationStrategy::NONE)
5✔
35
        this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
×
36
    this->includes_stream_ << "#define __daisy_min(a,b) ((a)<(b)?(a):(b))" << std::endl;
5✔
37
    this->includes_stream_ << "#define __daisy_max(a,b) ((a)>(b)?(a):(b))" << std::endl;
5✔
38
    this->includes_stream_ << "#define __daisy_fma(a,b,c) a * b + c" << std::endl;
5✔
39
};
5✔
40

41
void CPPCodeGenerator::dispatch_structures() {
5✔
42
    // Forward declarations
43
    for (auto& structure : sdfg_.structures()) {
9✔
44
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
4✔
45
    }
46

47
    // Generate topology-sorted structure definitions
48
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
49
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
50
    std::vector<std::string> names;
5✔
51
    for (auto& structure : sdfg_.structures()) {
9✔
52
        names.push_back(structure);
4✔
53
    }
54
    structures_graph graph(names.size());
5✔
55

56
    for (auto& structure : names) {
9✔
57
        auto& definition = sdfg_.structure(structure);
4✔
58
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
59
            auto member_type = &definition.member_type(symbolic::integer(i));
4✔
60
            while (dynamic_cast<const types::Array*>(member_type)) {
4✔
61
                auto array_type = static_cast<const types::Array*>(member_type);
×
62
                member_type = &array_type->element_type();
×
63
            }
64

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

75
    std::list<Vertex> order;
5✔
76
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
5✔
77
    boost::topological_sort(
5✔
78
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
5✔
79
    );
80
    order.reverse();
5✔
81

82
    for (auto& structure_index : order) {
9✔
83
        std::string structure = names.at(structure_index);
4✔
84
        auto& definition = sdfg_.structure(structure);
4✔
85
        this->classes_stream_ << "struct ";
4✔
86
        if (definition.is_packed()) {
4✔
87
            this->classes_stream_ << "__attribute__((packed)) ";
1✔
88
        }
1✔
89
        this->classes_stream_ << structure << std::endl;
4✔
90
        this->classes_stream_ << "{\n";
4✔
91

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

102
        this->classes_stream_ << "};" << std::endl;
4✔
103
    }
4✔
104
};
5✔
105

106
void CPPCodeGenerator::dispatch_globals() {
5✔
107
    for (auto& container : sdfg_.externals()) {
6✔
108
        this->globals_stream_ << "extern " << language_extension_.declaration(container, sdfg_.type(container)) << ";"
2✔
109
                              << std::endl;
1✔
110
    }
111
};
5✔
112

113
void CPPCodeGenerator::dispatch_schedule() {
5✔
114
    // Map external variables to internal variables
115
    for (auto& container : sdfg_.containers()) {
6✔
116
        if (!sdfg_.is_internal(container)) {
1✔
117
            continue;
1✔
118
        }
NEW
119
        std::string external_name = container.substr(0, container.length() - external_suffix.length());
×
120
        this->main_stream_ << language_extension_.declaration(container, sdfg_.type(container));
×
NEW
121
        this->main_stream_ << " = " << language_extension_.type_cast("&" + external_name, sdfg_.type(container));
×
122
        this->main_stream_ << ";" << std::endl;
×
123
    }
×
124

125
    // Declare transient containers
126
    for (auto& container : sdfg_.containers()) {
6✔
127
        if (!sdfg_.is_transient(container)) {
1✔
128
            continue;
1✔
129
        }
130

NEW
131
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
132
        if (!val.empty()) {
×
133
            this->main_stream_ << val;
×
134
            this->main_stream_ << ";" << std::endl;
×
135
        }
×
136
    }
×
137

138
    // Add instrumentation
139
    auto instrumentation = create_instrumentation(instrumentation_strategy_, sdfg_);
5✔
140

141
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), *instrumentation);
5✔
142
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
5✔
143
};
5✔
144

145
} // namespace codegen
146
} // 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

© 2025 Coveralls, Inc