[llvm-dev] LLVM and Pthreads

Iulia Stirb via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 22 04:01:30 PDT 2019


Hello all,
I am returning to this subject after a while and I was looking for CBuilder::CreatePThreadCreateCall function below. I cannot find it anywere and I guess it has been rename or changed into a more generic one. I would want to insert a phtread call into the input source code. Which builder class to use for that instead of CBuilder class? If I insert the call into IR representation, do I need to insert the call also into machine representation?
Kind regards,
----------------------------------------------------------Iulia ȘTIRBPhD Student in Computer ScienceAssociate Teaching AssistantDepartment of Computers and Software EngineeringPolitehnica University of Timișoara2 Piața Victoriei, Timișoara, Romania, 300006 Tel.: +(40) 76 560 3230E-mail: iulia.stirb at gmail.com, iulia_s24 at yahoo.comSkype: iulia.stirb----------------------------------------------------------




 

    On Saturday, May 6, 2017, 5:58:03 PM GMT+3, Rob Cameron <cameron at cs.sfu.ca> wrote:  
 
 Hi, Iulia.

You can create calls to pthread routines.  For example, this is a declaration
in one of our header files.

  //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  //                    void *(*start_routine)(void*), void *arg);
  llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, 
                                        llvm::Function * start_routine, llvm::Value * arg);

Here is our implementation (mMod is the Module * containing the code we are building).

Value * CBuilder::CreatePThreadCreateCall(Value * thread, Value * attr, Function * start_routine, Value * arg) {
    Type * const voidPtrTy = getVoidPtrTy();
    Function * pthreadCreateFunc = mMod->getFunction("pthread_create");
    if (pthreadCreateFunc == nullptr) {
        Type * pthreadTy = getSizeTy();
        FunctionType * funVoidPtrVoidTy = FunctionType::get(getVoidTy(), {getVoidPtrTy()}, false);
        FunctionType * fty = FunctionType::get(getInt32Ty(), {pthreadTy->getPointerTo(), voidPtrTy, funVoidPtrVoidTy->getPointerTo(), voidPtrTy}, false);
        pthreadCreateFunc = Function::Create(fty, Function::ExternalLinkage, "pthread_create", mMod);
        pthreadCreateFunc->setCallingConv(CallingConv::C);
    }
    return CreateCall(pthreadCreateFunc, {thread, attr, start_routine, CreatePointerCast(arg, voidPtrTy)});
}

The actual routine called is determined at link time.  In our case, 
we rely on the ExecutionEngine to dynamically link to the pthread routines 
available on our target platform.  This approach works well for us on both
Linux and Mac OS.  I don't know what happens with Windows OS.



----- On May 6, 2017, at 4:16 AM, Iulia Stirb via llvm-dev llvm-dev at lists.llvm.org wrote:

> Hello,
> I know clang supports -pthread option (here). Does this mean I can call pthread
> routines inside llvm code and which pthread library is used, the one for Linux
> OS or the one for Windows?
> Thank you.Iulia
> 
> 
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190422/86f18e8c/attachment.html>


More information about the llvm-dev mailing list