File: SPIRVType.cpp

package info (click to toggle)
spirv-llvm-translator-19 19.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,996 kB
  • sloc: cpp: 45,009; ansic: 6,283; lisp: 3,739; sh: 162; python: 58; makefile: 33
file content (368 lines) | stat: -rw-r--r-- 12,876 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
//===- SPIRVtype.cpp - Class to represent a SPIR-V type ---------*- C++ -*-===//
//
//                     The LLVM/SPIRV Translator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimers in the documentation
// and/or other materials provided with the distribution.
// Neither the names of Advanced Micro Devices, Inc., nor the names of its
// contributors may be used to endorse or promote products derived from this
// Software without specific prior written permission.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
// THE SOFTWARE.
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file implements the types defined in SPIRV spec with op codes.
///
//===----------------------------------------------------------------------===//

#include "SPIRVType.h"
#include "SPIRVDecorate.h"
#include "SPIRVModule.h"
#include "SPIRVValue.h"

#include <cassert>

namespace SPIRV {

SPIRVType *SPIRVType::getArrayElementType() const {
  assert(OpCode == OpTypeArray && "Not array type");
  return static_cast<const SPIRVTypeArray *>(this)->getElementType();
}

uint64_t SPIRVType::getArrayLength() const {
  assert(OpCode == OpTypeArray && "Not array type");
  const SPIRVTypeArray *AsArray = static_cast<const SPIRVTypeArray *>(this);
  assert(AsArray->getLength()->getOpCode() == OpConstant &&
         "getArrayLength can only be called with constant array lengths");
  return static_cast<SPIRVConstant *>(AsArray->getLength())->getZExtIntValue();
}

SPIRVWord SPIRVType::getBitWidth() const {
  if (isTypeVector())
    return getVectorComponentType()->getBitWidth();
  if (isTypeBool())
    return 1;
  return isTypeInt() ? getIntegerBitWidth() : getFloatBitWidth();
}

SPIRVWord SPIRVType::getFloatBitWidth() const {
  assert(OpCode == OpTypeFloat && "Not a float type");
  return static_cast<const SPIRVTypeFloat *>(this)->getBitWidth();
}

SPIRVWord SPIRVType::getIntegerBitWidth() const {
  assert((OpCode == OpTypeInt || OpCode == OpTypeBool) &&
         "Not an integer type");
  if (isTypeBool())
    return 1;
  return static_cast<const SPIRVTypeInt *>(this)->getBitWidth();
}

SPIRVType *SPIRVType::getFunctionReturnType() const {
  assert(OpCode == OpTypeFunction);
  return static_cast<const SPIRVTypeFunction *>(this)->getReturnType();
}

SPIRVType *SPIRVType::getPointerElementType() const {
  assert(OpCode == OpTypePointer && "Not a pointer type");
  return static_cast<const SPIRVTypePointer *>(this)->getElementType();
}

SPIRVStorageClassKind SPIRVType::getPointerStorageClass() const {
  assert(OpCode == OpTypePointer && "Not a pointer type");
  return static_cast<const SPIRVTypePointer *>(this)->getStorageClass();
}

SPIRVType *SPIRVType::getStructMemberType(size_t Index) const {
  assert(OpCode == OpTypeStruct && "Not struct type");
  return static_cast<const SPIRVTypeStruct *>(this)->getMemberType(Index);
}

SPIRVWord SPIRVType::getStructMemberCount() const {
  assert(OpCode == OpTypeStruct && "Not struct type");
  return static_cast<const SPIRVTypeStruct *>(this)->getMemberCount();
}

SPIRVWord SPIRVType::getVectorComponentCount() const {
  assert(OpCode == OpTypeVector && "Not vector type");
  return static_cast<const SPIRVTypeVector *>(this)->getComponentCount();
}

SPIRVType *SPIRVType::getVectorComponentType() const {
  if (OpCode == OpTypeVector)
    return static_cast<const SPIRVTypeVector *>(this)->getComponentType();
  if (OpCode == internal::OpTypeJointMatrixINTEL)
    return static_cast<const SPIRVTypeJointMatrixINTEL *>(this)->getCompType();
  if (OpCode == OpTypeCooperativeMatrixKHR)
    return static_cast<const SPIRVTypeCooperativeMatrixKHR *>(this)
        ->getCompType();
  assert(0 && "getVectorComponentType(): Not a vector or joint matrix type");
  return nullptr;
}

SPIRVWord SPIRVType::getMatrixColumnCount() const {
  assert(OpCode == OpTypeMatrix && "Not matrix type");
  return static_cast<const SPIRVTypeMatrix *>(this)->getColumnCount();
}

SPIRVType *SPIRVType::getMatrixColumnType() const {
  assert(OpCode == OpTypeMatrix && "Not matrix type");
  return static_cast<const SPIRVTypeMatrix *>(this)->getColumnType();
}

SPIRVType *SPIRVType::getScalarType() const {
  switch (OpCode) {
  case OpTypePointer:
    return getPointerElementType()->getScalarType();
  case OpTypeArray:
    return getArrayElementType();
  case OpTypeVector:
    return getVectorComponentType();
  case OpTypeMatrix:
    return getMatrixColumnType()->getVectorComponentType();
  case OpTypeInt:
  case OpTypeFloat:
  case OpTypeBool:
    return const_cast<SPIRVType *>(this);
  default:
    break;
  }
  return nullptr;
}

bool SPIRVType::isTypeVoid() const { return OpCode == OpTypeVoid; }
bool SPIRVType::isTypeArray() const { return OpCode == OpTypeArray; }

bool SPIRVType::isTypeBool() const { return OpCode == OpTypeBool; }

bool SPIRVType::isTypeComposite() const {
  return isTypeVector() || isTypeArray() || isTypeStruct() ||
         isTypeJointMatrixINTEL() || isTypeCooperativeMatrixKHR();
}

bool SPIRVType::isTypeFloat(unsigned Bits,
                            unsigned FloatingPointEncoding) const {
  if (!isType<SPIRVTypeFloat>(this))
    return false;
  if (Bits == 0)
    return true;
  const auto *ThisFloat = static_cast<const SPIRVTypeFloat *>(this);
  return ThisFloat->getBitWidth() == Bits &&
         ThisFloat->getFloatingPointEncoding() == FloatingPointEncoding;
}

bool SPIRVType::isTypeOCLImage() const {
  return isTypeImage() &&
         static_cast<const SPIRVTypeImage *>(this)->isOCLImage();
}

bool SPIRVType::isTypePipe() const { return OpCode == OpTypePipe; }

bool SPIRVType::isTypePipeStorage() const {
  return OpCode == OpTypePipeStorage;
}

bool SPIRVType::isTypeReserveId() const { return OpCode == OpTypeReserveId; }

bool SPIRVType::isTypeInt(unsigned Bits) const {
  return isType<SPIRVTypeInt>(this, Bits);
}

bool SPIRVType::isTypePointer() const { return OpCode == OpTypePointer; }

bool SPIRVType::isTypeOpaque() const { return OpCode == OpTypeOpaque; }

bool SPIRVType::isTypeEvent() const { return OpCode == OpTypeEvent; }

bool SPIRVType::isTypeDeviceEvent() const {
  return OpCode == OpTypeDeviceEvent;
}

bool SPIRVType::isTypeSampler() const { return OpCode == OpTypeSampler; }

bool SPIRVType::isTypeImage() const { return OpCode == OpTypeImage; }

bool SPIRVType::isTypeSampledImage() const {
  return OpCode == OpTypeSampledImage;
}

bool SPIRVType::isTypeStruct() const { return OpCode == OpTypeStruct; }

bool SPIRVType::isTypeVector() const { return OpCode == OpTypeVector; }

bool SPIRVType::isTypeJointMatrixINTEL() const {
  return OpCode == internal::OpTypeJointMatrixINTEL ||
         OpCode == internal::OpTypeJointMatrixINTELv2;
}

bool SPIRVType::isTypeCooperativeMatrixKHR() const {
  return OpCode == OpTypeCooperativeMatrixKHR;
}

bool SPIRVType::isTypeVectorBool() const {
  return isTypeVector() && getVectorComponentType()->isTypeBool();
}

bool SPIRVType::isTypeVectorInt() const {
  return isTypeVector() && getVectorComponentType()->isTypeInt();
}

bool SPIRVType::isTypeVectorFloat() const {
  return isTypeVector() && getVectorComponentType()->isTypeFloat();
}

bool SPIRVType::isTypeVectorOrScalarBool() const {
  return isTypeBool() || isTypeVectorBool();
}

bool SPIRVType::isTypeVectorPointer() const {
  return isTypeVector() && getVectorComponentType()->isTypePointer();
}

bool SPIRVType::isTypeSubgroupAvcINTEL() const {
  return isSubgroupAvcINTELTypeOpCode(OpCode);
}

bool SPIRVType::isTypeSubgroupAvcMceINTEL() const {
  return OpCode == OpTypeAvcMcePayloadINTEL ||
         OpCode == OpTypeAvcMceResultINTEL;
}

bool SPIRVType::isTypeTaskSequenceINTEL() const {
  return OpCode == internal::OpTypeTaskSequenceINTEL;
}

bool SPIRVType::isTypeVectorOrScalarInt() const {
  return isTypeInt() || isTypeVectorInt();
}

bool SPIRVType::isTypeVectorOrScalarFloat() const {
  return isTypeFloat() || isTypeVectorFloat();
}

bool SPIRVTypeStruct::isPacked() const {
  return hasDecorate(DecorationCPacked);
}

void SPIRVTypeStruct::setPacked(bool Packed) {
  if (Packed)
    addDecorate(new SPIRVDecorate(DecorationCPacked, this));
  else
    eraseDecorate(DecorationCPacked);
}

SPIRVTypeArray::SPIRVTypeArray(SPIRVModule *M, SPIRVId TheId,
                               SPIRVType *TheElemType, SPIRVValue *TheLength)
    : SPIRVType(M, 4, OpTypeArray, TheId), ElemType(TheElemType),
      Length(TheLength->getId()) {
  validate();
}

void SPIRVTypeArray::validate() const {
  SPIRVEntry::validate();
  ElemType->validate();
  assert(getValue(Length)->getType()->isTypeInt());
  assert(isConstantOpCode(getValue(Length)->getOpCode()));
}

SPIRVValue *SPIRVTypeArray::getLength() const { return getValue(Length); }

_SPIRV_IMP_ENCDEC3(SPIRVTypeArray, Id, ElemType, Length)

void SPIRVTypeForwardPointer::encode(spv_ostream &O) const {
  getEncoder(O) << PointerId << SC;
}

void SPIRVTypeForwardPointer::decode(std::istream &I) {
  auto Decoder = getDecoder(I);
  Decoder >> PointerId >> SC;
}

SPIRVTypeJointMatrixINTEL::SPIRVTypeJointMatrixINTEL(
    SPIRVModule *M, SPIRVId TheId, Op OC, SPIRVType *CompType,
    std::vector<SPIRVValue *> Args)
    : SPIRVType(M, FixedWC + Args.size(), OC, TheId), CompType(CompType),
      Args(std::move(Args)) {}

SPIRVTypeJointMatrixINTEL::SPIRVTypeJointMatrixINTEL(
    SPIRVModule *M, SPIRVId TheId, SPIRVType *CompType,
    std::vector<SPIRVValue *> Args)
    : SPIRVType(M, FixedWC + Args.size(), internal::OpTypeJointMatrixINTEL,
                TheId),
      CompType(CompType), Args(std::move(Args)) {}

SPIRVTypeJointMatrixINTEL::SPIRVTypeJointMatrixINTEL()
    : SPIRVType(internal::OpTypeJointMatrixINTEL), CompType(nullptr),
      Args({nullptr, nullptr, nullptr, nullptr}) {}

void SPIRVTypeJointMatrixINTEL::encode(spv_ostream &O) const {
  auto Encoder = getEncoder(O);
  Encoder << Id << CompType << Args;
}

void SPIRVTypeJointMatrixINTEL::decode(std::istream &I) {
  auto Decoder = getDecoder(I);
  Decoder >> Id >> CompType >> Args;
}

SPIRVTypeCooperativeMatrixKHR::SPIRVTypeCooperativeMatrixKHR(
    SPIRVModule *M, SPIRVId TheId, SPIRVType *CompType,
    std::vector<SPIRVValue *> Args)
    : SPIRVType(M, FixedWC, OpTypeCooperativeMatrixKHR, TheId),
      CompType(CompType), Args(std::move(Args)) {}

SPIRVTypeCooperativeMatrixKHR::SPIRVTypeCooperativeMatrixKHR()
    : SPIRVType(OpTypeCooperativeMatrixKHR), CompType(nullptr),
      Args({nullptr, nullptr, nullptr, nullptr}) {}

void SPIRVTypeCooperativeMatrixKHR::encode(spv_ostream &O) const {
  auto Encoder = getEncoder(O);
  Encoder << Id << CompType << Args;
}

void SPIRVTypeCooperativeMatrixKHR::decode(std::istream &I) {
  auto Decoder = getDecoder(I);
  Decoder >> Id >> CompType >> Args;
}

void SPIRVTypeCooperativeMatrixKHR::validate() const {
  SPIRVEntry::validate();
  SPIRVErrorLog &SPVErrLog = this->getModule()->getErrorLog();
  SPIRVConstant *UseConst = static_cast<SPIRVConstant *>(this->getUse());
  auto InstName = OpCodeNameMap::map(OC);
  uint64_t UseValue = UseConst->getZExtIntValue();
  SPVErrLog.checkError(
      (UseValue <= CooperativeMatrixUseMatrixAccumulatorKHR),
      SPIRVEC_InvalidInstruction,
      InstName + "\nIncorrect Use parameter, should be MatrixA, MatrixB or "
                 "Accumulator\n");
  SPIRVConstant *ScopeConst = static_cast<SPIRVConstant *>(this->getScope());
  uint64_t ScopeValue = ScopeConst->getZExtIntValue();
  SPVErrLog.checkError((ScopeValue <= ScopeInvocation),
                       SPIRVEC_InvalidInstruction,
                       InstName + "\nUnsupported Scope parameter\n");
}

} // namespace SPIRV