xref: /llvm-project/mlir/include/mlir/Conversion/GPUCommon/GPUCommonPass.h (revision 733be4ed7dcf976719f424c0cb81b77a14f91f5a)
1 //===- GPUCommonPass.h - MLIR GPU runtime support -------------------------===//
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 #ifndef MLIR_CONVERSION_GPUCOMMON_GPUCOMMONPASS_H_
9 #define MLIR_CONVERSION_GPUCOMMON_GPUCOMMONPASS_H_
10 
11 #include "mlir/Dialect/GPU/Utils/GPUUtils.h"
12 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
13 #include "mlir/IR/Builders.h"
14 #include "mlir/IR/Types.h"
15 #include "mlir/Support/LLVM.h"
16 #include "llvm/ADT/StringRef.h"
17 #include <functional>
18 #include <vector>
19 
20 namespace llvm {
21 class LLVMContext;
22 class Module;
23 } // namespace llvm
24 
25 namespace mlir {
26 
27 class LLVMTypeConverter;
28 class Location;
29 class ModuleOp;
30 class Operation;
31 class RewritePatternSet;
32 class TypeConverter;
33 
34 class Pass;
35 
36 namespace gpu {
37 enum class AddressSpace : uint32_t;
38 class GPUModuleOp;
39 } // namespace gpu
40 
41 namespace LLVM {
42 class LLVMDialect;
43 } // namespace LLVM
44 
45 #define GEN_PASS_DECL_GPUTOLLVMCONVERSIONPASS
46 #include "mlir/Conversion/Passes.h.inc"
47 
48 using LoweringCallback = std::function<std::unique_ptr<llvm::Module>(
49     Operation *, llvm::LLVMContext &, StringRef)>;
50 
51 struct FunctionCallBuilder {
52   FunctionCallBuilder(StringRef functionName, Type returnType,
53                       ArrayRef<Type> argumentTypes)
54       : functionName(functionName),
55         functionType(LLVM::LLVMFunctionType::get(returnType, argumentTypes)) {}
56   LLVM::CallOp create(Location loc, OpBuilder &builder,
57                       ArrayRef<Value> arguments) const;
58 
59   StringRef functionName;
60   LLVM::LLVMFunctionType functionType;
61 };
62 
63 /// Collect a set of patterns to convert from the GPU dialect to LLVM and
64 /// populate converter for gpu types.
65 void populateGpuToLLVMConversionPatterns(
66     LLVMTypeConverter &converter, RewritePatternSet &patterns,
67     bool kernelBarePtrCallConv = false,
68     bool kernelIntersperseSizeCallConv = false);
69 
70 /// A function that maps a MemorySpace enum to a target-specific integer value.
71 using MemorySpaceMapping = std::function<unsigned(gpu::AddressSpace)>;
72 
73 /// Populates memory space attribute conversion rules for lowering
74 /// gpu.address_space to integer values.
75 void populateGpuMemorySpaceAttributeConversions(
76     TypeConverter &typeConverter, const MemorySpaceMapping &mapping);
77 } // namespace mlir
78 
79 #endif // MLIR_CONVERSION_GPUCOMMON_GPUCOMMONPASS_H_
80