-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcore.hpp
2797 lines (2302 loc) · 80.8 KB
/
core.hpp
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
/*******************************************************************************
* Copyright 2021-2025 Intel Corporation
*
* Licensed 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.
*******************************************************************************/
#ifndef GPU_INTEL_JIT_IR_CORE_HPP
#define GPU_INTEL_JIT_IR_CORE_HPP
#include <algorithm>
#include <atomic>
#include <cstdio>
#include <memory>
#include <numeric>
#include <string>
#include "common/bfloat16.hpp"
#include "common/c_types_map.hpp"
#include "common/float16.hpp"
#include "common/math_utils.hpp"
#include "gpu/intel/jit/codegen/register_allocator.hpp"
#include "gpu/intel/jit/utils/ngen_proxy.hpp"
#include "gpu/intel/jit/utils/utils.hpp"
#if !defined(NDEBUG) || defined(DNNL_DEV_MODE)
#define SANITY_CHECK 1
#endif
// All IR expression objects.
#define HANDLE_EXPR_IR_OBJECTS() \
HANDLE_IR_OBJECT(binary_op_t) \
HANDLE_IR_OBJECT(bool_imm_t) \
HANDLE_IR_OBJECT(cast_t) \
HANDLE_IR_OBJECT(const_var_t) \
HANDLE_IR_OBJECT(float_imm_t) \
HANDLE_IR_OBJECT(iif_t) \
HANDLE_IR_OBJECT(int_imm_t) \
HANDLE_IR_OBJECT(linear_t) \
HANDLE_IR_OBJECT(load_t) \
HANDLE_IR_OBJECT(ptr_t) \
HANDLE_IR_OBJECT(shuffle_t) \
HANDLE_IR_OBJECT(ternary_op_t) \
HANDLE_IR_OBJECT(unary_op_t) \
HANDLE_IR_OBJECT(var_t)
// All IR statement objects.
#define HANDLE_STMT_IR_OBJECTS() \
HANDLE_IR_OBJECT(alloc_t) \
HANDLE_IR_OBJECT(for_t) \
HANDLE_IR_OBJECT(func_call_t) \
HANDLE_IR_OBJECT(if_t) \
HANDLE_IR_OBJECT(let_t) \
HANDLE_IR_OBJECT(stmt_group_t) \
HANDLE_IR_OBJECT(stmt_seq_t) \
HANDLE_IR_OBJECT(store_t) \
HANDLE_IR_OBJECT(while_t)
#define HANDLE_TRAVERSE_TARGETS() \
HANDLE_EXPR_IR_OBJECTS() \
HANDLE_STMT_IR_OBJECTS() \
HANDLE_IR_OBJECT(func_impl_t) \
HANDLE_IR_OBJECT(nary_op_t) \
HANDLE_IR_OBJECT(pexpr_t)
#define HANDLE_ALL_IR_OBJECTS() \
HANDLE_EXPR_IR_OBJECTS() \
HANDLE_STMT_IR_OBJECTS() \
HANDLE_IR_OBJECT(func_impl_t)
enum ir_type_id_t : uint8_t {
#define HANDLE_IR_OBJECT(type) type,
// Create typeid for objects which can be visited/mutated. These need to be
// first as the typeid is used as an index into an array to dispatch to the
// correct mutate function.
HANDLE_ALL_IR_OBJECTS()
//Used to calculate number of IR objects that can be visited/mutated
end_visitable_ir_objects,
// Other IR object
expr_impl_t = end_visitable_ir_objects,
nary_op_t,
stmt_impl_t,
grf_permute_attr_t,
bank_conflict_attr_t,
instruction_modifier_attr_t,
builtin_t,
pexpr_t,
pint_imm_t,
factored_expr_t,
send_t,
dpas_t,
mad_t,
reduce_t,
reorder_t,
eltwise_t,
#undef HANDLE_IR_OBJECT
};
struct type_info_t {
type_info_t(ir_type_id_t type_id, bool is_expr, bool is_stmt)
: type_id(type_id), is_expr(is_expr), is_stmt(is_stmt) {};
ir_type_id_t type_id;
bool is_expr;
bool is_stmt;
};
// Auxiliary macros to reduce boilerplate.
#define IR_DECL_TYPE_ID(class_name) \
using self_type = class_name; \
static ir_type_id_t _type_id() { return ir_type_id_t::class_name; } \
static ir_type_id_t _dispatch_type_id() { return _type_id(); } \
static type_info_t _type_info() { \
return type_info_t(_type_id(), _is_expr(), _is_stmt()); \
}
#define IR_DECL_DERIVED_TYPE_ID(class_name, base_name) \
using self_type = class_name; \
static ir_type_id_t _type_id() { return ir_type_id_t::class_name; } \
static ir_type_id_t _dispatch_type_id() { return base_name::_type_id(); } \
ir_type_id_t dispatch_type_id() const override { \
return _dispatch_type_id(); \
} \
static type_info_t _type_info() { \
return type_info_t(_type_id(), _is_expr(), _is_stmt()); \
}
#define IR_DECL_EXPR_TYPE_ID(class_name) \
IR_DECL_TYPE_ID(class_name) \
static bool _is_expr() { return true; };
#define IR_DECL_STMT_TYPE_ID(class_name) \
IR_DECL_TYPE_ID(class_name) \
static bool _is_stmt() { return true; };
#define IR_DECLARE_TRAVERSERS() \
object_t _mutate(ir_mutator_t &mutator) const override { \
return mutator._mutate(*this); \
} \
void _visit(ir_visitor_t &visitor) const override { visitor._visit(*this); }
// Defines getter for a function argument.
#define IR_DEFINE_ARG_GET(name, index) \
static const expr_t &arg_##name(const func_call_t &c) { \
gpu_assert(c.func.is<self_type>()) << c; \
return c.args[index]; \
} \
static const expr_t &arg_##name(const stmt_t &s) { \
gpu_assert(s.is<func_call_t>()) << s; \
auto &c = s.as<func_call_t>(); \
return arg_##name(c); \
} \
template <typename T> \
static T &arg_##name(std::vector<T> &args) { \
return args[index]; \
} \
template <typename T> \
static const T &arg_##name(const std::vector<T> &args) { \
return args[index]; \
}
#if defined(__GNUC__)
// clang-format off
// Defines dump() method for debugging purposes, to pretty print the object.
#define IR_DEFINE_DUMP() \
__attribute__((noinline)) \
__attribute__((used)) \
void dump() const { \
printf("%s\n", str().c_str()); \
}
// clang-format on
#else
#define IR_DEFINE_DUMP()
#endif
namespace dnnl {
namespace impl {
namespace gpu {
namespace intel {
namespace jit {
enum class type_kind_t {
undef,
_bool,
// Integer types.
u8,
s8,
u16,
s16,
u32,
s32,
u64,
s64,
// Floating point types.
bf8,
f8_e5m2 = bf8,
hf8,
f8_e4m3 = hf8,
bf16,
f16,
tf32,
f32,
f64,
// Message data types.
byte,
dword,
qword,
oword,
hword
};
static auto type_kind_names = nstl::to_array({
make_enum_name(type_kind_t::undef, "undef"),
make_enum_name(type_kind_t::u8, "u8"),
make_enum_name(type_kind_t::s8, "s8"),
make_enum_name(type_kind_t::u16, "u16"),
make_enum_name(type_kind_t::s16, "s16"),
make_enum_name(type_kind_t::u32, "u32"),
make_enum_name(type_kind_t::s32, "s32"),
make_enum_name(type_kind_t::u64, "u64"),
make_enum_name(type_kind_t::s64, "s64"),
make_enum_name(type_kind_t::bf8, "bf8"),
make_enum_name(type_kind_t::hf8, "hf8"),
make_enum_name(type_kind_t::bf16, "bf16"),
make_enum_name(type_kind_t::f16, "f16"),
make_enum_name(type_kind_t::tf32, "tf32"),
make_enum_name(type_kind_t::f32, "f32"),
make_enum_name(type_kind_t::f64, "f64"),
make_enum_name(type_kind_t::byte, "byte"),
make_enum_name(type_kind_t::dword, "dword"),
make_enum_name(type_kind_t::qword, "qword"),
make_enum_name(type_kind_t::oword, "oword"),
make_enum_name(type_kind_t::hword, "hword"),
make_enum_name(type_kind_t::_bool, "bool"),
});
GPU_DEFINE_PARSE_ENUM(type_kind_t, type_kind_names)
class type_t {
public:
static type_t undef() { return type_t(type_kind_t::undef); }
static type_t _bool(int elems = 1) {
return type_t(type_kind_t::_bool, elems);
}
static type_t u8(int elems = 1) { return type_t(type_kind_t::u8, elems); }
static type_t s8(int elems = 1) { return type_t(type_kind_t::s8, elems); }
static type_t u16(int elems = 1) { return type_t(type_kind_t::u16, elems); }
static type_t s16(int elems = 1) { return type_t(type_kind_t::s16, elems); }
static type_t u32(int elems = 1) { return type_t(type_kind_t::u32, elems); }
static type_t s32(int elems = 1) { return type_t(type_kind_t::s32, elems); }
static type_t u64(int elems = 1) { return type_t(type_kind_t::u64, elems); }
static type_t s64(int elems = 1) { return type_t(type_kind_t::s64, elems); }
// Returns unsigned integer type.
static type_t u(int bits, int elems = 1) {
switch (bits) {
case 8: return u8(elems);
case 16: return u16(elems);
case 32: return u32(elems);
case 64: return u64(elems);
default: gpu_error_not_expected();
}
return type_t::undef();
}
// Returns signed integer type.
static type_t s(int bits, int elems = 1) {
switch (bits) {
case 8: return s8(elems);
case 16: return s16(elems);
case 32: return s32(elems);
case 64: return s64(elems);
default: gpu_error_not_expected();
}
return type_t::undef();
}
static type_t bf8(int elems = 1) { return type_t(type_kind_t::bf8, elems); }
static type_t hf8(int elems = 1) { return type_t(type_kind_t::hf8, elems); }
static type_t bf16(int elems = 1) {
return type_t(type_kind_t::bf16, elems);
}
static type_t f16(int elems = 1) { return type_t(type_kind_t::f16, elems); }
static type_t tf32(int elems = 1) {
return type_t(type_kind_t::tf32, elems);
}
static type_t f32(int elems = 1) { return type_t(type_kind_t::f32, elems); }
static type_t f64(int elems = 1) { return type_t(type_kind_t::f64, elems); }
static type_t byte(int elems = 1) {
return type_t(type_kind_t::byte, elems);
}
static type_t byte_ptr(int elems = 1) {
return type_t(type_kind_t::byte, elems).with_ptr();
}
static type_t dword(int elems = 1) {
return type_t(type_kind_t::dword, elems);
}
static type_t qword(int elems = 1) {
return type_t(type_kind_t::qword, elems);
}
static type_t oword(int elems = 1) {
return type_t(type_kind_t::oword, elems);
}
static type_t hword(int elems = 1) {
return type_t(type_kind_t::hword, elems);
}
template <typename T>
static type_t from_cpp() {
#define CASE(cpp_type, type) \
if (std::is_same<T, cpp_type>::value) return type()
CASE(bool, _bool);
CASE(float, f32);
CASE(double, f64);
CASE(int16_t, s16);
CASE(int32_t, s32);
CASE(int64_t, s64);
CASE(uint16_t, u16);
CASE(uint32_t, u32);
CASE(uint64_t, u64);
#undef CASE
gpu_error_not_expected();
return undef();
}
template <typename T>
T max() const {
switch (kind()) {
case type_kind_t::u8:
case type_kind_t::s8:
case type_kind_t::u16:
case type_kind_t::s16:
case type_kind_t::u32:
case type_kind_t::s32:
case type_kind_t::u64:
case type_kind_t::s64: {
int bits = 8 * size();
if (is_signed()) bits--;
T ret = T(1) << (bits - 1);
return ret + (ret - 1);
}
default: gpu_error_not_expected();
}
return 0;
}
template <typename T>
T min() const {
switch (kind()) {
case type_kind_t::u8:
case type_kind_t::s8:
case type_kind_t::u16:
case type_kind_t::s16:
case type_kind_t::u32:
case type_kind_t::s32:
case type_kind_t::u64:
case type_kind_t::s64: {
if (is_unsigned()) return 0;
return -max<T>() - 1;
}
default: gpu_error_not_expected();
}
return 0;
}
static bool is_vector(int elems) { return elems != 1; }
type_t() : type_t(type_t::undef()) {}
type_t(type_kind_t kind, uint32_t elems = 1) : kind_(kind), elems_(elems) {}
type_t(const std::string &s) : elems_(1) {
#define CASE(x) \
if (to_string(type_kind_t::x) == s) { \
kind_ = type_kind_t::x; \
return; \
}
CASE(bf16);
CASE(f16);
CASE(tf32);
CASE(f32);
CASE(f64);
CASE(s32);
CASE(s64);
CASE(s8);
CASE(u8);
CASE(s16);
CASE(s32);
CASE(s64);
CASE(u16);
CASE(u32);
CASE(u64);
#undef CASE
gpu_error_not_expected();
}
// Constructor from dnnl_data_type_t.
type_t(data_type_t dt) {
if (dt == data_type::undef) return;
elems_ = 1;
switch ((int)dt) {
#define CASE(x) \
case data_type::x: kind_ = type_kind_t::x; break;
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(bf16);
CASE(f16);
CASE(tf32);
CASE(f32);
CASE(f64);
CASE(s32);
CASE(s8);
CASE(u8);
#undef CASE
default: gpu_error_not_expected();
}
}
type_kind_t kind() const { return kind_; }
int elems() const { return elems_; }
bool is_ptr() const { return is_ptr_; }
bool operator==(const type_t &other) const {
return (kind() == other.kind()) && (elems() == other.elems())
&& (is_ptr() == other.is_ptr());
}
bool operator!=(const type_t &other) const { return !operator==(other); }
bool is_equal(const type_t &other) const { return operator==(other); }
size_t get_hash() const {
return ir_utils::get_hash(kind(), elems(), is_ptr());
}
static void init_parse_iface(parse_iface_t<type_t> *iface) {
iface->add<type_kind_t, &type_t::kind_>();
iface->set_pre_stringify_func([](const type_t &type) {
gpu_assert(!type.is_ptr() && (type.is_scalar() || type.is_undef()))
<< "Cannot stringify pointer/non-scalar type.";
});
}
bool is_undef() const { return kind() == type_kind_t::undef; }
bool is_vector() const { return type_t::is_vector(elems()); }
bool is_bool() const { return kind() == type_kind_t::_bool; }
bool is_fp() const {
return utils::one_of(kind(), type_kind_t::bf8, type_kind_t::hf8,
type_kind_t::bf16, type_kind_t::f16, type_kind_t::tf32,
type_kind_t::f32, type_kind_t::f64);
}
bool is_fp8() const {
return utils::one_of(kind(), type_kind_t::bf8, type_kind_t::hf8);
}
bool is_bf8() const { return kind() == type_kind_t::bf8; }
bool is_hf8() const { return kind() == type_kind_t::hf8; }
bool is_bf16() const { return kind() == type_kind_t::bf16; }
bool is_f16() const { return kind() == type_kind_t::f16; }
bool is_tf32() const { return kind() == type_kind_t::tf32; }
bool is_f32() const { return kind() == type_kind_t::f32; }
bool is_f64() const { return kind() == type_kind_t::f64; }
bool is_int() const {
return utils::one_of(kind(), type_kind_t::u8, type_kind_t::s8,
type_kind_t::u16, type_kind_t::s16, type_kind_t::u32,
type_kind_t::s32, type_kind_t::u64, type_kind_t::s64);
}
bool is_s8() const { return kind() == type_kind_t::s8; }
bool is_u8() const { return kind() == type_kind_t::u8; }
bool is_x8() const {
return utils::one_of(kind(), type_kind_t::s8, type_kind_t::u8);
}
bool is_s16() const { return kind() == type_kind_t::s16; }
bool is_u16() const { return kind() == type_kind_t::u16; }
bool is_x16() const {
return utils::one_of(kind(), type_kind_t::s16, type_kind_t::u16);
}
bool is_s32() const { return kind() == type_kind_t::s32; }
bool is_u32() const { return kind() == type_kind_t::u32; }
bool is_x32() const {
return utils::one_of(kind(), type_kind_t::s32, type_kind_t::u32);
}
bool is_s64() const { return kind() == type_kind_t::s64; }
bool is_u64() const { return kind() == type_kind_t::u64; }
bool is_x64() const {
return utils::one_of(kind(), type_kind_t::s64, type_kind_t::u64);
}
bool is_byte() const { return kind() == type_kind_t::byte; }
bool is_dword() const { return kind() == type_kind_t::dword; }
bool is_qword() const { return kind() == type_kind_t::qword; }
bool is_oword() const { return kind() == type_kind_t::oword; }
bool is_hword() const { return kind() == type_kind_t::hword; }
bool is_signed(int elems = -1) const {
if (elems != -1 && elems_ != elems) return false;
return utils::one_of(kind(), type_kind_t::s8, type_kind_t::s16,
type_kind_t::s32, type_kind_t::s64);
}
bool is_unsigned(int elems = -1) const {
if (elems != -1 && elems_ != elems) return false;
return utils::one_of(kind(), type_kind_t::u8, type_kind_t::u16,
type_kind_t::u32, type_kind_t::u64);
}
bool is_scalar() const { return elems() == 1; }
template <typename T>
bool is_cpp() const {
return *this == type_t::from_cpp<T>();
}
bool is_bitwise_compatible(const type_t &other) const {
if (*this == other) return true;
// tf32 is bitwise compatible with f32.
if (kind() == type_kind_t::f32 && other.kind() == type_kind_t::tf32)
return elems() == other.elems();
return false;
}
type_t remove_elems() const { return with_elems(1); }
type_t remove_ptr() const {
type_t copy = *this;
copy.is_ptr_ = false;
return copy;
}
type_t with_elems(int new_elems) const {
type_t copy = *this;
copy.elems_ = new_elems;
return copy;
}
type_t with_ptr() const {
type_t copy = *this;
copy.is_ptr_ = true;
return copy;
}
type_t scalar() const { return with_elems(1); }
// Returns size in bytes.
int size() const;
std::string str() const {
std::ostringstream oss;
oss << to_string(kind());
if (elems() > 1) oss << "x" << elems();
if (is_ptr()) oss << "*";
return oss.str();
}
IR_DEFINE_DUMP()
private:
type_kind_t kind_ = type_kind_t::undef;
int elems_ = 0;
bool is_ptr_ = false;
};
// type_t to dnnl_data_type_t convertor.
data_type_t to_dnnl(const type_t &type);
// Reference counter for IR objects.
class ref_count_t {
public:
ref_count_t() : value_(0) {}
ref_count_t(const ref_count_t &) = delete;
ref_count_t &operator=(const ref_count_t &) = delete;
~ref_count_t() = default;
uint32_t increment() { return ++value_; }
uint32_t decrement() { return --value_; }
private:
uint32_t value_;
};
// Forward Declare IR objects
class object_t;
class ir_mutator_t;
class ir_visitor_t;
// clang-tidy doesn't like the semicolon next to the class name.
#define CLASS_DECLARATION(name) class name
#define HANDLE_IR_OBJECT(type) CLASS_DECLARATION(type);
HANDLE_TRAVERSE_TARGETS()
#undef HANDLE_IR_OBJECT
#undef CLASS_DECLARATION
// Base class for all IR objects. Implemented as an intrusive pointer, with
// the reference counter stored inside the object.
class object_impl_t {
public:
object_impl_t(type_info_t type_info) : type_info_(type_info) {};
object_impl_t(const object_impl_t &) = delete;
object_impl_t &operator=(const object_impl_t &) = delete;
virtual ~object_impl_t() = default;
ref_count_t &ref_count() { return ref_count_; }
// Type ID used for dispatching in ir_visitor_t and ir_mutator_t.
// For some IR objects
virtual ir_type_id_t dispatch_type_id() const { return type_id(); }
// Provides equality semantics.
virtual bool is_equal(const object_impl_t &obj) const = 0;
virtual size_t get_hash() const = 0;
static bool _is_expr() { return false; };
static bool _is_stmt() { return false; };
bool is_expr() const { return type_info_.is_expr; }
bool is_stmt() const { return type_info_.is_stmt; }
// Downcasts the object to the IR type, returns a reference. The IR type
// must match the real IR type.
// N.B.: this can potentially be dangerous if applied to non-const objects,
// since assigning a different value to the source object might make
// the reference dangling due to the destruction of the former object;
// please only call this method on non-const objects if absolutely
// necessary, and please don't add a non-const variant of the method!
template <typename T>
const T &as() const {
gpu_assert(is<T>());
return *as_ptr<T>(); // fails on incorrect casts even in Release
}
// Downcasts the object to the IR type, returns a pointer. If the IR type
// doesn't match the real IR type, returns nullptr.
// N.B.: this can potentially be dangerous if applied to non-const objects,
// since assigning a different value to the source object might make
// the reference dangling due to the destruction of the former object;
// please only call this method on non-const objects if absolutely
// necessary, and please don't add a non-const variant of the method!
template <typename T>
const T *as_ptr() const {
return (is<T>()) ? (const T *)this : nullptr;
}
// Returns true if T matches the real IR type.
template <typename T>
bool is() const {
return type_id() == T::_type_id();
}
virtual std::string str() const;
virtual object_t _mutate(ir_mutator_t &mutator) const;
virtual void _visit(ir_visitor_t &visitor) const;
IR_DEFINE_DUMP()
private:
// Unique type ID.
ir_type_id_t type_id() const { return type_info_.type_id; };
ref_count_t ref_count_;
type_info_t type_info_;
};
// Base wrapper for IR objects.
class object_t {
public:
object_t(object_impl_t *impl = nullptr) : impl_(impl) {
increment(impl_);
#ifdef SANITY_CHECK
sanity_check();
#endif
}
object_t(const object_impl_t &impl)
: object_t(const_cast<object_impl_t *>(&impl)) {}
object_t(const object_impl_t *impl)
: object_t(const_cast<object_impl_t *>(impl)) {}
object_t(const object_t &obj) : object_t(obj.impl()) {}
object_t(object_t &&obj) : impl_(obj.impl_) {
obj.impl_ = nullptr;
#ifdef SANITY_CHECK
sanity_check();
#endif
}
#ifdef SANITY_CHECK
virtual ~object_t() { decrement_and_maybe_destroy(impl_); }
#else
~object_t() { decrement_and_maybe_destroy(impl_); }
#endif
object_t &operator=(const object_t &other) {
if (&other == this) return *this;
auto *other_impl = other.impl();
increment(other_impl);
decrement_and_maybe_destroy(impl_);
impl_ = other_impl;
#ifdef SANITY_CHECK
sanity_check();
#endif
return *this;
}
object_t &operator=(object_t &&other) {
std::swap(impl_, other.impl_);
#ifdef SANITY_CHECK
sanity_check();
#endif
return *this;
}
object_impl_t *impl() const { return impl_; }
bool is_empty() const { return !impl_; }
ir_type_id_t dispatch_type_id() const { return impl_->dispatch_type_id(); }
template <typename T>
const T &as() const {
gpu_assert(impl_);
return impl_->as<T>();
}
template <typename T>
const T *as_ptr() const {
if (!impl_) return nullptr;
return impl_->as_ptr<T>();
}
template <typename T>
bool is() const {
if (is_empty()) return false;
return impl_->is<T>();
}
// Comparison with identity semantics.
bool is_same(const object_t &other) const { return impl_ == other.impl(); }
// Comparison with equality semantics.
bool is_equal(const object_t &other) const {
if (is_empty() || other.is_empty())
return is_empty() == other.is_empty();
return impl_->is_equal(*other.impl());
}
size_t get_hash() const {
if (is_empty()) return 0;
return impl()->get_hash();
}
bool is_expr() const { return impl_ && impl_->is_expr(); }
bool is_stmt() const { return impl_ && impl_->is_stmt(); }
std::string str() const {
if (is_empty()) return "(nil)";
return impl()->str();
}
IR_DEFINE_DUMP()
protected:
#ifdef SANITY_CHECK
virtual void sanity_check() const {}
#endif
private:
static void increment(object_impl_t *impl) {
if (!impl) return;
impl->ref_count().increment();
}
static void decrement_and_maybe_destroy(object_impl_t *impl) {
if (!impl) return;
if (impl->ref_count().decrement() == 0) { delete impl; }
}
object_impl_t *impl_;
};
// Helper classes for containers to store object_t.
struct object_id_hash_t {
size_t operator()(const object_t &obj) const {
return std::hash<const object_impl_t *>()(obj.impl());
}
};
struct object_eq_hash_t {
size_t operator()(const object_t &obj) const { return obj.get_hash(); }
};
struct object_id_equal_t {
bool operator()(const object_t &a, const object_t &b) const {
return a.is_same(b);
}
};
struct object_eq_equal_t {
bool operator()(const object_t &a, const object_t &b) const {
return a.is_equal(b);
}
};
// Containers to store object_t.
// Unordered set, uses identity comparison for keys.
template <typename KeyT>
using object_set_t
= std::unordered_set<KeyT, object_id_hash_t, object_id_equal_t>;
// Unordered set, uses equality comparison for keys.
template <typename KeyT>
using object_eq_set_t
= std::unordered_set<KeyT, object_eq_hash_t, object_eq_equal_t>;
// Unordered map, uses identity comparison for keys.
template <typename KeyT, typename ValueT>
using object_map_t
= std::unordered_map<KeyT, ValueT, object_id_hash_t, object_id_equal_t>;
// Unordered map, uses equality comparison for keys.
template <typename KeyT, typename ValueT>
using object_eq_map_t
= std::unordered_map<KeyT, ValueT, object_eq_hash_t, object_eq_equal_t>;
// Helper class to mutate IR tree.
class ir_mutator_t {
public:
virtual ~ir_mutator_t() = default;
object_t mutate(const object_t &obj) {
auto impl = obj.impl();
if (!impl) return impl;
return impl->_mutate(*this);
}
template <typename T>
std::vector<T> mutate(const std::vector<T> &v) {
std::vector<T> new_v;
new_v.reserve(v.size());
for (auto &e : v)
new_v.push_back(mutate(e));
return new_v;
}
// To catch missing _mutate() handlers in ir_mutator_t.
object_t _mutate(const object_impl_t &obj) {
gpu_error_not_expected() << "Can't handle type: " << object_t(&obj);
return {};
}
#define HANDLE_IR_OBJECT(type) virtual object_t _mutate(const type &obj);
HANDLE_TRAVERSE_TARGETS()
#undef HANDLE_IR_OBJECT
};
// Helper class to walk through IR tree.
class ir_visitor_t {
public:
virtual ~ir_visitor_t() = default;
void visit(const object_t &obj) {
const object_impl_t *impl = obj.impl();
if (impl) {
pre_visit(*impl);
impl->_visit(*this);
post_visit(*impl);
};
}
template <typename T>
void visit(const std::vector<T> &v) {
for (auto &e : v)
visit(e);
}
virtual void pre_visit(const object_impl_t &obj) {}
virtual void post_visit(const object_impl_t &obj) {}
// To catch missing _visit() handlers in ir_visitor_t.
void _visit(const object_impl_t &obj) {
gpu_error_not_expected() << "Can't handle type: " << object_t(obj);
}
#define HANDLE_IR_OBJECT(type) virtual void _visit(const type &obj);
HANDLE_TRAVERSE_TARGETS()
#undef HANDLE_IR_OBJECT
};
// Base class for IR expression objects.
class expr_impl_t : public object_impl_t {
public:
IR_DECL_TYPE_ID(expr_impl_t)
expr_impl_t(type_info_t type_info, const type_t &type)
: object_impl_t(type_info), type(type) {}
type_t type;
};
// Wrapper for IR expression objects.
class expr_t : public object_t {
public:
using object_t::object_t;
expr_t() = default;
expr_t(const object_t &obj) : object_t(obj) {}
expr_t(object_t &&obj) : object_t(obj) {}
expr_t &operator=(const object_t &obj) {
object_t::operator=(obj);
return *this;
}
expr_t &operator=(object_t &&obj) {
object_t::operator=(obj);
return *this;
}
explicit expr_t(bool v);
expr_t(float v);
expr_t(double v);
expr_t(int16_t v);
expr_t(int32_t v);
expr_t(int64_t v);
expr_t(uint16_t v);
expr_t(uint32_t v);
expr_t(uint64_t v);
const type_t &type() const {
gpu_assert(!is_empty());
return ((const expr_impl_t *)impl())->type;
}
#define DECLARE_BINARY_ASSIGN_OPERATOR(op) \
expr_t &operator op##=(const expr_t &rhs);
DECLARE_BINARY_ASSIGN_OPERATOR(+)
DECLARE_BINARY_ASSIGN_OPERATOR(-)
DECLARE_BINARY_ASSIGN_OPERATOR(*)
DECLARE_BINARY_ASSIGN_OPERATOR(/)
DECLARE_BINARY_ASSIGN_OPERATOR(%)
DECLARE_BINARY_ASSIGN_OPERATOR(&)
#undef DECLARE_BINARY_ASSIGN_OPERATOR
// Returns a pointer shifted by `off` bytes relative to this pointer. The
// base expression must be a pointer.
expr_t operator[](const expr_t &off) const;
private:
#ifdef SANITY_CHECK
void sanity_check() const override {
gpu_assert(dynamic_cast<const expr_impl_t *>(impl()) == impl())
<< object_t(impl());
}
#endif
};
// Helper functions.
inline bool is_const(const expr_t &e);
inline bool is_var(const expr_t &e);
inline bool all_of(const expr_t &e, const expr_t &value);
// Unary and binary operators.
enum class op_kind_t {
undef,
_minus,
_add,