Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 301 lines (257 sloc) 10.48 kb
4a10645 New files for miscompilation detection
Chris Lattner authored
1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
7c0e022 Added copyright header to all C++ source files.
John Criswell authored
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
4a10645 New files for miscompilation detection
Chris Lattner authored
9 //
10 // This file contains code used to execute the program utilizing one of the
11 // various ways of running LLVM bytecode.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "BugDriver.h"
b687d82 * Reorder #includes
Misha Brukman authored
16 #include "llvm/Support/ToolRunner.h"
4a10645 New files for miscompilation detection
Chris Lattner authored
17 #include "Support/CommandLine.h"
c648dab DEBUG got moved to Debug.h
Chris Lattner authored
18 #include "Support/Debug.h"
65f6279 Use the new FileUtilities library to do diff'ing of files
Chris Lattner authored
19 #include "Support/FileUtilities.h"
e49603d Re-grouped and alphabetized headers for easier reading and cleaner style...
Misha Brukman authored
20 #include "Support/SystemUtils.h"
4a10645 New files for miscompilation detection
Chris Lattner authored
21 #include <fstream>
e1b52b7 Fix compilation on GCC 3.2
Chris Lattner authored
22 #include <iostream>
d0fde30 Put all LLVM code into the llvm namespace, as per bug 109.
Brian Gaeke authored
23 using namespace llvm;
24
4a10645 New files for miscompilation detection
Chris Lattner authored
25 namespace {
26 // OutputType - Allow the user to specify the way code should be run, to test
27 // for miscompilation.
28 //
29 enum OutputType {
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
30 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
4a10645 New files for miscompilation detection
Chris Lattner authored
31 };
4148556 Use the newly abstracted interface for running our tools and gcc.
Misha Brukman authored
32
4a10645 New files for miscompilation detection
Chris Lattner authored
33 cl::opt<OutputType>
34 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
35 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
b687d82 * Reorder #includes
Misha Brukman authored
36 clEnumValN(RunLLI, "run-int",
37 "Execute with the interpreter"),
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
38 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
39 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
40 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
4d143ee IA64 compat
Chris Lattner authored
41 clEnumValEnd),
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
42 cl::init(AutoPick));
3c053a0 Allow specifying an input file for the program being executed
Chris Lattner authored
43
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
44 cl::opt<bool>
45 CheckProgramExitCode("check-exit-code",
46 cl::desc("Assume nonzero exit code is failure (default on)"),
47 cl::init(true));
48
3c053a0 Allow specifying an input file for the program being executed
Chris Lattner authored
49 cl::opt<std::string>
50 InputFile("input", cl::init("/dev/null"),
51 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
7dac658 add support forloading additional .so files on the command line
Chris Lattner authored
52
53 cl::list<std::string>
54 AdditionalSOs("additional-so",
55 cl::desc("Additional shared objects to load "
56 "into executing programs"));
4a10645 New files for miscompilation detection
Chris Lattner authored
57 }
58
d0fde30 Put all LLVM code into the llvm namespace, as per bug 109.
Brian Gaeke authored
59 namespace llvm {
fa76183 finegrainify namespacification
Chris Lattner authored
60 // Anything specified after the --args option are taken as arguments to the
61 // program being debugged.
62 cl::list<std::string>
63 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
60083e2 Use the new commandline flag to allow us to call bugpoint like this:
Chris Lattner authored
64 cl::ZeroOrMore, cl::PositionalEatsArgs);
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
65
66 cl::list<std::string>
67 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
60083e2 Use the new commandline flag to allow us to call bugpoint like this:
Chris Lattner authored
68 cl::ZeroOrMore, cl::PositionalEatsArgs);
fa76183 finegrainify namespacification
Chris Lattner authored
69 }
9d679cb * Moved InputArgv out of anonymous scope to be extern'd in another file.
Misha Brukman authored
70
4a10645 New files for miscompilation detection
Chris Lattner authored
71 //===----------------------------------------------------------------------===//
72 // BugDriver method implementation
73 //
74
75 /// initializeExecutionEnvironment - This method is used to set up the
76 /// environment for executing LLVM programs.
77 ///
78 bool BugDriver::initializeExecutionEnvironment() {
79 std::cout << "Initializing execution environment: ";
80
4148556 Use the newly abstracted interface for running our tools and gcc.
Misha Brukman authored
81 // Create an instance of the AbstractInterpreter interface as specified on
82 // the command line
7bb1154 Update comments, if we are running with the CBE, make sure the Interpret...
Chris Lattner authored
83 cbe = 0;
4a10645 New files for miscompilation detection
Chris Lattner authored
84 std::string Message;
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
85
cc876a7 Add support for debugging miscompilations with the use of the JIT or CBE...
Chris Lattner authored
86 switch (InterpreterSel) {
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
87 case AutoPick:
88 InterpreterSel = RunCBE;
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
89 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
90 &ToolArgv);
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
91 if (!Interpreter) {
92 InterpreterSel = RunJIT;
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
93 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
94 &ToolArgv);
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
95 }
96 if (!Interpreter) {
97 InterpreterSel = RunLLC;
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
98 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
99 &ToolArgv);
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
100 }
101 if (!Interpreter) {
102 InterpreterSel = RunLLI;
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
103 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
104 &ToolArgv);
b5ee509 Fix the first FIXME in this file: automatically pick a "good"
Brian Gaeke authored
105 }
106 if (!Interpreter) {
107 InterpreterSel = AutoPick;
108 Message = "Sorry, I can't automatically select an interpreter!\n";
109 }
110 break;
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
111 case RunLLI:
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
112 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
113 &ToolArgv);
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
114 break;
115 case RunLLC:
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
116 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
117 &ToolArgv);
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
118 break;
119 case RunJIT:
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
120 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
121 &ToolArgv);
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
122 break;
123 case RunCBE:
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
124 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
125 &ToolArgv);
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
126 break;
cc876a7 Add support for debugging miscompilations with the use of the JIT or CBE...
Chris Lattner authored
127 default:
4148556 Use the newly abstracted interface for running our tools and gcc.
Misha Brukman authored
128 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
cc876a7 Add support for debugging miscompilations with the use of the JIT or CBE...
Chris Lattner authored
129 break;
4a10645 New files for miscompilation detection
Chris Lattner authored
130 }
4148556 Use the newly abstracted interface for running our tools and gcc.
Misha Brukman authored
131 std::cerr << Message;
4a10645 New files for miscompilation detection
Chris Lattner authored
132
a259c9b Made a bunch of cleanups, as per Chris' recommendations:
Misha Brukman authored
133 // Initialize auxiliary tools for debugging
7bb1154 Update comments, if we are running with the CBE, make sure the Interpret...
Chris Lattner authored
134 if (!cbe) {
636df3d Add --tool-args flag which lets you pass arguments to llc or lli.
Brian Gaeke authored
135 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
7bb1154 Update comments, if we are running with the CBE, make sure the Interpret...
Chris Lattner authored
136 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
137 }
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
138 gcc = GCC::create(getToolName(), Message);
a259c9b Made a bunch of cleanups, as per Chris' recommendations:
Misha Brukman authored
139 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
140
4a10645 New files for miscompilation detection
Chris Lattner authored
141 // If there was an error creating the selected interpreter, quit with error.
142 return Interpreter == 0;
143 }
144
ea9212c Add a new method for use by the code generator crash debugger.
Chris Lattner authored
145 /// compileProgram - Try to compile the specified module, throwing an exception
146 /// if an error occurs, or returning normally if not. This is used for code
147 /// generation crash testing.
148 ///
149 void BugDriver::compileProgram(Module *M) {
150 // Emit the program to a bytecode file...
151 std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
152 if (writeProgramToFile(BytecodeFile, M)) {
153 std::cerr << ToolName << ": Error emitting bytecode to file '"
154 << BytecodeFile << "'!\n";
155 exit(1);
156 }
157
158 // Remove the temporary bytecode file when we are done.
159 FileRemover BytecodeFileRemover(BytecodeFile);
160
161 // Actually compile the program!
162 Interpreter->compileProgram(BytecodeFile);
163 }
164
4a10645 New files for miscompilation detection
Chris Lattner authored
165
166 /// executeProgram - This method runs "Program", capturing the output of the
167 /// program to a file, returning the filename of the file. A recommended
168 /// filename may be optionally specified.
169 ///
170 std::string BugDriver::executeProgram(std::string OutputFile,
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
171 std::string BytecodeFile,
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
172 const std::string &SharedObj,
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
173 AbstractInterpreter *AI,
174 bool *ProgramExitedNonzero) {
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
175 if (AI == 0) AI = Interpreter;
176 assert(AI && "Interpreter should have been created already!");
4a10645 New files for miscompilation detection
Chris Lattner authored
177 bool CreatedBytecode = false;
178 if (BytecodeFile.empty()) {
179 // Emit the program to a bytecode file...
180 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
181
182 if (writeProgramToFile(BytecodeFile, Program)) {
183 std::cerr << ToolName << ": Error emitting bytecode to file '"
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
184 << BytecodeFile << "'!\n";
4a10645 New files for miscompilation detection
Chris Lattner authored
185 exit(1);
186 }
187 CreatedBytecode = true;
188 }
189
9709272 Make the executeProgram method exception safe, not leaving around byteco...
Chris Lattner authored
190 // Remove the temporary bytecode file when we are done.
191 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
192
4a10645 New files for miscompilation detection
Chris Lattner authored
193 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
194
4a10645 New files for miscompilation detection
Chris Lattner authored
195 // Check to see if this is a valid output filename...
196 OutputFile = getUniqueFilename(OutputFile);
197
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
198 // Figure out which shared objects to run, if any.
7dac658 add support forloading additional .so files on the command line
Chris Lattner authored
199 std::vector<std::string> SharedObjs(AdditionalSOs);
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
200 if (!SharedObj.empty())
201 SharedObjs.push_back(SharedObj);
202
4a10645 New files for miscompilation detection
Chris Lattner authored
203 // Actually execute the program!
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
204 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
205 OutputFile, SharedObjs);
206
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
207 if (ProgramExitedNonzero != 0)
208 *ProgramExitedNonzero = (RetVal != 0);
4a10645 New files for miscompilation detection
Chris Lattner authored
209
210 // Return the filename we captured the output to.
211 return OutputFile;
212 }
213
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
214 /// executeProgramWithCBE - Used to create reference output with the C
215 /// backend, if reference output is not provided.
216 ///
217 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
218 bool ProgramExitedNonzero;
219 std::string outFN = executeProgram(OutputFile, "", "",
220 (AbstractInterpreter*)cbe,
221 &ProgramExitedNonzero);
222 if (ProgramExitedNonzero) {
223 std::cerr
224 << "Warning: While generating reference output, program exited with\n"
225 << "non-zero exit code. This will NOT be treated as a failure.\n";
226 CheckProgramExitCode = false;
227 }
228 return outFN;
229 }
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
230
a0f5b15 The return value of compileSharedObject was never used. Return the shar...
Chris Lattner authored
231 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
232 assert(Interpreter && "Interpreter should have been created already!");
a0f5b15 The return value of compileSharedObject was never used. Return the shar...
Chris Lattner authored
233 std::string OutputCFile;
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
234
235 // Using CBE
236 cbe->OutputC(BytecodeFile, OutputCFile);
237
238 #if 0 /* This is an alternative, as yet unimplemented */
239 // Using LLC
a0f5b15 The return value of compileSharedObject was never used. Return the shar...
Chris Lattner authored
240 std::string Message;
4148556 Use the newly abstracted interface for running our tools and gcc.
Misha Brukman authored
241 LLC *llc = createLLCtool(Message);
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
242 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
243 std::cerr << "Could not generate asm code with `llc', exiting.\n";
244 exit(1);
245 }
246 #endif
247
a0f5b15 The return value of compileSharedObject was never used. Return the shar...
Chris Lattner authored
248 std::string SharedObjectFile;
769f1fe Change the execute methods to take the shared object filename by const r...
Chris Lattner authored
249 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
a0f5b15 The return value of compileSharedObject was never used. Return the shar...
Chris Lattner authored
250 exit(1);
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
251
252 // Remove the intermediate C file
253 removeFile(OutputCFile);
254
6ebe44d Ok, return an explicit path to the shared object, unbreaking code genera...
Chris Lattner authored
255 return "./" + SharedObjectFile;
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
256 }
257
258
4a10645 New files for miscompilation detection
Chris Lattner authored
259 /// diffProgram - This method executes the specified module and diffs the output
260 /// against the file specified by ReferenceOutputFile. If the output is
261 /// different, true is returned.
262 ///
5073336 Major addition to bugpoint: ability to debug code generators (LLC and LL...
Misha Brukman authored
263 bool BugDriver::diffProgram(const std::string &BytecodeFile,
264 const std::string &SharedObject,
640f22e Implement support for bugpoint to identify which FUNCTION an optimizatio...
Chris Lattner authored
265 bool RemoveBytecode) {
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
266 bool ProgramExitedNonzero;
267
4a10645 New files for miscompilation detection
Chris Lattner authored
268 // Execute the program, generating an output file...
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
269 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
270 &ProgramExitedNonzero);
271
272 // If we're checking the program exit code, assume anything nonzero is bad.
58d84ce If the program returns a non-zero exit value, don't leave files laying
Chris Lattner authored
273 if (CheckProgramExitCode && ProgramExitedNonzero) {
274 removeFile(Output);
275 if (RemoveBytecode) removeFile(BytecodeFile);
c5cad21 Add check-exit-code option, defaulting to true.
Brian Gaeke authored
276 return true;
58d84ce If the program returns a non-zero exit value, don't leave files laying
Chris Lattner authored
277 }
4a10645 New files for miscompilation detection
Chris Lattner authored
278
65f6279 Use the new FileUtilities library to do diff'ing of files
Chris Lattner authored
279 std::string Error;
4a10645 New files for miscompilation detection
Chris Lattner authored
280 bool FilesDifferent = false;
65f6279 Use the new FileUtilities library to do diff'ing of files
Chris Lattner authored
281 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
282 if (!Error.empty()) {
283 std::cerr << "While diffing output: " << Error << "\n";
284 exit(1);
285 }
286 FilesDifferent = true;
287 }
1a28a2b Don't leave a trail of bugpoint-execution-output-* breadcrumbs all over ...
Chris Lattner authored
288
289 // Remove the generated output.
290 removeFile(Output);
4a10645 New files for miscompilation detection
Chris Lattner authored
291
1a28a2b Don't leave a trail of bugpoint-execution-output-* breadcrumbs all over ...
Chris Lattner authored
292 // Remove the bytecode file if we are supposed to.
640f22e Implement support for bugpoint to identify which FUNCTION an optimizatio...
Chris Lattner authored
293 if (RemoveBytecode) removeFile(BytecodeFile);
4a10645 New files for miscompilation detection
Chris Lattner authored
294 return FilesDifferent;
295 }
91eabc1 BugDriver.h:
Misha Brukman authored
296
297 bool BugDriver::isExecutingJIT() {
298 return InterpreterSel == RunJIT;
299 }
d0fde30 Put all LLVM code into the llvm namespace, as per bug 109.
Brian Gaeke authored
300
Something went wrong with that request. Please try again.