草庐IT

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

coder 2024-02-12 原文

我想让我的大部分程序成为普通编译的 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

其中 pointerInc() 会将 JIT 代码插入当前 BasicBlock 以增加 toppointerInc()代码是:

// 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 );
}

不幸的是,这不起作用。这是错误的(较大的)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)

有两个问题:

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

即使我将 JIT 函数设为空,代码会在调用该函数的行进行核心转储。 LLVM JIT 设置代码看起来像我见过的所有示例,所以我也看不出有什么问题。

一点帮助?


更新

如果我更改已弃用的行:

void *const func_ptr = exec->getPointerToFunction( func );

换行:

uint64_t const func_ptr = exec->getFunctionAddress( "func" );

然后 func_ptr 为空。

最佳答案

在使用 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 );
}

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

vector<GenericValue> noargs;
exec->runFunction( func, noargs );

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

关于c++ - 与 LLVM JIT 代码共享 C++ 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31469956/

有关c++ - 与 LLVM JIT 代码共享 C++ 指针的更多相关文章

  1. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  2. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  5. ruby-on-rails - 浏览 Ruby 源代码 - 2

    我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru

  6. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  7. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  8. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  9. 程序员如何提高代码能力? - 2

    前言作为一名程序员,自己的本质工作就是做程序开发,那么程序开发的时候最直接的体现就是代码,检验一个程序员技术水平的一个核心环节就是开发时候的代码能力。众所周知,程序开发的水平提升是一个循序渐进的过程,每一位程序员都是从“菜鸟”变成“大神”的,所以程序员在程序开发过程中的代码能力也是根据平时开发中的业务实践来积累和提升的。提高代码能力核心要素程序员要想提高自身代码能力,尤其是新晋程序员的代码能力有很大的提升空间的时候,需要针对性的去提高自己的代码能力。提高代码能力其实有几个比较关键的点,只要把握住这些方面,就能很好的、快速的提高自己的一部分代码能力。1、多去阅读开源项目,如有机会可以亲自参与开源

  10. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

随机推荐