MLIR  21.0.0git
InliningUtils.cpp
Go to the documentation of this file.
1 //===- InliningUtils.cpp ---- Misc utilities for inlining -----------------===//
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 miscellaneous inlining utilities.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
15 #include "mlir/IR/Builders.h"
16 #include "mlir/IR/IRMapping.h"
17 #include "mlir/IR/Operation.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <optional>
23 
24 #define DEBUG_TYPE "inlining"
25 
26 using namespace mlir;
27 
28 /// Combine `callee` location with `caller` location to create a stack that
29 /// represents the call chain.
30 /// If `callee` location is a `CallSiteLoc`, indicating an existing stack of
31 /// locations, the `caller` location is appended to the end of it, extending
32 /// the chain.
33 /// Otherwise, a single `CallSiteLoc` is created, representing a direct call
34 /// from `caller` to `callee`.
35 static LocationAttr stackLocations(Location callee, Location caller) {
36  Location lastCallee = callee;
37  SmallVector<CallSiteLoc> calleeInliningStack;
38  while (auto nextCallSite = dyn_cast<CallSiteLoc>(lastCallee)) {
39  calleeInliningStack.push_back(nextCallSite);
40  lastCallee = nextCallSite.getCaller();
41  }
42 
43  CallSiteLoc firstCallSite = CallSiteLoc::get(lastCallee, caller);
44  for (CallSiteLoc currentCallSite : reverse(calleeInliningStack))
45  firstCallSite =
46  CallSiteLoc::get(currentCallSite.getCallee(), firstCallSite);
47 
48  return firstCallSite;
49 }
50 
51 /// Remap all locations reachable from the inlined blocks with CallSiteLoc
52 /// locations with the provided caller location.
53 static void
55  Location callerLoc) {
56  DenseMap<Location, LocationAttr> mappedLocations;
57  auto remapLoc = [&](Location loc) {
58  auto [it, inserted] = mappedLocations.try_emplace(loc);
59  // Only query the attribute uniquer once per callsite attribute.
60  if (inserted) {
61  LocationAttr newLoc = stackLocations(loc, callerLoc);
62  it->getSecond() = newLoc;
63  }
64  return it->second;
65  };
66 
67  AttrTypeReplacer attrReplacer;
68  attrReplacer.addReplacement(
69  [&](LocationAttr loc) -> std::pair<LocationAttr, WalkResult> {
70  return {remapLoc(loc), WalkResult::skip()};
71  });
72 
73  for (Block &block : inlinedBlocks) {
74  for (BlockArgument &arg : block.getArguments())
75  if (LocationAttr newLoc = remapLoc(arg.getLoc()))
76  arg.setLoc(newLoc);
77 
78  for (Operation &op : block)
79  attrReplacer.recursivelyReplaceElementsIn(&op, /*replaceAttrs=*/false,
80  /*replaceLocs=*/true);
81  }
82 }
83 
85  IRMapping &mapper) {
86  auto remapOperands = [&](Operation *op) {
87  for (auto &operand : op->getOpOperands())
88  if (auto mappedOp = mapper.lookupOrNull(operand.get()))
89  operand.set(mappedOp);
90  };
91  for (auto &block : inlinedBlocks)
92  block.walk(remapOperands);
93 }
94 
95 //===----------------------------------------------------------------------===//
96 // InlinerInterface
97 //===----------------------------------------------------------------------===//
98 
100  bool wouldBeCloned) const {
101  if (auto *handler = getInterfaceFor(call))
102  return handler->isLegalToInline(call, callable, wouldBeCloned);
103  return false;
104 }
105 
107  bool wouldBeCloned,
108  IRMapping &valueMapping) const {
109  if (auto *handler = getInterfaceFor(dest->getParentOp()))
110  return handler->isLegalToInline(dest, src, wouldBeCloned, valueMapping);
111  return false;
112 }
113 
115  bool wouldBeCloned,
116  IRMapping &valueMapping) const {
117  if (auto *handler = getInterfaceFor(op))
118  return handler->isLegalToInline(op, dest, wouldBeCloned, valueMapping);
119  return false;
120 }
121 
123  auto *handler = getInterfaceFor(op);
124  return handler ? handler->shouldAnalyzeRecursively(op) : true;
125 }
126 
127 /// Handle the given inlined terminator by replacing it with a new operation
128 /// as necessary.
130  auto *handler = getInterfaceFor(op);
131  assert(handler && "expected valid dialect handler");
132  handler->handleTerminator(op, newDest);
133 }
134 
135 /// Handle the given inlined terminator by replacing it with a new operation
136 /// as necessary.
138  ValueRange valuesToRepl) const {
139  auto *handler = getInterfaceFor(op);
140  assert(handler && "expected valid dialect handler");
141  handler->handleTerminator(op, valuesToRepl);
142 }
143 
144 /// Returns true if the inliner can assume a fast path of not creating a
145 /// new block, if there is only one block.
147  iterator_range<Region::iterator> inlinedBlocks) const {
148  if (inlinedBlocks.empty()) {
149  return true;
150  }
151  auto *handler = getInterfaceFor(inlinedBlocks.begin()->getParentOp());
152  assert(handler && "expected valid dialect handler");
153  return handler->allowSingleBlockOptimization(inlinedBlocks);
154 }
155 
157  Operation *callable, Value argument,
158  DictionaryAttr argumentAttrs) const {
159  auto *handler = getInterfaceFor(callable);
160  assert(handler && "expected valid dialect handler");
161  return handler->handleArgument(builder, call, callable, argument,
162  argumentAttrs);
163 }
164 
166  Operation *callable, Value result,
167  DictionaryAttr resultAttrs) const {
168  auto *handler = getInterfaceFor(callable);
169  assert(handler && "expected valid dialect handler");
170  return handler->handleResult(builder, call, callable, result, resultAttrs);
171 }
172 
174  Operation *call, iterator_range<Region::iterator> inlinedBlocks) const {
175  auto *handler = getInterfaceFor(call);
176  assert(handler && "expected valid dialect handler");
177  handler->processInlinedCallBlocks(call, inlinedBlocks);
178 }
179 
180 /// Utility to check that all of the operations within 'src' can be inlined.
181 static bool isLegalToInline(InlinerInterface &interface, Region *src,
182  Region *insertRegion, bool shouldCloneInlinedRegion,
183  IRMapping &valueMapping) {
184  for (auto &block : *src) {
185  for (auto &op : block) {
186  // Check this operation.
187  if (!interface.isLegalToInline(&op, insertRegion,
188  shouldCloneInlinedRegion, valueMapping)) {
189  LLVM_DEBUG({
190  llvm::dbgs() << "* Illegal to inline because of op: ";
191  op.dump();
192  });
193  return false;
194  }
195  // Check any nested regions.
196  if (interface.shouldAnalyzeRecursively(&op) &&
197  llvm::any_of(op.getRegions(), [&](Region &region) {
198  return !isLegalToInline(interface, &region, insertRegion,
199  shouldCloneInlinedRegion, valueMapping);
200  }))
201  return false;
202  }
203  }
204  return true;
205 }
206 
207 //===----------------------------------------------------------------------===//
208 // Inline Methods
209 //===----------------------------------------------------------------------===//
210 
211 static void handleArgumentImpl(InlinerInterface &interface, OpBuilder &builder,
212  CallOpInterface call,
213  CallableOpInterface callable,
214  IRMapping &mapper) {
215  // Unpack the argument attributes if there are any.
217  callable.getCallableRegion()->getNumArguments(),
218  builder.getDictionaryAttr({}));
219  if (ArrayAttr arrayAttr = callable.getArgAttrsAttr()) {
220  assert(arrayAttr.size() == argAttrs.size());
221  for (auto [idx, attr] : llvm::enumerate(arrayAttr))
222  argAttrs[idx] = cast<DictionaryAttr>(attr);
223  }
224 
225  // Run the argument attribute handler for the given argument and attribute.
226  for (auto [blockArg, argAttr] :
227  llvm::zip(callable.getCallableRegion()->getArguments(), argAttrs)) {
228  Value newArgument = interface.handleArgument(
229  builder, call, callable, mapper.lookup(blockArg), argAttr);
230  assert(newArgument.getType() == mapper.lookup(blockArg).getType() &&
231  "expected the argument type to not change");
232 
233  // Update the mapping to point the new argument returned by the handler.
234  mapper.map(blockArg, newArgument);
235  }
236 }
237 
238 static void handleResultImpl(InlinerInterface &interface, OpBuilder &builder,
239  CallOpInterface call, CallableOpInterface callable,
240  ValueRange results) {
241  // Unpack the result attributes if there are any.
242  SmallVector<DictionaryAttr> resAttrs(results.size(),
243  builder.getDictionaryAttr({}));
244  if (ArrayAttr arrayAttr = callable.getResAttrsAttr()) {
245  assert(arrayAttr.size() == resAttrs.size());
246  for (auto [idx, attr] : llvm::enumerate(arrayAttr))
247  resAttrs[idx] = cast<DictionaryAttr>(attr);
248  }
249 
250  // Run the result attribute handler for the given result and attribute.
251  SmallVector<DictionaryAttr> resultAttributes;
252  for (auto [result, resAttr] : llvm::zip(results, resAttrs)) {
253  // Store the original result users before running the handler.
254  DenseSet<Operation *> resultUsers(llvm::from_range, result.getUsers());
255 
256  Value newResult =
257  interface.handleResult(builder, call, callable, result, resAttr);
258  assert(newResult.getType() == result.getType() &&
259  "expected the result type to not change");
260 
261  // Replace the result uses except for the ones introduce by the handler.
262  result.replaceUsesWithIf(newResult, [&](OpOperand &operand) {
263  return resultUsers.count(operand.getOwner());
264  });
265  }
266 }
267 
268 static LogicalResult inlineRegionImpl(
269  InlinerInterface &interface,
271  Region *src, Block *inlineBlock, Block::iterator inlinePoint,
272  IRMapping &mapper, ValueRange resultsToReplace, TypeRange regionResultTypes,
273  std::optional<Location> inlineLoc, bool shouldCloneInlinedRegion,
274  CallOpInterface call = {}) {
275  assert(resultsToReplace.size() == regionResultTypes.size());
276  // We expect the region to have at least one block.
277  if (src->empty())
278  return failure();
279 
280  // Check that all of the region arguments have been mapped.
281  auto *srcEntryBlock = &src->front();
282  if (llvm::any_of(srcEntryBlock->getArguments(),
283  [&](BlockArgument arg) { return !mapper.contains(arg); }))
284  return failure();
285 
286  // Check that the operations within the source region are valid to inline.
287  Region *insertRegion = inlineBlock->getParent();
288  if (!interface.isLegalToInline(insertRegion, src, shouldCloneInlinedRegion,
289  mapper) ||
290  !isLegalToInline(interface, src, insertRegion, shouldCloneInlinedRegion,
291  mapper))
292  return failure();
293 
294  // Run the argument attribute handler before inlining the callable region.
295  OpBuilder builder(inlineBlock, inlinePoint);
296  auto callable = dyn_cast<CallableOpInterface>(src->getParentOp());
297  if (call && callable)
298  handleArgumentImpl(interface, builder, call, callable, mapper);
299 
300  // Clone the callee's source into the caller.
301  Block *postInsertBlock = inlineBlock->splitBlock(inlinePoint);
302  cloneCallback(builder, src, inlineBlock, postInsertBlock, mapper,
303  shouldCloneInlinedRegion);
304 
305  // Get the range of newly inserted blocks.
306  auto newBlocks = llvm::make_range(std::next(inlineBlock->getIterator()),
307  postInsertBlock->getIterator());
308  Block *firstNewBlock = &*newBlocks.begin();
309 
310  // Remap the locations of the inlined operations if a valid source location
311  // was provided.
312  if (inlineLoc && !llvm::isa<UnknownLoc>(*inlineLoc))
313  remapInlinedLocations(newBlocks, *inlineLoc);
314 
315  // If the blocks were moved in-place, make sure to remap any necessary
316  // operands.
317  if (!shouldCloneInlinedRegion)
318  remapInlinedOperands(newBlocks, mapper);
319 
320  // Process the newly inlined blocks.
321  if (call)
322  interface.processInlinedCallBlocks(call, newBlocks);
323  interface.processInlinedBlocks(newBlocks);
324 
325  bool singleBlockFastPath = interface.allowSingleBlockOptimization(newBlocks);
326 
327  // Handle the case where only a single block was inlined.
328  if (singleBlockFastPath && llvm::hasSingleElement(newBlocks)) {
329  // Run the result attribute handler on the terminator operands.
330  Operation *firstBlockTerminator = firstNewBlock->getTerminator();
331  builder.setInsertionPoint(firstBlockTerminator);
332  if (call && callable)
333  handleResultImpl(interface, builder, call, callable,
334  firstBlockTerminator->getOperands());
335 
336  // Have the interface handle the terminator of this block.
337  interface.handleTerminator(firstBlockTerminator, resultsToReplace);
338  firstBlockTerminator->erase();
339 
340  // Merge the post insert block into the cloned entry block.
341  firstNewBlock->getOperations().splice(firstNewBlock->end(),
342  postInsertBlock->getOperations());
343  postInsertBlock->erase();
344  } else {
345  // Otherwise, there were multiple blocks inlined. Add arguments to the post
346  // insertion block to represent the results to replace.
347  for (const auto &resultToRepl : llvm::enumerate(resultsToReplace)) {
348  resultToRepl.value().replaceAllUsesWith(
349  postInsertBlock->addArgument(regionResultTypes[resultToRepl.index()],
350  resultToRepl.value().getLoc()));
351  }
352 
353  // Run the result attribute handler on the post insertion block arguments.
354  builder.setInsertionPointToStart(postInsertBlock);
355  if (call && callable)
356  handleResultImpl(interface, builder, call, callable,
357  postInsertBlock->getArguments());
358 
359  /// Handle the terminators for each of the new blocks.
360  for (auto &newBlock : newBlocks)
361  interface.handleTerminator(newBlock.getTerminator(), postInsertBlock);
362  }
363 
364  // Splice the instructions of the inlined entry block into the insert block.
365  inlineBlock->getOperations().splice(inlineBlock->end(),
366  firstNewBlock->getOperations());
367  firstNewBlock->erase();
368  return success();
369 }
370 
371 static LogicalResult inlineRegionImpl(
372  InlinerInterface &interface,
374  Region *src, Block *inlineBlock, Block::iterator inlinePoint,
375  ValueRange inlinedOperands, ValueRange resultsToReplace,
376  std::optional<Location> inlineLoc, bool shouldCloneInlinedRegion,
377  CallOpInterface call = {}) {
378  // We expect the region to have at least one block.
379  if (src->empty())
380  return failure();
381 
382  auto *entryBlock = &src->front();
383  if (inlinedOperands.size() != entryBlock->getNumArguments())
384  return failure();
385 
386  // Map the provided call operands to the arguments of the region.
387  IRMapping mapper;
388  for (unsigned i = 0, e = inlinedOperands.size(); i != e; ++i) {
389  // Verify that the types of the provided values match the function argument
390  // types.
391  BlockArgument regionArg = entryBlock->getArgument(i);
392  if (inlinedOperands[i].getType() != regionArg.getType())
393  return failure();
394  mapper.map(regionArg, inlinedOperands[i]);
395  }
396 
397  // Call into the main region inliner function.
398  return inlineRegionImpl(interface, cloneCallback, src, inlineBlock,
399  inlinePoint, mapper, resultsToReplace,
400  resultsToReplace.getTypes(), inlineLoc,
401  shouldCloneInlinedRegion, call);
402 }
403 
404 LogicalResult mlir::inlineRegion(
405  InlinerInterface &interface,
407  Region *src, Operation *inlinePoint, IRMapping &mapper,
408  ValueRange resultsToReplace, TypeRange regionResultTypes,
409  std::optional<Location> inlineLoc, bool shouldCloneInlinedRegion) {
410  return inlineRegion(interface, cloneCallback, src, inlinePoint->getBlock(),
411  ++inlinePoint->getIterator(), mapper, resultsToReplace,
412  regionResultTypes, inlineLoc, shouldCloneInlinedRegion);
413 }
414 
415 LogicalResult mlir::inlineRegion(
416  InlinerInterface &interface,
418  Region *src, Block *inlineBlock, Block::iterator inlinePoint,
419  IRMapping &mapper, ValueRange resultsToReplace, TypeRange regionResultTypes,
420  std::optional<Location> inlineLoc, bool shouldCloneInlinedRegion) {
421  return inlineRegionImpl(
422  interface, cloneCallback, src, inlineBlock, inlinePoint, mapper,
423  resultsToReplace, regionResultTypes, inlineLoc, shouldCloneInlinedRegion);
424 }
425 
426 LogicalResult mlir::inlineRegion(
427  InlinerInterface &interface,
429  Region *src, Operation *inlinePoint, ValueRange inlinedOperands,
430  ValueRange resultsToReplace, std::optional<Location> inlineLoc,
431  bool shouldCloneInlinedRegion) {
432  return inlineRegion(interface, cloneCallback, src, inlinePoint->getBlock(),
433  ++inlinePoint->getIterator(), inlinedOperands,
434  resultsToReplace, inlineLoc, shouldCloneInlinedRegion);
435 }
436 
437 LogicalResult mlir::inlineRegion(
438  InlinerInterface &interface,
440  Region *src, Block *inlineBlock, Block::iterator inlinePoint,
441  ValueRange inlinedOperands, ValueRange resultsToReplace,
442  std::optional<Location> inlineLoc, bool shouldCloneInlinedRegion) {
443  return inlineRegionImpl(interface, cloneCallback, src, inlineBlock,
444  inlinePoint, inlinedOperands, resultsToReplace,
445  inlineLoc, shouldCloneInlinedRegion);
446 }
447 
448 /// Utility function used to generate a cast operation from the given interface,
449 /// or return nullptr if a cast could not be generated.
452  OpBuilder &castBuilder, Value arg, Type type,
453  Location conversionLoc) {
454  if (!interface)
455  return nullptr;
456 
457  // Check to see if the interface for the call can materialize a conversion.
458  Operation *castOp = interface->materializeCallConversion(castBuilder, arg,
459  type, conversionLoc);
460  if (!castOp)
461  return nullptr;
462  castOps.push_back(castOp);
463 
464  // Ensure that the generated cast is correct.
465  assert(castOp->getNumOperands() == 1 && castOp->getOperand(0) == arg &&
466  castOp->getNumResults() == 1 && *castOp->result_type_begin() == type);
467  return castOp->getResult(0);
468 }
469 
470 /// This function inlines a given region, 'src', of a callable operation,
471 /// 'callable', into the location defined by the given call operation. This
472 /// function returns failure if inlining is not possible, success otherwise. On
473 /// failure, no changes are made to the module. 'shouldCloneInlinedRegion'
474 /// corresponds to whether the source region should be cloned into the 'call' or
475 /// spliced directly.
476 LogicalResult mlir::inlineCall(
477  InlinerInterface &interface,
479  CallOpInterface call, CallableOpInterface callable, Region *src,
480  bool shouldCloneInlinedRegion) {
481  // We expect the region to have at least one block.
482  if (src->empty())
483  return failure();
484  auto *entryBlock = &src->front();
485  ArrayRef<Type> callableResultTypes = callable.getResultTypes();
486 
487  // Make sure that the number of arguments and results matchup between the call
488  // and the region.
489  SmallVector<Value, 8> callOperands(call.getArgOperands());
490  SmallVector<Value, 8> callResults(call->getResults());
491  if (callOperands.size() != entryBlock->getNumArguments() ||
492  callResults.size() != callableResultTypes.size())
493  return failure();
494 
495  // A set of cast operations generated to matchup the signature of the region
496  // with the signature of the call.
498  castOps.reserve(callOperands.size() + callResults.size());
499 
500  // Functor used to cleanup generated state on failure.
501  auto cleanupState = [&] {
502  for (auto *op : castOps) {
503  op->getResult(0).replaceAllUsesWith(op->getOperand(0));
504  op->erase();
505  }
506  return failure();
507  };
508 
509  // Builder used for any conversion operations that need to be materialized.
510  OpBuilder castBuilder(call);
511  Location castLoc = call.getLoc();
512  const auto *callInterface = interface.getInterfaceFor(call->getDialect());
513 
514  // Map the provided call operands to the arguments of the region.
515  IRMapping mapper;
516  for (unsigned i = 0, e = callOperands.size(); i != e; ++i) {
517  BlockArgument regionArg = entryBlock->getArgument(i);
518  Value operand = callOperands[i];
519 
520  // If the call operand doesn't match the expected region argument, try to
521  // generate a cast.
522  Type regionArgType = regionArg.getType();
523  if (operand.getType() != regionArgType) {
524  if (!(operand = materializeConversion(callInterface, castOps, castBuilder,
525  operand, regionArgType, castLoc)))
526  return cleanupState();
527  }
528  mapper.map(regionArg, operand);
529  }
530 
531  // Ensure that the resultant values of the call match the callable.
532  castBuilder.setInsertionPointAfter(call);
533  for (unsigned i = 0, e = callResults.size(); i != e; ++i) {
534  Value callResult = callResults[i];
535  if (callResult.getType() == callableResultTypes[i])
536  continue;
537 
538  // Generate a conversion that will produce the original type, so that the IR
539  // is still valid after the original call gets replaced.
540  Value castResult =
541  materializeConversion(callInterface, castOps, castBuilder, callResult,
542  callResult.getType(), castLoc);
543  if (!castResult)
544  return cleanupState();
545  callResult.replaceAllUsesWith(castResult);
546  castResult.getDefiningOp()->replaceUsesOfWith(castResult, callResult);
547  }
548 
549  // Check that it is legal to inline the callable into the call.
550  if (!interface.isLegalToInline(call, callable, shouldCloneInlinedRegion))
551  return cleanupState();
552 
553  // Attempt to inline the call.
554  if (failed(inlineRegionImpl(interface, cloneCallback, src, call->getBlock(),
555  ++call->getIterator(), mapper, callResults,
556  callableResultTypes, call.getLoc(),
557  shouldCloneInlinedRegion, call)))
558  return cleanupState();
559  return success();
560 }
static void remapInlinedOperands(iterator_range< Region::iterator > inlinedBlocks, IRMapping &mapper)
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
static void handleResultImpl(InlinerInterface &interface, OpBuilder &builder, CallOpInterface call, CallableOpInterface callable, ValueRange results)
static LocationAttr stackLocations(Location callee, Location caller)
Combine callee location with caller location to create a stack that represents the call chain.
static LogicalResult inlineRegionImpl(InlinerInterface &interface, function_ref< InlinerInterface::CloneCallbackSigTy > cloneCallback, Region *src, Block *inlineBlock, Block::iterator inlinePoint, IRMapping &mapper, ValueRange resultsToReplace, TypeRange regionResultTypes, std::optional< Location > inlineLoc, bool shouldCloneInlinedRegion, CallOpInterface call={})
static Value materializeConversion(const DialectInlinerInterface *interface, SmallVectorImpl< Operation * > &castOps, OpBuilder &castBuilder, Value arg, Type type, Location conversionLoc)
Utility function used to generate a cast operation from the given interface, or return nullptr if a c...
static void remapInlinedLocations(iterator_range< Region::iterator > inlinedBlocks, Location callerLoc)
Remap all locations reachable from the inlined blocks with CallSiteLoc locations with the provided ca...
static void handleArgumentImpl(InlinerInterface &interface, OpBuilder &builder, CallOpInterface call, CallableOpInterface callable, IRMapping &mapper)
This is an attribute/type replacer that is naively cached.
This class represents an argument of a Block.
Definition: Value.h:295
Block represents an ordered list of Operations.
Definition: Block.h:33
OpListType::iterator iterator
Definition: Block.h:140
void erase()
Unlink this Block from its parent region and delete it.
Definition: Block.cpp:68
Block * splitBlock(iterator splitBefore)
Split the block into two blocks before the specified operation or iterator.
Definition: Block.cpp:310
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition: Block.cpp:29
Operation * getTerminator()
Get the terminator operation of this block.
Definition: Block.cpp:246
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
Definition: Block.cpp:155
OpListType & getOperations()
Definition: Block.h:137
BlockArgListType getArguments()
Definition: Block.h:87
iterator end()
Definition: Block.h:144
iterator begin()
Definition: Block.h:143
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition: Builders.cpp:100
This is the interface that must be implemented by the dialects of operations to be inlined.
Definition: InliningUtils.h:44
virtual Operation * materializeCallConversion(OpBuilder &builder, Value input, Type resultType, Location conversionLoc) const
Attempt to materialize a conversion for a type mismatch between a call from this dialect,...
const DialectInlinerInterface * getInterfaceFor(Object *obj) const
Get the interface for a given object, or null if one is not registered.
This is a utility class for mapping one set of IR entities to another.
Definition: IRMapping.h:26
auto lookup(T from) const
Lookup a mapped value within the map.
Definition: IRMapping.h:72
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
Definition: IRMapping.h:30
auto lookupOrNull(T from) const
Lookup a mapped value within the map.
Definition: IRMapping.h:58
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
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 ...
Location objects represent source locations information in MLIR.
Definition: Location.h:31
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This class helps build Operations.
Definition: Builders.h:205
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition: Builders.h:410
This class represents an operand of an operation.
Definition: Value.h:243
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
void replaceUsesOfWith(Value from, Value to)
Replace any uses of 'from' with 'to' within this operation.
Definition: Operation.cpp:227
Value getOperand(unsigned idx)
Definition: Operation.h:350
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:407
unsigned getNumOperands()
Definition: Operation.h:346
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:213
result_type_iterator result_type_begin()
Definition: Operation.h:426
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition: Operation.h:378
void erase()
Remove this operation from its parent block and delete it.
Definition: Operation.cpp:539
unsigned getNumResults()
Return the number of results held by this operation.
Definition: Operation.h:404
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition: Region.h:200
bool empty()
Definition: Region.h:60
Block & front()
Definition: Region.h:65
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:37
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
type_range getTypes() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Type getType() const
Return the type of this value.
Definition: Value.h:105
void replaceAllUsesWith(Value newValue)
Replace all uses of 'this' value with the new value, updating anything in the IR that uses 'this' to ...
Definition: Value.h:149
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition: Value.cpp:20
static WalkResult skip()
Definition: Visitors.h:52
void recursivelyReplaceElementsIn(Operation *op, bool replaceAttrs=true, bool replaceLocs=false, bool replaceTypes=false)
Replace the elements within the given operation, and all nested operations.
void addReplacement(ReplaceFn< Attribute > fn)
Register a replacement function for mapping a given attribute or type.
Operation * getOwner() const
Return the owner of this operand.
Definition: UseDefLists.h:38
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:344
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition: Utils.cpp:305
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...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
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.