[Openmp-commits] [openmp] [OpenMP] Avoid using CPU-specific flags on GPU (PR #159053)

Tulio Magno Quites Machado Filho via Openmp-commits openmp-commits at lists.llvm.org
Tue Sep 16 13:50:49 PDT 2025


https://github.com/tuliom updated https://github.com/llvm/llvm-project/pull/159053

>From ba4bb1fbd4e876a60a5b34770d720bfc6331cd89 Mon Sep 17 00:00:00 2001
From: Tulio Magno Quites Machado Filho <tuliom at redhat.com>
Date: Mon, 15 Sep 2025 17:10:23 -0300
Subject: [PATCH 1/2] [OpenMP] Avoid using CPU-specific flags for GPU

Since commit be6f110bc08fd5fb622485b50e30619936acc124 we lost a few
workarounds that prevented CPU-specific flags to leak from CXXFLAGS to a
binary targetting GPUs causing errors and/or warnings.

Re-add those workarounds and expand them with -mtune,
-mbranch-protection and -fcf-protection.
---
 openmp/device/CMakeLists.txt | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/openmp/device/CMakeLists.txt b/openmp/device/CMakeLists.txt
index ded961adcce1f..76e05d5cb3cb0 100644
--- a/openmp/device/CMakeLists.txt
+++ b/openmp/device/CMakeLists.txt
@@ -36,6 +36,13 @@ list(APPEND compile_options -Wno-unknown-cuda-version)
 if(LLVM_DEFAULT_TARGET_TRIPLE)
   list(APPEND compile_options --target=${LLVM_DEFAULT_TARGET_TRIPLE})
 endif()
+# Ensure that CPU-related parameters won't be brought in.
+string(REGEX REPLACE "-mcpu[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+string(REGEX REPLACE "-march[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+string(REGEX REPLACE "-mtune[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+string(REGEX REPLACE "-mbranch-protection[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+string(REGEX REPLACE "-fcf-protection[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+list(APPEND compile_options -Wno-unused-command-line-argument)
 
 # We disable the slp vectorizer during the runtime optimization to avoid
 # vectorized accesses to the shared state. Generally, those are "good" but

>From 7ac8ce43ef836e5823c59c7689a8d284726fcd52 Mon Sep 17 00:00:00 2001
From: Tulio Magno Quites Machado Filho <tuliom at redhat.com>
Date: Mon, 15 Sep 2025 17:10:23 -0300
Subject: [PATCH 2/2] [OpenMP] Document how to specify flags to device
 libraries

Add an explanation on how to use RUNTIMES_<triple>_CMAKE_CXX_FLAGS in
order to specify different compiler flags for OpenMP device libraries.
---
 openmp/device/CMakeLists.txt  | 7 -------
 openmp/docs/SupportAndFAQ.rst | 6 ++++--
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/openmp/device/CMakeLists.txt b/openmp/device/CMakeLists.txt
index 76e05d5cb3cb0..ded961adcce1f 100644
--- a/openmp/device/CMakeLists.txt
+++ b/openmp/device/CMakeLists.txt
@@ -36,13 +36,6 @@ list(APPEND compile_options -Wno-unknown-cuda-version)
 if(LLVM_DEFAULT_TARGET_TRIPLE)
   list(APPEND compile_options --target=${LLVM_DEFAULT_TARGET_TRIPLE})
 endif()
-# Ensure that CPU-related parameters won't be brought in.
-string(REGEX REPLACE "-mcpu[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
-string(REGEX REPLACE "-march[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
-string(REGEX REPLACE "-mtune[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
-string(REGEX REPLACE "-mbranch-protection[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
-string(REGEX REPLACE "-fcf-protection[^ \t\r\n]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
-list(APPEND compile_options -Wno-unused-command-line-argument)
 
 # We disable the slp vectorizer during the runtime optimization to avoid
 # vectorized accesses to the shared state. Generally, those are "good" but
diff --git a/openmp/docs/SupportAndFAQ.rst b/openmp/docs/SupportAndFAQ.rst
index 2684d20c5b0b6..f5a84784c8de8 100644
--- a/openmp/docs/SupportAndFAQ.rst
+++ b/openmp/docs/SupportAndFAQ.rst
@@ -82,8 +82,10 @@ Support for the device library comes from a separate build of the OpenMP library
 that targets the GPU architecture. Building it requires enabling the runtime
 targets, or setting the target manually when doing a standalone build. This is
 done with the ``LLVM_RUNTIME_TARGETS`` option and then enabling the OpenMP
-runtime for the GPU target. ``RUNTIMES_<triple>_LLVM_ENABLE_RUNTIMES``. Refer to
-the cache file for the specific invocation.
+runtime for the GPU target via ``RUNTIMES_<triple>_LLVM_ENABLE_RUNTIMES``.
+It's possible to set different flags for each device library by using
+``RUNTIMES_<triple>_CMAKE_CXX_FLAGS``. Refer to the cache file for the specific
+invocation.
 
 For Nvidia offload, please see :ref:`build_nvidia_offload_capable_compiler`.
 For AMDGPU offload, please see :ref:`build_amdgpu_offload_capable_compiler`.



More information about the Openmp-commits mailing list