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/Support/LLVM.h"
12 #include "llvm/IR/Module.h"
13 #include <vector>
14 
15 namespace mlir {
16 
17 class LLVMTypeConverter;
18 class Location;
19 struct LogicalResult;
20 class ModuleOp;
21 class Operation;
22 class OwningRewritePatternList;
23 
24 template <typename T>
25 class OperationPass;
26 
27 namespace gpu {
28 class GPUModuleOp;
29 } // namespace gpu
30 
31 namespace LLVM {
32 class LLVMDialect;
33 } // namespace LLVM
34 
35 using OwnedBlob = std::unique_ptr<std::vector<char>>;
36 using BlobGenerator =
37     std::function<OwnedBlob(const std::string &, Location, StringRef)>;
38 using LoweringCallback = std::function<std::unique_ptr<llvm::Module>(
39     Operation *, llvm::LLVMContext &, StringRef)>;
40 
41 /// Creates a pass to convert a gpu.launch_func operation into a sequence of
42 /// GPU runtime calls.
43 ///
44 /// This pass does not generate code to call GPU runtime APIs directly but
45 /// instead uses a small wrapper library that exports a stable and conveniently
46 /// typed ABI on top of GPU runtimes such as CUDA or ROCm (HIP).
47 std::unique_ptr<OperationPass<ModuleOp>>
48 createGpuToLLVMConversionPass(StringRef gpuBinaryAnnotation = "");
49 
50 /// Collect a set of patterns to convert from the GPU dialect to LLVM.
51 void populateGpuToLLVMConversionPatterns(LLVMTypeConverter &converter,
52                                          OwningRewritePatternList &patterns,
53                                          StringRef gpuBinaryAnnotation);
54 
55 /// Creates a pass to convert kernel functions into GPU target object blobs.
56 ///
57 /// This transformation takes the body of each function that is annotated with
58 /// the 'gpu.kernel' attribute, copies it to a new LLVM module, compiles the
59 /// module with help of the GPU backend to target object and then invokes
60 /// the provided blobGenerator to produce a binary blob. Such blob is then
61 /// attached as a string attribute to the kernel function.
62 ///
63 /// Following callbacks are to be provided by user:
64 /// - loweringCallback : lower the module to an LLVM module.
65 /// - blobGenerator : build a blob executable on target GPU.
66 ///
67 /// Information wrt LLVM backend are to be supplied by user:
68 /// - triple : target triple to be used.
69 /// - targetChip : mcpu to be used.
70 /// - features : target-specific features to be used.
71 ///
72 /// Information about result attribute is to be specified by user:
73 /// - gpuBinaryAnnotation : the name of the attribute which contains the blob.
74 ///
75 /// After the transformation, the body of the kernel function is removed (i.e.,
76 /// it is turned into a declaration).
77 std::unique_ptr<OperationPass<gpu::GPUModuleOp>>
78 createConvertGPUKernelToBlobPass(LoweringCallback loweringCallback,
79                                  BlobGenerator blobGenerator, StringRef triple,
80                                  StringRef targetChip, StringRef features,
81                                  StringRef gpuBinaryAnnotation);
82 
83 } // namespace mlir
84 
85 #endif // MLIR_CONVERSION_GPUCOMMON_GPUCOMMONPASS_H_
86