1cae4067eSGeorge Mitenkov //===- ConvertLaunchFuncToLLVMCalls.cpp - MLIR GPU launch to LLVM pass ----===//
2cae4067eSGeorge Mitenkov //
3cae4067eSGeorge Mitenkov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cae4067eSGeorge Mitenkov // See https://llvm.org/LICENSE.txt for license information.
5cae4067eSGeorge Mitenkov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cae4067eSGeorge Mitenkov //
7cae4067eSGeorge Mitenkov //===----------------------------------------------------------------------===//
8cae4067eSGeorge Mitenkov //
9cae4067eSGeorge Mitenkov // This file implements passes to convert `gpu.launch_func` op into a sequence
10cae4067eSGeorge Mitenkov // of LLVM calls that emulate the host and device sides.
11cae4067eSGeorge Mitenkov //
12cae4067eSGeorge Mitenkov //===----------------------------------------------------------------------===//
13cae4067eSGeorge Mitenkov 
14cae4067eSGeorge Mitenkov #include "../PassDetail.h"
15*b5d847b1SAlex Zinenko #include "mlir/Conversion/LLVMCommon/LoweringOptions.h"
16*b5d847b1SAlex Zinenko #include "mlir/Conversion/LLVMCommon/TypeConverter.h"
17930c74f1SLei Zhang #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVM.h"
18930c74f1SLei Zhang #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVMPass.h"
19cae4067eSGeorge Mitenkov #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
20cae4067eSGeorge Mitenkov #include "mlir/Dialect/GPU/GPUDialect.h"
21cae4067eSGeorge Mitenkov #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
2201178654SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
23cae4067eSGeorge Mitenkov #include "mlir/Dialect/StandardOps/IR/Ops.h"
2465fcddffSRiver Riddle #include "mlir/IR/BuiltinOps.h"
25cae4067eSGeorge Mitenkov #include "mlir/IR/SymbolTable.h"
26cae4067eSGeorge Mitenkov #include "mlir/Transforms/DialectConversion.h"
27cae4067eSGeorge Mitenkov 
28cae4067eSGeorge Mitenkov #include "llvm/ADT/DenseMap.h"
2956f987faSNico Weber #include "llvm/ADT/StringExtras.h"
30cae4067eSGeorge Mitenkov #include "llvm/Support/FormatVariadic.h"
31cae4067eSGeorge Mitenkov 
32cae4067eSGeorge Mitenkov using namespace mlir;
33cae4067eSGeorge Mitenkov 
34cae4067eSGeorge Mitenkov static constexpr const char kSPIRVModule[] = "__spv__";
35cae4067eSGeorge Mitenkov 
36cae4067eSGeorge Mitenkov //===----------------------------------------------------------------------===//
37cae4067eSGeorge Mitenkov // Utility functions
38cae4067eSGeorge Mitenkov //===----------------------------------------------------------------------===//
39cae4067eSGeorge Mitenkov 
40cae4067eSGeorge Mitenkov /// Returns the string name of the `DescriptorSet` decoration.
41cae4067eSGeorge Mitenkov static std::string descriptorSetName() {
42cae4067eSGeorge Mitenkov   return llvm::convertToSnakeFromCamelCase(
43cae4067eSGeorge Mitenkov       stringifyDecoration(spirv::Decoration::DescriptorSet));
44cae4067eSGeorge Mitenkov }
45cae4067eSGeorge Mitenkov 
46cae4067eSGeorge Mitenkov /// Returns the string name of the `Binding` decoration.
47cae4067eSGeorge Mitenkov static std::string bindingName() {
48cae4067eSGeorge Mitenkov   return llvm::convertToSnakeFromCamelCase(
49cae4067eSGeorge Mitenkov       stringifyDecoration(spirv::Decoration::Binding));
50cae4067eSGeorge Mitenkov }
51cae4067eSGeorge Mitenkov 
52cae4067eSGeorge Mitenkov /// Calculates the index of the kernel's operand that is represented by the
53cae4067eSGeorge Mitenkov /// given global variable with the `bind` attribute. We assume that the index of
54cae4067eSGeorge Mitenkov /// each kernel's operand is mapped to (descriptorSet, binding) by the map:
55cae4067eSGeorge Mitenkov ///   i -> (0, i)
56cae4067eSGeorge Mitenkov /// which is implemented under `LowerABIAttributesPass`.
57cae4067eSGeorge Mitenkov static unsigned calculateGlobalIndex(spirv::GlobalVariableOp op) {
580bf4a82aSChristian Sigg   IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName());
59cae4067eSGeorge Mitenkov   return binding.getInt();
60cae4067eSGeorge Mitenkov }
61cae4067eSGeorge Mitenkov 
62cae4067eSGeorge Mitenkov /// Copies the given number of bytes from src to dst pointers.
63cae4067eSGeorge Mitenkov static void copy(Location loc, Value dst, Value src, Value size,
64cae4067eSGeorge Mitenkov                  OpBuilder &builder) {
65cae4067eSGeorge Mitenkov   MLIRContext *context = builder.getContext();
662230bf99SAlex Zinenko   auto llvmI1Type = IntegerType::get(context, 1);
67cae4067eSGeorge Mitenkov   Value isVolatile = builder.create<LLVM::ConstantOp>(
68cae4067eSGeorge Mitenkov       loc, llvmI1Type, builder.getBoolAttr(false));
69cae4067eSGeorge Mitenkov   builder.create<LLVM::MemcpyOp>(loc, dst, src, size, isVolatile);
70cae4067eSGeorge Mitenkov }
71cae4067eSGeorge Mitenkov 
72cae4067eSGeorge Mitenkov /// Encodes the binding and descriptor set numbers into a new symbolic name.
73cae4067eSGeorge Mitenkov /// The name is specified by
74cae4067eSGeorge Mitenkov ///   {kernel_module_name}_{variable_name}_descriptor_set{ds}_binding{b}
75cae4067eSGeorge Mitenkov /// to avoid symbolic conflicts, where 'ds' and 'b' are descriptor set and
76cae4067eSGeorge Mitenkov /// binding numbers.
77cae4067eSGeorge Mitenkov static std::string
78cae4067eSGeorge Mitenkov createGlobalVariableWithBindName(spirv::GlobalVariableOp op,
79cae4067eSGeorge Mitenkov                                  StringRef kernelModuleName) {
80cae4067eSGeorge Mitenkov   IntegerAttr descriptorSet =
810bf4a82aSChristian Sigg       op->getAttrOfType<IntegerAttr>(descriptorSetName());
820bf4a82aSChristian Sigg   IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName());
83cae4067eSGeorge Mitenkov   return llvm::formatv("{0}_{1}_descriptor_set{2}_binding{3}",
84cae4067eSGeorge Mitenkov                        kernelModuleName.str(), op.sym_name().str(),
85cae4067eSGeorge Mitenkov                        std::to_string(descriptorSet.getInt()),
86cae4067eSGeorge Mitenkov                        std::to_string(binding.getInt()));
87cae4067eSGeorge Mitenkov }
88cae4067eSGeorge Mitenkov 
89cae4067eSGeorge Mitenkov /// Returns true if the given global variable has both a descriptor set number
90cae4067eSGeorge Mitenkov /// and a binding number.
91cae4067eSGeorge Mitenkov static bool hasDescriptorSetAndBinding(spirv::GlobalVariableOp op) {
92cae4067eSGeorge Mitenkov   IntegerAttr descriptorSet =
930bf4a82aSChristian Sigg       op->getAttrOfType<IntegerAttr>(descriptorSetName());
940bf4a82aSChristian Sigg   IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName());
95cae4067eSGeorge Mitenkov   return descriptorSet && binding;
96cae4067eSGeorge Mitenkov }
97cae4067eSGeorge Mitenkov 
98cae4067eSGeorge Mitenkov /// Fills `globalVariableMap` with SPIR-V global variables that represent kernel
99cae4067eSGeorge Mitenkov /// arguments from the given SPIR-V module. We assume that the module contains a
100c74eb466SKareemErgawy-TomTom /// single entry point function. Hence, all `spv.GlobalVariable`s with a bind
101cae4067eSGeorge Mitenkov /// attribute are kernel arguments.
102cae4067eSGeorge Mitenkov static LogicalResult getKernelGlobalVariables(
103cae4067eSGeorge Mitenkov     spirv::ModuleOp module,
104cae4067eSGeorge Mitenkov     DenseMap<uint32_t, spirv::GlobalVariableOp> &globalVariableMap) {
105cae4067eSGeorge Mitenkov   auto entryPoints = module.getOps<spirv::EntryPointOp>();
106cae4067eSGeorge Mitenkov   if (!llvm::hasSingleElement(entryPoints)) {
107cae4067eSGeorge Mitenkov     return module.emitError(
108cae4067eSGeorge Mitenkov         "The module must contain exactly one entry point function");
109cae4067eSGeorge Mitenkov   }
110cae4067eSGeorge Mitenkov   auto globalVariables = module.getOps<spirv::GlobalVariableOp>();
111cae4067eSGeorge Mitenkov   for (auto globalOp : globalVariables) {
112cae4067eSGeorge Mitenkov     if (hasDescriptorSetAndBinding(globalOp))
113cae4067eSGeorge Mitenkov       globalVariableMap[calculateGlobalIndex(globalOp)] = globalOp;
114cae4067eSGeorge Mitenkov   }
115cae4067eSGeorge Mitenkov   return success();
116cae4067eSGeorge Mitenkov }
117cae4067eSGeorge Mitenkov 
118cae4067eSGeorge Mitenkov /// Encodes the SPIR-V module's symbolic name into the name of the entry point
119cae4067eSGeorge Mitenkov /// function.
120cae4067eSGeorge Mitenkov static LogicalResult encodeKernelName(spirv::ModuleOp module) {
121cae4067eSGeorge Mitenkov   StringRef spvModuleName = module.sym_name().getValue();
122cae4067eSGeorge Mitenkov   // We already know that the module contains exactly one entry point function
123cae4067eSGeorge Mitenkov   // based on `getKernelGlobalVariables()` call. Update this function's name
124cae4067eSGeorge Mitenkov   // to:
125cae4067eSGeorge Mitenkov   //   {spv_module_name}_{function_name}
126cae4067eSGeorge Mitenkov   auto entryPoint = *module.getOps<spirv::EntryPointOp>().begin();
127cae4067eSGeorge Mitenkov   StringRef funcName = entryPoint.fn();
128cae4067eSGeorge Mitenkov   auto funcOp = module.lookupSymbol<spirv::FuncOp>(funcName);
129cae4067eSGeorge Mitenkov   std::string newFuncName = spvModuleName.str() + "_" + funcName.str();
130cae4067eSGeorge Mitenkov   if (failed(SymbolTable::replaceAllSymbolUses(funcOp, newFuncName, module)))
131cae4067eSGeorge Mitenkov     return failure();
132cae4067eSGeorge Mitenkov   SymbolTable::setSymbolName(funcOp, newFuncName);
133cae4067eSGeorge Mitenkov   return success();
134cae4067eSGeorge Mitenkov }
135cae4067eSGeorge Mitenkov 
136cae4067eSGeorge Mitenkov //===----------------------------------------------------------------------===//
137cae4067eSGeorge Mitenkov // Conversion patterns
138cae4067eSGeorge Mitenkov //===----------------------------------------------------------------------===//
139cae4067eSGeorge Mitenkov 
140cae4067eSGeorge Mitenkov namespace {
141cae4067eSGeorge Mitenkov 
142cae4067eSGeorge Mitenkov /// Structure to group information about the variables being copied.
143cae4067eSGeorge Mitenkov struct CopyInfo {
144cae4067eSGeorge Mitenkov   Value dst;
145cae4067eSGeorge Mitenkov   Value src;
146cae4067eSGeorge Mitenkov   Value size;
147cae4067eSGeorge Mitenkov };
148cae4067eSGeorge Mitenkov 
149cae4067eSGeorge Mitenkov /// This pattern emulates a call to the kernel in LLVM dialect. For that, we
150cae4067eSGeorge Mitenkov /// copy the data to the global variable (emulating device side), call the
151cae4067eSGeorge Mitenkov /// kernel as a normal void LLVM function, and copy the data back (emulating the
152cae4067eSGeorge Mitenkov /// host side).
153cae4067eSGeorge Mitenkov class GPULaunchLowering : public ConvertOpToLLVMPattern<gpu::LaunchFuncOp> {
154cae4067eSGeorge Mitenkov   using ConvertOpToLLVMPattern<gpu::LaunchFuncOp>::ConvertOpToLLVMPattern;
155cae4067eSGeorge Mitenkov 
156cae4067eSGeorge Mitenkov   LogicalResult
157acb69f3bSChristian Sigg   matchAndRewrite(gpu::LaunchFuncOp launchOp, ArrayRef<Value> operands,
158cae4067eSGeorge Mitenkov                   ConversionPatternRewriter &rewriter) const override {
159acb69f3bSChristian Sigg     auto *op = launchOp.getOperation();
160cae4067eSGeorge Mitenkov     MLIRContext *context = rewriter.getContext();
1610bf4a82aSChristian Sigg     auto module = launchOp->getParentOfType<ModuleOp>();
162cae4067eSGeorge Mitenkov 
163cae4067eSGeorge Mitenkov     // Get the SPIR-V module that represents the gpu kernel module. The module
164cae4067eSGeorge Mitenkov     // is named:
165cae4067eSGeorge Mitenkov     //   __spv__{kernel_module_name}
166cae4067eSGeorge Mitenkov     // based on GPU to SPIR-V conversion.
167cae4067eSGeorge Mitenkov     StringRef kernelModuleName = launchOp.getKernelModuleName();
168cae4067eSGeorge Mitenkov     std::string spvModuleName = kSPIRVModule + kernelModuleName.str();
169cae4067eSGeorge Mitenkov     auto spvModule = module.lookupSymbol<spirv::ModuleOp>(spvModuleName);
170cae4067eSGeorge Mitenkov     if (!spvModule) {
171cae4067eSGeorge Mitenkov       return launchOp.emitOpError("SPIR-V kernel module '")
172cae4067eSGeorge Mitenkov              << spvModuleName << "' is not found";
173cae4067eSGeorge Mitenkov     }
174cae4067eSGeorge Mitenkov 
175cae4067eSGeorge Mitenkov     // Declare kernel function in the main module so that it later can be linked
176cae4067eSGeorge Mitenkov     // with its definition from the kernel module. We know that the kernel
177cae4067eSGeorge Mitenkov     // function would have no arguments and the data is passed via global
178cae4067eSGeorge Mitenkov     // variables. The name of the kernel will be
179cae4067eSGeorge Mitenkov     //   {spv_module_name}_{kernel_function_name}
180cae4067eSGeorge Mitenkov     // to avoid symbolic name conflicts.
181cae4067eSGeorge Mitenkov     StringRef kernelFuncName = launchOp.getKernelName();
182cae4067eSGeorge Mitenkov     std::string newKernelFuncName = spvModuleName + "_" + kernelFuncName.str();
183cae4067eSGeorge Mitenkov     auto kernelFunc = module.lookupSymbol<LLVM::LLVMFuncOp>(newKernelFuncName);
184cae4067eSGeorge Mitenkov     if (!kernelFunc) {
185cae4067eSGeorge Mitenkov       OpBuilder::InsertionGuard guard(rewriter);
186cae4067eSGeorge Mitenkov       rewriter.setInsertionPointToStart(module.getBody());
187cae4067eSGeorge Mitenkov       kernelFunc = rewriter.create<LLVM::LLVMFuncOp>(
188cae4067eSGeorge Mitenkov           rewriter.getUnknownLoc(), newKernelFuncName,
1897ed9cfc7SAlex Zinenko           LLVM::LLVMFunctionType::get(LLVM::LLVMVoidType::get(context),
190c69c9e0fSAlex Zinenko                                       ArrayRef<Type>()));
191cae4067eSGeorge Mitenkov       rewriter.setInsertionPoint(launchOp);
192cae4067eSGeorge Mitenkov     }
193cae4067eSGeorge Mitenkov 
194cae4067eSGeorge Mitenkov     // Get all global variables associated with the kernel operands.
195cae4067eSGeorge Mitenkov     DenseMap<uint32_t, spirv::GlobalVariableOp> globalVariableMap;
196cae4067eSGeorge Mitenkov     if (failed(getKernelGlobalVariables(spvModule, globalVariableMap)))
197cae4067eSGeorge Mitenkov       return failure();
198cae4067eSGeorge Mitenkov 
199cae4067eSGeorge Mitenkov     // Traverse kernel operands that were converted to MemRefDescriptors. For
200cae4067eSGeorge Mitenkov     // each operand, create a global variable and copy data from operand to it.
201cae4067eSGeorge Mitenkov     Location loc = launchOp.getLoc();
202cae4067eSGeorge Mitenkov     SmallVector<CopyInfo, 4> copyInfo;
203cae4067eSGeorge Mitenkov     auto numKernelOperands = launchOp.getNumKernelOperands();
204cae4067eSGeorge Mitenkov     auto kernelOperands = operands.take_back(numKernelOperands);
205cae4067eSGeorge Mitenkov     for (auto operand : llvm::enumerate(kernelOperands)) {
206f88fab50SKazuaki Ishizaki       // Check if the kernel's operand is a ranked memref.
207cae4067eSGeorge Mitenkov       auto memRefType = launchOp.getKernelOperand(operand.index())
208cae4067eSGeorge Mitenkov                             .getType()
209cae4067eSGeorge Mitenkov                             .dyn_cast<MemRefType>();
210cae4067eSGeorge Mitenkov       if (!memRefType)
211cae4067eSGeorge Mitenkov         return failure();
212cae4067eSGeorge Mitenkov 
213cae4067eSGeorge Mitenkov       // Calculate the size of the memref and get the pointer to the allocated
214cae4067eSGeorge Mitenkov       // buffer.
215cae4067eSGeorge Mitenkov       SmallVector<Value, 4> sizes;
216bedaad44SChristian Sigg       SmallVector<Value, 4> strides;
217bedaad44SChristian Sigg       Value sizeBytes;
218d59ddba7SChristian Sigg       getMemRefDescriptorSizes(loc, memRefType, {}, rewriter, sizes, strides,
219d59ddba7SChristian Sigg                                sizeBytes);
220cae4067eSGeorge Mitenkov       MemRefDescriptor descriptor(operand.value());
221cae4067eSGeorge Mitenkov       Value src = descriptor.allocatedPtr(rewriter, loc);
222cae4067eSGeorge Mitenkov 
223cae4067eSGeorge Mitenkov       // Get the global variable in the SPIR-V module that is associated with
224cae4067eSGeorge Mitenkov       // the kernel operand. Construct its new name and create a corresponding
225cae4067eSGeorge Mitenkov       // LLVM dialect global variable.
226cae4067eSGeorge Mitenkov       spirv::GlobalVariableOp spirvGlobal = globalVariableMap[operand.index()];
227cae4067eSGeorge Mitenkov       auto pointeeType =
228cae4067eSGeorge Mitenkov           spirvGlobal.type().cast<spirv::PointerType>().getPointeeType();
229dcec2ca5SChristian Sigg       auto dstGlobalType = typeConverter->convertType(pointeeType);
230cae4067eSGeorge Mitenkov       if (!dstGlobalType)
231cae4067eSGeorge Mitenkov         return failure();
232cae4067eSGeorge Mitenkov       std::string name =
233cae4067eSGeorge Mitenkov           createGlobalVariableWithBindName(spirvGlobal, spvModuleName);
234cae4067eSGeorge Mitenkov       // Check if this variable has already been created.
235cae4067eSGeorge Mitenkov       auto dstGlobal = module.lookupSymbol<LLVM::GlobalOp>(name);
236cae4067eSGeorge Mitenkov       if (!dstGlobal) {
237cae4067eSGeorge Mitenkov         OpBuilder::InsertionGuard guard(rewriter);
238cae4067eSGeorge Mitenkov         rewriter.setInsertionPointToStart(module.getBody());
239cae4067eSGeorge Mitenkov         dstGlobal = rewriter.create<LLVM::GlobalOp>(
240c69c9e0fSAlex Zinenko             loc, dstGlobalType,
2419a0ea599SDumitru Potop             /*isConstant=*/false, LLVM::Linkage::Linkonce, name, Attribute(),
2429a0ea599SDumitru Potop             /*alignment=*/0);
243cae4067eSGeorge Mitenkov         rewriter.setInsertionPoint(launchOp);
244cae4067eSGeorge Mitenkov       }
245cae4067eSGeorge Mitenkov 
246cae4067eSGeorge Mitenkov       // Copy the data from src operand pointer to dst global variable. Save
247cae4067eSGeorge Mitenkov       // src, dst and size so that we can copy data back after emulating the
248cae4067eSGeorge Mitenkov       // kernel call.
249cae4067eSGeorge Mitenkov       Value dst = rewriter.create<LLVM::AddressOfOp>(loc, dstGlobal);
250bedaad44SChristian Sigg       copy(loc, dst, src, sizeBytes, rewriter);
251cae4067eSGeorge Mitenkov 
252cae4067eSGeorge Mitenkov       CopyInfo info;
253cae4067eSGeorge Mitenkov       info.dst = dst;
254cae4067eSGeorge Mitenkov       info.src = src;
255bedaad44SChristian Sigg       info.size = sizeBytes;
256cae4067eSGeorge Mitenkov       copyInfo.push_back(info);
257cae4067eSGeorge Mitenkov     }
258cae4067eSGeorge Mitenkov     // Create a call to the kernel and copy the data back.
259cae4067eSGeorge Mitenkov     rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, kernelFunc,
260cae4067eSGeorge Mitenkov                                               ArrayRef<Value>());
261cae4067eSGeorge Mitenkov     for (CopyInfo info : copyInfo)
262cae4067eSGeorge Mitenkov       copy(loc, info.src, info.dst, info.size, rewriter);
263cae4067eSGeorge Mitenkov     return success();
264cae4067eSGeorge Mitenkov   }
265cae4067eSGeorge Mitenkov };
266cae4067eSGeorge Mitenkov 
267cae4067eSGeorge Mitenkov class LowerHostCodeToLLVM
268cae4067eSGeorge Mitenkov     : public LowerHostCodeToLLVMBase<LowerHostCodeToLLVM> {
269cae4067eSGeorge Mitenkov public:
270cae4067eSGeorge Mitenkov   void runOnOperation() override {
271cae4067eSGeorge Mitenkov     ModuleOp module = getOperation();
272cae4067eSGeorge Mitenkov 
273cae4067eSGeorge Mitenkov     // Erase the GPU module.
274cae4067eSGeorge Mitenkov     for (auto gpuModule :
275cae4067eSGeorge Mitenkov          llvm::make_early_inc_range(module.getOps<gpu::GPUModuleOp>()))
276cae4067eSGeorge Mitenkov       gpuModule.erase();
277cae4067eSGeorge Mitenkov 
278cae4067eSGeorge Mitenkov     // Specify options to lower Standard to LLVM and pull in the conversion
279cae4067eSGeorge Mitenkov     // patterns.
280b3386a73SAlex Zinenko     LowerToLLVMOptions options(module.getContext());
281b3386a73SAlex Zinenko     options.emitCWrappers = true;
282cae4067eSGeorge Mitenkov     auto *context = module.getContext();
283dc4e913bSChris Lattner     RewritePatternSet patterns(context);
284cae4067eSGeorge Mitenkov     LLVMTypeConverter typeConverter(context, options);
285cae4067eSGeorge Mitenkov     populateStdToLLVMConversionPatterns(typeConverter, patterns);
286dc4e913bSChris Lattner     patterns.add<GPULaunchLowering>(typeConverter);
287cae4067eSGeorge Mitenkov 
288cae4067eSGeorge Mitenkov     // Pull in SPIR-V type conversion patterns to convert SPIR-V global
289cae4067eSGeorge Mitenkov     // variable's type to LLVM dialect type.
290cae4067eSGeorge Mitenkov     populateSPIRVToLLVMTypeConversion(typeConverter);
291cae4067eSGeorge Mitenkov 
292cae4067eSGeorge Mitenkov     ConversionTarget target(*context);
293cae4067eSGeorge Mitenkov     target.addLegalDialect<LLVM::LLVMDialect>();
2943fffffa8SRiver Riddle     if (failed(applyPartialConversion(module, target, std::move(patterns))))
295cae4067eSGeorge Mitenkov       signalPassFailure();
296cae4067eSGeorge Mitenkov 
297cae4067eSGeorge Mitenkov     // Finally, modify the kernel function in SPIR-V modules to avoid symbolic
298cae4067eSGeorge Mitenkov     // conflicts.
299cae4067eSGeorge Mitenkov     for (auto spvModule : module.getOps<spirv::ModuleOp>())
300e21adfa3SRiver Riddle       (void)encodeKernelName(spvModule);
301cae4067eSGeorge Mitenkov   }
302cae4067eSGeorge Mitenkov };
303cae4067eSGeorge Mitenkov } // namespace
304cae4067eSGeorge Mitenkov 
305cae4067eSGeorge Mitenkov std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
306cae4067eSGeorge Mitenkov mlir::createLowerHostCodeToLLVMPass() {
307cae4067eSGeorge Mitenkov   return std::make_unique<LowerHostCodeToLLVM>();
308cae4067eSGeorge Mitenkov }
309