ScopGraphPrinter.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //===- GraphPrinter.cpp - Create a DOT output describing the Scop. --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Create a DOT output describing the Scop.
  10. //
  11. // For each function a dot file is created that shows the control flow graph of
  12. // the function and highlights the detected Scops.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "polly/LinkAllPasses.h"
  16. #include "polly/ScopDetection.h"
  17. #include "polly/Support/ScopLocation.h"
  18. #include "llvm/Analysis/DOTGraphTraitsPass.h"
  19. #include "llvm/Analysis/RegionInfo.h"
  20. #include "llvm/Analysis/RegionIterator.h"
  21. #include "llvm/Support/CommandLine.h"
  22. using namespace polly;
  23. using namespace llvm;
  24. static cl::opt<std::string>
  25. ViewFilter("polly-view-only",
  26. cl::desc("Only view functions that match this pattern"),
  27. cl::Hidden, cl::init(""), cl::ZeroOrMore);
  28. static cl::opt<bool> ViewAll("polly-view-all",
  29. cl::desc("Also show functions without any scops"),
  30. cl::Hidden, cl::init(false), cl::ZeroOrMore);
  31. namespace llvm {
  32. template <>
  33. struct GraphTraits<ScopDetection *> : public GraphTraits<RegionInfo *> {
  34. static NodeRef getEntryNode(ScopDetection *SD) {
  35. return GraphTraits<RegionInfo *>::getEntryNode(SD->getRI());
  36. }
  37. static nodes_iterator nodes_begin(ScopDetection *SD) {
  38. return nodes_iterator::begin(getEntryNode(SD));
  39. }
  40. static nodes_iterator nodes_end(ScopDetection *SD) {
  41. return nodes_iterator::end(getEntryNode(SD));
  42. }
  43. };
  44. template <>
  45. struct GraphTraits<ScopDetectionWrapperPass *>
  46. : public GraphTraits<ScopDetection *> {
  47. static NodeRef getEntryNode(ScopDetectionWrapperPass *P) {
  48. return GraphTraits<ScopDetection *>::getEntryNode(&P->getSD());
  49. }
  50. static nodes_iterator nodes_begin(ScopDetectionWrapperPass *P) {
  51. return nodes_iterator::begin(getEntryNode(P));
  52. }
  53. static nodes_iterator nodes_end(ScopDetectionWrapperPass *P) {
  54. return nodes_iterator::end(getEntryNode(P));
  55. }
  56. };
  57. template <> struct DOTGraphTraits<RegionNode *> : public DefaultDOTGraphTraits {
  58. DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
  59. std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
  60. if (!Node->isSubRegion()) {
  61. BasicBlock *BB = Node->getNodeAs<BasicBlock>();
  62. if (isSimple())
  63. return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(BB, nullptr);
  64. else
  65. return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(BB, nullptr);
  66. }
  67. return "Not implemented";
  68. }
  69. };
  70. template <>
  71. struct DOTGraphTraits<ScopDetectionWrapperPass *>
  72. : public DOTGraphTraits<RegionNode *> {
  73. DOTGraphTraits(bool isSimple = false)
  74. : DOTGraphTraits<RegionNode *>(isSimple) {}
  75. static std::string getGraphName(ScopDetectionWrapperPass *SD) {
  76. return "Scop Graph";
  77. }
  78. std::string getEdgeAttributes(RegionNode *srcNode,
  79. GraphTraits<RegionInfo *>::ChildIteratorType CI,
  80. ScopDetectionWrapperPass *P) {
  81. RegionNode *destNode = *CI;
  82. auto *SD = &P->getSD();
  83. if (srcNode->isSubRegion() || destNode->isSubRegion())
  84. return "";
  85. // In case of a backedge, do not use it to define the layout of the nodes.
  86. BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
  87. BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
  88. RegionInfo *RI = SD->getRI();
  89. Region *R = RI->getRegionFor(destBB);
  90. while (R && R->getParent())
  91. if (R->getParent()->getEntry() == destBB)
  92. R = R->getParent();
  93. else
  94. break;
  95. if (R && R->getEntry() == destBB && R->contains(srcBB))
  96. return "constraint=false";
  97. return "";
  98. }
  99. std::string getNodeLabel(RegionNode *Node, ScopDetectionWrapperPass *P) {
  100. return DOTGraphTraits<RegionNode *>::getNodeLabel(
  101. Node, reinterpret_cast<RegionNode *>(
  102. P->getSD().getRI()->getTopLevelRegion()));
  103. }
  104. static std::string escapeString(std::string String) {
  105. std::string Escaped;
  106. for (const auto &C : String) {
  107. if (C == '"')
  108. Escaped += '\\';
  109. Escaped += C;
  110. }
  111. return Escaped;
  112. }
  113. // Print the cluster of the subregions. This groups the single basic blocks
  114. // and adds a different background color for each group.
  115. static void printRegionCluster(ScopDetection *SD, const Region *R,
  116. raw_ostream &O, unsigned depth = 0) {
  117. O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void *>(R)
  118. << " {\n";
  119. unsigned LineBegin, LineEnd;
  120. std::string FileName;
  121. getDebugLocation(R, LineBegin, LineEnd, FileName);
  122. std::string Location;
  123. if (LineBegin != (unsigned)-1) {
  124. Location = escapeString(FileName + ":" + std::to_string(LineBegin) + "-" +
  125. std::to_string(LineEnd) + "\n");
  126. }
  127. std::string ErrorMessage = SD->regionIsInvalidBecause(R);
  128. ErrorMessage = escapeString(ErrorMessage);
  129. O.indent(2 * (depth + 1))
  130. << "label = \"" << Location << ErrorMessage << "\";\n";
  131. if (SD->isMaxRegionInScop(*R)) {
  132. O.indent(2 * (depth + 1)) << "style = filled;\n";
  133. // Set color to green.
  134. O.indent(2 * (depth + 1)) << "color = 3";
  135. } else {
  136. O.indent(2 * (depth + 1)) << "style = solid;\n";
  137. int color = (R->getDepth() * 2 % 12) + 1;
  138. // We do not want green again.
  139. if (color == 3)
  140. color = 6;
  141. O.indent(2 * (depth + 1)) << "color = " << color << "\n";
  142. }
  143. for (const auto &SubRegion : *R)
  144. printRegionCluster(SD, SubRegion.get(), O, depth + 1);
  145. RegionInfo *RI = R->getRegionInfo();
  146. for (BasicBlock *BB : R->blocks())
  147. if (RI->getRegionFor(BB) == R)
  148. O.indent(2 * (depth + 1))
  149. << "Node"
  150. << static_cast<void *>(RI->getTopLevelRegion()->getBBNode(BB))
  151. << ";\n";
  152. O.indent(2 * depth) << "}\n";
  153. }
  154. static void
  155. addCustomGraphFeatures(const ScopDetectionWrapperPass *SD,
  156. GraphWriter<ScopDetectionWrapperPass *> &GW) {
  157. raw_ostream &O = GW.getOStream();
  158. O << "\tcolorscheme = \"paired12\"\n";
  159. printRegionCluster(&SD->getSD(), SD->getSD().getRI()->getTopLevelRegion(),
  160. O, 4);
  161. }
  162. };
  163. } // end namespace llvm
  164. struct ScopViewer
  165. : public DOTGraphTraitsViewer<ScopDetectionWrapperPass, false> {
  166. static char ID;
  167. ScopViewer()
  168. : DOTGraphTraitsViewer<ScopDetectionWrapperPass, false>("scops", ID) {}
  169. bool processFunction(Function &F, ScopDetectionWrapperPass &SD) override {
  170. if (ViewFilter != "" && !F.getName().count(ViewFilter))
  171. return false;
  172. if (ViewAll)
  173. return true;
  174. // Check that at least one scop was detected.
  175. return std::distance(SD.getSD().begin(), SD.getSD().end()) > 0;
  176. }
  177. };
  178. char ScopViewer::ID = 0;
  179. struct ScopOnlyViewer
  180. : public DOTGraphTraitsViewer<ScopDetectionWrapperPass, true> {
  181. static char ID;
  182. ScopOnlyViewer()
  183. : DOTGraphTraitsViewer<ScopDetectionWrapperPass, true>("scopsonly", ID) {}
  184. };
  185. char ScopOnlyViewer::ID = 0;
  186. struct ScopPrinter
  187. : public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false> {
  188. static char ID;
  189. ScopPrinter()
  190. : DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false>("scops", ID) {}
  191. };
  192. char ScopPrinter::ID = 0;
  193. struct ScopOnlyPrinter
  194. : public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true> {
  195. static char ID;
  196. ScopOnlyPrinter()
  197. : DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true>("scopsonly", ID) {
  198. }
  199. };
  200. char ScopOnlyPrinter::ID = 0;
  201. static RegisterPass<ScopViewer> X("view-scops",
  202. "Polly - View Scops of function");
  203. static RegisterPass<ScopOnlyViewer>
  204. Y("view-scops-only",
  205. "Polly - View Scops of function (with no function bodies)");
  206. static RegisterPass<ScopPrinter> M("dot-scops",
  207. "Polly - Print Scops of function");
  208. static RegisterPass<ScopOnlyPrinter>
  209. N("dot-scops-only",
  210. "Polly - Print Scops of function (with no function bodies)");
  211. Pass *polly::createDOTViewerPass() { return new ScopViewer(); }
  212. Pass *polly::createDOTOnlyViewerPass() { return new ScopOnlyViewer(); }
  213. Pass *polly::createDOTPrinterPass() { return new ScopPrinter(); }
  214. Pass *polly::createDOTOnlyPrinterPass() { return new ScopOnlyPrinter(); }