MLIR  17.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  unsigned workgroupAddrSpace, StringAttr kernelAttributeName)
20  : ConvertOpToLLVMPattern<gpu::GPUFuncOp>(converter),
21  allocaAddrSpace(allocaAddrSpace),
22  workgroupAddrSpace(workgroupAddrSpace),
23  kernelAttributeName(kernelAttributeName) {}
24 
26  matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
27  ConversionPatternRewriter &rewriter) const override;
28 
29 private:
30  /// The address space to use for `alloca`s in private memory.
31  unsigned allocaAddrSpace;
32  /// The address space to use declaring workgroup memory.
33  unsigned workgroupAddrSpace;
34 
35  /// The attribute name to use instead of `gpu.kernel`.
36  StringAttr kernelAttributeName;
37 };
38 
39 /// The lowering of gpu.printf to a call to HIP hostcalls
40 ///
41 /// Simplifies llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp, as we don't have
42 /// to deal with %s (even if there were first-class strings in MLIR, they're not
43 /// legal input to gpu.printf) or non-constant format strings
44 struct GPUPrintfOpToHIPLowering : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
46 
48  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
49  ConversionPatternRewriter &rewriter) const override;
50 };
51 
52 /// The lowering of gpu.printf to a call to an external printf() function
53 ///
54 /// This pass will add a declaration of printf() to the GPUModule if needed
55 /// and seperate out the format strings into global constants. For some
56 /// runtimes, such as OpenCL on AMD, this is sufficient setup, as the compiler
57 /// will lower printf calls to appropriate device-side code
59  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
61  int addressSpace = 0)
62  : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter),
63  addressSpace(addressSpace) {}
64 
66  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
67  ConversionPatternRewriter &rewriter) const override;
68 
69 private:
70  int addressSpace;
71 };
72 
73 /// Lowering of gpu.printf to a vprintf standard library.
75  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
77 
79  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
80  ConversionPatternRewriter &rewriter) const override;
81 };
82 
83 struct GPUReturnOpLowering : public ConvertOpToLLVMPattern<gpu::ReturnOp> {
85 
87  matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor,
88  ConversionPatternRewriter &rewriter) const override {
89  rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, adaptor.getOperands());
90  return success();
91  }
92 };
93 
94 namespace impl {
95 /// Unrolls op if it's operating on vectors.
96 LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands,
97  ConversionPatternRewriter &rewriter,
98  LLVMTypeConverter &converter);
99 } // namespace impl
100 
101 /// Rewriting that unrolls SourceOp to scalars if it's operating on vectors.
102 template <typename SourceOp>
104 public:
106 
108  matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor,
109  ConversionPatternRewriter &rewriter) const override {
110  return impl::scalarizeVectorOp(op, adaptor.getOperands(), rewriter,
111  *this->getTypeConverter());
112  }
113 };
114 
115 /// A function that maps a MemorySpace enum to a target-specific integer value.
117  std::function<unsigned(gpu::AddressSpace gpuAddressSpace)>;
118 
119 /// Populates memory space attribute conversion rules for lowering
120 /// gpu.address_space to integer values.
122  TypeConverter &typeConverter, const MemorySpaceMapping &mapping);
123 } // namespace mlir
124 
125 #endif // MLIR_CONVERSION_GPUCOMMON_GPUOPSLOWERING_H_
This class implements a pattern rewriter for use with ConversionPatterns.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition: Pattern.h:135
typename SourceOp::Adaptor OpAdaptor
Definition: Pattern.h:137
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:33
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replaces the result op with a new op that is created without verification.
Definition: PatternMatch.h:482
Type conversion class.
LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands, ConversionPatternRewriter &rewriter, LLVMTypeConverter &converter)
Unrolls op if it's operating on vectors.
Include the generated interface declarations.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
void populateGpuMemorySpaceAttributeConversions(TypeConverter &typeConverter, const MemorySpaceMapping &mapping)
Populates memory space attribute conversion rules for lowering gpu.address_space to integer values.
std::function< unsigned(gpu::AddressSpace gpuAddressSpace)> MemorySpaceMapping
A function that maps a MemorySpace enum to a target-specific integer value.
LogicalResult matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUFuncOpLowering(LLVMTypeConverter &converter, unsigned allocaAddrSpace, unsigned workgroupAddrSpace, StringAttr kernelAttributeName)
The lowering of gpu.printf to a call to HIP hostcalls.
LogicalResult matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
The lowering of gpu.printf to a call to an external printf() function.
GPUPrintfOpToLLVMCallLowering(LLVMTypeConverter &converter, int addressSpace=0)
LogicalResult matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
Lowering of gpu.printf to a vprintf standard library.
LogicalResult matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
LogicalResult matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Rewriting that unrolls SourceOp to scalars if it's operating on vectors.
LogicalResult matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override