• 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

42.2
/src/codegen/code_generators/c_style_base_code_generator.cpp
1

2
#include "../../../include/sdfg/codegen/code_generators/c_style_base_code_generator.h"
3

4
namespace sdfg::codegen {
5

6
CStyleBaseCodeGenerator::CStyleBaseCodeGenerator(
15✔
7
    StructuredSDFG& sdfg,
8
    InstrumentationStrategy instrumentation_strategy,
9
    bool capture_args_results,
10
    const std::pair<std::filesystem::path, std::filesystem::path>* output_and_header_paths
11
)
12
    : CodeGenerator(sdfg, instrumentation_strategy, capture_args_results, output_and_header_paths) {
15✔
13
    if (sdfg.type() != FunctionType_CPU) {
15✔
NEW
14
        throw std::runtime_error("CStyleBaseCodeGenerator can only be used for CPU SDFGs");
×
15
    }
16
};
15✔
17

18
bool CStyleBaseCodeGenerator::generate() {
9✔
19
    this->dispatch_includes();
9✔
20
    this->dispatch_structures();
9✔
21
    this->dispatch_globals();
9✔
22
    this->dispatch_schedule();
9✔
23
    return true;
9✔
24
};
25

NEW
26
bool CStyleBaseCodeGenerator::as_source(const std::filesystem::path& header_path, const std::filesystem::path& source_path) {
×
NEW
27
    std::ofstream ofs_header(header_path, std::ofstream::out);
×
NEW
28
    if (!ofs_header.is_open()) {
×
NEW
29
        return false;
×
30
    }
31

NEW
32
    std::ofstream ofs_source(source_path, std::ofstream::out);
×
NEW
33
    if (!ofs_source.is_open()) {
×
NEW
34
        return false;
×
35
    }
36

NEW
37
    ofs_header << "#pragma once" << std::endl;
×
NEW
38
    ofs_header << this->includes_stream_.str() << std::endl;
×
NEW
39
    ofs_header << this->classes_stream_.str() << std::endl;
×
NEW
40
    ofs_header.close();
×
41

NEW
42
    ofs_source << "#include \"" << header_path.filename().string() << "\"" << std::endl;
×
NEW
43
    ofs_source << this->globals_stream_.str() << std::endl;
×
44

NEW
45
    append_function_source(ofs_source);
×
46

NEW
47
    ofs_source.close();
×
48

NEW
49
    return true;
×
NEW
50
}
×
51

NEW
52
void CStyleBaseCodeGenerator::append_function_source(std::ofstream& ofs_source) {
×
NEW
53
    std::unique_ptr<std::vector<CaptureVarPlan>> capturePlan;
×
NEW
54
    if (capture_args_results_) {
×
NEW
55
        capturePlan = create_capture_plans();
×
NEW
56
        if (capturePlan) {
×
NEW
57
            this->emit_capture_context_init(ofs_source);
×
NEW
58
        } else {
×
NEW
59
            std::cerr << "Cannot capture all args for SDFG '" << sdfg_.name() << "'. Skpping capture instrumentation!"
×
NEW
60
                      << std::endl;
×
61
        }
NEW
62
    }
×
63

NEW
64
    ofs_source << this->function_definition() << std::endl;
×
NEW
65
    ofs_source << "{" << std::endl;
×
66

NEW
67
    if (instrumentation_strategy_ != InstrumentationStrategy::NONE) {
×
NEW
68
        ofs_source << "__daisy_instrument_init();" << std::endl;
×
NEW
69
    }
×
70

NEW
71
    if (capturePlan) {
×
NEW
72
        this->emit_arg_captures(ofs_source, *capturePlan, false);
×
NEW
73
    }
×
74

NEW
75
    auto init_once = library_snippet_factory_.find(CODE_SNIPPET_INIT_ONCE);
×
NEW
76
    if (init_once != library_snippet_factory_.snippets().end()) {
×
NEW
77
        ofs_source << init_once->second.stream().str() << std::endl;
×
NEW
78
    }
×
79

NEW
80
    ofs_source << this->main_stream_.str() << std::endl;
×
81

NEW
82
    auto deinit_once = library_snippet_factory_.find(CODE_SNIPPET_DEINIT_ONCE);
×
NEW
83
    if (deinit_once != library_snippet_factory_.snippets().end()) {
×
NEW
84
        ofs_source << deinit_once->second.stream().str() << std::endl;
×
NEW
85
    }
×
86

NEW
87
    if (capturePlan) {
×
NEW
88
        this->emit_arg_captures(ofs_source, *capturePlan, true);
×
NEW
89
    }
×
90

NEW
91
    if (instrumentation_strategy_ != InstrumentationStrategy::NONE) {
×
NEW
92
        ofs_source << "__daisy_instrument_finalize();" << std::endl;
×
NEW
93
    }
×
94

NEW
95
    ofs_source << "}" << std::endl;
×
NEW
96
}
×
97

98
void CStyleBaseCodeGenerator::
99
    emit_arg_captures(std::ostream& ofs_source, const std::vector<CaptureVarPlan>& plan, bool after) {
2✔
100
    std::string name = sdfg_.name();
2✔
101

102
    if (!after) {
2✔
103
        ofs_source << "const bool __daisy_cap_en = __daisy_capture_enter(__capture_ctx);" << std::endl;
1✔
104
    }
1✔
105

106
    const auto& args = sdfg_.arguments();
2✔
107
    const auto& exts = sdfg_.externals();
2✔
108

109
    ofs_source << "if (__daisy_cap_en) {" << std::endl;
2✔
110

111
    auto afterBoolStr = after ? "true" : "false";
2✔
112

113
    for (auto& varPlan : plan) {
9✔
114
        auto argIdx = varPlan.arg_idx;
7✔
115
        auto argName = varPlan.is_external ? exts[argIdx - args.size()] : args[argIdx];
7✔
116

117
        if ((!after && varPlan.capture_input) || (after && varPlan.capture_output)) {
7✔
118
            switch (varPlan.type) {
5✔
119
                case CaptureVarType::CapRaw: {
120
                    ofs_source << "\t__daisy_capture_raw(" << "__capture_ctx, " << argIdx << ", " << "&" << argName
2✔
121
                               << ", " << "sizeof(" << argName << "), " << varPlan.inner_type << ", " << afterBoolStr
2✔
122
                               << ");" << std::endl;
2✔
123
                    break;
2✔
124
                }
125
                case CaptureVarType::Cap1D: {
126
                    ofs_source << "\t__daisy_capture_1d(" << "__capture_ctx, " << argIdx << ", " << argName << ", "
2✔
127
                               << "sizeof(" << language_extension().primitive_type(varPlan.inner_type) << "), "
1✔
128
                               << varPlan.inner_type << ", " << language_extension().expression(varPlan.dim1) << ", "
1✔
129
                               << afterBoolStr << ");" << std::endl;
1✔
130
                    break;
1✔
131
                }
132
                case CaptureVarType::Cap2D: {
133
                    ofs_source << "\t__daisy_capture_2d(" << "__capture_ctx, " << argIdx << ", " << argName << ", "
4✔
134
                               << "sizeof(" << language_extension().primitive_type(varPlan.inner_type) << "), "
2✔
135
                               << varPlan.inner_type << ", " << language_extension().expression(varPlan.dim1) << ", "
2✔
136
                               << language_extension().expression(varPlan.dim2) << ", " << afterBoolStr << ");"
2✔
137
                               << std::endl;
2✔
138
                    break;
2✔
139
                }
140
                case CaptureVarType::Cap3D: {
NEW
141
                    ofs_source << "\t__daisy_capture_3d(" << "__capture_ctx, " << argIdx << ", " << argName << ", "
×
NEW
142
                               << "sizeof(" << language_extension().primitive_type(varPlan.inner_type) << "), "
×
NEW
143
                               << varPlan.inner_type << ", " << language_extension().expression(varPlan.dim1) << ", "
×
NEW
144
                               << language_extension().expression(varPlan.dim2) << ", "
×
NEW
145
                               << language_extension().expression(varPlan.dim3) << ", " << afterBoolStr << ");"
×
NEW
146
                               << std::endl;
×
NEW
147
                    break;
×
148
                }
149
                default:
NEW
150
                    std::cerr << "Unknown capture type " << static_cast<int>(varPlan.type) << " for arg " << argIdx
×
NEW
151
                              << " at " << (after ? "result" : "input") << " time" << std::endl;
×
NEW
152
                    break;
×
153
            }
154
        }
5✔
155
    }
7✔
156

157
    if (after) {
2✔
158
        ofs_source << "\t__daisy_capture_end(__capture_ctx);" << std::endl;
1✔
159
    }
1✔
160

161
    ofs_source << "}" << std::endl;
2✔
162
}
2✔
163

164
} // namespace sdfg::codegen
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