[PATCH] D126826: [BOLT][NFC] Replacing stdio functions with raw_ostream functions

Huan Nguyen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 1 14:14:19 PDT 2022


nhuhuan created this revision.
Herald added a reviewer: rafauler.
Herald added a subscriber: ayermolo.
Herald added a reviewer: Amir.
Herald added a reviewer: maksfb.
Herald added a project: All.
nhuhuan requested review of this revision.
Herald added subscribers: llvm-commits, yota9.
Herald added a project: LLVM.

Replacing stdio functions, e.g., fopen, fprintf, with raw_ostream.

Test Plan:

  ninja check-bolt


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126826

Files:
  bolt/include/bolt/Passes/CallGraph.h


Index: bolt/include/bolt/Passes/CallGraph.h
===================================================================
--- bolt/include/bolt/Passes/CallGraph.h
+++ bolt/include/bolt/Passes/CallGraph.h
@@ -9,9 +9,14 @@
 #ifndef BOLT_PASSES_CALLGRAPH_H
 #define BOLT_PASSES_CALLGRAPH_H
 
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/raw_ostream.h"
 #include <cassert>
+#include <cmath>
 #include <cstdint>
 #include <cstdio>
+#include <iomanip>
+#include <string>
 #include <unordered_set>
 #include <vector>
 
@@ -160,31 +165,33 @@
 };
 
 template <class L> void CallGraph::printDot(char *FileName, L GetLabel) const {
-  FILE *File = fopen(FileName, "wt");
-  if (!File)
+  std::error_code EC;
+  raw_fd_ostream OS(std::string(FileName), EC, llvm::sys::fs::OF_None);
+  if (EC)
     return;
 
-  fprintf(File, "digraph g {\n");
-  for (NodeId F = 0; F < Nodes.size(); F++) {
+  OS << "digraph g {\n";
+  for (auto F = 0; F < Nodes.size(); ++F) {
     if (Nodes[F].samples() == 0)
       continue;
-    fprintf(File, "f%lu [label=\"%s\\nsamples=%u\\nsize=%u\"];\n", F,
-            GetLabel(F), Nodes[F].samples(), Nodes[F].size());
+    OS << "f" << (unsigned long)F << " [label=\"" << GetLabel(F)
+       << "\\nsamples=" << (unsigned int)(Nodes[F].samples())
+       << "\\nsize=" << (unsigned int)(Nodes[F].size()) << "\"];\n";
   }
-  for (NodeId F = 0; F < Nodes.size(); F++) {
+
+  for (auto F = 0; F < Nodes.size(); ++F) {
     if (Nodes[F].samples() == 0)
       continue;
-    for (NodeId Dst : Nodes[F].successors()) {
+    for (auto Dst : Nodes[F].successors()) {
       ArcConstIterator Arc = findArc(F, Dst);
-      fprintf(
-          File,
-          "f%lu -> f%u [label=\"normWgt=%.3lf,weight=%.0lf,callOffset=%.1lf\"];"
-          "\n",
-          F, Dst, Arc->normalizedWeight(), Arc->weight(), Arc->avgCallOffset());
+      OS << "f" << (unsigned long)F << " -> f" << (unsigned)Dst
+         << " [label=\"normWgt=" << (double)(Arc->normalizedWeight())
+         << ",weight=" << std::round((double)(Arc->weight()))
+         << ",callOffset=" << (double)(Arc->avgCallOffset()) << "\"];\n";
     }
   }
-  fprintf(File, "}\n");
-  fclose(File);
+  OS << "}\n";
+  OS.close();
 }
 
 } // namespace bolt


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126826.433546.patch
Type: text/x-patch
Size: 2231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220601/eec247b2/attachment.bin>


More information about the llvm-commits mailing list