MLIR  19.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 
14 #include "../SPIRVCommon/Pattern.h"
20 #include "mlir/IR/AffineMap.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:
42 
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:
63 
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 
92  RewritePatternSet &patterns) {
93  MLIRContext *context = patterns.getContext();
94 
95  patterns.add<ReturnOpPattern, CallOpPattern>(typeConverter, context);
96 }
This class implements a pattern rewriter for use with ConversionPatterns.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
MLIRContext * getContext() const
Definition: PatternMatch.h:822
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:846
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:536
Type conversion from builtin types to SPIR-V types for shader interface.
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
void populateFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Func ops to SPIR-V ops.
Definition: FuncToSPIRV.cpp:91
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26