|
| 1 | +# ============================================================================== |
| 2 | +# HCU Backend Configuration |
| 3 | +# ============================================================================== |
| 4 | + |
| 5 | +message(STATUS "Configuring HCU backend...") |
| 6 | + |
| 7 | +# HCU uses HIP runtime |
| 8 | +# Find HIP package - typically provided by DTK installation |
| 9 | +list(APPEND CMAKE_PREFIX_PATH |
| 10 | + "$ENV{ROCM_PATH}" |
| 11 | + "$ENV{HIP_PATH}" |
| 12 | + "/opt/rocm" |
| 13 | + "/opt/rocm/hip" |
| 14 | + "/opt/dtk" |
| 15 | + "/opt/dtk/hip" |
| 16 | +) |
| 17 | + |
| 18 | +# DTK's MIOpen depends on /usr/lib/x86_64-linux-gnu/librt.so in its CMake target, |
| 19 | +# but on Ubuntu 22.04+ (glibc 2.34) librt was merged into libc and the stub |
| 20 | +# symlink librt.so is no longer shipped. Create it manually if needed: |
| 21 | +# sudo ln -s /usr/lib/x86_64-linux-gnu/librt.so.1 /usr/lib/x86_64-linux-gnu/librt.so |
| 22 | +# Or opt in to automatic creation at configure time (requires write access to /usr/lib): |
| 23 | +option(HCU_CREATE_LIBRT_STUB |
| 24 | + "Create /usr/lib/x86_64-linux-gnu/librt.so symlink for DTK MIOpen (requires root)" |
| 25 | + OFF) |
| 26 | +if(HCU_CREATE_LIBRT_STUB |
| 27 | + AND NOT EXISTS "/usr/lib/x86_64-linux-gnu/librt.so" |
| 28 | + AND EXISTS "/usr/lib/x86_64-linux-gnu/librt.so.1") |
| 29 | + message(STATUS "HCU_CREATE_LIBRT_STUB=ON: creating librt.so symlink (required by DTK MIOpen)") |
| 30 | + execute_process( |
| 31 | + COMMAND ${CMAKE_COMMAND} -E create_symlink |
| 32 | + /usr/lib/x86_64-linux-gnu/librt.so.1 |
| 33 | + /usr/lib/x86_64-linux-gnu/librt.so |
| 34 | + RESULT_VARIABLE _librt_symlink_result |
| 35 | + ) |
| 36 | + if(_librt_symlink_result EQUAL 0) |
| 37 | + message(STATUS "Created /usr/lib/x86_64-linux-gnu/librt.so -> librt.so.1") |
| 38 | + else() |
| 39 | + message(WARNING |
| 40 | + "Failed to create /usr/lib/x86_64-linux-gnu/librt.so -> librt.so.1 " |
| 41 | + "(exit code ${_librt_symlink_result}). " |
| 42 | + "Build may fail with 'No rule to make target librt.so'. " |
| 43 | + "Fix manually with: " |
| 44 | + "sudo ln -s /usr/lib/x86_64-linux-gnu/librt.so.1 /usr/lib/x86_64-linux-gnu/librt.so") |
| 45 | + endif() |
| 46 | +elseif(NOT EXISTS "/usr/lib/x86_64-linux-gnu/librt.so" |
| 47 | + AND EXISTS "/usr/lib/x86_64-linux-gnu/librt.so.1") |
| 48 | + message(WARNING |
| 49 | + "Missing /usr/lib/x86_64-linux-gnu/librt.so (required by DTK MIOpen on glibc 2.34+). " |
| 50 | + "Create it manually with: " |
| 51 | + "sudo ln -s /usr/lib/x86_64-linux-gnu/librt.so.1 /usr/lib/x86_64-linux-gnu/librt.so " |
| 52 | + "or reconfigure with -DHCU_CREATE_LIBRT_STUB=ON") |
| 53 | +endif() |
| 54 | + |
| 55 | +find_package(hip REQUIRED) |
| 56 | +message(STATUS "Found HIP: ${hip_VERSION}") |
| 57 | + |
| 58 | +# The HIP runtime (libgalaxyhip.so) carries RUNPATH=/opt/dtk/lib which |
| 59 | +# contains a bundled libunwind.so.8 (from hipprof_utils). CMake warns |
| 60 | +# about an RPATH conflict with the identical-SONAME system libunwind in |
| 61 | +# /usr/lib/x86_64-linux-gnu. The DTK compiler already treats several |
| 62 | +# /opt/dtk subdirectories as implicit link dirs; extend this to the parent |
| 63 | +# /opt/dtk/lib so CMake omits it from RPATH (the HIP library's own RUNPATH |
| 64 | +# still provides it at runtime). |
| 65 | +if(DEFINED ENV{ROCM_PATH}) |
| 66 | + file(REAL_PATH "$ENV{ROCM_PATH}/lib" _rocm_lib_real) |
| 67 | + list(APPEND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES |
| 68 | + "$ENV{ROCM_PATH}/lib" "${_rocm_lib_real}") |
| 69 | +endif() |
| 70 | + |
| 71 | +get_target_property(_torch_hip_opts torch_hip INTERFACE_COMPILE_OPTIONS) |
| 72 | +if(_torch_hip_opts) |
| 73 | + # torch_hip sets -std=c++17 in INTERFACE_COMPILE_OPTIONS, which overrides |
| 74 | + # our C++20 standard. Strip it so C++20 concepts work. |
| 75 | + # See Caffe2Targets.cmake for more details. |
| 76 | + list(FILTER _torch_hip_opts EXCLUDE REGEX "-std=c\\+\\+17") |
| 77 | + # -Wno-duplicate-decl-specifier is valid for C/ObjC only; GCC warns when |
| 78 | + # passed to C++ compilation. Strip it from the interface flags. |
| 79 | + # See Caffe2Targets.cmake for more details. |
| 80 | + list(FILTER _torch_hip_opts EXCLUDE REGEX "-Wno-duplicate-decl-specifier") |
| 81 | + set_property(TARGET torch_hip PROPERTY INTERFACE_COMPILE_OPTIONS ${_torch_hip_opts}) |
| 82 | +endif() |
| 83 | + |
| 84 | +# PyTorch pip wheels bundle libibverbs inside torch.libs/ with hashed SONAMEs |
| 85 | +# (e.g. libnl-3-04364822.so.200.26.0) that are not on the linker search path. |
| 86 | +# The linker follows libibverbs's DT_NEEDED chain and fails to resolve them at |
| 87 | +# link time, even though libtorch_hip.so's $ORIGIN/../../torch.libs RPATH |
| 88 | +# resolves them correctly at runtime. Suppress the link-time error. |
| 89 | +add_link_options(-Wl,--allow-shlib-undefined) |
| 90 | + |
| 91 | +# ------------------------------- Helper Function ------------------------------ |
| 92 | +function(target_link_hcu_libraries target_name) |
| 93 | + target_link_libraries(${target_name} PRIVATE hip::host) |
| 94 | +endfunction() |
| 95 | + |
| 96 | +message(STATUS "HCU backend configuration complete") |
0 commit comments