From d1507361ecf11c7c84c6c59aa62997b7722c8137 Mon Sep 17 00:00:00 2001 From: "Sv. Lockal" Date: Tue, 28 Jan 2025 16:52:44 +0000 Subject: [PATCH] Fix build issues for musl libc (#267) Change-Id: Ia31330b0f96669966712b58986abeca754c2cbb9 [ROCm/ROCR-Runtime commit: 5d04bd42f3856a4d1fbe51661dec3ec5968065a8] --- .../rocr-runtime/libhsakmt/src/libhsakmt.h | 5 +++-- .../tests/kfdtest/src/KFDTestUtilQueue.cpp | 6 +++--- .../libhsakmt/tests/kfdtest/src/OSWrapper.hpp | 4 ++-- .../runtime/hsa-ext-finalize/CMakeLists.txt | 12 +++++++++++ .../runtime/hsa-runtime/CMakeLists.txt | 12 +++++++++++ .../hsa-runtime/core/util/lnx/os_linux.cpp | 21 ++++++++++++------- 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/projects/rocr-runtime/libhsakmt/src/libhsakmt.h b/projects/rocr-runtime/libhsakmt/src/libhsakmt.h index 822c6fe7cc..329cbd39d0 100644 --- a/projects/rocr-runtime/libhsakmt/src/libhsakmt.h +++ b/projects/rocr-runtime/libhsakmt/src/libhsakmt.h @@ -64,14 +64,15 @@ extern HsaVersionInfo hsakmt_kfd_version_info; do { if ((minor) > hsakmt_kfd_version_info.KernelInterfaceMinorVersion)\ return HSAKMT_STATUS_NOT_SUPPORTED; } while (0) +extern int hsakmt_page_size; +extern int hsakmt_page_shift; + /* Might be defined in limits.h on platforms where it is constant (used by musl) */ /* See also: https://pubs.opengroup.org/onlinepubs/7908799/xsh/limits.h.html */ #ifndef PAGE_SIZE -extern int hsakmt_page_size; #define PAGE_SIZE hsakmt_page_size #endif #ifndef PAGE_SHIFT -extern int hsakmt_page_shift; #define PAGE_SHIFT hsakmt_page_shift #endif diff --git a/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/KFDTestUtilQueue.cpp b/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/KFDTestUtilQueue.cpp index 8f9b857a70..aa709f3ab1 100644 --- a/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/KFDTestUtilQueue.cpp +++ b/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/KFDTestUtilQueue.cpp @@ -57,13 +57,13 @@ class AsyncMPSQ { void PlacePacketOnNode(PacketList &packetList, int node, TSPattern tsp); /* Run the packets placed on nodes and return immediately.*/ - void Submit(void) { ASSERT_NE((HSAuint64)m_queue, NULL); m_queue->SubmitPacket(); } + void Submit(void) { ASSERT_NE(m_queue, nullptr); m_queue->SubmitPacket(); } /* Return only when all packets are consumed. * If there is any packet issues some IO operations, wait these IO to complete too. */ void Wait(void) { - ASSERT_NE((HSAuint64)m_queue, NULL); + ASSERT_NE(m_queue, nullptr); m_queue->Wait4PacketConsumption(m_event, std::max((unsigned int)6000, g_TestTimeOut)); } @@ -244,7 +244,7 @@ HSAuint64 AsyncMPSQ::Report(int indexOfPacket, HSAuint64 &begin, HSAuint64 &end) if (m_ts_pattern == HEAD_TAIL) indexOfPacket = 0; - EXPECT_NE((HSAuint64)m_ts, NULL) + EXPECT_NE(m_ts, nullptr) << " Error " << ++error << ": No timestamp buf!" << std::endl; /* m_ts_count is equal to packets count + 1, see PlacePacketOnNode(). * So the max index of a packet is m_ts_count - 2. diff --git a/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/OSWrapper.hpp b/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/OSWrapper.hpp index 6c3b24f1d1..ce4bfecba6 100644 --- a/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/OSWrapper.hpp +++ b/projects/rocr-runtime/libhsakmt/tests/kfdtest/src/OSWrapper.hpp @@ -23,6 +23,8 @@ #include #include +#include +#include #include #include "KFDTestFlags.hpp" @@ -33,10 +35,8 @@ #ifndef PAGE_SIZE #define PAGE_SIZE (1<<12) -#define PAGE_SHIFT (12) #endif #ifndef PAGE_SHIFT -#define PAGE_SIZE (1<<12) #define PAGE_SHIFT (12) #endif diff --git a/projects/rocr-runtime/runtime/hsa-ext-finalize/CMakeLists.txt b/projects/rocr-runtime/runtime/hsa-ext-finalize/CMakeLists.txt index 6c6dbdcba2..f18b09912d 100755 --- a/projects/rocr-runtime/runtime/hsa-ext-finalize/CMakeLists.txt +++ b/projects/rocr-runtime/runtime/hsa-ext-finalize/CMakeLists.txt @@ -101,6 +101,18 @@ if( NOT DEFINED OPEN_SOURCE_DIR ) set ( OPEN_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." ) endif() +## Check for _GNU_SOURCE pthread extensions +set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) +CHECK_SYMBOL_EXISTS ( "pthread_attr_setaffinity_np" "pthread.h" HAVE_PTHREAD_ATTR_SETAFFINITY_NP ) +CHECK_SYMBOL_EXISTS ( "pthread_rwlockattr_setkind_np" "pthread.h" HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP ) +unset(CMAKE_REQUIRED_DEFINITIONS) +if ( HAVE_PTHREAD_ATTR_SETAFFINITY_NP ) + target_compile_definitions(${CORE_RUNTIME_TARGET} PRIVATE HAVE_PTHREAD_ATTR_SETAFFINITY_NP ) +endif() +if ( HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP ) + target_compile_definitions(${CORE_RUNTIME_TARGET} PRIVATE HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP ) +endif() + ## ------------------------- Linux Compiler and Linker options ------------------------- set ( CMAKE_CXX_FLAGS "-std=c++11 " ) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt b/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt index fbbcaf032a..03a36a50cb 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt +++ b/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt @@ -109,6 +109,18 @@ if ( HAVE_MEMFD_CREATE ) target_compile_definitions(${CORE_RUNTIME_TARGET} PRIVATE HAVE_MEMFD_CREATE ) endif() +## Check for _GNU_SOURCE pthread extensions +set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) +CHECK_SYMBOL_EXISTS ( "pthread_attr_setaffinity_np" "pthread.h" HAVE_PTHREAD_ATTR_SETAFFINITY_NP ) +CHECK_SYMBOL_EXISTS ( "pthread_rwlockattr_setkind_np" "pthread.h" HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP ) +unset(CMAKE_REQUIRED_DEFINITIONS) +if ( HAVE_PTHREAD_ATTR_SETAFFINITY_NP ) + target_compile_definitions(${CORE_RUNTIME_TARGET} PRIVATE HAVE_PTHREAD_ATTR_SETAFFINITY_NP ) +endif() +if ( HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP ) + target_compile_definitions(${CORE_RUNTIME_TARGET} PRIVATE HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP ) +endif() + ## Set include directories for ROCr runtime target_include_directories( ${CORE_RUNTIME_TARGET} PUBLIC diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index d1e9d0e805..4d62979829 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -138,12 +138,14 @@ class os_thread { for (int i = 0; i < cores; i++) { CPU_SET_S(i, CPU_ALLOC_SIZE(cores), cpuset); } +#ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP err = pthread_attr_setaffinity_np(&attrib, CPU_ALLOC_SIZE(cores), cpuset); CPU_FREE(cpuset); if (err != 0) { fprintf(stderr, "pthread_setaffinity_np failed: %s\n", strerror(err)); return; } +#endif } do { @@ -166,6 +168,17 @@ class os_thread { } } while (stackSize < 20 * 1024 * 1024); +#ifndef HAVE_PTHREAD_ATTR_SETAFFINITY_NP + if (cores && cpuset) { + err = pthread_setaffinity_np(thread, CPU_ALLOC_SIZE(cores), cpuset); + CPU_FREE(cpuset); + if (err != 0) { + fprintf(stderr, "pthread_setaffinity_np failed: %s\n", strerror(err)); + thread = 0; + return; + } + } +#endif struct sched_param param = {}; if (priority != OS_THREAD_PRIORITY_DEFAULT) { int set_priority; @@ -696,18 +709,12 @@ SharedMutex CreateSharedMutex() { return nullptr; } -#ifdef __GLIBC__ +#ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP err = pthread_rwlockattr_setkind_np(&attrib, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); if (err != 0) { fprintf(stderr, "Set rw lock attribute failure: %s\n", strerror(err)); return nullptr; } -#else - err = pthread_rwlockattr_setkind(&attrib, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); - if (err != 0) { - fprintf(stderr, "Set rw lock attribute failure: %s\n", strerror(err)); - return nullptr; - } #endif pthread_rwlock_t* lock = new pthread_rwlock_t;