在llvm JIT符号表中找不到全局变量



我正试图获得一个带有全局变量的llvm::模块,以便使用KaleidoscopeJIT编译器进行编译,但是,我在JIT编译器的符号查找中遇到了错误。(KaleidoscopeJIT.h源代码来自https://github.com/llvm-mirror/llvm/blob/master/examples/Kaleidoscope/include/KaleidoscopeJIT.h(

在检查LegacyRTDyldObjectLinkingLayerBase中的Symbol表时,我确实发现全局变量尚未添加到Symbol表中。这是因为全局变量未初始化吗?如果是,我应该如何使用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不能找到对应于全局变量"的符号;r〃;JIT的符号表中。

问题似乎是未初始化的全局变量以某种方式进行了优化,而没有添加到符号表中。

确保变量被添加到符号表的一种快速方法是用";未定义的值";。

下面的代码允许用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));

这就解决了问题。

最新更新