• 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

79.21
/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.h"
8
#include "sdfg/codegen/instrumentation/outermost_loops_instrumentation.h"
9

10
namespace sdfg {
11
namespace codegen {
12

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

22
    return "extern void " + sdfg_.name() + "(" + arglist.str() + ")";
1✔
23
};
1✔
24

25
void CCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
1✔
26
    std::string name = sdfg_.name();
1✔
27

28
    ofs_source << "static void* __capture_ctx;" << std::endl;
1✔
29
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
1✔
30
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\");" << std::endl;
1✔
31
    ofs_source << "}" << std::endl;
1✔
32
    ofs_source << std::endl;
1✔
33
}
1✔
34

35
void CCodeGenerator::dispatch_includes() {
4✔
36
    this->includes_stream_ << "#include <math.h>" << std::endl;
4✔
37
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
4✔
38
    this->includes_stream_ << "#include <stdlib.h>" << std::endl;
4✔
39
    if (this->instrumentation_strategy_ != InstrumentationStrategy::NONE)
4✔
40
        this->includes_stream_ << "#include <daisy_rtl.h>" << std::endl;
×
41

42
    this->includes_stream_ << "#define __daisy_min(a,b) ((a)<(b)?(a):(b))" << std::endl;
4✔
43
    this->includes_stream_ << "#define __daisy_max(a,b) ((a)>(b)?(a):(b))" << std::endl;
4✔
44
    this->includes_stream_ << "#define __daisy_fma(a,b,c) a * b + c" << std::endl;
4✔
45
};
4✔
46

47
void CCodeGenerator::dispatch_structures() {
4✔
48
    // Forward declarations
49
    for (auto& structure : sdfg_.structures()) {
7✔
50
        this->classes_stream_ << "typedef struct " << structure << " " << structure << ";" << std::endl;
3✔
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()) {
7✔
58
        names.push_back(structure);
3✔
59
    }
60
    structures_graph graph(names.size());
4✔
61

62
    for (auto& structure : names) {
7✔
63
        auto& definition = sdfg_.structure(structure);
3✔
64
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
65
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
66
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
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)) {
3✔
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
        }
3✔
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) {
7✔
89
        std::string structure = names.at(structure_index);
3✔
90
        auto& definition = sdfg_.structure(structure);
3✔
91
        this->classes_stream_ << "typedef struct ";
3✔
92
        if (definition.is_packed()) {
3✔
93
            this->classes_stream_ << "__attribute__((packed)) ";
×
94
        }
×
95
        this->classes_stream_ << structure << std::endl;
3✔
96
        this->classes_stream_ << "{\n";
3✔
97

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

110
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
111
    }
3✔
112
};
4✔
113

114
void CCodeGenerator::dispatch_globals() {
4✔
115
    for (auto& container : sdfg_.externals()) {
5✔
116
        this->globals_stream_ << "extern " << language_extension_.declaration(container, sdfg_.type(container)) << ";"
2✔
117
                              << std::endl;
1✔
118
    }
119
};
4✔
120

121
void CCodeGenerator::dispatch_schedule() {
4✔
122
    // Map external variables to internal variables
123
    for (auto& container : sdfg_.containers()) {
5✔
124
        if (!sdfg_.is_internal(container)) {
1✔
125
            continue;
1✔
126
        }
NEW
127
        std::string external_name = container.substr(0, container.length() - external_suffix.length());
×
128
        this->main_stream_ << language_extension_.declaration(container, sdfg_.type(container));
×
NEW
129
        this->main_stream_ << " = " << language_extension_.type_cast("&" + external_name, sdfg_.type(container));
×
130
        this->main_stream_ << ";" << std::endl;
×
131
    }
×
132

133
    // Declare transient containers
134
    for (auto& container : sdfg_.containers()) {
5✔
135
        if (!sdfg_.is_transient(container)) {
1✔
136
            continue;
1✔
137
        }
138

NEW
139
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
140
        if (!val.empty()) {
×
141
            this->main_stream_ << val;
×
142
            this->main_stream_ << ";" << std::endl;
×
143
        }
×
144
    }
×
145

146
    // Add instrumentation
147
    auto instrumentation = create_instrumentation(instrumentation_strategy_, sdfg_);
4✔
148

149
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), *instrumentation);
4✔
150
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
4✔
151
};
4✔
152

153
} // namespace codegen
154
} // 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