blob: 0e5d10148f55923240cf00144e07c3b3ce716833 [file] [log] [blame]
Chris Lattner4c95a502018-06-23 16:03:42 -07001//===- 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 Bondhugulafaf37dd2018-06-29 18:09:29 -070023#include "mlir/IR/AffineExpr.h"
24#include "mlir/IR/AffineMap.h"
Chris Lattner7121b802018-07-04 20:45:39 -070025#include "mlir/IR/Attributes.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070026#include "mlir/IR/CFGFunction.h"
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -070027#include "mlir/IR/MLFunction.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070028#include "mlir/IR/Module.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070029#include "mlir/IR/OperationSet.h"
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -070030#include "mlir/IR/Statements.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070031#include "mlir/IR/Types.h"
32#include "mlir/Support/STLExtras.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070033#include "llvm/ADT/DenseMap.h"
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070034#include "llvm/Support/raw_ostream.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070035using namespace mlir;
36
MLIR Team54b55a22018-07-18 10:16:05 -070037void Identifier::print(raw_ostream &os) const { os << str(); }
Chris Lattner4c95a502018-06-23 16:03:42 -070038
MLIR Team54b55a22018-07-18 10:16:05 -070039void Identifier::dump() const { print(llvm::errs()); }
Chris Lattner7121b802018-07-04 20:45:39 -070040
Chris Lattner4c95a502018-06-23 16:03:42 -070041//===----------------------------------------------------------------------===//
MLIR Team4718bc92018-07-17 16:56:54 -070042// Module printing
43//===----------------------------------------------------------------------===//
44
45namespace {
MLIR Team54b55a22018-07-18 10:16:05 -070046class ModuleState {
47public:
MLIR Team4718bc92018-07-17 16:56:54 -070048 ModuleState(raw_ostream &os);
49
50 void initialize(const Module *module);
51
52 void print(const Module *module);
MLIR Teamb61885d2018-07-18 16:29:21 -070053 void print(const Attribute *attr) const;
MLIR Team4718bc92018-07-17 16:56:54 -070054 void print(const Type *type) const;
MLIR Team54b55a22018-07-18 10:16:05 -070055 void print(const Function *fn);
56 void print(const ExtFunction *fn);
57 void print(const CFGFunction *fn);
58 void print(const MLFunction *fn);
MLIR Team4718bc92018-07-17 16:56:54 -070059
MLIR Team54b55a22018-07-18 10:16:05 -070060 void recordAffineMapReference(const AffineMap *affineMap) {
MLIR Team4718bc92018-07-17 16:56:54 -070061 if (affineMapIds.count(affineMap) == 0) {
62 affineMapIds[affineMap] = nextAffineMapId++;
63 }
64 }
65
MLIR Team54b55a22018-07-18 10:16:05 -070066 int getAffineMapId(const AffineMap *affineMap) const {
MLIR Team4718bc92018-07-17 16:56:54 -070067 auto it = affineMapIds.find(affineMap);
68 if (it == affineMapIds.end()) {
69 return -1;
70 }
71 return it->second;
72 }
73
MLIR Team54b55a22018-07-18 10:16:05 -070074private:
MLIR Team4718bc92018-07-17 16:56:54 -070075 // Visit functions.
76 void visitFunction(const Function *fn);
77 void visitExtFunction(const ExtFunction *fn);
78 void visitCFGFunction(const CFGFunction *fn);
79 void visitMLFunction(const MLFunction *fn);
80 void visitType(const Type *type);
MLIR Teamb61885d2018-07-18 16:29:21 -070081 void visitAttribute(const Attribute *attr);
82 void visitOperation(const Operation *op);
83
84 void printAffineMapId(int affineMapId) const;
85 void printAffineMapReference(const AffineMap* affineMap) const;
MLIR Team4718bc92018-07-17 16:56:54 -070086
87 raw_ostream &os;
MLIR Team54b55a22018-07-18 10:16:05 -070088 DenseMap<const AffineMap *, int> affineMapIds;
MLIR Team4718bc92018-07-17 16:56:54 -070089 int nextAffineMapId = 0;
90};
MLIR Team54b55a22018-07-18 10:16:05 -070091} // end anonymous namespace
MLIR Team4718bc92018-07-17 16:56:54 -070092
MLIR Team54b55a22018-07-18 10:16:05 -070093ModuleState::ModuleState(raw_ostream &os) : os(os) {}
MLIR Team4718bc92018-07-17 16:56:54 -070094
95// Initializes module state, populating affine map state.
96void ModuleState::initialize(const Module *module) {
97 for (auto fn : module->functionList) {
98 visitFunction(fn);
99 }
100}
101
102// TODO Support visiting other types/instructions when implemented.
103void ModuleState::visitType(const Type *type) {
104 if (type->getKind() == Type::Kind::Function) {
105 // Visit input and result types for functions.
106 auto *funcType = cast<FunctionType>(type);
MLIR Team54b55a22018-07-18 10:16:05 -0700107 for (auto *input : funcType->getInputs()) {
MLIR Team4718bc92018-07-17 16:56:54 -0700108 visitType(input);
109 }
MLIR Team54b55a22018-07-18 10:16:05 -0700110 for (auto *result : funcType->getResults()) {
MLIR Team4718bc92018-07-17 16:56:54 -0700111 visitType(result);
112 }
113 } else if (type->getKind() == Type::Kind::MemRef) {
114 // Visit affine maps in memref type.
115 auto *memref = cast<MemRefType>(type);
MLIR Team54b55a22018-07-18 10:16:05 -0700116 for (AffineMap *map : memref->getAffineMaps()) {
MLIR Team4718bc92018-07-17 16:56:54 -0700117 recordAffineMapReference(map);
118 }
119 }
120}
121
MLIR Teamb61885d2018-07-18 16:29:21 -0700122void ModuleState::visitAttribute(const Attribute *attr) {
123 if (isa<AffineMapAttr>(attr)) {
124 recordAffineMapReference(cast<AffineMapAttr>(attr)->getValue());
125 } else if (isa<ArrayAttr>(attr)) {
126 for (auto elt : cast<ArrayAttr>(attr)->getValue()) {
127 visitAttribute(elt);
128 }
129 }
130}
131
132void ModuleState::visitOperation(const Operation *op) {
133 for (auto elt : op->getAttrs()) {
134 visitAttribute(elt.second);
135 }
136}
137
MLIR Team4718bc92018-07-17 16:56:54 -0700138void ModuleState::visitExtFunction(const ExtFunction *fn) {
139 visitType(fn->getType());
140}
141
142void ModuleState::visitCFGFunction(const CFGFunction *fn) {
143 visitType(fn->getType());
144 // TODO Visit function body instructions.
MLIR Teamb61885d2018-07-18 16:29:21 -0700145 for (auto &block : *fn) {
146 for (auto &op : block.getOperations()) {
147 visitOperation(&op);
148 }
149 }
MLIR Team4718bc92018-07-17 16:56:54 -0700150}
151
152void ModuleState::visitMLFunction(const MLFunction *fn) {
153 visitType(fn->getType());
MLIR Teamb61885d2018-07-18 16:29:21 -0700154 // TODO Visit function body statements (and attributes if required).
MLIR Team4718bc92018-07-17 16:56:54 -0700155}
156
157void ModuleState::visitFunction(const Function *fn) {
158 switch (fn->getKind()) {
MLIR Team54b55a22018-07-18 10:16:05 -0700159 case Function::Kind::ExtFunc:
160 return visitExtFunction(cast<ExtFunction>(fn));
161 case Function::Kind::CFGFunc:
162 return visitCFGFunction(cast<CFGFunction>(fn));
163 case Function::Kind::MLFunc:
164 return visitMLFunction(cast<MLFunction>(fn));
MLIR Team4718bc92018-07-17 16:56:54 -0700165 }
166}
167
MLIR Team4718bc92018-07-17 16:56:54 -0700168// Prints function with initialized module state.
MLIR Team54b55a22018-07-18 10:16:05 -0700169void ModuleState::print(const Function *fn) {
MLIR Team4718bc92018-07-17 16:56:54 -0700170 switch (fn->getKind()) {
MLIR Team54b55a22018-07-18 10:16:05 -0700171 case Function::Kind::ExtFunc:
172 return print(cast<ExtFunction>(fn));
173 case Function::Kind::CFGFunc:
174 return print(cast<CFGFunction>(fn));
175 case Function::Kind::MLFunc:
176 return print(cast<MLFunction>(fn));
MLIR Team4718bc92018-07-17 16:56:54 -0700177 }
178}
179
180// Prints affine map identifier.
MLIR Teamb61885d2018-07-18 16:29:21 -0700181void ModuleState::printAffineMapId(int affineMapId) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700182 os << "#map" << affineMapId;
183}
184
MLIR Teamb61885d2018-07-18 16:29:21 -0700185void ModuleState::printAffineMapReference(const AffineMap* affineMap) const {
186 const int mapId = getAffineMapId(affineMap);
187 if (mapId >= 0) {
188 // Map will be printed at top of module so print reference to its id.
189 printAffineMapId(mapId);
190 } else {
191 // Map not in module state so print inline.
192 affineMap->print(os);
193 }
194}
195
MLIR Team4718bc92018-07-17 16:56:54 -0700196void ModuleState::print(const Module *module) {
MLIR Team54b55a22018-07-18 10:16:05 -0700197 for (const auto &mapAndId : affineMapIds) {
MLIR Teamb61885d2018-07-18 16:29:21 -0700198 printAffineMapId(mapAndId.second);
MLIR Team4718bc92018-07-17 16:56:54 -0700199 os << " = ";
200 mapAndId.first->print(os);
201 os << '\n';
202 }
MLIR Team54b55a22018-07-18 10:16:05 -0700203 for (auto *fn : module->functionList) print(fn);
MLIR Team4718bc92018-07-17 16:56:54 -0700204}
205
MLIR Teamb61885d2018-07-18 16:29:21 -0700206void ModuleState::print(const Attribute *attr) const {
207 switch (attr->getKind()) {
208 case Attribute::Kind::Bool:
209 os << (cast<BoolAttr>(attr)->getValue() ? "true" : "false");
210 break;
211 case Attribute::Kind::Integer:
212 os << cast<IntegerAttr>(attr)->getValue();
213 break;
214 case Attribute::Kind::Float:
215 // FIXME: this isn't precise, we should print with a hex format.
216 os << cast<FloatAttr>(attr)->getValue();
217 break;
218 case Attribute::Kind::String:
219 // FIXME: should escape the string.
220 os << '"' << cast<StringAttr>(attr)->getValue() << '"';
221 break;
222 case Attribute::Kind::Array: {
223 auto elts = cast<ArrayAttr>(attr)->getValue();
224 os << '[';
225 interleave(elts,
226 [&](Attribute *attr) { print(attr); },
227 [&]() { os << ", "; });
228 os << ']';
229 break;
230 }
231 case Attribute::Kind::AffineMap:
232 printAffineMapReference(cast<AffineMapAttr>(attr)->getValue());
233 break;
234 }
235}
236
MLIR Team4718bc92018-07-17 16:56:54 -0700237void ModuleState::print(const Type *type) const {
238 switch (type->getKind()) {
MLIR Team54b55a22018-07-18 10:16:05 -0700239 case Type::Kind::AffineInt:
240 os << "affineint";
241 return;
242 case Type::Kind::BF16:
243 os << "bf16";
244 return;
245 case Type::Kind::F16:
246 os << "f16";
247 return;
248 case Type::Kind::F32:
249 os << "f32";
250 return;
251 case Type::Kind::F64:
252 os << "f64";
253 return;
MLIR Team4718bc92018-07-17 16:56:54 -0700254
255 case Type::Kind::Integer: {
256 auto *integer = cast<IntegerType>(type);
257 os << 'i' << integer->getWidth();
258 return;
259 }
260 case Type::Kind::Function: {
261 auto *func = cast<FunctionType>(type);
262 os << '(';
MLIR Team54b55a22018-07-18 10:16:05 -0700263 interleave(func->getInputs(), [&](Type *type) { os << *type; },
MLIR Team4718bc92018-07-17 16:56:54 -0700264 [&]() { os << ", "; });
265 os << ") -> ";
266 auto results = func->getResults();
267 if (results.size() == 1)
268 os << *results[0];
269 else {
270 os << '(';
MLIR Team54b55a22018-07-18 10:16:05 -0700271 interleave(results, [&](Type *type) { os << *type; },
MLIR Team4718bc92018-07-17 16:56:54 -0700272 [&]() { os << ", "; });
273 os << ')';
274 }
275 return;
276 }
277 case Type::Kind::Vector: {
278 auto *v = cast<VectorType>(type);
279 os << "vector<";
MLIR Team54b55a22018-07-18 10:16:05 -0700280 for (auto dim : v->getShape()) os << dim << 'x';
MLIR Team4718bc92018-07-17 16:56:54 -0700281 os << *v->getElementType() << '>';
282 return;
283 }
284 case Type::Kind::RankedTensor: {
285 auto *v = cast<RankedTensorType>(type);
286 os << "tensor<";
287 for (auto dim : v->getShape()) {
288 if (dim < 0)
289 os << '?';
290 else
291 os << dim;
292 os << 'x';
293 }
294 os << *v->getElementType() << '>';
295 return;
296 }
297 case Type::Kind::UnrankedTensor: {
298 auto *v = cast<UnrankedTensorType>(type);
299 os << "tensor<??" << *v->getElementType() << '>';
300 return;
301 }
302 case Type::Kind::MemRef: {
303 auto *v = cast<MemRefType>(type);
304 os << "memref<";
305 for (auto dim : v->getShape()) {
306 if (dim < 0)
307 os << '?';
308 else
309 os << dim;
310 os << 'x';
311 }
312 os << *v->getElementType();
313 for (auto map : v->getAffineMaps()) {
314 os << ", ";
MLIR Teamb61885d2018-07-18 16:29:21 -0700315 printAffineMapReference(map);
MLIR Team4718bc92018-07-17 16:56:54 -0700316 }
317 os << ", " << v->getMemorySpace();
318 os << '>';
319 return;
320 }
321 }
322}
323
324//===----------------------------------------------------------------------===//
Chris Lattner4c95a502018-06-23 16:03:42 -0700325// Function printing
326//===----------------------------------------------------------------------===//
327
MLIR Team4718bc92018-07-17 16:56:54 -0700328static void printFunctionSignature(const Function *fn,
329 const ModuleState *moduleState,
330 raw_ostream &os) {
Chris Lattner4c95a502018-06-23 16:03:42 -0700331 auto type = fn->getType();
332
333 os << "@" << fn->getName() << '(';
334 interleave(type->getInputs(),
MLIR Team4718bc92018-07-17 16:56:54 -0700335 [&](Type *eltType) { moduleState->print(eltType); },
Chris Lattner4c95a502018-06-23 16:03:42 -0700336 [&]() { os << ", "; });
337 os << ')';
338
339 switch (type->getResults().size()) {
MLIR Team54b55a22018-07-18 10:16:05 -0700340 case 0:
341 break;
Chris Lattner4c95a502018-06-23 16:03:42 -0700342 case 1:
MLIR Team4718bc92018-07-17 16:56:54 -0700343 os << " -> ";
344 moduleState->print(type->getResults()[0]);
Chris Lattner4c95a502018-06-23 16:03:42 -0700345 break;
346 default:
347 os << " -> (";
348 interleave(type->getResults(),
MLIR Team4718bc92018-07-17 16:56:54 -0700349 [&](Type *eltType) { moduleState->print(eltType); },
Chris Lattner4c95a502018-06-23 16:03:42 -0700350 [&]() { os << ", "; });
351 os << ')';
352 break;
353 }
354}
355
MLIR Team54b55a22018-07-18 10:16:05 -0700356void ModuleState::print(const ExtFunction *fn) {
Chris Lattner4c95a502018-06-23 16:03:42 -0700357 os << "extfunc ";
MLIR Team54b55a22018-07-18 10:16:05 -0700358 printFunctionSignature(fn, this, os);
359 os << '\n';
Chris Lattner4c95a502018-06-23 16:03:42 -0700360}
361
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700362namespace {
363
364// FunctionState contains common functionality for printing
365// CFG and ML functions.
366class FunctionState {
367public:
MLIR Team4718bc92018-07-17 16:56:54 -0700368 FunctionState(MLIRContext *context, const ModuleState *moduleState,
369 raw_ostream &os);
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700370
371 void printOperation(const Operation *op);
372
373protected:
374 raw_ostream &os;
MLIR Team4718bc92018-07-17 16:56:54 -0700375 const ModuleState *moduleState;
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700376 const OperationSet &operationSet;
377};
MLIR Team54b55a22018-07-18 10:16:05 -0700378} // end anonymous namespace
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700379
MLIR Team4718bc92018-07-17 16:56:54 -0700380FunctionState::FunctionState(MLIRContext *context,
MLIR Team54b55a22018-07-18 10:16:05 -0700381 const ModuleState *moduleState, raw_ostream &os)
382 : os(os),
383 moduleState(moduleState),
MLIR Team4718bc92018-07-17 16:56:54 -0700384 operationSet(OperationSet::get(context)) {}
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700385
386void FunctionState::printOperation(const Operation *op) {
387 // Check to see if this is a known operation. If so, use the registered
388 // custom printer hook.
389 if (auto opInfo = operationSet.lookup(op->getName().str())) {
390 os << " ";
391 opInfo->printAssembly(op, os);
392 return;
393 }
394
395 // TODO: escape name if necessary.
396 os << " \"" << op->getName().str() << "\"()";
397
398 auto attrs = op->getAttrs();
399 if (!attrs.empty()) {
400 os << '{';
401 interleave(
402 attrs,
MLIR Teamb61885d2018-07-18 16:29:21 -0700403 [&](NamedAttribute attr) {
404 os << attr.first << ": ";
405 moduleState->print(attr.second); },
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700406 [&]() { os << ", "; });
407 os << '}';
408 }
Chris Lattner3b2ef762018-07-18 15:31:25 -0700409
410 // TODO: Print signature type once that is plumbed through to Operation.
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700411}
412
Chris Lattner4c95a502018-06-23 16:03:42 -0700413//===----------------------------------------------------------------------===//
414// CFG Function printing
415//===----------------------------------------------------------------------===//
416
417namespace {
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700418class CFGFunctionState : public FunctionState {
Chris Lattner4c95a502018-06-23 16:03:42 -0700419public:
MLIR Team4718bc92018-07-17 16:56:54 -0700420 CFGFunctionState(const CFGFunction *function, const ModuleState *moduleState,
421 raw_ostream &os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700422
423 const CFGFunction *getFunction() const { return function; }
424
425 void print();
426 void print(const BasicBlock *block);
Chris Lattnered65a732018-06-28 20:45:33 -0700427
428 void print(const Instruction *inst);
429 void print(const OperationInst *inst);
430 void print(const ReturnInst *inst);
431 void print(const BranchInst *inst);
Chris Lattner4c95a502018-06-23 16:03:42 -0700432
433 unsigned getBBID(const BasicBlock *block) {
434 auto it = basicBlockIDs.find(block);
435 assert(it != basicBlockIDs.end() && "Block not in this function?");
436 return it->second;
437 }
438
439private:
440 const CFGFunction *function;
MLIR Team54b55a22018-07-18 10:16:05 -0700441 DenseMap<const BasicBlock *, unsigned> basicBlockIDs;
Chris Lattner4c95a502018-06-23 16:03:42 -0700442};
MLIR Team54b55a22018-07-18 10:16:05 -0700443} // end anonymous namespace
Chris Lattner4c95a502018-06-23 16:03:42 -0700444
MLIR Team4718bc92018-07-17 16:56:54 -0700445CFGFunctionState::CFGFunctionState(const CFGFunction *function,
446 const ModuleState *moduleState,
447 raw_ostream &os)
448 : FunctionState(function->getContext(), moduleState, os),
449 function(function) {
Chris Lattner4c95a502018-06-23 16:03:42 -0700450 // Each basic block gets a unique ID per function.
451 unsigned blockID = 0;
MLIR Team54b55a22018-07-18 10:16:05 -0700452 for (auto &block : *function) basicBlockIDs[&block] = blockID++;
Chris Lattner4c95a502018-06-23 16:03:42 -0700453}
454
455void CFGFunctionState::print() {
456 os << "cfgfunc ";
MLIR Team4718bc92018-07-17 16:56:54 -0700457 printFunctionSignature(this->getFunction(), moduleState, os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700458 os << " {\n";
459
MLIR Team54b55a22018-07-18 10:16:05 -0700460 for (auto &block : *function) print(&block);
Chris Lattner4c95a502018-06-23 16:03:42 -0700461 os << "}\n\n";
462}
463
464void CFGFunctionState::print(const BasicBlock *block) {
465 os << "bb" << getBBID(block) << ":\n";
466
Chris Lattnered65a732018-06-28 20:45:33 -0700467 // TODO Print arguments.
Jacques Pienaarb020c542018-07-15 00:06:54 -0700468 for (auto &inst : block->getOperations()) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700469 print(&inst);
Jacques Pienaarb020c542018-07-15 00:06:54 -0700470 os << "\n";
471 }
Chris Lattner4c95a502018-06-23 16:03:42 -0700472
473 print(block->getTerminator());
Jacques Pienaarb020c542018-07-15 00:06:54 -0700474 os << "\n";
Chris Lattner4c95a502018-06-23 16:03:42 -0700475}
476
Chris Lattnered65a732018-06-28 20:45:33 -0700477void CFGFunctionState::print(const Instruction *inst) {
Chris Lattner4c95a502018-06-23 16:03:42 -0700478 switch (inst->getKind()) {
Chris Lattnered65a732018-06-28 20:45:33 -0700479 case Instruction::Kind::Operation:
480 return print(cast<OperationInst>(inst));
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700481 case TerminatorInst::Kind::Branch:
Chris Lattnered65a732018-06-28 20:45:33 -0700482 return print(cast<BranchInst>(inst));
Chris Lattner4c95a502018-06-23 16:03:42 -0700483 case TerminatorInst::Kind::Return:
Chris Lattnered65a732018-06-28 20:45:33 -0700484 return print(cast<ReturnInst>(inst));
Chris Lattner4c95a502018-06-23 16:03:42 -0700485 }
486}
487
Chris Lattnered65a732018-06-28 20:45:33 -0700488void CFGFunctionState::print(const OperationInst *inst) {
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700489 printOperation(inst);
Chris Lattnered65a732018-06-28 20:45:33 -0700490
Chris Lattner3b2ef762018-07-18 15:31:25 -0700491 // FIXME: Move this into printOperation when Operation has operands and
492 // results
493
494 // Print the type signature of the operation.
495 os << " : (";
496 interleave(
497 inst->getOperands(),
498 [&](const InstOperand &op) { moduleState->print(op.get()->getType()); },
499 [&]() { os << ", "; });
500 os << ") -> ";
501
502 auto resultList = inst->getResults();
503 if (resultList.size() == 1) {
504 moduleState->print(resultList[0].getType());
505 } else {
506 os << '(';
507 interleave(
508 resultList,
509 [&](const InstResult &result) { moduleState->print(result.getType()); },
510 [&]() { os << ", "; });
511 os << ')';
512 }
513}
Chris Lattnered65a732018-06-28 20:45:33 -0700514void CFGFunctionState::print(const BranchInst *inst) {
Jacques Pienaarb020c542018-07-15 00:06:54 -0700515 os << " br bb" << getBBID(inst->getDest());
Chris Lattnered65a732018-06-28 20:45:33 -0700516}
MLIR Team54b55a22018-07-18 10:16:05 -0700517void CFGFunctionState::print(const ReturnInst *inst) { os << " return"; }
518
519void ModuleState::print(const CFGFunction *fn) {
520 CFGFunctionState state(fn, this, os);
521 state.print();
Chris Lattnered65a732018-06-28 20:45:33 -0700522}
523
Chris Lattner4c95a502018-06-23 16:03:42 -0700524//===----------------------------------------------------------------------===//
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700525// ML Function printing
Chris Lattner4c95a502018-06-23 16:03:42 -0700526//===----------------------------------------------------------------------===//
527
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700528namespace {
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700529class MLFunctionState : public FunctionState {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700530public:
MLIR Team4718bc92018-07-17 16:56:54 -0700531 MLFunctionState(const MLFunction *function, const ModuleState *moduleState,
532 raw_ostream &os);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700533
534 const MLFunction *getFunction() const { return function; }
535
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700536 // Prints ML function
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700537 void print();
538
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700539 // Methods to print ML function statements
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700540 void print(const Statement *stmt);
Tatiana Shpeismanfa412f72018-07-09 17:42:46 -0700541 void print(const OperationStmt *stmt);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700542 void print(const ForStmt *stmt);
543 void print(const IfStmt *stmt);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700544 void print(const StmtBlock *block);
545
546 // Number of spaces used for indenting nested statements
547 const static unsigned indentWidth = 2;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700548
549private:
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700550 const MLFunction *function;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700551 int numSpaces;
552};
MLIR Team54b55a22018-07-18 10:16:05 -0700553} // end anonymous namespace
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700554
MLIR Team4718bc92018-07-17 16:56:54 -0700555MLFunctionState::MLFunctionState(const MLFunction *function,
556 const ModuleState *moduleState,
557 raw_ostream &os)
558 : FunctionState(function->getContext(), moduleState, os),
MLIR Team54b55a22018-07-18 10:16:05 -0700559 function(function),
560 numSpaces(0) {}
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700561
562void MLFunctionState::print() {
563 os << "mlfunc ";
564 // FIXME: should print argument names rather than just signature
MLIR Team4718bc92018-07-17 16:56:54 -0700565 printFunctionSignature(function, moduleState, os);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700566 os << " {\n";
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700567 print(function);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700568 os << " return\n";
569 os << "}\n\n";
570}
571
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700572void MLFunctionState::print(const StmtBlock *block) {
573 numSpaces += indentWidth;
Jacques Pienaarb020c542018-07-15 00:06:54 -0700574 for (auto &stmt : block->getStatements()) {
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700575 print(&stmt);
Jacques Pienaarb020c542018-07-15 00:06:54 -0700576 os << "\n";
577 }
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700578 numSpaces -= indentWidth;
579}
580
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700581void MLFunctionState::print(const Statement *stmt) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700582 switch (stmt->getKind()) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700583 case Statement::Kind::Operation:
584 return print(cast<OperationStmt>(stmt));
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700585 case Statement::Kind::For:
586 return print(cast<ForStmt>(stmt));
587 case Statement::Kind::If:
588 return print(cast<IfStmt>(stmt));
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700589 }
590}
591
MLIR Team54b55a22018-07-18 10:16:05 -0700592void MLFunctionState::print(const OperationStmt *stmt) { printOperation(stmt); }
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700593
594void MLFunctionState::print(const ForStmt *stmt) {
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700595 os.indent(numSpaces) << "for {\n";
596 print(static_cast<const StmtBlock *>(stmt));
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700597 os.indent(numSpaces) << "}";
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700598}
599
600void MLFunctionState::print(const IfStmt *stmt) {
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700601 os.indent(numSpaces) << "if () {\n";
602 print(stmt->getThenClause());
603 os.indent(numSpaces) << "}";
604 if (stmt->hasElseClause()) {
605 os << " else {\n";
606 print(stmt->getElseClause());
607 os.indent(numSpaces) << "}";
608 }
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700609}
610
MLIR Team54b55a22018-07-18 10:16:05 -0700611void ModuleState::print(const MLFunction *fn) {
612 MLFunctionState state(fn, this, os);
MLIR Team4718bc92018-07-17 16:56:54 -0700613 state.print();
614}
615
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700616//===----------------------------------------------------------------------===//
617// print and dump methods
618//===----------------------------------------------------------------------===//
Chris Lattnered65a732018-06-28 20:45:33 -0700619
MLIR Teamb61885d2018-07-18 16:29:21 -0700620void Attribute::print(raw_ostream &os) const {
621 ModuleState moduleState(os);
622 moduleState.print(this);
623}
624
625void Attribute::dump() const {
626 print(llvm::errs());
627}
628
MLIR Team4718bc92018-07-17 16:56:54 -0700629void Type::print(raw_ostream &os) const {
630 ModuleState moduleState(os);
631 moduleState.print(this);
632}
633
MLIR Team54b55a22018-07-18 10:16:05 -0700634void Type::dump() const { print(llvm::errs()); }
MLIR Team4718bc92018-07-17 16:56:54 -0700635
Chris Lattnered65a732018-06-28 20:45:33 -0700636void Instruction::print(raw_ostream &os) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700637 ModuleState moduleState(os);
638 CFGFunctionState state(getFunction(), &moduleState, os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700639 state.print(this);
640}
641
Chris Lattnered65a732018-06-28 20:45:33 -0700642void Instruction::dump() const {
Chris Lattner4c95a502018-06-23 16:03:42 -0700643 print(llvm::errs());
Jacques Pienaarb020c542018-07-15 00:06:54 -0700644 llvm::errs() << "\n";
Chris Lattner4c95a502018-06-23 16:03:42 -0700645}
646
MLIR Team718c82f2018-07-16 09:45:22 -0700647void AffineMap::dump() const {
648 print(llvm::errs());
649 llvm::errs() << "\n";
650}
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700651
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700652void AffineExpr::dump() const {
653 print(llvm::errs());
654 llvm::errs() << "\n";
655}
656
657void AffineAddExpr::print(raw_ostream &os) const {
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700658 os << "(" << *getLHS() << " + " << *getRHS() << ")";
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700659}
660
661void AffineSubExpr::print(raw_ostream &os) const {
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700662 os << "(" << *getLHS() << " - " << *getRHS() << ")";
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700663}
664
665void AffineMulExpr::print(raw_ostream &os) const {
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700666 os << "(" << *getLHS() << " * " << *getRHS() << ")";
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700667}
668
669void AffineModExpr::print(raw_ostream &os) const {
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700670 os << "(" << *getLHS() << " mod " << *getRHS() << ")";
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700671}
672
673void AffineFloorDivExpr::print(raw_ostream &os) const {
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700674 os << "(" << *getLHS() << " floordiv " << *getRHS() << ")";
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700675}
676
677void AffineCeilDivExpr::print(raw_ostream &os) const {
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700678 os << "(" << *getLHS() << " ceildiv " << *getRHS() << ")";
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700679}
680
681void AffineSymbolExpr::print(raw_ostream &os) const {
682 os << "s" << getPosition();
683}
684
685void AffineDimExpr::print(raw_ostream &os) const { os << "d" << getPosition(); }
686
687void AffineConstantExpr::print(raw_ostream &os) const { os << getValue(); }
688
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700689void AffineExpr::print(raw_ostream &os) const {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700690 switch (getKind()) {
691 case Kind::SymbolId:
692 return cast<AffineSymbolExpr>(this)->print(os);
693 case Kind::DimId:
694 return cast<AffineDimExpr>(this)->print(os);
695 case Kind::Constant:
696 return cast<AffineConstantExpr>(this)->print(os);
697 case Kind::Add:
698 return cast<AffineAddExpr>(this)->print(os);
699 case Kind::Sub:
700 return cast<AffineSubExpr>(this)->print(os);
701 case Kind::Mul:
702 return cast<AffineMulExpr>(this)->print(os);
703 case Kind::FloorDiv:
704 return cast<AffineFloorDivExpr>(this)->print(os);
705 case Kind::CeilDiv:
706 return cast<AffineCeilDivExpr>(this)->print(os);
707 case Kind::Mod:
708 return cast<AffineModExpr>(this)->print(os);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700709 }
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700710}
711
712void AffineMap::print(raw_ostream &os) const {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700713 // Dimension identifiers.
714 os << "(";
MLIR Team54b55a22018-07-18 10:16:05 -0700715 for (int i = 0; i < (int)getNumDims() - 1; i++) os << "d" << i << ", ";
716 if (getNumDims() >= 1) os << "d" << getNumDims() - 1;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700717 os << ")";
718
719 // Symbolic identifiers.
720 if (getNumSymbols() >= 1) {
721 os << " [";
MLIR Team54b55a22018-07-18 10:16:05 -0700722 for (int i = 0; i < (int)getNumSymbols() - 1; i++) os << "s" << i << ", ";
723 if (getNumSymbols() >= 1) os << "s" << getNumSymbols() - 1;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700724 os << "]";
725 }
726
727 // AffineMap should have at least one result.
728 assert(!getResults().empty());
729 // Result affine expressions.
730 os << " -> (";
731 interleave(getResults(), [&](AffineExpr *expr) { os << *expr; },
732 [&]() { os << ", "; });
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700733 os << ")";
734
735 if (!isBounded()) {
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700736 return;
737 }
738
739 // Print range sizes for bounded affine maps.
740 os << " size (";
741 interleave(getRangeSizes(), [&](AffineExpr *expr) { os << *expr; },
742 [&]() { os << ", "; });
MLIR Team718c82f2018-07-16 09:45:22 -0700743 os << ")";
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700744}
745
Chris Lattner4c95a502018-06-23 16:03:42 -0700746void BasicBlock::print(raw_ostream &os) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700747 ModuleState moduleState(os);
748 CFGFunctionState state(getFunction(), &moduleState, os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700749 state.print();
750}
751
MLIR Team54b55a22018-07-18 10:16:05 -0700752void BasicBlock::dump() const { print(llvm::errs()); }
Chris Lattner4c95a502018-06-23 16:03:42 -0700753
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700754void Statement::print(raw_ostream &os) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700755 ModuleState moduleState(os);
756 MLFunctionState state(getFunction(), &moduleState, os);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700757 state.print(this);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700758}
759
MLIR Team54b55a22018-07-18 10:16:05 -0700760void Statement::dump() const { print(llvm::errs()); }
Jacques Pienaarb020c542018-07-15 00:06:54 -0700761
Chris Lattner4c95a502018-06-23 16:03:42 -0700762void Function::print(raw_ostream &os) const {
763 switch (getKind()) {
MLIR Team54b55a22018-07-18 10:16:05 -0700764 case Kind::ExtFunc:
765 return cast<ExtFunction>(this)->print(os);
766 case Kind::CFGFunc:
767 return cast<CFGFunction>(this)->print(os);
768 case Kind::MLFunc:
769 return cast<MLFunction>(this)->print(os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700770 }
771}
772
MLIR Team54b55a22018-07-18 10:16:05 -0700773void Function::dump() const { print(llvm::errs()); }
774
775void ExtFunction::print(raw_ostream &os) const {
776 ModuleState moduleState(os);
777 os << "extfunc ";
778 printFunctionSignature(this, &moduleState, os);
779 os << "\n";
Chris Lattner4c95a502018-06-23 16:03:42 -0700780}
781
782void CFGFunction::print(raw_ostream &os) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700783 ModuleState moduleState(os);
784 CFGFunctionState state(this, &moduleState, os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700785 state.print();
786}
787
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700788void MLFunction::print(raw_ostream &os) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700789 ModuleState moduleState(os);
790 MLFunctionState state(this, &moduleState, os);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700791 state.print();
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700792}
793
Chris Lattner4c95a502018-06-23 16:03:42 -0700794void Module::print(raw_ostream &os) const {
MLIR Team4718bc92018-07-17 16:56:54 -0700795 ModuleState moduleState(os);
796 moduleState.initialize(this);
797 moduleState.print(this);
Chris Lattner4c95a502018-06-23 16:03:42 -0700798}
799
MLIR Team54b55a22018-07-18 10:16:05 -0700800void Module::dump() const { print(llvm::errs()); }