mirror-llvm-project/openmp/module/CMakeLists.txt
Spencer Bryngelson 2f5771c8ec
[openmp][cmake] Match GPU triples consistently in openmp/module (#211138)
Fixes #211135.

`openmp/module/CMakeLists.txt:29` gates the GPU-only Fortran compile
options on `"^amdgcn|^nvptx"`, while `openmp/CMakeLists.txt:176-177`
selects host-vs-device layout for the same build using
`"^amdgpu|^amdgcn|^nvptx|^spirv64"` against both
`LLVM_DEFAULT_TARGET_TRIPLE` and `CMAKE_CXX_COMPILER_TARGET`.

All four offload cache files use the triple `amdgpu-amd-amdhsa`, which
`^amdgcn` does not match, so `-nogpulib -flto` are silently not applied
to `libomp-mod` in the recommended AMDGPU offload configurations.

Compute the test once as `LIBOMP_TARGET_IS_GPU` in
`openmp/CMakeLists.txt` and use it at both sites, rather than
duplicating a widened regex. The conditions differ, not just the
patterns: copying the regex alone would leave a build that sets
`CMAKE_CXX_COMPILER_TARGET` without `LLVM_DEFAULT_TARGET_TRIPLE` still
taking the device path in `openmp/` while `libomp-mod` misses the flags.
`libomp-mod` compiles Fortran, so `CMAKE_Fortran_COMPILER_TARGET` is
also consulted; neither site did before.

Evaluated with CMake's regex engine:

| triple | before | after |
|---|---|---|
| `amdgpu-amd-amdhsa` | 0 | 1 |
| `amdgcn-amd-amdhsa` | 1 | 1 |
| `nvptx64-nvidia-cuda` | 1 | 1 |
| `spirv64-amd-amdhsa` | 0 | 1 |
| `x86_64-unknown-linux-gnu` | 0 | 0 |

`^spirv64` only brings `module/` in line with
`openmp/CMakeLists.txt:176`, which already routes spirv64 to `device/`.
Host builds are unaffected.

Roughly five other sites carry the same `amdgcn`-only pattern
(`offload/CMakeLists.txt:30`, `flang-rt/CMakeLists.txt:126`,
`flang-rt/lib/runtime/CMakeLists.txt:317`,
`flang-rt/cmake/modules/AddFlangRT.cmake:298`,
`cmake/Modules/GetToolchainDirs.cmake:118`) and are left for a
follow-up.

No test: configure-time logic, no test mechanism exists in `openmp/`.

This affects performance-critical applications on large AMD GPU
supercomputers, including [MFC](https://github.com/MFlowCode/MFC).

All numbers above come from the validated reproducers included with this
report and are independently reproducible; they stand on their own.

This was found and root-caused with the assistance of AI tools.
2026-07-22 14:49:38 +00:00

53 lines
2 KiB
CMake

#//===----------------------------------------------------------------------===//
#//
#// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
#// See https://llvm.org/LICENSE.txt for license information.
#// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#//
#//===----------------------------------------------------------------------===//
# Build the module files if a Fortran compiler is available.
configure_file(omp_lib.F90.var "${CMAKE_CURRENT_BINARY_DIR}/omp_lib.F90" @ONLY)
configure_file(omp_lib.h.var "${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}/omp_lib.h" @ONLY)
# One compilation step creates both, omp_lib.mod and omp_lib_kinds.mod. Only
# these files are used, the object file itself can be discarded.
# TODO: Adding it to libomp ($<TARGET_OBJECTS:libomp-mod>) would allow implementing Fortran API in Fortran
add_library(libomp-mod OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/omp_lib.F90"
)
set_target_properties(libomp-mod PROPERTIES FOLDER "OpenMP/Fortran Modules")
# The following requests explicit building of the Fortran module files
# Workaround for gfortran to build modules with the
# omp_sched_monotonic integer parameter
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
target_compile_options(libomp-mod PRIVATE -fno-range-check)
endif ()
if (LIBOMP_TARGET_IS_GPU)
target_compile_options(libomp-mod PRIVATE
$<$<COMPILE_LANGUAGE:Fortran>:-nogpulib -flto>
)
endif ()
flang_module_target(libomp-mod PUBLIC)
install(FILES
"${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}/omp_lib.h"
DESTINATION "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}"
COMPONENT libomp-mod
EXCLUDE_FROM_ALL
)
if (NOT CMAKE_CONFIGURATION_TYPES)
add_custom_target(install-libomp-mod
DEPENDS libomp-mod
COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
--component libomp-mod)
add_custom_target(install-libomp-mod-stripped
DEPENDS libomp-mod
COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
--component libomp-mod --strip)
endif ()