1 //===- ConvertLaunchFuncToVulkanCalls.cpp - MLIR Vulkan conversion passes -===//
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 convert vulkan launch call into a sequence of
10 // Vulkan runtime calls. The Vulkan runtime API surface is huge so currently we
11 // don't expose separate external functions in IR for each of them, instead we
12 // expose a few external functions to wrapper libraries which manages Vulkan
13 // runtime.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "../PassDetail.h"
18 #include "mlir/Conversion/GPUToVulkan/ConvertGPUToVulkanPass.h"
19 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
20 #include "mlir/IR/Attributes.h"
21 #include "mlir/IR/Builders.h"
22 #include "mlir/IR/BuiltinOps.h"
23 
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Support/FormatVariadic.h"
26 
27 using namespace mlir;
28 
29 static constexpr const char *kCInterfaceVulkanLaunch =
30     "_mlir_ciface_vulkanLaunch";
31 static constexpr const char *kDeinitVulkan = "deinitVulkan";
32 static constexpr const char *kRunOnVulkan = "runOnVulkan";
33 static constexpr const char *kInitVulkan = "initVulkan";
34 static constexpr const char *kSetBinaryShader = "setBinaryShader";
35 static constexpr const char *kSetEntryPoint = "setEntryPoint";
36 static constexpr const char *kSetNumWorkGroups = "setNumWorkGroups";
37 static constexpr const char *kSPIRVBinary = "SPIRV_BIN";
38 static constexpr const char *kSPIRVBlobAttrName = "spirv_blob";
39 static constexpr const char *kSPIRVEntryPointAttrName = "spirv_entry_point";
40 static constexpr const char *kVulkanLaunch = "vulkanLaunch";
41 
42 namespace {
43 
44 /// A pass to convert vulkan launch call op into a sequence of Vulkan
45 /// runtime calls in the following order:
46 ///
47 /// * initVulkan           -- initializes vulkan runtime
48 /// * bindMemRef           -- binds memref
49 /// * setBinaryShader      -- sets the binary shader data
50 /// * setEntryPoint        -- sets the entry point name
51 /// * setNumWorkGroups     -- sets the number of a local workgroups
52 /// * runOnVulkan          -- runs vulkan runtime
53 /// * deinitVulkan         -- deinitializes vulkan runtime
54 ///
55 class VulkanLaunchFuncToVulkanCallsPass
56     : public ConvertVulkanLaunchFuncToVulkanCallsBase<
57           VulkanLaunchFuncToVulkanCallsPass> {
58 private:
59   void initializeCachedTypes() {
60     llvmFloatType = Float32Type::get(&getContext());
61     llvmVoidType = LLVM::LLVMVoidType::get(&getContext());
62     llvmPointerType =
63         LLVM::LLVMPointerType::get(IntegerType::get(&getContext(), 8));
64     llvmInt32Type = IntegerType::get(&getContext(), 32);
65     llvmInt64Type = IntegerType::get(&getContext(), 64);
66   }
67 
68   Type getMemRefType(uint32_t rank, Type elemenType) {
69     // According to the MLIR doc memref argument is converted into a
70     // pointer-to-struct argument of type:
71     // template <typename Elem, size_t Rank>
72     // struct {
73     //   Elem *allocated;
74     //   Elem *aligned;
75     //   int64_t offset;
76     //   int64_t sizes[Rank]; // omitted when rank == 0
77     //   int64_t strides[Rank]; // omitted when rank == 0
78     // };
79     auto llvmPtrToElementType = LLVM::LLVMPointerType::get(elemenType);
80     auto llvmArrayRankElementSizeType =
81         LLVM::LLVMArrayType::get(getInt64Type(), rank);
82 
83     // Create a type
84     // `!llvm<"{ `element-type`*, `element-type`*, i64,
85     // [`rank` x i64], [`rank` x i64]}">`.
86     return LLVM::LLVMStructType::getLiteral(
87         &getContext(),
88         {llvmPtrToElementType, llvmPtrToElementType, getInt64Type(),
89          llvmArrayRankElementSizeType, llvmArrayRankElementSizeType});
90   }
91 
92   Type getVoidType() { return llvmVoidType; }
93   Type getPointerType() { return llvmPointerType; }
94   Type getInt32Type() { return llvmInt32Type; }
95   Type getInt64Type() { return llvmInt64Type; }
96 
97   /// Creates an LLVM global for the given `name`.
98   Value createEntryPointNameConstant(StringRef name, Location loc,
99                                      OpBuilder &builder);
100 
101   /// Declares all needed runtime functions.
102   void declareVulkanFunctions(Location loc);
103 
104   /// Checks whether the given LLVM::CallOp is a vulkan launch call op.
105   bool isVulkanLaunchCallOp(LLVM::CallOp callOp) {
106     return (callOp.getCallee() &&
107             callOp.getCallee().getValue() == kVulkanLaunch &&
108             callOp.getNumOperands() >= kVulkanLaunchNumConfigOperands);
109   }
110 
111   /// Checks whether the given LLVM::CallOp is a "ci_face" vulkan launch call
112   /// op.
113   bool isCInterfaceVulkanLaunchCallOp(LLVM::CallOp callOp) {
114     return (callOp.getCallee() &&
115             callOp.getCallee().getValue() == kCInterfaceVulkanLaunch &&
116             callOp.getNumOperands() >= kVulkanLaunchNumConfigOperands);
117   }
118 
119   /// Translates the given `vulkanLaunchCallOp` to the sequence of Vulkan
120   /// runtime calls.
121   void translateVulkanLaunchCall(LLVM::CallOp vulkanLaunchCallOp);
122 
123   /// Creates call to `bindMemRef` for each memref operand.
124   void createBindMemRefCalls(LLVM::CallOp vulkanLaunchCallOp,
125                              Value vulkanRuntime);
126 
127   /// Collects SPIRV attributes from the given `vulkanLaunchCallOp`.
128   void collectSPIRVAttributes(LLVM::CallOp vulkanLaunchCallOp);
129 
130   /// Deduces a rank and element type from the given 'ptrToMemRefDescriptor`.
131   LogicalResult deduceMemRefRankAndType(Value ptrToMemRefDescriptor,
132                                         uint32_t &rank, Type &type);
133 
134   /// Returns a string representation from the given `type`.
135   StringRef stringifyType(Type type) {
136     if (type.isa<Float32Type>())
137       return "Float";
138     if (type.isa<Float16Type>())
139       return "Half";
140     if (auto intType = type.dyn_cast<IntegerType>()) {
141       if (intType.getWidth() == 32)
142         return "Int32";
143       if (intType.getWidth() == 16)
144         return "Int16";
145       if (intType.getWidth() == 8)
146         return "Int8";
147     }
148 
149     llvm_unreachable("unsupported type");
150   }
151 
152 public:
153   void runOnOperation() override;
154 
155 private:
156   Type llvmFloatType;
157   Type llvmVoidType;
158   Type llvmPointerType;
159   Type llvmInt32Type;
160   Type llvmInt64Type;
161 
162   // TODO: Use an associative array to support multiple vulkan launch calls.
163   std::pair<StringAttr, StringAttr> spirvAttributes;
164   /// The number of vulkan launch configuration operands, placed at the leading
165   /// positions of the operand list.
166   static constexpr unsigned kVulkanLaunchNumConfigOperands = 3;
167 };
168 
169 } // anonymous namespace
170 
171 void VulkanLaunchFuncToVulkanCallsPass::runOnOperation() {
172   initializeCachedTypes();
173 
174   // Collect SPIR-V attributes such as `spirv_blob` and
175   // `spirv_entry_point_name`.
176   getOperation().walk([this](LLVM::CallOp op) {
177     if (isVulkanLaunchCallOp(op))
178       collectSPIRVAttributes(op);
179   });
180 
181   // Convert vulkan launch call op into a sequence of Vulkan runtime calls.
182   getOperation().walk([this](LLVM::CallOp op) {
183     if (isCInterfaceVulkanLaunchCallOp(op))
184       translateVulkanLaunchCall(op);
185   });
186 }
187 
188 void VulkanLaunchFuncToVulkanCallsPass::collectSPIRVAttributes(
189     LLVM::CallOp vulkanLaunchCallOp) {
190   // Check that `kSPIRVBinary` and `kSPIRVEntryPoint` are present in attributes
191   // for the given vulkan launch call.
192   auto spirvBlobAttr =
193       vulkanLaunchCallOp->getAttrOfType<StringAttr>(kSPIRVBlobAttrName);
194   if (!spirvBlobAttr) {
195     vulkanLaunchCallOp.emitError()
196         << "missing " << kSPIRVBlobAttrName << " attribute";
197     return signalPassFailure();
198   }
199 
200   auto spirvEntryPointNameAttr =
201       vulkanLaunchCallOp->getAttrOfType<StringAttr>(kSPIRVEntryPointAttrName);
202   if (!spirvEntryPointNameAttr) {
203     vulkanLaunchCallOp.emitError()
204         << "missing " << kSPIRVEntryPointAttrName << " attribute";
205     return signalPassFailure();
206   }
207 
208   spirvAttributes = std::make_pair(spirvBlobAttr, spirvEntryPointNameAttr);
209 }
210 
211 void VulkanLaunchFuncToVulkanCallsPass::createBindMemRefCalls(
212     LLVM::CallOp cInterfaceVulkanLaunchCallOp, Value vulkanRuntime) {
213   if (cInterfaceVulkanLaunchCallOp.getNumOperands() ==
214       kVulkanLaunchNumConfigOperands)
215     return;
216   OpBuilder builder(cInterfaceVulkanLaunchCallOp);
217   Location loc = cInterfaceVulkanLaunchCallOp.getLoc();
218 
219   // Create LLVM constant for the descriptor set index.
220   // Bind all memrefs to the `0` descriptor set, the same way as `GPUToSPIRV`
221   // pass does.
222   Value descriptorSet = builder.create<LLVM::ConstantOp>(
223       loc, getInt32Type(), builder.getI32IntegerAttr(0));
224 
225   for (auto en :
226        llvm::enumerate(cInterfaceVulkanLaunchCallOp.getOperands().drop_front(
227            kVulkanLaunchNumConfigOperands))) {
228     // Create LLVM constant for the descriptor binding index.
229     Value descriptorBinding = builder.create<LLVM::ConstantOp>(
230         loc, getInt32Type(), builder.getI32IntegerAttr(en.index()));
231 
232     auto ptrToMemRefDescriptor = en.value();
233     uint32_t rank = 0;
234     Type type;
235     if (failed(deduceMemRefRankAndType(ptrToMemRefDescriptor, rank, type))) {
236       cInterfaceVulkanLaunchCallOp.emitError()
237           << "invalid memref descriptor " << ptrToMemRefDescriptor.getType();
238       return signalPassFailure();
239     }
240 
241     auto symbolName =
242         llvm::formatv("bindMemRef{0}D{1}", rank, stringifyType(type)).str();
243     // Special case for fp16 type. Since it is not a supported type in C we use
244     // int16_t and bitcast the descriptor.
245     if (type.isa<Float16Type>()) {
246       auto memRefTy = getMemRefType(rank, IntegerType::get(&getContext(), 16));
247       ptrToMemRefDescriptor = builder.create<LLVM::BitcastOp>(
248           loc, LLVM::LLVMPointerType::get(memRefTy), ptrToMemRefDescriptor);
249     }
250     // Create call to `bindMemRef`.
251     builder.create<LLVM::CallOp>(
252         loc, TypeRange(), StringRef(symbolName.data(), symbolName.size()),
253         ValueRange{vulkanRuntime, descriptorSet, descriptorBinding,
254                    ptrToMemRefDescriptor});
255   }
256 }
257 
258 LogicalResult VulkanLaunchFuncToVulkanCallsPass::deduceMemRefRankAndType(
259     Value ptrToMemRefDescriptor, uint32_t &rank, Type &type) {
260   auto llvmPtrDescriptorTy =
261       ptrToMemRefDescriptor.getType().dyn_cast<LLVM::LLVMPointerType>();
262   if (!llvmPtrDescriptorTy)
263     return failure();
264 
265   auto llvmDescriptorTy =
266       llvmPtrDescriptorTy.getElementType().dyn_cast<LLVM::LLVMStructType>();
267   // template <typename Elem, size_t Rank>
268   // struct {
269   //   Elem *allocated;
270   //   Elem *aligned;
271   //   int64_t offset;
272   //   int64_t sizes[Rank]; // omitted when rank == 0
273   //   int64_t strides[Rank]; // omitted when rank == 0
274   // };
275   if (!llvmDescriptorTy)
276     return failure();
277 
278   type = llvmDescriptorTy.getBody()[0]
279              .cast<LLVM::LLVMPointerType>()
280              .getElementType();
281   if (llvmDescriptorTy.getBody().size() == 3) {
282     rank = 0;
283     return success();
284   }
285   rank = llvmDescriptorTy.getBody()[3]
286              .cast<LLVM::LLVMArrayType>()
287              .getNumElements();
288   return success();
289 }
290 
291 void VulkanLaunchFuncToVulkanCallsPass::declareVulkanFunctions(Location loc) {
292   ModuleOp module = getOperation();
293   auto builder = OpBuilder::atBlockEnd(module.getBody());
294 
295   if (!module.lookupSymbol(kSetEntryPoint)) {
296     builder.create<LLVM::LLVMFuncOp>(
297         loc, kSetEntryPoint,
298         LLVM::LLVMFunctionType::get(getVoidType(),
299                                     {getPointerType(), getPointerType()}));
300   }
301 
302   if (!module.lookupSymbol(kSetNumWorkGroups)) {
303     builder.create<LLVM::LLVMFuncOp>(
304         loc, kSetNumWorkGroups,
305         LLVM::LLVMFunctionType::get(getVoidType(),
306                                     {getPointerType(), getInt64Type(),
307                                      getInt64Type(), getInt64Type()}));
308   }
309 
310   if (!module.lookupSymbol(kSetBinaryShader)) {
311     builder.create<LLVM::LLVMFuncOp>(
312         loc, kSetBinaryShader,
313         LLVM::LLVMFunctionType::get(
314             getVoidType(),
315             {getPointerType(), getPointerType(), getInt32Type()}));
316   }
317 
318   if (!module.lookupSymbol(kRunOnVulkan)) {
319     builder.create<LLVM::LLVMFuncOp>(
320         loc, kRunOnVulkan,
321         LLVM::LLVMFunctionType::get(getVoidType(), {getPointerType()}));
322   }
323 
324   for (unsigned i = 1; i <= 3; i++) {
325     SmallVector<Type, 5> types{
326         Float32Type::get(&getContext()), IntegerType::get(&getContext(), 32),
327         IntegerType::get(&getContext(), 16), IntegerType::get(&getContext(), 8),
328         Float16Type::get(&getContext())};
329     for (auto type : types) {
330       std::string fnName = "bindMemRef" + std::to_string(i) + "D" +
331                            std::string(stringifyType(type));
332       if (type.isa<Float16Type>())
333         type = IntegerType::get(&getContext(), 16);
334       if (!module.lookupSymbol(fnName)) {
335         auto fnType = LLVM::LLVMFunctionType::get(
336             getVoidType(),
337             {getPointerType(), getInt32Type(), getInt32Type(),
338              LLVM::LLVMPointerType::get(getMemRefType(i, type))},
339             /*isVarArg=*/false);
340         builder.create<LLVM::LLVMFuncOp>(loc, fnName, fnType);
341       }
342     }
343   }
344 
345   if (!module.lookupSymbol(kInitVulkan)) {
346     builder.create<LLVM::LLVMFuncOp>(
347         loc, kInitVulkan, LLVM::LLVMFunctionType::get(getPointerType(), {}));
348   }
349 
350   if (!module.lookupSymbol(kDeinitVulkan)) {
351     builder.create<LLVM::LLVMFuncOp>(
352         loc, kDeinitVulkan,
353         LLVM::LLVMFunctionType::get(getVoidType(), {getPointerType()}));
354   }
355 }
356 
357 Value VulkanLaunchFuncToVulkanCallsPass::createEntryPointNameConstant(
358     StringRef name, Location loc, OpBuilder &builder) {
359   SmallString<16> shaderName(name.begin(), name.end());
360   // Append `\0` to follow C style string given that LLVM::createGlobalString()
361   // won't handle this directly for us.
362   shaderName.push_back('\0');
363 
364   std::string entryPointGlobalName = (name + "_spv_entry_point_name").str();
365   return LLVM::createGlobalString(loc, builder, entryPointGlobalName,
366                                   shaderName, LLVM::Linkage::Internal);
367 }
368 
369 void VulkanLaunchFuncToVulkanCallsPass::translateVulkanLaunchCall(
370     LLVM::CallOp cInterfaceVulkanLaunchCallOp) {
371   OpBuilder builder(cInterfaceVulkanLaunchCallOp);
372   Location loc = cInterfaceVulkanLaunchCallOp.getLoc();
373   // Create call to `initVulkan`.
374   auto initVulkanCall = builder.create<LLVM::CallOp>(
375       loc, TypeRange{getPointerType()}, kInitVulkan);
376   // The result of `initVulkan` function is a pointer to Vulkan runtime, we
377   // need to pass that pointer to each Vulkan runtime call.
378   auto vulkanRuntime = initVulkanCall.getResult(0);
379 
380   // Create LLVM global with SPIR-V binary data, so we can pass a pointer with
381   // that data to runtime call.
382   Value ptrToSPIRVBinary = LLVM::createGlobalString(
383       loc, builder, kSPIRVBinary, spirvAttributes.first.getValue(),
384       LLVM::Linkage::Internal);
385 
386   // Create LLVM constant for the size of SPIR-V binary shader.
387   Value binarySize = builder.create<LLVM::ConstantOp>(
388       loc, getInt32Type(),
389       builder.getI32IntegerAttr(spirvAttributes.first.getValue().size()));
390 
391   // Create call to `bindMemRef` for each memref operand.
392   createBindMemRefCalls(cInterfaceVulkanLaunchCallOp, vulkanRuntime);
393 
394   // Create call to `setBinaryShader` runtime function with the given pointer to
395   // SPIR-V binary and binary size.
396   builder.create<LLVM::CallOp>(
397       loc, TypeRange(), kSetBinaryShader,
398       ValueRange{vulkanRuntime, ptrToSPIRVBinary, binarySize});
399   // Create LLVM global with entry point name.
400   Value entryPointName = createEntryPointNameConstant(
401       spirvAttributes.second.getValue(), loc, builder);
402   // Create call to `setEntryPoint` runtime function with the given pointer to
403   // entry point name.
404   builder.create<LLVM::CallOp>(loc, TypeRange(), kSetEntryPoint,
405                                ValueRange{vulkanRuntime, entryPointName});
406 
407   // Create number of local workgroup for each dimension.
408   builder.create<LLVM::CallOp>(
409       loc, TypeRange(), kSetNumWorkGroups,
410       ValueRange{vulkanRuntime, cInterfaceVulkanLaunchCallOp.getOperand(0),
411                  cInterfaceVulkanLaunchCallOp.getOperand(1),
412                  cInterfaceVulkanLaunchCallOp.getOperand(2)});
413 
414   // Create call to `runOnVulkan` runtime function.
415   builder.create<LLVM::CallOp>(loc, TypeRange(), kRunOnVulkan,
416                                ValueRange{vulkanRuntime});
417 
418   // Create call to 'deinitVulkan' runtime function.
419   builder.create<LLVM::CallOp>(loc, TypeRange(), kDeinitVulkan,
420                                ValueRange{vulkanRuntime});
421 
422   // Declare runtime functions.
423   declareVulkanFunctions(loc);
424 
425   cInterfaceVulkanLaunchCallOp.erase();
426 }
427 
428 std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
429 mlir::createConvertVulkanLaunchFuncToVulkanCallsPass() {
430   return std::make_unique<VulkanLaunchFuncToVulkanCallsPass>();
431 }
432