Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 1 | //===- ConvertToCFG.cpp - ML function to CFG function converstion ---------===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | // |
| 18 | // This file implements APIs to convert ML functions into CFG functions. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 22 | #include "mlir/IR/Builders.h" |
| 23 | #include "mlir/IR/CFGFunction.h" |
| 24 | #include "mlir/IR/MLFunction.h" |
| 25 | #include "mlir/IR/Module.h" |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 26 | #include "mlir/IR/Pass.h" |
| 27 | #include "mlir/Transforms/Passes.h" |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | using namespace mlir; |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // ML function converter |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace { |
| 36 | // Generates CFG function equivalent to the given ML function. |
| 37 | class FunctionConverter { |
| 38 | public: |
| 39 | FunctionConverter(CFGFunction *cfgFunc) |
| 40 | : cfgFunc(cfgFunc), builder(cfgFunc) {} |
| 41 | CFGFunction *convert(const MLFunction *mlFunc); |
| 42 | |
| 43 | private: |
| 44 | CFGFunction *cfgFunc; |
| 45 | CFGFuncBuilder builder; |
| 46 | }; |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 47 | } // end anonymous namespace |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 48 | |
| 49 | CFGFunction *FunctionConverter::convert(const MLFunction *mlFunc) { |
| 50 | builder.createBlock(); |
| 51 | |
| 52 | // Creates return instruction with no operands. |
| 53 | // TODO: convert return operands. |
| 54 | builder.createReturnInst({}); |
| 55 | |
| 56 | // TODO: convert ML function body. |
| 57 | |
| 58 | return cfgFunc; |
| 59 | } |
| 60 | |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | // Module converter |
| 63 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 64 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 65 | namespace { |
| 66 | // ModuleConverter class does CFG conversion for the whole module. |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 67 | class ModuleConverter : public ModulePass { |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 68 | public: |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 69 | explicit ModuleConverter() {} |
| 70 | |
| 71 | void runOnModule(Module *m) override; |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | // Generates CFG functions for all ML functions in the module. |
| 75 | void convertMLFunctions(); |
| 76 | // Generates CFG function for the given ML function. |
| 77 | CFGFunction *convert(const MLFunction *mlFunc); |
| 78 | // Replaces all ML function references in the module |
| 79 | // with references to the generated CFG functions. |
| 80 | void replaceReferences(); |
| 81 | // Replaces function references in the given function. |
| 82 | void replaceReferences(CFGFunction *cfgFunc); |
| 83 | void replaceReferences(MLFunction *mlFunc); |
| 84 | // Removes all ML funtions from the module. |
| 85 | void removeMLFunctions(); |
| 86 | |
| 87 | // Map from ML functions to generated CFG functions. |
| 88 | llvm::DenseMap<const MLFunction *, CFGFunction *> generatedFuncs; |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 89 | Module *module = nullptr; |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 90 | }; |
| 91 | } // end anonymous namespace |
| 92 | |
| 93 | // Iterates over all functions in the module generating CFG functions |
| 94 | // equivalent to ML functions and replacing references to ML functions |
| 95 | // with references to the generated ML functions. |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 96 | void ModuleConverter::runOnModule(Module *m) { |
| 97 | module = m; |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 98 | convertMLFunctions(); |
| 99 | replaceReferences(); |
| 100 | } |
| 101 | |
| 102 | void ModuleConverter::convertMLFunctions() { |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 103 | for (Function &fn : *module) { |
| 104 | if (auto *mlFunc = dyn_cast<MLFunction>(&fn)) |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 105 | generatedFuncs[mlFunc] = convert(mlFunc); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Creates CFG function equivalent to the given ML function. |
| 110 | CFGFunction *ModuleConverter::convert(const MLFunction *mlFunc) { |
| 111 | // TODO: ensure that CFG function name is unique. |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 112 | auto *cfgFunc = |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 113 | new CFGFunction(mlFunc->getName() + "_cfg", mlFunc->getType()); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 114 | module->getFunctions().push_back(cfgFunc); |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 115 | |
| 116 | // Generates the body of the CFG function. |
| 117 | return FunctionConverter(cfgFunc).convert(mlFunc); |
| 118 | } |
| 119 | |
| 120 | void ModuleConverter::replaceReferences() { |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 121 | for (Function &fn : *module) { |
| 122 | switch (fn.getKind()) { |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 123 | case Function::Kind::CFGFunc: |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 124 | replaceReferences(&cast<CFGFunction>(fn)); |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 125 | break; |
| 126 | case Function::Kind::MLFunc: |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 127 | replaceReferences(&cast<MLFunction>(fn)); |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 128 | break; |
| 129 | case Function::Kind::ExtFunc: |
| 130 | // nothing to do for external functions |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void ModuleConverter::replaceReferences(CFGFunction *func) { |
| 137 | // TODO: NOP for now since function attributes are not yet implemented. |
| 138 | } |
| 139 | |
| 140 | void ModuleConverter::replaceReferences(MLFunction *func) { |
| 141 | // TODO: NOP for now since function attributes are not yet implemented. |
| 142 | } |
| 143 | |
| 144 | // Removes all ML functions from the module. |
| 145 | void ModuleConverter::removeMLFunctions() { |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 146 | // Delete ML functions from the module. |
| 147 | for (auto it = module->begin(), e = module->end(); it != e;) { |
| 148 | // Manipulate iterator carefully to avoid deleting a function we're pointing |
| 149 | // at. |
| 150 | Function &fn = *it++; |
| 151 | if (auto mlFunc = dyn_cast<MLFunction>(&fn)) |
| 152 | mlFunc->eraseFromModule(); |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 153 | } |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | // Entry point method |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 160 | /// Replaces all ML functions in the module with equivalent CFG functions. |
| 161 | /// Function references are appropriately patched to refer to the newly |
| 162 | /// generated CFG functions. |
| 163 | ModulePass *mlir::createConvertToCFGPass() { return new ModuleConverter(); } |