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 | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 25 | #include "mlir/IR/Attributes.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 26 | #include "mlir/IR/CFGFunction.h" |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 27 | #include "mlir/IR/MLFunction.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 28 | #include "mlir/IR/Module.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 29 | #include "mlir/IR/OperationSet.h" |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 30 | #include "mlir/IR/Statements.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 31 | #include "mlir/IR/Types.h" |
| 32 | #include "mlir/Support/STLExtras.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 35 | using namespace mlir; |
| 36 | |
| 37 | |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 38 | void Identifier::print(raw_ostream &os) const { |
| 39 | os << str(); |
| 40 | } |
| 41 | |
| 42 | void Identifier::dump() const { |
| 43 | print(llvm::errs()); |
| 44 | } |
| 45 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Function printing |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | static void printFunctionSignature(const Function *fn, raw_ostream &os) { |
| 51 | auto type = fn->getType(); |
| 52 | |
| 53 | os << "@" << fn->getName() << '('; |
| 54 | interleave(type->getInputs(), |
| 55 | [&](Type *eltType) { os << *eltType; }, |
| 56 | [&]() { os << ", "; }); |
| 57 | os << ')'; |
| 58 | |
| 59 | switch (type->getResults().size()) { |
| 60 | case 0: break; |
| 61 | case 1: |
| 62 | os << " -> " << *type->getResults()[0]; |
| 63 | break; |
| 64 | default: |
| 65 | os << " -> ("; |
| 66 | interleave(type->getResults(), |
| 67 | [&](Type *eltType) { os << *eltType; }, |
| 68 | [&]() { os << ", "; }); |
| 69 | os << ')'; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void ExtFunction::print(raw_ostream &os) const { |
| 75 | os << "extfunc "; |
| 76 | printFunctionSignature(this, os); |
| 77 | os << "\n"; |
| 78 | } |
| 79 | |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 80 | namespace { |
| 81 | |
| 82 | // FunctionState contains common functionality for printing |
| 83 | // CFG and ML functions. |
| 84 | class FunctionState { |
| 85 | public: |
| 86 | FunctionState(MLIRContext *context, raw_ostream &os); |
| 87 | |
| 88 | void printOperation(const Operation *op); |
| 89 | |
| 90 | protected: |
| 91 | raw_ostream &os; |
| 92 | const OperationSet &operationSet; |
| 93 | }; |
| 94 | } // end anonymous namespace |
| 95 | |
| 96 | FunctionState::FunctionState(MLIRContext *context, raw_ostream &os) |
| 97 | : os(os), operationSet(OperationSet::get(context)) {} |
| 98 | |
| 99 | void FunctionState::printOperation(const Operation *op) { |
| 100 | // Check to see if this is a known operation. If so, use the registered |
| 101 | // custom printer hook. |
| 102 | if (auto opInfo = operationSet.lookup(op->getName().str())) { |
| 103 | os << " "; |
| 104 | opInfo->printAssembly(op, os); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // TODO: escape name if necessary. |
| 109 | os << " \"" << op->getName().str() << "\"()"; |
| 110 | |
| 111 | auto attrs = op->getAttrs(); |
| 112 | if (!attrs.empty()) { |
| 113 | os << '{'; |
| 114 | interleave( |
| 115 | attrs, |
| 116 | [&](NamedAttribute attr) { os << attr.first << ": " << *attr.second; }, |
| 117 | [&]() { os << ", "; }); |
| 118 | os << '}'; |
| 119 | } |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 122 | //===----------------------------------------------------------------------===// |
| 123 | // CFG Function printing |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | |
| 126 | namespace { |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 127 | class CFGFunctionState : public FunctionState { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 128 | public: |
| 129 | CFGFunctionState(const CFGFunction *function, raw_ostream &os); |
| 130 | |
| 131 | const CFGFunction *getFunction() const { return function; } |
| 132 | |
| 133 | void print(); |
| 134 | void print(const BasicBlock *block); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 135 | |
| 136 | void print(const Instruction *inst); |
| 137 | void print(const OperationInst *inst); |
| 138 | void print(const ReturnInst *inst); |
| 139 | void print(const BranchInst *inst); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 140 | |
| 141 | unsigned getBBID(const BasicBlock *block) { |
| 142 | auto it = basicBlockIDs.find(block); |
| 143 | assert(it != basicBlockIDs.end() && "Block not in this function?"); |
| 144 | return it->second; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | const CFGFunction *function; |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 149 | DenseMap<const BasicBlock*, unsigned> basicBlockIDs; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 150 | }; |
| 151 | } // end anonymous namespace |
| 152 | |
| 153 | CFGFunctionState::CFGFunctionState(const CFGFunction *function, raw_ostream &os) |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 154 | : FunctionState(function->getContext(), os), function(function) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 155 | // Each basic block gets a unique ID per function. |
| 156 | unsigned blockID = 0; |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 157 | for (auto &block : *function) |
| 158 | basicBlockIDs[&block] = blockID++; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void CFGFunctionState::print() { |
| 162 | os << "cfgfunc "; |
| 163 | printFunctionSignature(this->getFunction(), os); |
| 164 | os << " {\n"; |
| 165 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 166 | for (auto &block : *function) |
| 167 | print(&block); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 168 | os << "}\n\n"; |
| 169 | } |
| 170 | |
| 171 | void CFGFunctionState::print(const BasicBlock *block) { |
| 172 | os << "bb" << getBBID(block) << ":\n"; |
| 173 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 174 | // TODO Print arguments. |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 175 | for (auto &inst : block->getOperations()) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 176 | print(&inst); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 177 | os << "\n"; |
| 178 | } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 179 | |
| 180 | print(block->getTerminator()); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 181 | os << "\n"; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 184 | void CFGFunctionState::print(const Instruction *inst) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 185 | switch (inst->getKind()) { |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 186 | case Instruction::Kind::Operation: |
| 187 | return print(cast<OperationInst>(inst)); |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 188 | case TerminatorInst::Kind::Branch: |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 189 | return print(cast<BranchInst>(inst)); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 190 | case TerminatorInst::Kind::Return: |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 191 | return print(cast<ReturnInst>(inst)); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 195 | void CFGFunctionState::print(const OperationInst *inst) { |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 196 | printOperation(inst); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void CFGFunctionState::print(const BranchInst *inst) { |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 200 | os << " br bb" << getBBID(inst->getDest()); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 201 | } |
| 202 | void CFGFunctionState::print(const ReturnInst *inst) { |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 203 | os << " return"; |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 207 | // ML Function printing |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 208 | //===----------------------------------------------------------------------===// |
| 209 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 210 | namespace { |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 211 | class MLFunctionState : public FunctionState { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 212 | public: |
| 213 | MLFunctionState(const MLFunction *function, raw_ostream &os); |
| 214 | |
| 215 | const MLFunction *getFunction() const { return function; } |
| 216 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 217 | // Prints ML function |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 218 | void print(); |
| 219 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 220 | // Methods to print ML function statements |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 221 | void print(const Statement *stmt); |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 222 | void print(const OperationStmt *stmt); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 223 | void print(const ForStmt *stmt); |
| 224 | void print(const IfStmt *stmt); |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 225 | void print(const StmtBlock *block); |
| 226 | |
| 227 | // Number of spaces used for indenting nested statements |
| 228 | const static unsigned indentWidth = 2; |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 229 | |
| 230 | private: |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 231 | const MLFunction *function; |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 232 | int numSpaces; |
| 233 | }; |
| 234 | } // end anonymous namespace |
| 235 | |
| 236 | MLFunctionState::MLFunctionState(const MLFunction *function, raw_ostream &os) |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 237 | : FunctionState(function->getContext(), os), function(function), |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 238 | numSpaces(0) {} |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 239 | |
| 240 | void MLFunctionState::print() { |
| 241 | os << "mlfunc "; |
| 242 | // FIXME: should print argument names rather than just signature |
| 243 | printFunctionSignature(function, os); |
| 244 | os << " {\n"; |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 245 | print(function); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 246 | os << " return\n"; |
| 247 | os << "}\n\n"; |
| 248 | } |
| 249 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 250 | void MLFunctionState::print(const StmtBlock *block) { |
| 251 | numSpaces += indentWidth; |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 252 | for (auto &stmt : block->getStatements()) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 253 | print(&stmt); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 254 | os << "\n"; |
| 255 | } |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 256 | numSpaces -= indentWidth; |
| 257 | } |
| 258 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 259 | void MLFunctionState::print(const Statement *stmt) { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 260 | switch (stmt->getKind()) { |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame^] | 261 | case Statement::Kind::Operation: |
| 262 | return print(cast<OperationStmt>(stmt)); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 263 | case Statement::Kind::For: |
| 264 | return print(cast<ForStmt>(stmt)); |
| 265 | case Statement::Kind::If: |
| 266 | return print(cast<IfStmt>(stmt)); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 270 | void MLFunctionState::print(const OperationStmt *stmt) { |
| 271 | printOperation(stmt); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void MLFunctionState::print(const ForStmt *stmt) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 275 | os.indent(numSpaces) << "for {\n"; |
| 276 | print(static_cast<const StmtBlock *>(stmt)); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame^] | 277 | os.indent(numSpaces) << "}"; |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void MLFunctionState::print(const IfStmt *stmt) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 281 | os.indent(numSpaces) << "if () {\n"; |
| 282 | print(stmt->getThenClause()); |
| 283 | os.indent(numSpaces) << "}"; |
| 284 | if (stmt->hasElseClause()) { |
| 285 | os << " else {\n"; |
| 286 | print(stmt->getElseClause()); |
| 287 | os.indent(numSpaces) << "}"; |
| 288 | } |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 291 | //===----------------------------------------------------------------------===// |
| 292 | // print and dump methods |
| 293 | //===----------------------------------------------------------------------===// |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 294 | |
| 295 | void Instruction::print(raw_ostream &os) const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 296 | CFGFunctionState state(getFunction(), os); |
| 297 | state.print(this); |
| 298 | } |
| 299 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 300 | void Instruction::dump() const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 301 | print(llvm::errs()); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 302 | llvm::errs() << "\n"; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 303 | } |
| 304 | |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 305 | void AffineMap::dump() const { |
| 306 | print(llvm::errs()); |
| 307 | llvm::errs() << "\n"; |
| 308 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 309 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 310 | void AffineExpr::dump() const { |
| 311 | print(llvm::errs()); |
| 312 | llvm::errs() << "\n"; |
| 313 | } |
| 314 | |
| 315 | void AffineAddExpr::print(raw_ostream &os) const { |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 316 | os << "(" << *getLHS() << " + " << *getRHS() << ")"; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void AffineSubExpr::print(raw_ostream &os) const { |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 320 | os << "(" << *getLHS() << " - " << *getRHS() << ")"; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void AffineMulExpr::print(raw_ostream &os) const { |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 324 | os << "(" << *getLHS() << " * " << *getRHS() << ")"; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void AffineModExpr::print(raw_ostream &os) const { |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 328 | os << "(" << *getLHS() << " mod " << *getRHS() << ")"; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void AffineFloorDivExpr::print(raw_ostream &os) const { |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 332 | os << "(" << *getLHS() << " floordiv " << *getRHS() << ")"; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void AffineCeilDivExpr::print(raw_ostream &os) const { |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 336 | os << "(" << *getLHS() << " ceildiv " << *getRHS() << ")"; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void AffineSymbolExpr::print(raw_ostream &os) const { |
| 340 | os << "s" << getPosition(); |
| 341 | } |
| 342 | |
| 343 | void AffineDimExpr::print(raw_ostream &os) const { os << "d" << getPosition(); } |
| 344 | |
| 345 | void AffineConstantExpr::print(raw_ostream &os) const { os << getValue(); } |
| 346 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 347 | void AffineExpr::print(raw_ostream &os) const { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 348 | switch (getKind()) { |
| 349 | case Kind::SymbolId: |
| 350 | return cast<AffineSymbolExpr>(this)->print(os); |
| 351 | case Kind::DimId: |
| 352 | return cast<AffineDimExpr>(this)->print(os); |
| 353 | case Kind::Constant: |
| 354 | return cast<AffineConstantExpr>(this)->print(os); |
| 355 | case Kind::Add: |
| 356 | return cast<AffineAddExpr>(this)->print(os); |
| 357 | case Kind::Sub: |
| 358 | return cast<AffineSubExpr>(this)->print(os); |
| 359 | case Kind::Mul: |
| 360 | return cast<AffineMulExpr>(this)->print(os); |
| 361 | case Kind::FloorDiv: |
| 362 | return cast<AffineFloorDivExpr>(this)->print(os); |
| 363 | case Kind::CeilDiv: |
| 364 | return cast<AffineCeilDivExpr>(this)->print(os); |
| 365 | case Kind::Mod: |
| 366 | return cast<AffineModExpr>(this)->print(os); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 367 | } |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void AffineMap::print(raw_ostream &os) const { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 371 | // Dimension identifiers. |
| 372 | os << "("; |
| 373 | for (int i = 0; i < (int)getNumDims() - 1; i++) |
| 374 | os << "d" << i << ", "; |
| 375 | if (getNumDims() >= 1) |
| 376 | os << "d" << getNumDims() - 1; |
| 377 | os << ")"; |
| 378 | |
| 379 | // Symbolic identifiers. |
| 380 | if (getNumSymbols() >= 1) { |
| 381 | os << " ["; |
| 382 | for (int i = 0; i < (int)getNumSymbols() - 1; i++) |
| 383 | os << "s" << i << ", "; |
| 384 | if (getNumSymbols() >= 1) |
| 385 | os << "s" << getNumSymbols() - 1; |
| 386 | os << "]"; |
| 387 | } |
| 388 | |
| 389 | // AffineMap should have at least one result. |
| 390 | assert(!getResults().empty()); |
| 391 | // Result affine expressions. |
| 392 | os << " -> ("; |
| 393 | interleave(getResults(), [&](AffineExpr *expr) { os << *expr; }, |
| 394 | [&]() { os << ", "; }); |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 395 | os << ")"; |
| 396 | |
| 397 | if (!isBounded()) { |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | |
| 401 | // Print range sizes for bounded affine maps. |
| 402 | os << " size ("; |
| 403 | interleave(getRangeSizes(), [&](AffineExpr *expr) { os << *expr; }, |
| 404 | [&]() { os << ", "; }); |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 405 | os << ")"; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 408 | void BasicBlock::print(raw_ostream &os) const { |
| 409 | CFGFunctionState state(getFunction(), os); |
| 410 | state.print(); |
| 411 | } |
| 412 | |
| 413 | void BasicBlock::dump() const { |
| 414 | print(llvm::errs()); |
| 415 | } |
| 416 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 417 | void Statement::print(raw_ostream &os) const { |
| 418 | MLFunctionState state(getFunction(), os); |
| 419 | state.print(this); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 422 | void Statement::dump() const { |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 423 | print(llvm::errs()); |
| 424 | } |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 425 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 426 | void Function::print(raw_ostream &os) const { |
| 427 | switch (getKind()) { |
| 428 | case Kind::ExtFunc: return cast<ExtFunction>(this)->print(os); |
| 429 | case Kind::CFGFunc: return cast<CFGFunction>(this)->print(os); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 430 | case Kind::MLFunc: return cast<MLFunction>(this)->print(os); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | void Function::dump() const { |
| 435 | print(llvm::errs()); |
| 436 | } |
| 437 | |
| 438 | void CFGFunction::print(raw_ostream &os) const { |
| 439 | CFGFunctionState state(this, os); |
| 440 | state.print(); |
| 441 | } |
| 442 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 443 | void MLFunction::print(raw_ostream &os) const { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 444 | MLFunctionState state(this, os); |
| 445 | state.print(); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 448 | void Module::print(raw_ostream &os) const { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 449 | unsigned id = 0; |
| 450 | for (auto *map : affineMapList) { |
| 451 | os << "#" << id++ << " = "; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 452 | map->print(os); |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 453 | os << '\n'; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 454 | } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 455 | for (auto *fn : functionList) |
| 456 | fn->print(os); |
| 457 | } |
| 458 | |
| 459 | void Module::dump() const { |
| 460 | print(llvm::errs()); |
| 461 | } |