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 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame^] | 154 | // ML Function printing |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 155 | //===----------------------------------------------------------------------===// |
| 156 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame^] | 157 | namespace { |
| 158 | class MLFunctionState { |
| 159 | public: |
| 160 | MLFunctionState(const MLFunction *function, raw_ostream &os); |
| 161 | |
| 162 | const MLFunction *getFunction() const { return function; } |
| 163 | |
| 164 | void print(); |
| 165 | |
| 166 | void print(const Statement *stmt); |
| 167 | void print(const ForStmt *stmt); |
| 168 | void print(const IfStmt *stmt); |
| 169 | void print(const ElseClause *stmt, bool isLast); |
| 170 | |
| 171 | private: |
| 172 | // Print statements nested within this node statement. |
| 173 | void printNestedStatements(const NodeStmt *stmt); |
| 174 | |
| 175 | const MLFunction *function; |
| 176 | raw_ostream &os; |
| 177 | int numSpaces; |
| 178 | }; |
| 179 | } // end anonymous namespace |
| 180 | |
| 181 | MLFunctionState::MLFunctionState(const MLFunction *function, raw_ostream &os) |
| 182 | : function(function), os(os), numSpaces(2) {} |
| 183 | |
| 184 | void MLFunctionState::print() { |
| 185 | os << "mlfunc "; |
| 186 | // FIXME: should print argument names rather than just signature |
| 187 | printFunctionSignature(function, os); |
| 188 | os << " {\n"; |
| 189 | for (auto *stmt : function->stmtList) |
| 190 | print(stmt); |
| 191 | os << " return\n"; |
| 192 | os << "}\n\n"; |
| 193 | } |
| 194 | |
| 195 | void MLFunctionState::print(const Statement *stmt) { |
| 196 | os.indent(numSpaces); |
| 197 | switch (stmt->getKind()) { |
| 198 | case Statement::Kind::Operation: // TODO |
| 199 | assert(0 && "Operation statement is not yet implemented"); |
| 200 | case Statement::Kind::For: |
| 201 | return print(cast<ForStmt>(stmt)); |
| 202 | case Statement::Kind::If: |
| 203 | return print(cast<IfStmt>(stmt)); |
| 204 | case Statement::Kind::Else: |
| 205 | return print(cast<ElseClause>(stmt)); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void MLFunctionState::printNestedStatements(const NodeStmt *stmt) { |
| 210 | os << "{\n"; |
| 211 | numSpaces += 2; |
| 212 | for (auto * nestedStmt : stmt->children) |
| 213 | print(nestedStmt); |
| 214 | numSpaces -= 2; |
| 215 | os.indent(numSpaces) << "}"; |
| 216 | } |
| 217 | |
| 218 | void MLFunctionState::print(const ForStmt *stmt) { |
| 219 | os << "for "; |
| 220 | printNestedStatements(stmt); |
| 221 | os << "\n"; |
| 222 | } |
| 223 | |
| 224 | void MLFunctionState::print(const IfStmt *stmt) { |
| 225 | os << "if "; |
| 226 | printNestedStatements(stmt); |
| 227 | |
| 228 | int numClauses = stmt->elseClauses.size(); |
| 229 | for (auto e : stmt->elseClauses) |
| 230 | print(e, e->getClauseNumber() == numClauses - 1); |
| 231 | os << "\n"; |
| 232 | } |
| 233 | |
| 234 | void MLFunctionState::print(const ElseClause *stmt, bool isLast) { |
| 235 | if (!isLast) |
| 236 | os << " if"; |
| 237 | os << " else ";; |
| 238 | printNestedStatements(stmt); |
| 239 | } |
| 240 | |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | // print and dump methods |
| 243 | //===----------------------------------------------------------------------===// |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 244 | |
| 245 | void Instruction::print(raw_ostream &os) const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 246 | CFGFunctionState state(getFunction(), os); |
| 247 | state.print(this); |
| 248 | } |
| 249 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 250 | void Instruction::dump() const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 251 | print(llvm::errs()); |
| 252 | } |
| 253 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 254 | void AffineExpr::print(raw_ostream &os) const { |
| 255 | // TODO(bondhugula): print out affine expression |
| 256 | } |
| 257 | |
| 258 | void AffineMap::print(raw_ostream &os) const { |
| 259 | // TODO(andydavis) Print out affine map based on dimensionCount and |
| 260 | // symbolCount: (d0, d1) [S0, S1] -> (d0 + S0, d1 + S1) |
| 261 | } |
| 262 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 263 | void BasicBlock::print(raw_ostream &os) const { |
| 264 | CFGFunctionState state(getFunction(), os); |
| 265 | state.print(); |
| 266 | } |
| 267 | |
| 268 | void BasicBlock::dump() const { |
| 269 | print(llvm::errs()); |
| 270 | } |
| 271 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame^] | 272 | void Statement::print(raw_ostream &os) const { |
| 273 | MLFunctionState state(getFunction(), os); |
| 274 | state.print(this); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame^] | 277 | void Statement::dump() const { |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 278 | print(llvm::errs()); |
| 279 | } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 280 | void Function::print(raw_ostream &os) const { |
| 281 | switch (getKind()) { |
| 282 | case Kind::ExtFunc: return cast<ExtFunction>(this)->print(os); |
| 283 | case Kind::CFGFunc: return cast<CFGFunction>(this)->print(os); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 284 | case Kind::MLFunc: return cast<MLFunction>(this)->print(os); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | void Function::dump() const { |
| 289 | print(llvm::errs()); |
| 290 | } |
| 291 | |
| 292 | void CFGFunction::print(raw_ostream &os) const { |
| 293 | CFGFunctionState state(this, os); |
| 294 | state.print(); |
| 295 | } |
| 296 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 297 | void MLFunction::print(raw_ostream &os) const { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame^] | 298 | MLFunctionState state(this, os); |
| 299 | state.print(); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 302 | void Module::print(raw_ostream &os) const { |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 303 | for (auto *map : affineMapList) |
| 304 | map->print(os); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 305 | for (auto *fn : functionList) |
| 306 | fn->print(os); |
| 307 | } |
| 308 | |
| 309 | void Module::dump() const { |
| 310 | print(llvm::errs()); |
| 311 | } |
| 312 | |