<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [C++] Clarify support for static locals"
   href="https://bugs.llvm.org/show_bug.cgi?id=48012">48012</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[C++] Clarify support for static locals
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>OpenCL
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>anastasia.stulova@arm.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>anastasia.stulova@arm.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Currently Clang User Manual explains workaround for global variable ctors on
the application side for drivers that don't support kernel languages with C++
features. It doesn't mention however whever this applies to static locals or
not.

After looking at this topic I have noted the following.

See example: <a href="https://godbolt.org/z/bfaxKT">https://godbolt.org/z/bfaxKT</a>

1. Constructors.

(i) It seems that to support thread safe implementation some sort of locking
mechanism is to be used but however it is not provided by all OpenCL vendors.

(ii) To address portability it seems that Clang should not define
__cpp_threadsafe_static_init by default, that is controlled by
LangOpts.ThreadsafeStatics. So this option should be set to 0 in OpenCL mode.

(iii) Vendors that support thread safe implementation of static initialization
can enable the LangOpt or alternatively it can be altered using
-fthreadsafe-statics flag.

(iv) Application code can check the "__cpp_threadsafe_static_init" define to
either use the static initialzation or emulate it on the application side. For
example the initialization can be done by one work item by guarding the
initialization code with the work item ID.

(v) Vendor will have to implement @__cxa_guard_acquire, @__cxa_guard_release
(Itanium ABI), but the naive non-thread safe implementation can be very simple
- just checking and setting flag variable generated in IR. (see
@_ZGVZ8get_fredvE12a_local_fred in the example). NOTE that we don't neeed to
worry about recursions since they are disallowed in OpenCL and recursiong in
static locals initialization is undefined behavior in C++.

2. Destructors. Clang seems to provide regualr atexit mechanisms for program
scope variable and static local variables. However there is no easy workaround
for those.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>