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

daisytuner / sdfglib / 15453311876

04 Jun 2025 09:38PM UTC coverage: 58.159% (-0.3%) from 58.435%
15453311876

push

github

web-flow
Merge pull request #53 from daisytuner/element-ids

uses uuid as element id

206 of 236 new or added lines in 26 files covered. (87.29%)

6 existing lines in 6 files now uncovered.

8105 of 13936 relevant lines covered (58.16%)

107.66 hits per line

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

83.26
/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✔
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
    for (auto& node : dataflow.nodes()) {
42✔
85
        nlohmann::json node_json;
20✔
86
        node_json["element_id"] = node.element_id();
20✔
87

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();
×
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
        j["nodes"].push_back(node_json);
20✔
143
    }
20✔
144

145
    for (auto& edge : dataflow.edges()) {
37✔
146
        nlohmann::json edge_json;
15✔
147

148
        edge_json["src"] = edge.src().element_id();
15✔
149
        edge_json["dst"] = edge.dst().element_id();
15✔
150

151
        edge_json["src_conn"] = edge.src_conn();
15✔
152
        edge_json["dst_conn"] = edge.dst_conn();
15✔
153

154
        edge_json["subset"] = nlohmann::json::array();
15✔
155
        for (auto& subset : edge.subset()) {
21✔
156
            edge_json["subset"].push_back(expression(subset));
6✔
157
        }
158

159
        j["edges"].push_back(edge_json);
15✔
160
    }
15✔
161
}
22✔
162

163
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
20✔
164
    j["type"] = "block";
20✔
165
    j["element_id"] = block.element_id();
20✔
166
    nlohmann::json dataflow_json;
20✔
167
    dataflow_to_json(dataflow_json, block.dataflow());
20✔
168
    j["dataflow"] = dataflow_json;
20✔
169
}
20✔
170

171
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
172
    j["type"] = "for";
2✔
173
    j["element_id"] = for_node.element_id();
2✔
174
    j["indvar"] = expression(for_node.indvar());
2✔
175
    j["init"] = expression(for_node.init());
2✔
176
    j["condition"] = expression(for_node.condition());
2✔
177
    j["update"] = expression(for_node.update());
2✔
178

179
    nlohmann::json body_json;
2✔
180
    sequence_to_json(body_json, for_node.root());
2✔
181
    j["children"] = body_json;
2✔
182
}
2✔
183

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

199
void JSONSerializer::while_node_to_json(nlohmann::json& j,
5✔
200
                                        const structured_control_flow::While& while_node) {
201
    j["type"] = "while";
5✔
202
    j["element_id"] = while_node.element_id();
5✔
203
    nlohmann::json body_json;
5✔
204
    sequence_to_json(body_json, while_node.root());
5✔
205
    j["children"] = body_json;
5✔
206
}
5✔
207

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

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

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

230
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
231
    j["type"] = "map";
2✔
232
    j["element_id"] = map_node.element_id();
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
    j["element_id"] = return_node.element_id();
2✔
244
}
2✔
245

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

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

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

287
        j["children"].push_back(child_json);
22✔
288

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

301
        j["transitions"].push_back(transition_json);
22✔
302
    }
22✔
303
}
21✔
304

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

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

368
/*
369
 * * Deserialization logic
370
 */
371

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

376
    builder::StructuredSDFGBuilder builder(j["name"]);
3✔
377

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

387
    nlohmann::json& containers = j["containers"];
3✔
388

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

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

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

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

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

427
    return builder.move();
3✔
428
}
3✔
429

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

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

462
void JSONSerializer::json_to_dataflow(const nlohmann::json& j,
11✔
463
                                      builder::StructuredSDFGBuilder& builder,
464
                                      structured_control_flow::Block& parent) {
465
    std::unordered_map<std::string, data_flow::DataFlowNode&> nodes_map;
11✔
466

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

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

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

503
            nodes_map.insert({node["element_id"], access_node});
12✔
504
        } else {
12✔
505
            throw std::runtime_error("Unknown node type");
×
506
        }
507
    }
16✔
508

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

523
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
524
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
525
        auto& source = nodes_map.at(edge["src"]);
12✔
526
        auto& target = nodes_map.at(edge["dst"]);
12✔
527

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

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

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

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

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

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

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

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

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

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

694
    auto& while_node = builder.add_while(parent, assignments);
3✔
695

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

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

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

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

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

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

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

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

781
    builder.add_return(parent, assignments);
1✔
782
}
1✔
783

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

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

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

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

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

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

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

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

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

914
        s << ")";
×
915
    }
×
916

917
    str_ = s.str();
×
918
};
×
919

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

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

937
        s << ")";
×
938
    }
×
939

940
    str_ = s.str();
×
941
};
×
942

943
}  // namespace serializer
944
}  // 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