-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathFunction.cpp
More file actions
1471 lines (1269 loc) · 49.4 KB
/
Function.cpp
File metadata and controls
1471 lines (1269 loc) · 49.4 KB
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 <atomic>
#include <cstdlib>
#include <memory>
#include <set>
#include <utility>
#include "CSE.h"
#include "Func.h"
#include "Function.h"
#include "IR.h"
#include "IREquality.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "ParallelRVar.h"
#include "Random.h"
#include "Scope.h"
#include "Var.h"
namespace Halide {
namespace Internal {
using std::map;
using std::pair;
using std::string;
using std::vector;
typedef map<FunctionPtr, FunctionPtr> DeepCopyMap;
struct FunctionContents;
namespace {
// Weaken all the references to a particular Function to break
// reference cycles. Also count the number of references found.
class WeakenFunctionPtrs : public IRMutator {
using IRMutator::visit;
Expr visit(const Call *c) override {
Expr expr = IRMutator::visit(c);
c = expr.as<Call>();
internal_assert(c);
if (c->func.defined() &&
c->func.get() == func) {
FunctionPtr ptr = c->func;
ptr.weaken();
expr = Call::make(c->type, c->name, c->args, c->call_type,
ptr, c->value_index,
c->image, c->param);
count++;
}
return expr;
}
FunctionContents *func;
public:
int count = 0;
WeakenFunctionPtrs(FunctionContents *f)
: func(f) {
}
};
} // namespace
struct FunctionContents {
std::string name;
std::string origin_name;
std::vector<Type> output_types;
/** Optional type constraints on the Function:
* - If empty, there are no constraints.
* - If size == 1, the Func is only allowed to have values of Expr with that type
* - If size > 1, the Func is only allowed to have values of Tuple with those types
*
* Note that when this is nonempty, then output_types should match
* required_types for all defined Functions.
*/
std::vector<Type> required_types;
/** Optional dimension constraints on the Function:
* - If required_dims == AnyDims, there are no constraints.
* - Otherwise, the Function's dimensionality must exactly match required_dims.
*/
int required_dims = AnyDims;
// The names of the dimensions of the Function. Corresponds to the
// LHS of the pure definition if there is one. Is also the initial
// stage of the dims and storage_dims. Used to identify dimensions
// of the Function by name.
std::vector<string> args;
// Function-specific schedule. This schedule is applied to all stages
// within the function.
FuncSchedule func_schedule;
Definition init_def;
std::vector<Definition> updates;
std::string debug_file;
std::vector<Parameter> output_buffers;
std::vector<ExternFuncArgument> extern_arguments;
std::string extern_function_name;
NameMangling extern_mangling = NameMangling::Default;
DeviceAPI extern_function_device_api = DeviceAPI::Host;
Expr extern_proxy_expr;
bool trace_loads = false, trace_stores = false, trace_realizations = false;
std::vector<string> trace_tags;
bool no_profiling = false;
bool frozen = false;
void accept(IRVisitor *visitor) const {
func_schedule.accept(visitor);
if (init_def.defined()) {
init_def.accept(visitor);
}
for (const Definition &def : updates) {
def.accept(visitor);
}
if (!extern_function_name.empty()) {
for (const ExternFuncArgument &i : extern_arguments) {
if (i.is_func()) {
user_assert(i.func.get() != this)
<< "Extern Func has itself as an argument";
i.func->accept(visitor);
} else if (i.is_expr()) {
i.expr.accept(visitor);
}
}
if (extern_proxy_expr.defined()) {
extern_proxy_expr.accept(visitor);
}
}
for (const Parameter &i : output_buffers) {
for (size_t j = 0; j < args.size(); j++) {
if (i.min_constraint(j).defined()) {
i.min_constraint(j).accept(visitor);
}
if (i.stride_constraint(j).defined()) {
i.stride_constraint(j).accept(visitor);
}
if (i.extent_constraint(j).defined()) {
i.extent_constraint(j).accept(visitor);
}
}
}
}
// Pass an IRMutator through to all Exprs referenced in the FunctionContents
void mutate(IRMutator *mutator) {
func_schedule.mutate(mutator);
if (init_def.defined()) {
init_def.mutate(mutator);
}
for (Definition &def : updates) {
def.mutate(mutator);
}
if (!extern_function_name.empty()) {
for (ExternFuncArgument &i : extern_arguments) {
if (i.is_expr()) {
i.expr = mutator->mutate(i.expr);
}
}
extern_proxy_expr = mutator->mutate(extern_proxy_expr);
}
}
};
struct FunctionGroup {
mutable RefCount ref_count;
vector<FunctionContents> members;
};
FunctionContents *FunctionPtr::get() const {
return &(group()->members[idx]);
}
template<>
RefCount &ref_count<FunctionGroup>(const FunctionGroup *f) noexcept {
return f->ref_count;
}
template<>
void destroy<FunctionGroup>(const FunctionGroup *f) {
delete f;
}
namespace {
// All variables present in any part of a function definition must
// either be pure args, elements of the reduction domain, parameters
// (i.e. attached to some Parameter object), or part of a let node
// internal to the expression
struct CheckVars : public IRGraphVisitor {
vector<string> pure_args;
ReductionDomain reduction_domain;
Scope<> defined_internally;
const std::string name;
bool unbound_reduction_vars_ok = false;
bool pure;
CheckVars(const std::string &n, bool pure)
: name(n), pure(pure) {
}
using IRVisitor::visit;
void visit(const Let *let) override {
let->value.accept(this);
ScopedBinding<> bind(defined_internally, let->name);
let->body.accept(this);
}
void visit(const Call *op) override {
IRGraphVisitor::visit(op);
bool proper_func = (name == op->name || op->call_type != Call::Halide);
if (op->call_type == Call::Halide &&
op->func.defined() &&
op->name != name) {
Function func = Function(op->func);
proper_func = (name == op->name || func.has_pure_definition() || func.has_extern_definition());
}
if (pure) {
user_assert(proper_func)
<< "In pure definition of Func \"" << name << "\":\n"
<< "Can't call Func \"" << op->name
<< "\" because it has not yet been defined,"
<< " and it is not a recursive call.\n";
} else {
user_assert(proper_func)
<< "In update definition of Func \"" << name << "\":\n"
<< "Can't call Func \"" << op->name
<< "\" because it has not yet been defined.\n";
if (op->name == name && op->call_type == Call::Halide) {
for (size_t i = 0; i < op->args.size(); i++) {
const Variable *var = op->args[i].as<Variable>();
if (!pure_args[i].empty()) {
user_assert(var && var->name == pure_args[i])
<< "In update definition of Func \"" << name << "\":\n"
<< "All of a function's recursive references to itself"
<< " in update definitions must contain the same pure"
<< " variables in the same places as on the left-hand-side.\n";
}
}
}
}
}
void visit(const Variable *var) override {
// Is it a parameter?
if (var->param.defined()) {
return;
}
// Was it defined internally by a let expression?
if (defined_internally.contains(var->name)) {
return;
}
// Is it a pure argument?
for (auto &pure_arg : pure_args) {
if (var->name == pure_arg) {
return;
}
}
// Is it in a reduction domain?
if (var->reduction_domain.defined()) {
if (!reduction_domain.defined()) {
reduction_domain = var->reduction_domain;
return;
} else if (var->reduction_domain.same_as(reduction_domain)) {
// It's in a reduction domain we already know about
return;
} else {
user_error << "Multiple reduction domains found in definition of Func \"" << name << "\"\n";
}
} else if (reduction_domain.defined() && unbound_reduction_vars_ok) {
// Is it one of the RVars from the reduction domain we already
// know about (this can happen in the RDom predicate).
for (const ReductionVariable &rv : reduction_domain.domain()) {
if (rv.var == var->name) {
return;
}
}
}
user_error << "Undefined variable \"" << var->name << "\" in definition of Func \"" << name << "\"\n";
}
};
// Mark all functions found in an expr as frozen.
class FreezeFunctions : public IRGraphVisitor {
using IRGraphVisitor::visit;
const string &func;
void visit(const Call *op) override {
IRGraphVisitor::visit(op);
if (op->call_type == Call::Halide &&
op->func.defined() &&
op->name != func) {
Function f(op->func);
f.freeze();
}
}
public:
FreezeFunctions(const string &f)
: func(f) {
}
};
} // namespace
// A counter to use in tagging random variables.
// Note that this will be reset by Internal::reset_random_counters().
std::atomic<int> random_variable_counter = 0;
Function::Function(const FunctionPtr &ptr)
: contents(ptr) {
contents.strengthen();
internal_assert(ptr.defined())
<< "Can't construct Function from undefined FunctionContents ptr\n";
}
Function::Function(const std::string &n) {
for (size_t i = 0; i < n.size(); i++) {
user_assert(n[i] != '.')
<< "Func name \"" << n << "\" is invalid. "
<< "Func names may not contain the character '.', "
<< "as it is used internally by Halide as a separator\n";
}
contents.strong = new FunctionGroup;
contents.strong->members.resize(1);
contents->name = n;
contents->origin_name = n;
}
Function::Function(const std::vector<Type> &required_types, int required_dims, const std::string &n)
: Function(n) {
user_assert(required_dims >= AnyDims);
contents->required_types = required_types;
contents->required_dims = required_dims;
}
void Function::update_with_deserialization(const std::string &name,
const std::string &origin_name,
const std::vector<Halide::Type> &output_types,
const std::vector<Halide::Type> &required_types,
int required_dims,
const std::vector<std::string> &args,
const FuncSchedule &func_schedule,
const Definition &init_def,
const std::vector<Definition> &updates,
const std::string &debug_file,
const std::vector<Parameter> &output_buffers,
const std::vector<ExternFuncArgument> &extern_arguments,
const std::string &extern_function_name,
NameMangling name_mangling,
DeviceAPI device_api,
const Expr &extern_proxy_expr,
bool trace_loads,
bool trace_stores,
bool trace_realizations,
const std::vector<std::string> &trace_tags,
bool no_profiling,
bool frozen) {
contents->name = name;
contents->origin_name = origin_name;
contents->output_types = output_types;
contents->required_types = required_types;
contents->required_dims = required_dims;
contents->args = args;
contents->func_schedule = func_schedule;
contents->init_def = init_def;
contents->updates = updates;
contents->debug_file = debug_file;
contents->output_buffers = output_buffers;
contents->extern_arguments = extern_arguments;
contents->extern_function_name = extern_function_name;
contents->extern_mangling = name_mangling;
contents->extern_function_device_api = device_api;
contents->extern_proxy_expr = extern_proxy_expr;
contents->trace_loads = trace_loads;
contents->trace_stores = trace_stores;
contents->trace_realizations = trace_realizations;
contents->trace_tags = trace_tags;
contents->no_profiling = no_profiling;
contents->frozen = frozen;
}
namespace {
template<typename T>
struct PrintTypeList {
const std::vector<T> &list_;
explicit PrintTypeList(const std::vector<T> &list)
: list_(list) {
}
friend std::ostream &operator<<(std::ostream &s, const PrintTypeList &self) {
const size_t n = self.list_.size();
if (n != 1) {
s << "(";
}
const char *comma = "";
for (const auto &t : self.list_) {
if constexpr (std::is_same_v<Type, T>) {
s << comma << t;
} else {
s << comma << t.type();
}
comma = ", ";
}
if (n != 1) {
s << ")";
}
return s;
}
};
bool types_match(const std::vector<Type> &types, const std::vector<Expr> &exprs) {
size_t n = types.size();
if (n != exprs.size()) {
return false;
}
for (size_t i = 0; i < n; i++) {
if (types[i] != exprs[i].type()) {
return false;
}
}
return true;
}
} // namespace
void Function::check_types(const Expr &e) const {
check_types(std::vector<Expr>{e});
}
void Function::check_types(const Tuple &t) const {
check_types(t.as_vector());
}
void Function::check_types(const Type &t) const {
check_types(std::vector<Type>{t});
}
void Function::check_types(const std::vector<Expr> &exprs) const {
if (!contents->required_types.empty()) {
user_assert(types_match(contents->required_types, exprs))
<< "Func \"" << name() << "\" is constrained to only hold values of type " << PrintTypeList(contents->required_types)
<< " but is defined with values of type " << PrintTypeList(exprs) << ".\n";
}
}
void Function::check_types(const std::vector<Type> &types) const {
if (!contents->required_types.empty()) {
user_assert(contents->required_types == types)
<< "Func \"" << name() << "\" is constrained to only hold values of type " << PrintTypeList(contents->required_types)
<< " but is defined with values of type " << PrintTypeList(types) << ".\n";
}
}
void Function::check_dims(int dims) const {
if (contents->required_dims != AnyDims) {
user_assert(contents->required_dims == dims)
<< "Func \"" << name() << "\" is constrained to have exactly " << contents->required_dims
<< " dimensions, but is defined with " << dims << " dimensions.\n";
}
}
namespace {
// Return deep-copy of ExternFuncArgument 'src'
ExternFuncArgument deep_copy_extern_func_argument_helper(const ExternFuncArgument &src,
DeepCopyMap &copied_map) {
ExternFuncArgument copy;
copy.arg_type = src.arg_type;
copy.buffer = src.buffer;
copy.expr = src.expr;
copy.image_param = src.image_param;
if (!src.func.defined()) { // No need to deep-copy the func if it's undefined
internal_assert(!src.is_func())
<< "ExternFuncArgument has type FuncArg but has no function definition\n";
return copy;
}
// If the FunctionContents has already been deep-copied previously, i.e.
// it's in the 'copied_map', use the deep-copied version from the map instead
// of creating a new deep-copy
FunctionPtr &copied_func = copied_map[src.func];
internal_assert(copied_func.defined());
copy.func = copied_func;
return copy;
}
} // namespace
void Function::deep_copy(const FunctionPtr ©, DeepCopyMap &copied_map) const {
internal_assert(copy.defined())
<< "Cannot deep-copy to undefined Function\n";
internal_assert(contents.defined())
<< "Cannot deep-copy from undefined Function\n";
// Add reference to this Function's deep-copy to the map in case of
// self-reference, e.g. self-reference in an Definition.
copied_map[contents] = copy;
debug(4) << "Deep-copy function contents: \"" << contents->name << "\"\n";
copy->name = contents->name;
copy->origin_name = contents->origin_name;
copy->args = contents->args;
copy->output_types = contents->output_types;
copy->debug_file = contents->debug_file;
copy->extern_function_name = contents->extern_function_name;
copy->extern_mangling = contents->extern_mangling;
copy->extern_function_device_api = contents->extern_function_device_api;
copy->extern_proxy_expr = contents->extern_proxy_expr;
copy->trace_loads = contents->trace_loads;
copy->trace_stores = contents->trace_stores;
copy->trace_realizations = contents->trace_realizations;
copy->trace_tags = contents->trace_tags;
copy->no_profiling = contents->no_profiling;
copy->frozen = contents->frozen;
copy->output_buffers = contents->output_buffers;
copy->func_schedule = contents->func_schedule.deep_copy(copied_map);
// Copy the pure definition
if (contents->init_def.defined()) {
copy->init_def = contents->init_def.get_copy();
internal_assert(copy->init_def.is_init());
internal_assert(copy->init_def.schedule().rvars().empty())
<< "Init definition shouldn't have reduction domain\n";
}
for (const Definition &def : contents->updates) {
internal_assert(!def.is_init());
Definition def_copy = def.get_copy();
internal_assert(!def_copy.is_init());
copy->updates.push_back(std::move(def_copy));
}
for (const ExternFuncArgument &e : contents->extern_arguments) {
ExternFuncArgument e_copy = deep_copy_extern_func_argument_helper(e, copied_map);
copy->extern_arguments.push_back(std::move(e_copy));
}
}
void Function::deep_copy(string name, const FunctionPtr ©, DeepCopyMap &copied_map) const {
deep_copy(copy, copied_map);
copy->name = std::move(name);
}
void Function::define(const vector<string> &args, vector<Expr> values) {
user_assert(!frozen())
<< "Func " << name() << " cannot be given a new pure definition, "
<< "because it has already been realized or used in the definition of another Func.\n";
user_assert(!has_extern_definition())
<< "In pure definition of Func \"" << name() << "\":\n"
<< "Func with extern definition cannot be given a pure definition.\n";
user_assert(!name().empty()) << "A Func may not have an empty name.\n";
for (auto &value : values) {
user_assert(value.defined())
<< "In pure definition of Func \"" << name() << "\":\n"
<< "Undefined expression in right-hand-side of definition.\n";
}
// Make sure all the vars in the value are either args or are
// attached to some parameter
CheckVars check(name(), true);
check.pure_args = args;
for (const auto &value : values) {
value.accept(&check);
}
// Freeze all called functions
FreezeFunctions freezer(name());
// TODO: Check for calls to undefined Funcs
for (const auto &value : values) {
value.accept(&freezer);
}
// Make sure all the vars in the args have unique non-empty names
for (size_t i = 0; i < args.size(); i++) {
user_assert(!args[i].empty())
<< "In pure definition of Func \"" << name() << "\":\n"
<< "In left-hand-side of definition, argument "
<< i << " has an empty name.\n";
for (size_t j = 0; j < i; j++) {
user_assert(args[i] != args[j])
<< "In pure definition of Func \"" << name() << "\":\n"
<< "In left-hand-side of definition, arguments "
<< i << " and " << j
<< " both have the name \"" + args[i] + "\"\n";
}
}
for (auto &value : values) {
value = common_subexpression_elimination(value);
}
// Tag calls to random() with the free vars
int tag = random_variable_counter++;
vector<VarOrRVar> free_vars;
free_vars.reserve(args.size());
for (const auto &arg : args) {
free_vars.emplace_back(Var(arg));
}
for (auto &value : values) {
value = lower_random(value, free_vars, tag);
}
user_assert(!check.reduction_domain.defined())
<< "In pure definition of Func \"" << name() << "\":\n"
<< "Reduction domain referenced in pure function definition.\n";
if (!contents.defined()) {
contents.strong = new FunctionGroup;
contents.strong->members.resize(1);
contents->name = unique_name('f');
contents->origin_name = contents->name;
}
user_assert(!contents->init_def.defined())
<< "In pure definition of Func \"" << name() << "\":\n"
<< "Func is already defined.\n";
check_types(values);
check_dims((int)args.size());
contents->args = args;
std::vector<Expr> init_def_args;
init_def_args.resize(args.size());
for (size_t i = 0; i < args.size(); i++) {
init_def_args[i] = Var(args[i]);
}
// If the function is inductive,
// the value and args might refer back to the
// function itself, introducing circular references and hence
// memory leaks. We need to break these cycles.
WeakenFunctionPtrs weakener(contents.get());
for (auto &arg : init_def_args) {
arg = weakener.mutate(arg);
}
for (auto &value : values) {
value = weakener.mutate(value);
}
if (check.reduction_domain.defined()) {
check.reduction_domain.set_predicate(
weakener.mutate(check.reduction_domain.predicate()));
}
ReductionDomain rdom;
contents->init_def = Definition(init_def_args, values, rdom, true);
for (const auto &arg : args) {
DimType dtype = DimType::PureVar;
if (is_inductive(arg)) {
dtype = DimType::InductiveVar;
}
Dim d = {arg, ForType::Serial, DeviceAPI::None, dtype};
contents->init_def.schedule().dims().push_back(d);
StorageDim sd = {arg};
contents->func_schedule.storage_dims().push_back(sd);
}
// Add the dummy outermost dim
{
Dim d = {Var::outermost().name(), ForType::Serial, DeviceAPI::None, DimType::PureVar};
contents->init_def.schedule().dims().push_back(d);
}
contents->output_types.resize(values.size());
for (size_t i = 0; i < contents->output_types.size(); i++) {
contents->output_types[i] = values[i].type();
}
if (!contents->required_types.empty()) {
// Just a reality check; mismatches here really should have been caught earlier
internal_assert(contents->required_types == contents->output_types);
}
if (contents->required_dims != AnyDims) {
// Just a reality check; mismatches here really should have been caught earlier
internal_assert(contents->required_dims == (int)args.size());
}
if (contents->output_buffers.empty()) {
create_output_buffers(contents->output_types, (int)args.size());
}
}
void Function::create_output_buffers(const std::vector<Type> &types, int dims) const {
internal_assert(contents->output_buffers.empty());
internal_assert(!types.empty() && dims != AnyDims);
for (size_t i = 0; i < types.size(); i++) {
string buffer_name = name();
if (types.size() > 1) {
buffer_name += '.' + std::to_string((int)i);
}
Parameter output(types[i], true, dims, buffer_name);
contents->output_buffers.push_back(output);
}
}
void Function::define_update(const vector<Expr> &_args, vector<Expr> values, const ReductionDomain &rdom) {
int update_idx = static_cast<int>(contents->updates.size());
user_assert(!name().empty())
<< "Func has an empty name.\n";
user_assert(has_pure_definition())
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Can't add an update definition without a pure definition first.\n";
user_assert(!frozen())
<< "Func " << name() << " cannot be given a new update definition, "
<< "because it has already been realized or used in the definition of another Func.\n";
user_assert(!is_inductive())
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Inductive functions cannot have update definitions.\n";
for (auto &value : values) {
user_assert(value.defined())
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Undefined expression in right-hand-side of update.\n";
}
// Check the dimensionality matches
user_assert((int)_args.size() == dimensions())
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Dimensionality of update definition must match dimensionality of pure definition.\n";
user_assert(values.size() == contents->init_def.values().size())
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Number of tuple elements for update definition must "
<< "match number of tuple elements for pure definition.\n";
const auto &pure_def_vals = contents->init_def.values();
for (size_t i = 0; i < values.size(); i++) {
// Check that pure value and the update value have the same
// type. Without this check, allocations may be the wrong size
// relative to what update code expects.
Type pure_type = pure_def_vals[i].type();
if (pure_type != values[i].type()) {
std::ostringstream err;
err << "In update definition " << update_idx << " of Func \"" << name() << "\":\n";
if (!values.empty()) {
err << "Tuple element " << i << " of update definition has type ";
} else {
err << "Update definition has type ";
}
err << values[i].type() << ", but pure definition has type " << pure_type;
user_error << err.str() << "\n";
}
values[i] = common_subexpression_elimination(values[i]);
}
vector<Expr> args(_args.size());
for (size_t i = 0; i < args.size(); i++) {
args[i] = common_subexpression_elimination(_args[i]);
}
// The pure args are those naked vars in the args that are not in
// a reduction domain and are not parameters and line up with the
// pure args in the pure definition.
bool pure = true;
vector<string> pure_args(args.size());
for (size_t i = 0; i < args.size(); i++) {
pure_args[i] = ""; // Will never match a var name
user_assert(args[i].defined())
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Argument " << i
<< " in left-hand-side of update definition is undefined.\n";
if (const Variable *var = args[i].as<Variable>()) {
if (!var->param.defined() &&
!var->reduction_domain.defined() &&
var->name == contents->args[i]) {
pure_args[i] = var->name;
} else {
pure = false;
}
} else {
pure = false;
}
}
// Make sure all the vars in the args and the value are either
// pure args, in the reduction domain, or a parameter. Also checks
// that recursive references to the function contain all the pure
// vars in the LHS in the correct places.
CheckVars check(name(), false);
check.pure_args = pure_args;
for (const auto &arg : args) {
arg.accept(&check);
}
for (const auto &value : values) {
value.accept(&check);
}
if (!check.reduction_domain.defined()) {
// Use the provided one
check.reduction_domain = rdom;
} else if (rdom.defined()) {
// This is an internal error because the ability to pass an explicit
// RDom is not exposed to the front-end. At the time of writing this is
// only used by rfactor.
internal_assert(rdom.same_as(check.reduction_domain))
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Explicit reduction domain passed to Function::define_update, "
<< "but another reduction domain was referred to by the args or values.\n"
<< "Explicit reduction domain passed:\n"
<< RDom(rdom) << "\n"
<< "Found reduction domain:\n"
<< RDom(check.reduction_domain) << "\n";
}
if (check.reduction_domain.defined()) {
check.unbound_reduction_vars_ok = true;
check.reduction_domain.predicate().accept(&check);
}
// Freeze all called functions
FreezeFunctions freezer(name());
for (const auto &arg : args) {
arg.accept(&freezer);
}
for (const auto &value : values) {
value.accept(&freezer);
}
// Freeze the reduction domain if defined
if (check.reduction_domain.defined()) {
check.reduction_domain.predicate().accept(&freezer);
check.reduction_domain.freeze();
}
// Tag calls to random() with the free vars
vector<VarOrRVar> free_vars;
int num_free_vars = (int)pure_args.size();
if (check.reduction_domain.defined()) {
num_free_vars += (int)check.reduction_domain.domain().size();
}
free_vars.reserve(num_free_vars);
for (const auto &pure_arg : pure_args) {
if (!pure_arg.empty()) {
free_vars.emplace_back(Var(pure_arg));
}
}
if (check.reduction_domain.defined()) {
for (size_t i = 0; i < check.reduction_domain.domain().size(); i++) {
free_vars.emplace_back(RVar(check.reduction_domain, i));
}
}
int tag = random_variable_counter++;
for (auto &arg : args) {
arg = lower_random(arg, free_vars, tag);
}
for (auto &value : values) {
value = lower_random(value, free_vars, tag);
}
if (check.reduction_domain.defined()) {
check.reduction_domain.set_predicate(lower_random(check.reduction_domain.predicate(), free_vars, tag));
}
// The update value and args probably refer back to the
// function itself, introducing circular references and hence
// memory leaks. We need to break these cycles.
WeakenFunctionPtrs weakener(contents.get());
for (auto &arg : args) {
arg = weakener.mutate(arg);
}
for (auto &value : values) {
value = weakener.mutate(value);
}
if (check.reduction_domain.defined()) {
check.reduction_domain.set_predicate(
weakener.mutate(check.reduction_domain.predicate()));
}
Definition r(args, values, check.reduction_domain, false);
internal_assert(!r.is_init()) << "Should have been an update definition\n";
// First add any reduction domain
if (check.reduction_domain.defined()) {
for (const auto &rvar : check.reduction_domain.domain()) {
// Is this RVar actually pure (safe to parallelize and
// reorder)? It's pure if one value of the RVar can never
// access from the same memory that another RVar is
// writing to.
const string &v = rvar.var;
bool pure = can_parallelize_rvar(v, name(), r);
Dim d = {v, ForType::Serial, DeviceAPI::None,
pure ? DimType::PureRVar : DimType::ImpureRVar};
r.schedule().dims().push_back(d);
}
}
// Then add the pure args outside of that
for (const auto &pure_arg : pure_args) {
if (!pure_arg.empty()) {
Dim d = {pure_arg, ForType::Serial, DeviceAPI::None, DimType::PureVar};
r.schedule().dims().push_back(d);
}
}
// Then the dummy outermost dim
{
Dim d = {Var::outermost().name(), ForType::Serial, DeviceAPI::None, DimType::PureVar};
r.schedule().dims().push_back(d);
}
// If there's no recursive reference, no reduction domain, and all
// the args are pure, then this definition completely hides
// earlier ones!
if (!check.reduction_domain.defined() &&
weakener.count == 0 &&
pure) {
user_warning
<< "In update definition " << update_idx << " of Func \"" << name() << "\":\n"
<< "Update definition completely hides earlier definitions, "
<< " because all the arguments are pure, it contains no self-references, "
<< " and no reduction domain. This may be an accidental re-definition of "
<< " an already-defined function.\n";
}
contents->updates.push_back(r);
}
void Function::define_extern(const std::string &function_name,
const std::vector<ExternFuncArgument> &extern_args,
const std::vector<Type> &types,
const std::vector<Var> &args,
NameMangling mangling,
DeviceAPI device_api) {
check_types(types);
check_dims((int)args.size());
user_assert(!has_pure_definition() && !has_update_definition())
<< "In extern definition for Func \"" << name() << "\":\n"
<< "Func with a pure definition cannot have an extern definition.\n";
user_assert(!has_extern_definition())
<< "In extern definition for Func \"" << name() << "\":\n"
<< "Func already has an extern definition.\n";
std::vector<string> arg_names;
std::vector<Expr> arg_exprs;
for (const auto &arg : args) {
arg_names.push_back(arg.name());
arg_exprs.push_back(arg);
}
contents->args = arg_names;
contents->extern_function_name = function_name;
contents->extern_arguments = extern_args;
contents->output_types = types;
contents->extern_mangling = mangling;
contents->extern_function_device_api = device_api;
std::vector<Expr> values;
contents->output_buffers.clear();
for (size_t i = 0; i < types.size(); i++) {
string buffer_name = name();
if (types.size() > 1) {
buffer_name += '.' + std::to_string((int)i);
}
Parameter output(types[i], true, (int)args.size(), buffer_name);
contents->output_buffers.push_back(output);
values.push_back(undef(types[i]));
}
contents->init_def = Definition(arg_exprs, values, ReductionDomain(), true);
// Reset the storage dims to match the pure args
contents->func_schedule.storage_dims().clear();
contents->init_def.schedule().dims().clear();
for (size_t i = 0; i < args.size(); i++) {
StorageDim sd = {arg_names[i]};
contents->func_schedule.storage_dims().push_back(sd);
Dim d = {arg_names[i], ForType::Extern, DeviceAPI::None, DimType::PureVar};
contents->init_def.schedule().dims().push_back(d);
}
// Add the dummy outermost dim
Dim d = {Var::outermost().name(), ForType::Serial, DeviceAPI::None, DimType::PureVar};
contents->init_def.schedule().dims().push_back(d);