Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 1 | //===- AsmPrinter.cpp - MLIR Assembly Printer Implementation --------------===// |
| 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 the MLIR AsmPrinter class, which is used to implement |
| 19 | // the various print() methods on the core IR objects. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 23 | #include "mlir/IR/AffineExpr.h" |
| 24 | #include "mlir/IR/AffineMap.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 25 | #include "mlir/IR/CFGFunction.h" |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 26 | #include "mlir/IR/MLFunction.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 27 | #include "mlir/IR/Module.h" |
| 28 | #include "mlir/IR/Types.h" |
| 29 | #include "mlir/Support/STLExtras.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 30 | #include "llvm/ADT/DenseMap.h" |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 32 | using namespace mlir; |
| 33 | |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Function printing |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | static void printFunctionSignature(const Function *fn, raw_ostream &os) { |
| 40 | auto type = fn->getType(); |
| 41 | |
| 42 | os << "@" << fn->getName() << '('; |
| 43 | interleave(type->getInputs(), |
| 44 | [&](Type *eltType) { os << *eltType; }, |
| 45 | [&]() { os << ", "; }); |
| 46 | os << ')'; |
| 47 | |
| 48 | switch (type->getResults().size()) { |
| 49 | case 0: break; |
| 50 | case 1: |
| 51 | os << " -> " << *type->getResults()[0]; |
| 52 | break; |
| 53 | default: |
| 54 | os << " -> ("; |
| 55 | interleave(type->getResults(), |
| 56 | [&](Type *eltType) { os << *eltType; }, |
| 57 | [&]() { os << ", "; }); |
| 58 | os << ')'; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void ExtFunction::print(raw_ostream &os) const { |
| 64 | os << "extfunc "; |
| 65 | printFunctionSignature(this, os); |
| 66 | os << "\n"; |
| 67 | } |
| 68 | |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // CFG Function printing |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
| 73 | namespace { |
| 74 | class CFGFunctionState { |
| 75 | public: |
| 76 | CFGFunctionState(const CFGFunction *function, raw_ostream &os); |
| 77 | |
| 78 | const CFGFunction *getFunction() const { return function; } |
| 79 | |
| 80 | void print(); |
| 81 | void print(const BasicBlock *block); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 82 | |
| 83 | void print(const Instruction *inst); |
| 84 | void print(const OperationInst *inst); |
| 85 | void print(const ReturnInst *inst); |
| 86 | void print(const BranchInst *inst); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 87 | |
| 88 | unsigned getBBID(const BasicBlock *block) { |
| 89 | auto it = basicBlockIDs.find(block); |
| 90 | assert(it != basicBlockIDs.end() && "Block not in this function?"); |
| 91 | return it->second; |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | const CFGFunction *function; |
| 96 | raw_ostream &os; |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 97 | DenseMap<const BasicBlock*, unsigned> basicBlockIDs; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 98 | }; |
| 99 | } // end anonymous namespace |
| 100 | |
| 101 | CFGFunctionState::CFGFunctionState(const CFGFunction *function, raw_ostream &os) |
| 102 | : function(function), os(os) { |
| 103 | |
| 104 | // Each basic block gets a unique ID per function. |
| 105 | unsigned blockID = 0; |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 106 | for (auto &block : *function) |
| 107 | basicBlockIDs[&block] = blockID++; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void CFGFunctionState::print() { |
| 111 | os << "cfgfunc "; |
| 112 | printFunctionSignature(this->getFunction(), os); |
| 113 | os << " {\n"; |
| 114 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 115 | for (auto &block : *function) |
| 116 | print(&block); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 117 | os << "}\n\n"; |
| 118 | } |
| 119 | |
| 120 | void CFGFunctionState::print(const BasicBlock *block) { |
| 121 | os << "bb" << getBBID(block) << ":\n"; |
| 122 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 123 | // TODO Print arguments. |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 124 | for (auto &inst : block->getOperations()) |
| 125 | print(&inst); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 126 | |
| 127 | print(block->getTerminator()); |
| 128 | } |
| 129 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 130 | void CFGFunctionState::print(const Instruction *inst) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 131 | switch (inst->getKind()) { |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 132 | case Instruction::Kind::Operation: |
| 133 | return print(cast<OperationInst>(inst)); |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 134 | case TerminatorInst::Kind::Branch: |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 135 | return print(cast<BranchInst>(inst)); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 136 | case TerminatorInst::Kind::Return: |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 137 | return print(cast<ReturnInst>(inst)); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 141 | void CFGFunctionState::print(const OperationInst *inst) { |
| 142 | // TODO: escape name if necessary. |
| 143 | os << " \"" << inst->getName().str() << "\"()\n"; |
| 144 | } |
| 145 | |
| 146 | void CFGFunctionState::print(const BranchInst *inst) { |
| 147 | os << " br bb" << getBBID(inst->getDest()) << "\n"; |
| 148 | } |
| 149 | void CFGFunctionState::print(const ReturnInst *inst) { |
| 150 | os << " return\n"; |
| 151 | } |
| 152 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | // print and dump methods |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 157 | |
| 158 | void Instruction::print(raw_ostream &os) const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 159 | CFGFunctionState state(getFunction(), os); |
| 160 | state.print(this); |
| 161 | } |
| 162 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 163 | void Instruction::dump() const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 164 | print(llvm::errs()); |
| 165 | } |
| 166 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 167 | void AffineExpr::print(raw_ostream &os) const { |
| 168 | // TODO(bondhugula): print out affine expression |
| 169 | } |
| 170 | |
| 171 | void AffineMap::print(raw_ostream &os) const { |
| 172 | // TODO(andydavis) Print out affine map based on dimensionCount and |
| 173 | // symbolCount: (d0, d1) [S0, S1] -> (d0 + S0, d1 + S1) |
| 174 | } |
| 175 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 176 | void BasicBlock::print(raw_ostream &os) const { |
| 177 | CFGFunctionState state(getFunction(), os); |
| 178 | state.print(); |
| 179 | } |
| 180 | |
| 181 | void BasicBlock::dump() const { |
| 182 | print(llvm::errs()); |
| 183 | } |
| 184 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 185 | void MLStatement::print(raw_ostream &os) const { |
| 186 | //TODO |
| 187 | } |
| 188 | |
| 189 | void MLStatement::dump() const { |
| 190 | print(llvm::errs()); |
| 191 | } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 192 | void Function::print(raw_ostream &os) const { |
| 193 | switch (getKind()) { |
| 194 | case Kind::ExtFunc: return cast<ExtFunction>(this)->print(os); |
| 195 | case Kind::CFGFunc: return cast<CFGFunction>(this)->print(os); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 196 | case Kind::MLFunc: return cast<MLFunction>(this)->print(os); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | void Function::dump() const { |
| 201 | print(llvm::errs()); |
| 202 | } |
| 203 | |
| 204 | void CFGFunction::print(raw_ostream &os) const { |
| 205 | CFGFunctionState state(this, os); |
| 206 | state.print(); |
| 207 | } |
| 208 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 209 | void MLFunction::print(raw_ostream &os) const { |
| 210 | os << "mlfunc "; |
| 211 | // FIXME: should print argument names rather than just signature |
| 212 | printFunctionSignature(this, os); |
| 213 | os << " {\n"; |
| 214 | |
| 215 | for (auto *stmt : stmtList) |
| 216 | stmt->print(os); |
| 217 | os << " return\n"; |
| 218 | os << "}\n\n"; |
| 219 | } |
| 220 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 221 | void Module::print(raw_ostream &os) const { |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 222 | for (auto *map : affineMapList) |
| 223 | map->print(os); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 224 | for (auto *fn : functionList) |
| 225 | fn->print(os); |
| 226 | } |
| 227 | |
| 228 | void Module::dump() const { |
| 229 | print(llvm::errs()); |
| 230 | } |
| 231 | |