File: PrivateMemoryUsageAnalysis.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 (209 lines) | stat: -rw-r--r-- 6,897 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/Optimizer/OpenCLPasses/PrivateMemory/PrivateMemoryUsageAnalysis.hpp"
#include "AdaptorCommon/ImplicitArgs.hpp"
#include "AdaptorCommon/AddImplicitArgs.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;

// Register pass to igc-opt
#define PASS_FLAG "igc-private-mem-usage-analysis"
#define PASS_DESCRIPTION "Analyzes the presence of private memory allocation"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(PrivateMemoryUsageAnalysis, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_END(PrivateMemoryUsageAnalysis, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char PrivateMemoryUsageAnalysis::ID = 0;

PrivateMemoryUsageAnalysis::PrivateMemoryUsageAnalysis() : ModulePass(ID), m_hasPrivateMem(false) {
  initializePrivateMemoryUsageAnalysisPass(*PassRegistry::getPassRegistry());
}

bool PrivateMemoryUsageAnalysis::runOnModule(Module &M) {
  bool changed = false;
  m_pMDUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
  CodeGenContext *pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();

  bool hasStackCall = false;

  m_hasDPDivSqrtEmu = !pCtx->platform.hasNoFP64Inst() && !pCtx->platform.hasCorrectlyRoundedMacros() &&
                      pCtx->m_DriverInfo.NeedFP64DivSqrt();

  // Run on all functions defined in this module
  for (Function &F : M) {
    // Skip functions called from function marked with stackcall attribute
    if (AddImplicitArgs::hasStackCallInCG(&F)) {
      hasStackCall = true;
      continue;
    }
    if (F.isDeclaration())
      continue;
    if (m_pMDUtils->findFunctionsInfoItem(&F) == m_pMDUtils->end_FunctionsInfo())
      continue;

    if (runOnFunction(F)) {
      changed = true;
    }
  }

  // If there are stack called functions in the module, add PRIVATE_BASE to all kernels to be safe.
  // PRIVATE_BASE is needed for kernel to get the stack base offset.
  // Callee does not require this arg, since all stack access will be done using the stack-pointer
  if (hasStackCall || pCtx->m_enableFunctionPointer || IGC_IS_FLAG_ENABLED(ForceAddingStackcallKernelPrerequisites) ||
      IGC_IS_FLAG_ENABLED(StackOverflowDetection)) {
    for (Function &F : M) {
      if (isEntryFunc(m_pMDUtils, &F)) {
        SmallVector<ImplicitArg::ArgType, 1> implicitArgs;
        implicitArgs.push_back(ImplicitArg::PRIVATE_BASE);
        ImplicitArgs::addImplicitArgs(F, implicitArgs, m_pMDUtils);
        changed = true;
      }
    }
  }

  // Update LLVM metadata based on IGC MetaDataUtils
  if (changed)
    m_pMDUtils->save(M.getContext());

  return changed;
}

bool PrivateMemoryUsageAnalysis::runOnFunction(Function &F) {
  // Processing new function
  m_hasPrivateMem = false;

  visit(F);

  // Struct types always use private memory unless regtomem can
  // promote them. Check the function signature to see if any
  // structs are passed as arguments.
  if (!m_hasPrivateMem) {
    for (auto &Arg : F.args()) {
      if (Arg.hasAttribute(llvm::Attribute::ByVal)) {
        Type *ByValTy = Arg.getParamByValType();
        visitType(ByValTy);
        if (m_hasPrivateMem)
          break;
      }

      // Argument uses (which could signal they are of struct type) are
      // inspected in instruction visitors.
    }
  }

  // For double emulation, need to add private base (conservative).
  if (!m_hasPrivateMem) {
    CodeGenContext *pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();

    // This is the condition that double emulation is used.
    pCtx->checkDPEmulationEnabled();

    if (pCtx->m_hasDPEmu || pCtx->m_hasDPConvEmu) {
      m_hasPrivateMem = true;
    }
  }

  // Add private memory implicit arg
  SmallVector<ImplicitArg::ArgType, ImplicitArg::NUM_IMPLICIT_ARGS> implicitArgs;
  implicitArgs.push_back(ImplicitArg::R0);

  if (m_hasPrivateMem) {
    implicitArgs.push_back(ImplicitArg::PRIVATE_BASE);
  }
  ImplicitArgs::addImplicitArgs(F, implicitArgs, getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils());

  return true;
}

inline void PrivateMemoryUsageAnalysis::visitType(Type *Ty) {
  // Struct types always use private memory unless regtomem can promote them.
  if (auto *ST = dyn_cast<StructType>(Ty)) {
    if (!ST->isOpaque()) {
      m_hasPrivateMem = true;
    }
  }
}

void PrivateMemoryUsageAnalysis::visitAllocaInst(llvm::AllocaInst &AI) {
  IGC_ASSERT_MESSAGE(AI.getType()->getAddressSpace() == ADDRESS_SPACE_PRIVATE,
                     "Allocations are expected to be in private address space");

  // If we encountered Alloca, then the function uses private memory
  m_hasPrivateMem = true;
}

void PrivateMemoryUsageAnalysis::visitBinaryOperator(llvm::BinaryOperator &I) {
  // If we encountered Alloca, then the function uses private memory
  CodeGenContext *pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
  if (pCtx->platform.Enable32BitIntDivRemEmu()) {
    switch (I.getOpcode()) {
    case Instruction::UDiv:
    case Instruction::URem:
    case Instruction::SDiv:
    case Instruction::SRem:
      m_hasPrivateMem = true;
      break;
    default:
      break;
    }
  }

  // Check if an instruction is fp64 div to enable privMem
  if (m_hasDPDivSqrtEmu) {
    if (I.getOpcode() == Instruction::FDiv) {
      m_hasPrivateMem = true;
    }
  }
}

void PrivateMemoryUsageAnalysis::visitCallInst(llvm::CallInst &CI) {
  // Check if a sqrtd builtin is called to enable privMem
  if (m_hasDPDivSqrtEmu && CI.hasName()) {
    Function *calledFunc = CI.getCalledFunction();
    if (calledFunc && calledFunc->getName().startswith("__builtin_IB_native_sqrtd")) {
      m_hasPrivateMem = true;
    }
  }

  // Check byval arguments for struct usage
  Function *CalledF = CI.getCalledFunction();
  if (CalledF) {
    unsigned ArgNo = 0;
    for (auto It = CI.arg_begin(), End = CI.arg_end(); It != End; ++It) {
      if (CI.paramHasAttr(ArgNo, llvm::Attribute::ByVal)) {
        Type *ByValTy = CI.getParamByValType(ArgNo);
        visitType(ByValTy);
        if (m_hasPrivateMem)
          return;
      }
      ArgNo++;
    }
  }
}

void PrivateMemoryUsageAnalysis::visitLoadInst(LoadInst &LI) {
  Type *LoadTy = LI.getType();
  visitType(LoadTy);
}

void PrivateMemoryUsageAnalysis::visitStoreInst(StoreInst &SI) {
  Type *ValTy = SI.getValueOperand()->getType();
  visitType(ValTy);
}

void PrivateMemoryUsageAnalysis::visitGetElementPtrInst(GetElementPtrInst &GEP) {
  Type *SrcTy = GEP.getSourceElementType();
  visitType(SrcTy);
}