Skip to content

Commit 6fb0953

Browse files
committed
[openmp] Fix device omp_lib.mod build to use host flang-rt modules
Device runtimes sub-builds configure with LLVM_ENABLE_RUNTIMES=openmp only, so omp_lib.F90 for amdgcn/nvptx can compile before iso_c_binding.mod exists. Point libomp-mod at the host flang-rt module directory and order device runtimes builds after flang-rt-mod.
1 parent 844c3a8 commit 6fb0953

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

llvm/runtimes/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,21 @@ function(runtime_register_target name)
455455
list(APPEND ${name}_extra_args -DLLVM_USE_LINKER=${LLVM_USE_LINKER})
456456
append_passthrough_options(${name}_extra_args RUNTIMES "${ARG_BASE_NAME};${name}")
457457

458+
# Device-side omp_lib.mod needs host flang-rt intrinsic modules. Device
459+
# runtimes sub-builds are configured with LLVM_ENABLE_RUNTIMES=openmp only.
460+
if ("openmp" IN_LIST RUNTIMES_${name}_LLVM_ENABLE_RUNTIMES)
461+
if ("${name}" MATCHES "^(amdgcn|nvptx)" AND TARGET flang-rt-mod)
462+
list(APPEND ARG_DEPENDS flang-rt-mod)
463+
include(GetClangResourceDir)
464+
include(GetToolchainDirs)
465+
include(ExtendPath)
466+
get_clang_resource_dir(_resource_dir PREFIX "${LLVM_BINARY_DIR}")
467+
get_toolchain_module_subdir(_mod_subdir)
468+
extend_path(_host_mod_dir "${_resource_dir}" "${_mod_subdir}/${LLVM_HOST_TRIPLE}")
469+
list(APPEND ${name}_extra_args "-DLIBOMP_HOST_FLANG_RT_MODULE_DIR=${_host_mod_dir}")
470+
endif()
471+
endif()
472+
458473
set_enable_per_target_runtime_dir()
459474

460475
llvm_ExternalProject_Add(runtimes-${name}
@@ -800,6 +815,12 @@ if(build_runtimes)
800815
EXTRA_ARGS TARGET_TRIPLE ${name} ${extra_args})
801816
endforeach()
802817

818+
foreach(_device IN ITEMS amdgcn-amd-amdhsa nvptx64-nvidia-cuda)
819+
if (TARGET runtimes-${_device}-build AND TARGET flang-rt-mod)
820+
add_dependencies(runtimes-${_device}-build flang-rt-mod)
821+
endif()
822+
endforeach()
823+
803824
foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
804825
foreach(name ${LLVM_RUNTIME_MULTILIB_${multilib}_TARGETS})
805826
runtime_register_target(${name}+${multilib}

openmp/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ set(LIBOMP_BUILD_DATE "No_Timestamp")
117117
set(LIBOMP_FORTRAN_MODULES "${RUNTIMES_FORTRAN_MODULES}" CACHE BOOL
118118
"Create Fortran module files? (requires fortran compiler)")
119119

120+
set(LIBOMP_HOST_FLANG_RT_MODULE_DIR "" CACHE PATH
121+
"Directory containing host flang-rt .mod files (e.g. iso_c_binding.mod) when building device-side omp_lib.mod")
122+
120123
option(LIBOMP_FORTRAN_MODULES_ONLY
121124
"Build only Fortran modules without libomp runtime library" OFF)
122125
if(LIBOMP_FORTRAN_MODULES_ONLY)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#===-- openmp/cmake/OpenMPDeviceFortranModules.cmake ---------------------===#
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
#===------------------------------------------------------------------------===#
8+
#
9+
# Device-side OpenMP Fortran modules (omp_lib.mod) use iso_c_binding and other
10+
# intrinsic modules produced by the host flang-rt build. Device runtimes
11+
# sub-builds are typically configured with LLVM_ENABLE_RUNTIMES=openmp only, so
12+
# config-Fortran.cmake does not wire up flang-rt-mod automatically.
13+
#
14+
#===------------------------------------------------------------------------===#
15+
16+
include_guard(DIRECTORY)
17+
18+
function(openmp_get_host_flang_rt_module_dir out_var)
19+
if (LIBOMP_HOST_FLANG_RT_MODULE_DIR)
20+
set(${out_var} "${LIBOMP_HOST_FLANG_RT_MODULE_DIR}" PARENT_SCOPE)
21+
return()
22+
endif()
23+
24+
if (NOT LLVM_BINARY_DIR OR NOT LLVM_HOST_TRIPLE)
25+
set(${out_var} "" PARENT_SCOPE)
26+
return()
27+
endif()
28+
29+
include(GetClangResourceDir)
30+
include(GetToolchainDirs)
31+
include(ExtendPath)
32+
33+
get_clang_resource_dir(_resource_dir PREFIX "${LLVM_BINARY_DIR}")
34+
get_toolchain_module_subdir(_mod_subdir)
35+
extend_path(_host_mod_dir "${_resource_dir}" "${_mod_subdir}/${LLVM_HOST_TRIPLE}")
36+
37+
set(${out_var} "${_host_mod_dir}" PARENT_SCOPE)
38+
endfunction()
39+
40+
function(openmp_require_host_flang_rt_modules_for_device_mod target)
41+
if (NOT "${LLVM_DEFAULT_TARGET_TRIPLE}" MATCHES "^amdgcn|^nvptx|^spirv")
42+
return()
43+
endif()
44+
45+
# Same sub-build also builds flang-rt: existing flang_module_target() wiring is enough.
46+
if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES AND TARGET flang-rt-mod)
47+
add_dependencies(${target} flang-rt-mod)
48+
return()
49+
endif()
50+
51+
openmp_get_host_flang_rt_module_dir(_host_mod_dir)
52+
if (NOT _host_mod_dir)
53+
message(FATAL_ERROR
54+
"Building device-side ${target} for ${LLVM_DEFAULT_TARGET_TRIPLE} requires "
55+
"host Flang-RT intrinsic modules (e.g. iso_c_binding.mod). "
56+
"Set -DLIBOMP_HOST_FLANG_RT_MODULE_DIR=<path-to-host-modules> or ensure the "
57+
"host flang-rt-mod target is built before the device runtimes build.")
58+
endif()
59+
60+
target_compile_options(${target} PRIVATE
61+
"$<$<COMPILE_LANGUAGE:Fortran>:-fintrinsic-modules-path=${_host_mod_dir}>")
62+
63+
if (TARGET flang-rt-mod)
64+
add_dependencies(${target} flang-rt-mod)
65+
endif()
66+
endfunction()

openmp/module/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ endif ()
3434

3535
flang_module_target(libomp-mod PUBLIC)
3636

37+
include(OpenMPDeviceFortranModules)
38+
openmp_require_host_flang_rt_modules_for_device_mod(libomp-mod)
39+
3740
install(FILES
3841
"${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}/omp_lib.h"
3942
DESTINATION "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}"

0 commit comments

Comments
 (0)