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

daisytuner / sdfglib / 16352351849

17 Jul 2025 05:55PM UTC coverage: 64.93% (+0.07%) from 64.863%
16352351849

Pull #149

github

web-flow
Merge 48036fa97 into 414dcdab4
Pull Request #149: Extends handling of library nodes in passes

30 of 54 new or added lines in 2 files covered. (55.56%)

2 existing lines in 1 file now uncovered.

8750 of 13476 relevant lines covered (64.93%)

175.84 hits per line

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

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

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

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

81
    j["name"] = sdfg->name();
3✔
82
    j["type"] = std::string(sdfg->type().value());
3✔
83

84
    j["structures"] = nlohmann::json::array();
3✔
85
    for (const auto& structure_name : sdfg->structures()) {
3✔
86
        const auto& structure = sdfg->structure(structure_name);
×
87
        nlohmann::json structure_json;
×
88
        structure_definition_to_json(structure_json, structure);
×
89
        j["structures"].push_back(structure_json);
×
90
    }
×
91

92
    j["containers"] = nlohmann::json::object();
3✔
93
    for (const auto& container : sdfg->containers()) {
8✔
94
        nlohmann::json desc;
5✔
95
        type_to_json(desc, sdfg->type(container));
5✔
96
        j["containers"][container] = desc;
5✔
97
    }
5✔
98

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

104
    j["externals"] = nlohmann::json::array();
3✔
105
    for (const auto& external : sdfg->externals()) {
3✔
106
        j["externals"].push_back(external);
×
107
    }
108

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

114
    // Walk the SDFG
115
    nlohmann::json root_json;
3✔
116
    sequence_to_json(root_json, sdfg->root());
3✔
117
    j["root"] = root_json;
3✔
118

119
    return j;
3✔
120
}
3✔
121

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

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

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

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

172
        j["nodes"].push_back(node_json);
20✔
173
    }
20✔
174

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

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

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

185
        edge_json["src_conn"] = edge.src_conn();
15✔
186
        edge_json["dst_conn"] = edge.dst_conn();
15✔
187

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

438
/*
439
 * * Deserialization logic
440
 */
441

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

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

451
    // deserialize structures
452
    assert(j.contains("structures"));
3✔
453
    assert(j["structures"].is_array());
3✔
454
    for (const auto& structure : j["structures"]) {
3✔
455
        assert(structure.contains("name"));
×
456
        assert(structure["name"].is_string());
×
457
        json_to_structure_definition(structure, builder);
×
458
    }
459

460
    nlohmann::json& containers = j["containers"];
3✔
461

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

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

476
    // deserialize transients
477
    for (const auto& entry : containers.items()) {
8✔
478
        if (builder.subject().is_argument(entry.key())) {
5✔
479
            continue;
2✔
480
        }
481
        if (builder.subject().is_external(entry.key())) {
3✔
482
            continue;
×
483
        }
484
        auto type = json_to_type(entry.value());
3✔
485
        builder.add_container(entry.key(), *type, false, false);
3✔
486
    }
3✔
487

488
    // deserialize root node
489
    assert(j.contains("root"));
3✔
490
    auto& root = builder.subject().root();
3✔
491
    json_to_sequence(j["root"], builder, root);
3✔
492

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

500
    return builder.move();
3✔
501
}
3✔
502

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

519
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
4✔
520
    std::vector<std::pair<std::string, types::Scalar>> arguments;
4✔
521
    for (const auto& argument : j) {
12✔
522
        assert(argument.contains("name"));
8✔
523
        assert(argument["name"].is_string());
8✔
524
        assert(argument.contains("type"));
8✔
525
        assert(argument["type"].is_object());
8✔
526
        std::string name = argument["name"];
8✔
527
        auto type = json_to_type(argument["type"]);
8✔
528
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
8✔
529
    }
8✔
530
    return arguments;
4✔
531
}
4✔
532

533
void JSONSerializer::json_to_dataflow(
10✔
534
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
535
) {
536
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
10✔
537

538
    assert(j.contains("nodes"));
10✔
539
    assert(j["nodes"].is_array());
10✔
540
    for (const auto& node : j["nodes"]) {
26✔
541
        assert(node.contains("type"));
16✔
542
        assert(node["type"].is_string());
16✔
543
        assert(node.contains("element_id"));
16✔
544
        assert(node["element_id"].is_number_integer());
16✔
545
        std::string type = node["type"];
16✔
546
        if (type == "tasklet") {
16✔
547
            assert(node.contains("code"));
4✔
548
            assert(node["code"].is_number_integer());
4✔
549
            assert(node.contains("inputs"));
4✔
550
            assert(node["inputs"].is_array());
4✔
551
            assert(node.contains("output"));
4✔
552
            assert(node["output"].is_object());
4✔
553
            assert(node["output"].contains("name"));
4✔
554
            assert(node["output"].contains("type"));
4✔
555
            auto inputs = json_to_arguments(node["inputs"]);
4✔
556

557
            auto output_name = node["output"]["name"];
4✔
558
            auto output_type = json_to_type(node["output"]["type"]);
4✔
559
            auto& output_type_scalar = dynamic_cast<types::Scalar&>(*output_type);
4✔
560

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

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

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

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

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

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

668
    sequence.element_id_ = j["element_id"];
12✔
669
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
12✔
670

671
    std::string type = j["type"];
12✔
672
    if (type == "sequence") {
12✔
673
        for (size_t i = 0; i < j["children"].size(); i++) {
22✔
674
            auto& child = j["children"][i];
10✔
675
            auto& transition = j["transitions"][i];
10✔
676
            assert(child.contains("type"));
10✔
677
            assert(child["type"].is_string());
10✔
678

679
            assert(transition.contains("type"));
10✔
680
            assert(transition["type"].is_string());
10✔
681
            assert(transition.contains("assignments"));
10✔
682
            assert(transition["assignments"].is_array());
10✔
683
            control_flow::Assignments assignments;
10✔
684
            for (const auto& assignment : transition["assignments"]) {
11✔
685
                assert(assignment.contains("symbol"));
1✔
686
                assert(assignment["symbol"].is_string());
1✔
687
                assert(assignment.contains("expression"));
1✔
688
                assert(assignment["expression"].is_string());
1✔
689
                SymEngine::Expression expr(assignment["expression"]);
1✔
690
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
1✔
691
            }
1✔
692

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

716
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
10✔
717
            sequence.at(i).second.element_id_ = transition["element_id"];
10✔
718
        }
10✔
719
    } else {
12✔
720
        throw std::runtime_error("expected sequence type");
×
721
    }
722
}
12✔
723

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

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

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

775
    assert(j["root"].contains("type"));
1✔
776
    assert(j["root"]["type"].is_string());
1✔
777
    assert(j["root"]["type"] == "sequence");
1✔
778
    json_to_sequence(j["root"], builder, for_node.root());
1✔
779
}
1✔
780

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

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

827
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
828
    while_node.element_id_ = j["element_id"];
3✔
829

830
    assert(j["root"]["type"] == "sequence");
3✔
831
    json_to_sequence(j["root"], builder, while_node.root());
3✔
832
}
3✔
833

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

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

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

882
    structured_control_flow::ScheduleType schedule_type =
883
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
884

885
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
886
    SymEngine::Expression init(j["init"]);
1✔
887
    SymEngine::Expression condition_expr(j["condition"]);
1✔
888
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
889
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
890
    SymEngine::Expression update(j["update"]);
1✔
891

892
    auto& map_node = builder.add_map(
2✔
893
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
894
    );
895
    map_node.element_id_ = j["element_id"];
1✔
896

897
    assert(j["root"].contains("type"));
1✔
898
    assert(j["root"]["type"].is_string());
1✔
899
    assert(j["root"]["type"] == "sequence");
1✔
900
    json_to_sequence(j["root"], builder, map_node.root());
1✔
901
}
1✔
902

903
void JSONSerializer::json_to_return_node(
1✔
904
    const nlohmann::json& j,
905
    builder::StructuredSDFGBuilder& builder,
906
    structured_control_flow::Sequence& parent,
907
    control_flow::Assignments& assignments
908
) {
909
    assert(j.contains("type"));
1✔
910
    assert(j["type"].is_string());
1✔
911
    assert(j["type"] == "return");
1✔
912

913
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
914
    node.element_id_ = j["element_id"];
1✔
915
}
1✔
916

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

991
        } else {
1✔
992
            throw std::runtime_error("Unknown type");
×
993
        }
994
    } else {
995
        throw std::runtime_error("Type not found");
×
996
    }
997
}
31✔
998

999
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
68✔
1000
    assert(j.contains("has"));
68✔
1001
    assert(j["has"].is_boolean());
68✔
1002
    if (!j["has"]) {
68✔
1003
        return DebugInfo();
68✔
1004
    }
1005
    assert(j.contains("filename"));
×
1006
    assert(j["filename"].is_string());
×
1007
    std::string filename = j["filename"];
×
1008
    assert(j.contains("start_line"));
×
1009
    assert(j["start_line"].is_number_integer());
×
1010
    size_t start_line = j["start_line"];
×
1011
    assert(j.contains("start_column"));
×
1012
    assert(j["start_column"].is_number_integer());
×
1013
    size_t start_column = j["start_column"];
×
1014
    assert(j.contains("end_line"));
×
1015
    assert(j["end_line"].is_number_integer());
×
1016
    size_t end_line = j["end_line"];
×
1017
    assert(j.contains("end_column"));
×
1018
    assert(j["end_column"].is_number_integer());
×
1019
    size_t end_column = j["end_column"];
×
1020
    return DebugInfo(filename, start_line, start_column, end_line, end_column);
×
1021
}
68✔
1022

1023
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
45✔
1024
    JSONSymbolicPrinter printer;
45✔
1025
    return printer.apply(expr);
45✔
1026
};
45✔
1027

1028
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1029
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1030
    str_ = parenthesize(str_);
×
1031
};
×
1032

1033
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1034
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1035
    str_ = parenthesize(str_);
×
1036
};
×
1037

1038
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& 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::StrictLessThan& x) {
4✔
1044
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1045
    str_ = parenthesize(str_);
4✔
1046
};
4✔
1047

1048
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1049
    std::ostringstream s;
×
1050
    auto container = x.get_args();
×
1051
    if (container.size() == 1) {
×
1052
        s << apply(*container.begin());
×
1053
    } else {
×
1054
        s << "min(";
×
1055
        s << apply(*container.begin());
×
1056

1057
        // Recursively apply __daisy_min to the arguments
1058
        SymEngine::vec_basic subargs;
×
1059
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1060
            subargs.push_back(*it);
×
1061
        }
×
1062
        auto submin = SymEngine::min(subargs);
×
1063
        s << ", " << apply(submin);
×
1064

1065
        s << ")";
×
1066
    }
×
1067

1068
    str_ = s.str();
×
1069
};
×
1070

1071
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1072
    std::ostringstream s;
×
1073
    auto container = x.get_args();
×
1074
    if (container.size() == 1) {
×
1075
        s << apply(*container.begin());
×
1076
    } else {
×
1077
        s << "max(";
×
1078
        s << apply(*container.begin());
×
1079

1080
        // Recursively apply __daisy_max to the arguments
1081
        SymEngine::vec_basic subargs;
×
1082
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1083
            subargs.push_back(*it);
×
1084
        }
×
1085
        auto submax = SymEngine::max(subargs);
×
1086
        s << ", " << apply(submax);
×
1087

1088
        s << ")";
×
1089
    }
×
1090

1091
    str_ = s.str();
×
1092
};
×
1093

1094
void LibraryNodeSerializerRegistry::
1095
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
6✔
1096
    std::lock_guard<std::mutex> lock(mutex_);
6✔
1097
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
6✔
1098
        throw std::runtime_error(
×
1099
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1100
        );
1101
    }
1102
    factory_map_[library_node_code] = std::move(fn);
6✔
1103
}
6✔
1104

1105
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1106
    auto it = factory_map_.find(library_node_code);
1✔
1107
    if (it != factory_map_.end()) {
1✔
1108
        return it->second;
1✔
1109
    }
1110
    return nullptr;
×
1111
}
1✔
1112

1113
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1114

1115
void register_default_serializers() {
2✔
1116
    LibraryNodeSerializerRegistry::instance()
2✔
1117
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
1118
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1119
        });
1120
    LibraryNodeSerializerRegistry::instance()
2✔
1121
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1122
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1123
        });
1124

1125
    /* Math */
1126
    LibraryNodeSerializerRegistry::instance()
2✔
1127
        .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
2✔
1128
            return std::make_unique<math::ml::ReLUNodeSerializer>();
×
1129
        });
1130
    // Add more serializers as needed
1131
}
2✔
1132

1133
} // namespace serializer
1134
} // 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