[llvm-dev] __builtin_thread_pointer for RISC-V

Sam Elliott via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 25 08:36:23 PDT 2020

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


> On 25 Mar 2020, at 12:34 pm, kamlesh kumar via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 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?
> ./kamlesh
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Sam Elliott
Software Developer - LLVM and OpenTitan

More information about the llvm-dev mailing list