Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Handshake] Improve handshake .dot graph output #2038

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 138 additions & 9 deletions lib/Conversion/StandardToHandshake/StandardToHandshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,117 @@ void removeBasicBlocks(handshake::FuncOp funcOp) {
}
}

static bool isControlOp(Operation *op) {
return op->hasAttr("control") &&
op->getAttrOfType<BoolAttr>("control").getValue();
}

static void dotPrintNode(llvm::raw_fd_ostream &outfile, Operation *op,
DenseMap<Operation *, unsigned> &opIDs) {
outfile << "\t\t";
outfile << "\"" + op->getName().getStringRef().str() + "_" +
to_string(opIDs[op]) + "\"";
outfile << " [";

/// Fill color
outfile << "fillcolor = ";
outfile
<< llvm::TypeSwitch<Operation *, std::string>(op)
.Case<handshake::ForkOp, handshake::LazyForkOp, handshake::MuxOp,
handshake::JoinOp>([&](auto) { return "lavender"; })
.Case<handshake::BufferOp>([&](auto) { return "lightgreen"; })
.Case<handshake::ReturnOp>([&](auto) { return "gold"; })
.Case<handshake::SinkOp, handshake::ConstantOp>(
[&](auto) { return "gainsboro"; })
.Case<handshake::MemoryOp, handshake::LoadOp, handshake::StoreOp>(
[&](auto) { return "coral"; })
.Case<handshake::MergeOp, handshake::ControlMergeOp,
handshake::BranchOp, handshake::ConditionalBranchOp>(
[&](auto) { return "lightblue"; })
.Default([&](auto) { return "moccasin"; });

/// Shape
outfile << ", shape=";
if (op->getDialect()->getNamespace() == "handshake")
outfile << "box";
else
outfile << "oval";

/// Label
outfile << ", label=\"";
outfile << llvm::TypeSwitch<Operation *, std::string>(op)
.Case<handshake::ConstantOp>([&](auto op) {
return std::to_string(
op->template getAttrOfType<mlir::IntegerAttr>("value")
.getValue()
.getSExtValue());
})
.Case<handshake::ControlMergeOp>(
[&](auto) { return "cmerge"; })
.Case<handshake::ConditionalBranchOp>(
[&](auto) { return "cbranch"; })
.Case<arith::AddIOp>([&](auto) { return "+"; })
.Case<arith::SubIOp>([&](auto) { return "-"; })
.Case<arith::AndIOp>([&](auto) { return "&"; })
.Case<arith::OrIOp>([&](auto) { return "|"; })
.Case<arith::XOrIOp>([&](auto) { return "^"; })
.Case<arith::MulIOp>([&](auto) { return "*"; })
.Case<arith::ShRSIOp, arith::ShRUIOp>(
[&](auto) { return ">>"; })
.Case<arith::ShLIOp>([&](auto) { return "<<"; })
.Case<arith::CmpIOp>([&](arith::CmpIOp op) {
switch (op.predicate()) {
case arith::CmpIPredicate::eq:
return "==";
case arith::CmpIPredicate::ne:
return "!=";
case arith::CmpIPredicate::uge:
case arith::CmpIPredicate::sge:
return ">=";
case arith::CmpIPredicate::ugt:
case arith::CmpIPredicate::sgt:
return ">";
case arith::CmpIPredicate::ule:
case arith::CmpIPredicate::sle:
return "<=";
case arith::CmpIPredicate::ult:
case arith::CmpIPredicate::slt:
return "<";
}
llvm_unreachable("unhandled cmpi predicate");
})
.Default([&](auto op) {
auto opDialect = op->getDialect()->getNamespace();
std::string label = op->getName().getStringRef().str();
if (opDialect == "handshake")
label.erase(0, StringLiteral("handshake.").size());

return label;
});
outfile << "\"";

/// Style; add dashed border for control nodes
outfile << ", style=\"filled";
if (isControlOp(op))
outfile << ", dashed";

outfile << "\"";

outfile << "]\n";
}

/// Returns true if v is used as a control operand in op
static bool isControlOperand(Operation *op, Value v) {
if (isControlOp(op))
return true;

return llvm::TypeSwitch<Operation *, bool>(op)
.Case<handshake::MuxOp, handshake::ConditionalBranchOp>(
[&](auto op) { return v == op.getOperand(0); })
.Case<handshake::ControlMergeOp>([&](auto) { return true; })
.Default([](auto) { return false; });
}

template <typename FuncOp>
void dotPrint(FuncOp f, StringRef name) {
// Prints DOT representation of the dataflow graph, used for debugging.
Expand All @@ -96,15 +207,12 @@ void dotPrint(FuncOp f, StringRef name) {

for (Block &block : f) {
outfile << "\tsubgraph cluster_" + to_string(blockIDs[&block]) + " {\n";
outfile << "\tcolor = \"darkgreen\";\n";
outfile << "\t\tlabel = \" block " + to_string(blockIDs[&block]) + "\";\n";
outfile << "\tnode [shape=box style=filled fillcolor=\"white\"]\n";
outfile << "\tcolor = \"darkgreen\"\n";
outfile << "\t\tlabel = \" block " + to_string(blockIDs[&block]) + "\"\n";

for (Operation &op : block) {
outfile << "\t\t";
outfile << "\"" + op.getName().getStringRef().str() + "_" +
to_string(opIDs[&op]) + "\"";
outfile << "\n";
}
for (Operation &op : block)
dotPrintNode(outfile, &op, opIDs);

for (Operation &op : block) {
if (op.getNumResults() == 0)
Expand All @@ -120,12 +228,33 @@ void dotPrint(FuncOp f, StringRef name) {
outfile << " -> ";
outfile << "\"" + useOp->getName().getStringRef().str() + "_" +
to_string(opIDs[useOp]) + "\"";

if (isControlOp(&op) || isControlOperand(useOp, result))
outfile << " [style=\"dashed\"]\n";

outfile << "\n";
}
}
}
}

/// Annotate block argument uses
for (auto barg : enumerate(block.getArguments())) {
std::string argName = "arg" + std::to_string(barg.index());
outfile << "\t\"" << argName << "\" [shape=diamond";
if (barg.index() == block.getNumArguments() - 1)
outfile << ", style=dashed";
outfile << "]\n";
for (auto useOp : barg.value().getUsers()) {
outfile << argName << " -> \""
<< useOp->getName().getStringRef().str() + "_" +
to_string(opIDs[useOp]) + "\"";
if (isControlOperand(useOp, barg.value()))
outfile << " [style=\"dashed\"]";
outfile << "\n";
}
}

outfile << "\t}\n";

for (Operation &op : block) {
Expand Down Expand Up @@ -1770,7 +1899,7 @@ struct HandshakeAnalysisPass
ModuleOp m = getOperation();

for (auto func : m.getOps<handshake::FuncOp>()) {
dotPrint(func, "output");
dotPrint(func, func.getName());

int count = 0;
int fork_count = 0;
Expand Down