Skip to content

Commit 839198d

Browse files
committed
[Flang][Flang-RT][OpenMP] Move builtin .mod generation into runtimes
1 parent 60ae9c9 commit 839198d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+512
-400
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,10 @@ class Driver {
403403
SmallString<128> &CrashDiagDir);
404404

405405
public:
406-
407406
/// Takes the path to a binary that's either in bin/ or lib/ and returns
408407
/// the path to clang's resource directory.
408+
/// Do not pass argv[0] as argument, llvm-lit does not adjust argv[0] to the
409+
/// changing cwd. Use llvm::sys::fs::getMainExecutable instead.
409410
static std::string GetResourcesPath(StringRef BinaryPath);
410411

411412
Driver(StringRef ClangExecutable, StringRef TargetTriple,

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5921,7 +5921,7 @@ def prebind : Flag<["-"], "prebind">;
59215921
def preload : Flag<["-"], "preload">;
59225922
def print_file_name_EQ : Joined<["-", "--"], "print-file-name=">,
59235923
HelpText<"Print the full library path of <file>">, MetaVarName<"<file>">,
5924-
Visibility<[ClangOption, CLOption]>;
5924+
Visibility<[ClangOption, FlangOption, CLOption]>;
59255925
def print_ivar_layout : Flag<["-"], "print-ivar-layout">,
59265926
Visibility<[ClangOption, CC1Option]>,
59275927
HelpText<"Enable Objective-C Ivar layout bitmap print trace">,

clang/include/clang/Driver/ToolChain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ class ToolChain {
542542
// Returns Triple without the OSs version.
543543
llvm::Triple getTripleWithoutOSVersion() const;
544544

545+
/// Returns the target-specific path for Flang's intrinsic modules in the
546+
/// resource directory if it exists.
547+
std::optional<std::string> getDefaultIntrinsicModuleDir() const;
548+
545549
// Returns the target specific runtime path if it exists.
546550
std::optional<std::string> getRuntimePath() const;
547551

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6536,6 +6536,16 @@ std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
65366536
if (llvm::sys::fs::exists(Twine(P)))
65376537
return std::string(P);
65386538

6539+
if (IsFlangMode()) {
6540+
if (std::optional<std::string> IntrPath =
6541+
TC.getDefaultIntrinsicModuleDir()) {
6542+
SmallString<128> P(*IntrPath);
6543+
llvm::sys::path::append(P, Name);
6544+
if (llvm::sys::fs::exists(Twine(P)))
6545+
return std::string(P);
6546+
}
6547+
}
6548+
65396549
SmallString<128> D(Dir);
65406550
llvm::sys::path::append(D, "..", Name);
65416551
if (llvm::sys::fs::exists(Twine(D)))

clang/lib/Driver/ToolChain.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
983983
return {};
984984
}
985985

986+
std::optional<std::string> ToolChain::getDefaultIntrinsicModuleDir() const {
987+
SmallString<128> P(D.ResourceDir);
988+
llvm::sys::path::append(P, "finclude");
989+
return getTargetSubDirPath(P);
990+
}
991+
986992
std::optional<std::string> ToolChain::getRuntimePath() const {
987993
SmallString<128> P(D.ResourceDir);
988994
llvm::sys::path::append(P, "lib");

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,13 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
957957
CmdArgs.push_back("-resource-dir");
958958
CmdArgs.push_back(D.ResourceDir.c_str());
959959

960+
// Default intrinsic module dirs must be added after any user-provided
961+
// -fintrinsic-modules-path to have lower precedence
962+
if (auto IntrModPath = TC.getDefaultIntrinsicModuleDir()) {
963+
CmdArgs.push_back("-fintrinsic-modules-path");
964+
CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
965+
}
966+
960967
// Offloading related options
961968
addOffloadOptions(C, Inputs, JA, Args, CmdArgs);
962969

flang-rt/cmake/modules/GetToolchainDirs.cmake renamed to cmake/Modules/GetToolchainDirs.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ function (get_toolchain_library_subdir outvar)
4747
endfunction ()
4848

4949

50+
# Corresponds to Flang's ToolChain::getDefaultIntrinsicModuleDir().
51+
function (get_toolchain_module_subdir outvar)
52+
set(outval "finclude")
53+
54+
get_toolchain_arch_dirname(arch_dirname)
55+
set(outval "${outval}/${arch_dirname}")
56+
57+
set(${outvar} "${outval}" PARENT_SCOPE)
58+
endfunction ()
59+
60+
5061
# Corresponds to Clang's ToolChain::getOSLibName(). Adapted from Compiler-RT.
5162
function (get_toolchain_os_dirname outvar)
5263
if (ANDROID)

flang-rt/CMakeLists.txt

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -67,68 +67,17 @@ include(GetToolchainDirs)
6767
include(FlangCommon)
6868
include(HandleCompilerRT)
6969
include(ExtendPath)
70+
include(CheckFortranSourceCompiles)
71+
include(CMakePushCheckState)
7072

7173

7274
############################
7375
# Build Mode Introspection #
7476
############################
7577

76-
# Determine whether we are in the runtimes/runtimes-bins directory of a
77-
# bootstrap build.
78-
set(LLVM_TREE_AVAILABLE OFF)
79-
if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
80-
set(LLVM_TREE_AVAILABLE ON)
81-
endif()
82-
8378
# Path to LLVM development tools (FileCheck, llvm-lit, not, ...)
8479
set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")
8580

86-
# Determine build and install paths.
87-
# The build path is absolute, but the install dir is relative, CMake's install
88-
# command has to apply CMAKE_INSTALL_PREFIX itself.
89-
get_toolchain_library_subdir(toolchain_lib_subdir)
90-
if (LLVM_TREE_AVAILABLE)
91-
# In a bootstrap build emit the libraries into a default search path in the
92-
# build directory of the just-built compiler. This allows using the
93-
# just-built compiler without specifying paths to runtime libraries.
94-
#
95-
# Despite Clang in the name, get_clang_resource_dir does not depend on Clang
96-
# being added to the build. Flang uses the same resource dir as clang.
97-
include(GetClangResourceDir)
98-
get_clang_resource_dir(FLANG_RT_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..")
99-
get_clang_resource_dir(FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT)
100-
101-
extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "${toolchain_lib_subdir}")
102-
else ()
103-
# In a standalone runtimes build, do not write into LLVM_BINARY_DIR. It may be
104-
# read-only and/or shared by multiple runtimes with different build
105-
# configurations (e.g. Debug/Release). Use the runtime's own lib dir like any
106-
# non-toolchain library.
107-
# For the install prefix, still use the resource dir assuming that Flang will
108-
# be installed there using the same prefix. This is to not have a difference
109-
# between bootstrap and standalone runtimes builds.
110-
set(FLANG_RT_OUTPUT_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
111-
set(FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT "lib${LLVM_LIBDIR_SUFFIX}/clang/${LLVM_VERSION_MAJOR}")
112-
113-
extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "lib${LLVM_LIBDIR_SUFFIX}")
114-
endif ()
115-
set(FLANG_RT_INSTALL_RESOURCE_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT}"
116-
CACHE PATH "Path to install runtime libraries to (default: clang resource dir)")
117-
extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_lib_subdir}")
118-
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR)
119-
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
120-
# FIXME: For the libflang_rt.so, the toolchain resource lib dir is not a good
121-
# destination because it is not a ld.so default search path.
122-
# The machine where the executable is eventually executed may not be the
123-
# machine where the Flang compiler and its resource dir is installed, so
124-
# setting RPath by the driver is not an solution. It should belong into
125-
# /usr/lib/<triple>/libflang_rt.so, like e.g. libgcc_s.so.
126-
# But the linker as invoked by the Flang driver also requires
127-
# libflang_rt.so to be found when linking and the resource lib dir is
128-
# the only reliable location.
129-
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
130-
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
131-
13281

13382
#################
13483
# Build Options #
@@ -243,6 +192,22 @@ check_cxx_source_compiles(
243192
"
244193
HAVE_DECL_STRERROR_S)
245194

195+
include(CheckFortranSourceCompiles)
196+
include(CMakePushCheckState)
197+
cmake_push_check_state(RESET)
198+
set(CMAKE_REQUIRED_FLAGS "-ffree-form")
199+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
200+
check_fortran_source_compiles([[
201+
subroutine test_quadmath
202+
real(16) :: var1
203+
end
204+
]]
205+
FORTRAN_SUPPORTS_REAL16
206+
)
207+
cmake_pop_check_state()
208+
209+
flang_module_fortran_enable()
210+
246211
# Search for clang_rt.builtins library. Need in addition to msvcrt.
247212
if (WIN32)
248213
find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY)
@@ -338,4 +303,4 @@ if (FLANG_RT_INCLUDE_TESTS)
338303
add_subdirectory(unittests)
339304
else ()
340305
add_custom_target(check-flang-rt)
341-
endif()
306+
endif()

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ function (add_flangrt_library name)
190190
endif ()
191191
endif ()
192192

193+
if (build_object)
194+
add_library(${name}.compile ALIAS "${name_object}")
195+
else ()
196+
add_library(${name}.compile ALIAS "${default_target}")
197+
endif ()
198+
193199
foreach (tgtname IN LISTS libtargets)
194200
if (NOT WIN32)
195201
# Use same stem name for .a and .so. Common in UNIX environments.
@@ -334,13 +340,13 @@ function (add_flangrt_library name)
334340
if (ARG_INSTALL_WITH_TOOLCHAIN)
335341
set_target_properties(${tgtname}
336342
PROPERTIES
337-
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
338-
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
343+
ARCHIVE_OUTPUT_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_LIB_DIR}"
344+
LIBRARY_OUTPUT_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_LIB_DIR}"
339345
)
340346

341347
install(TARGETS ${tgtname}
342-
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
343-
LIBRARY DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
348+
ARCHIVE DESTINATION "${RUNTIMES_INSTALL_RESOURCE_LIB_PATH}"
349+
LIBRARY DESTINATION "${RUNTIMES_INSTALL_RESOURCE_LIB_PATH}"
344350
)
345351
endif ()
346352

flang-rt/cmake/modules/AddFlangRTOffload.cmake

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,29 @@ macro(enable_omp_offload_compilation name files)
8888
"${FLANG_RT_DEVICE_ARCHITECTURES}"
8989
)
9090

91-
set(OMP_COMPILE_OPTIONS
91+
set(OMP_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:C,CXX>:
9292
-fopenmp
9393
-fvisibility=hidden
9494
-fopenmp-cuda-mode
9595
--offload-arch=${compile_for_architectures}
9696
# Force LTO for the device part.
9797
-foffload-lto
98-
)
99-
set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS
100-
"${OMP_COMPILE_OPTIONS}"
98+
>)
99+
set_property(SOURCE ${files} APPEND
100+
PROPERTY COMPILE_DEFINITIONS ${OMP_COMPILE_OPTIONS}
101101
)
102102
target_link_options(${name}.static PUBLIC ${OMP_COMPILE_OPTIONS})
103103

104104
# Enable "declare target" in the source code.
105105
set_source_files_properties(${files}
106106
PROPERTIES COMPILE_DEFINITIONS OMP_OFFLOAD_BUILD
107107
)
108+
109+
# If building flang-rt together with libomp, ensure that libomp is built first and found because -fopenmp will try to link it.
110+
if (TARGET omp)
111+
add_dependencies(${name} omp)
112+
target_link_options(${name}.static PUBLIC "-L$<TARGET_FILE_DIR:omp>")
113+
endif ()
108114
else()
109115
message(FATAL_ERROR
110116
"Flang-rt build with OpenMP offload is not supported for these compilers:\n"

0 commit comments

Comments
 (0)