[llvm-bugs] [Bug 48012] New: [C++] Clarify support for static locals

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Oct 29 09:44:37 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=48012

            Bug ID: 48012
           Summary: [C++] Clarify support for static locals
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: OpenCL
          Assignee: unassignedclangbugs at nondot.org
          Reporter: anastasia.stulova at arm.com
                CC: anastasia.stulova at arm.com, llvm-bugs at lists.llvm.org

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: https://godbolt.org/z/bfaxKT

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201029/7cd3d70f/attachment.html>


More information about the llvm-bugs mailing list