File: GenXBTIAssignment.cpp

package info (click to toggle)
intel-graphics-compiler2 2.28.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792,744 kB
  • sloc: cpp: 5,761,745; ansic: 466,928; lisp: 312,143; python: 114,790; asm: 44,736; pascal: 10,930; sh: 8,033; perl: 7,914; ml: 3,625; awk: 3,523; yacc: 2,747; javascript: 2,667; lex: 1,898; f90: 1,028; cs: 573; xml: 474; makefile: 344; objc: 162
file content (436 lines) | stat: -rw-r--r-- 15,275 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*========================== begin_copyright_notice ============================

Copyright (C) 2021-2025 Intel Corporation

SPDX-License-Identifier: MIT

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

//===----------------------------------------------------------------------===//
//
/// GenXAssignBTI
/// -----------------
///
/// This pass calculates BT indices for kernel memory object arguments
/// that include buffers and images.
///
/// Calculated BTI are then used instead of corresponging kernel arguments
/// throughout the code. Additionally, all assigned values are saved to
/// kernel metadata to be retrieved later by runtime info pass.
///
//===----------------------------------------------------------------------===//

#include "vc/GenXOpts/GenXOpts.h"
#include "vc/Support/BackendConfig.h"
#include "vc/Support/GenXDiagnostic.h"
#include "vc/Utils/GenX/KernelInfo.h"

#include "llvm/GenXIntrinsics/GenXIntrinsics.h"

#include "Probe/Assertion.h"

#include "llvmWrapper/ADT/StringRef.h"

#include <llvm/ADT/StringRef.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/Module.h>
#include <llvm/Pass.h>
#include <llvm/Support/CommandLine.h>

#include <tuple>
#include <utility>
#include <vector>

using namespace llvm;
static cl::opt<bool> Efficient64bOpt(
    "vc-bti-assignment-efficient64b", cl::init(false), cl::Hidden,
    cl::desc("Should be used only in llvm opt to enable 64-bit addressing"));

static cl::opt<bool> EnforceBTIZeroReservation(
    "vc-reserve-bti-zero", cl::init(false), cl::Hidden,
    cl::desc("do not assign BTI index to zero (for testing purposes)"));

namespace {
class BTIAssignment final {
  Module &M;
  const bool emitDebuggableKernels;
  const bool useBindlessBuffers;
  const bool useBindlessImages;
  const bool useBindlessSamplers;

public:
  BTIAssignment(Module &InM, bool InEmitDebuggableKernels,
                bool InUseBindlessBuffers, bool InUseBindlessImages,
                bool InUseBindlessSamplers)
      : M(InM), emitDebuggableKernels(InEmitDebuggableKernels),
        useBindlessBuffers(InUseBindlessBuffers),
        useBindlessImages(InUseBindlessImages),
        useBindlessSamplers(InUseBindlessSamplers) {}

  bool run();

private:
  // Helper function to assign bti from corresponding category.
  // ZipTy -- tuple of ID, ArgKind and ArgDesc.
  // assignSRV return value -- current state of IDs for surface and
  // sampler assignment.
  template <typename ZipTy>
  std::pair<int, int> assignSRV(int SurfaceID, int SamplerID, ZipTy &&Zippy);
  template <typename ZipTy> int assignUAV(int SurfaceID, ZipTy &&Zippy);

  std::vector<int>
  computeBTIndices(vc::KernelMetadata &KM,
                   const std::vector<StringRef> &ExtendedArgDescs);
  bool rewriteArguments(vc::KernelMetadata &KM, Function &F,
                        const std::vector<int> &BTIndices,
                        const std::vector<StringRef> &ExtendedArgDescs);

  bool processKernel(Function &F);
};

class GenXBTIAssignment final : public ModulePass {
  bool Efficient64b = true;

#if LLVM_VERSION_MAJOR >= 16
  GenXBackendConfigPass::Result &BC;
#endif

public:
  static char ID;

#if LLVM_VERSION_MAJOR >= 16
  GenXBTIAssignment(GenXBackendConfigPass::Result &BC,
                    bool Efficient64b = false)
      : BC(BC), ModulePass(ID), Efficient64b(Efficient64b || Efficient64bOpt) {}
#else  // LLVM_VERSION_MAJOR >= 16
  GenXBTIAssignment(bool Efficient64b = false)
      : ModulePass(ID), Efficient64b(Efficient64b || Efficient64bOpt) {}
#endif // LLVM_VERSION_MAJOR >= 16

  void getAnalysisUsage(AnalysisUsage &AU) const override {
    AU.addRequired<GenXBackendConfig>();
  }

  StringRef getPassName() const override { return "GenX BTI Assignment"; }

  bool runOnModule(Module &M) override;
};
} // namespace

char GenXBTIAssignment::ID = 0;

INITIALIZE_PASS_BEGIN(GenXBTIAssignment, "GenXBTIAssignment",
                      "GenXBTIAssignment", false, false);
INITIALIZE_PASS_DEPENDENCY(GenXBackendConfig)
INITIALIZE_PASS_END(GenXBTIAssignment, "GenXBTIAssignment", "GenXBTIAssignment",
                    false, false);

#if LLVM_VERSION_MAJOR < 16
namespace llvm {
ModulePass *createGenXBTIAssignmentPass(bool Efficient64b) {
  initializeGenXBTIAssignmentPass(*PassRegistry::getPassRegistry());
  return new GenXBTIAssignment(Efficient64b);
}
} // namespace llvm
#else // LLVM_VERSION_MAJOR < 16
PreservedAnalyses
GenXBTIAssignmentPass::run(llvm::Module &M,
                           llvm::AnalysisManager<llvm::Module> &AM) {
  GenXBTIAssignment GenXBTI(BC, Efficient64b);
  if (GenXBTI.runOnModule(M))
    return PreservedAnalyses::none();
  return PreservedAnalyses::all();
}
#endif

bool GenXBTIAssignment::runOnModule(Module &M) {
#if LLVM_VERSION_MAJOR < 16
  auto &BC = getAnalysis<GenXBackendConfig>();
#endif
  bool emitDebuggableKernels = BC.emitDebuggableKernelsForLegacyPath();
  bool useBindlessBuffers = BC.useBindlessBuffers();
  bool useBindlessImages = BC.useBindlessImages();
  bool useBindlessSamplers = BC.useBindlessImages();

  useBindlessBuffers |= Efficient64b;
  useBindlessImages |= Efficient64b;
  useBindlessSamplers |= Efficient64b;

  BTIAssignment BA(M, emitDebuggableKernels, useBindlessBuffers,
                   useBindlessImages, useBindlessSamplers);

  return BA.run();
}

// Surfaces starting from 240 are reserved.
static constexpr int MaxAvailableSurfaceIndex = 239;
static constexpr int StatelessBti = 255;
static constexpr int MaxAvailableSamplerIndex = 14;

template <typename ZipTy>
std::pair<int, int> BTIAssignment::assignSRV(int SurfaceID, int SamplerID,
                                             ZipTy &&Zippy) {
  // SRV (read only) and samplers.
  for (auto &&[Idx, Kind, Desc] : Zippy) {
    if (Kind == vc::KernelMetadata::AK_SAMPLER) {
      Idx = useBindlessSamplers ? StatelessBti : SamplerID++;
      continue;
    }
    if (Kind == vc::KernelMetadata::AK_SURFACE && vc::isDescReadOnly(Desc)) {
      IGC_ASSERT_MESSAGE(vc::isDescImageType(Desc),
                         "RW qualifiers are allowed on images only");
      Idx = useBindlessImages ? StatelessBti : SurfaceID++;
      continue;
    }
  }
  return {SurfaceID, SamplerID};
}

template <typename ZipTy>
int BTIAssignment::assignUAV(int SurfaceID, ZipTy &&Zippy) {
  // UAV -- writable entities.
  for (auto &&[Idx, Kind, Desc] : Zippy) {
    // Already assigned entities should be skipped.
    if (Idx != -1)
      continue;

    if (Kind == vc::KernelMetadata::AK_SURFACE) {
      if (vc::isDescBufferType(Desc) && useBindlessBuffers)
        Idx = StatelessBti;
      else if (vc::isDescImageType(Desc) && useBindlessImages)
        Idx = StatelessBti;
      else
        Idx = SurfaceID++;
      continue;
    }
    if (Kind == vc::KernelMetadata::AK_NORMAL && vc::isDescSvmPtr(Desc)) {
      Idx = StatelessBti;
      continue;
    }
    // These 'ands' are definitely super buggy. Kinds should be
    // matched with masking and comparision (as in KernelArgInfo).
    // Anding will match more kinds that supposed. Here, for example,
    // SB_BTI is matches too that makes some tests magically work
    // on L0 runtime.
    // FIXME(aus): investigate the reason and rewrite with KAI.
    if (Kind & vc::KernelMetadata::IMP_OCL_PRINTF_BUFFER) {
      Idx = StatelessBti;
      continue;
    }
    if (Kind & vc::KernelMetadata::IMP_OCL_PRIVATE_BASE) {
      Idx = StatelessBti;
      continue;
    }
    if (Kind & vc::KernelMetadata::IMP_OCL_ASSERT_BUFFER) {
      Idx = StatelessBti;
      continue;
    }
    if (Kind & vc::KernelMetadata::IMP_OCL_SYNC_BUFFER) {
      Idx = StatelessBti;
      continue;
    }
  }
  return SurfaceID;
}

// Assign a BTI value to a surface or sampler, NEO path only.
// SRV and UAV is sort of direct3d terminology, though they
// are used across binary format structures.
// SRV -- constant resources -- samplers and read only images.
// UAV -- writeable resources -- buffers and rw/wo images.
// Additionally, ranges for SRV and UAV should be separate and contiguous
// so this code assigns SRV and then UAV resources.
std::vector<int> BTIAssignment::computeBTIndices(
    vc::KernelMetadata &KM, const std::vector<StringRef> &ExtendedArgDescs) {
  int SurfaceID = 0;
  int SamplerID = 0;

  if (emitDebuggableKernels || EnforceBTIZeroReservation) {
    // NOTE: at the current moment we don't use BTI=0, since it is reserved
    // for kernel debugging purposes (SIP uses BTI=0 in order to handle
    // breakpoints).
    SurfaceID = 1;
  }

  std::vector<int> Indices(KM.getArgKinds().size(), -1);

  ArrayRef<unsigned> ArgKinds = KM.getArgKinds();
  auto Zippy = llvm::zip(Indices, KM.getArgKinds(), ExtendedArgDescs);
  std::tie(SurfaceID, SamplerID) = assignSRV(SurfaceID, SamplerID, Zippy);
  SurfaceID = assignUAV(SurfaceID, Zippy);

  auto &Ctx = KM.getFunction()->getContext();
  if (SurfaceID > MaxAvailableSurfaceIndex)
    vc::diagnose(Ctx, "BTIAssignment", "not enough surface indices");
  if (SamplerID > MaxAvailableSamplerIndex)
    vc::diagnose(Ctx, "BTIAssignment", "not enough sampler indices");

  return Indices;
}

bool BTIAssignment::rewriteArguments(
    vc::KernelMetadata &KM, Function &F, const std::vector<int> &BTIndices,
    const std::vector<StringRef> &ExtendedArgDescs) {
  bool Changed = false;

  auto *I32Ty = Type::getInt32Ty(M.getContext());

  IRBuilder<> IRB{&F.front().front()};

  auto ArgKinds = KM.getArgKinds();
  IGC_ASSERT_MESSAGE(ArgKinds.size() == F.arg_size(),
                     "Inconsistent arg kinds metadata");
  for (auto &&[Arg, Kind, BTI, Desc] :
       llvm::zip(F.args(), ArgKinds, BTIndices, ExtendedArgDescs)) {
    if (Kind != vc::KernelMetadata::AK_SAMPLER &&
        Kind != vc::KernelMetadata::AK_SURFACE)
      continue;

    // For bindless buffer resource argument is ExBSO.
    if (useBindlessBuffers && vc::isDescBufferType(Desc))
      continue;

    // For bindless image resource argument is ExBSO.
    if (useBindlessImages && vc::isDescImageType(Desc))
      continue;

    // For bindless sampler resource argument is ExBSO.
    if (useBindlessSamplers && vc::isDescSamplerType(Desc))
      continue;

    IGC_ASSERT_MESSAGE(BTI >= 0, "unassigned BTI");
    Value *BTIConstant = ConstantInt::get(I32Ty, BTI);

    Type *ArgTy = Arg.getType();
    // This code is to handle DPC++ contexts with correct OCL types.
    // We either materialize the constants in-place of args for some
    // instructions (known cases where we do inttoptr followed by ptrtoint) or
    // we just do inttoptr and replace the arg with a new value (default option,
    // later passes clean up the code).
    // FIXME(aus): proper unification of incoming IR is
    // required. Current approach will constantly blow all passes
    // where some additional case should be handled.

    // Step 1: Directly replace if Arg is an integer is of integer type already.
    if (ArgTy->isIntegerTy(32)) {
      IGC_ASSERT_MESSAGE(
          ArgTy == BTIConstant->getType(),
          "Only explicit i32 indices or opaque types are allowed "
          "as bti argument");
      Arg.replaceAllUsesWith(BTIConstant);
      Changed = true;
      continue;
    }

    DenseMap<Type *, Value *> PtrConstCache;
    auto getPtrConst = [&](Type *PTy) -> Value * {
      auto It = PtrConstCache.find(PTy);
      if (It != PtrConstCache.end())
        return It->second;
      Value *V = IRB.CreateIntToPtr(BTIConstant, PTy, ".bti.cast");
      PtrConstCache[PTy] = V;
      return V;
    };

    // Step 2: Otherwise, traverse the chain to find the end constant users.
    SmallVector<Instruction *, 8> WorkList;
    for (Use &U : llvm::make_early_inc_range(Arg.uses())) {
      if (auto *I = dyn_cast<Instruction>(U.getUser()))
        WorkList.push_back(I);
    }

    while (!WorkList.empty()) {
      Instruction *I = WorkList.pop_back_val();

      StoreInst *SI = dyn_cast<StoreInst>(I);
      if (SI && SI->getValueOperand() == &Arg) {
        SI->setOperand(0, BTIConstant);
        Changed = true;
        continue;
      }

#if LLVM_VERSION_MAJOR >= 16
      // This is a workaround due to a bug in Khronos SPIR-V/LLVM Translator. In
      // some cases SPIR-V Writer emits the following sequences:
      // clang-format off
      //
      // TypeImage 104 89 1 0 0 0 0 0 0
      // FunctionParameter 104 350
      // ConvertPtrToU 9 372 350
      //
      // clang-format on
      // The last instruction is illegally trying to use an image type as a
      // source in ConvertPtrToU. Unfortunately, the bug has has not been
      // discovered until the switch to opaque pointers and TargetExtTy. With
      // typed pointers, given that images were represented using pointers to
      // opaque structs, the sequences were "legalized" in LLVM IR. With opaque
      // pointers this leads to an exception in the SPIR-V Reader since it is
      // illegal to use TargetExtTy as a source in LLVM's ptrtoint instruction.
      // As a workaround SPIR-V Reader is emitting builtin calls to
      // "__spirv_ConvertPtrToU" which can be replaced with proper ptrtoint
      // instructions in LLVM IR after retyping TargetExtTy to opaque pointers.
      CallInst *CI = dyn_cast<CallInst>(I);
      if (CI && CI->getOperand(0)->getType()->isTargetExtTy() &&
          CI->getCalledOperand()->getName().contains("__spirv_ConvertPtrToU")) {
        IGC_ASSERT_MESSAGE(CI->getType()->isIntegerTy(32),
                           "__spirv_ConvertPtrToU is expected to return i32!");
        CI->replaceAllUsesWith(BTIConstant);
        CI->eraseFromParent();
        Changed = true;
        continue;
      }
#endif
    }

    // Step 3: Fallback, if after targeted rewrites the argument still has uses,
    // provide a pointer constant. This approach does not work with opaque
    // pointers and TargetExtTy.
    if (!Arg.use_empty()) {
      Value *PtrConst = getPtrConst(ArgTy);
      Arg.replaceAllUsesWith(PtrConst);
      Changed = true;
    }
  }

  return Changed;
}

// Get arg type descs that are extended to arg kinds size.
static std::vector<StringRef> getExtendedArgDescs(vc::KernelMetadata &KM) {
  ArrayRef<unsigned> ArgKinds = KM.getArgKinds();
  ArrayRef<StringRef> ArgTypeDescs = KM.getArgTypeDescs();
  // ArgDescs can be lesser if there are implicit parameters.
  IGC_ASSERT_MESSAGE(
      ArgKinds.size() >= ArgTypeDescs.size(),
      "Expected same or less number of arguments for kinds and descs");

  // All arguments without arg type desc will get default empty description.
  std::vector<StringRef> ArgTypeDescsExt{ArgTypeDescs.begin(),
                                         ArgTypeDescs.end()};
  ArgTypeDescsExt.resize(ArgKinds.size());
  return ArgTypeDescsExt;
}

bool BTIAssignment::processKernel(Function &F) {
  vc::KernelMetadata KM{&F};

  std::vector<StringRef> ExtArgDescs = getExtendedArgDescs(KM);

  std::vector<int> BTIndices = computeBTIndices(KM, ExtArgDescs);

  bool Changed = rewriteArguments(KM, F, BTIndices, ExtArgDescs);

  KM.updateBTIndicesMD(std::move(BTIndices));

  return Changed;
}

bool BTIAssignment::run() {
  bool Changed = false;
  for (Function &Kernel : vc::kernels(M))
    Changed |= processKernel(Kernel);

  return Changed;
}