Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
76 changes: 47 additions & 29 deletions flang-rt/lib/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(HAVE_BACKTRACE ${Backtrace_FOUND})
set(BACKTRACE_HEADER ${Backtrace_HEADER})

# Module sources that are required by other modules
# Compiled sequentially to ensure dependencies
set(intrinsics_sources
__fortran_builtins.f90
__cuda_builtins.f90
Expand Down Expand Up @@ -295,39 +296,56 @@ if (FLANG_RT_FORTRAN_MODULES)
# 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.
if (RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND)
# 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.
set(intr_mod_deps "")
set(intr_mod_barriers "")
macro (module_dependency _srcfile)
set(_libname "flang_rt.mod.${_srcfile}")
set(_barriername "flang_rt.mod.${_srcfile}.barrier")
set(_objpath "${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/${_srcfile}${CMAKE_Fortran_OUTPUT_EXTENSION}")

add_flangrt_library("${_libname}" OBJECT "${_srcfile}")
set_property(SOURCE "${_srcfile}" PROPERTY OBJECT_OUTPUTS "${_objpath}")
set_property(SOURCE "${_srcfile}" APPEND PROPERTY OBJECT_DEPENDS "${intr_mod_deps}"
)
flang_module_target("${_libname}" PUBLIC)
if (intr_mod_barriers)
add_dependencies("${_libname}" ${intr_mod_barriers})
endif ()
add_module_barrier("${_barriername}" "${_libname}")

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)
list(APPEND intr_mod_deps "${_objpath}")
list(APPEND intr_mod_barriers "${_barriername}")
endmacro ()

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)
foreach (_srcfile IN LISTS intrinsics_sources)
module_dependency("${_srcfile}")
endforeach ()

# The modules themselves
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}"
# The modules that CMake correctly tracks dependencies of
add_flangrt_library(flang_rt.mod OBJECT
${module_sources}
)
endforeach()

add_dependencies(flang_rt.mod ${intr_mod_barriers})
foreach(_srcfile IN LISTS module_sources)
set_property(SOURCE ${_srcfile}
APPEND PROPERTY OBJECT_DEPENDS "${intr_mod_deps}"
)
endforeach()
else ()
# Workaround not needed
add_flangrt_library(flang_rt.mod OBJECT
${intrinsics_sources}
${module_sources}
)
endif ()
flang_module_target(flang_rt.mod PUBLIC)
add_module_barrier(flang-rt-mod flang_rt.mod)

Expand Down
4 changes: 2 additions & 2 deletions openmp/module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ endif ()

flang_module_target(libomp-mod PUBLIC)
add_dependencies(libomp-mod ${RUNTIMES_FORTRAN_BUILD_DEPS})
if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
if (RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND AND "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}"
"${CMAKE_BINARY_DIR}/modules/${CMAKE_CFG_INTDIR}/__fortran_builtins.f90${CMAKE_Fortran_OUTPUT_EXTENSION}"
)
endif ()

Expand Down
27 changes: 27 additions & 0 deletions runtimes/cmake/config-Fortran.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# RUNTIMES_INSTALL_RESOURCE_MOD_PATH - Where to install intrinsic module files
# in the install prefix. Relative to CMAKE_INSTALL_PREFIX. Only used when
# RUNTIMES_FORTRAN_MODULES is ON.
#
# RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND


# Check whether the Fortran compiler already has access to builtin modules. Sets
Expand Down Expand Up @@ -188,6 +190,25 @@ option(RUNTIMES_FORTRAN_MODULES "Make Fortran .mod files available to Flang; sho

# Determine the paths for Fortran .mod files.
if (RUNTIMES_FORTRAN_MODULES)
set(RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND ON)
if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
# "Unix Makefiles" generator supports CMAKE_Fortran_BUILDING_IN(S)TRINSIC_MODULES
set(RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND OFF)
elseif (CMAKE_GENERATOR MATCHES "^Ninja")
# Ninja generator supports CMAKE_Fortran_BUILDING_IN(S)TRINSIC_MODULES
# starting with CMake 4.5
if (CMAKE_VERSION VERSION_GREATER_EQUAL "4.5")
set(RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND OFF)
endif ()
endif ()
if (RUNTIMES_NEED_INTRINSIC_MODULES_WORKAROUND)
message(STATUS "CMAKE_Fortran_BUILDING_IN(S)TRINSIC_MODULES: workaround enabled")
else ()
message(STATUS "CMAKE_Fortran_BUILDING_IN(S)TRINSIC_MODULES: assumed to work")
endif ()



# Flang expects its builtin modules in Clang's resource directory.
get_toolchain_module_subdir(toolchain_mod_subdir)
extend_path(RUNTIMES_OUTPUT_RESOURCE_MOD_DIR "${RUNTIMES_OUTPUT_RESOURCE_DIR}" "${toolchain_mod_subdir}")
Expand Down Expand Up @@ -244,6 +265,12 @@ function (flang_module_target tgtname)
"$<$<COMPILE_LANGUAGE:Fortran>:-fintrinsic-modules-path=${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}>"
)

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 (CMAKE_Fortran_COMPILER_ID MATCHES "LLVM")
target_compile_options(${tgtname} PRIVATE
# Flang bug workaround: Reformating of cooked token buffer causes
Expand Down