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
|
// slang-ir-spirv-legalize.cpp
#include "slang-ir-spirv-legalize.h"
#include "slang-ir-glsl-legalize.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"
#include "slang-emit-base.h"
#include "slang-glsl-extension-tracker.h"
namespace Slang
{
//
// Legalization of IR for direct SPIRV emit.
//
struct SPIRVLegalizationContext : public SourceEmitterBase
{
SPIRVEmitSharedContext* m_sharedContext;
IRModule* m_module;
// We will use a single work list of instructions that need
// to be considered for specialization or simplification,
// whether generic, existential, etc.
//
OrderedHashSet<IRInst*> workList;
void addToWorkList(IRInst* inst)
{
if (workList.Add(inst))
{
addUsersToWorkList(inst);
}
}
void addUsersToWorkList(IRInst* inst)
{
for (auto use = inst->firstUse; use; use = use->nextUse)
{
auto user = use->getUser();
addToWorkList(user);
}
}
SPIRVLegalizationContext(SPIRVEmitSharedContext* sharedContext, IRModule* module)
: m_sharedContext(sharedContext), m_module(module)
{
}
void processGlobalParam(IRGlobalParam* inst)
{
// If the global param is not a pointer type, make it so and insert explicit load insts.
auto ptrType = as<IRPtrTypeBase>(inst->getDataType());
if (!ptrType)
{
SpvStorageClass storageClass = SpvStorageClassPrivate;
// Figure out storage class based on var layout.
if (auto layout = getVarLayout(inst))
{
if (auto systemValueAttr = layout->findAttr<IRSystemValueSemanticAttr>())
{
String semanticName = systemValueAttr->getName();
semanticName = semanticName.toLower();
if (semanticName == "sv_dispatchthreadid")
{
storageClass = SpvStorageClassInput;
}
}
}
// Make a pointer type of storageClass.
IRBuilder builder(m_sharedContext->m_sharedIRBuilder);
builder.setInsertBefore(inst);
ptrType = builder.getPtrType(kIROp_PtrType, inst->getFullType(), storageClass);
inst->setFullType(ptrType);
// Insert an explicit load at each use site.
List<IRUse*> uses;
for (auto use = inst->firstUse; use; use = use->nextUse)
{
uses.add(use);
}
for (auto use : uses)
{
builder.setInsertBefore(use->getUser());
auto loadedValue = builder.emitLoad(inst);
use->set(loadedValue);
}
}
processGlobalVar(inst);
}
void processGlobalVar(IRInst* inst)
{
auto oldPtrType = as<IRPtrTypeBase>(inst->getDataType());
if (!oldPtrType)
return;
// If the pointer type is already qualified with address spaces (such as
// lowered pointer type from a `HLSLStructuredBufferType`), make no
// further modifications.
if (oldPtrType->hasAddressSpace())
{
addUsersToWorkList(inst);
return;
}
auto varLayout = getVarLayout(inst);
if (!varLayout)
return;
SpvStorageClass storageClass = SpvStorageClassPrivate;
for (auto rr : varLayout->getOffsetAttrs())
{
switch (rr->getResourceKind())
{
case LayoutResourceKind::Uniform:
case LayoutResourceKind::ShaderResource:
case LayoutResourceKind::DescriptorTableSlot:
storageClass = SpvStorageClassUniform;
break;
case LayoutResourceKind::VaryingInput:
storageClass = SpvStorageClassInput;
break;
case LayoutResourceKind::VaryingOutput:
storageClass = SpvStorageClassOutput;
break;
case LayoutResourceKind::UnorderedAccess:
storageClass = SpvStorageClassStorageBuffer;
break;
case LayoutResourceKind::PushConstantBuffer:
storageClass = SpvStorageClassPushConstant;
break;
default:
break;
}
}
auto rate = inst->getRate();
if (as<IRGroupSharedRate>(rate))
{
storageClass = SpvStorageClassWorkgroup;
}
IRBuilder builder(m_sharedContext->m_sharedIRBuilder);
builder.setInsertBefore(inst);
auto newPtrType =
builder.getPtrType(oldPtrType->getOp(), oldPtrType->getValueType(), storageClass);
inst->setFullType(newPtrType);
addUsersToWorkList(inst);
return;
}
void processCall(IRCall* inst)
{
auto funcValue = inst->getOperand(0);
if (auto targetIntrinsic = Slang::findBestTargetIntrinsicDecoration(
funcValue, m_sharedContext->m_targetRequest->getTargetCaps()))
{
SpvSnippet* snippet = m_sharedContext->getParsedSpvSnippet(targetIntrinsic);
if (!snippet)
return;
if (snippet->resultStorageClass != SpvStorageClassMax)
{
auto ptrType = as<IRPtrTypeBase>(inst->getDataType());
if (!ptrType)
return;
IRBuilder builder(m_sharedContext->m_sharedIRBuilder);
builder.setInsertBefore(inst);
auto qualPtrType = builder.getPtrType(
ptrType->getOp(), ptrType->getValueType(), snippet->resultStorageClass);
List<IRInst*> args;
for (UInt i = 0; i < inst->getArgCount(); i++)
args.add(inst->getArg(i));
auto newCall = builder.emitCallInst(qualPtrType, funcValue, args);
inst->replaceUsesWith(newCall);
inst->removeAndDeallocate();
addUsersToWorkList(newCall);
}
}
}
void processGetElementPtr(IRGetElementPtr* inst)
{
if (auto ptrType = as<IRPtrTypeBase>(inst->getBase()->getDataType()))
{
if (!ptrType->hasAddressSpace())
return;
auto oldResultType = as<IRPtrTypeBase>(inst->getDataType());
if (oldResultType->getAddressSpace() != ptrType->getAddressSpace())
{
IRBuilder builder(m_sharedContext->m_sharedIRBuilder);
builder.setInsertBefore(inst);
auto newPtrType = builder.getPtrType(
oldResultType->getOp(),
oldResultType->getValueType(),
ptrType->getAddressSpace());
auto newInst =
builder.emitElementAddress(newPtrType, inst->getBase(), inst->getIndex());
inst->replaceUsesWith(newInst);
inst->removeAndDeallocate();
addUsersToWorkList(newInst);
}
}
}
void processFieldAddress(IRFieldAddress* inst)
{
if (auto ptrType = as<IRPtrTypeBase>(inst->getBase()->getDataType()))
{
if (!ptrType->hasAddressSpace())
return;
auto oldResultType = as<IRPtrTypeBase>(inst->getDataType());
if (oldResultType->getAddressSpace() != ptrType->getAddressSpace())
{
IRBuilder builder(m_sharedContext->m_sharedIRBuilder);
builder.setInsertBefore(inst);
auto newPtrType = builder.getPtrType(
oldResultType->getOp(),
oldResultType->getValueType(),
ptrType->getAddressSpace());
auto newInst =
builder.emitFieldAddress(newPtrType, inst->getBase(), inst->getField());
inst->replaceUsesWith(newInst);
inst->removeAndDeallocate();
addUsersToWorkList(newInst);
}
}
}
void processStructuredBufferType(IRHLSLStructuredBufferTypeBase* inst)
{
IRBuilder builder(m_sharedContext->m_sharedIRBuilder);
builder.setInsertBefore(inst);
auto arrayType = builder.getUnsizedArrayType(inst->getElementType());
auto structType = builder.createStructType();
auto arrayKey = builder.createStructKey();
builder.createStructField(structType, arrayKey, arrayType);
auto ptrType = builder.getPtrType(kIROp_PtrType, structType, SpvStorageClassStorageBuffer);
StringBuilder nameSb;
switch (inst->getOp())
{
case kIROp_HLSLRWStructuredBufferType:
nameSb << "RWStructuredBuffer";
break;
case kIROp_HLSLAppendStructuredBufferType:
nameSb << "AppendStructuredBuffer";
break;
case kIROp_HLSLConsumeStructuredBufferType:
nameSb << "ConsumeStructuredBuffer";
break;
default:
nameSb << "StructuredBuffer";
break;
}
builder.addNameHintDecoration(structType, nameSb.getUnownedSlice());
builder.addDecoration(structType, kIROp_SPIRVBufferBlockDecoration);
inst->replaceUsesWith(ptrType);
inst->removeAndDeallocate();
addUsersToWorkList(ptrType);
}
void processModule()
{
addToWorkList(m_module->getModuleInst());
while (workList.Count() != 0)
{
IRInst* inst = workList.getLast();
workList.removeLast();
switch (inst->getOp())
{
case kIROp_GlobalParam:
processGlobalParam(as<IRGlobalParam>(inst));
break;
case kIROp_GlobalVar:
processGlobalVar(as<IRGlobalVar>(inst));
break;
case kIROp_Call:
processCall(as<IRCall>(inst));
break;
case kIROp_getElementPtr:
processGetElementPtr(as<IRGetElementPtr>(inst));
break;
case kIROp_FieldAddress:
processFieldAddress(as<IRFieldAddress>(inst));
break;
case kIROp_HLSLStructuredBufferType:
case kIROp_HLSLRWStructuredBufferType:
processStructuredBufferType(as<IRHLSLStructuredBufferTypeBase>(inst));
break;
default:
for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
{
addToWorkList(child);
}
break;
}
}
}
};
void legalizeSPIRV(SPIRVEmitSharedContext* sharedContext, IRModule* module)
{
SPIRVLegalizationContext context(sharedContext, module);
context.processModule();
}
void legalizeIRForSPIRV(
SPIRVEmitSharedContext* context,
IRModule* module,
const List<IRFunc*>& entryPoints,
DiagnosticSink* sink)
{
SLANG_UNUSED(sink);
GLSLExtensionTracker extensionTracker;
legalizeEntryPointsForGLSL(module->getSession(), module, entryPoints, sink, &extensionTracker);
legalizeSPIRV(context, module);
}
} // namespace Slang
|