Skip to content

Commit

Permalink
[OpenMP] Rehash of #960 with GCC trick to get LLVM OMP (#1019)
Browse files Browse the repository at this point in the history
This is now more robust than my previous trick, using @dbabokin's code
and only hacking `openmp.cmake`, not the other ones. This works on my
machine for GCC and Clang, let's see what the CI thinks about it.

Fixes #959

---------

Co-authored-by: Dmitry Babokin <[email protected]>
  • Loading branch information
rengolin and dbabokin authored Mar 4, 2025
1 parent 70f8f17 commit 04015e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
49 changes: 34 additions & 15 deletions cmake/modules/openmp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@ option(USE_OpenMP "Use OpenMP" ON)
# This finds the library path from the system's clang for OpenMP
#
# On Fedora, it's at the same place as others, so we don't need to look elsewhere
# On Ubuntu, it's in /usr/lib/llvm-${version}, so find_package finds GOMP for GCC instead.
execute_process (
COMMAND bash -c "for lib in $(clang -lomp -### 2>&1); do echo $lib | grep -o \"\\/.*llvm.*\\w\"; done"
OUTPUT_VARIABLE LLVM_OMP_PATH
)
# Only if we found an "llvm" path that we need to add
if (LLVM_OMP_PATH)
set(CMAKE_PREFIX_PATH ${LLVM_OMP_PATH})
endif()
# On Ubuntu, it's in /usr/lib/llvm-${version}, so GCC finds GOMP instead.

if(USE_OpenMP)
find_package(OpenMP)
if(OPENMP_FOUND)
# Only bypass for GCC
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# Only if we found an "llvm" path that we need to add
find_library(LLVM_OMP NAMES omp libomp libomp.so libomp.so.5)
find_library(PTHREAD NAMES pthread libpthread libpthread.a libpthread.so)
if (NOT LLVM_OMP OR NOT PTHREAD)
message(FATAL_ERROR "LLVM OpenMP required but not found")
endif()
set(OPENMP_FOUND ON)
set(OpenMP_C_FLAGS "-fopenmp")
set(OpenMP_C_LIBRARIES "${LLVM_OMP};${PTHREAD}")
set(OpenMP_C_LIB_NAMES "omp;pthread")

message(STATUS "OpenMP found")
else()
message(FATAL_ERROR "OpenMP required but not found")
endif()
endif()
message(STATUS "OpenMP C flags: ${OpenMP_C_FLAGS}")
message(STATUS "OpenMP C libs: ${OpenMP_C_LIBRARIES}")
message(STATUS "OpenMP C libnames: ${OpenMP_C_LIB_NAMES}")

else() # GNU / LLVM

# Clang always prefers its own OpenMP
find_package(OpenMP)
if(OPENMP_FOUND)
message(STATUS "OpenMP found")
message(STATUS "OpenMP version: ${OpenMP_C_VERSION}")
message(STATUS "OpenMP C flags: ${OpenMP_C_FLAGS}")
message(STATUS "OpenMP C libs: ${OpenMP_C_LIBRARIES}")
message(STATUS "OpenMP C libnames: ${OpenMP_C_LIB_NAMES}")
else()
message(FATAL_ERROR "LLVM OpenMP required but not found")
endif()

endif() # GNU / LLVM
endif() # USE_OpenMP
2 changes: 1 addition & 1 deletion cmake/modules/xsmm-dnn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ target_include_directories(xsmm_dnn_mlp PRIVATE ${XSMM_DNN_INCLUDE_DIRS})
target_link_libraries(xsmm_dnn_mlp PRIVATE xsmm)
if (OPENMP_FOUND)
target_compile_options(xsmm_dnn_mlp PRIVATE ${OpenMP_C_FLAGS})
target_link_libraries(xsmm_dnn_mlp PRIVATE omp)
target_link_libraries(xsmm_dnn_mlp PRIVATE ${OpenMP_C_LIB_NAMES})
endif()
install(TARGETS xsmm_dnn_mlp RUNTIME DESTINATION bin)
6 changes: 1 addition & 5 deletions tools/tpp-run/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ if(USE_OneDNN)
set(ONEDNN_LIBS_INCL "-ltpp_dnnl_runner_utils")
endif()

if(USE_OpenMP)
set(OPENMP_LIBS_INCL "-lomp")
endif()

set(LIBS
${dialect_libs}
${conversion_libs}
Expand Down Expand Up @@ -81,7 +77,7 @@ target_link_options(tpp-run PRIVATE
-lmlir_runner_utils
-lmlir_async_runtime
${TPP_GPU_LINK_FLAGS}
${OPENMP_LIBS_INCL}
$<$<BOOL:${USE_OpenMP}>:${OpenMP_C_LIBRARIES}>
-Wl,--as-needed
)

Expand Down

0 comments on commit 04015e3

Please sign in to comment.