Skip to content

Commit 6fcf97d

Browse files
committed
-fmodule-intrinsic-path passed by driver
1 parent f0a07d9 commit 6fcf97d

File tree

19 files changed

+280
-109
lines changed

19 files changed

+280
-109
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ class Driver {
188188
/// The path to the compiler resource directory.
189189
std::string ResourceDir;
190190

191+
192+
191193
/// System directory for config files.
192194
std::string SystemConfigDir;
193195

@@ -406,6 +408,7 @@ class Driver {
406408

407409
/// Takes the path to a binary that's either in bin/ or lib/ and returns
408410
/// the path to clang's resource directory.
411+
/// Do not pass argv[0] as argument, llvm-lit does not adjust argv[0] to the changing cwd. Use llvm::sys::fs::getMainExecutable instead.
409412
static std::string GetResourcesPath(StringRef BinaryPath);
410413

411414
Driver(StringRef ClangExecutable, StringRef TargetTriple,

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,16 +4096,6 @@ def fsyntax_only : Flag<["-"], "fsyntax-only">,
40964096
Visibility<[ClangOption, CLOption, DXCOption, CC1Option, FC1Option, FlangOption]>,
40974097
Group<Action_Group>,
40984098
HelpText<"Run the preprocessor, parser and semantic analysis stages">;
4099-
4100-
4101-
def fno_builtin_modules : Flag<["-"], "fno-builtin-modules">,
4102-
Visibility<[FC1Option]>,
4103-
HelpText<"Do not implicitly use builtin modules (for internal use only)">;
4104-
def fbuiltin_modules_path : Joined<["--"], "fbuiltin-modules-path=">,
4105-
Visibility<[FlangOption, FC1Option]>,
4106-
HelpText<"Specify where Flang finds its builtin modules">;
4107-
4108-
41094099
def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group<f_Group>;
41104100
def ftemplate_depth_EQ : Joined<["-"], "ftemplate-depth=">, Group<f_Group>,
41114101
Visibility<[ClangOption, CC1Option]>,
@@ -5916,7 +5906,7 @@ def prebind : Flag<["-"], "prebind">;
59165906
def preload : Flag<["-"], "preload">;
59175907
def print_file_name_EQ : Joined<["-", "--"], "print-file-name=">,
59185908
HelpText<"Print the full library path of <file>">, MetaVarName<"<file>">,
5919-
Visibility<[ClangOption, CLOption]>;
5909+
Visibility<[ClangOption, FlangOption, CLOption]>;
59205910
def print_ivar_layout : Flag<["-"], "print-ivar-layout">,
59215911
Visibility<[ClangOption, CC1Option]>,
59225912
HelpText<"Enable Objective-C Ivar layout bitmap print trace">,
@@ -7097,14 +7087,11 @@ def cpp : Flag<["-"], "cpp">, Group<f_Group>,
70977087
HelpText<"Enable predefined and command line preprocessor macros">;
70987088
def nocpp : Flag<["-"], "nocpp">, Group<f_Group>,
70997089
HelpText<"Disable predefined and command line preprocessor macros">;
7100-
7101-
// TODO: separate
71027090
def module_dir : JoinedOrSeparate<["-"], "module-dir">, MetaVarName<"<dir>">,
71037091
HelpText<"Put MODULE files in <dir>">,
71047092
DocBrief<[{This option specifies where to put .mod files for compiled modules.
71057093
It is also added to the list of directories to be searched by an USE statement.
71067094
The default is the current directory.}]>;
7107-
def module_dir_EQ : Joined<["-"], "module-dir=">, Alias<module_dir>;
71087095

71097096
def ffixed_form : Flag<["-"], "ffixed-form">, Group<f_Group>,
71107097
HelpText<"Process source files in fixed form">;
@@ -7136,13 +7123,10 @@ def flarge_sizes : Flag<["-"],"flarge-sizes">, Group<f_Group>,
71367123

71377124
def falternative_parameter_statement : Flag<["-"], "falternative-parameter-statement">, Group<f_Group>,
71387125
HelpText<"Enable the old style PARAMETER statement">;
7139-
def fintrinsic_modules_path : Separate<["-"], "fintrinsic-modules-path">, Group<f_Group>, MetaVarName<"<dir>">, Visibility<[FlangOption, FC1Option]>,
7126+
def fintrinsic_modules_path : Separate<["-"], "fintrinsic-modules-path">, Group<f_Group>, MetaVarName<"<dir>">,
71407127
HelpText<"Specify where to find the compiled intrinsic modules">,
71417128
DocBrief<[{This option specifies the location of pre-compiled intrinsic modules,
71427129
if they are not in the default location expected by the compiler.}]>;
7143-
def fintrinsic_modules_path_EQ : Joined<["-"], "fintrinsic-modules-path=">, Group<f_Group>, Visibility<[FlangOption, FC1Option]>,
7144-
Alias<fintrinsic_modules_path>;
7145-
71467130

71477131
defm backslash : OptInFC1FFlag<"backslash", "Specify that backslash in string introduces an escape character">;
71487132
defm xor_operator : OptInFC1FFlag<"xor-operator", "Enable .XOR. as a synonym of .NEQV.">;
@@ -7244,8 +7228,7 @@ def fdo_concurrent_to_openmp_EQ : Joined<["-"], "fdo-concurrent-to-openmp=">,
72447228
def J : JoinedOrSeparate<["-"], "J">,
72457229
Flags<[RenderJoined]>, Visibility<[FlangOption, FC1Option]>,
72467230
Group<gfortran_Group>,
7247-
Alias<module_dir> // TODO: remove, searchDirectoriesFromDashJ vs searchDirectoriesFromIntrModPath
7248-
;
7231+
Alias<module_dir>;
72497232

72507233
//===----------------------------------------------------------------------===//
72517234
// FC1 Options

clang/include/clang/Driver/ToolChain.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class FileSystem;
5151
namespace clang {
5252

5353
class ObjCRuntime;
54+
class DiagnosticsEngine;
5455

5556
namespace driver {
5657

@@ -157,8 +158,9 @@ class ToolChain {
157158
/// The list of toolchain specific path prefixes to search for programs.
158159
path_list ProgramPaths;
159160

160-
path_list ModulePaths;
161-
path_list IntrinsicModulePaths;
161+
#if 0
162+
path_list IntrinsicModulePaths;
163+
#endif
162164

163165
mutable std::unique_ptr<Tool> Clang;
164166
mutable std::unique_ptr<Tool> Flang;
@@ -305,11 +307,11 @@ class ToolChain {
305307
path_list &getProgramPaths() { return ProgramPaths; }
306308
const path_list &getProgramPaths() const { return ProgramPaths; }
307309

308-
path_list &getModulePaths() { return ModulePaths; }
309-
const path_list &getModulePaths() const { return ModulePaths; }
310-
311-
path_list &getIntrinsicModulePaths() { return IntrinsicModulePaths; }
310+
#if 0
311+
path_list &getIntrinsicModulePaths() { return IntrinsicModulePaths; }
312312
const path_list &getIntrinsicModulePaths() const { return IntrinsicModulePaths; }
313+
#endif
314+
313315

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

@@ -549,7 +551,10 @@ class ToolChain {
549551
FileType Type = ToolChain::FT_Static) const;
550552

551553
// Returns Triple without the OSs version.
552-
llvm::Triple getTripleWithoutOSVersion() const;
554+
static llvm::Triple getTripleWithoutOSVersion(const llvm::Triple &T) ;
555+
556+
static std::optional<std::string> getDefaultIntrinsicModuleDir(StringRef resourceDir, const llvm::Triple &T ,llvm::vfs::FileSystem &VFS, clang::DiagnosticsEngine *Diags );
557+
std::optional<std::string> getDefaultIntrinsicModuleDir( ) const;
553558

554559
// Returns the target specific runtime path if it exists.
555560
std::optional<std::string> getRuntimePath() const;
@@ -560,9 +565,6 @@ class ToolChain {
560565
// Returns target specific standard library include path if it exists.
561566
std::optional<std::string> getStdlibIncludePath() const;
562567

563-
// Returns target specific module search paths.
564-
path_list getDefaultIntrinsicModulePaths() const;
565-
566568
// Returns <ResourceDir>/lib/<OSName>/<arch> or <ResourceDir>/lib/<triple>.
567569
// This is used by runtimes (such as OpenMP) to find arch-specific libraries.
568570
virtual path_list getArchSpecificLibPaths() const;

clang/lib/Driver/Driver.cpp

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

6540+
if (IsFlangMode()) {
6541+
if (auto IntrPath = 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+
65406549
SmallString<128> D(Dir);
65416550
llvm::sys::path::append(D, "..", Name);
65426551
if (llvm::sys::fs::exists(Twine(D)))

clang/lib/Driver/ToolChain.cpp

Lines changed: 130 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
103103
for (const auto &Path : getArchSpecificLibPaths())
104104
addIfExists(getFilePaths(), Path);
105105

106-
#if 0
107-
if (D.IsFlangMode()) {
108-
getIntrinsicModulePaths().append(getDefaultIntrinsicModulePaths());
109-
}
110-
#endif
106+
#if 0
107+
if (std::optional<std::string> Path = getDefaultIntrinsicModuleDir())
108+
getIntrinsicModulePaths().push_back(*Path);
109+
#endif
111110
}
112111

113112

113+
114114
llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
115115
ToolChain::executeToolChainProgram(StringRef Executable) const {
116116
llvm::SmallString<64> OutputFile;
@@ -874,8 +874,52 @@ void ToolChain::addFlangRTLibPath(const ArgList &Args,
874874
// Android target triples contain a target version. If we don't have libraries
875875
// for the exact target version, we should fall back to the next newest version
876876
// or a versionless path, if any.
877+
static
878+
std::optional<std::string>
879+
getFallbackAndroidTargetPath(StringRef BaseDir, const llvm::Triple &T, llvm::vfs::FileSystem &VFS, DiagnosticsEngine *Diags) {
880+
llvm::Triple TripleWithoutLevel(T);
881+
TripleWithoutLevel.setEnvironmentName("android"); // remove any version number
882+
const std::string &TripleWithoutLevelStr = TripleWithoutLevel.str();
883+
unsigned TripleVersion = T.getEnvironmentVersion().getMajor();
884+
unsigned BestVersion = 0;
885+
886+
SmallString<32> TripleDir;
887+
bool UsingUnversionedDir = false;
888+
std::error_code EC;
889+
for (llvm::vfs::directory_iterator LI = VFS.dir_begin(BaseDir, EC), LE;
890+
!EC && LI != LE; LI = LI.increment(EC)) {
891+
StringRef DirName = llvm::sys::path::filename(LI->path());
892+
StringRef DirNameSuffix = DirName;
893+
if (DirNameSuffix.consume_front(TripleWithoutLevelStr)) {
894+
if (DirNameSuffix.empty() && TripleDir.empty()) {
895+
TripleDir = DirName;
896+
UsingUnversionedDir = true;
897+
} else {
898+
unsigned Version;
899+
if (!DirNameSuffix.getAsInteger(10, Version) && Version > BestVersion &&
900+
Version < TripleVersion) {
901+
BestVersion = Version;
902+
TripleDir = DirName;
903+
UsingUnversionedDir = false;
904+
}
905+
}
906+
}
907+
}
908+
909+
if (TripleDir.empty())
910+
return {};
911+
912+
SmallString<128> P(BaseDir);
913+
llvm::sys::path::append(P, TripleDir);
914+
if (UsingUnversionedDir && Diags)
915+
Diags->Report(diag::warn_android_unversioned_fallback) << P << T.getTriple();
916+
return std::string(P);
917+
}
918+
877919
std::optional<std::string>
878920
ToolChain::getFallbackAndroidTargetPath(StringRef BaseDir) const {
921+
return ::getFallbackAndroidTargetPath(BaseDir, getTriple(), getVFS(), &D.getDiags());
922+
#if 0
879923
llvm::Triple TripleWithoutLevel(getTriple());
880924
TripleWithoutLevel.setEnvironmentName("android"); // remove any version number
881925
const std::string &TripleWithoutLevelStr = TripleWithoutLevel.str();
@@ -913,9 +957,10 @@ ToolChain::getFallbackAndroidTargetPath(StringRef BaseDir) const {
913957
if (UsingUnversionedDir)
914958
D.Diag(diag::warn_android_unversioned_fallback) << P << getTripleString();
915959
return std::string(P);
960+
#endif
916961
}
917962

918-
llvm::Triple ToolChain::getTripleWithoutOSVersion() const {
963+
llvm::Triple ToolChain::getTripleWithoutOSVersion(const llvm::Triple &Triple) {
919964
return (Triple.hasEnvironment()
920965
? llvm::Triple(Triple.getArchName(), Triple.getVendorName(),
921966
llvm::Triple::getOSTypeName(Triple.getOS()),
@@ -925,8 +970,74 @@ llvm::Triple ToolChain::getTripleWithoutOSVersion() const {
925970
llvm::Triple::getOSTypeName(Triple.getOS())));
926971
}
927972

973+
static std::optional<std::string> getTargetSubDirPath(StringRef BaseDir, const llvm::Triple &T ,llvm::vfs::FileSystem &VFS, clang::DiagnosticsEngine *Diags ) {
974+
auto getPathForTriple = [&](const llvm::Triple &Triple) -> std::optional<std::string> {
975+
SmallString<128> P(BaseDir);
976+
llvm::sys::path::append(P, Triple.str());
977+
if (VFS.exists(P))
978+
return std::string(P);
979+
return {};
980+
};
981+
982+
983+
if (auto Path = getPathForTriple(T))
984+
return *Path;
985+
986+
if (T.isOSAIX()) {
987+
llvm::Triple AIXTriple;
988+
if (T.getEnvironment() == Triple::UnknownEnvironment) {
989+
// Strip unknown environment and the OS version from the triple.
990+
AIXTriple = llvm::Triple(T.getArchName(), T.getVendorName(),
991+
llvm::Triple::getOSTypeName(T.getOS()));
992+
} else {
993+
// Strip the OS version from the triple.
994+
AIXTriple =ToolChain:: getTripleWithoutOSVersion(T);
995+
}
996+
if (auto Path = getPathForTriple(AIXTriple))
997+
return *Path;
998+
}
999+
1000+
if (T.isOSzOS() &&
1001+
(!T.getOSVersion().empty() || !T.getEnvironmentVersion().empty())) {
1002+
// Build the triple without version information
1003+
const llvm::Triple &TripleWithoutVersion =ToolChain:: getTripleWithoutOSVersion(T);
1004+
if (auto Path = getPathForTriple(TripleWithoutVersion))
1005+
return *Path;
1006+
}
1007+
1008+
// When building with per target runtime directories, various ways of naming
1009+
// the Arm architecture may have been normalised to simply "arm".
1010+
// For example "armv8l" (Armv8 AArch32 little endian) is replaced with "arm".
1011+
// Since an armv8l system can use libraries built for earlier architecture
1012+
// versions assuming endian and float ABI match.
1013+
//
1014+
// Original triple: armv8l-unknown-linux-gnueabihf
1015+
// Runtime triple: arm-unknown-linux-gnueabihf
1016+
//
1017+
// We do not do this for armeb (big endian) because doing so could make us
1018+
// select little endian libraries. In addition, all known armeb triples only
1019+
// use the "armeb" architecture name.
1020+
//
1021+
// M profile Arm is bare metal and we know they will not be using the per
1022+
// target runtime directory layout.
1023+
if (T.getArch() == Triple::arm && !T.isArmMClass()) {
1024+
llvm::Triple ArmTriple = T;
1025+
ArmTriple.setArch(Triple::arm);
1026+
if (auto Path = getPathForTriple(ArmTriple))
1027+
return *Path;
1028+
}
1029+
1030+
if (T.isAndroid())
1031+
return :: getFallbackAndroidTargetPath(BaseDir, T, VFS, Diags);
1032+
1033+
return {};
1034+
}
1035+
1036+
9281037
std::optional<std::string>
9291038
ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
1039+
return ::getTargetSubDirPath(BaseDir, getTriple(), getVFS(),& D.getDiags() );
1040+
#if 0
9301041
auto getPathForTriple =
9311042
[&](const llvm::Triple &Triple) -> std::optional<std::string> {
9321043
SmallString<128> P(BaseDir);
@@ -988,6 +1099,19 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
9881099
return getFallbackAndroidTargetPath(BaseDir);
9891100

9901101
return {};
1102+
#endif
1103+
}
1104+
1105+
std::optional< std::string> ToolChain::getDefaultIntrinsicModuleDir(StringRef ResourceDir, const llvm::Triple &T ,llvm::vfs::FileSystem &VFS, clang::DiagnosticsEngine *Diags ) {
1106+
SmallString<128> P(ResourceDir);
1107+
llvm::sys::path::append(P, "finclude");
1108+
return ::getTargetSubDirPath(P, T, VFS, Diags );
1109+
// llvm::sys::path::append(instrinsicsDir, "finclude", triple.str());
1110+
// return instrinsicsDir.str().str();
1111+
}
1112+
1113+
std::optional< std::string> ToolChain::getDefaultIntrinsicModuleDir( ) const {
1114+
return ToolChain::getDefaultIntrinsicModuleDir(D.ResourceDir, getTriple(), getVFS(),& D.getDiags() );
9911115
}
9921116

9931117
std::optional<std::string> ToolChain::getRuntimePath() const {
@@ -1015,24 +1139,6 @@ std::optional<std::string> ToolChain::getStdlibIncludePath() const {
10151139
return getTargetSubDirPath(P);
10161140
}
10171141

1018-
ToolChain:: path_list ToolChain:: getDefaultIntrinsicModulePaths() const{
1019-
SmallString<128> P(D.ResourceDir);
1020-
llvm::sys::path::append(P, "finclude");
1021-
1022-
// TODO: If there are multiple valid names for the target triple, prefer to add all of them instead probing which are existing.
1023-
ToolChain:: path_list Result;
1024-
if (std::optional<std::string> PerTargetPath = getTargetSubDirPath(P))
1025-
Result.push_back(*PerTargetPath);
1026-
1027-
// flang used this in the past, keep for compatibility
1028-
SmallString<128> CompatIntrModPath(D.Dir);
1029-
llvm::sys::path::append(CompatIntrModPath, "..", "include", "flang");
1030-
Result.push_back(std::string(CompatIntrModPath));
1031-
1032-
return Result;
1033-
}
1034-
1035-
10361142
ToolChain::path_list ToolChain::getArchSpecificLibPaths() const {
10371143
path_list Paths;
10381144

clang/lib/Driver/ToolChains/Flang.cpp

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

960-
// implicitly added by frontend
961-
// TODO: should be set by driver
962-
#if 0
963-
// Default intrinsic module dirs must be added after any user-provided -fintrinsic-modules-path
964-
for (auto && IntrModPath : TC.getIntrinsicModulePaths()) {
960+
961+
#if 1
962+
// Default intrinsic module dirs must be added after any user-provided -fintrinsic-modules-path to have lower precedence
963+
if ( auto IntrModPath = TC.getDefaultIntrinsicModuleDir()) {
965964
CmdArgs.push_back("-fintrinsic-modules-path");
966-
CmdArgs.push_back(IntrModPath.c_str());
965+
CmdArgs.push_back( Args.MakeArgString(*IntrModPath));
967966
}
968967
#endif
969968

flang/cmake/modules/FlangCommon.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
2424
is mapped to long double, etc."
2525
)
2626
if (FLANG_RUNTIME_F128_MATH_LIB)
27-
add_compile_definitions(FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}") # FIXME: quoting hell
27+
add_compile_definitions(FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}")
2828
endif()
2929

3030
# Check if 128-bit float computations can be done via long double

0 commit comments

Comments
 (0)