[PATCH] D32782: Add pthread_self function prototype and make it speculatable.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Sat May 20 16:21:12 PDT 2017


On Sat, May 20, 2017 at 3:51 PM, Peter Collingbourne <peter at pcc.me.uk> wrote:
> Couldn't pthread_self() return a different value if fork() is called in the
> body of the loop?
>

I was worried about a case where this could break but I wasn't able to
find a counterexample.
I guess yours holds. FWIW, I'm not sure what's the interaction between
fork() and pthread_self() but if fork()'ing can actually change the
value, GCC miscompiles the following code. Xin, I recommend to revert
this one until we get a better understanding.

#include <pthread.h>
#include <unistd.h>

extern void x(pthread_t);

int foo(int n) {
   for (int i = 0; i < n; i++) {
    auto id = pthread_self();
    x(id);
    fork();
  }
}
foo(int):
        test    edi, edi
        jle     .L6
        push    r12
        push    rbp
        mov     ebp, edi
        push    rbx
        xor     ebx, ebx
        call    pthread_self
        mov     r12, rax
.L3:
        mov     rdi, r12
        add     ebx, 1
        call    x(unsigned long)
        call    fork
        cmp     ebp, ebx
        jne     .L3
        pop     rbx
        pop     rbp
        pop     r12
        ret
.L6:
        rep ret


More information about the llvm-commits mailing list