Skip to content

[Flang][OpenMP] Move builtin .mod generation into runtimes #137828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
10 changes: 10 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4032,6 +4032,16 @@ def fsyntax_only : Flag<["-"], "fsyntax-only">,
Visibility<[ClangOption, CLOption, DXCOption, CC1Option, FC1Option, FlangOption]>,
Group<Action_Group>,
HelpText<"Run the preprocessor, parser and semantic analysis stages">;


def fno_builtin_modules : Flag<["-"], "fno-builtin-modules">,
Visibility<[FC1Option]>,
HelpText<"Do not implicitly use builtin modules (for internal use only)">;
Comment on lines +4037 to +4039
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be implied by the user specifying it below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-fintrinsic-modules-path is defined by gfortran, but flang's interpretation is incompatible (Even though they copy-pasted gfortran's help description). -fbuiltin-modules-path= is the temporary replacement. At the end, I hope I will not need either of them, by fixing -fintrinsic-modules-path.

def fbuiltin_modules_path : Joined<["--"], "fbuiltin-modules-path=">,
Visibility<[FlangOption, FC1Option]>,
HelpText<"Specify where Flang finds its builtin modules">;


def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group<f_Group>;
def ftemplate_depth_EQ : Joined<["-"], "ftemplate-depth=">, Group<f_Group>,
Visibility<[ClangOption, CC1Option]>,
Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ class ToolChain {
/// The list of toolchain specific path prefixes to search for programs.
path_list ProgramPaths;

path_list ModulePaths;
path_list IntrinsicModulePaths;
Comment on lines +160 to +161
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format.


mutable std::unique_ptr<Tool> Clang;
mutable std::unique_ptr<Tool> Flang;
mutable std::unique_ptr<Tool> Assemble;
Expand Down Expand Up @@ -297,6 +300,12 @@ class ToolChain {
path_list &getProgramPaths() { return ProgramPaths; }
const path_list &getProgramPaths() const { return ProgramPaths; }

path_list &getModulePaths() { return ModulePaths; }
const path_list &getModulePaths() const { return ModulePaths; }

path_list &getIntrinsicModulePaths() { return IntrinsicModulePaths; }
const path_list &getIntrinsicModulePaths() const { return IntrinsicModulePaths; }

const MultilibSet &getMultilibs() const { return Multilibs; }

const llvm::SmallVector<Multilib> &getSelectedMultilibs() const {
Expand Down Expand Up @@ -546,6 +555,10 @@ class ToolChain {
// Returns target specific standard library include path if it exists.
std::optional<std::string> getStdlibIncludePath() const;


path_list getDefaultIntrinsicModulePaths() const;


// Returns <ResourceDir>/lib/<OSName>/<arch> or <ResourceDir>/lib/<triple>.
// This is used by runtimes (such as OpenMP) to find arch-specific libraries.
virtual path_list getArchSpecificLibPaths() const;
Expand Down
22 changes: 22 additions & 0 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
getFilePaths().push_back(*Path);
for (const auto &Path : getArchSpecificLibPaths())
addIfExists(getFilePaths(), Path);

if (D.IsFlangMode()) {
getIntrinsicModulePaths().append(getDefaultIntrinsicModulePaths());
}
}

llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
Expand Down Expand Up @@ -1004,6 +1008,24 @@ std::optional<std::string> ToolChain::getStdlibIncludePath() const {
return getTargetSubDirPath(P);
}

ToolChain:: path_list ToolChain:: getDefaultIntrinsicModulePaths() const{
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "finclude");

// TODO: If there are multiple valid names for the target triple, prefer to add all of them instead probing which are existing.
ToolChain:: path_list Result;
if (std::optional<std::string> PerTargetPath = getTargetSubDirPath(P))
Result.push_back(*PerTargetPath);

// flang used this in the past, keep for compatibility
SmallString<128> CompatIntrModPath(D.Dir);
llvm::sys::path::append(CompatIntrModPath, "..", "include", "flang");
Result.push_back(std::string(CompatIntrModPath));

return Result;
}


ToolChain::path_list ToolChain::getArchSpecificLibPaths() const {
path_list Paths;

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 @@ -922,6 +922,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
for (auto && IntrModPath : TC.getIntrinsicModulePaths()) {
CmdArgs.push_back("-fintrinsic-modules-path");
CmdArgs.push_back(IntrModPath.c_str());
}



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

Expand Down
16 changes: 16 additions & 0 deletions flang-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")
# 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)
get_toolchain_module_subdir(toolchain_mod_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
Expand All @@ -99,6 +100,7 @@ if (LLVM_TREE_AVAILABLE)
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}")
extend_path(FLANG_RT_OUTPUT_RESOURCE_MOD_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "${toolchain_mod_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
Expand All @@ -111,10 +113,12 @@ else ()
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}")
extend_path(FLANG_RT_OUTPUT_RESOURCE_MOD_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "finclude")
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}")
extend_path(FLANG_RT_INSTALL_RESOURCE_MOD_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_mod_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
Expand All @@ -128,6 +132,13 @@ cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
# the only reliable location.
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_MOD_DIR)
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_MOD_PATH)

message("toolchain_mod_subdir: ${toolchain_mod_subdir}")
message("FLANG_RT_OUTPUT_RESOURCE_MOD_DIR: ${FLANG_RT_OUTPUT_RESOURCE_MOD_DIR}")
message("FLANG_RT_INSTALL_RESOURCE_MOD_PATH: ${FLANG_RT_INSTALL_RESOURCE_MOD_PATH}")



#################
Expand Down Expand Up @@ -290,6 +301,11 @@ set(HAVE_BACKTRACE ${Backtrace_FOUND})
set(BACKTRACE_HEADER ${Backtrace_HEADER})







#####################
# Build Preparation #
#####################
Expand Down
20 changes: 16 additions & 4 deletions flang-rt/cmake/modules/AddFlangRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ function (add_flangrt_library name)
# Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS
# build, to avoid an unwanted dependency on libstdc++/libc++.so.
if (FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
target_compile_options(${tgtname} PUBLIC -U_GLIBCXX_ASSERTIONS)
target_compile_options(${tgtname} PUBLIC -U_LIBCPP_ENABLE_ASSERTIONS)
target_compile_options(${tgtname} PUBLIC $<$<COMPILE_LANGUAGE:C,CXX>:-U_GLIBCXX_ASSERTIONS>)
target_compile_options(${tgtname} PUBLIC $<$<COMPILE_LANGUAGE:C,CXX>:-U_LIBCPP_ENABLE_ASSERTIONS>)
endif ()

# Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
Expand All @@ -281,12 +281,12 @@ function (add_flangrt_library name)
# dependency to Compiler-RT's builtin library where these are implemented.
if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if (FLANG_RT_BUILTINS_LIBRARY)
target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
#target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
endif ()
endif ()
if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
if (FLANG_RT_BUILTINS_LIBRARY)
target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
#target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
else ()
message(WARNING "Did not find libclang_rt.builtins.lib.
LLVM may emit builtins that are not implemented in msvcrt/ucrt and
Expand Down Expand Up @@ -315,6 +315,15 @@ function (add_flangrt_library name)
endif ()
endforeach ()

foreach (tgtname IN LISTS srctargets)
set_target_properties(${tgtname}
PROPERTIES
Fortran_MODULE_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_MOD_DIR}"
)
endforeach ()



foreach (tgtname IN LISTS libtargets)
# If this is part of the toolchain, put it into the compiler's resource
# directory. Otherwise it is part of testing and is not installed at all.
Expand All @@ -324,15 +333,18 @@ function (add_flangrt_library name)
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
#Fortran_MODULE_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_MOD_DIR}"
)

install(TARGETS ${tgtname}
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
LIBRARY DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
# Fortran ???
)
endif ()

if (ARG_TARGET_PROPERTIES)
message("Setting target properties of ${tgtname}: ${ARG_TARGET_PROPERTIES}" )
set_target_properties(${tgtname} PROPERTIES ${ARG_TARGET_PROPERTIES})
endif ()

Expand Down
10 changes: 10 additions & 0 deletions flang-rt/cmake/modules/GetToolchainDirs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ function (get_toolchain_library_subdir outvar)
endfunction ()


function (get_toolchain_module_subdir outvar)
set(outval "finclude")

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
Loading