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

daisytuner / sdfglib / 16779684622

06 Aug 2025 02:21PM UTC coverage: 64.3% (-1.0%) from 65.266%
16779684622

push

github

web-flow
Merge pull request #172 from daisytuner/opaque-pointers

Opaque pointers, typed memlets, untyped tasklet connectors

330 of 462 new or added lines in 38 files covered. (71.43%)

382 existing lines in 30 files now uncovered.

8865 of 13787 relevant lines covered (64.3%)

116.73 hits per line

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

80.72
/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/math/math.h"
10

11
#include "sdfg/data_flow/library_nodes/barrier_local_node.h"
12
#include "sdfg/data_flow/library_nodes/metadata_node.h"
13

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

32
namespace sdfg {
33
namespace serializer {
34

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

42
    return FunctionType(str);
×
43
}
4✔
44

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

60
    return types::StorageType(str);
×
61
}
38✔
62

63
structured_control_flow::ScheduleType schedule_type_from_string(const std::string& str) {
1✔
64
    if (str == structured_control_flow::ScheduleType_Sequential.value()) {
1✔
65
        return structured_control_flow::ScheduleType_Sequential;
1✔
66
    } else if (str == structured_control_flow::ScheduleType_CPU_Parallel.value()) {
×
67
        return structured_control_flow::ScheduleType_CPU_Parallel;
×
68
    }
69

70
    return structured_control_flow::ScheduleType(str);
×
71
}
1✔
72

73
/*
74
 * * JSONSerializer class
75
 * * Serialization logic
76
 */
77

78
nlohmann::json JSONSerializer::serialize(const sdfg::StructuredSDFG& sdfg) {
4✔
79
    nlohmann::json j;
4✔
80

81
    j["name"] = sdfg.name();
4✔
82
    j["element_counter"] = sdfg.element_counter();
4✔
83
    j["type"] = std::string(sdfg.type().value());
4✔
84

85
    j["structures"] = nlohmann::json::array();
4✔
86
    for (const auto& structure_name : sdfg.structures()) {
5✔
87
        const auto& structure = sdfg.structure(structure_name);
1✔
88
        nlohmann::json structure_json;
1✔
89
        structure_definition_to_json(structure_json, structure);
1✔
90
        j["structures"].push_back(structure_json);
1✔
91
    }
1✔
92

93
    j["containers"] = nlohmann::json::object();
4✔
94
    for (const auto& container : sdfg.containers()) {
13✔
95
        nlohmann::json desc;
9✔
96
        type_to_json(desc, sdfg.type(container));
9✔
97
        j["containers"][container] = desc;
9✔
98
    }
9✔
99

100
    j["arguments"] = nlohmann::json::array();
4✔
101
    for (const auto& argument : sdfg.arguments()) {
7✔
102
        j["arguments"].push_back(argument);
3✔
103
    }
104

105
    j["externals"] = nlohmann::json::array();
4✔
106
    for (const auto& external : sdfg.externals()) {
5✔
107
        j["externals"].push_back(external);
1✔
108
    }
109

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

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

120
    return j;
4✔
121
}
4✔
122

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

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

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

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

165
        j["nodes"].push_back(node_json);
20✔
166
    }
20✔
167

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

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

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

178
        edge_json["src_conn"] = edge.src_conn();
15✔
179
        edge_json["dst_conn"] = edge.dst_conn();
15✔
180

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

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

191
        edge_json["end_subset"] = nlohmann::json::array();
15✔
192
        for (auto& subset : edge.end_subset()) {
21✔
193
            edge_json["end_subset"].push_back(expression(subset));
6✔
194
        }
195

196
        nlohmann::json base_type_json;
15✔
197
        type_to_json(base_type_json, edge.base_type());
15✔
198
        edge_json["base_type"] = base_type_json;
15✔
199

200
        j["edges"].push_back(edge_json);
15✔
201
    }
15✔
202
}
21✔
203

204
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
19✔
205
    j["type"] = "block";
19✔
206
    j["element_id"] = block.element_id();
19✔
207

208
    j["debug_info"] = nlohmann::json::object();
19✔
209
    debug_info_to_json(j["debug_info"], block.debug_info());
19✔
210

211
    nlohmann::json dataflow_json;
19✔
212
    dataflow_to_json(dataflow_json, block.dataflow());
19✔
213
    j["dataflow"] = dataflow_json;
19✔
214
}
19✔
215

216
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
217
    j["type"] = "for";
2✔
218
    j["element_id"] = for_node.element_id();
2✔
219

220
    j["debug_info"] = nlohmann::json::object();
2✔
221
    debug_info_to_json(j["debug_info"], for_node.debug_info());
2✔
222

223
    j["indvar"] = expression(for_node.indvar());
2✔
224
    j["init"] = expression(for_node.init());
2✔
225
    j["condition"] = expression(for_node.condition());
2✔
226
    j["update"] = expression(for_node.update());
2✔
227

228
    nlohmann::json body_json;
2✔
229
    sequence_to_json(body_json, for_node.root());
2✔
230
    j["root"] = body_json;
2✔
231
}
2✔
232

233
void JSONSerializer::if_else_to_json(nlohmann::json& j, const structured_control_flow::IfElse& if_else_node) {
2✔
234
    j["type"] = "if_else";
2✔
235
    j["element_id"] = if_else_node.element_id();
2✔
236

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

240
    j["branches"] = nlohmann::json::array();
2✔
241
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
242
        nlohmann::json branch_json;
4✔
243
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
244
        nlohmann::json body_json;
4✔
245
        sequence_to_json(body_json, if_else_node.at(i).first);
4✔
246
        branch_json["root"] = body_json;
4✔
247
        j["branches"].push_back(branch_json);
4✔
248
    }
4✔
249
}
2✔
250

251
void JSONSerializer::while_node_to_json(nlohmann::json& j, const structured_control_flow::While& while_node) {
5✔
252
    j["type"] = "while";
5✔
253
    j["element_id"] = while_node.element_id();
5✔
254

255
    j["debug_info"] = nlohmann::json::object();
5✔
256
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
257

258
    nlohmann::json body_json;
5✔
259
    sequence_to_json(body_json, while_node.root());
5✔
260
    j["root"] = body_json;
5✔
261
}
5✔
262

263
void JSONSerializer::break_node_to_json(nlohmann::json& j, const structured_control_flow::Break& break_node) {
2✔
264
    j["type"] = "break";
2✔
265
    j["element_id"] = break_node.element_id();
2✔
266

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

271
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
272
    j["type"] = "continue";
2✔
273
    j["element_id"] = continue_node.element_id();
2✔
274

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

279
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
280
    j["type"] = "map";
2✔
281
    j["element_id"] = map_node.element_id();
2✔
282

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

286
    j["indvar"] = expression(map_node.indvar());
2✔
287
    j["init"] = expression(map_node.init());
2✔
288
    j["condition"] = expression(map_node.condition());
2✔
289
    j["update"] = expression(map_node.update());
2✔
290

291
    j["schedule_type"] = std::string(map_node.schedule_type().value());
2✔
292

293
    nlohmann::json body_json;
2✔
294
    sequence_to_json(body_json, map_node.root());
2✔
295
    j["root"] = body_json;
2✔
296
}
2✔
297

298
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
299
    j["type"] = "return";
2✔
300
    j["element_id"] = return_node.element_id();
2✔
301

302
    j["debug_info"] = nlohmann::json::object();
2✔
303
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
304
}
2✔
305

306
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
20✔
307
    j["type"] = "sequence";
20✔
308
    j["element_id"] = sequence.element_id();
20✔
309

310
    j["debug_info"] = nlohmann::json::object();
20✔
311
    debug_info_to_json(j["debug_info"], sequence.debug_info());
20✔
312

313
    j["children"] = nlohmann::json::array();
20✔
314
    j["transitions"] = nlohmann::json::array();
20✔
315

316
    for (size_t i = 0; i < sequence.size(); i++) {
41✔
317
        nlohmann::json child_json;
21✔
318
        auto& child = sequence.at(i).first;
21✔
319
        auto& transition = sequence.at(i).second;
21✔
320

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

343
        j["children"].push_back(child_json);
21✔
344

345
        // Add transition information
346
        nlohmann::json transition_json;
21✔
347
        transition_json["type"] = "transition";
21✔
348
        transition_json["element_id"] = transition.element_id();
21✔
349

350
        transition_json["debug_info"] = nlohmann::json::object();
21✔
351
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
21✔
352

353
        transition_json["assignments"] = nlohmann::json::array();
21✔
354
        for (const auto& assignment : transition.assignments()) {
24✔
355
            nlohmann::json assignment_json;
3✔
356
            assignment_json["symbol"] = expression(assignment.first);
3✔
357
            assignment_json["expression"] = expression(assignment.second);
3✔
358
            transition_json["assignments"].push_back(assignment_json);
3✔
359
        }
3✔
360

361
        j["transitions"].push_back(transition_json);
21✔
362
    }
21✔
363
}
20✔
364

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

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

428
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
112✔
429
    j["has"] = debug_info.has();
112✔
430
    j["filename"] = debug_info.filename();
112✔
431
    j["start_line"] = debug_info.start_line();
112✔
432
    j["start_column"] = debug_info.start_column();
112✔
433
    j["end_line"] = debug_info.end_line();
112✔
434
    j["end_column"] = debug_info.end_column();
112✔
435
}
112✔
436

437
/*
438
 * * Deserialization logic
439
 */
440

441
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
4✔
442
    assert(j.contains("name"));
4✔
443
    assert(j["name"].is_string());
4✔
444
    assert(j.contains("type"));
4✔
445
    assert(j["type"].is_string());
4✔
446
    assert(j["element_counter"].is_number_integer());
4✔
447

448
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
4✔
449
    builder::StructuredSDFGBuilder builder(j["name"], function_type);
4✔
450

451
    size_t element_counter = j["element_counter"];
4✔
452
    builder.set_element_counter(element_counter);
4✔
453

454
    // deserialize structures
455
    assert(j.contains("structures"));
4✔
456
    assert(j["structures"].is_array());
4✔
457
    for (const auto& structure : j["structures"]) {
5✔
458
        assert(structure.contains("name"));
1✔
459
        assert(structure["name"].is_string());
1✔
460
        json_to_structure_definition(structure, builder);
1✔
461
    }
462

463
    nlohmann::json& containers = j["containers"];
4✔
464

465
    // deserialize externals
466
    for (const auto& name : j["externals"]) {
5✔
467
        auto& type_desc = containers.at(name.get<std::string>());
1✔
468
        auto type = json_to_type(type_desc);
1✔
469
        builder.add_container(name, *type, false, true);
1✔
470
    }
1✔
471

472
    // deserialize arguments
473
    for (const auto& name : j["arguments"]) {
7✔
474
        auto& type_desc = containers.at(name.get<std::string>());
3✔
475
        auto type = json_to_type(type_desc);
3✔
476
        builder.add_container(name, *type, true, false);
3✔
477
    }
3✔
478

479
    // deserialize transients
480
    for (const auto& entry : containers.items()) {
13✔
481
        if (builder.subject().is_argument(entry.key())) {
9✔
482
            continue;
3✔
483
        }
484
        if (builder.subject().is_external(entry.key())) {
6✔
485
            continue;
1✔
486
        }
487
        auto type = json_to_type(entry.value());
5✔
488
        builder.add_container(entry.key(), *type, false, false);
5✔
489
    }
5✔
490

491
    // deserialize root node
492
    assert(j.contains("root"));
4✔
493
    auto& root = builder.subject().root();
4✔
494
    json_to_sequence(j["root"], builder, root);
4✔
495

496
    // deserialize metadata
497
    assert(j.contains("metadata"));
4✔
498
    assert(j["metadata"].is_object());
4✔
499
    for (const auto& entry : j["metadata"].items()) {
5✔
500
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
501
    }
502

503
    builder.set_element_counter(element_counter);
4✔
504

505
    return builder.move();
4✔
506
}
4✔
507

508
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
2✔
509
    assert(j.contains("name"));
2✔
510
    assert(j["name"].is_string());
2✔
511
    assert(j.contains("members"));
2✔
512
    assert(j["members"].is_array());
2✔
513
    assert(j.contains("is_packed"));
2✔
514
    assert(j["is_packed"].is_boolean());
2✔
515
    auto is_packed = j["is_packed"];
2✔
516
    auto& definition = builder.add_structure(j["name"], is_packed);
2✔
517
    for (const auto& member : j["members"]) {
4✔
518
        nlohmann::json member_json;
2✔
519
        auto member_type = json_to_type(member);
2✔
520
        definition.add_member(*member_type);
2✔
521
    }
2✔
522
}
2✔
523

UNCOV
524
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
×
UNCOV
525
    std::vector<std::pair<std::string, types::Scalar>> arguments;
×
UNCOV
526
    for (const auto& argument : j) {
×
UNCOV
527
        assert(argument.contains("name"));
×
UNCOV
528
        assert(argument["name"].is_string());
×
UNCOV
529
        assert(argument.contains("type"));
×
UNCOV
530
        assert(argument["type"].is_object());
×
UNCOV
531
        std::string name = argument["name"];
×
UNCOV
532
        auto type = json_to_type(argument["type"]);
×
UNCOV
533
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
×
UNCOV
534
    }
×
UNCOV
535
    return arguments;
×
UNCOV
536
}
×
537

538
void JSONSerializer::json_to_dataflow(
11✔
539
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
540
) {
541
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
11✔
542

543
    assert(j.contains("nodes"));
11✔
544
    assert(j["nodes"].is_array());
11✔
545
    for (const auto& node : j["nodes"]) {
27✔
546
        assert(node.contains("type"));
16✔
547
        assert(node["type"].is_string());
16✔
548
        assert(node.contains("element_id"));
16✔
549
        assert(node["element_id"].is_number_integer());
16✔
550
        std::string type = node["type"];
16✔
551
        if (type == "tasklet") {
16✔
552
            assert(node.contains("code"));
4✔
553
            assert(node["code"].is_number_integer());
4✔
554
            assert(node.contains("inputs"));
4✔
555
            assert(node["inputs"].is_array());
4✔
556
            assert(node.contains("output"));
4✔
557
            assert(node["output"].is_string());
4✔
558
            auto inputs = node["inputs"].get<std::vector<std::string>>();
4✔
559

560
            auto& tasklet =
4✔
561
                builder
8✔
562
                    .add_tasklet(parent, node["code"], node["output"], inputs, json_to_debug_info(node["debug_info"]));
4✔
563
            tasklet.element_id_ = node["element_id"];
4✔
564
            nodes_map.insert({node["element_id"], tasklet});
4✔
565
        } else if (type == "library_node") {
16✔
566
            assert(node.contains("code"));
×
567
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
568

569
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
570
            if (serializer_fn == nullptr) {
×
571
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
572
            }
573
            auto serializer = serializer_fn();
×
574
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
575
            lib_node.implementation_type() =
×
576
                data_flow::ImplementationType(node["implementation_type"].get<std::string>());
×
577
            lib_node.element_id_ = node["element_id"];
×
578
            nodes_map.insert({node["element_id"], lib_node});
×
579
        } else if (type == "access_node") {
12✔
580
            assert(node.contains("data"));
12✔
581
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
582
            access_node.element_id_ = node["element_id"];
12✔
583
            nodes_map.insert({node["element_id"], access_node});
12✔
584
        } else {
12✔
585
            throw std::runtime_error("Unknown node type");
×
586
        }
587
    }
16✔
588

589
    assert(j.contains("edges"));
11✔
590
    assert(j["edges"].is_array());
11✔
591
    for (const auto& edge : j["edges"]) {
23✔
592
        assert(edge.contains("src"));
12✔
593
        assert(edge["src"].is_number_integer());
12✔
594
        assert(edge.contains("dst"));
12✔
595
        assert(edge["dst"].is_number_integer());
12✔
596
        assert(edge.contains("src_conn"));
12✔
597
        assert(edge["src_conn"].is_string());
12✔
598
        assert(edge.contains("dst_conn"));
12✔
599
        assert(edge["dst_conn"].is_string());
12✔
600
        assert(edge.contains("subset"));
12✔
601
        assert(edge["subset"].is_array());
12✔
602

603
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
604
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
605
        auto& source = nodes_map.at(edge["src"]);
12✔
606
        auto& target = nodes_map.at(edge["dst"]);
12✔
607

608
        auto base_type = json_to_type(edge["base_type"]);
12✔
609

610
        if (edge.contains("begin_subset") && edge.contains("end_subset")) {
12✔
611
            assert(edge["begin_subset"].is_array());
12✔
612
            assert(edge["end_subset"].is_array());
12✔
613
            std::vector<symbolic::Expression> begin_subset;
12✔
614
            std::vector<symbolic::Expression> end_subset;
12✔
615
            for (const auto& subset_str : edge["begin_subset"]) {
16✔
616
                assert(subset_str.is_string());
4✔
617
                SymEngine::Expression subset_expr(subset_str);
4✔
618
                begin_subset.push_back(subset_expr);
4✔
619
            }
4✔
620
            for (const auto& subset_str : edge["end_subset"]) {
16✔
621
                assert(subset_str.is_string());
4✔
622
                SymEngine::Expression subset_expr(subset_str);
4✔
623
                end_subset.push_back(subset_expr);
4✔
624
            }
4✔
625
            auto& memlet = builder.add_memlet(
24✔
626
                parent,
12✔
627
                source,
12✔
628
                edge["src_conn"],
12✔
629
                target,
12✔
630
                edge["dst_conn"],
12✔
631
                begin_subset,
632
                end_subset,
633
                *base_type,
12✔
634
                json_to_debug_info(edge["debug_info"])
12✔
635
            );
636
            memlet.element_id_ = edge["element_id"];
12✔
637
        } else if (edge.contains("subset")) {
12✔
638
            assert(edge["subset"].is_array());
×
639
            std::vector<symbolic::Expression> subset;
×
640
            for (const auto& subset_str : edge["subset"]) {
×
641
                assert(subset_str.is_string());
×
642
                SymEngine::Expression subset_expr(subset_str);
×
643
                subset.push_back(subset_expr);
×
644
            }
×
645
            auto& memlet = builder.add_memlet(
×
646
                parent,
×
647
                source,
×
648
                edge["src_conn"],
×
649
                target,
×
650
                edge["dst_conn"],
×
651
                subset,
NEW
652
                *base_type,
×
UNCOV
653
                json_to_debug_info(edge["debug_info"])
×
654
            );
655
            memlet.element_id_ = edge["element_id"];
×
656
        } else {
×
657
            throw std::runtime_error("Subsets not specified in json");
×
658
        }
659
    }
12✔
660
}
11✔
661

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

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

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

684
            assert(transition.contains("type"));
11✔
685
            assert(transition["type"].is_string());
11✔
686
            assert(transition.contains("assignments"));
11✔
687
            assert(transition["assignments"].is_array());
11✔
688
            control_flow::Assignments assignments;
11✔
689
            for (const auto& assignment : transition["assignments"]) {
13✔
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
                SymEngine::Expression expr(assignment["expression"]);
2✔
695
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
2✔
696
            }
2✔
697

698
            if (child["type"] == "block") {
11✔
699
                json_to_block_node(child, builder, sequence, assignments);
9✔
700
            } else if (child["type"] == "for") {
11✔
701
                json_to_for_node(child, builder, sequence, assignments);
×
702
            } else if (child["type"] == "if_else") {
2✔
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"]);
11✔
722
            sequence.at(i).second.element_id_ = transition["element_id"];
11✔
723
        }
11✔
724
    } else {
13✔
725
        throw std::runtime_error("expected sequence type");
×
726
    }
727
}
13✔
728

729
void JSONSerializer::json_to_block_node(
10✔
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"));
10✔
736
    assert(j["type"].is_string());
10✔
737
    assert(j.contains("dataflow"));
10✔
738
    assert(j["dataflow"].is_object());
10✔
739
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
10✔
740
    block.element_id_ = j["element_id"];
10✔
741
    assert(j["dataflow"].contains("type"));
10✔
742
    assert(j["dataflow"]["type"].is_string());
10✔
743
    std::string type = j["dataflow"]["type"];
10✔
744
    if (type == "dataflow") {
10✔
745
        json_to_dataflow(j["dataflow"], builder, block);
10✔
746
    } else {
10✔
747
        throw std::runtime_error("Unknown dataflow type");
×
748
    }
749
}
10✔
750

751
void JSONSerializer::json_to_for_node(
1✔
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"));
1✔
758
    assert(j["type"].is_string());
1✔
759
    assert(j.contains("indvar"));
1✔
760
    assert(j["indvar"].is_string());
1✔
761
    assert(j.contains("init"));
1✔
762
    assert(j["init"].is_string());
1✔
763
    assert(j.contains("condition"));
1✔
764
    assert(j["condition"].is_string());
1✔
765
    assert(j.contains("update"));
1✔
766
    assert(j["update"].is_string());
1✔
767
    assert(j.contains("root"));
1✔
768
    assert(j["root"].is_object());
1✔
769

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

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

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

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

832
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
833
    while_node.element_id_ = j["element_id"];
3✔
834

835
    assert(j["root"]["type"] == "sequence");
3✔
836
    json_to_sequence(j["root"], builder, while_node.root());
3✔
837
}
3✔
838

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

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

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

887
    structured_control_flow::ScheduleType schedule_type =
888
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
889

890
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
891
    SymEngine::Expression init(j["init"]);
1✔
892
    SymEngine::Expression condition_expr(j["condition"]);
1✔
893
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
894
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
895
    SymEngine::Expression update(j["update"]);
1✔
896

897
    auto& map_node = builder.add_map(
2✔
898
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
899
    );
900
    map_node.element_id_ = j["element_id"];
1✔
901

902
    assert(j["root"].contains("type"));
1✔
903
    assert(j["root"]["type"].is_string());
1✔
904
    assert(j["root"]["type"] == "sequence");
1✔
905
    json_to_sequence(j["root"], builder, map_node.root());
1✔
906
}
1✔
907

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

918
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
919
    node.element_id_ = j["element_id"];
1✔
920
}
1✔
921

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

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

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

1037
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
47✔
1038
    JSONSymbolicPrinter printer;
47✔
1039
    return printer.apply(expr);
47✔
1040
};
47✔
1041

1042
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1043
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1044
    str_ = parenthesize(str_);
×
1045
};
×
1046

1047
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1048
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1049
    str_ = parenthesize(str_);
×
1050
};
×
1051

1052
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1053
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1054
    str_ = parenthesize(str_);
×
1055
};
×
1056

1057
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1058
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1059
    str_ = parenthesize(str_);
4✔
1060
};
4✔
1061

1062
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1063
    std::ostringstream s;
×
1064
    auto container = x.get_args();
×
1065
    if (container.size() == 1) {
×
1066
        s << apply(*container.begin());
×
1067
    } else {
×
1068
        s << "min(";
×
1069
        s << apply(*container.begin());
×
1070

1071
        // Recursively apply __daisy_min to the arguments
1072
        SymEngine::vec_basic subargs;
×
1073
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1074
            subargs.push_back(*it);
×
1075
        }
×
1076
        auto submin = SymEngine::min(subargs);
×
1077
        s << ", " << apply(submin);
×
1078

1079
        s << ")";
×
1080
    }
×
1081

1082
    str_ = s.str();
×
1083
};
×
1084

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

1094
        // Recursively apply __daisy_max to the arguments
1095
        SymEngine::vec_basic subargs;
×
1096
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1097
            subargs.push_back(*it);
×
1098
        }
×
1099
        auto submax = SymEngine::max(subargs);
×
1100
        s << ", " << apply(submax);
×
1101

1102
        s << ")";
×
1103
    }
×
1104

1105
    str_ = s.str();
×
1106
};
×
1107

1108
void LibraryNodeSerializerRegistry::
1109
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
14✔
1110
    std::lock_guard<std::mutex> lock(mutex_);
14✔
1111
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
14✔
1112
        throw std::runtime_error(
×
1113
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1114
        );
1115
    }
1116
    factory_map_[library_node_code] = std::move(fn);
14✔
1117
}
14✔
1118

1119
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1120
    auto it = factory_map_.find(library_node_code);
1✔
1121
    if (it != factory_map_.end()) {
1✔
1122
        return it->second;
1✔
1123
    }
1124
    return nullptr;
×
1125
}
1✔
1126

1127
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1128

1129
void register_default_serializers() {
2✔
1130
    // Metadata
1131
    LibraryNodeSerializerRegistry::instance()
2✔
1132
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
1133
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1134
        });
1135

1136
    // Barrier
1137
    LibraryNodeSerializerRegistry::instance()
2✔
1138
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1139
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1140
        });
1141

1142
    // ML
1143
    LibraryNodeSerializerRegistry::instance()
2✔
1144
        .register_library_node_serializer(math::ml::LibraryNodeType_Conv.value(), []() {
2✔
1145
            return std::make_unique<math::ml::ConvNodeSerializer>();
×
1146
        });
1147
    LibraryNodeSerializerRegistry::instance()
2✔
1148
        .register_library_node_serializer(math::ml::LibraryNodeType_MaxPool.value(), []() {
2✔
1149
            return std::make_unique<math::ml::MaxPoolNodeSerializer>();
×
1150
        });
1151
    LibraryNodeSerializerRegistry::instance()
2✔
1152
        .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
2✔
1153
            return std::make_unique<math::ml::ReLUNodeSerializer>();
×
1154
        });
1155

1156
    // BLAS
1157
    LibraryNodeSerializerRegistry::instance()
2✔
1158
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1159
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1160
        });
1161
    LibraryNodeSerializerRegistry::instance()
2✔
1162
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1163
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1164
        });
1165
}
2✔
1166

1167
} // namespace serializer
1168
} // 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