File: InstRebuilder.h

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 (312 lines) | stat: -rw-r--r-- 12,677 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#ifndef VC_UTILS_GENERAL_INST_REBUILDER_H
#define VC_UTILS_GENERAL_INST_REBUILDER_H

#include "Probe/Assertion.h"

#include <llvm/ADT/ArrayRef.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/Use.h>
#include <llvm/Support/Casting.h>

#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace vc {

// Creates new instruction with all the properties taken from the \p OrigInst
// except for operands that are taken from \p NewOps.
// nullptr is returned when clonning is imposible or yet unsupported.
llvm::Instruction *cloneInstWithNewOps(llvm::Instruction &OrigInst,
                                       llvm::ArrayRef<llvm::Value *> NewOps);

// This structure defines a use with \p User instruction and \p OperandNo of its
// operand. And there's new value \p NewOperand for this operand.
struct UseToRebuild {
  llvm::Instruction *User = nullptr;
  int OperandNo;
  llvm::Value *NewOperand;
  bool IsTerminal = false;
};

// This structure defines which \p OperandNos of \p User instruction should be
// rebuilt. Corresponding new values are provided in \p NewOperands.
// (OperandNos.size() == NewOperands.size())
struct InstToRebuild {
  llvm::Instruction *User = nullptr;
  std::vector<int> OperandNos;
  std::vector<llvm::Value *> NewOperands;
  bool IsTerminal = false;

  // Returns whether the structure represents a valid info to rebuild an
  // instruction.
  bool validate() const {
    if (!User)
      return false;
    if (OperandNos.size() != NewOperands.size())
      return false;
    int NumOperands = User->getNumOperands();
    return std::all_of(OperandNos.begin(), OperandNos.end(),
                       [NumOperands](auto OperandNo) {
                         return OperandNo >= 0 && OperandNo < NumOperands;
                       });
  }
};

// The info required to rebuild the instructions.
// If element's NewOperand is equal to nullptr, it means that this operand/use
// should be replaced with previously build instruction.
using RebuildInfo = std::vector<UseToRebuild>;

// A helper class to generate RebuildInfo.
// Abstract:
// One does not simply change an operand of an instruction with a value with a
// different type. In this case instruction changes its type and must be
// rebuild. That causes a chain reaction as instruction's users now has to be
// rebuild to.
//
// Usage:
// A user should provide instructions into this builder in reverse post-order.
// An instruction must be defined as entry (the one that causes chain reaction)
// or as a potential node inside the chain(user can pass all instructions that
// are not entries - that's fine, if user knows for sure that this instruction
// isn't in the chain, user is able to not pass this instruction).
// A user must provide a functor (const Instruction &)-> bool that will define
// whether an instruction is a terminal - the last instruction in the chain
// reaction. For all other instructions it is considered that they are
// continuing the reaction.
template <typename IsTerminalFunc> class RebuildInfoBuilder {
  IsTerminalFunc IsTerminal;
  RebuildInfo Info;
  std::unordered_set<llvm::Value *> ChangedOperands;

public:
  RebuildInfoBuilder(IsTerminalFunc IsTerminalIn) : IsTerminal{IsTerminalIn} {}

  void addEntry(llvm::Instruction &Inst, int OperandNo,
                llvm::Value &NewOperand) {
    addNode(Inst, OperandNo, &NewOperand);
  }

  void addNodeIfRequired(llvm::Instruction &Inst, int OperandNo) {
    if (ChangedOperands.count(Inst.getOperand(OperandNo)))
      addNode(Inst, OperandNo, nullptr);
  }

  // Emit the gathered data.
  RebuildInfo emit() && { return std::move(Info); }

private:
  void addNode(llvm::Instruction &Inst, int OperandNo,
               llvm::Value *NewOperand) {
    // Users are covered here too as phi use can be back edge, so RPO won't help
    // and it won't be covered.
    IGC_ASSERT_MESSAGE(!llvm::isa<llvm::PHINode>(Inst),
                       "phi-nodes aren't yet supported");
    IGC_ASSERT_MESSAGE(IsTerminal(Inst) ||
                           std::all_of(Inst.user_begin(), Inst.user_end(),
                                       [](const llvm::User *U) {
                                         return !llvm::isa<llvm::PHINode>(U);
                                       }),
                       "phi-nodes aren't yet supported");
    auto InstIsTerminal = IsTerminal(Inst);
    Info.push_back({&Inst, OperandNo, NewOperand, InstIsTerminal});
    if (!InstIsTerminal)
      ChangedOperands.insert(&Inst);
  }
};

template <typename IsTerminalFunc>
RebuildInfoBuilder<IsTerminalFunc>
MakeRebuildInfoBuilder(IsTerminalFunc IsTerminator) {
  return RebuildInfoBuilder{IsTerminator};
}

// Rebuilds instructions according to info provided in RebuildInfo.
// New instructions inherit all properties of original ones, only
// operands change. User can customize this behaviour with two functors:
//    IsSpecialInst: (const InstToRebuild&) -> bool - returns whether inst
//      should be processed with a custom handler
//    CreateSpecialInst: (const InstToRebuild&) -> Value* - custom handler
//      to rebuild provided instruction.
template <typename IsSpecialInstFunc, typename CreateSpecialInstFunc>
class InstructionRebuilder {
  // Pop should be called only in getNextInstToRebuild.
  std::vector<UseToRebuild> ToRebuild;
  IsSpecialInstFunc IsSpecialInst;
  CreateSpecialInstFunc CreateSpecialInst;
  // Map between original inst and its replacement.
  std::unordered_map<llvm::Instruction *, llvm::Value *> Replacement;
  std::vector<llvm::Instruction *> ToErase;

public:
  InstructionRebuilder(RebuildInfo ToRebuildIn,
                       IsSpecialInstFunc IsSpecialInstIn,
                       CreateSpecialInstFunc CreateSpecialInstIn)
      : ToRebuild{ToRebuildIn}, IsSpecialInst{IsSpecialInstIn},
        CreateSpecialInst{CreateSpecialInstIn} {}

  void rebuild() && {
    std::vector<llvm::Instruction *> Terminals;
    for (auto First = ToRebuild.begin(), Last = ToRebuild.end();
         First != Last;) {
      InstToRebuild InstInfo;
      std::tie(InstInfo, First) = getNextInstToRebuild(First, Last);
      IGC_ASSERT_MESSAGE(!llvm::isa<llvm::PHINode>(InstInfo.User),
                         "phi-nodes aren't yet supported");
      IGC_ASSERT_MESSAGE(InstInfo.validate(),
                         "an illegal rebuild info is generated");
      rebuildNonPhiInst(InstInfo);
      if (InstInfo.IsTerminal)
        Terminals.push_back(InstInfo.User);
    }
    for (auto *Terminal : Terminals)
      Terminal->replaceAllUsesWith(Replacement[Terminal]);
    // Instructions must be deleted in post-order - uses first, than defs.
    // As ToErase is in RPO, reverse is required.
    for (auto *Inst : reverse(ToErase))
      Inst->eraseFromParent();
  }

private:
  // Takes a range of UseToRebuild - [\p First, \p Last).
  // Aggregates first uses with the same user from the range and adds collected
  // Replacement info to produce info for the next inst to rebuild. Returns
  // collected inst info and the first use with a different to returned user
  // (next user) or \p Last when there's no more users.
  template <typename InputIter>
  std::pair<InstToRebuild, InputIter> getNextInstToRebuild(InputIter First,
                                                           InputIter Last) {
    IGC_ASSERT_MESSAGE(First != Last,
                       "this method shouldn't be called when list of uses to "
                       "rebuild is already empty");
    InstToRebuild CurInst;
    CurInst.User = First->User;
    CurInst.IsTerminal = First->IsTerminal;
    auto LastUse = std::adjacent_find(
        First, Last, [](const UseToRebuild &LHS, const UseToRebuild &RHS) {
          return LHS.User != RHS.User;
        });
    if (LastUse != Last)
      ++LastUse;
    // Filling operand related fields.
    CurInst =
        std::accumulate(First, LastUse, std::move(CurInst),
                        [this](InstToRebuild Inst, const UseToRebuild &Use) {
                          return appendOperand(Inst, Use);
                        });
    return {CurInst, LastUse};
  }

  // Appends operand/use from \p CurUse to \p InstInfo.
  // Returns updated \p InstInfo.
  InstToRebuild appendOperand(InstToRebuild InstInfo,
                              const UseToRebuild &CurUse) {
    IGC_ASSERT_MESSAGE(InstInfo.User == CurUse.User,
                       "trying to append a wrong use with wrong user");
    IGC_ASSERT_MESSAGE(
        InstInfo.IsTerminal == CurUse.IsTerminal,
        "two uses don't agree on the instruction being terminal");
    InstInfo.OperandNos.push_back(CurUse.OperandNo);
    auto *NewOperand = CurUse.NewOperand;
    if (!NewOperand) {
      NewOperand = Replacement.at(llvm::cast<llvm::Instruction>(
          CurUse.User->getOperand(CurUse.OperandNo)));
    }
    InstInfo.NewOperands.push_back(NewOperand);
    return std::move(InstInfo);
  }

  void rebuildNonPhiInst(InstToRebuild &OrigInst) {
    auto *Replace = createNonPhiInst(OrigInst);
    Replacement[OrigInst.User] = Replace;
    ToErase.push_back(OrigInst.User);
  }

  // Unlike rebuildNonPhiInst method just creates instruction, doesn't
  // update the class state.
  llvm::Value *createNonPhiInst(InstToRebuild &OrigInst) const {
    if (IsSpecialInst(OrigInst))
      return CreateSpecialInst(OrigInst);
    llvm::Instruction *Replace =
        vc::cloneInstWithNewOps(*OrigInst.User, createNewOperands(OrigInst));
    if (!Replace)
      return coverNonCloneCase(*OrigInst.User, createNewOperands(OrigInst));
    Replace->takeName(OrigInst.User);
    Replace->insertBefore(OrigInst.User);
    Replace->setDebugLoc(OrigInst.User->getDebugLoc());
    return Replace;
  }

  static std::vector<llvm::Value *>
  getOrigOperands(llvm::Instruction &OrigInst) {
    if (llvm::isa<llvm::IntrinsicInst>(OrigInst)) {
      auto &OrigIntr = llvm::cast<llvm::IntrinsicInst>(OrigInst);
      return {OrigIntr.arg_begin(), OrigIntr.arg_end()};
    }
    return {OrigInst.value_op_begin(), OrigInst.value_op_end()};
  }

  // Takes arguments of the original instruction (OrigInst.User) and rewrites
  // the required ones with new values according to info in \p OrigInst
  static std::vector<llvm::Value *>
  createNewOperands(const InstToRebuild &OrigInst) {
    auto NewOperands = getOrigOperands(*OrigInst.User);
    for (auto &&OpReplacement :
         zip(OrigInst.OperandNos, OrigInst.NewOperands)) {
      int OperandNo = std::get<0>(OpReplacement);
      llvm::Value *NewOperand = std::get<1>(OpReplacement);
      IGC_ASSERT_MESSAGE(OperandNo >= 0, "no such operand");
      IGC_ASSERT_MESSAGE(OperandNo < static_cast<int>(NewOperands.size()),
                         "no such operand");
      NewOperands[OperandNo] = NewOperand;
    }
    return std::move(NewOperands);
  }

  // covers cases when \p OrigInst cannot be cloned by cloneInstWithNewOps
  // with the provided \p NewOps.
  // Peplacement for the \p OrigInst is returned.
  static llvm::Value *coverNonCloneCase(llvm::Instruction &OrigInst,
                                        llvm::ArrayRef<llvm::Value *> NewOps) {
    IGC_ASSERT_MESSAGE(llvm::isa<llvm::AddrSpaceCastInst>(OrigInst),
                       "only addr space cast case is yet considered");
    IGC_ASSERT_MESSAGE(NewOps.size() == 1, "cast has only one operand");
    llvm::Value *NewOp = NewOps.front();
    auto *NewOpTy = llvm::cast<llvm::PointerType>(NewOp->getType());
    auto *CastTy = llvm::cast<llvm::PointerType>(OrigInst.getType());
    IGC_ASSERT_MESSAGE(
        NewOpTy->getAddressSpace() == CastTy->getAddressSpace(),
        "when addrspaces different clonnig helps and it should've "
        "been covered before");
    return NewOp;
  }
};

template <typename IsSpecialInstFunc, typename CreateSpecialInstFunc>
InstructionRebuilder<IsSpecialInstFunc, CreateSpecialInstFunc>
MakeInstructionRebuilder(RebuildInfo Info, IsSpecialInstFunc IsSpecialInst,
                         CreateSpecialInstFunc CreateSpecialInst) {
  return {std::move(Info), std::move(IsSpecialInst),
          std::move(CreateSpecialInst)};
}

inline auto MakeInstructionRebuilder(RebuildInfo Info) {
  return MakeInstructionRebuilder(
      std::move(Info), [](const InstToRebuild &Inst) { return false; },
      [](const InstToRebuild &Inst) { return nullptr; });
}

} // namespace vc

#endif // VC_UTILS_GENERAL_INST_REBUILDER_H