MLIR 22.0.0git
InliningUtils.h
Go to the documentation of this file.
1//===- InliningUtils.h - Inliner utilities ----------------------*- C++ -*-===//
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 header file defines interfaces for various inlining utility methods.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_TRANSFORMS_INLININGUTILS_H
14#define MLIR_TRANSFORMS_INLININGUTILS_H
15
18#include "mlir/IR/Location.h"
19#include "mlir/IR/Region.h"
20#include "mlir/IR/ValueRange.h"
21#include <optional>
22
23namespace mlir {
24
25class Block;
26class IRMapping;
27class CallableOpInterface;
28class CallOpInterface;
29class OpBuilder;
30class Operation;
31class Region;
32class TypeRange;
33class Value;
34class ValueRange;
35class DialectInlinerInterface;
36
37/// This interface provides the hooks into the inlining interface.
38/// Note: this class automatically collects 'DialectInlinerInterface' objects
39/// registered to each dialect within the given context.
41 : public DialectInterfaceCollection<DialectInlinerInterface> {
42public:
43 using CloneCallbackSigTy = void(OpBuilder &builder, Region *src,
44 Block *inlineBlock, Block *postInsertBlock,
45 IRMapping &mapper,
46 bool shouldCloneInlinedRegion);
47 using CloneCallbackTy = std::function<CloneCallbackSigTy>;
48
49 using Base::Base;
50
51 /// Process a set of blocks that have been inlined. This callback is invoked
52 /// *before* inlined terminator operations have been processed.
53 virtual void
55
56 /// These hooks mirror the hooks for the DialectInlinerInterface, with default
57 /// implementations that call the hook on the handler for the dialect 'op' is
58 /// registered to.
59
60 //===--------------------------------------------------------------------===//
61 // Analysis Hooks
62 //===--------------------------------------------------------------------===//
63
64 virtual bool isLegalToInline(Operation *call, Operation *callable,
65 bool wouldBeCloned) const;
66 virtual bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
67 IRMapping &valueMapping) const;
68 virtual bool isLegalToInline(Operation *op, Region *dest, bool wouldBeCloned,
69 IRMapping &valueMapping) const;
70 virtual bool shouldAnalyzeRecursively(Operation *op) const;
71
72 //===--------------------------------------------------------------------===//
73 // Transformation Hooks
74 //===--------------------------------------------------------------------===//
75
76 virtual void handleTerminator(Operation *op, Block *newDest) const;
77 virtual void handleTerminator(Operation *op, ValueRange valuesToRepl) const;
78
79 virtual Value handleArgument(OpBuilder &builder, Operation *call,
80 Operation *callable, Value argument,
81 DictionaryAttr argumentAttrs) const;
82 virtual Value handleResult(OpBuilder &builder, Operation *call,
83 Operation *callable, Value result,
84 DictionaryAttr resultAttrs) const;
85
86 virtual void processInlinedCallBlocks(
87 Operation *call, iterator_range<Region::iterator> inlinedBlocks) const;
88
90 iterator_range<Region::iterator> inlinedBlocks) const;
91};
92
93//===----------------------------------------------------------------------===//
94// Inline Methods.
95//===----------------------------------------------------------------------===//
96
97/// This function inlines a region, 'src', into another. This function returns
98/// failure if it is not possible to inline this function. If the function
99/// returned failure, then no changes to the module have been made.
100///
101/// The provided 'inlinePoint' must be within a region, and corresponds to the
102/// location where the 'src' region should be inlined. 'mapping' contains any
103/// remapped operands that are used within the region, and *must* include
104/// remappings for the entry arguments to the region. 'resultsToReplace'
105/// corresponds to any results that should be replaced by terminators within the
106/// inlined region. 'regionResultTypes' specifies the expected return types of
107/// the terminators in the region. 'inlineLoc' is an optional Location that, if
108/// provided, will be used to update the inlined operations' location
109/// information. 'shouldCloneInlinedRegion' corresponds to whether the source
110/// region should be cloned into the 'inlinePoint' or spliced directly.
111LogicalResult
112inlineRegion(InlinerInterface &interface,
114 Region *src, Operation *inlinePoint, IRMapping &mapper,
115 ValueRange resultsToReplace, TypeRange regionResultTypes,
116 std::optional<Location> inlineLoc = std::nullopt,
117 bool shouldCloneInlinedRegion = true);
118LogicalResult
119inlineRegion(InlinerInterface &interface,
121 Region *src, Block *inlineBlock, Block::iterator inlinePoint,
122 IRMapping &mapper, ValueRange resultsToReplace,
123 TypeRange regionResultTypes,
124 std::optional<Location> inlineLoc = std::nullopt,
125 bool shouldCloneInlinedRegion = true);
126
127/// This function is an overload of the above 'inlineRegion' that allows for
128/// providing the set of operands ('inlinedOperands') that should be used
129/// in-favor of the region arguments when inlining.
130LogicalResult
131inlineRegion(InlinerInterface &interface,
133 Region *src, Operation *inlinePoint, ValueRange inlinedOperands,
134 ValueRange resultsToReplace,
135 std::optional<Location> inlineLoc = std::nullopt,
136 bool shouldCloneInlinedRegion = true);
137LogicalResult
138inlineRegion(InlinerInterface &interface,
140 Region *src, Block *inlineBlock, Block::iterator inlinePoint,
141 ValueRange inlinedOperands, ValueRange resultsToReplace,
142 std::optional<Location> inlineLoc = std::nullopt,
143 bool shouldCloneInlinedRegion = true);
144
145/// This function inlines a given region, 'src', of a callable operation,
146/// 'callable', into the location defined by the given call operation. This
147/// function returns failure if inlining is not possible, success otherwise. On
148/// failure, no changes are made to the module. 'shouldCloneInlinedRegion'
149/// corresponds to whether the source region should be cloned into the 'call' or
150/// spliced directly.
151LogicalResult
152inlineCall(InlinerInterface &interface,
154 CallOpInterface call, CallableOpInterface callable, Region *src,
155 bool shouldCloneInlinedRegion = true);
156
157} // namespace mlir
158
159#include "mlir/Transforms/DialectInlinerInterface.h.inc"
160
161#endif // MLIR_TRANSFORMS_INLININGUTILS_H
Block represents an ordered list of Operations.
Definition Block.h:33
OpListType::iterator iterator
Definition Block.h:140
DialectInterfaceCollection< DialectInlinerInterface > Base
This is a utility class for mapping one set of IR entities to another.
Definition IRMapping.h:26
This interface provides the hooks into the inlining interface.
virtual Value handleResult(OpBuilder &builder, Operation *call, Operation *callable, Value result, DictionaryAttr resultAttrs) const
virtual Value handleArgument(OpBuilder &builder, Operation *call, Operation *callable, Value argument, DictionaryAttr argumentAttrs) const
virtual bool shouldAnalyzeRecursively(Operation *op) const
std::function< CloneCallbackSigTy > CloneCallbackTy
void(OpBuilder &builder, Region *src, Block *inlineBlock, Block *postInsertBlock, IRMapping &mapper, bool shouldCloneInlinedRegion) CloneCallbackSigTy
virtual bool allowSingleBlockOptimization(iterator_range< Region::iterator > inlinedBlocks) const
Returns true if the inliner can assume a fast path of not creating a new block, if there is only one ...
virtual void handleTerminator(Operation *op, Block *newDest) const
Handle the given inlined terminator by replacing it with a new operation as necessary.
virtual void processInlinedCallBlocks(Operation *call, iterator_range< Region::iterator > inlinedBlocks) const
virtual void processInlinedBlocks(iterator_range< Region::iterator > inlinedBlocks)
Process a set of blocks that have been inlined.
virtual bool isLegalToInline(Operation *call, Operation *callable, bool wouldBeCloned) const
These hooks mirror the hooks for the DialectInlinerInterface, with default implementations that call ...
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:37
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Include the generated interface declarations.
LogicalResult inlineCall(InlinerInterface &interface, function_ref< InlinerInterface::CloneCallbackSigTy > cloneCallback, CallOpInterface call, CallableOpInterface callable, Region *src, bool shouldCloneInlinedRegion=true)
This function inlines a given region, 'src', of a callable operation, 'callable', into the location d...
LogicalResult inlineRegion(InlinerInterface &interface, function_ref< InlinerInterface::CloneCallbackSigTy > cloneCallback, Region *src, Operation *inlinePoint, IRMapping &mapper, ValueRange resultsToReplace, TypeRange regionResultTypes, std::optional< Location > inlineLoc=std::nullopt, bool shouldCloneInlinedRegion=true)
This function inlines a region, 'src', into another.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152