2018-06-24 07:03:42 +08:00
|
|
|
//===- AsmPrinter.cpp - MLIR Assembly Printer Implementation --------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file implements the MLIR AsmPrinter class, which is used to implement
|
|
|
|
// the various print() methods on the core IR objects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-30 09:09:29 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
2018-07-05 11:45:39 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
#include "mlir/IR/CFGFunction.h"
|
2018-06-29 08:02:32 +08:00
|
|
|
#include "mlir/IR/MLFunction.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2018-07-06 00:12:11 +08:00
|
|
|
#include "mlir/IR/OperationSet.h"
|
2018-07-14 04:03:13 +08:00
|
|
|
#include "mlir/IR/Statements.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
#include "mlir/IR/Types.h"
|
|
|
|
#include "mlir/Support/STLExtras.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2018-06-30 09:09:29 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Identifier::print(raw_ostream &os) const { os << str(); }
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Identifier::dump() const { print(llvm::errs()); }
|
2018-07-05 11:45:39 +08:00
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-21 00:35:47 +08:00
|
|
|
// ModuleState
|
2018-07-18 07:56:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2018-07-19 01:16:05 +08:00
|
|
|
class ModuleState {
|
|
|
|
public:
|
2018-07-21 00:35:47 +08:00
|
|
|
/// This is the operation set for the current context if it is knowable (a
|
|
|
|
/// context could be determined), otherwise this is null.
|
|
|
|
OperationSet *const operationSet;
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
explicit ModuleState(MLIRContext *context)
|
|
|
|
: operationSet(context ? &OperationSet::get(context) : nullptr) {}
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
// Initializes module state, populating affine map state.
|
|
|
|
void initialize(const Module *module);
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
int getAffineMapId(const AffineMap *affineMap) const {
|
2018-07-18 07:56:54 +08:00
|
|
|
auto it = affineMapIds.find(affineMap);
|
|
|
|
if (it == affineMapIds.end()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
const DenseMap<const AffineMap *, int> &getAffineMapIds() const {
|
|
|
|
return affineMapIds;
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
private:
|
2018-07-21 00:35:47 +08:00
|
|
|
void recordAffineMapReference(const AffineMap *affineMap) {
|
|
|
|
if (affineMapIds.count(affineMap) == 0) {
|
|
|
|
affineMapIds[affineMap] = nextAffineMapId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
// Visit functions.
|
|
|
|
void visitFunction(const Function *fn);
|
|
|
|
void visitExtFunction(const ExtFunction *fn);
|
|
|
|
void visitCFGFunction(const CFGFunction *fn);
|
|
|
|
void visitMLFunction(const MLFunction *fn);
|
|
|
|
void visitType(const Type *type);
|
2018-07-19 07:29:21 +08:00
|
|
|
void visitAttribute(const Attribute *attr);
|
|
|
|
void visitOperation(const Operation *op);
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
DenseMap<const AffineMap *, int> affineMapIds;
|
2018-07-18 07:56:54 +08:00
|
|
|
int nextAffineMapId = 0;
|
|
|
|
};
|
2018-07-19 01:16:05 +08:00
|
|
|
} // end anonymous namespace
|
2018-07-18 07:56:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
// TODO Support visiting other types/instructions when implemented.
|
|
|
|
void ModuleState::visitType(const Type *type) {
|
|
|
|
if (type->getKind() == Type::Kind::Function) {
|
|
|
|
// Visit input and result types for functions.
|
|
|
|
auto *funcType = cast<FunctionType>(type);
|
2018-07-19 01:16:05 +08:00
|
|
|
for (auto *input : funcType->getInputs()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
visitType(input);
|
|
|
|
}
|
2018-07-19 01:16:05 +08:00
|
|
|
for (auto *result : funcType->getResults()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
visitType(result);
|
|
|
|
}
|
|
|
|
} else if (type->getKind() == Type::Kind::MemRef) {
|
|
|
|
// Visit affine maps in memref type.
|
|
|
|
auto *memref = cast<MemRefType>(type);
|
2018-07-19 01:16:05 +08:00
|
|
|
for (AffineMap *map : memref->getAffineMaps()) {
|
2018-07-18 07:56:54 +08:00
|
|
|
recordAffineMapReference(map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 07:29:21 +08:00
|
|
|
void ModuleState::visitAttribute(const Attribute *attr) {
|
|
|
|
if (isa<AffineMapAttr>(attr)) {
|
|
|
|
recordAffineMapReference(cast<AffineMapAttr>(attr)->getValue());
|
|
|
|
} else if (isa<ArrayAttr>(attr)) {
|
|
|
|
for (auto elt : cast<ArrayAttr>(attr)->getValue()) {
|
|
|
|
visitAttribute(elt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleState::visitOperation(const Operation *op) {
|
|
|
|
for (auto elt : op->getAttrs()) {
|
|
|
|
visitAttribute(elt.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
void ModuleState::visitExtFunction(const ExtFunction *fn) {
|
|
|
|
visitType(fn->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleState::visitCFGFunction(const CFGFunction *fn) {
|
|
|
|
visitType(fn->getType());
|
|
|
|
// TODO Visit function body instructions.
|
2018-07-19 07:29:21 +08:00
|
|
|
for (auto &block : *fn) {
|
|
|
|
for (auto &op : block.getOperations()) {
|
|
|
|
visitOperation(&op);
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleState::visitMLFunction(const MLFunction *fn) {
|
|
|
|
visitType(fn->getType());
|
2018-07-19 07:29:21 +08:00
|
|
|
// TODO Visit function body statements (and attributes if required).
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleState::visitFunction(const Function *fn) {
|
|
|
|
switch (fn->getKind()) {
|
2018-07-19 01:16:05 +08:00
|
|
|
case Function::Kind::ExtFunc:
|
|
|
|
return visitExtFunction(cast<ExtFunction>(fn));
|
|
|
|
case Function::Kind::CFGFunc:
|
|
|
|
return visitCFGFunction(cast<CFGFunction>(fn));
|
|
|
|
case Function::Kind::MLFunc:
|
|
|
|
return visitMLFunction(cast<MLFunction>(fn));
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
// Initializes module state, populating affine map state.
|
|
|
|
void ModuleState::initialize(const Module *module) {
|
|
|
|
for (auto fn : module->functionList) {
|
|
|
|
visitFunction(fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ModulePrinter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ModulePrinter {
|
|
|
|
public:
|
|
|
|
ModulePrinter(raw_ostream &os, ModuleState &state) : os(os), state(state) {}
|
|
|
|
explicit ModulePrinter(const ModulePrinter &printer)
|
|
|
|
: os(printer.os), state(printer.state) {}
|
|
|
|
|
|
|
|
template <typename Container, typename UnaryFunctor>
|
|
|
|
inline void interleaveComma(const Container &c, UnaryFunctor each_fn) const {
|
|
|
|
interleave(c.begin(), c.end(), each_fn, [&]() { os << ", "; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const Module *module);
|
|
|
|
void print(const Attribute *attr) const;
|
|
|
|
void print(const Type *type) const;
|
|
|
|
void print(const Function *fn);
|
|
|
|
void print(const ExtFunction *fn);
|
|
|
|
void print(const CFGFunction *fn);
|
|
|
|
void print(const MLFunction *fn);
|
|
|
|
|
|
|
|
void print(const AffineMap *map);
|
|
|
|
void print(const AffineExpr *expr) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
raw_ostream &os;
|
|
|
|
ModuleState &state;
|
|
|
|
|
|
|
|
void printFunctionSignature(const Function *fn);
|
|
|
|
void printAffineMapId(int affineMapId) const;
|
|
|
|
void printAffineMapReference(const AffineMap *affineMap) const;
|
|
|
|
|
|
|
|
void print(const AffineBinaryOpExpr *expr) const;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
// Prints function with initialized module state.
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const Function *fn) {
|
2018-07-18 07:56:54 +08:00
|
|
|
switch (fn->getKind()) {
|
2018-07-19 01:16:05 +08:00
|
|
|
case Function::Kind::ExtFunc:
|
|
|
|
return print(cast<ExtFunction>(fn));
|
|
|
|
case Function::Kind::CFGFunc:
|
|
|
|
return print(cast<CFGFunction>(fn));
|
|
|
|
case Function::Kind::MLFunc:
|
|
|
|
return print(cast<MLFunction>(fn));
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints affine map identifier.
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::printAffineMapId(int affineMapId) const {
|
2018-07-18 07:56:54 +08:00
|
|
|
os << "#map" << affineMapId;
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::printAffineMapReference(const AffineMap *affineMap) const {
|
|
|
|
int mapId = state.getAffineMapId(affineMap);
|
2018-07-19 07:29:21 +08:00
|
|
|
if (mapId >= 0) {
|
|
|
|
// Map will be printed at top of module so print reference to its id.
|
|
|
|
printAffineMapId(mapId);
|
|
|
|
} else {
|
|
|
|
// Map not in module state so print inline.
|
|
|
|
affineMap->print(os);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const Module *module) {
|
|
|
|
for (const auto &mapAndId : state.getAffineMapIds()) {
|
2018-07-19 07:29:21 +08:00
|
|
|
printAffineMapId(mapAndId.second);
|
2018-07-18 07:56:54 +08:00
|
|
|
os << " = ";
|
|
|
|
mapAndId.first->print(os);
|
|
|
|
os << '\n';
|
|
|
|
}
|
2018-07-19 01:16:05 +08:00
|
|
|
for (auto *fn : module->functionList) print(fn);
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const Attribute *attr) const {
|
2018-07-19 07:29:21 +08:00
|
|
|
switch (attr->getKind()) {
|
|
|
|
case Attribute::Kind::Bool:
|
|
|
|
os << (cast<BoolAttr>(attr)->getValue() ? "true" : "false");
|
|
|
|
break;
|
|
|
|
case Attribute::Kind::Integer:
|
|
|
|
os << cast<IntegerAttr>(attr)->getValue();
|
|
|
|
break;
|
|
|
|
case Attribute::Kind::Float:
|
|
|
|
// FIXME: this isn't precise, we should print with a hex format.
|
|
|
|
os << cast<FloatAttr>(attr)->getValue();
|
|
|
|
break;
|
|
|
|
case Attribute::Kind::String:
|
|
|
|
// FIXME: should escape the string.
|
|
|
|
os << '"' << cast<StringAttr>(attr)->getValue() << '"';
|
|
|
|
break;
|
|
|
|
case Attribute::Kind::Array: {
|
|
|
|
auto elts = cast<ArrayAttr>(attr)->getValue();
|
|
|
|
os << '[';
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(elts, [&](Attribute *attr) { print(attr); });
|
2018-07-19 07:29:21 +08:00
|
|
|
os << ']';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Attribute::Kind::AffineMap:
|
|
|
|
printAffineMapReference(cast<AffineMapAttr>(attr)->getValue());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const Type *type) const {
|
2018-07-18 07:56:54 +08:00
|
|
|
switch (type->getKind()) {
|
2018-07-19 01:16:05 +08:00
|
|
|
case Type::Kind::AffineInt:
|
|
|
|
os << "affineint";
|
|
|
|
return;
|
|
|
|
case Type::Kind::BF16:
|
|
|
|
os << "bf16";
|
|
|
|
return;
|
|
|
|
case Type::Kind::F16:
|
|
|
|
os << "f16";
|
|
|
|
return;
|
|
|
|
case Type::Kind::F32:
|
|
|
|
os << "f32";
|
|
|
|
return;
|
|
|
|
case Type::Kind::F64:
|
|
|
|
os << "f64";
|
|
|
|
return;
|
2018-07-18 07:56:54 +08:00
|
|
|
|
|
|
|
case Type::Kind::Integer: {
|
|
|
|
auto *integer = cast<IntegerType>(type);
|
|
|
|
os << 'i' << integer->getWidth();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Type::Kind::Function: {
|
|
|
|
auto *func = cast<FunctionType>(type);
|
|
|
|
os << '(';
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(func->getInputs(), [&](Type *type) { os << *type; });
|
2018-07-18 07:56:54 +08:00
|
|
|
os << ") -> ";
|
|
|
|
auto results = func->getResults();
|
|
|
|
if (results.size() == 1)
|
|
|
|
os << *results[0];
|
|
|
|
else {
|
|
|
|
os << '(';
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(results, [&](Type *type) { os << *type; });
|
2018-07-18 07:56:54 +08:00
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Type::Kind::Vector: {
|
|
|
|
auto *v = cast<VectorType>(type);
|
|
|
|
os << "vector<";
|
2018-07-19 01:16:05 +08:00
|
|
|
for (auto dim : v->getShape()) os << dim << 'x';
|
2018-07-18 07:56:54 +08:00
|
|
|
os << *v->getElementType() << '>';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Type::Kind::RankedTensor: {
|
|
|
|
auto *v = cast<RankedTensorType>(type);
|
|
|
|
os << "tensor<";
|
|
|
|
for (auto dim : v->getShape()) {
|
|
|
|
if (dim < 0)
|
|
|
|
os << '?';
|
|
|
|
else
|
|
|
|
os << dim;
|
|
|
|
os << 'x';
|
|
|
|
}
|
|
|
|
os << *v->getElementType() << '>';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Type::Kind::UnrankedTensor: {
|
|
|
|
auto *v = cast<UnrankedTensorType>(type);
|
|
|
|
os << "tensor<??" << *v->getElementType() << '>';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Type::Kind::MemRef: {
|
|
|
|
auto *v = cast<MemRefType>(type);
|
|
|
|
os << "memref<";
|
|
|
|
for (auto dim : v->getShape()) {
|
|
|
|
if (dim < 0)
|
|
|
|
os << '?';
|
|
|
|
else
|
|
|
|
os << dim;
|
|
|
|
os << 'x';
|
|
|
|
}
|
|
|
|
os << *v->getElementType();
|
|
|
|
for (auto map : v->getAffineMaps()) {
|
|
|
|
os << ", ";
|
2018-07-19 07:29:21 +08:00
|
|
|
printAffineMapReference(map);
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
os << ", " << v->getMemorySpace();
|
|
|
|
os << '>';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Affine expressions and maps
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ModulePrinter::print(const AffineExpr *expr) const {
|
|
|
|
switch (expr->getKind()) {
|
|
|
|
case AffineExpr::Kind::SymbolId:
|
|
|
|
os << 's' << cast<AffineSymbolExpr>(expr)->getPosition();
|
|
|
|
return;
|
|
|
|
case AffineExpr::Kind::DimId:
|
|
|
|
os << 'd' << cast<AffineDimExpr>(expr)->getPosition();
|
|
|
|
return;
|
|
|
|
case AffineExpr::Kind::Constant:
|
|
|
|
os << cast<AffineConstantExpr>(expr)->getValue();
|
|
|
|
return;
|
|
|
|
case AffineExpr::Kind::Add:
|
|
|
|
case AffineExpr::Kind::Mul:
|
|
|
|
case AffineExpr::Kind::FloorDiv:
|
|
|
|
case AffineExpr::Kind::CeilDiv:
|
|
|
|
case AffineExpr::Kind::Mod:
|
|
|
|
return print(cast<AffineBinaryOpExpr>(expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::print(const AffineBinaryOpExpr *expr) const {
|
|
|
|
if (expr->getKind() != AffineExpr::Kind::Add) {
|
|
|
|
os << '(';
|
|
|
|
print(expr->getLHS());
|
|
|
|
switch (expr->getKind()) {
|
|
|
|
case AffineExpr::Kind::Mul:
|
|
|
|
os << " * ";
|
|
|
|
break;
|
|
|
|
case AffineExpr::Kind::FloorDiv:
|
|
|
|
os << " floordiv ";
|
|
|
|
break;
|
|
|
|
case AffineExpr::Kind::CeilDiv:
|
|
|
|
os << " ceildiv ";
|
|
|
|
break;
|
|
|
|
case AffineExpr::Kind::Mod:
|
|
|
|
os << " mod ";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected affine binary op expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
print(expr->getRHS());
|
|
|
|
os << ')';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out special "pretty" forms for add.
|
|
|
|
os << '(';
|
|
|
|
print(expr->getLHS());
|
|
|
|
|
|
|
|
// Pretty print addition to a product that has a negative operand as a
|
|
|
|
// subtraction.
|
|
|
|
if (auto *rhs = dyn_cast<AffineBinaryOpExpr>(expr->getRHS())) {
|
|
|
|
if (rhs->getKind() == AffineExpr::Kind::Mul) {
|
|
|
|
if (auto *rrhs = dyn_cast<AffineConstantExpr>(rhs->getRHS())) {
|
|
|
|
if (rrhs->getValue() < 0) {
|
|
|
|
os << " - (";
|
|
|
|
print(rhs->getLHS());
|
|
|
|
os << " * " << -rrhs->getValue() << "))";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pretty print addition to a negative number as a subtraction.
|
|
|
|
if (auto *rhs = dyn_cast<AffineConstantExpr>(expr->getRHS())) {
|
|
|
|
if (rhs->getValue() < 0) {
|
|
|
|
os << " - " << -rhs->getValue() << ")";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
os << " + ";
|
|
|
|
print(expr->getRHS());
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModulePrinter::print(const AffineMap *map) {
|
|
|
|
// Dimension identifiers.
|
|
|
|
os << '(';
|
|
|
|
for (int i = 0; i < (int)map->getNumDims() - 1; i++)
|
|
|
|
os << "d" << i << ", ";
|
|
|
|
if (map->getNumDims() >= 1)
|
|
|
|
os << "d" << map->getNumDims() - 1;
|
|
|
|
os << ")";
|
|
|
|
|
|
|
|
// Symbolic identifiers.
|
|
|
|
if (map->getNumSymbols() >= 1) {
|
|
|
|
os << " [";
|
|
|
|
for (int i = 0; i < (int)map->getNumSymbols() - 1; i++)
|
|
|
|
os << "s" << i << ", ";
|
|
|
|
if (map->getNumSymbols() >= 1)
|
|
|
|
os << "s" << map->getNumSymbols() - 1;
|
|
|
|
os << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
// AffineMap should have at least one result.
|
|
|
|
assert(!map->getResults().empty());
|
|
|
|
// Result affine expressions.
|
|
|
|
os << " -> (";
|
|
|
|
interleaveComma(map->getResults(), [&](AffineExpr *expr) { print(expr); });
|
|
|
|
os << ")";
|
|
|
|
|
|
|
|
if (!map->isBounded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print range sizes for bounded affine maps.
|
|
|
|
os << " size (";
|
|
|
|
interleaveComma(map->getRangeSizes(), [&](AffineExpr *expr) { print(expr); });
|
|
|
|
os << ")";
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function printing
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::printFunctionSignature(const Function *fn) {
|
2018-06-24 07:03:42 +08:00
|
|
|
auto type = fn->getType();
|
|
|
|
|
|
|
|
os << "@" << fn->getName() << '(';
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(type->getInputs(), [&](Type *eltType) { print(eltType); });
|
2018-06-24 07:03:42 +08:00
|
|
|
os << ')';
|
|
|
|
|
|
|
|
switch (type->getResults().size()) {
|
2018-07-19 01:16:05 +08:00
|
|
|
case 0:
|
|
|
|
break;
|
2018-06-24 07:03:42 +08:00
|
|
|
case 1:
|
2018-07-18 07:56:54 +08:00
|
|
|
os << " -> ";
|
2018-07-21 00:35:47 +08:00
|
|
|
print(type->getResults()[0]);
|
2018-06-24 07:03:42 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
os << " -> (";
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(type->getResults(), [&](Type *eltType) { print(eltType); });
|
2018-06-24 07:03:42 +08:00
|
|
|
os << ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const ExtFunction *fn) {
|
2018-06-24 07:03:42 +08:00
|
|
|
os << "extfunc ";
|
2018-07-21 00:35:47 +08:00
|
|
|
printFunctionSignature(fn);
|
2018-07-19 01:16:05 +08:00
|
|
|
os << '\n';
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 08:42:46 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// FunctionState contains common functionality for printing
|
|
|
|
// CFG and ML functions.
|
2018-07-21 00:35:47 +08:00
|
|
|
class FunctionState : public ModulePrinter {
|
2018-07-10 08:42:46 +08:00
|
|
|
public:
|
2018-07-21 00:35:47 +08:00
|
|
|
FunctionState(const ModulePrinter &other) : ModulePrinter(other) {}
|
2018-07-10 08:42:46 +08:00
|
|
|
|
|
|
|
void printOperation(const Operation *op);
|
|
|
|
|
|
|
|
protected:
|
2018-07-21 00:28:54 +08:00
|
|
|
void numberValueID(const SSAValue *value) {
|
|
|
|
assert(!valueIDs.count(value) && "Value numbered multiple times");
|
|
|
|
valueIDs[value] = nextValueID++;
|
|
|
|
}
|
|
|
|
|
2018-07-21 09:41:34 +08:00
|
|
|
void printValueID(const SSAValue *value,
|
|
|
|
bool dontPrintResultNo = false) const {
|
|
|
|
int resultNo = -1;
|
|
|
|
auto lookupValue = value;
|
|
|
|
|
|
|
|
// If this is a reference to the result of a multi-result instruction, print
|
|
|
|
// out the # identifier and make sure to map our lookup to the first result
|
|
|
|
// of the instruction.
|
|
|
|
if (auto *result = dyn_cast<InstResult>(value)) {
|
|
|
|
if (result->getOwner()->getNumResults() != 1) {
|
|
|
|
resultNo = result->getResultNumber();
|
|
|
|
lookupValue = result->getOwner()->getResult(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = valueIDs.find(lookupValue);
|
|
|
|
if (it == valueIDs.end()) {
|
2018-07-21 00:28:54 +08:00
|
|
|
os << "<<INVALID SSA VALUE>>";
|
2018-07-21 09:41:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << '%' << it->getSecond();
|
|
|
|
if (resultNo != -1 && !dontPrintResultNo)
|
|
|
|
os << '#' << resultNo;
|
2018-07-21 00:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// This is the value ID for each SSA value in the current function.
|
|
|
|
DenseMap<const SSAValue *, unsigned> valueIDs;
|
|
|
|
unsigned nextValueID = 0;
|
2018-07-10 08:42:46 +08:00
|
|
|
};
|
2018-07-19 01:16:05 +08:00
|
|
|
} // end anonymous namespace
|
2018-07-10 08:42:46 +08:00
|
|
|
|
|
|
|
void FunctionState::printOperation(const Operation *op) {
|
2018-07-21 00:28:54 +08:00
|
|
|
os << " ";
|
|
|
|
|
|
|
|
// TODO: When we have SSAValue version of operands & results wired into
|
|
|
|
// Operation this check can go away.
|
|
|
|
if (auto *inst = dyn_cast<OperationInst>(op)) {
|
|
|
|
if (inst->getNumResults()) {
|
2018-07-21 09:41:34 +08:00
|
|
|
printValueID(inst->getResult(0), /*dontPrintResultNo*/ true);
|
2018-07-21 00:28:54 +08:00
|
|
|
os << " = ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:42:46 +08:00
|
|
|
// Check to see if this is a known operation. If so, use the registered
|
|
|
|
// custom printer hook.
|
2018-07-21 00:35:47 +08:00
|
|
|
if (auto opInfo = state.operationSet->lookup(op->getName().str())) {
|
2018-07-10 08:42:46 +08:00
|
|
|
opInfo->printAssembly(op, os);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:28:54 +08:00
|
|
|
// Otherwise use the standard verbose printing approach.
|
|
|
|
|
2018-07-10 08:42:46 +08:00
|
|
|
// TODO: escape name if necessary.
|
2018-07-21 00:28:54 +08:00
|
|
|
os << "\"" << op->getName().str() << "\"(";
|
2018-07-10 08:42:46 +08:00
|
|
|
|
2018-07-21 00:28:54 +08:00
|
|
|
// TODO: When we have SSAValue version of operands & results wired into
|
|
|
|
// Operation this check can go away.
|
|
|
|
if (auto *inst = dyn_cast<OperationInst>(op)) {
|
|
|
|
// TODO: Use getOperands() when we have it.
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(inst->getInstOperands(), [&](const InstOperand &operand) {
|
|
|
|
printValueID(operand.get());
|
|
|
|
});
|
2018-07-21 00:28:54 +08:00
|
|
|
}
|
2018-07-19 23:35:28 +08:00
|
|
|
|
2018-07-21 00:28:54 +08:00
|
|
|
os << ')';
|
2018-07-10 08:42:46 +08:00
|
|
|
auto attrs = op->getAttrs();
|
|
|
|
if (!attrs.empty()) {
|
|
|
|
os << '{';
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(attrs, [&](NamedAttribute attr) {
|
2018-07-21 00:28:54 +08:00
|
|
|
os << attr.first << ": ";
|
2018-07-21 00:35:47 +08:00
|
|
|
print(attr.second);
|
2018-07-21 00:28:54 +08:00
|
|
|
});
|
2018-07-10 08:42:46 +08:00
|
|
|
os << '}';
|
|
|
|
}
|
2018-07-19 06:31:25 +08:00
|
|
|
|
2018-07-21 00:28:54 +08:00
|
|
|
// TODO: When we have SSAValue version of operands & results wired into
|
|
|
|
// Operation this check can go away.
|
|
|
|
if (auto *inst = dyn_cast<OperationInst>(op)) {
|
|
|
|
// Print the type signature of the operation.
|
|
|
|
os << " : (";
|
|
|
|
// TODO: Switch to getOperands() when we have it.
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(inst->getInstOperands(),
|
|
|
|
[&](const InstOperand &op) { print(op.get()->getType()); });
|
2018-07-21 00:28:54 +08:00
|
|
|
os << ") -> ";
|
|
|
|
|
|
|
|
// TODO: Switch to getResults() when we have it.
|
|
|
|
if (inst->getNumResults() == 1) {
|
2018-07-21 00:35:47 +08:00
|
|
|
print(inst->getInstResult(0).getType());
|
2018-07-21 00:28:54 +08:00
|
|
|
} else {
|
|
|
|
os << '(';
|
2018-07-21 00:35:47 +08:00
|
|
|
interleaveComma(inst->getInstResults(), [&](const InstResult &result) {
|
|
|
|
print(result.getType());
|
|
|
|
});
|
2018-07-21 00:28:54 +08:00
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 08:42:46 +08:00
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFG Function printing
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2018-07-21 00:35:47 +08:00
|
|
|
class CFGFunctionPrinter : public FunctionState {
|
2018-06-24 07:03:42 +08:00
|
|
|
public:
|
2018-07-21 00:35:47 +08:00
|
|
|
CFGFunctionPrinter(const CFGFunction *function, const ModulePrinter &other);
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
const CFGFunction *getFunction() const { return function; }
|
|
|
|
|
|
|
|
void print();
|
|
|
|
void print(const BasicBlock *block);
|
2018-06-29 11:45:33 +08:00
|
|
|
|
|
|
|
void print(const Instruction *inst);
|
|
|
|
void print(const OperationInst *inst);
|
|
|
|
void print(const ReturnInst *inst);
|
|
|
|
void print(const BranchInst *inst);
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
unsigned getBBID(const BasicBlock *block) {
|
|
|
|
auto it = basicBlockIDs.find(block);
|
|
|
|
assert(it != basicBlockIDs.end() && "Block not in this function?");
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CFGFunction *function;
|
2018-07-19 01:16:05 +08:00
|
|
|
DenseMap<const BasicBlock *, unsigned> basicBlockIDs;
|
2018-07-21 00:28:54 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void numberValuesInBlock(const BasicBlock *block);
|
2018-06-24 07:03:42 +08:00
|
|
|
};
|
2018-07-19 01:16:05 +08:00
|
|
|
} // end anonymous namespace
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
CFGFunctionPrinter::CFGFunctionPrinter(const CFGFunction *function,
|
|
|
|
const ModulePrinter &other)
|
|
|
|
: FunctionState(other), function(function) {
|
2018-06-24 07:03:42 +08:00
|
|
|
// Each basic block gets a unique ID per function.
|
|
|
|
unsigned blockID = 0;
|
2018-07-21 00:28:54 +08:00
|
|
|
for (auto &block : *function) {
|
|
|
|
basicBlockIDs[&block] = blockID++;
|
2018-07-21 00:35:47 +08:00
|
|
|
numberValuesInBlock(&block);
|
2018-07-21 00:28:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Number all of the SSA values in the specified basic block.
|
2018-07-21 00:35:47 +08:00
|
|
|
void CFGFunctionPrinter::numberValuesInBlock(const BasicBlock *block) {
|
2018-07-23 06:45:24 +08:00
|
|
|
for (auto *arg : block->getArguments()) {
|
|
|
|
numberValueID(arg);
|
|
|
|
}
|
2018-07-21 00:28:54 +08:00
|
|
|
for (auto &op : *block) {
|
|
|
|
// We number instruction that have results, and we only number the first
|
|
|
|
// result.
|
|
|
|
if (op.getNumResults() != 0)
|
|
|
|
numberValueID(op.getResult(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminators do not define values.
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void CFGFunctionPrinter::print() {
|
2018-06-24 07:03:42 +08:00
|
|
|
os << "cfgfunc ";
|
2018-07-21 00:35:47 +08:00
|
|
|
printFunctionSignature(getFunction());
|
2018-06-24 07:03:42 +08:00
|
|
|
os << " {\n";
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
for (auto &block : *function) print(&block);
|
2018-06-24 07:03:42 +08:00
|
|
|
os << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void CFGFunctionPrinter::print(const BasicBlock *block) {
|
2018-07-23 06:45:24 +08:00
|
|
|
os << "bb" << getBBID(block);
|
|
|
|
|
|
|
|
if (!block->args_empty()) {
|
|
|
|
os << '(';
|
|
|
|
interleaveComma(block->getArguments(), [&](const BBArgument *arg) {
|
|
|
|
printValueID(arg);
|
|
|
|
os << ": ";
|
|
|
|
ModulePrinter::print(arg->getType());
|
|
|
|
});
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
os << ":\n";
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-15 15:06:54 +08:00
|
|
|
for (auto &inst : block->getOperations()) {
|
2018-07-02 11:28:00 +08:00
|
|
|
print(&inst);
|
2018-07-23 06:45:24 +08:00
|
|
|
os << '\n';
|
2018-07-15 15:06:54 +08:00
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
print(block->getTerminator());
|
2018-07-23 06:45:24 +08:00
|
|
|
os << '\n';
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void CFGFunctionPrinter::print(const Instruction *inst) {
|
2018-06-24 07:03:42 +08:00
|
|
|
switch (inst->getKind()) {
|
2018-06-29 11:45:33 +08:00
|
|
|
case Instruction::Kind::Operation:
|
|
|
|
return print(cast<OperationInst>(inst));
|
2018-06-25 02:18:29 +08:00
|
|
|
case TerminatorInst::Kind::Branch:
|
2018-06-29 11:45:33 +08:00
|
|
|
return print(cast<BranchInst>(inst));
|
2018-06-24 07:03:42 +08:00
|
|
|
case TerminatorInst::Kind::Return:
|
2018-06-29 11:45:33 +08:00
|
|
|
return print(cast<ReturnInst>(inst));
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void CFGFunctionPrinter::print(const OperationInst *inst) {
|
2018-07-10 08:42:46 +08:00
|
|
|
printOperation(inst);
|
2018-07-19 06:31:25 +08:00
|
|
|
}
|
2018-07-21 00:35:47 +08:00
|
|
|
void CFGFunctionPrinter::print(const BranchInst *inst) {
|
2018-07-15 15:06:54 +08:00
|
|
|
os << " br bb" << getBBID(inst->getDest());
|
2018-06-29 11:45:33 +08:00
|
|
|
}
|
2018-07-22 05:32:09 +08:00
|
|
|
void CFGFunctionPrinter::print(const ReturnInst *inst) {
|
|
|
|
os << " return";
|
|
|
|
|
|
|
|
if (inst->getNumOperands() != 0)
|
|
|
|
os << ' ';
|
|
|
|
|
|
|
|
// TODO: Use getOperands() when we have it.
|
|
|
|
interleaveComma(inst->getInstOperands(), [&](const InstOperand &operand) {
|
|
|
|
printValueID(operand.get());
|
|
|
|
os << " : ";
|
|
|
|
ModulePrinter::print(operand.get()->getType());
|
|
|
|
});
|
|
|
|
}
|
2018-07-19 01:16:05 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const CFGFunction *fn) {
|
|
|
|
CFGFunctionPrinter(fn, *this).print();
|
2018-06-29 11:45:33 +08:00
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-04 08:51:28 +08:00
|
|
|
// ML Function printing
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-04 08:51:28 +08:00
|
|
|
namespace {
|
2018-07-21 00:35:47 +08:00
|
|
|
class MLFunctionPrinter : public FunctionState {
|
2018-07-04 08:51:28 +08:00
|
|
|
public:
|
2018-07-21 00:35:47 +08:00
|
|
|
MLFunctionPrinter(const MLFunction *function, const ModulePrinter &other);
|
2018-07-04 08:51:28 +08:00
|
|
|
|
|
|
|
const MLFunction *getFunction() const { return function; }
|
|
|
|
|
2018-07-14 04:03:13 +08:00
|
|
|
// Prints ML function
|
2018-07-04 08:51:28 +08:00
|
|
|
void print();
|
|
|
|
|
2018-07-14 04:03:13 +08:00
|
|
|
// Methods to print ML function statements
|
2018-07-04 08:51:28 +08:00
|
|
|
void print(const Statement *stmt);
|
2018-07-10 08:42:46 +08:00
|
|
|
void print(const OperationStmt *stmt);
|
2018-07-04 08:51:28 +08:00
|
|
|
void print(const ForStmt *stmt);
|
|
|
|
void print(const IfStmt *stmt);
|
2018-07-14 04:03:13 +08:00
|
|
|
void print(const StmtBlock *block);
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2018-07-14 04:03:13 +08:00
|
|
|
// Number of spaces used for indenting nested statements
|
|
|
|
const static unsigned indentWidth = 2;
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2018-07-14 04:03:13 +08:00
|
|
|
private:
|
2018-07-04 08:51:28 +08:00
|
|
|
const MLFunction *function;
|
|
|
|
int numSpaces;
|
|
|
|
};
|
2018-07-19 01:16:05 +08:00
|
|
|
} // end anonymous namespace
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
MLFunctionPrinter::MLFunctionPrinter(const MLFunction *function,
|
|
|
|
const ModulePrinter &other)
|
|
|
|
: FunctionState(other), function(function), numSpaces(0) {}
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void MLFunctionPrinter::print() {
|
2018-07-04 08:51:28 +08:00
|
|
|
os << "mlfunc ";
|
|
|
|
// FIXME: should print argument names rather than just signature
|
2018-07-21 00:35:47 +08:00
|
|
|
printFunctionSignature(function);
|
2018-07-04 08:51:28 +08:00
|
|
|
os << " {\n";
|
2018-07-14 04:03:13 +08:00
|
|
|
print(function);
|
2018-07-04 08:51:28 +08:00
|
|
|
os << " return\n";
|
|
|
|
os << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void MLFunctionPrinter::print(const StmtBlock *block) {
|
2018-07-14 04:03:13 +08:00
|
|
|
numSpaces += indentWidth;
|
2018-07-15 15:06:54 +08:00
|
|
|
for (auto &stmt : block->getStatements()) {
|
2018-07-14 04:03:13 +08:00
|
|
|
print(&stmt);
|
2018-07-15 15:06:54 +08:00
|
|
|
os << "\n";
|
|
|
|
}
|
2018-07-14 04:03:13 +08:00
|
|
|
numSpaces -= indentWidth;
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void MLFunctionPrinter::print(const Statement *stmt) {
|
2018-07-04 08:51:28 +08:00
|
|
|
switch (stmt->getKind()) {
|
2018-07-17 02:47:09 +08:00
|
|
|
case Statement::Kind::Operation:
|
|
|
|
return print(cast<OperationStmt>(stmt));
|
2018-07-04 08:51:28 +08:00
|
|
|
case Statement::Kind::For:
|
|
|
|
return print(cast<ForStmt>(stmt));
|
|
|
|
case Statement::Kind::If:
|
|
|
|
return print(cast<IfStmt>(stmt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void MLFunctionPrinter::print(const OperationStmt *stmt) {
|
|
|
|
printOperation(stmt);
|
|
|
|
}
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void MLFunctionPrinter::print(const ForStmt *stmt) {
|
2018-07-20 00:52:39 +08:00
|
|
|
os.indent(numSpaces) << "for x = " << *stmt->getLowerBound();
|
|
|
|
os << " to " << *stmt->getUpperBound();
|
|
|
|
if (stmt->getStep()->getValue() != 1)
|
|
|
|
os << " step " << *stmt->getStep();
|
|
|
|
|
|
|
|
os << " {\n";
|
2018-07-14 04:03:13 +08:00
|
|
|
print(static_cast<const StmtBlock *>(stmt));
|
2018-07-17 02:47:09 +08:00
|
|
|
os.indent(numSpaces) << "}";
|
2018-07-04 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void MLFunctionPrinter::print(const IfStmt *stmt) {
|
2018-07-14 04:03:13 +08:00
|
|
|
os.indent(numSpaces) << "if () {\n";
|
|
|
|
print(stmt->getThenClause());
|
|
|
|
os.indent(numSpaces) << "}";
|
|
|
|
if (stmt->hasElseClause()) {
|
|
|
|
os << " else {\n";
|
|
|
|
print(stmt->getElseClause());
|
|
|
|
os.indent(numSpaces) << "}";
|
|
|
|
}
|
2018-07-04 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void ModulePrinter::print(const MLFunction *fn) {
|
|
|
|
MLFunctionPrinter(fn, *this).print();
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 08:51:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// print and dump methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-29 11:45:33 +08:00
|
|
|
|
2018-07-19 07:29:21 +08:00
|
|
|
void Attribute::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(/*no context is known*/ nullptr);
|
|
|
|
ModulePrinter(os, state).print(this);
|
2018-07-19 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Attribute::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:56:54 +08:00
|
|
|
void Type::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(getContext());
|
|
|
|
ModulePrinter(os, state).print(this);
|
2018-07-18 07:56:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Type::dump() const { print(llvm::errs()); }
|
2018-07-18 07:56:54 +08:00
|
|
|
|
2018-07-17 00:45:22 +08:00
|
|
|
void AffineMap::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
2018-07-10 00:00:25 +08:00
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
void AffineExpr::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
|
2018-06-30 09:09:29 +08:00
|
|
|
void AffineExpr::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(/*no context is known*/ nullptr);
|
|
|
|
ModulePrinter(os, state).print(this);
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AffineMap::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(/*no context is known*/ nullptr);
|
|
|
|
ModulePrinter(os, state).print(this);
|
|
|
|
}
|
2018-07-12 12:31:07 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void Instruction::print(raw_ostream &os) const {
|
|
|
|
ModuleState state(getFunction()->getContext());
|
|
|
|
ModulePrinter modulePrinter(os, state);
|
|
|
|
CFGFunctionPrinter(getFunction(), modulePrinter).print(this);
|
|
|
|
}
|
2018-07-12 12:31:07 +08:00
|
|
|
|
2018-07-21 00:35:47 +08:00
|
|
|
void Instruction::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
void BasicBlock::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(getFunction()->getContext());
|
|
|
|
ModulePrinter modulePrinter(os, state);
|
|
|
|
CFGFunctionPrinter(getFunction(), modulePrinter).print(this);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void BasicBlock::dump() const { print(llvm::errs()); }
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-04 08:51:28 +08:00
|
|
|
void Statement::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(getFunction()->getContext());
|
|
|
|
ModulePrinter modulePrinter(os, state);
|
|
|
|
MLFunctionPrinter(getFunction(), modulePrinter).print(this);
|
2018-06-29 08:02:32 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Statement::dump() const { print(llvm::errs()); }
|
2018-07-15 15:06:54 +08:00
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
void Function::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(getContext());
|
|
|
|
ModulePrinter(os, state).print(this);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Function::dump() const { print(llvm::errs()); }
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
void Module::print(raw_ostream &os) const {
|
2018-07-21 00:35:47 +08:00
|
|
|
ModuleState state(getContext());
|
|
|
|
state.initialize(this);
|
|
|
|
ModulePrinter(os, state).print(this);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 01:16:05 +08:00
|
|
|
void Module::dump() const { print(llvm::errs()); }
|