在 llvm JIT 符号中找不到全局变量 table
global variable not found in llvm JIT symbol table
我正在尝试获取带有全局变量的 llvm::Module 以使用 KaleidoscopeJIT 编译器进行编译,但是,我在 JIT 编译器的符号查找中遇到错误。 (KaleidoscopeJIT.h 来自 https://github.com/llvm-mirror/llvm/blob/master/examples/Kaleidoscope/include/KaleidoscopeJIT.h 的源代码)
检查 LegacyRTDyldObjectLinkingLayerBase 中的 Symbol Table,我确实看到全局变量没有添加到 Symbol table。这是因为全局变量未初始化吗?如果是这样,我应该如何使用 llvm C++ api?
为结构指定初始化程序
我生成了如下所示的 IR 代码
ModuleID = 'my jit module'
source_filename = "my jit module"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
%g = type { double, double }
@r = external global %g
define double @b() {
entry_b:
%p = alloca %g, align 8
%0 = getelementptr %g, %g* %p, i32 0, i32 1
store double 1.170000e+02, double* %0, align 8
%1 = load %g, %g* %p, align 8
store %g %1, %g* @r, align 8
%2 = load double, double* %0, align 8
ret double %2
}
但是,当 JIT 编译器尝试编译函数“b”时,我收到一条错误消息
Failure value returned from cantFail wrapped call
Symbols not found: [ _r ]
尝试编译IR代码行时出现错误
store %g %1, %g* @r, align 8
因为JIT无法在JIT的符号table中找到对应于全局变量“r”的符号。
问题似乎是未初始化的全局变量以某种方式被优化掉并且没有添加到符号 table。
确保将变量添加到符号 table 的快速解决方法是使用“未定义值”对其进行初始化。
以下代码允许使用 c++ api
进行此类初始化
// code defining the struct type
std::vector<llvm::Type *> Members(2, llvm::Type::getDoubleTy(TheContext));
llvm::StructType *TypeG = llvm::StructType::create(TheContext,Members,"g",false);
// defining the global variable
TheModule->getOrInsertGlobal("r",TypeG);
llvm::GlobalVariable *gVar = TheModule->getNamedGlobal("r");
// initialize the variable with an undef value to ensure it is added to the symbol table
gVar->setInitializer(llvm::UndefValue::get(TypeG));
这解决了问题。
我正在尝试获取带有全局变量的 llvm::Module 以使用 KaleidoscopeJIT 编译器进行编译,但是,我在 JIT 编译器的符号查找中遇到错误。 (KaleidoscopeJIT.h 来自 https://github.com/llvm-mirror/llvm/blob/master/examples/Kaleidoscope/include/KaleidoscopeJIT.h 的源代码)
检查 LegacyRTDyldObjectLinkingLayerBase 中的 Symbol Table,我确实看到全局变量没有添加到 Symbol table。这是因为全局变量未初始化吗?如果是这样,我应该如何使用 llvm C++ api?
为结构指定初始化程序我生成了如下所示的 IR 代码
ModuleID = 'my jit module'
source_filename = "my jit module"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
%g = type { double, double }
@r = external global %g
define double @b() {
entry_b:
%p = alloca %g, align 8
%0 = getelementptr %g, %g* %p, i32 0, i32 1
store double 1.170000e+02, double* %0, align 8
%1 = load %g, %g* %p, align 8
store %g %1, %g* @r, align 8
%2 = load double, double* %0, align 8
ret double %2
}
但是,当 JIT 编译器尝试编译函数“b”时,我收到一条错误消息
Failure value returned from cantFail wrapped call
Symbols not found: [ _r ]
尝试编译IR代码行时出现错误
store %g %1, %g* @r, align 8
因为JIT无法在JIT的符号table中找到对应于全局变量“r”的符号。
问题似乎是未初始化的全局变量以某种方式被优化掉并且没有添加到符号 table。
确保将变量添加到符号 table 的快速解决方法是使用“未定义值”对其进行初始化。
以下代码允许使用 c++ api
进行此类初始化// code defining the struct type
std::vector<llvm::Type *> Members(2, llvm::Type::getDoubleTy(TheContext));
llvm::StructType *TypeG = llvm::StructType::create(TheContext,Members,"g",false);
// defining the global variable
TheModule->getOrInsertGlobal("r",TypeG);
llvm::GlobalVariable *gVar = TheModule->getNamedGlobal("r");
// initialize the variable with an undef value to ensure it is added to the symbol table
gVar->setInitializer(llvm::UndefValue::get(TypeG));
这解决了问题。