Skip to content

Commit ea7f2a4

Browse files
committed
Revert "[Flang] Move builtin .mod generation into runtimes (Reapply llvm#137828) (llvm#169638)"
needs more work This reverts commit 7675fc7.
1 parent 7085552 commit ea7f2a4

File tree

71 files changed

+514
-881
lines changed

Some content is hidden

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

71 files changed

+514
-881
lines changed

clang/include/clang/Driver/ToolChain.h

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

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

clang/include/clang/Options/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6257,7 +6257,7 @@ def prebind : Flag<["-"], "prebind">;
62576257
def preload : Flag<["-"], "preload">;
62586258
def print_file_name_EQ : Joined<["-", "--"], "print-file-name=">,
62596259
HelpText<"Print the full library path of <file>">, MetaVarName<"<file>">,
6260-
Visibility<[ClangOption, FlangOption, CLOption]>;
6260+
Visibility<[ClangOption, CLOption]>;
62616261
def print_ivar_layout : Flag<["-"], "print-ivar-layout">,
62626262
Visibility<[ClangOption, CC1Option]>,
62636263
HelpText<"Enable Objective-C Ivar layout bitmap print trace">,

clang/lib/Driver/Driver.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6655,17 +6655,6 @@ std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
66556655
if (llvm::sys::fs::exists(Twine(P)))
66566656
return std::string(P);
66576657

6658-
// With Flang, also look for instrinsic modules
6659-
if (IsFlangMode()) {
6660-
if (std::optional<std::string> IntrPath =
6661-
TC.getDefaultIntrinsicModuleDir()) {
6662-
SmallString<128> P(*IntrPath);
6663-
llvm::sys::path::append(P, Name);
6664-
if (llvm::sys::fs::exists(Twine(P)))
6665-
return std::string(P);
6666-
}
6667-
}
6668-
66696658
SmallString<128> D(Dir);
66706659
llvm::sys::path::append(D, "..", Name);
66716660
if (llvm::sys::fs::exists(Twine(D)))

clang/lib/Driver/ToolChain.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,12 +1020,6 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
10201020
return {};
10211021
}
10221022

1023-
std::optional<std::string> ToolChain::getDefaultIntrinsicModuleDir() const {
1024-
SmallString<128> P(D.ResourceDir);
1025-
llvm::sys::path::append(P, "finclude", "flang");
1026-
return getTargetSubDirPath(P);
1027-
}
1028-
10291023
std::optional<std::string> ToolChain::getRuntimePath() const {
10301024
SmallString<128> P(D.ResourceDir);
10311025
llvm::sys::path::append(P, "lib");

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,7 @@ void AMDGPUToolChain::addClangTargetOptions(
891891
// Default to "hidden" visibility, as object level linking will not be
892892
// supported for the foreseeable future.
893893
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
894-
options::OPT_fvisibility_ms_compat) &&
895-
!getDriver().IsFlangMode()) {
894+
options::OPT_fvisibility_ms_compat)) {
896895
CC1Args.push_back("-fvisibility=hidden");
897896
CC1Args.push_back("-fapply-global-visibility-to-externs");
898897
}

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,14 +1086,6 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
10861086
CmdArgs.push_back("-resource-dir");
10871087
CmdArgs.push_back(D.ResourceDir.c_str());
10881088

1089-
// Default intrinsic module dirs must be added after any user-provided
1090-
// -fintrinsic-modules-path to have lower precedence
1091-
if (std::optional<std::string> IntrModPath =
1092-
TC.getDefaultIntrinsicModuleDir()) {
1093-
CmdArgs.push_back("-fintrinsic-modules-path");
1094-
CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
1095-
}
1096-
10971089
// Offloading related options
10981090
addOffloadOptions(C, Inputs, JA, Args, CmdArgs);
10991091

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ void HIPAMDToolChain::addClangTargetOptions(
288288
// Default to "hidden" visibility, as object level linking will not be
289289
// supported for the foreseeable future.
290290
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
291-
options::OPT_fvisibility_ms_compat) &&
292-
!getDriver().IsFlangMode()) {
291+
options::OPT_fvisibility_ms_compat)) {
293292
CC1Args.append({"-fvisibility=hidden"});
294293
CC1Args.push_back("-fapply-global-visibility-to-externs");
295294
}

clang/lib/Driver/ToolChains/HIPSPV.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ void HIPSPVToolChain::addClangTargetOptions(
145145
// Default to "hidden" visibility, as object level linking will not be
146146
// supported for the foreseeable future.
147147
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
148-
options::OPT_fvisibility_ms_compat) &&
149-
!getDriver().IsFlangMode())
148+
options::OPT_fvisibility_ms_compat))
150149
CC1Args.append(
151150
{"-fvisibility=hidden", "-fapply-global-visibility-to-externs"});
152151

flang-rt/CMakeLists.txt

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@ set(FLANG_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
2323
set(FLANG_RT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
2424
set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../flang")
2525

26+
# CMake 3.24 is the first version of CMake that directly recognizes Flang.
27+
# LLVM's requirement is only CMake 3.20, teach CMake 3.20-3.23 how to use Flang.
28+
if (CMAKE_VERSION VERSION_LESS "3.24")
29+
cmake_path(GET CMAKE_Fortran_COMPILER STEM _Fortran_COMPILER_STEM)
30+
if (_Fortran_COMPILER_STEM STREQUAL "flang")
31+
include(CMakeForceCompiler)
32+
CMAKE_FORCE_Fortran_COMPILER("${CMAKE_Fortran_COMPILER}" "LLVMFlang")
33+
34+
set(CMAKE_Fortran_COMPILER_ID "LLVMFlang")
35+
set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
36+
37+
set(CMAKE_Fortran_SUBMODULE_SEP "-")
38+
set(CMAKE_Fortran_SUBMODULE_EXT ".mod")
39+
40+
set(CMAKE_Fortran_PREPROCESS_SOURCE
41+
"<CMAKE_Fortran_COMPILER> -cpp <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
42+
43+
set(CMAKE_Fortran_FORMAT_FIXED_FLAG "-ffixed-form")
44+
set(CMAKE_Fortran_FORMAT_FREE_FLAG "-ffree-form")
45+
46+
set(CMAKE_Fortran_MODDIR_FLAG "-module-dir")
47+
48+
set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON "-cpp")
49+
set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF "-nocpp")
50+
set(CMAKE_Fortran_POSTPROCESS_FLAG "-ffixed-line-length-72")
51+
52+
set(CMAKE_Fortran_COMPILE_OPTIONS_TARGET "--target=")
53+
54+
set(CMAKE_Fortran_LINKER_WRAPPER_FLAG "-Wl,")
55+
set(CMAKE_Fortran_LINKER_WRAPPER_FLAG_SEP ",")
56+
endif ()
57+
endif ()
58+
enable_language(Fortran)
59+
2660

2761
list(APPEND CMAKE_MODULE_PATH
2862
"${FLANG_RT_SOURCE_DIR}/cmake/modules"
@@ -31,22 +65,69 @@ list(APPEND CMAKE_MODULE_PATH
3165
include(AddFlangRT)
3266
include(GetToolchainDirs)
3367
include(FlangCommon)
34-
include(FlangRTIntrospection)
3568
include(HandleCompilerRT)
3669
include(ExtendPath)
37-
include(CheckFortranSourceCompiles)
38-
include(CMakePushCheckState)
3970

4071

4172
############################
4273
# Build Mode Introspection #
4374
############################
4475

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+
4583
# Path to LLVM development tools (FileCheck, llvm-lit, not, ...)
4684
set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")
4785

48-
# Fortran compiler not optional for building Flang-RT
49-
enable_language(Fortran)
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)
50131

51132

52133
#################
@@ -56,6 +137,8 @@ enable_language(Fortran)
56137
# Important: flang-rt user options must be prefixed with "FLANG_RT_". Variables
57138
# with this prefix will be forwarded in bootstrap builds.
58139

140+
option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")
141+
59142
# Provide an interface to link against the LLVM libc/libc++ projects directly.
60143
set(FLANG_RT_SUPPORTED_PROVIDERS system llvm)
61144
set(FLANG_RT_LIBC_PROVIDER "system" CACHE STRING "Specify C library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
@@ -68,14 +151,7 @@ if (NOT "${FLANG_RT_LIBCXX_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
68151
message(FATAL_ERROR "Unsupported library: '${FLANG_RT_LIBCXX_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
69152
endif ()
70153

71-
if (LLVM_RUNTIMES_TARGET MATCHES "^amdgcn|^nvptx")
72-
# Compiling libraries for offload targets is currently experimental;
73-
# Only build the builtin modules by default.
74-
set(FLANG_RT_ENABLE_STATIC_default OFF)
75-
else ()
76-
set(FLANG_RT_ENABLE_STATIC_default ON)
77-
endif ()
78-
option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." "${FLANG_RT_ENABLE_STATIC_default}")
154+
option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." ON)
79155
if (WIN32)
80156
# Windows DLL currently not implemented.
81157
set(FLANG_RT_ENABLE_SHARED OFF)
@@ -88,14 +164,11 @@ else ()
88164
# breaking change unless the driver is changed.
89165
option(FLANG_RT_ENABLE_SHARED "Build Flang-RT as a shared library." OFF)
90166
endif ()
91-
92-
93-
if (FLANG_RT_ENABLE_STATIC OR FLANG_RT_ENABLE_SHARED)
94-
option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")
95-
else ()
96-
# Tests require at least one of the libraries
97-
message(STATUS "Flang-RT testing disabled without either FLANG_RT_ENABLE_STATIC OR FLANG_RT_ENABLE_SHARED")
98-
set(FLANG_RT_INCLUDE_TESTS OFF)
167+
if (NOT FLANG_RT_ENABLE_STATIC AND NOT FLANG_RT_ENABLE_SHARED)
168+
message(FATAL_ERROR "
169+
Must build at least one type of library
170+
(FLANG_RT_ENABLE_STATIC=ON, FLANG_RT_ENABLE_SHARED=ON, or both)
171+
")
99172
endif ()
100173

101174

@@ -113,7 +186,7 @@ elseif (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")
113186
option(FLANG_RT_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS "Do not compile global variables' definitions when producing PTX library" OFF)
114187
elseif (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "OpenMP")
115188
# Support for OpenMP offloading
116-
set(FLANG_RT_DEVICE_ARCHITECTURES "${RUNTIMES_DEVICE_ARCHITECTURES}" CACHE STRING
189+
set(FLANG_RT_DEVICE_ARCHITECTURES "all" CACHE STRING
117190
"List of OpenMP device architectures to be used to compile the Fortran runtime (e.g. 'gfx1103;sm_90')"
118191
)
119192

@@ -162,10 +235,6 @@ check_cxx_source_compiles(
162235
"
163236
HAVE_DECL_STRERROR_S)
164237

165-
# Look for support of REAL(16), if not already defined via command
166-
# line via -DFORTRAN_SUPPORTS_REAL16=YES/NO
167-
check_fortran_quadmath_support()
168-
169238
# Search for clang_rt.builtins library. Need in addition to msvcrt.
170239
if (WIN32)
171240
find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY)
@@ -263,7 +332,6 @@ else ()
263332
add_custom_target(check-flang-rt)
264333
endif()
265334

266-
267335
###################
268336
# Install headers #
269337
###################

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ function (add_flangrt_library name)
9494
set(build_object ON)
9595
elseif (build_static AND build_shared)
9696
set(build_object ON)
97-
elseif (NOT build_static AND NOT build_shared)
98-
# If not building a library, still build the object files
99-
# Needed to generate the .mod files as byproduct
100-
set(build_object ON)
10197
endif ()
10298

10399
# srctargets: targets that contain source files
@@ -172,18 +168,14 @@ function (add_flangrt_library name)
172168
if (BUILD_SHARED_LIBS)
173169
if (build_shared)
174170
set(default_target "${name_shared}")
175-
elseif (build_static)
176-
set(default_target "${name_static}")
177171
else ()
178-
set(default_target "${name_object}")
172+
set(default_target "${name_static}")
179173
endif ()
180174
else ()
181175
if (build_static)
182176
set(default_target "${name_static}")
183-
elseif (build_shared)
184-
set(default_target "${name_shared}")
185177
else ()
186-
set(default_target "${name_object}")
178+
set(default_target "${name_shared}")
187179
endif ()
188180
endif ()
189181
add_library(${name}.default ALIAS "${default_target}")
@@ -198,12 +190,6 @@ function (add_flangrt_library name)
198190
endif ()
199191
endif ()
200192

201-
if (build_object)
202-
add_library(${name}.compile ALIAS "${name_object}")
203-
else ()
204-
add_library(${name}.compile ALIAS "${default_target}")
205-
endif ()
206-
207193
foreach (tgtname IN LISTS libtargets)
208194
if (NOT WIN32)
209195
# Use same stem name for .a and .so. Common in UNIX environments.
@@ -233,25 +219,13 @@ function (add_flangrt_library name)
233219
# Minimum required C++ version for Flang-RT, even if CMAKE_CXX_STANDARD is defined to something else.
234220
target_compile_features(${tgtname} PRIVATE cxx_std_17)
235221

236-
target_compile_options(${tgtname} PRIVATE
237-
# Always enable preprocessor regardless of file extension
238-
"$<$<COMPILE_LANGUAGE:Fortran>:-cpp>"
239-
240-
# Missing type descriptors are expected for intrinsic modules
241-
"$<$<COMPILE_LANGUAGE:Fortran>:SHELL:-mmlir;SHELL:-ignore-missing-type-desc>"
242-
)
243-
244222
# When building the flang runtime if LTO is enabled the archive file
245223
# contains LLVM IR rather than object code. Currently flang is not
246224
# LTO aware so cannot link this file to compiled Fortran code.
247225
if (FLANG_RT_HAS_FNO_LTO_FLAG)
248226
target_compile_options(${tgtname} PRIVATE -fno-lto)
249227
endif ()
250228

251-
if (FORTRAN_SUPPORTS_REAL16)
252-
target_compile_definitions(${tgtname} PRIVATE FLANG_SUPPORT_R16=1)
253-
endif ()
254-
255229
# Use compiler-specific options to disable exceptions and RTTI.
256230
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
257231
target_compile_options(${tgtname} PRIVATE
@@ -370,13 +344,13 @@ function (add_flangrt_library name)
370344
if (ARG_INSTALL_WITH_TOOLCHAIN)
371345
set_target_properties(${tgtname}
372346
PROPERTIES
373-
ARCHIVE_OUTPUT_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_LIB_DIR}"
374-
LIBRARY_OUTPUT_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_LIB_DIR}"
347+
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
348+
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
375349
)
376350

377351
install(TARGETS ${tgtname}
378-
ARCHIVE DESTINATION "${RUNTIMES_INSTALL_RESOURCE_LIB_PATH}"
379-
LIBRARY DESTINATION "${RUNTIMES_INSTALL_RESOURCE_LIB_PATH}"
352+
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
353+
LIBRARY DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
380354
)
381355
endif ()
382356

0 commit comments

Comments
 (0)