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