llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[OpenMP] libompd.so depends on libomp.so, but it should not

Open jdelsign opened this issue 2 years ago • 7 comments

libompd.so depends on libomp.so, which I think is a bug, because it requires setting LD_LIBRARY_PATH for the debugger process so that the OMPD DLL dependency can be resolved by the dynamic linker. Note that all of the LLVM clang-derived builds I checked have the same problem.

Here's an example of the dependency for Clang 17.0.1:

% readelf -d libompd.so | grep libomp
 0x0000000000000001 (NEEDED)             Shared library: [libomp.so]
 0x000000000000000e (SONAME)             Library soname: [libompd.so]
%

I think this is a problem because:

  1. The debugger is not an OpenMP program, and therefore should not have one or more OMP libraries loaded as a side-effect of loading one or more OMPD libraries.
  2. The dlopen() of the OMPD library fails unless the user sets LD_LIBRARY_PATH correctly or the debugger explicitly dlopens() the OMP library itself.
  3. In a multi-process debugger, it is not possible to set LD_LIBRARY_PATH correctly if multiple processes are using different OMPD libraries.
  4. The libomp.so dependency might bind to the wrong library.

For example, at LLNL with the CCE Clang compiler, I see this:

    rzvernal10{jdelsign}:ldd ./libompd.so | grep libomp
            libomp.so => /lib64/libomp.so (0x0000155555029000)
    rzvernal10{jdelsign}:md5sum ./libomp.so /lib64/libomp.so
    4816b8452c002e798cf7e146e6f179e5  ./libomp.so
    222130624ee2119d7c446ec9138ba712  /lib64/libomp.so
    rzvernal10{jdelsign}:ls -l ./libomp.so /lib64/libomp.so
    -rwxr-xr-x 1 root root 1354120 Nov  7 14:49 ./libomp.so
    -rwxr-xr-x 1 root root 1018800 Jul 15  2023 /lib64/libomp.so
    rzvernal10{jdelsign}:

So, at LLNL there just so happens to be a libomp.so in /lib64, which is not the CCE Clang OMP library.

jdelsign avatar Feb 05 '24 22:02 jdelsign

@llvm/issue-subscribers-openmp

Author: John V. DelSignore, Jr. (jdelsign)

libompd.so depends on libomp.so, which I think is a bug, because it requires setting LD_LIBRARY_PATH for the debugger process so that the OMPD DLL dependency can be resolved by the dynamic linker. Note that all of the LLVM clang-derived builds I checked have the same problem.

Here's an example of the dependency for Clang 17.0.1:

% readelf -d libompd.so | grep libomp
 0x0000000000000001 (NEEDED)             Shared library: [libomp.so]
 0x000000000000000e (SONAME)             Library soname: [libompd.so]
%

I think this is a problem because:

  1. The debugger is not an OpenMP program, and therefore should not have one or more OMP libraries loaded as a side-effect of loading one or more OMPD libraries.
  2. The dlopen() of the OMPD library fails unless the user sets LD_LIBRARY_PATH correctly or the debugger explicitly dlopens() the OMP library itself.
  3. In a multi-process debugger, it is not possible to set LD_LIBRARY_PATH correctly if multiple processes are using different OMPD libraries.
  4. The libomp.so dependency might bind to the wrong library.

For example, at LLNL with the CCE Clang compiler, I see this:

    rzvernal10{jdelsign}:ldd ./libompd.so | grep libomp
            libomp.so => /lib64/libomp.so (0x0000155555029000)
    rzvernal10{jdelsign}:md5sum ./libomp.so /lib64/libomp.so
    4816b8452c002e798cf7e146e6f179e5  ./libomp.so
    222130624ee2119d7c446ec9138ba712  /lib64/libomp.so
    rzvernal10{jdelsign}:ls -l ./libomp.so /lib64/libomp.so
    -rwxr-xr-x 1 root root 1354120 Nov  7 14:49 ./libomp.so
    -rwxr-xr-x 1 root root 1018800 Jul 15  2023 /lib64/libomp.so
    rzvernal10{jdelsign}:

So, at LLNL there just so happens to be a libomp.so in /lib64, which is not the CCE Clang OMP library.

llvmbot avatar Feb 05 '24 22:02 llvmbot

cc @jprotze

shiltian avatar Feb 25 '24 14:02 shiltian

This regression was introduced in https://reviews.llvm.org/D122667 . libompd should not link against hwloc nor libomp. From my understanding the dependency to hwloc.h comes through kmp.h, which is included by ompd-icv.cpp for kmp_sched* enum values.

I see two solutions to break the dependency:

  • separate out affinity definitions from kmp.h and include the new header where needed
  • duplicate the kmp_sched* enum values and define them in ompd-icv.h locally.

My preferred solution would be the first one.

jprotze avatar Feb 25 '24 14:02 jprotze

Note that Jini @ AMD fixed this problem in amdclang, so it might be interesting to check the solution AMD picked.

jdelsign avatar Feb 25 '24 16:02 jdelsign

We have fixed this internally within AMD by removing the link dependency of libompd with libomp. However, we included the directory for hwloc.h among the include directories so that "hwloc.h" gets included while building. This way we get to use kmp.h and need not to worry about maintaining the kmp changes in new header.

-target_link_libraries(ompd omp) # ensure generated import library is created first +# +# This is introduced in upstream through https://reviews.llvm.org/D123888 +# to support Portable Hardware Locality (hwloc). https://www.open-mpi.org/projects/hwloc/ +# omp-icv.cpp includes header kmp.h from openmp runtime and that will include hwloc.h +# when HWLOC(-DLIBOMP_USE_HWLOC) is enabled during configuration. +# LIBOMP_HWLOC_INSTALL_DIR is used to specify the HWLOC installation location and hwloc.h +# is picked from there. + +# OMPD doesn't use HWLOC and location of hwloc.h is required only because we include kmp.h. +# Adding libomp in link flags will create an unwanted dependency on libomp.so which might +# result in complications. +# +add_dependencies(ompd omp) # ensure generated import library is created first + +# for hwloc.h included from kmp.h +if(${LIBOMP_USE_HWLOC})

  • include_directories(${LIBOMP_HWLOC_INSTALL_DIR}/include) +endif()

@jprotze Please share your thoughts.

vigbalu avatar Feb 26 '24 06:02 vigbalu

I created a PR to fix this issue. @vigbalu what do you think about this solution?

jprotze avatar Feb 27 '24 10:02 jprotze

@jprotze PR looks good to me.

vigbalu avatar Feb 27 '24 13:02 vigbalu