Change how the LLVM execution engine to constructed and printf which target triple and CPU is being used

This commit is contained in:
Magnus Norddahl 2016-10-20 21:57:45 +02:00
parent 1c2dcad36e
commit 1cd27ca98a

View file

@ -446,10 +446,18 @@ LLVMProgram::LLVMProgram()
InitializeNativeTarget(); InitializeNativeTarget();
InitializeNativeTargetAsmPrinter(); InitializeNativeTargetAsmPrinter();
mContext = std::make_unique<LLVMContext>();
mModule = std::make_unique<Module>("render", context());
}
void LLVMProgram::CreateEE()
{
using namespace llvm;
std::string errorstring; std::string errorstring;
#if 0
std::string targetTriple = sys::getProcessTriple(); std::string targetTriple = sys::getProcessTriple();
std::string cpuName = sys::getHostCPUName();
StringMap<bool> cpuFeatures; StringMap<bool> cpuFeatures;
sys::getHostCPUFeatures(cpuFeatures); sys::getHostCPUFeatures(cpuFeatures);
std::string cpuFeaturesStr; std::string cpuFeaturesStr;
@ -461,40 +469,32 @@ LLVMProgram::LLVMProgram()
cpuFeaturesStr += it.getKey(); cpuFeaturesStr += it.getKey();
} }
DPrintf(DMSG_SPAMMY, "LLVM target triple: %s\n", targetTriple.c_str()); Printf("LLVM CPU features: %s\n", cpuFeaturesStr.c_str());
DPrintf(DMSG_SPAMMY, "LLVM CPU and features: %s, %s\n", cpuName.c_str(), cpuFeaturesStr.c_str());
const Target *target = TargetRegistry::lookupTarget(targetTriple, errorstring);
if (!target)
I_FatalError("Could not find LLVM target: %s", errorstring.c_str());
TargetOptions opt;
#if LLVM_VERSION_MAJOR < 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 9)
Reloc::Model relocModel = Reloc::Default;
#else
auto relocModel = Optional<Reloc::Model>();
#endif #endif
machine = target->createTargetMachine(targetTriple, cpuName, cpuFeaturesStr, opt, relocModel, CodeModel::JITDefault, CodeGenOpt::Aggressive);
llvm::Module *module = mModule.get();
EngineBuilder engineBuilder(std::move(mModule));
engineBuilder.setErrorStr(&errorstring);
engineBuilder.setOptLevel(CodeGenOpt::Aggressive);
engineBuilder.setEngineKind(EngineKind::JIT);
engineBuilder.setMCPU(sys::getHostCPUName());
machine = engineBuilder.selectTarget();
if (!machine) if (!machine)
I_FatalError("Could not create LLVM target machine"); I_FatalError("Could not create LLVM target machine");
mContext = std::make_unique<LLVMContext>(); std::string targetTriple = machine->getTargetTriple().getTriple();
std::string cpuName = machine->getTargetCPU();
Printf("LLVM target triple: %s\n", targetTriple.c_str());
Printf("LLVM target CPU: %s\n", cpuName.c_str());
mModule = std::make_unique<Module>("render", context()); module->setTargetTriple(targetTriple);
mModule->setTargetTriple(targetTriple);
#if LLVM_VERSION_MAJOR < 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 8) #if LLVM_VERSION_MAJOR < 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 8)
mModule->setDataLayout(new DataLayout(*machine->getSubtargetImpl()->getDataLayout())); module->setDataLayout(new DataLayout(*machine->getSubtargetImpl()->getDataLayout()));
#else #else
mModule->setDataLayout(machine->createDataLayout()); module->setDataLayout(machine->createDataLayout());
#endif #endif
} legacy::FunctionPassManager PerFunctionPasses(module);
void LLVMProgram::CreateEE()
{
using namespace llvm;
legacy::FunctionPassManager PerFunctionPasses(mModule.get());
legacy::PassManager PerModulePasses; legacy::PassManager PerModulePasses;
#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8) #if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8)
@ -514,7 +514,7 @@ void LLVMProgram::CreateEE()
// Run function passes: // Run function passes:
PerFunctionPasses.doInitialization(); PerFunctionPasses.doInitialization();
for (llvm::Function &func : *mModule.get()) for (llvm::Function &func : *module)
{ {
if (!func.isDeclaration()) if (!func.isDeclaration())
PerFunctionPasses.run(func); PerFunctionPasses.run(func);
@ -522,15 +522,9 @@ void LLVMProgram::CreateEE()
PerFunctionPasses.doFinalization(); PerFunctionPasses.doFinalization();
// Run module passes: // Run module passes:
PerModulePasses.run(*mModule.get()); PerModulePasses.run(*module);
std::string errorstring; // Create execution engine and generate machine code
EngineBuilder engineBuilder(std::move(mModule));
engineBuilder.setErrorStr(&errorstring);
engineBuilder.setOptLevel(CodeGenOpt::Aggressive);
engineBuilder.setRelocationModel(Reloc::Static);
engineBuilder.setEngineKind(EngineKind::JIT);
mEngine.reset(engineBuilder.create(machine)); mEngine.reset(engineBuilder.create(machine));
if (!mEngine) if (!mEngine)
I_FatalError("Could not create LLVM execution engine: %s", errorstring.c_str()); I_FatalError("Could not create LLVM execution engine: %s", errorstring.c_str());