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>
37 cl::OptionCategory
LLVMReduceOptions("llvm-reduce options");
39 static cl::opt
<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden
,
40 cl::cat(LLVMReduceOptions
));
41 static cl::opt
<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden
,
42 cl::cat(LLVMReduceOptions
));
44 static cl::opt
<bool> PreserveDebugEnvironment(
45 "preserve-debug-environment",
46 cl::desc("Don't disable features used for crash "
47 "debugging (crash reports, llvm-symbolizer and core dumps)"),
48 cl::cat(LLVMReduceOptions
));
51 PrintDeltaPasses("print-delta-passes",
52 cl::desc("Print list of delta passes, passable to "
53 "--delta-passes as a comma separated list"),
54 cl::cat(LLVMReduceOptions
));
56 static cl::opt
<std::string
> InputFilename(cl::Positional
,
57 cl::desc("<input llvm ll/bc file>"),
58 cl::cat(LLVMReduceOptions
));
60 static cl::opt
<std::string
>
62 cl::desc("Name of the interesting-ness test to be run"),
63 cl::cat(LLVMReduceOptions
));
65 static cl::list
<std::string
>
66 TestArguments("test-arg",
67 cl::desc("Arguments passed onto the interesting-ness test"),
68 cl::cat(LLVMReduceOptions
));
70 static cl::opt
<std::string
> OutputFilename(
72 cl::desc("Specify the output file. default: reduced.ll|.bc|.mir"));
73 static cl::alias
OutputFileAlias("o", cl::desc("Alias for -output"),
74 cl::aliasopt(OutputFilename
),
75 cl::cat(LLVMReduceOptions
));
78 ReplaceInput("in-place",
79 cl::desc("WARNING: This option will replace your input file "
80 "with the reduced version!"),
81 cl::cat(LLVMReduceOptions
));
83 enum class InputLanguages
{ None
, IR
, MIR
};
85 static cl::opt
<InputLanguages
>
86 InputLanguage("x", cl::ValueOptional
,
87 cl::desc("Input language ('ir' or 'mir')"),
88 cl::init(InputLanguages::None
),
89 cl::values(clEnumValN(InputLanguages::IR
, "ir", ""),
90 clEnumValN(InputLanguages::MIR
, "mir", "")),
91 cl::cat(LLVMReduceOptions
));
93 static cl::opt
<bool> ForceOutputBitcode(
95 cl::desc("Emit final result as bitcode instead of text IR"), cl::Hidden
,
96 cl::cat(LLVMReduceOptions
));
99 MaxPassIterations("max-pass-iterations",
100 cl::desc("Maximum number of times to run the full set "
101 "of delta passes (default=5)"),
102 cl::init(5), cl::cat(LLVMReduceOptions
));
104 static codegen::RegisterCodeGenFlags CGF
;
106 /// Turn off crash debugging features
108 /// Crash is expected, so disable crash reports and symbolization to reduce
109 /// output clutter and avoid potentially slow symbolization.
110 static void disableEnvironmentDebugFeatures() {
111 sys::Process::PreventCoreFiles();
113 // TODO: Copied from not. Should have a wrapper around setenv.
115 SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");
116 SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");
118 setenv("LLVM_DISABLE_CRASH_REPORT", "1", /*overwrite=*/1);
119 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", /*overwrite=*/1);
123 static std::pair
<StringRef
, bool> determineOutputType(bool IsMIR
,
124 bool InputIsBitcode
) {
125 bool OutputBitcode
= ForceOutputBitcode
|| InputIsBitcode
;
127 if (ReplaceInput
) { // In-place
128 OutputFilename
= InputFilename
.c_str();
129 } else if (OutputFilename
.empty()) {
130 // Default to producing bitcode if the input was bitcode, if not explicitly
134 IsMIR
? "reduced.mir" : (OutputBitcode
? "reduced.bc" : "reduced.ll");
137 return {OutputFilename
, OutputBitcode
};
140 int main(int Argc
, char **Argv
) {
141 InitLLVM
X(Argc
, Argv
);
142 const StringRef
ToolName(Argv
[0]);
144 cl::HideUnrelatedOptions({&LLVMReduceOptions
, &getColorCategory()});
145 cl::ParseCommandLineOptions(Argc
, Argv
, "LLVM automatic testcase reducer.\n");
148 cl::PrintHelpMessage();
152 if (PrintDeltaPasses
) {
153 printDeltaPasses(outs());
157 bool ReduceModeMIR
= false;
158 if (InputLanguage
!= InputLanguages::None
) {
159 if (InputLanguage
== InputLanguages::MIR
)
160 ReduceModeMIR
= true;
161 } else if (StringRef(InputFilename
).endswith(".mir")) {
162 ReduceModeMIR
= true;
165 if (InputFilename
.empty()) {
166 WithColor::error(errs(), ToolName
)
167 << "reduction testcase positional argument must be specified\n";
171 if (TestFilename
.empty()) {
172 WithColor::error(errs(), ToolName
) << "--test option must be specified\n";
176 if (!PreserveDebugEnvironment
)
177 disableEnvironmentDebugFeatures();
180 std::unique_ptr
<TargetMachine
> TM
;
182 auto [OriginalProgram
, InputIsBitcode
] =
183 parseReducerWorkItem(ToolName
, InputFilename
, Context
, TM
, ReduceModeMIR
);
184 if (!OriginalProgram
) {
188 StringRef OutputFilename
;
190 std::tie(OutputFilename
, OutputBitcode
) =
191 determineOutputType(ReduceModeMIR
, InputIsBitcode
);
193 // Initialize test environment
194 TestRunner
Tester(TestFilename
, TestArguments
, std::move(OriginalProgram
),
195 std::move(TM
), ToolName
, OutputFilename
, InputIsBitcode
,
198 // This parses and writes out the testcase into a temporary file copy for the
199 // test, rather than evaluating the source IR directly. This is for the
200 // convenience of lit tests; the stripped out comments may have broken the
201 // interestingness checks.
202 if (!Tester
.getProgram().isReduced(Tester
)) {
203 errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
207 // Try to reduce code
208 runDeltaPasses(Tester
, MaxPassIterations
);
210 // Print reduced file to STDOUT
211 if (OutputFilename
== "-")
212 Tester
.getProgram().print(outs(), nullptr);
214 Tester
.writeOutput("Done reducing! Reduced testcase: ");