forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpirvContext.cpp
More file actions
617 lines (503 loc) · 20.9 KB
/
SpirvContext.cpp
File metadata and controls
617 lines (503 loc) · 20.9 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//===--- SpirvContext.cpp - SPIR-V SpirvContext implementation-------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <algorithm>
#include <tuple>
#include "clang/SPIRV/SpirvContext.h"
#include "clang/SPIRV/SpirvModule.h"
namespace clang {
namespace spirv {
SpirvContext::SpirvContext()
: allocator(), voidType(nullptr), boolType(nullptr), sintTypes({}),
uintTypes({}), floatTypes({}), samplerType(nullptr),
curShaderModelKind(ShaderModelKind::Invalid), majorVersion(0),
minorVersion(0), currentLexicalScope(nullptr) {
voidType = new (this) VoidType;
boolType = new (this) BoolType;
samplerType = new (this) SamplerType;
accelerationStructureTypeNV = new (this) AccelerationStructureTypeNV;
rayQueryTypeKHR = new (this) RayQueryTypeKHR;
}
SpirvContext::~SpirvContext() {
voidType->~VoidType();
boolType->~BoolType();
samplerType->~SamplerType();
accelerationStructureTypeNV->~AccelerationStructureTypeNV();
rayQueryTypeKHR->~RayQueryTypeKHR();
for (auto *sintType : sintTypes)
if (sintType) // sintTypes may contain nullptr
sintType->~IntegerType();
for (auto *uintType : uintTypes)
if (uintType) // uintTypes may contain nullptr
uintType->~IntegerType();
for (auto *floatType : floatTypes)
if (floatType) // floatTypes may contain nullptr
floatType->~FloatType();
for (auto &pair : vecTypes)
for (auto *vecType : pair.second)
if (vecType) // vecTypes may contain nullptr
vecType->~VectorType();
for (auto &pair : matTypes)
for (auto *matType : pair.second)
matType->~MatrixType();
for (auto *arrType : arrayTypes)
arrType->~ArrayType();
for (auto *raType : runtimeArrayTypes)
raType->~RuntimeArrayType();
for (auto *fnType : functionTypes)
fnType->~FunctionType();
for (auto *structType : structTypes)
structType->~StructType();
for (auto *hybridStructType : hybridStructTypes)
hybridStructType->~HybridStructType();
for (auto pair : sampledImageTypes)
pair.second->~SampledImageType();
for (auto *hybridSampledImageType : hybridSampledImageTypes)
hybridSampledImageType->~HybridSampledImageType();
for (auto *imgType : imageTypes)
imgType->~ImageType();
for (auto &pair : pointerTypes)
for (auto &scPtrTypePair : pair.second)
scPtrTypePair.second->~SpirvPointerType();
for (auto *hybridPtrType : hybridPointerTypes)
hybridPtrType->~HybridPointerType();
for (auto &typePair : debugTypes)
typePair.second->releaseMemory();
for (auto &typePair : typeTemplates)
typePair.second->releaseMemory();
for (auto &typePair : typeTemplateParams)
typePair.second->releaseMemory();
for (auto &pair : spirvIntrinsicTypesById) {
assert(pair.second);
pair.second->~SpirvIntrinsicType();
}
for (auto *spirvIntrinsicType : spirvIntrinsicTypes) {
spirvIntrinsicType->~SpirvIntrinsicType();
}
}
inline uint32_t log2ForBitwidth(uint32_t bitwidth) {
assert(bitwidth >= 8 && bitwidth <= 64 && llvm::isPowerOf2_32(bitwidth));
return llvm::Log2_32(bitwidth);
}
const IntegerType *SpirvContext::getSIntType(uint32_t bitwidth) {
auto &type = sintTypes[log2ForBitwidth(bitwidth)];
if (type == nullptr) {
type = new (this) IntegerType(bitwidth, true);
}
return type;
}
const IntegerType *SpirvContext::getUIntType(uint32_t bitwidth) {
auto &type = uintTypes[log2ForBitwidth(bitwidth)];
if (type == nullptr) {
type = new (this) IntegerType(bitwidth, false);
}
return type;
}
const FloatType *SpirvContext::getFloatType(uint32_t bitwidth) {
auto &type = floatTypes[log2ForBitwidth(bitwidth)];
if (type == nullptr) {
type = new (this) FloatType(bitwidth);
}
return type;
}
const VectorType *SpirvContext::getVectorType(const SpirvType *elemType,
uint32_t count) {
// We are certain this should be a scalar type. Otherwise, cast causes an
// assertion failure.
const ScalarType *scalarType = cast<ScalarType>(elemType);
assert(count == 2 || count == 3 || count == 4);
auto found = vecTypes.find(scalarType);
if (found != vecTypes.end()) {
auto &type = found->second[count];
if (type != nullptr)
return type;
} else {
// Make sure to initialize since std::array is "an aggregate type with the
// same semantics as a struct holding a C-style array T[N]".
vecTypes[scalarType] = {};
}
return vecTypes[scalarType][count] = new (this) VectorType(scalarType, count);
}
const SpirvType *SpirvContext::getMatrixType(const SpirvType *elemType,
uint32_t count) {
// We are certain this should be a vector type. Otherwise, cast causes an
// assertion failure.
const VectorType *vecType = cast<VectorType>(elemType);
assert(count == 2 || count == 3 || count == 4);
// In the case of non-floating-point matrices, we represent them as array of
// vectors.
if (!isa<FloatType>(vecType->getElementType())) {
return getArrayType(elemType, count, llvm::None);
}
auto foundVec = matTypes.find(vecType);
if (foundVec != matTypes.end()) {
const auto &matVector = foundVec->second;
// Create a temporary object for finding in the vector.
MatrixType type(vecType, count);
for (const auto *cachedType : matVector)
if (type == *cachedType)
return cachedType;
}
const auto *ptr = new (this) MatrixType(vecType, count);
matTypes[vecType].push_back(ptr);
return ptr;
}
const ImageType *
SpirvContext::getImageType(const ImageType *imageTypeWithUnknownFormat,
spv::ImageFormat format) {
return getImageType(imageTypeWithUnknownFormat->getSampledType(),
imageTypeWithUnknownFormat->getDimension(),
imageTypeWithUnknownFormat->getDepth(),
imageTypeWithUnknownFormat->isArrayedImage(),
imageTypeWithUnknownFormat->isMSImage(),
imageTypeWithUnknownFormat->withSampler(), format);
}
const ImageType *SpirvContext::getImageType(const SpirvType *sampledType,
spv::Dim dim,
ImageType::WithDepth depth,
bool arrayed, bool ms,
ImageType::WithSampler sampled,
spv::ImageFormat format) {
// We are certain this should be a numerical type. Otherwise, cast causes an
// assertion failure.
const NumericalType *elemType = cast<NumericalType>(sampledType);
// Create a temporary object for finding in the set.
ImageType type(elemType, dim, depth, arrayed, ms, sampled, format);
auto found = imageTypes.find(&type);
if (found != imageTypes.end())
return *found;
auto inserted = imageTypes.insert(
new (this) ImageType(elemType, dim, depth, arrayed, ms, sampled, format));
return *(inserted.first);
}
const SampledImageType *
SpirvContext::getSampledImageType(const ImageType *image) {
auto found = sampledImageTypes.find(image);
if (found != sampledImageTypes.end())
return found->second;
return sampledImageTypes[image] = new (this) SampledImageType(image);
}
const HybridSampledImageType *
SpirvContext::getSampledImageType(QualType image) {
const HybridSampledImageType *result =
new (this) HybridSampledImageType(image);
hybridSampledImageTypes.push_back(result);
return result;
}
const ArrayType *
SpirvContext::getArrayType(const SpirvType *elemType, uint32_t elemCount,
llvm::Optional<uint32_t> arrayStride) {
ArrayType type(elemType, elemCount, arrayStride);
auto found = arrayTypes.find(&type);
if (found != arrayTypes.end())
return *found;
auto inserted =
arrayTypes.insert(new (this) ArrayType(elemType, elemCount, arrayStride));
// The return value is an (iterator, bool) pair. The boolean indicates whether
// it was actually added as a new type.
return *(inserted.first);
}
const RuntimeArrayType *
SpirvContext::getRuntimeArrayType(const SpirvType *elemType,
llvm::Optional<uint32_t> arrayStride) {
RuntimeArrayType type(elemType, arrayStride);
auto found = runtimeArrayTypes.find(&type);
if (found != runtimeArrayTypes.end())
return *found;
auto inserted = runtimeArrayTypes.insert(
new (this) RuntimeArrayType(elemType, arrayStride));
return *(inserted.first);
}
const StructType *
SpirvContext::getStructType(llvm::ArrayRef<StructType::FieldInfo> fields,
llvm::StringRef name, bool isReadOnly,
StructInterfaceType interfaceType) {
// We are creating a temporary struct type here for querying whether the
// same type was already created. It is a little bit costly, but we can
// avoid allocating directly from the bump pointer allocator, from which
// then we are unable to reclaim until the allocator itself is destroyed.
StructType type(fields, name, isReadOnly, interfaceType);
auto found = std::find_if(
structTypes.begin(), structTypes.end(),
[&type](const StructType *cachedType) { return type == *cachedType; });
if (found != structTypes.end())
return *found;
structTypes.push_back(
new (this) StructType(fields, name, isReadOnly, interfaceType));
return structTypes.back();
}
const HybridStructType *SpirvContext::getHybridStructType(
llvm::ArrayRef<HybridStructType::FieldInfo> fields, llvm::StringRef name,
bool isReadOnly, StructInterfaceType interfaceType) {
const HybridStructType *result =
new (this) HybridStructType(fields, name, isReadOnly, interfaceType);
hybridStructTypes.push_back(result);
return result;
}
const SpirvPointerType *SpirvContext::getPointerType(const SpirvType *pointee,
spv::StorageClass sc) {
auto foundPointee = pointerTypes.find(pointee);
if (foundPointee != pointerTypes.end()) {
auto &pointeeMap = foundPointee->second;
auto foundSC = pointeeMap.find(sc);
if (foundSC != pointeeMap.end())
return foundSC->second;
}
return pointerTypes[pointee][sc] = new (this) SpirvPointerType(pointee, sc);
}
const HybridPointerType *SpirvContext::getPointerType(QualType pointee,
spv::StorageClass sc) {
const HybridPointerType *result = new (this) HybridPointerType(pointee, sc);
hybridPointerTypes.push_back(result);
return result;
}
const ForwardPointerType *
SpirvContext::getForwardPointerType(QualType pointee) {
assert(hlsl::IsVKBufferPointerType(pointee));
auto foundPointee = forwardPointerTypes.find(pointee);
if (foundPointee != forwardPointerTypes.end()) {
return foundPointee->second;
}
return forwardPointerTypes[pointee] = new (this) ForwardPointerType(pointee);
}
const SpirvPointerType *SpirvContext::getForwardReference(QualType type) {
return forwardReferences[type];
}
void SpirvContext::registerForwardReference(
QualType type, const SpirvPointerType *pointerType) {
assert(pointerType->getStorageClass() ==
spv::StorageClass::PhysicalStorageBuffer);
forwardReferences[type] = pointerType;
}
FunctionType *
SpirvContext::getFunctionType(const SpirvType *ret,
llvm::ArrayRef<const SpirvType *> param) {
// Create a temporary object for finding in the set.
FunctionType type(ret, param);
auto found = functionTypes.find(&type);
if (found != functionTypes.end())
return *found;
auto inserted = functionTypes.insert(new (this) FunctionType(ret, param));
return *inserted.first;
}
const StructType *SpirvContext::getByteAddressBufferType(bool isWritable) {
// Create a uint RuntimeArray.
const auto *raType =
getRuntimeArrayType(getUIntType(32), /* ArrayStride */ 4);
// Create a struct containing the runtime array as its only member.
return getStructType({StructType::FieldInfo(raType, /*fieldIndex*/ 0,
/*name*/ "", /*offset*/ 0)},
isWritable ? "type.RWByteAddressBuffer"
: "type.ByteAddressBuffer",
!isWritable, StructInterfaceType::StorageBuffer);
}
const StructType *SpirvContext::getACSBufferCounterType() {
// Create int32.
const auto *int32Type = getSIntType(32);
// Create a struct containing the integer counter as its only member.
const StructType *type =
getStructType({StructType::FieldInfo(int32Type, /*fieldIndex*/ 0,
"counter", /*offset*/ 0)},
"type.ACSBuffer.counter",
/*isReadOnly*/ false, StructInterfaceType::StorageBuffer);
return type;
}
SpirvDebugType *SpirvContext::getDebugTypeBasic(const SpirvType *spirvType,
llvm::StringRef name,
SpirvConstant *size,
uint32_t encoding) {
// Reuse existing debug type if possible.
if (debugTypes.find(spirvType) != debugTypes.end())
return debugTypes[spirvType];
auto *debugType = new (this) SpirvDebugTypeBasic(name, size, encoding);
debugTypes[spirvType] = debugType;
return debugType;
}
SpirvDebugType *
SpirvContext::getDebugTypeMember(llvm::StringRef name, SpirvDebugType *type,
SpirvDebugSource *source, uint32_t line,
uint32_t column, SpirvDebugInstruction *parent,
uint32_t flags, uint32_t offsetInBits,
uint32_t sizeInBits, const APValue *value) {
// NOTE: Do not search it in debugTypes because it would have the same
// spirvType but has different parent i.e., type composite.
SpirvDebugTypeMember *debugType =
new (this) SpirvDebugTypeMember(name, type, source, line, column, parent,
flags, offsetInBits, sizeInBits, value);
return debugType;
}
SpirvDebugTypeComposite *SpirvContext::getDebugTypeComposite(
const SpirvType *spirvType, llvm::StringRef name, SpirvDebugSource *source,
uint32_t line, uint32_t column, SpirvDebugInstruction *parent,
llvm::StringRef linkageName, uint32_t flags, uint32_t tag) {
// Reuse existing debug type if possible.
auto it = debugTypes.find(spirvType);
if (it != debugTypes.end()) {
assert(it->second != nullptr && isa<SpirvDebugTypeComposite>(it->second));
return dyn_cast<SpirvDebugTypeComposite>(it->second);
}
auto *debugType = new (this) SpirvDebugTypeComposite(
name, source, line, column, parent, linkageName, flags, tag);
debugType->setDebugSpirvType(spirvType);
debugTypes[spirvType] = debugType;
return debugType;
}
SpirvDebugType *SpirvContext::getDebugType(const SpirvType *spirvType) {
auto it = debugTypes.find(spirvType);
if (it != debugTypes.end())
return it->second;
return nullptr;
}
SpirvDebugType *
SpirvContext::getDebugTypeArray(const SpirvType *spirvType,
SpirvDebugInstruction *elemType,
llvm::ArrayRef<uint32_t> elemCount) {
// Reuse existing debug type if possible.
if (debugTypes.find(spirvType) != debugTypes.end())
return debugTypes[spirvType];
auto *eTy = dyn_cast<SpirvDebugType>(elemType);
assert(eTy && "Element type must be a SpirvDebugType.");
auto *debugType = new (this) SpirvDebugTypeArray(eTy, elemCount);
debugTypes[spirvType] = debugType;
return debugType;
}
SpirvDebugType *
SpirvContext::getDebugTypeVector(const SpirvType *spirvType,
SpirvDebugInstruction *elemType,
uint32_t elemCount) {
// Reuse existing debug type if possible.
if (debugTypes.find(spirvType) != debugTypes.end())
return debugTypes[spirvType];
auto *eTy = dyn_cast<SpirvDebugType>(elemType);
assert(eTy && "Element type must be a SpirvDebugType.");
auto *debugType = new (this) SpirvDebugTypeVector(eTy, elemCount);
debugTypes[spirvType] = debugType;
return debugType;
}
SpirvDebugType *
SpirvContext::getDebugTypeMatrix(const SpirvType *spirvType,
SpirvDebugInstruction *vectorType,
uint32_t vectorCount) {
// Reuse existing debug type if possible.
if (debugTypes.find(spirvType) != debugTypes.end())
return debugTypes[spirvType];
auto *eTy = dyn_cast<SpirvDebugTypeVector>(vectorType);
assert(eTy && "Element type must be a SpirvDebugTypeVector.");
auto *debugType = new (this) SpirvDebugTypeMatrix(eTy, vectorCount);
debugTypes[spirvType] = debugType;
return debugType;
}
SpirvDebugType *
SpirvContext::getDebugTypeFunction(const SpirvType *spirvType, uint32_t flags,
SpirvDebugType *ret,
llvm::ArrayRef<SpirvDebugType *> params) {
// Reuse existing debug type if possible.
if (debugTypes.find(spirvType) != debugTypes.end())
return debugTypes[spirvType];
auto *debugType = new (this) SpirvDebugTypeFunction(flags, ret, params);
debugTypes[spirvType] = debugType;
return debugType;
}
SpirvDebugTypeTemplate *SpirvContext::createDebugTypeTemplate(
const ClassTemplateSpecializationDecl *templateType,
SpirvDebugInstruction *target,
const llvm::SmallVector<SpirvDebugTypeTemplateParameter *, 2> ¶ms) {
auto *tempTy = getDebugTypeTemplate(templateType);
if (tempTy != nullptr)
return tempTy;
tempTy = new (this) SpirvDebugTypeTemplate(target, params);
typeTemplates[templateType] = tempTy;
return tempTy;
}
SpirvDebugTypeTemplate *SpirvContext::getDebugTypeTemplate(
const ClassTemplateSpecializationDecl *templateType) {
auto it = typeTemplates.find(templateType);
if (it != typeTemplates.end())
return it->second;
return nullptr;
}
SpirvDebugTypeTemplateParameter *SpirvContext::createDebugTypeTemplateParameter(
const TemplateArgument *templateArg, llvm::StringRef name,
SpirvDebugType *type, SpirvInstruction *value, SpirvDebugSource *source,
uint32_t line, uint32_t column) {
auto *param = getDebugTypeTemplateParameter(templateArg);
if (param != nullptr)
return param;
param = new (this)
SpirvDebugTypeTemplateParameter(name, type, value, source, line, column);
typeTemplateParams[templateArg] = param;
return param;
}
SpirvDebugTypeTemplateParameter *SpirvContext::getDebugTypeTemplateParameter(
const TemplateArgument *templateArg) {
auto it = typeTemplateParams.find(templateArg);
if (it != typeTemplateParams.end())
return it->second;
return nullptr;
}
void SpirvContext::pushDebugLexicalScope(RichDebugInfo *info,
SpirvDebugInstruction *scope) {
assert((isa<SpirvDebugLexicalBlock>(scope) ||
isa<SpirvDebugFunction>(scope) ||
isa<SpirvDebugCompilationUnit>(scope) ||
isa<SpirvDebugTypeComposite>(scope)) &&
"Given scope is not a lexical scope");
currentLexicalScope = scope;
info->scopeStack.push_back(scope);
}
void SpirvContext::moveDebugTypesToModule(SpirvModule *module) {
for (const auto &typePair : debugTypes) {
module->addDebugInfo(typePair.second);
if (auto *composite = dyn_cast<SpirvDebugTypeComposite>(typePair.second)) {
for (auto *member : composite->getMembers()) {
module->addDebugInfo(member);
}
}
}
for (const auto &typePair : typeTemplates) {
module->addDebugInfo(typePair.second);
}
for (const auto &typePair : typeTemplateParams) {
module->addDebugInfo(typePair.second);
}
debugTypes.clear();
typeTemplates.clear();
typeTemplateParams.clear();
}
const SpirvIntrinsicType *SpirvContext::getOrCreateSpirvIntrinsicType(
unsigned typeId, unsigned typeOpCode,
llvm::ArrayRef<SpvIntrinsicTypeOperand> operands) {
if (spirvIntrinsicTypesById[typeId] == nullptr) {
spirvIntrinsicTypesById[typeId] =
new (this) SpirvIntrinsicType(typeOpCode, operands);
}
return spirvIntrinsicTypesById[typeId];
}
const SpirvIntrinsicType *SpirvContext::getOrCreateSpirvIntrinsicType(
unsigned typeOpCode, llvm::ArrayRef<SpvIntrinsicTypeOperand> operands) {
SpirvIntrinsicType type(typeOpCode, operands);
auto found =
std::find_if(spirvIntrinsicTypes.begin(), spirvIntrinsicTypes.end(),
[&type](const SpirvIntrinsicType *cachedType) {
return type == *cachedType;
});
if (found != spirvIntrinsicTypes.end())
return *found;
spirvIntrinsicTypes.push_back(new (this)
SpirvIntrinsicType(typeOpCode, operands));
return spirvIntrinsicTypes.back();
}
SpirvIntrinsicType *
SpirvContext::getCreatedSpirvIntrinsicType(unsigned typeId) {
if (spirvIntrinsicTypesById.find(typeId) == spirvIntrinsicTypesById.end()) {
return nullptr;
}
return spirvIntrinsicTypesById[typeId];
}
} // end namespace spirv
} // end namespace clang