blob: 376826b8b9e7bf27d7b4545391f068079ef3c8c8 [file] [log] [blame]
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +00001//===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===//
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//===----------------------------------------------------------------------===//
8//
9// This program tries to reduce an IR test case for a given interesting-ness
10// test. It runs multiple delta debugging passes in order to minimize the input
11// file. It's worth noting that this is a part of the bugpoint redesign
12// proposal, and thus a *temporary* tool that will eventually be integrated
13// into the bugpoint tool itself.
14//
15//===----------------------------------------------------------------------===//
16
17#include "DeltaManager.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Verifier.h"
21#include "llvm/IRReader/IRReader.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/InitLLVM.h"
24#include "llvm/Support/SourceMgr.h"
25#include "llvm/Support/raw_ostream.h"
26#include <system_error>
27#include <vector>
28
29using namespace llvm;
30
Roman Lebedev34b9bbb2020-07-21 15:36:31 +030031static cl::OptionCategory Options("llvm-reduce options");
32
33static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden,
34 cl::cat(Options));
35static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden,
36 cl::cat(Options));
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000037
38static cl::opt<std::string> InputFilename(cl::Positional, cl::Required,
Roman Lebedev34b9bbb2020-07-21 15:36:31 +030039 cl::desc("<input llvm ll/bc file>"),
40 cl::cat(Options));
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000041
42static cl::opt<std::string>
43 TestFilename("test", cl::Required,
Roman Lebedev34b9bbb2020-07-21 15:36:31 +030044 cl::desc("Name of the interesting-ness test to be run"),
45 cl::cat(Options));
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000046
47static cl::list<std::string>
48 TestArguments("test-arg", cl::ZeroOrMore,
Roman Lebedev34b9bbb2020-07-21 15:36:31 +030049 cl::desc("Arguments passed onto the interesting-ness test"),
50 cl::cat(Options));
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000051
52static cl::opt<std::string>
53 OutputFilename("output",
54 cl::desc("Specify the output file. default: reduced.ll"));
55static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"),
Roman Lebedev34b9bbb2020-07-21 15:36:31 +030056 cl::aliasopt(OutputFilename),
57 cl::cat(Options));
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000058
59static cl::opt<bool>
60 ReplaceInput("in-place",
George Burgess IV9b9327f2019-12-05 19:42:33 -080061 cl::desc("WARNING: This option will replace your input file "
Roman Lebedev34b9bbb2020-07-21 15:36:31 +030062 "with the reduced version!"),
63 cl::cat(Options));
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000064
65// Parses IR into a Module and verifies it
66static std::unique_ptr<Module> parseInputFile(StringRef Filename,
67 LLVMContext &Ctxt) {
68 SMDiagnostic Err;
69 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
70 if (!Result) {
71 Err.print("llvm-reduce", errs());
72 return Result;
73 }
74
75 if (verifyModule(*Result, &errs())) {
76 errs() << "Error: " << Filename << " - input module is broken!\n";
77 return std::unique_ptr<Module>();
78 }
79
80 return Result;
81}
82
Tyker7fef40d2020-08-22 19:04:20 +020083void writeOutput(Module *M, StringRef Message) {
84 if (ReplaceInput) // In-place
85 OutputFilename = InputFilename.c_str();
86 else if (OutputFilename.empty() || OutputFilename == "-")
87 OutputFilename = "reduced.ll";
88
89 std::error_code EC;
90 raw_fd_ostream Out(OutputFilename, EC);
91 if (EC) {
92 errs() << "Error opening output file: " << EC.message() << "!\n";
93 exit(1);
94 }
95 M->print(Out, /*AnnotationWriter=*/nullptr);
96 errs() << Message << OutputFilename << "\n";
97}
98
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000099int main(int argc, char **argv) {
100 InitLLVM X(argc, argv);
101
102 cl::ParseCommandLineOptions(argc, argv, "LLVM automatic testcase reducer.\n");
103
104 LLVMContext Context;
105 std::unique_ptr<Module> OriginalProgram =
106 parseInputFile(InputFilename, Context);
107
108 // Initialize test environment
David Blaikieaaef97a2019-09-12 00:31:57 +0000109 TestRunner Tester(TestFilename, TestArguments);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000110 Tester.setProgram(std::move(OriginalProgram));
111
112 // Try to reduce code
113 runDeltaPasses(Tester);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000114
David Blaikieaaef97a2019-09-12 00:31:57 +0000115 if (!Tester.getProgram()) {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000116 errs() << "\nCouldnt reduce input :/\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000117 } else {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000118 // Print reduced file to STDOUT
119 if (OutputFilename == "-")
120 Tester.getProgram()->print(outs(), nullptr);
Tyker7fef40d2020-08-22 19:04:20 +0200121 else
122 writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: ");
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000123 }
124
125 return 0;
126}