1 //===- GPUOpsLowering.h - GPU FuncOp / ReturnOp lowering -------*- C++ -*--===//
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_GPUOPSLOWERING_H_
9 #define MLIR_CONVERSION_GPUCOMMON_GPUOPSLOWERING_H_
10 
11 #include "mlir/Conversion/LLVMCommon/Pattern.h"
12 #include "mlir/Dialect/GPU/IR/GPUDialect.h"
13 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
14 
15 namespace mlir {
16 
17 struct GPUFuncOpLowering : ConvertOpToLLVMPattern<gpu::GPUFuncOp> {
GPUFuncOpLoweringGPUFuncOpLowering18   GPUFuncOpLowering(LLVMTypeConverter &converter, unsigned allocaAddrSpace,
19                     StringAttr kernelAttributeName)
20       : ConvertOpToLLVMPattern<gpu::GPUFuncOp>(converter),
21         allocaAddrSpace(allocaAddrSpace),
22         kernelAttributeName(kernelAttributeName) {}
23 
24   LogicalResult
25   matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
26                   ConversionPatternRewriter &rewriter) const override;
27 
28 private:
29   /// The address spcae to use for `alloca`s in private memory.
30   unsigned allocaAddrSpace;
31 
32   /// The attribute name to use instead of `gpu.kernel`.
33   StringAttr kernelAttributeName;
34 };
35 
36 /// The lowering of gpu.printf to a call to HIP hostcalls
37 ///
38 /// Simplifies llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp, as we don't have
39 /// to deal with %s (even if there were first-class strings in MLIR, they're not
40 /// legal input to gpu.printf) or non-constant format strings
41 struct GPUPrintfOpToHIPLowering : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
42   using ConvertOpToLLVMPattern<gpu::PrintfOp>::ConvertOpToLLVMPattern;
43 
44   LogicalResult
45   matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
46                   ConversionPatternRewriter &rewriter) const override;
47 };
48 
49 /// The lowering of gpu.printf to a call to an external printf() function
50 ///
51 /// This pass will add a declaration of printf() to the GPUModule if needed
52 /// and seperate out the format strings into global constants. For some
53 /// runtimes, such as OpenCL on AMD, this is sufficient setup, as the compiler
54 /// will lower printf calls to appropriate device-side code
55 struct GPUPrintfOpToLLVMCallLowering
56     : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
57   GPUPrintfOpToLLVMCallLowering(LLVMTypeConverter &converter,
58                                 int addressSpace = 0)
59       : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter),
60         addressSpace(addressSpace) {}
61 
62   LogicalResult
63   matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
64                   ConversionPatternRewriter &rewriter) const override;
65 
66 private:
67   int addressSpace;
68 };
69 
70 struct GPUReturnOpLowering : public ConvertOpToLLVMPattern<gpu::ReturnOp> {
71   using ConvertOpToLLVMPattern<gpu::ReturnOp>::ConvertOpToLLVMPattern;
72 
73   LogicalResult
matchAndRewriteGPUReturnOpLowering74   matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor,
75                   ConversionPatternRewriter &rewriter) const override {
76     rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, adaptor.getOperands());
77     return success();
78   }
79 };
80 
81 } // namespace mlir
82 
83 #endif // MLIR_CONVERSION_GPUCOMMON_GPUOPSLOWERING_H_
84