diff options
author | Mehdi Amini <aminim@google.com> | 2019-12-24 02:47:41 +0000 |
---|---|---|
committer | Mehdi Amini <aminim@google.com> | 2019-12-24 02:47:41 +0000 |
commit | 0f0d0ed1c78f1a80139a1f2133fad5284691a121 (patch) | |
tree | 31979a3137c364e3eb58e0169a7c4029c7ee7db3 /mlir/lib/Transforms/ViewRegionGraph.cpp | |
parent | 6f635f90929da9545dd696071a829a1a42f84b30 (diff) | |
parent | 5b4a01d4a63cb66ab981e52548f940813393bf42 (diff) | |
download | bcm5719-llvm-0f0d0ed1c78f1a80139a1f2133fad5284691a121.tar.gz bcm5719-llvm-0f0d0ed1c78f1a80139a1f2133fad5284691a121.zip |
Import MLIR into the LLVM tree
Diffstat (limited to 'mlir/lib/Transforms/ViewRegionGraph.cpp')
-rw-r--r-- | mlir/lib/Transforms/ViewRegionGraph.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/mlir/lib/Transforms/ViewRegionGraph.cpp b/mlir/lib/Transforms/ViewRegionGraph.cpp new file mode 100644 index 00000000000..77111087d07 --- /dev/null +++ b/mlir/lib/Transforms/ViewRegionGraph.cpp @@ -0,0 +1,85 @@ +//===- ViewRegionGraph.cpp - View/write graphviz graphs -------------------===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "mlir/Transforms/ViewRegionGraph.h" +#include "mlir/IR/RegionGraphTraits.h" +#include "mlir/Pass/Pass.h" + +using namespace mlir; + +namespace llvm { + +// Specialize DOTGraphTraits to produce more readable output. +template <> struct DOTGraphTraits<Region *> : public DefaultDOTGraphTraits { + using DefaultDOTGraphTraits::DefaultDOTGraphTraits; + + static std::string getNodeLabel(Block *Block, Region *); +}; + +std::string DOTGraphTraits<Region *>::getNodeLabel(Block *Block, Region *) { + // Reuse the print output for the node labels. + std::string outStreamStr; + raw_string_ostream os(outStreamStr); + Block->print(os); + std::string &outStr = os.str(); + + if (outStr[0] == '\n') + outStr.erase(outStr.begin()); + + // Process string output to left justify the block. + for (unsigned i = 0; i != outStr.length(); ++i) { + if (outStr[i] == '\n') { + outStr[i] = '\\'; + outStr.insert(outStr.begin() + i + 1, 'l'); + } + } + + return outStr; +} + +} // end namespace llvm + +void mlir::viewGraph(Region ®ion, const Twine &name, bool shortNames, + const Twine &title, llvm::GraphProgram::Name program) { + llvm::ViewGraph(®ion, name, shortNames, title, program); +} + +raw_ostream &mlir::writeGraph(raw_ostream &os, Region ®ion, bool shortNames, + const Twine &title) { + return llvm::WriteGraph(os, ®ion, shortNames, title); +} + +void mlir::Region::viewGraph(const Twine ®ionName) { + ::mlir::viewGraph(*this, regionName); +} +void mlir::Region::viewGraph() { viewGraph("region"); } + +namespace { +struct PrintCFGPass : public FunctionPass<PrintCFGPass> { + PrintCFGPass(raw_ostream &os = llvm::errs(), bool shortNames = false, + const Twine &title = "") + : os(os), shortNames(shortNames), title(title.str()) {} + void runOnFunction() override { + mlir::writeGraph(os, getFunction().getBody(), shortNames, title); + } + +private: + raw_ostream &os; + bool shortNames; + std::string title; +}; +} // namespace + +std::unique_ptr<mlir::OpPassBase<mlir::FuncOp>> +mlir::createPrintCFGGraphPass(raw_ostream &os, bool shortNames, + const Twine &title) { + return std::make_unique<PrintCFGPass>(os, shortNames, title); +} + +static PassRegistration<PrintCFGPass> pass("print-cfg-graph", + "Print CFG graph per Function"); |