//===- ConvertLaunchFuncToLLVMCalls.cpp - MLIR GPU launch to LLVM pass ----===// // // 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 passes to convert `gpu.launch_func` op into a sequence // of LLVM calls that emulate the host and device sides. // //===----------------------------------------------------------------------===// #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVMPass.h" #include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" #include "mlir/Conversion/LLVMCommon/LoweringOptions.h" #include "mlir/Conversion/LLVMCommon/Pattern.h" #include "mlir/Conversion/LLVMCommon/TypeConverter.h" #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVM.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/GPU/IR/GPUDialect.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/SymbolTable.h" #include "mlir/Pass/Pass.h" #include "mlir/Transforms/DialectConversion.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/FormatVariadic.h" namespace mlir { #define GEN_PASS_DEF_LOWERHOSTCODETOLLVMPASS #include "mlir/Conversion/Passes.h.inc" } // namespace mlir usingnamespacemlir; static constexpr const char kSPIRVModule[] = …; //===----------------------------------------------------------------------===// // Utility functions //===----------------------------------------------------------------------===// /// Returns the string name of the `DescriptorSet` decoration. static std::string descriptorSetName() { … } /// Returns the string name of the `Binding` decoration. static std::string bindingName() { … } /// Calculates the index of the kernel's operand that is represented by the /// given global variable with the `bind` attribute. We assume that the index of /// each kernel's operand is mapped to (descriptorSet, binding) by the map: /// i -> (0, i) /// which is implemented under `LowerABIAttributesPass`. static unsigned calculateGlobalIndex(spirv::GlobalVariableOp op) { … } /// Copies the given number of bytes from src to dst pointers. static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder) { … } /// Encodes the binding and descriptor set numbers into a new symbolic name. /// The name is specified by /// {kernel_module_name}_{variable_name}_descriptor_set{ds}_binding{b} /// to avoid symbolic conflicts, where 'ds' and 'b' are descriptor set and /// binding numbers. static std::string createGlobalVariableWithBindName(spirv::GlobalVariableOp op, StringRef kernelModuleName) { … } /// Returns true if the given global variable has both a descriptor set number /// and a binding number. static bool hasDescriptorSetAndBinding(spirv::GlobalVariableOp op) { … } /// Fills `globalVariableMap` with SPIR-V global variables that represent kernel /// arguments from the given SPIR-V module. We assume that the module contains a /// single entry point function. Hence, all `spirv.GlobalVariable`s with a bind /// attribute are kernel arguments. static LogicalResult getKernelGlobalVariables( spirv::ModuleOp module, DenseMap<uint32_t, spirv::GlobalVariableOp> &globalVariableMap) { … } /// Encodes the SPIR-V module's symbolic name into the name of the entry point /// function. static LogicalResult encodeKernelName(spirv::ModuleOp module) { … } //===----------------------------------------------------------------------===// // Conversion patterns //===----------------------------------------------------------------------===// namespace { /// Structure to group information about the variables being copied. struct CopyInfo { … }; /// This pattern emulates a call to the kernel in LLVM dialect. For that, we /// copy the data to the global variable (emulating device side), call the /// kernel as a normal void LLVM function, and copy the data back (emulating the /// host side). class GPULaunchLowering : public ConvertOpToLLVMPattern<gpu::LaunchFuncOp> { … }; class LowerHostCodeToLLVM : public impl::LowerHostCodeToLLVMPassBase<LowerHostCodeToLLVM> { … }; } // namespace