MLIR  19.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 /// Lowering for gpu.dynamic.shared.memory to LLVM dialect. The pattern first
18 /// create a 0-sized global array symbol similar as LLVM expects. It constructs
19 /// a memref descriptor with these values and return it.
21  : public ConvertOpToLLVMPattern<gpu::DynamicSharedMemoryOp> {
23  gpu::DynamicSharedMemoryOp>::ConvertOpToLLVMPattern;
25  unsigned alignmentBit = 0)
26  : ConvertOpToLLVMPattern<gpu::DynamicSharedMemoryOp>(converter),
27  alignmentBit(alignmentBit) {}
28 
30  matchAndRewrite(gpu::DynamicSharedMemoryOp op, OpAdaptor adaptor,
31  ConversionPatternRewriter &rewriter) const override;
32 
33 private:
34  // Alignment bit
35  unsigned alignmentBit;
36 };
37 
38 struct GPUFuncOpLowering : ConvertOpToLLVMPattern<gpu::GPUFuncOp> {
40  const LLVMTypeConverter &converter, unsigned allocaAddrSpace,
41  unsigned workgroupAddrSpace, StringAttr kernelAttributeName,
42  std::optional<StringAttr> kernelBlockSizeAttributeName = std::nullopt)
43  : ConvertOpToLLVMPattern<gpu::GPUFuncOp>(converter),
44  allocaAddrSpace(allocaAddrSpace),
45  workgroupAddrSpace(workgroupAddrSpace),
46  kernelAttributeName(kernelAttributeName),
47  kernelBlockSizeAttributeName(kernelBlockSizeAttributeName) {}
48 
50  matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
51  ConversionPatternRewriter &rewriter) const override;
52 
53 private:
54  /// The address space to use for `alloca`s in private memory.
55  unsigned allocaAddrSpace;
56  /// The address space to use declaring workgroup memory.
57  unsigned workgroupAddrSpace;
58 
59  /// The attribute name to use instead of `gpu.kernel`.
60  StringAttr kernelAttributeName;
61 
62  /// The attribute name to to set block size
63  std::optional<StringAttr> kernelBlockSizeAttributeName;
64 };
65 
66 /// The lowering of gpu.printf to a call to HIP hostcalls
67 ///
68 /// Simplifies llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp, as we don't have
69 /// to deal with %s (even if there were first-class strings in MLIR, they're not
70 /// legal input to gpu.printf) or non-constant format strings
71 struct GPUPrintfOpToHIPLowering : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
73 
75  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
76  ConversionPatternRewriter &rewriter) const override;
77 };
78 
79 /// The lowering of gpu.printf to a call to an external printf() function
80 ///
81 /// This pass will add a declaration of printf() to the GPUModule if needed
82 /// and seperate out the format strings into global constants. For some
83 /// runtimes, such as OpenCL on AMD, this is sufficient setup, as the compiler
84 /// will lower printf calls to appropriate device-side code
86  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
88  int addressSpace = 0)
89  : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter),
90  addressSpace(addressSpace) {}
91 
93  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
94  ConversionPatternRewriter &rewriter) const override;
95 
96 private:
97  int addressSpace;
98 };
99 
100 /// Lowering of gpu.printf to a vprintf standard library.
102  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
104 
106  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
107  ConversionPatternRewriter &rewriter) const override;
108 };
109 
110 struct GPUReturnOpLowering : public ConvertOpToLLVMPattern<gpu::ReturnOp> {
112 
114  matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor,
115  ConversionPatternRewriter &rewriter) const override {
116  rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, adaptor.getOperands());
117  return success();
118  }
119 };
120 
121 namespace impl {
122 /// Unrolls op if it's operating on vectors.
123 LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands,
124  ConversionPatternRewriter &rewriter,
125  const LLVMTypeConverter &converter);
126 } // namespace impl
127 
128 /// Rewriting that unrolls SourceOp to scalars if it's operating on vectors.
129 template <typename SourceOp>
131 public:
133 
135  matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor,
136  ConversionPatternRewriter &rewriter) const override {
137  return impl::scalarizeVectorOp(op, adaptor.getOperands(), rewriter,
138  *this->getTypeConverter());
139  }
140 };
141 } // namespace mlir
142 
143 #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:139
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition: Pattern.h:143
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:34
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replaces the result op with a new op that is created without verification.
Definition: PatternMatch.h:537
LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands, ConversionPatternRewriter &rewriter, const 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
Lowering for gpu.dynamic.shared.memory to LLVM dialect.
LogicalResult matchAndRewrite(gpu::DynamicSharedMemoryOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUDynamicSharedMemoryOpLowering(const LLVMTypeConverter &converter, unsigned alignmentBit=0)
LogicalResult matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUFuncOpLowering(const LLVMTypeConverter &converter, unsigned allocaAddrSpace, unsigned workgroupAddrSpace, StringAttr kernelAttributeName, std::optional< StringAttr > kernelBlockSizeAttributeName=std::nullopt)
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(const 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