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

daisytuner / sdfglib / 15911673517

26 Jun 2025 08:20PM UTC coverage: 65.102% (+0.003%) from 65.099%
15911673517

push

github

web-flow
Merge pull request #112 from daisytuner/raw-memory-address

removes reinterpret_cast workaround for memory addresses

6 of 13 new or added lines in 4 files covered. (46.15%)

3 existing lines in 3 files now uncovered.

8542 of 13121 relevant lines covered (65.1%)

144.95 hits per line

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

63.59
/src/builder/sdfg_builder.cpp
1
#include "sdfg/builder/sdfg_builder.h"
2

3
#include "sdfg/types/utils.h"
4

5
namespace sdfg {
6
namespace builder {
7

8
Function& SDFGBuilder::function() const { return static_cast<Function&>(*this->sdfg_); };
205✔
9

10
SDFGBuilder::SDFGBuilder(std::unique_ptr<SDFG>& sdfg)
×
11
    : FunctionBuilder(), sdfg_(std::move(sdfg)) {
×
12

13
      };
×
14

15
SDFGBuilder::SDFGBuilder(const std::string& name, FunctionType type)
49✔
16
    : FunctionBuilder(), sdfg_(new SDFG(name, type)) {
49✔
17

18
      };
49✔
19

20
SDFG& SDFGBuilder::subject() const { return *this->sdfg_; };
20✔
21

22
std::unique_ptr<SDFG> SDFGBuilder::move() { return std::move(this->sdfg_); };
31✔
23

24
/***** Section: Control-Flow Graph *****/
25

26
control_flow::State& SDFGBuilder::add_state(bool is_start_state, const DebugInfo& debug_info) {
61✔
27
    auto vertex = boost::add_vertex(this->sdfg_->graph_);
61✔
28
    auto res = this->sdfg_->states_.insert(
122✔
29
        {vertex, std::unique_ptr<control_flow::State>(
61✔
30
                     new control_flow::State(this->new_element_id(), debug_info, vertex))});
61✔
31

32
    assert(res.second);
61✔
33
    (*res.first).second->dataflow_->parent_ = (*res.first).second.get();
61✔
34

35
    if (is_start_state) {
61✔
36
        this->sdfg_->start_state_ = (*res.first).second.get();
12✔
37
    }
12✔
38

39
    return *(*res.first).second;
61✔
40
};
×
41

42
control_flow::State& SDFGBuilder::add_state_before(const control_flow::State& state,
1✔
43
                                                   bool is_start_state,
44
                                                   const DebugInfo& debug_info) {
45
    auto& new_state = this->add_state(false, debug_info);
1✔
46

47
    std::vector<const control_flow::InterstateEdge*> to_redirect;
1✔
48
    for (auto& e : this->sdfg_->in_edges(state)) to_redirect.push_back(&e);
2✔
49

50
    // Redirect control-flow
51
    for (auto edge : to_redirect) {
2✔
52
        this->add_edge(edge->src(), new_state, edge->condition());
1✔
53

54
        auto desc = edge->edge();
1✔
55
        this->sdfg_->edges_.erase(desc);
1✔
56
        boost::remove_edge(desc, this->sdfg_->graph_);
1✔
57
    }
58
    this->add_edge(new_state, state);
1✔
59

60
    if (is_start_state) {
1✔
61
        this->sdfg_->start_state_ = &new_state;
×
62
    }
×
63

64
    return new_state;
1✔
65
};
1✔
66

67
control_flow::State& SDFGBuilder::add_state_after(const control_flow::State& state,
5✔
68
                                                  bool connect_states,
69
                                                  const DebugInfo& debug_info) {
70
    auto& new_state = this->add_state(false, debug_info);
5✔
71

72
    std::vector<const control_flow::InterstateEdge*> to_redirect;
5✔
73
    for (auto& e : this->sdfg_->out_edges(state)) to_redirect.push_back(&e);
6✔
74

75
    // Redirect control-flow
76
    for (auto& edge : to_redirect) {
6✔
77
        this->add_edge(new_state, edge->dst(), edge->condition());
1✔
78

79
        auto desc = edge->edge();
1✔
80
        this->sdfg_->edges_.erase(desc);
1✔
81
        boost::remove_edge(desc, this->sdfg_->graph_);
1✔
82
    }
83
    if (connect_states) {
5✔
84
        this->add_edge(state, new_state);
4✔
85
    }
4✔
86

87
    return new_state;
5✔
88
};
5✔
89

90
control_flow::InterstateEdge& SDFGBuilder::add_edge(const control_flow::State& src,
39✔
91
                                                    const control_flow::State& dst,
92
                                                    const DebugInfo& debug_info) {
93
    return this->add_edge(src, dst, control_flow::Assignments{}, SymEngine::boolTrue, debug_info);
39✔
94
};
×
95

96
control_flow::InterstateEdge& SDFGBuilder::add_edge(const control_flow::State& src,
8✔
97
                                                    const control_flow::State& dst,
98
                                                    const symbolic::Condition condition,
99
                                                    const DebugInfo& debug_info) {
100
    return this->add_edge(src, dst, control_flow::Assignments{}, condition, debug_info);
8✔
101
};
×
102

103
control_flow::InterstateEdge& SDFGBuilder::add_edge(const control_flow::State& src,
2✔
104
                                                    const control_flow::State& dst,
105
                                                    const control_flow::Assignments& assignments,
106
                                                    const DebugInfo& debug_info) {
107
    return this->add_edge(src, dst, assignments, SymEngine::boolTrue, debug_info);
2✔
108
};
×
109

110
control_flow::InterstateEdge& SDFGBuilder::add_edge(const control_flow::State& src,
51✔
111
                                                    const control_flow::State& dst,
112
                                                    const control_flow::Assignments& assignments,
113
                                                    const symbolic::Condition condition,
114
                                                    const DebugInfo& debug_info) {
115
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, this->sdfg_->graph_);
51✔
116
    assert(edge.second);
51✔
117

118
    auto res = this->sdfg_->edges_.insert(
102✔
119
        {edge.first,
102✔
120
         std::unique_ptr<control_flow::InterstateEdge>(new control_flow::InterstateEdge(
51✔
121
             this->new_element_id(), debug_info, edge.first, src, dst, condition, assignments))});
51✔
122

123
    assert(res.second);
51✔
124

125
    return *(*res.first).second;
51✔
126
};
×
127

128
void SDFGBuilder::remove_edge(const control_flow::InterstateEdge& edge) {
×
129
    auto desc = edge.edge();
×
130
    this->sdfg_->edges_.erase(desc);
×
131

132
    boost::remove_edge(desc, this->sdfg_->graph_);
×
133
};
×
134

135
std::tuple<control_flow::State&, control_flow::State&, control_flow::State&> SDFGBuilder::add_loop(
1✔
136
    const control_flow::State& state, sdfg::symbolic::Symbol iterator,
137
    sdfg::symbolic::Expression init, sdfg::symbolic::Condition cond,
138
    sdfg::symbolic::Expression update, const DebugInfo& debug_info) {
139
    // Init: iterator = init
140
    auto& init_state = this->add_state_after(state, true, debug_info);
1✔
141
    const graph::Edge init_edge_desc = (*this->sdfg_->in_edges(init_state).begin()).edge_;
1✔
142
    auto& init_edge = this->sdfg_->edges_[init_edge_desc];
1✔
143
    init_edge->assignments_.insert({iterator, init});
1✔
144

145
    // Final state
146
    auto& final_state = this->add_state_after(init_state, false, debug_info);
1✔
147

148
    // Init -> early_exit -> final
149
    auto& early_exit_state = this->add_state(false, debug_info);
1✔
150
    this->add_edge(init_state, early_exit_state, symbolic::Not(cond));
1✔
151
    this->add_edge(early_exit_state, final_state);
1✔
152

153
    // Init -> header -> body
154
    auto& header_state = this->add_state(false, debug_info);
1✔
155
    this->add_edge(init_state, header_state, cond);
1✔
156

157
    auto& body_state = this->add_state(false, debug_info);
1✔
158
    this->add_edge(header_state, body_state);
1✔
159

160
    auto& update_state = this->add_state(false, debug_info);
1✔
161
    this->add_edge(body_state, update_state, {{iterator, update}});
1✔
162

163
    // Back edge and exit edge
164
    this->add_edge(update_state, header_state, cond);
1✔
165
    this->add_edge(update_state, final_state, symbolic::Not(cond));
1✔
166

167
    return {init_state, body_state, final_state};
1✔
168
};
×
169

170
/***** Section: Dataflow Graph *****/
171

172
data_flow::AccessNode& SDFGBuilder::add_access(control_flow::State& state, const std::string& data,
6✔
173
                                               const DebugInfo& debug_info) {
174
    // Check: Data exists
175
    if (!this->subject().exists(data)) {
6✔
176
        throw InvalidSDFGException("Data does not exist in SDFG: " + data);
×
177
    }
178

179
    auto& dataflow = state.dataflow();
6✔
180
    auto vertex = boost::add_vertex(dataflow.graph_);
6✔
181
    auto res = dataflow.nodes_.insert(
12✔
182
        {vertex, std::unique_ptr<data_flow::AccessNode>(new data_flow::AccessNode(
6✔
183
                     this->new_element_id(), debug_info, vertex, dataflow, data))});
6✔
184

185
    return dynamic_cast<data_flow::AccessNode&>(*(res.first->second));
6✔
186
};
×
187

188
data_flow::Tasklet& SDFGBuilder::add_tasklet(
3✔
189
    control_flow::State& state, const data_flow::TaskletCode code,
190
    const std::pair<std::string, sdfg::types::Scalar>& output,
191
    const std::vector<std::pair<std::string, sdfg::types::Scalar>>& inputs,
192
    const DebugInfo& debug_info) {
193
    // Check: Duplicate inputs
194
    std::unordered_set<std::string> input_names;
3✔
195
    for (auto& input : inputs) {
8✔
196
        if (!input.first.starts_with("_in")) {
5✔
197
            continue;
2✔
198
        }
199
        if (input_names.find(input.first) != input_names.end()) {
3✔
200
            throw InvalidSDFGException("Input " + input.first + " already exists in SDFG");
×
201
        }
202
        input_names.insert(input.first);
3✔
203
    }
204

205
    auto& dataflow = state.dataflow();
3✔
206
    auto vertex = boost::add_vertex(dataflow.graph_);
3✔
207
    auto res =
208
        dataflow.nodes_.insert({vertex, std::unique_ptr<data_flow::Tasklet>(new data_flow::Tasklet(
6✔
209
                                            this->new_element_id(), debug_info, vertex, dataflow,
3✔
210
                                            code, output, inputs, symbolic::__true__()))});
3✔
211

212
    return dynamic_cast<data_flow::Tasklet&>(*(res.first->second));
3✔
213
};
3✔
214

215
data_flow::Memlet& SDFGBuilder::add_memlet(control_flow::State& state, data_flow::DataFlowNode& src,
6✔
216
                                           const std::string& src_conn,
217
                                           data_flow::DataFlowNode& dst,
218
                                           const std::string& dst_conn,
219
                                           const data_flow::Subset& subset,
220
                                           const DebugInfo& debug_info) {
221
    auto& function_ = this->function();
6✔
222

223
    // Check - Case 1: Access Node -> Access Node
224
    // - src_conn or dst_conn must be refs. The other must be void.
225
    // - The side of the memlet that is void, is dereferenced.
226
    // - The dst type must always be a pointer after potential dereferencing.
227
    // - The src type can be any type after dereferecing (&dereferenced_src_type).
228
    if (dynamic_cast<data_flow::AccessNode*>(&src) && dynamic_cast<data_flow::AccessNode*>(&dst)) {
6✔
229
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
×
230
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
×
231
        if (src_conn == "refs") {
×
232
            if (dst_conn != "void") {
×
233
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
234
            }
235

236
            auto& dst_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
237
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
×
238
                throw InvalidSDFGException("dst type must be a pointer");
×
239
            }
240

241
            auto& src_type = function_.type(src_node.data());
×
242
            if (!dynamic_cast<const types::Pointer*>(&src_type)) {
×
243
                throw InvalidSDFGException("src type must be a pointer");
×
244
            }
245
        } else if (src_conn == "void") {
×
246
            if (dst_conn != "refs") {
×
247
                throw InvalidSDFGException("Invalid dst connector: " + dst_conn);
×
248
            }
249

NEW
250
            if (symbolic::is_pointer(symbolic::symbol(src_node.data())) ||
×
NEW
251
                helpers::is_number(src_node.data())) {
×
UNCOV
252
                throw InvalidSDFGException("src_conn is void: src cannot be a raw pointer");
×
253
            }
254

255
            // Trivially correct but checks inference
256
            auto& src_type = types::infer_type(function_, function_.type(src_node.data()), subset);
×
257
            types::Pointer ref_type(src_type);
×
258
            if (!dynamic_cast<const types::Pointer*>(&ref_type)) {
259
                throw InvalidSDFGException("src type must be a pointer");
260
            }
261

262
            auto& dst_type = function_.type(dst_node.data());
×
263
            if (!dynamic_cast<const types::Pointer*>(&dst_type)) {
×
264
                throw InvalidSDFGException("dst type must be a pointer");
×
265
            }
266
        } else {
×
267
            throw InvalidSDFGException("Invalid src connector: " + src_conn);
×
268
        }
269
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
9✔
270
               dynamic_cast<data_flow::Tasklet*>(&dst)) {
3✔
271
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
3✔
272
        auto& dst_node = dynamic_cast<data_flow::Tasklet&>(dst);
3✔
273
        if (src_conn != "void") {
3✔
274
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
275
        }
276
        bool found = false;
3✔
277
        for (auto& input : dst_node.inputs()) {
3✔
278
            if (input.first == dst_conn) {
3✔
279
                found = true;
3✔
280
                break;
3✔
281
            }
282
        }
283
        if (!found) {
3✔
284
            throw InvalidSDFGException("dst_conn not found in tasklet: " + dst_conn);
×
285
        }
286
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
3✔
287
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
3✔
288
            throw InvalidSDFGException("Tasklets inputs must be scalars");
×
289
        }
290
    } else if (dynamic_cast<data_flow::Tasklet*>(&src) &&
9✔
291
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
3✔
292
        auto& src_node = dynamic_cast<data_flow::Tasklet&>(src);
3✔
293
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
3✔
294
        if (src_conn != src_node.output().first) {
3✔
295
            throw InvalidSDFGException("src_conn must match tasklet output name");
×
296
        }
297
        if (dst_conn != "void") {
3✔
298
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
299
        }
300

301
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
3✔
302
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
3✔
303
            throw InvalidSDFGException("Tasklet output must be a scalar");
×
304
        }
305
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
3✔
306
               dynamic_cast<data_flow::LibraryNode*>(&dst)) {
×
307
        auto& dst_node = dynamic_cast<data_flow::LibraryNode&>(dst);
×
308
        if (src_conn != "void") {
×
309
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
310
        }
311
        bool found = false;
×
312
        for (auto& input : dst_node.inputs()) {
×
313
            if (input == dst_conn) {
×
314
                found = true;
×
315
                break;
×
316
            }
317
        }
318
        if (!found) {
×
319
            throw InvalidSDFGException("dst_conn not found in library node: " + dst_conn);
×
320
        }
321
    } else if (dynamic_cast<data_flow::LibraryNode*>(&src) &&
×
322
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
×
323
        auto& src_node = dynamic_cast<data_flow::LibraryNode&>(src);
×
324
        if (dst_conn != "void") {
×
325
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
326
        }
327
        bool found = false;
×
328
        for (auto& output : src_node.outputs()) {
×
329
            if (output == src_conn) {
×
330
                found = true;
×
331
                break;
×
332
            }
333
        }
334
        if (!found) {
×
335
            throw InvalidSDFGException("src_conn not found in library node: " + src_conn);
×
336
        }
337
    } else {
×
338
        throw InvalidSDFGException("Invalid src or dst node type");
×
339
    }
340

341
    auto& dataflow = state.dataflow();
6✔
342
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, dataflow.graph_);
6✔
343
    auto res = dataflow.edges_.insert(
12✔
344
        {edge.first, std::unique_ptr<data_flow::Memlet>(
12✔
345
                         new data_flow::Memlet(this->new_element_id(), debug_info, edge.first,
6✔
346
                                               dataflow, src, src_conn, dst, dst_conn, subset))});
6✔
347

348
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
6✔
349
};
×
350

351
}  // namespace builder
352
}  // 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