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