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 static cl::opt
<bool> TryUseNewDbgInfoFormat(
104 "try-experimental-debuginfo-iterators",
105 cl::desc("Enable debuginfo iterator positions, if they're built in"),
108 extern cl::opt
<bool> UseNewDbgInfoFormat
;
110 static codegen::RegisterCodeGenFlags CGF
;
112 /// Turn off crash debugging features
114 /// Crash is expected, so disable crash reports and symbolization to reduce
115 /// output clutter and avoid potentially slow symbolization.
116 static void disableEnvironmentDebugFeatures() {
117 sys::Process::PreventCoreFiles();
119 // TODO: Copied from not. Should have a wrapper around setenv.
121 SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");
122 SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");
124 setenv("LLVM_DISABLE_CRASH_REPORT", "1", /*overwrite=*/1);
125 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", /*overwrite=*/1);
129 static std::pair
<StringRef
, bool> determineOutputType(bool IsMIR
,
130 bool InputIsBitcode
) {
131 bool OutputBitcode
= ForceOutputBitcode
|| InputIsBitcode
;
133 if (ReplaceInput
) { // In-place
134 OutputFilename
= InputFilename
.c_str();
135 } else if (OutputFilename
.empty()) {
136 // Default to producing bitcode if the input was bitcode, if not explicitly
140 IsMIR
? "reduced.mir" : (OutputBitcode
? "reduced.bc" : "reduced.ll");
143 return {OutputFilename
, OutputBitcode
};
146 int main(int Argc
, char **Argv
) {
147 InitLLVM
X(Argc
, Argv
);
148 const StringRef
ToolName(Argv
[0]);
150 cl::HideUnrelatedOptions({&LLVMReduceOptions
, &getColorCategory()});
151 cl::ParseCommandLineOptions(Argc
, Argv
, "LLVM automatic testcase reducer.\n");
153 // RemoveDIs debug-info transition: tests may request that we /try/ to use the
154 // new debug-info format, if it's built in.
155 #ifdef EXPERIMENTAL_DEBUGINFO_ITERATORS
156 if (TryUseNewDbgInfoFormat
) {
157 // If LLVM was built with support for this, turn the new debug-info format
159 UseNewDbgInfoFormat
= true;
162 (void)TryUseNewDbgInfoFormat
;
165 cl::PrintHelpMessage();
169 if (PrintDeltaPasses
) {
170 printDeltaPasses(outs());
174 bool ReduceModeMIR
= false;
175 if (InputLanguage
!= InputLanguages::None
) {
176 if (InputLanguage
== InputLanguages::MIR
)
177 ReduceModeMIR
= true;
178 } else if (StringRef(InputFilename
).ends_with(".mir")) {
179 ReduceModeMIR
= true;
182 if (InputFilename
.empty()) {
183 WithColor::error(errs(), ToolName
)
184 << "reduction testcase positional argument must be specified\n";
188 if (TestFilename
.empty()) {
189 WithColor::error(errs(), ToolName
) << "--test option must be specified\n";
193 if (!PreserveDebugEnvironment
)
194 disableEnvironmentDebugFeatures();
197 std::unique_ptr
<TargetMachine
> TM
;
199 auto [OriginalProgram
, InputIsBitcode
] =
200 parseReducerWorkItem(ToolName
, InputFilename
, Context
, TM
, ReduceModeMIR
);
201 if (!OriginalProgram
) {
205 StringRef OutputFilename
;
207 std::tie(OutputFilename
, OutputBitcode
) =
208 determineOutputType(ReduceModeMIR
, InputIsBitcode
);
210 // Initialize test environment
211 TestRunner
Tester(TestFilename
, TestArguments
, std::move(OriginalProgram
),
212 std::move(TM
), ToolName
, OutputFilename
, InputIsBitcode
,
215 // This parses and writes out the testcase into a temporary file copy for the
216 // test, rather than evaluating the source IR directly. This is for the
217 // convenience of lit tests; the stripped out comments may have broken the
218 // interestingness checks.
219 if (!Tester
.getProgram().isReduced(Tester
)) {
220 errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
224 // Try to reduce code
225 runDeltaPasses(Tester
, MaxPassIterations
);
227 // Print reduced file to STDOUT
228 if (OutputFilename
== "-")
229 Tester
.getProgram().print(outs(), nullptr);
231 Tester
.writeOutput("Done reducing! Reduced testcase: ");