-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Bounds.cpp
1559 lines (1356 loc) · 57 KB
/
Bounds.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 <iostream>
#include "Bounds.h"
#include "IRVisitor.h"
#include "IR.h"
#include "IROperator.h"
#include "IREquality.h"
#include "Simplify.h"
#include "IRPrinter.h"
#include "Util.h"
#include "Var.h"
#include "Debug.h"
#include "ExprUsesVar.h"
#include "IRMutator.h"
#include "CSE.h"
namespace Halide {
namespace Internal {
using std::make_pair;
using std::map;
using std::vector;
using std::string;
using std::pair;
namespace {
int static_sign(Expr x) {
if (is_positive_const(x)) {
return 1;
} else if (is_negative_const(x)) {
return -1;
} else {
Expr zero = make_zero(x.type());
if (equal(const_true(), simplify(x > zero))) {
return 1;
} else if (equal(const_true(), simplify(x < zero))) {
return -1;
}
}
return 0;
}
}
Expr find_constant_bound(Expr e, Direction d) {
// We look through casts, so we only handle ops that can't
// overflow. E.g. if A >= a and B >= b, then you can't assume that
// (A + B) >= (a + b) in a world with overflow.
if (is_const(e)) {
return e;
} else if (const Min *min = e.as<Min>()) {
Expr a = find_constant_bound(min->a, d);
Expr b = find_constant_bound(min->b, d);
if (a.defined() && b.defined()) {
return simplify(Min::make(a, b));
} else if (a.defined() && d == Direction::Upper) {
return a;
} else if (b.defined() && d == Direction::Upper) {
return b;
}
} else if (const Max *max = e.as<Max>()) {
Expr a = find_constant_bound(max->a, d);
Expr b = find_constant_bound(max->b, d);
if (a.defined() && b.defined()) {
return simplify(Max::make(a, b));
} else if (a.defined() && d == Direction::Lower) {
return a;
} else if (b.defined() && d == Direction::Lower) {
return b;
}
} else if (const Cast *cast = e.as<Cast>()) {
Expr a = find_constant_bound(cast->value, d);
if (a.defined()) {
return simplify(Cast::make(cast->type, a));
}
}
return Expr();
}
class Bounds : public IRVisitor {
public:
Interval interval;
Scope<Interval> scope;
const FuncValueBounds &func_bounds;
Bounds(const Scope<Interval> *s, const FuncValueBounds &fb) :
func_bounds(fb) {
scope.set_containing_scope(s);
}
private:
// Compute the intrinsic bounds of a function.
void bounds_of_func(string name, int value_index, Type t) {
// if we can't get a good bound from the function, fall back to the bounds of the type.
bounds_of_type(t);
pair<string, int> key = make_pair(name, value_index);
FuncValueBounds::const_iterator iter = func_bounds.find(key);
if (iter != func_bounds.end()) {
if (iter->second.has_lower_bound()) {
interval.min = iter->second.min;
}
if (iter->second.has_upper_bound()) {
interval.max = iter->second.max;
}
}
}
void bounds_of_type(Type t) {
t = t.element_of();
if ((t.is_uint() || t.is_int()) && t.bits() <= 16) {
interval = Interval(t.min(), t.max());
} else {
interval = Interval::everything();
}
}
using IRVisitor::visit;
void visit(const IntImm *op) {
interval = Interval::single_point(op);
}
void visit(const UIntImm *op) {
interval = Interval::single_point(op);
}
void visit(const FloatImm *op) {
interval = Interval::single_point(op);
}
void visit(const Cast *op) {
op->value.accept(this);
Interval a = interval;
if (a.is_single_point(op->value)) {
interval = Interval::single_point(op);
return;
}
Type to = op->type.element_of();
Type from = op->value.type().element_of();
if (a.is_single_point()) {
interval = Interval::single_point(Cast::make(to, a.min));
return;
}
// If overflow is impossible, cast the min and max. If it's
// possible, use the bounds of the destination type.
bool could_overflow = true;
if (to.can_represent(from) || to.is_float()) {
could_overflow = false;
} else if (to.is_int() && to.bits() >= 32) {
// If we cast to an int32 or greater, assume that it won't
// overflow. Signed 32-bit integer overflow is undefined.
could_overflow = false;
} else if (a.is_bounded() && from.can_represent(to)) {
// The other case to consider is narrowing where the
// bounds of the original fit into the narrower type. We
// can only really prove that this is the case if they're
// constants, so try to make the constants first.
Expr lower_bound = find_constant_bound(a.min, Direction::Lower);
Expr upper_bound = find_constant_bound(a.max, Direction::Upper);
if (lower_bound.defined() && upper_bound.defined()) {
// Cast them to the narrow type and back and see if
// they're provably unchanged.
Expr test =
(cast(from, cast(to, lower_bound)) == lower_bound &&
cast(from, cast(to, upper_bound)) == upper_bound);
if (can_prove(test)) {
could_overflow = false;
// Relax the bounds to the constants we found. Not
// strictly necessary, but probably helpful to
// keep the expressions small.
a = Interval(lower_bound, upper_bound);
}
}
}
if (!could_overflow) {
// Start with the bounds of the narrow type.
bounds_of_type(from);
// If we have a better min or max for the arg use that.
if (a.has_lower_bound()) interval.min = a.min;
if (a.has_upper_bound()) interval.max = a.max;
// Then cast those bounds to the wider type.
if (interval.has_lower_bound()) interval.min = Cast::make(to, interval.min);
if (interval.has_upper_bound()) interval.max = Cast::make(to, interval.max);
} else {
// This might overflow, so use the bounds of the destination type.
bounds_of_type(to);
}
}
void visit(const Variable *op) {
if (scope.contains(op->name)) {
interval = scope.get(op->name);
} else if (op->type.is_vector()) {
// Uh oh, we need to take the min/max lane of some unknown vector. Treat as unbounded.
bounds_of_type(op->type);
} else {
interval = Interval::single_point(op);
}
}
void visit(const Add *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min + b.min);
} else {
interval = Interval::everything();
if (a.has_lower_bound() && b.has_lower_bound()) {
interval.min = a.min + b.min;
}
if (a.has_upper_bound() && b.has_upper_bound()) {
interval.max = a.max + b.max;
}
// Check for overflow for (u)int8 and (u)int16
if (!op->type.is_float() && op->type.bits() < 32) {
if (interval.has_upper_bound()) {
Expr no_overflow = (cast<int>(a.max) + cast<int>(b.max) == cast<int>(interval.max));
if (!can_prove(no_overflow)) {
bounds_of_type(op->type);
return;
}
}
if (interval.has_lower_bound()) {
Expr no_overflow = (cast<int>(a.min) + cast<int>(b.min) == cast<int>(interval.min));
if (!can_prove(no_overflow)) {
bounds_of_type(op->type);
return;
}
}
}
}
}
void visit(const Sub *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min - b.min);
} else {
interval = Interval::everything();
if (a.has_lower_bound() && b.has_upper_bound()) {
interval.min = a.min - b.max;
}
if (a.has_upper_bound() && b.has_lower_bound()) {
interval.max = a.max - b.min;
}
// Check for overflow for (u)int8 and (u)int16
if (!op->type.is_float() && op->type.bits() < 32) {
if (interval.has_upper_bound()) {
Expr no_overflow = (cast<int>(a.max) - cast<int>(b.min) == cast<int>(interval.max));
if (!can_prove(no_overflow)) {
bounds_of_type(op->type);
return;
}
}
if (interval.has_lower_bound()) {
Expr no_overflow = (cast<int>(a.min) - cast<int>(b.max) == cast<int>(interval.min));
if (!can_prove(no_overflow)) {
bounds_of_type(op->type);
return;
}
}
}
// Check underflow for uint
if (op->type.is_uint() &&
interval.has_lower_bound() &&
!can_prove(b.max <= a.min)) {
bounds_of_type(op->type);
}
}
}
void visit(const Mul *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
// Move constants to the right
if (a.is_single_point() && !b.is_single_point()) {
std::swap(a, b);
}
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min * b.min);
} else if (b.is_single_point()) {
Expr e1 = a.has_lower_bound() ? a.min * b.min : a.min;
Expr e2 = a.has_upper_bound() ? a.max * b.min : a.max;
if (is_zero(b.min)) {
interval = b;
} else if (is_positive_const(b.min) || op->type.is_uint()) {
interval = Interval(e1, e2);
} else if (is_negative_const(b.min)) {
interval = Interval(e2, e1);
} else if (a.is_bounded()) {
// Sign of b is unknown
Expr cmp = b.min >= make_zero(b.min.type().element_of());
interval = Interval(select(cmp, e1, e2), select(cmp, e2, e1));
} else {
interval = Interval::everything();
}
} else if (a.is_bounded() && b.is_bounded()) {
interval = Interval::nothing();
interval.include(a.min * b.min);
interval.include(a.min * b.max);
interval.include(a.max * b.min);
interval.include(a.max * b.max);
} else {
interval = Interval::everything();
}
if (op->type.bits() < 32 && !op->type.is_float()) {
if (a.is_bounded() && b.is_bounded()) {
// Try to prove it can't overflow
Expr test1 = (cast<int>(a.min) * cast<int>(b.min) == cast<int>(a.min * b.min));
Expr test2 = (cast<int>(a.min) * cast<int>(b.max) == cast<int>(a.min * b.max));
Expr test3 = (cast<int>(a.max) * cast<int>(b.min) == cast<int>(a.max * b.min));
Expr test4 = (cast<int>(a.max) * cast<int>(b.max) == cast<int>(a.max * b.max));
if (!can_prove(test1 && test2 && test3 && test4)) {
bounds_of_type(op->type);
}
} else {
bounds_of_type(op->type);
}
}
}
void visit(const Div *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (!b.is_bounded()) {
interval = Interval::everything();
} else if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (can_prove(b.min == b.max)) {
Expr e1 = a.has_lower_bound() ? a.min / b.min : a.min;
Expr e2 = a.has_upper_bound() ? a.max / b.max : a.max;
if (is_positive_const(b.min) || op->type.is_uint()) {
interval = Interval(e1, e2);
} else if (is_negative_const(b.min)) {
interval = Interval(e2, e1);
} else if (a.is_bounded()) {
// Sign of b is unknown. Note that this might divide
// by zero, but only in cases where the original code
// divides by zero.
Expr cmp = b.min > make_zero(b.min.type().element_of());
interval = Interval(select(cmp, e1, e2), select(cmp, e2, e1));
} else {
interval = Interval::everything();
}
} else if (a.is_bounded()) {
// if we can't statically prove that the divisor can't span zero, then we're unbounded
int min_sign = static_sign(b.min);
int max_sign = static_sign(b.max);
if (min_sign != max_sign || min_sign == 0 || max_sign == 0) {
interval = Interval::everything();
} else {
// Divisor is either strictly positive or strictly
// negative, so we can just take the extrema.
interval = Interval::nothing();
interval.include(a.min / b.min);
interval.include(a.max / b.min);
interval.include(a.min / b.max);
interval.include(a.max / b.max);
}
} else {
interval = Interval::everything();
}
}
void visit(const Mod *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
if (!interval.is_bounded()) {
return;
}
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
return;
}
Type t = op->type.element_of();
if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min % b.min);
} else {
// Only consider B (so A can be unbounded)
if (b.max.type().is_uint() || (b.max.type().is_int() && is_positive_const(b.min))) {
// If the RHS is a positive integer, the result is in [0, max_b-1]
interval = Interval(make_zero(t), b.max - make_one(t));
} else if (b.max.type().is_int()) {
// mod is always positive
// x % [4,10] -> [0,9]
// x % [-8,-3] -> [0,7]
// x % [-8, 10] -> [0,9]
interval = Interval(make_zero(t), Max::make(abs(b.min), abs(b.max)) - make_one(t));
} else {
// The floating point version has the same sign rules,
// but can reach all the way up to the original value,
// so there's no -1.
interval = Interval(make_zero(t), Max::make(abs(b.min), abs(b.max)));
}
}
}
void visit(const Min *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else {
interval = Interval(Interval::make_min(a.min, b.min),
Interval::make_min(a.max, b.max));
}
}
void visit(const Max *op) {
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else {
interval = Interval(Interval::make_max(a.min, b.min),
Interval::make_max(a.max, b.max));
}
}
void visit(const EQ *op) {
bounds_of_type(op->type);
}
void visit(const NE *op) {
bounds_of_type(op->type);
}
void visit(const LT *op) {
bounds_of_type(op->type);
}
void visit(const LE *op) {
bounds_of_type(op->type);
}
void visit(const GT *op) {
bounds_of_type(op->type);
}
void visit(const GE *op) {
bounds_of_type(op->type);
}
void visit(const And *op) {
bounds_of_type(op->type);
}
void visit(const Or *op) {
bounds_of_type(op->type);
}
void visit(const Not *op) {
bounds_of_type(op->type);
}
void visit(const Select *op) {
op->true_value.accept(this);
if (!interval.is_bounded()) {
return;
}
Interval a = interval;
op->false_value.accept(this);
if (!interval.is_bounded()) {
return;
}
Interval b = interval;
bool const_scalar_condition =
(op->condition.type().is_scalar() &&
!expr_uses_vars(op->condition, scope));
if (a.min.same_as(b.min)) {
interval.min = a.min;
} else if (const_scalar_condition) {
interval.min = select(op->condition, a.min, b.min);
} else {
interval.min = Interval::make_min(a.min, b.min);
}
if (a.max.same_as(b.max)) {
interval.max = a.max;
} else if (const_scalar_condition) {
interval.max = select(op->condition, a.max, b.max);
} else {
interval.max = Interval::make_max(a.max, b.max);
}
}
void visit(const Load *op) {
op->index.accept(this);
if (interval.is_single_point()) {
// If the index is const we can return the load of that index
Expr load_min =
Load::make(op->type.element_of(), op->name, interval.min, op->image, op->param);
interval = Interval::single_point(load_min);
} else {
// Otherwise use the bounds of the type
bounds_of_type(op->type);
}
}
void visit(const Ramp *op) {
// Treat the ramp lane as a free variable
string var_name = unique_name('t');
Expr var = Variable::make(op->base.type(), var_name);
Expr lane = op->base + var * op->stride;
scope.push(var_name, Interval(make_const(var.type(), 0),
make_const(var.type(), op->lanes-1)));
lane.accept(this);
scope.pop(var_name);
}
void visit(const Broadcast *op) {
op->value.accept(this);
}
void visit(const Call *op) {
// If the args are const we can return the call of those args
// for pure functions. For other types of functions, the same
// call in two different places might produce different
// results (e.g. during the update step of a reduction), so we
// can't move around call nodes.
std::vector<Expr> new_args(op->args.size());
bool const_args = true;
for (size_t i = 0; i < op->args.size() && const_args; i++) {
op->args[i].accept(this);
if (interval.is_single_point()) {
new_args[i] = interval.min;
} else {
const_args = false;
}
}
Type t = op->type.element_of();
if (t.is_handle()) {
interval = Interval::everything();
return;
}
if (const_args &&
(op->call_type == Call::PureExtern ||
op->call_type == Call::Image)) {
Expr call = Call::make(t, op->name, new_args, op->call_type,
op->func, op->value_index, op->image, op->param);
interval = Interval::single_point(call);
} else if (op->is_intrinsic(Call::abs)) {
Interval a = interval;
interval.min = make_zero(t);
if (a.is_bounded()) {
if (equal(a.min, a.max)) {
interval = Interval::single_point(Call::make(t, Call::abs, {a.max}, Call::PureIntrinsic));
} else if (op->args[0].type().is_int() && op->args[0].type().bits() >= 32) {
interval.max = Max::make(Cast::make(t, -a.min), Cast::make(t, a.max));
} else {
a.min = Call::make(t, Call::abs, {a.min}, Call::PureIntrinsic);
a.max = Call::make(t, Call::abs, {a.max}, Call::PureIntrinsic);
interval.max = Max::make(a.min, a.max);
}
} else {
// If the argument is unbounded on one side, then the max is unbounded.
interval.max = Interval::pos_inf;
}
} else if (op->is_intrinsic(Call::likely) ||
op->is_intrinsic(Call::likely_if_innermost)) {
assert(op->args.size() == 1);
op->args[0].accept(this);
} else if (op->is_intrinsic(Call::return_second)) {
assert(op->args.size() == 2);
op->args[1].accept(this);
} else if (op->is_intrinsic(Call::if_then_else)) {
assert(op->args.size() == 3);
// Probably more conservative than necessary
Expr equivalent_select = Select::make(op->args[0], op->args[1], op->args[2]);
equivalent_select.accept(this);
} else if (op->is_intrinsic(Call::shift_left) ||
op->is_intrinsic(Call::shift_right) ||
op->is_intrinsic(Call::bitwise_and)) {
Expr simplified = simplify(op);
if (!equal(simplified, op)) {
simplified.accept(this);
} else {
// Just use the bounds of the type
bounds_of_type(t);
}
} else if (op->args.size() == 1 && interval.is_bounded() &&
(op->name == "ceil_f32" || op->name == "ceil_f64" ||
op->name == "floor_f32" || op->name == "floor_f64" ||
op->name == "round_f32" || op->name == "round_f64" ||
op->name == "exp_f32" || op->name == "exp_f64" ||
op->name == "log_f32" || op->name == "log_f64")) {
// For monotonic, pure, single-argument functions, we can
// make two calls for the min and the max.
interval = Interval(
Call::make(t, op->name, {interval.min}, op->call_type,
op->func, op->value_index, op->image, op->param),
Call::make(t, op->name, {interval.max}, op->call_type,
op->func, op->value_index, op->image, op->param));
} else if (op->is_intrinsic(Call::extract_buffer_min) ||
op->is_intrinsic(Call::extract_buffer_max)) {
// Bounds query results should have perfect nesting. Their
// max over a loop is just the same bounds query call at
// an outer loop level. This requires that the query is
// also done at the outer loop level so that the buffer
// arg is still valid, which it is, so it is.
//
// TODO: There should be an assert injected in the inner
// loop to check perfect nesting.
interval = Interval(
Call::make(Int(32), Call::extract_buffer_min, op->args, Call::PureIntrinsic),
Call::make(Int(32), Call::extract_buffer_max, op->args, Call::PureIntrinsic));
} else if (op->is_intrinsic(Call::memoize_expr)) {
internal_assert(op->args.size() >= 1);
op->args[0].accept(this);
} else if (op->is_intrinsic(Call::trace_expr)) {
// trace_expr returns argument 4
internal_assert(op->args.size() >= 5);
op->args[4].accept(this);
} else if (op->call_type == Call::Halide) {
bounds_of_func(op->name, op->value_index, op->type);
} else {
// Just use the bounds of the type
bounds_of_type(t);
}
}
void visit(const Let *op) {
op->value.accept(this);
Interval val = interval;
// We'll either substitute the values in directly, or pass
// them in as variables and add an outer let (to avoid
// combinatorial explosion).
Interval var;
string min_name = op->name + ".min";
string max_name = op->name + ".max";
if (val.has_lower_bound()) {
if (is_const(val.min)) {
// Substitute it in
var.min = val.min;
val.min = Expr();
} else {
var.min = Variable::make(op->value.type().element_of(), min_name);
}
}
if (val.has_upper_bound()) {
if (is_const(val.max)) {
// Substitute it in
var.max = val.max;
val.max = Expr();
} else {
var.max = Variable::make(op->value.type().element_of(), max_name);
}
}
scope.push(op->name, var);
op->body.accept(this);
scope.pop(op->name);
if (interval.has_lower_bound()) {
if (val.min.defined() && expr_uses_var(interval.min, min_name)) {
interval.min = Let::make(min_name, val.min, interval.min);
}
if (val.max.defined() && expr_uses_var(interval.min, max_name)) {
interval.min = Let::make(max_name, val.max, interval.min);
}
}
if (interval.has_upper_bound()) {
if (val.min.defined() && expr_uses_var(interval.max, min_name)) {
interval.max = Let::make(min_name, val.min, interval.max);
}
if (val.max.defined() && expr_uses_var(interval.max, max_name)) {
interval.max = Let::make(max_name, val.max, interval.max);
}
}
}
void visit(const LetStmt *) {
internal_error << "Bounds of statement\n";
}
void visit(const AssertStmt *) {
internal_error << "Bounds of statement\n";
}
void visit(const ProducerConsumer *) {
internal_error << "Bounds of statement\n";
}
void visit(const For *) {
internal_error << "Bounds of statement\n";
}
void visit(const Store *) {
internal_error << "Bounds of statement\n";
}
void visit(const Provide *) {
internal_error << "Bounds of statement\n";
}
void visit(const Allocate *) {
internal_error << "Bounds of statement\n";
}
void visit(const Realize *) {
internal_error << "Bounds of statement\n";
}
void visit(const Block *) {
internal_error << "Bounds of statement\n";
}
};
Interval bounds_of_expr_in_scope(Expr expr, const Scope<Interval> &scope, const FuncValueBounds &fb) {
//debug(3) << "computing bounds_of_expr_in_scope " << expr << "\n";
Bounds b(&scope, fb);
expr.accept(&b);
//debug(3) << "bounds_of_expr_in_scope " << expr << " = " << simplify(b.min) << ", " << simplify(b.max) << "\n";
if (b.interval.has_lower_bound()) {
internal_assert(b.interval.min.type().is_scalar())
<< "Min of " << expr
<< " should have been a scalar: " << b.interval.min << "\n";
}
if (b.interval.has_upper_bound()) {
internal_assert(b.interval.max.type().is_scalar())
<< "Max of " << expr
<< " should have been a scalar: " << b.interval.max << "\n";
}
return b.interval;
}
Region region_union(const Region &a, const Region &b) {
internal_assert(a.size() == b.size()) << "Mismatched dimensionality in region union\n";
Region result;
for (size_t i = 0; i < a.size(); i++) {
Expr min = Min::make(a[i].min, b[i].min);
Expr max_a = a[i].min + a[i].extent;
Expr max_b = b[i].min + b[i].extent;
Expr max_plus_one = Max::make(max_a, max_b);
Expr extent = max_plus_one - min;
result.push_back(Range(simplify(min), simplify(extent)));
//result.push_back(Range(min, extent));
}
return result;
}
void merge_boxes(Box &a, const Box &b) {
if (b.empty()) {
return;
}
if (a.empty()) {
a = b;
return;
}
internal_assert(a.size() == b.size());
bool a_maybe_unused = a.maybe_unused();
bool b_maybe_unused = b.maybe_unused();
bool complementary = a_maybe_unused && b_maybe_unused &&
(equal(a.used, !b.used) || equal(!a.used, b.used));
for (size_t i = 0; i < a.size(); i++) {
if (!a[i].min.same_as(b[i].min)) {
if (a[i].has_lower_bound() && b[i].has_lower_bound()) {
if (a_maybe_unused && b_maybe_unused) {
if (complementary) {
a[i].min = select(a.used, a[i].min, b[i].min);
} else {
a[i].min = select(a.used && b.used, Interval::make_min(a[i].min, b[i].min),
a.used, a[i].min,
b[i].min);
}
} else if (a_maybe_unused) {
a[i].min = select(a.used, Interval::make_min(a[i].min, b[i].min), b[i].min);
} else if (b_maybe_unused) {
a[i].min = select(b.used, Interval::make_min(a[i].min, b[i].min), a[i].min);
} else {
a[i].min = Interval::make_min(a[i].min, b[i].min);
}
a[i].min = simplify(a[i].min);
} else {
a[i].min = Interval::neg_inf;
}
}
if (!a[i].max.same_as(b[i].max)) {
if (a[i].has_upper_bound() && b[i].has_upper_bound()) {
if (a_maybe_unused && b_maybe_unused) {
if (complementary) {
a[i].max = select(a.used, a[i].max, b[i].max);
} else {
a[i].max = select(a.used && b.used, Interval::make_max(a[i].max, b[i].max),
a.used, a[i].max,
b[i].max);
}
} else if (a_maybe_unused) {
a[i].max = select(a.used, Interval::make_max(a[i].max, b[i].max), b[i].max);
} else if (b_maybe_unused) {
a[i].max = select(b.used, Interval::make_max(a[i].max, b[i].max), a[i].max);
} else {
a[i].max = Interval::make_max(a[i].max, b[i].max);
}
a[i].max = simplify(a[i].max);
} else {
a[i].max = Interval::pos_inf;
}
}
}
if (a_maybe_unused && b_maybe_unused) {
if (!equal(a.used, b.used)) {
a.used = simplify(a.used || b.used);
if (is_one(a.used)) {
a.used = Expr();
}
}
} else {
a.used = Expr();
}
}
Box box_union(const Box &a, const Box &b) {
Box result = a;
merge_boxes(result, b);
return result;
}
bool boxes_overlap(const Box &a, const Box &b) {
// If one box is scalar and the other is not, the boxes cannot
// overlap.
if (a.size() != b.size() && (a.size() == 0 || b.size() == 0)) {
return false;
}
internal_assert(a.size() == b.size());
bool a_maybe_unused = a.maybe_unused();
bool b_maybe_unused = b.maybe_unused();
// Overlapping requires both boxes to be used.
Expr overlap = ((a_maybe_unused ? a.used : const_true()) &&
(b_maybe_unused ? b.used : const_true()));
for (size_t i = 0; i < a.size(); i++) {
if (a[i].has_upper_bound() && b[i].has_lower_bound()) {
overlap = overlap && b[i].max >= a[i].min;
}
if (a[i].has_lower_bound() && b[i].has_upper_bound()) {
overlap = overlap && a[i].max >= b[i].min;
}
}
// Conservatively, assume they overlap if we can't prove there's no overlap
return !can_prove(simplify(!overlap));
}
bool box_contains(const Box &outer, const Box &inner) {
// If the inner box has more dimensions than the outer box, the
// inner box cannot fit in the outer box.
if (inner.size() > outer.size()) {
return false;
}
Expr condition = const_true();
for (size_t i = 0; i < inner.size(); i++) {
condition = (condition &&
(outer[i].min <= inner[i].min) &&
(outer[i].max >= inner[i].max));
}
if (outer.maybe_unused()) {
if (inner.maybe_unused()) {
// inner condition must imply outer one
condition = condition && ((outer.used && inner.used) == inner.used);
} else {
// outer box is conditional, but inner is not
return false;
}
}
return is_one(simplify(condition));
}
// Compute the box produced by a statement
class BoxesTouched : public IRGraphVisitor {
public:
BoxesTouched(bool calls, bool provides, string fn, const Scope<Interval> *s, const FuncValueBounds &fb) :
func(fn), consider_calls(calls), consider_provides(provides), func_bounds(fb) {
scope.set_containing_scope(s);
}
map<string, Box> boxes;
private:
string func;
bool consider_calls, consider_provides;
Scope<Interval> scope;
const FuncValueBounds &func_bounds;
using IRGraphVisitor::visit;
void visit(const Call *op) {
if (!consider_calls) return;
// Calls inside of an address_of aren't touched, because no
// actual memory access takes place.
if (op->is_intrinsic(Call::address_of)) {
// Visit the args of the inner call
internal_assert(op->args.size() == 1);
const Call *c = op->args[0].as<Call>();
if (c) {
for (size_t i = 0; i < c->args.size(); i++) {
c->args[i].accept(this);
}
} else {
const Load *l = op->args[0].as<Load>();
internal_assert(l);
l->index.accept(this);
}
return;
}
if (op->is_intrinsic(Call::if_then_else)) {
assert(op->args.size() == 3);
// We wrap 'then_case' and 'else_case' inside 'dummy' call since IfThenElse
// only takes Stmts as arguments.
Stmt then_case = Evaluate::make(op->args[1]);
Stmt else_case = Evaluate::make(op->args[2]);
Stmt equivalent_if = IfThenElse::make(op->args[0], then_case, else_case);
equivalent_if.accept(this);
return;
}
IRVisitor::visit(op);
if (op->call_type == Call::Halide ||