将inreg属性添加到LLVM IR函数参数 [英] Adding the inreg attribute to LLVM IR function parameters
问题描述
我正在使用LLVM,我想用API重新创建一段IR:
I'm working with LLVM and I want to recreate a piece of IR with the API:
declare void @fun(i32* inreg, i32 inreg)
但是我似乎无法真正做到这一点.
But I can't seem to get it to actually do it.
我当前的尝试是:
Function* fun = cast<Function>(M.getOrInsertFunction("fun",type));
((fun -> getAttributes()).getParamAttributes(0)).addAttribute(c,0,Attribute::InReg);
((fun -> getAttributes()).getParamAttributes(1)).addAttribute(c,0,Attribute::InReg);
在第1行,第2行和第3行被完全忽略之后,此代码实际上不执行任何操作,我在IR输出中得到的只是:
This code literally doesn't do anything after line 1, lines 2 and 3 are completely ignored and all I get in the IR output is:
declare void @fun(i32* , i32 )
我如何使其正常工作?
推荐答案
在LLVM中管理函数属性非常不便,因为属性打包成不可变的全局集合.为函数参数分配属性实际上意味着用新的属性替换代表所有函数和参数属性的集合.
Managing function attributes in LLVM is quite inconvenient as attributes are packed into immutable and global sets. Assigning an attribute to a function argument actually means replacing the set representing all function and argument attributes with a new one.
幸运的是,至少有一些辅助函数使这项工作变得容易一些.我建议使用 llvm::Function::addAttribute()
方法.
Fortunately, there are at least helper functions that makes that job a bit easier. I suggest using llvm::Function::addAttribute()
method.
Function* fun = cast<Function>(M.getOrInsertFunction("fun", type));
fun->addAttribute(1, Attribute::InReg);
fun->addAttribute(2, Attribute::InReg);
请记住,索引0代表 function 属性, argument 属性从索引1开始.
Keep in mind that index 0 represents function attributes, argument attributes starts from index 1.
这篇关于将inreg属性添加到LLVM IR函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!