MLIR 22.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
15
16namespace mlir {
17
18//===----------------------------------------------------------------------===//
19// Helper Functions
20//===----------------------------------------------------------------------===//
21
22/// Note that these functions don't take a `SymbolTable` because GPU module
23/// lowerings can have name collisions as an intermediate state.
24
25/// Find or create an external function declaration in the given module.
26LLVM::LLVMFuncOp getOrDefineFunction(Operation *moduleOp, Location loc,
27 OpBuilder &b, StringRef name,
28 LLVM::LLVMFunctionType type);
29
30/// Create a global that contains the given string. If a global with the same
31/// string already exists in the module, return that global.
33 Operation *moduleOp, Type llvmI8,
34 StringRef namePrefix, StringRef str,
35 uint64_t alignment = 0,
36 unsigned addrSpace = 0);
37
38//===----------------------------------------------------------------------===//
39// Lowering Patterns
40//===----------------------------------------------------------------------===//
41
42/// Lowering for gpu.dynamic.shared.memory to LLVM dialect. The pattern first
43/// create a 0-sized global array symbol similar as LLVM expects. It constructs
44/// a memref descriptor with these values and return it.
46 : public ConvertOpToLLVMPattern<gpu::DynamicSharedMemoryOp> {
48 gpu::DynamicSharedMemoryOp>::ConvertOpToLLVMPattern;
50 unsigned alignmentBit = 0,
51 PatternBenefit benefit = 1)
52 : ConvertOpToLLVMPattern<gpu::DynamicSharedMemoryOp>(converter, benefit),
53 alignmentBit(alignmentBit) {}
54
55 LogicalResult
56 matchAndRewrite(gpu::DynamicSharedMemoryOp op, OpAdaptor adaptor,
57 ConversionPatternRewriter &rewriter) const override;
58
59private:
60 // Alignment bit
61 unsigned alignmentBit;
62};
63
65 /// The address space to use for `alloca`s in private memory.
67 /// The address space to use declaring workgroup memory.
69
70 /// The attribute name to use instead of `gpu.kernel`. Null if no attribute
71 /// should be used.
73 /// The attribute name to to set block size. Null if no attribute should be
74 /// used.
76
77 /// The calling convention to use for kernel functions.
78 LLVM::CConv kernelCallingConvention = LLVM::CConv::C;
79 /// The calling convention to use for non-kernel functions.
80 LLVM::CConv nonKernelCallingConvention = LLVM::CConv::C;
81
82 /// Whether to encode workgroup attributions as additional arguments instead
83 /// of a global variable.
85};
86
87struct GPUFuncOpLowering : ConvertOpToLLVMPattern<gpu::GPUFuncOp> {
90 PatternBenefit benefit = 1)
91 : ConvertOpToLLVMPattern<gpu::GPUFuncOp>(converter, benefit),
92 allocaAddrSpace(options.allocaAddrSpace),
93 workgroupAddrSpace(options.workgroupAddrSpace),
94 kernelAttributeName(options.kernelAttributeName),
95 kernelBlockSizeAttributeName(options.kernelBlockSizeAttributeName),
96 kernelCallingConvention(options.kernelCallingConvention),
97 nonKernelCallingConvention(options.nonKernelCallingConvention),
98 encodeWorkgroupAttributionsAsArguments(
99 options.encodeWorkgroupAttributionsAsArguments) {}
100
101 LogicalResult
102 matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
103 ConversionPatternRewriter &rewriter) const override;
104
105private:
106 /// The address space to use for `alloca`s in private memory.
107 unsigned allocaAddrSpace;
108 /// The address space to use declaring workgroup memory.
109 unsigned workgroupAddrSpace;
110
111 /// The attribute name to use instead of `gpu.kernel`. Null if no attribute
112 /// should be used.
113 StringAttr kernelAttributeName;
114 /// The attribute name to to set block size. Null if no attribute should be
115 /// used.
116 StringAttr kernelBlockSizeAttributeName;
117
118 /// The calling convention to use for kernel functions
119 LLVM::CConv kernelCallingConvention;
120 /// The calling convention to use for non-kernel functions
121 LLVM::CConv nonKernelCallingConvention;
122
123 /// Whether to encode workgroup attributions as additional arguments instead
124 /// of a global variable.
125 bool encodeWorkgroupAttributionsAsArguments;
126};
127
128/// The lowering of gpu.printf to a call to HIP hostcalls
129///
130/// Simplifies llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp, as we don't have
131/// to deal with %s (even if there were first-class strings in MLIR, they're not
132/// legal input to gpu.printf) or non-constant format strings
133struct GPUPrintfOpToHIPLowering : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
135
136 LogicalResult
137 matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
138 ConversionPatternRewriter &rewriter) const override;
139};
140
141/// The lowering of gpu.printf to a call to an external printf() function
142///
143/// This pass will add a declaration of printf() to the GPUModule if needed
144/// and separate out the format strings into global constants. For some
145/// runtimes, such as OpenCL on AMD, this is sufficient setup, as the compiler
146/// will lower printf calls to appropriate device-side code.
147/// However not all backends use the same calling convention and function
148/// naming.
149/// For example, the LLVM SPIRV backend requires calling convention
150/// LLVM::cconv::CConv::SPIR_FUNC and function name needs to be
151/// mangled as "_Z6printfPU3AS2Kcz".
152/// Default callingConvention is LLVM::cconv::CConv::C and
153/// funcName is "printf" but they can be customized as needed.
155 : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
157 const LLVMTypeConverter &converter, int addressSpace = 0,
158 LLVM::cconv::CConv callingConvention = LLVM::cconv::CConv::C,
159 StringRef funcName = "printf")
160 : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter),
161 addressSpace(addressSpace), callingConvention(callingConvention),
162 funcName(funcName) {}
163
164 LogicalResult
165 matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
166 ConversionPatternRewriter &rewriter) const override;
167
168private:
169 int addressSpace;
170 LLVM::cconv::CConv callingConvention;
171 StringRef funcName;
172};
173
174/// Lowering of gpu.printf to a vprintf standard library.
176 : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
178
179 LogicalResult
180 matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
181 ConversionPatternRewriter &rewriter) const override;
182};
183
184struct GPUReturnOpLowering : public ConvertOpToLLVMPattern<gpu::ReturnOp> {
186
187 LogicalResult
188 matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor,
189 ConversionPatternRewriter &rewriter) const override;
190};
191
192namespace impl {
193/// Unrolls op to array/vector elements.
194LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands,
195 ConversionPatternRewriter &rewriter,
196 const LLVMTypeConverter &converter);
197} // namespace impl
198
199/// Unrolls SourceOp to array/vector elements.
200template <typename SourceOp>
202public:
204
205 LogicalResult
206 matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor,
207 ConversionPatternRewriter &rewriter) const override {
208 return impl::scalarizeVectorOp(op, adaptor.getOperands(), rewriter,
209 *this->getTypeConverter());
210 }
211};
212
213} // namespace mlir
214
215#endif // MLIR_CONVERSION_GPUCOMMON_GPUOPSLOWERING_H_
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
static llvm::ManagedStatic< PassManagerOptions > options
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:215
typename gpu::DynamicSharedMemoryOp::Adaptor OpAdaptor
Definition Pattern.h:211
Conversion from types to the LLVM IR dialect.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands, ConversionPatternRewriter &rewriter, const LLVMTypeConverter &converter)
Unrolls op to array/vector elements.
Include the generated interface declarations.
LLVM::LLVMFuncOp getOrDefineFunction(Operation *moduleOp, Location loc, OpBuilder &b, StringRef name, LLVM::LLVMFunctionType type)
Note that these functions don't take a SymbolTable because GPU module lowerings can have name collisi...
LLVM::GlobalOp getOrCreateStringConstant(OpBuilder &b, Location loc, Operation *moduleOp, Type llvmI8, StringRef namePrefix, StringRef str, uint64_t alignment=0, unsigned addrSpace=0)
Create a global that contains the given string.
LogicalResult matchAndRewrite(gpu::DynamicSharedMemoryOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUDynamicSharedMemoryOpLowering(const LLVMTypeConverter &converter, unsigned alignmentBit=0, PatternBenefit benefit=1)
unsigned allocaAddrSpace
The address space to use for allocas in private memory.
LLVM::CConv nonKernelCallingConvention
The calling convention to use for non-kernel functions.
StringAttr kernelBlockSizeAttributeName
The attribute name to to set block size.
LLVM::CConv kernelCallingConvention
The calling convention to use for kernel functions.
unsigned workgroupAddrSpace
The address space to use declaring workgroup memory.
bool encodeWorkgroupAttributionsAsArguments
Whether to encode workgroup attributions as additional arguments instead of a global variable.
StringAttr kernelAttributeName
The attribute name to use instead of gpu.kernel.
LogicalResult matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUFuncOpLowering(const LLVMTypeConverter &converter, const GPUFuncOpLoweringOptions &options, PatternBenefit benefit=1)
The lowering of gpu.printf to a call to HIP hostcalls.
LogicalResult matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
LogicalResult matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUPrintfOpToLLVMCallLowering(const LLVMTypeConverter &converter, int addressSpace=0, LLVM::cconv::CConv callingConvention=LLVM::cconv::CConv::C, StringRef funcName="printf")
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
Unrolls SourceOp to array/vector elements.
LogicalResult matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
Methods that operate on the SourceOp type.