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

daisytuner / sdfglib / 15453698313

04 Jun 2025 10:04PM UTC coverage: 58.264% (+0.1%) from 58.159%
15453698313

push

github

web-flow
Merge pull request #54 from daisytuner/dbg-infos

serialize debug_info

35 of 35 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

8140 of 13971 relevant lines covered (58.26%)

107.53 hits per line

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

84.04
/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
        j["debug_info"] = nlohmann::json::object();
20✔
89
        debug_info_to_json(j["debug_info"], node.debug_info());
20✔
90

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

145
        j["nodes"].push_back(node_json);
20✔
146
    }
20✔
147

148
    for (auto& edge : dataflow.edges()) {
37✔
149
        nlohmann::json edge_json;
15✔
150
        edge_json["element_id"] = edge.element_id();
15✔
151

152
        edge_json["debug_info"] = nlohmann::json::object();
15✔
153
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
15✔
154

155
        edge_json["src"] = edge.src().element_id();
15✔
156
        edge_json["dst"] = edge.dst().element_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
    j["element_id"] = block.element_id();
20✔
173

174
    j["debug_info"] = nlohmann::json::object();
20✔
175
    debug_info_to_json(j["debug_info"], block.debug_info());
20✔
176

177
    nlohmann::json dataflow_json;
20✔
178
    dataflow_to_json(dataflow_json, block.dataflow());
20✔
179
    j["dataflow"] = dataflow_json;
20✔
180
}
20✔
181

182
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
183
    j["type"] = "for";
2✔
184
    j["element_id"] = for_node.element_id();
2✔
185

186
    j["debug_info"] = nlohmann::json::object();
2✔
187
    debug_info_to_json(j["debug_info"], for_node.debug_info());
2✔
188

189
    j["indvar"] = expression(for_node.indvar());
2✔
190
    j["init"] = expression(for_node.init());
2✔
191
    j["condition"] = expression(for_node.condition());
2✔
192
    j["update"] = expression(for_node.update());
2✔
193

194
    nlohmann::json body_json;
2✔
195
    sequence_to_json(body_json, for_node.root());
2✔
196
    j["children"] = body_json;
2✔
197
}
2✔
198

199
void JSONSerializer::if_else_to_json(nlohmann::json& j,
2✔
200
                                     const structured_control_flow::IfElse& if_else_node) {
201
    j["type"] = "if_else";
2✔
202
    j["element_id"] = if_else_node.element_id();
2✔
203

204
    j["debug_info"] = nlohmann::json::object();
2✔
205
    debug_info_to_json(j["debug_info"], if_else_node.debug_info());
2✔
206

207
    j["branches"] = nlohmann::json::array();
2✔
208
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
209
        nlohmann::json branch_json;
4✔
210
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
211
        nlohmann::json body_json;
4✔
212
        sequence_to_json(body_json, if_else_node.at(i).first);
4✔
213
        branch_json["children"] = body_json;
4✔
214
        j["branches"].push_back(branch_json);
4✔
215
    }
4✔
216
}
2✔
217

218
void JSONSerializer::while_node_to_json(nlohmann::json& j,
5✔
219
                                        const structured_control_flow::While& while_node) {
220
    j["type"] = "while";
5✔
221
    j["element_id"] = while_node.element_id();
5✔
222

223
    j["debug_info"] = nlohmann::json::object();
5✔
224
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
225

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

231
void JSONSerializer::break_node_to_json(nlohmann::json& j,
2✔
232
                                        const structured_control_flow::Break& break_node) {
233
    j["type"] = "break";
2✔
234
    j["element_id"] = break_node.element_id();
2✔
235

236
    j["debug_info"] = nlohmann::json::object();
2✔
237
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
238
}
2✔
239

240
void JSONSerializer::continue_node_to_json(nlohmann::json& j,
2✔
241
                                           const structured_control_flow::Continue& continue_node) {
242
    j["type"] = "continue";
2✔
243
    j["element_id"] = continue_node.element_id();
2✔
244

245
    j["debug_info"] = nlohmann::json::object();
2✔
246
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
247
}
2✔
248

249
void JSONSerializer::kernel_to_json(nlohmann::json& j,
2✔
250
                                    const structured_control_flow::Kernel& kernel_node) {
251
    j["type"] = "kernel";
2✔
252
    j["element_id"] = kernel_node.element_id();
2✔
253

254
    j["debug_info"] = nlohmann::json::object();
2✔
255
    debug_info_to_json(j["debug_info"], kernel_node.debug_info());
2✔
256

257
    j["suffix"] = kernel_node.suffix();
2✔
258

259
    nlohmann::json body_json;
2✔
260
    sequence_to_json(body_json, kernel_node.root());
2✔
261
    j["children"] = body_json;
2✔
262
}
2✔
263

264
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
265
    j["type"] = "map";
2✔
266
    j["element_id"] = map_node.element_id();
2✔
267

268
    j["debug_info"] = nlohmann::json::object();
2✔
269
    debug_info_to_json(j["debug_info"], map_node.debug_info());
2✔
270

271
    j["indvar"] = expression(map_node.indvar());
2✔
272
    j["num_iterations"] = expression(map_node.num_iterations());
2✔
273
    nlohmann::json body_json;
2✔
274
    sequence_to_json(body_json, map_node.root());
2✔
275
    j["children"] = body_json;
2✔
276
}
2✔
277

278
void JSONSerializer::return_node_to_json(nlohmann::json& j,
2✔
279
                                         const structured_control_flow::Return& return_node) {
280
    j["type"] = "return";
2✔
281
    j["element_id"] = return_node.element_id();
2✔
282

283
    j["debug_info"] = nlohmann::json::object();
2✔
284
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
285
}
2✔
286

287
void JSONSerializer::sequence_to_json(nlohmann::json& j,
21✔
288
                                      const structured_control_flow::Sequence& sequence) {
289
    j["type"] = "sequence";
21✔
290
    j["element_id"] = sequence.element_id();
21✔
291

292
    j["debug_info"] = nlohmann::json::object();
21✔
293
    debug_info_to_json(j["debug_info"], sequence.debug_info());
21✔
294

295
    j["children"] = nlohmann::json::array();
21✔
296
    j["transitions"] = nlohmann::json::array();
21✔
297

298
    for (size_t i = 0; i < sequence.size(); i++) {
43✔
299
        nlohmann::json child_json;
22✔
300
        auto& child = sequence.at(i).first;
22✔
301
        auto& transition = sequence.at(i).second;
22✔
302

303
        if (auto block = dynamic_cast<const structured_control_flow::Block*>(&child)) {
22✔
304
            block_to_json(child_json, *block);
18✔
305
        } else if (auto for_node = dynamic_cast<const structured_control_flow::For*>(&child)) {
22✔
306
            for_to_json(child_json, *for_node);
×
307
        } else if (auto sequence_node =
4✔
308
                       dynamic_cast<const structured_control_flow::Sequence*>(&child)) {
4✔
309
            sequence_to_json(child_json, *sequence_node);
×
310
        } else if (auto condition_node =
4✔
311
                       dynamic_cast<const structured_control_flow::IfElse*>(&child)) {
4✔
312
            if_else_to_json(child_json, *condition_node);
×
313
        } else if (auto while_node = dynamic_cast<const structured_control_flow::While*>(&child)) {
4✔
314
            while_node_to_json(child_json, *while_node);
×
315
        } else if (auto kernel_node =
4✔
316
                       dynamic_cast<const structured_control_flow::Kernel*>(&child)) {
4✔
317
            kernel_to_json(child_json, *kernel_node);
×
318
        } else if (auto return_node =
4✔
319
                       dynamic_cast<const structured_control_flow::Return*>(&child)) {
4✔
320
            return_node_to_json(child_json, *return_node);
×
321
        } else if (auto break_node = dynamic_cast<const structured_control_flow::Break*>(&child)) {
4✔
322
            break_node_to_json(child_json, *break_node);
2✔
323
        } else if (auto continue_node =
4✔
324
                       dynamic_cast<const structured_control_flow::Continue*>(&child)) {
2✔
325
            continue_node_to_json(child_json, *continue_node);
2✔
326
        } else if (auto map_node = dynamic_cast<const structured_control_flow::Map*>(&child)) {
2✔
327
            map_to_json(child_json, *map_node);
×
328
        } else {
×
329
            throw std::runtime_error("Unknown child type");
×
330
        }
331

332
        j["children"].push_back(child_json);
22✔
333

334
        // Add transition information
335
        nlohmann::json transition_json;
22✔
336
        transition_json["type"] = "transition";
22✔
337
        transition_json["element_id"] = transition.element_id();
22✔
338

339
        transition_json["debug_info"] = nlohmann::json::object();
22✔
340
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
22✔
341

342
        transition_json["assignments"] = nlohmann::json::array();
22✔
343
        for (const auto& assignment : transition.assignments()) {
24✔
344
            nlohmann::json assignment_json;
2✔
345
            assignment_json["symbol"] = expression(assignment.first);
2✔
346
            assignment_json["expression"] = expression(assignment.second);
2✔
347
            transition_json["assignments"].push_back(assignment_json);
2✔
348
        }
2✔
349

350
        j["transitions"].push_back(transition_json);
22✔
351
    }
22✔
352
}
21✔
353

354
void JSONSerializer::type_to_json(nlohmann::json& j, const types::IType& type) {
43✔
355
    if (auto scalar_type = dynamic_cast<const types::Scalar*>(&type)) {
43✔
356
        j["type"] = "scalar";
33✔
357
        j["primitive_type"] = scalar_type->primitive_type();
33✔
358
        j["address_space"] = scalar_type->address_space();
33✔
359
        j["initializer"] = scalar_type->initializer();
33✔
360
        j["device_location"] = scalar_type->device_location();
33✔
361
    } else if (auto array_type = dynamic_cast<const types::Array*>(&type)) {
43✔
362
        j["type"] = "array";
3✔
363
        nlohmann::json element_type_json;
3✔
364
        type_to_json(element_type_json, array_type->element_type());
3✔
365
        j["element_type"] = element_type_json;
3✔
366
        j["num_elements"] = expression(array_type->num_elements());
3✔
367
        j["address_space"] = array_type->address_space();
3✔
368
        j["initializer"] = array_type->initializer();
3✔
369
        j["device_location"] = array_type->device_location();
3✔
370
        j["alignment"] = array_type->alignment();
3✔
371
    } else if (auto pointer_type = dynamic_cast<const types::Pointer*>(&type)) {
10✔
372
        j["type"] = "pointer";
3✔
373
        nlohmann::json pointee_type_json;
3✔
374
        type_to_json(pointee_type_json, pointer_type->pointee_type());
3✔
375
        j["pointee_type"] = pointee_type_json;
3✔
376
        j["address_space"] = pointer_type->address_space();
3✔
377
        j["initializer"] = pointer_type->initializer();
3✔
378
        j["device_location"] = pointer_type->device_location();
3✔
379
    } else if (auto structure_type = dynamic_cast<const types::Structure*>(&type)) {
7✔
380
        j["type"] = "structure";
2✔
381
        j["name"] = structure_type->name();
2✔
382
        j["address_space"] = structure_type->address_space();
2✔
383
        j["initializer"] = structure_type->initializer();
2✔
384
        j["device_location"] = structure_type->device_location();
2✔
385
    } else if (auto function_type = dynamic_cast<const types::Function*>(&type)) {
4✔
386
        j["type"] = "function";
2✔
387
        nlohmann::json return_type_json;
2✔
388
        type_to_json(return_type_json, function_type->return_type());
2✔
389
        j["return_type"] = return_type_json;
2✔
390
        j["params"] = nlohmann::json::array();
2✔
391
        for (size_t i = 0; i < function_type->num_params(); i++) {
5✔
392
            nlohmann::json param_json;
3✔
393
            type_to_json(param_json, function_type->param_type(symbolic::integer(i)));
3✔
394
            j["params"].push_back(param_json);
3✔
395
        }
3✔
396
        j["is_var_arg"] = function_type->is_var_arg();
2✔
397
        j["address_space"] = function_type->address_space();
2✔
398
        j["initializer"] = function_type->initializer();
2✔
399
        j["device_location"] = function_type->device_location();
2✔
400
    } else {
2✔
401
        throw std::runtime_error("Unknown type");
×
402
    }
403
}
43✔
404

405
void JSONSerializer::structure_definition_to_json(nlohmann::json& j,
1✔
406
                                                  const types::StructureDefinition& definition) {
407
    j["name"] = definition.name();
1✔
408
    j["members"] = nlohmann::json::array();
1✔
409
    for (size_t i = 0; i < definition.num_members(); i++) {
3✔
410
        nlohmann::json member_json;
2✔
411
        type_to_json(member_json, definition.member_type(symbolic::integer(i)));
2✔
412
        j["members"].push_back(member_json);
2✔
413
    }
2✔
414
    j["is_packed"] = definition.is_packed();
1✔
415
}
1✔
416

417
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
117✔
418
    j["has"] = debug_info.has();
117✔
419
    j["filename"] = debug_info.filename();
117✔
420
    j["start_line"] = debug_info.start_line();
117✔
421
    j["start_column"] = debug_info.start_column();
117✔
422
    j["end_line"] = debug_info.end_line();
117✔
423
    j["end_column"] = debug_info.end_column();
117✔
424
}
117✔
425

426
/*
427
 * * Deserialization logic
428
 */
429

430
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
3✔
431
    assert(j.contains("name"));
3✔
432
    assert(j["name"].is_string());
3✔
433

434
    builder::StructuredSDFGBuilder builder(j["name"]);
3✔
435

436
    // deserialize structures
437
    assert(j.contains("structures"));
3✔
438
    assert(j["structures"].is_array());
3✔
439
    for (const auto& structure : j["structures"]) {
3✔
440
        assert(structure.contains("name"));
×
441
        assert(structure["name"].is_string());
×
442
        json_to_structure_definition(structure, builder);
×
443
    }
444

445
    nlohmann::json& containers = j["containers"];
3✔
446

447
    // deserialize externals
448
    for (const auto& name : j["externals"]) {
3✔
449
        auto& type_desc = containers.at(name.get<std::string>());
×
450
        auto type = json_to_type(type_desc);
×
451
        builder.add_container(name, *type, false, true);
×
452
    }
×
453

454
    // deserialize arguments
455
    for (const auto& name : j["arguments"]) {
5✔
456
        auto& type_desc = containers.at(name.get<std::string>());
2✔
457
        auto type = json_to_type(type_desc);
2✔
458
        builder.add_container(name, *type, true, false);
2✔
459
    }
2✔
460

461
    // deserialize transients
462
    for (const auto& entry : containers.items()) {
8✔
463
        if (builder.subject().is_argument(entry.key())) {
5✔
464
            continue;
2✔
465
        }
466
        if (builder.subject().is_external(entry.key())) {
3✔
467
            continue;
×
468
        }
469
        auto type = json_to_type(entry.value());
3✔
470
        builder.add_container(entry.key(), *type, false, false);
3✔
471
    }
3✔
472

473
    // deserialize root node
474
    assert(j.contains("root"));
3✔
475
    auto& root = builder.subject().root();
3✔
476
    json_to_sequence(j["root"], builder, root);
3✔
477

478
    // deserialize metadata
479
    assert(j.contains("metadata"));
3✔
480
    assert(j["metadata"].is_object());
3✔
481
    for (const auto& entry : j["metadata"].items()) {
4✔
482
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
483
    }
484

485
    return builder.move();
3✔
486
}
3✔
487

488
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j,
1✔
489
                                                  builder::StructuredSDFGBuilder& builder) {
490
    assert(j.contains("name"));
1✔
491
    assert(j["name"].is_string());
1✔
492
    assert(j.contains("members"));
1✔
493
    assert(j["members"].is_array());
1✔
494
    assert(j.contains("is_packed"));
1✔
495
    assert(j["is_packed"].is_boolean());
1✔
496
    auto is_packed = j["is_packed"];
1✔
497
    auto& definition = builder.add_structure(j["name"], is_packed);
1✔
498
    for (const auto& member : j["members"]) {
3✔
499
        nlohmann::json member_json;
2✔
500
        auto member_type = json_to_type(member);
2✔
501
        definition.add_member(*member_type);
2✔
502
    }
2✔
503
}
1✔
504

505
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(
8✔
506
    const nlohmann::json& j) {
507
    std::vector<std::pair<std::string, types::Scalar>> arguments;
8✔
508
    for (const auto& argument : j) {
20✔
509
        assert(argument.contains("name"));
12✔
510
        assert(argument["name"].is_string());
12✔
511
        assert(argument.contains("type"));
12✔
512
        assert(argument["type"].is_object());
12✔
513
        std::string name = argument["name"];
12✔
514
        auto type = json_to_type(argument["type"]);
12✔
515
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
12✔
516
    }
12✔
517
    return arguments;
8✔
518
}
8✔
519

520
void JSONSerializer::json_to_dataflow(const nlohmann::json& j,
11✔
521
                                      builder::StructuredSDFGBuilder& builder,
522
                                      structured_control_flow::Block& parent) {
523
    std::unordered_map<std::string, data_flow::DataFlowNode&> nodes_map;
11✔
524

525
    assert(j.contains("nodes"));
11✔
526
    assert(j["nodes"].is_array());
11✔
527
    for (const auto& node : j["nodes"]) {
27✔
528
        assert(node.contains("type"));
16✔
529
        assert(node["type"].is_string());
16✔
530
        assert(node.contains("element_id"));
16✔
531
        assert(node["element_id"].is_string());
16✔
532
        std::string type = node["type"];
16✔
533
        if (type == "tasklet") {
16✔
534
            assert(node.contains("code"));
4✔
535
            assert(node["code"].is_number_integer());
4✔
536
            assert(node.contains("inputs"));
4✔
537
            assert(node["inputs"].is_array());
4✔
538
            assert(node.contains("outputs"));
4✔
539
            assert(node["outputs"].is_array());
4✔
540
            auto outputs = json_to_arguments(node["outputs"]);
4✔
541
            assert(outputs.size() == 1);
4✔
542
            auto inputs = json_to_arguments(node["inputs"]);
4✔
543
            auto& tasklet = builder.add_tasklet(parent, node["code"], outputs.at(0), inputs);
4✔
544
            nodes_map.insert({node["element_id"], tasklet});
4✔
545
        } else if (type == "library_node") {
16✔
546
            assert(node.contains("call"));
×
547
            assert(node.contains("inputs"));
×
548
            assert(node["inputs"].is_array());
×
549
            assert(node.contains("outputs"));
×
550
            assert(node["outputs"].is_array());
×
551
            auto outputs = json_to_arguments(node["outputs"]);
×
552
            auto inputs = json_to_arguments(node["inputs"]);
×
553
            auto& lib_node = builder.add_library_node(parent, node["call"], outputs, inputs);
×
UNCOV
554
            nodes_map.insert({node["element_id"], lib_node});
×
555
        } else if (type == "access_node") {
12✔
556
            assert(node.contains("data"));
12✔
557
            auto& access_node = builder.add_access(parent, node["data"]);
12✔
558
            nodes_map.insert({node["element_id"], access_node});
12✔
559
        } else {
12✔
560
            throw std::runtime_error("Unknown node type");
×
561
        }
562
    }
16✔
563

564
    assert(j.contains("edges"));
11✔
565
    assert(j["edges"].is_array());
11✔
566
    for (const auto& edge : j["edges"]) {
23✔
567
        assert(edge.contains("src"));
12✔
568
        assert(edge["src"].is_string());
12✔
569
        assert(edge.contains("dst"));
12✔
570
        assert(edge["dst"].is_string());
12✔
571
        assert(edge.contains("src_conn"));
12✔
572
        assert(edge["src_conn"].is_string());
12✔
573
        assert(edge.contains("dst_conn"));
12✔
574
        assert(edge["dst_conn"].is_string());
12✔
575
        assert(edge.contains("subset"));
12✔
576
        assert(edge["subset"].is_array());
12✔
577

578
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
579
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
580
        auto& source = nodes_map.at(edge["src"]);
12✔
581
        auto& target = nodes_map.at(edge["dst"]);
12✔
582

583
        assert(edge.contains("subset"));
12✔
584
        assert(edge["subset"].is_array());
12✔
585
        std::vector<symbolic::Expression> subset;
12✔
586
        for (const auto& subset_str : edge["subset"]) {
16✔
587
            assert(subset_str.is_string());
4✔
588
            SymEngine::Expression subset_expr(subset_str);
4✔
589
            subset.push_back(subset_expr);
4✔
590
        }
4✔
591
        builder.add_memlet(parent, source, edge["src_conn"], target, edge["dst_conn"], subset);
12✔
592
    }
12✔
593
}
11✔
594

595
void JSONSerializer::json_to_sequence(const nlohmann::json& j,
13✔
596
                                      builder::StructuredSDFGBuilder& builder,
597
                                      structured_control_flow::Sequence& sequence) {
598
    assert(j.contains("type"));
13✔
599
    assert(j["type"].is_string());
13✔
600
    assert(j.contains("children"));
13✔
601
    assert(j["children"].is_array());
13✔
602
    assert(j.contains("transitions"));
13✔
603
    assert(j["transitions"].is_array());
13✔
604
    assert(j["transitions"].size() == j["children"].size());
13✔
605
    std::string type = j["type"];
13✔
606
    if (type == "sequence") {
13✔
607
        for (size_t i = 0; i < j["children"].size(); i++) {
24✔
608
            auto& child = j["children"][i];
11✔
609
            auto& transition = j["transitions"][i];
11✔
610
            assert(child.contains("type"));
11✔
611
            assert(child["type"].is_string());
11✔
612

613
            assert(transition.contains("type"));
11✔
614
            assert(transition["type"].is_string());
11✔
615
            assert(transition.contains("assignments"));
11✔
616
            assert(transition["assignments"].is_array());
11✔
617
            symbolic::Assignments assignments;
11✔
618
            for (const auto& assignment : transition["assignments"]) {
12✔
619
                assert(assignment.contains("symbol"));
1✔
620
                assert(assignment["symbol"].is_string());
1✔
621
                assert(assignment.contains("expression"));
1✔
622
                assert(assignment["expression"].is_string());
1✔
623
                SymEngine::Expression expr(assignment["expression"]);
1✔
624
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
1✔
625
            }
1✔
626

627
            if (child["type"] == "block") {
11✔
628
                json_to_block_node(child, builder, sequence, assignments);
9✔
629
            } else if (child["type"] == "for") {
11✔
630
                json_to_for_node(child, builder, sequence, assignments);
×
631
            } else if (child["type"] == "if_else") {
2✔
632
                json_to_if_else_node(child, builder, sequence);
×
633
            } else if (child["type"] == "while") {
2✔
634
                json_to_while_node(child, builder, sequence, assignments);
×
635
            } else if (child["type"] == "break") {
2✔
636
                json_to_break_node(child, builder, sequence);
1✔
637
            } else if (child["type"] == "continue") {
2✔
638
                json_to_continue_node(child, builder, sequence);
1✔
639
            } else if (child["type"] == "kernel") {
1✔
640
                json_to_kernel_node(child, builder, sequence);
×
641
            } else if (child["type"] == "return") {
×
642
                json_to_return_node(child, builder, sequence, assignments);
×
643
            } else {
×
644
                throw std::runtime_error("Unknown child type");
×
645
            }
646
        }
11✔
647
    } else {
13✔
648
        throw std::runtime_error("expected sequence type");
×
649
    }
650
}
13✔
651

652
void JSONSerializer::json_to_block_node(const nlohmann::json& j,
10✔
653
                                        builder::StructuredSDFGBuilder& builder,
654
                                        structured_control_flow::Sequence& parent,
655
                                        symbolic::Assignments& assignments) {
656
    assert(j.contains("type"));
10✔
657
    assert(j["type"].is_string());
10✔
658
    assert(j.contains("dataflow"));
10✔
659
    assert(j["dataflow"].is_object());
10✔
660
    auto& block = builder.add_block(parent, assignments);
10✔
661
    assert(j["dataflow"].contains("type"));
10✔
662
    assert(j["dataflow"]["type"].is_string());
10✔
663
    std::string type = j["dataflow"]["type"];
10✔
664
    if (type == "dataflow") {
10✔
665
        json_to_dataflow(j["dataflow"], builder, block);
10✔
666
    } else {
10✔
667
        throw std::runtime_error("Unknown dataflow type");
×
668
    }
669
}
10✔
670

671
void JSONSerializer::json_to_for_node(const nlohmann::json& j,
1✔
672
                                      builder::StructuredSDFGBuilder& builder,
673
                                      structured_control_flow::Sequence& parent,
674
                                      symbolic::Assignments& assignments) {
675
    assert(j.contains("type"));
1✔
676
    assert(j["type"].is_string());
1✔
677
    assert(j.contains("indvar"));
1✔
678
    assert(j["indvar"].is_string());
1✔
679
    assert(j.contains("init"));
1✔
680
    assert(j["init"].is_string());
1✔
681
    assert(j.contains("condition"));
1✔
682
    assert(j["condition"].is_string());
1✔
683
    assert(j.contains("update"));
1✔
684
    assert(j["update"].is_string());
1✔
685
    assert(j.contains("children"));
1✔
686
    assert(j["children"].is_object());
1✔
687

688
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
689
    SymEngine::Expression init(j["init"]);
1✔
690
    SymEngine::Expression condition_expr(j["condition"]);
1✔
691
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
1✔
692
                .is_null());
693
    symbolic::Condition condition =
694
        SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
695
    SymEngine::Expression update(j["update"]);
1✔
696
    auto& for_node = builder.add_for(parent, indvar, condition, init, update, assignments);
1✔
697

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

708
void JSONSerializer::json_to_if_else_node(const nlohmann::json& j,
1✔
709
                                          builder::StructuredSDFGBuilder& builder,
710
                                          structured_control_flow::Sequence& parent) {
711
    assert(j.contains("type"));
1✔
712
    assert(j["type"].is_string());
1✔
713
    assert(j["type"] == "if_else");
1✔
714
    assert(j.contains("branches"));
1✔
715
    assert(j["branches"].is_array());
1✔
716
    auto& if_else_node = builder.add_if_else(parent);
1✔
717
    for (const auto& branch : j["branches"]) {
3✔
718
        assert(branch.contains("condition"));
2✔
719
        assert(branch["condition"].is_string());
2✔
720
        assert(branch.contains("children"));
2✔
721
        assert(branch["children"].is_object());
2✔
722
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
723
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
2✔
724
                    .is_null());
725
        symbolic::Condition condition =
726
            SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
2✔
727
        auto& true_case = builder.add_case(if_else_node, condition);
2✔
728
        assert(branch["children"].contains("type"));
2✔
729
        assert(branch["children"]["type"].is_string());
2✔
730
        std::string type = branch["children"]["type"];
2✔
731
        if (type == "sequence") {
2✔
732
            json_to_sequence(branch["children"], builder, true_case);
2✔
733
        } else {
2✔
734
            throw std::runtime_error("Unknown child type");
×
735
        }
736
    }
2✔
737
}
1✔
738

739
void JSONSerializer::json_to_while_node(const nlohmann::json& j,
3✔
740
                                        builder::StructuredSDFGBuilder& builder,
741
                                        structured_control_flow::Sequence& parent,
742
                                        symbolic::Assignments& assignments) {
743
    assert(j.contains("type"));
3✔
744
    assert(j["type"].is_string());
3✔
745
    assert(j["type"] == "while");
3✔
746
    assert(j.contains("children"));
3✔
747
    assert(j["children"].is_object());
3✔
748

749
    auto& while_node = builder.add_while(parent, assignments);
3✔
750

751
    assert(j["children"].contains("type"));
3✔
752
    assert(j["children"]["type"].is_string());
3✔
753
    std::string type = j["children"]["type"];
3✔
754
    if (type == "sequence") {
3✔
755
        json_to_sequence(j["children"], builder, while_node.root());
3✔
756
    } else {
3✔
757
        throw std::runtime_error("Unknown child type");
×
758
    }
759
}
3✔
760

761
void JSONSerializer::json_to_break_node(const nlohmann::json& j,
1✔
762
                                        builder::StructuredSDFGBuilder& builder,
763
                                        structured_control_flow::Sequence& parent) {
764
    assert(j.contains("type"));
1✔
765
    assert(j["type"].is_string());
1✔
766
    assert(j["type"] == "break");
1✔
767
    builder.add_break(parent);
1✔
768
}
1✔
769

770
void JSONSerializer::json_to_continue_node(const nlohmann::json& j,
1✔
771
                                           builder::StructuredSDFGBuilder& builder,
772
                                           structured_control_flow::Sequence& parent) {
773
    assert(j.contains("type"));
1✔
774
    assert(j["type"].is_string());
1✔
775
    assert(j["type"] == "continue");
1✔
776
    builder.add_continue(parent);
1✔
777
}
1✔
778

779
void JSONSerializer::json_to_kernel_node(const nlohmann::json& j,
1✔
780
                                         builder::StructuredSDFGBuilder& builder,
781
                                         structured_control_flow::Sequence& parent) {
782
    assert(j.contains("type"));
1✔
783
    assert(j["type"].is_string());
1✔
784
    assert(j["type"] == "kernel");
1✔
785
    assert(j.contains("suffix"));
1✔
786
    assert(j["suffix"].is_string());
1✔
787
    assert(j.contains("children"));
1✔
788
    assert(j["children"].is_object());
1✔
789
    auto& kernel_node = builder.add_kernel(parent, j["suffix"]);
1✔
790

791
    assert(j["children"].contains("type"));
1✔
792
    assert(j["children"]["type"].is_string());
1✔
793
    std::string type = j["children"]["type"];
1✔
794
    if (type == "sequence") {
1✔
795
        json_to_sequence(j["children"], builder, kernel_node.root());
1✔
796
    } else {
1✔
797
        throw std::runtime_error("Unknown child type");
×
798
    }
799
}
1✔
800

801
void JSONSerializer::json_to_map_node(const nlohmann::json& j,
1✔
802
                                      builder::StructuredSDFGBuilder& builder,
803
                                      structured_control_flow::Sequence& parent,
804
                                      symbolic::Assignments& assignments) {
805
    assert(j.contains("type"));
1✔
806
    assert(j["type"].is_string());
1✔
807
    assert(j["type"] == "map");
1✔
808
    assert(j.contains("indvar"));
1✔
809
    assert(j["indvar"].is_string());
1✔
810
    assert(j.contains("num_iterations"));
1✔
811
    assert(j["num_iterations"].is_string());
1✔
812
    assert(j.contains("children"));
1✔
813
    assert(j["children"].is_object());
1✔
814
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
815
    SymEngine::Expression num_iterations(j["num_iterations"]);
1✔
816

817
    auto& map_node = builder.add_map(parent, indvar, num_iterations, assignments);
1✔
818
    assert(j["children"].contains("type"));
1✔
819
    assert(j["children"]["type"].is_string());
1✔
820
    std::string type = j["children"]["type"];
1✔
821
    if (type == "sequence") {
1✔
822
        json_to_sequence(j["children"], builder, map_node.root());
1✔
823
    } else {
1✔
824
        throw std::runtime_error("Unknown child type");
×
825
    }
826
}
1✔
827

828
void JSONSerializer::json_to_return_node(const nlohmann::json& j,
1✔
829
                                         builder::StructuredSDFGBuilder& builder,
830
                                         structured_control_flow::Sequence& parent,
831
                                         symbolic::Assignments& assignments) {
832
    assert(j.contains("type"));
1✔
833
    assert(j["type"].is_string());
1✔
834
    assert(j["type"] == "return");
1✔
835

836
    builder.add_return(parent, assignments);
1✔
837
}
1✔
838

839
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
31✔
840
    if (j.contains("type")) {
31✔
841
        if (j["type"] == "scalar") {
31✔
842
            // Deserialize scalar type
843
            assert(j.contains("primitive_type"));
25✔
844
            types::PrimitiveType primitive_type = j["primitive_type"];
25✔
845
            assert(j.contains("device_location"));
25✔
846
            types::DeviceLocation device_location = j["device_location"];
25✔
847
            assert(j.contains("address_space"));
25✔
848
            uint address_space = j["address_space"];
25✔
849
            assert(j.contains("initializer"));
25✔
850
            std::string initializer = j["initializer"];
25✔
851
            return std::make_unique<types::Scalar>(primitive_type, device_location, address_space,
25✔
852
                                                   initializer);
853
        } else if (j["type"] == "array") {
31✔
854
            // Deserialize array type
855
            assert(j.contains("element_type"));
2✔
856
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
857
            assert(j.contains("num_elements"));
2✔
858
            std::string num_elements_str = j["num_elements"];
2✔
859
            // Convert num_elements_str to symbolic::Expression
860
            SymEngine::Expression num_elements(num_elements_str);
2✔
861
            assert(j.contains("address_space"));
2✔
862
            uint address_space = j["address_space"];
2✔
863
            assert(j.contains("initializer"));
2✔
864
            std::string initializer = j["initializer"];
2✔
865
            assert(j.contains("device_location"));
2✔
866
            types::DeviceLocation device_location = j["device_location"];
2✔
867
            assert(j.contains("alignment"));
2✔
868
            size_t alignment = j["alignment"];
2✔
869
            return std::make_unique<types::Array>(*member_type, num_elements, device_location,
2✔
870
                                                  address_space, initializer, alignment);
871
        } else if (j["type"] == "pointer") {
6✔
872
            // Deserialize pointer type
873
            assert(j.contains("pointee_type"));
2✔
874
            std::unique_ptr<types::IType> pointee_type = json_to_type(j["pointee_type"]);
2✔
875
            assert(j.contains("address_space"));
2✔
876
            uint address_space = j["address_space"];
2✔
877
            assert(j.contains("initializer"));
2✔
878
            std::string initializer = j["initializer"];
2✔
879
            assert(j.contains("device_location"));
2✔
880
            types::DeviceLocation device_location = j["device_location"];
2✔
881
            return std::make_unique<types::Pointer>(*pointee_type, device_location, address_space,
2✔
882
                                                    initializer);
883
        } else if (j["type"] == "structure") {
4✔
884
            // Deserialize structure type
885
            assert(j.contains("name"));
1✔
886
            std::string name = j["name"];
1✔
887
            assert(j.contains("address_space"));
1✔
888
            uint address_space = j["address_space"];
1✔
889
            assert(j.contains("initializer"));
1✔
890
            std::string initializer = j["initializer"];
1✔
891
            assert(j.contains("device_location"));
1✔
892
            types::DeviceLocation device_location = j["device_location"];
1✔
893
            return std::make_unique<types::Structure>(name, device_location, address_space,
1✔
894
                                                      initializer);
895
        } else if (j["type"] == "function") {
2✔
896
            // Deserialize function type
897
            assert(j.contains("return_type"));
1✔
898
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
899
            assert(j.contains("params"));
1✔
900
            std::vector<std::unique_ptr<types::IType>> params;
1✔
901
            for (const auto& param : j["params"]) {
3✔
902
                params.push_back(json_to_type(param));
2✔
903
            }
904
            assert(j.contains("is_var_arg"));
1✔
905
            bool is_var_arg = j["is_var_arg"];
1✔
906
            assert(j.contains("address_space"));
1✔
907
            uint address_space = j["address_space"];
1✔
908
            assert(j.contains("initializer"));
1✔
909
            std::string initializer = j["initializer"];
1✔
910
            assert(j.contains("device_location"));
1✔
911
            types::DeviceLocation device_location = j["device_location"];
1✔
912
            auto function = std::make_unique<types::Function>(
1✔
913
                *return_type, is_var_arg, device_location, address_space, initializer);
1✔
914
            for (const auto& param : params) {
3✔
915
                function->add_param(*param);
2✔
916
            }
917
            return function->clone();
1✔
918

919
        } else {
1✔
920
            throw std::runtime_error("Unknown type");
×
921
        }
922
    } else {
923
        throw std::runtime_error("Type not found");
×
924
    }
925
}
31✔
926

927
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
29✔
928
    JSONSymbolicPrinter printer;
29✔
929
    return printer.apply(expr);
29✔
930
};
29✔
931

932
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
933
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
934
    str_ = parenthesize(str_);
×
935
};
×
936

937
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
938
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
939
    str_ = parenthesize(str_);
×
940
};
×
941

942
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
943
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
944
    str_ = parenthesize(str_);
×
945
};
×
946

947
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
2✔
948
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
2✔
949
    str_ = parenthesize(str_);
2✔
950
};
2✔
951

952
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
953
    std::ostringstream s;
×
954
    auto container = x.get_args();
×
955
    if (container.size() == 1) {
×
956
        s << apply(*container.begin());
×
957
    } else {
×
958
        s << "min(";
×
959
        s << apply(*container.begin());
×
960

961
        // Recursively apply __daisy_min to the arguments
962
        SymEngine::vec_basic subargs;
×
963
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
964
            subargs.push_back(*it);
×
965
        }
×
966
        auto submin = SymEngine::min(subargs);
×
967
        s << ", " << apply(submin);
×
968

969
        s << ")";
×
970
    }
×
971

972
    str_ = s.str();
×
973
};
×
974

975
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
976
    std::ostringstream s;
×
977
    auto container = x.get_args();
×
978
    if (container.size() == 1) {
×
979
        s << apply(*container.begin());
×
980
    } else {
×
981
        s << "max(";
×
982
        s << apply(*container.begin());
×
983

984
        // Recursively apply __daisy_max to the arguments
985
        SymEngine::vec_basic subargs;
×
986
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
987
            subargs.push_back(*it);
×
988
        }
×
989
        auto submax = SymEngine::max(subargs);
×
990
        s << ", " << apply(submax);
×
991

992
        s << ")";
×
993
    }
×
994

995
    str_ = s.str();
×
996
};
×
997

998
}  // namespace serializer
999
}  // 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