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 = rewriter.create<CallOp>(loc, resultMapping.getConvertedTypes(),
44  adaptor.getFlatOperands());
45  newOp->setAttrs(op->getAttrs());
46 
47  rewriter.replaceOp(op, newOp->getResults(), resultMapping);
48  return success();
49  }
50 };
51 
52 class ConvertTypesInFuncFuncOp : public OneToNOpConversionPattern<FuncOp> {
53 public:
55 
57  matchAndRewrite(FuncOp op, OpAdaptor adaptor,
58  OneToNPatternRewriter &rewriter) const override {
59  auto *typeConverter = getTypeConverter<OneToNTypeConverter>();
60 
61  // Construct mapping for function arguments.
62  OneToNTypeMapping argumentMapping(op.getArgumentTypes());
63  if (failed(typeConverter->computeTypeMapping(op.getArgumentTypes(),
64  argumentMapping)))
65  return failure();
66 
67  // Construct mapping for function results.
68  OneToNTypeMapping funcResultMapping(op.getResultTypes());
69  if (failed(typeConverter->computeTypeMapping(op.getResultTypes(),
70  funcResultMapping)))
71  return failure();
72 
73  // Nothing to do if the op doesn't have any non-identity conversions for its
74  // operands or results.
75  if (!argumentMapping.hasNonIdentityConversion() &&
76  !funcResultMapping.hasNonIdentityConversion())
77  return failure();
78 
79  // Update the function signature in-place.
80  auto newType = FunctionType::get(rewriter.getContext(),
81  argumentMapping.getConvertedTypes(),
82  funcResultMapping.getConvertedTypes());
83  rewriter.modifyOpInPlace(op, [&] { op.setType(newType); });
84 
85  // Update block signatures.
86  if (!op.isExternal()) {
87  Region *region = &op.getBody();
88  Block *block = &region->front();
89  rewriter.applySignatureConversion(block, argumentMapping);
90  }
91 
92  return success();
93  }
94 };
95 
96 class ConvertTypesInFuncReturnOp : public OneToNOpConversionPattern<ReturnOp> {
97 public:
99 
101  matchAndRewrite(ReturnOp op, OpAdaptor adaptor,
102  OneToNPatternRewriter &rewriter) const override {
103  // Nothing to do if there is no non-identity conversion.
104  if (!adaptor.getOperandMapping().hasNonIdentityConversion())
105  return failure();
106 
107  // Convert operands.
108  rewriter.modifyOpInPlace(
109  op, [&] { op->setOperands(adaptor.getFlatOperands()); });
110 
111  return success();
112  }
113 };
114 
115 } // namespace
116 
117 namespace mlir {
118 
120  RewritePatternSet &patterns) {
121  patterns.add<
122  // clang-format off
123  ConvertTypesInFuncCallOp,
124  ConvertTypesInFuncFuncOp,
125  ConvertTypesInFuncReturnOp
126  // clang-format on
127  >(typeConverter, patterns.getContext());
128 }
129 
130 } // namespace mlir
Block represents an ordered list of Operations.
Definition: Block.h:30
MLIRContext * getContext() const
Definition: Builders.h:55
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.
Block * applySignatureConversion(Block *block, OneToNTypeMapping &argumentConversion)
Applies the given argument conversion to the given block.
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
void setAttrs(DictionaryAttr newAttrs)
Set the attributes from a dictionary on this operation.
Definition: Operation.cpp:305
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
result_type_range getResultTypes()
Definition: Operation.h:423
void setOperands(ValueRange operands)
Replace the current operands of this operation with the ones provided in 'operands'.
Definition: Operation.cpp:237
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
Block & front()
Definition: Region.h:65
MLIRContext * getContext() const
Definition: PatternMatch.h:812
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:836
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
Definition: PatternMatch.h:628
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
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26