forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDxilTypeSystem.h
More file actions
407 lines (336 loc) · 13.3 KB
/
DxilTypeSystem.h
File metadata and controls
407 lines (336 loc) · 13.3 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
///////////////////////////////////////////////////////////////////////////////
// //
// DxilTypeSystem.h //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// DXIL extension to LLVM type system. //
// //
///////////////////////////////////////////////////////////////////////////////
#pragma once
#include "dxc/DXIL/DxilCompType.h"
#include "dxc/DXIL/DxilConstants.h"
#include "dxc/DXIL/DxilInterpolationMode.h"
#include "dxc/DXIL/DxilResourceProperties.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/StringRef.h"
#include <memory>
#include <string>
#include <vector>
namespace llvm {
class LLVMContext;
class Module;
class Function;
class MDNode;
class Type;
class StructType;
} // namespace llvm
namespace hlsl {
enum class MatrixOrientation {
Undefined = 0,
RowMajor,
ColumnMajor,
LastEntry
};
struct DxilMatrixAnnotation {
unsigned Rows;
unsigned Cols;
MatrixOrientation Orientation;
DxilMatrixAnnotation();
};
/// Use this class to represent type annotation for structure field.
class DxilFieldAnnotation {
public:
DxilFieldAnnotation();
bool IsPrecise() const;
void SetPrecise(bool b = true);
bool HasMatrixAnnotation() const;
const DxilMatrixAnnotation &GetMatrixAnnotation() const;
void SetMatrixAnnotation(const DxilMatrixAnnotation &MA);
unsigned GetVectorSize() const;
void SetVectorSize(unsigned size);
// Currently, ResourceProperties is only used to capture resource type
// information during CodeGen for the annotate handle generated during
// AddOpcodeParamForIntrinsic.
bool HasResourceProperties() const;
const DxilResourceProperties &GetResourceProperties() const;
void SetResourceProperties(const DxilResourceProperties &RP);
bool HasCBufferOffset() const;
unsigned GetCBufferOffset() const;
void SetCBufferOffset(unsigned Offset);
bool HasCompType() const;
const CompType &GetCompType() const;
void SetCompType(CompType::Kind kind);
bool HasSemanticString() const;
const std::string &GetSemanticString() const;
llvm::StringRef GetSemanticStringRef() const;
void SetSemanticString(const std::string &SemString);
bool HasInterpolationMode() const;
const InterpolationMode &GetInterpolationMode() const;
void SetInterpolationMode(const InterpolationMode &IM);
bool HasFieldName() const;
const std::string &GetFieldName() const;
void SetFieldName(const std::string &FieldName);
bool IsCBVarUsed() const;
void SetCBVarUsed(bool used);
bool HasBitFields() const;
const std::vector<DxilFieldAnnotation> &GetBitFields() const;
void SetBitFields(const std::vector<DxilFieldAnnotation> &Fields);
bool HasBitFieldWidth() const;
unsigned GetBitFieldWidth() const;
void SetBitFieldWidth(const unsigned BitWidth);
private:
bool m_bPrecise;
CompType m_CompType;
DxilMatrixAnnotation m_Matrix;
DxilResourceProperties m_ResourceProps;
unsigned m_CBufferOffset;
std::string m_Semantic;
InterpolationMode m_InterpMode;
std::string m_FieldName;
bool m_bCBufferVarUsed; // true if this field represents a top level variable
// in CB structure, and it is used.
std::vector<DxilFieldAnnotation> m_BitFields;
unsigned m_BitFieldWidth; // For bit field. 0 means not bitfield.
unsigned m_VectorSize;
};
class DxilTemplateArgAnnotation {
public:
DxilTemplateArgAnnotation();
bool IsType() const;
const llvm::Type *GetType() const;
void SetType(const llvm::Type *pType);
bool IsIntegral() const;
int64_t GetIntegral() const;
void SetIntegral(int64_t i64);
private:
const llvm::Type *m_Type;
int64_t m_Integral;
};
/// Use this class to represent LLVM structure annotation.
class DxilStructAnnotation {
friend class DxilTypeSystem;
public:
unsigned GetNumFields() const;
DxilFieldAnnotation &GetFieldAnnotation(unsigned FieldIdx);
const DxilFieldAnnotation &GetFieldAnnotation(unsigned FieldIdx) const;
const llvm::StructType *GetStructType() const;
void SetStructType(const llvm::StructType *Ty);
unsigned GetCBufferSize() const;
void SetCBufferSize(unsigned size);
void MarkEmptyStruct();
bool IsEmptyStruct();
// Since resources and target types don't take real space,
// IsEmptyBesidesResourcesAndTargetTypes() determines if the structure is
// empty or contains only resources or target types.
bool IsEmptyBesidesResourcesAndTargetTypes();
bool ContainsResources() const;
bool ContainsTargetTypes() const;
// For template args, GetNumTemplateArgs() will return 0 if not a template
unsigned GetNumTemplateArgs() const;
void SetNumTemplateArgs(unsigned count);
DxilTemplateArgAnnotation &GetTemplateArgAnnotation(unsigned argIdx);
const DxilTemplateArgAnnotation &
GetTemplateArgAnnotation(unsigned argIdx) const;
private:
const llvm::StructType *m_pStructType = nullptr;
std::vector<DxilFieldAnnotation> m_FieldAnnotations;
unsigned m_CBufferSize = 0; // The size of struct if inside constant buffer.
std::vector<DxilTemplateArgAnnotation> m_TemplateAnnotations;
// m_ResourcesContained property not stored to metadata
void SetContainsResources();
// HasResources::Only will be set on MarkEmptyStruct() when HasResources::True
enum class HasResources {
True,
False,
Only
} m_ResourcesContained = HasResources::False;
void SetContainsTargetTypes();
// HasTargetTypes::Only will be set on MarkEmptyStruct() when
// HasTargetTypes::True
enum class HasTargetTypes {
True,
False,
Only
} m_TargetTypesContained = HasTargetTypes::False;
};
/// Use this class to represent type annotation for DXR payload field.
class DxilPayloadFieldAnnotation {
public:
static unsigned
GetBitOffsetForShaderStage(DXIL::PayloadAccessShaderStage shaderStage);
DxilPayloadFieldAnnotation() = default;
bool HasCompType() const;
const CompType &GetCompType() const;
void SetCompType(CompType::Kind kind);
uint32_t GetPayloadFieldQualifierMask() const;
void SetPayloadFieldQualifierMask(uint32_t fieldBitmask);
void AddPayloadFieldQualifier(DXIL::PayloadAccessShaderStage shaderStage,
DXIL::PayloadAccessQualifier qualifier);
DXIL::PayloadAccessQualifier
GetPayloadFieldQualifier(DXIL::PayloadAccessShaderStage shaderStage) const;
bool HasAnnotations() const;
private:
CompType m_CompType;
unsigned m_bitmask = 0;
};
/// Use this class to represent DXR payload structures.
class DxilPayloadAnnotation {
friend class DxilTypeSystem;
public:
unsigned GetNumFields() const;
DxilPayloadFieldAnnotation &GetFieldAnnotation(unsigned FieldIdx);
const DxilPayloadFieldAnnotation &GetFieldAnnotation(unsigned FieldIdx) const;
const llvm::StructType *GetStructType() const;
void SetStructType(const llvm::StructType *Ty);
private:
const llvm::StructType *m_pStructType;
std::vector<DxilPayloadFieldAnnotation> m_FieldAnnotations;
};
enum class DxilParamInputQual {
In,
Out,
Inout,
InputPatch,
OutputPatch,
OutStream0,
OutStream1,
OutStream2,
OutStream3,
InputPrimitive,
OutIndices,
OutVertices,
OutPrimitives,
InPayload,
NodeIO
};
/// Use this class to represent type annotation for function parameter.
class DxilParameterAnnotation : public DxilFieldAnnotation {
public:
DxilParameterAnnotation();
DxilParamInputQual GetParamInputQual() const;
void SetParamInputQual(DxilParamInputQual qual);
const std::vector<unsigned> &GetSemanticIndexVec() const;
void SetSemanticIndexVec(const std::vector<unsigned> &Vec);
void AppendSemanticIndex(unsigned SemIdx);
bool IsParamInputQualNode();
private:
DxilParamInputQual m_inputQual;
std::vector<unsigned> m_semanticIndex;
};
/// Use this class to represent LLVM function annotation.
class DxilFunctionAnnotation {
friend class DxilTypeSystem;
public:
unsigned GetNumParameters() const;
DxilParameterAnnotation &GetParameterAnnotation(unsigned ParamIdx);
const DxilParameterAnnotation &
GetParameterAnnotation(unsigned ParamIdx) const;
const llvm::Function *GetFunction() const;
DxilParameterAnnotation &GetRetTypeAnnotation();
const DxilParameterAnnotation &GetRetTypeAnnotation() const;
bool ContainsResourceArgs() { return m_bContainsResourceArgs; }
private:
const llvm::Function *m_pFunction;
std::vector<DxilParameterAnnotation> m_parameterAnnotations;
DxilParameterAnnotation m_retTypeAnnotation;
// m_bContainsResourceArgs property not stored to metadata
void SetContainsResourceArgs() { m_bContainsResourceArgs = true; }
bool m_bContainsResourceArgs = false;
};
/// Use this class to represent structure type annotations in HL and DXIL.
class DxilTypeSystem {
public:
using StructAnnotationMap =
llvm::MapVector<const llvm::StructType *,
std::unique_ptr<DxilStructAnnotation>>;
using PayloadAnnotationMap =
llvm::MapVector<const llvm::StructType *,
std::unique_ptr<DxilPayloadAnnotation>>;
using FunctionAnnotationMap =
llvm::MapVector<const llvm::Function *,
std::unique_ptr<DxilFunctionAnnotation>>;
DxilTypeSystem(llvm::Module *pModule);
DxilStructAnnotation *AddStructAnnotation(const llvm::StructType *pStructType,
unsigned numTemplateArgs = 0);
void FinishStructAnnotation(DxilStructAnnotation &SA);
DxilStructAnnotation *
GetStructAnnotation(const llvm::StructType *pStructType);
const DxilStructAnnotation *
GetStructAnnotation(const llvm::StructType *pStructType) const;
void EraseStructAnnotation(const llvm::StructType *pStructType);
void EraseUnusedStructAnnotations();
StructAnnotationMap &GetStructAnnotationMap();
const StructAnnotationMap &GetStructAnnotationMap() const;
DxilPayloadAnnotation *
AddPayloadAnnotation(const llvm::StructType *pStructType);
DxilPayloadAnnotation *
GetPayloadAnnotation(const llvm::StructType *pStructType);
const DxilPayloadAnnotation *
GetPayloadAnnotation(const llvm::StructType *pStructType) const;
void ErasePayloadAnnotation(const llvm::StructType *pStructType);
PayloadAnnotationMap &GetPayloadAnnotationMap();
const PayloadAnnotationMap &GetPayloadAnnotationMap() const;
DxilFunctionAnnotation *
AddFunctionAnnotation(const llvm::Function *pFunction);
void FinishFunctionAnnotation(DxilFunctionAnnotation &FA);
DxilFunctionAnnotation *
GetFunctionAnnotation(const llvm::Function *pFunction);
const DxilFunctionAnnotation *
GetFunctionAnnotation(const llvm::Function *pFunction) const;
void EraseFunctionAnnotation(const llvm::Function *pFunction);
FunctionAnnotationMap &GetFunctionAnnotationMap();
// Utility methods to create stand-alone SNORM and UNORM.
// We may want to move them to a more centralized place for most utilities.
llvm::StructType *GetSNormF32Type(unsigned NumComps);
llvm::StructType *GetUNormF32Type(unsigned NumComps);
// Methods to copy annotation from another DxilTypeSystem.
void CopyTypeAnnotation(const llvm::Type *Ty, const DxilTypeSystem &src);
void CopyFunctionAnnotation(const llvm::Function *pDstFunction,
const llvm::Function *pSrcFunction,
const DxilTypeSystem &src);
bool UseMinPrecision();
void SetMinPrecision(bool bMinPrecision);
// Determines whether type is a resource or contains a resource
bool IsResourceContained(llvm::Type *Ty);
// Determines whether type is a target type or contains a target type
bool IsTargetTypeContained(llvm::Type *Ty);
private:
llvm::Module *m_pModule;
StructAnnotationMap m_StructAnnotations;
PayloadAnnotationMap m_PayloadAnnotations;
FunctionAnnotationMap m_FunctionAnnotations;
DXIL::LowPrecisionMode m_LowPrecisionMode;
llvm::StructType *GetNormFloatType(CompType CT, unsigned NumComps);
};
DXIL::SigPointKind SigPointFromInputQual(DxilParamInputQual Q,
DXIL::ShaderKind SK, bool isPC);
void RemapObsoleteSemantic(DxilParameterAnnotation ¶mInfo,
DXIL::SigPointKind sigPoint,
llvm::LLVMContext &Context);
class DxilStructTypeIterator {
private:
llvm::StructType *STy;
DxilStructAnnotation *SAnnotation;
unsigned index;
public:
using iterator_category = std::input_iterator_tag;
using value_type = std::pair<llvm::Type *, DxilFieldAnnotation *>;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
using reference = value_type &;
DxilStructTypeIterator(llvm::StructType *sTy,
DxilStructAnnotation *sAnnotation, unsigned idx = 0);
// prefix
DxilStructTypeIterator &operator++();
// postfix
DxilStructTypeIterator operator++(int);
bool operator==(DxilStructTypeIterator iter);
bool operator!=(DxilStructTypeIterator iter);
std::pair<llvm::Type *, DxilFieldAnnotation *> operator*();
};
DxilStructTypeIterator begin(llvm::StructType *STy,
DxilStructAnnotation *SAnno);
DxilStructTypeIterator end(llvm::StructType *STy, DxilStructAnnotation *SAnno);
} // namespace hlsl