-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathngen_auto_swsb.hpp
2630 lines (2286 loc) · 95.9 KB
/
ngen_auto_swsb.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 2019-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.
*******************************************************************************/
/*
* Do not #include this file directly; ngen uses it internally.
*/
#ifndef NGEN_AUTO_SWSB_HPP
#define NGEN_AUTO_SWSB_HPP
#if defined(NGEN_DEBUG) || defined(NGEN_DEBUG_PROPAGATE) || defined(NGEN_DEBUG_BB)
#include <iomanip>
#include <iostream>
#endif
#include <limits>
#include <list>
#include <map>
#include "ngen_core.hpp"
namespace NGEN_NAMESPACE {
namespace autoswsb {
/*******************/
/* Data structures */
/*******************/
typedef uint8_t PipeMask;
enum {
PipeMaskNone = 0,
PipeMaskA = 1, // All in-order pipes
PipeMaskF = 2,
PipeMaskI = 4,
PipeMaskL = 8,
PipeMaskM = 0x10,
PipeMaskS = 0x20,
PipeMaskC = 0x40, // All instructions (in-order/out-of-order).
PipeMaskO = 0x80, // All out-of-order pipes. Not a valid GeneralizedPipe.
PipeBitA = 0,
PipeBitF = 1,
PipeBitI = 2,
PipeBitL = 3,
PipeBitM = 4,
PipeBitS = 5,
PipeBitC = 6,
PipeBitO = 7,
};
static constexpr int NPipes = 7;
static inline PipeMask toMask(Pipe pipe) { return (1 << (static_cast<unsigned>(pipe) - 1)); }
static inline Pipe fromMask(PipeMask mask) { return mask ? static_cast<Pipe>(1 + utils::log2(mask)) : Pipe::Default; }
typedef uint8_t DestinationMask;
enum {
DestNone = 0,
DestNextIP = 1,
DestJIP = 2,
DestUIP = 4,
DestUnknown = 8
};
class GeneralizedPipe {
uint16_t v;
public:
static constexpr uint16_t vInOrder = 0x000;
static constexpr uint16_t vSend = 0x100; // OR'ed with SFID
static constexpr uint16_t vSystolic = 0x200;
static constexpr uint16_t vMath = 0x300;
private:
static constexpr uint16_t vTypeMask = 0x300;
GeneralizedPipe(uint16_t v_, int dummy) : v{v_} {}
public:
GeneralizedPipe() : v{uint16_t(0)} {}
GeneralizedPipe(PipeMask pipe) : v{uint16_t(vInOrder | pipe)} {}
GeneralizedPipe(SharedFunction sfid) : v{uint16_t(vSend | static_cast<uint8_t>(sfid))} {}
static GeneralizedPipe Systolic() { return GeneralizedPipe(vSystolic, 0); }
static GeneralizedPipe Math() { return GeneralizedPipe(vMath, 0); }
bool operator==(GeneralizedPipe other) const { return v == other.v; }
bool operator!=(GeneralizedPipe other) const { return v != other.v; }
bool inOrder() const { return ((v & vTypeMask) == vInOrder) && (v != PipeMaskNone); }
uint16_t type() const { return v & vTypeMask; }
PipeMask inOrderPipe() const { return inOrder() ? (v & ~vTypeMask) : PipeMaskNone; }
Pipe toPipe() const { return fromMask(inOrderPipe()); }
inline PipeMask syncPipes(HW hw) const;
inline unsigned sendClassXeHPC() const;
#ifdef NGEN_DEBUG
inline void dump() const;
#endif
};
struct DependencyRegion {
uint8_t base, size;
uint8_t unspecified : 1;
uint8_t checkWAW : 1;
uint8_t arf : 1;
HW hw;
std::array<uint32_t, 32> masks;
DependencyRegion() : DependencyRegion(HW::Unknown) {}
explicit DependencyRegion(HW hw_) : base(0), size(0), unspecified{true}, checkWAW{false}, arf{false}, hw{hw_} {
for (auto &m: masks) m = 0;
}
inline DependencyRegion(HW hw, GRFRange r);
inline DependencyRegion(HW hw, int esize, RegData rr);
inline void intersect(const DependencyRegion &other);
inline void subtract(const DependencyRegion &other);
bool empty() const {
if (unspecified) return false;
if (size == 0) return true;
for (auto m : masks)
if (m != 0)
return false;
return true;
}
void clear() { *this = DependencyRegion(hw); unspecified = false; checkWAW = false; arf = false; }
#ifdef NGEN_DEBUG
inline void dump() const;
#endif
};
template <bool consumer>
struct Dependency {
int32_t label; // Multipurpose label for use in algorithms
// Source instruction information.
GeneralizedPipe pipe; // Execution pipe for instruction
uint16_t tokenTime; // Estimated upper bound for token lifetime, in cycles.
std::array<int32_t, NPipes> counters; // Pipe counters, relative to start of BB.
uint32_t inum; // Instruction number.
// (Mostly) dependency information.
uint8_t token; // Out of order token
uint8_t tokenSrc : 1; // Src dependency on token?
uint8_t tokenDst : 1; // Dst dependency on token?
uint8_t rw : 1; // Flag: read or write?
uint8_t swsb : 1; // True for SWSB dependency consumers
uint8_t active : 1; // True if dependency is still alive.
PipeMask depPipe; // (swsb consumer only) Pipe to wait on
uint8_t dist; // (swsb consumer only) Pipe distance
DependencyRegion region; // GRF region covered
Dependency() : label{0}, pipe{}, tokenTime{0},
token{0}, tokenSrc{false}, tokenDst{false},
rw{false}, swsb{false}, active{true},
depPipe{PipeMaskNone}, dist{0}, region{} { counters.fill(0); }
bool operator==(const Dependency &other) {
return !std::memcmp(this, &other, sizeof(Dependency));
}
bool operator!=(const Dependency *other) { return !(operator==(other)); }
constexpr bool read() const { return !rw; }
constexpr bool write() const { return rw; }
constexpr bool hasToken() const { return tokenSrc || tokenDst; }
constexpr bool hasDist() const { return (dist > 0); }
Dependency<!consumer>& cast() { return reinterpret_cast<Dependency<!consumer>&>(*this); }
static constexpr uint8_t tokenTBD = 0xFF;
#ifdef NGEN_DEBUG
inline void dump() const;
#endif
};
template <bool consumer>
class DependencyTable {
enum {
ListTypeGRF = 0, // Lists of DependencyFragments filtered by GRF base register.
ListTypeToken = 1, // Lists of DependencyFragments filtered by token.
ListTypePipe = 2, // Lists of DependencyFragments filtered by (in-order) pipe.
// fragsByToken/fragsByPipe contain only one DependencyFragment per Dependency.
NListTypes = 3
};
enum : uint32_t {
none = ~uint32_t(0) // Special value indicating end of list.
};
enum : int {
maxGRF = 256,
grfListIdxUnspecified = maxGRF // GRF list index for all unspecified regions.
};
struct DependencyFragment {
uint32_t depID; // Index of main Dependency struct in array.
uint8_t before, after; // # of consecutive fragments associated with the same Dependency
// before and after this one.
uint32_t prev[NListTypes]; // Previous pointers for doubly-linked lists.
uint32_t next[NListTypes]; // Next pointers for doubly-linked lists.
};
std::vector<Dependency<consumer>> deps; // List of all Dependencies (active or not)
std::vector<DependencyFragment> frags; // List of all DependencyFragments (active or not)
std::array<uint32_t, 257> heads[NListTypes]; // Heads of doubly-linked lists.
static bool isHeadLink(uint32_t id) { return ((id & 0x80000000) != 0) && (id != none); }
static uint32_t readHeadLink(uint32_t id) { return id & 0x7FFFFFFF; }
static uint32_t makeHeadLink(uint32_t idx) { return idx | 0x80000000; }
template <bool iconsumer> inline void findAndRemoveIntersections(int listType, int listIdx, const Dependency<iconsumer> &dep, std::vector<Dependency<consumer>> *out, bool doRemove = true);
inline bool insertPrepare(int listType, int listIdx, Dependency<consumer> &dep, bool checkWeaker, bool checkStronger);
inline void insertLinkedList(int listType, int listIdx, int32_t fragID);
template <bool iconsumer> static inline int getPipeIndex(const Dependency<iconsumer> &dep);
public:
DependencyTable() { clear(); }
inline void clear();
inline void reserve(int icount);
inline bool insert(Dependency<consumer> &dep, bool checkWeaker = true, bool checkStronger = true);
inline bool insertWeak(Dependency<consumer> &dep) { return insert(dep, true, false); }
inline void insertStrong(const Dependency<consumer> &dep) { (void) insert(const_cast<Dependency<consumer> &>(dep), false, true); }
inline void remove(int fragID);
template <bool iconsumer> inline void findIntersections(const Dependency<iconsumer> &dep, std::vector<Dependency<consumer>> &out);
template <bool iconsumer> inline void findAndRemoveIntersections(const Dependency<iconsumer> &dep, std::vector<Dependency<consumer>> *out, bool doRemove = true);
template <bool iconsumer> inline void removeIntersections(const Dependency<iconsumer> &dep);
inline uint32_t removeByTokenMask(uint32_t mask, bool dst);
inline bool containsToken(int token) { return heads[ListTypeToken][token] != none; }
template <typename Func> inline void forEach(Func f) { for (auto &entry : deps) if (entry.active) f(entry); }
template <typename Func> inline void forEach(Func f) const { for (auto &entry : deps) if (entry.active) f(entry); }
#ifdef NGEN_DEBUG
inline void dump() const;
#endif
};
struct SyncInsertion {
uint32_t inum;
SWSBInfo swsb;
SyncFunction fc;
uint32_t mask; // (allrd/allwr) 0 indicates no mask to be applied.
};
struct DummyMovInsertion {
uint32_t inum;
SWSBInfo swsb;
uint16_t grf;
bool constant;
DataType dt;
};
struct BasicBlock;
struct BasicBlock {
uint32_t id; // Index
int32_t label; // Multipurpose flag for use in algorithms
uint32_t istart, iend; // Instruction range: [istart, iend)
uint32_t directives; // # of directives (pseudo-instructions) in this BB
std::array<uint32_t, NPipes> lengths; // # of instructions in each pipe in this BB
std::vector<BasicBlock *> pred, succ; // List of predecessor/successor BBs
DependencyTable<false> producers; // Table of dependencies produced and consumed by this BB.
DependencyTable<true> consumers; // Production table re-used for live incoming dependencies.
DependencyTable<false> incoming; // Table of dependencies produced by prior BBs (temporary).
std::vector<SyncInsertion> syncs; // List of sync instructions to generate.
std::vector<DummyMovInsertion> movs; // List of mov instructions to generate.
std::vector<std::array<DependencyRegion, 4>> opRegions; // Cache of instruction operand regions.
bool enablePVCWARWA = false; // Enable workaround for PVC WAR bug.
const DependencyRegion &getOperandRegion(int inum, int opNum) const {
return opRegions[inum - istart][opNum + 1];
}
};
using BasicBlockList = std::vector<BasicBlock>;
/*****************/
/* Pipe Handling */
/*****************/
// Get all pipes to track in-order dependencies on.
inline PipeMask allPipes(HW hw)
{
PipeMask mask = PipeMaskA | PipeMaskO;
if (hw >= HW::XeHP) mask |= PipeMaskF | PipeMaskI | PipeMaskL;
if (hw >= HW::XeHPC) mask |= PipeMaskM;
return mask;
}
// Get the execution data type for an instruction.
template <typename Instruction>
inline unsigned execType(const Instruction &insn)
{
auto execDT = insn.dstTypecode();
if (insn.src0Typecode() == 0b1011)
execDT = 0b1011;
return execDT;
}
// Get the execution pipe for an instruction.
template <typename Instruction>
inline GeneralizedPipe getPipe(HW hw, const Instruction &insn, bool checkOOO = true)
{
// Check jumps and no-ops
auto op = insn.opcode();
if (isBranch(op) || op == Opcode::nop_gen12 || op == Opcode::sync || op == Opcode::illegal || op == Opcode::directive)
return GeneralizedPipe();
// Check OOO instructions.
if (trackedByToken(hw, op, execType(insn))) {
if (!checkOOO)
return GeneralizedPipe();
switch (op) {
case Opcode::dpas:
case Opcode::dpasw:
return GeneralizedPipe::Systolic();
case Opcode::math:
default:
return GeneralizedPipe::Math();
case Opcode::send:
case Opcode::sendc:
return GeneralizedPipe(insn.sfid());
}
}
if (hw >= HW::XeHPC && (op == Opcode::math))
return PipeMaskM;
PipeMask mask = PipeMaskNone;
// For SWSB purposes, Gen12LP has a single in-order pipe.
// Otherwise, in-order pipe determined by destination type.
// Exception: if there are any long operands, it's a long pipe instruction.
if (hw >= HW::XeHP) {
auto dt = insn.dstTypecode();
unsigned lmask = (hw >= HW::XeHPC) ? 0b1011 : 0b0011; // Note: assumes PVC-XT
if ((dt & lmask) == lmask)
mask |= PipeMaskL;
else if (dt & 8)
mask |= PipeMaskF;
else
mask |= PipeMaskI;
if ((hw < HW::XeHPC) && !(mask & PipeMaskL)) {
if ((insn.src0Typecode() & lmask) == lmask)
mask = PipeMaskL;
else if ((insn.src1Typecode() & lmask) == lmask)
mask = PipeMaskL;
}
if (hw >= HW::Xe3) {
ARFType dstARF;
if (insn.getARFType(dstARF, -1, hw) && dstARF == ARFType::s)
mask = PipeMaskS;
}
} else
mask = PipeMaskA;
return mask;
}
template <typename Instruction>
inline PipeMask getPipeMask(HW hw, const Instruction &insn)
{
PipeMask pipe = getPipe(hw, insn, false).inOrderPipe();
if (pipe != PipeMaskNone)
pipe |= PipeMaskA;
return pipe | PipeMaskC;
}
PipeMask GeneralizedPipe::syncPipes(HW hw) const
{
if ((hw >= HW::XeHP) && (v & PipeMaskA))
return allPipes(hw) & ~PipeMaskA & ~PipeMaskO;
return (v == PipeMaskNone) ? allPipes(hw) : inOrderPipe();
}
unsigned GeneralizedPipe::sendClassXeHPC() const
{
if (type() == vSend) switch (static_cast<SharedFunction>(v & 0xF)) {
case SharedFunction::dcro:
case SharedFunction::dc0:
case SharedFunction::dc1:
case SharedFunction::slm:
case SharedFunction::ugm: return 1;
default: return 2;
}
return 0;
}
static inline DataType dtForPipe(Pipe p)
{
switch (p) {
default:
case Pipe::I: return DataType::ud;
case Pipe::F: return DataType::f;
case Pipe::L: return DataType::df;
}
}
/**********************/
/* Dependency Regions */
/**********************/
DependencyRegion::DependencyRegion(HW hw_, GRFRange r)
{
auto nmasks = int(masks.size());
#ifdef NGEN_SAFE
if (r.isInvalid() || (r.getLen() > nmasks))
throw invalid_region_exception();
#endif
hw = hw_;
unspecified = false;
checkWAW = false;
arf = false;
base = r.getBase();
size = r.getLen();
auto fullMask = ~uint32_t(0);
for (int i = 0; i < nmasks; i++)
masks[i] = (i < r.getLen()) ? fullMask : 0u;
}
DependencyRegion::DependencyRegion(HW hw_, int esize, RegData rr)
{
const auto mbits = GRF::bytes(hw_);
const auto log2MBits = GRF::log2Bytes(hw_);
hw = hw_;
base = rr.getBase();
unspecified = false;
checkWAW = false;
arf = rr.isARF();
int hs = rr.getHS(), vs = rr.getVS();
int nh = rr.getWidth();
if (nh == 0) nh = 1;
int nv = esize / nh;
int bytes = rr.getBytes();
int off = rr.getByteOffset();
auto makeMask = [](int sz) -> uint64_t {
if (sz == 64) return ~uint64_t(0);
return (uint64_t(1) << sz) - 1;
};
auto compress = [&](uint64_t m) -> uint32_t {
if (hw_ >= HW::XeHPC) {
// Regions tracked at word granularity. OR and pack adjacent bits.
// If any sub-word writes, need to track WAW dependencies.
if ((m ^ (m >> 1)) & 0x5555555555555555)
checkWAW = true;
m = (m | (m >> 1)) & 0x5555555555555555;
m = (m | (m >> 1)) & 0x3333333333333333;
m = (m | (m >> 2)) & 0x0F0F0F0F0F0F0F0F;
m = (m | (m >> 4)) & 0x00FF00FF00FF00FF;
m = (m | (m >> 8)) & 0x0000FFFF0000FFFF;
m |= (m >> 16);
}
return uint32_t(m);
};
if (hs == 0) nh = hs = 1;
if (vs == 0) nv = 1;
hs *= bytes;
vs *= bytes;
for (auto &m : masks)
m = 0;
uint64_t hmask = makeMask(bytes) * (makeMask(nh * hs) / makeMask(hs));
for (int j = 0; j < nv; j++) {
masks[off >> log2MBits] |= compress(hmask << (off & (mbits - 1)));
off += vs;
}
size = ((off - vs) >> log2MBits) + 1;
}
void DependencyRegion::intersect(const DependencyRegion &other)
{
if (arf != other.arf) {
clear();
return;
}
if (unspecified || other.unspecified)
return;
int i, iOther;
for (i = 0, iOther = base - other.base; i < size; i++, iOther++) {
if (iOther >= 0 && iOther < other.size)
masks[i] &= other.masks[iOther];
else
masks[i] = 0;
}
}
// Check whether two regions overlap.
inline bool intersects(const DependencyRegion &dep1, const DependencyRegion &dep2)
{
// Check register file.
if (dep1.arf != dep2.arf)
return false;
// Unspecified regions might always overlap.
if (dep1.unspecified || dep2.unspecified)
return true;
// Quick check based on register bounds.
int diff = dep1.base - dep2.base;
if ((diff >= dep2.size) || (diff <= -dep1.size))
return false;
// Precise check.
int i1, i2;
for (i1 = 0, i2 = diff; i1 < dep1.size; i1++, i2++)
if (i2 >= 0 && i2 < dep2.size)
if (dep1.masks[i1] & dep2.masks[i2])
return true;
return false;
}
void DependencyRegion::subtract(const DependencyRegion &other)
{
if (other.arf != arf)
return;
if (unspecified)
return;
if (other.unspecified)
clear();
else {
int i, iOther;
for (i = 0, iOther = base - other.base; i < size; i++, iOther++)
if (iOther >= 0 && iOther < other.size)
masks[i] &= ~other.masks[iOther];
}
}
inline bool contains(const DependencyRegion &dep1, const DependencyRegion &dep2)
{
using mtype = decltype(DependencyRegion::masks)::value_type;
if (dep1.arf != dep2.arf) return false;
if (dep1.unspecified) return true;
if (dep2.unspecified) return false;
int i1, i2;
for (i1 = dep2.base - dep1.base, i2 = 0; i2 < dep2.size; i1++, i2++) {
mtype mask = (i1 >= 0 && i1 < dep1.size) ? dep1.masks[i1] : 0;
if (~mask && dep2.masks[i2])
return false;
}
return true;
}
inline bool bboxContains(const DependencyRegion &dep1, const DependencyRegion &dep2)
{
if (dep1.arf != dep2.arf) return false;
if (dep1.unspecified || dep2.unspecified) return false;
return (dep1.base <= dep2.base && dep1.base + dep1.size >= dep2.base + dep2.size);
}
// Check if an ARF type needs SWSB tracking.
inline bool trackableARF(ARFType type)
{
return (type == ARFType::acc || type == ARFType::a || type == ARFType::s);
}
// Distance in an in-order pipe after which a dependency can be ignored.
inline int timeout(GeneralizedPipe pipe)
{
switch (pipe.inOrderPipe()) {
case PipeMaskA: return 11; // Gen12LP
case PipeMaskI: return 11;
case PipeMaskF: return 11;
case PipeMaskL: return 15;
case PipeMaskM: return 19;
case PipeMaskS: return 11; // FIXME: use correct value when available
default: return std::numeric_limits<int>::max();
}
}
// Approximate upper bound on cycle count for an OOO instruction.
template <typename Instruction>
inline int estimateLatency(HW hw, const Instruction &insn)
{
switch (insn.opcode()) {
default:
case Opcode::math: return (hw == HW::Gen12LP) ? 20 : 17;
case Opcode::dpas:
case Opcode::dpasw: return 20; // need correct value
case Opcode::send:
case Opcode::sendc: {
switch (insn.sfid()) {
case SharedFunction::dc0:
case SharedFunction::dc1: {
MessageDescriptor desc;
if (insn.getSendDesc(desc))
if (desc.surface.index == 0xFE)
return (hw == HW::Gen12LP) ? 33 : 25;
return (hw == HW::Gen12LP) ? 106 : 150;
}
case SharedFunction::sampler: return (hw == HW::Gen12LP) ? 175 : 210;
default: return 50;
}
}
}
}
// Measure instruction distance between two Dependencies in a given pipe.
template <bool consumer1, bool consumer2>
inline int distance(const Dependency<consumer1> &dep1, const Dependency<consumer2> &dep2, GeneralizedPipe pipe)
{
auto ioPipe = pipe.inOrderPipe();
if (ioPipe == PipeMaskNone)
return 0;
auto pidx = utils::log2(ioPipe);
return dep2.counters[pidx] - dep1.counters[pidx];
}
// Check whether two dependencies form a producer-consumer pair.
inline bool intersects(const Dependency<false> &dep1, const Dependency<true> &dep2)
{
if (!dep2.swsb) {
// Region-based dependency. First, quick check based on dependency type:
// RAR: ignore
// WAR/WAW: ignore if both instructions in same in-order pipe, or same out-of-order pipe (WAR only)
// If not ignorable, check:
// * If consumer is in-order, is that pipe still live (unsynchronized) in the producer?
// * If producer is in-order, is it close enough to require tracking the dependency?
// * Do the producer+consumer regions overlap?
if (dep1.read() && dep2.read()) return false;
if (!(dep1.write() && dep2.write() && (dep1.region.checkWAW || dep2.region.checkWAW)))
if (dep1.read() || dep1.pipe.inOrder())
if (dep2.write() && (dep1.pipe == dep2.pipe) && (dep1.pipe != GeneralizedPipe::Math())) return false;
if (dep1.pipe.inOrder() && (distance(dep1, dep2, dep1.pipe) >= timeout(dep1.pipe))) return false;
if ((dep2.region.base >> 4) != (static_cast<uint8_t>(ARFType::s) & 0xF))
if (dep2.region.arf && (dep2.read() || dep2.region.hw == HW::Gen12LP)) return false;
return intersects(dep1.region, dep2.region);
} else {
// SWSB dependency.
if (dep1.hasToken() && dep2.hasToken() && (dep1.token == dep2.token) && (dep1.tokenSrc || dep2.tokenDst))
return true;
if (dep1.pipe.inOrder()) {
auto commonPipe = (dep1.pipe.inOrderPipe() | PipeMaskA) & dep2.depPipe;
if (commonPipe)
return (distance(dep1, dep2, dep1.pipe) >= dep2.dist);
}
return false;
}
}
// Check whether two dependencies form a producer-consumer pair.
inline bool intersects(const Dependency<true> &dep1, const Dependency<false> &dep2)
{
return intersects(dep2, dep1);
}
// Check whether one producer dependency implies another, without checking regions.
inline bool impliesWithoutRegion(const Dependency<false> &dep1, const Dependency<false> &dep2)
{
// Reads never imply writes.
if (dep2.write() && dep1.read())
return false;
// Check pipes.
if (dep2.pipe != dep1.pipe)
return false;
if (dep2.hasToken()) {
// Token dependency: tokens must match. If tokens not assigned, instructions must match.
if (!dep1.hasToken())
return false;
if (!dep1.tokenDst && dep2.tokenDst)
return false;
if (dep1.token != dep2.token)
return false;
if ((dep1.token == dep1.tokenTBD) && (dep1.inum != dep2.inum))
return false;
}
if (dep2.pipe.inOrder()) {
// Pipeline dependency: compare counters.
if (dep1.counters[PipeBitA] < dep2.counters[PipeBitA])
return false;
auto pidx = utils::log2(dep2.pipe.inOrderPipe());
if (dep1.counters[pidx] < dep2.counters[pidx])
return false;
}
return true;
}
// Check whether one consumer dependency implies another, without checking regions.
inline bool impliesWithoutRegion(const Dependency<true> &dep1, const Dependency<true> &dep2)
{
// Writes never imply reads.
if (dep2.read() && dep1.write()) return false;
// Check pipes.
if (dep2.pipe != dep1.pipe)
return false;
if (dep2.depPipe != dep1.depPipe)
return false;
if (dep2.hasToken()) {
// Token dependency.
if (!dep1.hasToken())
return false;
if (!dep1.tokenDst && dep2.tokenDst)
return false;
if (dep1.token != dep2.token)
return false;
}
if (dep2.pipe.inOrder()) {
// Pipeline dependency. Consumer dependencies are only compared
// within BBs, so it's enough to check the A counter.
// Note distance check not always valid for A@ consumers >= XeHP,
// but is never used in these cases.
if (dep2.counters[PipeBitA] < dep1.counters[PipeBitA])
return false;
if (dep2.hasDist() != dep1.hasDist())
return false;
if (dep2.hasDist())
if (distance(dep1, dep2, dep2.pipe) - dep2.dist + dep1.dist < 0)
return false;
if (dep1.read() && dep2.write())
return false;
}
return true;
}
template <bool consumer>
void DependencyTable<consumer>::clear()
{
deps.clear();
frags.clear();
for (int l = 0; l < NListTypes; l++)
std::fill(heads[l].begin(), heads[l].end(), none);
}
template <bool consumer>
void DependencyTable<consumer>::reserve(int icount)
{
icount *= 4;
deps.reserve(icount);
frags.reserve(icount * 4);
}
template <bool consumer>
bool DependencyTable<consumer>::insertPrepare(int listType, int listIdx, Dependency<consumer> &dep, bool checkWeaker, bool checkStronger)
{
for (auto fragID = heads[listType][listIdx]; fragID != none;) {
auto &frag = frags[fragID];
auto &entry = deps[frag.depID];
bool noRegions = (dep.region.unspecified && entry.region.unspecified);
if (checkWeaker && impliesWithoutRegion(entry, dep)) {
if (noRegions)
return false;
dep.region.subtract(entry.region);
if (dep.region.empty())
return false;
}
if (checkStronger && impliesWithoutRegion(dep, entry)) {
entry.region.subtract(dep.region);
if (entry.region.empty() || noRegions)
remove(fragID);
}
fragID = frag.next[listType];
}
return true;
}
template <bool consumer>
void DependencyTable<consumer>::insertLinkedList(int listType, int listIdx, int32_t fragID)
{
auto &head = heads[listType][listIdx];
auto &frag = frags[fragID];
frag.next[listType] = head;
frag.prev[listType] = makeHeadLink(listIdx);
if (head != none)
frags[head].prev[listType] = fragID;
head = fragID;
}
template <bool consumer>
template <bool iconsumer>
int DependencyTable<consumer>::getPipeIndex(const Dependency<iconsumer> &dep)
{
auto checkPipe = iconsumer ? dep.depPipe : dep.pipe.inOrderPipe();
if (!checkPipe)
return -1;
return utils::log2(checkPipe);
}
// Insert dependency into table.
// If checkStronger set, remove any weaker existing dependencies.
// If checkWeaker set, the input dependency's region will be adjusted to remove
// overlapping stronger dependencies. If this dependency is already implied by the
// table, it will not be added.
// Return value indicates whether dependency added.
template <bool consumer>
bool DependencyTable<consumer>::insert(Dependency<consumer> &dep, bool checkWeaker, bool checkStronger)
{
bool toAdd = true;
int pidx = getPipeIndex(dep);
bool checkToken = dep.hasToken()
&& !(!consumer && dep.token == Dependency<consumer>::tokenTBD && !dep.region.unspecified);
if (checkToken)
toAdd = toAdd && insertPrepare(ListTypeToken, dep.token, dep, checkWeaker, checkStronger);
else if (!dep.region.unspecified) {
for (int r = dep.region.base; r < dep.region.base + dep.region.size; r++)
toAdd = toAdd && insertPrepare(ListTypeGRF, r, dep, checkWeaker, checkStronger);
} else if (pidx >= 0)
toAdd = toAdd && insertPrepare(ListTypePipe, pidx, dep, checkWeaker, checkStronger);
if (!toAdd)
return false;
auto depID = int(deps.size());
deps.push_back(dep);
// Create fragments.
bool hasRegion = !dep.region.unspecified && (dep.region.size > 0);
int ridx = hasRegion ? dep.region.base : grfListIdxUnspecified;
int nfrags = hasRegion ? dep.region.size : 1;
auto fragID = int(frags.size());
DependencyFragment frag;
frag.before = 0;
frag.after = nfrags - 1;
frag.depID = depID;
for (int l = 0; l < NListTypes; l++)
frag.prev[l] = frag.next[l] = none;
for (int o = 0; o < nfrags; o++, fragID++, frag.before++, frag.after--) {
frags.push_back(frag);
if (hasRegion || dep.region.unspecified)
insertLinkedList(ListTypeGRF, ridx++, fragID);
if (o > 0)
continue;
if (dep.hasToken())
insertLinkedList(ListTypeToken, dep.token, fragID);
if (pidx >= 0)
insertLinkedList(ListTypePipe, pidx, fragID);
}
return true;
}
template <bool consumer>
void DependencyTable<consumer>::remove(int fragID)
{
auto &frag0 = frags[fragID];
deps[frag0.depID].active = false;
fragID -= frag0.before;
int nfrag = frag0.before + frag0.after + 1;
for (int i = 0; i < nfrag; i++, fragID++) {
auto &frag = frags[fragID];
int lcount = (i == 0) ? NListTypes : 1; // Only GRF linked lists contain multiple fragments per dependency.
for (int l = 0; l < lcount; l++) {
if (isHeadLink(frag.prev[l]))
heads[l][readHeadLink(frag.prev[l])] = frag.next[l];
else if (frag.prev[l] != none)
frags[frag.prev[l]].next[l] = frag.next[l];
if (frag.next[l] != none)
frags[frag.next[l]].prev[l] = frag.prev[l];
}
}
}
// Find dependencies in the table intersecting the given dependency, and append them to the given list.
// NB: the resulting list may contain duplicate dependencies.
template <bool consumer>
template <bool iconsumer>
void DependencyTable<consumer>::findIntersections(const Dependency<iconsumer> &dep, std::vector<Dependency<consumer>> &out)
{
findAndRemoveIntersections(dep, &out, false);
}
template <bool consumer>
template <bool iconsumer>
void DependencyTable<consumer>::findAndRemoveIntersections(int listType, int listIdx, const Dependency<iconsumer> &dep, std::vector<Dependency<consumer>> *out, bool doRemove)
{
for (auto fragID = heads[listType][listIdx]; fragID != none;) {
auto &frag = frags[fragID];
auto &entry = deps[frag.depID];
if (doRemove && !consumer && (distance(entry, dep, entry.pipe) >= timeout(entry.pipe)))
remove(fragID);
else if (intersects(dep, entry)) {
if (out != nullptr)
out->push_back(entry);
if (doRemove)
remove(fragID);
}
fragID = frag.next[listType];
}
}
// Find dependencies in the table intersecting the given dependency.
// Append them to the given list, and remove from table.
// Also checks for, and removes, timed-out producer dependencies.
template <bool consumer>
template <bool iconsumer>
void DependencyTable<consumer>::findAndRemoveIntersections(const Dependency<iconsumer> &dep, std::vector<Dependency<consumer>> *out, bool doRemove)
{
PipeMask checkPipe = PipeMaskNone;
bool checkToken = false;
bool checkRegion = !dep.region.empty();
if (iconsumer) {
if (dep.swsb) {
checkToken = true;
checkPipe = dep.depPipe;
checkRegion = false;
}
} else {
checkToken = true;
checkPipe = dep.pipe.inOrderPipe();
}
// Handle token dependencies.
if (checkToken && dep.hasToken() && dep.token != dep.tokenTBD)
findAndRemoveIntersections(ListTypeToken, dep.token, dep, out, doRemove);
// Handle pipeline dependencies.
if (checkPipe & PipeMaskA) {
for (int pidx = 0; pidx < NPipes; pidx++)
findAndRemoveIntersections(ListTypePipe, pidx, dep, out, doRemove);
} else if (checkPipe != PipeMaskNone) {
int pidx = utils::log2(checkPipe);
findAndRemoveIntersections(ListTypePipe, pidx, dep, out, doRemove);
findAndRemoveIntersections(ListTypePipe, PipeBitA, dep, out, doRemove);
}
// Handle GRF dependencies.
if (checkRegion) {
int base = dep.region.unspecified ? 0 : dep.region.base;
int len = dep.region.unspecified ? maxGRF : dep.region.size;
for (int r = base; r < base + len; r++)
findAndRemoveIntersections(ListTypeGRF, r, dep, out, doRemove);
findAndRemoveIntersections(ListTypeGRF, grfListIdxUnspecified, dep, out, doRemove);
}
}
// Find dependencies in the table intersecting the given dependency, and remove them.
template <bool consumer>
template <bool iconsumer>
void DependencyTable<consumer>::removeIntersections(const Dependency<iconsumer> &dep)
{
findAndRemoveIntersections(dep, nullptr);
}
// Remove dependencies from the table matching a token mask.
// Returns mask of unmatched tokens.
template <bool consumer>
uint32_t DependencyTable<consumer>::removeByTokenMask(uint32_t mask, bool dst)
{
uint32_t unmatched = mask;
while (mask) {
uint32_t mask1 = mask & ~(mask & (mask - 1));
mask &= ~mask1;
int token = utils::log2(mask1);
for (auto fragID = heads[ListTypeToken][token]; fragID != none;) {
auto &frag = frags[fragID];
auto &entry = deps[frag.depID];
if (entry.tokenSrc || (entry.tokenDst && dst)) {