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

daisytuner / sdfglib / 15498944586

06 Jun 2025 08:12PM UTC coverage: 57.303% (-0.001%) from 57.304%
15498944586

Pull #61

github

web-flow
Merge 6adc89d23 into 0d30a6866
Pull Request #61: Copy

57 of 111 new or added lines in 16 files covered. (51.35%)

2 existing lines in 1 file now uncovered.

7584 of 13235 relevant lines covered (57.3%)

116.03 hits per line

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

62.1
/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_); };
77✔
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)
50✔
16
    : FunctionBuilder(), sdfg_(new SDFG(name, type)) {
50✔
17

18
      };
50✔
19

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

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

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

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

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

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

39
    return *(*res.first).second;
62✔
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, symbolic::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, symbolic::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 symbolic::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 symbolic::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(
51✔
119
        {edge.first, std::unique_ptr<control_flow::InterstateEdge>(new control_flow::InterstateEdge(
102✔
120
                         debug_info, edge.first, src, dst, condition, assignments))});
51✔
121

122
    assert(res.second);
51✔
123

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

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

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

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

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

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

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

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

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

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

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

169
/***** Section: Dataflow Graph *****/
170

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

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

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

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

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

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

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

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

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

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

248
            if (symbolic::is_pointer(symbolic::symbol(src_node.data()))) {
×
249
                throw InvalidSDFGException("src_conn is void: src cannot be a raw pointer");
×
250
            }
251

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

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

298
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
3✔
299
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
3✔
300
            throw InvalidSDFGException("Tasklet output must be a scalar");
×
301
        }
302
    } else if (dynamic_cast<data_flow::AccessNode*>(&src) &&
3✔
303
               dynamic_cast<data_flow::LibraryNode*>(&dst)) {
×
304
        auto& src_node = dynamic_cast<data_flow::AccessNode&>(src);
×
305
        auto& dst_node = dynamic_cast<data_flow::LibraryNode&>(dst);
×
306
        if (src_conn != "void") {
×
307
            throw InvalidSDFGException("src_conn must be void. Found: " + src_conn);
×
308
        }
309
        bool found = false;
×
310
        for (auto& input : dst_node.inputs()) {
×
NEW
311
            if (input == dst_conn) {
×
312
                found = true;
×
313
                break;
×
314
            }
315
        }
316
        if (!found) {
×
317
            throw InvalidSDFGException("dst_conn not found in library node: " + dst_conn);
×
318
        }
319

320
        auto& element_type = types::infer_type(function_, function_.type(src_node.data()), subset);
×
321
        if (!dynamic_cast<const types::Scalar*>(&element_type)) {
×
322
            throw InvalidSDFGException("Library node inputs must be scalars");
×
323
        }
324
    } else if (dynamic_cast<data_flow::LibraryNode*>(&src) &&
×
325
               dynamic_cast<data_flow::AccessNode*>(&dst)) {
×
326
        auto& src_node = dynamic_cast<data_flow::LibraryNode&>(src);
×
327
        auto& dst_node = dynamic_cast<data_flow::AccessNode&>(dst);
×
328
        if (dst_conn != "void") {
×
329
            throw InvalidSDFGException("dst_conn must be void. Found: " + dst_conn);
×
330
        }
331
        bool found = false;
×
332
        for (auto& output : src_node.outputs()) {
×
NEW
333
            if (output == src_conn) {
×
334
                found = true;
×
335
                break;
×
336
            }
337
        }
338
        if (!found) {
×
339
            throw InvalidSDFGException("src_conn not found in library node: " + src_conn);
×
340
        }
341

342
        auto& element_type = types::infer_type(function_, function_.type(dst_node.data()), subset);
×
343
        if (!dynamic_cast<const types::Pointer*>(&element_type)) {
×
344
            throw InvalidSDFGException("Access node must be a pointer");
×
345
        }
346
    } else {
×
347
        throw InvalidSDFGException("Invalid src or dst node type");
×
348
    }
349

350
    auto& dataflow = state.dataflow();
6✔
351
    auto edge = boost::add_edge(src.vertex_, dst.vertex_, dataflow.graph_);
6✔
352
    auto res = dataflow.edges_.insert(
6✔
353
        {edge.first, std::unique_ptr<data_flow::Memlet>(new data_flow::Memlet(
12✔
354
                         debug_info, edge.first, dataflow, src, src_conn, dst, dst_conn, subset))});
6✔
355

356
    return dynamic_cast<data_flow::Memlet&>(*(res.first->second));
6✔
357
};
×
358

359
data_flow::LibraryNode& SDFGBuilder::add_library_node(control_flow::State& state,
2✔
360
                                                      const data_flow::LibraryNodeCode& code,
361
                                                      const std::vector<std::string>& outputs,
362
                                                      const std::vector<std::string>& inputs,
363
                                                      const bool side_effect,
364
                                                      const DebugInfo& debug_info) {
365
    auto& dataflow = state.dataflow();
2✔
366

367
    auto vertex = boost::add_vertex(dataflow.graph_);
2✔
368
    auto res = dataflow.nodes_.insert(
2✔
369
        {vertex, std::unique_ptr<data_flow::LibraryNode>(new data_flow::LibraryNode(
4✔
370
                     debug_info, vertex, dataflow, code, outputs, inputs, side_effect))});
2✔
371

372
    return dynamic_cast<data_flow::LibraryNode&>(*(res.first->second));
2✔
373
}
×
374

375
}  // namespace builder
376
}  // 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