ExecutionEngine: refactor interface

The OptLevel is now redundant with the TargetMachine*.
And selectTarget() isn't really JIT-specific and could probably
get refactored into one of the lower level libraries.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@146355 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dylan Noblesmith 2011-12-12 04:20:36 +00:00
parent d93e4c3496
commit 9ea47179e6
7 changed files with 30 additions and 38 deletions

View File

@ -42,6 +42,7 @@ class MachineCodeInfo;
class Module; class Module;
class MutexGuard; class MutexGuard;
class TargetData; class TargetData;
class Triple;
class Type; class Type;
/// \brief Helper class for helping synchronize access to the global address map /// \brief Helper class for helping synchronize access to the global address map
@ -133,14 +134,12 @@ protected:
Module *M, Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM); TargetMachine *TM);
static ExecutionEngine *(*MCJITCtor)( static ExecutionEngine *(*MCJITCtor)(
Module *M, Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM); TargetMachine *TM);
static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr); static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
@ -584,7 +583,7 @@ public:
/// selectTarget - Pick a target either via -march or by guessing the native /// selectTarget - Pick a target either via -march or by guessing the native
/// arch. Add any CPU features specified via -mcpu or -mattr. /// arch. Add any CPU features specified via -mcpu or -mattr.
static TargetMachine *selectTarget(Module *M, static TargetMachine *selectTarget(const Triple &TargetTriple,
StringRef MArch, StringRef MArch,
StringRef MCPU, StringRef MCPU,
const SmallVectorImpl<std::string>& MAttrs, const SmallVectorImpl<std::string>& MAttrs,

View File

@ -28,6 +28,7 @@
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/Host.h" #include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include <cmath> #include <cmath>
@ -41,14 +42,12 @@ ExecutionEngine *(*ExecutionEngine::JITCtor)(
Module *M, Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM) = 0; TargetMachine *TM) = 0;
ExecutionEngine *(*ExecutionEngine::MCJITCtor)( ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
Module *M, Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM) = 0; TargetMachine *TM) = 0;
ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
@ -436,13 +435,14 @@ ExecutionEngine *ExecutionEngine::createJIT(Module *M,
StringRef MCPU = ""; StringRef MCPU = "";
SmallVector<std::string, 1> MAttrs; SmallVector<std::string, 1> MAttrs;
Triple TT(M->getTargetTriple());
// TODO: permit custom TargetOptions here // TODO: permit custom TargetOptions here
TargetMachine *TM = TargetMachine *TM =
EngineBuilder::selectTarget(M, MArch, MCPU, MAttrs, TargetOptions(), RM, EngineBuilder::selectTarget(TT, MArch, MCPU, MAttrs, TargetOptions(), RM,
CMM, OL, ErrorStr); CMM, OL, ErrorStr);
if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0; if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
return ExecutionEngine::JITCtor(M, ErrorStr, JMM, OL, GVsWithCode, TM); return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
} }
ExecutionEngine *EngineBuilder::create() { ExecutionEngine *EngineBuilder::create() {
@ -467,18 +467,25 @@ ExecutionEngine *EngineBuilder::create() {
// Unless the interpreter was explicitly selected or the JIT is not linked, // Unless the interpreter was explicitly selected or the JIT is not linked,
// try making a JIT. // try making a JIT.
if (WhichEngine & EngineKind::JIT) { if (WhichEngine & EngineKind::JIT) {
if (TargetMachine *TM = EngineBuilder::selectTarget(M, MArch, MCPU, MAttrs, Triple TT(M->getTargetTriple());
if (TargetMachine *TM = EngineBuilder::selectTarget(TT, MArch, MCPU, MAttrs,
Options, Options,
RelocModel, CMModel, RelocModel, CMModel,
OptLevel, ErrorStr)) { OptLevel, ErrorStr)) {
if (!TM->getTarget().hasJIT()) {
errs() << "WARNING: This target JIT is not designed for the host"
<< " you are running. If bad things happen, please choose"
<< " a different -march switch.\n";
}
if (UseMCJIT && ExecutionEngine::MCJITCtor) { if (UseMCJIT && ExecutionEngine::MCJITCtor) {
ExecutionEngine *EE = ExecutionEngine *EE =
ExecutionEngine::MCJITCtor(M, ErrorStr, JMM, OptLevel, ExecutionEngine::MCJITCtor(M, ErrorStr, JMM,
AllocateGVsWithCode, TM); AllocateGVsWithCode, TM);
if (EE) return EE; if (EE) return EE;
} else if (ExecutionEngine::JITCtor) { } else if (ExecutionEngine::JITCtor) {
ExecutionEngine *EE = ExecutionEngine *EE =
ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, ExecutionEngine::JITCtor(M, ErrorStr, JMM,
AllocateGVsWithCode, TM); AllocateGVsWithCode, TM);
if (EE) return EE; if (EE) return EE;
} }

View File

@ -206,7 +206,6 @@ void DarwinRegisterFrame(void* FrameBegin) {
ExecutionEngine *JIT::createJIT(Module *M, ExecutionEngine *JIT::createJIT(Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM) { TargetMachine *TM) {
// Try to register the program as a source of symbols to resolve against. // Try to register the program as a source of symbols to resolve against.
@ -216,7 +215,7 @@ ExecutionEngine *JIT::createJIT(Module *M,
// If the target supports JIT code generation, create the JIT. // If the target supports JIT code generation, create the JIT.
if (TargetJITInfo *TJ = TM->getJITInfo()) { if (TargetJITInfo *TJ = TM->getJITInfo()) {
return new JIT(M, *TM, *TJ, JMM, OptLevel, GVsWithCode); return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
} else { } else {
if (ErrorStr) if (ErrorStr)
*ErrorStr = "target does not support JIT code generation"; *ErrorStr = "target does not support JIT code generation";
@ -268,7 +267,7 @@ extern "C" {
} }
JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
JITMemoryManager *JMM, CodeGenOpt::Level OptLevel, bool GVsWithCode) JITMemoryManager *JMM, bool GVsWithCode)
: ExecutionEngine(M), TM(tm), TJI(tji), AllocateGVsWithCode(GVsWithCode), : ExecutionEngine(M), TM(tm), TJI(tji), AllocateGVsWithCode(GVsWithCode),
isAlreadyCodeGenerating(false) { isAlreadyCodeGenerating(false) {
setTargetData(TM.getTargetData()); setTargetData(TM.getTargetData());

View File

@ -78,8 +78,7 @@ class JIT : public ExecutionEngine {
JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
JITMemoryManager *JMM, CodeGenOpt::Level OptLevel, JITMemoryManager *JMM, bool AllocateGVsWithCode);
bool AllocateGVsWithCode);
public: public:
~JIT(); ~JIT();
@ -185,7 +184,6 @@ public:
static ExecutionEngine *createJIT(Module *M, static ExecutionEngine *createJIT(Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM); TargetMachine *TM);

View File

@ -36,7 +36,6 @@ extern "C" void LLVMLinkInMCJIT() {
ExecutionEngine *MCJIT::createJIT(Module *M, ExecutionEngine *MCJIT::createJIT(Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM) { TargetMachine *TM) {
// Try to register the program as a source of symbols to resolve against. // Try to register the program as a source of symbols to resolve against.
@ -46,8 +45,7 @@ ExecutionEngine *MCJIT::createJIT(Module *M,
// If the target supports JIT code generation, create the JIT. // If the target supports JIT code generation, create the JIT.
if (TargetJITInfo *TJ = TM->getJITInfo()) if (TargetJITInfo *TJ = TM->getJITInfo())
return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), OptLevel, return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), GVsWithCode);
GVsWithCode);
if (ErrorStr) if (ErrorStr)
*ErrorStr = "target does not support JIT code generation"; *ErrorStr = "target does not support JIT code generation";
@ -55,8 +53,7 @@ ExecutionEngine *MCJIT::createJIT(Module *M,
} }
MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji, MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel, RTDyldMemoryManager *MM, bool AllocateGVsWithCode)
bool AllocateGVsWithCode)
: ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) { : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
setTargetData(TM->getTargetData()); setTargetData(TM->getTargetData());

View File

@ -24,8 +24,7 @@ namespace llvm {
class MCJIT : public ExecutionEngine { class MCJIT : public ExecutionEngine {
MCJIT(Module *M, TargetMachine *tm, TargetJITInfo &tji, MCJIT(Module *M, TargetMachine *tm, TargetJITInfo &tji,
RTDyldMemoryManager *MemMgr, CodeGenOpt::Level OptLevel, RTDyldMemoryManager *MemMgr, bool AllocateGVsWithCode);
bool AllocateGVsWithCode);
TargetMachine *TM; TargetMachine *TM;
MCContext *Ctx; MCContext *Ctx;
@ -79,7 +78,6 @@ public:
static ExecutionEngine *createJIT(Module *M, static ExecutionEngine *createJIT(Module *M,
std::string *ErrorStr, std::string *ErrorStr,
JITMemoryManager *JMM, JITMemoryManager *JMM,
CodeGenOpt::Level OptLevel,
bool GVsWithCode, bool GVsWithCode,
TargetMachine *TM); TargetMachine *TM);

View File

@ -7,26 +7,26 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// //
// This just asks the TargetRegistry for the appropriate JIT to use, and allows // This just asks the TargetRegistry for the appropriate target to use, and
// the user to specify a specific one on the commandline with -march=x. Clients // allows the user to specify a specific one on the commandline with -march=x,
// should initialize targets prior to calling createJIT. // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
// calling selectTarget().
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/Module.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/MC/SubtargetFeature.h" #include "llvm/MC/SubtargetFeature.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Host.h" #include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm; using namespace llvm;
/// selectTarget - Pick a target either via -march or by guessing the native /// selectTarget - Pick a target either via -march or by guessing the native
/// arch. Add any CPU features specified via -mcpu or -mattr. /// arch. Add any CPU features specified via -mcpu or -mattr.
TargetMachine *EngineBuilder::selectTarget(Module *Mod, TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
StringRef MArch, StringRef MArch,
StringRef MCPU, StringRef MCPU,
const SmallVectorImpl<std::string>& MAttrs, const SmallVectorImpl<std::string>& MAttrs,
@ -35,7 +35,7 @@ TargetMachine *EngineBuilder::selectTarget(Module *Mod,
CodeModel::Model CM, CodeModel::Model CM,
CodeGenOpt::Level OL, CodeGenOpt::Level OL,
std::string *ErrorStr) { std::string *ErrorStr) {
Triple TheTriple(Mod->getTargetTriple()); Triple TheTriple(TargetTriple);
if (TheTriple.getTriple().empty()) if (TheTriple.getTriple().empty())
TheTriple.setTriple(sys::getDefaultTargetTriple()); TheTriple.setTriple(sys::getDefaultTargetTriple());
@ -57,7 +57,7 @@ TargetMachine *EngineBuilder::selectTarget(Module *Mod,
} }
// Adjust the triple to match (if known), otherwise stick with the // Adjust the triple to match (if known), otherwise stick with the
// module/host triple. // requested/host triple.
Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
if (Type != Triple::UnknownArch) if (Type != Triple::UnknownArch)
TheTriple.setArch(Type); TheTriple.setArch(Type);
@ -71,12 +71,6 @@ TargetMachine *EngineBuilder::selectTarget(Module *Mod,
} }
} }
if (!TheTarget->hasJIT()) {
errs() << "WARNING: This target JIT is not designed for the host you are"
<< " running. If bad things happen, please choose a different "
<< "-march switch.\n";
}
// Package up features to be passed to target/subtarget // Package up features to be passed to target/subtarget
std::string FeaturesStr; std::string FeaturesStr;
if (!MAttrs.empty()) { if (!MAttrs.empty()) {