在 LLVM IR 中添加来自外部库 (rpclib) 的函数调用

Adding a function call from an external library (rpclib) in LLVM IR

我有一个非常简单的程序,它接受两个整数并将它们相加。我想在该程序中添加一段代码,以便在调用 add 函数的地方为用户提供选择,而不是在本地或服务器上进行计算。

我原来的程序是这样的:

#include <iostream>

using namespace std;

extern "C" double add(double a, double b);

int main()
{
    double a, b, result;
    cout<<"Enter first argument: ";
    cin>>a;
    cout<<"Enter second argument: ";
    cin>>b;
    result = add(a, b);
    cout<<"Locally computed result: "<<result<<endl;
    return 0;
}


extern "C" double add(double a, double b)
{
    return a + b;
}

所需的转换:

#include <iostream>
#include "rpc/client.h"
#include "rpc/rpc_error.h"

using namespace std;

extern "C" double add(double a, double b);

const int PORT = 20143;

int main()
{
    // Init RPC client
    rpc::client c("localhost", PORT);
    double a, b, result;
    cout<<"Enter first argument: ";
    cin>>a;
    cout<<"Enter second argument: ";
    cin>>b;
    char location;
    cout<<"execute function on server or client? (s/c) ";
    cin>>location;
    if (location == 'c') {
        result = add(a, b);
        cout<<"Locally computed result: "<<result<<endl;
    }
    else if (location == 's') {
        result = c.call("run_on_server", "add", a, b).as<double>();
        cout<<"Response received from edge: "<<result<<endl;
    }
    return 0;
}

extern "C" double add(double a, double b)
{
    return a + b;
}

我正在使用 rpclib 进行远程调用。我用不同版本的程序生成了 IR 来比较它们,看看我需要添加什么。

  1. 为原始程序生成了一个看起来非常正常的 IR
  2. 如果我添加 rpc headers 和 rpc::client c("localhost", PORT); 大约会添加 50 行。
  3. 当我添加 result = c.call("run_on_server", "add", a, b).as<double>(); 时,它爆炸并添加了大约 11k 行。我不知道为什么。它不应该已经在我创建 object 的步骤 2 中添加了所需的所有内容吗?

我的问题是,如何进行这样的转换?我可以向 IR 添加简单的函数调用,但不确定我应该如何包含 rpc headers.

创建一个可以完成所有繁重工作的辅助函数。像

extern "C" void helper(const char* funcName)
{
rpc::client c("localhost", PORT);
c.call("run_on_server", funcName);
}

将此代码编译成 .bc 文件,并 link 将其放入您的通行证中。之后,在任何需要的地方插入对 helper() 的调用。

您可以使用 helper 的代码作为一种模板,并添加所需的参数和返回值。