[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
This commit is contained in:
Florian Hahn 2021-11-16 12:39:32 +00:00
parent f1dfc0275c
commit 28d95a2610
No known key found for this signature in database
GPG Key ID: EEF712BB5E80EBBA
3 changed files with 68 additions and 1 deletions

View File

@ -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)

View File

@ -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
}

View 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();