File: LocalRA.h

package info (click to toggle)
intel-graphics-compiler2 2.24.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 113,504 kB
  • sloc: cpp: 812,849; lisp: 288,219; ansic: 102,423; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 318; sh: 162; makefile: 38
file content (555 lines) | stat: -rw-r--r-- 17,004 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#ifndef _INC_LOCALRA_H_
#define _INC_LOCALRA_H_

#include "BitSet.h"
#include "BuildIR.h"
#include "FlowGraph.h"
#include "G4_Opcode.h"
#include <list>

// Forward decls
namespace vISA {
class G4_Declare;
class G4_INST;
class G4_BB;
class LinearScan;
class LocalLiveRange;
class PhyRegLocalRA;
class PhyRegsManager;
class PhyRegSummary;
class BankConflictPass;
class GlobalRA;
} // namespace vISA

vISA::G4_Declare *GetTopDclFromRegRegion(vISA::G4_Operand *opnd);

typedef std::map<vISA::LocalLiveRange *,
                 std::vector<std::pair<INST_LIST_ITER, unsigned int>>>
    LLR_USE_MAP;
typedef std::map<vISA::LocalLiveRange *,
                 std::vector<std::pair<INST_LIST_ITER, unsigned int>>>::iterator
    LLR_USE_MAP_ITER;

// Each declaration will have a LocalLiveRange object allocated for it
namespace vISA {
class PhyRegsLocalRA;
class InputLiveRange;

class LocalRA {
private:
  G4_Kernel &kernel;
  IR_Builder &builder;
  PhyRegsLocalRA *pregs = nullptr;
  LLR_USE_MAP LLRUseMap;
  unsigned int numRegLRA = 0;
  unsigned int globalLRSize = 0;
  bool doSplitLLR = false;
  std::list<InputLiveRange> inputIntervals;
  BankConflictPass &bc;
  GlobalRA &gra;
  bool doBCR = false;
  bool highInternalConflict = false;
  bool hasSplitInsts = false;

  BankAlign getBankAlignForUniqueAssign(G4_Declare *dcl);
  bool hasBackEdge();
  void specialAlign();
  void preLocalRAAnalysis();
  void trivialAssignRA(bool &needGlobalRA, bool threeSourceCandidate);
  bool localRAPass(bool doRoundRobin, bool doSplitLLR);
  void resetMasks();
  unsigned int getLargestInputGRF();
  void blockOutputPhyRegs();
  void removeUnrequiredLifetimeOps();
  bool assignUniqueRegisters(bool twoBanksRA, bool twoDirectionsAssign,
                             bool hybridWithSpill);
  bool unassignedRangeFound();
  void updateRegUsage(PhyRegSummary *summary, unsigned int &numRegsUsed);
  void localRAOptReport();
  void markReferencesInOpnd(G4_Operand *opnd, bool isEOT,
                            INST_LIST_ITER inst_it, unsigned int pos);
  void addOutOfBoundForbidden(G4_Declare *dcl, G4_Operand *opnd);
  void markSpecialForbidden(INST_LIST_ITER inst_it);
  void markReferencesInInst(INST_LIST_ITER inst_it);
  void setLexicalID(bool includePseudo);
  void markReferences(unsigned int &numRowsEOT, bool &lifetimeOpFound);
  void calculateInputIntervals();
  bool hasDstSrcOverlapPotential(G4_DstRegRegion *dst, G4_SrcRegRegion *src);
  void calculateLiveIntervals(G4_BB *bb,
                              std::vector<LocalLiveRange *> &liveIntervals);
  void printAddressTakenDecls();
  void printLocalRACandidates();
  void printLocalLiveIntervals(G4_BB *bb,
                               std::vector<LocalLiveRange *> &liveIntervals);
  void printInputLiveIntervals();
  bool countLiveIntervals();

  // scratch fields used for parameter passing
  G4_BB *curBB = nullptr;

public:
  static void getRowInfo(int size, int &nrows, int &lastRowSize,
                         const IR_Builder &builder);
  static unsigned int convertSubRegOffFromWords(G4_Declare *dcl,
                                                int subregnuminwords);
  static unsigned int convertSubRegOffToWords(G4_Declare *dcl, int subregnum);
  static void
  countLocalLiveIntervals(std::vector<LocalLiveRange *> &liveIntervals,
                          unsigned grfSize);
  static void printLocalLiveIntervalDistribution(unsigned int numScalars,
                                                 unsigned int numHalfGRF,
                                                 unsigned int numOneGRF,
                                                 unsigned int numTwoOrMoreGRF,
                                                 unsigned int numTotal);

  LocalRA(BankConflictPass &, GlobalRA &);
  bool localRA();
  void undoLocalRAAssignments(bool clearInterval);
  bool doHybridBCR() const { return doBCR; }
  bool hasHighInternalBC() const { return highInternalConflict; }
};

class LocalLiveRange {
private:
  G4_Declare *topdcl;
  G4_INST *firstRef;
  G4_INST *lastRef;
  unsigned int lrStartIdx, lrEndIdx;
  bool isIndirectAccess;
  G4_VarBase *preg;
  // pregoff is stored in word here
  // But subreg offset stored in regvar should be in units of dcl's element size
  int pregoff;

  unsigned int numRefsInFG;
  G4_BB *prevBBRef;
  bool eot;

  bool assigned;
  bool isSplit;
  bool defined;

  IR_Builder &builder;

  std::unordered_set<unsigned int> forbiddenGRFs;

public:
  LocalLiveRange(IR_Builder &b) : builder(b) {
    topdcl = NULL;
    firstRef = lastRef = NULL;
    lrStartIdx = lrEndIdx = 0;
    isIndirectAccess = false;
    numRefsInFG = 0;
    prevBBRef = NULL;
    preg = NULL;
    pregoff = 0;
    assigned = false;
    eot = false;
    isSplit = false;
    defined = false;

    if (!builder.canWriteR0())
      addForbidden(0);
    if (builder.mustReserveR1())
      addForbidden(1);
  }

  // A reference to this live range exists in bb basic block, record it
  void recordRef(G4_BB *);

  void markIndirectRef() { isIndirectAccess = true; }

  // A live range is local if it is never accessed indirectly (via address taken
  // register) and only a single basic block references the range
  bool isLiveRangeLocal() const;

  bool isLiveRangeGlobal();

  bool isGRFRegAssigned();

  void setTopDcl(G4_Declare *dcl) {
    vISA_ASSERT(topdcl == NULL, "Redefining top dcl");
    topdcl = dcl;
  }

  G4_Declare *getTopDcl() const { return topdcl; }

  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }

  bool hasIndirectAccess() const { return isIndirectAccess; }

  void setFirstRef(G4_INST *inst, unsigned int idx) {
    firstRef = inst;
    lrStartIdx = idx;
  }

  G4_INST *getFirstRef(unsigned int &idx) const {
    idx = lrStartIdx;
    return firstRef;
  }

  void setLastRef(G4_INST *inst, unsigned int idx) {
    lastRef = inst;
    lrEndIdx = idx;
  }

  G4_INST *getLastRef(unsigned int &idx) {
    idx = lrEndIdx;
    return lastRef;
  }

  void setPhyReg(G4_VarBase *pr, int subreg) {
    preg = pr;
    pregoff = subreg;
  }
  G4_VarBase *getPhyReg(int &subreg) {
    subreg = pregoff;
    return preg;
  }

  unsigned int getSizeInWords();

  void setAssigned(bool a) { assigned = a; }
  bool getAssigned() const { return assigned; }

  void markEOT() { eot = true; }
  bool isEOT() const { return eot; }

  void markSplit() { isSplit = true; }
  bool getSplit() const { return isSplit; }

  void markDefined() { defined = true; }
  bool isDefined() const { return defined; }

  void addForbidden(unsigned int f) { forbiddenGRFs.insert(f); }
  std::unordered_set<unsigned int> &getForbidden() { return forbiddenGRFs; }
};

class InputLiveRange {
private:
  unsigned int regWordIdx;
  unsigned int lrEndIdx;

public:
  InputLiveRange(unsigned int regId, unsigned int endId)
      : regWordIdx(regId), lrEndIdx(endId) {}

  unsigned int getRegWordIdx() { return regWordIdx; }
  unsigned int getLrEndIdx() { return lrEndIdx; }
};
} // namespace vISA
#define SECOND_HALF_BANK_START_GRF 64
#define RR_HEURISTIC 0.66
enum {
  WORD_FREE = 0,
  WORD_BUSY = 1,
};

namespace vISA {
class PhyRegsLocalRA {
private:
  const IR_Builder &builder;
  unsigned int numRegs;
  // nth bit represents whether the register's nth word is free/busy
  // 1 - busy, 0 - free
  // It is possible to use bit-vector in place of this array
  // bitvector does not provide coarse grained access to mark
  // entire grf busy/available
  std::vector<uint32_t> regBusyVector;
  std::vector<int32_t> regLastUse;
  std::vector<bool> grfAvialable;
  int lastUseSum1;
  int lastUseSum2;
  int bank1AvailableRegNum;
  int bank2AvailableRegNum;

  bool twoBanksRA;
  bool simpleGRFAvailable;
  bool r0Forbidden;
  bool r1Forbidden;

  int LraFFWindowSize;

public:
  PhyRegsLocalRA(const IR_Builder &_builder, uint32_t nregs)
      : builder(_builder), numRegs(nregs) {
    uint32_t grfFree = 0;

    regBusyVector.resize(numRegs);
    regLastUse.resize(numRegs);
    grfAvialable.resize(numRegs);

    for (int i = 0; i < (int)nregs; i++) {
      regBusyVector[i] = grfFree;
      regLastUse[i] = 0;
      grfAvialable[i] = true;
    }

    lastUseSum1 = 0;
    lastUseSum2 = 0;
    bank1AvailableRegNum = 0;
    bank2AvailableRegNum = 0;

    twoBanksRA = false;
    simpleGRFAvailable = false;
    r0Forbidden = false;
    r1Forbidden = false;
    LraFFWindowSize =
        (int)builder.getOptions()->getuInt32Option(vISA_LraFFWindowSize);
  }

  void findRegisterCandiateWithAlignForward(int &i, BankAlign align,
                                            bool evenAlign);

  unsigned int get_bundle(unsigned int baseReg, int offset);

  int findBundleConflictFreeRegister(int curReg, int endReg,
                                     unsigned short occupiedBundles,
                                     BankAlign align, bool evenAlign);

  void findRegisterCandiateWithAlignBackward(int &i, BankAlign align,
                                             bool evenAlign);

  void setGRFBusy(int which);
  void setGRFBusy(int which, bool isInput);
  void setGRFBusy(int which, int howmany, bool isInput);
  void setGRFBusy(int which, int howmany);
  void setWordBusy(int whichgrf, int word, bool isInput);
  void setGRFNotBusy(int which, int instID);
  void setH1GRFBusy(int which);
  void setH2GRFBusy(int which);
  void setWordBusy(int whichgrf, int word);
  void setWordBusy(int whichgrf, int word, int howmany, bool isInput);
  void setWordBusy(int whichgrf, int word, int howmany);
  void setWordNotBusy(int whichgrf, int word, int instID);

  inline bool isGRFBusy(int which) const {
    vISA_ASSERT(isGRFAvailable(which), "Invalid register");
    return (regBusyVector[which] != 0);
  }

  inline bool isGRFBusy(int which, int howmany) const {
    bool retval = false;

    for (int i = 0; i < howmany && !retval; i++) {
      retval |= isGRFBusy(which + i);
    }

    return retval;
  }

  bool isH1GRFBusy(int which);
  bool isH2GRFBusy(int which);
  inline bool isWordBusy(int whichgrf, int word);
  bool isWordBusy(int whichgrf, int word, int howmany);

  bool findFreeMultipleRegsForward(int regIdx, BankAlign align, int &regnum,
                                   int nrows, int lastRowSize, int endReg,
                                   unsigned short occupiedBundles, int instID,
                                   bool isHybridAlloc,
                                   std::unordered_set<unsigned int> &forbidden);

  void markPhyRegs(G4_Declare *topdcl);

  // Available/unavailable is different from busy/free
  // Unavailable GRFs are not available for allocation
  void setGRFUnavailable(int which) { grfAvialable[which] = false; }
  bool isGRFAvailable(int which) const {

    if (simpleGRFAvailable) {
      if (which > 1) {
        return true;
      } else {
        if (r0Forbidden && which == 0) {
          return false;
        }

        if (r1Forbidden && which <= 1) {
          return false;
        }
        return true;
      }
    } else {
      vISA_ASSERT(which < (int)numRegs, "invalid GRF");
      return (grfAvialable[which] == true);
    }
  }

  bool isGRFAvailable(int which, int howmany) const {
    if (simpleGRFAvailable) {
      if (which > 1) {
        return true;
      } else {
        if (r0Forbidden && which == 0) {
          return false;
        }

        if (r1Forbidden && which <= 1) {
          return false;
        }
        return true;
      }
    } else {
      for (int i = 0; i < howmany; i++) {
        if (!isGRFAvailable(which + i)) {
          return false;
        }
      }
    }

    return true;
  }

  void printBusyRegs();
  int getRegLastUse(int reg) { return regLastUse[reg]; }

  int getLastUseSum1() { return lastUseSum1; }
  int getLastUseSum2() { return lastUseSum2; }
  int getBank1AvailableRegNum() { return bank1AvailableRegNum; }
  int getBank2AvailableRegNum() { return bank2AvailableRegNum; }

  void setBank1AvailableRegNum(int avail1) { bank1AvailableRegNum = avail1; }
  void setBank2AvailableRegNum(int avail2) { bank2AvailableRegNum = avail2; }

  void setTwoBanksRA(bool twoBanks) { twoBanksRA = twoBanks; }
  void setSimpleGRFAvailable(bool simple) { simpleGRFAvailable = simple; }
  void setR0Forbidden() { r0Forbidden = true; }
  void setR1Forbidden() { r1Forbidden = true; }
  bool
  findFreeMultipleRegsBackward(int regIdx, BankAlign align, int &regnum,
                               int nrows, int lastRowSize, int endReg,
                               int instID, bool isHybridAlloc,
                               std::unordered_set<unsigned int> &forbidden);
  bool findFreeSingleReg(int regIdx, G4_SubReg_Align subalign, int &regnum,
                         int &subregnum, int size);
  bool findFreeSingleReg(int regIdx, int size, BankAlign align,
                         G4_SubReg_Align subalign, int &regnum, int &subregnum,
                         int endReg, int instID, bool isHybridAlloc,
                         bool forward,
                         std::unordered_set<unsigned int> &forbidden);

  bool findFreeMultipleRegsForward(int regIdx, BankAlign align, int &regnum,
                                   int nrows, int lastRowSize, int endReg,
                                   int instID, const bool *forbidden);

  bool findFreeSingleReg(int regIdx, int size, BankAlign align,
                         G4_SubReg_Align subalign, int &regnum, int &subregnum,
                         int endReg, const bool *forbidden);
};

class PhyRegsManager {
private:
  const IR_Builder &builder;
  PhyRegsLocalRA availableRegs;
  bool twoBanksRA;

public:
  PhyRegsManager(const IR_Builder &irb, const PhyRegsLocalRA& pregs, bool _twoBanksRA)
      : builder(irb), availableRegs(pregs), twoBanksRA(_twoBanksRA) {
    availableRegs.setTwoBanksRA(_twoBanksRA);
  }

  void freeRegs(int regnum, int subregnum, int numwords, int instID);
  PhyRegsLocalRA *getAvailableRegs() { return &availableRegs; }
  int findFreeRegs(int size, BankAlign align, G4_SubReg_Align subalign,
                   int &regnum, int &subregnum, int startRegNum, int endRegNum,
                   unsigned short occupiedBundles, unsigned int instID,
                   bool isHybridAlloc,
                   std::unordered_set<unsigned int> &forbidden);
  int findFreeRegs(int size, BankAlign align, G4_SubReg_Align subalign,
                   int &regnum, int &subregnum, int startRegNum, int endRegNum,
                   unsigned int instID, const bool *forbidden);
};

class LinearScan {
private:
  GlobalRA &gra;
  IR_Builder &builder;
  PhyRegsManager &pregManager;
  PhyRegsLocalRA &initPregs;
  std::vector<LocalLiveRange *> &liveIntervals;
  std::list<InputLiveRange> &inputIntervals;
  std::list<LocalLiveRange *> active;
  PhyRegSummary *summary;

  void expireRanges(unsigned int);
  void expireInputRanges(unsigned int, unsigned int, unsigned int);
  void expireAllActive();
  bool allocateRegsFromBanks(LocalLiveRange *);
  bool allocateRegs(LocalLiveRange *lr, G4_BB *bb, IR_Builder &builder,
                    LLR_USE_MAP &LLRUseMap);
  void freeAllocedRegs(LocalLiveRange *, bool);
  void updateActiveList(LocalLiveRange *);

  BitSet pregs;
  unsigned int simdSize;

  unsigned int globalLRSize;
  unsigned int *startGRFReg;
  unsigned int numRegLRA;

  unsigned int bank1StartGRFReg;
  unsigned int bank2StartGRFReg;
  unsigned int bank1_start;
  unsigned int bank1_end;
  unsigned int bank2_start;
  unsigned int bank2_end;

  bool useRoundRobin;
  bool doBankConflict;
  bool highInternalConflict;
  bool doSplitLLR;

public:
  LinearScan(GlobalRA &g, std::vector<LocalLiveRange *> &localLiveIntervals,
             std::list<InputLiveRange> &inputLivelIntervals,
             PhyRegsManager &pregMgr, PhyRegsLocalRA &pregs,
             PhyRegSummary *s, unsigned int numReg,
             unsigned int glrs, bool roundRobin, bool bankConflict,
             bool internalConflict, bool splitLLR, unsigned int simdS);

  void run(G4_BB *bb, IR_Builder &builder, LLR_USE_MAP &LLRUseMap);
};

class PhyRegSummary {
private:
  const IR_Builder *builder = nullptr;
  std::vector<bool> GRFUsage;

public:
  PhyRegSummary() {}

  PhyRegSummary(const IR_Builder *irb, uint32_t numGRF) : builder(irb) {
    GRFUsage.resize(numGRF, false);
  }

  uint32_t getNumGRF() const { return GRFUsage.size(); }

  void markPhyRegs(G4_VarBase *pr, unsigned int words);

  bool isGRFBusy(int regnum) const { return GRFUsage[regnum]; }

  void setGRFBusy(int regnum) { GRFUsage[regnum] = true; }

  void printBusyRegs();

  uint32_t getNumFreeGRF() const {
    uint32_t numFreeGRF = 0;
    for (int i = 0, size = getNumGRF(); i < size; ++i) {
      if (!isGRFBusy(i)) {
        numFreeGRF++;
      }
    }
    return numFreeGRF;
  }
};
} // namespace vISA
#endif // _INC_LOCALRA_H_