[PATCH] D66122: [CodeGen] Emit dynamic initializers for static TLS vars in outlined scopes

Princeton Ferro via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 17:05:09 PDT 2019


Prince781 created this revision.
Prince781 added reviewers: ABataev, rsmith.
Herald added subscribers: cfe-commits, jfb.
Herald added a reviewer: jdoerfert.
Herald added a project: clang.

For static TLS vars only visible inside a function, clang will only generate an initializer inside the function body where the variable was declared. However, it is possible for the variable to be indirectly referenced without ever calling the function it was declared in, if a scope referring to the variable gets outlined into a function that is executed on a new thread. Here are two examples that demonstrate this:

  #include <thread>
  #include <iostream>
  
  struct Object {
      int i;
      Object() : i(3) {}
  };
  
  int main(void) {
      static thread_local Object o;
  
      std::cout << "[main] o.i = " << o.i << std::endl;
      std::thread t([] { std::cout << "[new thread] o.i = " << o.i << std::endl; });
      t.join();
  }



  #include <iostream>
  #include <omp.h>
  
  struct Object {
      int i;
      Object() : i(3) {}
  };
  
  int main(void) {
      static thread_local Object o;
  
      #pragma omp parallel
      #pragma omp critical
      std::cout << "[" << omp_get_thread_num() << "] o.i = " << o.i << std::endl;
  }

In this patch, we generate an initializer in a function for every unique reference to a static TLS var that was declared in a different function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66122

Files:
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/CodeGenCXX/cxx11-thread-local.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66122.214743.patch
Type: text/x-patch
Size: 6358 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190813/8e3386ad/attachment.bin>


More information about the cfe-commits mailing list