Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
839198d
[Flang][Flang-RT][OpenMP] Move builtin .mod generation into runtimes
Meinersbur Jul 17, 2025
a1533cc
python format
Meinersbur Jul 17, 2025
60ad12f
Minimize changes in .f90 files
Meinersbur Jul 17, 2025
e58c524
Move CMake 3.20 workaround to where it is needed
Meinersbur Jul 17, 2025
7d1a8be
Don't forget to enable Fortran
Meinersbur Jul 17, 2025
adc0149
enable_fortran test for CMake 3.20
Meinersbur Jul 17, 2025
498d60c
CI test
Meinersbur Jul 18, 2025
1bb0144
Enforce REAL(16) support
Meinersbur Jul 18, 2025
8ec6d03
Also add dependency barrier for WIN32
Meinersbur Jul 18, 2025
580a0c5
Dependency barrier test
Meinersbur Jul 18, 2025
979691a
Dependency barrier info
Meinersbur Jul 18, 2025
a720b31
Merge branch 'main' into flang_builtin-mods
Meinersbur Jul 19, 2025
f280fba
Merge branch 'main' into flang_builtin-mods
Meinersbur Jul 21, 2025
b4adeab
Avoid unrelated changes
Meinersbur Jul 21, 2025
c102b36
Merge branch 'main' into flang_builtin-mods
Meinersbur Jul 22, 2025
c40f43c
Use -fintrinsic-modules-path=
Meinersbur Jul 22, 2025
18422d4
Rework intrinsic-module-path.f90 test
Meinersbur Jul 22, 2025
9143388
Remove hint to reduce diff size
Meinersbur Jul 22, 2025
68efc67
Enable preprocessor for test
Meinersbur Jul 22, 2025
a00485b
python-format
Meinersbur Jul 22, 2025
bfb16f5
Merge branch 'main' into flang_builtin-mods
Meinersbur Aug 28, 2025
593baa6
Merge commit 'e6f5aa9e2b69d1e84bd281c2525004800828b30d^' into flang_b…
Meinersbur Oct 23, 2025
08255eb
Merge commit 'e6f5aa9e2b69d1e84bd281c2525004800828b30d' into flang_bu…
Meinersbur Oct 23, 2025
0454c86
Merge commit 'e915d9addeae5431f869d9a10021382871c11169^' into flang_b…
Meinersbur Oct 23, 2025
feffa35
Merge commit 'e915d9addeae5431f869d9a10021382871c11169' into flang_bu…
Meinersbur Oct 23, 2025
3cb53d7
Merge commit 'f205a2c0a093767144f474fe54bbfa103e1d6431^' into flang_b…
Meinersbur Oct 23, 2025
d3b5285
Merge commit 'f205a2c0a093767144f474fe54bbfa103e1d6431' into flang_bu…
Meinersbur Oct 23, 2025
2cfab17
Merge commit '3f3a20f654f913f7e251e3bf4bd5a63e73e5571a^' into flang_b…
Meinersbur Oct 23, 2025
5ee6a1c
Merge commit '3f3a20f654f913f7e251e3bf4bd5a63e73e5571a' into flang_bu…
Meinersbur Oct 23, 2025
72cfb12
Merge commit 'c2765b74ed2bb71bebe96c7dd49f69ade714e5a8^' into flang_b…
Meinersbur Oct 23, 2025
b4f0763
Merge commit 'c2765b74ed2bb71bebe96c7dd49f69ade714e5a8' into flang_bu…
Meinersbur Oct 23, 2025
a2251d9
Merge commit '2499fe1ac40f7a50e71bf93c26921f93df2bcc52^' into flang_b…
Meinersbur Oct 23, 2025
7e5b145
Merge commit '2499fe1ac40f7a50e71bf93c26921f93df2bcc52' into flang_bu…
Meinersbur Oct 23, 2025
a2477de
Post-merge fixes
Meinersbur Oct 23, 2025
8884ed2
Merge branch 'main' into flang_builtin-mods
Meinersbur Oct 23, 2025
731288c
Post-merge fixes
Meinersbur Oct 23, 2025
2de49b8
Test fixes
Meinersbur Oct 24, 2025
c6dbb47
Search path fix
Meinersbur Oct 24, 2025
f2ad4b2
Build config fixes
Meinersbur Oct 24, 2025
7578763
Separate flang-rt test fix
Meinersbur Oct 24, 2025
a07aaca
cleanup
Meinersbur Oct 25, 2025
6368ca2
Cleanup switching on/off tests
Meinersbur Oct 29, 2025
4bec2f7
clang-format
Meinersbur Oct 29, 2025
c53c94d
finclude -> finclude/flang
Meinersbur Oct 29, 2025
7133be1
remove diagnostic code and workarounds
Meinersbur Oct 29, 2025
25625fc
Reuse LIBOMP_FORTRAN_MODULES
Meinersbur Oct 30, 2025
a195150
Allow passing addition OPENMP_TEST_Fortran_FLAGS flags
Meinersbur Oct 30, 2025
511d809
Merge branch 'main' into flang_builtin-mods
Meinersbur Oct 30, 2025
2e72c7a
Remove workaround
Meinersbur Oct 30, 2025
95e9020
Remove legacy Fortran handling
Meinersbur Oct 31, 2025
17b235c
FLANG_TEST_ENABLE_INTRINSICS -> FLANG_TEST_ENABLE_MODULES
Meinersbur Oct 31, 2025
ef680fc
Apply python code formatting
Meinersbur Oct 31, 2025
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/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6052,7 +6052,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
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
11 changes: 11 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6541,6 +6541,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 @@ -1017,6 +1017,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
8 changes: 8 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ function (get_toolchain_library_subdir outvar)
endfunction ()


# Corresponds to Flang's ToolChain::getDefaultIntrinsicModuleDir().
function (get_toolchain_module_subdir outvar)
set(outval "finclude/flang")

get_toolchain_arch_dirname(arch_dirname)
set(outval "${outval}/${arch_dirname}")

set(${outvar} "${outval}" PARENT_SCOPE)
endfunction ()


# Corresponds to Clang's ToolChain::getOSLibName(). Adapted from Compiler-RT.
function (get_toolchain_os_dirname outvar)
if (ANDROID)
Expand Down
108 changes: 22 additions & 86 deletions flang-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,6 @@ set(FLANG_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(FLANG_RT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../flang")

# CMake 3.24 is the first version of CMake that directly recognizes Flang.
# LLVM's requirement is only CMake 3.20, teach CMake 3.20-3.23 how to use Flang.
if (CMAKE_VERSION VERSION_LESS "3.24")
cmake_path(GET CMAKE_Fortran_COMPILER STEM _Fortran_COMPILER_STEM)
if (_Fortran_COMPILER_STEM STREQUAL "flang-new" OR _Fortran_COMPILER_STEM STREQUAL "flang")
include(CMakeForceCompiler)
CMAKE_FORCE_Fortran_COMPILER("${CMAKE_Fortran_COMPILER}" "LLVMFlang")

set(CMAKE_Fortran_COMPILER_ID "LLVMFlang")
set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")

set(CMAKE_Fortran_SUBMODULE_SEP "-")
set(CMAKE_Fortran_SUBMODULE_EXT ".mod")

set(CMAKE_Fortran_PREPROCESS_SOURCE
"<CMAKE_Fortran_COMPILER> -cpp <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")

set(CMAKE_Fortran_FORMAT_FIXED_FLAG "-ffixed-form")
set(CMAKE_Fortran_FORMAT_FREE_FLAG "-ffree-form")

set(CMAKE_Fortran_MODDIR_FLAG "-module-dir")

set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON "-cpp")
set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF "-nocpp")
set(CMAKE_Fortran_POSTPROCESS_FLAG "-ffixed-line-length-72")

set(CMAKE_Fortran_COMPILE_OPTIONS_TARGET "--target=")

set(CMAKE_Fortran_LINKER_WRAPPER_FLAG "-Wl,")
set(CMAKE_Fortran_LINKER_WRAPPER_FLAG_SEP ",")
endif ()
endif ()
enable_language(Fortran)


list(APPEND CMAKE_MODULE_PATH
"${FLANG_RT_SOURCE_DIR}/cmake/modules"
Expand All @@ -67,67 +33,21 @@ include(GetToolchainDirs)
include(FlangCommon)
include(HandleCompilerRT)
include(ExtendPath)
include(CheckFortranSourceCompiles)
include(CMakePushCheckState)


############################
# Build Mode Introspection #
############################

# Determine whether we are in the runtimes/runtimes-bins directory of a
# bootstrap build.
set(LLVM_TREE_AVAILABLE OFF)
if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
set(LLVM_TREE_AVAILABLE ON)
endif()

# Path to LLVM development tools (FileCheck, llvm-lit, not, ...)
set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")

# Determine build and install paths.
# The build path is absolute, but the install dir is relative, CMake's install
# command has to apply CMAKE_INSTALL_PREFIX itself.
get_toolchain_library_subdir(toolchain_lib_subdir)
if (LLVM_TREE_AVAILABLE)
# In a bootstrap build emit the libraries into a default search path in the
# build directory of the just-built compiler. This allows using the
# just-built compiler without specifying paths to runtime libraries.
#
# Despite Clang in the name, get_clang_resource_dir does not depend on Clang
# being added to the build. Flang uses the same resource dir as clang.
include(GetClangResourceDir)
get_clang_resource_dir(FLANG_RT_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..")
get_clang_resource_dir(FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT)

extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "${toolchain_lib_subdir}")
else ()
# In a standalone runtimes build, do not write into LLVM_BINARY_DIR. It may be
# read-only and/or shared by multiple runtimes with different build
# configurations (e.g. Debug/Release). Use the runtime's own lib dir like any
# non-toolchain library.
# For the install prefix, still use the resource dir assuming that Flang will
# be installed there using the same prefix. This is to not have a difference
# between bootstrap and standalone runtimes builds.
set(FLANG_RT_OUTPUT_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT "lib${LLVM_LIBDIR_SUFFIX}/clang/${LLVM_VERSION_MAJOR}")

extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "lib${LLVM_LIBDIR_SUFFIX}")
endif ()
set(FLANG_RT_INSTALL_RESOURCE_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT}"
CACHE PATH "Path to install runtime libraries to (default: clang resource dir)")
extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_lib_subdir}")
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR)
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
# FIXME: For the libflang_rt.so, the toolchain resource lib dir is not a good
# destination because it is not a ld.so default search path.
# The machine where the executable is eventually executed may not be the
# machine where the Flang compiler and its resource dir is installed, so
# setting RPath by the driver is not an solution. It should belong into
# /usr/lib/<triple>/libflang_rt.so, like e.g. libgcc_s.so.
# But the linker as invoked by the Flang driver also requires
# libflang_rt.so to be found when linking and the resource lib dir is
# the only reliable location.
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
# Fortran compiler not optional for building Flang-RT
enable_language(Fortran)

flang_module_fortran_enable()


#################
Expand Down Expand Up @@ -234,6 +154,22 @@ check_cxx_source_compiles(
"
HAVE_DECL_STRERROR_S)


# Look for support of REAL(16), if not already defined via command line.
# NOTE: Does not work with Flang and CMake < 3.24
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_FLAGS "-ffree-form")
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
check_fortran_source_compiles([[
subroutine test_quadmath
real(16) :: var1
end
]]
FORTRAN_SUPPORTS_REAL16
)
cmake_pop_check_state()


# Search for clang_rt.builtins library. Need in addition to msvcrt.
if (WIN32)
find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY)
Expand Down
14 changes: 10 additions & 4 deletions flang-rt/cmake/modules/AddFlangRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,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 @@ -344,13 +350,13 @@ function (add_flangrt_library name)
if (ARG_INSTALL_WITH_TOOLCHAIN)
set_target_properties(${tgtname}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_LIB_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_LIB_DIR}"
)

install(TARGETS ${tgtname}
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
LIBRARY DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
ARCHIVE DESTINATION "${RUNTIMES_INSTALL_RESOURCE_LIB_PATH}"
LIBRARY DESTINATION "${RUNTIMES_INSTALL_RESOURCE_LIB_PATH}"
)
endif ()

Expand Down
14 changes: 10 additions & 4 deletions flang-rt/cmake/modules/AddFlangRTOffload.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,29 @@ 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
Loading