Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

[C++4OpenCL] Clarify support for static locals #46981

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR48012
Status NEW
Importance P enhancement
Reported by Anastasia Stulova (anastasia.stulova@arm.com)
Reported on 2020-10-29 09:44:37 -0700
Last modified on 2021-01-14 07:02:00 -0800
Version trunk
Hardware PC All
CC anastasia.stulova@arm.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also

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++.

  1. Destructors. Clang seems to provide regualr atexit mechanisms for program scope variable and static local variables. However there is no easy workaround for those.
Quuxplusone commented 3 years ago

Regarding 1.(ii) one improvement can be done is to provide a warning when ctors are used for sttic locals.

Quuxplusone commented 3 years ago

To elaborate on use case for mon-multi threaded initialization in OpenCL - it relies on application to ensure that the status variable initialization code only runs in by one thread (for example by guarding global ID).