File: SplitStructurePhisPass.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (275 lines) | stat: -rw-r--r-- 9,422 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2025 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "SplitStructurePhisPass.hpp"

using namespace llvm;
using namespace IGC;

char SplitStructurePhisPass::ID = 0;

#define PASS_FLAG "split-structure-phis"
#define PASS_DESCRIPTION "Split structure phis pass."
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
#define POISON_SIZE_T 999

// The SplitStructurePhisPass is a function pass designed to optimize the handling of PHI nodes that operate on
// structures containing multiple fields, such as vectors and scalars. This pass splits the PHI nodes into separate PHI
// nodes for each individual field in the structure in case one of the incoming values is a zeroinitializer. This helps
// the emitter avoid generating intermediate mov instructions to initialize the structure with zero values.

IGC_INITIALIZE_PASS_BEGIN(SplitStructurePhisPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(SplitStructurePhisPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

SplitStructurePhisPass::SplitStructurePhisPass() : FunctionPass(ID) {
  initializeSplitStructurePhisPassPass(*PassRegistry::getPassRegistry());
}

bool SplitStructurePhisPass::runOnFunction(Function &F) {
  if (skipFunction(F))
    return false;

  for (auto &BB : F) {
    // Iterate over all instructions in the basic block.
    for (auto &I : BB) {
      auto *Phi = dyn_cast<PHINode>(&I);
      IncomingValuesMap InsertValues;
      ExtractValueMap ExtractValues;

      // Skip non-phi instructions.
      if (!Phi)
        continue;

      // Currently, we only support PHI nodes with two incoming values.
      if (Phi->getNumIncomingValues() != 2)
        continue;

      // Skip phi node instruction if its structure type doesn't have vector types.
      if (!isStructOfVectorsType(Phi->getType()))
        continue;

      // Get indices of the incoming values.
      IndicesTuple Indices = getIndices(Phi);

      // Skip if phi node doesn't have zero incoming value.
      if (std::get<Zero>(Indices) == POISON_SIZE_T || std::get<NonZero>(Indices) == POISON_SIZE_T)
        continue;

      // Skip phi nodes that are used by other instructions, other than extractvalue.
      if (!isPhiNodeParsedByExtrVal(Phi, ExtractValues))
        continue;

      Value *NonZeroIncVal = Phi->getIncomingValue(std::get<NonZero>(Indices));

      // Check that the non-zero incoming value was created by insertvalue instructions.
      if (!checkNonZeroIncValue(NonZeroIncVal, InsertValues))
        continue;

      PhiNodes[Phi] = std::make_tuple(Indices, ExtractValues, InsertValues);
    }
  }

  bool Changed = PhiNodes.size() > 0 ? true : false;

  // Iterate over the collected PHI nodes and
  // 1. create new phis for each vector type
  // 2. create new phis for each scalar types
  // 3. save dead instructions for removal
  // 4. update incoming phis incoming values
  for (auto &PhiPair : PhiNodes) {
    PHINode *OldPhi = PhiPair.first;
    auto Indices = std::get<0>(PhiPair.second);
    ExtractValueMap ExtractValues = std::get<1>(PhiPair.second);
    IncomingValuesMap InsertValues = std::get<2>(PhiPair.second);

    StructType *StTy = cast<StructType>(OldPhi->getType());
    for (unsigned i = 0; i < StTy->getNumElements(); ++i) {
      auto *VecTy = dyn_cast<VectorType>(StTy->getElementType(i));

      if (VecTy) {
        createVectorPhi(OldPhi, Indices, ExtractValues[i], InsertValues[i]);
      } else {
        createScalarPhi(OldPhi, StTy->getElementType(i), Indices, ExtractValues[i], InsertValues[i]);
      }
    }

    // Save old phi to remove it later.
    PhiNodeInstsToRemove.insert(OldPhi);
  }

  // Clean up the dead instructions.
  cleanUp();

  return Changed;
}

void SplitStructurePhisPass::cleanUp() {
  for (auto *ExtrValInst : ExtractValueInstsToRemove)
    ExtrValInst->eraseFromParent();

  for (auto *Phi : PhiNodeInstsToRemove)
    Phi->eraseFromParent();

  for (auto *InsValInst : InsertValueInstsToRemove) {
    while (InsValInst) {
      InsertValueInst *InstToRemov = InsValInst;
      InsValInst = dyn_cast<InsertValueInst>(InsValInst->getAggregateOperand());
      InstToRemov->eraseFromParent();
    }
  }

  // Clear the maps and sets after work on function.
  PhiNodes.clear();
  InsertValueInstsToRemove.clear();
  ExtractValueInstsToRemove.clear();
  PhiNodeInstsToRemove.clear();
}

IndicesTuple SplitStructurePhisPass::getIndices(PHINode *Phi) {
  size_t ZeroIncValIndex = POISON_SIZE_T;
  size_t OtherIncValIndex = POISON_SIZE_T;

  if (isa<ConstantAggregateZero>(Phi->getIncomingValue(0))) {
    ZeroIncValIndex = 0;
    OtherIncValIndex = 1;
  } else if (isa<ConstantAggregateZero>(Phi->getIncomingValue(1))) {
    ZeroIncValIndex = 1;
    OtherIncValIndex = 0;
  } else {
    return std::make_tuple(POISON_SIZE_T, POISON_SIZE_T);
  }

  return std::make_tuple(ZeroIncValIndex, OtherIncValIndex);
}

void SplitStructurePhisPass::createScalarPhi(PHINode *OldPhi, Type *NewScalarType, const IndicesTuple &Indices,
                                             ExtractValueInst *OldExtractInst, InsertValueInst *OldInsertValInst) {
  IRBuilder<> Builder(OldPhi);
  auto *NewPhi = cast<PHINode>(Builder.CreatePHI(NewScalarType, 2, "splitted_phi"));

  size_t ZeroIncomingIndex = std::get<Zero>(Indices);
  size_t NonZeroIncomingIndex = std::get<NonZero>(Indices);

  NewPhi->addIncoming(Constant::getNullValue(NewScalarType), OldPhi->getIncomingBlock(ZeroIncomingIndex));
  NewPhi->addIncoming(OldInsertValInst->getInsertedValueOperand(), OldPhi->getIncomingBlock(NonZeroIncomingIndex));

  OldExtractInst->replaceAllUsesWith(NewPhi);
  ExtractValueInstsToRemove.insert(OldExtractInst);
  if (isLastInsertValueInst(OldInsertValInst, OldPhi))
    InsertValueInstsToRemove.insert(OldInsertValInst);
}

void SplitStructurePhisPass::createVectorPhi(PHINode *OldPhi, const IndicesTuple &Indices,
                                             ExtractValueInst *ExtractInst, InsertValueInst *InsertValInst) {
  Value *NewIncomingNonZeroVal = InsertValInst->getInsertedValueOperand();
  Type *NewIncomingTy = NewIncomingNonZeroVal->getType();

  IRBuilder<> Builder(OldPhi);
  PHINode *NewPhi = cast<PHINode>(Builder.CreatePHI(NewIncomingTy, 2, "splitted_phi"));

  size_t ZeroIncomingIndex = std::get<Zero>(Indices);
  size_t NonZeroIncomingIndex = std::get<NonZero>(Indices);

  NewPhi->addIncoming(ConstantAggregateZero::get(NewIncomingTy), OldPhi->getIncomingBlock(ZeroIncomingIndex));
  NewPhi->addIncoming(NewIncomingNonZeroVal, OldPhi->getIncomingBlock(NonZeroIncomingIndex));
  ExtractInst->replaceAllUsesWith(NewPhi);

  ExtractValueInstsToRemove.insert(ExtractInst);

  // Save only the last insert value instruction for safe removal.
  if (isLastInsertValueInst(InsertValInst, OldPhi))
    InsertValueInstsToRemove.insert(InsertValInst);
}

bool SplitStructurePhisPass::isLastInsertValueInst(InsertValueInst *InsertValInst, PHINode *OldPhi) {
  auto U = *InsertValInst->user_begin();
  if (U != OldPhi)
    return false;

  return true;
}

// Check if non-zero increment value was created by insertvalue instructions.
bool SplitStructurePhisPass::checkNonZeroIncValue(Value *IncVal, IncomingValuesMap &InsertValues) {
  StructType *StTy = cast<StructType>(IncVal->getType());

  Value *InsertVal = IncVal;
  for (unsigned i = 0; i < StTy->getNumElements(); ++i) {
    InsertValueInst *InsertInst = dyn_cast<InsertValueInst>(InsertVal);

    if (!InsertInst)
      return false;

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

    if (InsertInst->getNumIndices() != 1)
      return false;

    size_t ValueIndexInStruct = InsertInst->getIndices()[0];
    if (InsertValues.find(ValueIndexInStruct) != InsertValues.end())
      return false;

    InsertValues[ValueIndexInStruct] = InsertInst;
    InsertVal = InsertInst->getAggregateOperand();
  }

  if (!isa<PoisonValue>(InsertVal) && !isa<UndefValue>(InsertVal))
    return false;

  return true;
}

bool SplitStructurePhisPass::isPhiNodeParsedByExtrVal(PHINode *Phi, ExtractValueMap &ExtractValues) {
  for (auto *User : Phi->users()) {
    ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(User);
    if (!ExtractInst)
      return false;

    if (ExtractInst->getNumIndices() != 1)
      return false;

    size_t ValueIndexInStruct = ExtractInst->getIndices()[0];
    if (ExtractValues.find(ValueIndexInStruct) != ExtractValues.end())
      return false;

    ExtractValues[ValueIndexInStruct] = ExtractInst;
  }

  return true;
}

bool SplitStructurePhisPass::isStructOfVectorsType(Type *Ty) {
  bool HasVector = false;
  // Check if the type is a struct
  auto *STy = dyn_cast<StructType>(Ty);

  if (!STy)
    return false;

  // Check if the struct type is an array of structs
  for (unsigned i = 0; i < STy->getNumElements(); ++i) {
    Type *ElemTy = STy->getElementType(i);

    if (ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() || ElemTy->isPointerTy())
      continue;

    auto *VecTy = dyn_cast<VectorType>(ElemTy);
    if (!VecTy)
      return false;

    Type *VecElTy = VecTy->getElementType();
    if (!VecElTy->isIntegerTy() && !VecElTy->isFloatingPointTy() && !VecElTy->isPointerTy())
      return false;

    HasVector = true;
  }

  return HasVector;
}