forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunc.cpp
More file actions
1754 lines (1491 loc) · 55.5 KB
/
Func.cpp
File metadata and controls
1754 lines (1491 loc) · 55.5 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 <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#include "IR.h"
#include "Func.h"
#include "Util.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "Function.h"
#include "Argument.h"
#include "Lower.h"
#include "Image.h"
#include "Param.h"
#include "PrintLoopNest.h"
#include "Debug.h"
#include "IREquality.h"
#include "CodeGen_LLVM.h"
#include "LLVM_Headers.h"
#include "Output.h"
#include "LLVM_Output.h"
namespace Halide {
using std::max;
using std::min;
using std::make_pair;
using std::string;
using std::vector;
using std::pair;
using std::ofstream;
using namespace Internal;
Func::Func(const string &name) : func(unique_name(name)) {}
Func::Func() : func(make_entity_name(this, "Halide::Func", 'f')) {}
Func::Func(Expr e) : func(make_entity_name(this, "Halide::Func", 'f')) {
(*this)(_) = e;
}
Func::Func(Function f) : func(f) {}
const string &Func::name() const {
return func.name();
}
/** Get the pure arguments. */
std::vector<Var> Func::args() const {
const std::vector<std::string> arg_names = func.args();
std::vector<Var> args(arg_names.size());
for (size_t i = 0; i < arg_names.size(); i++) {
args[i] = Var(arg_names[i]);
}
return args;
}
/** The right-hand-side value of the pure definition of this
* function. An error if the Func has no definition, or is defined as
* a Tuple. */
Expr Func::value() const {
user_assert(defined())
<< "Can't call Func::value() on an undefined Func. To check if a Func is defined, call Func::defined()\n";
user_assert(func.outputs() == 1)
<< "Can't call Func::value() on Func \"" << name() << "\", because it has multiple values.\n";
return func.values()[0];
}
/** The values returned by a Func, in Tuple form. */
Tuple Func::values() const {
user_assert(defined())
<< "Can't call Func::values() on an undefined Func. To check if a Func is defined, call Func::defined().\n";
return Tuple(func.values());
}
/** Get the left-hand-side of the update definition. An empty
* vector if there's no update definition. */
const std::vector<Expr> &Func::update_args(int idx) const {
user_assert(has_update_definition())
<< "Can't call Func::update_args() on Func \"" << name()
<< "\" as it has no update definition. "
<< "Use Func::has_update_definition() to check for the existence of an update definition.\n";
user_assert(idx < num_update_definitions())
<< "Update definition index out of bounds.\n";
return func.updates()[idx].args;
}
/** Get the right-hand-side of the update definition. An error if
* there is no update definition. */
Expr Func::update_value(int idx) const {
user_assert(has_update_definition())
<< "Can't call Func::update_args() on Func \"" << name() << "\" as it has no update definition. "
<< "Use Func::has_update_definition() to check for the existence of an update definition.\n";
user_assert(idx < num_update_definitions())
<< "Update definition index out of bounds.\n";
user_assert(func.updates()[idx].values.size() == 1)
<< "Can't call Func::update_value() on Func \"" << name() << "\", because it has multiple values.\n";
return func.updates()[idx].values[0];
}
/** The update values returned by a Func, in Tuple form. */
Tuple Func::update_values(int idx) const {
user_assert(has_update_definition())
<< "Can't call Func::update_args() on Func \"" << name() << "\" as it has no update definition. "
<< "Use Func::has_update_definition() to check for the existence of an update definition.\n";
user_assert(idx < num_update_definitions())
<< "Update definition index out of bounds.\n";
return Tuple(func.updates()[idx].values);
}
/** Get the reduction domain for the update definition. Returns an
* undefined RDom if there's no update definition, or if the
* update definition has no domain. */
RDom Func::reduction_domain(int idx) const {
user_assert(has_update_definition())
<< "Can't call Func::update_args() on Func \"" << name() << "\" as it has no update definition. "
<< "Use Func::has_update_definition() to check for the existence of an update definition.\n";
user_assert(idx < num_update_definitions())
<< "Update definition index out of bounds.\n";
return func.updates()[idx].domain;
}
bool Func::defined() const {
return func.has_pure_definition() || func.has_extern_definition();
}
/** Is this function a reduction? */
bool Func::has_update_definition() const {
return func.has_update_definition();
}
/** How many update definitions are there? */
int Func::num_update_definitions() const {
return static_cast<int>(func.updates().size());
}
/** Is this function external? */
EXPORT bool Func::is_extern() const {
return func.has_extern_definition();
}
/** Add an extern definition for this Func. */
void Func::define_extern(const std::string &function_name,
const std::vector<ExternFuncArgument> &args,
const std::vector<Type> &types,
int dimensionality,
bool is_c_plus_plus) {
func.define_extern(function_name, args, types, dimensionality, is_c_plus_plus);
}
/** Get the types of the buffers returned by an extern definition. */
const std::vector<Type> &Func::output_types() const {
return func.output_types();
}
/** Get the number of outputs this function has. */
int Func::outputs() const {
return func.outputs();
}
/** Get the name of the extern function called for an extern
* definition. */
EXPORT const std::string &Func::extern_function_name() const {
return func.extern_function_name();
}
int Func::dimensions() const {
if (!defined()) return 0;
return func.dimensions();
}
FuncRefVar Func::operator()(vector<Var> args) const {
int placeholder_pos = add_implicit_vars(args);
return FuncRefVar(func, args, placeholder_pos);
}
FuncRefExpr Func::operator()(vector<Expr> args) const {
int placeholder_pos = add_implicit_vars(args);
return FuncRefExpr(func, args, placeholder_pos);
}
int Func::add_implicit_vars(vector<Var> &args) const {
int placeholder_pos = -1;
std::vector<Var>::iterator iter = args.begin();
while (iter != args.end() && !iter->same_as(_)) {
iter++;
}
if (iter != args.end()) {
placeholder_pos = (int)(iter - args.begin());
int i = 0;
iter = args.erase(iter);
while ((int)args.size() < dimensions()) {
Internal::debug(2) << "Adding implicit var " << i << " to call to " << name() << "\n";
iter = args.insert(iter, Var::implicit(i++));
iter++;
}
}
if (func.has_pure_definition() && args.size() != (size_t)dimensions()) {
user_error << "Func \"" << name() << "\" was called with "
<< args.size() << " arguments, but was defined with " << dimensions() << "\n";
}
return placeholder_pos;
}
int Func::add_implicit_vars(vector<Expr> &args) const {
int placeholder_pos = -1;
std::vector<Expr>::iterator iter = args.begin();
while (iter != args.end()) {
const Variable *var = iter->as<Variable>();
if (var && var->name == _.name())
break;
iter++;
}
if (iter != args.end()) {
placeholder_pos = (int)(iter - args.begin());
int i = 0;
iter = args.erase(iter);
while ((int)args.size() < dimensions()) {
Internal::debug(2) << "Adding implicit var " << i << " to call to " << name() << "\n";
iter = args.insert(iter, Var::implicit(i++));
iter++;
}
}
if (func.has_pure_definition() && args.size() != (size_t)dimensions()) {
user_error << "Func \"" << name() << "\" was called with "
<< args.size() << " arguments, but was defined with " << dimensions() << "\n";
}
return placeholder_pos;
}
namespace {
bool var_name_match(string candidate, string var) {
if (candidate == var) return true;
return Internal::ends_with(candidate, "." + var);
}
}
const std::string &Stage::name() const {
return stage_name;
}
void Stage::set_dim_type(VarOrRVar var, ForType t) {
bool found = false;
vector<Dim> &dims = schedule.dims();
for (size_t i = 0; i < dims.size(); i++) {
if (var_name_match(dims[i].var, var.name())) {
found = true;
dims[i].for_type = t;
// If it's an rvar and the for type is parallel, we need to
// validate that this doesn't introduce a race condition.
if (!dims[i].pure && var.is_rvar && (t == ForType::Vectorized || t == ForType::Parallel)) {
user_assert(schedule.allow_race_conditions())
<< "In schedule for " << stage_name
<< ", marking var " << var.name()
<< " as parallel or vectorized may introduce a race"
<< " condition resulting in incorrect output."
<< " It is possible to override this error using"
<< " the allow_race_conditions() method. Use this"
<< " with great caution, and only when you are willing"
<< " to accept non-deterministic output, or you can prove"
<< " that any race conditions in this code do not change"
<< " the output, or you can prove that there are actually"
<< " no race conditions, and that Halide is being too cautious.\n";
}
} else if (t == ForType::Vectorized) {
user_assert(dims[i].for_type != ForType::Vectorized)
<< "In schedule for " << stage_name
<< ", can't vectorize across " << var.name()
<< " because Func is already vectorized across " << dims[i].var << "\n";
}
}
if (!found) {
user_error << "In schedule for " << stage_name
<< ", could not find dimension "
<< var.name()
<< " to mark as " << t
<< " in vars for function\n"
<< dump_argument_list();
}
}
void Stage::set_dim_device_api(VarOrRVar var, DeviceAPI device_api) {
bool found = false;
vector<Dim> &dims = schedule.dims();
for (size_t i = 0; i < dims.size(); i++) {
if (var_name_match(dims[i].var, var.name())) {
found = true;
dims[i].device_api = device_api;
}
}
if (!found) {
user_error << "In schedule for " << stage_name
<< ", could not find dimension "
<< var.name()
<< " to set to device API " << static_cast<int>(device_api)
<< " in vars for function\n"
<< dump_argument_list();
}
}
std::string Stage::dump_argument_list() const {
std::ostringstream oss;
oss << "Vars:";
for (size_t i = 0; i < schedule.dims().size(); i++) {
oss << " " << schedule.dims()[i].var;
}
oss << "\n";
return oss.str();
}
void Stage::split(const string &old, const string &outer, const string &inner, Expr factor, bool exact) {
vector<Dim> &dims = schedule.dims();
// Check that the new names aren't already in the dims list.
for (size_t i = 0; i < dims.size(); i++) {
string new_names[2] = {inner, outer};
for (int j = 0; j < 2; j++) {
if (var_name_match(dims[i].var, new_names[j]) && new_names[j] != old) {
user_error << "In schedule for " << stage_name
<< ", Can't create var " << new_names[j]
<< " using a split or tile, because " << new_names[j]
<< " is already used in this Func's schedule elsewhere.\n" << dump_argument_list();
}
}
}
// Replace the old dimension with the new dimensions in the dims list
bool found = false;
string inner_name, outer_name, old_name;
for (size_t i = 0; (!found) && i < dims.size(); i++) {
if (var_name_match(dims[i].var, old)) {
found = true;
old_name = dims[i].var;
inner_name = old_name + "." + inner;
outer_name = old_name + "." + outer;
dims.insert(dims.begin() + i, dims[i]);
dims[i].var = inner_name;
dims[i+1].var = outer_name;
dims[i+1].pure = dims[i].pure;
}
}
if (!found) {
user_error << "In schedule for " << stage_name
<< "Could not find split dimension: "
<< old
<< "\n"
<< dump_argument_list();
}
// Add the split to the splits list
Split split = {old_name, outer_name, inner_name, factor, exact, Split::SplitVar};
schedule.splits().push_back(split);
}
Stage &Stage::split(VarOrRVar old, VarOrRVar outer, VarOrRVar inner, Expr factor) {
if (old.is_rvar) {
user_assert(outer.is_rvar) << "Can't split RVar " << old.name() << " into Var " << outer.name() << "\n";
user_assert(inner.is_rvar) << "Can't split RVar " << old.name() << " into Var " << inner.name() << "\n";
} else {
user_assert(!outer.is_rvar) << "Can't split Var " << old.name() << " into RVar " << outer.name() << "\n";
user_assert(!inner.is_rvar) << "Can't split Var " << old.name() << " into RVar " << inner.name() << "\n";
}
split(old.name(), outer.name(), inner.name(), factor, old.is_rvar);
return *this;
}
Stage &Stage::fuse(VarOrRVar inner, VarOrRVar outer, VarOrRVar fused) {
if (inner.is_rvar) {
user_assert(outer.is_rvar) << "Can't fuse RVar " << inner.name()
<< " with Var " << outer.name() << "\n";
user_assert(fused.is_rvar) << "Can't fuse RVar " << inner.name()
<< "into Var " << fused.name() << "\n";
} else {
user_assert(!outer.is_rvar) << "Can't fuse Var " << inner.name()
<< " with RVar " << outer.name() << "\n";
user_assert(!fused.is_rvar) << "Can't fuse Var " << inner.name()
<< "into RVar " << fused.name() << "\n";
}
// Replace the old dimensions with the new dimension in the dims list
bool found_outer = false, found_inner = false;
string inner_name, outer_name, fused_name;
vector<Dim> &dims = schedule.dims();
bool outer_pure = false;
for (size_t i = 0; (!found_outer) && i < dims.size(); i++) {
if (var_name_match(dims[i].var, outer.name())) {
found_outer = true;
outer_name = dims[i].var;
outer_pure = dims[i].pure;
dims.erase(dims.begin() + i);
}
}
if (!found_outer) {
user_error << "In schedule for " << stage_name
<< ", could not find outer fuse dimension: "
<< outer.name()
<< "\n"
<< dump_argument_list();
}
for (size_t i = 0; (!found_inner) && i < dims.size(); i++) {
if (var_name_match(dims[i].var, inner.name())) {
found_inner = true;
inner_name = dims[i].var;
fused_name = inner_name + "." + fused.name();
dims[i].var = fused_name;
dims[i].pure &= outer_pure;
}
}
if (!found_inner) {
user_error << "In schedule for " << stage_name
<< "Could not find inner fuse dimension: "
<< inner.name()
<< "\n"
<< dump_argument_list();
}
// Add the fuse to the splits list
Split split = {fused_name, outer_name, inner_name, Expr(), true, Split::FuseVars};
schedule.splits().push_back(split);
return *this;
}
namespace Internal {
class CheckForFreeVars : public IRGraphVisitor {
public:
string offending_var;
protected:
using IRGraphVisitor::visit;
void visit(const Variable *var) {
if (!var->param.defined() && !var->image.defined()) {
offending_var = var->name;
}
}
};
}
Stage Stage::specialize(Expr condition) {
user_assert(condition.type().is_bool()) << "Argument passed to specialize must be of type bool\n";
// The condition may not depend on Vars or RVars
Internal::CheckForFreeVars check;
condition.accept(&check);
if (!check.offending_var.empty()) {
user_error << "Specialization condition " << condition << " for " << stage_name
<< " depends on Var or RVar " << check.offending_var << ". "
<< "Specialization conditions may not depend on any Vars or RVars.\n";
}
// The user may be retrieving a reference to an existing
// specialization.
for (size_t i = 0; i < schedule.specializations().size(); i++) {
if (equal(condition, schedule.specializations()[i].condition)) {
return Stage(schedule.specializations()[i].schedule, stage_name);
}
}
const Specialization &s = schedule.add_specialization(condition);
return Stage(s.schedule, stage_name);
}
Stage &Stage::rename(VarOrRVar old_var, VarOrRVar new_var) {
if (old_var.is_rvar) {
user_assert(new_var.is_rvar)
<< "In schedule for " << stage_name
<< ", can't rename RVar " << old_var.name()
<< " to Var " << new_var.name() << "\n";
} else {
user_assert(!new_var.is_rvar)
<< "In schedule for " << stage_name
<< ", can't rename Var " << old_var.name()
<< " to RVar " << new_var.name() << "\n";
}
// Replace the old dimension with the new dimensions in the dims list
bool found = false;
string old_name;
vector<Dim> &dims = schedule.dims();
for (size_t i = 0; (!found) && i < dims.size(); i++) {
if (var_name_match(dims[i].var, old_var.name())) {
found = true;
old_name = dims[i].var;
dims[i].var += "." + new_var.name();
}
}
string new_name = old_name + "." + new_var.name();
if (!found) {
user_error
<< "In schedule for " << stage_name
<< ", could not find rename dimension: "
<< old_var.name()
<< "\n"
<< dump_argument_list();
}
// If possible, rewrite the split or rename that defines it.
found = false;
for (size_t i = schedule.splits().size(); i > 0; i--) {
if (schedule.splits()[i-1].is_fuse()) {
if (schedule.splits()[i-1].inner == old_name ||
schedule.splits()[i-1].outer == old_name) {
user_error
<< "In schedule for " << stage_name
<< ", can't rename variable " << old_name
<< " because it has already been fused into "
<< schedule.splits()[i-1].old_var << "\n"
<< dump_argument_list();
}
if (schedule.splits()[i-1].old_var == old_name) {
schedule.splits()[i-1].old_var = new_name;
found = true;
break;
}
} else {
if (schedule.splits()[i-1].inner == old_name) {
schedule.splits()[i-1].inner = new_name;
found = true;
break;
}
if (schedule.splits()[i-1].outer == old_name) {
schedule.splits()[i-1].outer = new_name;
found = true;
break;
}
if (schedule.splits()[i-1].old_var == old_name) {
user_error
<< "In schedule for " << stage_name
<< ", can't rename a variable " << old_name
<< " because it has already been renamed or split.\n"
<< dump_argument_list();
}
}
}
if (!found) {
Split split = {old_name, new_name, "", 1, old_var.is_rvar, Split::RenameVar};
schedule.splits().push_back(split);
}
return *this;
}
Stage &Stage::allow_race_conditions() {
schedule.allow_race_conditions() = true;
return *this;
}
Stage &Stage::serial(VarOrRVar var) {
set_dim_type(var, ForType::Serial);
return *this;
}
Stage &Stage::parallel(VarOrRVar var) {
set_dim_type(var, ForType::Parallel);
return *this;
}
Stage &Stage::vectorize(VarOrRVar var) {
set_dim_type(var, ForType::Vectorized);
return *this;
}
Stage &Stage::unroll(VarOrRVar var) {
set_dim_type(var, ForType::Unrolled);
return *this;
}
Stage &Stage::parallel(VarOrRVar var, Expr factor) {
if (var.is_rvar) {
RVar tmp;
split(var.rvar, var.rvar, tmp, factor);
} else {
Var tmp;
split(var.var, var.var, tmp, factor);
}
parallel(var);
return *this;
}
Stage &Stage::vectorize(VarOrRVar var, int factor) {
if (var.is_rvar) {
RVar tmp;
split(var.rvar, var.rvar, tmp, factor);
vectorize(tmp);
} else {
Var tmp;
split(var.var, var.var, tmp, factor);
vectorize(tmp);
}
return *this;
}
Stage &Stage::unroll(VarOrRVar var, int factor) {
if (var.is_rvar) {
RVar tmp;
split(var.rvar, var.rvar, tmp, factor);
unroll(tmp);
} else {
Var tmp;
split(var.var, var.var, tmp, factor);
unroll(tmp);
}
return *this;
}
Stage &Stage::tile(VarOrRVar x, VarOrRVar y,
VarOrRVar xo, VarOrRVar yo,
VarOrRVar xi, VarOrRVar yi,
Expr xfactor, Expr yfactor) {
split(x, xo, xi, xfactor);
split(y, yo, yi, yfactor);
reorder(xi, yi, xo, yo);
return *this;
}
Stage &Stage::tile(VarOrRVar x, VarOrRVar y,
VarOrRVar xi, VarOrRVar yi,
Expr xfactor, Expr yfactor) {
split(x, x, xi, xfactor);
split(y, y, yi, yfactor);
reorder(xi, yi, x, y);
return *this;
}
namespace {
// An helper function for reordering vars in a schedule.
void reorder_vars(vector<Dim> &dims_old, const VarOrRVar *vars, size_t size, const Stage &stage) {
vector<Dim> dims = dims_old;
// Tag all the vars with their locations in the dims list.
vector<size_t> idx(size);
for (size_t i = 0; i < size; i++) {
bool found = false;
for (size_t j = 0; j < dims.size(); j++) {
if (var_name_match(dims[j].var, vars[i].name())) {
idx[i] = j;
found = true;
}
}
user_assert(found)
<< "In schedule for " << stage.name()
<< ", could not find var " << vars[i].name()
<< " to reorder in the argument list.\n"
<< stage.dump_argument_list();
}
// Look for illegal reorderings
for (size_t i = 0; i < idx.size(); i++) {
if (dims[idx[i]].pure) continue;
for (size_t j = i+1; j < idx.size(); j++) {
if (dims[idx[j]].pure) continue;
if (idx[i] > idx[j]) {
user_error
<< "In schedule for " << stage.name()
<< ", can't reorder RVars " << vars[i].name()
<< " and " << vars[j].name()
<< " because it may change the meaning of the algorithm.\n";
}
}
}
// Sort idx to get the new locations
vector<size_t> sorted = idx;
std::sort(sorted.begin(), sorted.end());
for (size_t i = 0; i < size; i++) {
dims[sorted[i]] = dims_old[idx[i]];
}
dims_old.swap(dims);
}
}
Stage &Stage::reorder(const std::vector<VarOrRVar>& vars) {
reorder_vars(schedule.dims(), &vars[0], vars.size(), *this);
return *this;
}
Stage &Stage::gpu_threads(VarOrRVar tx, DeviceAPI device_api) {
set_dim_device_api(tx, device_api);
parallel(tx);
rename(tx, VarOrRVar("__thread_id_x", tx.is_rvar));
return *this;
}
Stage &Stage::gpu_threads(VarOrRVar tx, VarOrRVar ty, DeviceAPI device_api) {
set_dim_device_api(tx, device_api);
set_dim_device_api(ty, device_api);
parallel(tx);
parallel(ty);
rename(tx, VarOrRVar("__thread_id_x", tx.is_rvar));
rename(ty, VarOrRVar("__thread_id_y", ty.is_rvar));
return *this;
}
Stage &Stage::gpu_threads(VarOrRVar tx, VarOrRVar ty, VarOrRVar tz, DeviceAPI device_api) {
set_dim_device_api(tx, device_api);
set_dim_device_api(ty, device_api);
set_dim_device_api(tz, device_api);
parallel(tx);
parallel(ty);
parallel(tz);
rename(tx, VarOrRVar("__thread_id_x", tx.is_rvar));
rename(ty, VarOrRVar("__thread_id_y", ty.is_rvar));
rename(tz, VarOrRVar("__thread_id_z", tz.is_rvar));
return *this;
}
Stage &Stage::gpu_blocks(VarOrRVar tx, DeviceAPI device_api) {
set_dim_device_api(tx, device_api);
parallel(tx);
rename(tx, VarOrRVar("__block_id_x", tx.is_rvar));
return *this;
}
Stage &Stage::gpu_blocks(VarOrRVar tx, VarOrRVar ty, DeviceAPI device_api) {
set_dim_device_api(tx, device_api);
set_dim_device_api(ty, device_api);
parallel(tx);
parallel(ty);
rename(tx, VarOrRVar("__block_id_x", tx.is_rvar));
rename(ty, VarOrRVar("__block_id_y", ty.is_rvar));
return *this;
}
Stage &Stage::gpu_blocks(VarOrRVar tx, VarOrRVar ty, VarOrRVar tz, DeviceAPI device_api) {
set_dim_device_api(tx, device_api);
set_dim_device_api(ty, device_api);
set_dim_device_api(tz, device_api);
parallel(tx);
parallel(ty);
parallel(tz);
rename(tx, VarOrRVar("__block_id_x", tx.is_rvar));
rename(ty, VarOrRVar("__block_id_y", ty.is_rvar));
rename(tz, VarOrRVar("__block_id_z", tz.is_rvar));
return *this;
}
Stage &Stage::gpu_single_thread(DeviceAPI device_api) {
split(Var::outermost(), Var::outermost(), Var::gpu_blocks(), 1);
set_dim_device_api(Var::gpu_blocks(), device_api);
parallel(Var::gpu_blocks());
return *this;
}
Stage &Stage::gpu(VarOrRVar bx, VarOrRVar tx, DeviceAPI device_api) {
return gpu_blocks(bx).gpu_threads(tx);
}
Stage &Stage::gpu(VarOrRVar bx, VarOrRVar by,
VarOrRVar tx, VarOrRVar ty, DeviceAPI device_api) {
return gpu_blocks(bx, by).gpu_threads(tx, ty);
}
Stage &Stage::gpu(VarOrRVar bx, VarOrRVar by, VarOrRVar bz,
VarOrRVar tx, VarOrRVar ty, VarOrRVar tz,
DeviceAPI device_api) {
return gpu_blocks(bx, by, bz).gpu_threads(tx, ty, tz);
}
Stage &Stage::gpu_tile(VarOrRVar x, Expr x_size, DeviceAPI device_api) {
VarOrRVar bx("__block_id_x", x.is_rvar),
tx("__thread_id_x", x.is_rvar);
split(x, bx, tx, x_size);
set_dim_device_api(bx, device_api);
set_dim_device_api(tx, device_api);
parallel(bx);
parallel(tx);
return *this;
}
Stage &Stage::gpu_tile(VarOrRVar x, VarOrRVar y,
Expr x_size, Expr y_size,
DeviceAPI device_api) {
VarOrRVar bx("__block_id_x", x.is_rvar),
by("__block_id_y", y.is_rvar),
tx("__thread_id_x", x.is_rvar),
ty("__thread_id_y", y.is_rvar);
tile(x, y, bx, by, tx, ty, x_size, y_size);
set_dim_device_api(bx, device_api);
set_dim_device_api(by, device_api);
set_dim_device_api(tx, device_api);
set_dim_device_api(ty, device_api);
parallel(bx);
parallel(by);
parallel(tx);
parallel(ty);
return *this;
}
Stage &Stage::gpu_tile(VarOrRVar x, VarOrRVar y, VarOrRVar z,
Expr x_size, Expr y_size, Expr z_size,
DeviceAPI device_api) {
VarOrRVar bx("__block_id_x", x.is_rvar),
by("__block_id_y", y.is_rvar),
bz("__block_id_z", z.is_rvar),
tx("__thread_id_x", x.is_rvar),
ty("__thread_id_y", y.is_rvar),
tz("__thread_id_z", z.is_rvar);
split(x, bx, tx, x_size);
split(y, by, ty, y_size);
split(z, bz, tz, z_size);
// current order is:
// tx bx ty by tz bz
reorder(ty, bx);
// tx ty bx by tz bz
reorder(tz, bx);
// tx ty tz by bx bz
reorder(bx, by);
// tx ty tz bx by bz
set_dim_device_api(bx, device_api);
set_dim_device_api(by, device_api);
set_dim_device_api(bz, device_api);
set_dim_device_api(tx, device_api);
set_dim_device_api(ty, device_api);
set_dim_device_api(tz, device_api);
parallel(bx);
parallel(by);
parallel(bz);
parallel(tx);
parallel(ty);
parallel(tz);
return *this;
}
void Func::invalidate_cache() {
if (pipeline_.defined()) {
pipeline_.invalidate_cache();
}
}
Func &Func::split(VarOrRVar old, VarOrRVar outer, VarOrRVar inner, Expr factor) {
invalidate_cache();
Stage(func.schedule(), name()).split(old, outer, inner, factor);
return *this;
}
Func &Func::fuse(VarOrRVar inner, VarOrRVar outer, VarOrRVar fused) {
invalidate_cache();
Stage(func.schedule(), name()).fuse(inner, outer, fused);
return *this;
}
Func &Func::rename(VarOrRVar old_name, VarOrRVar new_name) {
invalidate_cache();
Stage(func.schedule(), name()).rename(old_name, new_name);
return *this;
}
Func &Func::allow_race_conditions() {
Stage(func.schedule(), name()).allow_race_conditions();
return *this;
}
Func &Func::memoize() {
invalidate_cache();
func.schedule().memoized() = true;
return *this;
}
Stage Func::specialize(Expr c) {
invalidate_cache();
return Stage(func.schedule(), name()).specialize(c);
}
Func &Func::serial(VarOrRVar var) {
invalidate_cache();
Stage(func.schedule(), name()).serial(var);
return *this;
}
Func &Func::parallel(VarOrRVar var) {
invalidate_cache();
Stage(func.schedule(), name()).parallel(var);
return *this;
}
Func &Func::vectorize(VarOrRVar var) {
invalidate_cache();
Stage(func.schedule(), name()).vectorize(var);
return *this;
}
Func &Func::unroll(VarOrRVar var) {
invalidate_cache();
Stage(func.schedule(), name()).unroll(var);
return *this;
}
Func &Func::parallel(VarOrRVar var, Expr factor) {
invalidate_cache();
Stage(func.schedule(), name()).parallel(var, factor);
return *this;
}
Func &Func::vectorize(VarOrRVar var, int factor) {
invalidate_cache();
Stage(func.schedule(), name()).vectorize(var, factor);
return *this;
}
Func &Func::unroll(VarOrRVar var, int factor) {
invalidate_cache();
Stage(func.schedule(), name()).unroll(var, factor);
return *this;
}
Func &Func::bound(Var var, Expr min, Expr extent) {
invalidate_cache();
bool found = false;
for (size_t i = 0; i < func.args().size(); i++) {
if (var.name() == func.args()[i]) {
found = true;
}
}
user_assert(found)
<< "Can't bound variable " << var.name()
<< " of function " << name()
<< " because " << var.name()
<< " is not one of the pure variables of " << name() << ".\n";
Bound b = {var.name(), min, extent};
func.schedule().bounds().push_back(b);
return *this;
}
Func &Func::tile(VarOrRVar x, VarOrRVar y,
VarOrRVar xo, VarOrRVar yo,
VarOrRVar xi, VarOrRVar yi,
Expr xfactor, Expr yfactor) {
invalidate_cache();
Stage(func.schedule(), name()).tile(x, y, xo, yo, xi, yi, xfactor, yfactor);
return *this;
}
Func &Func::tile(VarOrRVar x, VarOrRVar y,
VarOrRVar xi, VarOrRVar yi,
Expr xfactor, Expr yfactor) {
invalidate_cache();
Stage(func.schedule(), name()).tile(x, y, xi, yi, xfactor, yfactor);
return *this;
}
Func &Func::reorder(const std::vector<VarOrRVar> &vars) {
invalidate_cache();
Stage(func.schedule(), name()).reorder(vars);
return *this;
}
Func &Func::gpu_threads(VarOrRVar tx, DeviceAPI device_api) {
invalidate_cache();
Stage(func.schedule(), name()).gpu_threads(tx, device_api);
return *this;
}
Func &Func::gpu_threads(VarOrRVar tx, VarOrRVar ty, DeviceAPI device_api) {
invalidate_cache();
Stage(func.schedule(), name()).gpu_threads(tx, ty, device_api);
return *this;
}
Func &Func::gpu_threads(VarOrRVar tx, VarOrRVar ty, VarOrRVar tz, DeviceAPI device_api) {
invalidate_cache();
Stage(func.schedule(), name()).gpu_threads(tx, ty, tz, device_api);
return *this;
}
Func &Func::gpu_blocks(VarOrRVar bx, DeviceAPI device_api) {
invalidate_cache();
Stage(func.schedule(), name()).gpu_blocks(bx, device_api);
return *this;
}