-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Deserialization.cpp
1616 lines (1475 loc) · 78.1 KB
/
Deserialization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Deserialization.h"
#ifdef WITH_SERIALIZATION
#include "FindCalls.h"
#include "Func.h"
#include "Function.h"
#include "IR.h"
#include "Schedule.h"
#include "halide_ir.fbs.h"
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
namespace Halide {
namespace Internal {
class Deserializer {
public:
Deserializer() = default;
explicit Deserializer(const std::map<std::string, Parameter> &user_params)
: user_params(user_params) {
}
// Deserialize a pipeline from the given filename
Pipeline deserialize(const std::string &filename);
// Deserialize a pipeline from the given input stream
Pipeline deserialize(std::istream &in);
// Deserialize a pipeline from the given buffer of bytes
Pipeline deserialize(const std::vector<uint8_t> &data);
// Deserialize just the unbound external parameters that need to be defined for the pipeline from the given filename
// (so they can be remapped and overridden with user parameters prior to deserializing the pipeline)
std::map<std::string, Parameter> deserialize_parameters(const std::string &filename);
// Deserialize just the unbound external parameters that need to be defined for the pipeline from the given input stream
std::map<std::string, Parameter> deserialize_parameters(std::istream &in);
// Deserialize just the unbound external parameters that need to be defined for the pipeline from the given buffer of bytes
std::map<std::string, Parameter> deserialize_parameters(const std::vector<uint8_t> &data);
private:
// Helper function to deserialize a homogenous vector from a flatbuffer vector,
// does not apply to union types like Stmt and Expr or enum types like MemoryType
template<typename src, typename dst>
std::vector<dst> deserialize_vector(const flatbuffers::Vector<::flatbuffers::Offset<src>> *flatbuffer_vec,
std::function<dst(Deserializer &, const src *)> deserialize_func) {
user_assert(flatbuffer_vec != nullptr) << "deserializing a null vector\n";
std::vector<dst> result;
result.reserve(flatbuffer_vec->size());
for (const auto &elem : *flatbuffer_vec) {
result.push_back(deserialize_func(*this, elem));
}
return result;
}
// A lookup table for translating function ids to actual FunctionPtrs
std::map<int32_t, FunctionPtr> reverse_function_mappings;
// A lookup table for finding parameters object via their names,
// used for preventing the same parameter being deserialized multiple times
std::map<std::string, Parameter> parameters_in_pipeline;
// A lookup table for finding buffer object via their names,
// used for preventing the same buffer being deserialized multiple times
std::map<std::string, Buffer<>> buffers_in_pipeline;
// External parameters that are not deserialized but will be used in the pipeline
std::map<std::string, Parameter> user_params;
// Default external parameters that were created during deserialization
std::map<std::string, Parameter> external_params;
MemoryType deserialize_memory_type(Serialize::MemoryType memory_type);
ForType deserialize_for_type(Serialize::ForType for_type);
DeviceAPI deserialize_device_api(Serialize::DeviceAPI device_api);
Partition deserialize_partition(Serialize::Partition partition);
Call::CallType deserialize_call_type(Serialize::CallType call_type);
VectorReduce::Operator deserialize_vector_reduce_op(Serialize::VectorReduceOp vector_reduce_op);
PrefetchBoundStrategy deserialize_prefetch_bound_strategy(Serialize::PrefetchBoundStrategy prefetch_bound_strategy);
NameMangling deserialize_name_mangling(Serialize::NameMangling name_mangling);
TailStrategy deserialize_tail_strategy(Serialize::TailStrategy tail_strategy);
Split::SplitType deserialize_split_type(Serialize::SplitType split_type);
DimType deserialize_dim_type(Serialize::DimType dim_type);
LoopAlignStrategy deserialize_loop_align_strategy(Serialize::LoopAlignStrategy loop_align_strategy);
ExternFuncArgument::ArgType deserialize_extern_func_argument_type(Serialize::ExternFuncArgumentType extern_func_argument_type);
std::string deserialize_string(const flatbuffers::String *str);
Type deserialize_type(const Serialize::Type *type);
void deserialize_function(const Serialize::Func *function, Function &hl_function);
Stmt deserialize_stmt(Serialize::Stmt type_code, const void *stmt);
Expr deserialize_expr(Serialize::Expr type_code, const void *expr);
std::vector<Expr> deserialize_expr_vector(const flatbuffers::Vector<Serialize::Expr> *exprs_types, const flatbuffers::Vector<flatbuffers::Offset<void>> *exprs_serialized);
Range deserialize_range(const Serialize::Range *range);
Bound deserialize_bound(const Serialize::Bound *bound);
StorageDim deserialize_storage_dim(const Serialize::StorageDim *storage_dim);
LoopLevel deserialize_loop_level(const Serialize::LoopLevel *loop_level);
FuncSchedule deserialize_func_schedule(const Serialize::FuncSchedule *func_schedule);
Specialization deserialize_specialization(const Serialize::Specialization *specialization);
Definition deserialize_definition(const Serialize::Definition *definition);
ReductionVariable deserialize_reduction_variable(const Serialize::ReductionVariable *reduction_variable);
ReductionDomain deserialize_reduction_domain(const Serialize::ReductionDomain *reduction_domain);
ModulusRemainder deserialize_modulus_remainder(const Serialize::ModulusRemainder *modulus_remainder);
PrefetchDirective deserialize_prefetch_directive(const Serialize::PrefetchDirective *prefetch_directive);
Split deserialize_split(const Serialize::Split *split);
Dim deserialize_dim(const Serialize::Dim *dim);
FuseLoopLevel deserialize_fuse_loop_level(const Serialize::FuseLoopLevel *fuse_loop_level);
FusedPair deserialize_fused_pair(const Serialize::FusedPair *fused_pair);
StageSchedule deserialize_stage_schedule(const Serialize::StageSchedule *stage_schedule);
BufferConstraint deserialize_buffer_constraint(const Serialize::BufferConstraint *buffer_constraint);
Parameter deserialize_parameter(const Serialize::Parameter *parameter);
Parameter deserialize_external_parameter(const Serialize::ExternalParameter *external_parameter);
ExternFuncArgument deserialize_extern_func_argument(const Serialize::ExternFuncArgument *extern_func_argument);
std::map<std::string, FunctionPtr> deserialize_wrapper_refs(const flatbuffers::Vector<flatbuffers::Offset<Serialize::WrapperRef>> *wrappers);
Buffer<> deserialize_buffer(const Serialize::Buffer *buffer);
void build_reverse_function_mappings(const std::vector<Function> &functions);
};
std::string Deserializer::deserialize_string(const flatbuffers::String *str) {
user_assert(str != nullptr) << "deserializing a null string\n";
return str->str();
}
MemoryType Deserializer::deserialize_memory_type(Serialize::MemoryType memory_type) {
switch (memory_type) {
case Serialize::MemoryType::Auto:
return MemoryType::Auto;
case Serialize::MemoryType::Heap:
return MemoryType::Heap;
case Serialize::MemoryType::Stack:
return MemoryType::Stack;
case Serialize::MemoryType::Register:
return MemoryType::Register;
case Serialize::MemoryType::GPUShared:
return MemoryType::GPUShared;
case Serialize::MemoryType::GPUTexture:
return MemoryType::GPUTexture;
case Serialize::MemoryType::LockedCache:
return MemoryType::LockedCache;
case Serialize::MemoryType::VTCM:
return MemoryType::VTCM;
case Serialize::MemoryType::AMXTile:
return MemoryType::AMXTile;
default:
user_error << "unknown memory type " << (int)memory_type << "\n";
return MemoryType::Auto;
}
}
ForType Deserializer::deserialize_for_type(Serialize::ForType for_type) {
switch (for_type) {
case Serialize::ForType::Serial:
return ForType::Serial;
case Serialize::ForType::Parallel:
return ForType::Parallel;
case Serialize::ForType::Vectorized:
return ForType::Vectorized;
case Serialize::ForType::Unrolled:
return ForType::Unrolled;
case Serialize::ForType::Extern:
return ForType::Extern;
case Serialize::ForType::GPUBlock:
return ForType::GPUBlock;
case Serialize::ForType::GPUThread:
return ForType::GPUThread;
case Serialize::ForType::GPULane:
return ForType::GPULane;
default:
user_error << "unknown for type " << (int)for_type << "\n";
return ForType::Serial;
}
}
Partition Deserializer::deserialize_partition(Serialize::Partition partition) {
switch (partition) {
case Serialize::Partition::Auto:
return Halide::Partition::Auto;
case Serialize::Partition::Never:
return Halide::Partition::Never;
case Serialize::Partition::Always:
return Halide::Partition::Always;
default:
user_error << "unknown loop partition policy " << (int)partition << "\n";
return Halide::Partition::Auto;
}
}
DeviceAPI Deserializer::deserialize_device_api(Serialize::DeviceAPI device_api) {
switch (device_api) {
case Serialize::DeviceAPI::None:
return DeviceAPI::None;
case Serialize::DeviceAPI::Host:
return DeviceAPI::Host;
case Serialize::DeviceAPI::Default_GPU:
return DeviceAPI::Default_GPU;
case Serialize::DeviceAPI::CUDA:
return DeviceAPI::CUDA;
case Serialize::DeviceAPI::OpenCL:
return DeviceAPI::OpenCL;
case Serialize::DeviceAPI::Metal:
return DeviceAPI::Metal;
case Serialize::DeviceAPI::Hexagon:
return DeviceAPI::Hexagon;
case Serialize::DeviceAPI::HexagonDma:
return DeviceAPI::HexagonDma;
case Serialize::DeviceAPI::D3D12Compute:
return DeviceAPI::D3D12Compute;
case Serialize::DeviceAPI::Vulkan:
return DeviceAPI::Vulkan;
case Serialize::DeviceAPI::WebGPU:
return DeviceAPI::WebGPU;
default:
user_error << "unknown device api " << (int)device_api << "\n";
return DeviceAPI::None;
}
}
Call::CallType Deserializer::deserialize_call_type(Serialize::CallType call_type) {
switch (call_type) {
case Serialize::CallType::Image:
return Call::CallType::Image;
case Serialize::CallType::Extern:
return Call::CallType::Extern;
case Serialize::CallType::ExternCPlusPlus:
return Call::CallType::ExternCPlusPlus;
case Serialize::CallType::PureExtern:
return Call::CallType::PureExtern;
case Serialize::CallType::Halide:
return Call::CallType::Halide;
case Serialize::CallType::Intrinsic:
return Call::CallType::Intrinsic;
case Serialize::CallType::PureIntrinsic:
return Call::CallType::PureIntrinsic;
default:
user_error << "unknown call type " << (int)call_type << "\n";
return Call::CallType::Image;
}
}
VectorReduce::Operator Deserializer::deserialize_vector_reduce_op(Serialize::VectorReduceOp vector_reduce_op) {
switch (vector_reduce_op) {
case Serialize::VectorReduceOp::Add:
return VectorReduce::Operator::Add;
case Serialize::VectorReduceOp::SaturatingAdd:
return VectorReduce::Operator::SaturatingAdd;
case Serialize::VectorReduceOp::Mul:
return VectorReduce::Operator::Mul;
case Serialize::VectorReduceOp::Min:
return VectorReduce::Operator::Min;
case Serialize::VectorReduceOp::Max:
return VectorReduce::Operator::Max;
case Serialize::VectorReduceOp::And:
return VectorReduce::Operator::And;
case Serialize::VectorReduceOp::Or:
return VectorReduce::Operator::Or;
default:
user_error << "unknown vector reduce op " << (int)vector_reduce_op << "\n";
return VectorReduce::Operator::Add;
}
}
PrefetchBoundStrategy Deserializer::deserialize_prefetch_bound_strategy(Serialize::PrefetchBoundStrategy prefetch_bound_strategy) {
switch (prefetch_bound_strategy) {
case Serialize::PrefetchBoundStrategy::Clamp:
return PrefetchBoundStrategy::Clamp;
case Serialize::PrefetchBoundStrategy::GuardWithIf:
return PrefetchBoundStrategy::GuardWithIf;
case Serialize::PrefetchBoundStrategy::NonFaulting:
return PrefetchBoundStrategy::NonFaulting;
default:
user_error << "unknown prefetch bound strategy " << (int)prefetch_bound_strategy << "\n";
return PrefetchBoundStrategy::Clamp;
}
}
NameMangling Deserializer::deserialize_name_mangling(Serialize::NameMangling name_mangling) {
switch (name_mangling) {
case Serialize::NameMangling::Default:
return NameMangling::Default;
case Serialize::NameMangling::C:
return NameMangling::C;
case Serialize::NameMangling::CPlusPlus:
return NameMangling::CPlusPlus;
default:
user_error << "unknown name mangling " << (int)name_mangling << "\n";
return NameMangling::Default;
}
}
TailStrategy Deserializer::deserialize_tail_strategy(Serialize::TailStrategy tail_strategy) {
switch (tail_strategy) {
case Serialize::TailStrategy::RoundUp:
return TailStrategy::RoundUp;
case Serialize::TailStrategy::GuardWithIf:
return TailStrategy::GuardWithIf;
case Serialize::TailStrategy::Predicate:
return TailStrategy::Predicate;
case Serialize::TailStrategy::PredicateLoads:
return TailStrategy::PredicateLoads;
case Serialize::TailStrategy::PredicateStores:
return TailStrategy::PredicateStores;
case Serialize::TailStrategy::ShiftInwards:
return TailStrategy::ShiftInwards;
case Serialize::TailStrategy::ShiftInwardsAndBlend:
return TailStrategy::ShiftInwardsAndBlend;
case Serialize::TailStrategy::RoundUpAndBlend:
return TailStrategy::RoundUpAndBlend;
case Serialize::TailStrategy::Auto:
return TailStrategy::Auto;
default:
user_error << "unknown tail strategy " << (int)tail_strategy << "\n";
return TailStrategy::RoundUp;
}
}
Split::SplitType Deserializer::deserialize_split_type(Serialize::SplitType split_type) {
switch (split_type) {
case Serialize::SplitType::SplitVar:
return Split::SplitType::SplitVar;
case Serialize::SplitType::RenameVar:
return Split::SplitType::RenameVar;
case Serialize::SplitType::FuseVars:
return Split::SplitType::FuseVars;
case Serialize::SplitType::PurifyRVar:
return Split::SplitType::PurifyRVar;
default:
user_error << "unknown split type " << (int)split_type << "\n";
return Split::SplitType::SplitVar;
}
}
DimType Deserializer::deserialize_dim_type(Serialize::DimType dim_type) {
switch (dim_type) {
case Serialize::DimType::PureVar:
return DimType::PureVar;
case Serialize::DimType::PureRVar:
return DimType::PureRVar;
case Serialize::DimType::ImpureRVar:
return DimType::ImpureRVar;
default:
user_error << "unknown dim type " << (int)dim_type << "\n";
return DimType::PureVar;
}
}
LoopAlignStrategy Deserializer::deserialize_loop_align_strategy(Serialize::LoopAlignStrategy loop_align_strategy) {
switch (loop_align_strategy) {
case Serialize::LoopAlignStrategy::AlignStart:
return LoopAlignStrategy::AlignStart;
case Serialize::LoopAlignStrategy::AlignEnd:
return LoopAlignStrategy::AlignEnd;
case Serialize::LoopAlignStrategy::NoAlign:
return LoopAlignStrategy::NoAlign;
case Serialize::LoopAlignStrategy::Auto:
return LoopAlignStrategy::Auto;
default:
user_error << "unknown loop align strategy " << (int)loop_align_strategy << "\n";
return LoopAlignStrategy::AlignStart;
}
}
ExternFuncArgument::ArgType Deserializer::deserialize_extern_func_argument_type(Serialize::ExternFuncArgumentType extern_func_argument_type) {
switch (extern_func_argument_type) {
case Serialize::ExternFuncArgumentType::UndefinedArg:
return ExternFuncArgument::ArgType::UndefinedArg;
case Serialize::ExternFuncArgumentType::FuncArg:
return ExternFuncArgument::ArgType::FuncArg;
case Serialize::ExternFuncArgumentType::BufferArg:
return ExternFuncArgument::ArgType::BufferArg;
case Serialize::ExternFuncArgumentType::ExprArg:
return ExternFuncArgument::ArgType::ExprArg;
case Serialize::ExternFuncArgumentType::ImageParamArg:
return ExternFuncArgument::ArgType::ImageParamArg;
default:
user_error << "unknown extern func argument type " << (int)extern_func_argument_type << "\n";
return ExternFuncArgument::ArgType::UndefinedArg;
}
}
Type Deserializer::deserialize_type(const Serialize::Type *type) {
user_assert(type != nullptr) << "deserializing a null Type\n";
using Serialize::TypeCode;
const int bits = type->bits();
const int lanes = type->lanes();
const TypeCode code_deserialized = type->code();
halide_type_code_t code = halide_type_uint;
switch (code_deserialized) {
case TypeCode::Int:
code = halide_type_int;
break;
case TypeCode::UInt:
code = halide_type_uint;
break;
case TypeCode::Float:
code = halide_type_float;
break;
case TypeCode::Handle:
code = halide_type_handle;
break;
case TypeCode::BFloat:
code = halide_type_bfloat;
break;
default:
user_error << "unknown type code " << (int)code_deserialized << "\n";
}
return Type(code, bits, lanes);
}
void Deserializer::deserialize_function(const Serialize::Func *function, Function &hl_function) {
user_assert(function != nullptr) << "deserializing a null Function\n";
const std::string name = deserialize_string(function->name());
const std::string origin_name = deserialize_string(function->origin_name());
const std::vector<Type> output_types =
deserialize_vector<Serialize::Type, Type>(function->output_types(),
&Deserializer::deserialize_type);
const std::vector<Type> required_types =
deserialize_vector<Serialize::Type, Type>(function->required_types(),
&Deserializer::deserialize_type);
const int required_dim = function->required_dims();
const std::vector<std::string> args =
deserialize_vector<flatbuffers::String, std::string>(function->args(),
&Deserializer::deserialize_string);
const auto func_schedule = deserialize_func_schedule(function->func_schedule());
const auto init_def = deserialize_definition(function->init_def());
const std::vector<Definition> updates =
deserialize_vector<Serialize::Definition, Definition>(function->updates(),
&Deserializer::deserialize_definition);
const std::string debug_file = deserialize_string(function->debug_file());
std::vector<Parameter> output_buffers;
output_buffers.reserve(function->output_buffers_names()->size());
for (const auto &output_buffer_name_serialized : *function->output_buffers_names()) {
auto output_buffer_name = deserialize_string(output_buffer_name_serialized);
Parameter output_buffer;
if (auto it = user_params.find(output_buffer_name); it != user_params.end()) {
output_buffer = it->second;
} else if (auto it = external_params.find(output_buffer_name); it != external_params.end()) {
output_buffer = it->second;
} else if (auto it = parameters_in_pipeline.find(output_buffer_name); it != parameters_in_pipeline.end()) {
output_buffer = it->second;
} else if (!output_buffer_name.empty()) {
user_error << "unknown output buffer used in pipeline '" << output_buffer_name << "'\n";
}
output_buffers.push_back(output_buffer);
}
const std::vector<ExternFuncArgument> extern_arguments =
deserialize_vector<Serialize::ExternFuncArgument, ExternFuncArgument>(function->extern_arguments(),
&Deserializer::deserialize_extern_func_argument);
const std::string extern_function_name = deserialize_string(function->extern_function_name());
const auto name_mangling = deserialize_name_mangling(function->extern_mangling());
const auto extern_function_device_api = deserialize_device_api(function->extern_function_device_api());
const auto extern_proxy_expr = deserialize_expr(function->extern_proxy_expr_type(), function->extern_proxy_expr());
const bool trace_loads = function->trace_loads();
const bool trace_stores = function->trace_stores();
const bool trace_realizations = function->trace_realizations();
const std::vector<std::string> trace_tags =
deserialize_vector<flatbuffers::String, std::string>(function->trace_tags(),
&Deserializer::deserialize_string);
const bool frozen = function->frozen();
hl_function.update_with_deserialization(name, origin_name, output_types, required_types,
required_dim, args, func_schedule, init_def, updates,
debug_file, output_buffers, extern_arguments, extern_function_name,
name_mangling, extern_function_device_api, extern_proxy_expr,
trace_loads, trace_stores, trace_realizations, trace_tags, frozen);
}
Stmt Deserializer::deserialize_stmt(Serialize::Stmt type_code, const void *stmt) {
user_assert(stmt != nullptr) << "deserializing a null Stmt\n";
switch (type_code) {
case Serialize::Stmt::LetStmt: {
const auto *let_stmt = (const Serialize::LetStmt *)stmt;
const auto name = deserialize_string(let_stmt->name());
const auto value = deserialize_expr(let_stmt->value_type(), let_stmt->value());
const auto body = deserialize_stmt(let_stmt->body_type(), let_stmt->body());
return LetStmt::make(name, value, body);
}
case Serialize::Stmt::AssertStmt: {
const auto *assert_stmt = (const Serialize::AssertStmt *)stmt;
const auto condition = deserialize_expr(assert_stmt->condition_type(), assert_stmt->condition());
const auto message = deserialize_expr(assert_stmt->message_type(), assert_stmt->message());
return AssertStmt::make(condition, message);
}
case Serialize::Stmt::ProducerConsumer: {
const auto *producer_consumer = (const Serialize::ProducerConsumer *)stmt;
const auto name = deserialize_string(producer_consumer->name());
const auto is_producer = producer_consumer->is_producer();
const auto body = deserialize_stmt(producer_consumer->body_type(), producer_consumer->body());
return ProducerConsumer::make(name, is_producer, body);
}
case Serialize::Stmt::For: {
const auto *for_stmt = (const Serialize::For *)stmt;
const auto name = deserialize_string(for_stmt->name());
const auto min = deserialize_expr(for_stmt->min_type(), for_stmt->min());
const auto extent = deserialize_expr(for_stmt->extent_type(), for_stmt->extent());
const ForType for_type = deserialize_for_type(for_stmt->for_type());
const Partition partition_policy = deserialize_partition(for_stmt->partition_policy());
const DeviceAPI device_api = deserialize_device_api(for_stmt->device_api());
const auto body = deserialize_stmt(for_stmt->body_type(), for_stmt->body());
return For::make(name, min, extent, for_type, partition_policy, device_api, body);
}
case Serialize::Stmt::Store: {
const auto *store_stmt = (const Serialize::Store *)stmt;
const auto name = deserialize_string(store_stmt->name());
const auto predicate = deserialize_expr(store_stmt->predicate_type(), store_stmt->predicate());
const auto value = deserialize_expr(store_stmt->value_type(), store_stmt->value());
const auto index = deserialize_expr(store_stmt->index_type(), store_stmt->index());
const auto param_name = deserialize_string(store_stmt->param_name());
Parameter param;
if (auto it = user_params.find(param_name); it != user_params.end()) {
param = it->second;
} else if (auto it = external_params.find(param_name); it != external_params.end()) {
param = it->second;
} else if (auto it = parameters_in_pipeline.find(param_name); it != parameters_in_pipeline.end()) {
param = it->second;
} else if (!param_name.empty()) {
user_error << "unknown parameter used in pipeline '" << param_name << "'\n";
}
const auto alignment = deserialize_modulus_remainder(store_stmt->alignment());
return Store::make(name, value, index, param, predicate, alignment);
}
case Serialize::Stmt::Provide: {
const auto *provide_stmt = (const Serialize::Provide *)stmt;
const auto name = deserialize_string(provide_stmt->name());
const std::vector<Expr> values = deserialize_expr_vector(provide_stmt->values_type(), provide_stmt->values());
const std::vector<Expr> args = deserialize_expr_vector(provide_stmt->args_type(), provide_stmt->args());
const auto predicate = deserialize_expr(provide_stmt->predicate_type(), provide_stmt->predicate());
return Provide::make(name, values, args, predicate);
}
case Serialize::Stmt::Allocate: {
const auto *allocate_stmt = (const Serialize::Allocate *)stmt;
const auto name = deserialize_string(allocate_stmt->name());
const auto type = deserialize_type(allocate_stmt->type());
const MemoryType memory_type = deserialize_memory_type(allocate_stmt->memory_type());
const std::vector<Expr> extents = deserialize_expr_vector(allocate_stmt->extents_type(), allocate_stmt->extents());
const auto condition = deserialize_expr(allocate_stmt->condition_type(), allocate_stmt->condition());
const auto new_expr = deserialize_expr(allocate_stmt->new_expr_type(), allocate_stmt->new_expr());
const auto free_function = deserialize_string(allocate_stmt->free_function());
const auto padding = allocate_stmt->padding();
const auto body = deserialize_stmt(allocate_stmt->body_type(), allocate_stmt->body());
return Allocate::make(name, type, memory_type, extents, condition, body, new_expr, free_function, padding);
}
case Serialize::Stmt::Free: {
const auto *free_stmt = (const Serialize::Free *)stmt;
const auto name = deserialize_string(free_stmt->name());
return Free::make(name);
}
case Serialize::Stmt::Realize: {
const auto *realize_stmt = (const Serialize::Realize *)stmt;
const auto name = deserialize_string(realize_stmt->name());
const std::vector<Type> types = deserialize_vector<Serialize::Type, Type>(realize_stmt->types(),
&Deserializer::deserialize_type);
const MemoryType memory_type = deserialize_memory_type(realize_stmt->memory_type());
const std::vector<Range> bounds = deserialize_vector<Serialize::Range, Range>(realize_stmt->bounds(),
&Deserializer::deserialize_range);
const auto condition = deserialize_expr(realize_stmt->condition_type(), realize_stmt->condition());
const auto body = deserialize_stmt(realize_stmt->body_type(), realize_stmt->body());
return Realize::make(name, types, memory_type, bounds, condition, body);
}
case Serialize::Stmt::Block: {
const auto *block_stmt = (const Serialize::Block *)stmt;
const auto first = deserialize_stmt(block_stmt->first_type(), block_stmt->first());
const auto rest = deserialize_stmt(block_stmt->rest_type(), block_stmt->rest());
return Block::make(first, rest);
}
case Serialize::Stmt::IfThenElse: {
const auto *if_then_else_stmt = (const Serialize::IfThenElse *)stmt;
const auto condition = deserialize_expr(if_then_else_stmt->condition_type(), if_then_else_stmt->condition());
const auto then_case = deserialize_stmt(if_then_else_stmt->then_case_type(), if_then_else_stmt->then_case());
const auto else_case = deserialize_stmt(if_then_else_stmt->else_case_type(), if_then_else_stmt->else_case());
return IfThenElse::make(condition, then_case, else_case);
}
case Serialize::Stmt::Evaluate: {
const auto *evaluate_stmt = (const Serialize::Evaluate *)stmt;
const auto value = deserialize_expr(evaluate_stmt->value_type(), evaluate_stmt->value());
return Evaluate::make(value);
}
case Serialize::Stmt::Prefetch: {
const auto *prefetch_stmt = (const Serialize::Prefetch *)stmt;
const auto name = deserialize_string(prefetch_stmt->name());
const std::vector<Type> types = deserialize_vector<Serialize::Type, Type>(prefetch_stmt->types(),
&Deserializer::deserialize_type);
const std::vector<Range> bounds = deserialize_vector<Serialize::Range, Range>(prefetch_stmt->bounds(),
&Deserializer::deserialize_range);
const auto prefetch = deserialize_prefetch_directive(prefetch_stmt->prefetch());
const auto condition = deserialize_expr(prefetch_stmt->condition_type(), prefetch_stmt->condition());
const auto body = deserialize_stmt(prefetch_stmt->body_type(), prefetch_stmt->body());
return Prefetch::make(name, types, bounds, prefetch, condition, body);
}
case Serialize::Stmt::Acquire: {
const auto *acquire_stmt = (const Serialize::Acquire *)stmt;
const auto semaphore = deserialize_expr(acquire_stmt->semaphore_type(), acquire_stmt->semaphore());
const auto count = deserialize_expr(acquire_stmt->count_type(), acquire_stmt->count());
const auto body = deserialize_stmt(acquire_stmt->body_type(), acquire_stmt->body());
return Acquire::make(semaphore, count, body);
}
case Serialize::Stmt::Fork: {
const auto *fork_stmt = (const Serialize::Fork *)stmt;
const auto first = deserialize_stmt(fork_stmt->first_type(), fork_stmt->first());
const auto rest = deserialize_stmt(fork_stmt->rest_type(), fork_stmt->rest());
return Fork::make(first, rest);
}
case Serialize::Stmt::Atomic: {
const auto *atomic_stmt = (const Serialize::Atomic *)stmt;
const auto producer_name = deserialize_string(atomic_stmt->producer_name());
const auto mutex_name = deserialize_string(atomic_stmt->mutex_name());
const auto body = deserialize_stmt(atomic_stmt->body_type(), atomic_stmt->body());
return Atomic::make(producer_name, mutex_name, body);
}
case Serialize::Stmt::HoistedStorage: {
const auto *hoisted_storage_stmt = (const Serialize::HoistedStorage *)stmt;
const auto name = deserialize_string(hoisted_storage_stmt->name());
const auto body = deserialize_stmt(hoisted_storage_stmt->body_type(), hoisted_storage_stmt->body());
return HoistedStorage::make(name, body);
}
case Serialize::Stmt::UndefinedStmt: {
return Stmt();
}
default:
user_error << "unknown type code " << (int)type_code << "\n";
return Stmt();
}
}
Expr Deserializer::deserialize_expr(Serialize::Expr type_code, const void *expr) {
user_assert(expr != nullptr);
switch (type_code) {
case Serialize::Expr::IntImm: {
const auto *int_imm_expr = (const Serialize::IntImm *)expr;
const auto value = int_imm_expr->value();
const auto type = deserialize_type(int_imm_expr->type());
return IntImm::make(type, value);
}
case Serialize::Expr::UIntImm: {
const auto *uint_imm_expr = (const Serialize::UIntImm *)expr;
const auto value = uint_imm_expr->value();
const auto type = deserialize_type(uint_imm_expr->type());
return UIntImm::make(type, value);
}
case Serialize::Expr::FloatImm: {
const auto *float_imm_expr = (const Serialize::FloatImm *)expr;
const auto value = float_imm_expr->value();
const auto type = deserialize_type(float_imm_expr->type());
return FloatImm::make(type, value);
}
case Serialize::Expr::StringImm: {
const auto *string_imm_expr = (const Serialize::StringImm *)expr;
const auto value = deserialize_string(string_imm_expr->value());
return StringImm::make(value);
}
case Serialize::Expr::Cast: {
const auto *cast_expr = (const Serialize::Cast *)expr;
const auto value = deserialize_expr(cast_expr->value_type(), cast_expr->value());
const auto type = deserialize_type(cast_expr->type());
return Cast::make(type, value);
}
case Serialize::Expr::Reinterpret: {
const auto *reinterpret_expr = (const Serialize::Reinterpret *)expr;
const auto value = deserialize_expr(reinterpret_expr->value_type(), reinterpret_expr->value());
const auto type = deserialize_type(reinterpret_expr->type());
return Reinterpret::make(type, value);
}
case Serialize::Expr::Add: {
const auto *add_expr = (const Serialize::Add *)expr;
const auto a = deserialize_expr(add_expr->a_type(), add_expr->a());
const auto b = deserialize_expr(add_expr->b_type(), add_expr->b());
return Add::make(a, b);
}
case Serialize::Expr::Sub: {
const auto *sub_expr = (const Serialize::Sub *)expr;
const auto a = deserialize_expr(sub_expr->a_type(), sub_expr->a());
const auto b = deserialize_expr(sub_expr->b_type(), sub_expr->b());
return Sub::make(a, b);
}
case Serialize::Expr::Mul: {
const auto *mul_expr = (const Serialize::Mul *)expr;
const auto a = deserialize_expr(mul_expr->a_type(), mul_expr->a());
const auto b = deserialize_expr(mul_expr->b_type(), mul_expr->b());
return Mul::make(a, b);
}
case Serialize::Expr::Div: {
const auto *div_expr = (const Serialize::Div *)expr;
const auto a = deserialize_expr(div_expr->a_type(), div_expr->a());
const auto b = deserialize_expr(div_expr->b_type(), div_expr->b());
return Div::make(a, b);
}
case Serialize::Expr::Mod: {
const auto *mod_expr = (const Serialize::Mod *)expr;
const auto a = deserialize_expr(mod_expr->a_type(), mod_expr->a());
const auto b = deserialize_expr(mod_expr->b_type(), mod_expr->b());
return Mod::make(a, b);
}
case Serialize::Expr::Min: {
const auto *min_expr = (const Serialize::Min *)expr;
const auto a = deserialize_expr(min_expr->a_type(), min_expr->a());
const auto b = deserialize_expr(min_expr->b_type(), min_expr->b());
return Min::make(a, b);
}
case Serialize::Expr::Max: {
const auto *max_expr = (const Serialize::Max *)expr;
const auto a = deserialize_expr(max_expr->a_type(), max_expr->a());
const auto b = deserialize_expr(max_expr->b_type(), max_expr->b());
return Max::make(a, b);
}
case Serialize::Expr::EQ: {
const auto *eq_expr = (const Serialize::EQ *)expr;
const auto a = deserialize_expr(eq_expr->a_type(), eq_expr->a());
const auto b = deserialize_expr(eq_expr->b_type(), eq_expr->b());
return EQ::make(a, b);
}
case Serialize::Expr::NE: {
const auto *ne_expr = (const Serialize::NE *)expr;
const auto a = deserialize_expr(ne_expr->a_type(), ne_expr->a());
const auto b = deserialize_expr(ne_expr->b_type(), ne_expr->b());
return NE::make(a, b);
}
case Serialize::Expr::LT: {
const Serialize::LT *lt_expr = (const Serialize::LT *)expr;
const auto a = deserialize_expr(lt_expr->a_type(), lt_expr->a());
const auto b = deserialize_expr(lt_expr->b_type(), lt_expr->b());
return LT::make(a, b);
}
case Serialize::Expr::LE: {
const auto *le_expr = (const Serialize::LE *)expr;
const auto a = deserialize_expr(le_expr->a_type(), le_expr->a());
const auto b = deserialize_expr(le_expr->b_type(), le_expr->b());
return LE::make(a, b);
}
case Serialize::Expr::GT: {
const auto *gt_expr = (const Serialize::GT *)expr;
const auto a = deserialize_expr(gt_expr->a_type(), gt_expr->a());
const auto b = deserialize_expr(gt_expr->b_type(), gt_expr->b());
return GT::make(a, b);
}
case Serialize::Expr::GE: {
const auto *ge_expr = (const Serialize::GE *)expr;
const auto a = deserialize_expr(ge_expr->a_type(), ge_expr->a());
const auto b = deserialize_expr(ge_expr->b_type(), ge_expr->b());
return GE::make(a, b);
}
case Serialize::Expr::And: {
const auto *and_expr = (const Serialize::And *)expr;
const auto a = deserialize_expr(and_expr->a_type(), and_expr->a());
const auto b = deserialize_expr(and_expr->b_type(), and_expr->b());
return And::make(a, b);
}
case Serialize::Expr::Or: {
const auto *or_expr = (const Serialize::Or *)expr;
const auto a = deserialize_expr(or_expr->a_type(), or_expr->a());
const auto b = deserialize_expr(or_expr->b_type(), or_expr->b());
return Or::make(a, b);
}
case Serialize::Expr::Not: {
const auto *not_expr = (const Serialize::Not *)expr;
const auto a = deserialize_expr(not_expr->a_type(), not_expr->a());
return Not::make(a);
}
case Serialize::Expr::Select: {
const auto *select_expr = (const Serialize::Select *)expr;
const auto condition = deserialize_expr(select_expr->condition_type(), select_expr->condition());
const auto true_value = deserialize_expr(select_expr->true_value_type(), select_expr->true_value());
const auto false_value = deserialize_expr(select_expr->false_value_type(), select_expr->false_value());
return Select::make(condition, true_value, false_value);
}
case Serialize::Expr::Load: {
const auto *load_expr = (const Serialize::Load *)expr;
const auto name = deserialize_string(load_expr->name());
const auto predicate = deserialize_expr(load_expr->predicate_type(), load_expr->predicate());
const auto index = deserialize_expr(load_expr->index_type(), load_expr->index());
Buffer<> image;
const auto image_name = deserialize_string(load_expr->image_name());
if (auto it = buffers_in_pipeline.find(image_name); it != buffers_in_pipeline.end()) {
image = it->second;
}
const auto param_name = deserialize_string(load_expr->param_name());
Parameter param;
if (auto it = user_params.find(param_name); it != user_params.end()) {
param = it->second;
} else if (auto it = external_params.find(param_name); it != external_params.end()) {
param = it->second;
} else if (auto it = parameters_in_pipeline.find(param_name); it != parameters_in_pipeline.end()) {
param = it->second;
} else if (!param_name.empty()) {
user_error << "unknown parameter used in pipeline '" << param_name << "'\n";
}
const auto alignment = deserialize_modulus_remainder(load_expr->alignment());
const auto type = deserialize_type(load_expr->type());
return Load::make(type, name, index, image, param, predicate, alignment);
}
case Serialize::Expr::Ramp: {
const auto *ramp_expr = (const Serialize::Ramp *)expr;
const auto base = deserialize_expr(ramp_expr->base_type(), ramp_expr->base());
const auto stride = deserialize_expr(ramp_expr->stride_type(), ramp_expr->stride());
const auto lanes = ramp_expr->lanes();
return Ramp::make(base, stride, lanes);
}
case Serialize::Expr::Broadcast: {
const auto *broadcast_expr = (const Serialize::Broadcast *)expr;
const auto value = deserialize_expr(broadcast_expr->value_type(), broadcast_expr->value());
const auto lanes = broadcast_expr->lanes();
return Broadcast::make(value, lanes);
}
case Serialize::Expr::Let: {
const auto *let_expr = (const Serialize::Let *)expr;
auto name = deserialize_string(let_expr->name());
auto value = deserialize_expr(let_expr->value_type(), let_expr->value());
auto body = deserialize_expr(let_expr->body_type(), let_expr->body());
return Let::make(name, value, body);
}
case Serialize::Expr::Call: {
const auto *call_expr = (const Serialize::Call *)expr;
const auto name = deserialize_string(call_expr->name());
std::vector<Expr> args = deserialize_expr_vector(call_expr->args_type(), call_expr->args());
const auto value_index = call_expr->value_index();
const int func_index = call_expr->func_index();
FunctionPtr func_ptr;
if (auto it = this->reverse_function_mappings.find(func_index); it != this->reverse_function_mappings.end() && func_index != -1) {
FunctionPtr called_func_ptr = it->second;
func_ptr.weak = called_func_ptr.group();
func_ptr.idx = called_func_ptr.idx;
}
const auto call_type = deserialize_call_type(call_expr->call_type());
const auto image_name = deserialize_string(call_expr->image_name());
Buffer<> image;
if (auto it = buffers_in_pipeline.find(image_name); it != buffers_in_pipeline.end()) {
image = it->second;
}
const auto param_name = deserialize_string(call_expr->param_name());
Parameter param;
if (auto it = user_params.find(param_name); it != user_params.end()) {
param = it->second;
} else if (auto it = external_params.find(param_name); it != external_params.end()) {
param = it->second;
} else if (auto it = parameters_in_pipeline.find(param_name); it != parameters_in_pipeline.end()) {
param = it->second;
} else if (!param_name.empty()) {
user_error << "unknown parameter used in pipeline '" << param_name << "'\n";
}
const auto type = deserialize_type(call_expr->type());
return Call::make(type, name, args, call_type, func_ptr, value_index, image, param);
}
case Serialize::Expr::Variable: {
const auto *variable_expr = (const Serialize::Variable *)expr;
const auto name = deserialize_string(variable_expr->name());
const auto type = deserialize_type(variable_expr->type());
const auto param_name = deserialize_string(variable_expr->param_name());
Parameter param;
if (auto it = user_params.find(param_name); it != user_params.end()) {
param = it->second;
} else if (auto it = external_params.find(param_name); it != external_params.end()) {
param = it->second;
} else if (auto it = parameters_in_pipeline.find(param_name); it != parameters_in_pipeline.end()) {
param = it->second;
} else if (!param_name.empty()) {
user_error << "unknown parameter used in pipeline '" << param_name << "'\n";
}
auto image_name = deserialize_string(variable_expr->image_name());
Buffer<> image;
if (auto it = buffers_in_pipeline.find(image_name); it != buffers_in_pipeline.end()) {
image = it->second;
}
const auto reduction_domain = deserialize_reduction_domain(variable_expr->reduction_domain());
return Variable::make(type, name, image, param, reduction_domain);
}
case Serialize::Expr::Shuffle: {
const auto *shuffle_expr = (const Serialize::Shuffle *)expr;
std::vector<Expr> vectors = deserialize_expr_vector(shuffle_expr->vectors_type(), shuffle_expr->vectors());
const auto *const indices_serialized = shuffle_expr->indices();
std::vector<int32_t> indices;
indices.reserve(indices_serialized->size());
for (size_t i = 0; i < indices_serialized->size(); ++i) {
indices.push_back(indices_serialized->Get(i));
}
return Shuffle::make(vectors, indices);
}
case Serialize::Expr::VectorReduce: {
const auto *vector_reduce_expr = (const Serialize::VectorReduce *)expr;
const auto value = deserialize_expr(vector_reduce_expr->value_type(), vector_reduce_expr->value());
const auto reduction_op = deserialize_vector_reduce_op(vector_reduce_expr->reduction_op());
const int32_t lanes = vector_reduce_expr->lanes();
return VectorReduce::make(reduction_op, value, lanes);
}
case Serialize::Expr::UndefinedExpr: {
return Expr();
}
default: {
user_error << "unknown type code " << (int)type_code << "\n";
return Expr();
}
}
}
std::vector<Expr> Deserializer::deserialize_expr_vector(const flatbuffers::Vector<Serialize::Expr> *exprs_types,
const flatbuffers::Vector<flatbuffers::Offset<void>> *exprs_serialized) {
user_assert(exprs_types != nullptr);
user_assert(exprs_serialized != nullptr);
std::vector<Expr> result;
result.reserve(exprs_serialized->size());
for (size_t i = 0; i < exprs_serialized->size(); ++i) {
auto expr = deserialize_expr(static_cast<Serialize::Expr>(exprs_types->Get(i)), exprs_serialized->Get(i));
result.push_back(expr);
}
return result;
}
Range Deserializer::deserialize_range(const Serialize::Range *range) {
user_assert(range != nullptr);
const auto min = deserialize_expr(range->min_type(), range->min());
const auto extent = deserialize_expr(range->extent_type(), range->extent());
return Range(min, extent);
}
Bound Deserializer::deserialize_bound(const Serialize::Bound *bound) {
user_assert(bound != nullptr);
const auto var = deserialize_string(bound->var());
const auto min = deserialize_expr(bound->min_type(), bound->min());
const auto extent = deserialize_expr(bound->extent_type(), bound->extent());
const auto modulus = deserialize_expr(bound->modulus_type(), bound->modulus());
const auto remainder = deserialize_expr(bound->remainder_type(), bound->remainder());
auto hl_bound = Bound();
hl_bound.var = var;
hl_bound.min = min;
hl_bound.extent = extent;
hl_bound.modulus = modulus;
hl_bound.remainder = remainder;
return hl_bound;
}
StorageDim Deserializer::deserialize_storage_dim(const Serialize::StorageDim *storage_dim) {
user_assert(storage_dim != nullptr);
const auto var = deserialize_string(storage_dim->var());
const auto alignment = deserialize_expr(storage_dim->alignment_type(), storage_dim->alignment());
const auto bound = deserialize_expr(storage_dim->bound_type(), storage_dim->bound());
const auto fold_factor = deserialize_expr(storage_dim->fold_factor_type(), storage_dim->fold_factor());
const auto fold_forward = storage_dim->fold_forward();
auto hl_storage_dim = StorageDim();
hl_storage_dim.var = var;
hl_storage_dim.alignment = alignment;
hl_storage_dim.bound = bound;
hl_storage_dim.fold_factor = fold_factor;
hl_storage_dim.fold_forward = fold_forward;
return hl_storage_dim;
}
LoopLevel Deserializer::deserialize_loop_level(const Serialize::LoopLevel *loop_level) {
user_assert(loop_level != nullptr);
const auto func_name = deserialize_string(loop_level->func_name());
const auto stage_index = loop_level->stage_index();
const auto var_name = deserialize_string(loop_level->var_name());
const auto is_rvar = loop_level->is_rvar();
const auto locked = loop_level->locked();
return LoopLevel(func_name, var_name, is_rvar, stage_index, locked);
}