-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
op.cc
983 lines (847 loc) · 34.7 KB
/
op.cc
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file tir/op/op.cc
*
* Common operator definitions for ops in tir/op.h
*/
#include <tvm/runtime/registry.h>
#include <tvm/tir/builtin.h>
#include <tvm/tir/expr.h>
#include <tvm/tir/op.h>
#include <tvm/tir/op_attr_types.h>
#include <cmath>
// Centralized header for constant folders.
#include "../../arith/const_fold.h"
#include "../../target/datatype/registry.h"
namespace tvm {
using namespace tir;
// macro to register an unary op
#define TIR_REGISTER_PURE_UNARY_OP(OpName) \
TVM_REGISTER_OP(OpName).set_num_inputs(1).set_attr<TCallEffectKind>( \
"TCallEffectKind", Integer(CallEffectKind::kPure))
// macro to register an binary op
#define TIR_REGISTER_PURE_BINARY_OP(OpName) \
TVM_REGISTER_OP(OpName).set_num_inputs(2).set_attr<TCallEffectKind>( \
"TCallEffectKind", Integer(CallEffectKind::kPure))
runtime::DataType GetRuntimeDataType(const Type& type) {
if (auto* n = type.as<PrimTypeNode>()) {
return n->dtype;
} else if (type.as<PointerTypeNode>()) {
return DataType::Handle();
} else if (IsVoidType(type)) {
return DataType::Void();
} else {
LOG(FATAL) << "Type " << type << " does not have a corresponding runtime::DataType";
return DataType::Handle();
}
}
Type GetType(const PrimExpr& expr) {
// TODO(tqchen): add recursive type inference for Call here
// once we introduced the corresponding fields to the IR.
if (auto* ptr = expr.as<tir::VarNode>()) {
// If Var has a more refined type annotation,
// return the type anotation
if (ptr->type_annotation.defined()) {
return ptr->type_annotation;
}
}
// Default: return the type indicated by the dtype.
runtime::DataType dtype = expr.dtype();
if (dtype.is_void()) {
return VoidType();
}
return PrimType(dtype);
}
// simple cast that only checks if type matches and cast
inline PrimExpr SimpleCast(const DataType& t, PrimExpr value, Span span) {
if (value.dtype() == t) return value;
return tir::Cast(t, value, span);
}
// LargeUIntImm
PrimExpr LargeUIntImm(DataType t, int64_t low, int64_t high, Span span) {
return tir::Call(
t, tir::builtin::large_uint_imm(),
{make_const(DataType::UInt(32), low, span), make_const(DataType::UInt(32), high, span)},
span);
}
// Q-multiplication
PrimExpr q_multiply_shift(PrimExpr x, PrimExpr y, PrimExpr q, PrimExpr s, Span span) {
return tir::Call(DataType::Int(32, x.dtype().lanes()), tir::builtin::q_multiply_shift(),
{x, y, q, s}, span);
}
// The public function with a quick checking path.
void BinaryOpMatchTypes(PrimExpr& lhs, PrimExpr& rhs, Span span) { // NOLINT(*)
if (lhs.dtype() == rhs.dtype()) return;
DataType ltype = lhs.dtype();
DataType rtype = rhs.dtype();
if (ltype.lanes() == 1 && rtype.lanes() != 1) {
lhs = tir::Broadcast(lhs, rtype.lanes());
} else if (rtype.lanes() == 1 && ltype.lanes() != 1) {
rhs = tir::Broadcast(rhs, ltype.lanes());
} else {
ICHECK(ltype.lanes() == rtype.lanes()) << "Cannot match type " << ltype << " vs " << rtype;
}
if (lhs.dtype() == rhs.dtype()) return;
// We keep dtypes conversion to be relatively consistent to reduce the amount code generated by
// operators. This can be helpful for users to find potential type conversion problems. The
// following are exceptions:
if (lhs.dtype().is_float() && rhs.dtype().is_float()) {
// Given two dissimilar floats, cast the lower bit version to the higher bit version.
// E.g. fp16 + fp32 --> fp32 + fp32
if (lhs.dtype().bits() < rhs.dtype().bits()) {
lhs = cast(rhs.dtype(), lhs);
} else if (lhs.dtype().bits() > rhs.dtype().bits()) {
rhs = cast(lhs.dtype(), rhs);
}
} else if (!lhs.dtype().is_float() &&
(rhs.dtype().is_float() ||
datatype::Registry::Global()->GetTypeRegistered(rhs.dtype().code()))) {
// Cast int->float when the other operand is a float
lhs = cast(rhs.dtype(), lhs);
} else if ((lhs.dtype().is_float() ||
datatype::Registry::Global()->GetTypeRegistered(lhs.dtype().code())) &&
!rhs.dtype().is_float()) {
// Cast int->float when the other operand is a float
rhs = cast(lhs.dtype(), rhs);
} else if ((lhs.dtype().is_int() && rhs.dtype().is_int()) ||
(lhs.dtype().is_uint() && rhs.dtype().is_uint())) {
// Promote int to higher bits e.g. int8 + int16 --> int16 + int16
if (lhs.dtype().bits() < rhs.dtype().bits()) {
lhs = cast(rhs.dtype(), lhs);
} else {
rhs = cast(lhs.dtype(), rhs);
}
} else if ((lhs.dtype().is_int() && rhs.dtype().is_uint()) ||
(lhs.dtype().is_uint() && rhs.dtype().is_int())) {
// Handle mixing signed and unsigned integers
int bits = std::max(lhs.dtype().bits(), rhs.dtype().bits());
// if the signed int range is bigger than that of uint, try uint->int
if (lhs.dtype().is_int() && rhs.dtype().bits() <= bits - 1) {
rhs = cast(lhs.dtype(), rhs);
} else if (rhs.dtype().is_int() && lhs.dtype().bits() <= bits - 1) {
lhs = cast(rhs.dtype(), lhs);
} else {
// the ranges of uint and int types conflit, try SimpleCast
lhs = SimpleCast(DataType::Int(bits, lhs.dtype().lanes()), lhs, span);
rhs = SimpleCast(DataType::Int(bits, rhs.dtype().lanes()), rhs, span);
}
} else {
LOG(FATAL) << "Cannot match type " << ltype << " vs " << rtype;
}
}
PrimExpr ret(PrimExpr value, Span span) {
return tir::Call(value.dtype(), tir::builtin::ret(), {value}, span);
}
// maximum and min limits
PrimExpr max_value(const DataType& dtype, Span span) {
using namespace tir;
ICHECK_EQ(dtype.lanes(), 1);
if (dtype.is_int()) {
if (dtype.bits() == 64) {
return IntImm(dtype, std::numeric_limits<int64_t>::max(), span);
} else if (dtype.bits() < 64) {
int64_t val = 1;
val = (val << (dtype.bits() - 1)) - 1;
return IntImm(dtype, val, span);
}
} else if (dtype.is_uint()) {
if (dtype.bits() == 64) {
return make_const(dtype, std::numeric_limits<uint64_t>::max(), span);
} else if (dtype.bits() < 64) {
uint64_t val = 1;
val = (val << static_cast<uint64_t>(dtype.bits())) - 1;
return IntImm(dtype, static_cast<int64_t>(val), span);
}
} else if (dtype.is_float()) {
if (dtype.bits() == 64) {
return FloatImm(dtype, std::numeric_limits<double>::max(), span);
} else if (dtype.bits() == 32) {
return FloatImm(dtype, std::numeric_limits<float>::max(), span);
} else if (dtype.bits() == 16) {
return FloatImm(dtype, 65504.0, span);
}
}
LOG(FATAL) << "Cannot decide max_value for type" << dtype;
return PrimExpr();
}
PrimExpr min_value(const DataType& dtype, Span span) {
using namespace tir;
ICHECK_EQ(dtype.lanes(), 1);
if (datatype::Registry::Global()->GetTypeRegistered(dtype.code())) {
// TODO(tkonolige): need to convert all registered min functions to use the span.
auto f = datatype::GetMinFunc(dtype.code());
ICHECK(f) << "No minimum function registered for custom dtype " << (unsigned int)dtype.code();
// TODO(@hypercubestart) Document this change (and others associated with the overflowing
// floatimm min bug)
return (*f)(dtype.bits());
} else if (dtype.is_int()) {
if (dtype.bits() == 64) {
return IntImm(dtype, std::numeric_limits<int64_t>::lowest(), span);
} else if (dtype.bits() < 64) {
int64_t val = 1;
val = -(val << (dtype.bits() - 1));
return IntImm(dtype, val, span);
}
} else if (dtype.is_uint()) {
return IntImm(dtype, 0, span);
} else if (dtype.is_float()) {
if (dtype.bits() == 64) {
return FloatImm(dtype, std::numeric_limits<double>::lowest(), span);
} else if (dtype.bits() == 32) {
return FloatImm(dtype, std::numeric_limits<float>::lowest(), span);
} else if (dtype.bits() == 16) {
return FloatImm(dtype, -65504.0, span);
}
}
LOG(FATAL) << "Cannot decide min_value for type" << dtype;
return PrimExpr();
}
// infinity
PrimExpr infinity(const DataType& dtype, Span span) {
using namespace tir;
ICHECK_EQ(dtype.lanes(), 1);
if (dtype.is_float()) {
if (dtype.bits() == 64) {
return FloatImm(dtype, std::numeric_limits<double>::infinity(), span);
} else if (dtype.bits() == 32 || dtype.bits() == 16) {
return FloatImm(dtype, std::numeric_limits<float>::infinity(), span);
}
}
LOG(FATAL) << "Cannot decide infinity for type " << dtype;
return PrimExpr();
}
namespace tir {
template <typename ValueType>
inline bool ConstPowerHelper(ValueType val, int* shift) {
if (val <= 0) return false;
shift[0] = 0;
while (val != 0) {
if (val & 1) {
return (val == 1);
}
++shift[0];
val = val >> 1;
}
return true;
}
bool is_const_power_of_two_integer(const PrimExpr& x, int* shift) {
if (const auto* op = x.as<tir::IntImmNode>()) {
return ConstPowerHelper(op->value, shift);
} else {
return false;
}
}
} // namespace tir
PrimExpr cast(const DataType& t, PrimExpr value, Span span) {
using tir::FloatImmNode;
if (value.dtype() == t) return value;
// const fold IntImm as they are used in index computations
if (t.lanes() == 1) {
if (const IntImmNode* op = value.as<IntImmNode>()) {
return make_const(t, op->value, op->span);
} else if (const FloatImmNode* op = value.as<FloatImmNode>()) {
return make_const(t, op->value, op->span);
}
return tir::Cast(t, value, span);
} else {
if (value.dtype().lanes() == 1) {
// manually unroll cast
DataType vtype = t.element_of();
if (value.dtype() != vtype) {
if (const IntImmNode* op = value.as<IntImmNode>()) {
value = make_const(vtype, op->value, op->span);
} else if (const FloatImmNode* op = value.as<FloatImmNode>()) {
value = make_const(vtype, op->value, op->span);
} else {
value = tir::Cast(vtype, value, span);
}
}
return tir::Broadcast(value, t.lanes(), span);
} else {
ICHECK(value.dtype().lanes() == t.lanes());
return tir::Cast(t, value, span);
}
}
}
// reinterpret
PrimExpr reinterpret(const DataType& t, PrimExpr value, Span span) {
if (value.dtype() == t) return value;
return tir::Call(t, tir::builtin::reinterpret(), {value}, span);
}
// operator+
PrimExpr operator+(PrimExpr a, PrimExpr b) { return add(a, b); }
PrimExpr add(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Add>(a, b);
if (ret.defined()) return ret;
return tir::Add(a, b, span);
}
// negation
PrimExpr operator-(PrimExpr a) { return neg(a); }
PrimExpr neg(PrimExpr a, Span span) {
using tir::FloatImmNode;
using tir::IntImmNode;
const IntImmNode* pa = a.as<IntImmNode>();
const FloatImmNode* fa = a.as<FloatImmNode>();
if (pa) return IntImm(a.dtype(), -pa->value, span);
if (fa) return FloatImm(a.dtype(), -fa->value, span);
return make_zero(a.dtype(), span) - a;
}
PrimExpr operator-(PrimExpr a, PrimExpr b) { return sub(a, b); }
PrimExpr sub(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Sub>(a, b);
if (ret.defined()) return ret;
return tir::Sub(a, b, span);
}
PrimExpr operator*(PrimExpr a, PrimExpr b) { return mul(a, b); }
PrimExpr mul(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Mul>(a, b);
if (ret.defined()) return ret;
return tir::Mul(a, b, span);
}
PrimExpr div(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Div>(a, b);
if (ret.defined()) return ret;
return tir::Div(a, b, span);
}
PrimExpr truncdiv(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint()) << a;
ICHECK(b.dtype().is_int() || b.dtype().is_uint()) << b;
return div(a, b, span);
}
PrimExpr truncmod(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Mod>(a, b);
if (ret.defined()) return ret;
return tir::Mod(a, b, span);
}
PrimExpr operator/(PrimExpr a, PrimExpr b) { return div(a, b); }
PrimExpr operator%(PrimExpr a, PrimExpr b) { return truncmod(a, b); }
// TODO(tqchen): switch to floordiv
PrimExpr indexdiv(PrimExpr a, PrimExpr b, Span span) { return floordiv(a, b, span); }
PrimExpr indexmod(PrimExpr a, PrimExpr b, Span span) { return floormod(a, b, span); }
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint()) << a;
ICHECK(b.dtype().is_int() || b.dtype().is_uint()) << b;
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::FloorDiv>(a, b);
if (ret.defined()) return ret;
return tir::FloorDiv(a, b, span);
}
PrimExpr floormod(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint()) << a;
ICHECK(b.dtype().is_int() || b.dtype().is_uint()) << b;
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::FloorMod>(a, b);
if (ret.defined()) return ret;
return tir::FloorMod(a, b, span);
}
PrimExpr min(PrimExpr a, PrimExpr b, Span span) {
// inf-aware simplificaiton
using arith::is_neg_inf;
using arith::is_pos_inf;
if (is_pos_inf(a)) return b;
if (is_neg_inf(a)) return a;
if (is_pos_inf(b)) return a;
if (is_neg_inf(b)) return b;
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Min>(a, b);
if (ret.defined()) return ret;
return tir::Min(a, b, span);
}
PrimExpr max(PrimExpr a, PrimExpr b, Span span) {
// inf-aware simplificaiton
using arith::is_neg_inf;
using arith::is_pos_inf;
if (is_pos_inf(a)) return a;
if (is_neg_inf(a)) return b;
if (is_pos_inf(b)) return b;
if (is_neg_inf(b)) return a;
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::Max>(a, b);
if (ret.defined()) return ret;
return tir::Max(a, b, span);
}
// if_then_else
PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Span span) {
ICHECK(cond.dtype() == DataType::Bool(1))
<< "if_then_else only accept the condition to be boolean type.";
BinaryOpMatchTypes(true_value, false_value, span);
if (const IntImmNode* op = cond.as<IntImmNode>()) {
if (op->value != 0) {
return true_value;
} else {
return false_value;
}
}
return tir::Call(true_value.dtype(), tir::builtin::if_then_else(),
{cond, true_value, false_value}, span);
}
// likely
PrimExpr likely(PrimExpr cond, Span span) {
if (is_const_int(cond)) return cond;
return tir::Call(cond.dtype(), tir::builtin::likely(), {cond}, span);
}
// operator>
PrimExpr operator>(PrimExpr a, PrimExpr b) { return greater(a, b); }
PrimExpr greater(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::GT>(a, b);
if (ret.defined()) return ret;
return tir::GT(a, b, span);
}
PrimExpr operator>=(PrimExpr a, PrimExpr b) { return greater_equal(a, b); }
PrimExpr greater_equal(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::GE>(a, b);
if (ret.defined()) return ret;
return tir::GE(a, b, span);
}
PrimExpr operator<(PrimExpr a, PrimExpr b) { return less(a, b); }
PrimExpr less(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::LT>(a, b);
if (ret.defined()) return ret;
return tir::LT(a, b, span);
}
PrimExpr operator<=(PrimExpr a, PrimExpr b) { return less_equal(a, b); }
PrimExpr less_equal(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::LE>(a, b);
if (ret.defined()) return ret;
return tir::LE(a, b, span);
}
PrimExpr operator==(PrimExpr a, PrimExpr b) { return equal(a, b); }
PrimExpr equal(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::EQ>(a, b);
if (ret.defined()) return ret;
return tir::EQ(a, b, span);
}
PrimExpr operator!=(PrimExpr a, PrimExpr b) { return not_equal(a, b); }
PrimExpr not_equal(PrimExpr a, PrimExpr b, Span span) {
BinaryOpMatchTypes(a, b, span);
PrimExpr ret = arith::TryConstFold<tir::NE>(a, b);
if (ret.defined()) return ret;
return tir::NE(a, b, span);
}
PrimExpr operator&&(PrimExpr a, PrimExpr b) { return logical_and(a, b); }
PrimExpr logical_and(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_bool());
ICHECK(b.dtype().is_bool());
PrimExpr ret = arith::TryConstFold<tir::And>(a, b);
if (ret.defined()) return ret;
return tir::And(a, b, span);
}
PrimExpr operator||(PrimExpr a, PrimExpr b) { return logical_or(a, b); }
PrimExpr logical_or(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_bool());
ICHECK(b.dtype().is_bool());
PrimExpr ret = arith::TryConstFold<tir::Or>(a, b);
if (ret.defined()) return ret;
return tir::Or(a, b, span);
}
PrimExpr operator!(PrimExpr a) { return logical_not(a); }
PrimExpr logical_not(PrimExpr a, Span span) {
ICHECK(a.dtype().is_bool());
PrimExpr ret = arith::TryConstFold<tir::Not>(a);
if (ret.defined()) return ret;
return tir::Not(a, span);
}
// shift right
PrimExpr operator>>(PrimExpr a, PrimExpr b) { return right_shift(a, b); }
PrimExpr right_shift(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint());
ICHECK(b.dtype().is_int() || b.dtype().is_uint());
BinaryOpMatchTypes(a, b, span);
TVM_INDEX_CONST_PROPAGATION({
const DataType& rtype = a.dtype();
if (pb)
ICHECK(pb->value >= 0 && pb->value < rtype.bits())
<< "Shift amount must be non-negative and less than " << rtype.bits() << " for type "
<< rtype;
if (pa && pb) {
return IntImm(rtype, (pa->value >> pb->value), span);
}
if (pb) {
if (pb->value == 0) return a;
}
});
return tir::Call(a.dtype(), tir::builtin::shift_right(), {a, b}, span);
}
// shift left
PrimExpr operator<<(PrimExpr a, PrimExpr b) { return left_shift(a, b); }
PrimExpr left_shift(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint());
ICHECK(b.dtype().is_int() || b.dtype().is_uint());
BinaryOpMatchTypes(a, b, span);
TVM_INDEX_CONST_PROPAGATION({
const DataType& rtype = a.dtype();
if (pb)
ICHECK(pb->value >= 0 && pb->value < rtype.bits())
<< "Shift amount must be non-negative and less than " << rtype.bits() << " for type "
<< rtype;
if (pa && pb) return IntImm(rtype, (pa->value << pb->value), span);
if (pb) {
if (pb->value == 0) return a;
}
});
return tir::Call(a.dtype(), tir::builtin::shift_left(), {a, b}, span);
}
// bitwise and
PrimExpr operator&(PrimExpr a, PrimExpr b) { return bitwise_and(a, b); }
PrimExpr bitwise_and(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint());
ICHECK(b.dtype().is_int() || b.dtype().is_uint());
BinaryOpMatchTypes(a, b, span);
TVM_INDEX_CONST_PROPAGATION({
const DataType& rtype = a.dtype();
if (pa && pb) return IntImm(rtype, (pa->value & pb->value), span);
});
return tir::Call(a.dtype(), tir::builtin::bitwise_and(), {a, b}, span);
}
// bitwise_or
PrimExpr operator|(PrimExpr a, PrimExpr b) { return bitwise_or(a, b); }
PrimExpr bitwise_or(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint());
ICHECK(b.dtype().is_int() || b.dtype().is_uint());
BinaryOpMatchTypes(a, b, span);
TVM_INDEX_CONST_PROPAGATION({
const DataType& rtype = a.dtype();
if (pa && pb) return IntImm(rtype, (pa->value | pb->value), span);
});
return tir::Call(a.dtype(), tir::builtin::bitwise_or(), {a, b}, span);
}
// bitwise_xor
PrimExpr operator^(PrimExpr a, PrimExpr b) { return bitwise_xor(a, b); }
PrimExpr bitwise_xor(PrimExpr a, PrimExpr b, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint());
ICHECK(b.dtype().is_int() || b.dtype().is_uint());
BinaryOpMatchTypes(a, b, span);
TVM_INDEX_CONST_PROPAGATION({
const DataType& rtype = a.dtype();
if (pa && pb) return IntImm(rtype, (pa->value ^ pb->value), span);
});
return tir::Call(a.dtype(), tir::builtin::bitwise_xor(), {a, b}, span);
}
// bitwise_not
PrimExpr operator~(PrimExpr a) { return bitwise_neg(a); }
PrimExpr bitwise_neg(PrimExpr a, Span span) {
ICHECK(a.dtype().is_int() || a.dtype().is_uint());
return tir::Call(a.dtype(), tir::builtin::bitwise_not(), {a}, span);
}
TVM_REGISTER_GLOBAL("tir.bitwise_not").set_body_typed([](PrimExpr a, Span span) {
return bitwise_neg(a, span);
});
// pow
PrimExpr pow(PrimExpr x, PrimExpr y, Span span) {
BinaryOpMatchTypes(x, y, span);
ICHECK(x.dtype().is_float()) << "power only applies to float";
static auto op = Op::Get("tir.pow");
return tir::Call(x.dtype(), op, {x, y}, span);
}
TIR_REGISTER_PURE_BINARY_OP("tir.pow").set_attr<TVectorizable>("TVectorizable", true);
// abs
PrimExpr abs(PrimExpr x, Span span) {
if (x.dtype().is_int()) {
using tir::IntImmNode;
const IntImmNode* px = x.as<IntImmNode>();
if (px) {
return IntImm(x.dtype(), std::abs(px->value), px->span);
}
return tir::Select(x >= make_zero(x.dtype()), x, -x, span);
} else if (x.dtype().is_float()) {
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) {
return FloatImm(x.dtype(), std::fabs(fx->value), fx->span);
}
static auto op = Op::Get("tir.fabs");
return tir::Call(x.dtype(), op, {x}, span);
} else if (x.dtype().is_uint()) {
return x;
} else {
LOG(FATAL) << "Data type " << x.dtype()
<< " not supported for absolute op. Skipping absolute op...";
return x;
}
}
TIR_REGISTER_PURE_UNARY_OP("tir.fabs").set_attr<TVectorizable>("TVectorizable", true);
// isnan
PrimExpr isnan(PrimExpr x, Span span) {
DataType t = DataType::Bool(x.dtype().lanes());
if (x.dtype().is_int() || x.dtype().is_uint()) {
return make_const(t, false);
} else if (x.dtype().is_float()) {
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) {
return make_const(t, std::isnan(fx->value), fx->span);
}
static auto op = Op::Get("tir.isnan");
if (x.dtype().bits() == 16) {
return tir::Call(t, op, {cast(DataType::Float(32, t.lanes()), std::move(x), span)}, span);
} else {
return tir::Call(t, op, {x}, span);
}
} else {
LOG(FATAL) << "Data type " << x.dtype() << " not supported for isnan op. Skipping isnan op...";
return x;
}
}
// isinf
PrimExpr isinf(PrimExpr x, Span span) {
DataType t = DataType::Bool(x.dtype().lanes());
if (x.dtype().is_int() || x.dtype().is_uint()) {
return make_const(t, false, span);
} else if (x.dtype().is_float()) {
PrimExpr infX = infinity(x.dtype(), span);
return abs(x, span) == infX && !isnan(x, span);
} else {
LOG(FATAL) << "Data type " << x.dtype() << " not supported for finiteness ops. Skipping it...";
return x;
}
}
// isfinite
PrimExpr isfinite(PrimExpr x, Span span) { return !isinf(x, span) && !isnan(x, span); }
PrimExpr sum(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
Var x("x", source.dtype(), span), y("y", source.dtype(), span);
PrimExpr result = tir::Add(x, y, span);
PrimExpr identity_element = make_zero(source.dtype(), span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
PrimExpr all(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
ICHECK(source.dtype().is_bool());
Var x("x", source.dtype(), span), y("y", source.dtype());
PrimExpr result = tir::And(x, y, span);
PrimExpr identity_element = make_const(source.dtype(), true, span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
PrimExpr any(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
ICHECK(source.dtype().is_bool());
Var x("x", source.dtype(), span), y("y", source.dtype(), span);
PrimExpr result = tir::Or(x, y, span);
PrimExpr identity_element = make_const(source.dtype(), false, span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
PrimExpr max(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
Var x("x", source.dtype(), span), y("y", source.dtype(), span);
PrimExpr result = tir::Max(x, y, span);
PrimExpr identity_element = min_value(source.dtype(), span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
PrimExpr min(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
Var x("x", source.dtype(), span), y("y", source.dtype(), span);
PrimExpr result = tir::Min(x, y, span);
PrimExpr identity_element = max_value(source.dtype(), span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
PrimExpr prod(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
Var x("x", source.dtype(), span), y("y", source.dtype(), span);
PrimExpr result = tir::Mul(x, y, span);
PrimExpr identity_element = make_const(source.dtype(), 1, span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
// fmod
PrimExpr fmod(PrimExpr x, PrimExpr y, Span span) {
BinaryOpMatchTypes(x, y, span);
ICHECK(x.dtype().is_float()) << "fmod only applies to float";
static auto op = Op::Get("tir.fmod");
return tir::Call(x.dtype(), op, {x, y}, span);
}
TIR_REGISTER_PURE_UNARY_OP("tir.fmod");
// floor
PrimExpr floor(PrimExpr x, Span span) {
if (x.dtype().is_int() || x.dtype().is_uint()) {
return x;
}
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) return FloatImm(x.dtype(), std::floor(fx->value), fx->span);
static auto op = Op::Get("tir.floor");
return tir::Call(x.dtype(), op, {x}, span);
}
TIR_REGISTER_PURE_UNARY_OP("tir.floor").set_attr<TVectorizable>("TVectorizable", true);
// ceil
PrimExpr ceil(PrimExpr x, Span span) {
if (x.dtype().is_int() || x.dtype().is_uint()) {
return x;
}
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) return FloatImm(x.dtype(), std::ceil(fx->value), fx->span);
static auto op = Op::Get("tir.ceil");
return tir::Call(x.dtype(), op, {x}, span);
}
TIR_REGISTER_PURE_UNARY_OP("tir.ceil").set_attr<TVectorizable>("TVectorizable", true);
// round
PrimExpr round(PrimExpr x, Span span) {
if (x.dtype().is_int() || x.dtype().is_uint()) {
return x;
}
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) return FloatImm(x.dtype(), std::nearbyint(fx->value), fx->span);
static auto op = Op::Get("tir.round");
return tir::Call(x.dtype(), op, {x}, span);
}
TIR_REGISTER_PURE_UNARY_OP("tir.round").set_attr<TVectorizable>("TVectorizable", true);
// nearbyint
PrimExpr nearbyint(PrimExpr x, Span span) {
if (x.dtype().is_int() || x.dtype().is_uint()) {
return x;
}
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) return FloatImm(x.dtype(), std::nearbyint(fx->value), fx->span);
static auto op = Op::Get("tir.nearbyint");
return tir::Call(x.dtype(), op, {x}, span);
}
TIR_REGISTER_PURE_UNARY_OP("tir.nearbyint");
// trunc
PrimExpr trunc(PrimExpr x, Span span) {
if (x.dtype().is_int() || x.dtype().is_uint()) {
return x;
}
using tir::FloatImmNode;
const FloatImmNode* fx = x.as<FloatImmNode>();
if (fx) {
return FloatImm(x.dtype(), (fx->value < 0 ? std::ceil(fx->value) : std::floor(fx->value)),
fx->span);
}
static auto op = Op::Get("tir.trunc");
return tir::Call(x.dtype(), op, {x}, span);
}
TIR_REGISTER_PURE_UNARY_OP("tir.trunc").set_attr<TVectorizable>("TVectorizable", true);
// unary op registration.
TIR_REGISTER_PURE_UNARY_OP("tir.exp").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.exp2").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.exp10").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.erf");
TIR_REGISTER_PURE_UNARY_OP("tir.tanh").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.sigmoid");
TIR_REGISTER_PURE_UNARY_OP("tir.sqrt").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.rsqrt");
TIR_REGISTER_PURE_UNARY_OP("tir.log").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.log2").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.log1p");
TIR_REGISTER_PURE_UNARY_OP("tir.log10").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.tan").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.cos").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.cosh").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.sin").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.sinh").set_attr<TVectorizable>("TVectorizable", true);
TIR_REGISTER_PURE_UNARY_OP("tir.asin");
TIR_REGISTER_PURE_UNARY_OP("tir.acos");
TIR_REGISTER_PURE_UNARY_OP("tir.atan");
TIR_REGISTER_PURE_UNARY_OP("tir.acosh");
TIR_REGISTER_PURE_UNARY_OP("tir.asinh");
TIR_REGISTER_PURE_UNARY_OP("tir.atanh");
TIR_REGISTER_PURE_UNARY_OP("tir.clz");
// binary intrinsics
TIR_REGISTER_PURE_BINARY_OP("tir.atan2");
TIR_REGISTER_PURE_BINARY_OP("tir.nextafter");
TIR_REGISTER_PURE_BINARY_OP("tir.hypot");
TIR_REGISTER_PURE_BINARY_OP("tir.copysign");
TIR_REGISTER_PURE_BINARY_OP("tir.ldexp");
// expose basic functions to node namespace
TVM_REGISTER_GLOBAL("node._const").set_body([](TVMArgs args, TVMRetValue* ret) {
if (args[0].type_code() == kDLInt) {
*ret = tir::make_const(args[1], args[0].operator int64_t(), args[2]);
} else if (args[0].type_code() == kDLFloat) {
*ret = tir::make_const(args[1], args[0].operator double(), args[2]);
} else {
LOG(FATAL) << "only accept int or float"; // FIXME
}
});
TVM_REGISTER_GLOBAL("node.LargeUIntImm").set_body_typed(LargeUIntImm);
TVM_REGISTER_GLOBAL("tir.min_value").set_body_typed(min_value);
TVM_REGISTER_GLOBAL("tir.max_value").set_body_typed(max_value);
TVM_REGISTER_GLOBAL("tir.abs").set_body_typed(tvm::abs);
TVM_REGISTER_GLOBAL("tir.isnan").set_body_typed(tvm::isnan);
TVM_REGISTER_GLOBAL("tir.isfinite").set_body_typed(tvm::isfinite);
TVM_REGISTER_GLOBAL("tir.isinf").set_body_typed(tvm::isinf);
TVM_REGISTER_GLOBAL("tir.floor").set_body_typed(tvm::floor);
TVM_REGISTER_GLOBAL("tir.ceil").set_body_typed(tvm::ceil);
TVM_REGISTER_GLOBAL("tir.round").set_body_typed(tvm::round);
TVM_REGISTER_GLOBAL("tir.nearbyint").set_body_typed(tvm::nearbyint);
TVM_REGISTER_GLOBAL("tir.trunc").set_body_typed(tvm::trunc);
TVM_REGISTER_GLOBAL("tir._cast").set_body_typed(tvm::cast);
// operator overloading, smarter than make
#define REGISTER_MAKE_BINARY_OP(Node, Func) \
TVM_REGISTER_GLOBAL("tir." #Node).set_body_typed([](PrimExpr a, PrimExpr b, Span span) { \
return (Func(a, b, span)); \
})
#define REGISTER_MAKE_BIT_OP(Node, Func) \
TVM_REGISTER_GLOBAL("tir." #Node).set_body([](TVMArgs args, TVMRetValue* ret) { \
bool lhs_is_int = args[0].type_code() == kDLInt; \
bool rhs_is_int = args[1].type_code() == kDLInt; \
if (lhs_is_int) { \
*ret = (Func(args[0].operator int(), args[1].operator PrimExpr(), args[2])); \
} else if (rhs_is_int) { \
*ret = (Func(args[0].operator PrimExpr(), args[1].operator int(), args[2])); \
} else { \
*ret = (Func(args[0].operator PrimExpr(), args[1].operator PrimExpr(), args[2])); \
} \
})
REGISTER_MAKE_BINARY_OP(_OpAdd, add);
REGISTER_MAKE_BINARY_OP(_OpSub, sub);
REGISTER_MAKE_BINARY_OP(_OpMul, mul);
REGISTER_MAKE_BINARY_OP(_OpDiv, div);
REGISTER_MAKE_BINARY_OP(_OpMod, truncmod);
REGISTER_MAKE_BINARY_OP(_OpIndexDiv, indexdiv);
REGISTER_MAKE_BINARY_OP(_OpIndexMod, indexmod);
REGISTER_MAKE_BINARY_OP(_OpFloorDiv, floordiv);
REGISTER_MAKE_BINARY_OP(_OpFloorMod, floormod);
REGISTER_MAKE_BINARY_OP(_OpTruncDiv, truncdiv);
REGISTER_MAKE_BINARY_OP(_OpTruncMod, truncmod);
REGISTER_MAKE_BINARY_OP(_OpPow, pow);
REGISTER_MAKE_BINARY_OP(_OpMin, min);
REGISTER_MAKE_BINARY_OP(_OpMax, max);
REGISTER_MAKE_BINARY_OP(_OpEQ, equal);
REGISTER_MAKE_BINARY_OP(_OpNE, not_equal);
REGISTER_MAKE_BINARY_OP(_OpLT, less); // NOLINT(*)
REGISTER_MAKE_BINARY_OP(_OpLE, less_equal); // NOLINT(*)
REGISTER_MAKE_BINARY_OP(_OpGT, greater); // NOLINT(*)
REGISTER_MAKE_BINARY_OP(_OpGE, greater_equal);
REGISTER_MAKE_BINARY_OP(_OpAnd, logical_and);
REGISTER_MAKE_BINARY_OP(_OpOr, logical_or);
REGISTER_MAKE_BIT_OP(bitwise_and, bitwise_and);
REGISTER_MAKE_BIT_OP(bitwise_or, bitwise_or);
REGISTER_MAKE_BIT_OP(bitwise_xor, bitwise_xor);
REGISTER_MAKE_BIT_OP(left_shift, left_shift); // NOLINT(*)
REGISTER_MAKE_BIT_OP(right_shift, right_shift);
TVM_REGISTER_GLOBAL("tir._OpIfThenElse")
.set_body_typed([](PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Span span) {
return if_then_else(cond, true_value, false_value, span);
});
TVM_REGISTER_GLOBAL("tir.const_true").set_body_typed([](DataType t, Span span) {
return const_true(t.lanes(), span);
});
} // namespace tvm