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

daisytuner / docc / 28188098396

25 Jun 2026 05:21PM UTC coverage: 61.746% (+0.1%) from 61.644%
28188098396

Pull #802

github

web-flow
Merge 08b5c3f1d into fe9abd1cb
Pull Request #802: adds reduce as new structured loop type

705 of 985 new or added lines in 34 files covered. (71.57%)

5 existing lines in 4 files now uncovered.

38839 of 62901 relevant lines covered (61.75%)

978.2 hits per line

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

79.05
/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/tasklet_node.h"
15
#include "sdfg/data_flow/library_nodes/metadata_node.h"
16
#include "sdfg/data_flow/library_nodes/stdlib/stdlib.h"
17

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

40
namespace sdfg {
41
namespace serializer {
42

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

50
    return FunctionType(str);
×
51
}
28✔
52

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

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

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

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

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

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

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

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

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

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

113
    return j;
26✔
114
}
26✔
115

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

138
void JSONSerializer::dataflow_to_json(nlohmann::json& j, const data_flow::DataFlowGraph& dataflow) {
56✔
139
    j["type"] = "dataflow";
56✔
140
    j["nodes"] = nlohmann::json::array();
56✔
141
    j["edges"] = nlohmann::json::array();
56✔
142

143
    for (auto& node : dataflow.nodes()) {
211✔
144
        nlohmann::json node_json;
211✔
145
        node_json["element_id"] = node.element_id();
211✔
146

147
        node_json["debug_info"] = nlohmann::json::object();
211✔
148
        debug_info_to_json(node_json["debug_info"], node.debug_info());
211✔
149

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

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

183
        j["nodes"].push_back(node_json);
211✔
184
    }
211✔
185

186
    for (auto& edge : dataflow.edges()) {
186✔
187
        nlohmann::json edge_json;
186✔
188
        edge_json["element_id"] = edge.element_id();
186✔
189

190
        edge_json["debug_info"] = nlohmann::json::object();
186✔
191
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
186✔
192

193
        edge_json["src"] = edge.src().element_id();
186✔
194
        edge_json["dst"] = edge.dst().element_id();
186✔
195

196
        edge_json["src_conn"] = edge.src_conn();
186✔
197
        edge_json["dst_conn"] = edge.dst_conn();
186✔
198

199
        edge_json["subset"] = nlohmann::json::array();
186✔
200
        for (auto& subset : edge.subset()) {
186✔
201
            edge_json["subset"].push_back(expression(subset));
72✔
202
        }
72✔
203

204
        nlohmann::json base_type_json;
186✔
205
        type_to_json(base_type_json, edge.base_type());
186✔
206
        edge_json["base_type"] = base_type_json;
186✔
207

208
        j["edges"].push_back(edge_json);
186✔
209
    }
186✔
210
}
56✔
211

212
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
53✔
213
    j["type"] = "block";
53✔
214
    j["element_id"] = block.element_id();
53✔
215

216
    j["debug_info"] = nlohmann::json::object();
53✔
217
    debug_info_to_json(j["debug_info"], block.debug_info());
53✔
218

219
    if (this->recurse_) {
53✔
220
        nlohmann::json dataflow_json;
53✔
221
        dataflow_to_json(dataflow_json, block.dataflow());
53✔
222
        j["dataflow"] = dataflow_json;
53✔
223
    }
53✔
224
}
53✔
225

226
void JSONSerializer::structured_loop_to_json(nlohmann::json& j, const structured_control_flow::StructuredLoop& for_node) {
171✔
227
    j["type"] = "for";
171✔
228
    // Backward compatibility (now encapsulated in sub_type)
229
    if (dynamic_cast<const structured_control_flow::Map*>(&for_node)) {
171✔
230
        j["type"] = "map";
64✔
231
    }
64✔
232

233
    j["element_id"] = for_node.element_id();
171✔
234
    j["debug_info"] = nlohmann::json::object();
171✔
235
    debug_info_to_json(j["debug_info"], for_node.debug_info());
171✔
236

237
    j["indvar"] = expression(for_node.indvar());
171✔
238
    j["init"] = expression(for_node.init());
171✔
239
    j["condition"] = expression(for_node.condition());
171✔
240
    j["update"] = expression(for_node.update());
171✔
241

242
    j["schedule_type"] = nlohmann::json::object();
171✔
243
    schedule_type_to_json(j["schedule_type"], for_node.schedule_type());
171✔
244

245
    j["sub_type"] = "for";
171✔
246
    if (dynamic_cast<const structured_control_flow::Map*>(&for_node)) {
171✔
247
        j["sub_type"] = "map";
64✔
248
    } else if (dynamic_cast<const structured_control_flow::Reduce*>(&for_node)) {
107✔
249
        j["sub_type"] = "reduce";
2✔
250
        j["reductions"] = nlohmann::json::array();
2✔
251
        const auto& reduce_node = dynamic_cast<const structured_control_flow::Reduce&>(for_node);
2✔
252
        for (const auto& reduction : reduce_node.reductions()) {
2✔
253
            nlohmann::json reduction_json;
2✔
254
            reduction_json["op"] = structured_control_flow::reduction_operation_to_string(reduction.operation);
2✔
255
            reduction_json["container"] = reduction.container;
2✔
256
            j["reductions"].push_back(reduction_json);
2✔
257
        }
2✔
258
    }
2✔
259

260
    if (this->recurse_) {
171✔
261
        nlohmann::json body_json;
31✔
262
        sequence_to_json(body_json, for_node.root());
31✔
263
        j["root"] = body_json;
31✔
264
    }
31✔
265
}
171✔
266

267
void JSONSerializer::if_else_to_json(nlohmann::json& j, const structured_control_flow::IfElse& if_else_node) {
2✔
268
    j["type"] = "if_else";
2✔
269
    j["element_id"] = if_else_node.element_id();
2✔
270

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

274
    j["branches"] = nlohmann::json::array();
2✔
275
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
276
        nlohmann::json branch_json;
4✔
277
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
278
        if (this->recurse_) {
4✔
279
            nlohmann::json body_json;
4✔
280
            sequence_to_json(body_json, if_else_node.at(i).first);
4✔
281
            branch_json["root"] = body_json;
4✔
282
        }
4✔
283
        j["branches"].push_back(branch_json);
4✔
284
    }
4✔
285
}
2✔
286

287
void JSONSerializer::while_node_to_json(nlohmann::json& j, const structured_control_flow::While& while_node) {
5✔
288
    j["type"] = "while";
5✔
289
    j["element_id"] = while_node.element_id();
5✔
290

291
    j["debug_info"] = nlohmann::json::object();
5✔
292
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
293

294
    if (this->recurse_) {
5✔
295
        nlohmann::json body_json;
5✔
296
        sequence_to_json(body_json, while_node.root());
5✔
297
        j["root"] = body_json;
5✔
298
    }
5✔
299
}
5✔
300

301
void JSONSerializer::break_node_to_json(nlohmann::json& j, const structured_control_flow::Break& break_node) {
2✔
302
    j["type"] = "break";
2✔
303
    j["element_id"] = break_node.element_id();
2✔
304

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

309
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
310
    j["type"] = "continue";
2✔
311
    j["element_id"] = continue_node.element_id();
2✔
312

313
    j["debug_info"] = nlohmann::json::object();
2✔
314
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
315
}
2✔
316

317
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
318
    j["type"] = "return";
2✔
319
    j["element_id"] = return_node.element_id();
2✔
320
    j["data"] = return_node.data();
2✔
321

322
    if (return_node.is_constant()) {
2✔
323
        nlohmann::json type_json;
×
324
        type_to_json(type_json, return_node.type());
×
325
        j["data_type"] = type_json;
×
326
    }
×
327

328
    j["debug_info"] = nlohmann::json::object();
2✔
329
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
330
}
2✔
331

332
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
71✔
333
    j["type"] = "sequence";
71✔
334
    j["element_id"] = sequence.element_id();
71✔
335

336
    j["debug_info"] = nlohmann::json::object();
71✔
337
    debug_info_to_json(j["debug_info"], sequence.debug_info());
71✔
338

339
    if (!this->recurse_) {
71✔
340
        return;
×
341
    }
×
342

343
    j["children"] = nlohmann::json::array();
71✔
344
    j["transitions"] = nlohmann::json::array();
71✔
345
    for (size_t i = 0; i < sequence.size(); i++) {
151✔
346
        nlohmann::json child_json;
80✔
347
        auto& child = sequence.at(i).first;
80✔
348
        auto& transition = sequence.at(i).second;
80✔
349

350
        this->serialize_node(child_json, child);
80✔
351
        j["children"].push_back(child_json);
80✔
352

353
        // Add transition information
354
        nlohmann::json transition_json;
80✔
355
        transition_json["type"] = "transition";
80✔
356
        transition_json["element_id"] = transition.element_id();
80✔
357

358
        transition_json["debug_info"] = nlohmann::json::object();
80✔
359
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
80✔
360

361
        transition_json["assignments"] = nlohmann::json::array();
80✔
362
        for (const auto& assignment : transition.assignments()) {
80✔
363
            nlohmann::json assignment_json;
3✔
364
            assignment_json["symbol"] = expression(assignment.first);
3✔
365
            assignment_json["expression"] = expression(assignment.second);
3✔
366
            transition_json["assignments"].push_back(assignment_json);
3✔
367
        }
3✔
368

369
        j["transitions"].push_back(transition_json);
80✔
370
    }
80✔
371
}
71✔
372

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

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

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

474

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

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

499

500
/*
501
 * * Deserialization logic
502
 */
503

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

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

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

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

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

534
    nlohmann::json& containers = j["containers"];
28✔
535

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

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

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

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

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

578
    builder.set_element_counter(element_counter);
28✔
579

580
    return builder.move();
28✔
581
}
28✔
582

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

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

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

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

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

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

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

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

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

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

693
        auto base_type = json_to_type(edge["base_type"]);
183✔
694

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

718
void JSONSerializer::json_to_sequence(
719
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
720
) {
63✔
721
    assert(j.contains("type"));
63✔
722
    assert(j["type"].is_string());
63✔
723
    assert(j.contains("children"));
63✔
724
    assert(j["children"].is_array());
63✔
725
    assert(j.contains("transitions"));
63✔
726
    assert(j["transitions"].is_array());
63✔
727
    assert(j["transitions"].size() == j["children"].size());
63✔
728

729
    sequence.element_id_ = j["element_id"];
63✔
730
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
63✔
731

732
    std::string type = j["type"];
63✔
733
    if (type == "sequence") {
63✔
734
        for (size_t i = 0; i < j["children"].size(); i++) {
132✔
735
            auto& child = j["children"][i];
69✔
736
            auto& transition = j["transitions"][i];
69✔
737
            assert(child.contains("type"));
69✔
738
            assert(child["type"].is_string());
69✔
739

740
            assert(transition.contains("type"));
69✔
741
            assert(transition["type"].is_string());
69✔
742
            assert(transition.contains("assignments"));
69✔
743
            assert(transition["assignments"].is_array());
69✔
744
            control_flow::Assignments assignments;
69✔
745
            for (const auto& assignment : transition["assignments"]) {
69✔
746
                assert(assignment.contains("symbol"));
2✔
747
                assert(assignment["symbol"].is_string());
2✔
748
                assert(assignment.contains("expression"));
2✔
749
                assert(assignment["expression"].is_string());
2✔
750
                auto expr = symbolic::parse(assignment["expression"].get<std::string>());
2✔
751
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
2✔
752
            }
2✔
753

754
            if (child["type"] == "block") {
69✔
755
                json_to_block_node(child, builder, sequence, assignments);
42✔
756
            } else if (child["type"] == "if_else") {
42✔
UNCOV
757
                json_to_if_else_node(child, builder, sequence, assignments);
×
758
            } else if (child["type"] == "while") {
27✔
759
                json_to_while_node(child, builder, sequence, assignments);
×
760
            } else if (child["type"] == "break") {
27✔
761
                json_to_break_node(child, builder, sequence, assignments);
1✔
762
            } else if (child["type"] == "continue") {
26✔
763
                json_to_continue_node(child, builder, sequence, assignments);
1✔
764
            } else if (child["type"] == "return") {
25✔
765
                json_to_return_node(child, builder, sequence, assignments);
×
766
            } else if (child["type"] == "for" || child["type"] == "map") {
25✔
767
                json_to_structured_loop_node(child, builder, sequence, assignments);
25✔
768
            } else if (child["type"] == "sequence") {
25✔
769
                auto& subseq = builder.add_sequence(sequence, assignments, json_to_debug_info(child["debug_info"]));
×
770
                json_to_sequence(child, builder, subseq);
×
771
            } else {
×
772
                throw std::runtime_error("Unknown child type");
×
773
            }
×
774

775
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
69✔
776
            sequence.at(i).second.element_id_ = transition["element_id"];
69✔
777
        }
69✔
778
    } else {
63✔
779
        throw std::runtime_error("expected sequence type");
×
780
    }
×
781
}
63✔
782

783
void JSONSerializer::json_to_block_node(
784
    const nlohmann::json& j,
785
    builder::StructuredSDFGBuilder& builder,
786
    structured_control_flow::Sequence& parent,
787
    control_flow::Assignments& assignments
788
) {
43✔
789
    assert(j.contains("type"));
43✔
790
    assert(j["type"].is_string());
43✔
791
    assert(j.contains("dataflow"));
43✔
792
    assert(j["dataflow"].is_object());
43✔
793
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
43✔
794
    block.element_id_ = j["element_id"];
43✔
795
    assert(j["dataflow"].contains("type"));
43✔
796
    assert(j["dataflow"]["type"].is_string());
43✔
797
    std::string type = j["dataflow"]["type"];
43✔
798
    if (type == "dataflow") {
43✔
799
        json_to_dataflow(j["dataflow"], builder, block);
43✔
800
    } else {
43✔
801
        throw std::runtime_error("Unknown dataflow type");
×
802
    }
×
803
}
43✔
804

805
void JSONSerializer::json_to_structured_loop_node(
806
    const nlohmann::json& j,
807
    builder::StructuredSDFGBuilder& builder,
808
    structured_control_flow::Sequence& parent,
809
    control_flow::Assignments& assignments
810
) {
28✔
811
    // Backward compatibility: if the type is "map"
812
    if (j["type"] == "map") {
28✔
813
        json_to_map_node(j, builder, parent, assignments);
7✔
814
        return;
7✔
815
    }
7✔
816
    // Sub types
817
    if (j.contains("sub_type")) {
21✔
818
        std::string sub_type = j["sub_type"];
21✔
819
        if (sub_type == "map") {
21✔
NEW
820
            json_to_map_node(j, builder, parent, assignments);
×
NEW
821
            return;
×
822
        } else if (sub_type == "reduce") {
21✔
823
            json_to_reduce_node(j, builder, parent, assignments);
1✔
824
            return;
1✔
825
        }
1✔
826
    }
21✔
827

828
    assert(j.contains("type"));
21✔
829
    assert(j["type"].is_string());
20✔
830
    assert(j.contains("indvar"));
20✔
831
    assert(j["indvar"].is_string());
20✔
832
    assert(j.contains("init"));
20✔
833
    assert(j["init"].is_string());
20✔
834
    assert(j.contains("condition"));
20✔
835
    assert(j["condition"].is_string());
20✔
836
    assert(j.contains("update"));
20✔
837
    assert(j["update"].is_string());
20✔
838
    assert(j.contains("root"));
20✔
839
    assert(j["root"].is_object());
20✔
840

841
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
20✔
842
    auto init = symbolic::parse(j["init"].get<std::string>());
20✔
843
    auto update = symbolic::parse(j["update"].get<std::string>());
20✔
844

845
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
20✔
846
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
20✔
847
    if (condition.is_null()) {
20✔
848
        throw InvalidSDFGException("For loop condition is not a boolean expression");
×
849
    }
×
850

851
    auto& for_node =
20✔
852
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
20✔
853
    for_node.element_id_ = j["element_id"];
20✔
854

855
    assert(j["root"].contains("type"));
20✔
856
    assert(j["root"]["type"].is_string());
20✔
857
    assert(j["root"]["type"] == "sequence");
20✔
858
    json_to_sequence(j["root"], builder, for_node.root());
20✔
859
}
20✔
860

861
void JSONSerializer::json_to_if_else_node(
862
    const nlohmann::json& j,
863
    builder::StructuredSDFGBuilder& builder,
864
    structured_control_flow::Sequence& parent,
865
    control_flow::Assignments& assignments
866
) {
1✔
867
    assert(j.contains("type"));
1✔
868
    assert(j["type"].is_string());
1✔
869
    assert(j["type"] == "if_else");
1✔
870
    assert(j.contains("branches"));
1✔
871
    assert(j["branches"].is_array());
1✔
872
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
873
    if_else_node.element_id_ = j["element_id"];
1✔
874
    for (const auto& branch : j["branches"]) {
2✔
875
        assert(branch.contains("condition"));
2✔
876
        assert(branch["condition"].is_string());
2✔
877
        assert(branch.contains("root"));
2✔
878
        assert(branch["root"].is_object());
2✔
879

880
        auto condition_expr = symbolic::parse(branch["condition"].get<std::string>());
2✔
881
        symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
2✔
882
        if (condition.is_null()) {
2✔
883
            throw InvalidSDFGException("If condition is not a boolean expression");
×
884
        }
×
885
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
886
        assert(branch["root"].contains("type"));
2✔
887
        assert(branch["root"]["type"].is_string());
2✔
888
        std::string type = branch["root"]["type"];
2✔
889
        if (type == "sequence") {
2✔
890
            json_to_sequence(branch["root"], builder, branch_node);
2✔
891
        } else {
2✔
892
            throw std::runtime_error("Unknown child type");
×
893
        }
×
894
    }
2✔
895
}
1✔
896

897
void JSONSerializer::json_to_while_node(
898
    const nlohmann::json& j,
899
    builder::StructuredSDFGBuilder& builder,
900
    structured_control_flow::Sequence& parent,
901
    control_flow::Assignments& assignments
902
) {
3✔
903
    assert(j.contains("type"));
3✔
904
    assert(j["type"].is_string());
3✔
905
    assert(j["type"] == "while");
3✔
906
    assert(j.contains("root"));
3✔
907
    assert(j["root"].is_object());
3✔
908

909
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
910
    while_node.element_id_ = j["element_id"];
3✔
911

912
    assert(j["root"]["type"] == "sequence");
3✔
913
    json_to_sequence(j["root"], builder, while_node.root());
3✔
914
}
3✔
915

916
void JSONSerializer::json_to_break_node(
917
    const nlohmann::json& j,
918
    builder::StructuredSDFGBuilder& builder,
919
    structured_control_flow::Sequence& parent,
920
    control_flow::Assignments& assignments
921
) {
1✔
922
    assert(j.contains("type"));
1✔
923
    assert(j["type"].is_string());
1✔
924
    assert(j["type"] == "break");
1✔
925
    auto& node = builder.add_break(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
926
    node.element_id_ = j["element_id"];
1✔
927
}
1✔
928

929
void JSONSerializer::json_to_continue_node(
930
    const nlohmann::json& j,
931
    builder::StructuredSDFGBuilder& builder,
932
    structured_control_flow::Sequence& parent,
933
    control_flow::Assignments& assignments
934
) {
1✔
935
    assert(j.contains("type"));
1✔
936
    assert(j["type"].is_string());
1✔
937
    assert(j["type"] == "continue");
1✔
938
    auto& node = builder.add_continue(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
939
    node.element_id_ = j["element_id"];
1✔
940
}
1✔
941

942
void JSONSerializer::json_to_map_node(
943
    const nlohmann::json& j,
944
    builder::StructuredSDFGBuilder& builder,
945
    structured_control_flow::Sequence& parent,
946
    control_flow::Assignments& assignments
947
) {
7✔
948
    assert(j.contains("type"));
7✔
949
    assert(j["type"].is_string());
7✔
950
    assert(j["type"] == "map");
7✔
951
    assert(j.contains("indvar"));
7✔
952
    assert(j["indvar"].is_string());
7✔
953
    assert(j.contains("init"));
7✔
954
    assert(j["init"].is_string());
7✔
955
    assert(j.contains("condition"));
7✔
956
    assert(j["condition"].is_string());
7✔
957
    assert(j.contains("update"));
7✔
958
    assert(j["update"].is_string());
7✔
959
    assert(j.contains("root"));
7✔
960
    assert(j["root"].is_object());
7✔
961
    assert(j.contains("schedule_type"));
7✔
962
    assert(j["schedule_type"].is_object());
7✔
963

964
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
7✔
965

966
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
7✔
967
    auto init = symbolic::parse(j["init"].get<std::string>());
7✔
968
    auto update = symbolic::parse(j["update"].get<std::string>());
7✔
969
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
7✔
970
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
7✔
971
    if (condition.is_null()) {
7✔
972
        throw InvalidSDFGException("Map condition is not a boolean expression");
×
973
    }
×
974

975
    auto& map_node = builder.add_map(
7✔
976
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
7✔
977
    );
7✔
978
    map_node.element_id_ = j["element_id"];
7✔
979

980
    assert(j["root"].contains("type"));
7✔
981
    assert(j["root"]["type"].is_string());
7✔
982
    assert(j["root"]["type"] == "sequence");
7✔
983
    json_to_sequence(j["root"], builder, map_node.root());
7✔
984
}
7✔
985

986
void JSONSerializer::json_to_reduce_node(
987
    const nlohmann::json& j,
988
    builder::StructuredSDFGBuilder& builder,
989
    structured_control_flow::Sequence& parent,
990
    control_flow::Assignments& assignments
991
) {
1✔
992
    assert(j.contains("type"));
1✔
993
    assert(j["type"].is_string());
1✔
994
    assert(j["type"] == "for");
1✔
995
    assert(j.contains("sub_type"));
1✔
996
    assert(j["sub_type"].is_string());
1✔
997
    assert(j["sub_type"] == "reduce");
1✔
998
    assert(j.contains("indvar"));
1✔
999
    assert(j["indvar"].is_string());
1✔
1000
    assert(j.contains("init"));
1✔
1001
    assert(j["init"].is_string());
1✔
1002
    assert(j.contains("condition"));
1✔
1003
    assert(j["condition"].is_string());
1✔
1004
    assert(j.contains("update"));
1✔
1005
    assert(j["update"].is_string());
1✔
1006
    assert(j.contains("root"));
1✔
1007
    assert(j["root"].is_object());
1✔
1008
    assert(j.contains("reductions"));
1✔
1009
    assert(j["reductions"].is_array());
1✔
1010
    assert(j.contains("schedule_type"));
1✔
1011
    assert(j["schedule_type"].is_object());
1✔
1012

1013
    std::vector<structured_control_flow::ReductionInfo> reductions;
1✔
1014
    for (const auto& reduction_json : j["reductions"]) {
1✔
1015
        assert(reduction_json.contains("op"));
1✔
1016
        assert(reduction_json["op"].is_string());
1✔
1017
        assert(reduction_json.contains("container"));
1✔
1018
        assert(reduction_json["container"].is_string());
1✔
1019
        reductions.push_back(structured_control_flow::ReductionInfo{
1✔
1020
            structured_control_flow::reduction_operation_from_string(reduction_json["op"].get<std::string>()),
1✔
1021
            reduction_json["container"].get<std::string>()
1✔
1022
        });
1✔
1023
    }
1✔
1024

1025
    structured_control_flow::ScheduleType schedule_type = json_to_schedule_type(j["schedule_type"]);
1✔
1026

1027
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
1028
    auto init = symbolic::parse(j["init"].get<std::string>());
1✔
1029
    auto update = symbolic::parse(j["update"].get<std::string>());
1✔
1030
    auto condition_expr = symbolic::parse(j["condition"].get<std::string>());
1✔
1031
    symbolic::Condition condition = SymEngine::rcp_dynamic_cast<const SymEngine::Boolean>(condition_expr);
1✔
1032
    if (condition.is_null()) {
1✔
NEW
1033
        throw InvalidSDFGException("Reduce condition is not a boolean expression");
×
NEW
1034
    }
×
1035

1036
    auto& reduce_node = builder.add_reduce(
1✔
1037
        parent,
1✔
1038
        indvar,
1✔
1039
        condition,
1✔
1040
        init,
1✔
1041
        update,
1✔
1042
        reductions,
1✔
1043
        schedule_type,
1✔
1044
        assignments,
1✔
1045
        json_to_debug_info(j["debug_info"])
1✔
1046
    );
1✔
1047
    reduce_node.element_id_ = j["element_id"];
1✔
1048

1049
    assert(j["root"].contains("type"));
1✔
1050
    assert(j["root"]["type"].is_string());
1✔
1051
    assert(j["root"]["type"] == "sequence");
1✔
1052
    json_to_sequence(j["root"], builder, reduce_node.root());
1✔
1053
}
1✔
1054

1055
void JSONSerializer::json_to_return_node(
1056
    const nlohmann::json& j,
1057
    builder::StructuredSDFGBuilder& builder,
1058
    structured_control_flow::Sequence& parent,
1059
    control_flow::Assignments& assignments
1060
) {
1✔
1061
    assert(j.contains("type"));
1✔
1062
    assert(j["type"].is_string());
1✔
1063
    assert(j["type"] == "return");
1✔
1064

1065
    std::string data = j["data"];
1✔
1066
    std::unique_ptr<types::IType> data_type = nullptr;
1✔
1067
    if (j.contains("data_type")) {
1✔
1068
        data_type = json_to_type(j["data_type"]);
×
1069
    }
×
1070

1071
    if (data_type == nullptr) {
1✔
1072
        auto& node = builder.add_return(parent, data, assignments, json_to_debug_info(j["debug_info"]));
1✔
1073
        node.element_id_ = j["element_id"];
1✔
1074
    } else {
1✔
1075
        auto& node =
×
1076
            builder.add_constant_return(parent, data, *data_type, assignments, json_to_debug_info(j["debug_info"]));
×
1077
        node.element_id_ = j["element_id"];
×
1078
    }
×
1079
}
1✔
1080

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

1207
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
600✔
1208
    assert(j.contains("has"));
600✔
1209
    assert(j["has"].is_boolean());
600✔
1210
    if (!j["has"]) {
600✔
1211
        return DebugInfo();
596✔
1212
    }
596✔
1213
    assert(j.contains("filename"));
600✔
1214
    assert(j["filename"].is_string());
4✔
1215
    std::string filename = j["filename"];
4✔
1216
    assert(j.contains("function"));
4✔
1217
    assert(j["function"].is_string());
4✔
1218
    std::string function = j["function"];
4✔
1219
    assert(j.contains("start_line"));
4✔
1220
    assert(j["start_line"].is_number_integer());
4✔
1221
    size_t start_line = j["start_line"];
4✔
1222
    assert(j.contains("start_column"));
4✔
1223
    assert(j["start_column"].is_number_integer());
4✔
1224
    size_t start_column = j["start_column"];
4✔
1225
    assert(j.contains("end_line"));
4✔
1226
    assert(j["end_line"].is_number_integer());
4✔
1227
    size_t end_line = j["end_line"];
4✔
1228
    assert(j.contains("end_column"));
4✔
1229
    assert(j["end_column"].is_number_integer());
4✔
1230
    size_t end_column = j["end_column"];
4✔
1231
    return DebugInfo(filename, function, start_line, start_column, end_line, end_column);
4✔
1232
}
4✔
1233

1234
ScheduleType JSONSerializer::json_to_schedule_type(const nlohmann::json& j) {
9✔
1235
    assert(j.contains("value"));
9✔
1236
    assert(j["value"].is_string());
9✔
1237
    // assert(j.contains("category"));
1238
    // assert(j["category"].is_number_integer());
1239
    assert(j.contains("properties"));
9✔
1240
    assert(j["properties"].is_object());
9✔
1241
    ScheduleTypeCategory category = ScheduleTypeCategory::None;
9✔
1242
    if (j.contains("category")) {
9✔
1243
        category = static_cast<ScheduleTypeCategory>(j["category"].get<int>());
9✔
1244
    }
9✔
1245
    ScheduleType schedule_type(j["value"].get<std::string>(), category);
9✔
1246
    for (const auto& [key, value] : j["properties"].items()) {
9✔
1247
        assert(value.is_string());
3✔
1248
        schedule_type.set_property(key, value.get<std::string>());
3✔
1249
    }
3✔
1250
    return schedule_type;
9✔
1251
}
9✔
1252

1253
types::StorageType JSONSerializer::json_to_storage_type(const nlohmann::json& j) {
526✔
1254
    if (!j.contains("value")) {
526✔
1255
        return types::StorageType::CPU_Stack();
×
1256
    }
×
1257
    std::string value = j["value"].get<std::string>();
526✔
1258

1259
    symbolic::Expression allocation_size = SymEngine::null;
526✔
1260
    if (j.contains("allocation_size")) {
526✔
1261
        allocation_size = symbolic::parse(j["allocation_size"].get<std::string>());
×
1262
    }
×
1263

1264
    types::StorageType::AllocationType allocation = j["allocation"];
526✔
1265
    types::StorageType::AllocationType deallocation = j["deallocation"];
526✔
1266

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

1269
    if (j.contains("args")) {
526✔
1270
        nlohmann::json::array_t args = j["args"];
×
1271
        if (args.size() > 0) {
×
1272
            storageType.arg1(symbolic::parse(args[0].get<std::string>()));
×
1273
        }
×
1274
    }
×
1275
    return storageType;
526✔
1276
}
526✔
1277

1278
std::string JSONSerializer::expression(const symbolic::Expression expr) {
922✔
1279
    JSONSymbolicPrinter printer;
922✔
1280
    return printer.apply(expr);
922✔
1281
};
922✔
1282

1283
symbolic::Expression JSONSerializer::json_to_expr(const nlohmann::json& j) {
3✔
1284
    return symbolic::parse(j.get<std::string>());
3✔
1285
}
3✔
1286

1287
void JSONSerializer::writeToFile(const StructuredSDFG& sdfg, const std::filesystem::path& file) {
×
1288
    JSONSerializer ser;
×
1289
    auto json = ser.serialize(sdfg);
×
1290

1291
    auto parent_path = file.parent_path();
×
1292
    if (!parent_path.empty()) {
×
1293
        std::filesystem::create_directories(file.parent_path());
×
1294
    }
×
1295

1296
    std::ofstream out(file, std::ofstream::out);
×
1297
    if (!out.is_open()) {
×
1298
        std::cerr << "Could not open file " << file << " for writing JSON output." << std::endl;
×
1299
    }
×
1300
    out << json << std::endl;
×
1301
    out.close();
×
1302
}
×
1303

1304
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
1305
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
1306
    str_ = parenthesize(str_);
×
1307
};
×
1308

1309
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1310
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1311
    str_ = parenthesize(str_);
×
1312
};
×
1313

1314
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
4✔
1315
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
4✔
1316
    str_ = parenthesize(str_);
4✔
1317
};
4✔
1318

1319
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
194✔
1320
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
194✔
1321
    str_ = parenthesize(str_);
194✔
1322
};
194✔
1323

1324
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
2✔
1325
    std::ostringstream s;
2✔
1326
    auto container = x.get_args();
2✔
1327
    if (container.size() == 1) {
2✔
1328
        s << apply(*container.begin());
×
1329
    } else {
2✔
1330
        s << "min(";
2✔
1331
        s << apply(*container.begin());
2✔
1332

1333
        // Recursively apply __daisy_min to the arguments
1334
        SymEngine::vec_basic subargs;
2✔
1335
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
4✔
1336
            subargs.push_back(*it);
2✔
1337
        }
2✔
1338
        auto submin = SymEngine::min(subargs);
2✔
1339
        s << ", " << apply(submin);
2✔
1340

1341
        s << ")";
2✔
1342
    }
2✔
1343

1344
    str_ = s.str();
2✔
1345
};
2✔
1346

1347
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
4✔
1348
    std::ostringstream s;
4✔
1349
    auto container = x.get_args();
4✔
1350
    if (container.size() == 1) {
4✔
1351
        s << apply(*container.begin());
×
1352
    } else {
4✔
1353
        s << "max(";
4✔
1354
        s << apply(*container.begin());
4✔
1355

1356
        // Recursively apply __daisy_max to the arguments
1357
        SymEngine::vec_basic subargs;
4✔
1358
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
8✔
1359
            subargs.push_back(*it);
4✔
1360
        }
4✔
1361
        auto submax = SymEngine::max(subargs);
4✔
1362
        s << ", " << apply(submax);
4✔
1363

1364
        s << ")";
4✔
1365
    }
4✔
1366

1367
    str_ = s.str();
4✔
1368
};
4✔
1369

1370
void LibraryNodeSerializerRegistry::
1371
    register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn) {
226✔
1372
    std::lock_guard<std::mutex> lock(mutex_);
226✔
1373
    if (factory_map_.find(library_node_code) != factory_map_.end()) {
226✔
1374
        return;
8✔
1375
    }
8✔
1376
    factory_map_[library_node_code] = std::move(fn);
218✔
1377
}
218✔
1378

1379
LibraryNodeSerializerFn LibraryNodeSerializerRegistry::get_library_node_serializer(std::string library_node_code) {
17✔
1380
    auto it = factory_map_.find(library_node_code);
17✔
1381
    if (it != factory_map_.end()) {
17✔
1382
        return it->second;
17✔
1383
    }
17✔
1384
    return nullptr;
×
1385
}
17✔
1386

1387
size_t LibraryNodeSerializerRegistry::size() const { return factory_map_.size(); }
×
1388

1389
void register_default_serializers() {
4✔
1390
    // stdlib
1391
    LibraryNodeSerializerRegistry::instance()
4✔
1392
        .register_library_node_serializer(stdlib::LibraryNodeType_Alloca.value(), []() {
4✔
1393
            return std::make_unique<stdlib::AllocaNodeSerializer>();
×
1394
        });
×
1395
    LibraryNodeSerializerRegistry::instance()
4✔
1396
        .register_library_node_serializer(stdlib::LibraryNodeType_Assert.value(), []() {
4✔
1397
            return std::make_unique<stdlib::AssertNodeSerializer>();
×
1398
        });
×
1399
    LibraryNodeSerializerRegistry::instance()
4✔
1400
        .register_library_node_serializer(stdlib::LibraryNodeType_Calloc.value(), []() {
4✔
1401
            return std::make_unique<stdlib::CallocNodeSerializer>();
×
1402
        });
×
1403
    LibraryNodeSerializerRegistry::instance()
4✔
1404
        .register_library_node_serializer(stdlib::LibraryNodeType_Free.value(), []() {
4✔
1405
            return std::make_unique<stdlib::FreeNodeSerializer>();
×
1406
        });
×
1407
    LibraryNodeSerializerRegistry::instance()
4✔
1408
        .register_library_node_serializer(stdlib::LibraryNodeType_Malloc.value(), []() {
4✔
1409
            return std::make_unique<stdlib::MallocNodeSerializer>();
×
1410
        });
×
1411
    LibraryNodeSerializerRegistry::instance()
4✔
1412
        .register_library_node_serializer(stdlib::LibraryNodeType_Memcpy.value(), []() {
4✔
1413
            return std::make_unique<stdlib::MemcpyNodeSerializer>();
×
1414
        });
×
1415
    LibraryNodeSerializerRegistry::instance()
4✔
1416
        .register_library_node_serializer(stdlib::LibraryNodeType_Memmove.value(), []() {
4✔
1417
            return std::make_unique<stdlib::MemmoveNodeSerializer>();
×
1418
        });
×
1419
    LibraryNodeSerializerRegistry::instance()
4✔
1420
        .register_library_node_serializer(stdlib::LibraryNodeType_Memset.value(), []() {
4✔
1421
            return std::make_unique<stdlib::MemsetNodeSerializer>();
×
1422
        });
×
1423
    LibraryNodeSerializerRegistry::instance()
4✔
1424
        .register_library_node_serializer(stdlib::LibraryNodeType_Trap.value(), []() {
4✔
1425
            return std::make_unique<stdlib::TrapNodeSerializer>();
×
1426
        });
×
1427
    LibraryNodeSerializerRegistry::instance()
4✔
1428
        .register_library_node_serializer(stdlib::LibraryNodeType_Unreachable.value(), []() {
4✔
1429
            return std::make_unique<stdlib::UnreachableNodeSerializer>();
×
1430
        });
×
1431

1432
    // Metadata
1433
    LibraryNodeSerializerRegistry::instance()
4✔
1434
        .register_library_node_serializer(data_flow::LibraryNodeType_Metadata.value(), []() {
4✔
1435
            return std::make_unique<data_flow::MetadataNodeSerializer>();
×
1436
        });
×
1437

1438
    // Barrier
1439
    LibraryNodeSerializerRegistry::instance()
4✔
1440
        .register_library_node_serializer(data_flow::LibraryNodeType_BarrierLocal.value(), []() {
4✔
1441
            return std::make_unique<data_flow::BarrierLocalNodeSerializer>();
1✔
1442
        });
1✔
1443

1444
    // Call Node
1445
    LibraryNodeSerializerRegistry::instance()
4✔
1446
        .register_library_node_serializer(data_flow::LibraryNodeType_Call.value(), []() {
6✔
1447
            return std::make_unique<data_flow::CallNodeSerializer>();
6✔
1448
        });
6✔
1449
    LibraryNodeSerializerRegistry::instance()
4✔
1450
        .register_library_node_serializer(data_flow::LibraryNodeType_Invoke.value(), []() {
4✔
1451
            return std::make_unique<data_flow::InvokeNodeSerializer>();
×
1452
        });
×
1453

1454
    // LoadConst
1455
    LibraryNodeSerializerRegistry::instance()
4✔
1456
        .register_library_node_serializer(data_flow::LibraryNodeType_LoadConst.value(), []() {
4✔
1457
            return std::make_unique<data_flow::LoadConstNodeSerializer>();
2✔
1458
        });
2✔
1459

1460
    // CMath
1461
    LibraryNodeSerializerRegistry::instance()
4✔
1462
        .register_library_node_serializer(math::cmath::LibraryNodeType_CMath.value(), []() {
4✔
1463
            return std::make_unique<math::cmath::CMathNodeSerializer>();
×
1464
        });
×
1465
    // Backward compatibility
1466
    LibraryNodeSerializerRegistry::instance()
4✔
1467
        .register_library_node_serializer(math::cmath::LibraryNodeType_CMath_Deprecated.value(), []() {
4✔
1468
            return std::make_unique<math::cmath::CMathNodeSerializer>();
×
1469
        });
×
1470

1471
    // BLAS
1472
    LibraryNodeSerializerRegistry::instance()
4✔
1473
        .register_library_node_serializer(math::blas::LibraryNodeType_DOT.value(), []() {
4✔
1474
            return std::make_unique<math::blas::DotNodeSerializer>();
×
1475
        });
×
1476
    LibraryNodeSerializerRegistry::instance()
4✔
1477
        .register_library_node_serializer(math::blas::LibraryNodeType_GEMM.value(), []() {
4✔
1478
            return std::make_unique<math::blas::GEMMNodeSerializer>();
×
1479
        });
×
1480
    LibraryNodeSerializerRegistry::instance()
4✔
1481
        .register_library_node_serializer(math::blas::LibraryNodeType_BatchedGEMM.value(), []() {
4✔
1482
            return std::make_unique<math::blas::BatchedGEMMNodeSerializer>();
×
1483
        });
×
1484

1485
    // Tensor
1486

1487
    LibraryNodeSerializerRegistry::instance()
4✔
1488
        .register_library_node_serializer(math::tensor::LibraryNodeType_Broadcast.value(), []() {
4✔
1489
            return std::make_unique<math::tensor::BroadcastNodeSerializer>();
×
1490
        });
×
1491
    LibraryNodeSerializerRegistry::instance()
4✔
1492
        .register_library_node_serializer(math::tensor::LibraryNodeType_Conv.value(), []() {
4✔
1493
            return std::make_unique<math::tensor::ConvNodeSerializer>();
×
1494
        });
×
1495
    LibraryNodeSerializerRegistry::instance()
4✔
1496
        .register_library_node_serializer(math::tensor::LibraryNodeType_Pooling.value(), []() {
4✔
1497
            return std::make_unique<math::tensor::PoolingNodeSerializer>();
×
1498
        });
×
1499
    LibraryNodeSerializerRegistry::instance()
4✔
1500
        .register_library_node_serializer(math::tensor::LibraryNodeType_Transpose.value(), []() {
4✔
1501
            return std::make_unique<math::tensor::TransposeNodeSerializer>();
×
1502
        });
×
1503
    LibraryNodeSerializerRegistry::instance()
4✔
1504
        .register_library_node_serializer(math::tensor::LibraryNodeType_MatMul.value(), []() {
4✔
1505
            return std::make_unique<math::tensor::MatMulNodeSerializer>();
×
1506
        });
×
1507

1508
    // Elementwise
1509
    LibraryNodeSerializerRegistry::instance()
4✔
1510
        .register_library_node_serializer(math::tensor::LibraryNodeType_Abs.value(), []() {
4✔
1511
            return std::make_unique<math::tensor::AbsNodeSerializer>();
×
1512
        });
×
1513
    LibraryNodeSerializerRegistry::instance()
4✔
1514
        .register_library_node_serializer(math::tensor::LibraryNodeType_Add.value(), []() {
4✔
1515
            return std::make_unique<math::tensor::AddNodeSerializer>();
×
1516
        });
×
1517
    LibraryNodeSerializerRegistry::instance()
4✔
1518
        .register_library_node_serializer(math::tensor::LibraryNodeType_Div.value(), []() {
4✔
1519
            return std::make_unique<math::tensor::DivNodeSerializer>();
×
1520
        });
×
1521
    LibraryNodeSerializerRegistry::instance()
4✔
1522
        .register_library_node_serializer(math::tensor::LibraryNodeType_Elu.value(), []() {
4✔
1523
            return std::make_unique<math::tensor::EluNodeSerializer>();
×
1524
        });
×
1525
    LibraryNodeSerializerRegistry::instance()
4✔
1526
        .register_library_node_serializer(math::tensor::LibraryNodeType_Exp.value(), []() {
4✔
1527
            return std::make_unique<math::tensor::ExpNodeSerializer>();
×
1528
        });
×
1529
    LibraryNodeSerializerRegistry::instance()
4✔
1530
        .register_library_node_serializer(math::tensor::LibraryNodeType_Erf.value(), []() {
4✔
1531
            return std::make_unique<math::tensor::ErfNodeSerializer>();
×
1532
        });
×
1533
    LibraryNodeSerializerRegistry::instance()
4✔
1534
        .register_library_node_serializer(math::tensor::LibraryNodeType_HardSigmoid.value(), []() {
4✔
1535
            return std::make_unique<math::tensor::HardSigmoidNodeSerializer>();
×
1536
        });
×
1537
    LibraryNodeSerializerRegistry::instance()
4✔
1538
        .register_library_node_serializer(math::tensor::LibraryNodeType_LeakyReLU.value(), []() {
4✔
1539
            return std::make_unique<math::tensor::LeakyReLUNodeSerializer>();
×
1540
        });
×
1541
    LibraryNodeSerializerRegistry::instance()
4✔
1542
        .register_library_node_serializer(math::tensor::LibraryNodeType_Mul.value(), []() {
4✔
1543
            return std::make_unique<math::tensor::MulNodeSerializer>();
×
1544
        });
×
1545
    LibraryNodeSerializerRegistry::instance()
4✔
1546
        .register_library_node_serializer(math::tensor::LibraryNodeType_Pow.value(), []() {
4✔
1547
            return std::make_unique<math::tensor::PowNodeSerializer>();
×
1548
        });
×
1549
    LibraryNodeSerializerRegistry::instance()
4✔
1550
        .register_library_node_serializer(math::tensor::LibraryNodeType_ReLU.value(), []() {
4✔
1551
            return std::make_unique<math::tensor::ReLUNodeSerializer>();
×
1552
        });
×
1553
    LibraryNodeSerializerRegistry::instance()
4✔
1554
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sigmoid.value(), []() {
4✔
1555
            return std::make_unique<math::tensor::SigmoidNodeSerializer>();
×
1556
        });
×
1557
    LibraryNodeSerializerRegistry::instance()
4✔
1558
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sqrt.value(), []() {
4✔
1559
            return std::make_unique<math::tensor::SqrtNodeSerializer>();
×
1560
        });
×
1561
    LibraryNodeSerializerRegistry::instance()
4✔
1562
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sub.value(), []() {
4✔
1563
            return std::make_unique<math::tensor::SubNodeSerializer>();
×
1564
        });
×
1565
    LibraryNodeSerializerRegistry::instance()
4✔
1566
        .register_library_node_serializer(math::tensor::LibraryNodeType_Tanh.value(), []() {
4✔
1567
            return std::make_unique<math::tensor::TanhNodeSerializer>();
×
1568
        });
×
1569
    LibraryNodeSerializerRegistry::instance()
4✔
1570
        .register_library_node_serializer(math::tensor::LibraryNodeType_Minimum.value(), []() {
4✔
1571
            return std::make_unique<math::tensor::MinimumNodeSerializer>();
×
1572
        });
×
1573
    LibraryNodeSerializerRegistry::instance()
4✔
1574
        .register_library_node_serializer(math::tensor::LibraryNodeType_Maximum.value(), []() {
4✔
1575
            return std::make_unique<math::tensor::MaximumNodeSerializer>();
×
1576
        });
×
1577
    LibraryNodeSerializerRegistry::instance()
4✔
1578
        .register_library_node_serializer(math::tensor::LibraryNodeType_Fill.value(), []() {
4✔
1579
            return std::make_unique<math::tensor::FillNodeSerializer>();
×
1580
        });
×
1581
    LibraryNodeSerializerRegistry::instance()
4✔
1582
        .register_library_node_serializer(math::tensor::LibraryNodeType_TensorTasklet.value(), []() {
4✔
1583
            return std::make_unique<math::tensor::TaskletTensorNodeSerializer>();
×
1584
        });
×
1585
    LibraryNodeSerializerRegistry::instance()
4✔
1586
        .register_library_node_serializer(math::tensor::LibraryNodeType_TensorCMath.value(), []() {
4✔
1587
            return std::make_unique<math::tensor::CMathTensorNodeSerializer>();
×
1588
        });
×
1589
    LibraryNodeSerializerRegistry::instance()
4✔
1590
        .register_library_node_serializer(math::tensor::LibraryNodeType_Cast.value(), []() {
4✔
1591
            return std::make_unique<math::tensor::CastNodeSerializer>();
×
1592
        });
×
1593

1594
    LibraryNodeSerializerRegistry::instance()
4✔
1595
        .register_library_node_serializer(math::tensor::LibraryNodeType_BatchNorm.value(), [] {
4✔
1596
            return std::make_unique<math::tensor::BatchNormNodeSerializer>();
×
1597
        });
×
1598

1599
    // Reduce
1600
    LibraryNodeSerializerRegistry::instance()
4✔
1601
        .register_library_node_serializer(math::tensor::LibraryNodeType_Sum.value(), []() {
4✔
1602
            return std::make_unique<math::tensor::SumNodeSerializer>();
×
1603
        });
×
1604
    LibraryNodeSerializerRegistry::instance()
4✔
1605
        .register_library_node_serializer(math::tensor::LibraryNodeType_Max.value(), []() {
4✔
1606
            return std::make_unique<math::tensor::MaxNodeSerializer>();
×
1607
        });
×
1608
    LibraryNodeSerializerRegistry::instance()
4✔
1609
        .register_library_node_serializer(math::tensor::LibraryNodeType_Min.value(), []() {
4✔
1610
            return std::make_unique<math::tensor::MinNodeSerializer>();
×
1611
        });
×
1612
    LibraryNodeSerializerRegistry::instance()
4✔
1613
        .register_library_node_serializer(math::tensor::LibraryNodeType_Softmax.value(), []() {
4✔
1614
            return std::make_unique<math::tensor::SoftmaxNodeSerializer>();
×
1615
        });
×
1616
    LibraryNodeSerializerRegistry::instance()
4✔
1617
        .register_library_node_serializer(math::tensor::LibraryNodeType_Mean.value(), []() {
4✔
1618
            return std::make_unique<math::tensor::MeanNodeSerializer>();
×
1619
        });
×
1620
    LibraryNodeSerializerRegistry::instance()
4✔
1621
        .register_library_node_serializer(math::tensor::LibraryNodeType_Std.value(), []() {
4✔
1622
            return std::make_unique<math::tensor::StdNodeSerializer>();
×
1623
        });
×
1624

1625
    // Einsum
1626
    LibraryNodeSerializerRegistry::instance()
4✔
1627
        .register_library_node_serializer(math::tensor::LibraryNodeType_Einsum.value(), []() {
4✔
1628
            return std::make_unique<math::tensor::EinsumSerializer>();
×
1629
        });
×
1630
}
4✔
1631

1632
} // namespace serializer
1633
} // 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