File: ImplicitArgsBuffer.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (211 lines) | stat: -rw-r--r-- 9,030 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2022 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "vc/Utils/GenX/ImplicitArgsBuffer.h"

#include "vc/Utils/GenX/IRBuilder.h"
#include "vc/Utils/GenX/PredefinedVariable.h"
#include "vc/Utils/General/Types.h"

#include "Probe/Assertion.h"
#include "llvmWrapper/IR/Module.h"

#include <llvm/GenXIntrinsics/GenXIntrinsics.h>

#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>

#include <algorithm>
#include <array>

using namespace llvm;

StructType &vc::ImplicitArgs::Buffer::getType(Module &M) {
  using namespace vc::ImplicitArgs::Buffer;
  auto *MaybeResult = IGCLLVM::getTypeByName(M, TypeName);
  if (MaybeResult)
    return *MaybeResult;

  // Have to construct the type.
  auto &C = M.getContext();
  auto *Int8Ty = IntegerType::getInt8Ty(C);
  auto *Int32Ty = IntegerType::getInt32Ty(C);
  auto *Int64Ty = IntegerType::getInt64Ty(C);
  std::array<Type *, Indices::Size> ElementTys = {nullptr};
  ElementTys[Indices::StructSize] = Int8Ty;
  ElementTys[Indices::StructVersion] = Int8Ty;
  ElementTys[Indices::NumWorkDim] = Int8Ty;
  ElementTys[Indices::SIMDWidth] = Int8Ty;
  ElementTys[Indices::LocalSizeX] = Int32Ty;
  ElementTys[Indices::LocalSizeY] = Int32Ty;
  ElementTys[Indices::LocalSizeZ] = Int32Ty;
  ElementTys[Indices::GlobalSizeX] = Int64Ty;
  ElementTys[Indices::GlobalSizeY] = Int64Ty;
  ElementTys[Indices::GlobalSizeZ] = Int64Ty;
  ElementTys[Indices::PrintfBufferPtr] = Int64Ty;
  ElementTys[Indices::GlobalOffsetX] = Int64Ty;
  ElementTys[Indices::GlobalOffsetY] = Int64Ty;
  ElementTys[Indices::GlobalOffsetZ] = Int64Ty;
  ElementTys[Indices::LocalIDTablePtr] = Int64Ty;
  ElementTys[Indices::GroupCountX] = Int32Ty;
  ElementTys[Indices::GroupCountY] = Int32Ty;
  ElementTys[Indices::GroupCountZ] = Int32Ty;
  IGC_ASSERT_MESSAGE(std::all_of(ElementTys.begin(), ElementTys.end(),
                                 [](Type *Ty) -> bool { return Ty; }),
                     "all structure element types must be set by the function");
  return *StructType::create(C, ElementTys, TypeName);
}

vc::AddrSpace::Enum
vc::ImplicitArgs::Buffer::getPtrAddrSpace(vc::ThreadPayloadKind Kind) {
  // For targets with the payload in memory A32 addressing is used.
  if (Kind == vc::ThreadPayloadKind::InMemory)
    return vc::AddrSpace::GlobalA32;
  // Otherwise A64/A32 addressing is used.
  return vc::AddrSpace::Global;
}

PointerType &vc::ImplicitArgs::Buffer::getPtrType(Module &M,
                                                  vc::ThreadPayloadKind Kind) {
  return *PointerType::get(&vc::ImplicitArgs::Buffer::getType(M),
                           vc::ImplicitArgs::Buffer::getPtrAddrSpace(Kind));
}

template <>
Value &vc::ImplicitArgs::Buffer::getPointer<vc::ThreadPayloadKind::InMemory>(
    IRBuilder<> &IRB) {
  auto *R0Decl = GenXIntrinsic::getGenXDeclaration(
      IRB.GetInsertBlock()->getModule(), GenXIntrinsic::genx_r0,
      IRB.getInt32Ty());
  auto *R0 = IRB.CreateCall(R0Decl, None, "r0.0");
  auto *IntPtr = IRB.CreateAnd(
      R0, IRB.getInt32(maskTrailingZeros<uint32_t>(vc::PtrOffsetInR00)),
      "indir.data.heap.ptr.int");
  auto *PtrTy = &vc::ImplicitArgs::Buffer::getPtrType(
      *IRB.GetInsertBlock()->getModule(), vc::ThreadPayloadKind::InMemory);
  auto *Ptr = IRB.CreateIntToPtr(IntPtr, PtrTy, "indir.data.heap.ptr");
  return *Ptr;
}

template <>
Value &vc::ImplicitArgs::Buffer::getPointer<vc::ThreadPayloadKind::OnRegister>(
    IRBuilder<> &IRB) {
  GlobalVariable *IAVar = IRB.GetInsertPoint()->getModule()->getNamedGlobal(
      vc::PredefVar::ImplicitArgsBufferName);
  IGC_ASSERT_MESSAGE(IAVar, "Implicit args buffer predefined variable must "
                            "have already been created");
  auto *IntPtr = vc::createReadVariableRegion(*IAVar, IRB);
  auto *PtrTy = &vc::ImplicitArgs::Buffer::getPtrType(
      *IRB.GetInsertBlock()->getModule(), vc::ThreadPayloadKind::OnRegister);
  auto *Ptr = IRB.CreateIntToPtr(IntPtr, PtrTy, "indir.data.heap.ptr");
  return *Ptr;
}

Value &vc::ImplicitArgs::Buffer::getPointer(llvm::IRBuilder<> &IRB,
                                            ThreadPayloadKind Kind) {
  if (Kind == vc::ThreadPayloadKind::InMemory)
    return vc::ImplicitArgs::Buffer::getPointer<
        vc::ThreadPayloadKind::InMemory>(IRB);
  IGC_ASSERT_MESSAGE(Kind == vc::ThreadPayloadKind::OnRegister,
                     "an unexpected thread payload kind");
  return vc::ImplicitArgs::Buffer::getPointer<
      vc::ThreadPayloadKind::OnRegister>(IRB);
}

Value &vc::ImplicitArgs::Buffer::loadField(Value &BufferPtr,
                                           Indices::Enum FieldIdx,
                                           IRBuilder<> &IRB,
                                           const Twine &Name) {
  IGC_ASSERT_MESSAGE(
      BufferPtr.getType() == &vc::ImplicitArgs::Buffer::getPtrType(
                                 *IRB.GetInsertBlock()->getModule(),
                                 vc::ThreadPayloadKind::InMemory) ||
          BufferPtr.getType() == &vc::ImplicitArgs::Buffer::getPtrType(
                                     *IRB.GetInsertBlock()->getModule(),
                                     vc::ThreadPayloadKind::OnRegister),
      "wrong argument: a wrong type for buffer pointer value");
  auto *FieldPtr = IRB.CreateInBoundsGEP(
      BufferPtr.getType()->getPointerElementType(), &BufferPtr,
      {IRB.getInt32(0), IRB.getInt32(FieldIdx)}, Name + ".ptr");
  auto *FieldVal = IRB.CreateLoad(FieldPtr->getType()->getPointerElementType(),
                                  FieldPtr, Name);
  return *FieldVal;
}

StructType &vc::ImplicitArgs::LocalID::getType(Module &M) {
  auto *MaybeResult = IGCLLVM::getTypeByName(M, TypeName);
  if (MaybeResult)
    return *MaybeResult;

  auto &C = M.getContext();
  auto *Int16Ty = IntegerType::getInt16Ty(C);
  return *StructType::create(C, {Int16Ty, Int16Ty, Int16Ty},
                             vc::ImplicitArgs::LocalID::TypeName);
}

PointerType &vc::ImplicitArgs::LocalID::getPtrType(Module &M) {
  return *PointerType::get(&vc::ImplicitArgs::LocalID::getType(M),
                           vc::AddrSpace::Global);
}

Value &vc::ImplicitArgs::LocalID::getBasePtr(Value &BufferPtr, IRBuilder<> &IRB,
                                             const Twine &Name) {
  auto &IntPtr = vc::ImplicitArgs::Buffer::loadField(
      BufferPtr, vc::ImplicitArgs::Buffer::Indices::LocalIDTablePtr, IRB,
      Name + ".int");
  return *IRB.CreateIntToPtr(&IntPtr,
                             &vc::ImplicitArgs::LocalID::getPtrType(
                                 *IRB.GetInsertBlock()->getModule()),
                             Name);
}
template <>
Value &vc::ImplicitArgs::LocalID::getPointer<vc::ThreadPayloadKind::InMemory>(
    Value &BufferPtr, IRBuilder<> &IRB, const Twine &Name) {
  auto &BasePtr =
      vc::ImplicitArgs::LocalID::getBasePtr(BufferPtr, IRB, Name + ".base");
  Value *Index = vc::getGroupThreadIDForPIM(IRB);
  return *IRB.CreateGEP(BasePtr.getType()->getPointerElementType(), &BasePtr,
                        Index, Name);
}

template <>
Value &vc::ImplicitArgs::LocalID::getPointer<vc::ThreadPayloadKind::OnRegister>(
    Value &BufferPtr, IRBuilder<> &IRB, const Twine &Name) {
  GlobalVariable *IAVar = IRB.GetInsertPoint()->getModule()->getNamedGlobal(
      vc::PredefVar::LocalIDBufferName);
  IGC_ASSERT_MESSAGE(IAVar, "Local ID buffer predefined variable must "
                            "have already been created");
  auto *IntPtr = vc::createReadVariableRegion(*IAVar, IRB, Name + ".intptr");
  return *IRB.CreateIntToPtr(IntPtr,
                             &vc::ImplicitArgs::LocalID::getPtrType(
                                 *IRB.GetInsertPoint()->getModule()),
                             Name);
}

Value &vc::ImplicitArgs::LocalID::getPointer(Value &BufferPtr,
                                             llvm::IRBuilder<> &IRB,
                                             ThreadPayloadKind Kind,
                                             const Twine &Name) {
  if (Kind == vc::ThreadPayloadKind::InMemory)
    return vc::ImplicitArgs::LocalID::getPointer<
        vc::ThreadPayloadKind::InMemory>(BufferPtr, IRB, Name);
  IGC_ASSERT_MESSAGE(Kind == vc::ThreadPayloadKind::OnRegister,
                     "an unexpected thread payload kind");
  return vc::ImplicitArgs::LocalID::getPointer<
      vc::ThreadPayloadKind::OnRegister>(BufferPtr, IRB, Name);
}

Value &vc::ImplicitArgs::LocalID::loadField(
    Value &LIDStructPtr, vc::ImplicitArgs::LocalID::Indices::Enum FieldIdx,
    IRBuilder<> &IRB, const Twine &Name) {
  auto *Ptr = IRB.CreateInBoundsGEP(
      LIDStructPtr.getType()->getPointerElementType(), &LIDStructPtr,
      {IRB.getInt32(0), IRB.getInt32(FieldIdx)}, Name + ".ptr");
  return *IRB.CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
}