|
10 | 10 |
|
11 | 11 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
12 | 12 | #include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 13 | +#include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
13 | 14 | #include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
14 | 15 |
|
15 | 16 | namespace mlir { |
@@ -142,20 +143,32 @@ struct GPUPrintfOpToHIPLowering : public ConvertOpToLLVMPattern<gpu::PrintfOp> { |
142 | 143 | /// This pass will add a declaration of printf() to the GPUModule if needed |
143 | 144 | /// and separate out the format strings into global constants. For some |
144 | 145 | /// runtimes, such as OpenCL on AMD, this is sufficient setup, as the compiler |
145 | | -/// will lower printf calls to appropriate device-side code |
| 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. |
146 | 154 | struct GPUPrintfOpToLLVMCallLowering |
147 | 155 | : public ConvertOpToLLVMPattern<gpu::PrintfOp> { |
148 | | - GPUPrintfOpToLLVMCallLowering(const LLVMTypeConverter &converter, |
149 | | - int addressSpace = 0) |
| 156 | + GPUPrintfOpToLLVMCallLowering( |
| 157 | + const LLVMTypeConverter &converter, int addressSpace = 0, |
| 158 | + LLVM::cconv::CConv callingConvention = LLVM::cconv::CConv::C, |
| 159 | + StringRef funcName = "printf") |
150 | 160 | : ConvertOpToLLVMPattern<gpu::PrintfOp>(converter), |
151 | | - addressSpace(addressSpace) {} |
| 161 | + addressSpace(addressSpace), callingConvention(callingConvention), |
| 162 | + funcName(funcName) {} |
152 | 163 |
|
153 | 164 | LogicalResult |
154 | 165 | matchAndRewrite(gpu::PrintfOp gpuPrintfOp, gpu::PrintfOpAdaptor adaptor, |
155 | 166 | ConversionPatternRewriter &rewriter) const override; |
156 | 167 |
|
157 | 168 | private: |
158 | 169 | int addressSpace; |
| 170 | + LLVM::cconv::CConv callingConvention; |
| 171 | + StringRef funcName; |
159 | 172 | }; |
160 | 173 |
|
161 | 174 | /// Lowering of gpu.printf to a vprintf standard library. |
|
0 commit comments