[llvm-reduce] Allow writing temporary files as bitcode.
authorFlorian Hahn <flo@fhahn.com>
Tue, 16 Nov 2021 12:39:32 +0000 (12:39 +0000)
committerFlorian Hahn <flo@fhahn.com>
Tue, 16 Nov 2021 12:39:42 +0000 (12:39 +0000)
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

llvm/test/tools/llvm-reduce/Inputs/llvm-dis-and-filecheck.py [new file with mode: 0755]
llvm/test/tools/llvm-reduce/temporary-files-as-bitcode.ll [new file with mode: 0644]
llvm/tools/llvm-reduce/deltas/Delta.cpp

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 (executable)
index 0000000..9fa1363
--- /dev/null
@@ -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 (file)
index 0000000..06cf789
--- /dev/null
@@ -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
+}
index f4f77c6..7aa0167 100644 (file)
@@ -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();