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[] = {
86 "-abort-on-exception",
92 return RunProgramWithTimeout(LLIPath, Args,
93 InputFile, OutputFile, OutputFile);
97 //===----------------------------------------------------------------------===//
98 // BugDriver method implementation
101 /// initializeExecutionEnvironment - This method is used to set up the
102 /// environment for executing LLVM programs.
104 bool BugDriver::initializeExecutionEnvironment() {
105 std::cout << "Initializing execution environment: ";
107 // FIXME: This should default to searching for the best interpreter to use on
108 // this platform, which would be JIT, then LLC, then CBE, then LLI.
110 // Create an instance of the AbstractInterpreter interface as specified on the
113 if (InterpreterSel == RunLLI) {
114 Interpreter = LLI::create(this, Message);
116 Message = " Sorry, only LLI is supported right now!";
119 std::cout << Message;
121 // If there was an error creating the selected interpreter, quit with error.
122 return Interpreter == 0;
126 /// executeProgram - This method runs "Program", capturing the output of the
127 /// program to a file, returning the filename of the file. A recommended
128 /// filename may be optionally specified.
130 std::string BugDriver::executeProgram(std::string OutputFile,
131 std::string BytecodeFile) {
132 assert(Interpreter && "Interpreter should have been created already!");
133 bool CreatedBytecode = false;
134 if (BytecodeFile.empty()) {
135 // Emit the program to a bytecode file...
136 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
138 if (writeProgramToFile(BytecodeFile, Program)) {
139 std::cerr << ToolName << ": Error emitting bytecode to file '"
140 << BytecodeFile << "'!\n";
143 CreatedBytecode = true;
146 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
148 // Check to see if this is a valid output filename...
149 OutputFile = getUniqueFilename(OutputFile);
151 // Actually execute the program!
152 int RetVal = Interpreter->ExecuteProgram(BytecodeFile, OutputFile);
154 // Remove the temporary bytecode file.
156 removeFile(BytecodeFile);
158 // Return the filename we captured the output to.
162 /// diffProgram - This method executes the specified module and diffs the output
163 /// against the file specified by ReferenceOutputFile. If the output is
164 /// different, true is returned.
166 bool BugDriver::diffProgram(const std::string &ReferenceOutputFile,
167 const std::string &BytecodeFile,
168 bool RemoveBytecode) {
169 // Execute the program, generating an output file...
170 std::string Output = executeProgram("", BytecodeFile);
172 std::ifstream ReferenceFile(ReferenceOutputFile.c_str());
173 if (!ReferenceFile) {
174 std::cerr << "Couldn't open reference output file '"
175 << ReferenceOutputFile << "'\n";
179 std::ifstream OutputFile(Output.c_str());
181 std::cerr << "Couldn't open output file: " << Output << "'!\n";
185 bool FilesDifferent = false;
187 // Compare the two files...
190 C1 = ReferenceFile.get();
191 C2 = OutputFile.get();
192 if (C1 != C2) { FilesDifferent = true; break; }
196 if (RemoveBytecode) removeFile(BytecodeFile);
197 return FilesDifferent;