LLVM中的参数转发 [英] Argument forwarding in LLVM

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

问题描述

我需要一些关于将参数转发给被调用者的建议(在LLVM-IR中)。



假设我有一个函数 F ,它在 all 其他开头被调用功能在模块中。从 F >我需要访问(读取)传递给它的直接调用者的参数。



现在执行此操作我将调用者中的所有参数都放在一个结构体中,并将​​一个 i8 * 指针传递给 F 标识符,告诉来自哪个调用者 F F 然后有一个巨大的开关,分支到相应的拆箱代码。这是必须完成的,因为模块中的函数具有不同的签名(不同的参数/返回值计数和类型;甚至不同的调用约定),但显然是不理想的(从性能和代码大小的角度来看),因为我需要在堆栈中分配结构,复制其中的参数,传递一个额外的指针到 F ,然后执行拆箱操作。



我想知道是否有更好的方法可以做到这一点,即从函数访问其直接调用方的栈帧的方法(知道,由于标识符,哪个调用者该函数被调用),或者更一般地说,在它的直接调用者中定义的任意值。任何建议?



注意:我正在做的工作的重点是单个函数 F 就是这么做的; split * / inlining / specializing / templating F 不是一个选项。






澄清,假设我们有以下函数 FuncA FuncB (注意:接下来的只是伪C -code,总是记得我们正在谈论LLVM-IR!)

pre $ Type1 FuncA(Type2 ArgA1){
F ();
// ...
}

Type3 FuncB(Type4 ArgB1,Type5 ArgB2,Type6 ArgB3){
F();
// ...
}

我需要的是一种有效的方式对于函数 F 执行以下操作:

  void F() {
switch(caller){
case FuncA:
//用ArgA1
break做一些事情;
case FuncB:
//用ArgB1,ArgB2,ArgB3
break做一些事情;


$ / code>

正如我在第一部分所解释的那样,现在我的 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;
//用ArgA1
break做些事情;
case ID_FuncB:
Args_FuncB * ArgsFuncB =(Args_FuncB *)args;
Type4 ArgB1 = ArgsFuncB-> ArgB1;
Type5 ArgB2 = ArgsFuncB-> ArgB2;
Type6 ArgB3 = ArgsFuncB-> ArgB3;
//用ArgB1,ArgB2,ArgB3
break做一些事情;


$ / code $ / pre

和这两个函数变成:

  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);
// ...
}


解决方案

恕我直言,你做对了。虽然在机器码组装中有解决方案,但恐怕LLVM组装中可能没有解决方案,因为它是更高级别。如果你想在某些函数的开头运行一个函数,你有没有想过检查


  • 调试器源代码(比如gdb) li>
  • 使用 Valgrind 进行二元测试



我知道这不是直接的答案,但我希望这可能会有所帮助;)。


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);
  // ...
}

解决方案

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

  • debugger sources (like gdb)
  • Binary Instrumentation with Valgrind

I know it's not direct answer, but I hope it might be helpful in some way ;).

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

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