MLIR  20.0.0git
LegalizeForExport.cpp
Go to the documentation of this file.
1 //===- LegalizeForExport.cpp - Prepare for translation to LLVM IR ---------===//
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 
10 
13 #include "mlir/IR/Block.h"
14 #include "mlir/IR/Builders.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/Pass/Pass.h"
17 
18 namespace mlir {
19 namespace LLVM {
20 #define GEN_PASS_DEF_LLVMLEGALIZEFOREXPORT
21 #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
22 } // namespace LLVM
23 } // namespace mlir
24 
25 using namespace mlir;
26 
27 /// If the given block has the same successor with different arguments,
28 /// introduce dummy successor blocks so that all successors of the given block
29 /// are different.
30 static void ensureDistinctSuccessors(Block &bb) {
31  // Early exit if the block cannot have successors.
32  if (bb.empty() || !bb.back().mightHaveTrait<OpTrait::IsTerminator>())
33  return;
34 
35  auto *terminator = bb.getTerminator();
36 
37  // Find repeated successors with arguments.
38  llvm::SmallDenseMap<Block *, SmallVector<int, 4>> successorPositions;
39  for (int i = 0, e = terminator->getNumSuccessors(); i < e; ++i) {
40  Block *successor = terminator->getSuccessor(i);
41  // Blocks with no arguments are safe even if they appear multiple times
42  // because they don't need PHI nodes.
43  if (successor->getNumArguments() == 0)
44  continue;
45  successorPositions[successor].push_back(i);
46  }
47 
48  // If a successor appears for the second or more time in the terminator,
49  // create a new dummy block that unconditionally branches to the original
50  // destination, and retarget the terminator to branch to this new block.
51  // There is no need to pass arguments to the dummy block because it will be
52  // dominated by the original block and can therefore use any values defined in
53  // the original block.
54  OpBuilder builder(terminator->getContext());
55  for (const auto &successor : successorPositions) {
56  // Start from the second occurrence of a block in the successor list.
57  for (int position : llvm::drop_begin(successor.second, 1)) {
58  Block *dummyBlock = builder.createBlock(bb.getParent());
59  terminator->setSuccessor(dummyBlock, position);
60  for (BlockArgument arg : successor.first->getArguments())
61  dummyBlock->addArgument(arg.getType(), arg.getLoc());
62  builder.create<LLVM::BrOp>(terminator->getLoc(),
63  dummyBlock->getArguments(), successor.first);
64  }
65  }
66 }
67 
69  op->walk([](Operation *nested) {
70  for (Region &region : llvm::make_early_inc_range(nested->getRegions())) {
71  for (Block &block : llvm::make_early_inc_range(region)) {
72  ::ensureDistinctSuccessors(block);
73  }
74  }
75  });
76 }
77 
78 namespace {
79 struct LegalizeForExportPass
80  : public LLVM::impl::LLVMLegalizeForExportBase<LegalizeForExportPass> {
81  void runOnOperation() override {
82  LLVM::ensureDistinctSuccessors(getOperation());
84  }
85 };
86 } // namespace
87 
88 std::unique_ptr<Pass> LLVM::createLegalizeForExportPass() {
89  return std::make_unique<LegalizeForExportPass>();
90 }
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.
Definition: Value.h:319
Block represents an ordered list of Operations.
Definition: Block.h:33
bool empty()
Definition: Block.h:148
unsigned getNumArguments()
Definition: Block.h:128
Operation & back()
Definition: Block.h:152
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
BlockArgListType getArguments()
Definition: Block.h:87
Block * getSuccessor(unsigned i)
Definition: Block.cpp:261
void push_back(Operation *op)
Definition: Block.h:149
This class helps build Operations.
Definition: Builders.h:215
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.
Definition: Builders.cpp:470
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:497
This class provides the API for ops that are known to be terminators.
Definition: OpDefinition.h:764
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool mightHaveTrait()
Returns true if the operation might have the provided trait.
Definition: Operation.h:753
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),...
Definition: Operation.h:793
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition: Operation.h:672
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
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.