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

daisytuner / sdfglib / 17637380013

11 Sep 2025 07:29AM UTC coverage: 59.755% (+0.6%) from 59.145%
17637380013

push

github

web-flow
New debug info (#210)

* initial draft

* update data structure and construction logic

* finalize DebugInfo draft

* fix tests

* Update serializer and fix tests

* fix append bug

* update data structure

* sdfg builder update

* const ref vectors

* update implementation and partial tests

* compiling state

* update serializer interface

* update dot test

* reset interface to debug_info in json to maintain compatibility with tools

* first review batch

* second batch of changes

* merge fixes

777 of 1111 new or added lines in 46 files covered. (69.94%)

11 existing lines in 11 files now uncovered.

9755 of 16325 relevant lines covered (59.75%)

115.06 hits per line

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

83.52
/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/debug_info.h"
17
#include "sdfg/element.h"
18
#include "sdfg/structured_control_flow/block.h"
19
#include "sdfg/structured_control_flow/for.h"
20
#include "sdfg/structured_control_flow/if_else.h"
21
#include "sdfg/structured_control_flow/map.h"
22
#include "sdfg/structured_control_flow/return.h"
23
#include "sdfg/structured_control_flow/sequence.h"
24
#include "sdfg/structured_control_flow/while.h"
25
#include "sdfg/structured_sdfg.h"
26
#include "sdfg/symbolic/symbolic.h"
27
#include "sdfg/types/function.h"
28
#include "sdfg/types/scalar.h"
29
#include "sdfg/types/type.h"
30
#include "symengine/expression.h"
31
#include "symengine/logic.h"
32
#include "symengine/symengine_rcp.h"
33

34
namespace sdfg {
35
namespace serializer {
36

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

44
    return FunctionType(str);
×
45
}
5✔
46

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

62
    return types::StorageType(str);
×
63
}
41✔
64

65
/*
66
 * * JSONSerializer class
67
 * * Serialization logic
68
 */
69

70
nlohmann::json JSONSerializer::serialize(const sdfg::StructuredSDFG& sdfg) {
5✔
71
    nlohmann::json j;
5✔
72

73
    j["name"] = sdfg.name();
5✔
74
    j["element_counter"] = sdfg.element_counter();
5✔
75
    j["type"] = std::string(sdfg.type().value());
5✔
76
    j["debug_info"] = nlohmann::json::object();
5✔
77
    debug_table_to_json(j["debug_info"], sdfg.debug_info());
5✔
78

79
    j["structures"] = nlohmann::json::array();
5✔
80
    for (const auto& structure_name : sdfg.structures()) {
6✔
81
        const auto& structure = sdfg.structure(structure_name);
1✔
82
        nlohmann::json structure_json;
1✔
83
        structure_definition_to_json(structure_json, structure);
1✔
84
        j["structures"].push_back(structure_json);
1✔
85
    }
1✔
86

87
    j["containers"] = nlohmann::json::object();
5✔
88
    for (const auto& container : sdfg.containers()) {
17✔
89
        nlohmann::json desc;
12✔
90
        type_to_json(desc, sdfg.type(container));
12✔
91
        j["containers"][container] = desc;
12✔
92
    }
12✔
93

94
    j["arguments"] = nlohmann::json::array();
5✔
95
    for (const auto& argument : sdfg.arguments()) {
8✔
96
        j["arguments"].push_back(argument);
3✔
97
    }
98

99
    j["externals"] = nlohmann::json::array();
5✔
100
    for (const auto& external : sdfg.externals()) {
9✔
101
        nlohmann::json external_json;
4✔
102
        external_json["name"] = external;
4✔
103
        external_json["linkage_type"] = sdfg.linkage_type(external);
4✔
104
        j["externals"].push_back(external_json);
4✔
105
    }
4✔
106

107
    j["metadata"] = nlohmann::json::object();
5✔
108
    for (const auto& entry : sdfg.metadata()) {
6✔
109
        j["metadata"][entry.first] = entry.second;
1✔
110
    }
111

112
    // Walk the SDFG
113
    nlohmann::json root_json;
5✔
114
    sequence_to_json(root_json, sdfg.root());
5✔
115
    j["root"] = root_json;
5✔
116

117
    return j;
5✔
118
}
5✔
119

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

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

129
        node_json["debug_info"] = nlohmann::json::object();
20✔
130
        debug_info_region_to_json(node_json["debug_info"], node.debug_info());
20✔
131

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

162
        j["nodes"].push_back(node_json);
20✔
163
    }
20✔
164

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

169
        edge_json["debug_info"] = nlohmann::json::object();
15✔
170
        debug_info_region_to_json(edge_json["debug_info"], edge.debug_info());
15✔
171

172
        edge_json["src"] = edge.src().element_id();
15✔
173
        edge_json["dst"] = edge.dst().element_id();
15✔
174

175
        edge_json["src_conn"] = edge.src_conn();
15✔
176
        edge_json["dst_conn"] = edge.dst_conn();
15✔
177

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

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

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

193
        nlohmann::json base_type_json;
15✔
194
        type_to_json(base_type_json, edge.base_type());
15✔
195
        edge_json["base_type"] = base_type_json;
15✔
196

197
        j["edges"].push_back(edge_json);
15✔
198
    }
15✔
199
}
21✔
200

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

205
    j["debug_info"] = nlohmann::json::object();
19✔
206
    debug_info_region_to_json(j["debug_info"], block.debug_info());
19✔
207

208
    nlohmann::json dataflow_json;
19✔
209
    dataflow_to_json(dataflow_json, block.dataflow());
19✔
210
    j["dataflow"] = dataflow_json;
19✔
211
}
19✔
212

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

217
    j["debug_info"] = nlohmann::json::object();
2✔
218
    debug_info_region_to_json(j["debug_info"], for_node.debug_info());
2✔
219

220
    j["indvar"] = expression(for_node.indvar());
2✔
221
    j["init"] = expression(for_node.init());
2✔
222
    j["condition"] = expression(for_node.condition());
2✔
223
    j["update"] = expression(for_node.update());
2✔
224

225
    nlohmann::json body_json;
2✔
226
    sequence_to_json(body_json, for_node.root());
2✔
227
    j["root"] = body_json;
2✔
228
}
2✔
229

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

234
    j["debug_info"] = nlohmann::json::object();
2✔
235
    debug_info_region_to_json(j["debug_info"], if_else_node.debug_info());
2✔
236

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

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

252
    j["debug_info"] = nlohmann::json::object();
5✔
253
    debug_info_region_to_json(j["debug_info"], while_node.debug_info());
5✔
254

255
    nlohmann::json body_json;
5✔
256
    sequence_to_json(body_json, while_node.root());
5✔
257
    j["root"] = body_json;
5✔
258
}
5✔
259

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

264
    j["debug_info"] = nlohmann::json::object();
2✔
265
    debug_info_region_to_json(j["debug_info"], break_node.debug_info());
2✔
266
}
2✔
267

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

272
    j["debug_info"] = nlohmann::json::object();
2✔
273
    debug_info_region_to_json(j["debug_info"], continue_node.debug_info());
2✔
274
}
2✔
275

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

280
    j["debug_info"] = nlohmann::json::object();
2✔
281
    debug_info_region_to_json(j["debug_info"], map_node.debug_info());
2✔
282

283
    j["indvar"] = expression(map_node.indvar());
2✔
284
    j["init"] = expression(map_node.init());
2✔
285
    j["condition"] = expression(map_node.condition());
2✔
286
    j["update"] = expression(map_node.update());
2✔
287

288
    j["schedule_type"] = nlohmann::json::object();
2✔
289
    schedule_type_to_json(j["schedule_type"], map_node.schedule_type());
2✔
290

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

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

300
    j["debug_info"] = nlohmann::json::object();
2✔
301
    debug_info_region_to_json(j["debug_info"], return_node.debug_info());
2✔
302
}
2✔
303

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

308
    j["debug_info"] = nlohmann::json::object();
21✔
309
    debug_info_region_to_json(j["debug_info"], sequence.debug_info());
21✔
310

311
    j["children"] = nlohmann::json::array();
21✔
312
    j["transitions"] = nlohmann::json::array();
21✔
313

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

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

341
        j["children"].push_back(child_json);
21✔
342

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

348
        transition_json["debug_info"] = nlohmann::json::object();
21✔
349
        debug_info_region_to_json(transition_json["debug_info"], transition.debug_info());
21✔
350

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

359
        j["transitions"].push_back(transition_json);
21✔
360
    }
21✔
361
}
21✔
362

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

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

426
void JSONSerializer::debug_loc_to_json(nlohmann::json& j, const DebugLoc& loc) {
7✔
427
    j["has"] = loc.has;
7✔
428
    if (!loc.has) {
7✔
NEW
429
        return;
×
430
    }
431
    j["filename"] = loc.filename;
7✔
432
    j["function"] = loc.function;
7✔
433
    j["line"] = loc.line;
7✔
434
    j["column"] = loc.column;
7✔
435
}
7✔
436

437

438
void JSONSerializer::debug_info_element_to_json(nlohmann::json& j, const DebugInfo& debug_info_element) {
3✔
439
    j["has"] = debug_info_element.has();
3✔
440
    if (!debug_info_element.has()) {
3✔
NEW
441
        return;
×
442
    }
443
    j["locations"] = nlohmann::json::array();
3✔
444
    for (const auto& loc : debug_info_element.locations()) {
9✔
445
        nlohmann::json loc_json;
6✔
446
        debug_loc_to_json(loc_json, loc);
6✔
447
        j["locations"].push_back(loc_json);
6✔
448
    }
6✔
449
}
3✔
450

451
void JSONSerializer::debug_info_region_to_json(nlohmann::json& j, const DebugInfoRegion& debug_info) {
115✔
452
    j["has"] = debug_info.has();
115✔
453
    j["filename"] = debug_info.filename();
115✔
454
    j["function"] = debug_info.function();
115✔
455
    j["start_line"] = debug_info.start_line();
115✔
456
    j["start_column"] = debug_info.start_column();
115✔
457
    j["end_line"] = debug_info.end_line();
115✔
458
    j["end_column"] = debug_info.end_column();
115✔
459

460
    j["indices"] = nlohmann::json::array();
115✔
461
    for (const auto& index : debug_info.indices()) {
119✔
462
        j["indices"].push_back(index);
4✔
463
    }
464
}
115✔
465

466
void JSONSerializer::debug_table_to_json(nlohmann::json& j, const DebugTable& debug_info) {
6✔
467
    j["elements"] = nlohmann::json::array();
6✔
468
    for (const auto& instruction : debug_info.elements()) {
8✔
469
        nlohmann::json instruction_json;
2✔
470
        debug_info_element_to_json(instruction_json, instruction);
2✔
471
        j["elements"].push_back(instruction_json);
2✔
472
    }
2✔
473
}
6✔
474

475
void JSONSerializer::schedule_type_to_json(nlohmann::json& j, const ScheduleType& schedule_type) {
3✔
476
    j["value"] = schedule_type.value();
3✔
477
    j["properties"] = nlohmann::json::object();
3✔
478
    for (const auto& prop : schedule_type.properties()) {
4✔
479
        j["properties"][prop.first] = prop.second;
1✔
480
    }
481
}
3✔
482

483
/*
484
 * * Deserialization logic
485
 */
486

487
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
5✔
488
    assert(j.contains("name"));
5✔
489
    assert(j["name"].is_string());
5✔
490
    assert(j.contains("type"));
5✔
491
    assert(j["type"].is_string());
5✔
492
    assert(j["element_counter"].is_number_integer());
5✔
493
    assert(j.contains("debug_info"));
5✔
494
    assert(j["debug_info"].is_object());
5✔
495

496
    DebugTable debug_info = json_to_debug_table(j["debug_info"]);
5✔
497
    debug_info_ = &debug_info;
5✔
498

499
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
5✔
500
    builder::StructuredSDFGBuilder builder(j["name"], function_type);
5✔
501
    builder.subject().debug_info(debug_info);
5✔
502

503
    size_t element_counter = j["element_counter"];
5✔
504
    builder.set_element_counter(element_counter);
5✔
505

506
    // deserialize structures
507
    assert(j.contains("structures"));
5✔
508
    assert(j["structures"].is_array());
5✔
509
    for (const auto& structure : j["structures"]) {
6✔
510
        assert(structure.contains("name"));
1✔
511
        assert(structure["name"].is_string());
1✔
512
        json_to_structure_definition(structure, builder);
1✔
513
    }
514

515
    nlohmann::json& containers = j["containers"];
5✔
516

517
    // deserialize externals
518
    for (const auto& external : j["externals"]) {
9✔
519
        assert(external.contains("name"));
4✔
520
        assert(external["name"].is_string());
4✔
521
        assert(external.contains("linkage_type"));
4✔
522
        assert(external["linkage_type"].is_number_integer());
4✔
523
        auto& type_desc = containers.at(external["name"].get<std::string>());
4✔
524
        auto type = json_to_type(type_desc);
4✔
525
        builder.add_external(external["name"], *type, LinkageType(external["linkage_type"]));
4✔
526
    }
4✔
527

528
    // deserialize arguments
529
    for (const auto& name : j["arguments"]) {
8✔
530
        auto& type_desc = containers.at(name.get<std::string>());
3✔
531
        auto type = json_to_type(type_desc);
3✔
532
        builder.add_container(name, *type, true, false);
3✔
533
    }
3✔
534

535
    // deserialize transients
536
    for (const auto& entry : containers.items()) {
17✔
537
        if (builder.subject().is_argument(entry.key())) {
12✔
538
            continue;
3✔
539
        }
540
        if (builder.subject().is_external(entry.key())) {
9✔
541
            continue;
4✔
542
        }
543
        auto type = json_to_type(entry.value());
5✔
544
        builder.add_container(entry.key(), *type, false, false);
5✔
545
    }
5✔
546

547
    // deserialize root node
548
    assert(j.contains("root"));
5✔
549
    auto& root = builder.subject().root();
5✔
550
    json_to_sequence(j["root"], builder, root);
5✔
551

552
    // deserialize metadata
553
    assert(j.contains("metadata"));
5✔
554
    assert(j["metadata"].is_object());
5✔
555
    for (const auto& entry : j["metadata"].items()) {
6✔
556
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
557
    }
558

559
    builder.set_element_counter(element_counter);
5✔
560

561
    return builder.move();
5✔
562
}
5✔
563

564
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
2✔
565
    assert(j.contains("name"));
2✔
566
    assert(j["name"].is_string());
2✔
567
    assert(j.contains("members"));
2✔
568
    assert(j["members"].is_array());
2✔
569
    assert(j.contains("is_packed"));
2✔
570
    assert(j["is_packed"].is_boolean());
2✔
571
    auto is_packed = j["is_packed"];
2✔
572
    auto& definition = builder.add_structure(j["name"], is_packed);
2✔
573
    for (const auto& member : j["members"]) {
4✔
574
        nlohmann::json member_json;
2✔
575
        auto member_type = json_to_type(member);
2✔
576
        definition.add_member(*member_type);
2✔
577
    }
2✔
578
}
2✔
579

580
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
×
581
    std::vector<std::pair<std::string, types::Scalar>> arguments;
×
582
    for (const auto& argument : j) {
×
583
        assert(argument.contains("name"));
×
584
        assert(argument["name"].is_string());
×
585
        assert(argument.contains("type"));
×
586
        assert(argument["type"].is_object());
×
587
        std::string name = argument["name"];
×
588
        auto type = json_to_type(argument["type"]);
×
589
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
×
590
    }
×
591
    return arguments;
×
592
}
×
593

594
void JSONSerializer::json_to_dataflow(
11✔
595
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
596
) {
597
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
11✔
598

599
    assert(j.contains("nodes"));
11✔
600
    assert(j["nodes"].is_array());
11✔
601
    for (const auto& node : j["nodes"]) {
27✔
602
        assert(node.contains("type"));
16✔
603
        assert(node["type"].is_string());
16✔
604
        assert(node.contains("element_id"));
16✔
605
        assert(node["element_id"].is_number_integer());
16✔
606
        std::string type = node["type"];
16✔
607
        if (type == "tasklet") {
16✔
608
            assert(node.contains("code"));
4✔
609
            assert(node["code"].is_number_integer());
4✔
610
            assert(node.contains("inputs"));
4✔
611
            assert(node["inputs"].is_array());
4✔
612
            assert(node.contains("output"));
4✔
613
            assert(node["output"].is_string());
4✔
614
            auto inputs = node["inputs"].get<std::vector<std::string>>();
4✔
615

616
            auto& tasklet = builder.add_tasklet(
8✔
617
                parent,
4✔
618
                node["code"],
4✔
619
                node["output"],
4✔
620
                inputs,
621
                builder.subject().debug_info().get_region(json_to_debug_info_region(node["debug_info"], *debug_info_)
8✔
622
                                                              .indices())
4✔
623
            );
624
            tasklet.element_id_ = node["element_id"];
4✔
625
            nodes_map.insert({node["element_id"], tasklet});
4✔
626
        } else if (type == "library_node") {
16✔
627
            assert(node.contains("code"));
×
628
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
629

630
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
631
            if (serializer_fn == nullptr) {
×
632
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
633
            }
634
            auto serializer = serializer_fn();
×
635
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
636
            lib_node.implementation_type() =
×
637
                data_flow::ImplementationType(node["implementation_type"].get<std::string>());
×
638
            lib_node.element_id_ = node["element_id"];
×
639
            nodes_map.insert({node["element_id"], lib_node});
×
640
        } else if (type == "access_node") {
12✔
641
            assert(node.contains("data"));
12✔
642
            auto& access_node = builder.add_access(
24✔
643
                parent,
12✔
644
                node["data"],
12✔
645
                builder.subject().debug_info().get_region(json_to_debug_info_region(node["debug_info"], *debug_info_)
24✔
646
                                                              .indices())
12✔
647
            );
648
            access_node.element_id_ = node["element_id"];
12✔
649
            nodes_map.insert({node["element_id"], access_node});
12✔
650
        } else {
12✔
651
            throw std::runtime_error("Unknown node type");
×
652
        }
653
    }
16✔
654

655
    assert(j.contains("edges"));
11✔
656
    assert(j["edges"].is_array());
11✔
657
    for (const auto& edge : j["edges"]) {
23✔
658
        assert(edge.contains("src"));
12✔
659
        assert(edge["src"].is_number_integer());
12✔
660
        assert(edge.contains("dst"));
12✔
661
        assert(edge["dst"].is_number_integer());
12✔
662
        assert(edge.contains("src_conn"));
12✔
663
        assert(edge["src_conn"].is_string());
12✔
664
        assert(edge.contains("dst_conn"));
12✔
665
        assert(edge["dst_conn"].is_string());
12✔
666
        assert(edge.contains("subset"));
12✔
667
        assert(edge["subset"].is_array());
12✔
668

669
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
670
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
671
        auto& source = nodes_map.at(edge["src"]);
12✔
672
        auto& target = nodes_map.at(edge["dst"]);
12✔
673

674
        auto base_type = json_to_type(edge["base_type"]);
12✔
675

676
        if (edge.contains("begin_subset") && edge.contains("end_subset")) {
12✔
677
            assert(edge["begin_subset"].is_array());
12✔
678
            assert(edge["end_subset"].is_array());
12✔
679
            std::vector<symbolic::Expression> begin_subset;
12✔
680
            std::vector<symbolic::Expression> end_subset;
12✔
681
            for (const auto& subset_str : edge["begin_subset"]) {
16✔
682
                assert(subset_str.is_string());
4✔
683
                SymEngine::Expression subset_expr(subset_str);
4✔
684
                begin_subset.push_back(subset_expr);
4✔
685
            }
4✔
686
            for (const auto& subset_str : edge["end_subset"]) {
16✔
687
                assert(subset_str.is_string());
4✔
688
                SymEngine::Expression subset_expr(subset_str);
4✔
689
                end_subset.push_back(subset_expr);
4✔
690
            }
4✔
691
            auto& memlet = builder.add_memlet(
24✔
692
                parent,
12✔
693
                source,
12✔
694
                edge["src_conn"],
12✔
695
                target,
12✔
696
                edge["dst_conn"],
12✔
697
                begin_subset,
698
                end_subset,
699
                *base_type,
12✔
700
                builder.subject().debug_info().get_region(json_to_debug_info_region(edge["debug_info"], *debug_info_)
24✔
701
                                                              .indices())
12✔
702
            );
703
            memlet.element_id_ = edge["element_id"];
12✔
704
        } else if (edge.contains("subset")) {
12✔
705
            assert(edge["subset"].is_array());
×
706
            std::vector<symbolic::Expression> subset;
×
707
            for (const auto& subset_str : edge["subset"]) {
×
708
                assert(subset_str.is_string());
×
709
                SymEngine::Expression subset_expr(subset_str);
×
710
                subset.push_back(subset_expr);
×
711
            }
×
712
            auto& memlet = builder.add_memlet(
×
713
                parent,
×
714
                source,
×
715
                edge["src_conn"],
×
716
                target,
×
717
                edge["dst_conn"],
×
718
                subset,
719
                *base_type,
×
NEW
720
                builder.subject().debug_info().get_region(json_to_debug_info_region(edge["debug_info"], *debug_info_)
×
NEW
721
                                                              .indices())
×
722
            );
723
            memlet.element_id_ = edge["element_id"];
×
724
        } else {
×
725
            throw std::runtime_error("Subsets not specified in json");
×
726
        }
727
    }
12✔
728
}
11✔
729

730
void JSONSerializer::json_to_sequence(
14✔
731
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
732
) {
733
    assert(j.contains("type"));
14✔
734
    assert(j["type"].is_string());
14✔
735
    assert(j.contains("children"));
14✔
736
    assert(j["children"].is_array());
14✔
737
    assert(j.contains("transitions"));
14✔
738
    assert(j["transitions"].is_array());
14✔
739
    assert(j["transitions"].size() == j["children"].size());
14✔
740

741
    sequence.element_id_ = j["element_id"];
14✔
742
    sequence.debug_info_ = json_to_debug_info_region(j["debug_info"], *debug_info_);
14✔
743

744
    std::string type = j["type"];
14✔
745
    if (type == "sequence") {
14✔
746
        for (size_t i = 0; i < j["children"].size(); i++) {
25✔
747
            auto& child = j["children"][i];
11✔
748
            auto& transition = j["transitions"][i];
11✔
749
            assert(child.contains("type"));
11✔
750
            assert(child["type"].is_string());
11✔
751

752
            assert(transition.contains("type"));
11✔
753
            assert(transition["type"].is_string());
11✔
754
            assert(transition.contains("assignments"));
11✔
755
            assert(transition["assignments"].is_array());
11✔
756
            control_flow::Assignments assignments;
11✔
757
            for (const auto& assignment : transition["assignments"]) {
13✔
758
                assert(assignment.contains("symbol"));
2✔
759
                assert(assignment["symbol"].is_string());
2✔
760
                assert(assignment.contains("expression"));
2✔
761
                assert(assignment["expression"].is_string());
2✔
762
                SymEngine::Expression expr(assignment["expression"]);
2✔
763
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
2✔
764
            }
2✔
765

766
            if (child["type"] == "block") {
11✔
767
                json_to_block_node(child, builder, sequence, assignments);
9✔
768
            } else if (child["type"] == "for") {
11✔
769
                json_to_for_node(child, builder, sequence, assignments);
×
770
            } else if (child["type"] == "if_else") {
2✔
771
                json_to_if_else_node(child, builder, sequence, assignments);
×
772
            } else if (child["type"] == "while") {
2✔
773
                json_to_while_node(child, builder, sequence, assignments);
×
774
            } else if (child["type"] == "break") {
2✔
775
                json_to_break_node(child, builder, sequence, assignments);
1✔
776
            } else if (child["type"] == "continue") {
2✔
777
                json_to_continue_node(child, builder, sequence, assignments);
1✔
778
            } else if (child["type"] == "return") {
1✔
779
                json_to_return_node(child, builder, sequence, assignments);
×
780
            } else if (child["type"] == "map") {
×
781
                json_to_map_node(child, builder, sequence, assignments);
×
782
            } else if (child["type"] == "sequence") {
×
NEW
783
                auto& subseq = builder.add_sequence(
×
NEW
784
                    sequence,
×
785
                    assignments,
NEW
786
                    builder.subject()
×
NEW
787
                        .debug_info()
×
NEW
788
                        .get_region(json_to_debug_info_region(child["debug_info"], *debug_info_).indices())
×
789
                );
790
                json_to_sequence(child, builder, subseq);
×
791
            } else {
×
792
                throw std::runtime_error("Unknown child type");
×
793
            }
794

795
            sequence.at(i).second.debug_info_ = json_to_debug_info_region(transition["debug_info"], *debug_info_);
11✔
796
            sequence.at(i).second.element_id_ = transition["element_id"];
11✔
797
        }
11✔
798
    } else {
14✔
799
        throw std::runtime_error("expected sequence type");
×
800
    }
801
}
14✔
802

803
void JSONSerializer::json_to_block_node(
10✔
804
    const nlohmann::json& j,
805
    builder::StructuredSDFGBuilder& builder,
806
    structured_control_flow::Sequence& parent,
807
    control_flow::Assignments& assignments
808
) {
809
    assert(j.contains("type"));
10✔
810
    assert(j["type"].is_string());
10✔
811
    assert(j.contains("dataflow"));
10✔
812
    assert(j["dataflow"].is_object());
10✔
813
    auto& block = builder.add_block(
20✔
814
        parent,
10✔
815
        assignments,
10✔
816
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
10✔
817
    );
818
    block.element_id_ = j["element_id"];
10✔
819
    assert(j["dataflow"].contains("type"));
10✔
820
    assert(j["dataflow"]["type"].is_string());
10✔
821
    std::string type = j["dataflow"]["type"];
10✔
822
    if (type == "dataflow") {
10✔
823
        json_to_dataflow(j["dataflow"], builder, block);
10✔
824
    } else {
10✔
825
        throw std::runtime_error("Unknown dataflow type");
×
826
    }
827
}
10✔
828

829
void JSONSerializer::json_to_for_node(
1✔
830
    const nlohmann::json& j,
831
    builder::StructuredSDFGBuilder& builder,
832
    structured_control_flow::Sequence& parent,
833
    control_flow::Assignments& assignments
834
) {
835
    assert(j.contains("type"));
1✔
836
    assert(j["type"].is_string());
1✔
837
    assert(j.contains("indvar"));
1✔
838
    assert(j["indvar"].is_string());
1✔
839
    assert(j.contains("init"));
1✔
840
    assert(j["init"].is_string());
1✔
841
    assert(j.contains("condition"));
1✔
842
    assert(j["condition"].is_string());
1✔
843
    assert(j.contains("update"));
1✔
844
    assert(j["update"].is_string());
1✔
845
    assert(j.contains("root"));
1✔
846
    assert(j["root"].is_object());
1✔
847

848
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
849
    SymEngine::Expression init(j["init"]);
1✔
850
    SymEngine::Expression condition_expr(j["condition"]);
1✔
851
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
852
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
853
    SymEngine::Expression update(j["update"]);
1✔
854
    auto& for_node = builder.add_for(
2✔
855
        parent,
1✔
856
        indvar,
857
        condition,
858
        init,
1✔
859
        update,
1✔
860
        assignments,
1✔
861
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
1✔
862
    );
863
    for_node.element_id_ = j["element_id"];
1✔
864

865
    assert(j["root"].contains("type"));
1✔
866
    assert(j["root"]["type"].is_string());
1✔
867
    assert(j["root"]["type"] == "sequence");
1✔
868
    json_to_sequence(j["root"], builder, for_node.root());
1✔
869
}
1✔
870

871
void JSONSerializer::json_to_if_else_node(
1✔
872
    const nlohmann::json& j,
873
    builder::StructuredSDFGBuilder& builder,
874
    structured_control_flow::Sequence& parent,
875
    control_flow::Assignments& assignments
876
) {
877
    assert(j.contains("type"));
1✔
878
    assert(j["type"].is_string());
1✔
879
    assert(j["type"] == "if_else");
1✔
880
    assert(j.contains("branches"));
1✔
881
    assert(j["branches"].is_array());
1✔
882
    auto& if_else_node = builder.add_if_else(
2✔
883
        parent,
1✔
884
        assignments,
1✔
885
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
1✔
886
    );
887
    if_else_node.element_id_ = j["element_id"];
1✔
888
    for (const auto& branch : j["branches"]) {
3✔
889
        assert(branch.contains("condition"));
2✔
890
        assert(branch["condition"].is_string());
2✔
891
        assert(branch.contains("root"));
2✔
892
        assert(branch["root"].is_object());
2✔
893
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
894
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
2✔
895
        symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()
2✔
896
        );
897
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
898
        assert(branch["root"].contains("type"));
2✔
899
        assert(branch["root"]["type"].is_string());
2✔
900
        std::string type = branch["root"]["type"];
2✔
901
        if (type == "sequence") {
2✔
902
            json_to_sequence(branch["root"], builder, branch_node);
2✔
903
        } else {
2✔
904
            throw std::runtime_error("Unknown child type");
×
905
        }
906
    }
2✔
907
}
1✔
908

909
void JSONSerializer::json_to_while_node(
3✔
910
    const nlohmann::json& j,
911
    builder::StructuredSDFGBuilder& builder,
912
    structured_control_flow::Sequence& parent,
913
    control_flow::Assignments& assignments
914
) {
915
    assert(j.contains("type"));
3✔
916
    assert(j["type"].is_string());
3✔
917
    assert(j["type"] == "while");
3✔
918
    assert(j.contains("root"));
3✔
919
    assert(j["root"].is_object());
3✔
920

921
    auto& while_node = builder.add_while(
6✔
922
        parent,
3✔
923
        assignments,
3✔
924
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
3✔
925
    );
926
    while_node.element_id_ = j["element_id"];
3✔
927

928
    assert(j["root"]["type"] == "sequence");
3✔
929
    json_to_sequence(j["root"], builder, while_node.root());
3✔
930
}
3✔
931

932
void JSONSerializer::json_to_break_node(
1✔
933
    const nlohmann::json& j,
934
    builder::StructuredSDFGBuilder& builder,
935
    structured_control_flow::Sequence& parent,
936
    control_flow::Assignments& assignments
937
) {
938
    assert(j.contains("type"));
1✔
939
    assert(j["type"].is_string());
1✔
940
    assert(j["type"] == "break");
1✔
941
    auto& node = builder.add_break(
2✔
942
        parent,
1✔
943
        assignments,
1✔
944
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
1✔
945
    );
946
    node.element_id_ = j["element_id"];
1✔
947
}
1✔
948

949
void JSONSerializer::json_to_continue_node(
1✔
950
    const nlohmann::json& j,
951
    builder::StructuredSDFGBuilder& builder,
952
    structured_control_flow::Sequence& parent,
953
    control_flow::Assignments& assignments
954
) {
955
    assert(j.contains("type"));
1✔
956
    assert(j["type"].is_string());
1✔
957
    assert(j["type"] == "continue");
1✔
958
    auto& node = builder.add_continue(
2✔
959
        parent,
1✔
960
        assignments,
1✔
961
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
1✔
962
    );
963
    node.element_id_ = j["element_id"];
1✔
964
}
1✔
965

966
void JSONSerializer::json_to_map_node(
1✔
967
    const nlohmann::json& j,
968
    builder::StructuredSDFGBuilder& builder,
969
    structured_control_flow::Sequence& parent,
970
    control_flow::Assignments& assignments
971
) {
972
    assert(j.contains("type"));
1✔
973
    assert(j["type"].is_string());
1✔
974
    assert(j["type"] == "map");
1✔
975
    assert(j.contains("indvar"));
1✔
976
    assert(j["indvar"].is_string());
1✔
977
    assert(j.contains("init"));
1✔
978
    assert(j["init"].is_string());
1✔
979
    assert(j.contains("condition"));
1✔
980
    assert(j["condition"].is_string());
1✔
981
    assert(j.contains("update"));
1✔
982
    assert(j["update"].is_string());
1✔
983
    assert(j.contains("root"));
1✔
984
    assert(j["root"].is_object());
1✔
985
    assert(j.contains("schedule_type"));
1✔
986
    assert(j["schedule_type"].is_object());
1✔
987

988
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
989

990
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
991
    SymEngine::Expression init(j["init"]);
1✔
992
    SymEngine::Expression condition_expr(j["condition"]);
1✔
993
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
994
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
995
    SymEngine::Expression update(j["update"]);
1✔
996

997
    auto& map_node = builder.add_map(
2✔
998
        parent,
1✔
999
        indvar,
1000
        condition,
1001
        init,
1✔
1002
        update,
1✔
1003
        schedule_type,
1004
        assignments,
1✔
1005
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
1✔
1006
    );
1007
    map_node.element_id_ = j["element_id"];
1✔
1008

1009
    assert(j["root"].contains("type"));
1✔
1010
    assert(j["root"]["type"].is_string());
1✔
1011
    assert(j["root"]["type"] == "sequence");
1✔
1012
    json_to_sequence(j["root"], builder, map_node.root());
1✔
1013
}
1✔
1014

1015
void JSONSerializer::json_to_return_node(
1✔
1016
    const nlohmann::json& j,
1017
    builder::StructuredSDFGBuilder& builder,
1018
    structured_control_flow::Sequence& parent,
1019
    control_flow::Assignments& assignments
1020
) {
1021
    assert(j.contains("type"));
1✔
1022
    assert(j["type"].is_string());
1✔
1023
    assert(j["type"] == "return");
1✔
1024

1025
    auto& node = builder.add_return(
2✔
1026
        parent,
1✔
1027
        assignments,
1✔
1028
        builder.subject().debug_info().get_region(json_to_debug_info_region(j["debug_info"], *debug_info_).indices())
1✔
1029
    );
1030
    node.element_id_ = j["element_id"];
1✔
1031
}
1✔
1032

1033
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
41✔
1034
    if (j.contains("type")) {
41✔
1035
        if (j["type"] == "scalar") {
41✔
1036
            // Deserialize scalar type
1037
            assert(j.contains("primitive_type"));
30✔
1038
            types::PrimitiveType primitive_type = j["primitive_type"];
30✔
1039
            assert(j.contains("storage_type"));
30✔
1040
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
30✔
1041
            assert(j.contains("initializer"));
30✔
1042
            std::string initializer = j["initializer"];
30✔
1043
            assert(j.contains("alignment"));
30✔
1044
            size_t alignment = j["alignment"];
30✔
1045
            return std::make_unique<types::Scalar>(storage_type, alignment, initializer, primitive_type);
30✔
1046
        } else if (j["type"] == "array") {
41✔
1047
            // Deserialize array type
1048
            assert(j.contains("element_type"));
2✔
1049
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
1050
            assert(j.contains("num_elements"));
2✔
1051
            std::string num_elements_str = j["num_elements"];
2✔
1052
            // Convert num_elements_str to symbolic::Expression
1053
            SymEngine::Expression num_elements(num_elements_str);
2✔
1054
            assert(j.contains("storage_type"));
2✔
1055
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
1056
            assert(j.contains("initializer"));
2✔
1057
            std::string initializer = j["initializer"];
2✔
1058
            assert(j.contains("alignment"));
2✔
1059
            size_t alignment = j["alignment"];
2✔
1060
            return std::make_unique<types::Array>(storage_type, alignment, initializer, *member_type, num_elements);
2✔
1061
        } else if (j["type"] == "pointer") {
11✔
1062
            // Deserialize pointer type
1063
            std::optional<std::unique_ptr<types::IType>> pointee_type;
6✔
1064
            if (j.contains("pointee_type")) {
6✔
1065
                assert(j.contains("pointee_type"));
5✔
1066
                pointee_type = json_to_type(j["pointee_type"]);
5✔
1067
            } else {
5✔
1068
                pointee_type = std::nullopt;
1✔
1069
            }
1070
            assert(j.contains("storage_type"));
6✔
1071
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
6✔
1072
            assert(j.contains("initializer"));
6✔
1073
            std::string initializer = j["initializer"];
6✔
1074
            assert(j.contains("alignment"));
6✔
1075
            size_t alignment = j["alignment"];
6✔
1076
            if (pointee_type.has_value()) {
6✔
1077
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer, *pointee_type.value());
5✔
1078
            } else {
1079
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer);
1✔
1080
            }
1081
        } else if (j["type"] == "structure") {
9✔
1082
            // Deserialize structure type
1083
            assert(j.contains("name"));
2✔
1084
            std::string name = j["name"];
2✔
1085
            assert(j.contains("storage_type"));
2✔
1086
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
1087
            assert(j.contains("initializer"));
2✔
1088
            std::string initializer = j["initializer"];
2✔
1089
            assert(j.contains("alignment"));
2✔
1090
            size_t alignment = j["alignment"];
2✔
1091
            return std::make_unique<types::Structure>(storage_type, alignment, initializer, name);
2✔
1092
        } else if (j["type"] == "function") {
3✔
1093
            // Deserialize function type
1094
            assert(j.contains("return_type"));
1✔
1095
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
1096
            assert(j.contains("params"));
1✔
1097
            std::vector<std::unique_ptr<types::IType>> params;
1✔
1098
            for (const auto& param : j["params"]) {
3✔
1099
                params.push_back(json_to_type(param));
2✔
1100
            }
1101
            assert(j.contains("is_var_arg"));
1✔
1102
            bool is_var_arg = j["is_var_arg"];
1✔
1103
            assert(j.contains("storage_type"));
1✔
1104
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
1✔
1105
            assert(j.contains("initializer"));
1✔
1106
            std::string initializer = j["initializer"];
1✔
1107
            assert(j.contains("alignment"));
1✔
1108
            size_t alignment = j["alignment"];
1✔
1109
            auto function =
1110
                std::make_unique<types::Function>(storage_type, alignment, initializer, *return_type, is_var_arg);
1✔
1111
            for (const auto& param : params) {
3✔
1112
                function->add_param(*param);
2✔
1113
            }
1114
            return function->clone();
1✔
1115

1116
        } else {
1✔
1117
            throw std::runtime_error("Unknown type");
×
1118
        }
1119
    } else {
1120
        throw std::runtime_error("Type not found");
×
1121
    }
1122
}
41✔
1123

1124
DebugLoc JSONSerializer::json_to_debug_loc(const nlohmann::json& j) {
7✔
1125
    assert(j.contains("has"));
7✔
1126
    assert(j["has"].is_boolean());
7✔
1127
    if (!j["has"]) {
7✔
NEW
1128
        return DebugLoc();
×
1129
    }
1130
    assert(j.contains("filename"));
7✔
1131
    assert(j["filename"].is_string());
7✔
1132
    std::string filename = j["filename"];
7✔
1133
    assert(j.contains("function"));
7✔
1134
    assert(j["function"].is_string());
7✔
1135
    std::string function = j["function"];
7✔
1136
    assert(j.contains("line"));
7✔
1137
    assert(j["line"].is_number_integer());
7✔
1138
    size_t line = j["line"];
7✔
1139
    assert(j.contains("column"));
7✔
1140
    assert(j["column"].is_number_integer());
7✔
1141
    size_t column = j["column"];
7✔
1142
    return DebugLoc(filename, function, line, column, true);
7✔
1143
}
7✔
1144

1145
DebugInfo JSONSerializer::json_to_debug_info_element(const nlohmann::json& j) {
3✔
1146
    assert(j.contains("has"));
3✔
1147
    assert(j["has"].is_boolean());
3✔
1148
    if (!j["has"]) {
3✔
UNCOV
1149
        return DebugInfo();
×
1150
    }
1151
    assert(j.contains("locations"));
3✔
1152
    assert(j["locations"].is_array());
3✔
1153
    std::vector<DebugLoc> locations;
3✔
1154
    for (const auto& loc_json : j["locations"]) {
9✔
1155
        locations.push_back(json_to_debug_loc(loc_json));
6✔
1156
    }
1157
    return DebugInfo(locations);
3✔
1158
}
3✔
1159

1160
DebugInfoRegion JSONSerializer::json_to_debug_info_region(const nlohmann::json& j, const DebugTable& debug_info) {
74✔
1161
    assert(j.contains("has"));
74✔
1162
    assert(j["has"].is_boolean());
74✔
1163
    if (!j["has"]) {
74✔
1164
        return DebugInfoRegion();
72✔
1165
    }
1166

1167
    assert(j.contains("indices"));
2✔
1168
    assert(j["indices"].is_array());
2✔
1169
    std::unordered_set<size_t> indices;
2✔
1170
    for (auto index_json : j["indices"]) {
6✔
1171
        size_t index = index_json;
4✔
1172
        indices.insert(index);
4✔
1173
    }
4✔
1174
    DebugInfoRegion debug_info_region(indices, debug_info.elements());
2✔
1175
    assert(j.contains("filename"));
2✔
1176
    assert(j["filename"].is_string());
2✔
1177
    std::string filename = j["filename"];
2✔
1178
    assert(filename == debug_info_region.filename());
2✔
1179

1180
    assert(j.contains("function"));
2✔
1181
    assert(j["function"].is_string());
2✔
1182
    std::string function = j["function"];
2✔
1183
    assert(function == debug_info_region.function());
2✔
1184

1185
    assert(j.contains("start_line"));
2✔
1186
    assert(j["start_line"].is_number_integer());
2✔
1187
    size_t start_line = j["start_line"];
2✔
1188
    assert(start_line == debug_info_region.start_line());
2✔
1189

1190
    assert(j.contains("start_column"));
2✔
1191
    assert(j["start_column"].is_number_integer());
2✔
1192
    size_t start_column = j["start_column"];
2✔
1193
    assert(start_column == debug_info_region.start_column());
2✔
1194

1195
    assert(j.contains("end_line"));
2✔
1196
    assert(j["end_line"].is_number_integer());
2✔
1197
    size_t end_line = j["end_line"];
2✔
1198
    assert(end_line == debug_info_region.end_line());
2✔
1199

1200
    assert(j.contains("end_column"));
2✔
1201
    assert(j["end_column"].is_number_integer());
2✔
1202
    size_t end_column = j["end_column"];
2✔
1203
    assert(end_column == debug_info_region.end_column());
2✔
1204

1205
    return debug_info_region;
2✔
1206
}
74✔
1207

1208
DebugTable JSONSerializer::json_to_debug_table(const nlohmann::json& j) {
6✔
1209
    assert(j.contains("elements"));
6✔
1210
    assert(j["elements"].is_array());
6✔
1211
    DebugTable debug_info;
6✔
1212
    DebugInfos elements;
6✔
1213
    for (const auto& entry_json : j["elements"]) {
8✔
1214
        debug_info.add_element(json_to_debug_info_element(entry_json));
2✔
1215
    }
1216
    return debug_info;
6✔
1217
}
6✔
1218

1219
ScheduleType JSONSerializer::json_to_schedule_type(const nlohmann::json& j) {
2✔
1220
    assert(j.contains("value"));
2✔
1221
    assert(j["value"].is_string());
2✔
1222
    assert(j.contains("properties"));
2✔
1223
    assert(j["properties"].is_object());
2✔
1224
    ScheduleType schedule_type(j["value"].get<std::string>());
2✔
1225
    for (const auto& [key, value] : j["properties"].items()) {
4✔
1226
        assert(value.is_string());
1✔
1227
        schedule_type.set_property(key, value.get<std::string>());
1✔
1228
    }
1229
    return schedule_type;
2✔
1230
}
2✔
1231

1232
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
48✔
1233
    JSONSymbolicPrinter printer;
48✔
1234
    return printer.apply(expr);
48✔
1235
};
48✔
1236

1237
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1238
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1239
    str_ = parenthesize(str_);
×
1240
};
×
1241

1242
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1243
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1244
    str_ = parenthesize(str_);
×
1245
};
×
1246

1247
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1248
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1249
    str_ = parenthesize(str_);
×
1250
};
×
1251

1252
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1253
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1254
    str_ = parenthesize(str_);
4✔
1255
};
4✔
1256

1257
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1258
    std::ostringstream s;
×
1259
    auto container = x.get_args();
×
1260
    if (container.size() == 1) {
×
1261
        s << apply(*container.begin());
×
1262
    } else {
×
1263
        s << "min(";
×
1264
        s << apply(*container.begin());
×
1265

1266
        // Recursively apply __daisy_min to the arguments
1267
        SymEngine::vec_basic subargs;
×
1268
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1269
            subargs.push_back(*it);
×
1270
        }
×
1271
        auto submin = SymEngine::min(subargs);
×
1272
        s << ", " << apply(submin);
×
1273

1274
        s << ")";
×
1275
    }
×
1276

1277
    str_ = s.str();
×
1278
};
×
1279

1280
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1281
    std::ostringstream s;
×
1282
    auto container = x.get_args();
×
1283
    if (container.size() == 1) {
×
1284
        s << apply(*container.begin());
×
1285
    } else {
×
1286
        s << "max(";
×
1287
        s << apply(*container.begin());
×
1288

1289
        // Recursively apply __daisy_max to the arguments
1290
        SymEngine::vec_basic subargs;
×
1291
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1292
            subargs.push_back(*it);
×
1293
        }
×
1294
        auto submax = SymEngine::max(subargs);
×
1295
        s << ", " << apply(submax);
×
1296

1297
        s << ")";
×
1298
    }
×
1299

1300
    str_ = s.str();
×
1301
};
×
1302

1303
void LibraryNodeSerializerRegistry::
1304
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
58✔
1305
    std::lock_guard<std::mutex> lock(mutex_);
58✔
1306
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
58✔
1307
        throw std::runtime_error(
×
1308
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1309
        );
1310
    }
1311
    factory_map_[library_node_code] = std::move(fn);
58✔
1312
}
58✔
1313

1314
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1315
    auto it = factory_map_.find(library_node_code);
1✔
1316
    if (it != factory_map_.end()) {
1✔
1317
        return it->second;
1✔
1318
    }
1319
    return nullptr;
×
1320
}
1✔
1321

1322
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1323

1324
void register_default_serializers() {
2✔
1325
    // Metadata
1326
    LibraryNodeSerializerRegistry::instance()
2✔
1327
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
1328
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1329
        });
1330

1331
    // Barrier
1332
    LibraryNodeSerializerRegistry::instance()
2✔
1333
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1334
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1335
        });
1336

1337
    // ML
1338
    LibraryNodeSerializerRegistry::instance()
2✔
1339
        .register_library_node_serializer(math::ml::LibraryNodeType_Abs.value(), []() {
2✔
1340
            return std::make_unique<math::ml::AbsNodeSerializer>();
×
1341
        });
1342
    LibraryNodeSerializerRegistry::instance()
2✔
1343
        .register_library_node_serializer(math::ml::LibraryNodeType_Add.value(), []() {
2✔
1344
            return std::make_unique<math::ml::AddNodeSerializer>();
×
1345
        });
1346
    LibraryNodeSerializerRegistry::instance()
2✔
1347
        .register_library_node_serializer(math::ml::LibraryNodeType_BatchNormalization.value(), []() {
2✔
1348
            return std::make_unique<math::ml::BatchNormalizationNodeSerializer>();
×
1349
        });
1350
    LibraryNodeSerializerRegistry::instance()
2✔
1351
        .register_library_node_serializer(math::ml::LibraryNodeType_Clip.value(), []() {
2✔
1352
            return std::make_unique<math::ml::ClipNodeSerializer>();
×
1353
        });
1354
    LibraryNodeSerializerRegistry::instance()
2✔
1355
        .register_library_node_serializer(math::ml::LibraryNodeType_Conv.value(), []() {
2✔
1356
            return std::make_unique<math::ml::ConvNodeSerializer>();
×
1357
        });
1358
    LibraryNodeSerializerRegistry::instance()
2✔
1359
        .register_library_node_serializer(math::ml::LibraryNodeType_Div.value(), []() {
2✔
1360
            return std::make_unique<math::ml::DivNodeSerializer>();
×
1361
        });
1362
    LibraryNodeSerializerRegistry::instance()
2✔
1363
        .register_library_node_serializer(math::ml::LibraryNodeType_Dropout.value(), []() {
2✔
1364
            return std::make_unique<math::ml::DropoutSerializer>();
×
1365
        });
1366
    LibraryNodeSerializerRegistry::instance()
2✔
1367
        .register_library_node_serializer(math::ml::LibraryNodeType_Elu.value(), []() {
2✔
1368
            return std::make_unique<math::ml::EluNodeSerializer>();
×
1369
        });
1370
    LibraryNodeSerializerRegistry::instance()
2✔
1371
        .register_library_node_serializer(math::ml::LibraryNodeType_Erf.value(), []() {
2✔
1372
            return std::make_unique<math::ml::ErfNodeSerializer>();
×
1373
        });
1374
    LibraryNodeSerializerRegistry::instance()
2✔
1375
        .register_library_node_serializer(math::ml::LibraryNodeType_Gemm.value(), []() {
2✔
1376
            return std::make_unique<math::ml::GemmNodeSerializer>();
×
1377
        });
1378
    LibraryNodeSerializerRegistry::instance()
2✔
1379
        .register_library_node_serializer(math::ml::LibraryNodeType_HardSigmoid.value(), []() {
2✔
1380
            return std::make_unique<math::ml::HardSigmoidNodeSerializer>();
×
1381
        });
1382
    LibraryNodeSerializerRegistry::instance()
2✔
1383
        .register_library_node_serializer(math::ml::LibraryNodeType_LayerNormalization.value(), []() {
2✔
1384
            return std::make_unique<math::ml::LayerNormalizationNodeSerializer>();
×
1385
        });
1386
    LibraryNodeSerializerRegistry::instance()
2✔
1387
        .register_library_node_serializer(math::ml::LibraryNodeType_LeakyReLU.value(), []() {
2✔
1388
            return std::make_unique<math::ml::LeakyReLUNodeSerializer>();
×
1389
        });
1390
    LibraryNodeSerializerRegistry::instance()
2✔
1391
        .register_library_node_serializer(math::ml::LibraryNodeType_LogSoftmax.value(), []() {
2✔
1392
            return std::make_unique<math::ml::LogSoftmaxNodeSerializer>();
×
1393
        });
1394
    LibraryNodeSerializerRegistry::instance()
2✔
1395
        .register_library_node_serializer(math::ml::LibraryNodeType_MatMul.value(), []() {
2✔
1396
            return std::make_unique<math::ml::MatMulNodeSerializer>();
×
1397
        });
1398
    LibraryNodeSerializerRegistry::instance()
2✔
1399
        .register_library_node_serializer(math::ml::LibraryNodeType_MaxPool.value(), []() {
2✔
1400
            return std::make_unique<math::ml::MaxPoolNodeSerializer>();
×
1401
        });
1402
    LibraryNodeSerializerRegistry::instance()
2✔
1403
        .register_library_node_serializer(math::ml::LibraryNodeType_Mul.value(), []() {
2✔
1404
            return std::make_unique<math::ml::MulNodeSerializer>();
×
1405
        });
1406
    LibraryNodeSerializerRegistry::instance()
2✔
1407
        .register_library_node_serializer(math::ml::LibraryNodeType_Pow.value(), []() {
2✔
1408
            return std::make_unique<math::ml::PowNodeSerializer>();
×
1409
        });
1410
    LibraryNodeSerializerRegistry::instance()
2✔
1411
        .register_library_node_serializer(math::ml::LibraryNodeType_ReduceMean.value(), []() {
2✔
1412
            return std::make_unique<math::ml::ReduceMeanNodeSerializer>();
×
1413
        });
1414
    LibraryNodeSerializerRegistry::instance()
2✔
1415
        .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
2✔
1416
            return std::make_unique<math::ml::ReLUNodeSerializer>();
×
1417
        });
1418
    LibraryNodeSerializerRegistry::instance()
2✔
1419
        .register_library_node_serializer(math::ml::LibraryNodeType_Sigmoid.value(), []() {
2✔
1420
            return std::make_unique<math::ml::SigmoidNodeSerializer>();
×
1421
        });
1422
    LibraryNodeSerializerRegistry::instance()
2✔
1423
        .register_library_node_serializer(math::ml::LibraryNodeType_Softmax.value(), []() {
2✔
1424
            return std::make_unique<math::ml::SoftmaxNodeSerializer>();
×
1425
        });
1426
    LibraryNodeSerializerRegistry::instance()
2✔
1427
        .register_library_node_serializer(math::ml::LibraryNodeType_Sqrt.value(), []() {
2✔
1428
            return std::make_unique<math::ml::SqrtNodeSerializer>();
×
1429
        });
1430
    LibraryNodeSerializerRegistry::instance()
2✔
1431
        .register_library_node_serializer(math::ml::LibraryNodeType_Sub.value(), []() {
2✔
1432
            return std::make_unique<math::ml::SubNodeSerializer>();
×
1433
        });
1434
    LibraryNodeSerializerRegistry::instance()
2✔
1435
        .register_library_node_serializer(math::ml::LibraryNodeType_Tanh.value(), []() {
2✔
1436
            return std::make_unique<math::ml::TanhNodeSerializer>();
×
1437
        });
1438

1439
    // BLAS
1440
    LibraryNodeSerializerRegistry::instance()
2✔
1441
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1442
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1443
        });
1444
    LibraryNodeSerializerRegistry::instance()
2✔
1445
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1446
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1447
        });
1448
}
2✔
1449

1450
} // namespace serializer
1451
} // 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