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

daisytuner / sdfglib / 16454790608

22 Jul 2025 08:26PM UTC coverage: 65.244% (-0.8%) from 66.011%
16454790608

Pull #156

github

web-flow
Merge 8b3fea29a into 4c085404b
Pull Request #156: adds draft for GEMM node

10 of 165 new or added lines in 5 files covered. (6.06%)

3 existing lines in 1 file now uncovered.

8331 of 12769 relevant lines covered (65.24%)

132.3 hits per line

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

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

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

9
#include "sdfg/data_flow/library_nodes/math/math.h"
10

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

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

32
namespace sdfg {
33
namespace serializer {
34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

120
    return j;
4✔
121
}
4✔
122

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

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

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

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

174
        j["nodes"].push_back(node_json);
20✔
175
    }
20✔
176

177
    for (auto& edge : dataflow.edges()) {
36✔
178
        nlohmann::json edge_json;
15✔
179
        edge_json["element_id"] = edge.element_id();
15✔
180

181
        edge_json["debug_info"] = nlohmann::json::object();
15✔
182
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
15✔
183

184
        edge_json["src"] = edge.src().element_id();
15✔
185
        edge_json["dst"] = edge.dst().element_id();
15✔
186

187
        edge_json["src_conn"] = edge.src_conn();
15✔
188
        edge_json["dst_conn"] = edge.dst_conn();
15✔
189

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

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

200
        edge_json["end_subset"] = nlohmann::json::array();
15✔
201
        for (auto& subset : edge.end_subset()) {
21✔
202
            edge_json["end_subset"].push_back(expression(subset));
6✔
203
        }
204

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

315
    j["debug_info"] = nlohmann::json::object();
20✔
316
    debug_info_to_json(j["debug_info"], sequence.debug_info());
20✔
317

318
    j["children"] = nlohmann::json::array();
20✔
319
    j["transitions"] = nlohmann::json::array();
20✔
320

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

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

348
        j["children"].push_back(child_json);
21✔
349

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

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

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

366
        j["transitions"].push_back(transition_json);
21✔
367
    }
21✔
368
}
20✔
369

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

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

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

440
/*
441
 * * Deserialization logic
442
 */
443

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

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

454
    size_t element_counter = j["element_counter"];
4✔
455
    builder.set_element_counter(element_counter);
4✔
456

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

466
    nlohmann::json& containers = j["containers"];
4✔
467

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

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

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

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

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

506
    builder.set_element_counter(element_counter);
4✔
507

508
    return builder.move();
4✔
509
}
4✔
510

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

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

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

546
    assert(j.contains("nodes"));
11✔
547
    assert(j["nodes"].is_array());
11✔
548
    for (const auto& node : j["nodes"]) {
27✔
549
        assert(node.contains("type"));
16✔
550
        assert(node["type"].is_string());
16✔
551
        assert(node.contains("element_id"));
16✔
552
        assert(node["element_id"].is_number_integer());
16✔
553
        std::string type = node["type"];
16✔
554
        if (type == "tasklet") {
16✔
555
            assert(node.contains("code"));
4✔
556
            assert(node["code"].is_number_integer());
4✔
557
            assert(node.contains("inputs"));
4✔
558
            assert(node["inputs"].is_array());
4✔
559
            assert(node.contains("output"));
4✔
560
            assert(node["output"].is_object());
4✔
561
            assert(node["output"].contains("name"));
4✔
562
            assert(node["output"].contains("type"));
4✔
563
            auto inputs = json_to_arguments(node["inputs"]);
4✔
564

565
            auto output_name = node["output"]["name"];
4✔
566
            auto output_type = json_to_type(node["output"]["type"]);
4✔
567
            auto& output_type_scalar = dynamic_cast<types::Scalar&>(*output_type);
4✔
568

569
            auto& tasklet = builder.add_tasklet(
8✔
570
                parent, node["code"], {output_name, output_type_scalar}, inputs, json_to_debug_info(node["debug_info"])
4✔
571
            );
572
            tasklet.element_id_ = node["element_id"];
4✔
573
            nodes_map.insert({node["element_id"], tasklet});
4✔
574
        } else if (type == "library_node") {
16✔
575
            assert(node.contains("code"));
×
576
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
577

578
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
579
            if (serializer_fn == nullptr) {
×
580
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
581
            }
582
            auto serializer = serializer_fn();
×
583
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
NEW
584
            lib_node.implementation_type() =
×
NEW
585
                data_flow::ImplementationType(node["implementation_type"].get<std::string>());
×
586
            lib_node.element_id_ = node["element_id"];
×
587
            nodes_map.insert({node["element_id"], lib_node});
×
588
        } else if (type == "access_node") {
12✔
589
            assert(node.contains("data"));
12✔
590
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
591
            access_node.element_id_ = node["element_id"];
12✔
592
            nodes_map.insert({node["element_id"], access_node});
12✔
593
        } else {
12✔
594
            throw std::runtime_error("Unknown node type");
×
595
        }
596
    }
16✔
597

598
    assert(j.contains("edges"));
11✔
599
    assert(j["edges"].is_array());
11✔
600
    for (const auto& edge : j["edges"]) {
23✔
601
        assert(edge.contains("src"));
12✔
602
        assert(edge["src"].is_number_integer());
12✔
603
        assert(edge.contains("dst"));
12✔
604
        assert(edge["dst"].is_number_integer());
12✔
605
        assert(edge.contains("src_conn"));
12✔
606
        assert(edge["src_conn"].is_string());
12✔
607
        assert(edge.contains("dst_conn"));
12✔
608
        assert(edge["dst_conn"].is_string());
12✔
609
        assert(edge.contains("subset"));
12✔
610
        assert(edge["subset"].is_array());
12✔
611

612
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
613
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
614
        auto& source = nodes_map.at(edge["src"]);
12✔
615
        auto& target = nodes_map.at(edge["dst"]);
12✔
616

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

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

678
    sequence.element_id_ = j["element_id"];
13✔
679
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
13✔
680

681
    std::string type = j["type"];
13✔
682
    if (type == "sequence") {
13✔
683
        for (size_t i = 0; i < j["children"].size(); i++) {
24✔
684
            auto& child = j["children"][i];
11✔
685
            auto& transition = j["transitions"][i];
11✔
686
            assert(child.contains("type"));
11✔
687
            assert(child["type"].is_string());
11✔
688

689
            assert(transition.contains("type"));
11✔
690
            assert(transition["type"].is_string());
11✔
691
            assert(transition.contains("assignments"));
11✔
692
            assert(transition["assignments"].is_array());
11✔
693
            control_flow::Assignments assignments;
11✔
694
            for (const auto& assignment : transition["assignments"]) {
13✔
695
                assert(assignment.contains("symbol"));
2✔
696
                assert(assignment["symbol"].is_string());
2✔
697
                assert(assignment.contains("expression"));
2✔
698
                assert(assignment["expression"].is_string());
2✔
699
                SymEngine::Expression expr(assignment["expression"]);
2✔
700
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
2✔
701
            }
2✔
702

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

726
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
11✔
727
            sequence.at(i).second.element_id_ = transition["element_id"];
11✔
728
        }
11✔
729
    } else {
13✔
730
        throw std::runtime_error("expected sequence type");
×
731
    }
732
}
13✔
733

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

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

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

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

791
void JSONSerializer::json_to_if_else_node(
1✔
792
    const nlohmann::json& j,
793
    builder::StructuredSDFGBuilder& builder,
794
    structured_control_flow::Sequence& parent,
795
    control_flow::Assignments& assignments
796
) {
797
    assert(j.contains("type"));
1✔
798
    assert(j["type"].is_string());
1✔
799
    assert(j["type"] == "if_else");
1✔
800
    assert(j.contains("branches"));
1✔
801
    assert(j["branches"].is_array());
1✔
802
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
803
    if_else_node.element_id_ = j["element_id"];
1✔
804
    for (const auto& branch : j["branches"]) {
3✔
805
        assert(branch.contains("condition"));
2✔
806
        assert(branch["condition"].is_string());
2✔
807
        assert(branch.contains("root"));
2✔
808
        assert(branch["root"].is_object());
2✔
809
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
810
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
2✔
811
        symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()
2✔
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_string());
1✔
891

892
    structured_control_flow::ScheduleType schedule_type =
893
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
894

895
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
896
    SymEngine::Expression init(j["init"]);
1✔
897
    SymEngine::Expression condition_expr(j["condition"]);
1✔
898
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
899
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
900
    SymEngine::Expression update(j["update"]);
1✔
901

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

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

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

923
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
924
    node.element_id_ = j["element_id"];
1✔
925
}
1✔
926

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

1001
        } else {
1✔
1002
            throw std::runtime_error("Unknown type");
×
1003
        }
1004
    } else {
1005
        throw std::runtime_error("Type not found");
×
1006
    }
1007
}
35✔
1008

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

1033
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
47✔
1034
    JSONSymbolicPrinter printer;
47✔
1035
    return printer.apply(expr);
47✔
1036
};
47✔
1037

1038
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1039
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1040
    str_ = parenthesize(str_);
×
1041
};
×
1042

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

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

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

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

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

1075
        s << ")";
×
1076
    }
×
1077

1078
    str_ = s.str();
×
1079
};
×
1080

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

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

1098
        s << ")";
×
1099
    }
×
1100

1101
    str_ = s.str();
×
1102
};
×
1103

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

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

1123
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1124

1125
void register_default_serializers() {
2✔
1126
    LibraryNodeSerializerRegistry::instance()
2✔
1127
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
1128
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1129
        });
1130
    LibraryNodeSerializerRegistry::instance()
2✔
1131
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1132
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1133
        });
1134

1135
    /* Math */
1136
    LibraryNodeSerializerRegistry::instance()
2✔
1137
        .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
2✔
1138
            return std::make_unique<math::ml::ReLUNodeSerializer>();
×
1139
        });
1140
    // Add more serializers as needed
1141
}
2✔
1142

1143
} // namespace serializer
1144
} // 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