Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 5 additions & 66 deletions flang-rt/lib/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ find_package(Backtrace)
set(HAVE_BACKTRACE ${Backtrace_FOUND})
set(BACKTRACE_HEADER ${Backtrace_HEADER})

# Module sources that are required by other modules
set(intrinsics_sources
__fortran_builtins.f90
__cuda_builtins.f90
)

# Fortran sources for builtin .mod files
set(module_sources
__fortran_builtins.f90
__cuda_builtins.f90
__fortran_ieee_exceptions.f90
__fortran_type_info.f90
flang_debug.f90
Expand Down Expand Up @@ -215,10 +211,8 @@ file(GLOB_RECURSE private_headers
)

if (LLVM_TARGET_TRIPLE MATCHES "^ppc|^powerpc")
list(APPEND intrinsics_sources
__ppc_types.f90
)
list(APPEND module_sources
__ppc_types.f90
__ppc_intrinsics.f90
mma.f90
)
Expand Down Expand Up @@ -264,72 +258,17 @@ if (TARGET FortranFloat128MathILib)
endif ()


# When a target depends on an object library, CMake seems to try to only build
# the object files that the target actual needs. If we are only interested
# in the module files, nothing gets built at all. To ensure that the module
# files are built, insert a custom target that is opaque to CMake so it cannot
# apply this optimization. Dependees on module files must depend on this
# barrier instead. An actual COMMAND (that does nothing) seems to be necessary
# on Windows to work.
function (add_module_barrier barriername objlib)
add_custom_target(${barriername}
COMMAND ${CMAKE_COMMAND} -E true
)
add_dependencies(${barriername} ${objlib})
endfunction ()


# Build module files if requested.
# The object files written by Flang for these are unused. In the future parts
# of flang-rt may itself be implemented in Fortran in which case these Fortran
# sources need to be added to ${sources} to be included in
# libflang_rt.runtime{.a/.so}. If they also provide an importable .mod, add them
# to flang_module_target(... PUBLIC).
if (FLANG_RT_FORTRAN_MODULES)
# CMake ignores intrinsic USE dependencies
# CMake has an option Fortran_BUILDING_INSTRINSIC_MODULES/Fortran_BUILDING_INTRINSIC_MODULES
# to disable this behavior, unfortunately it does not work with Ninja
# (https://gitlab.kitware.com/cmake/cmake/-/issues/26803)
# As a workaround, we build those intrinsic modules first such that the main
# runtime can depend on it. To ensure that modules files are also transitively
# updated if a USE'd .mod file changes (a .mod stores the checksums of all
# .mod files it depends on and therefore needs to be updated as well), inject
# an file-level dependency via OBJECT_DEPENDS.

add_flangrt_library(flang_rt.mod.fortran.builtins OBJECT
__fortran_builtins.f90
)
set_property(SOURCE __fortran_builtins.f90 PROPERTY OBJECT_OUTPUTS "${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__fortran_builtins${CMAKE_Fortran_OUTPUT_EXTENSION}")
flang_module_target(flang_rt.mod.fortran.builtins PUBLIC)
add_module_barrier(flang_rt.mod.fortran.builtins.barrier flang_rt.mod.fortran.builtins)

add_flangrt_library(flang_rt.mod.cuda.builtins OBJECT
__cuda_builtins.f90
)
set_property(SOURCE __cuda_builtins.f90 PROPERTY OBJECT_OUTPUTS "${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__cuda_builtins${CMAKE_Fortran_OUTPUT_EXTENSION}")
add_dependencies(flang_rt.mod.cuda.builtins flang_rt.mod.fortran.builtins.barrier)
set_property(SOURCE __cuda_builtins.f90
APPEND PROPERTY OBJECT_DEPENDS
"${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__fortran_builtins${CMAKE_Fortran_OUTPUT_EXTENSION}"
)
flang_module_target(flang_rt.mod.cuda.builtins PUBLIC)
add_module_barrier(flang_rt.mod.cuda.builtins.barrier flang_rt.mod.cuda.builtins)

# The modules themselves
add_flangrt_library(flang_rt.mod OBJECT
add_flangrt_library(flang-rt-mod OBJECT
${module_sources}
)
add_dependencies(flang_rt.mod flang_rt.mod.fortran.builtins.barrier flang_rt.mod.cuda.builtins.barrier)
foreach(_srcfile IN LISTS module_sources)
set_property(SOURCE ${_srcfile}
APPEND PROPERTY OBJECT_DEPENDS
"${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__fortran_builtins${CMAKE_Fortran_OUTPUT_EXTENSION}"
"${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__cuda_builtins${CMAKE_Fortran_OUTPUT_EXTENSION}"
)
endforeach()

flang_module_target(flang_rt.mod PUBLIC)
add_module_barrier(flang-rt-mod flang_rt.mod)
flang_module_target(flang-rt-mod PUBLIC)

if (RUNTIMES_FORTRAN_MODULES)
set(_mod_install_dest "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}/..")
Expand Down
2 changes: 1 addition & 1 deletion flang-rt/lib/runtime/__fortran_type_info.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

module __fortran_type_info

use, intrinsic :: __fortran_builtins, &
use __fortran_builtins, &
only: __builtin_c_ptr, __builtin_c_devptr, __builtin_c_funptr
implicit none

Expand Down
4 changes: 2 additions & 2 deletions flang-rt/lib/runtime/cooperative_groups.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

module cooperative_groups

use, intrinsic :: __fortran_builtins, only: c_devptr => __builtin_c_devptr
use :: cudadevice ! implicit dependency, made explicit for CMake
use __fortran_builtins, only: c_devptr => __builtin_c_devptr
use cudadevice ! implicit dependency, made explicit for CMake

implicit none

Expand Down
6 changes: 3 additions & 3 deletions flang-rt/lib/runtime/cudadevice.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

module cudadevice
use __cuda_device
use, intrinsic :: __fortran_builtins, only: dim3 => __builtin_dim3
use, intrinsic :: __fortran_builtins, only: c_devptr => __builtin_c_devptr
use, intrinsic :: __fortran_builtins, only: c_devloc => __builtin_c_devloc
use __fortran_builtins, only: dim3 => __builtin_dim3
use __fortran_builtins, only: c_devptr => __builtin_c_devptr
use __fortran_builtins, only: c_devloc => __builtin_c_devloc
implicit none

! Synchronization Functions
Expand Down
7 changes: 0 additions & 7 deletions openmp/module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ if ("${LLVM_DEFAULT_TARGET_TRIPLE}" MATCHES "^amdgcn|^nvptx")
endif ()

flang_module_target(libomp-mod PUBLIC)
add_dependencies(libomp-mod ${RUNTIMES_FORTRAN_BUILD_DEPS})
if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
set_property(SOURCE "${CMAKE_CURRENT_BINARY_DIR}/omp_lib.F90"
APPEND PROPERTY OBJECT_DEPENDS
"${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__fortran_builtins${CMAKE_Fortran_OUTPUT_EXTENSION}"
)
endif ()

install(FILES
"${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}/omp_lib.h"
Expand Down
40 changes: 20 additions & 20 deletions openmp/module/omp_lib.F90.var
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

module omp_lib_kinds

use, intrinsic :: iso_c_binding
use :: iso_c_binding

! Set PRIVATE by default to explicitly only export what is meant
! to be exported by this MODULE.
Expand Down Expand Up @@ -640,28 +640,28 @@
end subroutine omp_display_env

function omp_target_alloc(size, device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_int
use :: iso_c_binding, only: c_ptr, c_size_t, c_int
type(c_ptr) omp_target_alloc
integer(c_size_t), value :: size
integer(c_int), value :: device_num
end function omp_target_alloc

subroutine omp_target_free(device_ptr, device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use :: iso_c_binding, only: c_ptr, c_int
type(c_ptr), value :: device_ptr
integer(c_int), value :: device_num
end subroutine omp_target_free

function omp_target_is_present(ptr, device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use :: iso_c_binding, only: c_ptr, c_int
integer(c_int) omp_target_is_present
type(c_ptr), value :: ptr
integer(c_int), value :: device_num
end function omp_target_is_present

function omp_target_memcpy(dst, src, length, dst_offset, src_offset, &
dst_device_num, src_device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_int
use :: iso_c_binding, only: c_ptr, c_size_t, c_int
integer(c_int) omp_target_memcpy
type(c_ptr), value :: dst, src
integer(c_size_t), value :: length, dst_offset, src_offset
Expand All @@ -671,7 +671,7 @@
function omp_target_memcpy_rect(dst, src, element_size, num_dims, &
volume, dst_offsets, src_offsets, dst_dimensions, &
src_dimensions, dst_device_num, src_device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_int
use :: iso_c_binding, only: c_ptr, c_size_t, c_int
integer(c_int) omp_target_memcpy_rect
type(c_ptr), value :: dst, src
integer(c_size_t), value :: element_size
Expand All @@ -684,7 +684,7 @@
src_offset, dst_device_num, src_device_num, depobj_count, &
depobj_list) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_int
use :: iso_c_binding, only: c_ptr, c_size_t, c_int
integer(c_int) omp_target_memcpy_async
type(c_ptr), value :: dst, src
integer(c_size_t), value :: length, dst_offset, src_offset
Expand All @@ -698,7 +698,7 @@
src_dimensions, dst_device_num, src_device_num, depobj_count, &
depobj_list) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_int
use :: iso_c_binding, only: c_ptr, c_size_t, c_int
integer(c_int) omp_target_memcpy_rect_async
type(c_ptr), value :: dst, src
integer(c_size_t), value :: element_size
Expand All @@ -710,7 +710,7 @@
end function omp_target_memcpy_rect_async

function omp_target_memset(ptr, val, count, device_num) bind(c)
use, intrinsic :: iso_c_binding, only : c_ptr, c_int, c_size_t
use :: iso_c_binding, only : c_ptr, c_int, c_size_t
type(c_ptr) :: omp_target_memset
type(c_ptr), value :: ptr
integer(c_int), value :: val
Expand All @@ -721,7 +721,7 @@
function omp_target_memset_async(ptr, val, count, device_num, &
depobj_count, depobj_list) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr, c_int, c_size_t
use :: iso_c_binding, only : c_ptr, c_int, c_size_t
type(c_ptr) :: omp_target_memset_async
type(c_ptr), value :: ptr
integer(c_int), value :: val
Expand All @@ -733,29 +733,29 @@

function omp_target_associate_ptr(host_ptr, device_ptr, size, &
device_offset, device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_int
use :: iso_c_binding, only: c_ptr, c_size_t, c_int
integer(c_int) omp_target_associate_ptr
type(c_ptr), value :: host_ptr, device_ptr
integer(c_size_t), value :: size, device_offset
integer(c_int), value :: device_num
end function omp_target_associate_ptr

function omp_get_mapped_ptr(ptr, device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use :: iso_c_binding, only: c_ptr, c_int
type(c_ptr) omp_get_mapped_ptr
type(c_ptr), value :: ptr
integer(c_int), value :: device_num
end function omp_get_mapped_ptr

function omp_target_disassociate_ptr(ptr, device_num) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use :: iso_c_binding, only: c_ptr, c_int
integer(c_int) omp_target_disassociate_ptr
type(c_ptr), value :: ptr
integer(c_int), value :: device_num
end function omp_target_disassociate_ptr

function omp_target_is_accessible(ptr, size, device_num) bind(c)
use, intrinsic :: iso_c_binding, only : c_ptr, c_size_t, c_int
use :: iso_c_binding, only : c_ptr, c_size_t, c_int
integer(c_int) omp_target_is_accessible
type(c_ptr), value :: ptr
integer(c_size_t), value :: size
Expand All @@ -764,39 +764,39 @@

function omp_alloc(size, allocator) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr, c_size_t
use :: iso_c_binding, only : c_ptr, c_size_t
type(c_ptr) omp_alloc
integer(c_size_t), value :: size
integer(omp_allocator_handle_kind), value :: allocator
end function omp_alloc

function omp_aligned_alloc(alignment, size, allocator) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr, c_size_t
use :: iso_c_binding, only : c_ptr, c_size_t
type(c_ptr) omp_aligned_alloc
integer(c_size_t), value :: alignment, size
integer(omp_allocator_handle_kind), value :: allocator
end function omp_aligned_alloc

function omp_calloc(nmemb, size, allocator) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr, c_size_t
use :: iso_c_binding, only : c_ptr, c_size_t
type(c_ptr) omp_calloc
integer(c_size_t), value :: nmemb, size
integer(omp_allocator_handle_kind), value :: allocator
end function omp_calloc

function omp_aligned_calloc(alignment, nmemb, size, allocator) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr, c_size_t
use :: iso_c_binding, only : c_ptr, c_size_t
type(c_ptr) omp_aligned_calloc
integer(c_size_t), value :: alignment, nmemb, size
integer(omp_allocator_handle_kind), value :: allocator
end function omp_aligned_calloc

function omp_realloc(ptr, size, allocator, free_allocator) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr, c_size_t
use :: iso_c_binding, only : c_ptr, c_size_t
type(c_ptr) omp_realloc
type(c_ptr), value :: ptr
integer(c_size_t), value :: size
Expand All @@ -806,7 +806,7 @@

subroutine omp_free(ptr, allocator) bind(c)
use omp_lib_kinds
use, intrinsic :: iso_c_binding, only : c_ptr
use :: iso_c_binding, only : c_ptr
type(c_ptr), value :: ptr
integer(omp_allocator_handle_kind), value :: allocator
end subroutine omp_free
Expand Down
24 changes: 17 additions & 7 deletions runtimes/cmake/config-Fortran.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,8 @@ endfunction ()

set(RUNTIMES_ENABLE_FORTRAN OFF)

# Insert at least one element for
#
# add_dependencies(target ${RUNTIMES_FORTRAN_BUILD_DEPS})
#
# to not fail
add_custom_target(fortran-dummy-dep)
set(RUNTIMES_FORTRAN_BUILD_DEPS fortran-dummy-dep)

set(RUNTIMES_FORTRAN_BUILD_DEPS "")

if (CMAKE_Fortran_COMPILER)
# Workarounds for older versions of CMake not recognizing FLang. Hence, we
Expand Down Expand Up @@ -244,6 +238,22 @@ function (flang_module_target tgtname)
"$<$<COMPILE_LANGUAGE:Fortran>:-fintrinsic-modules-path=${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}>"
)

# Make CMake not ignore "use, intrinsic ::"-dependencies. Unfortunately,
# it is not universally handled by CMake s.t. we currently must not use
# "use, intrinsic ::" in our sources.
# * CMake 3.22: Added Fortran_BUILDING_INSTRINSIC_MODULES handling "Unix Makefiles" generator
# * CMake 4.0: Renamed INSTRINSIC to INTRINSIC (typo fix); INSTRINSIC spelling kept but deprecated
# * CMake 4.5: Added handling by Ninja generators as well
set_target_properties(${tgtname}
PROPERTIES
Fortran_BUILDING_INTRINSIC_MODULES ON
Fortran_BUILDING_INSTRINSIC_MODULES ON
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the other changes, is this needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not strictly. However, I thought we may keep it because it is the behaviour we actually want from CMake, just not all versions implement it yet.


if (RUNTIMES_FORTRAN_BUILD_DEPS AND NOT tgtname IN_LIST RUNTIMES_FORTRAN_BUILD_DEPS)
target_link_libraries(${tgtname} PRIVATE ${RUNTIMES_FORTRAN_BUILD_DEPS})
endif ()

if (CMAKE_Fortran_COMPILER_ID MATCHES "LLVM")
target_compile_options(${tgtname} PRIVATE
# Flang bug workaround: Reformating of cooked token buffer causes
Expand Down
Loading