https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/176157
>From a0258878453f27b3abfb7e5f3f78145c7f437b63 Mon Sep 17 00:00:00 2001 From: David Truby <[email protected]> Date: Thu, 15 Jan 2026 12:53:09 +0000 Subject: [PATCH 1/2] [openmp] Add support for Arm64X to libomp This patch allows building libomp.dll and libomp.lib as Arm64X binaries containing both arm64 and arm64ec code and useable from applications compiled for both architectures. --- llvm/CMakeLists.txt | 16 +++++ llvm/runtimes/CMakeLists.txt | 6 ++ openmp/runtime/CMakeLists.txt | 11 ++++ openmp/runtime/cmake/arm64x.cmake | 97 +++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 openmp/runtime/cmake/arm64x.cmake diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 420774b629b8b..90390e1b2962e 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -223,6 +223,22 @@ if(LIBC_GPU_BUILD) list(APPEND RUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES "libc") endif() +# Set up Arm64X build of the OpenMP runtime +option(LIBOMP_ENABLE_ARM64X "Build the OpenMP library as Arm64X for compatibility with both Arm64 and Arm64EC" OFF) +if(LIBOMP_ENABLE_ARM64X) + if(NOT LLVM_HOST_TRIPLE STREQUAL "aarch64-pc-windows-msvc") + messsage(FATAL_ERROR "Arm64X builds are only supported on Windows on Arm hosts.") + endif() + if(NOT LLVM_RUNTIME_TARGETS) + set(LLVM_RUNTIME_TARGETS "default") + endif() + + if(NOT "arm64ec-pc-windows-msvc" IN_LIST LLVM_RUNTIME_TARGETS) + list(APPEND LLVM_RUNTIME_TARGETS "arm64ec-pc-windows-msvc") + endif() + set(RUNTIMES_arm64ec-pc-windows-msvc_LIBOMP_ENABLE_ARM64X ON) +endif() + foreach(_name ${LLVM_RUNTIME_TARGETS}) if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES) if("${_name}" STREQUAL "amdgcn-amd-amdhsa" OR "${_name}" STREQUAL "nvptx64-nvidia-cuda" OR "${_name}" STREQUAL "spirv64-intel-unknown") diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt index fba0c7a01f972..56f0bd7891039 100644 --- a/llvm/runtimes/CMakeLists.txt +++ b/llvm/runtimes/CMakeLists.txt @@ -716,3 +716,9 @@ if(build_runtimes) set_property(GLOBAL APPEND PROPERTY LLVM_ALL_ADDITIONAL_TEST_TARGETS runtimes ${extra_deps}) endif() endif() + +if(LIBOMP_ENABLE_ARM64X) + # TODO: This only needs to depend on openmp-arm64ec-pc-windows-msvc, but the + # openmp targets set up for cross build runtimes don't currently work correctly. + add_dependencies(runtimes-configure runtimes-arm64ec-pc-windows-msvc-build) +endif() diff --git a/openmp/runtime/CMakeLists.txt b/openmp/runtime/CMakeLists.txt index 680e845ffdc95..06d015be85d0f 100644 --- a/openmp/runtime/CMakeLists.txt +++ b/openmp/runtime/CMakeLists.txt @@ -81,6 +81,11 @@ if(LIBOMP_ARCH STREQUAL "aarch64") endif() endif() +# Include support file for arm64x builds +if(LIBOMP_ENABLE_ARM64X) + include(arm64x) +endif() + libomp_check_variable(LIBOMP_ARCH 32e x86_64 32 i386 arm ppc ppc64 ppc64le aarch64 aarch64_32 aarch64_a64fx arm64ec mic mips mips64 riscv64 loongarch64 ve s390x sparc sparcv9 wasm32) set(LIBOMP_LIB_TYPE normal CACHE STRING @@ -432,3 +437,9 @@ set(LIBOMP_OMP_TOOLS_INCLUDE_DIR ${LIBOMP_OMP_TOOLS_INCLUDE_DIR} PARENT_SCOPE) # make these variables available for tools/libompd: set(LIBOMP_SRC_DIR ${LIBOMP_SRC_DIR} PARENT_SCOPE) set(LIBOMP_OMPD_SUPPORT ${LIBOMP_OMPD_SUPPORT} PARENT_SCOPE) + +# Set up the targets we want to build arm64x libs for +if(LIBOMP_ENABLE_ARM64X) + set(ARM64X_TARGETS omp ompimp) + handle_arm64x() +endif() diff --git a/openmp/runtime/cmake/arm64x.cmake b/openmp/runtime/cmake/arm64x.cmake new file mode 100644 index 0000000000000..a32485fda1a17 --- /dev/null +++ b/openmp/runtime/cmake/arm64x.cmake @@ -0,0 +1,97 @@ +# check if we have the right linker flags to enable Arm64X +include(CheckLinkerFlag) +check_linker_flag(CXX "LINKER:/linkreprofullpathrsp:test_rsp" LIBOMP_HAVE_LINKREPROFULLPATHRSP_FLAG) +if(NOT LIBOMP_HAVE_LINKREPROFULLPATHRSP_FLAG) + message(FATAL_ERROR "Arm64X builds are enabled but the linker does not support the required flag. " + "Either update Visual Studio if using link.exe or add lld to LLVM_ENABLE_PROJECTS to use a newer lld.") +endif() + +# directory where the link.rsp file generated during arm64 build will be stored +set(arm64ReproDir "${LLVM_BINARY_DIR}/runtimes/repros-arm64ec") + +# Don't install the runtime if we are doing an arm64ec build for arm64x. +# The hybrid arm64x runtime will get installed by the host (default) runtime build +if (LIBOMP_ARCH STREQUAL "arm64ec") + set(CMAKE_SKIP_INSTALL_RULES On) +endif() + +# This function reads in the content of the rsp file outputted from the arm64ec build for a target, +# then passes the arm64ec libs and objs to the linker using /machine:arm64x to combine them with the +# arm64 counterparts and create an arm64x binary. +function(set_arm64ec_dll_dependencies target) + set(REPRO_FILE "${arm64ReproDir}/${target}.rsp") + file(STRINGS "${REPRO_FILE}" ARM64_OBJS REGEX obj\"$) + file(STRINGS "${REPRO_FILE}" ARM64_LIBS REGEX lib\"$) + string(REPLACE "\"" ";" ARM64_OBJS "${ARM64_OBJS}") + string(REPLACE "\"" ";" ARM64_LIBS "${ARM64_LIBS}") + + get_target_property(libs "${target}" LINK_FLAGS) + set(non_def "") + + # Separate out the /def flag from the other link flags, so we can replcae it with /defArm64Native. + foreach(lib ${libs}) + if(lib MATCHES ".*\.def") + string(REPLACE "/DEF:" "" def ${lib}) + else() + list(APPEND non_def "${lib}") + endif() + endforeach() + # Remove the /def link flag + set_target_properties("${target}" PROPERTIES LINK_FLAGS "${non_def}") + + target_sources("${target}" PRIVATE ${ARM64_OBJS}) + target_link_options("${target}" PRIVATE /machine:arm64x "/def:${arm64ReproDir}/${target}.def" "/defArm64Native:${def}") +endfunction() + +# Replace the /def flag with /defArm64Native and add the arm64ec /def file. +function(set_arm64ec_lib_dependencies target) + get_target_property(opts ${target} STATIC_LIBRARY_OPTIONS) + string(REPLACE "/DEF:" "/defArm64Native:" opts "${opts}") + set_target_properties(${target} PROPERTIES STATIC_LIBRARY_OPTIONS "/machine:arm64x;${opts};/def:${arm64ReproDir}/${target}.def") +endfunction() + +# Copy the def file for arm64ec to the repros directory so we can use it in the arm64x builds and add the linkreprofullpathrsp flag. +function(handle_arm64ec_target target) + get_target_property(type "${target}" TYPE) + if(type STREQUAL "SHARED_LIBRARY") + get_target_property(libs "${target}" LINK_FLAGS) + elseif(type STREQUAL "STATIC_LIBRARY") + get_target_property(libs "${target}" STATIC_LIBRARY_OPTIONS) + endif() + list(FILTER libs INCLUDE REGEX ".*\.def") + string(REPLACE "/DEF:" "" def "${libs}") + + add_custom_target("${target}.def" + BYPRODUCTS "${arm64ReproDir}/${target}.def" + COMMAND ${CMAKE_COMMAND} -E copy + "${def}" + "${arm64ReproDir}/${target}.def" + DEPENDS "${def}") + add_dependencies(${target} "${target}.def") + # tell the linker to produce this special rsp file that has absolute paths to its inputs + if(type STREQUAL "SHARED_LIBRARY") + target_link_options(${target} PRIVATE "/LINKREPROFULLPATHRSP:${arm64ReproDir}/${target}.rsp") + endif() +endfunction() + +# Handle the targets we have requested arm64x builds for. +function(handle_arm64x) + # During the arm64ec build, create rsp files that containes the absolute path to the inputs passed to the linker (objs, libs). + if("${LIBOMP_ARCH}" STREQUAL "arm64ec") + file(MAKE_DIRECTORY ${arm64ReproDir}) + foreach (target ${ARM64X_TARGETS}) + handle_arm64ec_target("${target}") + endforeach() + + # During the ARM64 build, modify the link step appropriately to produce an arm64x binary + elseif("${LIBOMP_ARCH}" STREQUAL "aarch64") + foreach (target ${ARM64X_TARGETS}) + get_target_property(type ${target} TYPE) + if(type STREQUAL "SHARED_LIBRARY") + set_arm64ec_dll_dependencies("${target}") + elseif(type STREQUAL "STATIC_LIBRARY") + set_arm64ec_lib_dependencies("${target}") + endif() + endforeach() + endif() +endfunction() >From 17db346dbcd55c452c03312a81d6a831dd67cf24 Mon Sep 17 00:00:00 2001 From: David Truby <[email protected]> Date: Tue, 3 Mar 2026 15:37:50 +0000 Subject: [PATCH 2/2] CMake fixes --- llvm/CMakeLists.txt | 4 ++-- openmp/runtime/CMakeLists.txt | 3 +-- openmp/runtime/cmake/arm64x.cmake | 16 ++++++++-------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 90390e1b2962e..f8108e31edd81 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -226,8 +226,8 @@ endif() # Set up Arm64X build of the OpenMP runtime option(LIBOMP_ENABLE_ARM64X "Build the OpenMP library as Arm64X for compatibility with both Arm64 and Arm64EC" OFF) if(LIBOMP_ENABLE_ARM64X) - if(NOT LLVM_HOST_TRIPLE STREQUAL "aarch64-pc-windows-msvc") - messsage(FATAL_ERROR "Arm64X builds are only supported on Windows on Arm hosts.") + if(NOT CMAKE_HOST_WIN32 AND NOT CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "ARM64") + message(FATAL_ERROR "Arm64X builds are only supported on Windows hosts.") endif() if(NOT LLVM_RUNTIME_TARGETS) set(LLVM_RUNTIME_TARGETS "default") diff --git a/openmp/runtime/CMakeLists.txt b/openmp/runtime/CMakeLists.txt index 06d015be85d0f..3ccfa8463810f 100644 --- a/openmp/runtime/CMakeLists.txt +++ b/openmp/runtime/CMakeLists.txt @@ -440,6 +440,5 @@ set(LIBOMP_OMPD_SUPPORT ${LIBOMP_OMPD_SUPPORT} PARENT_SCOPE) # Set up the targets we want to build arm64x libs for if(LIBOMP_ENABLE_ARM64X) - set(ARM64X_TARGETS omp ompimp) - handle_arm64x() + handle_arm64x(omp ompimp) endif() diff --git a/openmp/runtime/cmake/arm64x.cmake b/openmp/runtime/cmake/arm64x.cmake index a32485fda1a17..37682131b7c4c 100644 --- a/openmp/runtime/cmake/arm64x.cmake +++ b/openmp/runtime/cmake/arm64x.cmake @@ -28,10 +28,10 @@ function(set_arm64ec_dll_dependencies target) get_target_property(libs "${target}" LINK_FLAGS) set(non_def "") - # Separate out the /def flag from the other link flags, so we can replcae it with /defArm64Native. - foreach(lib ${libs}) + # Separate out the /def flag from the other link flags, so we can replace it with /defArm64Native. + foreach(lib IN LISTS libs) if(lib MATCHES ".*\.def") - string(REPLACE "/DEF:" "" def ${lib}) + string(REPLACE "/DEF:" "" "def" "${lib}") else() list(APPEND non_def "${lib}") endif() @@ -75,17 +75,17 @@ function(handle_arm64ec_target target) endfunction() # Handle the targets we have requested arm64x builds for. -function(handle_arm64x) +function(handle_arm64x targets) # During the arm64ec build, create rsp files that containes the absolute path to the inputs passed to the linker (objs, libs). - if("${LIBOMP_ARCH}" STREQUAL "arm64ec") + if(LIBOMP_ARCH STREQUAL "arm64ec") file(MAKE_DIRECTORY ${arm64ReproDir}) - foreach (target ${ARM64X_TARGETS}) + foreach (target IN LISTS targets) handle_arm64ec_target("${target}") endforeach() # During the ARM64 build, modify the link step appropriately to produce an arm64x binary - elseif("${LIBOMP_ARCH}" STREQUAL "aarch64") - foreach (target ${ARM64X_TARGETS}) + elseif(LIBOMP_ARCH STREQUAL "aarch64") + foreach (target IN LISTS targets) get_target_property(type ${target} TYPE) if(type STREQUAL "SHARED_LIBRARY") set_arm64ec_dll_dependencies("${target}") _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
