1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
3 // This file contains code used to execute the program utilizing one of the
4 // various ways of running LLVM bytecode.
6 //===----------------------------------------------------------------------===//
11 1. Bugpoint should not leave any files behind if the program works properly
12 2. There should be an option to specify the program name, which specifies a
13 unique string to put into output files. This allows operation in the
14 SingleSource directory f.e. Default to the first input filename.
17 #include "BugDriver.h"
18 #include "SystemUtils.h"
19 #include "Support/CommandLine.h"
24 // OutputType - Allow the user to specify the way code should be run, to test
25 // for miscompilation.
28 RunLLI, RunJIT, RunLLC, RunCBE
31 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
32 cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"),
33 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
34 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
35 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
39 InputFile("input", cl::init("/dev/null"),
40 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
43 /// AbstractInterpreter Class - Subclasses of this class are used to execute
44 /// LLVM bytecode in a variety of ways. This abstract interface hides this
45 /// complexity behind a simple interface.
47 struct AbstractInterpreter {
49 virtual ~AbstractInterpreter() {}
51 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
52 /// specified filename. This returns the exit code of the program.
54 virtual int ExecuteProgram(const std::string &Bytecode,
55 const std::string &OutputFile) = 0;
60 //===----------------------------------------------------------------------===//
61 // LLI Implementation of AbstractIntepreter interface
63 class LLI : public AbstractInterpreter {
64 std::string LLIPath; // The path to the LLI executable
66 LLI(const std::string &Path) : LLIPath(Path) { }
68 // LLI create method - Try to find the LLI executable
69 static LLI *create(BugDriver *BD, std::string &Message) {
70 std::string LLIPath = FindExecutable("lli", BD->getToolName());
71 if (!LLIPath.empty()) {
72 Message = "Found lli: " + LLIPath + "\n";
73 return new LLI(LLIPath);
76 Message = "Cannot find 'lli' in bugpoint executable directory or PATH!\n";
79 virtual int ExecuteProgram(const std::string &Bytecode,
80 const std::string &OutputFile);
83 int LLI::ExecuteProgram(const std::string &Bytecode,
84 const std::string &OutputFile) {
85 const char *Args[] = {
87 "-abort-on-exception",
89 "-force-interpreter=true",
94 return RunProgramWithTimeout(LLIPath, Args,
95 InputFile, OutputFile, OutputFile);
98 //===----------------------------------------------------------------------===//
99 // JIT Implementation of AbstractIntepreter interface
101 class JIT : public AbstractInterpreter {
102 std::string LLIPath; // The path to the LLI executable
104 JIT(const std::string &Path) : LLIPath(Path) { }
106 // JIT create method - Try to find the LLI executable
107 static JIT *create(BugDriver *BD, std::string &Message) {
108 std::string LLIPath = FindExecutable("lli", BD->getToolName());
109 if (!LLIPath.empty()) {
110 Message = "Found lli: " + LLIPath + "\n";
111 return new JIT(LLIPath);
114 Message = "Cannot find 'lli' in bugpoint executable directory or PATH!\n";
117 virtual int ExecuteProgram(const std::string &Bytecode,
118 const std::string &OutputFile);
121 int JIT::ExecuteProgram(const std::string &Bytecode,
122 const std::string &OutputFile) {
123 const char *Args[] = {
126 "-force-interpreter=false",
131 return RunProgramWithTimeout(LLIPath, Args,
132 InputFile, OutputFile, OutputFile);
135 //===----------------------------------------------------------------------===//
136 // CBE Implementation of AbstractIntepreter interface
138 class CBE : public AbstractInterpreter {
139 std::string DISPath; // The path to the LLVM 'dis' executable
140 std::string GCCPath; // The path to the gcc executable
142 CBE(const std::string &disPath, const std::string &gccPath)
143 : DISPath(disPath), GCCPath(gccPath) { }
145 // CBE create method - Try to find the 'dis' executable
146 static CBE *create(BugDriver *BD, std::string &Message) {
147 std::string DISPath = FindExecutable("dis", BD->getToolName());
148 if (DISPath.empty()) {
149 Message = "Cannot find 'dis' in bugpoint executable directory or PATH!\n";
153 Message = "Found dis: " + DISPath + "\n";
155 std::string GCCPath = FindExecutable("gcc", BD->getToolName());
156 if (GCCPath.empty()) {
157 Message = "Cannot find 'gcc' in bugpoint executable directory or PATH!\n";
161 Message += "Found gcc: " + GCCPath + "\n";
162 return new CBE(DISPath, GCCPath);
164 virtual int ExecuteProgram(const std::string &Bytecode,
165 const std::string &OutputFile);
168 int CBE::ExecuteProgram(const std::string &Bytecode,
169 const std::string &OutputFile) {
170 std::string OutputCFile = getUniqueFilename("bugpoint.cbe.c");
171 const char *DisArgs[] = {
173 "-o", OutputCFile.c_str(), // Output to the C file
175 "-f", // Overwrite as necessary...
176 Bytecode.c_str(), // This is the input bytecode
180 std::cout << "<cbe>";
181 if (RunProgramWithTimeout(DISPath, DisArgs, "/dev/null", "/dev/null",
183 // If dis failed on the bytecode, print error...
184 std::cerr << "bugpoint error: dis -c failed!?\n";
185 removeFile(OutputCFile);
189 // Assuming the c backend worked, compile the result with GCC...
190 std::string OutputBinary = getUniqueFilename("bugpoint.cbe.exe");
191 const char *GCCArgs[] = {
193 "-x", "c", // Force recognition as a C file
194 "-o", OutputBinary.c_str(), // Output to the right filename...
195 OutputCFile.c_str(), // Specify the input filename...
196 "-O2", // Optimize the program a bit...
200 // FIXME: Eventually the CC program and arguments for it should be settable on
201 // the bugpoint command line!
203 std::cout << "<gcc>";
205 // Run the C compiler on the output of the C backend...
206 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
208 std::cerr << "\n*** bugpoint error: invocation of the C compiler "
209 "failed on CBE result!\n";
210 for (const char **Arg = DisArgs; *Arg; ++Arg)
211 std::cerr << " " << *Arg;
213 for (const char **Arg = GCCArgs; *Arg; ++Arg)
214 std::cerr << " " << *Arg;
217 // Rerun the compiler, capturing any error messages to print them.
218 std::string ErrorFilename = getUniqueFilename("bugpoint.cbe.errors");
219 RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(),
220 ErrorFilename.c_str());
222 // Print out the error messages generated by GCC if possible...
223 std::ifstream ErrorFile(ErrorFilename.c_str());
225 std::copy(std::istreambuf_iterator<char>(ErrorFile),
226 std::istreambuf_iterator<char>(),
227 std::ostreambuf_iterator<char>(std::cerr));
232 removeFile(ErrorFilename);
233 exit(1); // Leave stuff around for the user to inspect or debug the CBE
236 const char *ProgramArgs[] = {
237 OutputBinary.c_str(),
241 std::cout << "<program>";
243 // Now that we have a binary, run it!
244 int Result = RunProgramWithTimeout(OutputBinary, ProgramArgs,
245 InputFile, OutputFile, OutputFile);
247 removeFile(OutputCFile);
248 removeFile(OutputBinary);
252 //===----------------------------------------------------------------------===//
253 // BugDriver method implementation
256 /// initializeExecutionEnvironment - This method is used to set up the
257 /// environment for executing LLVM programs.
259 bool BugDriver::initializeExecutionEnvironment() {
260 std::cout << "Initializing execution environment: ";
262 // FIXME: This should default to searching for the best interpreter to use on
263 // this platform, which would be JIT, then LLC, then CBE, then LLI.
265 // Create an instance of the AbstractInterpreter interface as specified on the
268 switch (InterpreterSel) {
269 case RunLLI: Interpreter = LLI::create(this, Message); break;
270 case RunJIT: Interpreter = JIT::create(this, Message); break;
271 case RunCBE: Interpreter = CBE::create(this, Message); break;
273 Message = " Sorry, this back-end is not supported by bugpoint right now!\n";
277 std::cout << Message;
279 // If there was an error creating the selected interpreter, quit with error.
280 return Interpreter == 0;
284 /// executeProgram - This method runs "Program", capturing the output of the
285 /// program to a file, returning the filename of the file. A recommended
286 /// filename may be optionally specified.
288 std::string BugDriver::executeProgram(std::string OutputFile,
289 std::string BytecodeFile) {
290 assert(Interpreter && "Interpreter should have been created already!");
291 bool CreatedBytecode = false;
292 if (BytecodeFile.empty()) {
293 // Emit the program to a bytecode file...
294 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
296 if (writeProgramToFile(BytecodeFile, Program)) {
297 std::cerr << ToolName << ": Error emitting bytecode to file '"
298 << BytecodeFile << "'!\n";
301 CreatedBytecode = true;
304 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
306 // Check to see if this is a valid output filename...
307 OutputFile = getUniqueFilename(OutputFile);
309 // Actually execute the program!
310 int RetVal = Interpreter->ExecuteProgram(BytecodeFile, OutputFile);
312 // Remove the temporary bytecode file.
314 removeFile(BytecodeFile);
316 // Return the filename we captured the output to.
320 /// diffProgram - This method executes the specified module and diffs the output
321 /// against the file specified by ReferenceOutputFile. If the output is
322 /// different, true is returned.
324 bool BugDriver::diffProgram(const std::string &ReferenceOutputFile,
325 const std::string &BytecodeFile,
326 bool RemoveBytecode) {
327 // Execute the program, generating an output file...
328 std::string Output = executeProgram("", BytecodeFile);
330 std::ifstream ReferenceFile(ReferenceOutputFile.c_str());
331 if (!ReferenceFile) {
332 std::cerr << "Couldn't open reference output file '"
333 << ReferenceOutputFile << "'\n";
337 std::ifstream OutputFile(Output.c_str());
339 std::cerr << "Couldn't open output file: " << Output << "'!\n";
343 bool FilesDifferent = false;
345 // Compare the two files...
348 C1 = ReferenceFile.get();
349 C2 = OutputFile.get();
350 if (C1 != C2) { FilesDifferent = true; break; }
354 if (RemoveBytecode) removeFile(BytecodeFile);
355 return FilesDifferent;