[llvm] Add option to dump IR to files intstead of stderr (PR #66412)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 23:26:09 PDT 2023


================
@@ -684,9 +686,64 @@ PrintIRInstrumentation::~PrintIRInstrumentation() {
   assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit");
 }
 
-void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
+static SmallString<32> getIRDisplayName(Any IR) {
+
+  auto hashName = [](StringRef name) {
+    const size_t hashValue = hash_value(name);
+    return std::to_string(hashValue);
+  };
+
+  SmallString<32> Result;
+  const Module *M = unwrapModule(IR);
+  std::string ModuleName = hashName(M->getName());
+  SmallString<32> IRName;
+  if (any_cast<const Module *>(&IR)) {
+    IRName += "-module";
+  } else if (const Function **F = any_cast<const Function *>(&IR)) {
+    IRName += "-function-";
+    IRName += hashName((*F)->getName());
+  } else if (const LazyCallGraph::SCC **C =
+                 any_cast<const LazyCallGraph::SCC *>(&IR)) {
+    IRName += "-scc-";
+    IRName += hashName((*C)->getName());
+  } else if (const Loop **L = any_cast<const Loop *>(&IR)) {
+    IRName += "-loop-";
+    IRName += hashName((*L)->getName());
+  } else {
+    llvm_unreachable("Unknown wrapped IR type");
+  }
+
+  Result += ModuleName;
+  Result += IRName;
+  return Result;
+}
+
+SmallString<128> PrintIRInstrumentation::fetchDumpFilename(StringRef PassID,
+                                                           Any IR) {
+  const std::string &RootDirectory = irDumpDirectory();
+  assert(!RootDirectory.empty() &&
+         "The flag -ir-dump-directory must be passed to dump IR to files");
+  SmallString<128> ResultPath;
+  ResultPath += RootDirectory;
+  SmallString<16> Filename;
+  Filename += std::to_string(CurrentPassNumber);
----------------
arsenm wrote:

Use raw_svector_ostream?

https://github.com/llvm/llvm-project/pull/66412


More information about the llvm-commits mailing list