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

daisytuner / sdfglib / 19192861975

08 Nov 2025 12:14PM UTC coverage: 61.281% (-0.1%) from 61.415%
19192861975

push

github

web-flow
Merge pull request #329 from daisytuner/trap-nodes

Trap nodes and unreachable return

17 of 101 new or added lines in 7 files covered. (16.83%)

6 existing lines in 2 files now uncovered.

10264 of 16749 relevant lines covered (61.28%)

100.39 hits per line

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

80.49
/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/invoke_node.h"
12
#include "sdfg/data_flow/library_nodes/math/math.h"
13
#include "sdfg/data_flow/library_nodes/metadata_node.h"
14
#include "sdfg/data_flow/library_nodes/stdlib/stdlib.h"
15

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

35
namespace sdfg {
36
namespace serializer {
37

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

45
    return FunctionType(str);
×
46
}
7✔
47

48
/*
49
 * * JSONSerializer class
50
 * * Serialization logic
51
 */
52

53
nlohmann::json JSONSerializer::serialize(const sdfg::StructuredSDFG& sdfg) {
7✔
54
    nlohmann::json j;
7✔
55

56
    j["name"] = sdfg.name();
7✔
57
    j["element_counter"] = sdfg.element_counter();
7✔
58
    j["type"] = std::string(sdfg.type().value());
7✔
59

60
    nlohmann::json return_type_json;
7✔
61
    type_to_json(return_type_json, sdfg.return_type());
7✔
62
    j["return_type"] = return_type_json;
7✔
63

64
    j["structures"] = nlohmann::json::array();
7✔
65
    for (const auto& structure_name : sdfg.structures()) {
8✔
66
        const auto& structure = sdfg.structure(structure_name);
1✔
67
        nlohmann::json structure_json;
1✔
68
        structure_definition_to_json(structure_json, structure);
1✔
69
        j["structures"].push_back(structure_json);
1✔
70
    }
1✔
71

72
    j["containers"] = nlohmann::json::object();
7✔
73
    for (const auto& container : sdfg.containers()) {
61✔
74
        nlohmann::json desc;
54✔
75
        type_to_json(desc, sdfg.type(container));
54✔
76
        j["containers"][container] = desc;
54✔
77
    }
54✔
78

79
    j["arguments"] = nlohmann::json::array();
7✔
80
    for (const auto& argument : sdfg.arguments()) {
30✔
81
        j["arguments"].push_back(argument);
23✔
82
    }
83

84
    j["externals"] = nlohmann::json::array();
7✔
85
    for (const auto& external : sdfg.externals()) {
11✔
86
        nlohmann::json external_json;
4✔
87
        external_json["name"] = external;
4✔
88
        external_json["linkage_type"] = sdfg.linkage_type(external);
4✔
89
        j["externals"].push_back(external_json);
4✔
90
    }
4✔
91

92
    j["metadata"] = nlohmann::json::object();
7✔
93
    for (const auto& entry : sdfg.metadata()) {
8✔
94
        j["metadata"][entry.first] = entry.second;
1✔
95
    }
96

97
    // Walk the SDFG
98
    nlohmann::json root_json;
7✔
99
    sequence_to_json(root_json, sdfg.root());
7✔
100
    j["root"] = root_json;
7✔
101

102
    return j;
7✔
103
}
7✔
104

105
void JSONSerializer::dataflow_to_json(nlohmann::json& j, const data_flow::DataFlowGraph& dataflow) {
33✔
106
    j["type"] = "dataflow";
33✔
107
    j["nodes"] = nlohmann::json::array();
33✔
108
    j["edges"] = nlohmann::json::array();
33✔
109

110
    for (auto& node : dataflow.nodes()) {
111✔
111
        nlohmann::json node_json;
78✔
112
        node_json["element_id"] = node.element_id();
78✔
113

114
        node_json["debug_info"] = nlohmann::json::object();
78✔
115
        debug_info_to_json(node_json["debug_info"], node.debug_info());
78✔
116

117
        if (auto tasklet = dynamic_cast<const data_flow::Tasklet*>(&node)) {
78✔
118
            node_json["type"] = "tasklet";
19✔
119
            node_json["code"] = tasklet->code();
19✔
120
            node_json["inputs"] = nlohmann::json::array();
19✔
121
            for (auto& input : tasklet->inputs()) {
61✔
122
                node_json["inputs"].push_back(input);
42✔
123
            }
124
            node_json["output"] = tasklet->output();
19✔
125
        } else if (auto lib_node = dynamic_cast<const data_flow::LibraryNode*>(&node)) {
78✔
126
            node_json["type"] = "library_node";
×
127
            node_json["implementation_type"] = std::string(lib_node->implementation_type().value());
×
128
            auto serializer_fn =
129
                LibraryNodeSerializerRegistry::instance().get_library_node_serializer(lib_node->code().value());
×
130
            if (serializer_fn == nullptr) {
×
131
                throw std::runtime_error("Unknown library node code: " + std::string(lib_node->code().value()));
×
132
            }
133
            auto serializer = serializer_fn();
×
134
            auto lib_node_json = serializer->serialize(*lib_node);
×
135
            node_json.merge_patch(lib_node_json);
×
136
        } else if (auto code_node = dynamic_cast<const data_flow::ConstantNode*>(&node)) {
59✔
137
            node_json["type"] = "constant_node";
×
138
            node_json["data"] = code_node->data();
×
139

140
            nlohmann::json type_json;
×
141
            type_to_json(type_json, code_node->type());
×
142
            node_json["data_type"] = type_json;
×
143
        } else if (auto code_node = dynamic_cast<const data_flow::AccessNode*>(&node)) {
59✔
144
            node_json["type"] = "access_node";
59✔
145
            node_json["data"] = code_node->data();
59✔
146
        } else {
59✔
147
            throw std::runtime_error("Unknown node type");
×
148
        }
149

150
        j["nodes"].push_back(node_json);
78✔
151
    }
78✔
152

153
    for (auto& edge : dataflow.edges()) {
94✔
154
        nlohmann::json edge_json;
61✔
155
        edge_json["element_id"] = edge.element_id();
61✔
156

157
        edge_json["debug_info"] = nlohmann::json::object();
61✔
158
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
61✔
159

160
        edge_json["src"] = edge.src().element_id();
61✔
161
        edge_json["dst"] = edge.dst().element_id();
61✔
162

163
        edge_json["src_conn"] = edge.src_conn();
61✔
164
        edge_json["dst_conn"] = edge.dst_conn();
61✔
165

166
        edge_json["subset"] = nlohmann::json::array();
61✔
167
        for (auto& subset : edge.subset()) {
109✔
168
            edge_json["subset"].push_back(expression(subset));
48✔
169
        }
170

171
        nlohmann::json base_type_json;
61✔
172
        type_to_json(base_type_json, edge.base_type());
61✔
173
        edge_json["base_type"] = base_type_json;
61✔
174

175
        j["edges"].push_back(edge_json);
61✔
176
    }
61✔
177
}
33✔
178

179
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
31✔
180
    j["type"] = "block";
31✔
181
    j["element_id"] = block.element_id();
31✔
182

183
    j["debug_info"] = nlohmann::json::object();
31✔
184
    debug_info_to_json(j["debug_info"], block.debug_info());
31✔
185

186
    nlohmann::json dataflow_json;
31✔
187
    dataflow_to_json(dataflow_json, block.dataflow());
31✔
188
    j["dataflow"] = dataflow_json;
31✔
189
}
31✔
190

191
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
16✔
192
    j["type"] = "for";
16✔
193
    j["element_id"] = for_node.element_id();
16✔
194

195
    j["debug_info"] = nlohmann::json::object();
16✔
196
    debug_info_to_json(j["debug_info"], for_node.debug_info());
16✔
197

198
    j["indvar"] = expression(for_node.indvar());
16✔
199
    j["init"] = expression(for_node.init());
16✔
200
    j["condition"] = expression(for_node.condition());
16✔
201
    j["update"] = expression(for_node.update());
16✔
202

203
    nlohmann::json body_json;
16✔
204
    sequence_to_json(body_json, for_node.root());
16✔
205
    j["root"] = body_json;
16✔
206
}
16✔
207

208
void JSONSerializer::if_else_to_json(nlohmann::json& j, const structured_control_flow::IfElse& if_else_node) {
2✔
209
    j["type"] = "if_else";
2✔
210
    j["element_id"] = if_else_node.element_id();
2✔
211

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

215
    j["branches"] = nlohmann::json::array();
2✔
216
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
217
        nlohmann::json branch_json;
4✔
218
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
219
        nlohmann::json body_json;
4✔
220
        sequence_to_json(body_json, if_else_node.at(i).first);
4✔
221
        branch_json["root"] = body_json;
4✔
222
        j["branches"].push_back(branch_json);
4✔
223
    }
4✔
224
}
2✔
225

226
void JSONSerializer::while_node_to_json(nlohmann::json& j, const structured_control_flow::While& while_node) {
5✔
227
    j["type"] = "while";
5✔
228
    j["element_id"] = while_node.element_id();
5✔
229

230
    j["debug_info"] = nlohmann::json::object();
5✔
231
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
232

233
    nlohmann::json body_json;
5✔
234
    sequence_to_json(body_json, while_node.root());
5✔
235
    j["root"] = body_json;
5✔
236
}
5✔
237

238
void JSONSerializer::break_node_to_json(nlohmann::json& j, const structured_control_flow::Break& break_node) {
2✔
239
    j["type"] = "break";
2✔
240
    j["element_id"] = break_node.element_id();
2✔
241

242
    j["debug_info"] = nlohmann::json::object();
2✔
243
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
244
}
2✔
245

246
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
247
    j["type"] = "continue";
2✔
248
    j["element_id"] = continue_node.element_id();
2✔
249

250
    j["debug_info"] = nlohmann::json::object();
2✔
251
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
252
}
2✔
253

254
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
255
    j["type"] = "map";
2✔
256
    j["element_id"] = map_node.element_id();
2✔
257

258
    j["debug_info"] = nlohmann::json::object();
2✔
259
    debug_info_to_json(j["debug_info"], map_node.debug_info());
2✔
260

261
    j["indvar"] = expression(map_node.indvar());
2✔
262
    j["init"] = expression(map_node.init());
2✔
263
    j["condition"] = expression(map_node.condition());
2✔
264
    j["update"] = expression(map_node.update());
2✔
265

266
    j["schedule_type"] = nlohmann::json::object();
2✔
267
    schedule_type_to_json(j["schedule_type"], map_node.schedule_type());
2✔
268

269
    nlohmann::json body_json;
2✔
270
    sequence_to_json(body_json, map_node.root());
2✔
271
    j["root"] = body_json;
2✔
272
}
2✔
273

274
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
275
    j["type"] = "return";
2✔
276
    j["element_id"] = return_node.element_id();
2✔
277
    j["data"] = return_node.data();
2✔
278

279
    if (return_node.is_constant()) {
2✔
280
        nlohmann::json type_json;
×
281
        type_to_json(type_json, return_node.type());
×
282
        j["data_type"] = type_json;
×
283
    }
×
284

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

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

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

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

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

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

326
        j["children"].push_back(child_json);
47✔
327

328
        // Add transition information
329
        nlohmann::json transition_json;
47✔
330
        transition_json["type"] = "transition";
47✔
331
        transition_json["element_id"] = transition.element_id();
47✔
332

333
        transition_json["debug_info"] = nlohmann::json::object();
47✔
334
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
47✔
335

336
        transition_json["assignments"] = nlohmann::json::array();
47✔
337
        for (const auto& assignment : transition.assignments()) {
50✔
338
            nlohmann::json assignment_json;
3✔
339
            assignment_json["symbol"] = expression(assignment.first);
3✔
340
            assignment_json["expression"] = expression(assignment.second);
3✔
341
            transition_json["assignments"].push_back(assignment_json);
3✔
342
        }
3✔
343

344
        j["transitions"].push_back(transition_json);
47✔
345
    }
47✔
346
}
37✔
347

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

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

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

426
void JSONSerializer::schedule_type_to_json(nlohmann::json& j, const ScheduleType& schedule_type) {
3✔
427
    j["value"] = schedule_type.value();
3✔
428
    j["properties"] = nlohmann::json::object();
3✔
429
    for (const auto& prop : schedule_type.properties()) {
4✔
430
        j["properties"][prop.first] = prop.second;
1✔
431
    }
432
}
3✔
433

434
void JSONSerializer::storage_type_to_json(nlohmann::json& j, const types::StorageType& storage_type) {
212✔
435
    j["value"] = storage_type.value();
212✔
436
    j["allocation"] = storage_type.allocation();
212✔
437
    j["deallocation"] = storage_type.deallocation();
212✔
438
    if (!storage_type.allocation_size().is_null()) {
212✔
439
        j["allocation_size"] = expression(storage_type.allocation_size());
×
440
    }
×
441
}
212✔
442

443

444
/*
445
 * * Deserialization logic
446
 */
447

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

456
    std::unique_ptr<types::IType> return_type;
7✔
457
    if (j.contains("return_type")) {
7✔
458
        return_type = json_to_type(j["return_type"]);
7✔
459
    } else {
7✔
460
        return_type = std::make_unique<types::Scalar>(types::PrimitiveType::Void);
×
461
    }
462

463
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
7✔
464
    builder::StructuredSDFGBuilder builder(j["name"], function_type, *return_type);
7✔
465

466
    size_t element_counter = j["element_counter"];
7✔
467
    builder.set_element_counter(element_counter);
7✔
468

469
    // deserialize structures
470
    assert(j.contains("structures"));
7✔
471
    assert(j["structures"].is_array());
7✔
472
    for (const auto& structure : j["structures"]) {
8✔
473
        assert(structure.contains("name"));
1✔
474
        assert(structure["name"].is_string());
1✔
475
        json_to_structure_definition(structure, builder);
1✔
476
    }
477

478
    nlohmann::json& containers = j["containers"];
7✔
479

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

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

498
    // deserialize transients
499
    for (const auto& entry : containers.items()) {
61✔
500
        if (builder.subject().is_argument(entry.key())) {
54✔
501
            continue;
23✔
502
        }
503
        if (builder.subject().is_external(entry.key())) {
31✔
504
            continue;
4✔
505
        }
506
        auto type = json_to_type(entry.value());
27✔
507
        builder.add_container(entry.key(), *type, false, false);
27✔
508
    }
27✔
509

510
    // deserialize root node
511
    assert(j.contains("root"));
7✔
512
    auto& root = builder.subject().root();
7✔
513
    json_to_sequence(j["root"], builder, root);
7✔
514

515
    // deserialize metadata
516
    assert(j.contains("metadata"));
7✔
517
    assert(j["metadata"].is_object());
7✔
518
    for (const auto& entry : j["metadata"].items()) {
8✔
519
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
520
    }
521

522
    builder.set_element_counter(element_counter);
7✔
523

524
    return builder.move();
7✔
525
}
7✔
526

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

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

557
void JSONSerializer::json_to_dataflow(
23✔
558
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
559
) {
560
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
23✔
561

562
    assert(j.contains("nodes"));
23✔
563
    assert(j["nodes"].is_array());
23✔
564
    for (const auto& node : j["nodes"]) {
97✔
565
        assert(node.contains("type"));
74✔
566
        assert(node["type"].is_string());
74✔
567
        assert(node.contains("element_id"));
74✔
568
        assert(node["element_id"].is_number_integer());
74✔
569
        std::string type = node["type"];
74✔
570
        if (type == "tasklet") {
74✔
571
            assert(node.contains("code"));
18✔
572
            assert(node["code"].is_number_integer());
18✔
573
            assert(node.contains("inputs"));
18✔
574
            assert(node["inputs"].is_array());
18✔
575
            assert(node.contains("output"));
18✔
576
            assert(node["output"].is_string());
18✔
577
            auto inputs = node["inputs"].get<std::vector<std::string>>();
18✔
578

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

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

607
            auto type = json_to_type(node["data_type"]);
×
608

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

618
    assert(j.contains("edges"));
23✔
619
    assert(j["edges"].is_array());
23✔
620
    for (const auto& edge : j["edges"]) {
81✔
621
        assert(edge.contains("src"));
58✔
622
        assert(edge["src"].is_number_integer());
58✔
623
        assert(edge.contains("dst"));
58✔
624
        assert(edge["dst"].is_number_integer());
58✔
625
        assert(edge.contains("src_conn"));
58✔
626
        assert(edge["src_conn"].is_string());
58✔
627
        assert(edge.contains("dst_conn"));
58✔
628
        assert(edge["dst_conn"].is_string());
58✔
629
        assert(edge.contains("subset"));
58✔
630
        assert(edge["subset"].is_array());
58✔
631

632
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
58✔
633
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
58✔
634
        auto& source = nodes_map.at(edge["src"]);
58✔
635
        auto& target = nodes_map.at(edge["dst"]);
58✔
636

637
        auto base_type = json_to_type(edge["base_type"]);
58✔
638

639
        assert(edge.contains("subset"));
58✔
640
        assert(edge["subset"].is_array());
58✔
641
        std::vector<symbolic::Expression> subset;
58✔
642
        for (const auto& subset_ : edge["subset"]) {
104✔
643
            assert(subset_.is_string());
46✔
644
            std::string subset_str = subset_;
46✔
645
            auto expr = symbolic::parse(subset_str);
46✔
646
            subset.push_back(expr);
46✔
647
        }
46✔
648
        auto& memlet = builder.add_memlet(
116✔
649
            parent,
58✔
650
            source,
58✔
651
            edge["src_conn"],
58✔
652
            target,
58✔
653
            edge["dst_conn"],
58✔
654
            subset,
655
            *base_type,
58✔
656
            json_to_debug_info(edge["debug_info"])
58✔
657
        );
658
        memlet.element_id_ = edge["element_id"];
58✔
659
    }
58✔
660
}
23✔
661

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

673
    sequence.element_id_ = j["element_id"];
30✔
674
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
30✔
675

676
    std::string type = j["type"];
30✔
677
    if (type == "sequence") {
30✔
678
        for (size_t i = 0; i < j["children"].size(); i++) {
67✔
679
            auto& child = j["children"][i];
37✔
680
            auto& transition = j["transitions"][i];
37✔
681
            assert(child.contains("type"));
37✔
682
            assert(child["type"].is_string());
37✔
683

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

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

721
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
37✔
722
            sequence.at(i).second.element_id_ = transition["element_id"];
37✔
723
        }
37✔
724
    } else {
30✔
725
        throw std::runtime_error("expected sequence type");
×
726
    }
727
}
30✔
728

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

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

770
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
15✔
771
    auto init = symbolic::parse(j["init"].get<std::string>());
15✔
772
    auto update = symbolic::parse(j["update"].get<std::string>());
15✔
773

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

780
    auto& for_node =
15✔
781
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
15✔
782
    for_node.element_id_ = j["element_id"];
15✔
783

784
    assert(j["root"].contains("type"));
15✔
785
    assert(j["root"]["type"].is_string());
15✔
786
    assert(j["root"]["type"] == "sequence");
15✔
787
    json_to_sequence(j["root"], builder, for_node.root());
15✔
788
}
15✔
789

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

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

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

838
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
839
    while_node.element_id_ = j["element_id"];
3✔
840

841
    assert(j["root"]["type"] == "sequence");
3✔
842
    json_to_sequence(j["root"], builder, while_node.root());
3✔
843
}
3✔
844

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

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

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

893
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
894

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

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

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

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

925
    std::string data = j["data"];
1✔
926
    std::unique_ptr<types::IType> data_type = nullptr;
1✔
927
    if (j.contains("data_type")) {
1✔
928
        data_type = json_to_type(j["data_type"]);
×
929
    }
×
930

931
    if (data_type == nullptr) {
1✔
932
        auto& node = builder.add_return(parent, data, assignments, json_to_debug_info(j["debug_info"]));
1✔
933
        node.element_id_ = j["element_id"];
1✔
934
    } else {
1✔
935
        auto& node =
×
936
            builder.add_constant_return(parent, data, *data_type, assignments, json_to_debug_info(j["debug_info"]));
×
937
        node.element_id_ = j["element_id"];
×
938
    }
939
}
1✔
940

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

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

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

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

1072
types::StorageType JSONSerializer::json_to_storage_type(const nlohmann::json& j) {
198✔
1073
    if (!j.contains("value")) {
198✔
1074
        return types::StorageType::CPU_Stack();
×
1075
    }
1076
    std::string value = j["value"].get<std::string>();
198✔
1077

1078
    symbolic::Expression allocation_size = SymEngine::null;
198✔
1079
    if (j.contains("allocation_size")) {
198✔
1080
        allocation_size = symbolic::parse(j["allocation_size"].get<std::string>());
×
1081
    }
×
1082

1083
    types::StorageType::AllocationType allocation = j["allocation"];
198✔
1084
    types::StorageType::AllocationType deallocation = j["deallocation"];
198✔
1085

1086
    return types::StorageType(j["value"].get<std::string>(), allocation_size, allocation, deallocation);
198✔
1087
}
198✔
1088

1089
std::string JSONSerializer::expression(const symbolic::Expression expr) {
187✔
1090
    JSONSymbolicPrinter printer;
187✔
1091
    return printer.apply(expr);
187✔
1092
};
187✔
1093

1094
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1095
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1096
    str_ = parenthesize(str_);
×
1097
};
×
1098

1099
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1100
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1101
    str_ = parenthesize(str_);
×
1102
};
×
1103

1104
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1105
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1106
    str_ = parenthesize(str_);
×
1107
};
×
1108

1109
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
18✔
1110
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
18✔
1111
    str_ = parenthesize(str_);
18✔
1112
};
18✔
1113

1114
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1115
    std::ostringstream s;
×
1116
    auto container = x.get_args();
×
1117
    if (container.size() == 1) {
×
1118
        s << apply(*container.begin());
×
1119
    } else {
×
1120
        s << "min(";
×
1121
        s << apply(*container.begin());
×
1122

1123
        // Recursively apply __daisy_min to the arguments
1124
        SymEngine::vec_basic subargs;
×
1125
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1126
            subargs.push_back(*it);
×
1127
        }
×
1128
        auto submin = SymEngine::min(subargs);
×
1129
        s << ", " << apply(submin);
×
1130

1131
        s << ")";
×
1132
    }
×
1133

1134
    str_ = s.str();
×
1135
};
×
1136

1137
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1138
    std::ostringstream s;
×
1139
    auto container = x.get_args();
×
1140
    if (container.size() == 1) {
×
1141
        s << apply(*container.begin());
×
1142
    } else {
×
1143
        s << "max(";
×
1144
        s << apply(*container.begin());
×
1145

1146
        // Recursively apply __daisy_max to the arguments
1147
        SymEngine::vec_basic subargs;
×
1148
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1149
            subargs.push_back(*it);
×
1150
        }
×
1151
        auto submax = SymEngine::max(subargs);
×
1152
        s << ", " << apply(submax);
×
1153

1154
        s << ")";
×
1155
    }
×
1156

1157
    str_ = s.str();
×
1158
};
×
1159

1160
void LibraryNodeSerializerRegistry::
1161
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
60✔
1162
    std::lock_guard<std::mutex> lock(mutex_);
60✔
1163
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
60✔
1164
        throw std::runtime_error(
×
1165
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1166
        );
1167
    }
1168
    factory_map_[library_node_code] = std::move(fn);
60✔
1169
}
60✔
1170

1171
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1172
    auto it = factory_map_.find(library_node_code);
1✔
1173
    if (it != factory_map_.end()) {
1✔
1174
        return it->second;
1✔
1175
    }
1176
    return nullptr;
×
1177
}
1✔
1178

1179
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1180

1181
void register_default_serializers() {
2✔
1182
    // stdlib
1183
    LibraryNodeSerializerRegistry::instance()
2✔
1184
        .register_library_node_serializer(stdlib::LibraryNodeType_Alloca.value(), []() {
2✔
1185
            return std::make_unique<stdlib::AllocaNodeSerializer>();
×
1186
        });
1187
    LibraryNodeSerializerRegistry::instance()
2✔
1188
        .register_library_node_serializer(stdlib::LibraryNodeType_Calloc.value(), []() {
2✔
1189
            return std::make_unique<stdlib::CallocNodeSerializer>();
×
1190
        });
1191
    LibraryNodeSerializerRegistry::instance()
2✔
1192
        .register_library_node_serializer(stdlib::LibraryNodeType_Free.value(), []() {
2✔
1193
            return std::make_unique<stdlib::FreeNodeSerializer>();
×
1194
        });
1195
    LibraryNodeSerializerRegistry::instance()
2✔
1196
        .register_library_node_serializer(stdlib::LibraryNodeType_Malloc.value(), []() {
2✔
1197
            return std::make_unique<stdlib::MallocNodeSerializer>();
×
1198
        });
1199
    LibraryNodeSerializerRegistry::instance()
2✔
1200
        .register_library_node_serializer(stdlib::LibraryNodeType_Memcpy.value(), []() {
2✔
1201
            return std::make_unique<stdlib::MemcpyNodeSerializer>();
×
1202
        });
1203
    LibraryNodeSerializerRegistry::instance()
2✔
1204
        .register_library_node_serializer(stdlib::LibraryNodeType_Memmove.value(), []() {
2✔
1205
            return std::make_unique<stdlib::MemmoveNodeSerializer>();
×
1206
        });
1207
    LibraryNodeSerializerRegistry::instance()
2✔
1208
        .register_library_node_serializer(stdlib::LibraryNodeType_Memset.value(), []() {
2✔
1209
            return std::make_unique<stdlib::MemsetNodeSerializer>();
×
1210
        });
1211
    LibraryNodeSerializerRegistry::instance()
2✔
1212
        .register_library_node_serializer(stdlib::LibraryNodeType_Trap.value(), []() {
2✔
NEW
1213
            return std::make_unique<stdlib::TrapNodeSerializer>();
×
1214
        });
1215
    LibraryNodeSerializerRegistry::instance()
2✔
1216
        .register_library_node_serializer(stdlib::LibraryNodeType_Unreachable.value(), []() {
2✔
NEW
1217
            return std::make_unique<stdlib::UnreachableNodeSerializer>();
×
1218
        });
1219

1220
    // Metadata
1221
    LibraryNodeSerializerRegistry::instance()
2✔
1222
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
1223
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1224
        });
1225

1226
    // Barrier
1227
    LibraryNodeSerializerRegistry::instance()
2✔
1228
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1229
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1230
        });
1231

1232
    // Call Node
1233
    LibraryNodeSerializerRegistry::instance()
2✔
1234
        .register_library_node_serializer(data_flow::LibraryNodeType_Call.value(), []() {
2✔
1235
            return std::make_unique<data_flow::CallNodeSerializer>();
×
1236
        });
1237
    LibraryNodeSerializerRegistry::instance()
2✔
1238
        .register_library_node_serializer(data_flow::LibraryNodeType_Invoke.value(), []() {
2✔
1239
            return std::make_unique<data_flow::InvokeNodeSerializer>();
×
1240
        });
1241

1242
    // Intrinsic
1243
    LibraryNodeSerializerRegistry::instance()
2✔
1244
        .register_library_node_serializer(math::LibraryNodeType_Intrinsic.value(), []() {
2✔
1245
            return std::make_unique<math::IntrinsicNodeSerializer>();
×
1246
        });
1247

1248
    // BLAS
1249
    LibraryNodeSerializerRegistry::instance()
2✔
1250
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1251
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1252
        });
1253
    LibraryNodeSerializerRegistry::instance()
2✔
1254
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1255
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1256
        });
1257

1258
    // ML
1259
    LibraryNodeSerializerRegistry::instance()
2✔
1260
        .register_library_node_serializer(math::ml::LibraryNodeType_Abs.value(), []() {
2✔
1261
            return std::make_unique<math::ml::AbsNodeSerializer>();
×
1262
        });
1263
    LibraryNodeSerializerRegistry::instance()
2✔
1264
        .register_library_node_serializer(math::ml::LibraryNodeType_Add.value(), []() {
2✔
1265
            return std::make_unique<math::ml::AddNodeSerializer>();
×
1266
        });
1267
    LibraryNodeSerializerRegistry::instance()
2✔
1268
        .register_library_node_serializer(math::ml::LibraryNodeType_Div.value(), []() {
2✔
1269
            return std::make_unique<math::ml::DivNodeSerializer>();
×
1270
        });
1271
    LibraryNodeSerializerRegistry::instance()
2✔
1272
        .register_library_node_serializer(math::ml::LibraryNodeType_Elu.value(), []() {
2✔
1273
            return std::make_unique<math::ml::EluNodeSerializer>();
×
1274
        });
1275
    LibraryNodeSerializerRegistry::instance()
2✔
1276
        .register_library_node_serializer(math::ml::LibraryNodeType_Erf.value(), []() {
2✔
1277
            return std::make_unique<math::ml::ErfNodeSerializer>();
×
1278
        });
1279
    LibraryNodeSerializerRegistry::instance()
2✔
1280
        .register_library_node_serializer(math::ml::LibraryNodeType_HardSigmoid.value(), []() {
2✔
1281
            return std::make_unique<math::ml::HardSigmoidNodeSerializer>();
×
1282
        });
1283
    LibraryNodeSerializerRegistry::instance()
2✔
1284
        .register_library_node_serializer(math::ml::LibraryNodeType_LeakyReLU.value(), []() {
2✔
1285
            return std::make_unique<math::ml::LeakyReLUNodeSerializer>();
×
1286
        });
1287
    LibraryNodeSerializerRegistry::instance()
2✔
1288
        .register_library_node_serializer(math::ml::LibraryNodeType_Mul.value(), []() {
2✔
1289
            return std::make_unique<math::ml::MulNodeSerializer>();
×
1290
        });
1291
    LibraryNodeSerializerRegistry::instance()
2✔
1292
        .register_library_node_serializer(math::ml::LibraryNodeType_Pow.value(), []() {
2✔
1293
            return std::make_unique<math::ml::PowNodeSerializer>();
×
1294
        });
1295
    LibraryNodeSerializerRegistry::instance()
2✔
1296
        .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
2✔
1297
            return std::make_unique<math::ml::ReLUNodeSerializer>();
×
1298
        });
1299
    LibraryNodeSerializerRegistry::instance()
2✔
1300
        .register_library_node_serializer(math::ml::LibraryNodeType_Sigmoid.value(), []() {
2✔
1301
            return std::make_unique<math::ml::SigmoidNodeSerializer>();
×
1302
        });
1303
    LibraryNodeSerializerRegistry::instance()
2✔
1304
        .register_library_node_serializer(math::ml::LibraryNodeType_Sqrt.value(), []() {
2✔
1305
            return std::make_unique<math::ml::SqrtNodeSerializer>();
×
1306
        });
1307
    LibraryNodeSerializerRegistry::instance()
2✔
1308
        .register_library_node_serializer(math::ml::LibraryNodeType_Sub.value(), []() {
2✔
1309
            return std::make_unique<math::ml::SubNodeSerializer>();
×
1310
        });
1311
    LibraryNodeSerializerRegistry::instance()
2✔
1312
        .register_library_node_serializer(math::ml::LibraryNodeType_Tanh.value(), []() {
2✔
1313
            return std::make_unique<math::ml::TanhNodeSerializer>();
×
1314
        });
1315
}
2✔
1316

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

© 2025 Coveralls, Inc