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

daisytuner / sdfglib / 17264921398

27 Aug 2025 11:05AM UTC coverage: 60.053% (+0.3%) from 59.755%
17264921398

Pull #210

github

web-flow
Merge ff8271e01 into 12d242c5d
Pull Request #210: New debug info

174 of 212 new or added lines in 3 files covered. (82.08%)

1 existing line in 1 file now uncovered.

9501 of 15821 relevant lines covered (60.05%)

115.24 hits per line

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

82.53
/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) {
5✔
36
    if (str == FunctionType_CPU.value()) {
5✔
37
        return FunctionType_CPU;
5✔
38
    } else if (str == FunctionType_NV_GLOBAL.value()) {
×
39
        return FunctionType_NV_GLOBAL;
×
40
    }
41

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

45
types::StorageType storage_type_from_string(const std::string& str) {
41✔
46
    if (str == types::StorageType_CPU_Heap.value()) {
41✔
47
        return types::StorageType_CPU_Heap;
×
48
    } else if (str == types::StorageType_CPU_Stack.value()) {
41✔
49
        return types::StorageType_CPU_Stack;
41✔
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
}
41✔
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) {
5✔
79
    nlohmann::json j;
5✔
80

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

85
    j["structures"] = nlohmann::json::array();
5✔
86
    for (const auto& structure_name : sdfg.structures()) {
6✔
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();
5✔
94
    for (const auto& container : sdfg.containers()) {
17✔
95
        nlohmann::json desc;
12✔
96
        type_to_json(desc, sdfg.type(container));
12✔
97
        j["containers"][container] = desc;
12✔
98
    }
12✔
99

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

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

113
    j["metadata"] = nlohmann::json::object();
5✔
114
    for (const auto& entry : sdfg.metadata()) {
6✔
115
        j["metadata"][entry.first] = entry.second;
1✔
116
    }
117

118
    // Walk the SDFG
119
    nlohmann::json root_json;
5✔
120
    sequence_to_json(root_json, sdfg.root());
5✔
121
    j["root"] = root_json;
5✔
122

123
    return j;
5✔
124
}
5✔
125

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

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

135
        node_json["debug_info"] = nlohmann::json::object();
20✔
136
        debug_info_to_json(node_json["debug_info"], node.debug_info());
20✔
137

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

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

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

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

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

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

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

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

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

199
        nlohmann::json base_type_json;
15✔
200
        type_to_json(base_type_json, edge.base_type());
15✔
201
        edge_json["base_type"] = base_type_json;
15✔
202

203
        j["edges"].push_back(edge_json);
15✔
204
    }
15✔
205
}
21✔
206

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

211
    j["debug_info"] = nlohmann::json::object();
19✔
212
    debug_info_to_json(j["debug_info"], block.debug_info());
19✔
213

214
    nlohmann::json dataflow_json;
19✔
215
    dataflow_to_json(dataflow_json, block.dataflow());
19✔
216
    j["dataflow"] = dataflow_json;
19✔
217
}
19✔
218

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

223
    j["debug_info"] = nlohmann::json::object();
2✔
224
    debug_info_to_json(j["debug_info"], for_node.debug_info());
2✔
225

226
    j["indvar"] = expression(for_node.indvar());
2✔
227
    j["init"] = expression(for_node.init());
2✔
228
    j["condition"] = expression(for_node.condition());
2✔
229
    j["update"] = expression(for_node.update());
2✔
230

231
    nlohmann::json body_json;
2✔
232
    sequence_to_json(body_json, for_node.root());
2✔
233
    j["root"] = body_json;
2✔
234
}
2✔
235

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

240
    j["debug_info"] = nlohmann::json::object();
2✔
241
    debug_info_to_json(j["debug_info"], if_else_node.debug_info());
2✔
242

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

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

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

261
    nlohmann::json body_json;
5✔
262
    sequence_to_json(body_json, while_node.root());
5✔
263
    j["root"] = body_json;
5✔
264
}
5✔
265

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

270
    j["debug_info"] = nlohmann::json::object();
2✔
271
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
272
}
2✔
273

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

278
    j["debug_info"] = nlohmann::json::object();
2✔
279
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
280
}
2✔
281

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

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

289
    j["indvar"] = expression(map_node.indvar());
2✔
290
    j["init"] = expression(map_node.init());
2✔
291
    j["condition"] = expression(map_node.condition());
2✔
292
    j["update"] = expression(map_node.update());
2✔
293

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

296
    nlohmann::json body_json;
2✔
297
    sequence_to_json(body_json, map_node.root());
2✔
298
    j["root"] = body_json;
2✔
299
}
2✔
300

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

305
    j["debug_info"] = nlohmann::json::object();
2✔
306
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
307
}
2✔
308

309
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
21✔
310
    j["type"] = "sequence";
21✔
311
    j["element_id"] = sequence.element_id();
21✔
312

313
    j["debug_info"] = nlohmann::json::object();
21✔
314
    debug_info_to_json(j["debug_info"], sequence.debug_info());
21✔
315

316
    j["children"] = nlohmann::json::array();
21✔
317
    j["transitions"] = nlohmann::json::array();
21✔
318

319
    for (size_t i = 0; i < sequence.size(); i++) {
42✔
320
        nlohmann::json child_json;
21✔
321
        auto& child = sequence.at(i).first;
21✔
322
        auto& transition = sequence.at(i).second;
21✔
323

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

346
        j["children"].push_back(child_json);
21✔
347

348
        // Add transition information
349
        nlohmann::json transition_json;
21✔
350
        transition_json["type"] = "transition";
21✔
351
        transition_json["element_id"] = transition.element_id();
21✔
352

353
        transition_json["debug_info"] = nlohmann::json::object();
21✔
354
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
21✔
355

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

364
        j["transitions"].push_back(transition_json);
21✔
365
    }
21✔
366
}
21✔
367

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

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

431
void JSONSerializer::debug_loc_to_json(nlohmann::json& j, const DebugLoc& loc) {
10✔
432
    j["has"] = loc.has_;
10✔
433
    if (!loc.has_) {
10✔
NEW
434
        return;
×
435
    }
436
    j["filename"] = loc.filename_;
10✔
437
    j["function"] = loc.function_;
10✔
438
    j["line"] = loc.line_;
10✔
439
    j["column"] = loc.column_;
10✔
440
}
10✔
441

442

443
void JSONSerializer::debug_info_element_to_json(nlohmann::json& j, const DebugInfoElement& debug_info_element) {
5✔
444
    j["has"] = debug_info_element.has();
5✔
445
    if (!debug_info_element.has()) {
5✔
NEW
446
        return;
×
447
    }
448
    j["locations"] = nlohmann::json::array();
5✔
449
    for (const auto& loc : debug_info_element.locations()) {
14✔
450
        nlohmann::json loc_json;
9✔
451
        debug_loc_to_json(loc_json, loc);
9✔
452
        j["locations"].push_back(loc_json);
9✔
453
    }
9✔
454
}
5✔
455

456
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
115✔
457
    j["has"] = debug_info.has();
115✔
458
    j["filename"] = debug_info.filename();
115✔
459
    j["function"] = debug_info.function();
115✔
460
    j["start_line"] = debug_info.start_line();
115✔
461
    j["start_column"] = debug_info.start_column();
115✔
462
    j["end_line"] = debug_info.end_line();
115✔
463
    j["end_column"] = debug_info.end_column();
115✔
464

465
    j["instructions"] = nlohmann::json::array();
115✔
466
    for (const auto& instruction : debug_info.instructions()) {
119✔
467
        nlohmann::json instruction_json;
4✔
468
        debug_info_element_to_json(instruction_json, instruction);
4✔
469
        j["instructions"].push_back(instruction_json);
4✔
470
    }
4✔
471
}
115✔
472

473
/*
474
 * * Deserialization logic
475
 */
476

477
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
5✔
478
    assert(j.contains("name"));
5✔
479
    assert(j["name"].is_string());
5✔
480
    assert(j.contains("type"));
5✔
481
    assert(j["type"].is_string());
5✔
482
    assert(j["element_counter"].is_number_integer());
5✔
483

484
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
5✔
485
    builder::StructuredSDFGBuilder builder(j["name"], function_type);
5✔
486

487
    size_t element_counter = j["element_counter"];
5✔
488
    builder.set_element_counter(element_counter);
5✔
489

490
    // deserialize structures
491
    assert(j.contains("structures"));
5✔
492
    assert(j["structures"].is_array());
5✔
493
    for (const auto& structure : j["structures"]) {
6✔
494
        assert(structure.contains("name"));
1✔
495
        assert(structure["name"].is_string());
1✔
496
        json_to_structure_definition(structure, builder);
1✔
497
    }
498

499
    nlohmann::json& containers = j["containers"];
5✔
500

501
    // deserialize externals
502
    for (const auto& external : j["externals"]) {
9✔
503
        assert(external.contains("name"));
4✔
504
        assert(external["name"].is_string());
4✔
505
        assert(external.contains("linkage_type"));
4✔
506
        assert(external["linkage_type"].is_number_integer());
4✔
507
        auto& type_desc = containers.at(external["name"].get<std::string>());
4✔
508
        auto type = json_to_type(type_desc);
4✔
509
        builder.add_external(external["name"], *type, LinkageType(external["linkage_type"]));
4✔
510
    }
4✔
511

512
    // deserialize arguments
513
    for (const auto& name : j["arguments"]) {
8✔
514
        auto& type_desc = containers.at(name.get<std::string>());
3✔
515
        auto type = json_to_type(type_desc);
3✔
516
        builder.add_container(name, *type, true, false);
3✔
517
    }
3✔
518

519
    // deserialize transients
520
    for (const auto& entry : containers.items()) {
17✔
521
        if (builder.subject().is_argument(entry.key())) {
12✔
522
            continue;
3✔
523
        }
524
        if (builder.subject().is_external(entry.key())) {
9✔
525
            continue;
4✔
526
        }
527
        auto type = json_to_type(entry.value());
5✔
528
        builder.add_container(entry.key(), *type, false, false);
5✔
529
    }
5✔
530

531
    // deserialize root node
532
    assert(j.contains("root"));
5✔
533
    auto& root = builder.subject().root();
5✔
534
    json_to_sequence(j["root"], builder, root);
5✔
535

536
    // deserialize metadata
537
    assert(j.contains("metadata"));
5✔
538
    assert(j["metadata"].is_object());
5✔
539
    for (const auto& entry : j["metadata"].items()) {
6✔
540
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
541
    }
542

543
    builder.set_element_counter(element_counter);
5✔
544

545
    return builder.move();
5✔
546
}
5✔
547

548
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
2✔
549
    assert(j.contains("name"));
2✔
550
    assert(j["name"].is_string());
2✔
551
    assert(j.contains("members"));
2✔
552
    assert(j["members"].is_array());
2✔
553
    assert(j.contains("is_packed"));
2✔
554
    assert(j["is_packed"].is_boolean());
2✔
555
    auto is_packed = j["is_packed"];
2✔
556
    auto& definition = builder.add_structure(j["name"], is_packed);
2✔
557
    for (const auto& member : j["members"]) {
4✔
558
        nlohmann::json member_json;
2✔
559
        auto member_type = json_to_type(member);
2✔
560
        definition.add_member(*member_type);
2✔
561
    }
2✔
562
}
2✔
563

564
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
×
565
    std::vector<std::pair<std::string, types::Scalar>> arguments;
×
566
    for (const auto& argument : j) {
×
567
        assert(argument.contains("name"));
×
568
        assert(argument["name"].is_string());
×
569
        assert(argument.contains("type"));
×
570
        assert(argument["type"].is_object());
×
571
        std::string name = argument["name"];
×
572
        auto type = json_to_type(argument["type"]);
×
573
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
×
574
    }
×
575
    return arguments;
×
576
}
×
577

578
void JSONSerializer::json_to_dataflow(
11✔
579
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
580
) {
581
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
11✔
582

583
    assert(j.contains("nodes"));
11✔
584
    assert(j["nodes"].is_array());
11✔
585
    for (const auto& node : j["nodes"]) {
27✔
586
        assert(node.contains("type"));
16✔
587
        assert(node["type"].is_string());
16✔
588
        assert(node.contains("element_id"));
16✔
589
        assert(node["element_id"].is_number_integer());
16✔
590
        std::string type = node["type"];
16✔
591
        if (type == "tasklet") {
16✔
592
            assert(node.contains("code"));
4✔
593
            assert(node["code"].is_number_integer());
4✔
594
            assert(node.contains("inputs"));
4✔
595
            assert(node["inputs"].is_array());
4✔
596
            assert(node.contains("output"));
4✔
597
            assert(node["output"].is_string());
4✔
598
            auto inputs = node["inputs"].get<std::vector<std::string>>();
4✔
599

600
            auto& tasklet =
4✔
601
                builder
8✔
602
                    .add_tasklet(parent, node["code"], node["output"], inputs, json_to_debug_info(node["debug_info"]));
4✔
603
            tasklet.element_id_ = node["element_id"];
4✔
604
            nodes_map.insert({node["element_id"], tasklet});
4✔
605
        } else if (type == "library_node") {
16✔
606
            assert(node.contains("code"));
×
607
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
608

609
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
610
            if (serializer_fn == nullptr) {
×
611
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
612
            }
613
            auto serializer = serializer_fn();
×
614
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
615
            lib_node.implementation_type() =
×
616
                data_flow::ImplementationType(node["implementation_type"].get<std::string>());
×
617
            lib_node.element_id_ = node["element_id"];
×
618
            nodes_map.insert({node["element_id"], lib_node});
×
619
        } else if (type == "access_node") {
12✔
620
            assert(node.contains("data"));
12✔
621
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
622
            access_node.element_id_ = node["element_id"];
12✔
623
            nodes_map.insert({node["element_id"], access_node});
12✔
624
        } else {
12✔
625
            throw std::runtime_error("Unknown node type");
×
626
        }
627
    }
16✔
628

629
    assert(j.contains("edges"));
11✔
630
    assert(j["edges"].is_array());
11✔
631
    for (const auto& edge : j["edges"]) {
23✔
632
        assert(edge.contains("src"));
12✔
633
        assert(edge["src"].is_number_integer());
12✔
634
        assert(edge.contains("dst"));
12✔
635
        assert(edge["dst"].is_number_integer());
12✔
636
        assert(edge.contains("src_conn"));
12✔
637
        assert(edge["src_conn"].is_string());
12✔
638
        assert(edge.contains("dst_conn"));
12✔
639
        assert(edge["dst_conn"].is_string());
12✔
640
        assert(edge.contains("subset"));
12✔
641
        assert(edge["subset"].is_array());
12✔
642

643
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
644
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
645
        auto& source = nodes_map.at(edge["src"]);
12✔
646
        auto& target = nodes_map.at(edge["dst"]);
12✔
647

648
        auto base_type = json_to_type(edge["base_type"]);
12✔
649

650
        if (edge.contains("begin_subset") && edge.contains("end_subset")) {
12✔
651
            assert(edge["begin_subset"].is_array());
12✔
652
            assert(edge["end_subset"].is_array());
12✔
653
            std::vector<symbolic::Expression> begin_subset;
12✔
654
            std::vector<symbolic::Expression> end_subset;
12✔
655
            for (const auto& subset_str : edge["begin_subset"]) {
16✔
656
                assert(subset_str.is_string());
4✔
657
                SymEngine::Expression subset_expr(subset_str);
4✔
658
                begin_subset.push_back(subset_expr);
4✔
659
            }
4✔
660
            for (const auto& subset_str : edge["end_subset"]) {
16✔
661
                assert(subset_str.is_string());
4✔
662
                SymEngine::Expression subset_expr(subset_str);
4✔
663
                end_subset.push_back(subset_expr);
4✔
664
            }
4✔
665
            auto& memlet = builder.add_memlet(
24✔
666
                parent,
12✔
667
                source,
12✔
668
                edge["src_conn"],
12✔
669
                target,
12✔
670
                edge["dst_conn"],
12✔
671
                begin_subset,
672
                end_subset,
673
                *base_type,
12✔
674
                json_to_debug_info(edge["debug_info"])
12✔
675
            );
676
            memlet.element_id_ = edge["element_id"];
12✔
677
        } else if (edge.contains("subset")) {
12✔
678
            assert(edge["subset"].is_array());
×
679
            std::vector<symbolic::Expression> subset;
×
680
            for (const auto& subset_str : edge["subset"]) {
×
681
                assert(subset_str.is_string());
×
682
                SymEngine::Expression subset_expr(subset_str);
×
683
                subset.push_back(subset_expr);
×
684
            }
×
685
            auto& memlet = builder.add_memlet(
×
686
                parent,
×
687
                source,
×
688
                edge["src_conn"],
×
689
                target,
×
690
                edge["dst_conn"],
×
691
                subset,
692
                *base_type,
×
693
                json_to_debug_info(edge["debug_info"])
×
694
            );
695
            memlet.element_id_ = edge["element_id"];
×
696
        } else {
×
697
            throw std::runtime_error("Subsets not specified in json");
×
698
        }
699
    }
12✔
700
}
11✔
701

702
void JSONSerializer::json_to_sequence(
14✔
703
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
704
) {
705
    assert(j.contains("type"));
14✔
706
    assert(j["type"].is_string());
14✔
707
    assert(j.contains("children"));
14✔
708
    assert(j["children"].is_array());
14✔
709
    assert(j.contains("transitions"));
14✔
710
    assert(j["transitions"].is_array());
14✔
711
    assert(j["transitions"].size() == j["children"].size());
14✔
712

713
    sequence.element_id_ = j["element_id"];
14✔
714
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
14✔
715

716
    std::string type = j["type"];
14✔
717
    if (type == "sequence") {
14✔
718
        for (size_t i = 0; i < j["children"].size(); i++) {
25✔
719
            auto& child = j["children"][i];
11✔
720
            auto& transition = j["transitions"][i];
11✔
721
            assert(child.contains("type"));
11✔
722
            assert(child["type"].is_string());
11✔
723

724
            assert(transition.contains("type"));
11✔
725
            assert(transition["type"].is_string());
11✔
726
            assert(transition.contains("assignments"));
11✔
727
            assert(transition["assignments"].is_array());
11✔
728
            control_flow::Assignments assignments;
11✔
729
            for (const auto& assignment : transition["assignments"]) {
13✔
730
                assert(assignment.contains("symbol"));
2✔
731
                assert(assignment["symbol"].is_string());
2✔
732
                assert(assignment.contains("expression"));
2✔
733
                assert(assignment["expression"].is_string());
2✔
734
                SymEngine::Expression expr(assignment["expression"]);
2✔
735
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
2✔
736
            }
2✔
737

738
            if (child["type"] == "block") {
11✔
739
                json_to_block_node(child, builder, sequence, assignments);
9✔
740
            } else if (child["type"] == "for") {
11✔
741
                json_to_for_node(child, builder, sequence, assignments);
×
742
            } else if (child["type"] == "if_else") {
2✔
743
                json_to_if_else_node(child, builder, sequence, assignments);
×
744
            } else if (child["type"] == "while") {
2✔
745
                json_to_while_node(child, builder, sequence, assignments);
×
746
            } else if (child["type"] == "break") {
2✔
747
                json_to_break_node(child, builder, sequence, assignments);
1✔
748
            } else if (child["type"] == "continue") {
2✔
749
                json_to_continue_node(child, builder, sequence, assignments);
1✔
750
            } else if (child["type"] == "return") {
1✔
751
                json_to_return_node(child, builder, sequence, assignments);
×
752
            } else if (child["type"] == "map") {
×
753
                json_to_map_node(child, builder, sequence, assignments);
×
754
            } else if (child["type"] == "sequence") {
×
755
                auto& subseq = builder.add_sequence(sequence, assignments, json_to_debug_info(child["debug_info"]));
×
756
                json_to_sequence(child, builder, subseq);
×
757
            } else {
×
758
                throw std::runtime_error("Unknown child type");
×
759
            }
760

761
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
11✔
762
            sequence.at(i).second.element_id_ = transition["element_id"];
11✔
763
        }
11✔
764
    } else {
14✔
765
        throw std::runtime_error("expected sequence type");
×
766
    }
767
}
14✔
768

769
void JSONSerializer::json_to_block_node(
10✔
770
    const nlohmann::json& j,
771
    builder::StructuredSDFGBuilder& builder,
772
    structured_control_flow::Sequence& parent,
773
    control_flow::Assignments& assignments
774
) {
775
    assert(j.contains("type"));
10✔
776
    assert(j["type"].is_string());
10✔
777
    assert(j.contains("dataflow"));
10✔
778
    assert(j["dataflow"].is_object());
10✔
779
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
10✔
780
    block.element_id_ = j["element_id"];
10✔
781
    assert(j["dataflow"].contains("type"));
10✔
782
    assert(j["dataflow"]["type"].is_string());
10✔
783
    std::string type = j["dataflow"]["type"];
10✔
784
    if (type == "dataflow") {
10✔
785
        json_to_dataflow(j["dataflow"], builder, block);
10✔
786
    } else {
10✔
787
        throw std::runtime_error("Unknown dataflow type");
×
788
    }
789
}
10✔
790

791
void JSONSerializer::json_to_for_node(
1✔
792
    const nlohmann::json& j,
793
    builder::StructuredSDFGBuilder& builder,
794
    structured_control_flow::Sequence& parent,
795
    control_flow::Assignments& assignments
796
) {
797
    assert(j.contains("type"));
1✔
798
    assert(j["type"].is_string());
1✔
799
    assert(j.contains("indvar"));
1✔
800
    assert(j["indvar"].is_string());
1✔
801
    assert(j.contains("init"));
1✔
802
    assert(j["init"].is_string());
1✔
803
    assert(j.contains("condition"));
1✔
804
    assert(j["condition"].is_string());
1✔
805
    assert(j.contains("update"));
1✔
806
    assert(j["update"].is_string());
1✔
807
    assert(j.contains("root"));
1✔
808
    assert(j["root"].is_object());
1✔
809

810
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
811
    SymEngine::Expression init(j["init"]);
1✔
812
    SymEngine::Expression condition_expr(j["condition"]);
1✔
813
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
814
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
815
    SymEngine::Expression update(j["update"]);
1✔
816
    auto& for_node =
1✔
817
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
1✔
818
    for_node.element_id_ = j["element_id"];
1✔
819

820
    assert(j["root"].contains("type"));
1✔
821
    assert(j["root"]["type"].is_string());
1✔
822
    assert(j["root"]["type"] == "sequence");
1✔
823
    json_to_sequence(j["root"], builder, for_node.root());
1✔
824
}
1✔
825

826
void JSONSerializer::json_to_if_else_node(
1✔
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"));
1✔
833
    assert(j["type"].is_string());
1✔
834
    assert(j["type"] == "if_else");
1✔
835
    assert(j.contains("branches"));
1✔
836
    assert(j["branches"].is_array());
1✔
837
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
838
    if_else_node.element_id_ = j["element_id"];
1✔
839
    for (const auto& branch : j["branches"]) {
3✔
840
        assert(branch.contains("condition"));
2✔
841
        assert(branch["condition"].is_string());
2✔
842
        assert(branch.contains("root"));
2✔
843
        assert(branch["root"].is_object());
2✔
844
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
845
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
2✔
846
        symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()
2✔
847
        );
848
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
849
        assert(branch["root"].contains("type"));
2✔
850
        assert(branch["root"]["type"].is_string());
2✔
851
        std::string type = branch["root"]["type"];
2✔
852
        if (type == "sequence") {
2✔
853
            json_to_sequence(branch["root"], builder, branch_node);
2✔
854
        } else {
2✔
855
            throw std::runtime_error("Unknown child type");
×
856
        }
857
    }
2✔
858
}
1✔
859

860
void JSONSerializer::json_to_while_node(
3✔
861
    const nlohmann::json& j,
862
    builder::StructuredSDFGBuilder& builder,
863
    structured_control_flow::Sequence& parent,
864
    control_flow::Assignments& assignments
865
) {
866
    assert(j.contains("type"));
3✔
867
    assert(j["type"].is_string());
3✔
868
    assert(j["type"] == "while");
3✔
869
    assert(j.contains("root"));
3✔
870
    assert(j["root"].is_object());
3✔
871

872
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
873
    while_node.element_id_ = j["element_id"];
3✔
874

875
    assert(j["root"]["type"] == "sequence");
3✔
876
    json_to_sequence(j["root"], builder, while_node.root());
3✔
877
}
3✔
878

879
void JSONSerializer::json_to_break_node(
1✔
880
    const nlohmann::json& j,
881
    builder::StructuredSDFGBuilder& builder,
882
    structured_control_flow::Sequence& parent,
883
    control_flow::Assignments& assignments
884
) {
885
    assert(j.contains("type"));
1✔
886
    assert(j["type"].is_string());
1✔
887
    assert(j["type"] == "break");
1✔
888
    auto& node = builder.add_break(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
889
    node.element_id_ = j["element_id"];
1✔
890
}
1✔
891

892
void JSONSerializer::json_to_continue_node(
1✔
893
    const nlohmann::json& j,
894
    builder::StructuredSDFGBuilder& builder,
895
    structured_control_flow::Sequence& parent,
896
    control_flow::Assignments& assignments
897
) {
898
    assert(j.contains("type"));
1✔
899
    assert(j["type"].is_string());
1✔
900
    assert(j["type"] == "continue");
1✔
901
    auto& node = builder.add_continue(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
902
    node.element_id_ = j["element_id"];
1✔
903
}
1✔
904

905
void JSONSerializer::json_to_map_node(
1✔
906
    const nlohmann::json& j,
907
    builder::StructuredSDFGBuilder& builder,
908
    structured_control_flow::Sequence& parent,
909
    control_flow::Assignments& assignments
910
) {
911
    assert(j.contains("type"));
1✔
912
    assert(j["type"].is_string());
1✔
913
    assert(j["type"] == "map");
1✔
914
    assert(j.contains("indvar"));
1✔
915
    assert(j["indvar"].is_string());
1✔
916
    assert(j.contains("init"));
1✔
917
    assert(j["init"].is_string());
1✔
918
    assert(j.contains("condition"));
1✔
919
    assert(j["condition"].is_string());
1✔
920
    assert(j.contains("update"));
1✔
921
    assert(j["update"].is_string());
1✔
922
    assert(j.contains("root"));
1✔
923
    assert(j["root"].is_object());
1✔
924
    assert(j.contains("schedule_type"));
1✔
925
    assert(j["schedule_type"].is_string());
1✔
926

927
    structured_control_flow::ScheduleType schedule_type =
928
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
929

930
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
931
    SymEngine::Expression init(j["init"]);
1✔
932
    SymEngine::Expression condition_expr(j["condition"]);
1✔
933
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
934
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
935
    SymEngine::Expression update(j["update"]);
1✔
936

937
    auto& map_node = builder.add_map(
2✔
938
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
939
    );
940
    map_node.element_id_ = j["element_id"];
1✔
941

942
    assert(j["root"].contains("type"));
1✔
943
    assert(j["root"]["type"].is_string());
1✔
944
    assert(j["root"]["type"] == "sequence");
1✔
945
    json_to_sequence(j["root"], builder, map_node.root());
1✔
946
}
1✔
947

948
void JSONSerializer::json_to_return_node(
1✔
949
    const nlohmann::json& j,
950
    builder::StructuredSDFGBuilder& builder,
951
    structured_control_flow::Sequence& parent,
952
    control_flow::Assignments& assignments
953
) {
954
    assert(j.contains("type"));
1✔
955
    assert(j["type"].is_string());
1✔
956
    assert(j["type"] == "return");
1✔
957

958
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
959
    node.element_id_ = j["element_id"];
1✔
960
}
1✔
961

962
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
41✔
963
    if (j.contains("type")) {
41✔
964
        if (j["type"] == "scalar") {
41✔
965
            // Deserialize scalar type
966
            assert(j.contains("primitive_type"));
30✔
967
            types::PrimitiveType primitive_type = j["primitive_type"];
30✔
968
            assert(j.contains("storage_type"));
30✔
969
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
30✔
970
            assert(j.contains("initializer"));
30✔
971
            std::string initializer = j["initializer"];
30✔
972
            assert(j.contains("alignment"));
30✔
973
            size_t alignment = j["alignment"];
30✔
974
            return std::make_unique<types::Scalar>(storage_type, alignment, initializer, primitive_type);
30✔
975
        } else if (j["type"] == "array") {
41✔
976
            // Deserialize array type
977
            assert(j.contains("element_type"));
2✔
978
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
979
            assert(j.contains("num_elements"));
2✔
980
            std::string num_elements_str = j["num_elements"];
2✔
981
            // Convert num_elements_str to symbolic::Expression
982
            SymEngine::Expression num_elements(num_elements_str);
2✔
983
            assert(j.contains("storage_type"));
2✔
984
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
985
            assert(j.contains("initializer"));
2✔
986
            std::string initializer = j["initializer"];
2✔
987
            assert(j.contains("alignment"));
2✔
988
            size_t alignment = j["alignment"];
2✔
989
            return std::make_unique<types::Array>(storage_type, alignment, initializer, *member_type, num_elements);
2✔
990
        } else if (j["type"] == "pointer") {
11✔
991
            // Deserialize pointer type
992
            std::optional<std::unique_ptr<types::IType>> pointee_type;
6✔
993
            if (j.contains("pointee_type")) {
6✔
994
                assert(j.contains("pointee_type"));
5✔
995
                pointee_type = json_to_type(j["pointee_type"]);
5✔
996
            } else {
5✔
997
                pointee_type = std::nullopt;
1✔
998
            }
999
            assert(j.contains("storage_type"));
6✔
1000
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
6✔
1001
            assert(j.contains("initializer"));
6✔
1002
            std::string initializer = j["initializer"];
6✔
1003
            assert(j.contains("alignment"));
6✔
1004
            size_t alignment = j["alignment"];
6✔
1005
            if (pointee_type.has_value()) {
6✔
1006
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer, *pointee_type.value());
5✔
1007
            } else {
1008
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer);
1✔
1009
            }
1010
        } else if (j["type"] == "structure") {
9✔
1011
            // Deserialize structure type
1012
            assert(j.contains("name"));
2✔
1013
            std::string name = j["name"];
2✔
1014
            assert(j.contains("storage_type"));
2✔
1015
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
1016
            assert(j.contains("initializer"));
2✔
1017
            std::string initializer = j["initializer"];
2✔
1018
            assert(j.contains("alignment"));
2✔
1019
            size_t alignment = j["alignment"];
2✔
1020
            return std::make_unique<types::Structure>(storage_type, alignment, initializer, name);
2✔
1021
        } else if (j["type"] == "function") {
3✔
1022
            // Deserialize function type
1023
            assert(j.contains("return_type"));
1✔
1024
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
1025
            assert(j.contains("params"));
1✔
1026
            std::vector<std::unique_ptr<types::IType>> params;
1✔
1027
            for (const auto& param : j["params"]) {
3✔
1028
                params.push_back(json_to_type(param));
2✔
1029
            }
1030
            assert(j.contains("is_var_arg"));
1✔
1031
            bool is_var_arg = j["is_var_arg"];
1✔
1032
            assert(j.contains("storage_type"));
1✔
1033
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
1✔
1034
            assert(j.contains("initializer"));
1✔
1035
            std::string initializer = j["initializer"];
1✔
1036
            assert(j.contains("alignment"));
1✔
1037
            size_t alignment = j["alignment"];
1✔
1038
            auto function =
1039
                std::make_unique<types::Function>(storage_type, alignment, initializer, *return_type, is_var_arg);
1✔
1040
            for (const auto& param : params) {
3✔
1041
                function->add_param(*param);
2✔
1042
            }
1043
            return function->clone();
1✔
1044

1045
        } else {
1✔
1046
            throw std::runtime_error("Unknown type");
×
1047
        }
1048
    } else {
1049
        throw std::runtime_error("Type not found");
×
1050
    }
1051
}
41✔
1052

1053
DebugLoc JSONSerializer::json_to_debug_loc(const nlohmann::json& j) {
10✔
1054
    assert(j.contains("has"));
10✔
1055
    assert(j["has"].is_boolean());
10✔
1056
    if (!j["has"]) {
10✔
NEW
1057
        return DebugLoc();
×
1058
    }
1059
    assert(j.contains("filename"));
10✔
1060
    assert(j["filename"].is_string());
10✔
1061
    std::string filename = j["filename"];
10✔
1062
    assert(j.contains("function"));
10✔
1063
    assert(j["function"].is_string());
10✔
1064
    std::string function = j["function"];
10✔
1065
    assert(j.contains("line"));
10✔
1066
    assert(j["line"].is_number_integer());
10✔
1067
    size_t line = j["line"];
10✔
1068
    assert(j.contains("column"));
10✔
1069
    assert(j["column"].is_number_integer());
10✔
1070
    size_t column = j["column"];
10✔
1071
    return DebugLoc(filename, function, line, column, true);
10✔
1072
}
10✔
1073

1074
DebugInfoElement JSONSerializer::json_to_debug_info_element(const nlohmann::json& j) {
5✔
1075
    assert(j.contains("has"));
5✔
1076
    assert(j["has"].is_boolean());
5✔
1077
    if (!j["has"]) {
5✔
NEW
1078
        return DebugInfoElement();
×
1079
    }
1080
    assert(j.contains("locations"));
5✔
1081
    assert(j["locations"].is_array());
5✔
1082
    std::vector<DebugLoc> locations;
5✔
1083
    for (const auto& loc_json : j["locations"]) {
14✔
1084
        locations.push_back(json_to_debug_loc(loc_json));
9✔
1085
    }
1086
    return DebugInfoElement(locations);
5✔
1087
}
5✔
1088

1089
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
74✔
1090
    assert(j.contains("has"));
74✔
1091
    assert(j["has"].is_boolean());
74✔
1092
    if (!j["has"]) {
74✔
1093
        return DebugInfo();
72✔
1094
    }
1095

1096
    assert(j.contains("instructions"));
2✔
1097
    assert(j["instructions"].is_array());
2✔
1098
    std::vector<DebugInfoElement> instructions;
2✔
1099
    for (const auto& instruction_json : j["instructions"]) {
6✔
1100
        instructions.push_back(json_to_debug_info_element(instruction_json));
4✔
1101
    }
1102
    DebugInfo debug_info(instructions);
2✔
1103
    assert(j.contains("filename"));
2✔
1104
    assert(j["filename"].is_string());
2✔
1105
    std::string filename = j["filename"];
2✔
1106
    assert(filename == debug_info.filename());
2✔
1107

1108
    assert(j.contains("function"));
2✔
1109
    assert(j["function"].is_string());
2✔
1110
    std::string function = j["function"];
2✔
1111
    assert(function == debug_info.function());
2✔
1112

1113
    assert(j.contains("start_line"));
2✔
1114
    assert(j["start_line"].is_number_integer());
2✔
1115
    size_t start_line = j["start_line"];
2✔
1116
    assert(start_line == debug_info.start_line());
2✔
1117

1118
    assert(j.contains("start_column"));
2✔
1119
    assert(j["start_column"].is_number_integer());
2✔
1120
    size_t start_column = j["start_column"];
2✔
1121
    assert(start_column == debug_info.start_column());
2✔
1122

1123
    assert(j.contains("end_line"));
2✔
1124
    assert(j["end_line"].is_number_integer());
2✔
1125
    size_t end_line = j["end_line"];
2✔
1126
    assert(end_line == debug_info.end_line());
2✔
1127

1128
    assert(j.contains("end_column"));
2✔
1129
    assert(j["end_column"].is_number_integer());
2✔
1130
    size_t end_column = j["end_column"];
2✔
1131
    assert(end_column == debug_info.end_column());
2✔
1132

1133
    return debug_info;
2✔
1134
}
74✔
1135

1136
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
47✔
1137
    JSONSymbolicPrinter printer;
47✔
1138
    return printer.apply(expr);
47✔
1139
};
47✔
1140

1141
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1142
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1143
    str_ = parenthesize(str_);
×
1144
};
×
1145

1146
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1147
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1148
    str_ = parenthesize(str_);
×
1149
};
×
1150

1151
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1152
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1153
    str_ = parenthesize(str_);
×
1154
};
×
1155

1156
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1157
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1158
    str_ = parenthesize(str_);
4✔
1159
};
4✔
1160

1161
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1162
    std::ostringstream s;
×
1163
    auto container = x.get_args();
×
1164
    if (container.size() == 1) {
×
1165
        s << apply(*container.begin());
×
1166
    } else {
×
1167
        s << "min(";
×
1168
        s << apply(*container.begin());
×
1169

1170
        // Recursively apply __daisy_min to the arguments
1171
        SymEngine::vec_basic subargs;
×
1172
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1173
            subargs.push_back(*it);
×
1174
        }
×
1175
        auto submin = SymEngine::min(subargs);
×
1176
        s << ", " << apply(submin);
×
1177

1178
        s << ")";
×
1179
    }
×
1180

1181
    str_ = s.str();
×
1182
};
×
1183

1184
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1185
    std::ostringstream s;
×
1186
    auto container = x.get_args();
×
1187
    if (container.size() == 1) {
×
1188
        s << apply(*container.begin());
×
1189
    } else {
×
1190
        s << "max(";
×
1191
        s << apply(*container.begin());
×
1192

1193
        // Recursively apply __daisy_max to the arguments
1194
        SymEngine::vec_basic subargs;
×
1195
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1196
            subargs.push_back(*it);
×
1197
        }
×
1198
        auto submax = SymEngine::max(subargs);
×
1199
        s << ", " << apply(submax);
×
1200

1201
        s << ")";
×
1202
    }
×
1203

1204
    str_ = s.str();
×
1205
};
×
1206

1207
void LibraryNodeSerializerRegistry::
1208
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
58✔
1209
    std::lock_guard<std::mutex> lock(mutex_);
58✔
1210
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
58✔
1211
        throw std::runtime_error(
×
1212
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1213
        );
1214
    }
1215
    factory_map_[library_node_code] = std::move(fn);
58✔
1216
}
58✔
1217

1218
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1219
    auto it = factory_map_.find(library_node_code);
1✔
1220
    if (it != factory_map_.end()) {
1✔
1221
        return it->second;
1✔
1222
    }
1223
    return nullptr;
×
1224
}
1✔
1225

1226
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1227

1228
void register_default_serializers() {
2✔
1229
    // Metadata
1230
    LibraryNodeSerializerRegistry::instance()
2✔
1231
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
1232
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1233
        });
1234

1235
    // Barrier
1236
    LibraryNodeSerializerRegistry::instance()
2✔
1237
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1238
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1239
        });
1240

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

1343
    // BLAS
1344
    LibraryNodeSerializerRegistry::instance()
2✔
1345
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1346
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1347
        });
1348
    LibraryNodeSerializerRegistry::instance()
2✔
1349
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1350
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1351
        });
1352
}
2✔
1353

1354
} // namespace serializer
1355
} // 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