1e119980fSAlex Zinenko //===- LegalizeForExport.cpp - Prepare for translation to LLVM IR ---------===//
2e119980fSAlex Zinenko //
3e119980fSAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e119980fSAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
5e119980fSAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e119980fSAlex Zinenko //
7e119980fSAlex Zinenko //===----------------------------------------------------------------------===//
8e119980fSAlex Zinenko 
9e119980fSAlex Zinenko #include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h"
101834ad4aSRiver Riddle #include "PassDetail.h"
11e119980fSAlex Zinenko #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
12e119980fSAlex Zinenko #include "mlir/IR/Block.h"
13e119980fSAlex Zinenko #include "mlir/IR/Builders.h"
1465fcddffSRiver Riddle #include "mlir/IR/BuiltinOps.h"
15e119980fSAlex Zinenko 
16e119980fSAlex Zinenko using namespace mlir;
17e119980fSAlex Zinenko 
181ad48d6dSAlex Zinenko /// If the given block has the same successor with different arguments,
191ad48d6dSAlex Zinenko /// introduce dummy successor blocks so that all successors of the given block
201ad48d6dSAlex Zinenko /// are different.
ensureDistinctSuccessors(Block & bb)21e119980fSAlex Zinenko static void ensureDistinctSuccessors(Block &bb) {
221ad48d6dSAlex Zinenko   // Early exit if the block cannot have successors.
231ad48d6dSAlex Zinenko   if (bb.empty() || !bb.back().mightHaveTrait<OpTrait::IsTerminator>())
241ad48d6dSAlex Zinenko     return;
251ad48d6dSAlex Zinenko 
26e119980fSAlex Zinenko   auto *terminator = bb.getTerminator();
27e119980fSAlex Zinenko 
28e119980fSAlex Zinenko   // Find repeated successors with arguments.
29e119980fSAlex Zinenko   llvm::SmallDenseMap<Block *, SmallVector<int, 4>> successorPositions;
30e119980fSAlex Zinenko   for (int i = 0, e = terminator->getNumSuccessors(); i < e; ++i) {
31e119980fSAlex Zinenko     Block *successor = terminator->getSuccessor(i);
32e119980fSAlex Zinenko     // Blocks with no arguments are safe even if they appear multiple times
33e119980fSAlex Zinenko     // because they don't need PHI nodes.
34e119980fSAlex Zinenko     if (successor->getNumArguments() == 0)
35e119980fSAlex Zinenko       continue;
36e119980fSAlex Zinenko     successorPositions[successor].push_back(i);
37e119980fSAlex Zinenko   }
38e119980fSAlex Zinenko 
39e119980fSAlex Zinenko   // If a successor appears for the second or more time in the terminator,
40e119980fSAlex Zinenko   // create a new dummy block that unconditionally branches to the original
41e119980fSAlex Zinenko   // destination, and retarget the terminator to branch to this new block.
42e119980fSAlex Zinenko   // There is no need to pass arguments to the dummy block because it will be
43e119980fSAlex Zinenko   // dominated by the original block and can therefore use any values defined in
44e119980fSAlex Zinenko   // the original block.
45e119980fSAlex Zinenko   OpBuilder builder(terminator->getContext());
46e119980fSAlex Zinenko   for (const auto &successor : successorPositions) {
47e119980fSAlex Zinenko     // Start from the second occurrence of a block in the successor list.
48e119980fSAlex Zinenko     for (int position : llvm::drop_begin(successor.second, 1)) {
49e119980fSAlex Zinenko       Block *dummyBlock = builder.createBlock(bb.getParent());
50e119980fSAlex Zinenko       terminator->setSuccessor(dummyBlock, position);
51*e084679fSRiver Riddle       for (BlockArgument arg : successor.first->getArguments())
52*e084679fSRiver Riddle         dummyBlock->addArgument(arg.getType(), arg.getLoc());
53e119980fSAlex Zinenko       builder.create<LLVM::BrOp>(terminator->getLoc(),
54e119980fSAlex Zinenko                                  dummyBlock->getArguments(), successor.first);
55e119980fSAlex Zinenko     }
56e119980fSAlex Zinenko   }
57e119980fSAlex Zinenko }
58e119980fSAlex Zinenko 
ensureDistinctSuccessors(Operation * op)59e119980fSAlex Zinenko void mlir::LLVM::ensureDistinctSuccessors(Operation *op) {
601ad48d6dSAlex Zinenko   op->walk([](Operation *nested) {
611ad48d6dSAlex Zinenko     for (Region &region : llvm::make_early_inc_range(nested->getRegions())) {
621ad48d6dSAlex Zinenko       for (Block &block : llvm::make_early_inc_range(region)) {
631ad48d6dSAlex Zinenko         ::ensureDistinctSuccessors(block);
641ad48d6dSAlex Zinenko       }
65e119980fSAlex Zinenko     }
66e119980fSAlex Zinenko   });
67e119980fSAlex Zinenko }
68e119980fSAlex Zinenko 
69e119980fSAlex Zinenko namespace {
7080aca1eaSRiver Riddle struct LegalizeForExportPass
711834ad4aSRiver Riddle     : public LLVMLegalizeForExportBase<LegalizeForExportPass> {
runOnOperation__anon4306a1f70211::LegalizeForExportPass72e119980fSAlex Zinenko   void runOnOperation() override {
73e119980fSAlex Zinenko     LLVM::ensureDistinctSuccessors(getOperation());
74e119980fSAlex Zinenko   }
75e119980fSAlex Zinenko };
76be0a7e9fSMehdi Amini } // namespace
77e119980fSAlex Zinenko 
createLegalizeForExportPass()78e119980fSAlex Zinenko std::unique_ptr<Pass> LLVM::createLegalizeForExportPass() {
79e119980fSAlex Zinenko   return std::make_unique<LegalizeForExportPass>();
80e119980fSAlex Zinenko }
81