访问LLVM数组中的元素 [英] Accessing elements in LLVM arrays

查看:458
本文介绍了访问LLVM数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开始使用LLVM,以便为我的代码添加即时编译,但是尽管已经通过万花筒教程进行了检查,但发现很难找到有关如何在LLVM中执行所需操作的参考. ,语言参考手册,程序员手册和doxygen文档.除了这些以外,还有更多关于LLVM C ++ API的引用吗?

I am trying to get started with LLVM in order to add just-in-time compilation for my code, but finding it very difficult find references on how to do the things I want in LLVM, despite having checked through the Kaleidoscope tutorial, the language reference manual, the programmer's manual and the doxygen documentation. Are there more references to LLVM's C++ API than these?

现在提出具体问题.我已经为数组对象分配了两个元素(我假设它对应于C ++中的double[2]):

Now for the concrete question. I have allocated an array object with two elements (which I assume corresponds to double[2] in C++):

const llvm::Type* double_t = llvm::Type::getDoubleTy(llvm::getGlobalContext());
const llvm::Type* array_t =  llvm::ArrayType::get(double_t,2)

稍后在代码中,我创建了一个函数,其中此数组是参数之一.然后,在我的函数中,提取数组中的第一个元素,并将其返回给用户:

Later in the code, I create a function, where this array is one of the arguments. Then, in my function I extract the first element in the array and return it to the user:

llvm::Function::arg_iterator AI = jit_function_->arg_begin();
llvm::Value *x = AI;
llvm::Value *x0 = Builder.CreateExtractValue(x,0);
Builder.CreateRet(x0);

代码运行良好,但是当我尝试运行它时,它不起作用.例如:

The code jits fine, but when I try to run it, it doesn't work. For example:

typedef double (*GenType)(double[2]);
GenType FP = GenType(intptr_t(TheExecutionEngine->getPointerToFunction(jit_function_)));
double y[2] = {10,20};
double r = FP(y);
printf("r = %g\n", r);

返回值只是胡说八道,我看不到我在做什么错.如果我将数组(10和20)中的值作为标量参数传递给该函数,则效果很好.

The return value is just nonsense and I can't see what I'm doing wrong. If I pass the values in the array (10 and 20) as scalar arguments to the function, it works fine.

推荐答案

我认为我能够回答自己的问题.对于问题的第一部分,对LLVM的一个很好的参考(除了上面提到的那些)是在纯C语言中编写所需的内容,并使用Clang对其进行编译,然后查看LLVM的输出:clang -S -emit-llvm test.c -o -.

I think I am able to answer my own question. For the first part of the question, a good reference to LLVM (in addition to the ones mentioned above) is to write what you want in plain C and compile it with Clang and look at the LLVM output: clang -S -emit-llvm test.c -o -.

对于具体问题,我的问题是我假设我将两个双精度数的数组传递给jitted函数,而实际上我正在将 pointer 传递给具有两个值的数组.因此解决方案是更改:

For the concrete question, my problem was that I assumed that I was passing an array of two doubles to the jitted function, while in fact I was passing a pointer to an array with two values. So the solution is to change:

const llvm::Type* array_t =  llvm::ArrayType::get(double_t,2);

const llvm::Type* array_t =  llvm::PointerType::getUnqual(llvm::ArrayType::get(double_t,2));

并通过更改添加其他解除引用

And to add an additional dereferencing by changing

llvm::Value *x0 = Builder.CreateExtractValue(x,0);

llvm::Value *x0 = Builder.CreateExtractValue(Builder.CreateLoad(x),0);

这篇关于访问LLVM数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆