MLIR  19.0.0git
OneToNFuncConversions.cpp
Go to the documentation of this file.
1 //===-- OneToNTypeFuncConversions.cpp - Func 1:N type conversion-*- C++ -*-===//
2 //
3 // Licensed 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 // The patterns in this file are heavily inspired (and copied from)
10 // convertFuncOpTypes in lib/Transforms/Utils/DialectConversion.cpp and the
11 // patterns in lib/Dialect/Func/Transforms/FuncConversions.cpp but work for 1:N
12 // type conversions.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 
20 
21 using namespace mlir;
22 using namespace mlir::func;
23 
24 namespace {
25 
26 class ConvertTypesInFuncCallOp : public OneToNOpConversionPattern<CallOp> {
27 public:
29 
31  matchAndRewrite(CallOp op, OpAdaptor adaptor,
32  OneToNPatternRewriter &rewriter) const override {
33  Location loc = op->getLoc();
34  const OneToNTypeMapping &resultMapping = adaptor.getResultMapping();
35 
36  // Nothing to do if the op doesn't have any non-identity conversions for its
37  // operands or results.
38  if (!adaptor.getOperandMapping().hasNonIdentityConversion() &&
39  !resultMapping.hasNonIdentityConversion())
40  return failure();
41 
42  // Create new CallOp.
43  auto newOp =
44  rewriter.create<CallOp>(loc, resultMapping.getConvertedTypes(),
45  adaptor.getFlatOperands(), op->getAttrs());
46 
47  rewriter.replaceOp(op, newOp->getResults(), resultMapping);
48  return success();
49  }
50 };
51 
52 class ConvertTypesInFuncReturnOp : public OneToNOpConversionPattern<ReturnOp> {
53 public:
55 
57  matchAndRewrite(ReturnOp op, OpAdaptor adaptor,
58  OneToNPatternRewriter &rewriter) const override {
59  // Nothing to do if there is no non-identity conversion.
60  if (!adaptor.getOperandMapping().hasNonIdentityConversion())
61  return failure();
62 
63  // Convert operands.
64  rewriter.modifyOpInPlace(
65  op, [&] { op->setOperands(adaptor.getFlatOperands()); });
66 
67  return success();
68  }
69 };
70 
71 } // namespace
72 
73 namespace mlir {
74 
76  RewritePatternSet &patterns) {
77  patterns.add<
78  // clang-format off
79  ConvertTypesInFuncCallOp,
80  ConvertTypesInFuncReturnOp
81  // clang-format on
82  >(typeConverter, patterns.getContext());
83  populateOneToNFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
84  typeConverter, patterns);
85 }
86 
87 } // namespace mlir
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class is a wrapper around OneToNConversionPattern for matching against instances of a particular...
Specialization of PatternRewriter that OneToNConversionPatterns use.
void replaceOp(Operation *op, ValueRange newValues, const OneToNTypeMapping &resultMapping)
Replaces the results of the operation with the specified list of values mapped back to the original t...
Stores a 1:N mapping of types and provides several useful accessors.
TypeRange getConvertedTypes(unsigned originalTypeNo) const
Returns the list of types that corresponds to the original type at the given index.
bool hasNonIdentityConversion() const
Returns true iff at least one type conversion maps an input type to a type that is different from its...
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:464
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition: Operation.h:507
void setOperands(ValueRange operands)
Replace the current operands of this operation with the ones provided in 'operands'.
Definition: Operation.cpp:237
MLIRContext * getContext() const
Definition: PatternMatch.h:823
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:847
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
Definition: PatternMatch.h:630
Type conversion class.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
void populateFuncTypeConversionPatterns(TypeConverter &typeConverter, RewritePatternSet &patterns)
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26