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 
9 #include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h"
10 #include "PassDetail.h"
11 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
12 #include "mlir/IR/Block.h"
13 #include "mlir/IR/Builders.h"
14 #include "mlir/IR/BuiltinOps.h"
15 
16 using namespace mlir;
17 
ensureDistinctSuccessors(Block & bb)18 static void ensureDistinctSuccessors(Block &bb) {
19   auto *terminator = bb.getTerminator();
20 
21   // Find repeated successors with arguments.
22   llvm::SmallDenseMap<Block *, SmallVector<int, 4>> successorPositions;
23   for (int i = 0, e = terminator->getNumSuccessors(); i < e; ++i) {
24     Block *successor = terminator->getSuccessor(i);
25     // Blocks with no arguments are safe even if they appear multiple times
26     // because they don't need PHI nodes.
27     if (successor->getNumArguments() == 0)
28       continue;
29     successorPositions[successor].push_back(i);
30   }
31 
32   // If a successor appears for the second or more time in the terminator,
33   // create a new dummy block that unconditionally branches to the original
34   // destination, and retarget the terminator to branch to this new block.
35   // There is no need to pass arguments to the dummy block because it will be
36   // dominated by the original block and can therefore use any values defined in
37   // the original block.
38   OpBuilder builder(terminator->getContext());
39   for (const auto &successor : successorPositions) {
40     // Start from the second occurrence of a block in the successor list.
41     for (int position : llvm::drop_begin(successor.second, 1)) {
42       Block *dummyBlock = builder.createBlock(bb.getParent());
43       terminator->setSuccessor(dummyBlock, position);
44       dummyBlock->addArguments(successor.first->getArgumentTypes());
45       builder.create<LLVM::BrOp>(terminator->getLoc(),
46                                  dummyBlock->getArguments(), successor.first);
47     }
48   }
49 }
50 
ensureDistinctSuccessors(Operation * op)51 void mlir::LLVM::ensureDistinctSuccessors(Operation *op) {
52   op->walk([](LLVMFuncOp f) {
53     for (auto &bb : f) {
54       ::ensureDistinctSuccessors(bb);
55     }
56   });
57 }
58 
59 namespace {
60 struct LegalizeForExportPass
61     : public LLVMLegalizeForExportBase<LegalizeForExportPass> {
runOnOperation__anona2ba9db70211::LegalizeForExportPass62   void runOnOperation() override {
63     LLVM::ensureDistinctSuccessors(getOperation());
64   }
65 };
66 } // end namespace
67 
createLegalizeForExportPass()68 std::unique_ptr<Pass> LLVM::createLegalizeForExportPass() {
69   return std::make_unique<LegalizeForExportPass>();
70 }
71