[openmp] Add support for Arm64X to libomp (#176157)
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.
This commit is contained in:
parent
b66d98afb9
commit
720b8ea893
@ -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 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")
|
||||
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")
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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,8 @@ 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)
|
||||
handle_arm64x(omp ompimp)
|
||||
endif()
|
||||
|
||||
98
openmp/runtime/cmake/arm64x.cmake
Normal file
98
openmp/runtime/cmake/arm64x.cmake
Normal file
@ -0,0 +1,98 @@
|
||||
# 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 replace it with /defArm64Native.
|
||||
foreach(lib IN LISTS 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)
|
||||
set(targets "${ARGV}")
|
||||
# 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 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 IN LISTS 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()
|
||||
Loading…
x
Reference in New Issue
Block a user