-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
GlobalVariableRewriter.cpp
386 lines (335 loc) · 13.5 KB
/
GlobalVariableRewriter.cpp
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
/*
* Copyright (c) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GlobalVariableRewriter.h"
#include "AST.h"
#include "ASTIdentifier.h"
#include "ASTVisitor.h"
#include "CallGraph.h"
#include "WGSL.h"
#include "WGSLShaderModule.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
namespace WGSL {
class RewriteGlobalVariables : public AST::Visitor {
public:
RewriteGlobalVariables(CallGraph& callGraph, const HashMap<String, std::optional<PipelineLayout>>& pipelineLayouts, PrepareResult& result)
: AST::Visitor()
, m_callGraph(callGraph)
, m_result(result)
{
UNUSED_PARAM(pipelineLayouts);
}
void run();
void visit(AST::Function&) override;
void visit(AST::Variable&) override;
void visit(AST::IdentifierExpression&) override;
private:
template<typename Value>
using IndexMap = HashMap<unsigned, Value, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned>>;
using IndexSet = HashSet<unsigned, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned>>;
using UsedGlobals = IndexMap<IndexSet>;
struct Global {
struct Resource {
unsigned group;
unsigned binding;
};
std::optional<Resource> resource;
AST::Variable* declaration;
};
static AST::Identifier argumentBufferParameterName(unsigned group);
static AST::Identifier argumentBufferStructName(unsigned group);
void def(const String&);
Global* read(const String&);
void collectGlobals();
void visitEntryPoint(AST::Function&, AST::StageAttribute::Stage, PipelineLayout&);
UsedGlobals determineUsedGlobals(PipelineLayout&, AST::StageAttribute::Stage);
void usesOverride(AST::Variable&);
void insertStructs(const UsedGlobals&);
void insertParameters(AST::Function&, const UsedGlobals&);
CallGraph& m_callGraph;
PrepareResult& m_result;
HashMap<String, Global> m_globals;
IndexMap<Vector<std::pair<unsigned, Global*>>> m_groupBindingMap;
IndexMap<Type*> m_structTypes;
HashSet<String> m_defs;
HashSet<String> m_reads;
Reflection::EntryPointInformation* m_entryPointInformation { nullptr };
};
void RewriteGlobalVariables::run()
{
collectGlobals();
for (auto& entryPoint : m_callGraph.entrypoints()) {
PipelineLayout pipelineLayout;
auto it = m_result.entryPoints.find(entryPoint.function.name());
RELEASE_ASSERT(it != m_result.entryPoints.end());
m_entryPointInformation = &it->value;
visitEntryPoint(entryPoint.function, entryPoint.stage, pipelineLayout);
m_entryPointInformation->defaultLayout = WTFMove(pipelineLayout);
}
}
void RewriteGlobalVariables::visit(AST::Function& function)
{
// FIXME: visit callee once we have any
for (auto& parameter : function.parameters())
def(parameter.name());
// FIXME: detect when we shadow a global that a callee needs
AST::Visitor::visit(function.body());
}
void RewriteGlobalVariables::visit(AST::Variable& variable)
{
def(variable.name());
AST::Visitor::visit(variable);
}
void RewriteGlobalVariables::visit(AST::IdentifierExpression& identifier)
{
auto name = identifier.identifier();
if (Global* global = read(name)) {
if (auto resource = global->resource) {
auto base = makeUniqueRef<AST::IdentifierExpression>(identifier.span(), argumentBufferParameterName(resource->group));
auto structureAccess = makeUniqueRef<AST::FieldAccessExpression>(identifier.span(), WTFMove(base), WTFMove(name));
m_callGraph.ast().replace(&identifier, AST::IdentityExpression(identifier.span(), WTFMove(structureAccess)));
}
}
}
void RewriteGlobalVariables::collectGlobals()
{
auto& globalVars = m_callGraph.ast().variables();
for (auto& globalVar : globalVars) {
std::optional<unsigned> group;
std::optional<unsigned> binding;
for (auto& attribute : globalVar.attributes()) {
if (is<AST::GroupAttribute>(attribute)) {
group = { downcast<AST::GroupAttribute>(attribute).group() };
continue;
}
if (is<AST::BindingAttribute>(attribute)) {
binding = { downcast<AST::BindingAttribute>(attribute).binding() };
continue;
}
}
std::optional<Global::Resource> resource;
if (group.has_value()) {
RELEASE_ASSERT(binding.has_value());
resource = { *group, *binding };
}
auto result = m_globals.add(globalVar.name(), Global {
resource,
&globalVar
});
ASSERT(result, result.isNewEntry);
if (resource.has_value()) {
Global& global = result.iterator->value;
auto result = m_groupBindingMap.add(resource->group, Vector<std::pair<unsigned, Global*>>());
result.iterator->value.append({ resource->binding, &global });
}
}
}
void RewriteGlobalVariables::visitEntryPoint(AST::Function& function, AST::StageAttribute::Stage stage, PipelineLayout& pipelineLayout)
{
m_reads.clear();
m_defs.clear();
m_structTypes.clear();
visit(function);
if (m_reads.isEmpty())
return;
auto usedGlobals = determineUsedGlobals(pipelineLayout, stage);
insertStructs(usedGlobals);
// FIXME: not all globals are parameters
insertParameters(function, usedGlobals);
}
auto RewriteGlobalVariables::determineUsedGlobals(PipelineLayout& pipelineLayout, AST::StageAttribute::Stage stage) -> UsedGlobals
{
UsedGlobals usedGlobals;
for (const auto& globalName : m_reads) {
auto it = m_globals.find(globalName);
RELEASE_ASSERT(it != m_globals.end());
auto& global = it->value;
AST::Variable& variable = *global.declaration;
switch (variable.flavor()) {
case AST::VariableFlavor::Override:
usesOverride(variable);
break;
case AST::VariableFlavor::Var:
case AST::VariableFlavor::Let:
if (!global.resource.has_value())
continue;
break;
case AST::VariableFlavor::Const:
// Constants must be resolved at an earlier phase
RELEASE_ASSERT_NOT_REACHED();
}
auto group = global.resource->group;
auto result = usedGlobals.add(group, IndexSet());
result.iterator->value.add(global.resource->binding);
if (pipelineLayout.bindGroupLayouts.size() <= group)
pipelineLayout.bindGroupLayouts.grow(group + 1);
ShaderStage shaderStage;
switch (stage) {
case AST::StageAttribute::Stage::Compute:
shaderStage = ShaderStage::Compute;
break;
case AST::StageAttribute::Stage::Vertex:
shaderStage = ShaderStage::Vertex;
break;
case AST::StageAttribute::Stage::Fragment:
shaderStage = ShaderStage::Fragment;
break;
}
// FIXME: we need to check for an existing entry with the same binding
pipelineLayout.bindGroupLayouts[group].entries.append({
global.resource->binding,
shaderStage,
// FIXME: add the missing bindingMember information
{ }
});
}
return usedGlobals;
}
void RewriteGlobalVariables::usesOverride(AST::Variable& variable)
{
Reflection::SpecializationConstantType constantType;
const Type* type = nullptr;
if (auto* typeName = variable.maybeTypeName())
type = typeName->resolvedType();
else {
auto* initializer = variable.maybeInitializer();
ASSERT(initializer);
type = initializer->inferredType();
}
ASSERT(std::holds_alternative<Types::Primitive>(*type));
const auto& primitive = std::get<Types::Primitive>(*type);
switch (primitive.kind) {
case Types::Primitive::Bool:
constantType = Reflection::SpecializationConstantType::Boolean;
break;
case Types::Primitive::F32:
constantType = Reflection::SpecializationConstantType::Float;
break;
case Types::Primitive::I32:
constantType = Reflection::SpecializationConstantType::Int;
break;
case Types::Primitive::U32:
constantType = Reflection::SpecializationConstantType::Unsigned;
break;
case Types::Primitive::Void:
case Types::Primitive::AbstractInt:
case Types::Primitive::AbstractFloat:
case Types::Primitive::Sampler:
RELEASE_ASSERT_NOT_REACHED();
}
m_entryPointInformation->specializationConstants.add(variable.name(), Reflection::SpecializationConstant { String(), constantType });
}
void RewriteGlobalVariables::insertStructs(const UsedGlobals& usedGlobals)
{
for (auto& groupBinding : m_groupBindingMap) {
unsigned group = groupBinding.key;
auto usedGlobal = usedGlobals.find(group);
if (usedGlobal == usedGlobals.end())
continue;
const auto& bindingGlobalMap = groupBinding.value;
const IndexSet& usedBindings = usedGlobal->value;
AST::Identifier structName = argumentBufferStructName(group);
AST::StructureMember::List structMembers;
for (auto [binding, global] : bindingGlobalMap) {
if (!usedBindings.contains(binding))
continue;
ASSERT(global->declaration->maybeTypeName());
auto span = global->declaration->span();
auto* type = global->declaration->maybeTypeName()->resolvedType();
bool shouldBeReference = true;
if (std::get_if<Types::Texture>(type))
shouldBeReference = false;
else if (auto* primitive = std::get_if<Types::Primitive>(type)) {
if (primitive->kind == Types::Primitive::Sampler)
shouldBeReference = false;
}
AST::TypeName::Ref memberType = *global->declaration->maybeTypeName();
if (shouldBeReference)
memberType = adoptRef(*new AST::ReferenceTypeName(span, WTFMove(memberType)));
structMembers.append(m_callGraph.ast().astBuilder().construct<AST::StructureMember>(
span,
AST::Identifier::make(global->declaration->name()),
WTFMove(memberType),
AST::Attribute::List {
adoptRef(*new AST::BindingAttribute(span, binding))
}
));
}
m_callGraph.ast().append(m_callGraph.ast().structures(), makeUniqueRef<AST::Structure>(
SourceSpan::empty(),
WTFMove(structName),
WTFMove(structMembers),
AST::Attribute::List { },
AST::StructureRole::BindGroup
));
m_structTypes.add(groupBinding.key, m_callGraph.ast().types().structType(m_callGraph.ast().structures().last()));
}
}
void RewriteGlobalVariables::insertParameters(AST::Function& function, const UsedGlobals& usedGlobals)
{
auto span = function.span();
for (auto& it : usedGlobals) {
unsigned group = it.key;
auto type = adoptRef(*new AST::NamedTypeName(span, argumentBufferStructName(group)));
type->m_resolvedType = m_structTypes.get(group);
m_callGraph.ast().append(function.parameters(), adoptRef(*new AST::Parameter(
span,
argumentBufferParameterName(group),
WTFMove(type),
AST::Attribute::List {
adoptRef(*new AST::GroupAttribute(span, group))
},
AST::ParameterRole::BindGroup
)));
}
}
void RewriteGlobalVariables::def(const String& name)
{
m_defs.add(name);
}
auto RewriteGlobalVariables::read(const String& name) -> Global*
{
if (m_defs.contains(name))
return nullptr;
auto it = m_globals.find(name);
if (it == m_globals.end())
return nullptr;
m_reads.add(name);
return &it->value;
}
AST::Identifier RewriteGlobalVariables::argumentBufferParameterName(unsigned group)
{
return AST::Identifier::make(makeString("__ArgumentBufer_", String::number(group)));
}
AST::Identifier RewriteGlobalVariables::argumentBufferStructName(unsigned group)
{
return AST::Identifier::make(makeString("__ArgumentBuferT_", String::number(group)));
}
void rewriteGlobalVariables(CallGraph& callGraph, const HashMap<String, std::optional<PipelineLayout>>& pipelineLayouts, PrepareResult& result)
{
RewriteGlobalVariables(callGraph, pipelineLayouts, result).run();
}
} // namespace WGSL