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

daisytuner / sdfglib / 17656823807

11 Sep 2025 08:42PM UTC coverage: 60.447% (+1.1%) from 59.335%
17656823807

Pull #219

github

web-flow
Merge d5416236f into 6c1992b40
Pull Request #219: stdlib Library Nodes and ConstantNodes

460 of 1635 new or added lines in 81 files covered. (28.13%)

93 existing lines in 35 files now uncovered.

9385 of 15526 relevant lines covered (60.45%)

107.21 hits per line

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

81.02
/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

453
    std::unique_ptr<types::IType> return_type;
5✔
454
    if (j.contains("return_type")) {
5✔
455
        return_type = json_to_type(j["return_type"]);
5✔
456
    } else {
5✔
NEW
457
        return_type = std::make_unique<types::Scalar>(types::PrimitiveType::Void);
×
458
    }
459

460
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
5✔
461
    builder::StructuredSDFGBuilder builder(j["name"], function_type, *return_type);
5✔
462

463
    size_t element_counter = j["element_counter"];
5✔
464
    builder.set_element_counter(element_counter);
5✔
465

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

475
    nlohmann::json& containers = j["containers"];
5✔
476

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

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

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

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

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

519
    builder.set_element_counter(element_counter);
5✔
520

521
    return builder.move();
5✔
522
}
5✔
523

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

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

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

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

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

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

NEW
604
            auto type = json_to_type(node["data_type"]);
×
605

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

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

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

634
        auto base_type = json_to_type(edge["base_type"]);
12✔
635

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

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

669
    sequence.element_id_ = j["element_id"];
14✔
670
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
14✔
671

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

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

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

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

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

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

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

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

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

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

828
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
829
    while_node.element_id_ = j["element_id"];
3✔
830

831
    assert(j["root"]["type"] == "sequence");
3✔
832
    json_to_sequence(j["root"], builder, while_node.root());
3✔
833
}
3✔
834

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

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

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

883
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
884

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

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

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

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

913
    std::string data;
1✔
914
    if (j.contains("data") && j["data"].is_string()) {
1✔
915
        data = j["data"];
1✔
916
    } else {
1✔
NEW
917
        data = "";
×
918
    }
919

920
    bool unreachable = false;
1✔
921
    if (j.contains("unreachable") && j["unreachable"].is_boolean()) {
1✔
922
        unreachable = j["unreachable"];
1✔
923
    } else {
1✔
NEW
924
        unreachable = false;
×
925
    }
926

927
    auto& node = builder.add_return(parent, data, unreachable, assignments, json_to_debug_info(j["debug_info"]));
1✔
928
    node.element_id_ = j["element_id"];
1✔
929
}
1✔
930

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

1014
        } else {
1✔
1015
            throw std::runtime_error("Unknown type");
×
1016
        }
1017
    } else {
1018
        throw std::runtime_error("Type not found");
×
1019
    }
1020
}
46✔
1021

1022
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
72✔
1023
    assert(j.contains("has"));
72✔
1024
    assert(j["has"].is_boolean());
72✔
1025
    if (!j["has"]) {
72✔
1026
        return DebugInfo();
72✔
1027
    }
1028
    assert(j.contains("filename"));
×
1029
    assert(j["filename"].is_string());
×
1030
    std::string filename = j["filename"];
×
1031
    assert(j.contains("function"));
×
1032
    assert(j["function"].is_string());
×
1033
    std::string function = j["function"];
×
1034
    assert(j.contains("start_line"));
×
1035
    assert(j["start_line"].is_number_integer());
×
1036
    size_t start_line = j["start_line"];
×
1037
    assert(j.contains("start_column"));
×
1038
    assert(j["start_column"].is_number_integer());
×
1039
    size_t start_column = j["start_column"];
×
1040
    assert(j.contains("end_line"));
×
1041
    assert(j["end_line"].is_number_integer());
×
1042
    size_t end_line = j["end_line"];
×
1043
    assert(j.contains("end_column"));
×
1044
    assert(j["end_column"].is_number_integer());
×
1045
    size_t end_column = j["end_column"];
×
1046
    return DebugInfo(filename, function, start_line, start_column, end_line, end_column);
×
1047
}
72✔
1048

1049
ScheduleType JSONSerializer::json_to_schedule_type(const nlohmann::json& j) {
2✔
1050
    assert(j.contains("value"));
2✔
1051
    assert(j["value"].is_string());
2✔
1052
    assert(j.contains("properties"));
2✔
1053
    assert(j["properties"].is_object());
2✔
1054
    ScheduleType schedule_type(j["value"].get<std::string>());
2✔
1055
    for (const auto& [key, value] : j["properties"].items()) {
4✔
1056
        assert(value.is_string());
1✔
1057
        schedule_type.set_property(key, value.get<std::string>());
1✔
1058
    }
1059
    return schedule_type;
2✔
1060
}
2✔
1061

1062
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
36✔
1063
    JSONSymbolicPrinter printer;
36✔
1064
    return printer.apply(expr);
36✔
1065
};
36✔
1066

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

1072
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1073
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1074
    str_ = parenthesize(str_);
×
1075
};
×
1076

1077
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1078
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1079
    str_ = parenthesize(str_);
×
1080
};
×
1081

1082
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1083
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1084
    str_ = parenthesize(str_);
4✔
1085
};
4✔
1086

1087
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1088
    std::ostringstream s;
×
1089
    auto container = x.get_args();
×
1090
    if (container.size() == 1) {
×
1091
        s << apply(*container.begin());
×
1092
    } else {
×
1093
        s << "min(";
×
1094
        s << apply(*container.begin());
×
1095

1096
        // Recursively apply __daisy_min to the arguments
1097
        SymEngine::vec_basic subargs;
×
1098
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1099
            subargs.push_back(*it);
×
1100
        }
×
1101
        auto submin = SymEngine::min(subargs);
×
1102
        s << ", " << apply(submin);
×
1103

1104
        s << ")";
×
1105
    }
×
1106

1107
    str_ = s.str();
×
1108
};
×
1109

1110
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1111
    std::ostringstream s;
×
1112
    auto container = x.get_args();
×
1113
    if (container.size() == 1) {
×
1114
        s << apply(*container.begin());
×
1115
    } else {
×
1116
        s << "max(";
×
1117
        s << apply(*container.begin());
×
1118

1119
        // Recursively apply __daisy_max to the arguments
1120
        SymEngine::vec_basic subargs;
×
1121
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1122
            subargs.push_back(*it);
×
1123
        }
×
1124
        auto submax = SymEngine::max(subargs);
×
1125
        s << ", " << apply(submax);
×
1126

1127
        s << ")";
×
1128
    }
×
1129

1130
    str_ = s.str();
×
1131
};
×
1132

1133
void LibraryNodeSerializerRegistry::
1134
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
34✔
1135
    std::lock_guard<std::mutex> lock(mutex_);
34✔
1136
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
34✔
1137
        throw std::runtime_error(
×
1138
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1139
        );
1140
    }
1141
    factory_map_[library_node_code] = std::move(fn);
34✔
1142
}
34✔
1143

1144
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1145
    auto it = factory_map_.find(library_node_code);
1✔
1146
    if (it != factory_map_.end()) {
1✔
1147
        return it->second;
1✔
1148
    }
1149
    return nullptr;
×
1150
}
1✔
1151

1152
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1153

1154
void register_default_serializers() {
2✔
1155
    // stdlib
1156
    LibraryNodeSerializerRegistry::instance()
2✔
1157
        .register_library_node_serializer(stdlib::LibraryNodeType_Alloca.value(), []() {
2✔
NEW
1158
            return std::make_unique<stdlib::AllocaNodeSerializer>();
×
1159
        });
1160
    LibraryNodeSerializerRegistry::instance()
2✔
1161
        .register_library_node_serializer(stdlib::LibraryNodeType_Calloc.value(), []() {
2✔
NEW
1162
            return std::make_unique<stdlib::CallocNodeSerializer>();
×
1163
        });
1164
    LibraryNodeSerializerRegistry::instance()
2✔
1165
        .register_library_node_serializer(stdlib::LibraryNodeType_Fprintf.value(), []() {
2✔
NEW
1166
            return std::make_unique<stdlib::FprintfNodeSerializer>();
×
1167
        });
1168
    LibraryNodeSerializerRegistry::instance()
2✔
1169
        .register_library_node_serializer(stdlib::LibraryNodeType_Fputc.value(), []() {
2✔
NEW
1170
            return std::make_unique<stdlib::FputcNodeSerializer>();
×
1171
        });
1172
    LibraryNodeSerializerRegistry::instance()
2✔
1173
        .register_library_node_serializer(stdlib::LibraryNodeType_Free.value(), []() {
2✔
NEW
1174
            return std::make_unique<stdlib::FreeNodeSerializer>();
×
1175
        });
1176
    LibraryNodeSerializerRegistry::instance()
2✔
1177
        .register_library_node_serializer(stdlib::LibraryNodeType_FWrite.value(), []() {
2✔
NEW
1178
            return std::make_unique<stdlib::FWriteNodeSerializer>();
×
1179
        });
1180
    LibraryNodeSerializerRegistry::instance()
2✔
1181
        .register_library_node_serializer(stdlib::LibraryNodeType_Malloc.value(), []() {
2✔
NEW
1182
            return std::make_unique<stdlib::MallocNodeSerializer>();
×
1183
        });
1184
    LibraryNodeSerializerRegistry::instance()
2✔
1185
        .register_library_node_serializer(stdlib::LibraryNodeType_Memcpy.value(), []() {
2✔
NEW
1186
            return std::make_unique<stdlib::MemcpyNodeSerializer>();
×
1187
        });
1188
    LibraryNodeSerializerRegistry::instance()
2✔
1189
        .register_library_node_serializer(stdlib::LibraryNodeType_Memmove.value(), []() {
2✔
NEW
1190
            return std::make_unique<stdlib::MemmoveNodeSerializer>();
×
1191
        });
1192
    LibraryNodeSerializerRegistry::instance()
2✔
1193
        .register_library_node_serializer(stdlib::LibraryNodeType_Memset.value(), []() {
2✔
NEW
1194
            return std::make_unique<stdlib::MemsetNodeSerializer>();
×
1195
        });
1196
    LibraryNodeSerializerRegistry::instance()
2✔
1197
        .register_library_node_serializer(stdlib::LibraryNodeType_Rand.value(), []() {
2✔
NEW
1198
            return std::make_unique<stdlib::RandNodeSerializer>();
×
1199
        });
1200
    LibraryNodeSerializerRegistry::instance()
2✔
1201
        .register_library_node_serializer(stdlib::LibraryNodeType_Srand.value(), []() {
2✔
NEW
1202
            return std::make_unique<stdlib::SrandNodeSerializer>();
×
1203
        });
1204

1205
    // Metadata
1206
    LibraryNodeSerializerRegistry::instance()
2✔
1207
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
NEW
1208
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1209
        });
1210

1211
    // Barrier
1212
    LibraryNodeSerializerRegistry::instance()
2✔
1213
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1214
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1215
        });
1216

1217
    // Barrier
1218
    LibraryNodeSerializerRegistry::instance()
2✔
1219
        .register_library_node_serializer(data_flow::LibraryNodeType_Call.value(), []() {
2✔
NEW
1220
            return std::make_unique<data_flow::CallNodeSerializer>();
×
1221
        });
1222

1223
    // ML
1224
    // LibraryNodeSerializerRegistry::instance()
1225
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Abs.value(), []() {
1226
    //         return std::make_unique<math::ml::AbsNodeSerializer>();
1227
    //     });
1228
    // LibraryNodeSerializerRegistry::instance()
1229
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Add.value(), []() {
1230
    //         return std::make_unique<math::ml::AddNodeSerializer>();
1231
    //     });
1232
    // LibraryNodeSerializerRegistry::instance()
1233
    //     .register_library_node_serializer(math::ml::LibraryNodeType_BatchNormalization.value(), []() {
1234
    //         return std::make_unique<math::ml::BatchNormalizationNodeSerializer>();
1235
    //     });
1236
    // LibraryNodeSerializerRegistry::instance()
1237
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Clip.value(), []() {
1238
    //         return std::make_unique<math::ml::ClipNodeSerializer>();
1239
    //     });
1240
    // LibraryNodeSerializerRegistry::instance()
1241
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Conv.value(), []() {
1242
    //         return std::make_unique<math::ml::ConvNodeSerializer>();
1243
    //     });
1244
    // LibraryNodeSerializerRegistry::instance()
1245
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Div.value(), []() {
1246
    //         return std::make_unique<math::ml::DivNodeSerializer>();
1247
    //     });
1248
    // LibraryNodeSerializerRegistry::instance()
1249
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Dropout.value(), []() {
1250
    //         return std::make_unique<math::ml::DropoutSerializer>();
1251
    //     });
1252
    // LibraryNodeSerializerRegistry::instance()
1253
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Elu.value(), []() {
1254
    //         return std::make_unique<math::ml::EluNodeSerializer>();
1255
    //     });
1256
    // LibraryNodeSerializerRegistry::instance()
1257
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Erf.value(), []() {
1258
    //         return std::make_unique<math::ml::ErfNodeSerializer>();
1259
    //     });
1260
    // LibraryNodeSerializerRegistry::instance()
1261
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Gemm.value(), []() {
1262
    //         return std::make_unique<math::ml::GemmNodeSerializer>();
1263
    //     });
1264
    // LibraryNodeSerializerRegistry::instance()
1265
    //     .register_library_node_serializer(math::ml::LibraryNodeType_HardSigmoid.value(), []() {
1266
    //         return std::make_unique<math::ml::HardSigmoidNodeSerializer>();
1267
    //     });
1268
    // LibraryNodeSerializerRegistry::instance()
1269
    //     .register_library_node_serializer(math::ml::LibraryNodeType_LayerNormalization.value(), []() {
1270
    //         return std::make_unique<math::ml::LayerNormalizationNodeSerializer>();
1271
    //     });
1272
    // LibraryNodeSerializerRegistry::instance()
1273
    //     .register_library_node_serializer(math::ml::LibraryNodeType_LeakyReLU.value(), []() {
1274
    //         return std::make_unique<math::ml::LeakyReLUNodeSerializer>();
1275
    //     });
1276
    // LibraryNodeSerializerRegistry::instance()
1277
    //     .register_library_node_serializer(math::ml::LibraryNodeType_LogSoftmax.value(), []() {
1278
    //         return std::make_unique<math::ml::LogSoftmaxNodeSerializer>();
1279
    //     });
1280
    // LibraryNodeSerializerRegistry::instance()
1281
    //     .register_library_node_serializer(math::ml::LibraryNodeType_MatMul.value(), []() {
1282
    //         return std::make_unique<math::ml::MatMulNodeSerializer>();
1283
    //     });
1284
    // LibraryNodeSerializerRegistry::instance()
1285
    //     .register_library_node_serializer(math::ml::LibraryNodeType_MaxPool.value(), []() {
1286
    //         return std::make_unique<math::ml::MaxPoolNodeSerializer>();
1287
    //     });
1288
    // LibraryNodeSerializerRegistry::instance()
1289
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Mul.value(), []() {
1290
    //         return std::make_unique<math::ml::MulNodeSerializer>();
1291
    //     });
1292
    // LibraryNodeSerializerRegistry::instance()
1293
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Pow.value(), []() {
1294
    //         return std::make_unique<math::ml::PowNodeSerializer>();
1295
    //     });
1296
    // LibraryNodeSerializerRegistry::instance()
1297
    //     .register_library_node_serializer(math::ml::LibraryNodeType_ReduceMean.value(), []() {
1298
    //         return std::make_unique<math::ml::ReduceMeanNodeSerializer>();
1299
    //     });
1300
    // LibraryNodeSerializerRegistry::instance()
1301
    //     .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
1302
    //         return std::make_unique<math::ml::ReLUNodeSerializer>();
1303
    //     });
1304
    // LibraryNodeSerializerRegistry::instance()
1305
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Sigmoid.value(), []() {
1306
    //         return std::make_unique<math::ml::SigmoidNodeSerializer>();
1307
    //     });
1308
    // LibraryNodeSerializerRegistry::instance()
1309
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Softmax.value(), []() {
1310
    //         return std::make_unique<math::ml::SoftmaxNodeSerializer>();
1311
    //     });
1312
    // LibraryNodeSerializerRegistry::instance()
1313
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Sqrt.value(), []() {
1314
    //         return std::make_unique<math::ml::SqrtNodeSerializer>();
1315
    //     });
1316
    // LibraryNodeSerializerRegistry::instance()
1317
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Sub.value(), []() {
1318
    //         return std::make_unique<math::ml::SubNodeSerializer>();
1319
    //     });
1320
    // LibraryNodeSerializerRegistry::instance()
1321
    //     .register_library_node_serializer(math::ml::LibraryNodeType_Tanh.value(), []() {
1322
    //         return std::make_unique<math::ml::TanhNodeSerializer>();
1323
    //     });
1324

1325
    // BLAS
1326
    LibraryNodeSerializerRegistry::instance()
2✔
1327
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1328
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1329
        });
1330
    LibraryNodeSerializerRegistry::instance()
2✔
1331
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1332
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1333
        });
1334
}
2✔
1335

1336
} // namespace serializer
1337
} // 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