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

daisytuner / sdfglib / 17697650771

13 Sep 2025 02:09PM UTC coverage: 60.533% (+1.2%) from 59.335%
17697650771

Pull #219

github

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

563 of 1790 new or added lines in 102 files covered. (31.45%)

102 existing lines in 38 files now uncovered.

9442 of 15598 relevant lines covered (60.53%)

107.06 hits per line

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

80.78
/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_ : edge["subset"]) {
16✔
640
            assert(subset_.is_string());
4✔
641
            std::string subset_str = subset_;
4✔
642
            auto expr = symbolic::parse(subset_str);
4✔
643
            subset.push_back(expr);
4✔
644
        }
4✔
645
        auto& memlet = builder.add_memlet(
24✔
646
            parent,
12✔
647
            source,
12✔
648
            edge["src_conn"],
12✔
649
            target,
12✔
650
            edge["dst_conn"],
12✔
651
            subset,
652
            *base_type,
12✔
653
            json_to_debug_info(edge["debug_info"])
12✔
654
        );
655
        memlet.element_id_ = edge["element_id"];
12✔
656
    }
12✔
657
}
11✔
658

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

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

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

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

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

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

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

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

767
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
768
    auto init = symbolic::parse(j["init"].get<std::string>());
1✔
769
    auto update = symbolic::parse(j["update"].get<std::string>());
1✔
770

771
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
1✔
772
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
1✔
773
    if (condition.is_null()) {
1✔
NEW
774
        throw InvalidSDFGException("For loop condition is not a boolean expression");
×
775
    }
776

777
    auto& for_node =
1✔
778
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
1✔
779
    for_node.element_id_ = j["element_id"];
1✔
780

781
    assert(j["root"].contains("type"));
1✔
782
    assert(j["root"]["type"].is_string());
1✔
783
    assert(j["root"]["type"] == "sequence");
1✔
784
    json_to_sequence(j["root"], builder, for_node.root());
1✔
785
}
1✔
786

787
void JSONSerializer::json_to_if_else_node(
1✔
788
    const nlohmann::json& j,
789
    builder::StructuredSDFGBuilder& builder,
790
    structured_control_flow::Sequence& parent,
791
    control_flow::Assignments& assignments
792
) {
793
    assert(j.contains("type"));
1✔
794
    assert(j["type"].is_string());
1✔
795
    assert(j["type"] == "if_else");
1✔
796
    assert(j.contains("branches"));
1✔
797
    assert(j["branches"].is_array());
1✔
798
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
799
    if_else_node.element_id_ = j["element_id"];
1✔
800
    for (const auto& branch : j["branches"]) {
3✔
801
        assert(branch.contains("condition"));
2✔
802
        assert(branch["condition"].is_string());
2✔
803
        assert(branch.contains("root"));
2✔
804
        assert(branch["root"].is_object());
2✔
805

806
        auto condition_expr = symbolic::parse(branch["condition"].get<std::string>());
2✔
807
        symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
2✔
808
        if (condition.is_null()) {
2✔
NEW
809
            throw InvalidSDFGException("If condition is not a boolean expression");
×
810
        }
811
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
812
        assert(branch["root"].contains("type"));
2✔
813
        assert(branch["root"]["type"].is_string());
2✔
814
        std::string type = branch["root"]["type"];
2✔
815
        if (type == "sequence") {
2✔
816
            json_to_sequence(branch["root"], builder, branch_node);
2✔
817
        } else {
2✔
818
            throw std::runtime_error("Unknown child type");
×
819
        }
820
    }
2✔
821
}
1✔
822

823
void JSONSerializer::json_to_while_node(
3✔
824
    const nlohmann::json& j,
825
    builder::StructuredSDFGBuilder& builder,
826
    structured_control_flow::Sequence& parent,
827
    control_flow::Assignments& assignments
828
) {
829
    assert(j.contains("type"));
3✔
830
    assert(j["type"].is_string());
3✔
831
    assert(j["type"] == "while");
3✔
832
    assert(j.contains("root"));
3✔
833
    assert(j["root"].is_object());
3✔
834

835
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
836
    while_node.element_id_ = j["element_id"];
3✔
837

838
    assert(j["root"]["type"] == "sequence");
3✔
839
    json_to_sequence(j["root"], builder, while_node.root());
3✔
840
}
3✔
841

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

855
void JSONSerializer::json_to_continue_node(
1✔
856
    const nlohmann::json& j,
857
    builder::StructuredSDFGBuilder& builder,
858
    structured_control_flow::Sequence& parent,
859
    control_flow::Assignments& assignments
860
) {
861
    assert(j.contains("type"));
1✔
862
    assert(j["type"].is_string());
1✔
863
    assert(j["type"] == "continue");
1✔
864
    auto& node = builder.add_continue(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
865
    node.element_id_ = j["element_id"];
1✔
866
}
1✔
867

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

890
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
891

892
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
893
    auto init = symbolic::parse(j["init"].get<std::string>());
1✔
894
    auto update = symbolic::parse(j["update"].get<std::string>());
1✔
895
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
1✔
896
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
1✔
897
    if (condition.is_null()) {
1✔
NEW
898
        throw InvalidSDFGException("Map condition is not a boolean expression");
×
899
    }
900

901
    auto& map_node = builder.add_map(
2✔
902
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
903
    );
904
    map_node.element_id_ = j["element_id"];
1✔
905

906
    assert(j["root"].contains("type"));
1✔
907
    assert(j["root"]["type"].is_string());
1✔
908
    assert(j["root"]["type"] == "sequence");
1✔
909
    json_to_sequence(j["root"], builder, map_node.root());
1✔
910
}
1✔
911

912
void JSONSerializer::json_to_return_node(
1✔
913
    const nlohmann::json& j,
914
    builder::StructuredSDFGBuilder& builder,
915
    structured_control_flow::Sequence& parent,
916
    control_flow::Assignments& assignments
917
) {
918
    assert(j.contains("type"));
1✔
919
    assert(j["type"].is_string());
1✔
920
    assert(j["type"] == "return");
1✔
921

922
    std::string data;
1✔
923
    if (j.contains("data") && j["data"].is_string()) {
1✔
924
        data = j["data"];
1✔
925
    } else {
1✔
NEW
926
        data = "";
×
927
    }
928

929
    bool unreachable = false;
1✔
930
    if (j.contains("unreachable") && j["unreachable"].is_boolean()) {
1✔
931
        unreachable = j["unreachable"];
1✔
932
    } else {
1✔
NEW
933
        unreachable = false;
×
934
    }
935

936
    auto& node = builder.add_return(parent, data, unreachable, assignments, json_to_debug_info(j["debug_info"]));
1✔
937
    node.element_id_ = j["element_id"];
1✔
938
}
1✔
939

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

1023
        } else {
1✔
1024
            throw std::runtime_error("Unknown type");
×
1025
        }
1026
    } else {
1027
        throw std::runtime_error("Type not found");
×
1028
    }
1029
}
46✔
1030

1031
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
72✔
1032
    assert(j.contains("has"));
72✔
1033
    assert(j["has"].is_boolean());
72✔
1034
    if (!j["has"]) {
72✔
1035
        return DebugInfo();
72✔
1036
    }
1037
    assert(j.contains("filename"));
×
1038
    assert(j["filename"].is_string());
×
1039
    std::string filename = j["filename"];
×
1040
    assert(j.contains("function"));
×
1041
    assert(j["function"].is_string());
×
1042
    std::string function = j["function"];
×
1043
    assert(j.contains("start_line"));
×
1044
    assert(j["start_line"].is_number_integer());
×
1045
    size_t start_line = j["start_line"];
×
1046
    assert(j.contains("start_column"));
×
1047
    assert(j["start_column"].is_number_integer());
×
1048
    size_t start_column = j["start_column"];
×
1049
    assert(j.contains("end_line"));
×
1050
    assert(j["end_line"].is_number_integer());
×
1051
    size_t end_line = j["end_line"];
×
1052
    assert(j.contains("end_column"));
×
1053
    assert(j["end_column"].is_number_integer());
×
1054
    size_t end_column = j["end_column"];
×
1055
    return DebugInfo(filename, function, start_line, start_column, end_line, end_column);
×
1056
}
72✔
1057

1058
ScheduleType JSONSerializer::json_to_schedule_type(const nlohmann::json& j) {
2✔
1059
    assert(j.contains("value"));
2✔
1060
    assert(j["value"].is_string());
2✔
1061
    assert(j.contains("properties"));
2✔
1062
    assert(j["properties"].is_object());
2✔
1063
    ScheduleType schedule_type(j["value"].get<std::string>());
2✔
1064
    for (const auto& [key, value] : j["properties"].items()) {
4✔
1065
        assert(value.is_string());
1✔
1066
        schedule_type.set_property(key, value.get<std::string>());
1✔
1067
    }
1068
    return schedule_type;
2✔
1069
}
2✔
1070

1071
std::string JSONSerializer::expression(const symbolic::Expression expr) {
36✔
1072
    JSONSymbolicPrinter printer;
36✔
1073
    return printer.apply(expr);
36✔
1074
};
36✔
1075

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

1081
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1082
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1083
    str_ = parenthesize(str_);
×
1084
};
×
1085

1086
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1087
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1088
    str_ = parenthesize(str_);
×
1089
};
×
1090

1091
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1092
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1093
    str_ = parenthesize(str_);
4✔
1094
};
4✔
1095

1096
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1097
    std::ostringstream s;
×
1098
    auto container = x.get_args();
×
1099
    if (container.size() == 1) {
×
1100
        s << apply(*container.begin());
×
1101
    } else {
×
1102
        s << "min(";
×
1103
        s << apply(*container.begin());
×
1104

1105
        // Recursively apply __daisy_min to the arguments
1106
        SymEngine::vec_basic subargs;
×
1107
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1108
            subargs.push_back(*it);
×
1109
        }
×
1110
        auto submin = SymEngine::min(subargs);
×
1111
        s << ", " << apply(submin);
×
1112

1113
        s << ")";
×
1114
    }
×
1115

1116
    str_ = s.str();
×
1117
};
×
1118

1119
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1120
    std::ostringstream s;
×
1121
    auto container = x.get_args();
×
1122
    if (container.size() == 1) {
×
1123
        s << apply(*container.begin());
×
1124
    } else {
×
1125
        s << "max(";
×
1126
        s << apply(*container.begin());
×
1127

1128
        // Recursively apply __daisy_max to the arguments
1129
        SymEngine::vec_basic subargs;
×
1130
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1131
            subargs.push_back(*it);
×
1132
        }
×
1133
        auto submax = SymEngine::max(subargs);
×
1134
        s << ", " << apply(submax);
×
1135

1136
        s << ")";
×
1137
    }
×
1138

1139
    str_ = s.str();
×
1140
};
×
1141

1142
void LibraryNodeSerializerRegistry::
1143
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
34✔
1144
    std::lock_guard<std::mutex> lock(mutex_);
34✔
1145
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
34✔
1146
        throw std::runtime_error(
×
1147
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1148
        );
1149
    }
1150
    factory_map_[library_node_code] = std::move(fn);
34✔
1151
}
34✔
1152

1153
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1154
    auto it = factory_map_.find(library_node_code);
1✔
1155
    if (it != factory_map_.end()) {
1✔
1156
        return it->second;
1✔
1157
    }
1158
    return nullptr;
×
1159
}
1✔
1160

1161
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1162

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

1214
    // Metadata
1215
    LibraryNodeSerializerRegistry::instance()
2✔
1216
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
NEW
1217
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1218
        });
1219

1220
    // Barrier
1221
    LibraryNodeSerializerRegistry::instance()
2✔
1222
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1223
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1224
        });
1225

1226
    // Barrier
1227
    LibraryNodeSerializerRegistry::instance()
2✔
1228
        .register_library_node_serializer(data_flow::LibraryNodeType_Call.value(), []() {
2✔
NEW
1229
            return std::make_unique<data_flow::CallNodeSerializer>();
×
1230
        });
1231

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

1334
    // BLAS
1335
    LibraryNodeSerializerRegistry::instance()
2✔
1336
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1337
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1338
        });
1339
    LibraryNodeSerializerRegistry::instance()
2✔
1340
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1341
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1342
        });
1343
}
2✔
1344

1345
} // namespace serializer
1346
} // 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