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

daisytuner / sdfglib / 15373564831

01 Jun 2025 09:41AM UTC coverage: 58.528% (-0.2%) from 58.751%
15373564831

push

github

web-flow
Merge pull request #49 from daisytuner/missing-containers

Debugging of missing containers bug

62 of 81 new or added lines in 2 files covered. (76.54%)

13 existing lines in 1 file now uncovered.

8205 of 14019 relevant lines covered (58.53%)

109.64 hits per line

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

83.15
/src/serializer/json_serializer.cpp
1
#include "sdfg/serializer/json_serializer.h"
2

3
#include <cassert>
4
#include <memory>
5
#include <unordered_map>
6
#include <utility>
7
#include <vector>
8

9
#include "sdfg/builder/structured_sdfg_builder.h"
10
#include "sdfg/data_flow/library_node.h"
11
#include "sdfg/element.h"
12
#include "sdfg/structured_control_flow/block.h"
13
#include "sdfg/structured_control_flow/for.h"
14
#include "sdfg/structured_control_flow/if_else.h"
15
#include "sdfg/structured_control_flow/kernel.h"
16
#include "sdfg/structured_control_flow/return.h"
17
#include "sdfg/structured_control_flow/sequence.h"
18
#include "sdfg/structured_control_flow/while.h"
19
#include "sdfg/structured_sdfg.h"
20
#include "sdfg/symbolic/symbolic.h"
21
#include "sdfg/types/function.h"
22
#include "sdfg/types/scalar.h"
23
#include "sdfg/types/type.h"
24
#include "symengine/expression.h"
25
#include "symengine/logic.h"
26
#include "symengine/symengine_rcp.h"
27

28
namespace sdfg {
29
namespace serializer {
30

31
/*
32
 * * JSONSerializer class
33
 * * Serialization logic
34
 */
35

36
nlohmann::json JSONSerializer::serialize(std::unique_ptr<sdfg::StructuredSDFG>& sdfg) {
3✔
37
    nlohmann::json j;
3✔
38

39
    j["name"] = sdfg->name();
3✔
40

41
    j["structures"] = nlohmann::json::array();
3✔
42
    for (const auto& structure_name : sdfg->structures()) {
3✔
43
        const auto& structure = sdfg->structure(structure_name);
×
44
        nlohmann::json structure_json;
×
45
        structure_definition_to_json(structure_json, structure);
×
46
        j["structures"].push_back(structure_json);
×
47
    }
×
48

49
    j["containers"] = nlohmann::json::object();
3✔
50
    for (const auto& container : sdfg->containers()) {
8✔
51
        nlohmann::json desc;
5✔
52
        type_to_json(desc, sdfg->type(container));
5✔
53
        j["containers"][container] = desc;
5✔
54
    }
5✔
55

56
    j["arguments"] = nlohmann::json::array();
3✔
57
    for (const auto& argument : sdfg->arguments()) {
5✔
58
        j["arguments"].push_back(argument);
2✔
59
    }
60

61
    j["externals"] = nlohmann::json::array();
3✔
62
    for (const auto& external : sdfg->externals()) {
3✔
NEW
63
        j["externals"].push_back(external);
×
64
    }
65

66
    j["metadata"] = nlohmann::json::object();
3✔
67
    for (const auto& entry : sdfg->metadata()) {
4✔
68
        j["metadata"][entry.first] = entry.second;
1✔
69
    }
70

71
    // Walk the SDFG
72
    nlohmann::json root_json;
3✔
73
    sequence_to_json(root_json, sdfg->root());
3✔
74
    j["root"] = root_json;
3✔
75

76
    return j;
3✔
77
}
3✔
78

79
void JSONSerializer::dataflow_to_json(nlohmann::json& j, const data_flow::DataFlowGraph& dataflow) {
22✔
80
    j["type"] = "dataflow";
22✔
81
    j["nodes"] = nlohmann::json::array();
22✔
82
    j["edges"] = nlohmann::json::array();
22✔
83

84
    size_t node_id = 0;
22✔
85
    std::unordered_map<const data_flow::DataFlowNode*, size_t> node_id_map;
22✔
86
    for (auto& node : dataflow.nodes()) {
42✔
87
        nlohmann::json node_json;
20✔
88
        if (auto code_node = dynamic_cast<const data_flow::Tasklet*>(&node)) {
20✔
89
            node_json["type"] = "tasklet";
5✔
90
            node_json["code"] = code_node->code();
5✔
91
            node_json["inputs"] = nlohmann::json::array();
5✔
92
            for (auto& input : code_node->inputs()) {
15✔
93
                nlohmann::json input_json;
10✔
94
                nlohmann::json type_json;
10✔
95
                type_to_json(type_json, input.second);
10✔
96
                input_json["type"] = type_json;
10✔
97
                input_json["name"] = input.first;
10✔
98
                node_json["inputs"].push_back(input_json);
10✔
99
            }
10✔
100
            node_json["outputs"] = nlohmann::json::array();
5✔
101
            for (auto& output : code_node->outputs()) {
10✔
102
                nlohmann::json output_json;
5✔
103
                nlohmann::json type_json;
5✔
104
                type_to_json(type_json, output.second);
5✔
105
                output_json["type"] = type_json;
5✔
106
                output_json["name"] = output.first;
5✔
107
                node_json["outputs"].push_back(output_json);
5✔
108
            }
5✔
109
            // node_json["conditional"] = code_node->is_conditional();
110
            // if (code_node->is_conditional()) {
111
            //     node_json["condition"] = dumps_expression(code_node->condition());
112
            // }
113
        } else if (auto lib_node = dynamic_cast<const data_flow::LibraryNode*>(&node)) {
20✔
114
            node_json["type"] = "library_node";
×
115
            node_json["call"] = lib_node->call();
×
NEW
116
            node_json["has_side_effect"] = lib_node->has_side_effect();
×
117
            node_json["inputs"] = nlohmann::json::array();
×
118
            for (auto& input : lib_node->inputs()) {
×
119
                nlohmann::json input_json;
×
120
                nlohmann::json type_json;
×
121
                type_to_json(type_json, input.second);
×
122
                input_json["type"] = type_json;
×
123
                input_json["name"] = input.first;
×
124
                node_json["inputs"].push_back(input_json);
×
125
            }
×
126
            node_json["outputs"] = nlohmann::json::array();
×
127
            for (auto& output : lib_node->outputs()) {
×
128
                nlohmann::json output_json;
×
129
                nlohmann::json type_json;
×
130
                type_to_json(type_json, output.second);
×
131
                output_json["type"] = type_json;
×
132
                output_json["name"] = output.first;
×
133
                node_json["outputs"].push_back(output_json);
×
134
            }
×
135
        } else if (auto code_node = dynamic_cast<const data_flow::AccessNode*>(&node)) {
15✔
136
            node_json["type"] = "access_node";
15✔
137
            node_json["data"] = code_node->data();
15✔
138
        } else {
15✔
139
            throw std::runtime_error("Unknown node type");
×
140
        }
141

142
        // Internal id for serialization
143
        node_json["serialization_id"] = node_id;
20✔
144
        node_id_map.insert({&node, node_id});
20✔
145
        node_id++;
20✔
146

147
        j["nodes"].push_back(node_json);
20✔
148
    }
20✔
149

150
    for (auto& edge : dataflow.edges()) {
37✔
151
        nlohmann::json edge_json;
15✔
152

153
        size_t src_id = node_id_map.at(&edge.src());
15✔
154
        size_t dst_id = node_id_map.at(&edge.dst());
15✔
155
        edge_json["src"] = src_id;
15✔
156
        edge_json["dst"] = dst_id;
15✔
157

158
        edge_json["src_conn"] = edge.src_conn();
15✔
159
        edge_json["dst_conn"] = edge.dst_conn();
15✔
160

161
        edge_json["subset"] = nlohmann::json::array();
15✔
162
        for (auto& subset : edge.subset()) {
21✔
163
            edge_json["subset"].push_back(expression(subset));
6✔
164
        }
165

166
        j["edges"].push_back(edge_json);
15✔
167
    }
15✔
168
}
22✔
169

170
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
20✔
171
    j["type"] = "block";
20✔
172
    nlohmann::json dataflow_json;
20✔
173
    dataflow_to_json(dataflow_json, block.dataflow());
20✔
174
    j["dataflow"] = dataflow_json;
20✔
175
}
20✔
176

177
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
178
    j["type"] = "for";
2✔
179
    j["indvar"] = expression(for_node.indvar());
2✔
180
    j["init"] = expression(for_node.init());
2✔
181
    j["condition"] = expression(for_node.condition());
2✔
182
    j["update"] = expression(for_node.update());
2✔
183

184
    nlohmann::json body_json;
2✔
185
    sequence_to_json(body_json, for_node.root());
2✔
186
    j["children"] = body_json;
2✔
187
}
2✔
188

189
void JSONSerializer::if_else_to_json(nlohmann::json& j,
2✔
190
                                     const structured_control_flow::IfElse& if_else_node) {
191
    j["type"] = "if_else";
2✔
192
    j["branches"] = nlohmann::json::array();
2✔
193
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
194
        nlohmann::json branch_json;
4✔
195
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
196
        nlohmann::json body_json;
4✔
197
        sequence_to_json(body_json, if_else_node.at(i).first);
4✔
198
        branch_json["children"] = body_json;
4✔
199
        j["branches"].push_back(branch_json);
4✔
200
    }
4✔
201
}
2✔
202

203
void JSONSerializer::while_node_to_json(nlohmann::json& j,
5✔
204
                                        const structured_control_flow::While& while_node) {
205
    j["type"] = "while";
5✔
206

207
    nlohmann::json body_json;
5✔
208
    sequence_to_json(body_json, while_node.root());
5✔
209
    j["children"] = body_json;
5✔
210
}
5✔
211

212
void JSONSerializer::break_node_to_json(nlohmann::json& j,
2✔
213
                                        const structured_control_flow::Break& break_node) {
214
    j["type"] = "break";
2✔
215
}
2✔
216
void JSONSerializer::continue_node_to_json(nlohmann::json& j,
2✔
217
                                           const structured_control_flow::Continue& continue_node) {
218
    j["type"] = "continue";
2✔
219
}
2✔
220

221
void JSONSerializer::kernel_to_json(nlohmann::json& j,
2✔
222
                                    const structured_control_flow::Kernel& kernel_node) {
223
    j["type"] = "kernel";
2✔
224
    j["suffix"] = kernel_node.suffix();
2✔
225

226
    nlohmann::json body_json;
2✔
227
    sequence_to_json(body_json, kernel_node.root());
2✔
228
    j["children"] = body_json;
2✔
229
}
2✔
230

231
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
232
    j["type"] = "map";
2✔
233
    j["indvar"] = expression(map_node.indvar());
2✔
234
    j["num_iterations"] = expression(map_node.num_iterations());
2✔
235
    nlohmann::json body_json;
2✔
236
    sequence_to_json(body_json, map_node.root());
2✔
237
    j["children"] = body_json;
2✔
238
}
2✔
239

240
void JSONSerializer::return_node_to_json(nlohmann::json& j,
2✔
241
                                         const structured_control_flow::Return& return_node) {
242
    j["type"] = "return";
2✔
243
}
2✔
244

245
void JSONSerializer::sequence_to_json(nlohmann::json& j,
21✔
246
                                      const structured_control_flow::Sequence& sequence) {
247
    j["type"] = "sequence";
21✔
248
    j["children"] = nlohmann::json::array();
21✔
249
    j["transitions"] = nlohmann::json::array();
21✔
250

251
    for (size_t i = 0; i < sequence.size(); i++) {
43✔
252
        nlohmann::json child_json;
22✔
253
        auto& child = sequence.at(i).first;
22✔
254
        auto& transition = sequence.at(i).second;
22✔
255

256
        if (auto block = dynamic_cast<const structured_control_flow::Block*>(&child)) {
22✔
257
            block_to_json(child_json, *block);
18✔
258
        } else if (auto for_node = dynamic_cast<const structured_control_flow::For*>(&child)) {
22✔
259
            for_to_json(child_json, *for_node);
×
260
        } else if (auto sequence_node =
4✔
261
                       dynamic_cast<const structured_control_flow::Sequence*>(&child)) {
4✔
262
            sequence_to_json(child_json, *sequence_node);
×
263
        } else if (auto condition_node =
4✔
264
                       dynamic_cast<const structured_control_flow::IfElse*>(&child)) {
4✔
265
            if_else_to_json(child_json, *condition_node);
×
266
        } else if (auto while_node = dynamic_cast<const structured_control_flow::While*>(&child)) {
4✔
267
            while_node_to_json(child_json, *while_node);
×
268
        } else if (auto kernel_node =
4✔
269
                       dynamic_cast<const structured_control_flow::Kernel*>(&child)) {
4✔
270
            kernel_to_json(child_json, *kernel_node);
×
271
        } else if (auto return_node =
4✔
272
                       dynamic_cast<const structured_control_flow::Return*>(&child)) {
4✔
273
            return_node_to_json(child_json, *return_node);
×
274
        } else if (auto break_node = dynamic_cast<const structured_control_flow::Break*>(&child)) {
4✔
275
            break_node_to_json(child_json, *break_node);
2✔
276
        } else if (auto continue_node =
4✔
277
                       dynamic_cast<const structured_control_flow::Continue*>(&child)) {
2✔
278
            continue_node_to_json(child_json, *continue_node);
2✔
279
        } else if (auto map_node = dynamic_cast<const structured_control_flow::Map*>(&child)) {
2✔
280
            map_to_json(child_json, *map_node);
×
281
        } else {
×
282
            throw std::runtime_error("Unknown child type");
×
283
        }
284

285
        j["children"].push_back(child_json);
22✔
286

287
        // Add transition information
288
        nlohmann::json transition_json;
22✔
289
        transition_json["type"] = "transition";
22✔
290
        transition_json["assignments"] = nlohmann::json::array();
22✔
291
        for (const auto& assignment : transition.assignments()) {
24✔
292
            nlohmann::json assignment_json;
2✔
293
            assignment_json["symbol"] = expression(assignment.first);
2✔
294
            assignment_json["expression"] = expression(assignment.second);
2✔
295
            transition_json["assignments"].push_back(assignment_json);
2✔
296
        }
2✔
297

298
        j["transitions"].push_back(transition_json);
22✔
299
    }
22✔
300
}
21✔
301

302
void JSONSerializer::type_to_json(nlohmann::json& j, const types::IType& type) {
43✔
303
    if (auto scalar_type = dynamic_cast<const types::Scalar*>(&type)) {
43✔
304
        j["type"] = "scalar";
33✔
305
        j["primitive_type"] = scalar_type->primitive_type();
33✔
306
        j["address_space"] = scalar_type->address_space();
33✔
307
        j["initializer"] = scalar_type->initializer();
33✔
308
        j["device_location"] = scalar_type->device_location();
33✔
309
    } else if (auto array_type = dynamic_cast<const types::Array*>(&type)) {
43✔
310
        j["type"] = "array";
3✔
311
        nlohmann::json element_type_json;
3✔
312
        type_to_json(element_type_json, array_type->element_type());
3✔
313
        j["element_type"] = element_type_json;
3✔
314
        j["num_elements"] = expression(array_type->num_elements());
3✔
315
        j["address_space"] = array_type->address_space();
3✔
316
        j["initializer"] = array_type->initializer();
3✔
317
        j["device_location"] = array_type->device_location();
3✔
318
        j["alignment"] = array_type->alignment();
3✔
319
    } else if (auto pointer_type = dynamic_cast<const types::Pointer*>(&type)) {
10✔
320
        j["type"] = "pointer";
3✔
321
        nlohmann::json pointee_type_json;
3✔
322
        type_to_json(pointee_type_json, pointer_type->pointee_type());
3✔
323
        j["pointee_type"] = pointee_type_json;
3✔
324
        j["address_space"] = pointer_type->address_space();
3✔
325
        j["initializer"] = pointer_type->initializer();
3✔
326
        j["device_location"] = pointer_type->device_location();
3✔
327
    } else if (auto structure_type = dynamic_cast<const types::Structure*>(&type)) {
7✔
328
        j["type"] = "structure";
2✔
329
        j["name"] = structure_type->name();
2✔
330
        j["address_space"] = structure_type->address_space();
2✔
331
        j["initializer"] = structure_type->initializer();
2✔
332
        j["device_location"] = structure_type->device_location();
2✔
333
    } else if (auto function_type = dynamic_cast<const types::Function*>(&type)) {
4✔
334
        j["type"] = "function";
2✔
335
        nlohmann::json return_type_json;
2✔
336
        type_to_json(return_type_json, function_type->return_type());
2✔
337
        j["return_type"] = return_type_json;
2✔
338
        j["params"] = nlohmann::json::array();
2✔
339
        for (size_t i = 0; i < function_type->num_params(); i++) {
5✔
340
            nlohmann::json param_json;
3✔
341
            type_to_json(param_json, function_type->param_type(symbolic::integer(i)));
3✔
342
            j["params"].push_back(param_json);
3✔
343
        }
3✔
344
        j["is_var_arg"] = function_type->is_var_arg();
2✔
345
        j["address_space"] = function_type->address_space();
2✔
346
        j["initializer"] = function_type->initializer();
2✔
347
        j["device_location"] = function_type->device_location();
2✔
348
    } else {
2✔
349
        throw std::runtime_error("Unknown type");
×
350
    }
351
}
43✔
352

353
void JSONSerializer::structure_definition_to_json(nlohmann::json& j,
1✔
354
                                                  const types::StructureDefinition& definition) {
355
    j["name"] = definition.name();
1✔
356
    j["members"] = nlohmann::json::array();
1✔
357
    for (size_t i = 0; i < definition.num_members(); i++) {
3✔
358
        nlohmann::json member_json;
2✔
359
        type_to_json(member_json, definition.member_type(symbolic::integer(i)));
2✔
360
        j["members"].push_back(member_json);
2✔
361
    }
2✔
362
    j["is_packed"] = definition.is_packed();
1✔
363
}
1✔
364

365
/*
366
 * * Deserialization logic
367
 */
368

369
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
3✔
370
    assert(j.contains("name"));
3✔
371
    assert(j["name"].is_string());
3✔
372

373
    builder::StructuredSDFGBuilder builder(j["name"]);
3✔
374

375
    // deserialize structures
376
    assert(j.contains("structures"));
3✔
377
    assert(j["structures"].is_array());
3✔
378
    for (const auto& structure : j["structures"]) {
3✔
379
        assert(structure.contains("name"));
×
380
        assert(structure["name"].is_string());
×
381
        json_to_structure_definition(structure, builder);
×
382
    }
383

384
    nlohmann::json& containers = j["containers"];
3✔
385

386
    // deserialize externals
387
    for (const auto& name : j["externals"]) {
3✔
NEW
388
        auto& type_desc = containers.at(name.get<std::string>());
×
NEW
389
        auto type = json_to_type(type_desc);
×
NEW
390
        builder.add_container(name, *type, false, true);
×
NEW
391
    }
×
392

393
    // deserialize arguments
394
    for (const auto& name : j["arguments"]) {
5✔
395
        auto& type_desc = containers.at(name.get<std::string>());
2✔
396
        auto type = json_to_type(type_desc);
2✔
397
        builder.add_container(name, *type, true, false);
2✔
398
    }
2✔
399

400
    // deserialize transients
401
    for (const auto& entry : containers.items()) {
8✔
402
        if (builder.subject().is_argument(entry.key())) {
5✔
403
            continue;
2✔
404
        }
405
        if (builder.subject().is_external(entry.key())) {
3✔
NEW
406
            continue;
×
407
        }
408
        auto type = json_to_type(entry.value());
3✔
409
        builder.add_container(entry.key(), *type, false, false);
3✔
410
    }
3✔
411

412
    // deserialize root node
413
    assert(j.contains("root"));
3✔
414
    auto& root = builder.subject().root();
3✔
415
    json_to_sequence(j["root"], builder, root);
3✔
416

417
    // deserialize metadata
418
    assert(j.contains("metadata"));
3✔
419
    assert(j["metadata"].is_object());
3✔
420
    for (const auto& entry : j["metadata"].items()) {
4✔
421
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
422
    }
423

424
    return builder.move();
3✔
425
}
3✔
426

427
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j,
1✔
428
                                                  builder::StructuredSDFGBuilder& builder) {
429
    assert(j.contains("name"));
1✔
430
    assert(j["name"].is_string());
1✔
431
    assert(j.contains("members"));
1✔
432
    assert(j["members"].is_array());
1✔
433
    assert(j.contains("is_packed"));
1✔
434
    assert(j["is_packed"].is_boolean());
1✔
435
    auto is_packed = j["is_packed"];
1✔
436
    auto& definition = builder.add_structure(j["name"], is_packed);
1✔
437
    for (const auto& member : j["members"]) {
3✔
438
        nlohmann::json member_json;
2✔
439
        auto member_type = json_to_type(member);
2✔
440
        definition.add_member(*member_type);
2✔
441
    }
2✔
442
}
1✔
443

444
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(
8✔
445
    const nlohmann::json& j) {
446
    std::vector<std::pair<std::string, types::Scalar>> arguments;
8✔
447
    for (const auto& argument : j) {
20✔
448
        assert(argument.contains("name"));
12✔
449
        assert(argument["name"].is_string());
12✔
450
        assert(argument.contains("type"));
12✔
451
        assert(argument["type"].is_object());
12✔
452
        std::string name = argument["name"];
12✔
453
        auto type = json_to_type(argument["type"]);
12✔
454
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
12✔
455
    }
12✔
456
    return arguments;
8✔
457
}
8✔
458

459
void JSONSerializer::json_to_dataflow(const nlohmann::json& j,
11✔
460
                                      builder::StructuredSDFGBuilder& builder,
461
                                      structured_control_flow::Block& parent) {
462
    std::unordered_map<long, data_flow::DataFlowNode&> nodes_map;
11✔
463

464
    assert(j.contains("nodes"));
11✔
465
    assert(j["nodes"].is_array());
11✔
466
    for (const auto& node : j["nodes"]) {
27✔
467
        assert(node.contains("type"));
16✔
468
        assert(node["type"].is_string());
16✔
469
        assert(node.contains("serialization_id"));
16✔
470
        assert(node["serialization_id"].is_number_integer());
16✔
471
        std::string type = node["type"];
16✔
472
        if (type == "tasklet") {
16✔
473
            assert(node.contains("code"));
4✔
474
            assert(node["code"].is_number_integer());
4✔
475
            assert(node.contains("inputs"));
4✔
476
            assert(node["inputs"].is_array());
4✔
477
            assert(node.contains("outputs"));
4✔
478
            assert(node["outputs"].is_array());
4✔
479
            auto outputs = json_to_arguments(node["outputs"]);
4✔
480
            assert(outputs.size() == 1);
4✔
481
            auto inputs = json_to_arguments(node["inputs"]);
4✔
482
            auto& tasklet = builder.add_tasklet(parent, node["code"], outputs.at(0), inputs);
4✔
483

484
            nodes_map.insert({node["serialization_id"], tasklet});
4✔
485
        } else if (type == "library_node") {
16✔
486
            assert(node.contains("call"));
×
487
            assert(node.contains("inputs"));
×
488
            assert(node["inputs"].is_array());
×
489
            assert(node.contains("outputs"));
×
490
            assert(node["outputs"].is_array());
×
491
            auto outputs = json_to_arguments(node["outputs"]);
×
492
            auto inputs = json_to_arguments(node["inputs"]);
×
493
            auto& lib_node = builder.add_library_node(parent, node["call"], outputs, inputs);
×
494

NEW
495
            nodes_map.insert({node["serialization_id"], lib_node});
×
496
        } else if (type == "access_node") {
12✔
497
            assert(node.contains("data"));
12✔
498
            auto& access_node = builder.add_access(parent, node["data"]);
12✔
499

500
            nodes_map.insert({node["serialization_id"], access_node});
12✔
501
        } else {
12✔
502
            throw std::runtime_error("Unknown node type");
×
503
        }
504
    }
16✔
505

506
    assert(j.contains("edges"));
11✔
507
    assert(j["edges"].is_array());
11✔
508
    for (const auto& edge : j["edges"]) {
23✔
509
        assert(edge.contains("src"));
12✔
510
        assert(edge["src"].is_number_integer());
12✔
511
        assert(edge.contains("dst"));
12✔
512
        assert(edge["dst"].is_number_integer());
12✔
513
        assert(edge.contains("src_conn"));
12✔
514
        assert(edge["src_conn"].is_string());
12✔
515
        assert(edge.contains("dst_conn"));
12✔
516
        assert(edge["dst_conn"].is_string());
12✔
517
        assert(edge.contains("subset"));
12✔
518
        assert(edge["subset"].is_array());
12✔
519

520
        assert(nodes_map.contains(edge["src"]));
12✔
521
        assert(nodes_map.contains(edge["dst"]));
12✔
522
        auto& source = nodes_map.at(edge["src"]);
12✔
523
        auto& target = nodes_map.at(edge["dst"]);
12✔
524

525
        assert(edge.contains("subset"));
12✔
526
        assert(edge["subset"].is_array());
12✔
527
        std::vector<symbolic::Expression> subset;
12✔
528
        for (const auto& subset_str : edge["subset"]) {
16✔
529
            assert(subset_str.is_string());
4✔
530
            SymEngine::Expression subset_expr(subset_str);
4✔
531
            subset.push_back(subset_expr);
4✔
532
        }
4✔
533
        builder.add_memlet(parent, source, edge["src_conn"], target, edge["dst_conn"], subset);
12✔
534
    }
12✔
535
}
11✔
536

537
void JSONSerializer::json_to_sequence(const nlohmann::json& j,
13✔
538
                                      builder::StructuredSDFGBuilder& builder,
539
                                      structured_control_flow::Sequence& sequence) {
540
    assert(j.contains("type"));
13✔
541
    assert(j["type"].is_string());
13✔
542
    assert(j.contains("children"));
13✔
543
    assert(j["children"].is_array());
13✔
544
    assert(j.contains("transitions"));
13✔
545
    assert(j["transitions"].is_array());
13✔
546
    assert(j["transitions"].size() == j["children"].size());
13✔
547
    std::string type = j["type"];
13✔
548
    if (type == "sequence") {
13✔
549
        for (size_t i = 0; i < j["children"].size(); i++) {
24✔
550
            auto& child = j["children"][i];
11✔
551
            auto& transition = j["transitions"][i];
11✔
552
            assert(child.contains("type"));
11✔
553
            assert(child["type"].is_string());
11✔
554

555
            assert(transition.contains("type"));
11✔
556
            assert(transition["type"].is_string());
11✔
557
            assert(transition.contains("assignments"));
11✔
558
            assert(transition["assignments"].is_array());
11✔
559
            symbolic::Assignments assignments;
11✔
560
            for (const auto& assignment : transition["assignments"]) {
12✔
561
                assert(assignment.contains("symbol"));
1✔
562
                assert(assignment["symbol"].is_string());
1✔
563
                assert(assignment.contains("expression"));
1✔
564
                assert(assignment["expression"].is_string());
1✔
565
                SymEngine::Expression expr(assignment["expression"]);
1✔
566
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
1✔
567
            }
1✔
568

569
            if (child["type"] == "block") {
11✔
570
                json_to_block_node(child, builder, sequence, assignments);
9✔
571
            } else if (child["type"] == "for") {
11✔
572
                json_to_for_node(child, builder, sequence, assignments);
×
573
            } else if (child["type"] == "if_else") {
2✔
574
                json_to_if_else_node(child, builder, sequence);
×
575
            } else if (child["type"] == "while") {
2✔
576
                json_to_while_node(child, builder, sequence, assignments);
×
577
            } else if (child["type"] == "break") {
2✔
578
                json_to_break_node(child, builder, sequence);
1✔
579
            } else if (child["type"] == "continue") {
2✔
580
                json_to_continue_node(child, builder, sequence);
1✔
581
            } else if (child["type"] == "kernel") {
1✔
582
                json_to_kernel_node(child, builder, sequence);
×
583
            } else if (child["type"] == "return") {
×
584
                json_to_return_node(child, builder, sequence, assignments);
×
585
            } else {
×
586
                throw std::runtime_error("Unknown child type");
×
587
            }
588
        }
11✔
589
    } else {
13✔
590
        throw std::runtime_error("expected sequence type");
×
591
    }
592
}
13✔
593

594
void JSONSerializer::json_to_block_node(const nlohmann::json& j,
10✔
595
                                        builder::StructuredSDFGBuilder& builder,
596
                                        structured_control_flow::Sequence& parent,
597
                                        symbolic::Assignments& assignments) {
598
    assert(j.contains("type"));
10✔
599
    assert(j["type"].is_string());
10✔
600
    assert(j.contains("dataflow"));
10✔
601
    assert(j["dataflow"].is_object());
10✔
602
    auto& block = builder.add_block(parent, assignments);
10✔
603
    assert(j["dataflow"].contains("type"));
10✔
604
    assert(j["dataflow"]["type"].is_string());
10✔
605
    std::string type = j["dataflow"]["type"];
10✔
606
    if (type == "dataflow") {
10✔
607
        json_to_dataflow(j["dataflow"], builder, block);
10✔
608
    } else {
10✔
609
        throw std::runtime_error("Unknown dataflow type");
×
610
    }
611
}
10✔
612

613
void JSONSerializer::json_to_for_node(const nlohmann::json& j,
1✔
614
                                      builder::StructuredSDFGBuilder& builder,
615
                                      structured_control_flow::Sequence& parent,
616
                                      symbolic::Assignments& assignments) {
617
    assert(j.contains("type"));
1✔
618
    assert(j["type"].is_string());
1✔
619
    assert(j.contains("indvar"));
1✔
620
    assert(j["indvar"].is_string());
1✔
621
    assert(j.contains("init"));
1✔
622
    assert(j["init"].is_string());
1✔
623
    assert(j.contains("condition"));
1✔
624
    assert(j["condition"].is_string());
1✔
625
    assert(j.contains("update"));
1✔
626
    assert(j["update"].is_string());
1✔
627
    assert(j.contains("children"));
1✔
628
    assert(j["children"].is_object());
1✔
629

630
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
631
    SymEngine::Expression init(j["init"]);
1✔
632
    SymEngine::Expression condition_expr(j["condition"]);
1✔
633
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
1✔
634
                .is_null());
635
    symbolic::Condition condition =
636
        SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
637
    SymEngine::Expression update(j["update"]);
1✔
638
    auto& for_node = builder.add_for(parent, indvar, condition, init, update, assignments);
1✔
639

640
    assert(j["children"].contains("type"));
1✔
641
    assert(j["children"]["type"].is_string());
1✔
642
    std::string type = j["children"]["type"];
1✔
643
    if (type == "sequence") {
1✔
644
        json_to_sequence(j["children"], builder, for_node.root());
1✔
645
    } else {
1✔
646
        throw std::runtime_error("Unknown child type");
×
647
    }
648
}
1✔
649

650
void JSONSerializer::json_to_if_else_node(const nlohmann::json& j,
1✔
651
                                          builder::StructuredSDFGBuilder& builder,
652
                                          structured_control_flow::Sequence& parent) {
653
    assert(j.contains("type"));
1✔
654
    assert(j["type"].is_string());
1✔
655
    assert(j["type"] == "if_else");
1✔
656
    assert(j.contains("branches"));
1✔
657
    assert(j["branches"].is_array());
1✔
658
    auto& if_else_node = builder.add_if_else(parent);
1✔
659
    for (const auto& branch : j["branches"]) {
3✔
660
        assert(branch.contains("condition"));
2✔
661
        assert(branch["condition"].is_string());
2✔
662
        assert(branch.contains("children"));
2✔
663
        assert(branch["children"].is_object());
2✔
664
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
665
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
2✔
666
                    .is_null());
667
        symbolic::Condition condition =
668
            SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
2✔
669
        auto& true_case = builder.add_case(if_else_node, condition);
2✔
670
        assert(branch["children"].contains("type"));
2✔
671
        assert(branch["children"]["type"].is_string());
2✔
672
        std::string type = branch["children"]["type"];
2✔
673
        if (type == "sequence") {
2✔
674
            json_to_sequence(branch["children"], builder, true_case);
2✔
675
        } else {
2✔
676
            throw std::runtime_error("Unknown child type");
×
677
        }
678
    }
2✔
679
}
1✔
680

681
void JSONSerializer::json_to_while_node(const nlohmann::json& j,
3✔
682
                                        builder::StructuredSDFGBuilder& builder,
683
                                        structured_control_flow::Sequence& parent,
684
                                        symbolic::Assignments& assignments) {
685
    assert(j.contains("type"));
3✔
686
    assert(j["type"].is_string());
3✔
687
    assert(j["type"] == "while");
3✔
688
    assert(j.contains("children"));
3✔
689
    assert(j["children"].is_object());
3✔
690

691
    auto& while_node = builder.add_while(parent, assignments);
3✔
692

693
    assert(j["children"].contains("type"));
3✔
694
    assert(j["children"]["type"].is_string());
3✔
695
    std::string type = j["children"]["type"];
3✔
696
    if (type == "sequence") {
3✔
697
        json_to_sequence(j["children"], builder, while_node.root());
3✔
698
    } else {
3✔
699
        throw std::runtime_error("Unknown child type");
×
700
    }
701
}
3✔
702

703
void JSONSerializer::json_to_break_node(const nlohmann::json& j,
1✔
704
                                        builder::StructuredSDFGBuilder& builder,
705
                                        structured_control_flow::Sequence& parent) {
706
    assert(j.contains("type"));
1✔
707
    assert(j["type"].is_string());
1✔
708
    assert(j["type"] == "break");
1✔
709
    builder.add_break(parent);
1✔
710
}
1✔
711

712
void JSONSerializer::json_to_continue_node(const nlohmann::json& j,
1✔
713
                                           builder::StructuredSDFGBuilder& builder,
714
                                           structured_control_flow::Sequence& parent) {
715
    assert(j.contains("type"));
1✔
716
    assert(j["type"].is_string());
1✔
717
    assert(j["type"] == "continue");
1✔
718
    builder.add_continue(parent);
1✔
719
}
1✔
720

721
void JSONSerializer::json_to_kernel_node(const nlohmann::json& j,
1✔
722
                                         builder::StructuredSDFGBuilder& builder,
723
                                         structured_control_flow::Sequence& parent) {
724
    assert(j.contains("type"));
1✔
725
    assert(j["type"].is_string());
1✔
726
    assert(j["type"] == "kernel");
1✔
727
    assert(j.contains("suffix"));
1✔
728
    assert(j["suffix"].is_string());
1✔
729
    assert(j.contains("children"));
1✔
730
    assert(j["children"].is_object());
1✔
731
    auto& kernel_node = builder.add_kernel(parent, j["suffix"]);
1✔
732

733
    assert(j["children"].contains("type"));
1✔
734
    assert(j["children"]["type"].is_string());
1✔
735
    std::string type = j["children"]["type"];
1✔
736
    if (type == "sequence") {
1✔
737
        json_to_sequence(j["children"], builder, kernel_node.root());
1✔
738
    } else {
1✔
739
        throw std::runtime_error("Unknown child type");
×
740
    }
741
}
1✔
742

743
void JSONSerializer::json_to_map_node(const nlohmann::json& j,
1✔
744
                                      builder::StructuredSDFGBuilder& builder,
745
                                      structured_control_flow::Sequence& parent,
746
                                      symbolic::Assignments& assignments) {
747
    assert(j.contains("type"));
1✔
748
    assert(j["type"].is_string());
1✔
749
    assert(j["type"] == "map");
1✔
750
    assert(j.contains("indvar"));
1✔
751
    assert(j["indvar"].is_string());
1✔
752
    assert(j.contains("num_iterations"));
1✔
753
    assert(j["num_iterations"].is_string());
1✔
754
    assert(j.contains("children"));
1✔
755
    assert(j["children"].is_object());
1✔
756
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
757
    SymEngine::Expression num_iterations(j["num_iterations"]);
1✔
758

759
    auto& map_node = builder.add_map(parent, indvar, num_iterations, assignments);
1✔
760
    assert(j["children"].contains("type"));
1✔
761
    assert(j["children"]["type"].is_string());
1✔
762
    std::string type = j["children"]["type"];
1✔
763
    if (type == "sequence") {
1✔
764
        json_to_sequence(j["children"], builder, map_node.root());
1✔
765
    } else {
1✔
766
        throw std::runtime_error("Unknown child type");
×
767
    }
768
}
1✔
769

770
void JSONSerializer::json_to_return_node(const nlohmann::json& j,
1✔
771
                                         builder::StructuredSDFGBuilder& builder,
772
                                         structured_control_flow::Sequence& parent,
773
                                         symbolic::Assignments& assignments) {
774
    assert(j.contains("type"));
1✔
775
    assert(j["type"].is_string());
1✔
776
    assert(j["type"] == "return");
1✔
777

778
    builder.add_return(parent, assignments);
1✔
779
}
1✔
780

781
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
31✔
782
    if (j.contains("type")) {
31✔
783
        if (j["type"] == "scalar") {
31✔
784
            // Deserialize scalar type
785
            assert(j.contains("primitive_type"));
25✔
786
            types::PrimitiveType primitive_type = j["primitive_type"];
25✔
787
            assert(j.contains("device_location"));
25✔
788
            types::DeviceLocation device_location = j["device_location"];
25✔
789
            assert(j.contains("address_space"));
25✔
790
            uint address_space = j["address_space"];
25✔
791
            assert(j.contains("initializer"));
25✔
792
            std::string initializer = j["initializer"];
25✔
793
            return std::make_unique<types::Scalar>(primitive_type, device_location, address_space,
25✔
794
                                                   initializer);
795
        } else if (j["type"] == "array") {
31✔
796
            // Deserialize array type
797
            assert(j.contains("element_type"));
2✔
798
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
799
            assert(j.contains("num_elements"));
2✔
800
            std::string num_elements_str = j["num_elements"];
2✔
801
            // Convert num_elements_str to symbolic::Expression
802
            SymEngine::Expression num_elements(num_elements_str);
2✔
803
            assert(j.contains("address_space"));
2✔
804
            uint address_space = j["address_space"];
2✔
805
            assert(j.contains("initializer"));
2✔
806
            std::string initializer = j["initializer"];
2✔
807
            assert(j.contains("device_location"));
2✔
808
            types::DeviceLocation device_location = j["device_location"];
2✔
809
            assert(j.contains("alignment"));
2✔
810
            size_t alignment = j["alignment"];
2✔
811
            return std::make_unique<types::Array>(*member_type, num_elements, device_location,
2✔
812
                                                  address_space, initializer, alignment);
813
        } else if (j["type"] == "pointer") {
6✔
814
            // Deserialize pointer type
815
            assert(j.contains("pointee_type"));
2✔
816
            std::unique_ptr<types::IType> pointee_type = json_to_type(j["pointee_type"]);
2✔
817
            assert(j.contains("address_space"));
2✔
818
            uint address_space = j["address_space"];
2✔
819
            assert(j.contains("initializer"));
2✔
820
            std::string initializer = j["initializer"];
2✔
821
            assert(j.contains("device_location"));
2✔
822
            types::DeviceLocation device_location = j["device_location"];
2✔
823
            return std::make_unique<types::Pointer>(*pointee_type, device_location, address_space,
2✔
824
                                                    initializer);
825
        } else if (j["type"] == "structure") {
4✔
826
            // Deserialize structure type
827
            assert(j.contains("name"));
1✔
828
            std::string name = j["name"];
1✔
829
            assert(j.contains("address_space"));
1✔
830
            uint address_space = j["address_space"];
1✔
831
            assert(j.contains("initializer"));
1✔
832
            std::string initializer = j["initializer"];
1✔
833
            assert(j.contains("device_location"));
1✔
834
            types::DeviceLocation device_location = j["device_location"];
1✔
835
            return std::make_unique<types::Structure>(name, device_location, address_space,
1✔
836
                                                      initializer);
837
        } else if (j["type"] == "function") {
2✔
838
            // Deserialize function type
839
            assert(j.contains("return_type"));
1✔
840
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
841
            assert(j.contains("params"));
1✔
842
            std::vector<std::unique_ptr<types::IType>> params;
1✔
843
            for (const auto& param : j["params"]) {
3✔
844
                params.push_back(json_to_type(param));
2✔
845
            }
846
            assert(j.contains("is_var_arg"));
1✔
847
            bool is_var_arg = j["is_var_arg"];
1✔
848
            assert(j.contains("address_space"));
1✔
849
            uint address_space = j["address_space"];
1✔
850
            assert(j.contains("initializer"));
1✔
851
            std::string initializer = j["initializer"];
1✔
852
            assert(j.contains("device_location"));
1✔
853
            types::DeviceLocation device_location = j["device_location"];
1✔
854
            auto function = std::make_unique<types::Function>(
1✔
855
                *return_type, is_var_arg, device_location, address_space, initializer);
1✔
856
            for (const auto& param : params) {
3✔
857
                function->add_param(*param);
2✔
858
            }
859
            return function->clone();
1✔
860

861
        } else {
1✔
862
            throw std::runtime_error("Unknown type");
×
863
        }
864
    } else {
865
        throw std::runtime_error("Type not found");
×
866
    }
867
}
31✔
868

869
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
29✔
870
    JSONSymbolicPrinter printer;
29✔
871
    return printer.apply(expr);
29✔
872
};
29✔
873

874
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
875
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
876
    str_ = parenthesize(str_);
×
877
};
×
878

879
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
880
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
881
    str_ = parenthesize(str_);
×
882
};
×
883

884
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
885
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
886
    str_ = parenthesize(str_);
×
887
};
×
888

889
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
2✔
890
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
2✔
891
    str_ = parenthesize(str_);
2✔
892
};
2✔
893

894
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
895
    std::ostringstream s;
×
896
    auto container = x.get_args();
×
897
    if (container.size() == 1) {
×
898
        s << apply(*container.begin());
×
899
    } else {
×
900
        s << "min(";
×
901
        s << apply(*container.begin());
×
902

903
        // Recursively apply __daisy_min to the arguments
904
        SymEngine::vec_basic subargs;
×
905
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
906
            subargs.push_back(*it);
×
907
        }
×
908
        auto submin = SymEngine::min(subargs);
×
909
        s << ", " << apply(submin);
×
910

911
        s << ")";
×
912
    }
×
913

914
    str_ = s.str();
×
915
};
×
916

917
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
918
    std::ostringstream s;
×
919
    auto container = x.get_args();
×
920
    if (container.size() == 1) {
×
921
        s << apply(*container.begin());
×
922
    } else {
×
923
        s << "max(";
×
924
        s << apply(*container.begin());
×
925

926
        // Recursively apply __daisy_max to the arguments
927
        SymEngine::vec_basic subargs;
×
928
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
929
            subargs.push_back(*it);
×
930
        }
×
931
        auto submax = SymEngine::max(subargs);
×
932
        s << ", " << apply(submax);
×
933

934
        s << ")";
×
935
    }
×
936

937
    str_ = s.str();
×
938
};
×
939

940
}  // namespace serializer
941
}  // 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