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

daisytuner / sdfglib / 16345126205

17 Jul 2025 12:29PM UTC coverage: 64.863% (-0.3%) from 65.184%
16345126205

Pull #147

github

web-flow
Merge 28b2c5314 into 56ea48e3e
Pull Request #147: Adds metadata library node

106 of 200 new or added lines in 9 files covered. (53.0%)

1 existing line in 1 file now uncovered.

8717 of 13439 relevant lines covered (64.86%)

176.14 hits per line

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

82.35
/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
        j["edges"].push_back(edge_json);
15✔
194
    }
15✔
195
}
20✔
196

197
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
18✔
198
    j["type"] = "block";
18✔
199
    j["element_id"] = block.element_id();
18✔
200

201
    j["debug_info"] = nlohmann::json::object();
18✔
202
    debug_info_to_json(j["debug_info"], block.debug_info());
18✔
203

204
    nlohmann::json dataflow_json;
18✔
205
    dataflow_to_json(dataflow_json, block.dataflow());
18✔
206
    j["dataflow"] = dataflow_json;
18✔
207
}
18✔
208

209
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
210
    j["type"] = "for";
2✔
211
    j["element_id"] = for_node.element_id();
2✔
212

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

216
    j["indvar"] = expression(for_node.indvar());
2✔
217
    j["init"] = expression(for_node.init());
2✔
218
    j["condition"] = expression(for_node.condition());
2✔
219
    j["update"] = expression(for_node.update());
2✔
220

221
    nlohmann::json body_json;
2✔
222
    sequence_to_json(body_json, for_node.root());
2✔
223
    j["root"] = body_json;
2✔
224
}
2✔
225

226
void JSONSerializer::if_else_to_json(nlohmann::json& j, const structured_control_flow::IfElse& if_else_node) {
2✔
227
    j["type"] = "if_else";
2✔
228
    j["element_id"] = if_else_node.element_id();
2✔
229

230
    j["debug_info"] = nlohmann::json::object();
2✔
231
    debug_info_to_json(j["debug_info"], if_else_node.debug_info());
2✔
232

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

244
void JSONSerializer::while_node_to_json(nlohmann::json& j, const structured_control_flow::While& while_node) {
5✔
245
    j["type"] = "while";
5✔
246
    j["element_id"] = while_node.element_id();
5✔
247

248
    j["debug_info"] = nlohmann::json::object();
5✔
249
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
250

251
    nlohmann::json body_json;
5✔
252
    sequence_to_json(body_json, while_node.root());
5✔
253
    j["root"] = body_json;
5✔
254
}
5✔
255

256
void JSONSerializer::break_node_to_json(nlohmann::json& j, const structured_control_flow::Break& break_node) {
2✔
257
    j["type"] = "break";
2✔
258
    j["element_id"] = break_node.element_id();
2✔
259

260
    j["debug_info"] = nlohmann::json::object();
2✔
261
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
262
}
2✔
263

264
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
265
    j["type"] = "continue";
2✔
266
    j["element_id"] = continue_node.element_id();
2✔
267

268
    j["debug_info"] = nlohmann::json::object();
2✔
269
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
270
}
2✔
271

272
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
273
    j["type"] = "map";
2✔
274
    j["element_id"] = map_node.element_id();
2✔
275

276
    j["debug_info"] = nlohmann::json::object();
2✔
277
    debug_info_to_json(j["debug_info"], map_node.debug_info());
2✔
278

279
    j["indvar"] = expression(map_node.indvar());
2✔
280
    j["init"] = expression(map_node.init());
2✔
281
    j["condition"] = expression(map_node.condition());
2✔
282
    j["update"] = expression(map_node.update());
2✔
283

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

286
    nlohmann::json body_json;
2✔
287
    sequence_to_json(body_json, map_node.root());
2✔
288
    j["root"] = body_json;
2✔
289
}
2✔
290

291
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
292
    j["type"] = "return";
2✔
293
    j["element_id"] = return_node.element_id();
2✔
294

295
    j["debug_info"] = nlohmann::json::object();
2✔
296
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
297
}
2✔
298

299
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
19✔
300
    j["type"] = "sequence";
19✔
301
    j["element_id"] = sequence.element_id();
19✔
302

303
    j["debug_info"] = nlohmann::json::object();
19✔
304
    debug_info_to_json(j["debug_info"], sequence.debug_info());
19✔
305

306
    j["children"] = nlohmann::json::array();
19✔
307
    j["transitions"] = nlohmann::json::array();
19✔
308

309
    for (size_t i = 0; i < sequence.size(); i++) {
39✔
310
        nlohmann::json child_json;
20✔
311
        auto& child = sequence.at(i).first;
20✔
312
        auto& transition = sequence.at(i).second;
20✔
313

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

336
        j["children"].push_back(child_json);
20✔
337

338
        // Add transition information
339
        nlohmann::json transition_json;
20✔
340
        transition_json["type"] = "transition";
20✔
341
        transition_json["element_id"] = transition.element_id();
20✔
342

343
        transition_json["debug_info"] = nlohmann::json::object();
20✔
344
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
20✔
345

346
        transition_json["assignments"] = nlohmann::json::array();
20✔
347
        for (const auto& assignment : transition.assignments()) {
22✔
348
            nlohmann::json assignment_json;
2✔
349
            assignment_json["symbol"] = expression(assignment.first);
2✔
350
            assignment_json["expression"] = expression(assignment.second);
2✔
351
            transition_json["assignments"].push_back(assignment_json);
2✔
352
        }
2✔
353

354
        j["transitions"].push_back(transition_json);
20✔
355
    }
20✔
356
}
19✔
357

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

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

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

428
/*
429
 * * Deserialization logic
430
 */
431

432
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
3✔
433
    assert(j.contains("name"));
3✔
434
    assert(j["name"].is_string());
3✔
435
    assert(j.contains("type"));
3✔
436
    assert(j["type"].is_string());
3✔
437

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

441
    // deserialize structures
442
    assert(j.contains("structures"));
3✔
443
    assert(j["structures"].is_array());
3✔
444
    for (const auto& structure : j["structures"]) {
3✔
445
        assert(structure.contains("name"));
×
446
        assert(structure["name"].is_string());
×
447
        json_to_structure_definition(structure, builder);
×
448
    }
449

450
    nlohmann::json& containers = j["containers"];
3✔
451

452
    // deserialize externals
453
    for (const auto& name : j["externals"]) {
3✔
454
        auto& type_desc = containers.at(name.get<std::string>());
×
455
        auto type = json_to_type(type_desc);
×
456
        builder.add_container(name, *type, false, true);
×
457
    }
×
458

459
    // deserialize arguments
460
    for (const auto& name : j["arguments"]) {
5✔
461
        auto& type_desc = containers.at(name.get<std::string>());
2✔
462
        auto type = json_to_type(type_desc);
2✔
463
        builder.add_container(name, *type, true, false);
2✔
464
    }
2✔
465

466
    // deserialize transients
467
    for (const auto& entry : containers.items()) {
8✔
468
        if (builder.subject().is_argument(entry.key())) {
5✔
469
            continue;
2✔
470
        }
471
        if (builder.subject().is_external(entry.key())) {
3✔
472
            continue;
×
473
        }
474
        auto type = json_to_type(entry.value());
3✔
475
        builder.add_container(entry.key(), *type, false, false);
3✔
476
    }
3✔
477

478
    // deserialize root node
479
    assert(j.contains("root"));
3✔
480
    auto& root = builder.subject().root();
3✔
481
    json_to_sequence(j["root"], builder, root);
3✔
482

483
    // deserialize metadata
484
    assert(j.contains("metadata"));
3✔
485
    assert(j["metadata"].is_object());
3✔
486
    for (const auto& entry : j["metadata"].items()) {
4✔
487
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
488
    }
489

490
    return builder.move();
3✔
491
}
3✔
492

493
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
1✔
494
    assert(j.contains("name"));
1✔
495
    assert(j["name"].is_string());
1✔
496
    assert(j.contains("members"));
1✔
497
    assert(j["members"].is_array());
1✔
498
    assert(j.contains("is_packed"));
1✔
499
    assert(j["is_packed"].is_boolean());
1✔
500
    auto is_packed = j["is_packed"];
1✔
501
    auto& definition = builder.add_structure(j["name"], is_packed);
1✔
502
    for (const auto& member : j["members"]) {
3✔
503
        nlohmann::json member_json;
2✔
504
        auto member_type = json_to_type(member);
2✔
505
        definition.add_member(*member_type);
2✔
506
    }
2✔
507
}
1✔
508

509
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
4✔
510
    std::vector<std::pair<std::string, types::Scalar>> arguments;
4✔
511
    for (const auto& argument : j) {
12✔
512
        assert(argument.contains("name"));
8✔
513
        assert(argument["name"].is_string());
8✔
514
        assert(argument.contains("type"));
8✔
515
        assert(argument["type"].is_object());
8✔
516
        std::string name = argument["name"];
8✔
517
        auto type = json_to_type(argument["type"]);
8✔
518
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
8✔
519
    }
8✔
520
    return arguments;
4✔
521
}
4✔
522

523
void JSONSerializer::json_to_dataflow(
10✔
524
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
525
) {
526
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
10✔
527

528
    assert(j.contains("nodes"));
10✔
529
    assert(j["nodes"].is_array());
10✔
530
    for (const auto& node : j["nodes"]) {
26✔
531
        assert(node.contains("type"));
16✔
532
        assert(node["type"].is_string());
16✔
533
        assert(node.contains("element_id"));
16✔
534
        assert(node["element_id"].is_number_integer());
16✔
535
        std::string type = node["type"];
16✔
536
        if (type == "tasklet") {
16✔
537
            assert(node.contains("code"));
4✔
538
            assert(node["code"].is_number_integer());
4✔
539
            assert(node.contains("inputs"));
4✔
540
            assert(node["inputs"].is_array());
4✔
541
            assert(node.contains("output"));
4✔
542
            assert(node["output"].is_object());
4✔
543
            assert(node["output"].contains("name"));
4✔
544
            assert(node["output"].contains("type"));
4✔
545
            auto inputs = json_to_arguments(node["inputs"]);
4✔
546

547
            auto output_name = node["output"]["name"];
4✔
548
            auto output_type = json_to_type(node["output"]["type"]);
4✔
549
            auto& output_type_scalar = dynamic_cast<types::Scalar&>(*output_type);
4✔
550

551
            auto& tasklet = builder.add_tasklet(
8✔
552
                parent, node["code"], {output_name, output_type_scalar}, inputs, json_to_debug_info(node["debug_info"])
4✔
553
            );
554
            tasklet.element_id_ = node["element_id"];
4✔
555
            nodes_map.insert({node["element_id"], tasklet});
4✔
556
        } else if (type == "library_node") {
16✔
557
            assert(node.contains("code"));
×
558
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
559

560
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
561
            if (serializer_fn == nullptr) {
×
562
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
563
            }
564
            auto serializer = serializer_fn();
×
565
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
566
            lib_node.element_id_ = node["element_id"];
×
567
            nodes_map.insert({node["element_id"], lib_node});
×
568
        } else if (type == "access_node") {
12✔
569
            assert(node.contains("data"));
12✔
570
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
571
            access_node.element_id_ = node["element_id"];
12✔
572
            nodes_map.insert({node["element_id"], access_node});
12✔
573
        } else {
12✔
574
            throw std::runtime_error("Unknown node type");
×
575
        }
576
    }
16✔
577

578
    assert(j.contains("edges"));
10✔
579
    assert(j["edges"].is_array());
10✔
580
    for (const auto& edge : j["edges"]) {
22✔
581
        assert(edge.contains("src"));
12✔
582
        assert(edge["src"].is_number_integer());
12✔
583
        assert(edge.contains("dst"));
12✔
584
        assert(edge["dst"].is_number_integer());
12✔
585
        assert(edge.contains("src_conn"));
12✔
586
        assert(edge["src_conn"].is_string());
12✔
587
        assert(edge.contains("dst_conn"));
12✔
588
        assert(edge["dst_conn"].is_string());
12✔
589
        assert(edge.contains("subset"));
12✔
590
        assert(edge["subset"].is_array());
12✔
591

592
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
593
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
594
        auto& source = nodes_map.at(edge["src"]);
12✔
595
        auto& target = nodes_map.at(edge["dst"]);
12✔
596

597
        assert(edge.contains("subset"));
12✔
598
        assert(edge["subset"].is_array());
12✔
599
        std::vector<symbolic::Expression> subset;
12✔
600
        for (const auto& subset_str : edge["subset"]) {
16✔
601
            assert(subset_str.is_string());
4✔
602
            SymEngine::Expression subset_expr(subset_str);
4✔
603
            subset.push_back(subset_expr);
4✔
604
        }
4✔
605
        auto& memlet = builder.add_memlet(
24✔
606
            parent, source, edge["src_conn"], target, edge["dst_conn"], subset, json_to_debug_info(edge["debug_info"])
12✔
607
        );
608
        memlet.element_id_ = edge["element_id"];
12✔
609
    }
12✔
610
}
10✔
611

612
void JSONSerializer::json_to_sequence(
12✔
613
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
614
) {
615
    assert(j.contains("type"));
12✔
616
    assert(j["type"].is_string());
12✔
617
    assert(j.contains("children"));
12✔
618
    assert(j["children"].is_array());
12✔
619
    assert(j.contains("transitions"));
12✔
620
    assert(j["transitions"].is_array());
12✔
621
    assert(j["transitions"].size() == j["children"].size());
12✔
622

623
    sequence.element_id_ = j["element_id"];
12✔
624
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
12✔
625

626
    std::string type = j["type"];
12✔
627
    if (type == "sequence") {
12✔
628
        for (size_t i = 0; i < j["children"].size(); i++) {
22✔
629
            auto& child = j["children"][i];
10✔
630
            auto& transition = j["transitions"][i];
10✔
631
            assert(child.contains("type"));
10✔
632
            assert(child["type"].is_string());
10✔
633

634
            assert(transition.contains("type"));
10✔
635
            assert(transition["type"].is_string());
10✔
636
            assert(transition.contains("assignments"));
10✔
637
            assert(transition["assignments"].is_array());
10✔
638
            control_flow::Assignments assignments;
10✔
639
            for (const auto& assignment : transition["assignments"]) {
11✔
640
                assert(assignment.contains("symbol"));
1✔
641
                assert(assignment["symbol"].is_string());
1✔
642
                assert(assignment.contains("expression"));
1✔
643
                assert(assignment["expression"].is_string());
1✔
644
                SymEngine::Expression expr(assignment["expression"]);
1✔
645
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
1✔
646
            }
1✔
647

648
            if (child["type"] == "block") {
10✔
649
                json_to_block_node(child, builder, sequence, assignments);
8✔
650
            } else if (child["type"] == "for") {
10✔
651
                json_to_for_node(child, builder, sequence, assignments);
×
652
            } else if (child["type"] == "if_else") {
2✔
653
                json_to_if_else_node(child, builder, sequence, assignments);
×
654
            } else if (child["type"] == "while") {
2✔
655
                json_to_while_node(child, builder, sequence, assignments);
×
656
            } else if (child["type"] == "break") {
2✔
657
                json_to_break_node(child, builder, sequence, assignments);
1✔
658
            } else if (child["type"] == "continue") {
2✔
659
                json_to_continue_node(child, builder, sequence, assignments);
1✔
660
            } else if (child["type"] == "return") {
1✔
661
                json_to_return_node(child, builder, sequence, assignments);
×
662
            } else if (child["type"] == "map") {
×
663
                json_to_map_node(child, builder, sequence, assignments);
×
664
            } else if (child["type"] == "sequence") {
×
665
                auto& subseq = builder.add_sequence(sequence, assignments, json_to_debug_info(child["debug_info"]));
×
666
                json_to_sequence(child, builder, subseq);
×
667
            } else {
×
668
                throw std::runtime_error("Unknown child type");
×
669
            }
670

671
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
10✔
672
            sequence.at(i).second.element_id_ = transition["element_id"];
10✔
673
        }
10✔
674
    } else {
12✔
675
        throw std::runtime_error("expected sequence type");
×
676
    }
677
}
12✔
678

679
void JSONSerializer::json_to_block_node(
9✔
680
    const nlohmann::json& j,
681
    builder::StructuredSDFGBuilder& builder,
682
    structured_control_flow::Sequence& parent,
683
    control_flow::Assignments& assignments
684
) {
685
    assert(j.contains("type"));
9✔
686
    assert(j["type"].is_string());
9✔
687
    assert(j.contains("dataflow"));
9✔
688
    assert(j["dataflow"].is_object());
9✔
689
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
9✔
690
    block.element_id_ = j["element_id"];
9✔
691
    assert(j["dataflow"].contains("type"));
9✔
692
    assert(j["dataflow"]["type"].is_string());
9✔
693
    std::string type = j["dataflow"]["type"];
9✔
694
    if (type == "dataflow") {
9✔
695
        json_to_dataflow(j["dataflow"], builder, block);
9✔
696
    } else {
9✔
697
        throw std::runtime_error("Unknown dataflow type");
×
698
    }
699
}
9✔
700

701
void JSONSerializer::json_to_for_node(
1✔
702
    const nlohmann::json& j,
703
    builder::StructuredSDFGBuilder& builder,
704
    structured_control_flow::Sequence& parent,
705
    control_flow::Assignments& assignments
706
) {
707
    assert(j.contains("type"));
1✔
708
    assert(j["type"].is_string());
1✔
709
    assert(j.contains("indvar"));
1✔
710
    assert(j["indvar"].is_string());
1✔
711
    assert(j.contains("init"));
1✔
712
    assert(j["init"].is_string());
1✔
713
    assert(j.contains("condition"));
1✔
714
    assert(j["condition"].is_string());
1✔
715
    assert(j.contains("update"));
1✔
716
    assert(j["update"].is_string());
1✔
717
    assert(j.contains("root"));
1✔
718
    assert(j["root"].is_object());
1✔
719

720
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
721
    SymEngine::Expression init(j["init"]);
1✔
722
    SymEngine::Expression condition_expr(j["condition"]);
1✔
723
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
724
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
725
    SymEngine::Expression update(j["update"]);
1✔
726
    auto& for_node =
1✔
727
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
1✔
728
    for_node.element_id_ = j["element_id"];
1✔
729

730
    assert(j["root"].contains("type"));
1✔
731
    assert(j["root"]["type"].is_string());
1✔
732
    assert(j["root"]["type"] == "sequence");
1✔
733
    json_to_sequence(j["root"], builder, for_node.root());
1✔
734
}
1✔
735

736
void JSONSerializer::json_to_if_else_node(
1✔
737
    const nlohmann::json& j,
738
    builder::StructuredSDFGBuilder& builder,
739
    structured_control_flow::Sequence& parent,
740
    control_flow::Assignments& assignments
741
) {
742
    assert(j.contains("type"));
1✔
743
    assert(j["type"].is_string());
1✔
744
    assert(j["type"] == "if_else");
1✔
745
    assert(j.contains("branches"));
1✔
746
    assert(j["branches"].is_array());
1✔
747
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
748
    if_else_node.element_id_ = j["element_id"];
1✔
749
    for (const auto& branch : j["branches"]) {
3✔
750
        assert(branch.contains("condition"));
2✔
751
        assert(branch["condition"].is_string());
2✔
752
        assert(branch.contains("root"));
2✔
753
        assert(branch["root"].is_object());
2✔
754
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
755
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
2✔
756
        symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()
2✔
757
        );
758
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
759
        assert(branch["root"].contains("type"));
2✔
760
        assert(branch["root"]["type"].is_string());
2✔
761
        std::string type = branch["root"]["type"];
2✔
762
        if (type == "sequence") {
2✔
763
            json_to_sequence(branch["root"], builder, branch_node);
2✔
764
        } else {
2✔
765
            throw std::runtime_error("Unknown child type");
×
766
        }
767
    }
2✔
768
}
1✔
769

770
void JSONSerializer::json_to_while_node(
3✔
771
    const nlohmann::json& j,
772
    builder::StructuredSDFGBuilder& builder,
773
    structured_control_flow::Sequence& parent,
774
    control_flow::Assignments& assignments
775
) {
776
    assert(j.contains("type"));
3✔
777
    assert(j["type"].is_string());
3✔
778
    assert(j["type"] == "while");
3✔
779
    assert(j.contains("root"));
3✔
780
    assert(j["root"].is_object());
3✔
781

782
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
783
    while_node.element_id_ = j["element_id"];
3✔
784

785
    assert(j["root"]["type"] == "sequence");
3✔
786
    json_to_sequence(j["root"], builder, while_node.root());
3✔
787
}
3✔
788

789
void JSONSerializer::json_to_break_node(
1✔
790
    const nlohmann::json& j,
791
    builder::StructuredSDFGBuilder& builder,
792
    structured_control_flow::Sequence& parent,
793
    control_flow::Assignments& assignments
794
) {
795
    assert(j.contains("type"));
1✔
796
    assert(j["type"].is_string());
1✔
797
    assert(j["type"] == "break");
1✔
798
    auto& node = builder.add_break(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
799
    node.element_id_ = j["element_id"];
1✔
800
}
1✔
801

802
void JSONSerializer::json_to_continue_node(
1✔
803
    const nlohmann::json& j,
804
    builder::StructuredSDFGBuilder& builder,
805
    structured_control_flow::Sequence& parent,
806
    control_flow::Assignments& assignments
807
) {
808
    assert(j.contains("type"));
1✔
809
    assert(j["type"].is_string());
1✔
810
    assert(j["type"] == "continue");
1✔
811
    auto& node = builder.add_continue(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
812
    node.element_id_ = j["element_id"];
1✔
813
}
1✔
814

815
void JSONSerializer::json_to_map_node(
1✔
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"));
1✔
822
    assert(j["type"].is_string());
1✔
823
    assert(j["type"] == "map");
1✔
824
    assert(j.contains("indvar"));
1✔
825
    assert(j["indvar"].is_string());
1✔
826
    assert(j.contains("init"));
1✔
827
    assert(j["init"].is_string());
1✔
828
    assert(j.contains("condition"));
1✔
829
    assert(j["condition"].is_string());
1✔
830
    assert(j.contains("update"));
1✔
831
    assert(j["update"].is_string());
1✔
832
    assert(j.contains("root"));
1✔
833
    assert(j["root"].is_object());
1✔
834
    assert(j.contains("schedule_type"));
1✔
835
    assert(j["schedule_type"].is_string());
1✔
836

837
    structured_control_flow::ScheduleType schedule_type =
838
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
839

840
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
841
    SymEngine::Expression init(j["init"]);
1✔
842
    SymEngine::Expression condition_expr(j["condition"]);
1✔
843
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
844
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
845
    SymEngine::Expression update(j["update"]);
1✔
846

847
    auto& map_node = builder.add_map(
2✔
848
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
849
    );
850
    map_node.element_id_ = j["element_id"];
1✔
851

852
    assert(j["root"].contains("type"));
1✔
853
    assert(j["root"]["type"].is_string());
1✔
854
    assert(j["root"]["type"] == "sequence");
1✔
855
    json_to_sequence(j["root"], builder, map_node.root());
1✔
856
}
1✔
857

858
void JSONSerializer::json_to_return_node(
1✔
859
    const nlohmann::json& j,
860
    builder::StructuredSDFGBuilder& builder,
861
    structured_control_flow::Sequence& parent,
862
    control_flow::Assignments& assignments
863
) {
864
    assert(j.contains("type"));
1✔
865
    assert(j["type"].is_string());
1✔
866
    assert(j["type"] == "return");
1✔
867

868
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
869
    node.element_id_ = j["element_id"];
1✔
870
}
1✔
871

872
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
31✔
873
    if (j.contains("type")) {
31✔
874
        if (j["type"] == "scalar") {
31✔
875
            // Deserialize scalar type
876
            assert(j.contains("primitive_type"));
25✔
877
            types::PrimitiveType primitive_type = j["primitive_type"];
25✔
878
            assert(j.contains("storage_type"));
25✔
879
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
25✔
880
            assert(j.contains("initializer"));
25✔
881
            std::string initializer = j["initializer"];
25✔
882
            assert(j.contains("alignment"));
25✔
883
            size_t alignment = j["alignment"];
25✔
884
            return std::make_unique<types::Scalar>(storage_type, alignment, initializer, primitive_type);
25✔
885
        } else if (j["type"] == "array") {
31✔
886
            // Deserialize array type
887
            assert(j.contains("element_type"));
2✔
888
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
889
            assert(j.contains("num_elements"));
2✔
890
            std::string num_elements_str = j["num_elements"];
2✔
891
            // Convert num_elements_str to symbolic::Expression
892
            SymEngine::Expression num_elements(num_elements_str);
2✔
893
            assert(j.contains("storage_type"));
2✔
894
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
895
            assert(j.contains("initializer"));
2✔
896
            std::string initializer = j["initializer"];
2✔
897
            assert(j.contains("alignment"));
2✔
898
            size_t alignment = j["alignment"];
2✔
899
            return std::make_unique<types::Array>(storage_type, alignment, initializer, *member_type, num_elements);
2✔
900
        } else if (j["type"] == "pointer") {
6✔
901
            // Deserialize pointer type
902
            assert(j.contains("pointee_type"));
2✔
903
            std::unique_ptr<types::IType> pointee_type = json_to_type(j["pointee_type"]);
2✔
904
            assert(j.contains("storage_type"));
2✔
905
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
2✔
906
            assert(j.contains("initializer"));
2✔
907
            std::string initializer = j["initializer"];
2✔
908
            assert(j.contains("alignment"));
2✔
909
            size_t alignment = j["alignment"];
2✔
910
            return std::make_unique<types::Pointer>(storage_type, alignment, initializer, *pointee_type);
2✔
911
        } else if (j["type"] == "structure") {
4✔
912
            // Deserialize structure type
913
            assert(j.contains("name"));
1✔
914
            std::string name = j["name"];
1✔
915
            assert(j.contains("storage_type"));
1✔
916
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
1✔
917
            assert(j.contains("initializer"));
1✔
918
            std::string initializer = j["initializer"];
1✔
919
            assert(j.contains("alignment"));
1✔
920
            size_t alignment = j["alignment"];
1✔
921
            return std::make_unique<types::Structure>(storage_type, alignment, initializer, name);
1✔
922
        } else if (j["type"] == "function") {
2✔
923
            // Deserialize function type
924
            assert(j.contains("return_type"));
1✔
925
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
926
            assert(j.contains("params"));
1✔
927
            std::vector<std::unique_ptr<types::IType>> params;
1✔
928
            for (const auto& param : j["params"]) {
3✔
929
                params.push_back(json_to_type(param));
2✔
930
            }
931
            assert(j.contains("is_var_arg"));
1✔
932
            bool is_var_arg = j["is_var_arg"];
1✔
933
            assert(j.contains("storage_type"));
1✔
934
            types::StorageType storage_type = storage_type_from_string(j["storage_type"].get<std::string>());
1✔
935
            assert(j.contains("initializer"));
1✔
936
            std::string initializer = j["initializer"];
1✔
937
            assert(j.contains("alignment"));
1✔
938
            size_t alignment = j["alignment"];
1✔
939
            auto function =
940
                std::make_unique<types::Function>(storage_type, alignment, initializer, *return_type, is_var_arg);
1✔
941
            for (const auto& param : params) {
3✔
942
                function->add_param(*param);
2✔
943
            }
944
            return function->clone();
1✔
945

946
        } else {
1✔
947
            throw std::runtime_error("Unknown type");
×
948
        }
949
    } else {
950
        throw std::runtime_error("Type not found");
×
951
    }
952
}
31✔
953

954
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
68✔
955
    assert(j.contains("has"));
68✔
956
    assert(j["has"].is_boolean());
68✔
957
    if (!j["has"]) {
68✔
958
        return DebugInfo();
68✔
959
    }
960
    assert(j.contains("filename"));
×
961
    assert(j["filename"].is_string());
×
962
    std::string filename = j["filename"];
×
963
    assert(j.contains("start_line"));
×
964
    assert(j["start_line"].is_number_integer());
×
965
    size_t start_line = j["start_line"];
×
966
    assert(j.contains("start_column"));
×
967
    assert(j["start_column"].is_number_integer());
×
968
    size_t start_column = j["start_column"];
×
969
    assert(j.contains("end_line"));
×
970
    assert(j["end_line"].is_number_integer());
×
971
    size_t end_line = j["end_line"];
×
972
    assert(j.contains("end_column"));
×
973
    assert(j["end_column"].is_number_integer());
×
974
    size_t end_column = j["end_column"];
×
975
    return DebugInfo(filename, start_line, start_column, end_line, end_column);
×
976
}
68✔
977

978
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
33✔
979
    JSONSymbolicPrinter printer;
33✔
980
    return printer.apply(expr);
33✔
981
};
33✔
982

983
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
984
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
985
    str_ = parenthesize(str_);
×
986
};
×
987

988
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
989
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
990
    str_ = parenthesize(str_);
×
991
};
×
992

993
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
994
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
995
    str_ = parenthesize(str_);
×
996
};
×
997

998
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
999
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1000
    str_ = parenthesize(str_);
4✔
1001
};
4✔
1002

1003
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1004
    std::ostringstream s;
×
1005
    auto container = x.get_args();
×
1006
    if (container.size() == 1) {
×
1007
        s << apply(*container.begin());
×
1008
    } else {
×
1009
        s << "min(";
×
1010
        s << apply(*container.begin());
×
1011

1012
        // Recursively apply __daisy_min to the arguments
1013
        SymEngine::vec_basic subargs;
×
1014
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1015
            subargs.push_back(*it);
×
1016
        }
×
1017
        auto submin = SymEngine::min(subargs);
×
1018
        s << ", " << apply(submin);
×
1019

1020
        s << ")";
×
1021
    }
×
1022

1023
    str_ = s.str();
×
1024
};
×
1025

1026
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1027
    std::ostringstream s;
×
1028
    auto container = x.get_args();
×
1029
    if (container.size() == 1) {
×
1030
        s << apply(*container.begin());
×
1031
    } else {
×
1032
        s << "max(";
×
1033
        s << apply(*container.begin());
×
1034

1035
        // Recursively apply __daisy_max to the arguments
1036
        SymEngine::vec_basic subargs;
×
1037
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1038
            subargs.push_back(*it);
×
1039
        }
×
1040
        auto submax = SymEngine::max(subargs);
×
1041
        s << ", " << apply(submax);
×
1042

1043
        s << ")";
×
1044
    }
×
1045

1046
    str_ = s.str();
×
1047
};
×
1048

1049
void LibraryNodeSerializerRegistry::
1050
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
6✔
1051
    std::lock_guard<std::mutex> lock(mutex_);
6✔
1052
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
6✔
NEW
1053
        throw std::runtime_error(
×
NEW
1054
            "Library node serializer already registered for library node code: " + std::string(library_node_code)
×
1055
        );
1056
    }
1057
    factory_map_[library_node_code] = std::move(fn);
6✔
1058
}
6✔
1059

1060
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
1✔
1061
    auto it = factory_map_.find(library_node_code);
1✔
1062
    if (it != factory_map_.end()) {
1✔
1063
        return it->second;
1✔
1064
    }
NEW
1065
    return nullptr;
×
1066
}
1✔
1067

NEW
1068
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1069

1070
void register_default_serializers() {
2✔
1071
    LibraryNodeSerializerRegistry::instance()
2✔
1072
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
2✔
NEW
1073
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1074
        });
1075
    LibraryNodeSerializerRegistry::instance()
2✔
1076
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
3✔
1077
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1078
        });
1079

1080
    /* Math */
1081
    LibraryNodeSerializerRegistry::instance()
2✔
1082
        .register_library_node_serializer(math::ml::LibraryNodeType_ReLU.value(), []() {
2✔
NEW
1083
            return std::make_unique<math::ml::ReLUNodeSerializer>();
×
1084
        });
1085
    // Add more serializers as needed
1086
}
2✔
1087

1088
} // namespace serializer
1089
} // 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