MLIR 22.0.0git
FuncToSPIRV.cpp
Go to the documentation of this file.
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
18#include "mlir/IR/AffineMap.h"
19
20#define DEBUG_TYPE "func-to-spirv-pattern"
21
22using 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
32namespace {
33
34/// Converts func.return to spirv.Return.
35class ReturnOpPattern final : public OpConversionPattern<func::ReturnOp> {
36public:
37 using Base::Base;
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 returnOp, adaptor.getOperands()[0]);
48 } else {
49 rewriter.replaceOpWithNewOp<spirv::ReturnOp>(returnOp);
50 }
51 return success();
52 }
53};
54
55/// Converts func.call to spirv.FunctionCall.
56class CallOpPattern final : public OpConversionPattern<func::CallOp> {
57public:
58 using Base::Base;
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(callOp.getResult(0).getType());
69 if (!resultType)
70 return failure();
71 rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>(
72 callOp, resultType, adaptor.getOperands(), callOp->getAttrs());
73 } else {
74 rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>(
75 callOp, TypeRange(), adaptor.getOperands(), callOp->getAttrs());
76 }
77 return success();
78 }
79};
80
81} // namespace
82
83//===----------------------------------------------------------------------===//
84// Pattern population
85//===----------------------------------------------------------------------===//
86
89 MLIRContext *context = patterns.getContext();
90
91 patterns.add<ReturnOpPattern, CallOpPattern>(typeConverter, context);
92}
return success()
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Type conversion from builtin types to SPIR-V types for shader interface.
Include the generated interface declarations.
void populateFuncToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Func ops to SPIR-V ops.
const FrozenRewritePatternSet & patterns