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 --- /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 + + [arguments passed to FileCheck] + +""" + + +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 --- /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 --- 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 @@ "starting-granularity-level", cl::desc("Number of times to divide chunks prior to first test")); +static cl::opt 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 @@ // 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();