MLIR  16.0.0git
GPUOpsLowering.h
Go to the documentation of this file.
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 
14 
15 namespace mlir {
16 
17 struct GPUFuncOpLowering : ConvertOpToLLVMPattern<gpu::GPUFuncOp> {
18  GPUFuncOpLowering(LLVMTypeConverter &converter, unsigned allocaAddrSpace,
19  StringAttr kernelAttributeName)
20  : ConvertOpToLLVMPattern<gpu::GPUFuncOp>(converter),
21  allocaAddrSpace(allocaAddrSpace),
22  kernelAttributeName(kernelAttributeName) {}
23 
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> {
43 
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
56  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
58  int addressSpace = 0)
59  : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter),
60  addressSpace(addressSpace) {}
61 
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> {
72 
74  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_
Include the generated interface declarations.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition: Pattern.h:133
typename gpu::GPUFuncOp ::Adaptor OpAdaptor
Definition: Pattern.h:135
The lowering of gpu.printf to a call to an external printf() function.
GPUPrintfOpToLLVMCallLowering(LLVMTypeConverter &converter, int addressSpace=0)
LogicalResult matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
The lowering of gpu.printf to a call to HIP hostcalls.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replaces the result op with a new op that is created without verification.
Definition: PatternMatch.h:451
LogicalResult matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:30
This class implements a pattern rewriter for use with ConversionPatterns.
GPUFuncOpLowering(LLVMTypeConverter &converter, unsigned allocaAddrSpace, StringAttr kernelAttributeName)