1 | //===- FuncToSPIRV.cpp - Func to SPIR-V Patterns ------------------===// |
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 | // |
9 | // This file implements patterns to convert Func dialect to SPIR-V dialect. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h" |
14 | #include "../SPIRVCommon/Pattern.h" |
15 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
16 | #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" |
17 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
18 | #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" |
19 | #include "mlir/Dialect/SPIRV/Utils/LayoutUtils.h" |
20 | #include "mlir/IR/AffineMap.h" |
21 | #include "mlir/Support/LogicalResult.h" |
22 | #include "llvm/Support/Debug.h" |
23 | |
24 | #define DEBUG_TYPE "func-to-spirv-pattern" |
25 | |
26 | using namespace mlir; |
27 | |
28 | //===----------------------------------------------------------------------===// |
29 | // Operation conversion |
30 | //===----------------------------------------------------------------------===// |
31 | |
32 | // Note that DRR cannot be used for the patterns in this file: we may need to |
33 | // convert type along the way, which requires ConversionPattern. DRR generates |
34 | // normal RewritePattern. |
35 | |
36 | namespace { |
37 | |
38 | /// Converts func.return to spirv.Return. |
39 | class ReturnOpPattern final : public OpConversionPattern<func::ReturnOp> { |
40 | public: |
41 | using OpConversionPattern<func::ReturnOp>::OpConversionPattern; |
42 | |
43 | LogicalResult |
44 | matchAndRewrite(func::ReturnOp returnOp, OpAdaptor adaptor, |
45 | ConversionPatternRewriter &rewriter) const override { |
46 | if (returnOp.getNumOperands() > 1) |
47 | return failure(); |
48 | |
49 | if (returnOp.getNumOperands() == 1) { |
50 | rewriter.replaceOpWithNewOp<spirv::ReturnValueOp>( |
51 | returnOp, adaptor.getOperands()[0]); |
52 | } else { |
53 | rewriter.replaceOpWithNewOp<spirv::ReturnOp>(returnOp); |
54 | } |
55 | return success(); |
56 | } |
57 | }; |
58 | |
59 | /// Converts func.call to spirv.FunctionCall. |
60 | class CallOpPattern final : public OpConversionPattern<func::CallOp> { |
61 | public: |
62 | using OpConversionPattern<func::CallOp>::OpConversionPattern; |
63 | |
64 | LogicalResult |
65 | matchAndRewrite(func::CallOp callOp, OpAdaptor adaptor, |
66 | ConversionPatternRewriter &rewriter) const override { |
67 | // multiple results func was not converted to spirv.func |
68 | if (callOp.getNumResults() > 1) |
69 | return failure(); |
70 | if (callOp.getNumResults() == 1) { |
71 | auto resultType = |
72 | getTypeConverter()->convertType(callOp.getResult(0).getType()); |
73 | if (!resultType) |
74 | return failure(); |
75 | rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>( |
76 | callOp, resultType, adaptor.getOperands(), callOp->getAttrs()); |
77 | } else { |
78 | rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>( |
79 | callOp, TypeRange(), adaptor.getOperands(), callOp->getAttrs()); |
80 | } |
81 | return success(); |
82 | } |
83 | }; |
84 | |
85 | } // namespace |
86 | |
87 | //===----------------------------------------------------------------------===// |
88 | // Pattern population |
89 | //===----------------------------------------------------------------------===// |
90 | |
91 | void mlir::populateFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter, |
92 | RewritePatternSet &patterns) { |
93 | MLIRContext *context = patterns.getContext(); |
94 | |
95 | patterns.add<ReturnOpPattern, CallOpPattern>(arg&: typeConverter, args&: context); |
96 | } |
97 | |