[all-commits] [llvm/llvm-project] 237adf: [OpenMP] Rework handling of global ctor/dtors in ...

Joseph Huber via All-commits all-commits at lists.llvm.org
Fri Nov 10 12:54:08 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 237adfca4ef8d94b7d3d8817da615c5e5897c3da
      https://github.com/llvm/llvm-project/commit/237adfca4ef8d94b7d3d8817da615c5e5897c3da
  Author: Joseph Huber <jhuber6 at vols.utk.edu>
  Date:   2023-11-10 (Fri, 10 Nov 2023)

  Changed paths:
    M clang/include/clang/Basic/LangOptions.h
    M clang/lib/CodeGen/CGDeclCXX.cpp
    M clang/lib/CodeGen/CGOpenMPRuntime.cpp
    M clang/lib/CodeGen/CGOpenMPRuntime.h
    M clang/lib/CodeGen/CodeGenFunction.h
    M clang/lib/CodeGen/CodeGenModule.h
    M clang/lib/CodeGen/ItaniumCXXABI.cpp
    M clang/test/Headers/amdgcn_openmp_device_math_constexpr.cpp
    M clang/test/OpenMP/amdgcn_target_global_constructor.cpp
    M clang/test/OpenMP/declare_target_codegen.cpp
    M clang/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp
    M llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
    M llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
    M openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp
    M openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h
    M openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp
    M openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
    M openmp/libomptarget/plugins-nextgen/cuda/src/rtl.cpp
    M openmp/libomptarget/src/rtl.cpp
    A openmp/libomptarget/test/libc/global_ctor_dtor.cpp

  Log Message:
  -----------
   [OpenMP] Rework handling of global ctor/dtors in OpenMP (#71739)

Summary:
This patch reworks how we handle global constructors in OpenMP.
Previously, we emitted individual kernels that were all registered and
called individually. In order to provide more generic support, this
patch moves all handling of this to the target backend and the runtime
plugin. This has the benefit of supporting the GNU extensions for
constructors an destructors, removing a class of failures related to
shared library destruction order, and allows targets other than OpenMP
to use the same support without needing to change the frontend.

This is primarily done by calling kernels that the backend emits to
iterate a list of ctor / dtor functions. For x64, this is automatic and
we get it for free with the standard `dlopen` handling. For AMDGPU, we
emit `amdgcn.device.init` and `amdgcn.device.fini` functions which
handle everything atuomatically and simply need to be called. For NVPTX,
a patch https://github.com/llvm/llvm-project/pull/71549 provides the
kernels to call, but the runtime needs to set up the array manually by
pulling out all the known constructor / destructor functions.

One concession that this patch requires is the change that for GPU
targets in OpenMP offloading we will use `llvm.global_dtors` instead of
using `atexit`. This is because `atexit` is a separate runtime function
that does not mesh well with the handling we're trying to do here. This
should be equivalent in all cases except for cases where we would need
to destruct manually such as:

```
struct S { ~S() { foo(); } };
void foo() {
  static S s;
}
```

However this is broken in many other ways on the GPU, so it is not
regressing any support, simply increasing the scope of what we can
handle.

This changes the handling of ctors / dtors. This patch now outputs a
information message regarding the deprecation if the old format is used.
This will be completely removed in a later release.

Depends on: https://github.com/llvm/llvm-project/pull/71549




More information about the All-commits mailing list