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 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 37 | void Identifier::print(raw_ostream &os) const { os << str(); } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 38 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 39 | void Identifier::dump() const { print(llvm::errs()); } |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 40 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 42 | // ModuleState |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | namespace { |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 46 | class ModuleState { |
| 47 | public: |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 48 | /// This is the operation set for the current context if it is knowable (a |
| 49 | /// context could be determined), otherwise this is null. |
| 50 | OperationSet *const operationSet; |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 51 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 52 | explicit ModuleState(MLIRContext *context) |
| 53 | : operationSet(context ? &OperationSet::get(context) : nullptr) {} |
| 54 | |
| 55 | // Initializes module state, populating affine map state. |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 56 | void initialize(const Module *module); |
| 57 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 58 | int getAffineMapId(const AffineMap *affineMap) const { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 59 | auto it = affineMapIds.find(affineMap); |
| 60 | if (it == affineMapIds.end()) { |
| 61 | return -1; |
| 62 | } |
| 63 | return it->second; |
| 64 | } |
| 65 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 66 | const DenseMap<const AffineMap *, int> &getAffineMapIds() const { |
| 67 | return affineMapIds; |
| 68 | } |
| 69 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 70 | private: |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 71 | void recordAffineMapReference(const AffineMap *affineMap) { |
| 72 | if (affineMapIds.count(affineMap) == 0) { |
| 73 | affineMapIds[affineMap] = nextAffineMapId++; |
| 74 | } |
| 75 | } |
| 76 | |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 77 | // Visit functions. |
| 78 | void visitFunction(const Function *fn); |
| 79 | void visitExtFunction(const ExtFunction *fn); |
| 80 | void visitCFGFunction(const CFGFunction *fn); |
| 81 | void visitMLFunction(const MLFunction *fn); |
| 82 | void visitType(const Type *type); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 83 | void visitAttribute(const Attribute *attr); |
| 84 | void visitOperation(const Operation *op); |
| 85 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 86 | DenseMap<const AffineMap *, int> affineMapIds; |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 87 | int nextAffineMapId = 0; |
| 88 | }; |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 89 | } // end anonymous namespace |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 90 | |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 91 | |
| 92 | // TODO Support visiting other types/instructions when implemented. |
| 93 | void ModuleState::visitType(const Type *type) { |
| 94 | if (type->getKind() == Type::Kind::Function) { |
| 95 | // Visit input and result types for functions. |
| 96 | auto *funcType = cast<FunctionType>(type); |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 97 | for (auto *input : funcType->getInputs()) { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 98 | visitType(input); |
| 99 | } |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 100 | for (auto *result : funcType->getResults()) { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 101 | visitType(result); |
| 102 | } |
| 103 | } else if (type->getKind() == Type::Kind::MemRef) { |
| 104 | // Visit affine maps in memref type. |
| 105 | auto *memref = cast<MemRefType>(type); |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 106 | for (AffineMap *map : memref->getAffineMaps()) { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 107 | recordAffineMapReference(map); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 112 | void ModuleState::visitAttribute(const Attribute *attr) { |
| 113 | if (isa<AffineMapAttr>(attr)) { |
| 114 | recordAffineMapReference(cast<AffineMapAttr>(attr)->getValue()); |
| 115 | } else if (isa<ArrayAttr>(attr)) { |
| 116 | for (auto elt : cast<ArrayAttr>(attr)->getValue()) { |
| 117 | visitAttribute(elt); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void ModuleState::visitOperation(const Operation *op) { |
| 123 | for (auto elt : op->getAttrs()) { |
| 124 | visitAttribute(elt.second); |
| 125 | } |
| 126 | } |
| 127 | |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 128 | void ModuleState::visitExtFunction(const ExtFunction *fn) { |
| 129 | visitType(fn->getType()); |
| 130 | } |
| 131 | |
| 132 | void ModuleState::visitCFGFunction(const CFGFunction *fn) { |
| 133 | visitType(fn->getType()); |
| 134 | // TODO Visit function body instructions. |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 135 | for (auto &block : *fn) { |
| 136 | for (auto &op : block.getOperations()) { |
| 137 | visitOperation(&op); |
| 138 | } |
| 139 | } |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void ModuleState::visitMLFunction(const MLFunction *fn) { |
| 143 | visitType(fn->getType()); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 144 | // TODO Visit function body statements (and attributes if required). |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void ModuleState::visitFunction(const Function *fn) { |
| 148 | switch (fn->getKind()) { |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 149 | case Function::Kind::ExtFunc: |
| 150 | return visitExtFunction(cast<ExtFunction>(fn)); |
| 151 | case Function::Kind::CFGFunc: |
| 152 | return visitCFGFunction(cast<CFGFunction>(fn)); |
| 153 | case Function::Kind::MLFunc: |
| 154 | return visitMLFunction(cast<MLFunction>(fn)); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 158 | // Initializes module state, populating affine map state. |
| 159 | void ModuleState::initialize(const Module *module) { |
| 160 | for (auto fn : module->functionList) { |
| 161 | visitFunction(fn); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | // ModulePrinter |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | |
| 169 | namespace { |
| 170 | class ModulePrinter { |
| 171 | public: |
| 172 | ModulePrinter(raw_ostream &os, ModuleState &state) : os(os), state(state) {} |
| 173 | explicit ModulePrinter(const ModulePrinter &printer) |
| 174 | : os(printer.os), state(printer.state) {} |
| 175 | |
| 176 | template <typename Container, typename UnaryFunctor> |
| 177 | inline void interleaveComma(const Container &c, UnaryFunctor each_fn) const { |
| 178 | interleave(c.begin(), c.end(), each_fn, [&]() { os << ", "; }); |
| 179 | } |
| 180 | |
| 181 | void print(const Module *module); |
| 182 | void print(const Attribute *attr) const; |
| 183 | void print(const Type *type) const; |
| 184 | void print(const Function *fn); |
| 185 | void print(const ExtFunction *fn); |
| 186 | void print(const CFGFunction *fn); |
| 187 | void print(const MLFunction *fn); |
| 188 | |
| 189 | void print(const AffineMap *map); |
| 190 | void print(const AffineExpr *expr) const; |
| 191 | |
| 192 | protected: |
| 193 | raw_ostream &os; |
| 194 | ModuleState &state; |
| 195 | |
| 196 | void printFunctionSignature(const Function *fn); |
| 197 | void printAffineMapId(int affineMapId) const; |
| 198 | void printAffineMapReference(const AffineMap *affineMap) const; |
| 199 | |
| 200 | void print(const AffineBinaryOpExpr *expr) const; |
| 201 | }; |
| 202 | } // end anonymous namespace |
| 203 | |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 204 | // Prints function with initialized module state. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 205 | void ModulePrinter::print(const Function *fn) { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 206 | switch (fn->getKind()) { |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 207 | case Function::Kind::ExtFunc: |
| 208 | return print(cast<ExtFunction>(fn)); |
| 209 | case Function::Kind::CFGFunc: |
| 210 | return print(cast<CFGFunction>(fn)); |
| 211 | case Function::Kind::MLFunc: |
| 212 | return print(cast<MLFunction>(fn)); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | // Prints affine map identifier. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 217 | void ModulePrinter::printAffineMapId(int affineMapId) const { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 218 | os << "#map" << affineMapId; |
| 219 | } |
| 220 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 221 | void ModulePrinter::printAffineMapReference(const AffineMap *affineMap) const { |
| 222 | int mapId = state.getAffineMapId(affineMap); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 223 | if (mapId >= 0) { |
| 224 | // Map will be printed at top of module so print reference to its id. |
| 225 | printAffineMapId(mapId); |
| 226 | } else { |
| 227 | // Map not in module state so print inline. |
| 228 | affineMap->print(os); |
| 229 | } |
| 230 | } |
| 231 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 232 | void ModulePrinter::print(const Module *module) { |
| 233 | for (const auto &mapAndId : state.getAffineMapIds()) { |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 234 | printAffineMapId(mapAndId.second); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 235 | os << " = "; |
| 236 | mapAndId.first->print(os); |
| 237 | os << '\n'; |
| 238 | } |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 239 | for (auto *fn : module->functionList) print(fn); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 242 | void ModulePrinter::print(const Attribute *attr) const { |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 243 | switch (attr->getKind()) { |
| 244 | case Attribute::Kind::Bool: |
| 245 | os << (cast<BoolAttr>(attr)->getValue() ? "true" : "false"); |
| 246 | break; |
| 247 | case Attribute::Kind::Integer: |
| 248 | os << cast<IntegerAttr>(attr)->getValue(); |
| 249 | break; |
| 250 | case Attribute::Kind::Float: |
| 251 | // FIXME: this isn't precise, we should print with a hex format. |
| 252 | os << cast<FloatAttr>(attr)->getValue(); |
| 253 | break; |
| 254 | case Attribute::Kind::String: |
| 255 | // FIXME: should escape the string. |
| 256 | os << '"' << cast<StringAttr>(attr)->getValue() << '"'; |
| 257 | break; |
| 258 | case Attribute::Kind::Array: { |
| 259 | auto elts = cast<ArrayAttr>(attr)->getValue(); |
| 260 | os << '['; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 261 | interleaveComma(elts, [&](Attribute *attr) { print(attr); }); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 262 | os << ']'; |
| 263 | break; |
| 264 | } |
| 265 | case Attribute::Kind::AffineMap: |
| 266 | printAffineMapReference(cast<AffineMapAttr>(attr)->getValue()); |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 271 | void ModulePrinter::print(const Type *type) const { |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 272 | switch (type->getKind()) { |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 273 | case Type::Kind::AffineInt: |
| 274 | os << "affineint"; |
| 275 | return; |
| 276 | case Type::Kind::BF16: |
| 277 | os << "bf16"; |
| 278 | return; |
| 279 | case Type::Kind::F16: |
| 280 | os << "f16"; |
| 281 | return; |
| 282 | case Type::Kind::F32: |
| 283 | os << "f32"; |
| 284 | return; |
| 285 | case Type::Kind::F64: |
| 286 | os << "f64"; |
| 287 | return; |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 288 | |
| 289 | case Type::Kind::Integer: { |
| 290 | auto *integer = cast<IntegerType>(type); |
| 291 | os << 'i' << integer->getWidth(); |
| 292 | return; |
| 293 | } |
| 294 | case Type::Kind::Function: { |
| 295 | auto *func = cast<FunctionType>(type); |
| 296 | os << '('; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 297 | interleaveComma(func->getInputs(), [&](Type *type) { os << *type; }); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 298 | os << ") -> "; |
| 299 | auto results = func->getResults(); |
| 300 | if (results.size() == 1) |
| 301 | os << *results[0]; |
| 302 | else { |
| 303 | os << '('; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 304 | interleaveComma(results, [&](Type *type) { os << *type; }); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 305 | os << ')'; |
| 306 | } |
| 307 | return; |
| 308 | } |
| 309 | case Type::Kind::Vector: { |
| 310 | auto *v = cast<VectorType>(type); |
| 311 | os << "vector<"; |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 312 | for (auto dim : v->getShape()) os << dim << 'x'; |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 313 | os << *v->getElementType() << '>'; |
| 314 | return; |
| 315 | } |
| 316 | case Type::Kind::RankedTensor: { |
| 317 | auto *v = cast<RankedTensorType>(type); |
| 318 | os << "tensor<"; |
| 319 | for (auto dim : v->getShape()) { |
| 320 | if (dim < 0) |
| 321 | os << '?'; |
| 322 | else |
| 323 | os << dim; |
| 324 | os << 'x'; |
| 325 | } |
| 326 | os << *v->getElementType() << '>'; |
| 327 | return; |
| 328 | } |
| 329 | case Type::Kind::UnrankedTensor: { |
| 330 | auto *v = cast<UnrankedTensorType>(type); |
| 331 | os << "tensor<??" << *v->getElementType() << '>'; |
| 332 | return; |
| 333 | } |
| 334 | case Type::Kind::MemRef: { |
| 335 | auto *v = cast<MemRefType>(type); |
| 336 | os << "memref<"; |
| 337 | for (auto dim : v->getShape()) { |
| 338 | if (dim < 0) |
| 339 | os << '?'; |
| 340 | else |
| 341 | os << dim; |
| 342 | os << 'x'; |
| 343 | } |
| 344 | os << *v->getElementType(); |
| 345 | for (auto map : v->getAffineMaps()) { |
| 346 | os << ", "; |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 347 | printAffineMapReference(map); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 348 | } |
| 349 | os << ", " << v->getMemorySpace(); |
| 350 | os << '>'; |
| 351 | return; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 357 | // Affine expressions and maps |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | |
| 360 | void ModulePrinter::print(const AffineExpr *expr) const { |
| 361 | switch (expr->getKind()) { |
| 362 | case AffineExpr::Kind::SymbolId: |
| 363 | os << 's' << cast<AffineSymbolExpr>(expr)->getPosition(); |
| 364 | return; |
| 365 | case AffineExpr::Kind::DimId: |
| 366 | os << 'd' << cast<AffineDimExpr>(expr)->getPosition(); |
| 367 | return; |
| 368 | case AffineExpr::Kind::Constant: |
| 369 | os << cast<AffineConstantExpr>(expr)->getValue(); |
| 370 | return; |
| 371 | case AffineExpr::Kind::Add: |
| 372 | case AffineExpr::Kind::Mul: |
| 373 | case AffineExpr::Kind::FloorDiv: |
| 374 | case AffineExpr::Kind::CeilDiv: |
| 375 | case AffineExpr::Kind::Mod: |
| 376 | return print(cast<AffineBinaryOpExpr>(expr)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void ModulePrinter::print(const AffineBinaryOpExpr *expr) const { |
| 381 | if (expr->getKind() != AffineExpr::Kind::Add) { |
| 382 | os << '('; |
| 383 | print(expr->getLHS()); |
| 384 | switch (expr->getKind()) { |
| 385 | case AffineExpr::Kind::Mul: |
| 386 | os << " * "; |
| 387 | break; |
| 388 | case AffineExpr::Kind::FloorDiv: |
| 389 | os << " floordiv "; |
| 390 | break; |
| 391 | case AffineExpr::Kind::CeilDiv: |
| 392 | os << " ceildiv "; |
| 393 | break; |
| 394 | case AffineExpr::Kind::Mod: |
| 395 | os << " mod "; |
| 396 | break; |
| 397 | default: |
| 398 | llvm_unreachable("unexpected affine binary op expression"); |
| 399 | } |
| 400 | |
| 401 | print(expr->getRHS()); |
| 402 | os << ')'; |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | // Print out special "pretty" forms for add. |
| 407 | os << '('; |
| 408 | print(expr->getLHS()); |
| 409 | |
| 410 | // Pretty print addition to a product that has a negative operand as a |
| 411 | // subtraction. |
| 412 | if (auto *rhs = dyn_cast<AffineBinaryOpExpr>(expr->getRHS())) { |
| 413 | if (rhs->getKind() == AffineExpr::Kind::Mul) { |
| 414 | if (auto *rrhs = dyn_cast<AffineConstantExpr>(rhs->getRHS())) { |
| 415 | if (rrhs->getValue() < 0) { |
| 416 | os << " - ("; |
| 417 | print(rhs->getLHS()); |
| 418 | os << " * " << -rrhs->getValue() << "))"; |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Pretty print addition to a negative number as a subtraction. |
| 426 | if (auto *rhs = dyn_cast<AffineConstantExpr>(expr->getRHS())) { |
| 427 | if (rhs->getValue() < 0) { |
| 428 | os << " - " << -rhs->getValue() << ")"; |
| 429 | return; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | os << " + "; |
| 434 | print(expr->getRHS()); |
| 435 | os << ')'; |
| 436 | } |
| 437 | |
| 438 | void ModulePrinter::print(const AffineMap *map) { |
| 439 | // Dimension identifiers. |
| 440 | os << '('; |
| 441 | for (int i = 0; i < (int)map->getNumDims() - 1; i++) |
| 442 | os << "d" << i << ", "; |
| 443 | if (map->getNumDims() >= 1) |
| 444 | os << "d" << map->getNumDims() - 1; |
| 445 | os << ")"; |
| 446 | |
| 447 | // Symbolic identifiers. |
| 448 | if (map->getNumSymbols() >= 1) { |
| 449 | os << " ["; |
| 450 | for (int i = 0; i < (int)map->getNumSymbols() - 1; i++) |
| 451 | os << "s" << i << ", "; |
| 452 | if (map->getNumSymbols() >= 1) |
| 453 | os << "s" << map->getNumSymbols() - 1; |
| 454 | os << "]"; |
| 455 | } |
| 456 | |
| 457 | // AffineMap should have at least one result. |
| 458 | assert(!map->getResults().empty()); |
| 459 | // Result affine expressions. |
| 460 | os << " -> ("; |
| 461 | interleaveComma(map->getResults(), [&](AffineExpr *expr) { print(expr); }); |
| 462 | os << ")"; |
| 463 | |
| 464 | if (!map->isBounded()) { |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | // Print range sizes for bounded affine maps. |
| 469 | os << " size ("; |
| 470 | interleaveComma(map->getRangeSizes(), [&](AffineExpr *expr) { print(expr); }); |
| 471 | os << ")"; |
| 472 | } |
| 473 | |
| 474 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 475 | // Function printing |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 478 | void ModulePrinter::printFunctionSignature(const Function *fn) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 479 | auto type = fn->getType(); |
| 480 | |
| 481 | os << "@" << fn->getName() << '('; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 482 | interleaveComma(type->getInputs(), [&](Type *eltType) { print(eltType); }); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 483 | os << ')'; |
| 484 | |
| 485 | switch (type->getResults().size()) { |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 486 | case 0: |
| 487 | break; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 488 | case 1: |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 489 | os << " -> "; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 490 | print(type->getResults()[0]); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 491 | break; |
| 492 | default: |
| 493 | os << " -> ("; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 494 | interleaveComma(type->getResults(), [&](Type *eltType) { print(eltType); }); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 495 | os << ')'; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 500 | void ModulePrinter::print(const ExtFunction *fn) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 501 | os << "extfunc "; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 502 | printFunctionSignature(fn); |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 503 | os << '\n'; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 506 | namespace { |
| 507 | |
| 508 | // FunctionState contains common functionality for printing |
| 509 | // CFG and ML functions. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 510 | class FunctionState : public ModulePrinter { |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 511 | public: |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 512 | FunctionState(const ModulePrinter &other) : ModulePrinter(other) {} |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 513 | |
| 514 | void printOperation(const Operation *op); |
| 515 | |
| 516 | protected: |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 517 | void numberValueID(const SSAValue *value) { |
| 518 | assert(!valueIDs.count(value) && "Value numbered multiple times"); |
| 519 | valueIDs[value] = nextValueID++; |
| 520 | } |
| 521 | |
| 522 | void printValueID(const SSAValue *value) const { |
| 523 | // TODO: If this is the result of an operation with multiple results, look |
| 524 | // up the first result, and print the #32 syntax. |
| 525 | auto it = valueIDs.find(value); |
| 526 | if (it != valueIDs.end()) |
| 527 | os << '%' << it->getSecond(); |
| 528 | else |
| 529 | os << "<<INVALID SSA VALUE>>"; |
| 530 | } |
| 531 | |
| 532 | private: |
| 533 | /// This is the value ID for each SSA value in the current function. |
| 534 | DenseMap<const SSAValue *, unsigned> valueIDs; |
| 535 | unsigned nextValueID = 0; |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 536 | }; |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 537 | } // end anonymous namespace |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 538 | |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 539 | void FunctionState::printOperation(const Operation *op) { |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 540 | os << " "; |
| 541 | |
| 542 | // TODO: When we have SSAValue version of operands & results wired into |
| 543 | // Operation this check can go away. |
| 544 | if (auto *inst = dyn_cast<OperationInst>(op)) { |
| 545 | if (inst->getNumResults()) { |
| 546 | printValueID(inst->getResult(0)); |
| 547 | os << " = "; |
| 548 | } |
| 549 | } |
| 550 | |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 551 | // Check to see if this is a known operation. If so, use the registered |
| 552 | // custom printer hook. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 553 | if (auto opInfo = state.operationSet->lookup(op->getName().str())) { |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 554 | opInfo->printAssembly(op, os); |
| 555 | return; |
| 556 | } |
| 557 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 558 | // Otherwise use the standard verbose printing approach. |
| 559 | |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 560 | // TODO: escape name if necessary. |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 561 | os << "\"" << op->getName().str() << "\"("; |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 562 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 563 | // TODO: When we have SSAValue version of operands & results wired into |
| 564 | // Operation this check can go away. |
| 565 | if (auto *inst = dyn_cast<OperationInst>(op)) { |
| 566 | // TODO: Use getOperands() when we have it. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 567 | interleaveComma(inst->getInstOperands(), [&](const InstOperand &operand) { |
| 568 | printValueID(operand.get()); |
| 569 | }); |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 570 | } |
Chris Lattner | 7f9cc27 | 2018-07-19 08:35:28 -0700 | [diff] [blame] | 571 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 572 | os << ')'; |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 573 | auto attrs = op->getAttrs(); |
| 574 | if (!attrs.empty()) { |
| 575 | os << '{'; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 576 | interleaveComma(attrs, [&](NamedAttribute attr) { |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 577 | os << attr.first << ": "; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 578 | print(attr.second); |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 579 | }); |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 580 | os << '}'; |
| 581 | } |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 582 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 583 | // TODO: When we have SSAValue version of operands & results wired into |
| 584 | // Operation this check can go away. |
| 585 | if (auto *inst = dyn_cast<OperationInst>(op)) { |
| 586 | // Print the type signature of the operation. |
| 587 | os << " : ("; |
| 588 | // TODO: Switch to getOperands() when we have it. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 589 | interleaveComma(inst->getInstOperands(), |
| 590 | [&](const InstOperand &op) { print(op.get()->getType()); }); |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 591 | os << ") -> "; |
| 592 | |
| 593 | // TODO: Switch to getResults() when we have it. |
| 594 | if (inst->getNumResults() == 1) { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 595 | print(inst->getInstResult(0).getType()); |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 596 | } else { |
| 597 | os << '('; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 598 | interleaveComma(inst->getInstResults(), [&](const InstResult &result) { |
| 599 | print(result.getType()); |
| 600 | }); |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 601 | os << ')'; |
| 602 | } |
| 603 | } |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 606 | //===----------------------------------------------------------------------===// |
| 607 | // CFG Function printing |
| 608 | //===----------------------------------------------------------------------===// |
| 609 | |
| 610 | namespace { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 611 | class CFGFunctionPrinter : public FunctionState { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 612 | public: |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 613 | CFGFunctionPrinter(const CFGFunction *function, const ModulePrinter &other); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 614 | |
| 615 | const CFGFunction *getFunction() const { return function; } |
| 616 | |
| 617 | void print(); |
| 618 | void print(const BasicBlock *block); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 619 | |
| 620 | void print(const Instruction *inst); |
| 621 | void print(const OperationInst *inst); |
| 622 | void print(const ReturnInst *inst); |
| 623 | void print(const BranchInst *inst); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 624 | |
| 625 | unsigned getBBID(const BasicBlock *block) { |
| 626 | auto it = basicBlockIDs.find(block); |
| 627 | assert(it != basicBlockIDs.end() && "Block not in this function?"); |
| 628 | return it->second; |
| 629 | } |
| 630 | |
| 631 | private: |
| 632 | const CFGFunction *function; |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 633 | DenseMap<const BasicBlock *, unsigned> basicBlockIDs; |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 634 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 635 | void numberValuesInBlock(const BasicBlock *block); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 636 | }; |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 637 | } // end anonymous namespace |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 638 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 639 | CFGFunctionPrinter::CFGFunctionPrinter(const CFGFunction *function, |
| 640 | const ModulePrinter &other) |
| 641 | : FunctionState(other), function(function) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 642 | // Each basic block gets a unique ID per function. |
| 643 | unsigned blockID = 0; |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 644 | for (auto &block : *function) { |
| 645 | basicBlockIDs[&block] = blockID++; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 646 | numberValuesInBlock(&block); |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | /// Number all of the SSA values in the specified basic block. |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 651 | void CFGFunctionPrinter::numberValuesInBlock(const BasicBlock *block) { |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 652 | // TODO: basic block arguments. |
| 653 | for (auto &op : *block) { |
| 654 | // We number instruction that have results, and we only number the first |
| 655 | // result. |
| 656 | if (op.getNumResults() != 0) |
| 657 | numberValueID(op.getResult(0)); |
| 658 | } |
| 659 | |
| 660 | // Terminators do not define values. |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 663 | void CFGFunctionPrinter::print() { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 664 | os << "cfgfunc "; |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 665 | printFunctionSignature(getFunction()); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 666 | os << " {\n"; |
| 667 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 668 | for (auto &block : *function) print(&block); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 669 | os << "}\n\n"; |
| 670 | } |
| 671 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 672 | void CFGFunctionPrinter::print(const BasicBlock *block) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 673 | os << "bb" << getBBID(block) << ":\n"; |
| 674 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 675 | // TODO Print arguments. |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 676 | for (auto &inst : block->getOperations()) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 677 | print(&inst); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 678 | os << "\n"; |
| 679 | } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 680 | |
| 681 | print(block->getTerminator()); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 682 | os << "\n"; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 685 | void CFGFunctionPrinter::print(const Instruction *inst) { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 686 | switch (inst->getKind()) { |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 687 | case Instruction::Kind::Operation: |
| 688 | return print(cast<OperationInst>(inst)); |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 689 | case TerminatorInst::Kind::Branch: |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 690 | return print(cast<BranchInst>(inst)); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 691 | case TerminatorInst::Kind::Return: |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 692 | return print(cast<ReturnInst>(inst)); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 696 | void CFGFunctionPrinter::print(const OperationInst *inst) { |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 697 | printOperation(inst); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 698 | } |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 699 | void CFGFunctionPrinter::print(const BranchInst *inst) { |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 700 | os << " br bb" << getBBID(inst->getDest()); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 701 | } |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 702 | void CFGFunctionPrinter::print(const ReturnInst *inst) { os << " return"; } |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 703 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 704 | void ModulePrinter::print(const CFGFunction *fn) { |
| 705 | CFGFunctionPrinter(fn, *this).print(); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 708 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 709 | // ML Function printing |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 710 | //===----------------------------------------------------------------------===// |
| 711 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 712 | namespace { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 713 | class MLFunctionPrinter : public FunctionState { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 714 | public: |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 715 | MLFunctionPrinter(const MLFunction *function, const ModulePrinter &other); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 716 | |
| 717 | const MLFunction *getFunction() const { return function; } |
| 718 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 719 | // Prints ML function |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 720 | void print(); |
| 721 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 722 | // Methods to print ML function statements |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 723 | void print(const Statement *stmt); |
Tatiana Shpeisman | fa412f7 | 2018-07-09 17:42:46 -0700 | [diff] [blame] | 724 | void print(const OperationStmt *stmt); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 725 | void print(const ForStmt *stmt); |
| 726 | void print(const IfStmt *stmt); |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 727 | void print(const StmtBlock *block); |
| 728 | |
| 729 | // Number of spaces used for indenting nested statements |
| 730 | const static unsigned indentWidth = 2; |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 731 | |
| 732 | private: |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 733 | const MLFunction *function; |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 734 | int numSpaces; |
| 735 | }; |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 736 | } // end anonymous namespace |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 737 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 738 | MLFunctionPrinter::MLFunctionPrinter(const MLFunction *function, |
| 739 | const ModulePrinter &other) |
| 740 | : FunctionState(other), function(function), numSpaces(0) {} |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 741 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 742 | void MLFunctionPrinter::print() { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 743 | os << "mlfunc "; |
| 744 | // FIXME: should print argument names rather than just signature |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 745 | printFunctionSignature(function); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 746 | os << " {\n"; |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 747 | print(function); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 748 | os << " return\n"; |
| 749 | os << "}\n\n"; |
| 750 | } |
| 751 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 752 | void MLFunctionPrinter::print(const StmtBlock *block) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 753 | numSpaces += indentWidth; |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 754 | for (auto &stmt : block->getStatements()) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 755 | print(&stmt); |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 756 | os << "\n"; |
| 757 | } |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 758 | numSpaces -= indentWidth; |
| 759 | } |
| 760 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 761 | void MLFunctionPrinter::print(const Statement *stmt) { |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 762 | switch (stmt->getKind()) { |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 763 | case Statement::Kind::Operation: |
| 764 | return print(cast<OperationStmt>(stmt)); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 765 | case Statement::Kind::For: |
| 766 | return print(cast<ForStmt>(stmt)); |
| 767 | case Statement::Kind::If: |
| 768 | return print(cast<IfStmt>(stmt)); |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 772 | void MLFunctionPrinter::print(const OperationStmt *stmt) { |
| 773 | printOperation(stmt); |
| 774 | } |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 775 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 776 | void MLFunctionPrinter::print(const ForStmt *stmt) { |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 777 | os.indent(numSpaces) << "for x = " << *stmt->getLowerBound(); |
| 778 | os << " to " << *stmt->getUpperBound(); |
| 779 | if (stmt->getStep()->getValue() != 1) |
| 780 | os << " step " << *stmt->getStep(); |
| 781 | |
| 782 | os << " {\n"; |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 783 | print(static_cast<const StmtBlock *>(stmt)); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 784 | os.indent(numSpaces) << "}"; |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 787 | void MLFunctionPrinter::print(const IfStmt *stmt) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 788 | os.indent(numSpaces) << "if () {\n"; |
| 789 | print(stmt->getThenClause()); |
| 790 | os.indent(numSpaces) << "}"; |
| 791 | if (stmt->hasElseClause()) { |
| 792 | os << " else {\n"; |
| 793 | print(stmt->getElseClause()); |
| 794 | os.indent(numSpaces) << "}"; |
| 795 | } |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 798 | void ModulePrinter::print(const MLFunction *fn) { |
| 799 | MLFunctionPrinter(fn, *this).print(); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 802 | //===----------------------------------------------------------------------===// |
| 803 | // print and dump methods |
| 804 | //===----------------------------------------------------------------------===// |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 805 | |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 806 | void Attribute::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 807 | ModuleState state(/*no context is known*/ nullptr); |
| 808 | ModulePrinter(os, state).print(this); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | void Attribute::dump() const { |
| 812 | print(llvm::errs()); |
| 813 | } |
| 814 | |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 815 | void Type::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 816 | ModuleState state(getContext()); |
| 817 | ModulePrinter(os, state).print(this); |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 818 | } |
| 819 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 820 | void Type::dump() const { print(llvm::errs()); } |
MLIR Team | 4718bc9 | 2018-07-17 16:56:54 -0700 | [diff] [blame] | 821 | |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 822 | void AffineMap::dump() const { |
| 823 | print(llvm::errs()); |
| 824 | llvm::errs() << "\n"; |
| 825 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 826 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 827 | void AffineExpr::dump() const { |
| 828 | print(llvm::errs()); |
| 829 | llvm::errs() << "\n"; |
| 830 | } |
| 831 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 832 | void AffineExpr::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 833 | ModuleState state(/*no context is known*/ nullptr); |
| 834 | ModulePrinter(os, state).print(this); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void AffineMap::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 838 | ModuleState state(/*no context is known*/ nullptr); |
| 839 | ModulePrinter(os, state).print(this); |
| 840 | } |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 841 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 842 | void Instruction::print(raw_ostream &os) const { |
| 843 | ModuleState state(getFunction()->getContext()); |
| 844 | ModulePrinter modulePrinter(os, state); |
| 845 | CFGFunctionPrinter(getFunction(), modulePrinter).print(this); |
| 846 | } |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 847 | |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 848 | void Instruction::dump() const { |
| 849 | print(llvm::errs()); |
| 850 | llvm::errs() << "\n"; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 853 | void BasicBlock::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 854 | ModuleState state(getFunction()->getContext()); |
| 855 | ModulePrinter modulePrinter(os, state); |
| 856 | CFGFunctionPrinter(getFunction(), modulePrinter).print(this); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 857 | } |
| 858 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 859 | void BasicBlock::dump() const { print(llvm::errs()); } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 860 | |
Tatiana Shpeisman | bf079c9 | 2018-07-03 17:51:28 -0700 | [diff] [blame] | 861 | void Statement::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 862 | ModuleState state(getFunction()->getContext()); |
| 863 | ModulePrinter modulePrinter(os, state); |
| 864 | MLFunctionPrinter(getFunction(), modulePrinter).print(this); |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 865 | } |
| 866 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 867 | void Statement::dump() const { print(llvm::errs()); } |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 868 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 869 | void Function::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 870 | ModuleState state(getContext()); |
| 871 | ModulePrinter(os, state).print(this); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 872 | } |
| 873 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 874 | void Function::dump() const { print(llvm::errs()); } |
| 875 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 876 | void Module::print(raw_ostream &os) const { |
Chris Lattner | 4fd59b0 | 2018-07-20 09:35:47 -0700 | [diff] [blame^] | 877 | ModuleState state(getContext()); |
| 878 | state.initialize(this); |
| 879 | ModulePrinter(os, state).print(this); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 880 | } |
| 881 | |
MLIR Team | 54b55a2 | 2018-07-18 10:16:05 -0700 | [diff] [blame] | 882 | void Module::dump() const { print(llvm::errs()); } |