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
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ void AMDGPUToolChain::addClangTargetOptions(
// TODO: remove the SPIR-V bypass once it can encode (hidden) visibility.
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
options::OPT_fvisibility_ms_compat) &&
!getEffectiveTriple().isSPIRV()) {
!getEffectiveTriple().isSPIRV() && !getDriver().IsFlangMode()) {
CC1Args.push_back("-fvisibility=hidden");
CC1Args.push_back("-fapply-global-visibility-to-externs");
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/HIPAMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void HIPAMDToolChain::addClangTargetOptions(
// TODO: remove the SPIR-V bypass once it can encode (hidden) visibility.
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
options::OPT_fvisibility_ms_compat) &&
!getEffectiveTriple().isSPIRV()) {
!getEffectiveTriple().isSPIRV() && !getDriver().IsFlangMode()) {
CC1Args.append({"-fvisibility=hidden"});
CC1Args.push_back("-fapply-global-visibility-to-externs");
}
Expand Down
12 changes: 11 additions & 1 deletion flang-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ include(GetToolchainDirs)
include(FlangCommon)
include(HandleCompilerRT)
include(ExtendPath)
include(FlangRTIntrospection)


############################
Expand Down Expand Up @@ -113,6 +114,11 @@ else ()
option(FLANG_RT_ENABLE_SHARED "Build Flang-RT as a shared library." OFF)
endif ()

# Enable building Fortran modules if Fortran is enabled
option(FLANG_RT_FORTRAN_MODULES "Build Flang-RT modules" "${RUNTIMES_FORTRAN_MODULES}")
if (NOT FLANG_RT_ENABLE_STATIC AND NOT FLANG_RT_ENABLE_SHARED AND NOT FLANG_RT_FORTRAN_MODULES)
message(WARNING "LLVM_ENABLE_RUNTIMES=flang-rt, but not building anything")
endif ()

# TODO: Support tests for the GPU target.
set(FLANG_RT_INCLUDE_TESTS_default ${LLVM_INCLUDE_TESTS})
Expand Down Expand Up @@ -184,6 +190,10 @@ check_cxx_source_compiles(
"
HAVE_DECL_STRERROR_S)

# Look for support of REAL(16), if not already defined via command
# line via -DFORTRAN_SUPPORTS_REAL16=YES/NO
check_fortran_quadmath_support()

# Search for clang_rt.builtins library. Need in addition to msvcrt.
if (WIN32)
find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY)
Expand All @@ -197,7 +207,7 @@ if (UNIX AND CMAKE_SYSTEM_NAME MATCHES "AIX")
endif ()

# Indicates REAL(16) support via an external library (e.g. libquadmath).
add_compile_definitions(FLANG_RT_SUPPORTS_REAL16=$<BOOL:${FLANG_RUNTIME_F128_MATH_LIB}>)
add_compile_definitions(FLANG_RT_SUPPORTS_REAL16=$<BOOL:${FORTRAN_SUPPORTS_REAL16}>)

# Check whether the compiler can undefine a macro using the "-Wp,-U" flag.
# Aternatively, we could use
Expand Down
35 changes: 33 additions & 2 deletions flang-rt/cmake/modules/AddFlangRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ function (add_flangrt_library name)
set(build_object ON)
elseif (build_static AND build_shared)
set(build_object ON)
elseif (NOT build_static AND NOT build_shared)
# If not building a library, still build the object files
# Needed to generate the .mod files as byproduct
set(build_object ON)
endif ()

# srctargets: targets that contain source files
Expand Down Expand Up @@ -172,14 +176,18 @@ function (add_flangrt_library name)
if (BUILD_SHARED_LIBS)
if (build_shared)
set(default_target "${name_shared}")
else ()
elseif (build_static)
set(default_target "${name_static}")
else ()
set(default_target "${name_object}")
endif ()
else ()
if (build_static)
set(default_target "${name_static}")
else ()
elseif (build_shared)
set(default_target "${name_shared}")
else ()
set(default_target "${name_object}")
endif ()
endif ()
add_library(${name}.default ALIAS "${default_target}")
Expand All @@ -194,6 +202,15 @@ function (add_flangrt_library name)
endif ()
endif ()

# An alias for the target that compiles the sources. When building a shared
# and static library at the same time, the sources are compiled in an object
# library, so there can be only one.
# Can be used to introspect and change the real target's properties, like:
#
# get_target_property(compile_target ${name}.compile ALIASED_TARGET)
# target_sources(${compile_target} more_sources.c)
add_library(${name}.compile ALIAS "${srctargets}")

foreach (tgtname IN LISTS libtargets)
if (NOT WIN32)
# Use same stem name for .a and .so. Common in UNIX environments.
Expand Down Expand Up @@ -223,13 +240,27 @@ function (add_flangrt_library name)
# Minimum required C++ version for Flang-RT, even if CMAKE_CXX_STANDARD is defined to something else.
target_compile_features(${tgtname} PRIVATE cxx_std_17)

if (CMAKE_Fortran_COMPILER_ID MATCHES "LLVM")
target_compile_options(${tgtname} PRIVATE
# Always enable preprocessor regardless of file extension
"$<$<COMPILE_LANGUAGE:Fortran>:-cpp>"

# Missing type descriptors are expected for intrinsic modules
"$<$<COMPILE_LANGUAGE:Fortran>:SHELL:-mmlir;SHELL:-ignore-missing-type-desc>"
)
endif ()

# When building the flang runtime if LTO is enabled the archive file
# contains LLVM IR rather than object code. Currently flang is not
# LTO aware so cannot link this file to compiled Fortran code.
if (FLANG_RT_HAS_FNO_LTO_FLAG)
target_compile_options(${tgtname} PRIVATE -fno-lto)
endif ()

if (FORTRAN_SUPPORTS_REAL16)
target_compile_definitions(${tgtname} PRIVATE FLANG_SUPPORT_R16=1)
endif ()

# Use compiler-specific options to disable exceptions and RTTI.
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
target_compile_options(${tgtname} PRIVATE
Expand Down
30 changes: 30 additions & 0 deletions flang-rt/cmake/modules/FlangRTIntrospection.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
include(CMakePushCheckState)
include(CheckFortranSourceCompiles)

# Check whether the Fortran compiler supports real(16)/quadmath types
#
# Implementation notes:
#
# * FORTRAN_SUPPORTS_REAL16 can be set externally in a bootstrapping-runtimes
# build to ensure consistency of real(16) support between compiler and
# runtime.
#
# * cmake_push_check_state/cmake_pop_check_state is insufficient to isolate
# a compiler introspection environment, see
# https://gitlab.kitware.com/cmake/cmake/-/issues/27419
# Additionally wrap it in a function namespace.
#
function (check_fortran_quadmath_support)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_FLAGS "-ffree-form")
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") # Skip link step
check_fortran_source_compiles([[
subroutine test_quadmath
real(16) :: var1
end
]]
FORTRAN_SUPPORTS_REAL16
)
cmake_pop_check_state()
endfunction ()

2 changes: 1 addition & 1 deletion flang-rt/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (FLANG_RT_ENABLE_STATIC OR FLANG_RT_ENABLE_SHARED)
if (FLANG_RT_INCLUDE_CUF)
add_subdirectory(cuda)
endif()
else ()
elseif (FLANG_RT_FORTRAN_MODULES)
# Generate modules files only, skip the libraries
add_subdirectory(runtime)
endif ()
Expand Down
94 changes: 94 additions & 0 deletions flang-rt/lib/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ set(BACKTRACE_HEADER ${Backtrace_HEADER})

# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")

# 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_ieee_exceptions.f90
__fortran_type_info.f90
f90deviceio.f90
flang_debug.f90
iso_fortran_env.f90
iso_fortran_env_impl.f90
ieee_arithmetic.f90
ieee_exceptions.f90
ieee_features.f90
iso_c_binding.f90

__cuda_device.f90
cooperative_groups.f90
cuda_runtime_api.f90
cudadevice.f90
)

# List of files that are buildable for all devices.
set(supported_sources
${FLANG_SOURCE_DIR}/lib/Decimal/binary-to-decimal.cpp
Expand Down Expand Up @@ -192,6 +217,25 @@ file(GLOB_RECURSE private_headers
"${FLANG_SOURCE_DIR}/lib/Common/*.h"
)

if (LLVM_TARGET_TRIPLE MATCHES "^ppc|^powerpc")
list(APPEND intrinsics_sources
__ppc_types.f90
)
list(APPEND module_sources
__ppc_intrinsics.f90
mma.f90
)
endif ()

# Compile as CUDA-Fortran, not directly supported by CMake
set_property(SOURCE
__cuda_device.f90
cooperative_groups.f90
cuda_runtime_api.f90
cudadevice.f90
APPEND PROPERTY
COMPILE_OPTIONS --offload-host-only -xcuda
)

# Import changes from flang_rt.quadmath
set(f128_sources "")
Expand Down Expand Up @@ -222,6 +266,56 @@ if (TARGET FortranFloat128MathILib)
endif ()
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 get is 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.
add_flangrt_library(flang_rt.mod.intrinsics OBJECT
${intrinsics_sources}
)
flang_module_target(flang_rt.mod.intrinsics PUBLIC)
add_module_barrier(flang_rt.mod.intrinsics.barrier flang_rt.mod.intrinsics)

# The modules themselves
add_flangrt_library(flang_rt.mod OBJECT
${module_sources}
)
add_dependencies(flang_rt.mod flang_rt.mod.intrinsics.barrier)
flang_module_target(flang_rt.mod PUBLIC)
add_module_barrier(flang-rt-mod flang_rt.mod)
endif ()


# Skip building libraries if not requested
if (NOT FLANG_RT_ENABLE_STATIC AND NOT FLANG_RT_ENABLE_SHARED)
return ()
endif ()

if ("${LLVM_DEFAULT_TARGET_TRIPLE}" MATCHES "^amdgcn|^nvptx")
set(sources ${gpu_sources})
elseif(FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
!
!===------------------------------------------------------------------------===!

#include '../include/flang/Runtime/magic-numbers.h'
#include '../../../flang/include/flang/Runtime/magic-numbers.h'

! These naming shenanigans prevent names from Fortran intrinsic modules
! from being usable on INTRINSIC statements, and force the program
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
! here under another name so that IEEE_ARITHMETIC can USE it and export its
! declarations without clashing with a non-intrinsic module in a program.

#include '../include/flang/Runtime/magic-numbers.h'
#include '../../../flang/include/flang/Runtime/magic-numbers.h'

module __fortran_ieee_exceptions
use __fortran_builtins, only: &
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
module cooperative_groups

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

implicit none

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

! Fortran 2018 Clause 17

#include '../include/flang/Runtime/magic-numbers.h'
#include '../../../flang/include/flang/Runtime/magic-numbers.h'

module ieee_arithmetic
! F18 Clause 17.1p1:
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

! See Fortran 2023, subclause 16.10.2

#include '../include/flang/Runtime/magic-numbers.h'
#include '../../../flang/include/flang/Runtime/magic-numbers.h'

module iso_fortran_env

Expand Down
10 changes: 5 additions & 5 deletions flang-rt/lib/runtime/iso_fortran_env_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ static constexpr std::int32_t selectedBfloat16{3};
static constexpr std::int32_t selectedReal32{4};
static constexpr std::int32_t selectedReal64{8};

#if HAS_FLOAT80
static constexpr std::int32_t selectedReal80{10};
#elif HAS_LDBL128 || FLANG_RT_SUPPORTS_REAL16
#if FLANG_RT_SUPPORTS_REAL16
static constexpr std::int32_t selectedReal80{16};
#elif HAS_FLOAT80
static constexpr std::int32_t selectedReal80{10};
#else
static constexpr std::int32_t selectedReal80{-3};
#endif

#if HAS_LDBL128 || FLANG_RT_SUPPORTS_REAL16
#if FLANG_RT_SUPPORTS_REAL16
static constexpr std::int32_t selectedReal64x2{16};
static constexpr std::int32_t selectedReal128{16};
#elif HAS_FLOAT80
Expand Down Expand Up @@ -245,7 +245,7 @@ extern const std::int32_t FORTRAN_NAMED_CONST(__builtin_real_kinds)[]{
#if HAS_FLOAT80
10,
#endif
#if HAS_LDBL128 || FLANG_RT_SUPPORTS_REAL16
#if FLANG_RT_SUPPORTS_REAL16
16,
#endif
};
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions flang-rt/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
# for use by Lit, and delegates to LLVM's lit test handlers.

llvm_canonicalize_cmake_booleans(
FLANG_RT_FORTRAN_MODULES
FLANG_STANDALONE_BUILD
LLVM_BUILD_EXAMPLES
LLVM_BYE_LINK_INTO_TOOLS
LLVM_ENABLE_PLUGINS
LLVM_TREE_AVAILABLE
)

configure_lit_site_cfg(
Expand Down
3 changes: 2 additions & 1 deletion flang-rt/test/Driver/compare_iso_fortran_env_symbols.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
! UNSUPPORTED: offload-cuda, system-windows
! REQUIRES: fortran-modules

! RUN: %flang -c -funsigned %include/../module/iso_fortran_env_impl.f90 -o %t.f90.o
! RUN: %flang -c -funsigned %S/../../lib/runtime/iso_fortran_env_impl.f90 -o %t.f90.o

! Extract defined symbol names and sizes from the Fortran object and the
! already-built runtime library (which was compiled with the correct CMake flags).
Expand Down
2 changes: 2 additions & 0 deletions flang-rt/test/Driver/iso_fortran_env_impl.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
! (iso_fortran_env_impl.cpp) match the Fortran module definitions.

! UNSUPPORTED: offload-cuda
! REQUIRES: fortran-modules

! RUN: %flang %isysroot -L"%libdir" %s -o %t
! RUN: env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%libdir" %t | FileCheck %s

Expand Down
Loading
Loading