Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / tools / llvm-reduce / TestRunner.cpp
blob8a61aae64b9029207d2f8b0f5e11049c2513a4dc
1 //===-- TestRunner.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "TestRunner.h"
10 #include "ReducerWorkItem.h"
11 #include "deltas/Utils.h"
12 #include "llvm/Support/WithColor.h"
14 using namespace llvm;
16 TestRunner::TestRunner(StringRef TestName,
17 const std::vector<std::string> &TestArgs,
18 std::unique_ptr<ReducerWorkItem> Program,
19 std::unique_ptr<TargetMachine> TM, StringRef ToolName,
20 StringRef OutputName, bool InputIsBitcode,
21 bool OutputBitcode)
22 : TestName(TestName), ToolName(ToolName), TestArgs(TestArgs),
23 Program(std::move(Program)), TM(std::move(TM)),
24 OutputFilename(OutputName), InputIsBitcode(InputIsBitcode),
25 EmitBitcode(OutputBitcode) {
26 assert(this->Program && "Initialized with null program?");
29 static constexpr std::array<std::optional<StringRef>, 3> DefaultRedirects = {
30 StringRef()};
31 static constexpr std::array<std::optional<StringRef>, 3> NullRedirects;
33 /// Runs the interestingness test, passes file to be tested as first argument
34 /// and other specified test arguments after that.
35 int TestRunner::run(StringRef Filename) const {
36 std::vector<StringRef> ProgramArgs;
37 ProgramArgs.push_back(TestName);
39 for (const auto &Arg : TestArgs)
40 ProgramArgs.push_back(Arg);
42 ProgramArgs.push_back(Filename);
44 std::string ErrMsg;
46 int Result =
47 sys::ExecuteAndWait(TestName, ProgramArgs, /*Env=*/std::nullopt,
48 Verbose ? DefaultRedirects : NullRedirects,
49 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg);
51 if (Result < 0) {
52 Error E = make_error<StringError>("Error running interesting-ness test: " +
53 ErrMsg,
54 inconvertibleErrorCode());
55 WithColor::error(errs(), ToolName) << toString(std::move(E)) << '\n';
56 exit(1);
59 return !Result;
62 void TestRunner::writeOutput(StringRef Message) {
63 std::error_code EC;
64 raw_fd_ostream Out(OutputFilename, EC,
65 EmitBitcode && !Program->isMIR() ? sys::fs::OF_None
66 : sys::fs::OF_Text);
67 if (EC) {
68 errs() << "Error opening output file: " << EC.message() << "!\n";
69 exit(1);
72 Program->writeOutput(Out, EmitBitcode);
73 errs() << Message << OutputFilename << '\n';