File: LowerGPCallArg.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 (337 lines) | stat: -rw-r--r-- 12,329 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2024 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "LowerGPCallArg.h"

#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "LLVM3DBuilder/MetadataBuilder.h"

#include "common/LLVMWarningsPush.hpp"
#include "llvm/ADT/PostOrderIterator.h"
#include <llvm/IR/DIBuilder.h>
#include "llvmWrapper/IR/Function.h"
#include "common/LLVMWarningsPop.hpp"
#include "llvmWrapper/IR/DerivedTypes.h"
#include <optional>

// (1)
// Optimization pass to lower generic pointers in function arguments.
// If all call sites have the same origin address space, address space
// casts with the form of non-generic->generic can safely removed and
// function updated with non-generic pointer argument.
//
// The complete process to lower generic pointer args consists of 5 steps:
//   1) find all functions that are candidates
//   2) update functions and their signatures
//   3) update all call sites
//   4) update functions metadata
//   5) validate that all function calls are properly formed
//
//
// Current limitations/considerations:
// - only arguments of non-extern functions can be lowered
// - no recursive functions support
//
// (2)
//   Once (1) is done. Do further check if there is a cast from local to GAS or
//   a cast from private to GAS. If there is no such cast, GAS inst (such as
//   ld/st, etc, can be converted safely to ld/st on globals.

ModulePass *IGC::createLowerGPCallArg() { return new LowerGPCallArg(); }

char LowerGPCallArg::ID = 0;

#define PASS_FLAG "igc-lower-gp-arg"
#define PASS_DESC "Lower generic pointers in call arguments"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(LowerGPCallArg, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
IGC_INITIALIZE_PASS_END(LowerGPCallArg, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)

bool LowerGPCallArg::runOnModule(llvm::Module &M) {
  m_ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
  m_mdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
  m_module = &M;

  CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
  std::vector<Function *> candidates = findCandidates(CG);
  bool changed = false;
  for (auto F : reverse(candidates)) {
    GenericPointerArgs genericArgsInfo;
    for (auto &arg : F->args()) {
      if (arg.use_empty())
        continue;

      Type *argTy = arg.getType();
      if (argTy->isPointerTy() && argTy->getPointerAddressSpace() == ADDRESS_SPACE_GENERIC) {
        if (auto originAddrSpace = getOriginAddressSpace(F, arg.getArgNo()))
          genericArgsInfo.push_back({arg.getArgNo(), originAddrSpace.value()});
      }
    }

    if (genericArgsInfo.empty())
      continue;

    Function *newFunc = createFuncWithLoweredArgs(F, genericArgsInfo);
    updateFunctionArgs(F, newFunc);
    updateAllUsesWithNewFunction(F, newFunc);
    updateMetadata(F, newFunc);

    F->eraseFromParent();
    changed = true;
  }

  return changed;
}

std::vector<Function *> LowerGPCallArg::findCandidates(CallGraph &CG) {
  auto skip = [](Function *F) {
    // Skip functions with variable number of arguments, e.g. printf.
    if (F->isVarArg())
      return true;

    // Only non-extern functions within the module are optimized
    if (F->hasFnAttribute("referenced-indirectly") || F->isDeclaration() || F->isIntrinsic() || F->user_empty())
      return true;

    return false;
  };

  std::vector<Function *> candidates;
  for (auto I : post_order(&CG)) {
    auto F = I->getFunction();
    if (!F)
      continue;
    if (skip(F))
      continue;

    auto hasGenericArg = [](Argument &arg) {
      Type *argTy = arg.getType();
      return argTy->isPointerTy() && argTy->getPointerAddressSpace() == ADDRESS_SPACE_GENERIC;
    };

    if (std::any_of(F->arg_begin(), F->arg_end(), hasGenericArg))
      candidates.push_back(F);
  }

  return candidates;
}

void LowerGPCallArg::updateMetadata(Function *oldFunc, Function *newFunc) {
  MetadataBuilder mbuilder(m_module);
  auto &FuncMD = m_ctx->getModuleMetaData()->FuncMD;

  auto oldFuncIter = m_mdUtils->findFunctionsInfoItem(oldFunc);
  m_mdUtils->setFunctionsInfoItem(newFunc, oldFuncIter->second);
  m_mdUtils->eraseFunctionsInfoItem(oldFuncIter);
  mbuilder.UpdateShadingRate(oldFunc, newFunc);
  auto loc = FuncMD.find(oldFunc);
  if (loc != FuncMD.end()) {
    auto funcInfo = loc->second;
    FuncMD.erase(oldFunc);
    FuncMD[newFunc] = std::move(funcInfo);
  }

  m_mdUtils->save(m_module->getContext());
}

Function *LowerGPCallArg::createFuncWithLoweredArgs(Function *F, GenericPointerArgs &argsInfo) {
  FunctionType *pFuncType = F->getFunctionType();
  std::vector<Type *> newParamTypes(pFuncType->param_begin(), pFuncType->param_end());
  for (auto &argInfo : argsInfo) {
    PointerType *ptrType =
        IGCLLVM::getWithSamePointeeType(dyn_cast<PointerType>(newParamTypes[argInfo.argNo]), argInfo.addrSpace);
    newParamTypes[argInfo.argNo] = ptrType;
  }

  FunctionType *newFTy = FunctionType::get(F->getReturnType(), newParamTypes, F->isVarArg());
  Function *newFunc = Function::Create(newFTy, F->getLinkage());
  newFunc->copyAttributesFrom(F);
  newFunc->setSubprogram(F->getSubprogram());
  m_module->getFunctionList().insert(F->getIterator(), newFunc);
  newFunc->takeName(F);
  IGCLLVM::splice(newFunc, newFunc->begin(), F);

  return newFunc;
}

std::optional<unsigned> LowerGPCallArg::getOriginAddressSpace(Function *func, unsigned argNo) {
  std::optional<unsigned> originAddressSpace;

  // Check if all the callers have the same pointer address space
  for (auto U : func->users()) {
    auto CI = cast<CallInst>(U);
    Value *V = CI->getArgOperand(argNo);

    if (!V->getType()->isPointerTy())
      continue;

    if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) {
      IGC_ASSERT(ASC->getDestAddressSpace() == ADDRESS_SPACE_GENERIC);

      unsigned srcAddrSpace = ASC->getSrcAddressSpace();
      if (originAddressSpace && originAddressSpace.value() != srcAddrSpace)
        return std::nullopt;

      originAddressSpace = srcAddrSpace;
    } else {
      return std::nullopt;
    }
  }

  return originAddressSpace;
}

// Loops over the argument list transferring uses from old function to new one.
void LowerGPCallArg::updateFunctionArgs(Function *oldFunc, Function *newFunc) {
  for (const auto &ArgPair : llvm::zip(oldFunc->args(), newFunc->args())) {
    Value *oldArg = &std::get<0>(ArgPair);
    Value *newArg = &std::get<1>(ArgPair);

    newArg->takeName(oldArg);

    if (oldArg->getType() == newArg->getType()) {
      oldArg->replaceAllUsesWith(newArg);
      continue;
    }

    auto *NewArgToGeneric = CastInst::Create(Instruction::AddrSpaceCast, newArg, oldArg->getType(), "",
                                             newFunc->getEntryBlock().getFirstNonPHI());
    oldArg->replaceAllUsesWith(NewArgToGeneric);

    LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(*newFunc).getLoopInfo();
    GASPropagator Propagator(newFunc->getContext(), &LI);
    Propagator.propagate(newArg);
  }
}

// This function takes an Old value and a New value. If Old value is referenced in a
// dbg.value or dbg.declare instruction, it replaces that intrinsic and makes new one
// use the New value.
//
// This function is required anytime a pass modifies IR such that RAUW cannot be
// used to directly update uses in metadata node. In case of GAS, RAUW asserts because
// addrspace used in Old/New values are different and this is interpreted as different
// types by LLVM and RAUW on different types is forbidden.
void replaceValueInDbgInfoIntrinsic(llvm::Value *Old, llvm::Value *New, llvm::Module &M) {
  if (Old->isUsedByMetadata()) {
    auto localAsMD = ValueAsMetadata::getIfExists(Old);
    auto addrSpaceMD = MetadataAsValue::getIfExists(Old->getContext(), localAsMD);
    if (addrSpaceMD) {
      llvm::DIBuilder DIB(M);
      std::vector<llvm::DbgInfoIntrinsic *> DbgInfoInstToDelete;
      for (auto *User : addrSpaceMD->users()) {
        if (cast<DbgInfoIntrinsic>(User)) {
          // User->dump();
          if (auto DbgV = cast<DbgValueInst>(User)) {
            DIB.insertDbgValueIntrinsic(New, DbgV->getVariable(), DbgV->getExpression(), DbgV->getDebugLoc().get(),
                                        cast<llvm::Instruction>(User));
          } else if (auto DbgD = cast<DbgDeclareInst>(User)) {
            DIB.insertDeclare(New, DbgD->getVariable(), DbgD->getExpression(), DbgD->getDebugLoc().get(),
                              cast<llvm::Instruction>(User));
          }
          DbgInfoInstToDelete.push_back(cast<llvm::DbgInfoIntrinsic>(User));
        }
      }

      for (auto DbgInfoInst : DbgInfoInstToDelete)
        DbgInfoInst->eraseFromParent();
    }
  }
}

void LowerGPCallArg::updateAllUsesWithNewFunction(Function *oldFunc, Function *newFunc) {
  IGC_ASSERT(!oldFunc->use_empty());

  // Keep track of old calls and addrspacecast to be deleted later
  std::vector<CallInst *> callsToDelete;
  std::vector<AddrSpaceCastInst *> ASCToDelete;
  std::vector<Use *> UsesToReplace;

  for (auto U = oldFunc->user_begin(), E = oldFunc->user_end(); U != E; ++U) {
    CallInst *cInst = dyn_cast<CallInst>(*U);
    auto BC = dyn_cast<BitCastInst>(*U);
    if (BC && BC->hasOneUse())
      cInst = dyn_cast<CallInst>(BC->user_back());

    if (!cInst) {
      IGC_ASSERT_MESSAGE(0, "Unknown function usage");
      return;
    }

    if (cInst->getCalledFunction() != oldFunc) {
      for (Use &cArg : cInst->args())
        if (cArg == oldFunc)
          UsesToReplace.push_back(&cArg);
      continue;
    }

    // Prepare args for new call
    std::vector<Value *> newCallArgs;

    auto AI = newFunc->arg_begin();
    for (unsigned int i = 0; i < IGCLLVM::getNumArgOperands(cInst); ++i, ++AI) {
      Value *callArg = cInst->getOperand(i);
      Value *funcArg = AI;
      if (callArg->getType() != funcArg->getType()) {
        IGC_ASSERT(callArg->getType()->isPointerTy() && funcArg->getType()->isPointerTy());

        PointerType *callArgTy = dyn_cast<PointerType>(callArg->getType());
        PointerType *funcArgTy = dyn_cast<PointerType>(funcArg->getType());
        IGC_ASSERT(callArgTy->getAddressSpace() == ADDRESS_SPACE_GENERIC &&
                   funcArgTy->getAddressSpace() != ADDRESS_SPACE_GENERIC);
        // If call site address space is generic and function arg is non-generic,
        // the addrspacecast is removed and non-generic address space lowered
        // to the function call.
        AddrSpaceCastInst *addrSpaceCastInst = dyn_cast<AddrSpaceCastInst>(callArg);
        if (addrSpaceCastInst) {
          callArg = addrSpaceCastInst->getOperand(0);
          if (addrSpaceCastInst->hasOneUse()) {
            // when addrspacecast is used in a metadata node, replacing it
            // requires reconstruction of the node. we cannot used standard
            // llvm APIs to replace uses as they require that type be
            // preserved, which is not in this case.
            replaceValueInDbgInfoIntrinsic(addrSpaceCastInst, addrSpaceCastInst->getPointerOperand(),
                                           *newFunc->getParent());
            ASCToDelete.push_back(addrSpaceCastInst);
          }
        }
      }
      newCallArgs.push_back(callArg);
    }

    // Create new call and insert it before old one
    CallInst *inst =
        CallInst::Create(newFunc, newCallArgs, newFunc->getReturnType()->isVoidTy() ? "" : newFunc->getName(), cInst);

    inst->setCallingConv(newFunc->getCallingConv());
    inst->setDebugLoc(cInst->getDebugLoc());
    cInst->replaceAllUsesWith(inst);
    callsToDelete.push_back(cInst);
  }

  // Delete old calls
  for (auto i : callsToDelete) {
    i->eraseFromParent();
  }

  // Delete addrspacecasts that are no longer needed
  for (auto i : ASCToDelete) {
    IGC_ASSERT(i->user_empty());
    i->eraseFromParent();
  }

  // Replace call arguments
  for (auto i : UsesToReplace) {
    i->set(newFunc);
  }
}