File: GenXVectorCombiner.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 (453 lines) | stat: -rw-r--r-- 16,058 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2022 Intel Corporation

SPDX-License-Identifier: MIT

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

//
/// GenXVectorCombiner
/// ------------
// This pass checks whether we use whole vector in operation,
// but this operation is splitted for parts of vector,
// and if we can combine them together, we do this
///
//===----------------------------------------------------------------------===//

#include <algorithm>

#include <llvm/ADT/Statistic.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/InstIterator.h>
#include "llvmWrapper/Support/TypeSize.h"

#include "GenX.h"
#include "GenXIntrinsics.h"
#include "GenXModule.h"
#include "vc/Utils/GenX/Region.h"
#include "vc/Utils/General/InstRebuilder.h"

#include "IGC/common/debug/DebugMacros.hpp"

using namespace llvm;
using namespace genx;

#define DEBUG_TYPE "GENX_VECTOR_COMBINER"

STATISTIC(NumOfWidenInsructions,
          "Number of combined to wider variant instructions");

static cl::opt<bool>
    EnableGenXVectorCombiner("enable-Vector-Combiner-pass", cl::init(true),
                             cl::Hidden,
                             cl::desc("Enable Vector Combiner pass"));

namespace {

// GenXVectorCombiner : vectorize already vector instructions

// Currently pass work with
// add
// fadd
// bitcast
// trunc
// llvm.genx.absi
// llvm.genx.absf
// llvm.genx.bf.cvt
class GenXVectorCombiner final : public FunctionPass {
  struct InstructionPack {
    Instruction *RdRegion;
    Instruction *Operation;
    Instruction *WrRegion;
    explicit InstructionPack(Instruction *Rd, Instruction *Oper,
                             Instruction *Wr)
        : RdRegion{Rd}, Operation{Oper}, WrRegion{Wr} {
      IGC_ASSERT_MESSAGE(Rd && Oper && Wr, "Error: get nullptr");
    }
  };
  using ListOfPacks = SmallVector<InstructionPack, 4>;
  std::unordered_map<Value *, ListOfPacks> WorkList;

public:
  static char ID;
  explicit GenXVectorCombiner() : FunctionPass(ID) {}
  StringRef getPassName() const override { return "GenXVectorCombiner"; }
  void getAnalysisUsage(AnalysisUsage &AU) const override {}
  bool runOnFunction(Function &F) override;

private:
  void dumpWorkList() const;
  void collectWorkList(Function &F);
  void filterWorkList();
  bool processWorkList();
  bool checkWrRegions(const SmallVectorImpl<InstructionPack> &List);
  bool checkRdRegions(const SmallVectorImpl<InstructionPack> &List);
  bool checkSameInst(const SmallVectorImpl<InstructionPack> &List);
  void createNewInstruction(Instruction *UnsteadOf, Instruction *Operation,
                            const SmallVectorImpl<Value *> &Vals);
  bool isSupportedInst(const Instruction *Inst);
  bool isSupportedGenXIntrinsic(GenXIntrinsic::ID OpCode);
  bool isSupportedOpcode(unsigned OpCode);
  bool isSuitableRdRegion(Instruction &RdRegionInst);
};
} // end namespace

char GenXVectorCombiner::ID = 0;
namespace llvm {
void initializeGenXVectorCombinerPass(PassRegistry &);
}

INITIALIZE_PASS_BEGIN(GenXVectorCombiner, "GenXVectorCombiner",
                      "GenXVectorCombiner", false, false)

INITIALIZE_PASS_END(GenXVectorCombiner, "GenXVectorCombiner",
                    "GenXVectorCombiner", false, false)
namespace llvm {
FunctionPass *createGenXVectorCombinerPass() {
  initializeGenXVectorCombinerPass(*PassRegistry::getPassRegistry());
  return new GenXVectorCombiner;
}
} // namespace llvm
// Checks whether this instruction can be optimized by this pass.
bool GenXVectorCombiner::isSupportedOpcode(unsigned OpCode) {
  switch (OpCode) {
  default:
    return false;
  case Instruction::Add:
  case Instruction::FAdd:
  case Instruction::BitCast:
  case Instruction::Trunc:
    return true;
  }
  IGC_ASSERT_MESSAGE(false, "unreachable");
  return false;
}

// Checks whether this GenXIntrinsic can be optimized by this pass.
bool GenXVectorCombiner::isSupportedGenXIntrinsic(GenXIntrinsic::ID IdCode) {
  IGC_ASSERT(IdCode != GenXIntrinsic::not_genx_intrinsic);
  switch (IdCode) {
  default:
    return false;
  case GenXIntrinsic::genx_bf_cvt:
  case GenXIntrinsic::genx_absf:
  case GenXIntrinsic::genx_absi:
    return true;
  }
  IGC_ASSERT(0);
  return false;
}

bool GenXVectorCombiner::isSupportedInst(const Instruction *Inst) {
  IGC_ASSERT_MESSAGE(Inst, "Error: nullptr");
  if (isSupportedOpcode(Inst->getOpcode()))
    return true;
  if (GenXIntrinsic::isGenXIntrinsic(Inst) &&
      isSupportedGenXIntrinsic(GenXIntrinsic::getGenXIntrinsicID(Inst)))
    return true;
  return false;
}

bool GenXVectorCombiner::isSuitableRdRegion(Instruction &RdRegionInst) {
  if (!GenXIntrinsic::isRdRegion(&RdRegionInst))
    return false;
  if (!RdRegionInst.getType()->isVectorTy())
    return false;
  if (!RdRegionInst.hasOneUse())
    return false;
  Value *Original = RdRegionInst.getOperand(0);

  Instruction *Inst = dyn_cast<Instruction>(*RdRegionInst.user_begin());
  if (!Inst)
    return false;

  if (!Inst->hasOneUse())
    return false;

  if (!isSupportedInst(Inst))
    return false;

  Instruction *WrRegionInst = dyn_cast<Instruction>(*Inst->user_begin());
  if (!WrRegionInst)
    return false;
  if (!GenXIntrinsic::isWrRegion(WrRegionInst))
    return false;
  // we must check that wr region and rdregion use the same part of vector, and
  // splat is equal to one
  vc::CMRegion RdRegion{&RdRegionInst};
  vc::CMRegion WrRegion{WrRegionInst};

  unsigned OriginalElementCount =
      cast<IGCLLVM::FixedVectorType>(Original->getType())->getNumElements();
  unsigned WrRegionElementCount =
      cast<IGCLLVM::FixedVectorType>(WrRegionInst->getType())->getNumElements();
  if (OriginalElementCount != WrRegionElementCount)
    return false;

  if (RdRegion.Width != WrRegion.Width)
    return false;
  if (RdRegion.Stride != 1 || RdRegion.VStride != 0)
    return false;
  if (RdRegion.Stride != WrRegion.Stride)
    return false;
  if (RdRegion.VStride != WrRegion.VStride)
    return false;
  // for trunc offset is different, but number of elements the same
  unsigned RdRegionElementTySize = RdRegion.ElementTy->getScalarSizeInBits();
  unsigned WrRegionElementTySize = WrRegion.ElementTy->getScalarSizeInBits();
  IGC_ASSERT_MESSAGE(RdRegionElementTySize != 0, "Error: get ScalarSizeInBits");
  IGC_ASSERT_MESSAGE(WrRegionElementTySize != 0, "Error: get ScalarSizeInBits");
  return RdRegion.Offset / RdRegionElementTySize ==
         WrRegion.Offset / WrRegionElementTySize;
}

// Checks that wrregions are in right order:
// 1) element in list should be connected with next one
// 2) RdRegions and WrRegions have the same arguments for offset and width, so do not check them twice
bool GenXVectorCombiner::checkWrRegions(
    const SmallVectorImpl<InstructionPack> &List) {
  if (!List.front().WrRegion->hasOneUse())
    return false;

  auto ResIterator =
      std::adjacent_find(List.begin(), List.end(),
                         [](const auto &CurrentPack, const auto &NextPack) {
                           Instruction *CurrentWrRegion = CurrentPack.WrRegion;
                           Instruction *NextWrRegion = NextPack.WrRegion;
                           if (NextWrRegion->getOperand(0) != CurrentWrRegion)
                             return true;
                           return !NextWrRegion->hasOneUse();
                         });
  return ResIterator == List.end();
}

// Checks that rdregions are in right order and "fill" all original vector.
bool GenXVectorCombiner::checkRdRegions(
    const SmallVectorImpl<InstructionPack> &List) {
  Value* Original = List.front().RdRegion->getOperand(0);
  unsigned OriginalWidth =
      cast<IGCLLVM::FixedVectorType>(Original->getType())->getNumElements();

  Type *RdRegionTy = List.front().RdRegion->getType();
  IGC_ASSERT_MESSAGE(!RdRegionTy->isPtrOrPtrVectorTy(),
                     "Error: PointerType not expected");
  unsigned ElementSizeInBits = RdRegionTy->getScalarSizeInBits();
  IGC_ASSERT_MESSAGE(ElementSizeInBits != 0, "getScalarSizeInBits return 0");

  unsigned ElementSize = ElementSizeInBits / ByteBits;

  vc::CMRegion FirstRdRegion{List.front().RdRegion};
  if (FirstRdRegion.Offset != 0)
    return false;

  auto IteratorRes = std::adjacent_find(
      List.begin(), List.end(),
      [ElementSize](const auto &CurrentPack, const auto &NextPack) {
        vc::CMRegion RdRegionCurrent{CurrentPack.RdRegion};
        vc::CMRegion RdRegionNext{NextPack.RdRegion};
        IGC_ASSERT(RdRegionCurrent.Width == RdRegionCurrent.NumElements);
        return RdRegionCurrent.Offset + ElementSize * RdRegionCurrent.Width !=
               RdRegionNext.Offset;
      });
  if (IteratorRes != List.end())
    return false;

  vc::CMRegion LastRdRegion{List.back().RdRegion};
  IGC_ASSERT(LastRdRegion.Width == LastRdRegion.NumElements);
  return LastRdRegion.Offset + ElementSize * LastRdRegion.Width ==
         OriginalWidth * ElementSize;
}

bool GenXVectorCombiner::checkSameInst(
    const SmallVectorImpl<InstructionPack> &List) {
  Instruction *Operation = List.front().Operation;
  if (GenXIntrinsic::isGenXIntrinsic(Operation)) {
    GenXIntrinsic::ID IdCode = GenXIntrinsic::getGenXIntrinsicID(Operation);
    IGC_ASSERT(IdCode != GenXIntrinsic::not_genx_intrinsic);

    // TODO: currently no support for intrinsics with two srcs
    return std::all_of(
        std::next(List.begin()), List.end(), [IdCode](const auto &InstPack) {
          Instruction *Operation = InstPack.Operation;
          return IdCode == GenXIntrinsic::getGenXIntrinsicID(Operation);
        });
  }
  unsigned OpCode = Operation->getOpcode();
  bool TwoSources = Operation->getNumOperands() == 2;
  Constant *SecondSrc = nullptr;
  if (TwoSources)
    SecondSrc = dyn_cast<Constant>(Operation->getOperand(1));

  if (TwoSources && SecondSrc == nullptr)
    return false;
  // check that second source is the same
  return std::all_of(std::next(List.begin()), List.end(),
                     [TwoSources, OpCode, SecondSrc](const auto &InstPack) {
                       Instruction *Operation = InstPack.Operation;
                       if (OpCode != Operation->getOpcode())
                         return false;
                       return !(TwoSources &&
                                Operation->getOperand(1) != SecondSrc);
                     });
}

// Performs analysis for collected SmallVectors:
// 1) Checks RdRegion "read" whole original vector
// 2) The same Operation is called for all small vectors
// 3) Write order is right
void GenXVectorCombiner::filterWorkList() {
  std::vector<Value *> ToEraseFromWorkList;
  for (auto &MapElement : WorkList) {
    if (MapElement.second.size() == 1) {
      ToEraseFromWorkList.push_back(MapElement.first);
      continue;
    }
    if (!checkRdRegions(MapElement.second)) {
      ToEraseFromWorkList.push_back(MapElement.first);
      continue;
    }
    if (!checkWrRegions(MapElement.second)) {
      ToEraseFromWorkList.push_back(MapElement.first);
      continue;
    }
    if (!checkSameInst(MapElement.second)) {
      ToEraseFromWorkList.push_back(MapElement.first);
      continue;
    }
  }
  for (auto *EraseVal : ToEraseFromWorkList)
    WorkList.erase(EraseVal);
}

// Places new instruction or GenX intrinsic.
void GenXVectorCombiner::createNewInstruction(
    Instruction *InsteadOf, Instruction *Operation,
    const SmallVectorImpl<Value *> &Vals) {
  IGC_ASSERT_MESSAGE(InsteadOf && Operation, "Error: nullptr input");
  if (GenXIntrinsic::isGenXIntrinsic(Operation)) {
    IRBuilder<> Builder{InsteadOf};
    Function *Fn = nullptr;
    GenXIntrinsic::ID IdCode = GenXIntrinsic::getGenXIntrinsicID(Operation);
    Module *M = Operation->getParent()->getParent()->getParent();
    switch (IdCode) {
    case GenXIntrinsic::genx_absf:
    case GenXIntrinsic::genx_absi:
      Fn = GenXIntrinsic::getAnyDeclaration(M, IdCode, {InsteadOf->getType()});
      break;
    case GenXIntrinsic::genx_bf_cvt:
      Fn = GenXIntrinsic::getAnyDeclaration(
          M, IdCode, {InsteadOf->getType(), Vals[0]->getType()});
      break;
    default:
      IGC_ASSERT_MESSAGE(false, "unsupported intrinsic");
      return;
    }
    IGC_ASSERT(Fn);
    CallInst *CI = Builder.CreateCall(Fn, Vals, VALUE_NAME("widened"));
    InsteadOf->replaceAllUsesWith(CI);
    return;
  }
  Instruction *NewInst = vc::cloneInstWithNewOps(*Operation, Vals);
  IGC_ASSERT(NewInst);
  NewInst->insertBefore(InsteadOf);
  NewInst->setDebugLoc(InsteadOf->getDebugLoc());
  NewInst->takeName(Operation);
  InsteadOf->replaceAllUsesWith(NewInst);
}

// Puts new instructions instead of every SmallVector.
// returns true if IR changed
bool GenXVectorCombiner::processWorkList() {
  if (WorkList.empty())
    return false;
  for (auto &MapElement : WorkList) {
    Value *OriginalSrc = MapElement.first;
    InstructionPack &RefAnyInstPack = MapElement.second.back();
    // last wrregion from sequence
    Instruction *InsteadOf = MapElement.second.back().WrRegion;

    SmallVector<Value *, 2> Vals;
    Vals.push_back(OriginalSrc);
    // two src - second should be const
    bool IsGenXIntrinsic =
        GenXIntrinsic::isGenXIntrinsic(RefAnyInstPack.Operation);
    if (!IsGenXIntrinsic && RefAnyInstPack.Operation->getNumOperands() == 2) {
      Constant *ScalarSecondOperand =
          cast<Constant>(RefAnyInstPack.Operation->getOperand(1))
              ->getSplatValue();
      unsigned NumericWidth = cast<IGCLLVM::FixedVectorType>(OriginalSrc->getType())
                      ->getNumElements();
      auto Width = IGCLLVM::getElementCount(NumericWidth);
      Constant *WideConstant =
          ConstantVector::getSplat(Width, ScalarSecondOperand);
      Vals.push_back(WideConstant);
    }
    createNewInstruction(InsteadOf, RefAnyInstPack.Operation, Vals);

    ++NumOfWidenInsructions;
  }
  for (auto &MapElement : WorkList) {
    // wrregions are connected with each other, so last wrregion first
    std::for_each(MapElement.second.rbegin(), MapElement.second.rend(),
                  [](InstructionPack &Pack) {
                    Pack.WrRegion->eraseFromParent();
                    Pack.Operation->eraseFromParent();
                    Pack.RdRegion->eraseFromParent();
                  });
  }
  WorkList.clear();
  return true;
}

// Collects and checks some available on that level restrictions:
// * pattern: RdRegion -> Operation -> WrRegion
void GenXVectorCombiner::collectWorkList(Function &F) {
  for (auto &Inst : instructions(F)) {
    if (!isSuitableRdRegion(Inst))
      continue;
    Value *SrcV = Inst.getOperand(0);
    Instruction *RdRegion = &Inst;
    Instruction *Operation = cast<Instruction>(*RdRegion->user_begin());
    Instruction *WrRegion = cast<Instruction>(*Operation->user_begin());
    IGC_ASSERT_MESSAGE(
        cast<IGCLLVM::FixedVectorType>(SrcV->getType())->getNumElements() ==
            cast<IGCLLVM::FixedVectorType>(WrRegion->getType())
                ->getNumElements(),
        "Error: mismatch in element count unexpected");

    WorkList[SrcV].emplace_back(RdRegion, Operation, WrRegion);
  }
}

// Algorithm :
// 1) find all available combinations of RdRegion, Operation and WrRegion,
//        put it in SmallVector in history order
// 2) Analyze collected SmallVectors and check if we can combine them into wider
// one
// 3) Perform transformation
bool GenXVectorCombiner::runOnFunction(Function &F) {
  LLVM_DEBUG(dbgs() << "GenXVectorCombiner started\n");
  if (!EnableGenXVectorCombiner)
    return false;
  collectWorkList(F);
  filterWorkList();
  return processWorkList();
}

void GenXVectorCombiner::dumpWorkList() const {
  for (auto &List : WorkList) {
    dbgs() << "Original: ";
    List.first->dump();
    for (auto &InstPack : List.second) {
      Instruction *RdRegion = InstPack.RdRegion;
      Instruction *WrRegion = InstPack.WrRegion;
      Instruction *Operation = InstPack.Operation;
      RdRegion->dump();
      dbgs() << "\t";
      Operation->dump();
      dbgs() << "\t\t";
      WrRegion->dump();
    }
  }
}