与LLVM JIT代码共享C++指针

Pau*_*cas 5 c++ llvm llvm-ir llvm-c++-api

我希望我的大部分程序都是一个通常编译的C++程序.所述程序使用一块连续的内存用于堆栈.堆栈的顶部由普通指针维护.

我想与通过LLVM JIT生成的代码共享该指针.例如,给定:

llvm::InitializeNativeTarget();

llvm::LLVMContext ctx;
std::unique_ptr<llvm::Module> uptr_module = llvm::make_unique<llvm::Module>( "lt", ctx );
llvm::Module *const module = uptr_module.get();

int *const stack = new int[100];
int *top = stack;                 // I want this pointer to be shared with JIT'd code

llvm::Function *const func = llvm::cast<llvm::Function>(
    module->getOrInsertFunction( "func", llvm::Type::getVoidTy( ctx ), (llvm::Type*)0 )
);
llvm::BasicBlock *const block = llvm::BasicBlock::Create( ctx, "entry", func );

pointerInc( &top, block );        // Increment the pointer in JIT'd code

llvm::ReturnInst::Create( ctx, block );
llvm::verifyFunction( *func, &llvm::outs() );
llvm::verifyModule( *module, &llvm::outs() );
module->dump();

llvm::EngineBuilder eb( std::move( uptr_module ) );
llvm::ExecutionEngine *const exec = eb.create();
assert( exec );

void *const func_ptr = exec->getPointerToFunction( func );
assert( func_ptr );
typedef void (*PFv_v)();
(*(PFv_v)func_ptr)();             // Call JIT'd function
Run Code Online (Sandbox Code Playgroud)

其中,pointerInc()将插入这些JIT过的代码插入到当前BasicBlock递增top.该pointerInc()代码是:

// Convert a raw C++ pointer into an LLVM Constant*.
template<typename T>
inline llvm::Value* ptrToValue( T **pptr, llvm::LLVMContext &ctx ) {
    return return llvm::ConstantInt::get( llvm::Type::getInt64Ty( ctx ), (uint64_t)pptr );
}

void pointerInc( llvm::Constant *pptrAsInt64, llvm::ConstantInt *sizeof_T,
                 llvm::BasicBlock *block ) {
    llvm::LLVMContext &ctx = block->getContext();

    llvm::Constant *const intToPtr8 = llvm::ConstantExpr::getIntToPtr(
        pptrAsInt64, llvm::PointerType::getUnqual( llvm::Type::getInt8Ty( ctx ) )
    );

    llvm::GetElementPtrInst *const inc =
        llvm::GetElementPtrInst::Create( intToPtr8, sizeof_T, "inc", block );

    llvm::CastInst *const cast = llvm::CastInst::CreatePointerCast(
        inc, llvm::Type::getInt64Ty( ctx ), "cast", block
    );

    llvm::Constant *const intToPtr64 = llvm::ConstantExpr::getIntToPtr(
        pptrAsInt64, llvm::PointerType::getUnqual( llvm::Type::getInt64Ty( ctx ) )
    );

    llvm::StoreInst *const store = new llvm::StoreInst( cast, intToPtr64, false, block );
    store->setAlignment( 8 );
}

template<typename T>
inline void pointerInc( T **pptr, llvm::BasicBlock *block ) {
    llvm::LLVMContext &ctx = block->getContext();
    llvm::ConstantInt *const sizeof_T =
        llvm::ConstantInt::get( llvm::Type::getInt64Ty( ctx ), sizeof( T ) );
    pointerInc( ptrToValue( pptr, ctx ), sizeof_T, block );
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用.这是(较大的)身体pointerInc()的错误.该代码实际上是从llc一个普通的C++程序生成的LLVM C++ API代码派生而来的,该程序使指针递增.

运行时,程序打印:

&p = 140734551679784
--------------------
; ModuleID = 'lt'

define void @func() {
entry:
  %inc = getelementptr i8* inttoptr (i64 140734551679784 to i8*), i64 4
  %cast = ptrtoint i8* %inc to i64
  store i64 %cast, i64* inttoptr (i64 140734551679784 to i64*), align 8
  ret void
}
Segmentation fault: 11 (core dumped)
Run Code Online (Sandbox Code Playgroud)

有两个问题:

  1. 它是否正确?我甚至可以做我想要的,即与JIT代码共享原始C++指针?
  2. 为什么倾销核心?

即使我使JIT'd函数为空,代码仍然会在调用该函数的行转储核心.LLVM JIT设置代码看起来就像我见过的所有示例,所以我也没有看到它的错误.

一点帮助?


更新

如果我更改了弃用的行:

void *const func_ptr = exec->getPointerToFunction( func );
Run Code Online (Sandbox Code Playgroud)

到新线:

uint64_t const func_ptr = exec->getFunctionAddress( "func" );
Run Code Online (Sandbox Code Playgroud)

然后func_ptr是null.

Pau*_*cas 2

经过更多的尝试lcc(并使用更好的 C++ 代码来填充它)后,我让它工作了:

llvm::Value* pointerToPointer( void *ptr, llvm::BasicBlock *block ) {
    using namespace llvm;
    LLVMContext &ctx = block->getContext();
    ConstantInt *const ptrAsInt =
        ConstantInt::get( IntegerType::get( ctx, 64 ), (uint64_t)ptr );
    PointerType *const Int8Ptr_type = Type::getInt8PtrTy( ctx );
    PointerType *const Int8PtrPtr_type = PointerType::getUnqual( Int8Ptr_type );
    return new IntToPtrInst( ptrAsInt, Int8PtrPtr_type, "pptr", block );
}

void pointerInc( llvm::Value *pptr, llvm::ConstantInt *sizeof_T,
                llvm::BasicBlock *block ) {
    using namespace llvm;
    LLVMContext &ctx = block->getContext();

    LoadInst *const ptr = new LoadInst( pptr, "ptr", block );
    ptr->setAlignment( sizeof(void*) );

    GetElementPtrInst *const inc =
        GetElementPtrInst::Create( ptr, sizeof_T, "inc", block );

    StoreInst *const store = new StoreInst( inc, pptr, block );
    store->setAlignment(sizeof(void*));
}

template<typename T>
inline void pointerInc( T **pptr, llvm::BasicBlock *block ) {
    using namespace llvm;
    LLVMContext &ctx = block->getContext();
    ConstantInt *const sizeof_T = ConstantInt::get(
        IntegerType::get( ctx, 64 ), (uint64_t)sizeof( T )
    );
    pointerInc( pointerToPointer( pptr, block ), sizeof_T, block );
}
Run Code Online (Sandbox Code Playgroud)

但是,只有当通过以下方式调用 JIT 函数时,程序才能成功运行:

vector<GenericValue> noargs;
exec->runFunction( func, noargs );
Run Code Online (Sandbox Code Playgroud)

使用getFunctionAddress()getPointerToFunction()转储核心。我仍然没有答案。