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
|
//===- LowerABIAttributesPass.cpp - Decorate composite type ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a pass to lower attributes that specify the shader ABI
// for the functions in the generated SPIR-V module.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/SPIRV/Transforms/Passes.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "mlir/Dialect/SPIRV/IR/TargetAndABI.h"
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
#include "mlir/Dialect/SPIRV/Utils/LayoutUtils.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/Support/FormatVariadic.h"
namespace mlir {
namespace spirv {
#define GEN_PASS_DEF_SPIRVLOWERABIATTRIBUTESPASS
#include "mlir/Dialect/SPIRV/Transforms/Passes.h.inc"
} // namespace spirv
} // namespace mlir
using namespace mlir;
/// Creates a global variable for an argument based on the ABI info.
static spirv::GlobalVariableOp
createGlobalVarForEntryPointArgument(OpBuilder &builder, spirv::FuncOp funcOp,
unsigned argIndex,
spirv::InterfaceVarABIAttr abiInfo) {
auto spirvModule = funcOp->getParentOfType<spirv::ModuleOp>();
if (!spirvModule)
return nullptr;
OpBuilder::InsertionGuard moduleInsertionGuard(builder);
builder.setInsertionPoint(funcOp.getOperation());
std::string varName =
funcOp.getName().str() + "_arg_" + std::to_string(argIndex);
// Get the type of variable. If this is a scalar/vector type and has an ABI
// info create a variable of type !spirv.ptr<!spirv.struct<elementType>>. If
// not it must already be a !spirv.ptr<!spirv.struct<...>>.
auto varType = funcOp.getFunctionType().getInput(argIndex);
if (cast<spirv::SPIRVType>(varType).isScalarOrVector()) {
auto storageClass = abiInfo.getStorageClass();
if (!storageClass)
return nullptr;
varType =
spirv::PointerType::get(spirv::StructType::get(varType), *storageClass);
}
auto varPtrType = cast<spirv::PointerType>(varType);
Type pointeeType = varPtrType.getPointeeType();
// Images are an opaque type and so we can just return a pointer to an image.
// Note that currently only sampled images are supported in the SPIR-V
// lowering.
if (isa<spirv::SampledImageType>(pointeeType))
return spirv::GlobalVariableOp::create(builder, funcOp.getLoc(), varType,
varName, abiInfo.getDescriptorSet(),
abiInfo.getBinding());
auto varPointeeType = cast<spirv::StructType>(pointeeType);
// Set the offset information.
varPointeeType =
cast<spirv::StructType>(VulkanLayoutUtils::decorateType(varPointeeType));
if (!varPointeeType)
return nullptr;
varType =
spirv::PointerType::get(varPointeeType, varPtrType.getStorageClass());
return spirv::GlobalVariableOp::create(builder, funcOp.getLoc(), varType,
varName, abiInfo.getDescriptorSet(),
abiInfo.getBinding());
}
/// Creates a global variable for an argument or result based on the ABI info.
static spirv::GlobalVariableOp
createGlobalVarForGraphEntryPoint(OpBuilder &builder, spirv::GraphARMOp graphOp,
unsigned index, bool isArg,
spirv::InterfaceVarABIAttr abiInfo) {
auto spirvModule = graphOp->getParentOfType<spirv::ModuleOp>();
if (!spirvModule)
return nullptr;
OpBuilder::InsertionGuard moduleInsertionGuard(builder);
builder.setInsertionPoint(graphOp.getOperation());
std::string varName = llvm::formatv("{}_{}_{}", graphOp.getName(),
isArg ? "arg" : "res", index);
Type varType = isArg ? graphOp.getFunctionType().getInput(index)
: graphOp.getFunctionType().getResult(index);
auto pointerType = spirv::PointerType::get(
varType,
abiInfo.getStorageClass().value_or(spirv::StorageClass::UniformConstant));
return spirv::GlobalVariableOp::create(builder, graphOp.getLoc(), pointerType,
varName, abiInfo.getDescriptorSet(),
abiInfo.getBinding());
}
/// Gets the global variables that need to be specified as interface variable
/// with an spirv.EntryPointOp. Traverses the body of a entry function to do so.
static LogicalResult
getInterfaceVariables(mlir::FunctionOpInterface funcOp,
SmallVectorImpl<Attribute> &interfaceVars) {
auto module = funcOp->getParentOfType<spirv::ModuleOp>();
if (!module) {
return failure();
}
spirv::TargetEnvAttr targetEnvAttr = spirv::lookupTargetEnv(funcOp);
spirv::TargetEnv targetEnv(targetEnvAttr);
SetVector<Operation *> interfaceVarSet;
// TODO: This should in reality traverse the entry function
// call graph and collect all the interfaces. For now, just traverse the
// instructions in this function.
funcOp.walk([&](spirv::AddressOfOp addressOfOp) {
auto var =
module.lookupSymbol<spirv::GlobalVariableOp>(addressOfOp.getVariable());
// Per SPIR-V spec: "Before version 1.4, the interface's
// storage classes are limited to the Input and Output storage classes.
// Starting with version 1.4, the interface's storage classes are all
// storage classes used in declaring all global variables referenced by the
// entry point’s call tree."
const spirv::StorageClass storageClass =
cast<spirv::PointerType>(var.getType()).getStorageClass();
if ((targetEnvAttr && targetEnv.getVersion() >= spirv::Version::V_1_4) ||
(llvm::is_contained(
{spirv::StorageClass::Input, spirv::StorageClass::Output},
storageClass))) {
interfaceVarSet.insert(var.getOperation());
}
});
for (auto &var : interfaceVarSet) {
interfaceVars.push_back(SymbolRefAttr::get(
funcOp.getContext(), cast<spirv::GlobalVariableOp>(var).getSymName()));
}
return success();
}
/// Lowers the entry point attribute.
static LogicalResult lowerEntryPointABIAttr(spirv::FuncOp funcOp,
OpBuilder &builder) {
auto entryPointAttrName = spirv::getEntryPointABIAttrName();
auto entryPointAttr =
funcOp->getAttrOfType<spirv::EntryPointABIAttr>(entryPointAttrName);
if (!entryPointAttr) {
return failure();
}
spirv::TargetEnvAttr targetEnvAttr = spirv::lookupTargetEnv(funcOp);
spirv::TargetEnv targetEnv(targetEnvAttr);
OpBuilder::InsertionGuard moduleInsertionGuard(builder);
auto spirvModule = funcOp->getParentOfType<spirv::ModuleOp>();
builder.setInsertionPointToEnd(spirvModule.getBody());
// Adds the spirv.EntryPointOp after collecting all the interface variables
// needed.
SmallVector<Attribute, 1> interfaceVars;
if (failed(getInterfaceVariables(funcOp, interfaceVars))) {
return failure();
}
FailureOr<spirv::ExecutionModel> executionModel =
spirv::getExecutionModel(targetEnvAttr);
if (failed(executionModel))
return funcOp.emitRemark("lower entry point failure: could not select "
"execution model based on 'spirv.target_env'");
spirv::EntryPointOp::create(builder, funcOp.getLoc(), *executionModel, funcOp,
interfaceVars);
// Specifies the spirv.ExecutionModeOp.
if (DenseI32ArrayAttr workgroupSizeAttr = entryPointAttr.getWorkgroupSize()) {
std::optional<ArrayRef<spirv::Capability>> caps =
spirv::getCapabilities(spirv::ExecutionMode::LocalSize);
if (!caps || targetEnv.allows(*caps)) {
spirv::ExecutionModeOp::create(builder, funcOp.getLoc(), funcOp,
spirv::ExecutionMode::LocalSize,
workgroupSizeAttr.asArrayRef());
// Erase workgroup size.
entryPointAttr = spirv::EntryPointABIAttr::get(
entryPointAttr.getContext(), DenseI32ArrayAttr(),
entryPointAttr.getSubgroupSize(), entryPointAttr.getTargetWidth());
}
}
if (std::optional<int> subgroupSize = entryPointAttr.getSubgroupSize()) {
std::optional<ArrayRef<spirv::Capability>> caps =
spirv::getCapabilities(spirv::ExecutionMode::SubgroupSize);
if (!caps || targetEnv.allows(*caps)) {
spirv::ExecutionModeOp::create(builder, funcOp.getLoc(), funcOp,
spirv::ExecutionMode::SubgroupSize,
*subgroupSize);
// Erase subgroup size.
entryPointAttr = spirv::EntryPointABIAttr::get(
entryPointAttr.getContext(), entryPointAttr.getWorkgroupSize(),
std::nullopt, entryPointAttr.getTargetWidth());
}
}
if (std::optional<int> targetWidth = entryPointAttr.getTargetWidth()) {
std::optional<ArrayRef<spirv::Capability>> caps =
spirv::getCapabilities(spirv::ExecutionMode::SignedZeroInfNanPreserve);
if (!caps || targetEnv.allows(*caps)) {
spirv::ExecutionModeOp::create(
builder, funcOp.getLoc(), funcOp,
spirv::ExecutionMode::SignedZeroInfNanPreserve, *targetWidth);
// Erase target width.
entryPointAttr = spirv::EntryPointABIAttr::get(
entryPointAttr.getContext(), entryPointAttr.getWorkgroupSize(),
entryPointAttr.getSubgroupSize(), std::nullopt);
}
}
if (entryPointAttr.getWorkgroupSize() || entryPointAttr.getSubgroupSize() ||
entryPointAttr.getTargetWidth())
funcOp->setAttr(entryPointAttrName, entryPointAttr);
else
funcOp->removeAttr(entryPointAttrName);
return success();
}
namespace {
/// A pattern to convert function signature according to interface variable ABI
/// attributes.
///
/// Specifically, this pattern creates global variables according to interface
/// variable ABI attributes attached to function arguments and converts all
/// function argument uses to those global variables. This is necessary because
/// Vulkan requires all shader entry points to be of void(void) type.
class ProcessInterfaceVarABI final : public OpConversionPattern<spirv::FuncOp> {
public:
using Base::Base;
LogicalResult
matchAndRewrite(spirv::FuncOp funcOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
/// A pattern to convert graph signature according to interface variable ABI
/// attributes.
///
/// Specifically, this pattern creates global variables according to interface
/// variable ABI attributes attached to graph arguments and results.
class ProcessGraphInterfaceVarABI final
: public OpConversionPattern<spirv::GraphARMOp> {
public:
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(spirv::GraphARMOp graphOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
/// Pass to implement the ABI information specified as attributes.
class LowerABIAttributesPass final
: public spirv::impl::SPIRVLowerABIAttributesPassBase<
LowerABIAttributesPass> {
void runOnOperation() override;
};
} // namespace
LogicalResult ProcessInterfaceVarABI::matchAndRewrite(
spirv::FuncOp funcOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
if (!funcOp->getAttrOfType<spirv::EntryPointABIAttr>(
spirv::getEntryPointABIAttrName())) {
// TODO: Non-entry point functions are not handled.
return failure();
}
TypeConverter::SignatureConversion signatureConverter(
funcOp.getFunctionType().getNumInputs());
auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>();
auto indexType = typeConverter.getIndexType();
auto attrName = spirv::getInterfaceVarABIAttrName();
OpBuilder::InsertionGuard funcInsertionGuard(rewriter);
rewriter.setInsertionPointToStart(&funcOp.front());
for (const auto &argType :
llvm::enumerate(funcOp.getFunctionType().getInputs())) {
auto abiInfo = funcOp.getArgAttrOfType<spirv::InterfaceVarABIAttr>(
argType.index(), attrName);
if (!abiInfo) {
// TODO: For non-entry point functions, it should be legal
// to pass around scalar/vector values and return a scalar/vector. For now
// non-entry point functions are not handled in this ABI lowering and will
// produce an error.
return failure();
}
spirv::GlobalVariableOp var = createGlobalVarForEntryPointArgument(
rewriter, funcOp, argType.index(), abiInfo);
if (!var)
return failure();
// Insert spirv::AddressOf and spirv::AccessChain operations.
Value replacement =
spirv::AddressOfOp::create(rewriter, funcOp.getLoc(), var);
// Check if the arg is a scalar or vector type. In that case, the value
// needs to be loaded into registers.
// TODO: This is loading value of the scalar into registers
// at the start of the function. It is probably better to do the load just
// before the use. There might be multiple loads and currently there is no
// easy way to replace all uses with a sequence of operations.
if (cast<spirv::SPIRVType>(argType.value()).isScalarOrVector()) {
auto zero =
spirv::ConstantOp::getZero(indexType, funcOp.getLoc(), rewriter);
auto loadPtr = spirv::AccessChainOp::create(
rewriter, funcOp.getLoc(), replacement, zero.getConstant());
replacement = spirv::LoadOp::create(rewriter, funcOp.getLoc(), loadPtr);
}
signatureConverter.remapInput(argType.index(), replacement);
}
if (failed(rewriter.convertRegionTypes(&funcOp.getBody(), *getTypeConverter(),
&signatureConverter)))
return failure();
// Creates a new function with the update signature.
rewriter.modifyOpInPlace(funcOp, [&] {
funcOp.setType(
rewriter.getFunctionType(signatureConverter.getConvertedTypes(), {}));
});
return success();
}
LogicalResult ProcessGraphInterfaceVarABI::matchAndRewrite(
spirv::GraphARMOp graphOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// Non-entry point graphs are not handled.
if (!graphOp.getEntryPoint().value_or(false))
return failure();
TypeConverter::SignatureConversion signatureConverter(
graphOp.getFunctionType().getNumInputs());
StringRef attrName = spirv::getInterfaceVarABIAttrName();
SmallVector<Attribute, 4> interfaceVars;
// Convert arguments.
unsigned numInputs = graphOp.getFunctionType().getNumInputs();
unsigned numResults = graphOp.getFunctionType().getNumResults();
for (unsigned index = 0; index < numInputs; ++index) {
auto abiInfo =
graphOp.getArgAttrOfType<spirv::InterfaceVarABIAttr>(index, attrName);
if (!abiInfo)
return failure();
spirv::GlobalVariableOp var = createGlobalVarForGraphEntryPoint(
rewriter, graphOp, index, true, abiInfo);
if (!var)
return failure();
interfaceVars.push_back(
SymbolRefAttr::get(rewriter.getContext(), var.getSymName()));
}
for (unsigned index = 0; index < numResults; ++index) {
auto abiInfo = graphOp.getResultAttrOfType<spirv::InterfaceVarABIAttr>(
index, attrName);
if (!abiInfo)
return failure();
spirv::GlobalVariableOp var = createGlobalVarForGraphEntryPoint(
rewriter, graphOp, index, false, abiInfo);
if (!var)
return failure();
interfaceVars.push_back(
SymbolRefAttr::get(rewriter.getContext(), var.getSymName()));
}
// Update graph signature.
rewriter.modifyOpInPlace(graphOp, [&] {
for (unsigned index = 0; index < numInputs; ++index) {
graphOp.removeArgAttr(index, attrName);
}
for (unsigned index = 0; index < numResults; ++index) {
graphOp.removeResultAttr(index, rewriter.getStringAttr(attrName));
}
});
spirv::GraphEntryPointARMOp::create(rewriter, graphOp.getLoc(), graphOp,
interfaceVars);
return success();
}
void LowerABIAttributesPass::runOnOperation() {
// Uses the signature conversion methodology of the dialect conversion
// framework to implement the conversion.
spirv::ModuleOp module = getOperation();
MLIRContext *context = &getContext();
spirv::TargetEnvAttr targetEnvAttr = spirv::lookupTargetEnv(module);
if (!targetEnvAttr) {
module->emitOpError("missing SPIR-V target env attribute");
return signalPassFailure();
}
spirv::TargetEnv targetEnv(targetEnvAttr);
SPIRVTypeConverter typeConverter(targetEnv);
// Insert a bitcast in the case of a pointer type change.
typeConverter.addSourceMaterialization([](OpBuilder &builder,
spirv::PointerType type,
ValueRange inputs, Location loc) {
if (inputs.size() != 1 || !isa<spirv::PointerType>(inputs[0].getType()))
return Value();
return spirv::BitcastOp::create(builder, loc, type, inputs[0]).getResult();
});
RewritePatternSet patterns(context);
patterns.add<ProcessInterfaceVarABI, ProcessGraphInterfaceVarABI>(
typeConverter, context);
ConversionTarget target(*context);
// "Legal" function ops should have no interface variable ABI attributes.
target.addDynamicallyLegalOp<spirv::FuncOp>([&](spirv::FuncOp op) {
StringRef attrName = spirv::getInterfaceVarABIAttrName();
for (unsigned i = 0, e = op.getNumArguments(); i < e; ++i)
if (op.getArgAttr(i, attrName))
return false;
return true;
});
target.addDynamicallyLegalOp<spirv::GraphARMOp>([&](spirv::GraphARMOp op) {
StringRef attrName = spirv::getInterfaceVarABIAttrName();
for (unsigned i = 0, e = op.getNumArguments(); i < e; ++i)
if (op.getArgAttr(i, attrName))
return false;
for (unsigned i = 0, e = op.getNumResults(); i < e; ++i)
if (op.getResultAttr(i, attrName))
return false;
return true;
});
// All other SPIR-V ops are legal.
target.markUnknownOpDynamicallyLegal([](Operation *op) {
return op->getDialect()->getNamespace() ==
spirv::SPIRVDialect::getDialectNamespace();
});
if (failed(applyPartialConversion(module, target, std::move(patterns))))
return signalPassFailure();
// Walks over all the FuncOps in spirv::ModuleOp to lower the entry point
// attributes.
OpBuilder builder(context);
SmallVector<spirv::FuncOp, 1> entryPointFns;
auto entryPointAttrName = spirv::getEntryPointABIAttrName();
module.walk([&](spirv::FuncOp funcOp) {
if (funcOp->getAttrOfType<spirv::EntryPointABIAttr>(entryPointAttrName)) {
entryPointFns.push_back(funcOp);
}
});
for (auto fn : entryPointFns) {
if (failed(lowerEntryPointABIAttr(fn, builder))) {
return signalPassFailure();
}
}
}
|