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 /// AbstractInterpreter Class - Subclasses of this class are used to execute
40 /// LLVM bytecode in a variety of ways. This abstract interface hides this
41 /// complexity behind a simple interface.
43 struct AbstractInterpreter {
45 virtual ~AbstractInterpreter() {}
47 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
48 /// specified filename. This returns the exit code of the program.
50 virtual int ExecuteProgram(const std::string &Bytecode,
51 const std::string &OutputFile) = 0;
56 //===----------------------------------------------------------------------===//
57 // LLI Implementation of AbstractIntepreter interface
59 class LLI : public AbstractInterpreter {
60 std::string LLIPath; // The path to the LLI executable
62 LLI(const std::string &Path) : LLIPath(Path) { }
64 // LLI create method - Try to find the LLI executable
65 static LLI *create(BugDriver *BD, std::string &Message) {
66 std::string LLIPath = FindExecutable("lli", BD->getToolName());
67 if (!LLIPath.empty()) {
68 Message = "Found lli: " + LLIPath + "\n";
69 return new LLI(LLIPath);
72 Message = "Cannot find 'lli' in bugpoint executable directory or PATH!\n";
75 virtual int ExecuteProgram(const std::string &Bytecode,
76 const std::string &OutputFile);
79 int LLI::ExecuteProgram(const std::string &Bytecode,
80 const std::string &OutputFile) {
81 const char *Args[] = {
82 "-abort-on-exception",
88 return RunProgramWithTimeout(LLIPath, Args,
89 "/dev/null", OutputFile, OutputFile);
93 //===----------------------------------------------------------------------===//
94 // BugDriver method implementation
97 /// initializeExecutionEnvironment - This method is used to set up the
98 /// environment for executing LLVM programs.
100 bool BugDriver::initializeExecutionEnvironment() {
101 std::cout << "Initializing execution environment: ";
103 // FIXME: This should default to searching for the best interpreter to use on
104 // this platform, which would be JIT, then LLC, then CBE, then LLI.
106 // Create an instance of the AbstractInterpreter interface as specified on the
109 if (InterpreterSel == RunLLI) {
110 Interpreter = LLI::create(this, Message);
112 Message = " Sorry, only LLI is supported right now!";
115 std::cout << Message;
117 // If there was an error creating the selected interpreter, quit with error.
118 return Interpreter == 0;
122 /// executeProgram - This method runs "Program", capturing the output of the
123 /// program to a file, returning the filename of the file. A recommended
124 /// filename may be optionally specified.
126 std::string BugDriver::executeProgram(std::string OutputFile,
127 std::string BytecodeFile) {
128 assert(Interpreter && "Interpreter should have been created already!");
129 bool CreatedBytecode = false;
130 if (BytecodeFile.empty()) {
131 // Emit the program to a bytecode file...
132 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
134 if (writeProgramToFile(BytecodeFile, Program)) {
135 std::cerr << ToolName << ": Error emitting bytecode to file '"
136 << BytecodeFile << "'!\n";
139 CreatedBytecode = true;
142 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
144 // Check to see if this is a valid output filename...
145 OutputFile = getUniqueFilename(OutputFile);
147 // Actually execute the program!
148 int RetVal = Interpreter->ExecuteProgram(BytecodeFile, OutputFile);
150 // Remove the temporary bytecode file.
152 removeFile(BytecodeFile);
154 // Return the filename we captured the output to.
158 /// diffProgram - This method executes the specified module and diffs the output
159 /// against the file specified by ReferenceOutputFile. If the output is
160 /// different, true is returned.
162 bool BugDriver::diffProgram(const std::string &ReferenceOutputFile,
163 const std::string &BytecodeFile) {
164 // Execute the program, generating an output file...
165 std::string Output = executeProgram("", BytecodeFile);
167 std::ifstream ReferenceFile(ReferenceOutputFile.c_str());
168 if (!ReferenceFile) {
169 std::cerr << "Couldn't open reference output file '"
170 << ReferenceOutputFile << "'\n";
174 std::ifstream OutputFile(Output.c_str());
176 std::cerr << "Couldn't open output file: " << Output << "'!\n";
180 bool FilesDifferent = false;
182 // Compare the two files...
185 C1 = ReferenceFile.get();
186 C2 = OutputFile.get();
187 if (C1 != C2) { FilesDifferent = true; break; }
191 return FilesDifferent;