Skip to content
Open
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
4 changes: 4 additions & 0 deletions clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@ class ToolChain {
// Returns Triple without the OSs version.
llvm::Triple getTripleWithoutOSVersion() const;

/// Returns the target-specific path for Flang's intrinsic modules in the
/// resource directory if it exists.
std::optional<std::string> getDefaultIntrinsicModuleDir() const;

// Returns the target specific runtime path if it exists.
std::optional<std::string> getRuntimePath() const;

Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Options/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6124,7 +6124,7 @@ def prebind : Flag<["-"], "prebind">;
def preload : Flag<["-"], "preload">;
def print_file_name_EQ : Joined<["-", "--"], "print-file-name=">,
HelpText<"Print the full library path of <file>">, MetaVarName<"<file>">,
Visibility<[ClangOption, CLOption]>;
Visibility<[ClangOption, FlangOption, CLOption]>;
def print_ivar_layout : Flag<["-"], "print-ivar-layout">,
Visibility<[ClangOption, CC1Option]>,
HelpText<"Enable Objective-C Ivar layout bitmap print trace">,
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6607,6 +6607,17 @@ std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
if (llvm::sys::fs::exists(Twine(P)))
return std::string(P);

// With Flang, also look for instrinsic modules
if (IsFlangMode()) {
if (std::optional<std::string> IntrPath =
TC.getDefaultIntrinsicModuleDir()) {
SmallString<128> P(*IntrPath);
llvm::sys::path::append(P, Name);
if (llvm::sys::fs::exists(Twine(P)))
return std::string(P);
}
}

SmallString<128> D(Dir);
llvm::sys::path::append(D, "..", Name);
if (llvm::sys::fs::exists(Twine(D)))
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
return {};
}

std::optional<std::string> ToolChain::getDefaultIntrinsicModuleDir() const {
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "finclude", "flang");
return getTargetSubDirPath(P);
}

std::optional<std::string> ToolChain::getRuntimePath() const {
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "lib");
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ void AMDGPUToolChain::addClangTargetOptions(
// Default to "hidden" visibility, as object level linking will not be
// supported for the foreseeable future.
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
options::OPT_fvisibility_ms_compat)) {
options::OPT_fvisibility_ms_compat) &&
!getDriver().IsFlangMode()) {
CC1Args.push_back("-fvisibility=hidden");
CC1Args.push_back("-fapply-global-visibility-to-externs");
}
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,14 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-resource-dir");
CmdArgs.push_back(D.ResourceDir.c_str());

// Default intrinsic module dirs must be added after any user-provided
// -fintrinsic-modules-path to have lower precedence
if (std::optional<std::string> IntrModPath =
TC.getDefaultIntrinsicModuleDir()) {
CmdArgs.push_back("-fintrinsic-modules-path");
CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
}

// Offloading related options
addOffloadOptions(C, Inputs, JA, Args, CmdArgs);

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/HIPAMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ void HIPAMDToolChain::addClangTargetOptions(
// Default to "hidden" visibility, as object level linking will not be
// supported for the foreseeable future.
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
options::OPT_fvisibility_ms_compat)) {
options::OPT_fvisibility_ms_compat) &&
!getDriver().IsFlangMode()) {
CC1Args.append({"-fvisibility=hidden"});
CC1Args.push_back("-fapply-global-visibility-to-externs");
}
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/HIPSPV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void HIPSPVToolChain::addClangTargetOptions(
// Default to "hidden" visibility, as object level linking will not be
// supported for the foreseeable future.
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
options::OPT_fvisibility_ms_compat))
options::OPT_fvisibility_ms_compat) &&
!getDriver().IsFlangMode())
CC1Args.append(
{"-fvisibility=hidden", "-fapply-global-visibility-to-externs"});

Expand Down
31 changes: 23 additions & 8 deletions flang-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ list(APPEND CMAKE_MODULE_PATH
include(AddFlangRT)
include(GetToolchainDirs)
include(FlangCommon)
include(FlangRTIntrospection)
include(HandleCompilerRT)
include(ExtendPath)
include(CheckFortranSourceCompiles)
include(CMakePushCheckState)


############################
Expand All @@ -49,8 +52,6 @@ set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")
# Important: flang-rt user options must be prefixed with "FLANG_RT_". Variables
# with this prefix will be forwarded in bootstrap builds.

option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")

# Provide an interface to link against the LLVM libc/libc++ projects directly.
set(FLANG_RT_SUPPORTED_PROVIDERS system llvm)
set(FLANG_RT_LIBC_PROVIDER "system" CACHE STRING "Specify C library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
Expand All @@ -63,7 +64,14 @@ if (NOT "${FLANG_RT_LIBCXX_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
message(FATAL_ERROR "Unsupported library: '${FLANG_RT_LIBCXX_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
endif ()

option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." ON)
if (LLVM_RUNTIMES_TARGET MATCHES "^amdgcn|^nvptx")
# Compiling libraries for offload targets is currently experimental;
# Only build the builtin modules by default.
set(FLANG_RT_ENABLE_STATIC_default OFF)
else ()
set(FLANG_RT_ENABLE_STATIC_default ON)
endif ()
option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." "${FLANG_RT_ENABLE_STATIC_default}")
if (WIN32)
# Windows DLL currently not implemented.
set(FLANG_RT_ENABLE_SHARED OFF)
Expand All @@ -76,11 +84,14 @@ else ()
# breaking change unless the driver is changed.
option(FLANG_RT_ENABLE_SHARED "Build Flang-RT as a shared library." OFF)
endif ()
if (NOT FLANG_RT_ENABLE_STATIC AND NOT FLANG_RT_ENABLE_SHARED)
message(FATAL_ERROR "
Must build at least one type of library
(FLANG_RT_ENABLE_STATIC=ON, FLANG_RT_ENABLE_SHARED=ON, or both)
")


if (FLANG_RT_ENABLE_STATIC OR FLANG_RT_ENABLE_SHARED)
option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")
else ()
# Tests require at least one of the libraries
message(STATUS "Flang-RT testing disabled without either FLANG_RT_ENABLE_STATIC OR FLANG_RT_ENABLE_SHARED")
set(FLANG_RT_INCLUDE_TESTS OFF)
endif ()


Expand Down Expand Up @@ -146,6 +157,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 Down
30 changes: 28 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 @@ -168,14 +172,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 @@ -190,6 +198,12 @@ function (add_flangrt_library name)
endif ()
endif ()

if (build_object)
add_library(${name}.compile ALIAS "${name_object}")
else ()
add_library(${name}.compile ALIAS "${default_target}")
endif ()

foreach (tgtname IN LISTS libtargets)
if (NOT WIN32)
# Use same stem name for .a and .so. Common in UNIX environments.
Expand Down Expand Up @@ -219,13 +233,25 @@ 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)

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>"
)

# 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
15 changes: 11 additions & 4 deletions flang-rt/cmake/modules/AddFlangRTOffload.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,30 @@ macro(enable_omp_offload_compilation name files)
"${FLANG_RT_DEVICE_ARCHITECTURES}"
)

set(OMP_COMPILE_OPTIONS
set(OMP_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:C,CXX>:
-fopenmp
-fvisibility=hidden
-fopenmp-cuda-mode
--offload-arch=${compile_for_architectures}
# Force LTO for the device part.
-foffload-lto
)
set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS
"${OMP_COMPILE_OPTIONS}"
>)
set_property(SOURCE ${files} APPEND
PROPERTY COMPILE_DEFINITIONS ${OMP_COMPILE_OPTIONS}
)
target_link_options(${name}.static PUBLIC ${OMP_COMPILE_OPTIONS})

# Enable "declare target" in the source code.
set_source_files_properties(${files}
PROPERTIES COMPILE_DEFINITIONS OMP_OFFLOAD_BUILD
)

# If building flang-rt together with libomp, ensure that libomp is built
# first and found because -fopenmp will try to link it.
if (TARGET omp)
add_dependencies(${name} omp)
target_link_options(${name}.static PUBLIC "-L$<TARGET_FILE_DIR:omp>")
endif ()
else()
message(FATAL_ERROR
"Flang-rt build with OpenMP offload is not supported for these compilers:\n"
Expand Down
36 changes: 36 additions & 0 deletions flang-rt/cmake/modules/FlangRTIntrospection.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#===-- cmake/modules/FlangRTIntrospection.cmake ----------------------------===#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===------------------------------------------------------------------------===#

include(CMakePushCheckState)

# 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 ()

15 changes: 10 additions & 5 deletions flang-rt/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
#
#===------------------------------------------------------------------------===#

add_subdirectory(quadmath)
add_subdirectory(runtime)
if (FLANG_RT_INCLUDE_CUF)
add_subdirectory(cuda)
endif()
if (FLANG_RT_ENABLE_STATIC OR FLANG_RT_ENABLE_SHARED)
add_subdirectory(quadmath)
add_subdirectory(runtime)
if (FLANG_RT_INCLUDE_CUF)
add_subdirectory(cuda)
endif()
else ()
# Generate modules files only, skip the libraries
add_subdirectory(runtime)
endif ()

if (FLANG_RT_INCLUDE_TESTS)
add_subdirectory(Testing)
Expand Down
Loading