blob: c1c1d947454f5ee7f9500e51e3f3c4778fa43582 [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 Lattner4c95a502018-06-23 16:03:42 -070025#include "mlir/IR/CFGFunction.h"
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -070026#include "mlir/IR/MLFunction.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070027#include "mlir/IR/Module.h"
28#include "mlir/IR/Types.h"
29#include "mlir/Support/STLExtras.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070030#include "llvm/ADT/DenseMap.h"
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070031#include "llvm/Support/raw_ostream.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070032using namespace mlir;
33
34
35//===----------------------------------------------------------------------===//
36// Function printing
37//===----------------------------------------------------------------------===//
38
39static void printFunctionSignature(const Function *fn, raw_ostream &os) {
40 auto type = fn->getType();
41
42 os << "@" << fn->getName() << '(';
43 interleave(type->getInputs(),
44 [&](Type *eltType) { os << *eltType; },
45 [&]() { os << ", "; });
46 os << ')';
47
48 switch (type->getResults().size()) {
49 case 0: break;
50 case 1:
51 os << " -> " << *type->getResults()[0];
52 break;
53 default:
54 os << " -> (";
55 interleave(type->getResults(),
56 [&](Type *eltType) { os << *eltType; },
57 [&]() { os << ", "; });
58 os << ')';
59 break;
60 }
61}
62
63void ExtFunction::print(raw_ostream &os) const {
64 os << "extfunc ";
65 printFunctionSignature(this, os);
66 os << "\n";
67}
68
69//===----------------------------------------------------------------------===//
70// CFG Function printing
71//===----------------------------------------------------------------------===//
72
73namespace {
74class CFGFunctionState {
75public:
76 CFGFunctionState(const CFGFunction *function, raw_ostream &os);
77
78 const CFGFunction *getFunction() const { return function; }
79
80 void print();
81 void print(const BasicBlock *block);
Chris Lattnered65a732018-06-28 20:45:33 -070082
83 void print(const Instruction *inst);
84 void print(const OperationInst *inst);
85 void print(const ReturnInst *inst);
86 void print(const BranchInst *inst);
Chris Lattner4c95a502018-06-23 16:03:42 -070087
88 unsigned getBBID(const BasicBlock *block) {
89 auto it = basicBlockIDs.find(block);
90 assert(it != basicBlockIDs.end() && "Block not in this function?");
91 return it->second;
92 }
93
94private:
95 const CFGFunction *function;
96 raw_ostream &os;
Chris Lattner3a467cc2018-07-01 20:28:00 -070097 DenseMap<const BasicBlock*, unsigned> basicBlockIDs;
Chris Lattner4c95a502018-06-23 16:03:42 -070098};
99} // end anonymous namespace
100
101CFGFunctionState::CFGFunctionState(const CFGFunction *function, raw_ostream &os)
102 : function(function), os(os) {
103
104 // Each basic block gets a unique ID per function.
105 unsigned blockID = 0;
Chris Lattner3a467cc2018-07-01 20:28:00 -0700106 for (auto &block : *function)
107 basicBlockIDs[&block] = blockID++;
Chris Lattner4c95a502018-06-23 16:03:42 -0700108}
109
110void CFGFunctionState::print() {
111 os << "cfgfunc ";
112 printFunctionSignature(this->getFunction(), os);
113 os << " {\n";
114
Chris Lattner3a467cc2018-07-01 20:28:00 -0700115 for (auto &block : *function)
116 print(&block);
Chris Lattner4c95a502018-06-23 16:03:42 -0700117 os << "}\n\n";
118}
119
120void CFGFunctionState::print(const BasicBlock *block) {
121 os << "bb" << getBBID(block) << ":\n";
122
Chris Lattnered65a732018-06-28 20:45:33 -0700123 // TODO Print arguments.
Chris Lattner3a467cc2018-07-01 20:28:00 -0700124 for (auto &inst : block->getOperations())
125 print(&inst);
Chris Lattner4c95a502018-06-23 16:03:42 -0700126
127 print(block->getTerminator());
128}
129
Chris Lattnered65a732018-06-28 20:45:33 -0700130void CFGFunctionState::print(const Instruction *inst) {
Chris Lattner4c95a502018-06-23 16:03:42 -0700131 switch (inst->getKind()) {
Chris Lattnered65a732018-06-28 20:45:33 -0700132 case Instruction::Kind::Operation:
133 return print(cast<OperationInst>(inst));
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700134 case TerminatorInst::Kind::Branch:
Chris Lattnered65a732018-06-28 20:45:33 -0700135 return print(cast<BranchInst>(inst));
Chris Lattner4c95a502018-06-23 16:03:42 -0700136 case TerminatorInst::Kind::Return:
Chris Lattnered65a732018-06-28 20:45:33 -0700137 return print(cast<ReturnInst>(inst));
Chris Lattner4c95a502018-06-23 16:03:42 -0700138 }
139}
140
Chris Lattnered65a732018-06-28 20:45:33 -0700141void CFGFunctionState::print(const OperationInst *inst) {
142 // TODO: escape name if necessary.
143 os << " \"" << inst->getName().str() << "\"()\n";
144}
145
146void CFGFunctionState::print(const BranchInst *inst) {
147 os << " br bb" << getBBID(inst->getDest()) << "\n";
148}
149void CFGFunctionState::print(const ReturnInst *inst) {
150 os << " return\n";
151}
152
Chris Lattner4c95a502018-06-23 16:03:42 -0700153//===----------------------------------------------------------------------===//
154// print and dump methods
155//===----------------------------------------------------------------------===//
156
Chris Lattnered65a732018-06-28 20:45:33 -0700157
158void Instruction::print(raw_ostream &os) const {
Chris Lattner4c95a502018-06-23 16:03:42 -0700159 CFGFunctionState state(getFunction(), os);
160 state.print(this);
161}
162
Chris Lattnered65a732018-06-28 20:45:33 -0700163void Instruction::dump() const {
Chris Lattner4c95a502018-06-23 16:03:42 -0700164 print(llvm::errs());
165}
166
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700167void AffineExpr::print(raw_ostream &os) const {
168 // TODO(bondhugula): print out affine expression
169}
170
171void AffineMap::print(raw_ostream &os) const {
172 // TODO(andydavis) Print out affine map based on dimensionCount and
173 // symbolCount: (d0, d1) [S0, S1] -> (d0 + S0, d1 + S1)
174}
175
Chris Lattner4c95a502018-06-23 16:03:42 -0700176void BasicBlock::print(raw_ostream &os) const {
177 CFGFunctionState state(getFunction(), os);
178 state.print();
179}
180
181void BasicBlock::dump() const {
182 print(llvm::errs());
183}
184
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700185void MLStatement::print(raw_ostream &os) const {
186 //TODO
187}
188
189void MLStatement::dump() const {
190 print(llvm::errs());
191}
Chris Lattner4c95a502018-06-23 16:03:42 -0700192void Function::print(raw_ostream &os) const {
193 switch (getKind()) {
194 case Kind::ExtFunc: return cast<ExtFunction>(this)->print(os);
195 case Kind::CFGFunc: return cast<CFGFunction>(this)->print(os);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700196 case Kind::MLFunc: return cast<MLFunction>(this)->print(os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700197 }
198}
199
200void Function::dump() const {
201 print(llvm::errs());
202}
203
204void CFGFunction::print(raw_ostream &os) const {
205 CFGFunctionState state(this, os);
206 state.print();
207}
208
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700209void MLFunction::print(raw_ostream &os) const {
210 os << "mlfunc ";
211 // FIXME: should print argument names rather than just signature
212 printFunctionSignature(this, os);
213 os << " {\n";
214
215 for (auto *stmt : stmtList)
216 stmt->print(os);
217 os << " return\n";
218 os << "}\n\n";
219}
220
Chris Lattner4c95a502018-06-23 16:03:42 -0700221void Module::print(raw_ostream &os) const {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700222 for (auto *map : affineMapList)
223 map->print(os);
Chris Lattner4c95a502018-06-23 16:03:42 -0700224 for (auto *fn : functionList)
225 fn->print(os);
226}
227
228void Module::dump() const {
229 print(llvm::errs());
230}
231