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

daisytuner / sdfglib / 18685751061

21 Oct 2025 01:38PM UTC coverage: 61.158% (+0.2%) from 60.927%
18685751061

push

github

web-flow
Merge pull request #290 from daisytuner/storage-types

utilizes storage type for allocations of variables

71 of 117 new or added lines in 6 files covered. (60.68%)

157 existing lines in 6 files now uncovered.

9337 of 15267 relevant lines covered (61.16%)

92.73 hits per line

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

80.47
/src/serializer/json_serializer.cpp
1
#include "sdfg/serializer/json_serializer.h"
2

3
#include <cassert>
4
#include <memory>
5
#include <unordered_map>
6
#include <utility>
7
#include <vector>
8

9
#include "sdfg/data_flow/library_nodes/barrier_local_node.h"
10
#include "sdfg/data_flow/library_nodes/call_node.h"
11
#include "sdfg/data_flow/library_nodes/math/math.h"
12
#include "sdfg/data_flow/library_nodes/metadata_node.h"
13
#include "sdfg/data_flow/library_nodes/stdlib/stdlib.h"
14

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

34
namespace sdfg {
35
namespace serializer {
36

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

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

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

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

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

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

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

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

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

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

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

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

101
    return j;
7✔
102
}
7✔
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

442

443
/*
444
 * * Deserialization logic
445
 */
446

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

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

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

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

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

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

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

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

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

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

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

521
    builder.set_element_counter(element_counter);
7✔
522

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1086
    types::StorageType::AllocationLifetime allocation_lifetime = j["allocation_lifetime"];
198✔
1087

1088
    return types::StorageType(j["value"].get<std::string>(), allocation_size, allocation_lifetime);
198✔
1089
}
198✔
1090

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

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

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

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

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

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

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

1133
        s << ")";
×
1134
    }
×
1135

1136
    str_ = s.str();
×
1137
};
×
1138

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

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

1156
        s << ")";
×
1157
    }
×
1158

1159
    str_ = s.str();
×
1160
};
×
1161

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

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

1181
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1182

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

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

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

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

1232
    // Intrinsic
1233
    LibraryNodeSerializerRegistry::instance()
2✔
1234
        .register_library_node_serializer(math::LibraryNodeType_Intrinsic.value(), []() {
2✔
1235
            return std::make_unique<math::IntrinsicNodeSerializer>();
×
1236
        });
1237

1238
    // BLAS
1239
    LibraryNodeSerializerRegistry::instance()
2✔
1240
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
2✔
1241
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1242
        });
1243
    LibraryNodeSerializerRegistry::instance()
2✔
1244
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
2✔
1245
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1246
        });
1247

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

1307
} // namespace serializer
1308
} // 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