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