[PATCH] D74920: emit bitcode to file when Verifier fails

Jameson Nash via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 20 11:33:36 PST 2020


vtjnash created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74920

Files:
  llvm/lib/IR/Verifier.cpp


Index: llvm/lib/IR/Verifier.cpp
===================================================================
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -5118,6 +5118,49 @@
            "inconsistent use of embedded source");
 }
 
+static void report_broken(Module *M, Function *F) {
+    std::string message = F ?
+            "Broken function found, compilation aborted!" :
+            "Broken module found, compilation aborted!";
+    if (!M) {
+        M = new Module(F->getName(), F->getContext());
+        M->getFunctionList().push_back(F);
+    }
+    int FD;
+    SmallString<128> GeneratedUniqPath;
+    // Create a template based on some characteristics of the failure,
+    // mangled and chopped to be safe for most file systems
+    std::string Pattern((F && F->hasName() ? F->getName() : M->getName()).slice(0, 32));
+    auto isunsafe = [](unsigned char C) {
+        return !(('A' <= C && C <= 'Z') ||
+                 ('a' <= C && C <= 'z') ||
+                 ('0' <= C && C <= '9') ||
+                 C == '-' || C == '_');
+    };
+    std::replace_if(Pattern.begin(), Pattern.end(), isunsafe, '_');
+    Pattern = "llvm-" + Pattern + "-%%%%%%.ll";
+    if (!sys::fs::createUniqueFile(Pattern, FD, GeneratedUniqPath)) {
+        raw_fd_ostream OS(FD, true, false);
+        // We'd like to use this, but linking can't handle it,
+        // so instead, we'll use the text format.
+        // WriteBitcodeToFile(*M, OS);
+        M->print(OS, nullptr, true, true);
+        message += "\nBroken bitcode dumped to \"";
+        message += GeneratedUniqPath;
+        message += "\"";
+    }
+    report_fatal_error(message);
+}
+
+static void report_broken(Function &F) {
+    report_broken(F.getParent(), &F);
+}
+
+static void report_broken(Module &M) {
+    report_broken(&M, nullptr);
+}
+
+
 //===----------------------------------------------------------------------===//
 //  Implement the public interfaces to this file...
 //===----------------------------------------------------------------------===//
@@ -5176,7 +5219,7 @@
   bool runOnFunction(Function &F) override {
     if (!V->verify(F) && FatalErrors) {
       errs() << "in function " << F.getName() << '\n'; 
-      report_fatal_error("Broken function found, compilation aborted!");
+      report_broken(F);
     }
     return false;
   }
@@ -5189,7 +5232,7 @@
 
     HasErrors |= !V->verify();
     if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
-      report_fatal_error("Broken module found, compilation aborted!");
+      report_broken(M);
     return false;
   }
 
@@ -5569,7 +5612,7 @@
 PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
   auto Res = AM.getResult<VerifierAnalysis>(M);
   if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
-    report_fatal_error("Broken module found, compilation aborted!");
+    report_broken(M);
 
   return PreservedAnalyses::all();
 }
@@ -5577,7 +5620,7 @@
 PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
   auto res = AM.getResult<VerifierAnalysis>(F);
   if (res.IRBroken && FatalErrors)
-    report_fatal_error("Broken function found, compilation aborted!");
+    report_broken(F);
 
   return PreservedAnalyses::all();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74920.245702.patch
Type: text/x-patch
Size: 3250 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200220/6a2a4fa7/attachment-0001.bin>


More information about the llvm-commits mailing list