File: WIFuncsAnalysis.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 (216 lines) | stat: -rw-r--r-- 8,208 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/Optimizer/OpenCLPasses/WIFuncs/WIFuncsAnalysis.hpp"
#include "Compiler/CISACodeGen/OpenCLKernelCodeGen.hpp"
#include "AdaptorCommon/ImplicitArgs.hpp"
#include "AdaptorCommon/AddImplicitArgs.hpp"
#include "Compiler/IGCPassSupport.h"

#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include "common/LLVMWarningsPop.hpp"

using namespace llvm;
using namespace IGC;

// Register pass to igc-opt
#define PASS_FLAG "igc-wi-func-analysis"
#define PASS_DESCRIPTION "Analyzes work item functions"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(WIFuncsAnalysis, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_END(WIFuncsAnalysis, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char WIFuncsAnalysis::ID = 0;

const llvm::StringRef WIFuncsAnalysis::GET_LOCAL_ID_X = "__builtin_IB_get_local_id_x";
const llvm::StringRef WIFuncsAnalysis::GET_LOCAL_ID_Y = "__builtin_IB_get_local_id_y";
const llvm::StringRef WIFuncsAnalysis::GET_LOCAL_ID_Z = "__builtin_IB_get_local_id_z";
const llvm::StringRef WIFuncsAnalysis::GET_GROUP_ID = "__builtin_IB_get_group_id";
const llvm::StringRef WIFuncsAnalysis::GET_LOCAL_THREAD_ID = "__builtin_IB_get_local_thread_id";
const llvm::StringRef WIFuncsAnalysis::GET_GLOBAL_SIZE = "__builtin_IB_get_global_size";
const llvm::StringRef WIFuncsAnalysis::GET_LOCAL_SIZE = "__builtin_IB_get_local_size";
const llvm::StringRef WIFuncsAnalysis::GET_GLOBAL_OFFSET = "__builtin_IB_get_global_offset";
const llvm::StringRef WIFuncsAnalysis::GET_WORK_DIM = "__builtin_IB_get_work_dim";
const llvm::StringRef WIFuncsAnalysis::GET_NUM_GROUPS = "__builtin_IB_get_num_groups";
const llvm::StringRef WIFuncsAnalysis::GET_ENQUEUED_LOCAL_SIZE = "__builtin_IB_get_enqueued_local_size";
const llvm::StringRef WIFuncsAnalysis::GET_STAGE_IN_GRID_ORIGIN = "__builtin_IB_get_stage_in_grid_origin";
const llvm::StringRef WIFuncsAnalysis::GET_STAGE_IN_GRID_SIZE = "__builtin_IB_get_stage_in_grid_size";
const llvm::StringRef WIFuncsAnalysis::GET_SYNC_BUFFER = "__builtin_IB_get_sync_buffer";
const llvm::StringRef WIFuncsAnalysis::GET_ASSERT_BUFFER = "__builtin_IB_get_assert_buffer";

WIFuncsAnalysis::WIFuncsAnalysis() : ModulePass(ID) { initializeWIFuncsAnalysisPass(*PassRegistry::getPassRegistry()); }

bool WIFuncsAnalysis::runOnModule(Module &M) {
  m_pMDUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
  m_ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
  // Run on all functions defined in this module
  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
    Function *pFunc = &(*I);
    if (pFunc->isDeclaration())
      continue;
    runOnFunction(*pFunc);
  }

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

  return true;
}

bool WIFuncsAnalysis::runOnFunction(Function &F) {
  // Processing new function
  m_hasGroupID = false;
  m_hasLocalThreadID = false;
  m_hasGlobalOffset = false;
  m_hasLocalID = false;
  m_hasGlobalSize = false;
  m_hasLocalSize = false;
  m_hasWorkDim = false;
  m_hasNumGroups = false;
  m_hasEnqueuedLocalSize = false;
  m_hasStageInGridOrigin = false;
  m_hasStageInGridSize = false;
  m_hasSyncBuffer = false;
  m_hasAssertBuffer = false;
  m_hasStackCalls = false;

  // Visit the function
  visit(F);

  // Analyze the implicit arguments needed by this function
  SmallVector<ImplicitArg::ArgType, ImplicitArg::NUM_IMPLICIT_ARGS> implicitArgs;

  const bool RequirePayloadHeader = m_ctx->m_DriverInfo.RequirePayloadHeader();
  // 8xi32 payload header packs 3xi32 global offset. If possible, use global offset directly.
  const auto PayloadHeaderType =
      AllowShortImplicitPayloadHeader(m_ctx) ? ImplicitArg::GLOBAL_OFFSET : ImplicitArg::PAYLOAD_HEADER;

  // All OpenCL kernels receive R0 and Payload Header implicitly
  if (isEntryFunc(m_pMDUtils, &F)) {
    implicitArgs.push_back(ImplicitArg::R0);

    if (RequirePayloadHeader)
      implicitArgs.push_back(PayloadHeaderType);

    if (!m_ctx->platform.isProductChildOf(IGFX_XE_HP_SDV) && IGC_IS_FLAG_ENABLED(EnableGlobalStateBuffer)) {
      if (m_hasStackCalls) {
        implicitArgs.push_back(ImplicitArg::ArgType::IMPLICIT_ARG_BUFFER_PTR);

        // force add local id implicit args to kernel as we need
        // those to populate local id buffer that stack call functions
        // may use.
        implicitArgs.push_back(ImplicitArg::ArgType::LOCAL_ID_X);
        implicitArgs.push_back(ImplicitArg::ArgType::LOCAL_ID_Y);
        implicitArgs.push_back(ImplicitArg::ArgType::LOCAL_ID_Z);
      }
    }

    if (m_ctx->type == ShaderType::OPENCL_SHADER) {
      auto *Ctx = static_cast<OpenCLProgramContext *>(m_ctx);
      if (Ctx->needsDivergentBarrierHandling())
        m_hasLocalSize = true;
    }
  } else {
    if (m_hasGroupID) {
      implicitArgs.push_back(ImplicitArg::R0);
    } else if (m_hasLocalThreadID) {
      implicitArgs.push_back(ImplicitArg::R0);
    }
    if (m_hasGlobalOffset && RequirePayloadHeader) {
      implicitArgs.push_back(PayloadHeaderType);
    }
  }
  if (m_hasGlobalOffset && !RequirePayloadHeader) {
    implicitArgs.push_back(PayloadHeaderType);
  }
  if (m_hasWorkDim) {
    implicitArgs.push_back(ImplicitArg::WORK_DIM);
  }
  if (m_hasNumGroups) {
    implicitArgs.push_back(ImplicitArg::NUM_GROUPS);
  }
  if (m_hasGlobalSize) {
    implicitArgs.push_back(ImplicitArg::GLOBAL_SIZE);
  }
  if (m_hasLocalSize) {
    implicitArgs.push_back(ImplicitArg::LOCAL_SIZE);
  }
  if (m_hasLocalID) {
    implicitArgs.push_back(ImplicitArg::LOCAL_ID_X);
    implicitArgs.push_back(ImplicitArg::LOCAL_ID_Y);
    implicitArgs.push_back(ImplicitArg::LOCAL_ID_Z);
  }
  if (m_hasEnqueuedLocalSize) {
    implicitArgs.push_back(ImplicitArg::ENQUEUED_LOCAL_WORK_SIZE);
  }
  if (m_hasStageInGridOrigin) {
    implicitArgs.push_back(ImplicitArg::STAGE_IN_GRID_ORIGIN);
  }
  if (m_hasStageInGridSize) {
    implicitArgs.push_back(ImplicitArg::STAGE_IN_GRID_SIZE);
  }
  if (m_hasSyncBuffer) {
    implicitArgs.push_back(ImplicitArg::SYNC_BUFFER);
  }
  if (m_hasAssertBuffer) {
    implicitArgs.push_back(ImplicitArg::ASSERT_BUFFER_POINTER);
  }

  // Create the metadata representing the implicit args needed by this function
  ImplicitArgs::addImplicitArgs(F, implicitArgs, m_pMDUtils);

  return true;
}

void WIFuncsAnalysis::visitCallInst(CallInst &CI) {
  Function *F = CI.getCalledFunction();

  if (CI.isIndirectCall() || (F && F->hasFnAttribute("visaStackCall"))) {
    m_hasStackCalls = true;
    return;
  }
  if (F == nullptr) {
    return;
  }

  // Check for OpenCL WI function calls
  StringRef funcName = F->getName();
  if (funcName.equals(GET_LOCAL_ID_X) || funcName.equals(GET_LOCAL_ID_Y) || funcName.equals(GET_LOCAL_ID_Z)) {
    m_hasLocalID = true;
  } else if (funcName.equals(GET_GROUP_ID)) {
    m_hasGroupID = true;
  } else if (funcName.equals(GET_LOCAL_THREAD_ID)) {
    m_hasLocalThreadID = true;
  } else if (funcName.equals(WIFuncsAnalysis::GET_GLOBAL_OFFSET)) {
    m_hasGlobalOffset = true;
  } else if (funcName.equals(GET_GLOBAL_SIZE)) {
    m_hasGlobalSize = true;
  } else if (funcName.equals(GET_LOCAL_SIZE)) {
    m_hasLocalSize = true;
  } else if (funcName.equals(GET_WORK_DIM)) {
    m_hasWorkDim = true;
  } else if (funcName.equals(GET_NUM_GROUPS)) {
    m_hasNumGroups = true;
  } else if (funcName.equals(GET_ENQUEUED_LOCAL_SIZE)) {
    m_hasEnqueuedLocalSize = true;
  } else if (funcName.equals(GET_STAGE_IN_GRID_ORIGIN)) {
    m_hasStageInGridOrigin = true;
  } else if (funcName.equals(GET_STAGE_IN_GRID_SIZE)) {
    m_hasStageInGridSize = true;
  } else if (funcName.equals(GET_SYNC_BUFFER)) {
    m_hasSyncBuffer = true;
  } else if (funcName.equals(GET_ASSERT_BUFFER)) {
    m_hasAssertBuffer = true;
  }
}