ToolRunner.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
  2. //
  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
  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. #ifndef LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
  16. #define LLVM_TOOLS_BUGPOINT_TOOLRUNNER_H
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/Path.h"
  21. #include "llvm/Support/SystemUtils.h"
  22. #include <exception>
  23. #include <vector>
  24. namespace llvm {
  25. extern cl::opt<bool> SaveTemps;
  26. extern Triple TargetTriple;
  27. class LLC;
  28. //===---------------------------------------------------------------------===//
  29. // CC abstraction
  30. //
  31. class CC {
  32. std::string CCPath; // The path to the cc executable.
  33. std::string RemoteClientPath; // The path to the rsh / ssh executable.
  34. std::vector<std::string> ccArgs; // CC-specific arguments.
  35. CC(StringRef ccPath, StringRef RemotePath,
  36. const std::vector<std::string> *CCArgs)
  37. : CCPath(std::string(ccPath)), RemoteClientPath(std::string(RemotePath)) {
  38. if (CCArgs)
  39. ccArgs = *CCArgs;
  40. }
  41. public:
  42. enum FileType { AsmFile, ObjectFile, CFile };
  43. static CC *create(const char *Argv0, std::string &Message,
  44. const std::string &CCBinary,
  45. const std::vector<std::string> *Args);
  46. /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
  47. /// either a .s file, or a .c file, specified by FileType), with the specified
  48. /// arguments. Standard input is specified with InputFile, and standard
  49. /// Output is captured to the specified OutputFile location. The SharedLibs
  50. /// option specifies optional native shared objects that can be loaded into
  51. /// the program for execution.
  52. ///
  53. Expected<int> ExecuteProgram(
  54. const std::string &ProgramFile, const std::vector<std::string> &Args,
  55. FileType fileType, const std::string &InputFile,
  56. const std::string &OutputFile,
  57. const std::vector<std::string> &CCArgs = std::vector<std::string>(),
  58. unsigned Timeout = 0, unsigned MemoryLimit = 0);
  59. /// MakeSharedObject - This compiles the specified file (which is either a .c
  60. /// file or a .s file) into a shared object.
  61. ///
  62. Error MakeSharedObject(const std::string &InputFile, FileType fileType,
  63. std::string &OutputFile,
  64. const std::vector<std::string> &ArgsForCC);
  65. };
  66. //===---------------------------------------------------------------------===//
  67. /// AbstractInterpreter Class - Subclasses of this class are used to execute
  68. /// LLVM bitcode in a variety of ways. This abstract interface hides this
  69. /// complexity behind a simple interface.
  70. ///
  71. class AbstractInterpreter {
  72. virtual void anchor();
  73. public:
  74. static LLC *createLLC(const char *Argv0, std::string &Message,
  75. const std::string &CCBinary,
  76. const std::vector<std::string> *Args = nullptr,
  77. const std::vector<std::string> *CCArgs = nullptr,
  78. bool UseIntegratedAssembler = false);
  79. static AbstractInterpreter *
  80. createLLI(const char *Argv0, std::string &Message,
  81. const std::vector<std::string> *Args = nullptr);
  82. static AbstractInterpreter *
  83. createJIT(const char *Argv0, std::string &Message,
  84. const std::vector<std::string> *Args = nullptr);
  85. static AbstractInterpreter *
  86. createCustomCompiler(const char *Argv0, std::string &Message,
  87. const std::string &CompileCommandLine);
  88. static AbstractInterpreter *
  89. createCustomExecutor(const char *Argv0, std::string &Message,
  90. const std::string &ExecCommandLine);
  91. virtual ~AbstractInterpreter() {}
  92. /// compileProgram - Compile the specified program from bitcode to executable
  93. /// code. This does not produce any output, it is only used when debugging
  94. /// the code generator. It returns false if the code generator fails.
  95. virtual Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
  96. unsigned MemoryLimit = 0) {
  97. return Error::success();
  98. }
  99. /// Compile the specified program from bitcode to code understood by the CC
  100. /// driver (either C or asm). Returns an error if the code generator fails,,
  101. /// otherwise, the type of code emitted.
  102. virtual Expected<CC::FileType> OutputCode(const std::string &Bitcode,
  103. std::string &OutFile,
  104. unsigned Timeout = 0,
  105. unsigned MemoryLimit = 0) {
  106. return make_error<StringError>(
  107. "OutputCode not supported by this AbstractInterpreter!",
  108. inconvertibleErrorCode());
  109. }
  110. /// ExecuteProgram - Run the specified bitcode file, emitting output to the
  111. /// specified filename. This sets RetVal to the exit code of the program or
  112. /// returns an Error if a problem was encountered that prevented execution of
  113. /// the program.
  114. ///
  115. virtual Expected<int> ExecuteProgram(
  116. const std::string &Bitcode, const std::vector<std::string> &Args,
  117. const std::string &InputFile, const std::string &OutputFile,
  118. const std::vector<std::string> &CCArgs = std::vector<std::string>(),
  119. const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
  120. unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0;
  121. };
  122. //===---------------------------------------------------------------------===//
  123. // LLC Implementation of AbstractIntepreter interface
  124. //
  125. class LLC : public AbstractInterpreter {
  126. std::string LLCPath; // The path to the LLC executable.
  127. std::vector<std::string> ToolArgs; // Extra args to pass to LLC.
  128. CC *cc;
  129. bool UseIntegratedAssembler;
  130. public:
  131. LLC(const std::string &llcPath, CC *cc, const std::vector<std::string> *Args,
  132. bool useIntegratedAssembler)
  133. : LLCPath(llcPath), cc(cc),
  134. UseIntegratedAssembler(useIntegratedAssembler) {
  135. ToolArgs.clear();
  136. if (Args)
  137. ToolArgs = *Args;
  138. }
  139. ~LLC() override { delete cc; }
  140. /// compileProgram - Compile the specified program from bitcode to executable
  141. /// code. This does not produce any output, it is only used when debugging
  142. /// the code generator. Returns false if the code generator fails.
  143. Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
  144. unsigned MemoryLimit = 0) override;
  145. Expected<int> ExecuteProgram(
  146. const std::string &Bitcode, const std::vector<std::string> &Args,
  147. const std::string &InputFile, const std::string &OutputFile,
  148. const std::vector<std::string> &CCArgs = std::vector<std::string>(),
  149. const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
  150. unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
  151. Expected<CC::FileType> OutputCode(const std::string &Bitcode,
  152. std::string &OutFile, unsigned Timeout = 0,
  153. unsigned MemoryLimit = 0) override;
  154. };
  155. /// Find the first executable file \ExeName, either in the user's PATH or,
  156. /// failing that, in the same directory as argv[0]. This allows us to find
  157. /// another LLVM tool if it is built in the same directory. If no executable is
  158. /// found, an error is returned.
  159. ErrorOr<std::string> FindProgramByName(const std::string &ExeName,
  160. const char *Argv0, void *MainAddr);
  161. } // End llvm namespace
  162. #endif