-
Notifications
You must be signed in to change notification settings - Fork 851
Expand file tree
/
Copy pathSpirvModule.cpp
More file actions
380 lines (317 loc) · 10.9 KB
/
SpirvModule.cpp
File metadata and controls
380 lines (317 loc) · 10.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
//===--- SpirvModule.cpp - SPIR-V Module Implementation ----------*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/SPIRV/SpirvModule.h"
#include "clang/SPIRV/SpirvFunction.h"
#include "clang/SPIRV/SpirvVisitor.h"
namespace clang {
namespace spirv {
SpirvModule::SpirvModule()
: capabilities({}), extensions({}), extInstSets({}), memoryModel(nullptr),
entryPoints({}), executionModes({}), moduleProcesses({}), decorations({}),
constants({}), undefs({}), variables({}), functions({}),
debugInstructions({}), perVertexInterp(false) {}
SpirvModule::~SpirvModule() {
for (auto *cap : capabilities)
cap->releaseMemory();
for (auto *ext : extensions)
ext->releaseMemory();
for (auto *set : extInstSets)
set->releaseMemory();
if (memoryModel)
memoryModel->releaseMemory();
for (auto *entry : entryPoints)
entry->releaseMemory();
for (auto *exec : executionModes)
exec->releaseMemory();
for (auto *str : constStrings)
str->releaseMemory();
for (auto *d : sources)
d->releaseMemory();
for (auto *mp : moduleProcesses)
mp->releaseMemory();
for (auto *decoration : decorations)
decoration->releaseMemory();
for (auto *constant : constants)
constant->releaseMemory();
for (auto *undef : undefs)
undef->releaseMemory();
for (auto *var : variables)
var->releaseMemory();
for (auto *di : debugInstructions)
di->releaseMemory();
for (auto *f : allFunctions)
f->~SpirvFunction();
}
bool SpirvModule::invokeVisitor(Visitor *visitor, bool reverseOrder) {
// Note: It is debatable whether reverse order of visiting the module should
// reverse everything in this method. For the time being, we just reverse the
// order of the function visitors, and keeping everything else the same.
// For example, it is not clear what the value would be of vising the last
// function first. We can update this methodology if needed.
if (!visitor->visit(this, Visitor::Phase::Init))
return false;
if (reverseOrder) {
// Reverse order of a SPIR-V module.
// Our transformations do not cross function bounaries, therefore the order
// of visiting functions is not important.
for (auto iter = functions.rbegin(); iter != functions.rend(); ++iter) {
auto *fn = *iter;
if (!fn->invokeVisitor(visitor, reverseOrder))
return false;
}
for (auto iter = debugInstructions.rbegin();
iter != debugInstructions.rend(); ++iter) {
auto *debugInstruction = *iter;
if (!debugInstruction->invokeVisitor(visitor))
return false;
}
for (auto iter = variables.rbegin(); iter != variables.rend(); ++iter) {
auto *var = *iter;
if (!var->invokeVisitor(visitor))
return false;
}
for (auto iter = constants.rbegin(); iter != constants.rend(); ++iter) {
auto *constant = *iter;
if (!constant->invokeVisitor(visitor))
return false;
}
for (auto iter = undefs.rbegin(); iter != undefs.rend(); ++iter) {
auto *undef = *iter;
if (!undef->invokeVisitor(visitor))
return false;
}
// Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
// manual indexing.
for (auto decorIndex = decorations.size(); decorIndex > 0; --decorIndex) {
auto *decoration = decorations[decorIndex - 1];
if (!decoration->invokeVisitor(visitor))
return false;
}
for (auto iter = moduleProcesses.rbegin(); iter != moduleProcesses.rend();
++iter) {
auto *moduleProcess = *iter;
if (!moduleProcess->invokeVisitor(visitor))
return false;
}
if (!sources.empty())
for (auto iter = sources.rbegin(); iter != sources.rend(); ++iter) {
auto *source = *iter;
if (!source->invokeVisitor(visitor))
return false;
}
for (auto iter = constStrings.rbegin(); iter != constStrings.rend();
++iter) {
if (!(*iter)->invokeVisitor(visitor))
return false;
}
for (auto iter = executionModes.rbegin(); iter != executionModes.rend();
++iter) {
auto *execMode = *iter;
if (!execMode->invokeVisitor(visitor))
return false;
}
for (auto iter = entryPoints.rbegin(); iter != entryPoints.rend(); ++iter) {
auto *entryPoint = *iter;
if (!entryPoint->invokeVisitor(visitor))
return false;
}
if (!memoryModel->invokeVisitor(visitor))
return false;
for (auto iter = extInstSets.rbegin(); iter != extInstSets.rend(); ++iter) {
auto *extInstSet = *iter;
if (!extInstSet->invokeVisitor(visitor))
return false;
}
// Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
// manual indexing.
for (auto extIndex = extensions.size(); extIndex > 0; --extIndex) {
auto *extension = extensions[extIndex - 1];
if (!extension->invokeVisitor(visitor))
return false;
}
// Since SetVector doesn't have 'rbegin()' and 'rend()' methods, we use
// manual indexing.
for (auto capIndex = capabilities.size(); capIndex > 0; --capIndex) {
auto *capability = capabilities[capIndex - 1];
if (!capability->invokeVisitor(visitor))
return false;
}
}
// Traverse the regular order of a SPIR-V module.
else {
for (auto *cap : capabilities)
if (!cap->invokeVisitor(visitor))
return false;
for (auto ext : extensions)
if (!ext->invokeVisitor(visitor))
return false;
for (auto extInstSet : extInstSets)
if (!extInstSet->invokeVisitor(visitor))
return false;
if (!memoryModel->invokeVisitor(visitor))
return false;
for (auto entryPoint : entryPoints)
if (!entryPoint->invokeVisitor(visitor))
return false;
for (auto execMode : executionModes)
if (!execMode->invokeVisitor(visitor))
return false;
for (auto *str : constStrings)
if (!str->invokeVisitor(visitor))
return false;
if (!sources.empty())
for (auto *source : sources)
if (!source->invokeVisitor(visitor))
return false;
for (auto moduleProcess : moduleProcesses)
if (!moduleProcess->invokeVisitor(visitor))
return false;
for (auto decoration : decorations)
if (!decoration->invokeVisitor(visitor))
return false;
for (auto constant : constants)
if (!constant->invokeVisitor(visitor))
return false;
for (auto undef : undefs)
if (!undef->invokeVisitor(visitor))
return false;
for (auto var : variables)
if (!var->invokeVisitor(visitor))
return false;
for (size_t i = 0; i < debugInstructions.size(); i++)
if (!debugInstructions[i]->invokeVisitor(visitor))
return false;
for (auto fn : functions)
if (!fn->invokeVisitor(visitor, reverseOrder))
return false;
}
if (!visitor->visit(this, Visitor::Phase::Done))
return false;
return true;
}
void SpirvModule::addFunctionToListOfSortedModuleFunctions(SpirvFunction *fn) {
assert(fn && "cannot add null function to the module");
functions.push_back(fn);
}
void SpirvModule::addFunction(SpirvFunction *fn) {
assert(fn && "cannot add null function to the module");
allFunctions.insert(fn);
}
bool SpirvModule::addCapability(SpirvCapability *cap) {
assert(cap && "cannot add null capability to the module");
return capabilities.insert(cap);
}
bool SpirvModule::hasCapability(SpirvCapability &cap) {
return capabilities.count(&cap) != 0;
}
void SpirvModule::setMemoryModel(SpirvMemoryModel *model) {
assert(model && "cannot set a null memory model");
if (memoryModel)
memoryModel->releaseMemory();
memoryModel = model;
}
bool SpirvModule::promoteAddressingModel(spv::AddressingModel addrModel) {
assert(memoryModel && "base memory model must be set first");
auto getPriority = [](spv::AddressingModel am) -> int {
switch (am) {
default:
assert(false && "unknown addressing model");
return 0;
case spv::AddressingModel::Logical:
return 0;
case spv::AddressingModel::Physical32:
return 1;
case spv::AddressingModel::Physical64:
return 2;
case spv::AddressingModel::PhysicalStorageBuffer64:
return 3;
}
};
int current = getPriority(memoryModel->getAddressingModel());
int pending = getPriority(addrModel);
if (pending > current) {
memoryModel->setAddressingModel(addrModel);
return true;
} else {
return false;
}
}
void SpirvModule::addEntryPoint(SpirvEntryPoint *ep) {
assert(ep && "cannot add null as an entry point");
entryPoints.push_back(ep);
}
SpirvExecutionModeBase *
SpirvModule::findExecutionMode(SpirvFunction *entryPoint,
spv::ExecutionMode em) {
for (SpirvExecutionModeBase *cem : executionModes) {
if (cem->getEntryPoint() != entryPoint)
continue;
if (cem->getExecutionMode() != em)
continue;
return cem;
}
return nullptr;
}
void SpirvModule::addExecutionMode(SpirvExecutionModeBase *em) {
assert(em && "cannot add null execution mode");
executionModes.push_back(em);
}
bool SpirvModule::addExtension(SpirvExtension *ext) {
assert(ext && "cannot add null extension");
return extensions.insert(ext);
}
void SpirvModule::addExtInstSet(SpirvExtInstImport *set) {
assert(set && "cannot add null extended instruction set");
extInstSets.push_back(set);
}
SpirvExtInstImport *SpirvModule::getExtInstSet(llvm::StringRef name) {
// We expect very few (usually 1) extended instruction sets to exist in the
// module, so this is not expensive.
auto found = std::find_if(extInstSets.begin(), extInstSets.end(),
[name](const SpirvExtInstImport *set) {
return set->getExtendedInstSetName() == name;
});
if (found != extInstSets.end())
return *found;
return nullptr;
}
void SpirvModule::addVariable(SpirvVariable *var) {
assert(var && "cannot add null variable to the module");
variables.push_back(var);
}
void SpirvModule::addDecoration(SpirvDecoration *decor) {
assert(decor && "cannot add null decoration to the module");
decorations.insert(decor);
}
void SpirvModule::addConstant(SpirvConstant *constant) {
assert(constant);
constants.push_back(constant);
}
void SpirvModule::addUndef(SpirvUndef *undef) {
assert(undef);
undefs.push_back(undef);
}
void SpirvModule::addString(SpirvString *str) {
assert(str);
constStrings.push_back(str);
}
void SpirvModule::addSource(SpirvSource *src) {
assert(src);
sources.push_back(src);
}
void SpirvModule::addDebugInfo(SpirvDebugInstruction *info) {
assert(info);
debugInstructions.push_back(info);
}
void SpirvModule::addModuleProcessed(SpirvModuleProcessed *p) {
assert(p);
moduleProcesses.push_back(p);
}
} // end namespace spirv
} // end namespace clang