MLIR 24.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 auto newCall = rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>(
72 callOp, resultType, callOp.getCalleeAttr(), adaptor.getOperands(),
73 callOp.getArgAttrsAttr(), callOp.getResAttrsAttr());
74 newCall->setDiscardableAttrs(callOp->getDiscardableAttrDictionary());
75 } else {
76 auto newCall = rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>(
77 callOp, TypeRange(), callOp.getCalleeAttr(), adaptor.getOperands(),
78 callOp.getArgAttrsAttr(), callOp.getResAttrsAttr());
79 newCall->setDiscardableAttrs(callOp->getDiscardableAttrDictionary());
80 }
81 return success();
82 }
83};
84
85} // namespace
86
87//===----------------------------------------------------------------------===//
88// Pattern population
89//===----------------------------------------------------------------------===//
90
92 RewritePatternSet &patterns) {
93 MLIRContext *context = patterns.getContext();
94
95 patterns.add<ReturnOpPattern, CallOpPattern>(typeConverter, context);
96}
return success()
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
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.