[llvm] 28d95a2 - [llvm-reduce] Allow writing temporary files as bitcode.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 16 04:40:01 PST 2021


Author: Florian Hahn
Date: 2021-11-16T12:39:42Z
New Revision: 28d95a26109ece40f675d6a78477a820488bcece

URL: https://github.com/llvm/llvm-project/commit/28d95a26109ece40f675d6a78477a820488bcece
DIFF: https://github.com/llvm/llvm-project/commit/28d95a26109ece40f675d6a78477a820488bcece.diff

LOG: [llvm-reduce] Allow writing temporary files as bitcode.

Textual LLVM IR files are much bigger and take longer to write to disk.
To avoid the extra cost incurred by serializing to text, this patch adds
an option to save temporary files as bitcode instead.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D113858

Added: 
    llvm/test/tools/llvm-reduce/Inputs/llvm-dis-and-filecheck.py
    llvm/test/tools/llvm-reduce/temporary-files-as-bitcode.ll

Modified: 
    llvm/tools/llvm-reduce/deltas/Delta.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-reduce/Inputs/llvm-dis-and-filecheck.py b/llvm/test/tools/llvm-reduce/Inputs/llvm-dis-and-filecheck.py
new file mode 100755
index 0000000000000..9fa1363462327
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/Inputs/llvm-dis-and-filecheck.py
@@ -0,0 +1,29 @@
+"""
+Script to disassembles a bitcode file and run FileCheck on the output with the
+provided arguments. The first 2 arguments are the paths to the llvm-dis and
+FileCheck binaries, followed by arguments to be passed to FileCheck. The last
+argument is the bitcode file to disassemble.
+
+Usage:
+    python llvm-dis-and-filecheck.py
+      <path to llvm-dis> <path to FileCheck>
+      [arguments passed to FileCheck] <path to bitcode file>
+
+"""
+
+
+import sys
+import subprocess
+
+llvm_dis = sys.argv[1]
+filecheck = sys.argv[2]
+filecheck_args = [filecheck, ]
+filecheck_args.extend(sys.argv[3:-1])
+bitcode_file = sys.argv[-1]
+
+disassemble = subprocess.Popen([llvm_dis, "-o", "-", bitcode_file],
+        stdout=subprocess.PIPE)
+check = subprocess.Popen(filecheck_args, stdin=disassemble.stdout)
+disassemble.stdout.close()
+check.communicate()
+sys.exit(check.returncode)

diff  --git a/llvm/test/tools/llvm-reduce/temporary-files-as-bitcode.ll b/llvm/test/tools/llvm-reduce/temporary-files-as-bitcode.ll
new file mode 100644
index 0000000000000..06cf789d04fc1
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/temporary-files-as-bitcode.ll
@@ -0,0 +1,18 @@
+; RUN: llvm-reduce -write-tmp-files-as-bitcode --delta-passes=basic-blocks %s -o %t \
+; RUN:     --test %python --test-arg %p/Inputs/llvm-dis-and-filecheck.py --test-arg llvm-dis --test-arg FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s
+; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s
+
+; CHECK-INTERESTINGNESS: @callee(
+; CHECK-FINAL: declare void @callee()
+define void @callee() {
+  ret void
+}
+
+; CHECK-ALL: define void @caller()
+define void @caller() {
+entry:
+; CHECK-ALL: call void @callee()
+; CHECK-ALL: ret void
+  call void @callee()
+  ret void
+}

diff  --git a/llvm/tools/llvm-reduce/deltas/Delta.cpp b/llvm/tools/llvm-reduce/deltas/Delta.cpp
index f4f77c6310d6d..7aa01672a0779 100644
--- a/llvm/tools/llvm-reduce/deltas/Delta.cpp
+++ b/llvm/tools/llvm-reduce/deltas/Delta.cpp
@@ -15,6 +15,7 @@
 #include "Delta.h"
 #include "ReducerWorkItem.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ToolOutputFile.h"
@@ -31,6 +32,11 @@ static cl::opt<unsigned int> StartingGranularityLevel(
     "starting-granularity-level",
     cl::desc("Number of times to divide chunks prior to first test"));
 
+static cl::opt<bool> TmpFilesAsBitcode(
+    "write-tmp-files-as-bitcode",
+    cl::desc("Write temporary files as bitcode, instead of textual IR"),
+    cl::init(false));
+
 void writeOutput(ReducerWorkItem &M, llvm::StringRef Message);
 
 bool isReduced(ReducerWorkItem &M, TestRunner &Test,
@@ -38,12 +44,26 @@ bool isReduced(ReducerWorkItem &M, TestRunner &Test,
   // Write ReducerWorkItem to tmp file
   int FD;
   std::error_code EC = sys::fs::createTemporaryFile(
-      "llvm-reduce", M.isMIR() ? "mir" : "ll", FD, CurrentFilepath);
+      "llvm-reduce", M.isMIR() ? "mir" : (TmpFilesAsBitcode ? "bc" : "ll"), FD,
+      CurrentFilepath);
   if (EC) {
     errs() << "Error making unique filename: " << EC.message() << "!\n";
     exit(1);
   }
 
+  if (TmpFilesAsBitcode) {
+    llvm::raw_fd_ostream OutStream(FD, true);
+    WriteBitcodeToFile(M, OutStream);
+    OutStream.close();
+    if (OutStream.has_error()) {
+      errs() << "Error emitting bitcode to file '" << CurrentFilepath << "'!\n";
+      sys::fs::remove(CurrentFilepath);
+      exit(1);
+    }
+    bool Res = Test.run(CurrentFilepath);
+    sys::fs::remove(CurrentFilepath);
+    return Res;
+  }
   ToolOutputFile Out(CurrentFilepath, FD);
   M.print(Out.os(), /*AnnotationWriter=*/nullptr);
   Out.os().close();


        


More information about the llvm-commits mailing list