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

daisytuner / sdfglib / 17594101385

09 Sep 2025 07:56PM UTC coverage: 61.049% (+1.9%) from 59.145%
17594101385

Pull #219

github

web-flow
Merge b4728128a into b8fdeb232
Pull Request #219: stdlib Library Nodes and ConstantNodes

427 of 1379 new or added lines in 74 files covered. (30.96%)

93 existing lines in 35 files now uncovered.

9310 of 15250 relevant lines covered (61.05%)

108.9 hits per line

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

81.42
/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/data_flow/library_nodes/barrier_local_node.h"
10
#include "sdfg/data_flow/library_nodes/call_node.h"
11
#include "sdfg/data_flow/library_nodes/math/math.h"
12
#include "sdfg/data_flow/library_nodes/metadata_node.h"
13
#include "sdfg/data_flow/library_nodes/stdlib/stdlib.h"
14

15
#include "sdfg/builder/structured_sdfg_builder.h"
16
#include "sdfg/data_flow/library_node.h"
17
#include "sdfg/element.h"
18
#include "sdfg/structured_control_flow/block.h"
19
#include "sdfg/structured_control_flow/for.h"
20
#include "sdfg/structured_control_flow/if_else.h"
21
#include "sdfg/structured_control_flow/map.h"
22
#include "sdfg/structured_control_flow/return.h"
23
#include "sdfg/structured_control_flow/sequence.h"
24
#include "sdfg/structured_control_flow/while.h"
25
#include "sdfg/structured_sdfg.h"
26
#include "sdfg/symbolic/symbolic.h"
27
#include "sdfg/types/function.h"
28
#include "sdfg/types/scalar.h"
29
#include "sdfg/types/type.h"
30
#include "symengine/expression.h"
31
#include "symengine/logic.h"
32
#include "symengine/symengine_rcp.h"
33

34
namespace sdfg {
35
namespace serializer {
36

37
FunctionType function_type_from_string(const std::string& str) {
5✔
38
    if (str == FunctionType_CPU.value()) {
5✔
39
        return FunctionType_CPU;
5✔
40
    } else if (str == FunctionType_NV_GLOBAL.value()) {
×
41
        return FunctionType_NV_GLOBAL;
×
42
    }
43

44
    return FunctionType(str);
×
45
}
5✔
46

47
types::StorageType storage_type_from_string(const std::string& str) {
46✔
48
    if (str == types::StorageType_CPU_Heap.value()) {
46✔
49
        return types::StorageType_CPU_Heap;
×
50
    } else if (str == types::StorageType_CPU_Stack.value()) {
46✔
51
        return types::StorageType_CPU_Stack;
46✔
52
    } else if (str == types::StorageType_NV_Global.value()) {
×
53
        return types::StorageType_NV_Global;
×
54
    } else if (str == types::StorageType_NV_Shared.value()) {
×
55
        return types::StorageType_NV_Shared;
×
56
    } else if (str == types::StorageType_NV_Constant.value()) {
×
57
        return types::StorageType_NV_Constant;
×
58
    } else if (str == types::StorageType_NV_Generic.value()) {
×
59
        return types::StorageType_NV_Generic;
×
60
    }
61

62
    return types::StorageType(str);
×
63
}
46✔
64

65
/*
66
 * * JSONSerializer class
67
 * * Serialization logic
68
 */
69

70
nlohmann::json JSONSerializer::serialize(const sdfg::StructuredSDFG& sdfg) {
5✔
71
    nlohmann::json j;
5✔
72

73
    j["name"] = sdfg.name();
5✔
74
    j["element_counter"] = sdfg.element_counter();
5✔
75
    j["type"] = std::string(sdfg.type().value());
5✔
76

77
    nlohmann::json return_type_json;
5✔
78
    type_to_json(return_type_json, sdfg.return_type());
5✔
79
    j["return_type"] = return_type_json;
5✔
80

81
    j["structures"] = nlohmann::json::array();
5✔
82
    for (const auto& structure_name : sdfg.structures()) {
6✔
83
        const auto& structure = sdfg.structure(structure_name);
1✔
84
        nlohmann::json structure_json;
1✔
85
        structure_definition_to_json(structure_json, structure);
1✔
86
        j["structures"].push_back(structure_json);
1✔
87
    }
1✔
88

89
    j["containers"] = nlohmann::json::object();
5✔
90
    for (const auto& container : sdfg.containers()) {
17✔
91
        nlohmann::json desc;
12✔
92
        type_to_json(desc, sdfg.type(container));
12✔
93
        j["containers"][container] = desc;
12✔
94
    }
12✔
95

96
    j["arguments"] = nlohmann::json::array();
5✔
97
    for (const auto& argument : sdfg.arguments()) {
8✔
98
        j["arguments"].push_back(argument);
3✔
99
    }
100

101
    j["externals"] = nlohmann::json::array();
5✔
102
    for (const auto& external : sdfg.externals()) {
9✔
103
        nlohmann::json external_json;
4✔
104
        external_json["name"] = external;
4✔
105
        external_json["linkage_type"] = sdfg.linkage_type(external);
4✔
106
        j["externals"].push_back(external_json);
4✔
107
    }
4✔
108

109
    j["metadata"] = nlohmann::json::object();
5✔
110
    for (const auto& entry : sdfg.metadata()) {
6✔
111
        j["metadata"][entry.first] = entry.second;
1✔
112
    }
113

114
    // Walk the SDFG
115
    nlohmann::json root_json;
5✔
116
    sequence_to_json(root_json, sdfg.root());
5✔
117
    j["root"] = root_json;
5✔
118

119
    return j;
5✔
120
}
5✔
121

122
void JSONSerializer::dataflow_to_json(nlohmann::json& j, const data_flow::DataFlowGraph& dataflow) {
21✔
123
    j["type"] = "dataflow";
21✔
124
    j["nodes"] = nlohmann::json::array();
21✔
125
    j["edges"] = nlohmann::json::array();
21✔
126

127
    for (auto& node : dataflow.nodes()) {
41✔
128
        nlohmann::json node_json;
20✔
129
        node_json["element_id"] = node.element_id();
20✔
130

131
        node_json["debug_info"] = nlohmann::json::object();
20✔
132
        debug_info_to_json(node_json["debug_info"], node.debug_info());
20✔
133

134
        if (auto tasklet = dynamic_cast<const data_flow::Tasklet*>(&node)) {
20✔
135
            node_json["type"] = "tasklet";
5✔
136
            node_json["code"] = tasklet->code();
5✔
137
            node_json["inputs"] = nlohmann::json::array();
5✔
138
            for (auto& input : tasklet->inputs()) {
15✔
139
                node_json["inputs"].push_back(input);
10✔
140
            }
141
            node_json["output"] = tasklet->output();
5✔
142
        } else if (auto lib_node = dynamic_cast<const data_flow::LibraryNode*>(&node)) {
20✔
UNCOV
143
            node_json["type"] = "library_node";
×
144
            node_json["implementation_type"] = std::string(lib_node->implementation_type().value());
×
145
            auto serializer_fn =
146
                LibraryNodeSerializerRegistry::instance().get_library_node_serializer(lib_node->code().value());
×
147
            if (serializer_fn == nullptr) {
×
148
                throw std::runtime_error("Unknown library node code: " + std::string(lib_node->code().value()));
×
149
            }
150
            auto serializer = serializer_fn();
×
151
            auto lib_node_json = serializer->serialize(*lib_node);
×
152
            node_json.merge_patch(lib_node_json);
×
153
        } else if (auto code_node = dynamic_cast<const data_flow::ConstantNode*>(&node)) {
15✔
NEW
154
            node_json["type"] = "constant_node";
×
NEW
155
            node_json["data"] = code_node->data();
×
156

NEW
157
            nlohmann::json type_json;
×
NEW
158
            type_to_json(type_json, code_node->type());
×
NEW
159
            node_json["data_type"] = type_json;
×
160
        } else if (auto code_node = dynamic_cast<const data_flow::AccessNode*>(&node)) {
15✔
161
            node_json["type"] = "access_node";
15✔
162
            node_json["data"] = code_node->data();
15✔
163
        } else {
15✔
164
            throw std::runtime_error("Unknown node type");
×
165
        }
166

167
        j["nodes"].push_back(node_json);
20✔
168
    }
20✔
169

170
    for (auto& edge : dataflow.edges()) {
36✔
171
        nlohmann::json edge_json;
15✔
172
        edge_json["element_id"] = edge.element_id();
15✔
173

174
        edge_json["debug_info"] = nlohmann::json::object();
15✔
175
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
15✔
176

177
        edge_json["src"] = edge.src().element_id();
15✔
178
        edge_json["dst"] = edge.dst().element_id();
15✔
179

180
        edge_json["src_conn"] = edge.src_conn();
15✔
181
        edge_json["dst_conn"] = edge.dst_conn();
15✔
182

183
        edge_json["subset"] = nlohmann::json::array();
15✔
184
        for (auto& subset : edge.subset()) {
21✔
185
            edge_json["subset"].push_back(expression(subset));
6✔
186
        }
187

188
        nlohmann::json base_type_json;
15✔
189
        type_to_json(base_type_json, edge.base_type());
15✔
190
        edge_json["base_type"] = base_type_json;
15✔
191

192
        j["edges"].push_back(edge_json);
15✔
193
    }
15✔
194
}
21✔
195

196
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
19✔
197
    j["type"] = "block";
19✔
198
    j["element_id"] = block.element_id();
19✔
199

200
    j["debug_info"] = nlohmann::json::object();
19✔
201
    debug_info_to_json(j["debug_info"], block.debug_info());
19✔
202

203
    nlohmann::json dataflow_json;
19✔
204
    dataflow_to_json(dataflow_json, block.dataflow());
19✔
205
    j["dataflow"] = dataflow_json;
19✔
206
}
19✔
207

208
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
209
    j["type"] = "for";
2✔
210
    j["element_id"] = for_node.element_id();
2✔
211

212
    j["debug_info"] = nlohmann::json::object();
2✔
213
    debug_info_to_json(j["debug_info"], for_node.debug_info());
2✔
214

215
    j["indvar"] = expression(for_node.indvar());
2✔
216
    j["init"] = expression(for_node.init());
2✔
217
    j["condition"] = expression(for_node.condition());
2✔
218
    j["update"] = expression(for_node.update());
2✔
219

220
    nlohmann::json body_json;
2✔
221
    sequence_to_json(body_json, for_node.root());
2✔
222
    j["root"] = body_json;
2✔
223
}
2✔
224

225
void JSONSerializer::if_else_to_json(nlohmann::json& j, const structured_control_flow::IfElse& if_else_node) {
2✔
226
    j["type"] = "if_else";
2✔
227
    j["element_id"] = if_else_node.element_id();
2✔
228

229
    j["debug_info"] = nlohmann::json::object();
2✔
230
    debug_info_to_json(j["debug_info"], if_else_node.debug_info());
2✔
231

232
    j["branches"] = nlohmann::json::array();
2✔
233
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
234
        nlohmann::json branch_json;
4✔
235
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
236
        nlohmann::json body_json;
4✔
237
        sequence_to_json(body_json, if_else_node.at(i).first);
4✔
238
        branch_json["root"] = body_json;
4✔
239
        j["branches"].push_back(branch_json);
4✔
240
    }
4✔
241
}
2✔
242

243
void JSONSerializer::while_node_to_json(nlohmann::json& j, const structured_control_flow::While& while_node) {
5✔
244
    j["type"] = "while";
5✔
245
    j["element_id"] = while_node.element_id();
5✔
246

247
    j["debug_info"] = nlohmann::json::object();
5✔
248
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
249

250
    nlohmann::json body_json;
5✔
251
    sequence_to_json(body_json, while_node.root());
5✔
252
    j["root"] = body_json;
5✔
253
}
5✔
254

255
void JSONSerializer::break_node_to_json(nlohmann::json& j, const structured_control_flow::Break& break_node) {
2✔
256
    j["type"] = "break";
2✔
257
    j["element_id"] = break_node.element_id();
2✔
258

259
    j["debug_info"] = nlohmann::json::object();
2✔
260
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
261
}
2✔
262

263
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
264
    j["type"] = "continue";
2✔
265
    j["element_id"] = continue_node.element_id();
2✔
266

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

271
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
272
    j["type"] = "map";
2✔
273
    j["element_id"] = map_node.element_id();
2✔
274

275
    j["debug_info"] = nlohmann::json::object();
2✔
276
    debug_info_to_json(j["debug_info"], map_node.debug_info());
2✔
277

278
    j["indvar"] = expression(map_node.indvar());
2✔
279
    j["init"] = expression(map_node.init());
2✔
280
    j["condition"] = expression(map_node.condition());
2✔
281
    j["update"] = expression(map_node.update());
2✔
282

283
    j["schedule_type"] = nlohmann::json::object();
2✔
284
    schedule_type_to_json(j["schedule_type"], map_node.schedule_type());
2✔
285

286
    nlohmann::json body_json;
2✔
287
    sequence_to_json(body_json, map_node.root());
2✔
288
    j["root"] = body_json;
2✔
289
}
2✔
290

291
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
292
    j["type"] = "return";
2✔
293
    j["element_id"] = return_node.element_id();
2✔
294
    j["data"] = return_node.data();
2✔
295
    j["unreachable"] = return_node.unreachable();
2✔
296

297
    j["debug_info"] = nlohmann::json::object();
2✔
298
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
299
}
2✔
300

301
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
21✔
302
    j["type"] = "sequence";
21✔
303
    j["element_id"] = sequence.element_id();
21✔
304

305
    j["debug_info"] = nlohmann::json::object();
21✔
306
    debug_info_to_json(j["debug_info"], sequence.debug_info());
21✔
307

308
    j["children"] = nlohmann::json::array();
21✔
309
    j["transitions"] = nlohmann::json::array();
21✔
310

311
    for (size_t i = 0; i < sequence.size(); i++) {
42✔
312
        nlohmann::json child_json;
21✔
313
        auto& child = sequence.at(i).first;
21✔
314
        auto& transition = sequence.at(i).second;
21✔
315

316
        if (auto block = dynamic_cast<const structured_control_flow::Block*>(&child)) {
21✔
317
            block_to_json(child_json, *block);
17✔
318
        } else if (auto for_node = dynamic_cast<const structured_control_flow::For*>(&child)) {
21✔
319
            for_to_json(child_json, *for_node);
×
320
        } else if (auto sequence_node = dynamic_cast<const structured_control_flow::Sequence*>(&child)) {
4✔
321
            sequence_to_json(child_json, *sequence_node);
×
322
        } else if (auto condition_node = dynamic_cast<const structured_control_flow::IfElse*>(&child)) {
4✔
323
            if_else_to_json(child_json, *condition_node);
×
324
        } else if (auto while_node = dynamic_cast<const structured_control_flow::While*>(&child)) {
4✔
325
            while_node_to_json(child_json, *while_node);
×
326
        } else if (auto return_node = dynamic_cast<const structured_control_flow::Return*>(&child)) {
4✔
327
            return_node_to_json(child_json, *return_node);
×
328
        } else if (auto break_node = dynamic_cast<const structured_control_flow::Break*>(&child)) {
4✔
329
            break_node_to_json(child_json, *break_node);
2✔
330
        } else if (auto continue_node = dynamic_cast<const structured_control_flow::Continue*>(&child)) {
4✔
331
            continue_node_to_json(child_json, *continue_node);
2✔
332
        } else if (auto map_node = dynamic_cast<const structured_control_flow::Map*>(&child)) {
2✔
333
            map_to_json(child_json, *map_node);
×
334
        } else {
×
335
            throw std::runtime_error("Unknown child type");
×
336
        }
337

338
        j["children"].push_back(child_json);
21✔
339

340
        // Add transition information
341
        nlohmann::json transition_json;
21✔
342
        transition_json["type"] = "transition";
21✔
343
        transition_json["element_id"] = transition.element_id();
21✔
344

345
        transition_json["debug_info"] = nlohmann::json::object();
21✔
346
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
21✔
347

348
        transition_json["assignments"] = nlohmann::json::array();
21✔
349
        for (const auto& assignment : transition.assignments()) {
24✔
350
            nlohmann::json assignment_json;
3✔
351
            assignment_json["symbol"] = expression(assignment.first);
3✔
352
            assignment_json["expression"] = expression(assignment.second);
3✔
353
            transition_json["assignments"].push_back(assignment_json);
3✔
354
        }
3✔
355

356
        j["transitions"].push_back(transition_json);
21✔
357
    }
21✔
358
}
21✔
359

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

412
void JSONSerializer::structure_definition_to_json(nlohmann::json& j, const types::StructureDefinition& definition) {
2✔
413
    j["name"] = definition.name();
2✔
414
    j["members"] = nlohmann::json::array();
2✔
415
    for (size_t i = 0; i < definition.num_members(); i++) {
4✔
416
        nlohmann::json member_json;
2✔
417
        type_to_json(member_json, definition.member_type(symbolic::integer(i)));
2✔
418
        j["members"].push_back(member_json);
2✔
419
    }
2✔
420
    j["is_packed"] = definition.is_packed();
2✔
421
}
2✔
422

423
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
113✔
424
    j["has"] = debug_info.has();
113✔
425
    j["filename"] = debug_info.filename();
113✔
426
    j["function"] = debug_info.function();
113✔
427
    j["start_line"] = debug_info.start_line();
113✔
428
    j["start_column"] = debug_info.start_column();
113✔
429
    j["end_line"] = debug_info.end_line();
113✔
430
    j["end_column"] = debug_info.end_column();
113✔
431
}
113✔
432

433
void JSONSerializer::schedule_type_to_json(nlohmann::json& j, const ScheduleType& schedule_type) {
3✔
434
    j["value"] = schedule_type.value();
3✔
435
    j["properties"] = nlohmann::json::object();
3✔
436
    for (const auto& prop : schedule_type.properties()) {
4✔
437
        j["properties"][prop.first] = prop.second;
1✔
438
    }
439
}
3✔
440

441
/*
442
 * * Deserialization logic
443
 */
444

445
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
5✔
446
    assert(j.contains("name"));
5✔
447
    assert(j["name"].is_string());
5✔
448
    assert(j.contains("type"));
5✔
449
    assert(j["type"].is_string());
5✔
450
    assert(j.contains("element_counter"));
5✔
451
    assert(j["element_counter"].is_number_integer());
5✔
452
    assert(j.contains("return_type"));
5✔
453
    assert(j["return_type"].is_object());
5✔
454

455
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
5✔
456
    auto return_type = json_to_type(j["return_type"]);
5✔
457

458
    builder::StructuredSDFGBuilder builder(j["name"], function_type, *return_type);
5✔
459

460
    size_t element_counter = j["element_counter"];
5✔
461
    builder.set_element_counter(element_counter);
5✔
462

463
    // deserialize structures
464
    assert(j.contains("structures"));
5✔
465
    assert(j["structures"].is_array());
5✔
466
    for (const auto& structure : j["structures"]) {
6✔
467
        assert(structure.contains("name"));
1✔
468
        assert(structure["name"].is_string());
1✔
469
        json_to_structure_definition(structure, builder);
1✔
470
    }
471

472
    nlohmann::json& containers = j["containers"];
5✔
473

474
    // deserialize externals
475
    for (const auto& external : j["externals"]) {
9✔
476
        assert(external.contains("name"));
4✔
477
        assert(external["name"].is_string());
4✔
478
        assert(external.contains("linkage_type"));
4✔
479
        assert(external["linkage_type"].is_number_integer());
4✔
480
        auto& type_desc = containers.at(external["name"].get<std::string>());
4✔
481
        auto type = json_to_type(type_desc);
4✔
482
        builder.add_external(external["name"], *type, LinkageType(external["linkage_type"]));
4✔
483
    }
4✔
484

485
    // deserialize arguments
486
    for (const auto& name : j["arguments"]) {
8✔
487
        auto& type_desc = containers.at(name.get<std::string>());
3✔
488
        auto type = json_to_type(type_desc);
3✔
489
        builder.add_container(name, *type, true, false);
3✔
490
    }
3✔
491

492
    // deserialize transients
493
    for (const auto& entry : containers.items()) {
17✔
494
        if (builder.subject().is_argument(entry.key())) {
12✔
495
            continue;
3✔
496
        }
497
        if (builder.subject().is_external(entry.key())) {
9✔
498
            continue;
4✔
499
        }
500
        auto type = json_to_type(entry.value());
5✔
501
        builder.add_container(entry.key(), *type, false, false);
5✔
502
    }
5✔
503

504
    // deserialize root node
505
    assert(j.contains("root"));
5✔
506
    auto& root = builder.subject().root();
5✔
507
    json_to_sequence(j["root"], builder, root);
5✔
508

509
    // deserialize metadata
510
    assert(j.contains("metadata"));
5✔
511
    assert(j["metadata"].is_object());
5✔
512
    for (const auto& entry : j["metadata"].items()) {
6✔
513
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
514
    }
515

516
    builder.set_element_counter(element_counter);
5✔
517

518
    return builder.move();
5✔
519
}
5✔
520

521
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
2✔
522
    assert(j.contains("name"));
2✔
523
    assert(j["name"].is_string());
2✔
524
    assert(j.contains("members"));
2✔
525
    assert(j["members"].is_array());
2✔
526
    assert(j.contains("is_packed"));
2✔
527
    assert(j["is_packed"].is_boolean());
2✔
528
    auto is_packed = j["is_packed"];
2✔
529
    auto& definition = builder.add_structure(j["name"], is_packed);
2✔
530
    for (const auto& member : j["members"]) {
4✔
531
        nlohmann::json member_json;
2✔
532
        auto member_type = json_to_type(member);
2✔
533
        definition.add_member(*member_type);
2✔
534
    }
2✔
535
}
2✔
536

537
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
×
538
    std::vector<std::pair<std::string, types::Scalar>> arguments;
×
539
    for (const auto& argument : j) {
×
540
        assert(argument.contains("name"));
×
541
        assert(argument["name"].is_string());
×
542
        assert(argument.contains("type"));
×
543
        assert(argument["type"].is_object());
×
544
        std::string name = argument["name"];
×
545
        auto type = json_to_type(argument["type"]);
×
546
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
×
547
    }
×
548
    return arguments;
×
549
}
×
550

551
void JSONSerializer::json_to_dataflow(
11✔
552
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
553
) {
554
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
11✔
555

556
    assert(j.contains("nodes"));
11✔
557
    assert(j["nodes"].is_array());
11✔
558
    for (const auto& node : j["nodes"]) {
27✔
559
        assert(node.contains("type"));
16✔
560
        assert(node["type"].is_string());
16✔
561
        assert(node.contains("element_id"));
16✔
562
        assert(node["element_id"].is_number_integer());
16✔
563
        std::string type = node["type"];
16✔
564
        if (type == "tasklet") {
16✔
565
            assert(node.contains("code"));
4✔
566
            assert(node["code"].is_number_integer());
4✔
567
            assert(node.contains("inputs"));
4✔
568
            assert(node["inputs"].is_array());
4✔
569
            assert(node.contains("output"));
4✔
570
            assert(node["output"].is_string());
4✔
571
            auto inputs = node["inputs"].get<std::vector<std::string>>();
4✔
572

573
            auto& tasklet =
4✔
574
                builder
8✔
575
                    .add_tasklet(parent, node["code"], node["output"], inputs, json_to_debug_info(node["debug_info"]));
4✔
576
            tasklet.element_id_ = node["element_id"];
4✔
577
            nodes_map.insert({node["element_id"], tasklet});
4✔
578
        } else if (type == "library_node") {
16✔
579
            assert(node.contains("code"));
×
580
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
581

582
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
583
            if (serializer_fn == nullptr) {
×
584
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
585
            }
586
            auto serializer = serializer_fn();
×
587
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
588
            lib_node.implementation_type() =
×
589
                data_flow::ImplementationType(node["implementation_type"].get<std::string>());
×
590
            lib_node.element_id_ = node["element_id"];
×
591
            nodes_map.insert({node["element_id"], lib_node});
×
592
        } else if (type == "access_node") {
12✔
593
            assert(node.contains("data"));
12✔
594
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
595
            access_node.element_id_ = node["element_id"];
12✔
596
            nodes_map.insert({node["element_id"], access_node});
12✔
597
        } else if (type == "constant_node") {
12✔
NEW
598
            assert(node.contains("data"));
×
NEW
599
            assert(node.contains("data_type"));
×
600

NEW
601
            auto type = json_to_type(node["data_type"]);
×
602

NEW
603
            auto& constant_node =
×
NEW
604
                builder.add_constant(parent, node["data"], *type, json_to_debug_info(node["debug_info"]));
×
NEW
605
            constant_node.element_id_ = node["element_id"];
×
NEW
606
            nodes_map.insert({node["element_id"], constant_node});
×
607
        } else {
×
608
            throw std::runtime_error("Unknown node type");
×
609
        }
610
    }
16✔
611

612
    assert(j.contains("edges"));
11✔
613
    assert(j["edges"].is_array());
11✔
614
    for (const auto& edge : j["edges"]) {
23✔
615
        assert(edge.contains("src"));
12✔
616
        assert(edge["src"].is_number_integer());
12✔
617
        assert(edge.contains("dst"));
12✔
618
        assert(edge["dst"].is_number_integer());
12✔
619
        assert(edge.contains("src_conn"));
12✔
620
        assert(edge["src_conn"].is_string());
12✔
621
        assert(edge.contains("dst_conn"));
12✔
622
        assert(edge["dst_conn"].is_string());
12✔
623
        assert(edge.contains("subset"));
12✔
624
        assert(edge["subset"].is_array());
12✔
625

626
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
627
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
628
        auto& source = nodes_map.at(edge["src"]);
12✔
629
        auto& target = nodes_map.at(edge["dst"]);
12✔
630

631
        auto base_type = json_to_type(edge["base_type"]);
12✔
632

633
        assert(edge.contains("subset"));
12✔
634
        assert(edge["subset"].is_array());
12✔
635
        std::vector<symbolic::Expression> subset;
12✔
636
        for (const auto& subset_str : edge["subset"]) {
16✔
637
            assert(subset_str.is_string());
4✔
638
            SymEngine::Expression subset_expr(subset_str);
4✔
639
            subset.push_back(subset_expr);
4✔
640
        }
4✔
641
        auto& memlet = builder.add_memlet(
24✔
642
            parent,
12✔
643
            source,
12✔
644
            edge["src_conn"],
12✔
645
            target,
12✔
646
            edge["dst_conn"],
12✔
647
            subset,
648
            *base_type,
12✔
649
            json_to_debug_info(edge["debug_info"])
12✔
650
        );
651
        memlet.element_id_ = edge["element_id"];
12✔
652
    }
12✔
653
}
11✔
654

655
void JSONSerializer::json_to_sequence(
14✔
656
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
657
) {
658
    assert(j.contains("type"));
14✔
659
    assert(j["type"].is_string());
14✔
660
    assert(j.contains("children"));
14✔
661
    assert(j["children"].is_array());
14✔
662
    assert(j.contains("transitions"));
14✔
663
    assert(j["transitions"].is_array());
14✔
664
    assert(j["transitions"].size() == j["children"].size());
14✔
665

666
    sequence.element_id_ = j["element_id"];
14✔
667
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
14✔
668

669
    std::string type = j["type"];
14✔
670
    if (type == "sequence") {
14✔
671
        for (size_t i = 0; i < j["children"].size(); i++) {
25✔
672
            auto& child = j["children"][i];
11✔
673
            auto& transition = j["transitions"][i];
11✔
674
            assert(child.contains("type"));
11✔
675
            assert(child["type"].is_string());
11✔
676

677
            assert(transition.contains("type"));
11✔
678
            assert(transition["type"].is_string());
11✔
679
            assert(transition.contains("assignments"));
11✔
680
            assert(transition["assignments"].is_array());
11✔
681
            control_flow::Assignments assignments;
11✔
682
            for (const auto& assignment : transition["assignments"]) {
13✔
683
                assert(assignment.contains("symbol"));
2✔
684
                assert(assignment["symbol"].is_string());
2✔
685
                assert(assignment.contains("expression"));
2✔
686
                assert(assignment["expression"].is_string());
2✔
687
                SymEngine::Expression expr(assignment["expression"]);
2✔
688
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
2✔
689
            }
2✔
690

691
            if (child["type"] == "block") {
11✔
692
                json_to_block_node(child, builder, sequence, assignments);
9✔
693
            } else if (child["type"] == "for") {
11✔
694
                json_to_for_node(child, builder, sequence, assignments);
×
695
            } else if (child["type"] == "if_else") {
2✔
696
                json_to_if_else_node(child, builder, sequence, assignments);
×
697
            } else if (child["type"] == "while") {
2✔
698
                json_to_while_node(child, builder, sequence, assignments);
×
699
            } else if (child["type"] == "break") {
2✔
700
                json_to_break_node(child, builder, sequence, assignments);
1✔
701
            } else if (child["type"] == "continue") {
2✔
702
                json_to_continue_node(child, builder, sequence, assignments);
1✔
703
            } else if (child["type"] == "return") {
1✔
704
                json_to_return_node(child, builder, sequence, assignments);
×
705
            } else if (child["type"] == "map") {
×
706
                json_to_map_node(child, builder, sequence, assignments);
×
707
            } else if (child["type"] == "sequence") {
×
708
                auto& subseq = builder.add_sequence(sequence, assignments, json_to_debug_info(child["debug_info"]));
×
709
                json_to_sequence(child, builder, subseq);
×
710
            } else {
×
711
                throw std::runtime_error("Unknown child type");
×
712
            }
713

714
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
11✔
715
            sequence.at(i).second.element_id_ = transition["element_id"];
11✔
716
        }
11✔
717
    } else {
14✔
718
        throw std::runtime_error("expected sequence type");
×
719
    }
720
}
14✔
721

722
void JSONSerializer::json_to_block_node(
10✔
723
    const nlohmann::json& j,
724
    builder::StructuredSDFGBuilder& builder,
725
    structured_control_flow::Sequence& parent,
726
    control_flow::Assignments& assignments
727
) {
728
    assert(j.contains("type"));
10✔
729
    assert(j["type"].is_string());
10✔
730
    assert(j.contains("dataflow"));
10✔
731
    assert(j["dataflow"].is_object());
10✔
732
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
10✔
733
    block.element_id_ = j["element_id"];
10✔
734
    assert(j["dataflow"].contains("type"));
10✔
735
    assert(j["dataflow"]["type"].is_string());
10✔
736
    std::string type = j["dataflow"]["type"];
10✔
737
    if (type == "dataflow") {
10✔
738
        json_to_dataflow(j["dataflow"], builder, block);
10✔
739
    } else {
10✔
740
        throw std::runtime_error("Unknown dataflow type");
×
741
    }
742
}
10✔
743

744
void JSONSerializer::json_to_for_node(
1✔
745
    const nlohmann::json& j,
746
    builder::StructuredSDFGBuilder& builder,
747
    structured_control_flow::Sequence& parent,
748
    control_flow::Assignments& assignments
749
) {
750
    assert(j.contains("type"));
1✔
751
    assert(j["type"].is_string());
1✔
752
    assert(j.contains("indvar"));
1✔
753
    assert(j["indvar"].is_string());
1✔
754
    assert(j.contains("init"));
1✔
755
    assert(j["init"].is_string());
1✔
756
    assert(j.contains("condition"));
1✔
757
    assert(j["condition"].is_string());
1✔
758
    assert(j.contains("update"));
1✔
759
    assert(j["update"].is_string());
1✔
760
    assert(j.contains("root"));
1✔
761
    assert(j["root"].is_object());
1✔
762

763
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
764
    SymEngine::Expression init(j["init"]);
1✔
765
    SymEngine::Expression condition_expr(j["condition"]);
1✔
766
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
767
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
768
    SymEngine::Expression update(j["update"]);
1✔
769
    auto& for_node =
1✔
770
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
1✔
771
    for_node.element_id_ = j["element_id"];
1✔
772

773
    assert(j["root"].contains("type"));
1✔
774
    assert(j["root"]["type"].is_string());
1✔
775
    assert(j["root"]["type"] == "sequence");
1✔
776
    json_to_sequence(j["root"], builder, for_node.root());
1✔
777
}
1✔
778

779
void JSONSerializer::json_to_if_else_node(
1✔
780
    const nlohmann::json& j,
781
    builder::StructuredSDFGBuilder& builder,
782
    structured_control_flow::Sequence& parent,
783
    control_flow::Assignments& assignments
784
) {
785
    assert(j.contains("type"));
1✔
786
    assert(j["type"].is_string());
1✔
787
    assert(j["type"] == "if_else");
1✔
788
    assert(j.contains("branches"));
1✔
789
    assert(j["branches"].is_array());
1✔
790
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
791
    if_else_node.element_id_ = j["element_id"];
1✔
792
    for (const auto& branch : j["branches"]) {
3✔
793
        assert(branch.contains("condition"));
2✔
794
        assert(branch["condition"].is_string());
2✔
795
        assert(branch.contains("root"));
2✔
796
        assert(branch["root"].is_object());
2✔
797
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
798
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
2✔
799
        symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()
2✔
800
        );
801
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
802
        assert(branch["root"].contains("type"));
2✔
803
        assert(branch["root"]["type"].is_string());
2✔
804
        std::string type = branch["root"]["type"];
2✔
805
        if (type == "sequence") {
2✔
806
            json_to_sequence(branch["root"], builder, branch_node);
2✔
807
        } else {
2✔
808
            throw std::runtime_error("Unknown child type");
×
809
        }
810
    }
2✔
811
}
1✔
812

813
void JSONSerializer::json_to_while_node(
3✔
814
    const nlohmann::json& j,
815
    builder::StructuredSDFGBuilder& builder,
816
    structured_control_flow::Sequence& parent,
817
    control_flow::Assignments& assignments
818
) {
819
    assert(j.contains("type"));
3✔
820
    assert(j["type"].is_string());
3✔
821
    assert(j["type"] == "while");
3✔
822
    assert(j.contains("root"));
3✔
823
    assert(j["root"].is_object());
3✔
824

825
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
826
    while_node.element_id_ = j["element_id"];
3✔
827

828
    assert(j["root"]["type"] == "sequence");
3✔
829
    json_to_sequence(j["root"], builder, while_node.root());
3✔
830
}
3✔
831

832
void JSONSerializer::json_to_break_node(
1✔
833
    const nlohmann::json& j,
834
    builder::StructuredSDFGBuilder& builder,
835
    structured_control_flow::Sequence& parent,
836
    control_flow::Assignments& assignments
837
) {
838
    assert(j.contains("type"));
1✔
839
    assert(j["type"].is_string());
1✔
840
    assert(j["type"] == "break");
1✔
841
    auto& node = builder.add_break(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
842
    node.element_id_ = j["element_id"];
1✔
843
}
1✔
844

845
void JSONSerializer::json_to_continue_node(
1✔
846
    const nlohmann::json& j,
847
    builder::StructuredSDFGBuilder& builder,
848
    structured_control_flow::Sequence& parent,
849
    control_flow::Assignments& assignments
850
) {
851
    assert(j.contains("type"));
1✔
852
    assert(j["type"].is_string());
1✔
853
    assert(j["type"] == "continue");
1✔
854
    auto& node = builder.add_continue(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
855
    node.element_id_ = j["element_id"];
1✔
856
}
1✔
857

858
void JSONSerializer::json_to_map_node(
1✔
859
    const nlohmann::json& j,
860
    builder::StructuredSDFGBuilder& builder,
861
    structured_control_flow::Sequence& parent,
862
    control_flow::Assignments& assignments
863
) {
864
    assert(j.contains("type"));
1✔
865
    assert(j["type"].is_string());
1✔
866
    assert(j["type"] == "map");
1✔
867
    assert(j.contains("indvar"));
1✔
868
    assert(j["indvar"].is_string());
1✔
869
    assert(j.contains("init"));
1✔
870
    assert(j["init"].is_string());
1✔
871
    assert(j.contains("condition"));
1✔
872
    assert(j["condition"].is_string());
1✔
873
    assert(j.contains("update"));
1✔
874
    assert(j["update"].is_string());
1✔
875
    assert(j.contains("root"));
1✔
876
    assert(j["root"].is_object());
1✔
877
    assert(j.contains("schedule_type"));
1✔
878
    assert(j["schedule_type"].is_object());
1✔
879

880
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
881

882
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
883
    SymEngine::Expression init(j["init"]);
1✔
884
    SymEngine::Expression condition_expr(j["condition"]);
1✔
885
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
886
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
887
    SymEngine::Expression update(j["update"]);
1✔
888

889
    auto& map_node = builder.add_map(
2✔
890
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
891
    );
892
    map_node.element_id_ = j["element_id"];
1✔
893

894
    assert(j["root"].contains("type"));
1✔
895
    assert(j["root"]["type"].is_string());
1✔
896
    assert(j["root"]["type"] == "sequence");
1✔
897
    json_to_sequence(j["root"], builder, map_node.root());
1✔
898
}
1✔
899

900
void JSONSerializer::json_to_return_node(
1✔
901
    const nlohmann::json& j,
902
    builder::StructuredSDFGBuilder& builder,
903
    structured_control_flow::Sequence& parent,
904
    control_flow::Assignments& assignments
905
) {
906
    assert(j.contains("type"));
1✔
907
    assert(j["type"].is_string());
1✔
908
    assert(j["type"] == "return");
1✔
909
    assert(j.contains("data"));
1✔
910
    assert(j["data"].is_string());
1✔
911
    assert(j.contains("unreachable"));
1✔
912
    assert(j["unreachable"].is_boolean());
1✔
913

914
    std::string data = j["data"];
1✔
915
    bool unreachable = j["unreachable"];
1✔
916
    auto& node = builder.add_return(parent, data, unreachable, assignments, json_to_debug_info(j["debug_info"]));
1✔
917
    node.element_id_ = j["element_id"];
1✔
918
}
1✔
919

920
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
46✔
921
    if (j.contains("type")) {
46✔
922
        if (j["type"] == "scalar") {
46✔
923
            // Deserialize scalar type
924
            assert(j.contains("primitive_type"));
35✔
925
            types::PrimitiveType primitive_type = j["primitive_type"];
35✔
926
            assert(j.contains("storage_type"));
35✔
927
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
35✔
928
            assert(j.contains("initializer"));
35✔
929
            std::string initializer = j["initializer"];
35✔
930
            assert(j.contains("alignment"));
35✔
931
            size_t alignment = j["alignment"];
35✔
932
            return std::make_unique<types::Scalar>(storage_type, alignment, initializer, primitive_type);
35✔
933
        } else if (j["type"] == "array") {
46✔
934
            // Deserialize array type
935
            assert(j.contains("element_type"));
2✔
936
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
937
            assert(j.contains("num_elements"));
2✔
938
            std::string num_elements_str = j["num_elements"];
2✔
939
            // Convert num_elements_str to symbolic::Expression
940
            SymEngine::Expression num_elements(num_elements_str);
2✔
941
            assert(j.contains("storage_type"));
2✔
942
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
943
            assert(j.contains("initializer"));
2✔
944
            std::string initializer = j["initializer"];
2✔
945
            assert(j.contains("alignment"));
2✔
946
            size_t alignment = j["alignment"];
2✔
947
            return std::make_unique<types::Array>(storage_type, alignment, initializer, *member_type, num_elements);
2✔
948
        } else if (j["type"] == "pointer") {
11✔
949
            // Deserialize pointer type
950
            std::optional<std::unique_ptr<types::IType>> pointee_type;
6✔
951
            if (j.contains("pointee_type")) {
6✔
952
                assert(j.contains("pointee_type"));
5✔
953
                pointee_type = json_to_type(j["pointee_type"]);
5✔
954
            } else {
5✔
955
                pointee_type = std::nullopt;
1✔
956
            }
957
            assert(j.contains("storage_type"));
6✔
958
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
6✔
959
            assert(j.contains("initializer"));
6✔
960
            std::string initializer = j["initializer"];
6✔
961
            assert(j.contains("alignment"));
6✔
962
            size_t alignment = j["alignment"];
6✔
963
            if (pointee_type.has_value()) {
6✔
964
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer, *pointee_type.value());
5✔
965
            } else {
966
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer);
1✔
967
            }
968
        } else if (j["type"] == "structure") {
9✔
969
            // Deserialize structure type
970
            assert(j.contains("name"));
2✔
971
            std::string name = j["name"];
2✔
972
            assert(j.contains("storage_type"));
2✔
973
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
974
            assert(j.contains("initializer"));
2✔
975
            std::string initializer = j["initializer"];
2✔
976
            assert(j.contains("alignment"));
2✔
977
            size_t alignment = j["alignment"];
2✔
978
            return std::make_unique<types::Structure>(storage_type, alignment, initializer, name);
2✔
979
        } else if (j["type"] == "function") {
3✔
980
            // Deserialize function type
981
            assert(j.contains("return_type"));
1✔
982
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
983
            assert(j.contains("params"));
1✔
984
            std::vector<std::unique_ptr<types::IType>> params;
1✔
985
            for (const auto& param : j["params"]) {
3✔
986
                params.push_back(json_to_type(param));
2✔
987
            }
988
            assert(j.contains("is_var_arg"));
1✔
989
            bool is_var_arg = j["is_var_arg"];
1✔
990
            assert(j.contains("storage_type"));
1✔
991
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
1✔
992
            assert(j.contains("initializer"));
1✔
993
            std::string initializer = j["initializer"];
1✔
994
            assert(j.contains("alignment"));
1✔
995
            size_t alignment = j["alignment"];
1✔
996
            auto function =
997
                std::make_unique<types::Function>(storage_type, alignment, initializer, *return_type, is_var_arg);
1✔
998
            for (const auto& param : params) {
3✔
999
                function->add_param(*param);
2✔
1000
            }
1001
            return function->clone();
1✔
1002

1003
        } else {
1✔
1004
            throw std::runtime_error("Unknown type");
×
1005
        }
1006
    } else {
1007
        throw std::runtime_error("Type not found");
×
1008
    }
1009
}
46✔
1010

1011
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
72✔
1012
    assert(j.contains("has"));
72✔
1013
    assert(j["has"].is_boolean());
72✔
1014
    if (!j["has"]) {
72✔
1015
        return DebugInfo();
72✔
1016
    }
1017
    assert(j.contains("filename"));
×
1018
    assert(j["filename"].is_string());
×
1019
    std::string filename = j["filename"];
×
1020
    assert(j.contains("function"));
×
1021
    assert(j["function"].is_string());
×
1022
    std::string function = j["function"];
×
1023
    assert(j.contains("start_line"));
×
1024
    assert(j["start_line"].is_number_integer());
×
1025
    size_t start_line = j["start_line"];
×
1026
    assert(j.contains("start_column"));
×
1027
    assert(j["start_column"].is_number_integer());
×
1028
    size_t start_column = j["start_column"];
×
1029
    assert(j.contains("end_line"));
×
1030
    assert(j["end_line"].is_number_integer());
×
1031
    size_t end_line = j["end_line"];
×
1032
    assert(j.contains("end_column"));
×
1033
    assert(j["end_column"].is_number_integer());
×
1034
    size_t end_column = j["end_column"];
×
1035
    return DebugInfo(filename, function, start_line, start_column, end_line, end_column);
×
1036
}
72✔
1037

1038
ScheduleType JSONSerializer::json_to_schedule_type(const nlohmann::json& j) {
2✔
1039
    assert(j.contains("value"));
2✔
1040
    assert(j["value"].is_string());
2✔
1041
    assert(j.contains("properties"));
2✔
1042
    assert(j["properties"].is_object());
2✔
1043
    ScheduleType schedule_type(j["value"].get<std::string>());
2✔
1044
    for (const auto& [key, value] : j["properties"].items()) {
4✔
1045
        assert(value.is_string());
1✔
1046
        schedule_type.set_property(key, value.get<std::string>());
1✔
1047
    }
1048
    return schedule_type;
2✔
1049
}
2✔
1050

1051
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
36✔
1052
    JSONSymbolicPrinter printer;
36✔
1053
    return printer.apply(expr);
36✔
1054
};
36✔
1055

1056
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1057
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1058
    str_ = parenthesize(str_);
×
1059
};
×
1060

1061
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1062
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1063
    str_ = parenthesize(str_);
×
1064
};
×
1065

1066
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1067
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1068
    str_ = parenthesize(str_);
×
1069
};
×
1070

1071
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1072
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1073
    str_ = parenthesize(str_);
4✔
1074
};
4✔
1075

1076
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1077
    std::ostringstream s;
×
1078
    auto container = x.get_args();
×
1079
    if (container.size() == 1) {
×
1080
        s << apply(*container.begin());
×
1081
    } else {
×
1082
        s << "min(";
×
1083
        s << apply(*container.begin());
×
1084

1085
        // Recursively apply __daisy_min to the arguments
1086
        SymEngine::vec_basic subargs;
×
1087
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1088
            subargs.push_back(*it);
×
1089
        }
×
1090
        auto submin = SymEngine::min(subargs);
×
1091
        s << ", " << apply(submin);
×
1092

1093
        s << ")";
×
1094
    }
×
1095

1096
    str_ = s.str();
×
1097
};
×
1098

1099
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1100
    std::ostringstream s;
×
1101
    auto container = x.get_args();
×
1102
    if (container.size() == 1) {
×
1103
        s << apply(*container.begin());
×
1104
    } else {
×
1105
        s << "max(";
×
1106
        s << apply(*container.begin());
×
1107

1108
        // Recursively apply __daisy_max to the arguments
1109
        SymEngine::vec_basic subargs;
×
1110
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1111
            subargs.push_back(*it);
×
1112
        }
×
1113
        auto submax = SymEngine::max(subargs);
×
1114
        s << ", " << apply(submax);
×
1115

1116
        s << ")";
×
1117
    }
×
1118

1119
    str_ = s.str();
×
1120
};
×
1121

1122
void LibraryNodeSerializerRegistry::
1123
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
26✔
1124
    std::lock_guard<std::mutex> lock(mutex_);
26✔
1125
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
26✔
1126
        throw std::runtime_error(
×
1127
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1128
        );
1129
    }
1130
    factory_map_[library_node_code] = std::move(fn);
26✔
1131
}
26✔
1132

1133
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1134
    auto it = factory_map_.find(library_node_code);
1✔
1135
    if (it != factory_map_.end()) {
1✔
1136
        return it->second;
1✔
1137
    }
1138
    return nullptr;
×
1139
}
1✔
1140

1141
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1142

1143
void register_default_serializers() {
2✔
1144
    // stdlib
1145
    LibraryNodeSerializerRegistry::instance()
2✔
1146
        .register_library_node_serializer(stdlib::LibraryNodeType_Calloc.value(), []() {
2✔
NEW
1147
            return std::make_unique<stdlib::CallocNodeSerializer>();
×
1148
        });
1149
    LibraryNodeSerializerRegistry::instance()
2✔
1150
        .register_library_node_serializer(stdlib::LibraryNodeType_Fprintf.value(), []() {
2✔
NEW
1151
            return std::make_unique<stdlib::FprintfNodeSerializer>();
×
1152
        });
1153
    LibraryNodeSerializerRegistry::instance()
2✔
1154
        .register_library_node_serializer(stdlib::LibraryNodeType_FPutc.value(), []() {
2✔
NEW
1155
            return std::make_unique<stdlib::FPutcNodeSerializer>();
×
1156
        });
1157
    LibraryNodeSerializerRegistry::instance()
2✔
1158
        .register_library_node_serializer(stdlib::LibraryNodeType_Free.value(), []() {
2✔
NEW
1159
            return std::make_unique<stdlib::FreeNodeSerializer>();
×
1160
        });
1161
    LibraryNodeSerializerRegistry::instance()
2✔
1162
        .register_library_node_serializer(stdlib::LibraryNodeType_FWrite.value(), []() {
2✔
NEW
1163
            return std::make_unique<stdlib::FWriteNodeSerializer>();
×
1164
        });
1165
    LibraryNodeSerializerRegistry::instance()
2✔
1166
        .register_library_node_serializer(stdlib::LibraryNodeType_Malloc.value(), []() {
2✔
NEW
1167
            return std::make_unique<stdlib::MallocNodeSerializer>();
×
1168
        });
1169
    LibraryNodeSerializerRegistry::instance()
2✔
1170
        .register_library_node_serializer(stdlib::LibraryNodeType_Rand.value(), []() {
2✔
NEW
1171
            return std::make_unique<stdlib::RandNodeSerializer>();
×
1172
        });
1173
    LibraryNodeSerializerRegistry::instance()
2✔
1174
        .register_library_node_serializer(stdlib::LibraryNodeType_Srand.value(), []() {
2✔
NEW
1175
            return std::make_unique<stdlib::SrandNodeSerializer>();
×
1176
        });
1177

1178
    // Metadata
1179
    LibraryNodeSerializerRegistry::instance()
2✔
1180
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
NEW
1181
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1182
        });
1183

1184
    // Barrier
1185
    LibraryNodeSerializerRegistry::instance()
2✔
1186
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1187
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1188
        });
1189

1190
    // Barrier
1191
    LibraryNodeSerializerRegistry::instance()
2✔
1192
        .register_library_node_serializer(data_flow::LibraryNodeType_Call.value(), []() {
2✔
NEW
1193
            return std::make_unique<data_flow::CallNodeSerializer>();
×
1194
        });
1195

1196
    // ML
1197
    // LibraryNodeSerializerRegistry::instance()
1198
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Abs.value(), []() {
1199
    //         return std::make_unique<math::ml::AbsNodeSerializer>();
1200
    //     });
1201
    // LibraryNodeSerializerRegistry::instance()
1202
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Add.value(), []() {
1203
    //         return std::make_unique<math::ml::AddNodeSerializer>();
1204
    //     });
1205
    // LibraryNodeSerializerRegistry::instance()
1206
    //     .register_library_node_serializer(math::ml::LibraryNodeType_BatchNormalization.value(), []() {
1207
    //         return std::make_unique<math::ml::BatchNormalizationNodeSerializer>();
1208
    //     });
1209
    // LibraryNodeSerializerRegistry::instance()
1210
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Clip.value(), []() {
1211
    //         return std::make_unique<math::ml::ClipNodeSerializer>();
1212
    //     });
1213
    // LibraryNodeSerializerRegistry::instance()
1214
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Conv.value(), []() {
1215
    //         return std::make_unique<math::ml::ConvNodeSerializer>();
1216
    //     });
1217
    // LibraryNodeSerializerRegistry::instance()
1218
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Div.value(), []() {
1219
    //         return std::make_unique<math::ml::DivNodeSerializer>();
1220
    //     });
1221
    // LibraryNodeSerializerRegistry::instance()
1222
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Dropout.value(), []() {
1223
    //         return std::make_unique<math::ml::DropoutSerializer>();
1224
    //     });
1225
    // LibraryNodeSerializerRegistry::instance()
1226
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Elu.value(), []() {
1227
    //         return std::make_unique<math::ml::EluNodeSerializer>();
1228
    //     });
1229
    // LibraryNodeSerializerRegistry::instance()
1230
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Erf.value(), []() {
1231
    //         return std::make_unique<math::ml::ErfNodeSerializer>();
1232
    //     });
1233
    // LibraryNodeSerializerRegistry::instance()
1234
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Gemm.value(), []() {
1235
    //         return std::make_unique<math::ml::GemmNodeSerializer>();
1236
    //     });
1237
    // LibraryNodeSerializerRegistry::instance()
1238
    //     .register_library_node_serializer(math::ml::LibraryNodeType_HardSigmoid.value(), []() {
1239
    //         return std::make_unique<math::ml::HardSigmoidNodeSerializer>();
1240
    //     });
1241
    // LibraryNodeSerializerRegistry::instance()
1242
    //     .register_library_node_serializer(math::ml::LibraryNodeType_LayerNormalization.value(), []() {
1243
    //         return std::make_unique<math::ml::LayerNormalizationNodeSerializer>();
1244
    //     });
1245
    // LibraryNodeSerializerRegistry::instance()
1246
    //     .register_library_node_serializer(math::ml::LibraryNodeType_LeakyReLU.value(), []() {
1247
    //         return std::make_unique<math::ml::LeakyReLUNodeSerializer>();
1248
    //     });
1249
    // LibraryNodeSerializerRegistry::instance()
1250
    //     .register_library_node_serializer(math::ml::LibraryNodeType_LogSoftmax.value(), []() {
1251
    //         return std::make_unique<math::ml::LogSoftmaxNodeSerializer>();
1252
    //     });
1253
    // LibraryNodeSerializerRegistry::instance()
1254
    //     .register_library_node_serializer(math::ml::LibraryNodeType_MatMul.value(), []() {
1255
    //         return std::make_unique<math::ml::MatMulNodeSerializer>();
1256
    //     });
1257
    // LibraryNodeSerializerRegistry::instance()
1258
    //     .register_library_node_serializer(math::ml::LibraryNodeType_MaxPool.value(), []() {
1259
    //         return std::make_unique<math::ml::MaxPoolNodeSerializer>();
1260
    //     });
1261
    // LibraryNodeSerializerRegistry::instance()
1262
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Mul.value(), []() {
1263
    //         return std::make_unique<math::ml::MulNodeSerializer>();
1264
    //     });
1265
    // LibraryNodeSerializerRegistry::instance()
1266
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Pow.value(), []() {
1267
    //         return std::make_unique<math::ml::PowNodeSerializer>();
1268
    //     });
1269
    // LibraryNodeSerializerRegistry::instance()
1270
    //     .register_library_node_serializer(math::ml::LibraryNodeType_ReduceMean.value(), []() {
1271
    //         return std::make_unique<math::ml::ReduceMeanNodeSerializer>();
1272
    //     });
1273
    // LibraryNodeSerializerRegistry::instance()
1274
    //     .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
1275
    //         return std::make_unique<math::ml::ReLUNodeSerializer>();
1276
    //     });
1277
    // LibraryNodeSerializerRegistry::instance()
1278
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Sigmoid.value(), []() {
1279
    //         return std::make_unique<math::ml::SigmoidNodeSerializer>();
1280
    //     });
1281
    // LibraryNodeSerializerRegistry::instance()
1282
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Softmax.value(), []() {
1283
    //         return std::make_unique<math::ml::SoftmaxNodeSerializer>();
1284
    //     });
1285
    // LibraryNodeSerializerRegistry::instance()
1286
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Sqrt.value(), []() {
1287
    //         return std::make_unique<math::ml::SqrtNodeSerializer>();
1288
    //     });
1289
    // LibraryNodeSerializerRegistry::instance()
1290
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Sub.value(), []() {
1291
    //         return std::make_unique<math::ml::SubNodeSerializer>();
1292
    //     });
1293
    // LibraryNodeSerializerRegistry::instance()
1294
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Tanh.value(), []() {
1295
    //         return std::make_unique<math::ml::TanhNodeSerializer>();
1296
    //     });
1297

1298
    // BLAS
1299
    LibraryNodeSerializerRegistry::instance()
2✔
1300
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1301
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1302
        });
1303
    LibraryNodeSerializerRegistry::instance()
2✔
1304
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1305
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1306
        });
1307
}
2✔
1308

1309
} // namespace serializer
1310
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc