20 #define GEN_PASS_DEF_LLVMLEGALIZEFOREXPORT
21 #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
38 llvm::SmallDenseMap<Block *, SmallVector<int, 4>> successorPositions;
39 for (
int i = 0, e = terminator->getNumSuccessors(); i < e; ++i) {
45 successorPositions[successor].
push_back(i);
54 OpBuilder builder(terminator->getContext());
55 for (
const auto &successor : successorPositions) {
57 for (
int position : llvm::drop_begin(successor.second, 1)) {
59 terminator->setSuccessor(dummyBlock, position);
61 dummyBlock->
addArgument(arg.getType(), arg.getLoc());
62 builder.
create<LLVM::BrOp>(terminator->getLoc(),
71 for (Block &block : llvm::make_early_inc_range(region)) {
72 ::ensureDistinctSuccessors(block);
79 struct LegalizeForExportPass
80 :
public LLVM::impl::LLVMLegalizeForExportBase<LegalizeForExportPass> {
81 void runOnOperation()
override {
89 return std::make_unique<LegalizeForExportPass>();
static void ensureDistinctSuccessors(Block &bb)
If the given block has the same successor with different arguments, introduce dummy successor blocks ...
This class represents an argument of a Block.
Block represents an ordered list of Operations.
unsigned getNumArguments()
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Operation * getTerminator()
Get the terminator operation of this block.
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
BlockArgListType getArguments()
Block * getSuccessor(unsigned i)
void push_back(Operation *op)
This class helps build Operations.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This class provides the API for ops that are known to be terminators.
Operation is the basic unit of execution within MLIR.
bool mightHaveTrait()
Returns true if the operation might have the provided trait.
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
std::unique_ptr< Pass > createLegalizeForExportPass()
Creates a pass that legalizes the LLVM dialect operations so that they can be translated to LLVM IR.
void legalizeDIExpressionsRecursively(Operation *op)
Register all known legalization patterns declared here and apply them to all ops in op.
void ensureDistinctSuccessors(Operation *op)
Make argument-taking successors of each block distinct.
Include the generated interface declarations.