Ejemplo n.º 1
0
int llvm::runOrcLazyJIT(std::vector<std::unique_ptr<Module>> Ms,
                        const std::vector<std::string> &Args) {
  // Add the program's symbols into the JIT's search space.
  if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
    errs() << "Error loading program symbols.\n";
    return 1;
  }

  // Grab a target machine and try to build a factory function for the
  // target-specific Orc callback manager.
  EngineBuilder EB;
  EB.setOptLevel(getOptLevel());
  auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
  Triple T(TM->getTargetTriple());
  auto CompileCallbackMgr = orc::createLocalCompileCallbackManager(T, 0);

  // If we couldn't build the factory function then there must not be a callback
  // manager for this target. Bail out.
  if (!CompileCallbackMgr) {
    errs() << "No callback manager available for target '"
           << TM->getTargetTriple().str() << "'.\n";
    return 1;
  }

  auto IndirectStubsMgrBuilder = orc::createLocalIndirectStubsManagerBuilder(T);

  // If we couldn't build a stubs-manager-builder for this target then bail out.
  if (!IndirectStubsMgrBuilder) {
    errs() << "No indirect stubs manager available for target '"
           << TM->getTargetTriple().str() << "'.\n";
    return 1;
  }

  // Everything looks good. Build the JIT.
  OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
               std::move(IndirectStubsMgrBuilder),
               OrcInlineStubs);

  // Add the module, look up main and run it.
  for (auto &M : Ms)
    cantFail(J.addModule(std::shared_ptr<Module>(std::move(M))));

  if (auto MainSym = J.findSymbol("main")) {
    typedef int (*MainFnPtr)(int, const char*[]);
    std::vector<const char *> ArgV;
    for (auto &Arg : Args)
      ArgV.push_back(Arg.c_str());
    auto Main = fromTargetAddress<MainFnPtr>(cantFail(MainSym.getAddress()));
    return Main(ArgV.size(), (const char**)ArgV.data());
  } else if (auto Err = MainSym.takeError())
    logAllUnhandledErrors(std::move(Err), llvm::errs(), "");
  else
    errs() << "Could not find main function.\n";

  return 1;
}
Ejemplo n.º 2
0
int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
  // Add the program's symbols into the JIT's search space.
  if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
    errs() << "Error loading program symbols.\n";
    return 1;
  }

  // Grab a target machine and try to build a factory function for the
  // target-specific Orc callback manager.
  EngineBuilder EB;
  EB.setOptLevel(getOptLevel());
  auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
  auto &Context = getGlobalContext();
  auto CallbackMgrBuilder =
    OrcLazyJIT::createCallbackMgrBuilder(Triple(TM->getTargetTriple()));

  // If we couldn't build the factory function then there must not be a callback
  // manager for this target. Bail out.
  if (!CallbackMgrBuilder) {
    errs() << "No callback manager available for target '"
           << TM->getTargetTriple().str() << "'.\n";
    return 1;
  }

  auto IndirectStubsMgrBuilder =
    OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));

  // If we couldn't build a stubs-manager-builder for this target then bail out.
  if (!IndirectStubsMgrBuilder) {
    errs() << "No indirect stubs manager available for target '"
           << TM->getTargetTriple().str() << "'.\n";
    return 1;
  }

  // Everything looks good. Build the JIT.
  OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder,
               std::move(IndirectStubsMgrBuilder),
               OrcInlineStubs);

  // Add the module, look up main and run it.
  auto MainHandle = J.addModule(std::move(M));
  auto MainSym = J.findSymbolIn(MainHandle, "main");

  if (!MainSym) {
    errs() << "Could not find main function.\n";
    return 1;
  }

  typedef int (*MainFnPtr)(int, char*[]);
  auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
  return Main(ArgC, ArgV);
}
Ejemplo n.º 3
0
ExecutionEngine* createExecutionEngine(Module* mod) {

    if (globalExecEngine == 0)
    {
        //we first have to initialize the native target for code generation
        const bool initFailed = InitializeNativeTarget();

        if (initFailed) {
            errs() << "ERROR: could not initialize native target (required for "
                << "LLVM execution engine)\n";
            return NULL;
        }

        std::string errorMessage = "";

        EngineBuilder eb = EngineBuilder(mod);
        eb.setEngineKind(EngineKind::JIT);
        eb.setErrorStr(&errorMessage);
        eb.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
        eb.setOptLevel(CodeGenOpt::Aggressive);
        eb.setAllocateGVsWithCode(false);
        eb.setCodeModel(CodeModel::Default);
        //eb.setMArch("x86-64");
        //eb.setMCPU("corei7");
        //std::vector<std::string> attrs;
        //attrs.push_back("+sse41");
        //eb.setMAttrs(attrs);


        globalExecEngine = eb.create();

        if (errorMessage != "") {
            errs() << "ERROR: could not create execution engine for module "
                << mod->getModuleIdentifier() << ": " << errorMessage << "\n";
            return NULL;
        }

        if (!globalExecEngine) {
            errs() << "ERROR: could not create execution engine for module "
                << mod->getModuleIdentifier() << "!\n";
            return NULL;
        }
    }

    return globalExecEngine;
}
Ejemplo n.º 4
0
int main(int argc, char**argv) {
    InitializeNativeTarget();
    InitializeNativeTargetAsmPrinter();
    Module* Mod = makeLLVMModule();
    verifyModule(*Mod, PrintMessageAction);
    PassManager PM;
    PM.add(createPrintModulePass(&outs()));
    PM.run(*Mod);
    //ExecutionEngine *exe=::llvm::Interpreter::create(Mod);
    //ExecutionEngine *exe = EngineBuilder(Mod).create();
    //printf("----%p\n",exe);
    EngineBuilder eb = EngineBuilder(Mod);

#if LLVM_VERSION >= 33
    eb.setEngineKind(EngineKind::JIT);
    eb.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
    eb.setAllocateGVsWithCode(false);
    eb.setOptLevel(CodeGenOpt::Aggressive);
    eb.setCodeModel(CodeModel::JITDefault);
#endif

    eb.setMArch("x86-64");
    eb.setMCPU("corei7-avx");
    eb.setUseMCJIT(true);
    ExecutionEngine *exe = eb.create();

    std::vector<GenericValue> args;
    GenericValue GVArgc;
    GVArgc.IntVal = APInt(32, 24);
    args.push_back(GVArgc);
    //printf("xxxx:%p,%p\n",func_factorial,(void*)(&exe->runFunction));
    GenericValue ret=exe->runFunction(func_factorial, args);
    printf("ret=%llu\n",ret.IntVal.getZExtValue());

#if LLVM_VERSION < 33
    exe->freeMachineCodeForFunction(func_factorial);
#endif

    delete exe;
    //llvm_shutdown();
    return 0;
}