从另一个 llvm::Module 获取函数声明
在我的应用程序中,我有 2 个 LLVM 模块 - 运行时模块(包含 void foo(int * a)
函数定义)和可执行模块(我使用 LLVM C++ API 创建)。
在我的可执行模块中,我创建了 int main(int argc, char ** argv)
并希望将 llvm::CallInst
放入其主体中,这将调用 foo来自运行时模块的 ()
函数。
这是我的代码:
Function * fooF = Function::Create(runtimeModule->getFunction("foo")->getFunctionType(),
GlobalValue::WeakAnyLinkage, "foo", execModule);
之后,我将两个模块链接在一起:
Linker linker("blabla", execModule, false);
linker.LinkInFile("/path/to/runtime.bc", false);
execModule = linker.releaseModule();
这编译正常,但是当我在链接的模块上运行 Verifier pass 时,我得到:
Global is external, but doesn't have external or dllimport or weak linkage!
void (%i32*)* @foo
invalid linkage type for function declaration
void (%i32*)* @foo
值得一提的是,运行时模块中的所有全局变量都是使用 Internalize pass 内部化的。链接之后,但在运行验证程序之前,我正在运行“死亡全局消除”传递以及其他一些优化。当我对生成的模块执行 dump()
时,我发现来自运行时模块的 @foo
也被删除了,尽管它被 main( 使用) )
。看来,LLVM认为运行时的@foo
定义和可执行文件中的@foo
声明是不相关的。
我尝试过使用链接类型 - 没有运气。
那么,从另一个模块创建对该函数的调用的正确方法是什么?
In my application i have 2 LLVM modules - the runtime one (which contains void foo(int * a)
function definition) and executable one (which i'm creating using LLVM C++ API).
In my executable module i create int main(int argc, char ** argv)
and want to put llvm::CallInst
into it's body, which would call foo()
function from runtime module.
Here is my code:
Function * fooF = Function::Create(runtimeModule->getFunction("foo")->getFunctionType(),
GlobalValue::WeakAnyLinkage, "foo", execModule);
After that, i link two modules together:
Linker linker("blabla", execModule, false);
linker.LinkInFile("/path/to/runtime.bc", false);
execModule = linker.releaseModule();
This compiles OK, however when i run Verifier pass on linked module i get:
Global is external, but doesn't have external or dllimport or weak linkage!
void (%i32*)* @foo
invalid linkage type for function declaration
void (%i32*)* @foo
It's worth mentioning, that all globals in runtime module are internalized using Internalize pass. After linking, but before running Verifier, i'm running Dead Global Elimination pass amongst some other optimizations. And when i do dump()
on resulting module, i see, that @foo
which is coming from runtime module gets removed too, despite it's used by main()
. It seems, LLVM thinks that @foo
definition in runtime and @foo
declaration in executable are unrelated.
I've tried to play with linkage types - no luck.
So, what is the right way to create a call to the function from another module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我已经解决了,但我仍然不明白问题出在哪里。在构建运行时位码模块期间,我一直在对其应用内部化转换。所以我尝试在链接后在运行时执行此操作,它对我有帮助。
啊,我一直在使用
GlobalValue::WeakAnyLinkage
。Ok, i've fixed it, but i still can't understand what was the problem. During building of my runtime bitcode module, i've been applying internalize transformation on it. So i tried to do this at run-time after linking and it helped me.
Ah, and i've been using
GlobalValue::WeakAnyLinkage
.