__builtin_thread_pointer for RISC-V

Hi Devs,
since risc-v has a register $tp which is thread pointer.
is it possible to have __builtin_thread_pointer for RISC-V?
I am not sure what could be corresponding instructions?


Hi kamlesh,

Does GCC provide such a builtin?

At the moment, you can write inline assembly to access the `tp` register if you need to read it directly, like so (you can directly inline this if you need to):

void* read_thread_pointer(void) {
  void* thread_pointer;
  asm("mov %0, tp" : "=r"(thread_pointer) : );
  return thread_pointer;

I am not entirely sure how where `tp` points to in the different thread local models. There is more information on these models in the RISC-V ELF psABI document: https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#thread-local-storage


Thanks for your reply Sam,
GCC rejects the builtin but clang crashes.

Oh Interesting. Please may you file a bug about clang crashing? We'll try to get to it soon.

In the meantime, I hope that inline assembly helps you.