gpt4 book ai didi

LLVM API : correct way to create/dispose

转载 作者:行者123 更新时间:2023-12-04 20:40:33 29 4
gpt4 key购买 nike

我正在尝试使用 LLVM C API 实现一个简单的 JIT 编译器。到目前为止,我在生成 IR 代码并执行它时没有问题,也就是说:直到我开始处理对象并重新创建它们。

我基本上想做的是在引擎不再使用它们时清理 JIT 的资源。我基本上试图做的是这样的:

while (true)
{
// Initialize module & builder
InitializeCore(GetGlobalPassRegistry());
module = ModuleCreateWithName(some_unique_name);
builder = CreateBuilder();

// Initialize target & execution engine
InitializeNativeTarget();
engine = CreateExecutionEngineForModule(...);
passmgr = CreateFunctionPassManagerForModule(module);
AddTargetData(GetExecutionEngineTargetData(engine), passmgr);
InitializeFunctionPassManager(passmgr);

// [... my fancy JIT code ...] --** Will give a serious error the second iteration

// Destroy
DisposePassManager(passmgr);
DisposeExecutionEngine(engine);
DisposeBuilder(builder);
// DisposeModule(module); //--> Commented out: Deleted by execution engine

Shutdown();
}

但是,这似乎工作不正常:循环的第二次迭代我得到了一个非常糟糕的错误......

总结一下:销毁和重新创建 LLVM API 的正确方法是什么?

最佳答案

将此作为答案发布,因为代码太长。如果可能并且没有其他限制,请尝试像这样使用 LLVM。我很确定 Shutdown()循环内部是这里的罪魁祸首。而且我认为保留 Builder 不会有什么坏处。外面也是。这很好地反射(reflect)了我在 JIT 中使用 LLVM 的方式。

InitializeCore(GetGlobalPassRegistry());
InitializeNativeTarget();
builder = CreateBuilder();

while (true)
{
// Initialize module & builder

module = ModuleCreateWithName(some_unique_name);


// Initialize target & execution engine
engine = CreateExecutionEngineForModule(...);
passmgr = CreateFunctionPassManagerForModule(module);
AddTargetData(GetExecutionEngineTargetData(engine), passmgr);
InitializeFunctionPassManager(passmgr);

// [... my fancy JIT code ...] --** Will give a serious error the second iteration

// Destroy
DisposePassManager(passmgr);
DisposeExecutionEngine(engine);
}
DisposeBuilder(builder);
Shutdown();

关于LLVM API : correct way to create/dispose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27103943/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com