File: GenXPrinter.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (308 lines) | stat: -rw-r--r-- 10,164 bytes parent folder | download | duplicates (3)
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

//
// GenXPrinter is a pass that prints the LLVM IR for a function, together
// GenX specific analyses (instruction baling, liveness, register allocation).
//
//===----------------------------------------------------------------------===//

#include "FunctionGroup.h"
#include "GenX.h"
#include "GenXBaling.h"
#include "GenXLiveness.h"
#include "GenXNumbering.h"
#include "GenXVisaRegAlloc.h"

#include "vc/Utils/GenX/RegCategory.h"

#include "llvm/IR/Function.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/raw_ostream.h"

#include "Probe/Assertion.h"

using namespace llvm;
using namespace genx;

namespace {

// GenXPrinter : an analysis to print a Function, with GenX specific analyses
class GenXPrinter : public FunctionPass {
  raw_ostream &OS;
  const std::string Banner;
public:
  static char ID;
  explicit GenXPrinter(raw_ostream &OS, const std::string &Banner)
    : FunctionPass(ID), OS(OS), Banner(Banner) { }
  StringRef getPassName() const override { return "GenX printer pass"; }
  void getAnalysisUsage(AnalysisUsage &AU) const override {
    AU.addUsedIfAvailable<FunctionGroupAnalysis>();
    AU.addUsedIfAvailable<GenXVisaRegAlloc>();
    AU.addUsedIfAvailable<GenXLiveness>();
    AU.addUsedIfAvailable<GenXNumbering>();
    AU.addUsedIfAvailable<GenXFuncBaling>();
    AU.addUsedIfAvailable<GenXGroupBaling>();
    AU.setPreservesAll();
  }
  bool runOnFunction(Function &F) override;
};

// GenXGroupPrinter : an analysis to print Module with all FunctionGroups, with
// GenX specific analyses
class GenXGroupPrinter : public ModulePass {
  raw_ostream &OS;
  const std::string Banner;
public:
  static char ID;
  explicit GenXGroupPrinter(raw_ostream &OS, const std::string &Banner)
      : ModulePass(ID), OS(OS), Banner(Banner) {}
  StringRef getPassName() const override {
    return "GenX FunctionGroup printer pass";
  }
  void getAnalysisUsage(AnalysisUsage &AU) const override {
    AU.addRequired<FunctionGroupAnalysis>();
    AU.addPreserved<FunctionGroupAnalysis>();
    AU.addUsedIfAvailable<GenXVisaRegAlloc>();
    AU.addUsedIfAvailable<GenXLiveness>();
    AU.addUsedIfAvailable<GenXNumbering>();
    AU.addUsedIfAvailable<GenXGroupBaling>();
    AU.setPreservesAll();
  }
  bool runOnModule(Module &M) override {
    bool Changed = false;
    FunctionGroupAnalysis &FGA = getAnalysis<FunctionGroupAnalysis>();
    for (auto *FunctionGroup : FGA.AllGroups())
      Changed |= runOnFunctionGroup(*FunctionGroup);
    return Changed;
  }
  bool runOnFunctionGroup(FunctionGroup &FG);
};

} // end namespace llvm

char GenXPrinter::ID = 0;

FunctionPass *llvm::createGenXPrinterPass(raw_ostream &O, const std::string &Banner)
{
  FunctionPass *Created = new GenXPrinter(O, Banner);
  IGC_ASSERT(Created);
  return Created;
}

char GenXGroupPrinter::ID = 0;

ModulePass *llvm::createGenXGroupPrinterPass(raw_ostream &O,
                                             const std::string &Banner) {
  ModulePass *Created = new GenXGroupPrinter(O, Banner);
  IGC_ASSERT(Created);
  return Created;
}

// If possible, print register info and return true.
// Otherwise do nothing and return false.
static bool tryPrintRegInfo(raw_ostream &OS, SimpleValue SV,
                            GenXVisaRegAlloc *RA) {
  if (!RA)
    return false;
  auto *Reg = RA->getRegForValueUntyped(SV);
  if (!Reg)
    return false;
  Reg->print(OS);
  // FIXME: currently it is impossible to print base reg for alias.
  // Visa regalloc does not store aliases for simple values, they
  // are created during cisa builder without actual mapping update.
  // To fix this, we should either change IR to actually represent
  // register aliasing (partially implemented by bitcasts) or change
  // regalloc API to store map of Use to Reg.
  // First option is preferrable because it allows cisa builder to
  // work without modification of regalloc maps.
  return true;
}

// If possible, print category info and return true.
// Otherwise do nothing and return false.
static bool tryPrintCategoryInfo(raw_ostream &OS, SimpleValue SV,
                                 GenXLiveness *Liveness) {
  if (!Liveness)
    return false;

  auto *LR = Liveness->getLiveRangeOrNull(SV);
  if (!LR || LR->Category == vc::RegCategory::None)
    return false;

  OS << vc::getRegCategoryShortName(LR->Category);
  return true;
}

// Print additional info for simple value.
// If there is allocated register then print it.
// Otherwise if category is assigned then print it.
// Otherwise print "-".
static void printPropertiesForSimpleValue(raw_ostream &OS, SimpleValue SV,
                                          GenXLiveness *Liveness,
                                          GenXVisaRegAlloc *RA) {
  if (tryPrintRegInfo(OS, SV, RA))
    return;

  if (tryPrintCategoryInfo(OS, SV, Liveness))
    return;

  OS << "-";
}

// Show allocated register or category (if any) in brackets. If it is
// a struct type, then show info for each simple value.
static void printPropertiesForValue(raw_ostream &OS, Value &V,
                                    GenXLiveness *Liveness,
                                    GenXVisaRegAlloc *RA) {
  // No info available.
  if (!RA && !Liveness)
    return;

  const unsigned SVNum = IndexFlattener::getNumElements(V.getType());
  // Void or empty struct.
  if (SVNum == 0)
    return;

  OS << "[";
  printPropertiesForSimpleValue(OS, {&V, 0}, Liveness, RA);
  for (unsigned I = 1; I != SVNum; ++I) {
    OS << ",";
    printPropertiesForSimpleValue(OS, {&V, I}, Liveness, RA);
  }
  OS << "]";
}

/***********************************************************************
 * printFunction : print function with GenX analyses
 */
static void printFunction(raw_ostream &OS, Function &F, GenXBaling *Baling,
                          GenXLiveness *Liveness, GenXNumbering *Numbering,
                          GenXVisaRegAlloc *RA) {
  // This code is a downmarket version of AssemblyWriter::printFunction.
  // We have our own version so we can show bales.
  OS << "\ndefine ";
  F.getReturnType()->print(OS, /*IsForDebug=*/false, /*NoDetails=*/true);
  OS << " @" << F.getName() << "(";
  for (Function::arg_iterator fb = F.arg_begin(), fi = fb, fe = F.arg_end();
      fi != fe; ) {
    if (fi != fb)
      OS << ", ";
    Argument *Arg = &*fi;
    ++fi;
    Arg->getType()->print(OS, /*IsForDebug=*/false, /*NoDetails=*/true);
    OS << " ";
    printPropertiesForValue(OS, *Arg, Liveness, RA);
    OS << "%" << Arg->getName();
  }
  OS << ") {\n";
  for (Function::iterator fi = F.begin(), fe = F.end(); fi != fe; ++fi) {
    BasicBlock *BB = &*fi;
    if (!BB->use_empty())
      OS << BB->getName() << ":\n";
    for (BasicBlock::iterator bi = BB->begin(), be = BB->end(); bi != be; ++bi) {
      Instruction *Inst = &*bi;
      if (!Baling || !Baling->isBaled(Inst)) {
        printPropertiesForValue(OS, *Inst, Liveness, RA);
        // Show instruction number in brackets.
        unsigned Num = 0;
        if (Numbering)
          Num = Numbering->getNumber(Inst);
        if (Num)
          OS << "[" << Num << "]";
        if (!Baling) {
          Inst->print(OS);
          OS << "\n";
        } else {
          Bale B;
          Baling->buildBale(Inst, &B);
          if (B.size() == 1) {
            Inst->print(OS);
            OS << "\n";
          } else {
            OS << "  bale {\n";
            for (Bale::iterator i = B.begin(),
                e = B.end(); i != e; ++i) {
              unsigned Num = 0;
              if (Numbering)
                Num = Numbering->getNumber(i->Inst);
              if (Num)
                OS << "[" << Num << "]";
              OS << "   ";
              i->Inst->print(OS);
              switch (i->Info.Type) {
                case BaleInfo::MAININST: break;
                default: OS << " {" << i->Info.getTypeString() << "}"; break;
              }
              OS << "\n";
            }
            if (Num)
              OS << "[" << Num << "]";
            OS << "  }\n";
          }
        }
      }
    }
  }
  OS << "}\n";
}

// Helper to get FG analysis for given FG if it is available.
template <typename Analysis>
static Analysis *getAnalysisForFGIfAvailable(Pass &P, FunctionGroup &FG) {
  using FGWrapperTy = FunctionGroupWrapperPass<Analysis>;
  if (auto *Wrapper = P.getAnalysisIfAvailable<FGWrapperTy>())
    return &Wrapper->getFGPassImpl(&FG);
  return nullptr;
}

// Dump function with GenX analyses.
bool GenXPrinter::runOnFunction(Function &F) {
  auto *FGA = getAnalysisIfAvailable<FunctionGroupAnalysis>();
  GenXBaling *Baling = getAnalysisIfAvailable<GenXFuncBaling>();
  GenXVisaRegAlloc *RA = nullptr;
  GenXLiveness *Liveness = nullptr;
  GenXNumbering *Numbering = nullptr;
  if (FGA) {
    auto *FG = FGA->getAnyGroup(&F);
    RA = getAnalysisForFGIfAvailable<GenXVisaRegAlloc>(*this, *FG);
    Numbering = getAnalysisForFGIfAvailable<GenXNumbering>(*this, *FG);
    Liveness = getAnalysisForFGIfAvailable<GenXLiveness>(*this, *FG);
    Baling = getAnalysisForFGIfAvailable<GenXGroupBaling>(*this, *FG);
  }

  OS << Banner;
  printFunction(OS, F, Baling, Liveness, Numbering, RA);
  return false;
}

// Dump function groups with GenX analyses.
bool GenXGroupPrinter::runOnFunctionGroup(FunctionGroup &FG) {
  GenXVisaRegAlloc *RA =
      getAnalysisForFGIfAvailable<GenXVisaRegAlloc>(*this, FG);
  GenXBaling *Baling = getAnalysisForFGIfAvailable<GenXGroupBaling>(*this, FG);

  GenXLiveness *Liveness = nullptr;
  GenXNumbering *Numbering = nullptr;
  if (!RA) {
    Liveness = getAnalysisForFGIfAvailable<GenXLiveness>(*this, FG);
    Numbering = getAnalysisForFGIfAvailable<GenXNumbering>(*this, FG);
  }
  OS << Banner;
  if (Liveness)
    OS << " (see below for GenXLiveness)";
  for (auto i = FG.begin(), e = FG.end(); i != e; ++i)
    printFunction(OS, **i, Baling, Liveness, Numbering, RA);
  if (Liveness) {
    Liveness->print(OS);
    OS << "\n";
  }
  OS << "\n";
  return false;
}