2019-09-06 03:23:45 +08:00
|
|
|
//===- Inliner.cpp - Pass to inline function calls ------------------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-09-06 03:23:45 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-10-04 14:04:56 +08:00
|
|
|
//
|
|
|
|
// This file implements a basic inlining algorithm that operates bottom up over
|
|
|
|
// the Strongly Connect Components(SCCs) of the CallGraph. This enables a more
|
|
|
|
// incremental propagation of inlining decisions from the leafs to the roots of
|
|
|
|
// the callgraph.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-09-06 03:23:45 +08:00
|
|
|
|
2020-04-08 04:58:12 +08:00
|
|
|
#include "PassDetail.h"
|
2019-10-04 14:04:56 +08:00
|
|
|
#include "mlir/Analysis/CallGraph.h"
|
2021-06-23 09:16:55 +08:00
|
|
|
#include "mlir/IR/Threading.h"
|
2020-05-14 01:27:19 +08:00
|
|
|
#include "mlir/Interfaces/SideEffectInterfaces.h"
|
2020-12-15 10:07:45 +08:00
|
|
|
#include "mlir/Pass/PassManager.h"
|
2019-09-06 03:23:45 +08:00
|
|
|
#include "mlir/Transforms/InliningUtils.h"
|
|
|
|
#include "mlir/Transforms/Passes.h"
|
2019-10-04 14:04:56 +08:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2019-12-19 04:33:02 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2019-09-06 03:23:45 +08:00
|
|
|
|
2019-12-19 04:33:02 +08:00
|
|
|
#define DEBUG_TYPE "inlining"
|
|
|
|
|
2019-09-06 03:23:45 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2020-12-15 10:07:45 +08:00
|
|
|
/// This function implements the default inliner optimization pipeline.
|
|
|
|
static void defaultInlinerOptPipeline(OpPassManager &pm) {
|
|
|
|
pm.addPass(createCanonicalizerPass());
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:10:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbol Use Tracking
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Walk all of the used symbol callgraph nodes referenced with the given op.
|
|
|
|
static void walkReferencedSymbolNodes(
|
2020-10-17 02:57:10 +08:00
|
|
|
Operation *op, CallGraph &cg, SymbolTableCollection &symbolTable,
|
2020-03-19 04:10:13 +08:00
|
|
|
DenseMap<Attribute, CallGraphNode *> &resolvedRefs,
|
|
|
|
function_ref<void(CallGraphNode *, Operation *)> callback) {
|
|
|
|
auto symbolUses = SymbolTable::getSymbolUses(op);
|
|
|
|
assert(symbolUses && "expected uses to be valid");
|
|
|
|
|
|
|
|
Operation *symbolTableOp = op->getParentOp();
|
|
|
|
for (const SymbolTable::SymbolUse &use : *symbolUses) {
|
|
|
|
auto refIt = resolvedRefs.insert({use.getSymbolRef(), nullptr});
|
|
|
|
CallGraphNode *&node = refIt.first->second;
|
|
|
|
|
|
|
|
// If this is the first instance of this reference, try to resolve a
|
|
|
|
// callgraph node for it.
|
|
|
|
if (refIt.second) {
|
2020-10-17 02:57:10 +08:00
|
|
|
auto *symbolOp = symbolTable.lookupNearestSymbolFrom(symbolTableOp,
|
|
|
|
use.getSymbolRef());
|
2020-03-19 04:10:13 +08:00
|
|
|
auto callableOp = dyn_cast_or_null<CallableOpInterface>(symbolOp);
|
|
|
|
if (!callableOp)
|
|
|
|
continue;
|
|
|
|
node = cg.lookupNode(callableOp.getCallableRegion());
|
|
|
|
}
|
|
|
|
if (node)
|
|
|
|
callback(node, use.getUser());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CGUseList
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// This struct tracks the uses of callgraph nodes that can be dropped when
|
|
|
|
/// use_empty. It directly tracks and manages a use-list for all of the
|
|
|
|
/// call-graph nodes. This is necessary because many callgraph nodes are
|
|
|
|
/// referenced by SymbolRefAttr, which has no mechanism akin to the SSA `Use`
|
|
|
|
/// class.
|
|
|
|
struct CGUseList {
|
|
|
|
/// This struct tracks the uses of callgraph nodes within a specific
|
|
|
|
/// operation.
|
|
|
|
struct CGUser {
|
|
|
|
/// Any nodes referenced in the top-level attribute list of this user. We
|
|
|
|
/// use a set here because the number of references does not matter.
|
|
|
|
DenseSet<CallGraphNode *> topLevelUses;
|
|
|
|
|
|
|
|
/// Uses of nodes referenced by nested operations.
|
|
|
|
DenseMap<CallGraphNode *, int> innerUses;
|
|
|
|
};
|
|
|
|
|
2020-10-17 02:57:10 +08:00
|
|
|
CGUseList(Operation *op, CallGraph &cg, SymbolTableCollection &symbolTable);
|
2020-03-19 04:10:13 +08:00
|
|
|
|
|
|
|
/// Drop uses of nodes referred to by the given call operation that resides
|
|
|
|
/// within 'userNode'.
|
|
|
|
void dropCallUses(CallGraphNode *userNode, Operation *callOp, CallGraph &cg);
|
|
|
|
|
|
|
|
/// Remove the given node from the use list.
|
|
|
|
void eraseNode(CallGraphNode *node);
|
|
|
|
|
|
|
|
/// Returns true if the given callgraph node has no uses and can be pruned.
|
|
|
|
bool isDead(CallGraphNode *node) const;
|
|
|
|
|
|
|
|
/// Returns true if the given callgraph node has a single use and can be
|
|
|
|
/// discarded.
|
|
|
|
bool hasOneUseAndDiscardable(CallGraphNode *node) const;
|
|
|
|
|
|
|
|
/// Recompute the uses held by the given callgraph node.
|
|
|
|
void recomputeUses(CallGraphNode *node, CallGraph &cg);
|
|
|
|
|
|
|
|
/// Merge the uses of 'lhs' with the uses of the 'rhs' after inlining a copy
|
|
|
|
/// of 'lhs' into 'rhs'.
|
|
|
|
void mergeUsesAfterInlining(CallGraphNode *lhs, CallGraphNode *rhs);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Decrement the uses of discardable nodes referenced by the given user.
|
|
|
|
void decrementDiscardableUses(CGUser &uses);
|
|
|
|
|
|
|
|
/// A mapping between a discardable callgraph node (that is a symbol) and the
|
|
|
|
/// number of uses for this node.
|
|
|
|
DenseMap<CallGraphNode *, int> discardableSymNodeUses;
|
2020-10-17 02:57:10 +08:00
|
|
|
|
2020-03-19 04:10:13 +08:00
|
|
|
/// A mapping between a callgraph node and the symbol callgraph nodes that it
|
|
|
|
/// uses.
|
|
|
|
DenseMap<CallGraphNode *, CGUser> nodeUses;
|
2020-10-17 02:57:10 +08:00
|
|
|
|
|
|
|
/// A symbol table to use when resolving call lookups.
|
|
|
|
SymbolTableCollection &symbolTable;
|
2020-03-19 04:10:13 +08:00
|
|
|
};
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace
|
2020-03-19 04:10:13 +08:00
|
|
|
|
2020-10-17 02:57:10 +08:00
|
|
|
CGUseList::CGUseList(Operation *op, CallGraph &cg,
|
|
|
|
SymbolTableCollection &symbolTable)
|
|
|
|
: symbolTable(symbolTable) {
|
2020-03-19 04:10:13 +08:00
|
|
|
/// A set of callgraph nodes that are always known to be live during inlining.
|
|
|
|
DenseMap<Attribute, CallGraphNode *> alwaysLiveNodes;
|
|
|
|
|
|
|
|
// Walk each of the symbol tables looking for discardable callgraph nodes.
|
|
|
|
auto walkFn = [&](Operation *symbolTableOp, bool allUsesVisible) {
|
2020-05-05 08:46:06 +08:00
|
|
|
for (Operation &op : symbolTableOp->getRegion(0).getOps()) {
|
|
|
|
// If this is a callgraph operation, check to see if it is discardable.
|
|
|
|
if (auto callable = dyn_cast<CallableOpInterface>(&op)) {
|
|
|
|
if (auto *node = cg.lookupNode(callable.getCallableRegion())) {
|
|
|
|
SymbolOpInterface symbol = dyn_cast<SymbolOpInterface>(&op);
|
|
|
|
if (symbol && (allUsesVisible || symbol.isPrivate()) &&
|
|
|
|
symbol.canDiscardOnUseEmpty()) {
|
|
|
|
discardableSymNodeUses.try_emplace(node, 0);
|
2020-03-19 04:10:13 +08:00
|
|
|
}
|
2020-05-05 08:46:06 +08:00
|
|
|
continue;
|
2020-03-19 04:10:13 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 08:46:06 +08:00
|
|
|
// Otherwise, check for any referenced nodes. These will be always-live.
|
2020-10-17 02:57:10 +08:00
|
|
|
walkReferencedSymbolNodes(&op, cg, symbolTable, alwaysLiveNodes,
|
2020-05-05 08:46:06 +08:00
|
|
|
[](CallGraphNode *, Operation *) {});
|
2020-03-19 04:10:13 +08:00
|
|
|
}
|
|
|
|
};
|
2020-04-28 03:58:23 +08:00
|
|
|
SymbolTable::walkSymbolTables(op, /*allSymUsesVisible=*/!op->getBlock(),
|
|
|
|
walkFn);
|
2020-03-19 04:10:13 +08:00
|
|
|
|
|
|
|
// Drop the use information for any discardable nodes that are always live.
|
|
|
|
for (auto &it : alwaysLiveNodes)
|
|
|
|
discardableSymNodeUses.erase(it.second);
|
|
|
|
|
|
|
|
// Compute the uses for each of the callable nodes in the graph.
|
|
|
|
for (CallGraphNode *node : cg)
|
|
|
|
recomputeUses(node, cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGUseList::dropCallUses(CallGraphNode *userNode, Operation *callOp,
|
|
|
|
CallGraph &cg) {
|
|
|
|
auto &userRefs = nodeUses[userNode].innerUses;
|
|
|
|
auto walkFn = [&](CallGraphNode *node, Operation *user) {
|
|
|
|
auto parentIt = userRefs.find(node);
|
|
|
|
if (parentIt == userRefs.end())
|
|
|
|
return;
|
|
|
|
--parentIt->second;
|
|
|
|
--discardableSymNodeUses[node];
|
|
|
|
};
|
|
|
|
DenseMap<Attribute, CallGraphNode *> resolvedRefs;
|
2020-10-17 02:57:10 +08:00
|
|
|
walkReferencedSymbolNodes(callOp, cg, symbolTable, resolvedRefs, walkFn);
|
2020-03-19 04:10:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGUseList::eraseNode(CallGraphNode *node) {
|
|
|
|
// Drop all child nodes.
|
|
|
|
for (auto &edge : *node)
|
|
|
|
if (edge.isChild())
|
|
|
|
eraseNode(edge.getTarget());
|
|
|
|
|
|
|
|
// Drop the uses held by this node and erase it.
|
|
|
|
auto useIt = nodeUses.find(node);
|
|
|
|
assert(useIt != nodeUses.end() && "expected node to be valid");
|
|
|
|
decrementDiscardableUses(useIt->getSecond());
|
|
|
|
nodeUses.erase(useIt);
|
|
|
|
discardableSymNodeUses.erase(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGUseList::isDead(CallGraphNode *node) const {
|
|
|
|
// If the parent operation isn't a symbol, simply check normal SSA deadness.
|
|
|
|
Operation *nodeOp = node->getCallableRegion()->getParentOp();
|
2020-04-28 03:57:32 +08:00
|
|
|
if (!isa<SymbolOpInterface>(nodeOp))
|
2020-03-19 04:10:13 +08:00
|
|
|
return MemoryEffectOpInterface::hasNoEffect(nodeOp) && nodeOp->use_empty();
|
|
|
|
|
|
|
|
// Otherwise, check the number of symbol uses.
|
|
|
|
auto symbolIt = discardableSymNodeUses.find(node);
|
|
|
|
return symbolIt != discardableSymNodeUses.end() && symbolIt->second == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGUseList::hasOneUseAndDiscardable(CallGraphNode *node) const {
|
|
|
|
// If this isn't a symbol node, check for side-effects and SSA use count.
|
|
|
|
Operation *nodeOp = node->getCallableRegion()->getParentOp();
|
2020-04-28 03:57:32 +08:00
|
|
|
if (!isa<SymbolOpInterface>(nodeOp))
|
2020-03-19 04:10:13 +08:00
|
|
|
return MemoryEffectOpInterface::hasNoEffect(nodeOp) && nodeOp->hasOneUse();
|
|
|
|
|
|
|
|
// Otherwise, check the number of symbol uses.
|
|
|
|
auto symbolIt = discardableSymNodeUses.find(node);
|
|
|
|
return symbolIt != discardableSymNodeUses.end() && symbolIt->second == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGUseList::recomputeUses(CallGraphNode *node, CallGraph &cg) {
|
|
|
|
Operation *parentOp = node->getCallableRegion()->getParentOp();
|
|
|
|
CGUser &uses = nodeUses[node];
|
|
|
|
decrementDiscardableUses(uses);
|
|
|
|
|
|
|
|
// Collect the new discardable uses within this node.
|
|
|
|
uses = CGUser();
|
|
|
|
DenseMap<Attribute, CallGraphNode *> resolvedRefs;
|
|
|
|
auto walkFn = [&](CallGraphNode *refNode, Operation *user) {
|
|
|
|
auto discardSymIt = discardableSymNodeUses.find(refNode);
|
|
|
|
if (discardSymIt == discardableSymNodeUses.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (user != parentOp)
|
|
|
|
++uses.innerUses[refNode];
|
|
|
|
else if (!uses.topLevelUses.insert(refNode).second)
|
|
|
|
return;
|
|
|
|
++discardSymIt->second;
|
|
|
|
};
|
2020-10-17 02:57:10 +08:00
|
|
|
walkReferencedSymbolNodes(parentOp, cg, symbolTable, resolvedRefs, walkFn);
|
2020-03-19 04:10:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGUseList::mergeUsesAfterInlining(CallGraphNode *lhs, CallGraphNode *rhs) {
|
|
|
|
auto &lhsUses = nodeUses[lhs], &rhsUses = nodeUses[rhs];
|
|
|
|
for (auto &useIt : lhsUses.innerUses) {
|
|
|
|
rhsUses.innerUses[useIt.first] += useIt.second;
|
|
|
|
discardableSymNodeUses[useIt.first] += useIt.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGUseList::decrementDiscardableUses(CGUser &uses) {
|
|
|
|
for (CallGraphNode *node : uses.topLevelUses)
|
|
|
|
--discardableSymNodeUses[node];
|
|
|
|
for (auto &it : uses.innerUses)
|
|
|
|
discardableSymNodeUses[it.first] -= it.second;
|
|
|
|
}
|
|
|
|
|
2019-10-04 14:04:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallGraph traversal
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-06-18 04:13:48 +08:00
|
|
|
namespace {
|
|
|
|
/// This class represents a specific callgraph SCC.
|
|
|
|
class CallGraphSCC {
|
|
|
|
public:
|
|
|
|
CallGraphSCC(llvm::scc_iterator<const CallGraph *> &parentIterator)
|
|
|
|
: parentIterator(parentIterator) {}
|
|
|
|
/// Return a range over the nodes within this SCC.
|
|
|
|
std::vector<CallGraphNode *>::iterator begin() { return nodes.begin(); }
|
|
|
|
std::vector<CallGraphNode *>::iterator end() { return nodes.end(); }
|
|
|
|
|
|
|
|
/// Reset the nodes of this SCC with those provided.
|
|
|
|
void reset(const std::vector<CallGraphNode *> &newNodes) { nodes = newNodes; }
|
|
|
|
|
|
|
|
/// Remove the given node from this SCC.
|
|
|
|
void remove(CallGraphNode *node) {
|
|
|
|
auto it = llvm::find(nodes, node);
|
|
|
|
if (it != nodes.end()) {
|
|
|
|
nodes.erase(it);
|
|
|
|
parentIterator.ReplaceNode(node, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<CallGraphNode *> nodes;
|
|
|
|
llvm::scc_iterator<const CallGraph *> &parentIterator;
|
|
|
|
};
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace
|
2020-06-18 04:13:48 +08:00
|
|
|
|
2019-10-04 14:04:56 +08:00
|
|
|
/// Run a given transformation over the SCCs of the callgraph in a bottom up
|
|
|
|
/// traversal.
|
2020-12-15 10:07:45 +08:00
|
|
|
static LogicalResult runTransformOnCGSCCs(
|
|
|
|
const CallGraph &cg,
|
|
|
|
function_ref<LogicalResult(CallGraphSCC &)> sccTransformer) {
|
2020-06-18 04:13:48 +08:00
|
|
|
llvm::scc_iterator<const CallGraph *> cgi = llvm::scc_begin(&cg);
|
|
|
|
CallGraphSCC currentSCC(cgi);
|
2019-10-11 03:12:39 +08:00
|
|
|
while (!cgi.isAtEnd()) {
|
|
|
|
// Copy the current SCC and increment so that the transformer can modify the
|
|
|
|
// SCC without invalidating our iterator.
|
2020-06-18 04:13:48 +08:00
|
|
|
currentSCC.reset(*cgi);
|
2019-10-11 03:12:39 +08:00
|
|
|
++cgi;
|
2020-12-15 10:07:45 +08:00
|
|
|
if (failed(sccTransformer(currentSCC)))
|
|
|
|
return failure();
|
2019-10-11 03:12:39 +08:00
|
|
|
}
|
2020-12-15 10:07:45 +08:00
|
|
|
return success();
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:23:45 +08:00
|
|
|
namespace {
|
2019-10-04 14:04:56 +08:00
|
|
|
/// This struct represents a resolved call to a given callgraph node. Given that
|
|
|
|
/// the call does not actually contain a direct reference to the
|
|
|
|
/// Region(CallGraphNode) that it is dispatching to, we need to resolve them
|
|
|
|
/// explicitly.
|
|
|
|
struct ResolvedCall {
|
2020-03-19 04:10:13 +08:00
|
|
|
ResolvedCall(CallOpInterface call, CallGraphNode *sourceNode,
|
|
|
|
CallGraphNode *targetNode)
|
|
|
|
: call(call), sourceNode(sourceNode), targetNode(targetNode) {}
|
2019-10-04 14:04:56 +08:00
|
|
|
CallOpInterface call;
|
2020-03-19 04:10:13 +08:00
|
|
|
CallGraphNode *sourceNode, *targetNode;
|
2019-10-04 14:04:56 +08:00
|
|
|
};
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace
|
2019-10-04 14:04:56 +08:00
|
|
|
|
|
|
|
/// Collect all of the callable operations within the given range of blocks. If
|
|
|
|
/// `traverseNestedCGNodes` is true, this will also collect call operations
|
|
|
|
/// inside of nested callgraph nodes.
|
2019-12-19 01:28:48 +08:00
|
|
|
static void collectCallOps(iterator_range<Region::iterator> blocks,
|
2020-03-19 04:10:13 +08:00
|
|
|
CallGraphNode *sourceNode, CallGraph &cg,
|
2020-10-17 02:57:10 +08:00
|
|
|
SymbolTableCollection &symbolTable,
|
2020-03-19 04:10:13 +08:00
|
|
|
SmallVectorImpl<ResolvedCall> &calls,
|
2019-10-04 14:04:56 +08:00
|
|
|
bool traverseNestedCGNodes) {
|
2020-03-19 04:10:13 +08:00
|
|
|
SmallVector<std::pair<Block *, CallGraphNode *>, 8> worklist;
|
|
|
|
auto addToWorklist = [&](CallGraphNode *node,
|
|
|
|
iterator_range<Region::iterator> blocks) {
|
2019-10-04 14:04:56 +08:00
|
|
|
for (Block &block : blocks)
|
2020-03-19 04:10:13 +08:00
|
|
|
worklist.emplace_back(&block, node);
|
2019-10-04 14:04:56 +08:00
|
|
|
};
|
|
|
|
|
2020-03-19 04:10:13 +08:00
|
|
|
addToWorklist(sourceNode, blocks);
|
2019-10-04 14:04:56 +08:00
|
|
|
while (!worklist.empty()) {
|
2020-03-19 04:10:13 +08:00
|
|
|
Block *block;
|
|
|
|
std::tie(block, sourceNode) = worklist.pop_back_val();
|
|
|
|
|
|
|
|
for (Operation &op : *block) {
|
2019-10-04 14:04:56 +08:00
|
|
|
if (auto call = dyn_cast<CallOpInterface>(op)) {
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: Support inlining nested call references.
|
2020-02-09 02:44:15 +08:00
|
|
|
CallInterfaceCallable callable = call.getCallableForCallee();
|
2020-01-14 07:46:40 +08:00
|
|
|
if (SymbolRefAttr symRef = callable.dyn_cast<SymbolRefAttr>()) {
|
|
|
|
if (!symRef.isa<FlatSymbolRefAttr>())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-17 02:57:10 +08:00
|
|
|
CallGraphNode *targetNode = cg.resolveCallable(call, symbolTable);
|
2020-03-19 04:10:13 +08:00
|
|
|
if (!targetNode->isExternal())
|
|
|
|
calls.emplace_back(call, sourceNode, targetNode);
|
2019-09-06 03:23:45 +08:00
|
|
|
continue;
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
2019-09-06 03:23:45 +08:00
|
|
|
|
2019-10-04 14:04:56 +08:00
|
|
|
// If this is not a call, traverse the nested regions. If
|
|
|
|
// `traverseNestedCGNodes` is false, then don't traverse nested call graph
|
|
|
|
// regions.
|
2020-03-19 04:10:13 +08:00
|
|
|
for (auto &nestedRegion : op.getRegions()) {
|
|
|
|
CallGraphNode *nestedNode = cg.lookupNode(&nestedRegion);
|
|
|
|
if (traverseNestedCGNodes || !nestedNode)
|
|
|
|
addToWorklist(nestedNode ? nestedNode : sourceNode, nestedRegion);
|
|
|
|
}
|
2019-09-06 03:23:45 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Inliner
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
|
|
|
/// This class provides a specialization of the main inlining interface.
|
|
|
|
struct Inliner : public InlinerInterface {
|
2020-10-17 02:57:10 +08:00
|
|
|
Inliner(MLIRContext *context, CallGraph &cg,
|
|
|
|
SymbolTableCollection &symbolTable)
|
|
|
|
: InlinerInterface(context), cg(cg), symbolTable(symbolTable) {}
|
2019-10-04 14:04:56 +08:00
|
|
|
|
|
|
|
/// Process a set of blocks that have been inlined. This callback is invoked
|
|
|
|
/// *before* inlined terminator operations have been processed.
|
2019-12-19 01:28:48 +08:00
|
|
|
void
|
|
|
|
processInlinedBlocks(iterator_range<Region::iterator> inlinedBlocks) final {
|
2020-03-19 04:10:13 +08:00
|
|
|
// Find the closest callgraph node from the first block.
|
|
|
|
CallGraphNode *node;
|
|
|
|
Region *region = inlinedBlocks.begin()->getParent();
|
|
|
|
while (!(node = cg.lookupNode(region))) {
|
|
|
|
region = region->getParentRegion();
|
|
|
|
assert(region && "expected valid parent node");
|
|
|
|
}
|
|
|
|
|
2020-10-17 02:57:10 +08:00
|
|
|
collectCallOps(inlinedBlocks, node, cg, symbolTable, calls,
|
2020-03-19 04:10:13 +08:00
|
|
|
/*traverseNestedCGNodes=*/true);
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 04:13:48 +08:00
|
|
|
/// Mark the given callgraph node for deletion.
|
|
|
|
void markForDeletion(CallGraphNode *node) { deadNodes.insert(node); }
|
|
|
|
|
|
|
|
/// This method properly disposes of callables that became dead during
|
|
|
|
/// inlining. This should not be called while iterating over the SCCs.
|
|
|
|
void eraseDeadCallables() {
|
|
|
|
for (CallGraphNode *node : deadNodes)
|
|
|
|
node->getCallableRegion()->getParentOp()->erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The set of callables known to be dead.
|
|
|
|
SmallPtrSet<CallGraphNode *, 8> deadNodes;
|
|
|
|
|
2019-10-04 14:04:56 +08:00
|
|
|
/// The current set of call instructions to consider for inlining.
|
|
|
|
SmallVector<ResolvedCall, 8> calls;
|
|
|
|
|
|
|
|
/// The callgraph being operated on.
|
|
|
|
CallGraph &cg;
|
2020-10-17 02:57:10 +08:00
|
|
|
|
|
|
|
/// A symbol table to use when resolving call lookups.
|
|
|
|
SymbolTableCollection &symbolTable;
|
2019-10-04 14:04:56 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/// Returns true if the given call should be inlined.
|
|
|
|
static bool shouldInline(ResolvedCall &resolvedCall) {
|
|
|
|
// Don't allow inlining terminator calls. We currently don't support this
|
|
|
|
// case.
|
2021-02-10 03:41:10 +08:00
|
|
|
if (resolvedCall.call->hasTrait<OpTrait::IsTerminator>())
|
2019-10-04 14:04:56 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't allow inlining if the target is an ancestor of the call. This
|
|
|
|
// prevents inlining recursively.
|
|
|
|
if (resolvedCall.targetNode->getCallableRegion()->isAncestor(
|
2020-12-09 18:50:18 +08:00
|
|
|
resolvedCall.call->getParentRegion()))
|
2019-10-04 14:04:56 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, inline.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-11 03:12:39 +08:00
|
|
|
/// Attempt to inline calls within the given scc. This function returns
|
|
|
|
/// success if any calls were inlined, failure otherwise.
|
2020-06-18 04:13:48 +08:00
|
|
|
static LogicalResult inlineCallsInSCC(Inliner &inliner, CGUseList &useList,
|
|
|
|
CallGraphSCC ¤tSCC) {
|
2019-10-04 14:04:56 +08:00
|
|
|
CallGraph &cg = inliner.cg;
|
|
|
|
auto &calls = inliner.calls;
|
|
|
|
|
2020-06-18 04:13:48 +08:00
|
|
|
// A set of dead nodes to remove after inlining.
|
2022-01-10 18:39:06 +08:00
|
|
|
llvm::SmallSetVector<CallGraphNode *, 1> deadNodes;
|
2020-06-18 04:13:48 +08:00
|
|
|
|
2019-10-04 14:04:56 +08:00
|
|
|
// Collect all of the direct calls within the nodes of the current SCC. We
|
|
|
|
// don't traverse nested callgraph nodes, because they are handled separately
|
|
|
|
// likely within a different SCC.
|
2020-03-19 04:10:13 +08:00
|
|
|
for (CallGraphNode *node : currentSCC) {
|
|
|
|
if (node->isExternal())
|
|
|
|
continue;
|
|
|
|
|
2020-06-18 04:13:48 +08:00
|
|
|
// Don't collect calls if the node is already dead.
|
2020-10-17 02:57:10 +08:00
|
|
|
if (useList.isDead(node)) {
|
2022-01-10 18:39:06 +08:00
|
|
|
deadNodes.insert(node);
|
2020-10-17 02:57:10 +08:00
|
|
|
} else {
|
|
|
|
collectCallOps(*node->getCallableRegion(), node, cg, inliner.symbolTable,
|
|
|
|
calls, /*traverseNestedCGNodes=*/false);
|
|
|
|
}
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
2020-03-19 04:10:13 +08:00
|
|
|
|
2019-10-04 14:04:56 +08:00
|
|
|
// Try to inline each of the call operations. Don't cache the end iterator
|
|
|
|
// here as more calls may be added during inlining.
|
2019-10-11 03:12:39 +08:00
|
|
|
bool inlinedAnyCalls = false;
|
2019-10-04 14:04:56 +08:00
|
|
|
for (unsigned i = 0; i != calls.size(); ++i) {
|
2022-01-10 18:39:06 +08:00
|
|
|
if (deadNodes.contains(calls[i].sourceNode))
|
|
|
|
continue;
|
2020-04-16 08:41:31 +08:00
|
|
|
ResolvedCall it = calls[i];
|
2020-04-18 11:14:41 +08:00
|
|
|
bool doInline = shouldInline(it);
|
2020-06-11 08:37:59 +08:00
|
|
|
CallOpInterface call = it.call;
|
2019-12-19 04:33:02 +08:00
|
|
|
LLVM_DEBUG({
|
2020-04-18 11:14:41 +08:00
|
|
|
if (doInline)
|
2020-06-11 08:37:59 +08:00
|
|
|
llvm::dbgs() << "* Inlining call: " << call << "\n";
|
2020-04-18 11:14:41 +08:00
|
|
|
else
|
2020-06-11 08:37:59 +08:00
|
|
|
llvm::dbgs() << "* Not inlining call: " << call << "\n";
|
2019-12-19 04:33:02 +08:00
|
|
|
});
|
2020-04-18 11:14:41 +08:00
|
|
|
if (!doInline)
|
2019-10-04 14:04:56 +08:00
|
|
|
continue;
|
2019-10-04 14:10:25 +08:00
|
|
|
Region *targetRegion = it.targetNode->getCallableRegion();
|
2020-03-19 04:10:13 +08:00
|
|
|
|
|
|
|
// If this is the last call to the target node and the node is discardable,
|
|
|
|
// then inline it in-place and delete the node if successful.
|
|
|
|
bool inlineInPlace = useList.hasOneUseAndDiscardable(it.targetNode);
|
|
|
|
|
2019-10-04 14:10:25 +08:00
|
|
|
LogicalResult inlineResult = inlineCall(
|
|
|
|
inliner, call, cast<CallableOpInterface>(targetRegion->getParentOp()),
|
2020-03-19 04:10:13 +08:00
|
|
|
targetRegion, /*shouldCloneInlinedRegion=*/!inlineInPlace);
|
2020-06-11 08:37:59 +08:00
|
|
|
if (failed(inlineResult)) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "** Failed to inline\n");
|
2019-10-04 14:04:56 +08:00
|
|
|
continue;
|
2020-06-11 08:37:59 +08:00
|
|
|
}
|
2020-03-19 04:10:13 +08:00
|
|
|
inlinedAnyCalls = true;
|
|
|
|
|
|
|
|
// If the inlining was successful, Merge the new uses into the source node.
|
|
|
|
useList.dropCallUses(it.sourceNode, call.getOperation(), cg);
|
|
|
|
useList.mergeUsesAfterInlining(it.targetNode, it.sourceNode);
|
2019-10-04 14:04:56 +08:00
|
|
|
|
2020-03-19 04:10:13 +08:00
|
|
|
// then erase the call.
|
2019-10-04 14:04:56 +08:00
|
|
|
call.erase();
|
2020-03-19 04:10:13 +08:00
|
|
|
|
|
|
|
// If we inlined in place, mark the node for deletion.
|
|
|
|
if (inlineInPlace) {
|
|
|
|
useList.eraseNode(it.targetNode);
|
2022-01-10 18:39:06 +08:00
|
|
|
deadNodes.insert(it.targetNode);
|
2020-03-19 04:10:13 +08:00
|
|
|
}
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
2020-03-19 04:10:13 +08:00
|
|
|
|
2020-06-18 04:13:48 +08:00
|
|
|
for (CallGraphNode *node : deadNodes) {
|
|
|
|
currentSCC.remove(node);
|
|
|
|
inliner.markForDeletion(node);
|
|
|
|
}
|
2019-10-04 14:04:56 +08:00
|
|
|
calls.clear();
|
2019-10-11 03:12:39 +08:00
|
|
|
return success(inlinedAnyCalls);
|
|
|
|
}
|
|
|
|
|
2020-04-09 03:57:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// InlinerPass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2020-12-15 10:07:45 +08:00
|
|
|
class InlinerPass : public InlinerBase<InlinerPass> {
|
|
|
|
public:
|
|
|
|
InlinerPass();
|
|
|
|
InlinerPass(const InlinerPass &) = default;
|
|
|
|
InlinerPass(std::function<void(OpPassManager &)> defaultPipeline);
|
|
|
|
InlinerPass(std::function<void(OpPassManager &)> defaultPipeline,
|
|
|
|
llvm::StringMap<OpPassManager> opPipelines);
|
2020-04-09 03:57:02 +08:00
|
|
|
void runOnOperation() override;
|
|
|
|
|
2020-12-15 10:07:45 +08:00
|
|
|
private:
|
|
|
|
/// Attempt to inline calls within the given scc, and run simplifications,
|
|
|
|
/// until a fixed point is reached. This allows for the inlining of newly
|
|
|
|
/// devirtualized calls. Returns failure if there was a fatal error during
|
|
|
|
/// inlining.
|
|
|
|
LogicalResult inlineSCC(Inliner &inliner, CGUseList &useList,
|
|
|
|
CallGraphSCC ¤tSCC, MLIRContext *context);
|
|
|
|
|
|
|
|
/// Optimize the nodes within the given SCC with one of the held optimization
|
|
|
|
/// pass pipelines. Returns failure if an error occurred during the
|
|
|
|
/// optimization of the SCC, success otherwise.
|
|
|
|
LogicalResult optimizeSCC(CallGraph &cg, CGUseList &useList,
|
|
|
|
CallGraphSCC ¤tSCC, MLIRContext *context);
|
|
|
|
|
|
|
|
/// Optimize the nodes within the given SCC in parallel. Returns failure if an
|
|
|
|
/// error occurred during the optimization of the SCC, success otherwise.
|
|
|
|
LogicalResult optimizeSCCAsync(MutableArrayRef<CallGraphNode *> nodesToVisit,
|
|
|
|
MLIRContext *context);
|
|
|
|
|
|
|
|
/// Optimize the given callable node with one of the pass managers provided
|
|
|
|
/// with `pipelines`, or the default pipeline. Returns failure if an error
|
|
|
|
/// occurred during the optimization of the callable, success otherwise.
|
|
|
|
LogicalResult optimizeCallable(CallGraphNode *node,
|
|
|
|
llvm::StringMap<OpPassManager> &pipelines);
|
|
|
|
|
|
|
|
/// Attempt to initialize the options of this pass from the given string.
|
|
|
|
/// Derived classes may override this method to hook into the point at which
|
|
|
|
/// options are initialized, but should generally always invoke this base
|
|
|
|
/// class variant.
|
|
|
|
LogicalResult initializeOptions(StringRef options) override;
|
|
|
|
|
|
|
|
/// An optional function that constructs a default optimization pipeline for
|
|
|
|
/// a given operation.
|
|
|
|
std::function<void(OpPassManager &)> defaultPipeline;
|
|
|
|
/// A map of operation names to pass pipelines to use when optimizing
|
|
|
|
/// callable operations of these types. This provides a specialized pipeline
|
|
|
|
/// instead of the default. The vector size is the number of threads used
|
|
|
|
/// during optimization.
|
|
|
|
SmallVector<llvm::StringMap<OpPassManager>, 8> opPipelines;
|
2020-04-09 03:57:02 +08:00
|
|
|
};
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace
|
2020-04-09 03:57:02 +08:00
|
|
|
|
2020-12-15 10:07:45 +08:00
|
|
|
InlinerPass::InlinerPass() : InlinerPass(defaultInlinerOptPipeline) {}
|
|
|
|
InlinerPass::InlinerPass(std::function<void(OpPassManager &)> defaultPipeline)
|
2022-01-02 09:26:44 +08:00
|
|
|
: defaultPipeline(std::move(defaultPipeline)) {
|
2020-12-15 10:07:45 +08:00
|
|
|
opPipelines.push_back({});
|
|
|
|
|
|
|
|
// Initialize the pass options with the provided arguments.
|
|
|
|
if (defaultPipeline) {
|
|
|
|
OpPassManager fakePM("__mlir_fake_pm_op");
|
|
|
|
defaultPipeline(fakePM);
|
|
|
|
llvm::raw_string_ostream strStream(defaultPipelineStr);
|
|
|
|
fakePM.printAsTextualPipeline(strStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InlinerPass::InlinerPass(std::function<void(OpPassManager &)> defaultPipeline,
|
|
|
|
llvm::StringMap<OpPassManager> opPipelines)
|
|
|
|
: InlinerPass(std::move(defaultPipeline)) {
|
|
|
|
if (opPipelines.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the option for the op specific optimization pipelines.
|
|
|
|
for (auto &it : opPipelines) {
|
|
|
|
std::string pipeline;
|
|
|
|
llvm::raw_string_ostream pipelineOS(pipeline);
|
|
|
|
pipelineOS << it.getKey() << "(";
|
|
|
|
it.second.printAsTextualPipeline(pipelineOS);
|
|
|
|
pipelineOS << ")";
|
|
|
|
opPipelineStrs.addValue(pipeline);
|
|
|
|
}
|
|
|
|
this->opPipelines.emplace_back(std::move(opPipelines));
|
|
|
|
}
|
|
|
|
|
2020-04-09 03:57:02 +08:00
|
|
|
void InlinerPass::runOnOperation() {
|
|
|
|
CallGraph &cg = getAnalysis<CallGraph>();
|
|
|
|
auto *context = &getContext();
|
|
|
|
|
|
|
|
// The inliner should only be run on operations that define a symbol table,
|
|
|
|
// as the callgraph will need to resolve references.
|
|
|
|
Operation *op = getOperation();
|
|
|
|
if (!op->hasTrait<OpTrait::SymbolTable>()) {
|
|
|
|
op->emitOpError() << " was scheduled to run under the inliner, but does "
|
|
|
|
"not define a symbol table";
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the inline transform in post-order over the SCCs in the callgraph.
|
2020-10-17 02:57:10 +08:00
|
|
|
SymbolTableCollection symbolTable;
|
|
|
|
Inliner inliner(context, cg, symbolTable);
|
|
|
|
CGUseList useList(getOperation(), cg, symbolTable);
|
2020-12-15 10:07:45 +08:00
|
|
|
LogicalResult result = runTransformOnCGSCCs(cg, [&](CallGraphSCC &scc) {
|
|
|
|
return inlineSCC(inliner, useList, scc, context);
|
2020-04-09 03:57:02 +08:00
|
|
|
});
|
2020-12-15 10:07:45 +08:00
|
|
|
if (failed(result))
|
|
|
|
return signalPassFailure();
|
2020-06-18 04:13:48 +08:00
|
|
|
|
|
|
|
// After inlining, make sure to erase any callables proven to be dead.
|
|
|
|
inliner.eraseDeadCallables();
|
2020-04-09 03:57:02 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 10:07:45 +08:00
|
|
|
LogicalResult InlinerPass::inlineSCC(Inliner &inliner, CGUseList &useList,
|
|
|
|
CallGraphSCC ¤tSCC,
|
|
|
|
MLIRContext *context) {
|
|
|
|
// Continuously simplify and inline until we either reach a fixed point, or
|
|
|
|
// hit the maximum iteration count. Simplifying early helps to refine the cost
|
|
|
|
// model, and in future iterations may devirtualize new calls.
|
2019-10-11 03:12:39 +08:00
|
|
|
unsigned iterationCount = 0;
|
2020-12-15 10:07:45 +08:00
|
|
|
do {
|
|
|
|
if (failed(optimizeSCC(inliner.cg, useList, currentSCC, context)))
|
|
|
|
return failure();
|
|
|
|
if (failed(inlineCallsInSCC(inliner, useList, currentSCC)))
|
2019-10-11 03:12:39 +08:00
|
|
|
break;
|
2020-12-15 10:07:45 +08:00
|
|
|
} while (++iterationCount < maxInliningIterations);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult InlinerPass::optimizeSCC(CallGraph &cg, CGUseList &useList,
|
|
|
|
CallGraphSCC ¤tSCC,
|
|
|
|
MLIRContext *context) {
|
|
|
|
// Collect the sets of nodes to simplify.
|
|
|
|
SmallVector<CallGraphNode *, 4> nodesToVisit;
|
|
|
|
for (auto *node : currentSCC) {
|
|
|
|
if (node->isExternal())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Don't simplify nodes with children. Nodes with children require special
|
|
|
|
// handling as we may remove the node during simplification. In the future,
|
|
|
|
// we should be able to handle this case with proper node deletion tracking.
|
|
|
|
if (node->hasChildren())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We also won't apply simplifications to nodes that can't have passes
|
|
|
|
// scheduled on them.
|
|
|
|
auto *region = node->getCallableRegion();
|
2021-02-10 03:41:10 +08:00
|
|
|
if (!region->getParentOp()->hasTrait<OpTrait::IsIsolatedFromAbove>())
|
2020-12-15 10:07:45 +08:00
|
|
|
continue;
|
|
|
|
nodesToVisit.push_back(node);
|
|
|
|
}
|
|
|
|
if (nodesToVisit.empty())
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// Optimize each of the nodes within the SCC in parallel.
|
2021-06-23 09:16:55 +08:00
|
|
|
if (failed(optimizeSCCAsync(nodesToVisit, context)))
|
2021-12-24 08:13:54 +08:00
|
|
|
return failure();
|
2020-12-15 10:07:45 +08:00
|
|
|
|
|
|
|
// Recompute the uses held by each of the nodes.
|
|
|
|
for (CallGraphNode *node : nodesToVisit)
|
|
|
|
useList.recomputeUses(node, cg);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
InlinerPass::optimizeSCCAsync(MutableArrayRef<CallGraphNode *> nodesToVisit,
|
2021-06-23 09:16:55 +08:00
|
|
|
MLIRContext *ctx) {
|
2021-12-24 08:13:54 +08:00
|
|
|
// We must maintain a fixed pool of pass managers which is at least as large
|
|
|
|
// as the maximum parallelism of the failableParallelForEach below.
|
|
|
|
// Note: The number of pass managers here needs to remain constant
|
2021-02-24 08:40:09 +08:00
|
|
|
// to prevent issues with pass instrumentations that rely on having the same
|
|
|
|
// pass manager for the main thread.
|
2021-12-24 09:41:21 +08:00
|
|
|
size_t numThreads = ctx->getNumThreads();
|
2021-02-24 06:22:30 +08:00
|
|
|
if (opPipelines.size() < numThreads) {
|
2020-12-15 10:07:45 +08:00
|
|
|
// Reserve before resizing so that we can use a reference to the first
|
|
|
|
// element.
|
|
|
|
opPipelines.reserve(numThreads);
|
|
|
|
opPipelines.resize(numThreads, opPipelines.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure an analysis manager has been constructed for each of the nodes.
|
|
|
|
// This prevents thread races when running the nested pipelines.
|
|
|
|
for (CallGraphNode *node : nodesToVisit)
|
|
|
|
getAnalysisManager().nest(node->getCallableRegion()->getParentOp());
|
|
|
|
|
2021-06-23 09:16:55 +08:00
|
|
|
// An atomic failure variable for the async executors.
|
|
|
|
std::vector<std::atomic<bool>> activePMs(opPipelines.size());
|
|
|
|
std::fill(activePMs.begin(), activePMs.end(), false);
|
|
|
|
return failableParallelForEach(ctx, nodesToVisit, [&](CallGraphNode *node) {
|
|
|
|
// Find a pass manager for this operation.
|
|
|
|
auto it = llvm::find_if(activePMs, [](std::atomic<bool> &isActive) {
|
|
|
|
bool expectedInactive = false;
|
|
|
|
return isActive.compare_exchange_strong(expectedInactive, true);
|
|
|
|
});
|
2021-12-24 08:13:54 +08:00
|
|
|
assert(it != activePMs.end() &&
|
|
|
|
"could not find inactive pass manager for thread");
|
2021-06-23 09:16:55 +08:00
|
|
|
unsigned pmIndex = it - activePMs.begin();
|
|
|
|
|
|
|
|
// Optimize this callable node.
|
|
|
|
LogicalResult result = optimizeCallable(node, opPipelines[pmIndex]);
|
|
|
|
|
|
|
|
// Reset the active bit for this pass manager.
|
|
|
|
activePMs[pmIndex].store(false);
|
|
|
|
return result;
|
|
|
|
});
|
2020-12-15 10:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
InlinerPass::optimizeCallable(CallGraphNode *node,
|
|
|
|
llvm::StringMap<OpPassManager> &pipelines) {
|
|
|
|
Operation *callable = node->getCallableRegion()->getParentOp();
|
|
|
|
StringRef opName = callable->getName().getStringRef();
|
|
|
|
auto pipelineIt = pipelines.find(opName);
|
|
|
|
if (pipelineIt == pipelines.end()) {
|
|
|
|
// If a pipeline didn't exist, use the default if possible.
|
|
|
|
if (!defaultPipeline)
|
|
|
|
return success();
|
|
|
|
|
|
|
|
OpPassManager defaultPM(opName);
|
|
|
|
defaultPipeline(defaultPM);
|
|
|
|
pipelineIt = pipelines.try_emplace(opName, std::move(defaultPM)).first;
|
2019-10-11 03:12:39 +08:00
|
|
|
}
|
2020-12-15 10:07:45 +08:00
|
|
|
return runPipeline(pipelineIt->second, callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult InlinerPass::initializeOptions(StringRef options) {
|
|
|
|
if (failed(Pass::initializeOptions(options)))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Initialize the default pipeline builder to use the option string.
|
|
|
|
if (!defaultPipelineStr.empty()) {
|
|
|
|
std::string defaultPipelineCopy = defaultPipelineStr;
|
|
|
|
defaultPipeline = [=](OpPassManager &pm) {
|
2021-02-05 06:57:50 +08:00
|
|
|
(void)parsePassPipeline(defaultPipelineCopy, pm);
|
2020-12-15 10:07:45 +08:00
|
|
|
};
|
|
|
|
} else if (defaultPipelineStr.getNumOccurrences()) {
|
|
|
|
defaultPipeline = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the op specific pass pipelines.
|
|
|
|
llvm::StringMap<OpPassManager> pipelines;
|
|
|
|
for (StringRef pipeline : opPipelineStrs) {
|
2020-12-30 05:59:53 +08:00
|
|
|
// Skip empty pipelines.
|
|
|
|
if (pipeline.empty())
|
|
|
|
continue;
|
2022-01-03 15:12:03 +08:00
|
|
|
FailureOr<OpPassManager> pm = parsePassPipeline(pipeline);
|
|
|
|
if (failed(pm))
|
2020-12-15 10:07:45 +08:00
|
|
|
return failure();
|
2022-01-03 15:12:03 +08:00
|
|
|
pipelines.try_emplace(pm->getOpName(), std::move(*pm));
|
2020-12-15 10:07:45 +08:00
|
|
|
}
|
|
|
|
opPipelines.assign({std::move(pipelines)});
|
|
|
|
|
|
|
|
return success();
|
2019-10-04 14:04:56 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 03:08:55 +08:00
|
|
|
std::unique_ptr<Pass> mlir::createInlinerPass() {
|
|
|
|
return std::make_unique<InlinerPass>();
|
|
|
|
}
|
2020-12-15 10:07:45 +08:00
|
|
|
std::unique_ptr<Pass>
|
|
|
|
mlir::createInlinerPass(llvm::StringMap<OpPassManager> opPipelines) {
|
|
|
|
return std::make_unique<InlinerPass>(defaultInlinerOptPipeline,
|
|
|
|
std::move(opPipelines));
|
|
|
|
}
|
2021-09-13 17:58:07 +08:00
|
|
|
std::unique_ptr<Pass> mlir::createInlinerPass(
|
|
|
|
llvm::StringMap<OpPassManager> opPipelines,
|
|
|
|
std::function<void(OpPassManager &)> defaultPipelineBuilder) {
|
2020-12-15 10:07:45 +08:00
|
|
|
return std::make_unique<InlinerPass>(std::move(defaultPipelineBuilder),
|
|
|
|
std::move(opPipelines));
|
|
|
|
}
|