1 //===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "DeltaManager.h"
18 #include "ReducerWorkItem.h"
19 #include "TestRunner.h"
20 #include "llvm/Bitcode/BitcodeReader.h"
21 #include "llvm/CodeGen/CommandFlags.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/MemoryBufferRef.h"
25 #include "llvm/Support/Process.h"
26 #include "llvm/Support/WithColor.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
36 cl::OptionCategory
LLVMReduceOptions("llvm-reduce options");
38 static cl::opt
<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden
,
39 cl::cat(LLVMReduceOptions
));
40 static cl::opt
<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden
,
41 cl::cat(LLVMReduceOptions
));
43 static cl::opt
<bool> PreserveDebugEnvironment(
44 "preserve-debug-environment",
45 cl::desc("Don't disable features used for crash "
46 "debugging (crash reports, llvm-symbolizer and core dumps)"),
47 cl::cat(LLVMReduceOptions
));
50 PrintDeltaPasses("print-delta-passes",
51 cl::desc("Print list of delta passes, passable to "
52 "--delta-passes as a comma separated list"),
53 cl::cat(LLVMReduceOptions
));
55 static cl::opt
<std::string
> InputFilename(cl::Positional
,
56 cl::desc("<input llvm ll/bc file>"),
57 cl::cat(LLVMReduceOptions
));
59 static cl::opt
<std::string
>
61 cl::desc("Name of the interesting-ness test to be run"),
62 cl::cat(LLVMReduceOptions
));
64 static cl::list
<std::string
>
65 TestArguments("test-arg",
66 cl::desc("Arguments passed onto the interesting-ness test"),
67 cl::cat(LLVMReduceOptions
));
69 static cl::opt
<std::string
> OutputFilename(
71 cl::desc("Specify the output file. default: reduced.ll|.bc|.mir"));
72 static cl::alias
OutputFileAlias("o", cl::desc("Alias for -output"),
73 cl::aliasopt(OutputFilename
),
74 cl::cat(LLVMReduceOptions
));
77 ReplaceInput("in-place",
78 cl::desc("WARNING: This option will replace your input file "
79 "with the reduced version!"),
80 cl::cat(LLVMReduceOptions
));
82 enum class InputLanguages
{ None
, IR
, MIR
};
84 static cl::opt
<InputLanguages
>
85 InputLanguage("x", cl::ValueOptional
,
86 cl::desc("Input language ('ir' or 'mir')"),
87 cl::init(InputLanguages::None
),
88 cl::values(clEnumValN(InputLanguages::IR
, "ir", ""),
89 clEnumValN(InputLanguages::MIR
, "mir", "")),
90 cl::cat(LLVMReduceOptions
));
92 static cl::opt
<bool> ForceOutputBitcode(
94 cl::desc("Emit final result as bitcode instead of text IR"), cl::Hidden
,
95 cl::cat(LLVMReduceOptions
));
98 MaxPassIterations("max-pass-iterations",
99 cl::desc("Maximum number of times to run the full set "
100 "of delta passes (default=5)"),
101 cl::init(5), cl::cat(LLVMReduceOptions
));
103 extern cl::opt
<cl::boolOrDefault
> PreserveInputDbgFormat
;
105 static codegen::RegisterCodeGenFlags CGF
;
107 /// Turn off crash debugging features
109 /// Crash is expected, so disable crash reports and symbolization to reduce
110 /// output clutter and avoid potentially slow symbolization.
111 static void disableEnvironmentDebugFeatures() {
112 sys::Process::PreventCoreFiles();
114 // TODO: Copied from not. Should have a wrapper around setenv.
116 SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");
117 SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");
119 setenv("LLVM_DISABLE_CRASH_REPORT", "1", /*overwrite=*/1);
120 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", /*overwrite=*/1);
124 static std::pair
<StringRef
, bool> determineOutputType(bool IsMIR
,
125 bool InputIsBitcode
) {
126 bool OutputBitcode
= ForceOutputBitcode
|| InputIsBitcode
;
128 if (ReplaceInput
) { // In-place
129 OutputFilename
= InputFilename
.c_str();
130 } else if (OutputFilename
.empty()) {
131 // Default to producing bitcode if the input was bitcode, if not explicitly
135 IsMIR
? "reduced.mir" : (OutputBitcode
? "reduced.bc" : "reduced.ll");
138 return {OutputFilename
, OutputBitcode
};
141 int main(int Argc
, char **Argv
) {
142 InitLLVM
X(Argc
, Argv
);
143 const StringRef
ToolName(Argv
[0]);
144 PreserveInputDbgFormat
= cl::boolOrDefault::BOU_TRUE
;
146 cl::HideUnrelatedOptions({&LLVMReduceOptions
, &getColorCategory()});
147 cl::ParseCommandLineOptions(Argc
, Argv
, "LLVM automatic testcase reducer.\n");
150 cl::PrintHelpMessage();
154 if (PrintDeltaPasses
) {
155 printDeltaPasses(outs());
159 bool ReduceModeMIR
= false;
160 if (InputLanguage
!= InputLanguages::None
) {
161 if (InputLanguage
== InputLanguages::MIR
)
162 ReduceModeMIR
= true;
163 } else if (StringRef(InputFilename
).ends_with(".mir")) {
164 ReduceModeMIR
= true;
167 if (InputFilename
.empty()) {
168 WithColor::error(errs(), ToolName
)
169 << "reduction testcase positional argument must be specified\n";
173 if (TestFilename
.empty()) {
174 WithColor::error(errs(), ToolName
) << "--test option must be specified\n";
178 if (!PreserveDebugEnvironment
)
179 disableEnvironmentDebugFeatures();
182 std::unique_ptr
<TargetMachine
> TM
;
184 auto [OriginalProgram
, InputIsBitcode
] =
185 parseReducerWorkItem(ToolName
, InputFilename
, Context
, TM
, ReduceModeMIR
);
186 if (!OriginalProgram
) {
190 StringRef OutputFilename
;
192 std::tie(OutputFilename
, OutputBitcode
) =
193 determineOutputType(ReduceModeMIR
, InputIsBitcode
);
195 // Initialize test environment
196 TestRunner
Tester(TestFilename
, TestArguments
, std::move(OriginalProgram
),
197 std::move(TM
), ToolName
, OutputFilename
, InputIsBitcode
,
200 // This parses and writes out the testcase into a temporary file copy for the
201 // test, rather than evaluating the source IR directly. This is for the
202 // convenience of lit tests; the stripped out comments may have broken the
203 // interestingness checks.
204 if (!Tester
.getProgram().isReduced(Tester
)) {
205 errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
209 // Try to reduce code
210 runDeltaPasses(Tester
, MaxPassIterations
);
212 // Print reduced file to STDOUT
213 if (OutputFilename
== "-")
214 Tester
.getProgram().print(outs(), nullptr);
216 Tester
.writeOutput("Done reducing! Reduced testcase: ");