gpt4 book ai didi

带有数组参数的 LLVM IR 函数

转载 作者:行者123 更新时间:2023-12-01 01:08:28 26 4
gpt4 key购买 nike

我想从两个基本的 C++ 函数生成 LLVM IR 代码,如下所示。

int newFun2(int x){
int z = x + x;
return z;
}

int newFun(int *y){
int first = y[3]; //How to define it using the LLVM API?
int num = newFun2(first);
return num;
}

我的问题是使用 LLVM API 获取数组参数的索引。有任何想法吗 ?
非常感谢

已编辑

这是我使用 API 的代码:
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::Module *module = new llvm::Module("AST", context);
llvm::IRBuilder<> builder(context);

//newFun2
llvm::FunctionType *newFunc2Type = llvm::FunctionType::get(builder.getInt32Ty(), builder.getInt32Ty(), false);
llvm::Function *newFunc2 = llvm::Function::Create(newFunc2Type, llvm::Function::ExternalLinkage, "newFun2", module);

llvm::Function::arg_iterator argsFun2 = newFunc2->arg_begin();
llvm::Value* x = argsFun2++;
x->setName("x");

llvm::BasicBlock* block = llvm::BasicBlock::Create(context, "entry", newFunc2);
llvm::IRBuilder<> builder2(block);

llvm::Value* tmp = builder2.CreateBinOp(llvm::Instruction::Add,
x, x, "tmp");

builder2.CreateRet(tmp);

//newFun
llvm::FunctionType *newFuncType = llvm::FunctionType::get(builder.getInt32Ty(), builder.getInt32Ty()->getPointerTo(), false);
llvm::Function *newFunc = llvm::Function::Create(newFuncType, llvm::Function::ExternalLinkage, "newFun", module);

llvm::BasicBlock* block2 = llvm::BasicBlock::Create(context, "entry", newFunc);
llvm::IRBuilder<> builder3(block2);

module->dump();

这是生成的 LLVM IR:
; ModuleID = 'AST'

define i32 @newFun2(i32 %x) {
entry:
%tmp = add i32 %x, %x
ret i32 %tmp
}

define i32 @newFun(i32*) {
entry:
}

由于数组访问,我被困在 newFun 的主体上。

最佳答案

我认为您首先需要了解 IR 应该是什么样子。可以通过查看 the language specification 来完成。或通过 using Clang to compile the C code into IR并查看结果。

在任何情况下,访问给定索引处的数组元素的方法是使用 extractvalue (仅接受常量索引)或使用 gep .这两个都有对应的构造函数/工厂方法和IRBuilder构造它们的方法,例如

builder.CreateExtractValue(y, 3);

创建 gep 稍微复杂一些;我建议看看 the gep guide .

但是,查看如何调用 LLVM API 以创建所需 IR 的一个好方法是使用 llc (LLVM 命令行工具之一)从 IR 文件生成带有这些调用本身的源文件,请参阅以下两个相关问题:
  • Possible to auto-generate llvm c++ api code from LLVM-IR?
  • Generate LLVM C++ API code as backend
  • 关于带有数组参数的 LLVM IR 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16823487/

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