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

daisytuner / docc / 30281151428

27 Jul 2026 03:38PM UTC coverage: 64.153% (+0.01%) from 64.141%
30281151428

Pull #891

github

web-flow
Merge 8502a9cdc into 080bf880c
Pull Request #891: Add Support for Elementwise Logical_Not Ops

28 of 39 new or added lines in 4 files covered. (71.79%)

42881 of 66842 relevant lines covered (64.15%)

732.44 hits per line

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

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

3
#include <cassert>
4
#include <memory>
5
#include <sdfg/data_flow/library_nodes/load_const_node.h>
6
#include <utility>
7
#include <vector>
8

9
#include "sdfg/data_flow/library_nodes/barrier_local_node.h"
10
#include "sdfg/data_flow/library_nodes/call_node.h"
11
#include "sdfg/data_flow/library_nodes/invoke_node.h"
12
#include "sdfg/data_flow/library_nodes/math/math.h"
13
#include "sdfg/data_flow/library_nodes/math/tensor/elementwise_ops/cmath_node.h"
14
#include "sdfg/data_flow/library_nodes/math/tensor/elementwise_ops/logical_not_node.h"
15
#include "sdfg/data_flow/library_nodes/math/tensor/elementwise_ops/tasklet_node.h"
16
#include "sdfg/data_flow/library_nodes/metadata_node.h"
17
#include "sdfg/data_flow/library_nodes/stdlib/stdlib.h"
18

19
#include "sdfg/analysis/users.h"
20
#include "sdfg/builder/structured_sdfg_builder.h"
21
#include "sdfg/data_flow/library_node.h"
22
#include "sdfg/data_flow/library_nodes/math/tensor/batchnorm_node.h"
23
#include "sdfg/element.h"
24
#include "sdfg/structured_control_flow/block.h"
25
#include "sdfg/structured_control_flow/for.h"
26
#include "sdfg/structured_control_flow/if_else.h"
27
#include "sdfg/structured_control_flow/map.h"
28
#include "sdfg/structured_control_flow/reduce.h"
29
#include "sdfg/structured_control_flow/return.h"
30
#include "sdfg/structured_control_flow/sequence.h"
31
#include "sdfg/structured_control_flow/while.h"
32
#include "sdfg/structured_sdfg.h"
33
#include "sdfg/symbolic/symbolic.h"
34
#include "sdfg/types/function.h"
35
#include "sdfg/types/scalar.h"
36
#include "sdfg/types/type.h"
37
#include "symengine/expression.h"
38
#include "symengine/logic.h"
39
#include "symengine/symengine_rcp.h"
40

41
namespace sdfg {
42
namespace serializer {
43

44
FunctionType function_type_from_string(const std::string& str) {
30✔
45
    if (str == FunctionType_CPU.value()) {
30✔
46
        return FunctionType_CPU;
30✔
47
    } else if (str == FunctionType_NV_GLOBAL.value()) {
30✔
48
        return FunctionType_NV_GLOBAL;
×
49
    }
×
50

51
    return FunctionType(str);
×
52
}
30✔
53

54
/*
55
 * * JSONSerializer class
56
 * * Serialization logic
57
 */
58

59
nlohmann::json JSONSerializer::serialize(
60
    const sdfg::StructuredSDFG& sdfg,
61
    analysis::AnalysisManager* analysis_manager,
62
    structured_control_flow::Sequence* root
63
) {
28✔
64
    nlohmann::json j;
28✔
65

66
    j["name"] = sdfg.name();
28✔
67
    j["element_counter"] = sdfg.element_counter();
28✔
68
    j["type"] = std::string(sdfg.type().value());
28✔
69

70
    nlohmann::json return_type_json;
28✔
71
    type_to_json(return_type_json, sdfg.return_type());
28✔
72
    j["return_type"] = return_type_json;
28✔
73

74
    j["structures"] = nlohmann::json::array();
28✔
75
    for (const auto& structure_name : sdfg.structures()) {
28✔
76
        const auto& structure = sdfg.structure(structure_name);
1✔
77
        nlohmann::json structure_json;
1✔
78
        structure_definition_to_json(structure_json, structure);
1✔
79
        j["structures"].push_back(structure_json);
1✔
80
    }
1✔
81

82
    j["containers"] = nlohmann::json::object();
28✔
83
    for (const auto& container : sdfg.containers()) {
151✔
84
        nlohmann::json desc;
151✔
85
        type_to_json(desc, sdfg.type(container));
151✔
86
        j["containers"][container] = desc;
151✔
87
    }
151✔
88

89
    j["arguments"] = nlohmann::json::array();
28✔
90
    for (const auto& argument : sdfg.arguments()) {
36✔
91
        j["arguments"].push_back(argument);
36✔
92
    }
36✔
93

94
    j["externals"] = nlohmann::json::array();
28✔
95
    for (const auto& external : sdfg.externals()) {
28✔
96
        nlohmann::json external_json;
8✔
97
        external_json["name"] = external;
8✔
98
        external_json["linkage_type"] = sdfg.linkage_type(external);
8✔
99
        j["externals"].push_back(external_json);
8✔
100
    }
8✔
101

102
    j["metadata"] = nlohmann::json::object();
28✔
103
    for (const auto& entry : sdfg.metadata()) {
28✔
104
        j["metadata"][entry.first] = entry.second;
1✔
105
    }
1✔
106

107
    // Walk the SDFG
108
    if (this->recurse_) {
28✔
109
        nlohmann::json root_json;
28✔
110
        sequence_to_json(root_json, sdfg.root());
28✔
111
        j["root"] = root_json;
28✔
112
    }
28✔
113

114
    return j;
28✔
115
}
28✔
116

117
void JSONSerializer::serialize_node(nlohmann::json& j, const structured_control_flow::ControlFlowNode& node) {
222✔
118
    if (auto block = dynamic_cast<const structured_control_flow::Block*>(&node)) {
222✔
119
        block_to_json(j, *block);
50✔
120
    } else if (auto assignment_block = dynamic_cast<const structured_control_flow::AssignmentBlock*>(&node)) {
172✔
121
        assignment_block_to_json(j, *assignment_block);
3✔
122
    } else if (auto sequence_node = dynamic_cast<const structured_control_flow::Sequence*>(&node)) {
169✔
123
        sequence_to_json(j, *sequence_node);
×
124
    } else if (auto condition_node = dynamic_cast<const structured_control_flow::IfElse*>(&node)) {
169✔
125
        if_else_to_json(j, *condition_node);
×
126
    } else if (auto while_node = dynamic_cast<const structured_control_flow::While*>(&node)) {
169✔
127
        while_node_to_json(j, *while_node);
×
128
    } else if (auto return_node = dynamic_cast<const structured_control_flow::Return*>(&node)) {
169✔
129
        return_node_to_json(j, *return_node);
×
130
    } else if (auto break_node = dynamic_cast<const structured_control_flow::Break*>(&node)) {
169✔
131
        break_node_to_json(j, *break_node);
2✔
132
    } else if (auto continue_node = dynamic_cast<const structured_control_flow::Continue*>(&node)) {
167✔
133
        continue_node_to_json(j, *continue_node);
2✔
134
    } else if (auto loop_node = dynamic_cast<const structured_control_flow::StructuredLoop*>(&node)) {
165✔
135
        structured_loop_to_json(j, *loop_node);
165✔
136
    } else {
165✔
137
        throw std::runtime_error("Unknown child type");
×
138
    }
×
139
}
222✔
140

141
void JSONSerializer::dataflow_to_json(nlohmann::json& j, const data_flow::DataFlowGraph& dataflow) {
55✔
142
    j["type"] = "dataflow";
55✔
143
    j["nodes"] = nlohmann::json::array();
55✔
144
    j["edges"] = nlohmann::json::array();
55✔
145

146
    for (auto& node : dataflow.nodes()) {
218✔
147
        nlohmann::json node_json;
218✔
148
        node_json["element_id"] = node.element_id();
218✔
149

150
        node_json["debug_info"] = nlohmann::json::object();
218✔
151
        debug_info_to_json(node_json["debug_info"], node.debug_info());
218✔
152

153
        if (auto tasklet = dynamic_cast<const data_flow::Tasklet*>(&node)) {
218✔
154
            node_json["type"] = "tasklet";
62✔
155
            node_json["code"] = tasklet->code();
62✔
156
            node_json["inputs"] = nlohmann::json::array();
62✔
157
            for (auto& input : tasklet->inputs()) {
104✔
158
                node_json["inputs"].push_back(input);
104✔
159
            }
104✔
160
            node_json["output"] = tasklet->output();
62✔
161
        } else if (auto lib_node = dynamic_cast<const data_flow::LibraryNode*>(&node)) {
156✔
162
            node_json["type"] = "library_node";
10✔
163
            node_json["implementation_type"] = std::string(lib_node->implementation_type().value());
10✔
164
            auto serializer_fn =
10✔
165
                LibraryNodeSerializerRegistry::instance().get_library_node_serializer(lib_node->code().value());
10✔
166
            if (serializer_fn == nullptr) {
10✔
167
                throw std::runtime_error("Unknown library node code: " + std::string(lib_node->code().value()));
×
168
            }
×
169
            auto serializer = serializer_fn();
10✔
170
            auto lib_node_json = serializer->serialize(*lib_node);
10✔
171
            node_json.merge_patch(lib_node_json);
10✔
172
        } else if (auto code_node = dynamic_cast<const data_flow::ConstantNode*>(&node)) {
146✔
173
            node_json["type"] = "constant_node";
×
174
            node_json["data"] = code_node->data();
×
175

176
            nlohmann::json type_json;
×
177
            type_to_json(type_json, code_node->type());
×
178
            node_json["data_type"] = type_json;
×
179
        } else if (auto code_node = dynamic_cast<const data_flow::AccessNode*>(&node)) {
146✔
180
            node_json["type"] = "access_node";
146✔
181
            node_json["data"] = code_node->data();
146✔
182
        } else {
146✔
183
            throw std::runtime_error("Unknown node type");
×
184
        }
×
185

186
        j["nodes"].push_back(node_json);
218✔
187
    }
218✔
188

189
    for (auto& edge : dataflow.edges()) {
191✔
190
        nlohmann::json edge_json;
191✔
191
        edge_json["element_id"] = edge.element_id();
191✔
192

193
        edge_json["debug_info"] = nlohmann::json::object();
191✔
194
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
191✔
195

196
        edge_json["src"] = edge.src().element_id();
191✔
197
        edge_json["dst"] = edge.dst().element_id();
191✔
198

199
        edge_json["src_conn"] = edge.src_conn();
191✔
200
        edge_json["dst_conn"] = edge.dst_conn();
191✔
201

202
        edge_json["subset"] = nlohmann::json::array();
191✔
203
        for (auto& subset : edge.subset()) {
191✔
204
            edge_json["subset"].push_back(expression(subset));
72✔
205
        }
72✔
206

207
        nlohmann::json base_type_json;
191✔
208
        type_to_json(base_type_json, edge.base_type());
191✔
209
        edge_json["base_type"] = base_type_json;
191✔
210

211
        j["edges"].push_back(edge_json);
191✔
212
    }
191✔
213
}
55✔
214

215
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
52✔
216
    j["type"] = "block";
52✔
217
    j["element_id"] = block.element_id();
52✔
218

219
    j["debug_info"] = nlohmann::json::object();
52✔
220
    debug_info_to_json(j["debug_info"], block.debug_info());
52✔
221

222
    if (this->recurse_) {
52✔
223
        nlohmann::json dataflow_json;
52✔
224
        dataflow_to_json(dataflow_json, block.dataflow());
52✔
225
        j["dataflow"] = dataflow_json;
52✔
226
    }
52✔
227
}
52✔
228

229
void JSONSerializer::assignment_block_to_json(nlohmann::json& j, const structured_control_flow::AssignmentBlock& block) {
3✔
230
    j["type"] = "assignment_block";
3✔
231
    j["element_id"] = block.element_id();
3✔
232
    j["debug_info"] = nlohmann::json::object();
3✔
233
    debug_info_to_json(j["debug_info"], block.debug_info());
3✔
234

235
    if (this->recurse_) {
3✔
236
        auto arr = nlohmann::json::array();
3✔
237
        for (const auto& assignment : block.assignments()) {
3✔
238
            nlohmann::json assignment_json;
3✔
239
            assignment_json["symbol"] = expression(assignment.first);
3✔
240
            assignment_json["expression"] = expression(assignment.second);
3✔
241
            arr.push_back(assignment_json);
3✔
242
        }
3✔
243
        j["assignments"] = arr;
3✔
244
    }
3✔
245
}
3✔
246

247
void JSONSerializer::structured_loop_to_json(nlohmann::json& j, const structured_control_flow::StructuredLoop& for_node) {
171✔
248
    j["type"] = "for";
171✔
249
    // Backward compatibility (now encapsulated in sub_type)
250
    if (dynamic_cast<const structured_control_flow::Map*>(&for_node)) {
171✔
251
        j["type"] = "map";
64✔
252
    }
64✔
253

254
    j["element_id"] = for_node.element_id();
171✔
255
    j["debug_info"] = nlohmann::json::object();
171✔
256
    debug_info_to_json(j["debug_info"], for_node.debug_info());
171✔
257

258
    j["indvar"] = expression(for_node.indvar());
171✔
259
    j["init"] = expression(for_node.init());
171✔
260
    j["condition"] = expression(for_node.condition());
171✔
261
    j["update"] = expression(for_node.update());
171✔
262

263
    j["schedule_type"] = nlohmann::json::object();
171✔
264
    schedule_type_to_json(j["schedule_type"], for_node.schedule_type());
171✔
265

266
    j["sub_type"] = "for";
171✔
267
    if (dynamic_cast<const structured_control_flow::Map*>(&for_node)) {
171✔
268
        j["sub_type"] = "map";
64✔
269
    } else if (dynamic_cast<const structured_control_flow::Reduce*>(&for_node)) {
107✔
270
        j["sub_type"] = "reduce";
2✔
271
        j["reductions"] = nlohmann::json::array();
2✔
272
        const auto& reduce_node = dynamic_cast<const structured_control_flow::Reduce&>(for_node);
2✔
273
        for (const auto& reduction : reduce_node.reductions()) {
2✔
274
            nlohmann::json reduction_json;
2✔
275
            reduction_json["op"] = structured_control_flow::reduction_operation_to_string(reduction.operation);
2✔
276
            reduction_json["container"] = reduction.container;
2✔
277
            j["reductions"].push_back(reduction_json);
2✔
278
        }
2✔
279
    }
2✔
280

281
    if (this->recurse_) {
171✔
282
        nlohmann::json body_json;
31✔
283
        sequence_to_json(body_json, for_node.root());
31✔
284
        j["root"] = body_json;
31✔
285
    }
31✔
286
}
171✔
287

288
void JSONSerializer::if_else_to_json(nlohmann::json& j, const structured_control_flow::IfElse& if_else_node) {
2✔
289
    j["type"] = "if_else";
2✔
290
    j["element_id"] = if_else_node.element_id();
2✔
291

292
    j["debug_info"] = nlohmann::json::object();
2✔
293
    debug_info_to_json(j["debug_info"], if_else_node.debug_info());
2✔
294

295
    j["branches"] = nlohmann::json::array();
2✔
296
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
297
        nlohmann::json branch_json;
4✔
298
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
299
        if (this->recurse_) {
4✔
300
            nlohmann::json body_json;
4✔
301
            sequence_to_json(body_json, if_else_node.at(i).first);
4✔
302
            branch_json["root"] = body_json;
4✔
303
        }
4✔
304
        j["branches"].push_back(branch_json);
4✔
305
    }
4✔
306
}
2✔
307

308
void JSONSerializer::while_node_to_json(nlohmann::json& j, const structured_control_flow::While& while_node) {
5✔
309
    j["type"] = "while";
5✔
310
    j["element_id"] = while_node.element_id();
5✔
311

312
    j["debug_info"] = nlohmann::json::object();
5✔
313
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
314

315
    if (this->recurse_) {
5✔
316
        nlohmann::json body_json;
5✔
317
        sequence_to_json(body_json, while_node.root());
5✔
318
        j["root"] = body_json;
5✔
319
    }
5✔
320
}
5✔
321

322
void JSONSerializer::break_node_to_json(nlohmann::json& j, const structured_control_flow::Break& break_node) {
2✔
323
    j["type"] = "break";
2✔
324
    j["element_id"] = break_node.element_id();
2✔
325

326
    j["debug_info"] = nlohmann::json::object();
2✔
327
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
328
}
2✔
329

330
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
331
    j["type"] = "continue";
2✔
332
    j["element_id"] = continue_node.element_id();
2✔
333

334
    j["debug_info"] = nlohmann::json::object();
2✔
335
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
336
}
2✔
337

338
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
339
    j["type"] = "return";
2✔
340
    j["element_id"] = return_node.element_id();
2✔
341
    j["data"] = return_node.data();
2✔
342

343
    if (return_node.is_constant()) {
2✔
344
        nlohmann::json type_json;
×
345
        type_to_json(type_json, return_node.type());
×
346
        j["data_type"] = type_json;
×
347
    }
×
348

349
    j["debug_info"] = nlohmann::json::object();
2✔
350
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
351
}
2✔
352

353
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
73✔
354
    j["type"] = "sequence";
73✔
355
    j["element_id"] = sequence.element_id();
73✔
356

357
    j["debug_info"] = nlohmann::json::object();
73✔
358
    debug_info_to_json(j["debug_info"], sequence.debug_info());
73✔
359

360
    if (!this->recurse_) {
73✔
361
        return;
×
362
    }
×
363

364
    j["children"] = nlohmann::json::array();
73✔
365
    for (size_t i = 0; i < sequence.size(); i++) {
155✔
366
        nlohmann::json child_json;
82✔
367
        auto& child = sequence.at(i);
82✔
368

369
        this->serialize_node(child_json, child);
82✔
370
        j["children"].push_back(child_json);
82✔
371
    }
82✔
372
}
73✔
373

374
void JSONSerializer::type_to_json(nlohmann::json& j, const types::IType& type) {
594✔
375
    if (auto scalar_type = dynamic_cast<const types::Scalar*>(&type)) {
594✔
376
        j["type"] = "scalar";
429✔
377
        j["primitive_type"] = scalar_type->primitive_type();
429✔
378
        j["storage_type"] = nlohmann::json::object();
429✔
379
        storage_type_to_json(j["storage_type"], scalar_type->storage_type());
429✔
380
        j["initializer"] = scalar_type->initializer();
429✔
381
        j["alignment"] = scalar_type->alignment();
429✔
382
    } else if (auto array_type = dynamic_cast<const types::Array*>(&type)) {
429✔
383
        j["type"] = "array";
79✔
384
        nlohmann::json element_type_json;
79✔
385
        type_to_json(element_type_json, array_type->element_type());
79✔
386
        j["element_type"] = element_type_json;
79✔
387
        j["num_elements"] = expression(array_type->num_elements());
79✔
388
        j["storage_type"] = nlohmann::json::object();
79✔
389
        storage_type_to_json(j["storage_type"], array_type->storage_type());
79✔
390
        j["initializer"] = array_type->initializer();
79✔
391
        j["alignment"] = array_type->alignment();
79✔
392
    } else if (auto pointer_type = dynamic_cast<const types::Pointer*>(&type)) {
86✔
393
        j["type"] = "pointer";
62✔
394
        if (pointer_type->has_pointee_type()) {
62✔
395
            nlohmann::json pointee_type_json;
61✔
396
            type_to_json(pointee_type_json, pointer_type->pointee_type());
61✔
397
            j["pointee_type"] = pointee_type_json;
61✔
398
        }
61✔
399
        j["storage_type"] = nlohmann::json::object();
62✔
400
        storage_type_to_json(j["storage_type"], pointer_type->storage_type());
62✔
401
        j["initializer"] = pointer_type->initializer();
62✔
402
        j["alignment"] = pointer_type->alignment();
62✔
403
    } else if (auto structure_type = dynamic_cast<const types::Structure*>(&type)) {
62✔
404
        j["type"] = "structure";
5✔
405
        j["name"] = structure_type->name();
5✔
406
        j["storage_type"] = nlohmann::json::object();
5✔
407
        storage_type_to_json(j["storage_type"], structure_type->storage_type());
5✔
408
        j["initializer"] = structure_type->initializer();
5✔
409
        j["alignment"] = structure_type->alignment();
5✔
410
    } else if (auto function_type = dynamic_cast<const types::Function*>(&type)) {
19✔
411
        j["type"] = "function";
5✔
412
        nlohmann::json return_type_json;
5✔
413
        type_to_json(return_type_json, function_type->return_type());
5✔
414
        j["return_type"] = return_type_json;
5✔
415
        j["params"] = nlohmann::json::array();
5✔
416
        for (size_t i = 0; i < function_type->num_params(); i++) {
16✔
417
            nlohmann::json param_json;
11✔
418
            type_to_json(param_json, function_type->param_type(symbolic::integer(i)));
11✔
419
            j["params"].push_back(param_json);
11✔
420
        }
11✔
421
        j["is_var_arg"] = function_type->is_var_arg();
5✔
422
        j["storage_type"] = nlohmann::json::object();
5✔
423
        storage_type_to_json(j["storage_type"], function_type->storage_type());
5✔
424
        j["initializer"] = function_type->initializer();
5✔
425
        j["alignment"] = function_type->alignment();
5✔
426
    } else if (auto reference_type = dynamic_cast<const sdfg::codegen::Reference*>(&type)) {
14✔
427
        j["type"] = "reference";
9✔
428
        nlohmann::json reference_type_json;
9✔
429
        type_to_json(reference_type_json, reference_type->reference_type());
9✔
430
        j["reference_type"] = reference_type_json;
9✔
431
    } else if (auto tensor_type = dynamic_cast<const types::Tensor*>(&type)) {
9✔
432
        j["type"] = "tensor";
5✔
433
        nlohmann::json element_type_json;
5✔
434
        type_to_json(element_type_json, tensor_type->element_type());
5✔
435
        j["element_type"] = element_type_json;
5✔
436
        j["shape"] = nlohmann::json::array();
5✔
437
        for (const auto& dim : tensor_type->shape()) {
13✔
438
            j["shape"].push_back(expression(dim));
13✔
439
        }
13✔
440
        j["strides"] = nlohmann::json::array();
5✔
441
        for (const auto& stride : tensor_type->strides()) {
13✔
442
            j["strides"].push_back(expression(stride));
13✔
443
        }
13✔
444
        j["offset"] = expression(tensor_type->offset());
5✔
445
        j["storage_type"] = nlohmann::json::object();
5✔
446
        storage_type_to_json(j["storage_type"], tensor_type->storage_type());
5✔
447
        j["initializer"] = tensor_type->initializer();
5✔
448
        j["alignment"] = tensor_type->alignment();
5✔
449
    } else {
5✔
450
        throw std::runtime_error("Unknown type");
×
451
    }
×
452
}
594✔
453

454
void JSONSerializer::structure_definition_to_json(nlohmann::json& j, const types::StructureDefinition& definition) {
2✔
455
    j["name"] = definition.name();
2✔
456
    j["members"] = nlohmann::json::array();
2✔
457
    for (size_t i = 0; i < definition.num_members(); i++) {
4✔
458
        nlohmann::json member_json;
2✔
459
        type_to_json(member_json, definition.member_type(symbolic::integer(i)));
2✔
460
        j["members"].push_back(member_json);
2✔
461
    }
2✔
462
    j["is_packed"] = definition.is_packed();
2✔
463
}
2✔
464

465
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
721✔
466
    j["has"] = debug_info.has();
721✔
467
    j["filename"] = debug_info.filename();
721✔
468
    j["function"] = debug_info.function();
721✔
469
    j["start_line"] = debug_info.start_line();
721✔
470
    j["start_column"] = debug_info.start_column();
721✔
471
    j["end_line"] = debug_info.end_line();
721✔
472
    j["end_column"] = debug_info.end_column();
721✔
473
}
721✔
474

475

476
void JSONSerializer::schedule_type_to_json(nlohmann::json& j, const ScheduleType& schedule_type) {
173✔
477
    j["value"] = schedule_type.value();
173✔
478
    j["category"] = static_cast<int>(schedule_type.category());
173✔
479
    j["properties"] = nlohmann::json::object();
173✔
480
    for (const auto& prop : schedule_type.properties()) {
173✔
481
        j["properties"][prop.first] = prop.second;
4✔
482
    }
4✔
483
}
173✔
484

485
void JSONSerializer::storage_type_to_json(nlohmann::json& j, const types::StorageType& storage_type) {
598✔
486
    j["value"] = storage_type.value();
598✔
487
    j["allocation"] = storage_type.allocation();
598✔
488
    j["deallocation"] = storage_type.deallocation();
598✔
489
    if (!storage_type.allocation_size().is_null()) {
598✔
490
        j["allocation_size"] = expression(storage_type.allocation_size());
×
491
    }
×
492
    const symbolic::Expression& arg1 = storage_type.arg1();
598✔
493
    if (!arg1.is_null()) {
598✔
494
        auto args = nlohmann::json::array();
×
495
        args.push_back(expression(arg1));
×
496
        j["args"] = args;
×
497
    }
×
498
}
598✔
499

500

501
/*
502
 * * Deserialization logic
503
 */
504

505
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
30✔
506
    assert(j.contains("name"));
30✔
507
    assert(j["name"].is_string());
30✔
508
    assert(j.contains("type"));
30✔
509
    assert(j["type"].is_string());
30✔
510
    assert(j.contains("element_counter"));
30✔
511
    assert(j["element_counter"].is_number_integer());
30✔
512

513
    std::unique_ptr<types::IType> return_type;
30✔
514
    if (j.contains("return_type")) {
30✔
515
        return_type = json_to_type(j["return_type"]);
30✔
516
    } else {
30✔
517
        return_type = std::make_unique<types::Scalar>(types::PrimitiveType::Void);
×
518
    }
×
519

520
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
30✔
521
    builder::StructuredSDFGBuilder builder(j["name"], function_type, *return_type);
30✔
522

523
    size_t element_counter = j["element_counter"];
30✔
524
    builder.set_element_counter(element_counter);
30✔
525

526
    // deserialize structures
527
    assert(j.contains("structures"));
30✔
528
    assert(j["structures"].is_array());
30✔
529
    for (const auto& structure : j["structures"]) {
30✔
530
        assert(structure.contains("name"));
1✔
531
        assert(structure["name"].is_string());
1✔
532
        json_to_structure_definition(structure, builder);
1✔
533
    }
1✔
534

535
    nlohmann::json& containers = j["containers"];
30✔
536

537
    // deserialize externals
538
    for (const auto& external : j["externals"]) {
30✔
539
        assert(external.contains("name"));
9✔
540
        assert(external["name"].is_string());
9✔
541
        assert(external.contains("linkage_type"));
9✔
542
        assert(external["linkage_type"].is_number_integer());
9✔
543
        auto& type_desc = containers.at(external["name"].get<std::string>());
9✔
544
        auto type = json_to_type(type_desc);
9✔
545
        builder.add_external(external["name"], *type, LinkageType(external["linkage_type"]));
9✔
546
    }
9✔
547

548
    // deserialize arguments
549
    for (const auto& name : j["arguments"]) {
41✔
550
        auto& type_desc = containers.at(name.get<std::string>());
41✔
551
        auto type = json_to_type(type_desc);
41✔
552
        builder.add_container(name, *type, true, false);
41✔
553
    }
41✔
554

555
    // deserialize transients
556
    for (const auto& entry : containers.items()) {
161✔
557
        if (builder.subject().is_argument(entry.key())) {
161✔
558
            continue;
41✔
559
        }
41✔
560
        if (builder.subject().is_external(entry.key())) {
120✔
561
            continue;
9✔
562
        }
9✔
563
        auto type = json_to_type(entry.value());
111✔
564
        builder.add_container(entry.key(), *type, false, false);
111✔
565
    }
111✔
566

567
    // deserialize root node
568
    assert(j.contains("root"));
30✔
569
    auto& root = builder.subject().root();
30✔
570
    json_to_sequence(j["root"], builder, root);
30✔
571

572
    // deserialize metadata
573
    assert(j.contains("metadata"));
30✔
574
    assert(j["metadata"].is_object());
30✔
575
    for (const auto& entry : j["metadata"].items()) {
30✔
576
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
577
    }
1✔
578

579
    builder.set_element_counter(element_counter);
30✔
580

581
    return builder.move();
30✔
582
}
30✔
583

584
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
2✔
585
    assert(j.contains("name"));
2✔
586
    assert(j["name"].is_string());
2✔
587
    assert(j.contains("members"));
2✔
588
    assert(j["members"].is_array());
2✔
589
    assert(j.contains("is_packed"));
2✔
590
    assert(j["is_packed"].is_boolean());
2✔
591
    auto is_packed = j["is_packed"];
2✔
592
    auto& definition = builder.add_structure(j["name"], is_packed);
2✔
593
    for (const auto& member : j["members"]) {
2✔
594
        nlohmann::json member_json;
2✔
595
        auto member_type = json_to_type(member);
2✔
596
        definition.add_member(*member_type);
2✔
597
    }
2✔
598
}
2✔
599

600
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
×
601
    std::vector<std::pair<std::string, types::Scalar>> arguments;
×
602
    for (const auto& argument : j) {
×
603
        assert(argument.contains("name"));
×
604
        assert(argument["name"].is_string());
×
605
        assert(argument.contains("type"));
×
606
        assert(argument["type"].is_object());
×
607
        std::string name = argument["name"];
×
608
        auto type = json_to_type(argument["type"]);
×
609
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
×
610
    }
×
611
    return arguments;
×
612
}
×
613

614
void JSONSerializer::json_to_dataflow(
615
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
616
) {
47✔
617
    std::map<size_t, data_flow::DataFlowNode&> nodes_map;
47✔
618

619
    assert(j.contains("nodes"));
47✔
620
    assert(j["nodes"].is_array());
47✔
621
    for (const auto& node : j["nodes"]) {
214✔
622
        assert(node.contains("type"));
214✔
623
        assert(node["type"].is_string());
214✔
624
        assert(node.contains("element_id"));
214✔
625
        assert(node["element_id"].is_number_integer());
214✔
626
        std::string type = node["type"];
214✔
627
        if (type == "tasklet") {
214✔
628
            assert(node.contains("code"));
61✔
629
            assert(node["code"].is_number_integer());
61✔
630
            assert(node.contains("inputs"));
61✔
631
            assert(node["inputs"].is_array());
61✔
632
            assert(node.contains("output"));
61✔
633
            assert(node["output"].is_string());
61✔
634
            auto inputs = node["inputs"].get<std::vector<std::string>>();
61✔
635

636
            auto& tasklet =
61✔
637
                builder
61✔
638
                    .add_tasklet(parent, node["code"], node["output"], inputs, json_to_debug_info(node["debug_info"]));
61✔
639
            tasklet.element_id_ = node["element_id"];
61✔
640
            nodes_map.insert({node["element_id"], tasklet});
61✔
641
        } else if (type == "library_node") {
153✔
642
            assert(node.contains("code"));
10✔
643
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
10✔
644

645
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
10✔
646
            if (serializer_fn == nullptr) {
10✔
647
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
648
            }
×
649
            auto serializer = serializer_fn();
10✔
650
            auto& lib_node = serializer->deserialize(node, builder, parent);
10✔
651
            lib_node.implementation_type() =
10✔
652
                data_flow::ImplementationType(node["implementation_type"].get<std::string>());
10✔
653
            lib_node.element_id_ = node["element_id"];
10✔
654
            nodes_map.insert({node["element_id"], lib_node});
10✔
655
        } else if (type == "access_node") {
143✔
656
            assert(node.contains("data"));
143✔
657
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
143✔
658
            access_node.element_id_ = node["element_id"];
143✔
659
            nodes_map.insert({node["element_id"], access_node});
143✔
660
        } else if (type == "constant_node") {
143✔
661
            assert(node.contains("data"));
×
662
            assert(node.contains("data_type"));
×
663

664
            auto type = json_to_type(node["data_type"]);
×
665

666
            auto& constant_node =
×
667
                builder.add_constant(parent, node["data"], *type, json_to_debug_info(node["debug_info"]));
×
668
            constant_node.element_id_ = node["element_id"];
×
669
            nodes_map.insert({node["element_id"], constant_node});
×
670
        } else {
×
671
            throw std::runtime_error("Unknown node type");
×
672
        }
×
673
    }
214✔
674

675
    assert(j.contains("edges"));
47✔
676
    assert(j["edges"].is_array());
47✔
677
    for (const auto& edge : j["edges"]) {
188✔
678
        assert(edge.contains("src"));
188✔
679
        assert(edge["src"].is_number_integer());
188✔
680
        assert(edge.contains("dst"));
188✔
681
        assert(edge["dst"].is_number_integer());
188✔
682
        assert(edge.contains("src_conn"));
188✔
683
        assert(edge["src_conn"].is_string());
188✔
684
        assert(edge.contains("dst_conn"));
188✔
685
        assert(edge["dst_conn"].is_string());
188✔
686
        assert(edge.contains("subset"));
188✔
687
        assert(edge["subset"].is_array());
188✔
688

689
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
188✔
690
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
188✔
691
        auto& source = nodes_map.at(edge["src"]);
188✔
692
        auto& target = nodes_map.at(edge["dst"]);
188✔
693

694
        auto base_type = json_to_type(edge["base_type"]);
188✔
695

696
        assert(edge.contains("subset"));
188✔
697
        assert(edge["subset"].is_array());
188✔
698
        std::vector<symbolic::Expression> subset;
188✔
699
        for (const auto& subset_ : edge["subset"]) {
188✔
700
            assert(subset_.is_string());
70✔
701
            std::string subset_str = subset_;
70✔
702
            auto expr = symbolic::parse(subset_str);
70✔
703
            subset.push_back(expr);
70✔
704
        }
70✔
705
        auto& memlet = builder.add_memlet(
188✔
706
            parent,
188✔
707
            source,
188✔
708
            edge["src_conn"],
188✔
709
            target,
188✔
710
            edge["dst_conn"],
188✔
711
            subset,
188✔
712
            *base_type,
188✔
713
            json_to_debug_info(edge["debug_info"])
188✔
714
        );
188✔
715
        memlet.element_id_ = edge["element_id"];
188✔
716
    }
188✔
717
}
47✔
718

719
control_flow::Assignments JSONSerializer::parse_assignments(const nlohmann::json& assignments_arr) {
4✔
720
    assert(assignments_arr.is_array());
4✔
721

722
    control_flow::Assignments assignments;
4✔
723
    for (const auto& assignment : assignments_arr) {
4✔
724
        assert(assignment.contains("symbol"));
3✔
725
        assert(assignment["symbol"].is_string());
3✔
726
        assert(assignment.contains("expression"));
3✔
727
        assert(assignment["expression"].is_string());
3✔
728
        auto expr = symbolic::parse(assignment["expression"].get<std::string>());
3✔
729
        assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
3✔
730
    }
3✔
731

732
    return std::move(assignments);
4✔
733
}
4✔
734

735
void JSONSerializer::parse_legacy_transition_to_assignment_block(
736
    const nlohmann::json& j,
737
    sdfg::builder::StructuredSDFGBuilder& builder,
738
    sdfg::structured_control_flow::Sequence& parent
739
) {
2✔
740
    assert(j.contains("type"));
2✔
741
    assert(j["type"].is_string());
2✔
742
    assert(j["type"] == "transition");
2✔
743
    assert(j.contains("assignments"));
2✔
744
    auto& assignments_arr = j["assignments"];
2✔
745

746
    auto assignments = parse_assignments(assignments_arr);
2✔
747

748
    if (!skip_empty_transitions || !assignments.empty()) {
2✔
749
        auto& block = builder.add_assignments(parent, assignments, json_to_debug_info(j["debug_info"]));
2✔
750
        block.element_id_ = j["element_id"];
2✔
751
    }
2✔
752
}
2✔
753

754
void JSONSerializer::json_to_sequence(
755
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
756
) {
66✔
757
    assert(j.contains("type"));
66✔
758
    assert(j["type"].is_string());
66✔
759
    assert(j.contains("children"));
66✔
760
    assert(j["children"].is_array());
66✔
761
    auto child_count = j["children"].size();
66✔
762

763
    sequence.element_id_ = j["element_id"];
66✔
764
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
66✔
765

766
    std::string type = j["type"];
66✔
767
    if (type == "sequence") {
66✔
768
        auto transition_it = j.find("transitions");
66✔
769
        const nlohmann::json* transition_arr = nullptr;
66✔
770
        if (transition_it != j.end()) {
66✔
771
            auto& transitions = *transition_it;
1✔
772
            if (transitions.is_array() && transitions.size() == child_count) {
1✔
773
                transition_arr = &transitions;
1✔
774
            } else {
1✔
775
                throw std::runtime_error(
×
776
                    "Contains legacy transitions, but count differs from chil count: " +
×
777
                    std::to_string(transitions.size()) + " vs. " + std::to_string(child_count)
×
778
                );
×
779
            }
×
780
        }
1✔
781

782
        for (size_t i = 0; i < child_count; i++) {
139✔
783
            auto& child = j["children"][i];
73✔
784

785
            auto child_type = child["type"];
73✔
786
            if (child_type == "block") {
73✔
787
                json_to_block_node(child, builder, sequence);
44✔
788
            } else if (child_type == "assignment_block") {
44✔
789
                json_to_assignment_block(child, builder, sequence);
2✔
790
            } else if (child_type == "if_else") {
27✔
791
                json_to_if_else_node(child, builder, sequence);
×
792
            } else if (child_type == "while") {
27✔
793
                json_to_while_node(child, builder, sequence);
×
794
            } else if (child_type == "break") {
27✔
795
                json_to_break_node(child, builder, sequence);
1✔
796
            } else if (child_type == "continue") {
26✔
797
                json_to_continue_node(child, builder, sequence);
1✔
798
            } else if (child_type == "return") {
25✔
799
                json_to_return_node(child, builder, sequence);
×
800
            } else if (child_type == "for" || child_type == "map") {
25✔
801
                json_to_structured_loop_node(child, builder, sequence);
25✔
802
            } else if (child_type == "sequence") {
25✔
803
                auto& subseq = builder.add_sequence(sequence, json_to_debug_info(child["debug_info"]));
×
804
                json_to_sequence(child, builder, subseq);
×
805
            } else {
×
806
                throw std::runtime_error("Unknown child type");
×
807
            }
×
808

809
            if (transition_arr) {
73✔
810
                // parses the transition after having already added its element
811
                // will then add the Transitions as a AssignmentBlock instead
812
                // keeps the original order of instructions, but changes sequence element count
813
                parse_legacy_transition_to_assignment_block(transition_arr->at(i), builder, sequence);
2✔
814
            }
2✔
815
        }
73✔
816
    } else {
66✔
817
        throw std::runtime_error("expected sequence type");
×
818
    }
×
819
}
66✔
820

821
void JSONSerializer::json_to_block_node(
822
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
823
) {
45✔
824
    assert(j.contains("type"));
45✔
825
    assert(j["type"].is_string());
45✔
826
    assert(j.contains("dataflow"));
45✔
827
    assert(j["dataflow"].is_object());
45✔
828
    auto& block = builder.add_block(parent, json_to_debug_info(j["debug_info"]));
45✔
829
    block.element_id_ = j["element_id"];
45✔
830
    assert(j["dataflow"].contains("type"));
45✔
831
    assert(j["dataflow"]["type"].is_string());
45✔
832
    std::string type = j["dataflow"]["type"];
45✔
833
    if (type == "dataflow") {
45✔
834
        json_to_dataflow(j["dataflow"], builder, block);
45✔
835
    } else {
45✔
836
        throw std::runtime_error("Unknown dataflow type");
×
837
    }
×
838
}
45✔
839

840
void JSONSerializer::json_to_assignment_block(
841
    const nlohmann::json& j,
842
    sdfg::builder::StructuredSDFGBuilder& builder,
843
    sdfg::structured_control_flow::Sequence& parent
844
) {
2✔
845
    assert(j.contains("type"));
2✔
846
    assert(j["type"].is_string());
2✔
847
    assert(j["type"] == "assignment_block");
2✔
848
    assert(j.contains("assignments"));
2✔
849
    auto assignments_arr = j["assignments"];
2✔
850
    assert(assignments_arr.is_array());
2✔
851

852
    control_flow::Assignments assignments = parse_assignments(assignments_arr);
2✔
853

854
    auto& block = builder.add_assignments(parent, assignments, json_to_debug_info(j["debug_info"]));
2✔
855
    block.element_id_ = j["element_id"];
2✔
856
}
2✔
857

858
void JSONSerializer::json_to_structured_loop_node(
859
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
860
) {
28✔
861
    // Backward compatibility: if the type is "map"
862
    if (j["type"] == "map") {
28✔
863
        json_to_map_node(j, builder, parent);
7✔
864
        return;
7✔
865
    }
7✔
866
    // Sub types
867
    if (j.contains("sub_type")) {
21✔
868
        std::string sub_type = j["sub_type"];
21✔
869
        if (sub_type == "map") {
21✔
870
            json_to_map_node(j, builder, parent);
×
871
            return;
×
872
        } else if (sub_type == "reduce") {
21✔
873
            json_to_reduce_node(j, builder, parent);
1✔
874
            return;
1✔
875
        }
1✔
876
    }
21✔
877

878
    assert(j.contains("type"));
21✔
879
    assert(j["type"].is_string());
20✔
880
    assert(j.contains("indvar"));
20✔
881
    assert(j["indvar"].is_string());
20✔
882
    assert(j.contains("init"));
20✔
883
    assert(j["init"].is_string());
20✔
884
    assert(j.contains("condition"));
20✔
885
    assert(j["condition"].is_string());
20✔
886
    assert(j.contains("update"));
20✔
887
    assert(j["update"].is_string());
20✔
888
    assert(j.contains("root"));
20✔
889
    assert(j["root"].is_object());
20✔
890

891
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
20✔
892
    auto init = symbolic::parse(j["init"].get<std::string>());
20✔
893
    auto update = symbolic::parse(j["update"].get<std::string>());
20✔
894

895
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
20✔
896
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
20✔
897
    if (condition.is_null()) {
20✔
898
        throw InvalidSDFGException("For loop condition is not a boolean expression");
×
899
    }
×
900

901
    auto& for_node = builder.add_for(parent, indvar, condition, init, update, json_to_debug_info(j["debug_info"]));
20✔
902
    for_node.element_id_ = j["element_id"];
20✔
903

904
    assert(j["root"].contains("type"));
20✔
905
    assert(j["root"]["type"].is_string());
20✔
906
    assert(j["root"]["type"] == "sequence");
20✔
907
    json_to_sequence(j["root"], builder, for_node.root());
20✔
908
}
20✔
909

910
void JSONSerializer::json_to_if_else_node(
911
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
912
) {
1✔
913
    assert(j.contains("type"));
1✔
914
    assert(j["type"].is_string());
1✔
915
    assert(j["type"] == "if_else");
1✔
916
    assert(j.contains("branches"));
1✔
917
    assert(j["branches"].is_array());
1✔
918
    auto& if_else_node = builder.add_if_else(parent, json_to_debug_info(j["debug_info"]));
1✔
919
    if_else_node.element_id_ = j["element_id"];
1✔
920
    for (const auto& branch : j["branches"]) {
2✔
921
        assert(branch.contains("condition"));
2✔
922
        assert(branch["condition"].is_string());
2✔
923
        assert(branch.contains("root"));
2✔
924
        assert(branch["root"].is_object());
2✔
925

926
        auto condition_expr = symbolic::parse(branch["condition"].get<std::string>());
2✔
927
        symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
2✔
928
        if (condition.is_null()) {
2✔
929
            throw InvalidSDFGException("If condition is not a boolean expression");
×
930
        }
×
931
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
932
        assert(branch["root"].contains("type"));
2✔
933
        assert(branch["root"]["type"].is_string());
2✔
934
        std::string type = branch["root"]["type"];
2✔
935
        if (type == "sequence") {
2✔
936
            json_to_sequence(branch["root"], builder, branch_node);
2✔
937
        } else {
2✔
938
            throw std::runtime_error("Unknown child type");
×
939
        }
×
940
    }
2✔
941
}
1✔
942

943
void JSONSerializer::json_to_while_node(
944
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
945
) {
3✔
946
    assert(j.contains("type"));
3✔
947
    assert(j["type"].is_string());
3✔
948
    assert(j["type"] == "while");
3✔
949
    assert(j.contains("root"));
3✔
950
    assert(j["root"].is_object());
3✔
951

952
    auto& while_node = builder.add_while(parent, json_to_debug_info(j["debug_info"]));
3✔
953
    while_node.element_id_ = j["element_id"];
3✔
954

955
    assert(j["root"]["type"] == "sequence");
3✔
956
    json_to_sequence(j["root"], builder, while_node.root());
3✔
957
}
3✔
958

959
void JSONSerializer::json_to_break_node(
960
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
961
) {
1✔
962
    assert(j.contains("type"));
1✔
963
    assert(j["type"].is_string());
1✔
964
    assert(j["type"] == "break");
1✔
965
    auto& node = builder.add_break(parent, json_to_debug_info(j["debug_info"]));
1✔
966
    node.element_id_ = j["element_id"];
1✔
967
}
1✔
968

969
void JSONSerializer::json_to_continue_node(
970
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
971
) {
1✔
972
    assert(j.contains("type"));
1✔
973
    assert(j["type"].is_string());
1✔
974
    assert(j["type"] == "continue");
1✔
975
    auto& node = builder.add_continue(parent, json_to_debug_info(j["debug_info"]));
1✔
976
    node.element_id_ = j["element_id"];
1✔
977
}
1✔
978

979
void JSONSerializer::json_to_map_node(
980
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
981
) {
7✔
982
    assert(j.contains("type"));
7✔
983
    assert(j["type"].is_string());
7✔
984
    assert(j["type"] == "map");
7✔
985
    assert(j.contains("indvar"));
7✔
986
    assert(j["indvar"].is_string());
7✔
987
    assert(j.contains("init"));
7✔
988
    assert(j["init"].is_string());
7✔
989
    assert(j.contains("condition"));
7✔
990
    assert(j["condition"].is_string());
7✔
991
    assert(j.contains("update"));
7✔
992
    assert(j["update"].is_string());
7✔
993
    assert(j.contains("root"));
7✔
994
    assert(j["root"].is_object());
7✔
995
    assert(j.contains("schedule_type"));
7✔
996
    assert(j["schedule_type"].is_object());
7✔
997

998
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
7✔
999

1000
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
7✔
1001
    auto init = symbolic::parse(j["init"].get<std::string>());
7✔
1002
    auto update = symbolic::parse(j["update"].get<std::string>());
7✔
1003
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
7✔
1004
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
7✔
1005
    if (condition.is_null()) {
7✔
1006
        throw InvalidSDFGException("Map condition is not a boolean expression");
×
1007
    }
×
1008

1009
    auto& map_node =
7✔
1010
        builder.add_map(parent, indvar, condition, init, update, schedule_type, json_to_debug_info(j["debug_info"]));
7✔
1011
    map_node.element_id_ = j["element_id"];
7✔
1012

1013
    assert(j["root"].contains("type"));
7✔
1014
    assert(j["root"]["type"].is_string());
7✔
1015
    assert(j["root"]["type"] == "sequence");
7✔
1016
    json_to_sequence(j["root"], builder, map_node.root());
7✔
1017
}
7✔
1018

1019
void JSONSerializer::json_to_reduce_node(
1020
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
1021
) {
1✔
1022
    assert(j.contains("type"));
1✔
1023
    assert(j["type"].is_string());
1✔
1024
    assert(j["type"] == "for");
1✔
1025
    assert(j.contains("sub_type"));
1✔
1026
    assert(j["sub_type"].is_string());
1✔
1027
    assert(j["sub_type"] == "reduce");
1✔
1028
    assert(j.contains("indvar"));
1✔
1029
    assert(j["indvar"].is_string());
1✔
1030
    assert(j.contains("init"));
1✔
1031
    assert(j["init"].is_string());
1✔
1032
    assert(j.contains("condition"));
1✔
1033
    assert(j["condition"].is_string());
1✔
1034
    assert(j.contains("update"));
1✔
1035
    assert(j["update"].is_string());
1✔
1036
    assert(j.contains("root"));
1✔
1037
    assert(j["root"].is_object());
1✔
1038
    assert(j.contains("reductions"));
1✔
1039
    assert(j["reductions"].is_array());
1✔
1040
    assert(j.contains("schedule_type"));
1✔
1041
    assert(j["schedule_type"].is_object());
1✔
1042

1043
    std::vector<structured_control_flow::ReductionInfo> reductions;
1✔
1044
    for (const auto& reduction_json : j["reductions"]) {
1✔
1045
        assert(reduction_json.contains("op"));
1✔
1046
        assert(reduction_json["op"].is_string());
1✔
1047
        assert(reduction_json.contains("container"));
1✔
1048
        assert(reduction_json["container"].is_string());
1✔
1049
        reductions.push_back(structured_control_flow::ReductionInfo{
1✔
1050
            structured_control_flow::reduction_operation_from_string(reduction_json["op"].get<std::string>()),
1✔
1051
            reduction_json["container"].get<std::string>()
1✔
1052
        });
1✔
1053
    }
1✔
1054

1055
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
1056

1057
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
1058
    auto init = symbolic::parse(j["init"].get<std::string>());
1✔
1059
    auto update = symbolic::parse(j["update"].get<std::string>());
1✔
1060
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
1✔
1061
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
1✔
1062
    if (condition.is_null()) {
1✔
1063
        throw InvalidSDFGException("Reduce condition is not a boolean expression");
×
1064
    }
×
1065

1066
    auto& reduce_node = builder.add_reduce(
1✔
1067
        parent, indvar, condition, init, update, reductions, schedule_type, json_to_debug_info(j["debug_info"])
1✔
1068
    );
1✔
1069
    reduce_node.element_id_ = j["element_id"];
1✔
1070

1071
    assert(j["root"].contains("type"));
1✔
1072
    assert(j["root"]["type"].is_string());
1✔
1073
    assert(j["root"]["type"] == "sequence");
1✔
1074
    json_to_sequence(j["root"], builder, reduce_node.root());
1✔
1075
}
1✔
1076

1077
void JSONSerializer::json_to_return_node(
1078
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
1079
) {
1✔
1080
    assert(j.contains("type"));
1✔
1081
    assert(j["type"].is_string());
1✔
1082
    assert(j["type"] == "return");
1✔
1083

1084
    std::string data = j["data"];
1✔
1085
    std::unique_ptr<types::IType> data_type = nullptr;
1✔
1086
    if (j.contains("data_type")) {
1✔
1087
        data_type = json_to_type(j["data_type"]);
×
1088
    }
×
1089

1090
    if (data_type == nullptr) {
1✔
1091
        auto& node = builder.add_return(parent, data, json_to_debug_info(j["debug_info"]));
1✔
1092
        node.element_id_ = j["element_id"];
1✔
1093
    } else {
1✔
1094
        auto& node = builder.add_constant_return(parent, data, *data_type, json_to_debug_info(j["debug_info"]));
×
1095
        node.element_id_ = j["element_id"];
×
1096
    }
×
1097
}
1✔
1098

1099
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
547✔
1100
    if (j.contains("type")) {
547✔
1101
        if (j["type"] == "scalar") {
547✔
1102
            // Deserialize scalar type
1103
            assert(j.contains("primitive_type"));
397✔
1104
            types::PrimitiveType primitive_type = j["primitive_type"];
397✔
1105
            assert(j.contains("storage_type"));
397✔
1106
            types::StorageType storage_type = json_to_storage_type(j["storage_type"]);
397✔
1107
            assert(j.contains("initializer"));
397✔
1108
            std::string initializer = j["initializer"];
397✔
1109
            assert(j.contains("alignment"));
397✔
1110
            size_t alignment = j["alignment"];
397✔
1111
            return std::make_unique<types::Scalar>(storage_type, alignment, initializer, primitive_type);
397✔
1112
        } else if (j["type"] == "array") {
397✔
1113
            // Deserialize array type
1114
            assert(j.contains("element_type"));
76✔
1115
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
76✔
1116
            assert(j.contains("num_elements"));
76✔
1117
            std::string num_elements_str = j["num_elements"];
76✔
1118
            // Convert num_elements_str to symbolic::Expression
1119
            auto num_elements = symbolic::parse(num_elements_str);
76✔
1120
            assert(j.contains("storage_type"));
76✔
1121
            types::StorageType storage_type = json_to_storage_type(j["storage_type"]);
76✔
1122
            assert(j.contains("initializer"));
76✔
1123
            std::string initializer = j["initializer"];
76✔
1124
            assert(j.contains("alignment"));
76✔
1125
            size_t alignment = j["alignment"];
76✔
1126
            return std::make_unique<types::Array>(storage_type, alignment, initializer, *member_type, num_elements);
76✔
1127
        } else if (j["type"] == "pointer") {
76✔
1128
            // Deserialize pointer type
1129
            std::optional<std::unique_ptr<types::IType>> pointee_type;
57✔
1130
            if (j.contains("pointee_type")) {
57✔
1131
                assert(j.contains("pointee_type"));
56✔
1132
                pointee_type = json_to_type(j["pointee_type"]);
56✔
1133
            } else {
56✔
1134
                pointee_type = std::nullopt;
1✔
1135
            }
1✔
1136
            assert(j.contains("storage_type"));
57✔
1137
            types::StorageType storage_type = json_to_storage_type(j["storage_type"]);
57✔
1138
            assert(j.contains("initializer"));
57✔
1139
            std::string initializer = j["initializer"];
57✔
1140
            assert(j.contains("alignment"));
57✔
1141
            size_t alignment = j["alignment"];
57✔
1142
            if (pointee_type.has_value()) {
57✔
1143
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer, *pointee_type.value());
56✔
1144
            } else {
56✔
1145
                return std::make_unique<types::Pointer>(storage_type, alignment, initializer);
1✔
1146
            }
1✔
1147
        } else if (j["type"] == "structure") {
57✔
1148
            // Deserialize structure type
1149
            assert(j.contains("name"));
3✔
1150
            std::string name = j["name"];
3✔
1151
            assert(j.contains("storage_type"));
3✔
1152
            types::StorageType storage_type = json_to_storage_type(j["storage_type"]);
3✔
1153
            assert(j.contains("initializer"));
3✔
1154
            std::string initializer = j["initializer"];
3✔
1155
            assert(j.contains("alignment"));
3✔
1156
            size_t alignment = j["alignment"];
3✔
1157
            return std::make_unique<types::Structure>(storage_type, alignment, initializer, name);
3✔
1158
        } else if (j["type"] == "function") {
14✔
1159
            // Deserialize function type
1160
            assert(j.contains("return_type"));
4✔
1161
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
4✔
1162
            assert(j.contains("params"));
4✔
1163
            std::vector<std::unique_ptr<types::IType>> params;
4✔
1164
            for (const auto& param : j["params"]) {
10✔
1165
                params.push_back(json_to_type(param));
10✔
1166
            }
10✔
1167
            assert(j.contains("is_var_arg"));
4✔
1168
            bool is_var_arg = j["is_var_arg"];
4✔
1169
            assert(j.contains("storage_type"));
4✔
1170
            types::StorageType storage_type = json_to_storage_type(j["storage_type"]);
4✔
1171
            assert(j.contains("initializer"));
4✔
1172
            std::string initializer = j["initializer"];
4✔
1173
            assert(j.contains("alignment"));
4✔
1174
            size_t alignment = j["alignment"];
4✔
1175
            auto function =
4✔
1176
                std::make_unique<types::Function>(storage_type, alignment, initializer, *return_type, is_var_arg);
4✔
1177
            for (const auto& param : params) {
10✔
1178
                function->add_param(*param);
10✔
1179
            }
10✔
1180
            return function->clone();
4✔
1181
        } else if (j["type"] == "reference") {
10✔
1182
            // Deserialize reference type
1183
            assert(j.contains("reference_type"));
5✔
1184
            std::unique_ptr<types::IType> reference_type = json_to_type(j["reference_type"]);
5✔
1185
            return std::make_unique<sdfg::codegen::Reference>(*reference_type);
5✔
1186
        } else if (j["type"] == "tensor") {
5✔
1187
            // Deserialize tensor type
1188
            assert(j.contains("element_type"));
5✔
1189
            std::unique_ptr<types::IType> element_type = json_to_type(j["element_type"]);
5✔
1190
            assert(j.contains("shape"));
5✔
1191
            std::vector<symbolic::Expression> shape;
5✔
1192
            for (const auto& dim : j["shape"]) {
13✔
1193
                assert(dim.is_string());
13✔
1194
                std::string dim_str = dim;
13✔
1195
                auto expr = symbolic::parse(dim_str);
13✔
1196
                shape.push_back(expr);
13✔
1197
            }
13✔
1198
            assert(j.contains("strides"));
5✔
1199
            std::vector<symbolic::Expression> strides;
5✔
1200
            for (const auto& stride : j["strides"]) {
13✔
1201
                assert(stride.is_string());
13✔
1202
                std::string stride_str = stride;
13✔
1203
                auto expr = symbolic::parse(stride_str);
13✔
1204
                strides.push_back(expr);
13✔
1205
            }
13✔
1206
            assert(j.contains("offset"));
5✔
1207
            symbolic::Expression offset = symbolic::parse(j["offset"].get<std::string>());
5✔
1208
            assert(j.contains("storage_type"));
5✔
1209
            types::StorageType storage_type = json_to_storage_type(j["storage_type"]);
5✔
1210
            assert(j.contains("initializer"));
5✔
1211
            std::string initializer = j["initializer"];
5✔
1212
            assert(j.contains("alignment"));
5✔
1213
            size_t alignment = j["alignment"];
5✔
1214
            return std::make_unique<types::Tensor>(
5✔
1215
                storage_type, alignment, initializer, dynamic_cast<types::Scalar&>(*element_type), shape, strides, offset
5✔
1216
            );
5✔
1217
        } else {
5✔
1218
            throw std::runtime_error("Unknown type");
×
1219
        }
×
1220
    } else {
547✔
1221
        throw std::runtime_error("Type not found");
×
1222
    }
×
1223
}
547✔
1224

1225
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
552✔
1226
    assert(j.contains("has"));
552✔
1227
    assert(j["has"].is_boolean());
552✔
1228
    if (!j["has"]) {
552✔
1229
        return DebugInfo();
543✔
1230
    }
543✔
1231
    assert(j.contains("filename"));
552✔
1232
    assert(j["filename"].is_string());
9✔
1233
    std::string filename = j["filename"];
9✔
1234
    assert(j.contains("function"));
9✔
1235
    assert(j["function"].is_string());
9✔
1236
    std::string function = j["function"];
9✔
1237
    assert(j.contains("start_line"));
9✔
1238
    assert(j["start_line"].is_number_integer());
9✔
1239
    size_t start_line = j["start_line"];
9✔
1240
    assert(j.contains("start_column"));
9✔
1241
    assert(j["start_column"].is_number_integer());
9✔
1242
    size_t start_column = j["start_column"];
9✔
1243
    assert(j.contains("end_line"));
9✔
1244
    assert(j["end_line"].is_number_integer());
9✔
1245
    size_t end_line = j["end_line"];
9✔
1246
    assert(j.contains("end_column"));
9✔
1247
    assert(j["end_column"].is_number_integer());
9✔
1248
    size_t end_column = j["end_column"];
9✔
1249
    return DebugInfo(filename, function, start_line, start_column, end_line, end_column);
9✔
1250
}
9✔
1251

1252
ScheduleType JSONSerializer::json_to_schedule_type(const nlohmann::json& j) {
9✔
1253
    assert(j.contains("value"));
9✔
1254
    assert(j["value"].is_string());
9✔
1255
    // assert(j.contains("category"));
1256
    // assert(j["category"].is_number_integer());
1257
    assert(j.contains("properties"));
9✔
1258
    assert(j["properties"].is_object());
9✔
1259
    ScheduleTypeCategory category = ScheduleTypeCategory::None;
9✔
1260
    if (j.contains("category")) {
9✔
1261
        category = static_cast<ScheduleTypeCategory>(j["category"].get<int>());
9✔
1262
    }
9✔
1263
    ScheduleType schedule_type(j["value"].get<std::string>(), category);
9✔
1264
    for (const auto& [key, value] : j["properties"].items()) {
9✔
1265
        assert(value.is_string());
3✔
1266
        schedule_type.set_property(key, value.get<std::string>());
3✔
1267
    }
3✔
1268
    return schedule_type;
9✔
1269
}
9✔
1270

1271
types::StorageType JSONSerializer::json_to_storage_type(const nlohmann::json& j) {
548✔
1272
    if (!j.contains("value")) {
548✔
1273
        return types::StorageType::CPU_Stack();
×
1274
    }
×
1275
    std::string value = j["value"].get<std::string>();
548✔
1276

1277
    symbolic::Expression allocation_size = SymEngine::null;
548✔
1278
    if (j.contains("allocation_size")) {
548✔
1279
        allocation_size = symbolic::parse(j["allocation_size"].get<std::string>());
×
1280
    }
×
1281

1282
    types::StorageType::AllocationType allocation = j["allocation"];
548✔
1283
    types::StorageType::AllocationType deallocation = j["deallocation"];
548✔
1284

1285
    auto storageType = types::StorageType(j["value"].get<std::string>(), allocation_size, allocation, deallocation);
548✔
1286

1287
    if (j.contains("args")) {
548✔
1288
        nlohmann::json::array_t args = j["args"];
×
1289
        if (args.size() > 0) {
×
1290
            storageType.arg1(symbolic::parse(args[0].get<std::string>()));
×
1291
        }
×
1292
    }
×
1293
    return storageType;
548✔
1294
}
548✔
1295

1296
std::string JSONSerializer::expression(const symbolic::Expression expr) {
996✔
1297
    JSONSymbolicPrinter printer;
996✔
1298
    return printer.apply(expr);
996✔
1299
};
996✔
1300

1301
symbolic::Expression JSONSerializer::json_to_expr(const nlohmann::json& j) {
3✔
1302
    return symbolic::parse(j.get<std::string>());
3✔
1303
}
3✔
1304

1305
void JSONSerializer::writeToFile(const StructuredSDFG& sdfg, const std::filesystem::path& file) {
×
1306
    JSONSerializer ser;
×
1307
    auto json = ser.serialize(sdfg);
×
1308

1309
    auto parent_path = file.parent_path();
×
1310
    if (!parent_path.empty()) {
×
1311
        std::filesystem::create_directories(file.parent_path());
×
1312
    }
×
1313

1314
    std::ofstream out(file, std::ofstream::out);
×
1315
    if (!out.is_open()) {
×
1316
        std::cerr << "Could not open file " << file << " for writing JSON output." << std::endl;
×
1317
    }
×
1318
    out << json << std::endl;
×
1319
    out.close();
×
1320
}
×
1321

1322
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1323
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1324
    str_ = parenthesize(str_);
×
1325
};
×
1326

1327
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1328
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1329
    str_ = parenthesize(str_);
×
1330
};
×
1331

1332
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
4✔
1333
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
4✔
1334
    str_ = parenthesize(str_);
4✔
1335
};
4✔
1336

1337
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
194✔
1338
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
194✔
1339
    str_ = parenthesize(str_);
194✔
1340
};
194✔
1341

1342
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
2✔
1343
    std::ostringstream s;
2✔
1344
    auto container = x.get_args();
2✔
1345
    if (container.size() == 1) {
2✔
1346
        s << apply(*container.begin());
×
1347
    } else {
2✔
1348
        s << "min(";
2✔
1349
        s << apply(*container.begin());
2✔
1350

1351
        // Recursively apply __daisy_min to the arguments
1352
        SymEngine::vec_basic subargs;
2✔
1353
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
4✔
1354
            subargs.push_back(*it);
2✔
1355
        }
2✔
1356
        auto submin = SymEngine::min(subargs);
2✔
1357
        s << ", " << apply(submin);
2✔
1358

1359
        s << ")";
2✔
1360
    }
2✔
1361

1362
    str_ = s.str();
2✔
1363
};
2✔
1364

1365
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
4✔
1366
    std::ostringstream s;
4✔
1367
    auto container = x.get_args();
4✔
1368
    if (container.size() == 1) {
4✔
1369
        s << apply(*container.begin());
×
1370
    } else {
4✔
1371
        s << "max(";
4✔
1372
        s << apply(*container.begin());
4✔
1373

1374
        // Recursively apply __daisy_max to the arguments
1375
        SymEngine::vec_basic subargs;
4✔
1376
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
8✔
1377
            subargs.push_back(*it);
4✔
1378
        }
4✔
1379
        auto submax = SymEngine::max(subargs);
4✔
1380
        s << ", " << apply(submax);
4✔
1381

1382
        s << ")";
4✔
1383
    }
4✔
1384

1385
    str_ = s.str();
4✔
1386
};
4✔
1387

1388
void LibraryNodeSerializerRegistry::
1389
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
234✔
1390
    std::lock_guard<std::mutex> lock(mutex_);
234✔
1391
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
234✔
1392
        return;
8✔
1393
    }
8✔
1394
    factory_map_[library_node_code] = std::move(fn);
226✔
1395
}
226✔
1396

1397
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
21✔
1398
    auto it = factory_map_.find(library_node_code);
21✔
1399
    if (it != factory_map_.end()) {
21✔
1400
        return it->second;
21✔
1401
    }
21✔
1402
    return nullptr;
×
1403
}
21✔
1404

1405
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1406

1407
void register_default_serializers() {
4✔
1408
    // stdlib
1409
    LibraryNodeSerializerRegistry::instance()
4✔
1410
        .register_library_node_serializer(stdlib::LibraryNodeType_Alloca.value(), []() {
4✔
1411
            return std::make_unique<stdlib::AllocaNodeSerializer>();
×
1412
        });
×
1413
    LibraryNodeSerializerRegistry::instance()
4✔
1414
        .register_library_node_serializer(stdlib::LibraryNodeType_Assert.value(), []() {
4✔
1415
            return std::make_unique<stdlib::AssertNodeSerializer>();
×
1416
        });
×
1417
    LibraryNodeSerializerRegistry::instance()
4✔
1418
        .register_library_node_serializer(stdlib::LibraryNodeType_Calloc.value(), []() {
4✔
1419
            return std::make_unique<stdlib::CallocNodeSerializer>();
×
1420
        });
×
1421
    LibraryNodeSerializerRegistry::instance()
4✔
1422
        .register_library_node_serializer(stdlib::LibraryNodeType_Free.value(), []() {
4✔
1423
            return std::make_unique<stdlib::FreeNodeSerializer>();
×
1424
        });
×
1425
    LibraryNodeSerializerRegistry::instance()
4✔
1426
        .register_library_node_serializer(stdlib::LibraryNodeType_Malloc.value(), []() {
4✔
1427
            return std::make_unique<stdlib::MallocNodeSerializer>();
×
1428
        });
×
1429
    LibraryNodeSerializerRegistry::instance()
4✔
1430
        .register_library_node_serializer(stdlib::LibraryNodeType_Memcpy.value(), []() {
4✔
1431
            return std::make_unique<stdlib::MemcpyNodeSerializer>();
×
1432
        });
×
1433
    LibraryNodeSerializerRegistry::instance()
4✔
1434
        .register_library_node_serializer(stdlib::LibraryNodeType_Memmove.value(), []() {
4✔
1435
            return std::make_unique<stdlib::MemmoveNodeSerializer>();
×
1436
        });
×
1437
    LibraryNodeSerializerRegistry::instance()
4✔
1438
        .register_library_node_serializer(stdlib::LibraryNodeType_Memset.value(), []() {
4✔
1439
            return std::make_unique<stdlib::MemsetNodeSerializer>();
×
1440
        });
×
1441
    LibraryNodeSerializerRegistry::instance()
4✔
1442
        .register_library_node_serializer(stdlib::LibraryNodeType_Trap.value(), []() {
4✔
1443
            return std::make_unique<stdlib::TrapNodeSerializer>();
×
1444
        });
×
1445
    LibraryNodeSerializerRegistry::instance()
4✔
1446
        .register_library_node_serializer(stdlib::LibraryNodeType_Unreachable.value(), []() {
4✔
1447
            return std::make_unique<stdlib::UnreachableNodeSerializer>();
×
1448
        });
×
1449

1450
    // Metadata
1451
    LibraryNodeSerializerRegistry::instance()
4✔
1452
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
4✔
1453
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1454
        });
×
1455

1456
    // Barrier
1457
    LibraryNodeSerializerRegistry::instance()
4✔
1458
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
4✔
1459
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1460
        });
1✔
1461

1462
    // Call Node
1463
    LibraryNodeSerializerRegistry::instance()
4✔
1464
        .register_library_node_serializer(data_flow::LibraryNodeType_Call.value(), []() {
6✔
1465
            return std::make_unique<data_flow::CallNodeSerializer>();
6✔
1466
        });
6✔
1467
    LibraryNodeSerializerRegistry::instance()
4✔
1468
        .register_library_node_serializer(data_flow::LibraryNodeType_Invoke.value(), []() {
4✔
1469
            return std::make_unique<data_flow::InvokeNodeSerializer>();
×
1470
        });
×
1471

1472
    // LoadConst
1473
    LibraryNodeSerializerRegistry::instance()
4✔
1474
        .register_library_node_serializer(data_flow::LibraryNodeType_LoadConst.value(), []() {
4✔
1475
            return std::make_unique<data_flow::LoadConstNodeSerializer>();
2✔
1476
        });
2✔
1477

1478
    // CMath
1479
    LibraryNodeSerializerRegistry::instance()
4✔
1480
        .register_library_node_serializer(math::cmath::LibraryNodeType_CMath.value(), []() {
4✔
1481
            return std::make_unique<math::cmath::CMathNodeSerializer>();
×
1482
        });
×
1483
    // Backward compatibility
1484
    LibraryNodeSerializerRegistry::instance()
4✔
1485
        .register_library_node_serializer(math::cmath::LibraryNodeType_CMath_Deprecated.value(), []() {
4✔
1486
            return std::make_unique<math::cmath::CMathNodeSerializer>();
×
1487
        });
×
1488

1489
    // BLAS
1490
    LibraryNodeSerializerRegistry::instance()
4✔
1491
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
4✔
1492
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1493
        });
×
1494
    LibraryNodeSerializerRegistry::instance()
4✔
1495
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
4✔
1496
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1497
        });
×
1498
    LibraryNodeSerializerRegistry::instance()
4✔
1499
        .register_library_node_serializer(math::blas::LibraryNodeType_BatchedGEMM.value(), []() {
4✔
1500
            return std::make_unique<math::blas::BatchedGEMMNodeSerializer>();
×
1501
        });
×
1502

1503
    // Tensor
1504

1505
    LibraryNodeSerializerRegistry::instance()
4✔
1506
        .register_library_node_serializer(math::tensor::LibraryNodeType_Broadcast.value(), []() {
4✔
1507
            return std::make_unique<math::tensor::BroadcastNodeSerializer>();
×
1508
        });
×
1509
    LibraryNodeSerializerRegistry::instance()
4✔
1510
        .register_library_node_serializer(math::tensor::LibraryNodeType_Conv.value(), []() {
4✔
1511
            return std::make_unique<math::tensor::ConvNodeSerializer>();
×
1512
        });
×
1513
    LibraryNodeSerializerRegistry::instance()
4✔
1514
        .register_library_node_serializer(math::tensor::LibraryNodeType_Pooling.value(), []() {
4✔
1515
            return std::make_unique<math::tensor::PoolingNodeSerializer>();
×
1516
        });
×
1517
    LibraryNodeSerializerRegistry::instance()
4✔
1518
        .register_library_node_serializer(math::tensor::LibraryNodeType_MatMul.value(), []() {
4✔
1519
            return std::make_unique<math::tensor::MatMulNodeSerializer>();
×
1520
        });
×
1521
    LibraryNodeSerializerRegistry::instance()
4✔
1522
        .register_library_node_serializer(math::tensor::LibraryNodeType_TensorCopy.value(), []() {
4✔
1523
            return std::make_unique<math::tensor::TensorCopyNodeSerializer>();
2✔
1524
        });
2✔
1525
    LibraryNodeSerializerRegistry::instance()
4✔
1526
        .register_library_node_serializer(math::tensor::LibraryNodeType_TensorConcat.value(), []() {
4✔
1527
            return std::make_unique<math::tensor::ConcatNodeSerializer>();
2✔
1528
        });
2✔
1529

1530
    // Elementwise
1531
    LibraryNodeSerializerRegistry::instance()
4✔
1532
        .register_library_node_serializer(math::tensor::LibraryNodeType_Abs.value(), []() {
4✔
1533
            return std::make_unique<math::tensor::AbsNodeSerializer>();
×
1534
        });
×
1535
    LibraryNodeSerializerRegistry::instance()
4✔
1536
        .register_library_node_serializer(math::tensor::LibraryNodeType_Add.value(), []() {
4✔
1537
            return std::make_unique<math::tensor::AddNodeSerializer>();
×
1538
        });
×
1539
    LibraryNodeSerializerRegistry::instance()
4✔
1540
        .register_library_node_serializer(math::tensor::LibraryNodeType_Div.value(), []() {
4✔
1541
            return std::make_unique<math::tensor::DivNodeSerializer>();
×
1542
        });
×
1543
    LibraryNodeSerializerRegistry::instance()
4✔
1544
        .register_library_node_serializer(math::tensor::LibraryNodeType_Elu.value(), []() {
4✔
1545
            return std::make_unique<math::tensor::EluNodeSerializer>();
×
1546
        });
×
1547
    LibraryNodeSerializerRegistry::instance()
4✔
1548
        .register_library_node_serializer(math::tensor::LibraryNodeType_Exp.value(), []() {
4✔
1549
            return std::make_unique<math::tensor::ExpNodeSerializer>();
×
1550
        });
×
1551
    LibraryNodeSerializerRegistry::instance()
4✔
1552
        .register_library_node_serializer(math::tensor::LibraryNodeType_Erf.value(), []() {
4✔
1553
            return std::make_unique<math::tensor::ErfNodeSerializer>();
×
1554
        });
×
1555
    LibraryNodeSerializerRegistry::instance()
4✔
1556
        .register_library_node_serializer(math::tensor::LibraryNodeType_HardSigmoid.value(), []() {
4✔
1557
            return std::make_unique<math::tensor::HardSigmoidNodeSerializer>();
×
1558
        });
×
1559
    LibraryNodeSerializerRegistry::instance()
4✔
1560
        .register_library_node_serializer(math::tensor::LibraryNodeType_LeakyReLU.value(), []() {
4✔
1561
            return std::make_unique<math::tensor::LeakyReLUNodeSerializer>();
×
1562
        });
×
1563
    LibraryNodeSerializerRegistry::instance()
4✔
1564
        .register_library_node_serializer(math::tensor::LibraryNodeType_LogicalNot.value(), []() {
4✔
NEW
1565
            return std::make_unique<math::tensor::LogicalNotNodeSerializer>();
×
NEW
1566
        });
×
1567
    LibraryNodeSerializerRegistry::instance()
4✔
1568
        .register_library_node_serializer(math::tensor::LibraryNodeType_Mul.value(), []() {
4✔
1569
            return std::make_unique<math::tensor::MulNodeSerializer>();
×
1570
        });
×
1571
    LibraryNodeSerializerRegistry::instance()
4✔
1572
        .register_library_node_serializer(math::tensor::LibraryNodeType_Pow.value(), []() {
4✔
1573
            return std::make_unique<math::tensor::PowNodeSerializer>();
×
1574
        });
×
1575
    LibraryNodeSerializerRegistry::instance()
4✔
1576
        .register_library_node_serializer(math::tensor::LibraryNodeType_ReLU.value(), []() {
4✔
1577
            return std::make_unique<math::tensor::ReLUNodeSerializer>();
×
1578
        });
×
1579
    LibraryNodeSerializerRegistry::instance()
4✔
1580
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sigmoid.value(), []() {
4✔
1581
            return std::make_unique<math::tensor::SigmoidNodeSerializer>();
×
1582
        });
×
1583
    LibraryNodeSerializerRegistry::instance()
4✔
1584
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sqrt.value(), []() {
4✔
1585
            return std::make_unique<math::tensor::SqrtNodeSerializer>();
×
1586
        });
×
1587
    LibraryNodeSerializerRegistry::instance()
4✔
1588
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sub.value(), []() {
4✔
1589
            return std::make_unique<math::tensor::SubNodeSerializer>();
×
1590
        });
×
1591
    LibraryNodeSerializerRegistry::instance()
4✔
1592
        .register_library_node_serializer(math::tensor::LibraryNodeType_Tanh.value(), []() {
4✔
1593
            return std::make_unique<math::tensor::TanhNodeSerializer>();
×
1594
        });
×
1595
    LibraryNodeSerializerRegistry::instance()
4✔
1596
        .register_library_node_serializer(math::tensor::LibraryNodeType_Minimum.value(), []() {
4✔
1597
            return std::make_unique<math::tensor::MinimumNodeSerializer>();
×
1598
        });
×
1599
    LibraryNodeSerializerRegistry::instance()
4✔
1600
        .register_library_node_serializer(math::tensor::LibraryNodeType_Maximum.value(), []() {
4✔
1601
            return std::make_unique<math::tensor::MaximumNodeSerializer>();
×
1602
        });
×
1603
    LibraryNodeSerializerRegistry::instance()
4✔
1604
        .register_library_node_serializer(math::tensor::LibraryNodeType_Fill.value(), []() {
4✔
1605
            return std::make_unique<math::tensor::FillNodeSerializer>();
×
1606
        });
×
1607
    LibraryNodeSerializerRegistry::instance()
4✔
1608
        .register_library_node_serializer(math::tensor::LibraryNodeType_TensorTasklet.value(), []() {
4✔
1609
            return std::make_unique<math::tensor::TaskletTensorNodeSerializer>();
×
1610
        });
×
1611
    LibraryNodeSerializerRegistry::instance()
4✔
1612
        .register_library_node_serializer(math::tensor::LibraryNodeType_TensorCMath.value(), []() {
4✔
1613
            return std::make_unique<math::tensor::CMathTensorNodeSerializer>();
×
1614
        });
×
1615
    LibraryNodeSerializerRegistry::instance()
4✔
1616
        .register_library_node_serializer(math::tensor::LibraryNodeType_Cast.value(), []() {
4✔
1617
            return std::make_unique<math::tensor::CastNodeSerializer>();
×
1618
        });
×
1619

1620
    LibraryNodeSerializerRegistry::instance()
4✔
1621
        .register_library_node_serializer(math::tensor::LibraryNodeType_BatchNorm.value(), [] {
4✔
1622
            return std::make_unique<math::tensor::BatchNormNodeSerializer>();
×
1623
        });
×
1624

1625
    // Reduce
1626
    LibraryNodeSerializerRegistry::instance()
4✔
1627
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sum.value(), []() {
4✔
1628
            return std::make_unique<math::tensor::SumNodeSerializer>();
×
1629
        });
×
1630
    LibraryNodeSerializerRegistry::instance()
4✔
1631
        .register_library_node_serializer(math::tensor::LibraryNodeType_Max.value(), []() {
4✔
1632
            return std::make_unique<math::tensor::MaxNodeSerializer>();
×
1633
        });
×
1634
    LibraryNodeSerializerRegistry::instance()
4✔
1635
        .register_library_node_serializer(math::tensor::LibraryNodeType_Min.value(), []() {
4✔
1636
            return std::make_unique<math::tensor::MinNodeSerializer>();
×
1637
        });
×
1638
    LibraryNodeSerializerRegistry::instance()
4✔
1639
        .register_library_node_serializer(math::tensor::LibraryNodeType_Softmax.value(), []() {
4✔
1640
            return std::make_unique<math::tensor::SoftmaxNodeSerializer>();
×
1641
        });
×
1642
    LibraryNodeSerializerRegistry::instance()
4✔
1643
        .register_library_node_serializer(math::tensor::LibraryNodeType_Mean.value(), []() {
4✔
1644
            return std::make_unique<math::tensor::MeanNodeSerializer>();
×
1645
        });
×
1646
    LibraryNodeSerializerRegistry::instance()
4✔
1647
        .register_library_node_serializer(math::tensor::LibraryNodeType_Std.value(), []() {
4✔
1648
            return std::make_unique<math::tensor::StdNodeSerializer>();
×
1649
        });
×
1650

1651
    // Einsum
1652
    LibraryNodeSerializerRegistry::instance()
4✔
1653
        .register_library_node_serializer(math::tensor::LibraryNodeType_Einsum.value(), []() {
4✔
1654
            return std::make_unique<math::tensor::EinsumSerializer>();
×
1655
        });
×
1656
}
4✔
1657

1658
} // namespace serializer
1659
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc