llvm clang JIT 程序 5.0.2

   windows vs2015 based:网上各种 jit demo已经是无效了。至少在3.9 以后的版本都是无法直接使用的。实际使用情况是这样的:

   使用llvm自带HowToUseJITdemo修改: 代码如下,并需要添加lib:

..\..\Release\lib\LLVMIRReader.lib

..\..\Release\lib\LLVMAsmParser.lib

..\..\Release\lib\LLVMMCJIT.lib

  以下是代码 直接cpp 贴过来的。

#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/MCJIT/MCJIT.h"

#include <algorithm>
#include <cassert>
#include <memory>
#include <vector>
#include <system_error>

#include <string>

using namespace llvm;


int main() {
    //MyMethod();
    InitializeNativeTarget();
    InitializeNativeTargetAsmPrinter();
    //InitializeAllTargets();

    MCJIT::Register();
    LLVMContext Context;
    SMDiagnostic Err;
    std::string error_code;
    //Module *mod = llvm::parseIRFile("hello.bc", Err, context);
    //std::unique_ptr<Module> Owner = make_unique<Module>("g:/hello.bc", Context);
    std::unique_ptr<Module> Owner = parseIRFile("g:/hello.bc", Err, Context);
    Module *M = Owner.get();
    /*EngineBuilder eb = EngineBuilder(mod);
    eb.setEngineKind(EngineKind::JIT);
    eb.setErrorStr(&error_code);
    ExecutionEngine* EE = eb.create();*/
    
    //Function *HelloF = cast<Function>(M->getOrInsertFunction("main", Type::getInt32Ty(Context)));
    Function *HelloF = M->getFunction("main");
    //Function* func = NULL;
    //func = mod->getFunction("_Z13CalcVaildRowsPPii");
    /*if (func == NULL)
    {
        std::cout << "--Night-- get function error.." << std::endl;
        return 0;
    }*/
    //BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", HelloF);

    std::string ErrStr;
    ExecutionEngine* EE(NULL);
    //EE = EngineBuilder(std::move(Owner)).create();
    //
    RTDyldMemoryManager* RTDyldMM = new SectionMemoryManager();
    EE = EngineBuilder(std::move(Owner))
        .setEngineKind(EngineKind::JIT)
        .setErrorStr(&ErrStr)
        .setVerifyModules(true)
//         .setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>(RTDyldMM))
//         .setOptLevel(CodeGenOpt::Default)
        .create();
    //
    EE->finalizeObject();
    std::vector<GenericValue> noargs;
    GenericValue gv = EE->runFunction(HelloF, noargs);

    /*void* jitAddr = EE->getPointerToFunction(func);
    EE->freeMachineCodeForFunction(func);*/
    delete EE;
    llvm_shutdown();
    return 0;
}





猜你喜欢

转载自blog.csdn.net/zsyddl2/article/details/80647936