1 //===- LowerABIAttributesPass.cpp - Decorate composite type ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a pass to lower attributes that specify the shader ABI
10 // for the functions in the generated SPIR-V module.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PassDetail.h"
15 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
16 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
17 #include "mlir/Dialect/SPIRV/Transforms/Passes.h"
18 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
19 #include "mlir/Dialect/SPIRV/Utils/LayoutUtils.h"
20 #include "mlir/Transforms/DialectConversion.h"
21 #include "llvm/ADT/SetVector.h"
22 
23 using namespace mlir;
24 
25 /// Creates a global variable for an argument based on the ABI info.
26 static spirv::GlobalVariableOp
createGlobalVarForEntryPointArgument(OpBuilder & builder,spirv::FuncOp funcOp,unsigned argIndex,spirv::InterfaceVarABIAttr abiInfo)27 createGlobalVarForEntryPointArgument(OpBuilder &builder, spirv::FuncOp funcOp,
28                                      unsigned argIndex,
29                                      spirv::InterfaceVarABIAttr abiInfo) {
30   auto spirvModule = funcOp->getParentOfType<spirv::ModuleOp>();
31   if (!spirvModule)
32     return nullptr;
33 
34   OpBuilder::InsertionGuard moduleInsertionGuard(builder);
35   builder.setInsertionPoint(funcOp.getOperation());
36   std::string varName =
37       funcOp.getName().str() + "_arg_" + std::to_string(argIndex);
38 
39   // Get the type of variable. If this is a scalar/vector type and has an ABI
40   // info create a variable of type !spv.ptr<!spv.struct<elementType>>. If not
41   // it must already be a !spv.ptr<!spv.struct<...>>.
42   auto varType = funcOp.getType().getInput(argIndex);
43   if (varType.cast<spirv::SPIRVType>().isScalarOrVector()) {
44     auto storageClass = abiInfo.getStorageClass();
45     if (!storageClass)
46       return nullptr;
47     varType =
48         spirv::PointerType::get(spirv::StructType::get(varType), *storageClass);
49   }
50   auto varPtrType = varType.cast<spirv::PointerType>();
51   auto varPointeeType = varPtrType.getPointeeType().cast<spirv::StructType>();
52 
53   // Set the offset information.
54   varPointeeType =
55       VulkanLayoutUtils::decorateType(varPointeeType).cast<spirv::StructType>();
56 
57   if (!varPointeeType)
58     return nullptr;
59 
60   varType =
61       spirv::PointerType::get(varPointeeType, varPtrType.getStorageClass());
62 
63   return builder.create<spirv::GlobalVariableOp>(
64       funcOp.getLoc(), varType, varName, abiInfo.getDescriptorSet(),
65       abiInfo.getBinding());
66 }
67 
68 /// Gets the global variables that need to be specified as interface variable
69 /// with an spv.EntryPointOp. Traverses the body of a entry function to do so.
70 static LogicalResult
getInterfaceVariables(spirv::FuncOp funcOp,SmallVectorImpl<Attribute> & interfaceVars)71 getInterfaceVariables(spirv::FuncOp funcOp,
72                       SmallVectorImpl<Attribute> &interfaceVars) {
73   auto module = funcOp->getParentOfType<spirv::ModuleOp>();
74   if (!module) {
75     return failure();
76   }
77   SetVector<Operation *> interfaceVarSet;
78 
79   // TODO: This should in reality traverse the entry function
80   // call graph and collect all the interfaces. For now, just traverse the
81   // instructions in this function.
82   funcOp.walk([&](spirv::AddressOfOp addressOfOp) {
83     auto var =
84         module.lookupSymbol<spirv::GlobalVariableOp>(addressOfOp.variable());
85     // TODO: Per SPIR-V spec: "Before version 1.4, the interface’s
86     // storage classes are limited to the Input and Output storage classes.
87     // Starting with version 1.4, the interface’s storage classes are all
88     // storage classes used in declaring all global variables referenced by the
89     // entry point’s call tree." We should consider the target environment here.
90     switch (var.type().cast<spirv::PointerType>().getStorageClass()) {
91     case spirv::StorageClass::Input:
92     case spirv::StorageClass::Output:
93       interfaceVarSet.insert(var.getOperation());
94       break;
95     default:
96       break;
97     }
98   });
99   for (auto &var : interfaceVarSet) {
100     interfaceVars.push_back(SymbolRefAttr::get(
101         funcOp.getContext(), cast<spirv::GlobalVariableOp>(var).sym_name()));
102   }
103   return success();
104 }
105 
106 /// Lowers the entry point attribute.
lowerEntryPointABIAttr(spirv::FuncOp funcOp,OpBuilder & builder)107 static LogicalResult lowerEntryPointABIAttr(spirv::FuncOp funcOp,
108                                             OpBuilder &builder) {
109   auto entryPointAttrName = spirv::getEntryPointABIAttrName();
110   auto entryPointAttr =
111       funcOp->getAttrOfType<spirv::EntryPointABIAttr>(entryPointAttrName);
112   if (!entryPointAttr) {
113     return failure();
114   }
115 
116   OpBuilder::InsertionGuard moduleInsertionGuard(builder);
117   auto spirvModule = funcOp->getParentOfType<spirv::ModuleOp>();
118   builder.setInsertionPointToEnd(spirvModule.getBody());
119 
120   // Adds the spv.EntryPointOp after collecting all the interface variables
121   // needed.
122   SmallVector<Attribute, 1> interfaceVars;
123   if (failed(getInterfaceVariables(funcOp, interfaceVars))) {
124     return failure();
125   }
126 
127   spirv::TargetEnvAttr targetEnv = spirv::lookupTargetEnv(funcOp);
128   FailureOr<spirv::ExecutionModel> executionModel =
129       spirv::getExecutionModel(targetEnv);
130   if (failed(executionModel))
131     return funcOp.emitRemark("lower entry point failure: could not select "
132                              "execution model based on 'spv.target_env'");
133 
134   builder.create<spirv::EntryPointOp>(
135       funcOp.getLoc(), executionModel.getValue(), funcOp, interfaceVars);
136 
137   // Specifies the spv.ExecutionModeOp.
138   auto localSizeAttr = entryPointAttr.local_size();
139   SmallVector<int32_t, 3> localSize(localSizeAttr.getValues<int32_t>());
140   builder.create<spirv::ExecutionModeOp>(
141       funcOp.getLoc(), funcOp, spirv::ExecutionMode::LocalSize, localSize);
142   funcOp->removeAttr(entryPointAttrName);
143   return success();
144 }
145 
146 namespace {
147 /// A pattern to convert function signature according to interface variable ABI
148 /// attributes.
149 ///
150 /// Specifically, this pattern creates global variables according to interface
151 /// variable ABI attributes attached to function arguments and converts all
152 /// function argument uses to those global variables. This is necessary because
153 /// Vulkan requires all shader entry points to be of void(void) type.
154 class ProcessInterfaceVarABI final : public OpConversionPattern<spirv::FuncOp> {
155 public:
156   using OpConversionPattern<spirv::FuncOp>::OpConversionPattern;
157 
158   LogicalResult
159   matchAndRewrite(spirv::FuncOp funcOp, ArrayRef<Value> operands,
160                   ConversionPatternRewriter &rewriter) const override;
161 };
162 
163 /// Pass to implement the ABI information specified as attributes.
164 class LowerABIAttributesPass final
165     : public SPIRVLowerABIAttributesBase<LowerABIAttributesPass> {
166   void runOnOperation() override;
167 };
168 } // namespace
169 
matchAndRewrite(spirv::FuncOp funcOp,ArrayRef<Value> operands,ConversionPatternRewriter & rewriter) const170 LogicalResult ProcessInterfaceVarABI::matchAndRewrite(
171     spirv::FuncOp funcOp, ArrayRef<Value> operands,
172     ConversionPatternRewriter &rewriter) const {
173   if (!funcOp->getAttrOfType<spirv::EntryPointABIAttr>(
174           spirv::getEntryPointABIAttrName())) {
175     // TODO: Non-entry point functions are not handled.
176     return failure();
177   }
178   TypeConverter::SignatureConversion signatureConverter(
179       funcOp.getType().getNumInputs());
180 
181   auto attrName = spirv::getInterfaceVarABIAttrName();
182   for (auto argType : llvm::enumerate(funcOp.getType().getInputs())) {
183     auto abiInfo = funcOp.getArgAttrOfType<spirv::InterfaceVarABIAttr>(
184         argType.index(), attrName);
185     if (!abiInfo) {
186       // TODO: For non-entry point functions, it should be legal
187       // to pass around scalar/vector values and return a scalar/vector. For now
188       // non-entry point functions are not handled in this ABI lowering and will
189       // produce an error.
190       return failure();
191     }
192     spirv::GlobalVariableOp var = createGlobalVarForEntryPointArgument(
193         rewriter, funcOp, argType.index(), abiInfo);
194     if (!var)
195       return failure();
196 
197     OpBuilder::InsertionGuard funcInsertionGuard(rewriter);
198     rewriter.setInsertionPointToStart(&funcOp.front());
199     // Insert spirv::AddressOf and spirv::AccessChain operations.
200     Value replacement =
201         rewriter.create<spirv::AddressOfOp>(funcOp.getLoc(), var);
202     // Check if the arg is a scalar or vector type. In that case, the value
203     // needs to be loaded into registers.
204     // TODO: This is loading value of the scalar into registers
205     // at the start of the function. It is probably better to do the load just
206     // before the use. There might be multiple loads and currently there is no
207     // easy way to replace all uses with a sequence of operations.
208     if (argType.value().cast<spirv::SPIRVType>().isScalarOrVector()) {
209       auto indexType = SPIRVTypeConverter::getIndexType(funcOp.getContext());
210       auto zero =
211           spirv::ConstantOp::getZero(indexType, funcOp.getLoc(), rewriter);
212       auto loadPtr = rewriter.create<spirv::AccessChainOp>(
213           funcOp.getLoc(), replacement, zero.constant());
214       replacement = rewriter.create<spirv::LoadOp>(funcOp.getLoc(), loadPtr);
215     }
216     signatureConverter.remapInput(argType.index(), replacement);
217   }
218   if (failed(rewriter.convertRegionTypes(&funcOp.getBody(), *getTypeConverter(),
219                                          &signatureConverter)))
220     return failure();
221 
222   // Creates a new function with the update signature.
223   rewriter.updateRootInPlace(funcOp, [&] {
224     funcOp.setType(rewriter.getFunctionType(
225         signatureConverter.getConvertedTypes(), llvm::None));
226   });
227   return success();
228 }
229 
runOnOperation()230 void LowerABIAttributesPass::runOnOperation() {
231   // Uses the signature conversion methodology of the dialect conversion
232   // framework to implement the conversion.
233   spirv::ModuleOp module = getOperation();
234   MLIRContext *context = &getContext();
235 
236   spirv::TargetEnv targetEnv(spirv::lookupTargetEnv(module));
237 
238   SPIRVTypeConverter typeConverter(targetEnv);
239 
240   // Insert a bitcast in the case of a pointer type change.
241   typeConverter.addSourceMaterialization([](OpBuilder &builder,
242                                             spirv::PointerType type,
243                                             ValueRange inputs, Location loc) {
244     if (inputs.size() != 1 || !inputs[0].getType().isa<spirv::PointerType>())
245       return Value();
246     return builder.create<spirv::BitcastOp>(loc, type, inputs[0]).getResult();
247   });
248 
249   RewritePatternSet patterns(context);
250   patterns.add<ProcessInterfaceVarABI>(typeConverter, context);
251 
252   ConversionTarget target(*context);
253   // "Legal" function ops should have no interface variable ABI attributes.
254   target.addDynamicallyLegalOp<spirv::FuncOp>([&](spirv::FuncOp op) {
255     StringRef attrName = spirv::getInterfaceVarABIAttrName();
256     for (unsigned i = 0, e = op.getNumArguments(); i < e; ++i)
257       if (op.getArgAttr(i, attrName))
258         return false;
259     return true;
260   });
261   // All other SPIR-V ops are legal.
262   target.markUnknownOpDynamicallyLegal([](Operation *op) {
263     return op->getDialect()->getNamespace() ==
264            spirv::SPIRVDialect::getDialectNamespace();
265   });
266   if (failed(applyPartialConversion(module, target, std::move(patterns))))
267     return signalPassFailure();
268 
269   // Walks over all the FuncOps in spirv::ModuleOp to lower the entry point
270   // attributes.
271   OpBuilder builder(context);
272   SmallVector<spirv::FuncOp, 1> entryPointFns;
273   auto entryPointAttrName = spirv::getEntryPointABIAttrName();
274   module.walk([&](spirv::FuncOp funcOp) {
275     if (funcOp->getAttrOfType<spirv::EntryPointABIAttr>(entryPointAttrName)) {
276       entryPointFns.push_back(funcOp);
277     }
278   });
279   for (auto fn : entryPointFns) {
280     if (failed(lowerEntryPointABIAttr(fn, builder))) {
281       return signalPassFailure();
282     }
283   }
284 }
285 
286 std::unique_ptr<OperationPass<spirv::ModuleOp>>
createLowerABIAttributesPass()287 mlir::spirv::createLowerABIAttributesPass() {
288   return std::make_unique<LowerABIAttributesPass>();
289 }
290