MLIR  20.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 
29  LogicalResult
30  matchAndRewrite(gpu::DynamicSharedMemoryOp op, OpAdaptor adaptor,
31  ConversionPatternRewriter &rewriter) const override;
32 
33 private:
34  // Alignment bit
35  unsigned alignmentBit;
36 };
37 
39  /// The address space to use for `alloca`s in private memory.
40  unsigned allocaAddrSpace;
41  /// The address space to use declaring workgroup memory.
43 
44  /// The attribute name to use instead of `gpu.kernel`. Null if no attribute
45  /// should be used.
46  StringAttr kernelAttributeName;
47  /// The attribute name to to set block size. Null if no attribute should be
48  /// used.
50 
51  /// The calling convention to use for kernel functions.
52  LLVM::CConv kernelCallingConvention = LLVM::CConv::C;
53  /// The calling convention to use for non-kernel functions.
54  LLVM::CConv nonKernelCallingConvention = LLVM::CConv::C;
55 
56  /// Whether to encode workgroup attributions as additional arguments instead
57  /// of a global variable.
59 };
60 
61 struct GPUFuncOpLowering : ConvertOpToLLVMPattern<gpu::GPUFuncOp> {
64  : ConvertOpToLLVMPattern<gpu::GPUFuncOp>(converter),
65  allocaAddrSpace(options.allocaAddrSpace),
66  workgroupAddrSpace(options.workgroupAddrSpace),
67  kernelAttributeName(options.kernelAttributeName),
68  kernelBlockSizeAttributeName(options.kernelBlockSizeAttributeName),
69  kernelCallingConvention(options.kernelCallingConvention),
70  nonKernelCallingConvention(options.nonKernelCallingConvention),
71  encodeWorkgroupAttributionsAsArguments(
72  options.encodeWorkgroupAttributionsAsArguments) {}
73 
74  LogicalResult
75  matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
76  ConversionPatternRewriter &rewriter) const override;
77 
78 private:
79  /// The address space to use for `alloca`s in private memory.
80  unsigned allocaAddrSpace;
81  /// The address space to use declaring workgroup memory.
82  unsigned workgroupAddrSpace;
83 
84  /// The attribute name to use instead of `gpu.kernel`. Null if no attribute
85  /// should be used.
86  StringAttr kernelAttributeName;
87  /// The attribute name to to set block size. Null if no attribute should be
88  /// used.
89  StringAttr kernelBlockSizeAttributeName;
90 
91  /// The calling convention to use for kernel functions
92  LLVM::CConv kernelCallingConvention;
93  /// The calling convention to use for non-kernel functions
94  LLVM::CConv nonKernelCallingConvention;
95 
96  /// Whether to encode workgroup attributions as additional arguments instead
97  /// of a global variable.
98  bool encodeWorkgroupAttributionsAsArguments;
99 };
100 
101 /// The lowering of gpu.printf to a call to HIP hostcalls
102 ///
103 /// Simplifies llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp, as we don't have
104 /// to deal with %s (even if there were first-class strings in MLIR, they're not
105 /// legal input to gpu.printf) or non-constant format strings
106 struct GPUPrintfOpToHIPLowering : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
108 
109  LogicalResult
110  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
111  ConversionPatternRewriter &rewriter) const override;
112 };
113 
114 /// The lowering of gpu.printf to a call to an external printf() function
115 ///
116 /// This pass will add a declaration of printf() to the GPUModule if needed
117 /// and separate out the format strings into global constants. For some
118 /// runtimes, such as OpenCL on AMD, this is sufficient setup, as the compiler
119 /// will lower printf calls to appropriate device-side code
121  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
123  int addressSpace = 0)
124  : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter),
125  addressSpace(addressSpace) {}
126 
127  LogicalResult
128  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
129  ConversionPatternRewriter &rewriter) const override;
130 
131 private:
132  int addressSpace;
133 };
134 
135 /// Lowering of gpu.printf to a vprintf standard library.
137  : public ConvertOpToLLVMPattern<gpu::PrintfOp> {
139 
140  LogicalResult
141  matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor,
142  ConversionPatternRewriter &rewriter) const override;
143 };
144 
145 struct GPUReturnOpLowering : public ConvertOpToLLVMPattern<gpu::ReturnOp> {
147 
148  LogicalResult
149  matchAndRewrite(gpu::ReturnOp op, OpAdaptor adaptor,
150  ConversionPatternRewriter &rewriter) const override;
151 };
152 
153 namespace impl {
154 /// Unrolls op if it's operating on vectors.
155 LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands,
156  ConversionPatternRewriter &rewriter,
157  const LLVMTypeConverter &converter);
158 } // namespace impl
159 
160 /// Rewriting that unrolls SourceOp to scalars if it's operating on vectors.
161 template <typename SourceOp>
163 public:
165 
166  LogicalResult
167  matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor,
168  ConversionPatternRewriter &rewriter) const override {
169  return impl::scalarizeVectorOp(op, adaptor.getOperands(), rewriter,
170  *this->getTypeConverter());
171  }
172 };
173 } // namespace mlir
174 
175 #endif // MLIR_CONVERSION_GPUCOMMON_GPUOPSLOWERING_H_
static llvm::ManagedStatic< PassManagerOptions > options
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:143
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition: Pattern.h:149
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:35
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
LogicalResult scalarizeVectorOp(Operation *op, ValueRange operands, ConversionPatternRewriter &rewriter, const LLVMTypeConverter &converter)
Unrolls op if it's operating on vectors.
Include the generated interface declarations.
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)
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)
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
Rewriting that unrolls SourceOp to scalars if it's operating on vectors.
LogicalResult matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override