删除不可移动的llvm指令

删除不可移动的llvm指令,llvm,llvm-clang,llvm-ir,Llvm,Llvm Clang,Llvm Ir,我试图使用llvm pass将pthread\u create函数调用转换为父线程中的普通函数调用,而不是子线程中的函数调用。输入和输出的简单说明如下所示。但是,当我尝试使用删除原始的pthread\u create CallInst时,它总是导致分段错误。在深入研究之后,我发现指令总是从返回false。我真的不知道为什么删除指令会引起任何副作用 void *foo(void *args) { sleep(2); } int main() { pthread_t tid; pthr

我试图使用llvm pass将
pthread\u create
函数调用转换为父线程中的普通函数调用,而不是子线程中的函数调用。输入和输出的简单说明如下所示。但是,当我尝试使用删除原始的
pthread\u create CallInst
时,它总是导致分段错误。在深入研究之后,我发现指令总是从返回
false
。我真的不知道为什么删除指令会引起任何副作用

void *foo(void *args) {
  sleep(2);
}

int main() {
  pthread_t tid;
  pthread_create(&tid, NULL, foo, NULL);
  // => foo();
  pthread_join();
}
感谢@arnt的评论。这正是我想要做的。我构造了一个CallInst,其调用者是foo函数,我将用原始的
pthread\u create
函数调用替换它

for (auto &inst: main_func) { // Iterate every instructions in the main function
  if (isa<CallInst>(inst)) {
    CallInst *ci = dyn_cast<CallInst>(&inst);
    if (ci->getCalledFunction()->getName().compare("pthread_create") == 0) {
      Function *start_routine = dyn_cast<Function>(ci->getArgOperand(2));
      Value *routine_args[1] = { ci->getArgOperand(3) };
      ArrayRef<Value *> arr_ref(routine_args);

      // Create a new CallInst and insert it before the "pthread_create" call instruction
      CallInst *foo = CallInst::create(start_routine, arr_ref, "startRoutineOutput", ci);

      ci->eraseFromParent(); // Segmentation fault happens here.
    }
  }
}
for(auto&inst:main_func){//迭代main函数中的每个指令
if(isa(inst)){
CallInst*ci=动态铸造和安装;
如果(ci->getCalledFunction()->getName().compare(“pthread_create”)==0){
函数*start_例程=dyn_cast(ci->GetArgOperator(2));
Value*routine_args[1]={ci->getArgOperand(3)};
ArrayRef arr_ref(例行参数);
//创建一个新的CallInst并将其插入到“pthread_Create”调用指令之前
CallInst*foo=CallInst::create(启动例程,arr\u ref,“startRoutineOutput”,ci);
ci->eraseFromParent();//此处发生分段错误。
}
}
}

您的代码是什么,具体是什么问题?顺便说一句,如果你用调试和断言编译LLVM,这类事情通常会给你一个准确或有用的错误消息。谢谢你的评论。我已经在我的问题中添加了更多细节。我从Ubuntu的apt工具安装我的llvm包,而不是自己构建它。我也可以得到这些调试信息吗?我希望,但不是绝对知道,ubuntu的包是在禁用断言的情况下构建的,并且你必须从源代码处编译LLVM才能获得断言。我已经尝试从LLVM源代码构建clang和所有库。我发现编译器有几个奇怪的行为。在编译和链接过程中,一切都变得极其缓慢。我怀疑原因是因为Ubuntu包的LLVM_ENABLE_项目与我的不同。我凭直觉将其设置为“全部”@arnt您知道建议的配置设置是什么吗?