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