I need to insert IR instruction to call pthread_create in my LoopPass, so I need to pass the actual function as an argument that pthread_create is supposed to call on the new thread.
Currently I have defined the function to run on the new thread as
Function *worker_func = Function::Create(funcType,
                                      GlobalValue::ExternalLinkage,
                                      "worker_func",
                                      CurrentModule);
And I got the pointer to pthread_create by the following:
Function *func_pthread_create = dyn_cast<Function>(
    TheModule->getOrInsertFunction("pthread_create", pthreadCreateTy));
And I need to pass an array of Type* as the arguments to pthread_create like below.
Value* pthread_create_call = builder.CreateCall(
    func_pthread_create, args, "pthread_create");
with the args as:
Value* args[4];
args[0] = pthread_t_ld
args[1] = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(getGlobalContext())->getPointerTo());
args[2] = ??? // supposed to be the pointer to worker_func`
args[3] = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(getGlobalContext())->getPointerTo());
So how can I get the pointer to this worker_func function to pass into pthread_create?
You need to bitcast func_pthread_create to the type the function expects and pass the result of that bitcast as the 3rd argument. You can use the static ConstantExpr::getBitCast method for that, passing it the function as the first argument and the type of the third parameter of func_pthread_create as the second argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With