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
269 inlineRegionImpl(InlinerInterface &interface, Region *src, Block *inlineBlock,
270  Block::iterator inlinePoint, IRMapping &mapper,
271  ValueRange resultsToReplace, TypeRange regionResultTypes,
272  std::optional<Location> inlineLoc,
273  bool shouldCloneInlinedRegion, CallOpInterface call = {}) {
274  assert(resultsToReplace.size() == regionResultTypes.size());
275  // We expect the region to have at least one block.
276  if (src->empty())
277  return failure();
278 
279  // Check that all of the region arguments have been mapped.
280  auto *srcEntryBlock = &src->front();
281  if (llvm::any_of(srcEntryBlock->getArguments(),
282  [&](BlockArgument arg) { return !mapper.contains(arg); }))
283  return failure();
284 
285  // Check that the operations within the source region are valid to inline.
286  Region *insertRegion = inlineBlock->getParent();
287  if (!interface.isLegalToInline(insertRegion, src, shouldCloneInlinedRegion,
288  mapper) ||
289  !isLegalToInline(interface, src, insertRegion, shouldCloneInlinedRegion,
290  mapper))
291  return failure();
292 
293  // Run the argument attribute handler before inlining the callable region.
294  OpBuilder builder(inlineBlock, inlinePoint);
295  auto callable = dyn_cast<CallableOpInterface>(src->getParentOp());
296  if (call && callable)
297  handleArgumentImpl(interface, builder, call, callable, mapper);
298 
299  // Check to see if the region is being cloned, or moved inline. In either
300  // case, move the new blocks after the 'insertBlock' to improve IR
301  // readability.
302  Block *postInsertBlock = inlineBlock->splitBlock(inlinePoint);
303  if (shouldCloneInlinedRegion)
304  src->cloneInto(insertRegion, postInsertBlock->getIterator(), mapper);
305  else
306  insertRegion->getBlocks().splice(postInsertBlock->getIterator(),
307  src->getBlocks(), src->begin(),
308  src->end());
309 
310  // Get the range of newly inserted blocks.
311  auto newBlocks = llvm::make_range(std::next(inlineBlock->getIterator()),
312  postInsertBlock->getIterator());
313  Block *firstNewBlock = &*newBlocks.begin();
314 
315  // Remap the locations of the inlined operations if a valid source location
316  // was provided.
317  if (inlineLoc && !llvm::isa<UnknownLoc>(*inlineLoc))
318  remapInlinedLocations(newBlocks, *inlineLoc);
319 
320  // If the blocks were moved in-place, make sure to remap any necessary
321  // operands.
322  if (!shouldCloneInlinedRegion)
323  remapInlinedOperands(newBlocks, mapper);
324 
325  // Process the newly inlined blocks.
326  if (call)
327  interface.processInlinedCallBlocks(call, newBlocks);
328  interface.processInlinedBlocks(newBlocks);
329 
330  bool singleBlockFastPath = interface.allowSingleBlockOptimization(newBlocks);
331 
332  // Handle the case where only a single block was inlined.
333  if (singleBlockFastPath && std::next(newBlocks.begin()) == newBlocks.end()) {
334  // Run the result attribute handler on the terminator operands.
335  Operation *firstBlockTerminator = firstNewBlock->getTerminator();
336  builder.setInsertionPoint(firstBlockTerminator);
337  if (call && callable)
338  handleResultImpl(interface, builder, call, callable,
339  firstBlockTerminator->getOperands());
340 
341  // Have the interface handle the terminator of this block.
342  interface.handleTerminator(firstBlockTerminator, resultsToReplace);
343  firstBlockTerminator->erase();
344 
345  // Merge the post insert block into the cloned entry block.
346  firstNewBlock->getOperations().splice(firstNewBlock->end(),
347  postInsertBlock->getOperations());
348  postInsertBlock->erase();
349  } else {
350  // Otherwise, there were multiple blocks inlined. Add arguments to the post
351  // insertion block to represent the results to replace.
352  for (const auto &resultToRepl : llvm::enumerate(resultsToReplace)) {
353  resultToRepl.value().replaceAllUsesWith(
354  postInsertBlock->addArgument(regionResultTypes[resultToRepl.index()],
355  resultToRepl.value().getLoc()));
356  }
357 
358  // Run the result attribute handler on the post insertion block arguments.
359  builder.setInsertionPointToStart(postInsertBlock);
360  if (call && callable)
361  handleResultImpl(interface, builder, call, callable,
362  postInsertBlock->getArguments());
363 
364  /// Handle the terminators for each of the new blocks.
365  for (auto &newBlock : newBlocks)
366  interface.handleTerminator(newBlock.getTerminator(), postInsertBlock);
367  }
368 
369  // Splice the instructions of the inlined entry block into the insert block.
370  inlineBlock->getOperations().splice(inlineBlock->end(),
371  firstNewBlock->getOperations());
372  firstNewBlock->erase();
373  return success();
374 }
375 
376 static LogicalResult
377 inlineRegionImpl(InlinerInterface &interface, Region *src, Block *inlineBlock,
378  Block::iterator inlinePoint, ValueRange inlinedOperands,
379  ValueRange resultsToReplace, std::optional<Location> inlineLoc,
380  bool shouldCloneInlinedRegion, CallOpInterface call = {}) {
381  // We expect the region to have at least one block.
382  if (src->empty())
383  return failure();
384 
385  auto *entryBlock = &src->front();
386  if (inlinedOperands.size() != entryBlock->getNumArguments())
387  return failure();
388 
389  // Map the provided call operands to the arguments of the region.
390  IRMapping mapper;
391  for (unsigned i = 0, e = inlinedOperands.size(); i != e; ++i) {
392  // Verify that the types of the provided values match the function argument
393  // types.
394  BlockArgument regionArg = entryBlock->getArgument(i);
395  if (inlinedOperands[i].getType() != regionArg.getType())
396  return failure();
397  mapper.map(regionArg, inlinedOperands[i]);
398  }
399 
400  // Call into the main region inliner function.
401  return inlineRegionImpl(interface, src, inlineBlock, inlinePoint, mapper,
402  resultsToReplace, resultsToReplace.getTypes(),
403  inlineLoc, shouldCloneInlinedRegion, call);
404 }
405 
406 LogicalResult mlir::inlineRegion(InlinerInterface &interface, Region *src,
407  Operation *inlinePoint, IRMapping &mapper,
408  ValueRange resultsToReplace,
409  TypeRange regionResultTypes,
410  std::optional<Location> inlineLoc,
411  bool shouldCloneInlinedRegion) {
412  return inlineRegion(interface, src, inlinePoint->getBlock(),
413  ++inlinePoint->getIterator(), mapper, resultsToReplace,
414  regionResultTypes, inlineLoc, shouldCloneInlinedRegion);
415 }
416 LogicalResult mlir::inlineRegion(InlinerInterface &interface, Region *src,
417  Block *inlineBlock,
418  Block::iterator inlinePoint, IRMapping &mapper,
419  ValueRange resultsToReplace,
420  TypeRange regionResultTypes,
421  std::optional<Location> inlineLoc,
422  bool shouldCloneInlinedRegion) {
423  return inlineRegionImpl(interface, src, inlineBlock, inlinePoint, mapper,
424  resultsToReplace, regionResultTypes, inlineLoc,
425  shouldCloneInlinedRegion);
426 }
427 
428 LogicalResult mlir::inlineRegion(InlinerInterface &interface, Region *src,
429  Operation *inlinePoint,
430  ValueRange inlinedOperands,
431  ValueRange resultsToReplace,
432  std::optional<Location> inlineLoc,
433  bool shouldCloneInlinedRegion) {
434  return inlineRegion(interface, src, inlinePoint->getBlock(),
435  ++inlinePoint->getIterator(), inlinedOperands,
436  resultsToReplace, inlineLoc, shouldCloneInlinedRegion);
437 }
438 LogicalResult mlir::inlineRegion(InlinerInterface &interface, Region *src,
439  Block *inlineBlock,
440  Block::iterator inlinePoint,
441  ValueRange inlinedOperands,
442  ValueRange resultsToReplace,
443  std::optional<Location> inlineLoc,
444  bool shouldCloneInlinedRegion) {
445  return inlineRegionImpl(interface, src, inlineBlock, inlinePoint,
446  inlinedOperands, resultsToReplace, inlineLoc,
447  shouldCloneInlinedRegion);
448 }
449 
450 /// Utility function used to generate a cast operation from the given interface,
451 /// or return nullptr if a cast could not be generated.
454  OpBuilder &castBuilder, Value arg, Type type,
455  Location conversionLoc) {
456  if (!interface)
457  return nullptr;
458 
459  // Check to see if the interface for the call can materialize a conversion.
460  Operation *castOp = interface->materializeCallConversion(castBuilder, arg,
461  type, conversionLoc);
462  if (!castOp)
463  return nullptr;
464  castOps.push_back(castOp);
465 
466  // Ensure that the generated cast is correct.
467  assert(castOp->getNumOperands() == 1 && castOp->getOperand(0) == arg &&
468  castOp->getNumResults() == 1 && *castOp->result_type_begin() == type);
469  return castOp->getResult(0);
470 }
471 
472 /// This function inlines a given region, 'src', of a callable operation,
473 /// 'callable', into the location defined by the given call operation. This
474 /// function returns failure if inlining is not possible, success otherwise. On
475 /// failure, no changes are made to the module. 'shouldCloneInlinedRegion'
476 /// corresponds to whether the source region should be cloned into the 'call' or
477 /// spliced directly.
478 LogicalResult mlir::inlineCall(InlinerInterface &interface,
479  CallOpInterface call,
480  CallableOpInterface callable, Region *src,
481  bool shouldCloneInlinedRegion) {
482  // We expect the region to have at least one block.
483  if (src->empty())
484  return failure();
485  auto *entryBlock = &src->front();
486  ArrayRef<Type> callableResultTypes = callable.getResultTypes();
487 
488  // Make sure that the number of arguments and results matchup between the call
489  // and the region.
490  SmallVector<Value, 8> callOperands(call.getArgOperands());
491  SmallVector<Value, 8> callResults(call->getResults());
492  if (callOperands.size() != entryBlock->getNumArguments() ||
493  callResults.size() != callableResultTypes.size())
494  return failure();
495 
496  // A set of cast operations generated to matchup the signature of the region
497  // with the signature of the call.
499  castOps.reserve(callOperands.size() + callResults.size());
500 
501  // Functor used to cleanup generated state on failure.
502  auto cleanupState = [&] {
503  for (auto *op : castOps) {
504  op->getResult(0).replaceAllUsesWith(op->getOperand(0));
505  op->erase();
506  }
507  return failure();
508  };
509 
510  // Builder used for any conversion operations that need to be materialized.
511  OpBuilder castBuilder(call);
512  Location castLoc = call.getLoc();
513  const auto *callInterface = interface.getInterfaceFor(call->getDialect());
514 
515  // Map the provided call operands to the arguments of the region.
516  IRMapping mapper;
517  for (unsigned i = 0, e = callOperands.size(); i != e; ++i) {
518  BlockArgument regionArg = entryBlock->getArgument(i);
519  Value operand = callOperands[i];
520 
521  // If the call operand doesn't match the expected region argument, try to
522  // generate a cast.
523  Type regionArgType = regionArg.getType();
524  if (operand.getType() != regionArgType) {
525  if (!(operand = materializeConversion(callInterface, castOps, castBuilder,
526  operand, regionArgType, castLoc)))
527  return cleanupState();
528  }
529  mapper.map(regionArg, operand);
530  }
531 
532  // Ensure that the resultant values of the call match the callable.
533  castBuilder.setInsertionPointAfter(call);
534  for (unsigned i = 0, e = callResults.size(); i != e; ++i) {
535  Value callResult = callResults[i];
536  if (callResult.getType() == callableResultTypes[i])
537  continue;
538 
539  // Generate a conversion that will produce the original type, so that the IR
540  // is still valid after the original call gets replaced.
541  Value castResult =
542  materializeConversion(callInterface, castOps, castBuilder, callResult,
543  callResult.getType(), castLoc);
544  if (!castResult)
545  return cleanupState();
546  callResult.replaceAllUsesWith(castResult);
547  castResult.getDefiningOp()->replaceUsesOfWith(castResult, callResult);
548  }
549 
550  // Check that it is legal to inline the callable into the call.
551  if (!interface.isLegalToInline(call, callable, shouldCloneInlinedRegion))
552  return cleanupState();
553 
554  // Attempt to inline the call.
555  if (failed(inlineRegionImpl(interface, src, call->getBlock(),
556  ++call->getIterator(), mapper, callResults,
557  callableResultTypes, call.getLoc(),
558  shouldCloneInlinedRegion, call)))
559  return cleanupState();
560  return success();
561 }
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 LogicalResult inlineRegionImpl(InlinerInterface &interface, Region *src, Block *inlineBlock, Block::iterator inlinePoint, IRMapping &mapper, ValueRange resultsToReplace, TypeRange regionResultTypes, std::optional< Location > inlineLoc, bool shouldCloneInlinedRegion, CallOpInterface call={})
static LocationAttr stackLocations(Location callee, Location caller)
Combine callee location with caller location to create a stack that represents the call chain.
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:319
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:267
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
void cloneInto(Region *dest, IRMapping &mapper)
Clone the internal blocks from this region into dest.
Definition: Region.cpp:70
iterator end()
Definition: Region.h:56
iterator begin()
Definition: Region.h:55
BlockListType & getBlocks()
Definition: Region.h:45
Block & front()
Definition: Region.h:65
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
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:381
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:129
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:173
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 inlineRegion(InlinerInterface &interface, 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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult inlineCall(InlinerInterface &interface, 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...