LLVM 中的参数转发
我需要一些关于“转发”参数给被调用者(在 LLVM-IR 中)的建议。
假设我有一个函数 F
,它在模块中所有其他函数的开头被调用。从 F
我需要访问(读取)传递给其直接调用者的参数。
现在,为了做到这一点,我将调用者中的所有参数放入一个结构体中,并将指向该结构体的 i8*
指针传递给 F
,以及一个告诉调用者 的标识符F 正在被调用。
F
然后有一个巨大的开关,分支到适当的拆箱代码。必须这样做,因为模块中的函数具有不同的签名(不同的参数/返回值计数和类型;甚至不同的调用约定),但它显然不是最佳的(从性能和代码大小的角度来看),因为我需要在堆栈上分配结构,复制其中的参数,传递一个额外的指针到F
,然后执行拆箱。
我想知道是否有更好的方法来做到这一点,即一种从函数访问其直接调用者的堆栈帧的方法(感谢标识符,知道函数是从哪个调用者调用的)或者,更一般地说,在其直接调用者中定义的任意值。有什么建议吗?
注意:我正在研究的重点是拥有一个单一函数F
来完成所有这一切;分割/内联/专门化/模板化 F
不是一个选项。
为了澄清这一点,假设我们有以下函数 FuncA
和 FuncB
(注意:接下来的内容只是伪 C 代码,永远记住我们正在谈论 LLVM-IR!)
Type1 FuncA(Type2 ArgA1) {
F();
// ...
}
Type3 FuncB(Type4 ArgB1, Type5 ArgB2, Type6 ArgB3) {
F();
// ...
}
我需要的是函数 F
执行以下操作的有效方法:
void F() {
switch (caller) {
case FuncA:
// do something with ArgA1
break;
case FuncB:
// do something with ArgB1, ArgB2, ArgB3
break;
}
}
正如我在第一部分中解释的那样,现在我的 F
看起来像这样:
struct Args_FuncA { Type2 ArgA1 };
struct Args_FuncB { Type4 ArgB1, Type5 ArgB2, Type6 ArgB3 };
void F(int callerID, void *args) {
switch (callerID) {
case ID_FuncA:
Args_FuncA *ArgsFuncA = (Args_FuncA*)args;
Type2 ArgA1 = ArgsFuncA->ArgA1;
// do something with ArgA1
break;
case ID_FuncB:
Args_FuncB *ArgsFuncB = (Args_FuncB*)args;
Type4 ArgB1 = ArgsFuncB->ArgB1;
Type5 ArgB2 = ArgsFuncB->ArgB2;
Type6 ArgB3 = ArgsFuncB->ArgB3;
// do something with ArgB1, ArgB2, ArgB3
break;
}
}
以及两个函数变得:
Type1 FuncA(Type2 ArgA1) {
Args_FuncA args = { ArgA1 };
F(ID_FuncA, (void*)&args);
// ...
}
Type3 FuncB(Type4 ArgB1, Type5 ArgB2, Type6 ArgB3) {
Args_FuncB args = { ArgB1, ArgB2, ArgB3 };
F(ID_FuncB, (void*)&args);
// ...
}
I need some advice on "forwarding" arguments to a callee (in the LLVM-IR).
Suppose I have a function F
that is called at the beginning of all other functions in the module. From F
I need to access (read) the arguments passed to its immediate caller.
Right now to do this I box all arguments in the caller inside a struct and pass a i8*
pointer to the struct to F
, alongside an identifier telling which caller F
is being called from. F
has then a giant switch that branches to the appropriate unboxing code. This must be done because the functions in the module have differing signatures (differing argument/return value count and types; even differing calling conventions), but it is obviously suboptimal (both from a performance and code size point-of-view) because I need to allocate the struct on the stack, copy the arguments inside of it, passing an additional pointer to F
and then performing the unboxing.
I was wondering if there's a better way to do this, i.e. a way to access from a function the stack frame of its immediate caller (knowing, thanks to the identifier, which caller the function was called from) or, more in general, arbitrary values defined in its immediate caller. Any suggestions?
note: the whole point of what I'm working on is having a single function F
that does all this; splitting/inlining/specializing/templating F
is not an option.
to clarify, suppose we have the following functions FuncA
and FuncB
(note: what follows is just pseudo-C-code, always remember we are talking about LLVM-IR!)
Type1 FuncA(Type2 ArgA1) {
F();
// ...
}
Type3 FuncB(Type4 ArgB1, Type5 ArgB2, Type6 ArgB3) {
F();
// ...
}
what I need is an efficient way for the function F
to do the following:
void F() {
switch (caller) {
case FuncA:
// do something with ArgA1
break;
case FuncB:
// do something with ArgB1, ArgB2, ArgB3
break;
}
}
as I explained in the first part, right now my F
looks like this:
struct Args_FuncA { Type2 ArgA1 };
struct Args_FuncB { Type4 ArgB1, Type5 ArgB2, Type6 ArgB3 };
void F(int callerID, void *args) {
switch (callerID) {
case ID_FuncA:
Args_FuncA *ArgsFuncA = (Args_FuncA*)args;
Type2 ArgA1 = ArgsFuncA->ArgA1;
// do something with ArgA1
break;
case ID_FuncB:
Args_FuncB *ArgsFuncB = (Args_FuncB*)args;
Type4 ArgB1 = ArgsFuncB->ArgB1;
Type5 ArgB2 = ArgsFuncB->ArgB2;
Type6 ArgB3 = ArgsFuncB->ArgB3;
// do something with ArgB1, ArgB2, ArgB3
break;
}
}
and the two functions become:
Type1 FuncA(Type2 ArgA1) {
Args_FuncA args = { ArgA1 };
F(ID_FuncA, (void*)&args);
// ...
}
Type3 FuncB(Type4 ArgB1, Type5 ArgB2, Type6 ArgB3) {
Args_FuncB args = { ArgB1, ArgB2, ArgB3 };
F(ID_FuncB, (void*)&args);
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,你做得对。虽然机器代码汇编中有解决方案,但恐怕 LLVM 汇编可能没有解决方案,因为它是“更高级别”的。如果您想在某些函数的开头运行一个函数,您是否考虑过使用 Valgrind 检查
我知道这不是直接答案,但我希望它在某种程度上有帮助;)。
IMHO you've done it right. While there are solutions in machinecode assembly, I am afraid there might be no solution in LLVM assembly, as it's "higher level". If you'd like to run a function on the beginning of some functions have you thought about checking
I know it's not direct answer, but I hope it might be helpful in some way ;).
不确定这是否有帮助,但我遇到了类似的问题,并通过使用 llvm 向量存储中间值来绕过 LLVM tbaa 分析的限制。 LLVM 优化过程后来能够优化向量加载/存储到标量寄存器中。
我记得有一些警告。如果您探索这条路线,请告诉我,我可以挖掘一些代码。
Not sure if this helps, but I had a similar problem and got around the limitations of LLVM's tbaa analysis by using a llvm vector to store the intermediate values. LLVM optimization passes were later able to optimize the vector load / stores into scalar registers.
There were a few caveats as I recall. Let me know if you explore this route and I can dig up some code.