File: GenXPropagateSurfaceState.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 (530 lines) | stat: -rw-r--r-- 16,286 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*========================== begin_copyright_notice ============================

Copyright (C) 2024-2025 Intel Corporation

SPDX-License-Identifier: MIT

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

//
/// GenXPropagateSurfaceState
/// -----------------------------
///
/// This pass transforms 32-bit surface pointer data flow into 64-bit one
/// making possible to enable efficient 64-bit addressing later on.
/// Mainly there are two cases:
/// 1. Array of surface pointers.
///    64-bit surface pointers are truncated to 32-bit ones,
///    array is formed using series of insertelement instructions
///    then rdregioni gets certain element
///    that is used by BTI memory instrinsics:
///
///    The above is transfomed to:
///    64-bit surface pointer array is formed using series of
///    insertelement instructions then rdregioni gets certain element
///    that is truncated to 32-bit
///    that is used by BTI memory instrinsics
///
/// 2. Function call with surface pointer argument(s).
///    64-bit surface pointers are truncated to 32-bit ones,
///    that are used as function arguments
///    then the functions pass 32-bit surface pointers to
///    BTI memory instrinsics:
///
///    The above is transfomed to:
///    Functions that have 32-bit surface pointer arguments are cloned
///    with 32-bit surface pointer arguments replaced with 64-bit one
///    that are truncated to 32-bit and passed to BTI memory instrinsics

#include "GenX.h"
#include "GenXSubtarget.h"
#include "GenXTargetMachine.h"

#include "vc/Support/GenXDiagnostic.h"
#include "vc/Utils/GenX/Intrinsics.h"

#include "Probe/Assertion.h"

#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/ADT/Twine.h>
#include <llvm/CodeGen/TargetPassConfig.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/InstVisitor.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/ValueMap.h>
#include <llvm/InitializePasses.h>
#include <llvm/Pass.h>
#include "llvmWrapper/IR/Function.h"

#include <queue>

#define DEBUG_TYPE "genx-propagate-surface-state"

using namespace llvm;

namespace {
class GenXPropagateSurfaceState final
    : public ModulePass,
      public InstVisitor<GenXPropagateSurfaceState, Value *> {
public:
  static char ID;

  GenXPropagateSurfaceState() : ModulePass(ID) {}

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

  StringRef getPassName() const override {
    return "GenX Propagate Surface State Pointer";
  }

  bool runOnModule(Module &M) override;

  Value *visitInstruction(Instruction &I) const;
  Value *visitTruncInst(TruncInst &I);
  Value *visitInsertElementInst(InsertElementInst &I);
  Value *visitCastInst(CastInst &I) const;
  Value *visitCallInst(CallInst &I);
  Value *visitSelectInst(SelectInst &I) const;

private:
  bool Modify = false;
  Function *Func = nullptr;

  // main queue to handle 32-bit surface pointer arguments
  std::queue<Value *> OpQueue;

  // instructions that will be replaced with new ones
  ValueMap<Value *, Value *> Map;

  // to track uniqueness of OpQueue
  SmallPtrSet<Value *, 16> ToHandle;

  // functions with their 32-bit surface pointer arguments
  ValueMap<Function *, SmallSet<unsigned, 6>> Fmap;

  // 64-bit functions that will replace 32-bit ones
  ValueMap<Function *, Function *> MapOldToNew;

  void transferArgumentUses(Function &NewF, Function &OldF);
  Function *cloneFunction(Function &F, const SmallSet<unsigned, 6> &Indices);
  void processKernelFunction(Function &F);
  void processFunction(Function &F);
};
} // namespace

char GenXPropagateSurfaceState::ID = 0;

INITIALIZE_PASS_BEGIN(GenXPropagateSurfaceState, "GenXPropagateSurfaceState",
                      "GenXPropagateSurfaceState", false, false);
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
INITIALIZE_PASS_END(GenXPropagateSurfaceState, "GenXPropagateSurfaceState",
                    "GenXPropagateSurfaceState", false, false);

namespace llvm {
ModulePass *createGenXPropagateSurfaceStatePass() {
  initializeGenXPropagateSurfaceStatePass(*PassRegistry::getPassRegistry());
  return new GenXPropagateSurfaceState();
}
} // namespace llvm

void GenXPropagateSurfaceState::transferArgumentUses(Function &NewF,
                                                     Function &OldF) {
  auto *InsPt = &*NewF.getEntryBlock().getFirstInsertionPt();
  IRBuilder<> Builder(InsPt);

  IGC_ASSERT(OldF.arg_size() == NewF.arg_size());
  for (auto &&[OldArg, NewArg] : zip(OldF.args(), NewF.args())) {
    Value *NewVal = &NewArg;

    if (OldArg.getType() != NewArg.getType()) {
      LLVM_DEBUG(dbgs() << "Updating argument <" << OldArg << "> to <" << NewArg
                        << ">\n");
      Modify = true;
      // inserting truncate as the first instruction
      NewVal = Builder.CreateTrunc(NewVal, OldArg.getType());

      OpQueue.push(NewVal);
      ToHandle.insert(NewVal);
    }

    OldArg.replaceAllUsesWith(NewVal);
    NewArg.takeName(&OldArg);
  }
}

Function *
GenXPropagateSurfaceState::cloneFunction(Function &F,
                                         const SmallSet<unsigned, 6> &Indices) {

  LLVM_DEBUG(dbgs() << "Cloning " << F.getName() << "\n");

  SmallVector<Type *> NewArgTypes;
  transform(
      F.args(), std::back_inserter(NewArgTypes), [&](auto &Arg) -> Type * {
        auto *Ty = Arg.getType();
        if (Indices.contains(Arg.getArgNo())) {
          auto *I64Ty = IntegerType::get(Ty->getContext(), 64);
          if (auto *VTy = dyn_cast<IGCLLVM::FixedVectorType>(Ty))
            return IGCLLVM::FixedVectorType::get(I64Ty, VTy->getNumElements());
          return I64Ty;
        }
        return Ty;
      });

  auto *NewFTy = FunctionType::get(F.getReturnType(), NewArgTypes, false);
  auto *NewF = Function::Create(NewFTy, F.getLinkage());

  // Copy name, atributes and calling convention
  NewF->takeName(&F);
  NewF->setAttributes(F.getAttributes());
  NewF->setCallingConv(F.getCallingConv());

  F.getParent()->getFunctionList().insert(F.getIterator(), NewF);

  // Transfer debug info to the new function
  auto *DISubprog = F.getSubprogram();
  NewF->setSubprogram(DISubprog);
  F.setSubprogram(nullptr);

  // Splice the body of the old function into the new function.
  IGCLLVM::splice(NewF, NewF->begin(), &F);

  LLVM_DEBUG(dbgs() << "NewF " << NewF->getName() << "\n");

  transferArgumentUses(*NewF, F);

  return NewF;
}

bool GenXPropagateSurfaceState::runOnModule(Module &M) {
  const GenXSubtarget &ST = getAnalysis<TargetPassConfig>()
                                .getTM<GenXTargetMachine>()
                                .getGenXSubtarget();

  // Doing nothing if e64 is not enabled
  if (!ST.hasEfficient64b())
    return false;

  for (auto &F : M.functions()) {
    if (!F.isDeclaration() && vc::isKernel(F))
      processKernelFunction(F);
  }

  while (!Fmap.empty()) {
    auto It = Fmap.begin();
    auto *F = It->first;
    auto &Indices = It->second;
    auto *NewF = cloneFunction(*F, Indices);
    MapOldToNew.insert({F, NewF});
    processFunction(*NewF);
    Fmap.erase(It);
  }

  for (auto &&[Old, New] : MapOldToNew) {
    for (auto UseIt = Old->use_begin(); UseIt != Old->use_end();) {
      Use &U = *UseIt++;
      auto *CI = cast<CallInst>(U.getUser());
      IRBuilder<> Builder(CI);
      SmallVector<Value *, 6> Args;
      std::transform(CI->arg_begin(), CI->arg_end(), New->arg_begin(),
                     std::back_inserter(Args), [](auto &OldArg, auto &NewArg) {
                       if (OldArg->getType() == NewArg.getType())
                         return OldArg.get();
                       return cast<TruncInst>(OldArg)->getOperand(0);
                     });
      auto *NewCI = Builder.CreateCall(New, Args);
      Modify = true;
      NewCI->takeName(CI);
      CI->replaceAllUsesWith(NewCI);
      CI->eraseFromParent();
    }
    Old->eraseFromParent();
  }

  return Modify;
}

void GenXPropagateSurfaceState::processKernelFunction(Function &F) {
  IGC_ASSERT(!F.isDeclaration() && vc::isKernel(F));

  Map.clear();
  ToHandle.clear();

  vc::KernelMetadata KM{&F};
  auto ArgKinds = KM.getArgKinds();

  IGC_ASSERT_MESSAGE(ArgKinds.size() == F.arg_size(),
                     "Inconsistent arg kind metadata");

  for (auto &&[Arg, Kind] : zip(F.args(), ArgKinds)) {
    if (Kind == vc::KernelMetadata::AK_SAMPLER ||
        Kind == vc::KernelMetadata::AK_SURFACE) {
      OpQueue.push(&Arg);
      ToHandle.insert(&Arg);
    }
  }
  processFunction(F);
}

static bool eligibleForGettingUsers(Value *V) {
  // We have to stop when we reach a memory intrinsic
  if (vc::InternalIntrinsic::isInternalMemoryIntrinsic(V))
    return false;

  return !isa<CallInst>(V) || vc::isAnyNonTrivialIntrinsic(V);
}

void GenXPropagateSurfaceState::processFunction(Function &F) {
  auto HandleUsers = [&](Value *V) {
    for (auto *User : V->users())
      if (!ToHandle.contains(User)) {
        OpQueue.push(User);
        ToHandle.insert(User);
      }
  };

  Func = &F;

  while (!OpQueue.empty()) {
    auto *V = OpQueue.front();
    OpQueue.pop();
    LLVM_DEBUG(dbgs() << "Processing value: " << *V << "\n");

    bool NeedsHandleUsers = true;

    if (auto *I = dyn_cast<Instruction>(V)) {
      NeedsHandleUsers = false;
      if (auto *NewV = visit(I)) {
        NeedsHandleUsers = true;
        if (NewV != I) {
          LLVM_DEBUG(dbgs() << "Map.insert: " << *I << "->" << *NewV << "\n");
          Map.insert({I, NewV});
        }
      }
    }

    if (NeedsHandleUsers)
      HandleUsers(V);
  }
}

Value *GenXPropagateSurfaceState::visitCastInst(CastInst &I) const {
  return &I;
}

Value *GenXPropagateSurfaceState::visitSelectInst(SelectInst &I) const {
  return &I;
}

Value *GenXPropagateSurfaceState::visitInstruction(Instruction &I) const {
  IGC_ASSERT_EXIT_MESSAGE(0, "yet unsupported instruction");
  return nullptr;
}

Value *GenXPropagateSurfaceState::visitTruncInst(TruncInst &I) {
  auto *Ty = I.getType();
  // Only dealing with 32-bit surface pointers
  if (!Ty->isIntOrIntVectorTy(32))
    return nullptr;

  IGC_ASSERT(I.getSrcTy()->isIntOrIntVectorTy(64));
  return I.getOperand(0);
}

Value *GenXPropagateSurfaceState::visitInsertElementInst(InsertElementInst &I) {
  auto *Ty = I.getType();
  // Only dealing with 32-bit surface pointers
  if (!Ty->isIntOrIntVectorTy(32))
    return nullptr;

  auto *Into = I.getOperand(0);
  auto *V = I.getOperand(1);
  auto *Index = I.getOperand(2);

  auto It = Map.find(V);
  IGC_ASSERT(It != Map.end());
  V = It->second;

  if (isa<UndefValue>(Into)) {
    auto *VTy = cast<IGCLLVM::FixedVectorType>(Into->getType());
    auto *NewVTy =
        IGCLLVM::FixedVectorType::get(V->getType(), VTy->getNumElements());
    Into = UndefValue::get(NewVTy);
  } else {
    auto It = Map.find(Into);
    if (It == Map.end()) {
      // Try again later if Into operand is not ready
      OpQueue.push(&I);
      return nullptr;
    }
    Into = It->second;
  }

  Modify = true;
  IRBuilder<> Builder(&I);
  return Builder.CreateInsertElement(Into, V, Index);
}

Value *GenXPropagateSurfaceState::visitCallInst(CallInst &I) {
  IRBuilder<> Builder(&I);

  auto IID = vc::getAnyIntrinsicID(&I);
  if (IID == vc::InternalIntrinsic::optimization_fence)
    return I.getArgOperand(0);

  if (GenXIntrinsic::isRdRegion(&I)) {
    auto *Ty = I.getType();
    // Only dealing with 32-bit surface pointers
    if (!Ty->isIntOrIntVectorTy(32))
      return nullptr;

    auto *Src = I.getOperand(0);
    auto It = Map.find(Src);
    if (It == Map.end()) {
      // Try again later if Src operand is not ready
      OpQueue.push(&I);
      return nullptr;
    }

    auto *NewSrc = It->second;
    auto *NewSrcTy = cast<IGCLLVM::FixedVectorType>(NewSrc->getType());

    auto *ResTy = I.getType();
    auto *NewResTy = NewSrcTy->getElementType();
    if (auto *ResVTy = dyn_cast<IGCLLVM::FixedVectorType>(ResTy))
      NewResTy =
          IGCLLVM::FixedVectorType::get(NewResTy, ResVTy->getNumElements());

    Modify = true;

    // Multiplying by 2 as 32-bit element is replaced with 64-bit
    SmallVector<Value *, 6> Args = {
        NewSrc,
        I.getOperand(1),                                         // vstride
        I.getOperand(2),                                         // width
        I.getOperand(3),                                         // stride
        Builder.CreateMul(I.getOperand(4), Builder.getInt16(2)), // offset
        I.getOperand(5),
    };

    auto *Decl = vc::getAnyDeclarationForArgs(
        Func->getParent(), GenXIntrinsic::genx_rdregioni, NewResTy, Args);
    return Builder.CreateCall(Decl, Args);
  }

  if (GenXIntrinsic::isWrRegion(&I)) {
    auto *Ty = I.getType();
    // Only dealing with 32-bit surface pointers
    if (!Ty->isIntOrIntVectorTy(32))
      return nullptr;

    auto *Src = I.getOperand(1);
    auto It = Map.find(Src);
    if (It == Map.end()) {
      // Try again later if Src operand is not ready
      OpQueue.push(&I);
      return nullptr;
    }

    auto *NewSrc = It->second;
    auto *NewSrcTy = cast<IGCLLVM::FixedVectorType>(NewSrc->getType());

    auto *Into = I.getOperand(0);
    if (isa<UndefValue>(Into)) {
      auto *VTy = cast<IGCLLVM::FixedVectorType>(Into->getType());
      auto *NewVTy = IGCLLVM::FixedVectorType::get(NewSrcTy->getElementType(),
                                                   VTy->getNumElements());
      Into = UndefValue::get(NewVTy);
    } else {
      auto It = Map.find(Into);
      if (It == Map.end()) {
        // Try again later if Into operand is not ready
        OpQueue.push(&I);
        return nullptr;
      }
      Into = It->second;
    }

    auto *ResTy = I.getType();
    auto *NewResTy = NewSrcTy->getElementType();
    if (auto *ResVTy = dyn_cast<IGCLLVM::FixedVectorType>(ResTy))
      NewResTy =
          IGCLLVM::FixedVectorType::get(NewResTy, ResVTy->getNumElements());

    Modify = true;

    // Multiplying by 2 as 32-bit element is replaced with 64-bit
    SmallVector<Value *, 8> Args = {
        Into,
        NewSrc,
        I.getOperand(2),                                         // vstride
        I.getOperand(3),                                         // width
        I.getOperand(4),                                         // stride
        Builder.CreateMul(I.getOperand(5), Builder.getInt16(2)), // offset
        I.getOperand(6),
        I.getOperand(7),
    };

    auto *Decl = vc::getAnyDeclarationForArgs(
        Func->getParent(), GenXIntrinsic::genx_wrregioni, NewResTy, Args);
    return Builder.CreateCall(Decl, Args);
  }

  if (vc::InternalIntrinsic::isInternalMemoryIntrinsic(&I)) {
    const auto IID = vc::getAnyIntrinsicID(&I);
    const auto BTIIndex =
        vc::InternalIntrinsic::getMemorySurfaceOperandIndex(IID);
    IGC_ASSERT_EXIT_MESSAGE(BTIIndex >= 0, "Unknown BTI operand number");
    auto *BTI = I.getOperand(BTIIndex);
    if (isa<Argument>(BTI)) {
      // generate an error if BTI is a function argument to avoid a hang
      auto &Ctx = Func->getContext();
      vc::diagnose(Ctx, "GenXPropagateSurfaceState",
                   "BTI argument is not expected");
      return nullptr;
    }
    auto It = Map.find(BTI);
    if (It == Map.end()) {
      // Try again later if BIT operand is not ready
      OpQueue.push(&I);
      return nullptr;
    }

    // truncate can have been already inserted by clone function
    if (isa<TruncInst>(BTI))
      return nullptr;

    Modify = true;
    auto *Trunc = Builder.CreateTrunc(It->second, BTI->getType());
    I.setOperand(BTIIndex, Trunc);
    return nullptr;
  }

  if (auto *F = I.getCalledFunction(); F && !F->isDeclaration()) {
    SmallSet<unsigned, 6> BtiIndexSet;
    auto Fit = Fmap.find(F);
    if (Fit == Fmap.end()) {
      auto [Jt, Ok] = Fmap.insert({F, BtiIndexSet});
      IGC_ASSERT(Ok);
      Fit = Jt;
    }

    for (auto &Arg : I.args()) {
      auto It = Map.find(Arg);
      if (It != Map.end()) {
        Fit->second.insert(Arg.getOperandNo());
        Modify = true;
        auto *Trunc = Builder.CreateTrunc(It->second, Arg->getType());
        Arg.set(Trunc);
      }
    }
    return nullptr;
  }

  IGC_ASSERT_EXIT_MESSAGE(0, "Unimplemented");
  return nullptr;
}