Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 1 | //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file exposes an abstraction around a platform C compiler, used to |
| 10 | // compile C and assembly code. It also exposes an "AbstractIntepreter" |
| 11 | // interface, which is used to execute code using one of the LLVM execution |
| 12 | // engines. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 16 | #ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H |
| 17 | #define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 18 | |
Daniel Dunbar | 8575a60 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Anton Korobeynikov | 7cbff91 | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Error.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 24 | #include <exception> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
Anton Korobeynikov | 7cbff91 | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 29 | extern cl::opt<bool> SaveTemps; |
Daniel Dunbar | 8575a60 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 30 | extern Triple TargetTriple; |
Anton Korobeynikov | 7cbff91 | 2009-08-05 09:32:10 +0000 | [diff] [blame] | 31 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 32 | class LLC; |
| 33 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 34 | //===---------------------------------------------------------------------===// |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 35 | // CC abstraction |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 36 | // |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 37 | class CC { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 38 | std::string CCPath; // The path to the cc executable. |
| 39 | std::string RemoteClientPath; // The path to the rsh / ssh executable. |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 40 | std::vector<std::string> ccArgs; // CC-specific arguments. |
| 41 | CC(StringRef ccPath, StringRef RemotePath, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 42 | const std::vector<std::string> *CCArgs) |
| 43 | : CCPath(ccPath), RemoteClientPath(RemotePath) { |
| 44 | if (CCArgs) |
| 45 | ccArgs = *CCArgs; |
Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 46 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 47 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 48 | public: |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 49 | enum FileType { AsmFile, ObjectFile, CFile }; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 50 | |
Brian Gesiak | 5cc8920 | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 51 | static CC *create(const char *Argv0, std::string &Message, |
| 52 | const std::string &CCBinary, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 53 | const std::vector<std::string> *Args); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 54 | |
| 55 | /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is |
| 56 | /// either a .s file, or a .c file, specified by FileType), with the specified |
| 57 | /// arguments. Standard input is specified with InputFile, and standard |
| 58 | /// Output is captured to the specified OutputFile location. The SharedLibs |
| 59 | /// option specifies optional native shared objects that can be loaded into |
| 60 | /// the program for execution. |
| 61 | /// |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 62 | Expected<int> ExecuteProgram( |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 63 | const std::string &ProgramFile, const std::vector<std::string> &Args, |
| 64 | FileType fileType, const std::string &InputFile, |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 65 | const std::string &OutputFile, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 66 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 67 | unsigned Timeout = 0, unsigned MemoryLimit = 0); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 68 | |
| 69 | /// MakeSharedObject - This compiles the specified file (which is either a .c |
| 70 | /// file or a .s file) into a shared object. |
| 71 | /// |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 72 | Error MakeSharedObject(const std::string &InputFile, FileType fileType, |
| 73 | std::string &OutputFile, |
| 74 | const std::vector<std::string> &ArgsForCC); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 77 | //===---------------------------------------------------------------------===// |
| 78 | /// AbstractInterpreter Class - Subclasses of this class are used to execute |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 79 | /// LLVM bitcode in a variety of ways. This abstract interface hides this |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 80 | /// complexity behind a simple interface. |
| 81 | /// |
| 82 | class AbstractInterpreter { |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 83 | virtual void anchor(); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 84 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 85 | public: |
Dan Gohman | 46ffffa | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 86 | static LLC *createLLC(const char *Argv0, std::string &Message, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 87 | const std::string &CCBinary, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 88 | const std::vector<std::string> *Args = nullptr, |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 89 | const std::vector<std::string> *CCArgs = nullptr, |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 90 | bool UseIntegratedAssembler = false); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 91 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 92 | static AbstractInterpreter * |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 93 | createLLI(const char *Argv0, std::string &Message, |
| 94 | const std::vector<std::string> *Args = nullptr); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 95 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 96 | static AbstractInterpreter * |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 97 | createJIT(const char *Argv0, std::string &Message, |
| 98 | const std::vector<std::string> *Args = nullptr); |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 99 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 100 | static AbstractInterpreter * |
Brian Gesiak | 5cc8920 | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 101 | createCustomCompiler(const char *Argv0, std::string &Message, |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 102 | const std::string &CompileCommandLine); |
| 103 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 104 | static AbstractInterpreter * |
Brian Gesiak | 5cc8920 | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 105 | createCustomExecutor(const char *Argv0, std::string &Message, |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 106 | const std::string &ExecCommandLine); |
Anton Korobeynikov | c53565c | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 107 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 108 | virtual ~AbstractInterpreter() {} |
| 109 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 110 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 111 | /// code. This does not produce any output, it is only used when debugging |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 112 | /// the code generator. It returns false if the code generator fails. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 113 | virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0, |
| 114 | unsigned MemoryLimit = 0) { |
| 115 | return Error::success(); |
| 116 | } |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 117 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 118 | /// Compile the specified program from bitcode to code understood by the CC |
| 119 | /// driver (either C or asm). Returns an error if the code generator fails,, |
| 120 | /// otherwise, the type of code emitted. |
| 121 | virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode, |
| 122 | std::string &OutFile, |
| 123 | unsigned Timeout = 0, |
| 124 | unsigned MemoryLimit = 0) { |
| 125 | return make_error<StringError>( |
| 126 | "OutputCode not supported by this AbstractInterpreter!", |
| 127 | inconvertibleErrorCode()); |
Chris Lattner | 634bc04 | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 128 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 129 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 130 | /// ExecuteProgram - Run the specified bitcode file, emitting output to the |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 131 | /// specified filename. This sets RetVal to the exit code of the program or |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 132 | /// returns an Error if a problem was encountered that prevented execution of |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 133 | /// the program. |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 134 | /// |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 135 | virtual Expected<int> ExecuteProgram( |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 136 | const std::string &Bitcode, const std::vector<std::string> &Args, |
| 137 | const std::string &InputFile, const std::string &OutputFile, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 138 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 139 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
| 140 | unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | //===---------------------------------------------------------------------===// |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 144 | // LLC Implementation of AbstractIntepreter interface |
| 145 | // |
| 146 | class LLC : public AbstractInterpreter { |
Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 147 | std::string LLCPath; // The path to the LLC executable. |
| 148 | std::vector<std::string> ToolArgs; // Extra args to pass to LLC. |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 149 | CC *cc; |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 150 | bool UseIntegratedAssembler; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 151 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 152 | public: |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 153 | LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args, |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 154 | bool useIntegratedAssembler) |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 155 | : LLCPath(llcPath), cc(cc), |
| 156 | UseIntegratedAssembler(useIntegratedAssembler) { |
Bill Wendling | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 157 | ToolArgs.clear(); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 158 | if (Args) |
| 159 | ToolArgs = *Args; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 160 | } |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 161 | ~LLC() override { delete cc; } |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 162 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 163 | /// compileProgram - Compile the specified program from bitcode to executable |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 164 | /// code. This does not produce any output, it is only used when debugging |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 165 | /// the code generator. Returns false if the code generator fails. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 166 | Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0, |
| 167 | unsigned MemoryLimit = 0) override; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 168 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 169 | Expected<int> ExecuteProgram( |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 170 | const std::string &Bitcode, const std::vector<std::string> &Args, |
| 171 | const std::string &InputFile, const std::string &OutputFile, |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 172 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
| 173 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
| 174 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 175 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 176 | Expected<CC::FileType> OutputCode(const std::string &Bitcode, |
| 177 | std::string &OutFile, unsigned Timeout = 0, |
| 178 | unsigned MemoryLimit = 0) override; |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Brian Gesiak | 5cc8920 | 2018-12-10 00:56:13 +0000 | [diff] [blame] | 181 | /// Find the first executable file \ExeName, either in the user's PATH or, |
| 182 | /// failing that, in the same directory as argv[0]. This allows us to find |
| 183 | /// another LLVM tool if it is built in the same directory. If no executable is |
| 184 | /// found, an error is returned. |
| 185 | ErrorOr<std::string> FindProgramByName(const std::string &ExeName, |
| 186 | const char *Argv0, void *MainAddr); |
| 187 | |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 188 | } // End llvm namespace |
| 189 | |
| 190 | #endif |