diff options
Diffstat (limited to 'contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp | 4979 |
1 files changed, 3319 insertions, 1660 deletions
diff --git a/contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp b/contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp index 659989ceb605..8858c318aba7 100644 --- a/contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp +++ b/contrib/llvm-project/clang/lib/Driver/ToolChains/Clang.cpp @@ -10,6 +10,9 @@ #include "AMDGPU.h" #include "Arch/AArch64.h" #include "Arch/ARM.h" +#include "Arch/CSKY.h" +#include "Arch/LoongArch.h" +#include "Arch/M68k.h" #include "Arch/Mips.h" #include "Arch/PPC.h" #include "Arch/RISCV.h" @@ -19,31 +22,47 @@ #include "Arch/X86.h" #include "CommonArgs.h" #include "Hexagon.h" -#include "InputInfo.h" #include "MSP430.h" #include "PS4CPU.h" +#include "clang/Basic/CLWarnings.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/CodeGenOptions.h" +#include "clang/Basic/HeaderInclude.h" #include "clang/Basic/LangOptions.h" +#include "clang/Basic/MakeSupport.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/Version.h" +#include "clang/Config/config.h" +#include "clang/Driver/Action.h" #include "clang/Driver/Distro.h" #include "clang/Driver/DriverDiagnostic.h" +#include "clang/Driver/InputInfo.h" #include "clang/Driver/Options.h" #include "clang/Driver/SanitizerArgs.h" +#include "clang/Driver/Types.h" #include "clang/Driver/XRayArgs.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/BinaryFormat/Magic.h" #include "llvm/Config/llvm-config.h" +#include "llvm/Frontend/Debug/Options.h" +#include "llvm/Object/ObjectFile.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Compression.h" +#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/Host.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" -#include "llvm/Support/TargetParser.h" #include "llvm/Support/YAMLParser.h" +#include "llvm/TargetParser/AArch64TargetParser.h" +#include "llvm/TargetParser/ARMTargetParserCommon.h" +#include "llvm/TargetParser/Host.h" +#include "llvm/TargetParser/LoongArchTargetParser.h" +#include "llvm/TargetParser/RISCVISAInfo.h" +#include "llvm/TargetParser/RISCVTargetParser.h" +#include <cctype> using namespace clang::driver; using namespace clang::driver::tools; @@ -51,8 +70,11 @@ using namespace clang; using namespace llvm::opt; static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { - if (Arg *A = - Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC)) { + if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC, + options::OPT_fminimize_whitespace, + options::OPT_fno_minimize_whitespace, + options::OPT_fkeep_system_includes, + options::OPT_fno_keep_system_includes)) { if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) && !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) { D.Diag(clang::diag::err_drv_argument_only_allowed_with) @@ -89,34 +111,6 @@ static void EscapeSpacesAndBackslashes(const char *Arg, } } -// Quote target names for inclusion in GNU Make dependency files. -// Only the characters '$', '#', ' ', '\t' are quoted. -static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) { - for (unsigned i = 0, e = Target.size(); i != e; ++i) { - switch (Target[i]) { - case ' ': - case '\t': - // Escape the preceding backslashes - for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j) - Res.push_back('\\'); - - // Escape the space/tab - Res.push_back('\\'); - break; - case '$': - Res.push_back('$'); - break; - case '#': - Res.push_back('\\'); - break; - default: - break; - } - - Res.push_back(Target[i]); - } -} - /// Apply \a Work on the current tool chain \a RegularToolChain and any other /// offloading tool chain that is associated with the current action \a JA. static void @@ -219,17 +213,21 @@ static void ParseMRecip(const Driver &D, const ArgList &Args, llvm::StringMap<bool> OptionStrings; OptionStrings.insert(std::make_pair("divd", false)); OptionStrings.insert(std::make_pair("divf", false)); + OptionStrings.insert(std::make_pair("divh", false)); OptionStrings.insert(std::make_pair("vec-divd", false)); OptionStrings.insert(std::make_pair("vec-divf", false)); + OptionStrings.insert(std::make_pair("vec-divh", false)); OptionStrings.insert(std::make_pair("sqrtd", false)); OptionStrings.insert(std::make_pair("sqrtf", false)); + OptionStrings.insert(std::make_pair("sqrth", false)); OptionStrings.insert(std::make_pair("vec-sqrtd", false)); OptionStrings.insert(std::make_pair("vec-sqrtf", false)); + OptionStrings.insert(std::make_pair("vec-sqrth", false)); for (unsigned i = 0; i != NumOptions; ++i) { StringRef Val = A->getValue(i); - bool IsDisabled = Val.startswith(DisabledPrefixIn); + bool IsDisabled = Val.starts_with(DisabledPrefixIn); // Ignore the disablement token for string matching. if (IsDisabled) Val = Val.substr(1); @@ -248,10 +246,11 @@ static void ParseMRecip(const Driver &D, const ArgList &Args, D.Diag(diag::err_drv_unknown_argument) << Val; return; } - // The option was specified without a float or double suffix. - // Make sure that the double entry was not already specified. + // The option was specified without a half or float or double suffix. + // Make sure that the double or half entry was not already specified. // The float entry will be checked below. - if (OptionStrings[ValBase.str() + 'd']) { + if (OptionStrings[ValBase.str() + 'd'] || + OptionStrings[ValBase.str() + 'h']) { D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; return; } @@ -266,9 +265,12 @@ static void ParseMRecip(const Driver &D, const ArgList &Args, // Mark the matched option as found. Do not allow duplicate specifiers. OptionIter->second = true; - // If the precision was not specified, also mark the double entry as found. - if (ValBase.back() != 'f' && ValBase.back() != 'd') + // If the precision was not specified, also mark the double and half entry + // as found. + if (ValBase.back() != 'f' && ValBase.back() != 'd' && ValBase.back() != 'h') { OptionStrings[ValBase.str() + 'd'] = true; + OptionStrings[ValBase.str() + 'h'] = true; + } // Build the output string. StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut; @@ -301,83 +303,6 @@ static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args, } } -static void getWebAssemblyTargetFeatures(const ArgList &Args, - std::vector<StringRef> &Features) { - handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group); -} - -static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, - const ArgList &Args, ArgStringList &CmdArgs, - bool ForAS, bool IsAux = false) { - std::vector<StringRef> Features; - switch (Triple.getArch()) { - default: - break; - case llvm::Triple::mips: - case llvm::Triple::mipsel: - case llvm::Triple::mips64: - case llvm::Triple::mips64el: - mips::getMIPSTargetFeatures(D, Triple, Args, Features); - break; - - case llvm::Triple::arm: - case llvm::Triple::armeb: - case llvm::Triple::thumb: - case llvm::Triple::thumbeb: - arm::getARMTargetFeatures(D, Triple, Args, CmdArgs, Features, ForAS); - break; - - case llvm::Triple::ppc: - case llvm::Triple::ppcle: - case llvm::Triple::ppc64: - case llvm::Triple::ppc64le: - ppc::getPPCTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::riscv32: - case llvm::Triple::riscv64: - riscv::getRISCVTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::systemz: - systemz::getSystemZTargetFeatures(D, Args, Features); - break; - case llvm::Triple::aarch64: - case llvm::Triple::aarch64_32: - case llvm::Triple::aarch64_be: - aarch64::getAArch64TargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::x86: - case llvm::Triple::x86_64: - x86::getX86TargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::hexagon: - hexagon::getHexagonTargetFeatures(D, Args, Features); - break; - case llvm::Triple::wasm32: - case llvm::Triple::wasm64: - getWebAssemblyTargetFeatures(Args, Features); - break; - case llvm::Triple::sparc: - case llvm::Triple::sparcel: - case llvm::Triple::sparcv9: - sparc::getSparcTargetFeatures(D, Args, Features); - break; - case llvm::Triple::r600: - case llvm::Triple::amdgcn: - amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::msp430: - msp430::getMSP430TargetFeatures(D, Args, Features); - break; - case llvm::Triple::ve: - ve::getVETargetFeatures(D, Args, Features); - } - - for (auto Feature : unifyTargetFeatures(Features)) { - CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature"); - CmdArgs.push_back(Feature.data()); - } -} - static bool shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, const llvm::Triple &Triple) { @@ -396,10 +321,10 @@ shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, } /// Adds exception related arguments to the driver command arguments. There's a -/// master flag, -fexceptions and also language specific flags to enable/disable +/// main flag, -fexceptions and also language specific flags to enable/disable /// C++ and Objective-C exceptions. This makes it possible to for example /// disable C++ exceptions but enable Objective-C exceptions. -static void addExceptionArgs(const ArgList &Args, types::ID InputType, +static bool addExceptionArgs(const ArgList &Args, types::ID InputType, const ToolChain &TC, bool KernelOrKext, const ObjCRuntime &objcRuntime, ArgStringList &CmdArgs) { @@ -414,13 +339,25 @@ static void addExceptionArgs(const ArgList &Args, types::ID InputType, Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); Args.ClaimAllArgs(options::OPT_fcxx_exceptions); Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); - return; + Args.ClaimAllArgs(options::OPT_fasync_exceptions); + Args.ClaimAllArgs(options::OPT_fno_async_exceptions); + return false; } // See if the user explicitly enabled exceptions. bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, false); + // Async exceptions are Windows MSVC only. + if (Triple.isWindowsMSVCEnvironment()) { + bool EHa = Args.hasFlag(options::OPT_fasync_exceptions, + options::OPT_fno_async_exceptions, false); + if (EHa) { + CmdArgs.push_back("-fasync-exceptions"); + EH = true; + } + } + // Obj-C exceptions are enabled by default, regardless of -fexceptions. This // is not necessarily sensible, but follows GCC. if (types::isObjC(InputType) && @@ -432,9 +369,9 @@ static void addExceptionArgs(const ArgList &Args, types::ID InputType, } if (types::isCXX(InputType)) { - // Disable C++ EH by default on XCore and PS4. - bool CXXExceptionsEnabled = - Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU(); + // Disable C++ EH by default on XCore and PS4/PS5. + bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore && + !Triple.isPS() && !Triple.isDriverKit(); Arg *ExceptionArg = Args.getLastArg( options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, options::OPT_fexceptions, options::OPT_fno_exceptions); @@ -455,8 +392,12 @@ static void addExceptionArgs(const ArgList &Args, types::ID InputType, // So we do not set EH to false. Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions); + Args.addOptInFlag(CmdArgs, options::OPT_fassume_nothrow_exception_dtor, + options::OPT_fno_assume_nothrow_exception_dtor); + if (EH) CmdArgs.push_back("-fexceptions"); + return EH; } static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, @@ -475,170 +416,77 @@ static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, Default); } -static bool ShouldDisableDwarfDirectory(const ArgList &Args, - const ToolChain &TC) { - bool UseDwarfDirectory = - Args.hasFlag(options::OPT_fdwarf_directory_asm, - options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs()); - return !UseDwarfDirectory; -} - -// Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases -// to the corresponding DebugInfoKind. -static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) { - assert(A.getOption().matches(options::OPT_gN_Group) && - "Not a -g option that specifies a debug-info level"); - if (A.getOption().matches(options::OPT_g0) || - A.getOption().matches(options::OPT_ggdb0)) - return codegenoptions::NoDebugInfo; - if (A.getOption().matches(options::OPT_gline_tables_only) || - A.getOption().matches(options::OPT_ggdb1)) - return codegenoptions::DebugLineTablesOnly; - if (A.getOption().matches(options::OPT_gline_directives_only)) - return codegenoptions::DebugDirectivesOnly; - return codegenoptions::LimitedDebugInfo; -} - -static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) { - switch (Triple.getArch()){ - default: - return false; - case llvm::Triple::arm: - case llvm::Triple::thumb: - // ARM Darwin targets require a frame pointer to be always present to aid - // offline debugging via backtraces. - return Triple.isOSDarwin(); +/// Add a CC1 option to specify the debug compilation directory. +static const char *addDebugCompDirArg(const ArgList &Args, + ArgStringList &CmdArgs, + const llvm::vfs::FileSystem &VFS) { + if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, + options::OPT_fdebug_compilation_dir_EQ)) { + if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) + CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") + + A->getValue())); + else + A->render(Args, CmdArgs); + } else if (llvm::ErrorOr<std::string> CWD = + VFS.getCurrentWorkingDirectory()) { + CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD)); } + StringRef Path(CmdArgs.back()); + return Path.substr(Path.find('=') + 1).data(); } -static bool useFramePointerForTargetByDefault(const ArgList &Args, - const llvm::Triple &Triple) { - if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry)) - return true; - - switch (Triple.getArch()) { - case llvm::Triple::xcore: - case llvm::Triple::wasm32: - case llvm::Triple::wasm64: - case llvm::Triple::msp430: - // XCore never wants frame pointers, regardless of OS. - // WebAssembly never wants frame pointers. - return false; - case llvm::Triple::ppc: - case llvm::Triple::ppcle: - case llvm::Triple::ppc64: - case llvm::Triple::ppc64le: - case llvm::Triple::riscv32: - case llvm::Triple::riscv64: - case llvm::Triple::amdgcn: - case llvm::Triple::r600: - return !areOptimizationsEnabled(Args); - default: - break; - } - - if (Triple.isOSNetBSD()) { - return !areOptimizationsEnabled(Args); - } - - if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI || - Triple.isOSHurd()) { - switch (Triple.getArch()) { - // Don't use a frame pointer on linux if optimizing for certain targets. - case llvm::Triple::arm: - case llvm::Triple::armeb: - case llvm::Triple::thumb: - case llvm::Triple::thumbeb: - if (Triple.isAndroid()) - return true; - LLVM_FALLTHROUGH; - case llvm::Triple::mips64: - case llvm::Triple::mips64el: - case llvm::Triple::mips: - case llvm::Triple::mipsel: - case llvm::Triple::systemz: - case llvm::Triple::x86: - case llvm::Triple::x86_64: - return !areOptimizationsEnabled(Args); - default: - return true; - } - } - - if (Triple.isOSWindows()) { - switch (Triple.getArch()) { - case llvm::Triple::x86: - return !areOptimizationsEnabled(Args); - case llvm::Triple::x86_64: - return Triple.isOSBinFormatMachO(); - case llvm::Triple::arm: - case llvm::Triple::thumb: - // Windows on ARM builds with FPO disabled to aid fast stack walking - return true; - default: - // All other supported Windows ISAs use xdata unwind information, so frame - // pointers are not generally useful. - return false; - } - } - - return true; -} +static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs, + const char *DebugCompilationDir, + const char *OutputFileName) { + // No need to generate a value for -object-file-name if it was provided. + for (auto *Arg : Args.filtered(options::OPT_Xclang)) + if (StringRef(Arg->getValue()).starts_with("-object-file-name")) + return; -static CodeGenOptions::FramePointerKind -getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) { - // We have 4 states: - // - // 00) leaf retained, non-leaf retained - // 01) leaf retained, non-leaf omitted (this is invalid) - // 10) leaf omitted, non-leaf retained - // (what -momit-leaf-frame-pointer was designed for) - // 11) leaf omitted, non-leaf omitted - // - // "omit" options taking precedence over "no-omit" options is the only way - // to make 3 valid states representable - Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer, - options::OPT_fno_omit_frame_pointer); - bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer); - bool NoOmitFP = - A && A->getOption().matches(options::OPT_fno_omit_frame_pointer); - bool OmitLeafFP = Args.hasFlag(options::OPT_momit_leaf_frame_pointer, - options::OPT_mno_omit_leaf_frame_pointer, - Triple.isAArch64() || Triple.isPS4CPU()); - if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) || - (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) { - if (OmitLeafFP) - return CodeGenOptions::FramePointerKind::NonLeaf; - return CodeGenOptions::FramePointerKind::All; - } - return CodeGenOptions::FramePointerKind::None; -} + if (Args.hasArg(options::OPT_object_file_name_EQ)) + return; -/// Add a CC1 option to specify the debug compilation directory. -static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs, - const llvm::vfs::FileSystem &VFS) { - if (Arg *A = Args.getLastArg(options::OPT_fdebug_compilation_dir)) { - CmdArgs.push_back("-fdebug-compilation-dir"); - CmdArgs.push_back(A->getValue()); - } else if (llvm::ErrorOr<std::string> CWD = - VFS.getCurrentWorkingDirectory()) { - CmdArgs.push_back("-fdebug-compilation-dir"); - CmdArgs.push_back(Args.MakeArgString(*CWD)); - } + SmallString<128> ObjFileNameForDebug(OutputFileName); + if (ObjFileNameForDebug != "-" && + !llvm::sys::path::is_absolute(ObjFileNameForDebug) && + (!DebugCompilationDir || + llvm::sys::path::is_absolute(DebugCompilationDir))) { + // Make the path absolute in the debug infos like MSVC does. + llvm::sys::fs::make_absolute(ObjFileNameForDebug); + } + // If the object file name is a relative path, then always use Windows + // backslash style as -object-file-name is used for embedding object file path + // in codeview and it can only be generated when targeting on Windows. + // Otherwise, just use native absolute path. + llvm::sys::path::Style Style = + llvm::sys::path::is_absolute(ObjFileNameForDebug) + ? llvm::sys::path::Style::native + : llvm::sys::path::Style::windows_backslash; + llvm::sys::path::remove_dots(ObjFileNameForDebug, /*remove_dot_dot=*/true, + Style); + CmdArgs.push_back( + Args.MakeArgString(Twine("-object-file-name=") + ObjFileNameForDebug)); } /// Add a CC1 and CC1AS option to specify the debug file path prefix map. -static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) { - for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, - options::OPT_fdebug_prefix_map_EQ)) { - StringRef Map = A->getValue(); - if (Map.find('=') == StringRef::npos) - D.Diag(diag::err_drv_invalid_argument_to_option) - << Map << A->getOption().getName(); +static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC, + const ArgList &Args, ArgStringList &CmdArgs) { + auto AddOneArg = [&](StringRef Map, StringRef Name) { + if (!Map.contains('=')) + D.Diag(diag::err_drv_invalid_argument_to_option) << Map << Name; else CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); + }; + + for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, + options::OPT_fdebug_prefix_map_EQ)) { + AddOneArg(A->getValue(), A->getOption().getName()); A->claim(); } + std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping(); + if (GlobalRemapEntry.empty()) + return; + AddOneArg(GlobalRemapEntry, "environment"); } /// Add a CC1 and CC1AS option to specify the macro file path prefix map. @@ -647,7 +495,7 @@ static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, options::OPT_fmacro_prefix_map_EQ)) { StringRef Map = A->getValue(); - if (Map.find('=') == StringRef::npos) + if (!Map.contains('=')) D.Diag(diag::err_drv_invalid_argument_to_option) << Map << A->getOption().getName(); else @@ -657,16 +505,16 @@ static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, } /// Add a CC1 and CC1AS option to specify the coverage file path prefix map. -static void addProfilePrefixMapArg(const Driver &D, const ArgList &Args, +static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) { for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, - options::OPT_fprofile_prefix_map_EQ)) { + options::OPT_fcoverage_prefix_map_EQ)) { StringRef Map = A->getValue(); - if (Map.find('=') == StringRef::npos) + if (!Map.contains('=')) D.Diag(diag::err_drv_invalid_argument_to_option) << Map << A->getOption().getName(); else - CmdArgs.push_back(Args.MakeArgString("-fprofile-prefix-map=" + Map)); + CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map)); A->claim(); } } @@ -736,10 +584,10 @@ static void addDashXForInput(const ArgList &Args, const InputInfo &Input, } static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, - const Driver &D, const InputInfo &Output, - const ArgList &Args, + const JobAction &JA, const InputInfo &Output, + const ArgList &Args, SanitizerArgs &SanArgs, ArgStringList &CmdArgs) { - + const Driver &D = TC.getDriver(); auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, options::OPT_fno_profile_generate); @@ -747,12 +595,7 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) PGOGenerateArg = nullptr; - auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate, - options::OPT_fcs_profile_generate_EQ, - options::OPT_fno_profile_generate); - if (CSPGOGenerateArg && - CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) - CSPGOGenerateArg = nullptr; + auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args); auto *ProfileGenerateArg = Args.getLastArg( options::OPT_fprofile_instr_generate, @@ -783,6 +626,12 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, PGOGenerateArg = nullptr; } + if (TC.getTriple().isOSAIX()) { + if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args)) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << ProfileSampleUseArg->getSpelling() << TC.getTriple().str(); + } + if (ProfileGenerateArg) { if (ProfileGenerateArg->getOption().matches( options::OPT_fprofile_instr_generate_EQ)) @@ -790,7 +639,9 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, ProfileGenerateArg->getValue())); // The default is to use Clang Instrumentation. CmdArgs.push_back("-fprofile-instrument=clang"); - if (TC.getTriple().isWindowsMSVCEnvironment()) { + if (TC.getTriple().isWindowsMSVCEnvironment() && + Args.hasFlag(options::OPT_frtlib_defaultlib, + options::OPT_fno_rtlib_defaultlib, true)) { // Add dependent lib for clang_rt.profile CmdArgs.push_back(Args.MakeArgString( "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); @@ -809,7 +660,9 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, CmdArgs.push_back("-fprofile-instrument=csllvm"); } if (PGOGenArg) { - if (TC.getTriple().isWindowsMSVCEnvironment()) { + if (TC.getTriple().isWindowsMSVCEnvironment() && + Args.hasFlag(options::OPT_frtlib_defaultlib, + options::OPT_fno_rtlib_defaultlib, true)) { // Add dependent lib for clang_rt.profile CmdArgs.push_back(Args.MakeArgString( "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); @@ -845,10 +698,6 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, options::OPT_fno_test_coverage, false) || Args.hasArg(options::OPT_coverage); bool EmitCovData = TC.needsGCovInstrumentation(Args); - if (EmitCovNotes) - CmdArgs.push_back("-ftest-coverage"); - if (EmitCovData) - CmdArgs.push_back("-fprofile-arcs"); if (Args.hasFlag(options::OPT_fcoverage_mapping, options::OPT_fno_coverage_mapping, false)) { @@ -860,6 +709,29 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, CmdArgs.push_back("-fcoverage-mapping"); } + if (Args.hasFlag(options::OPT_fmcdc_coverage, options::OPT_fno_mcdc_coverage, + false)) { + if (!Args.hasFlag(options::OPT_fcoverage_mapping, + options::OPT_fno_coverage_mapping, false)) + D.Diag(clang::diag::err_drv_argument_only_allowed_with) + << "-fcoverage-mcdc" + << "-fcoverage-mapping"; + + CmdArgs.push_back("-fcoverage-mcdc"); + } + + if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, + options::OPT_fcoverage_compilation_dir_EQ)) { + if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) + CmdArgs.push_back(Args.MakeArgString( + Twine("-fcoverage-compilation-dir=") + A->getValue())); + else + A->render(Args, CmdArgs); + } else if (llvm::ErrorOr<std::string> CWD = + D.getVFS().getCurrentWorkingDirectory()) { + CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD)); + } + if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) { auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ); if (!Args.hasArg(options::OPT_coverage)) @@ -889,11 +761,30 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, CmdArgs.push_back("-fprofile-update=atomic"); else if (Val != "single") D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; - } else if (TC.getSanitizerArgs().needsTsanRt()) { - CmdArgs.push_back("-fprofile-update=atomic"); + << A->getSpelling() << Val; } + int FunctionGroups = 1; + int SelectedFunctionGroup = 0; + if (const auto *A = Args.getLastArg(options::OPT_fprofile_function_groups)) { + StringRef Val = A->getValue(); + if (Val.getAsInteger(0, FunctionGroups) || FunctionGroups < 1) + D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; + } + if (const auto *A = + Args.getLastArg(options::OPT_fprofile_selected_function_group)) { + StringRef Val = A->getValue(); + if (Val.getAsInteger(0, SelectedFunctionGroup) || + SelectedFunctionGroup < 0 || SelectedFunctionGroup >= FunctionGroups) + D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; + } + if (FunctionGroups != 1) + CmdArgs.push_back(Args.MakeArgString("-fprofile-function-groups=" + + Twine(FunctionGroups))); + if (SelectedFunctionGroup != 0) + CmdArgs.push_back(Args.MakeArgString("-fprofile-selected-function-group=" + + Twine(SelectedFunctionGroup))); + // Leave -fprofile-dir= an unused argument unless .gcda emission is // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider // the flag used. There is no -fno-profile-dir, so the user has no @@ -903,112 +794,55 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, Args.hasArg(options::OPT_coverage)) FProfileDir = Args.getLastArg(options::OPT_fprofile_dir); - // Put the .gcno and .gcda files (if needed) next to the object file or - // bitcode file in the case of LTO. - // FIXME: There should be a simpler way to find the object file for this - // input, and this code probably does the wrong thing for commands that - // compile and link all at once. - if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) && - (EmitCovNotes || EmitCovData) && Output.isFilename()) { - SmallString<128> OutputFilename; - if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo)) - OutputFilename = FinalOutput->getValue(); - else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) - OutputFilename = FinalOutput->getValue(); - else - OutputFilename = llvm::sys::path::filename(Output.getBaseInput()); - SmallString<128> CoverageFilename = OutputFilename; + // TODO: Don't claim -c/-S to warn about -fsyntax-only -c/-S, -E -c/-S, + // like we warn about -fsyntax-only -E. + (void)(Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)); + + // Put the .gcno and .gcda files (if needed) next to the primary output file, + // or fall back to a file in the current directory for `clang -c --coverage + // d/a.c` in the absence of -o. + if (EmitCovNotes || EmitCovData) { + SmallString<128> CoverageFilename; + if (Arg *DumpDir = Args.getLastArgNoClaim(options::OPT_dumpdir)) { + // Form ${dumpdir}${basename}.gcno. Note that dumpdir may not end with a + // path separator. + CoverageFilename = DumpDir->getValue(); + CoverageFilename += llvm::sys::path::filename(Output.getBaseInput()); + } else if (Arg *FinalOutput = + C.getArgs().getLastArg(options::OPT__SLASH_Fo)) { + CoverageFilename = FinalOutput->getValue(); + } else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) { + CoverageFilename = FinalOutput->getValue(); + } else { + CoverageFilename = llvm::sys::path::filename(Output.getBaseInput()); + } if (llvm::sys::path::is_relative(CoverageFilename)) (void)D.getVFS().makeAbsolute(CoverageFilename); llvm::sys::path::replace_extension(CoverageFilename, "gcno"); - - CmdArgs.push_back("-coverage-notes-file"); - CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); + if (EmitCovNotes) { + CmdArgs.push_back( + Args.MakeArgString("-coverage-notes-file=" + CoverageFilename)); + } if (EmitCovData) { if (FProfileDir) { + SmallString<128> Gcno = std::move(CoverageFilename); CoverageFilename = FProfileDir->getValue(); - llvm::sys::path::append(CoverageFilename, OutputFilename); + llvm::sys::path::append(CoverageFilename, Gcno); } llvm::sys::path::replace_extension(CoverageFilename, "gcda"); - CmdArgs.push_back("-coverage-data-file"); - CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); - } - } -} - -/// Check whether the given input tree contains any compilation actions. -static bool ContainsCompileAction(const Action *A) { - if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A)) - return true; - - for (const auto &AI : A->inputs()) - if (ContainsCompileAction(AI)) - return true; - - return false; -} - -/// Check if -relax-all should be passed to the internal assembler. -/// This is done by default when compiling non-assembler source with -O0. -static bool UseRelaxAll(Compilation &C, const ArgList &Args) { - bool RelaxDefault = true; - - if (Arg *A = Args.getLastArg(options::OPT_O_Group)) - RelaxDefault = A->getOption().matches(options::OPT_O0); - - if (RelaxDefault) { - RelaxDefault = false; - for (const auto &Act : C.getActions()) { - if (ContainsCompileAction(Act)) { - RelaxDefault = true; - break; - } + CmdArgs.push_back( + Args.MakeArgString("-coverage-data-file=" + CoverageFilename)); } } - - return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, - RelaxDefault); } -// Extract the integer N from a string spelled "-dwarf-N", returning 0 -// on mismatch. The StringRef input (rather than an Arg) allows -// for use by the "-Xassembler" option parser. -static unsigned DwarfVersionNum(StringRef ArgValue) { - return llvm::StringSwitch<unsigned>(ArgValue) - .Case("-gdwarf-2", 2) - .Case("-gdwarf-3", 3) - .Case("-gdwarf-4", 4) - .Case("-gdwarf-5", 5) - .Default(0); -} - -static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, - codegenoptions::DebugInfoKind DebugInfoKind, - unsigned DwarfVersion, - llvm::DebuggerKind DebuggerTuning) { - switch (DebugInfoKind) { - case codegenoptions::DebugDirectivesOnly: - CmdArgs.push_back("-debug-info-kind=line-directives-only"); - break; - case codegenoptions::DebugLineTablesOnly: - CmdArgs.push_back("-debug-info-kind=line-tables-only"); - break; - case codegenoptions::DebugInfoConstructor: - CmdArgs.push_back("-debug-info-kind=constructor"); - break; - case codegenoptions::LimitedDebugInfo: - CmdArgs.push_back("-debug-info-kind=limited"); - break; - case codegenoptions::FullDebugInfo: - CmdArgs.push_back("-debug-info-kind=standalone"); - break; - case codegenoptions::UnusedTypeInfo: - CmdArgs.push_back("-debug-info-kind=unused-types"); - break; - default: - break; - } +static void +RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, + llvm::codegenoptions::DebugInfoKind DebugInfoKind, + unsigned DwarfVersion, + llvm::DebuggerKind DebuggerTuning) { + addDebugInfoKind(CmdArgs, DebugInfoKind); if (DwarfVersion > 0) CmdArgs.push_back( Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); @@ -1022,6 +856,9 @@ static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, case llvm::DebuggerKind::SCE: CmdArgs.push_back("-debugger-tuning=sce"); break; + case llvm::DebuggerKind::DBX: + CmdArgs.push_back("-debugger-tuning=dbx"); + break; default: break; } @@ -1048,45 +885,87 @@ static void RenderDebugInfoCompressionArgs(const ArgList &Args, StringRef Value = A->getValue(); if (Value == "none") { CmdArgs.push_back("--compress-debug-sections=none"); - } else if (Value == "zlib" || Value == "zlib-gnu") { - if (llvm::zlib::isAvailable()) { + } else if (Value == "zlib") { + if (llvm::compression::zlib::isAvailable()) { CmdArgs.push_back( Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); } else { - D.Diag(diag::warn_debug_compression_unavailable); + D.Diag(diag::warn_debug_compression_unavailable) << "zlib"; + } + } else if (Value == "zstd") { + if (llvm::compression::zstd::isAvailable()) { + CmdArgs.push_back( + Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); + } else { + D.Diag(diag::warn_debug_compression_unavailable) << "zstd"; } } else { D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Value; + << A->getSpelling() << Value; } } } -static const char *RelocationModelName(llvm::Reloc::Model Model) { - switch (Model) { - case llvm::Reloc::Static: - return "static"; - case llvm::Reloc::PIC_: - return "pic"; - case llvm::Reloc::DynamicNoPIC: - return "dynamic-no-pic"; - case llvm::Reloc::ROPI: - return "ropi"; - case llvm::Reloc::RWPI: - return "rwpi"; - case llvm::Reloc::ROPI_RWPI: - return "ropi-rwpi"; - } - llvm_unreachable("Unknown Reloc::Model kind"); -} static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, const ArgList &Args, - ArgStringList &CmdArgs) { - unsigned CodeObjVer = getOrCheckAMDGPUCodeObjectVersion(D, Args); - CmdArgs.insert(CmdArgs.begin() + 1, - Args.MakeArgString(Twine("--amdhsa-code-object-version=") + - Twine(CodeObjVer))); - CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); + ArgStringList &CmdArgs, + bool IsCC1As = false) { + // If no version was requested by the user, use the default value from the + // back end. This is consistent with the value returned from + // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without + // requiring the corresponding llvm to have the AMDGPU target enabled, + // provided the user (e.g. front end tests) can use the default. + if (haveAMDGPUCodeObjectVersionArgument(D, Args)) { + unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args); + CmdArgs.insert(CmdArgs.begin() + 1, + Args.MakeArgString(Twine("--amdhsa-code-object-version=") + + Twine(CodeObjVer))); + CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); + // -cc1as does not accept -mcode-object-version option. + if (!IsCC1As) + CmdArgs.insert(CmdArgs.begin() + 1, + Args.MakeArgString(Twine("-mcode-object-version=") + + Twine(CodeObjVer))); + } +} + +static bool maybeHasClangPchSignature(const Driver &D, StringRef Path) { + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MemBuf = + D.getVFS().getBufferForFile(Path); + if (!MemBuf) + return false; + llvm::file_magic Magic = llvm::identify_magic((*MemBuf)->getBuffer()); + if (Magic == llvm::file_magic::unknown) + return false; + // Return true for both raw Clang AST files and object files which may + // contain a __clangast section. + if (Magic == llvm::file_magic::clang_ast) + return true; + Expected<std::unique_ptr<llvm::object::ObjectFile>> Obj = + llvm::object::ObjectFile::createObjectFile(**MemBuf, Magic); + return !Obj.takeError(); +} + +static bool gchProbe(const Driver &D, StringRef Path) { + llvm::ErrorOr<llvm::vfs::Status> Status = D.getVFS().status(Path); + if (!Status) + return false; + + if (Status->isDirectory()) { + std::error_code EC; + for (llvm::vfs::directory_iterator DI = D.getVFS().dir_begin(Path, EC), DE; + !EC && DI != DE; DI = DI.increment(EC)) { + if (maybeHasClangPchSignature(D, DI->path())) + return true; + } + D.Diag(diag::warn_drv_pch_ignoring_gch_dir) << Path; + return false; + } + + if (maybeHasClangPchSignature(D, Path)) + return true; + D.Diag(diag::warn_drv_pch_ignoring_gch_file) << Path; + return false; } void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, @@ -1141,7 +1020,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, } else { CmdArgs.push_back("-MT"); SmallString<128> Quoted; - QuoteTarget(A->getValue(), Quoted); + quoteMakeTarget(A->getValue(), Quoted); CmdArgs.push_back(Args.MakeArgString(Quoted)); } } @@ -1152,7 +1031,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, // If user provided -o, that is the dependency target, except // when we are only generating a dependency file. - Arg *OutputOpt = Args.getLastArg(options::OPT_o); + Arg *OutputOpt = Args.getLastArg(options::OPT_o, options::OPT__SLASH_Fo); if (OutputOpt && Output.getType() != types::TY_Dependencies) { DepTarget = OutputOpt->getValue(); } else { @@ -1166,7 +1045,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, CmdArgs.push_back("-MT"); SmallString<128> Quoted; - QuoteTarget(DepTarget, Quoted); + quoteMakeTarget(DepTarget, Quoted); CmdArgs.push_back(Args.MakeArgString(Quoted)); } @@ -1201,7 +1080,10 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, // If we are offloading to a target via OpenMP we need to include the // openmp_wrappers folder which contains alternative system headers. if (JA.isDeviceOffloading(Action::OFK_OpenMP) && - getToolChain().getTriple().isNVPTX()){ + !Args.hasArg(options::OPT_nostdinc) && + !Args.hasArg(options::OPT_nogpuinc) && + (getToolChain().getTriple().isNVPTX() || + getToolChain().getTriple().isAMDGCN())) { if (!Args.hasArg(options::OPT_nobuiltininc)) { // Add openmp_wrappers/* to our system include path. This lets us wrap // standard library headers. @@ -1256,7 +1138,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, bool RenderedImplicitInclude = false; for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) { - if (A->getOption().matches(options::OPT_include)) { + if (A->getOption().matches(options::OPT_include) && + D.getProbePrecompiled()) { // Handling of gcc-style gch precompiled headers. bool IsFirstImplicitInclude = !RenderedImplicitInclude; RenderedImplicitInclude = true; @@ -1267,14 +1150,13 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, // so that replace_extension does the right thing. P += ".dummy"; llvm::sys::path::replace_extension(P, "pch"); - if (llvm::sys::fs::exists(P)) + if (D.getVFS().exists(P)) FoundPCH = true; if (!FoundPCH) { + // For GCC compat, probe for a file or directory ending in .gch instead. llvm::sys::path::replace_extension(P, "gch"); - if (llvm::sys::fs::exists(P)) { - FoundPCH = true; - } + FoundPCH = gchProbe(D, P.str()); } if (FoundPCH) { @@ -1299,6 +1181,9 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) { // Translated to -internal-isystem by the driver, no need to pass to cc1. continue; + } else if (A->getOption().matches(options::OPT_ibuiltininc)) { + // This is used only by the driver. No need to pass to cc1. + continue; } // Not translated, render as usual. @@ -1306,9 +1191,10 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, A->render(Args, CmdArgs); } - Args.AddAllArgs(CmdArgs, + Args.addAllArgs(CmdArgs, {options::OPT_D, options::OPT_U, options::OPT_I_Group, - options::OPT_F, options::OPT_index_header_map}); + options::OPT_F, options::OPT_index_header_map, + options::OPT_embed_dir_EQ}); // Add -Wp, and -Xpreprocessor if using the preprocessor. @@ -1363,6 +1249,35 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, }); } + // If we are compiling for a GPU target we want to override the system headers + // with ones created by the 'libc' project if present. + // TODO: This should be moved to `AddClangSystemIncludeArgs` by passing the + // OffloadKind as an argument. + if (!Args.hasArg(options::OPT_nostdinc) && + !Args.hasArg(options::OPT_nogpuinc) && + !Args.hasArg(options::OPT_nobuiltininc)) { + // Without an offloading language we will include these headers directly. + // Offloading languages will instead only use the declarations stored in + // the resource directory at clang/lib/Headers/llvm_libc_wrappers. + if ((getToolChain().getTriple().isNVPTX() || + getToolChain().getTriple().isAMDGCN()) && + C.getActiveOffloadKinds() == Action::OFK_None) { + SmallString<128> P(llvm::sys::path::parent_path(D.Dir)); + llvm::sys::path::append(P, "include"); + llvm::sys::path::append(P, getToolChain().getTripleString()); + CmdArgs.push_back("-internal-isystem"); + CmdArgs.push_back(Args.MakeArgString(P)); + } else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) { + // TODO: CUDA / HIP include their own headers for some common functions + // implemented here. We'll need to clean those up so they do not conflict. + SmallString<128> P(D.ResourceDir); + llvm::sys::path::append(P, "include"); + llvm::sys::path::append(P, "llvm_libc_wrappers"); + CmdArgs.push_back("-internal-isystem"); + CmdArgs.push_back(Args.MakeArgString(P)); + } + } + // Add system include arguments for all targets but IAMCU. if (!IsIAMCU) forAllAssociatedToolChains(C, JA, getToolChain(), @@ -1375,7 +1290,18 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, } addMacroPrefixMapArg(D, Args, CmdArgs); - addProfilePrefixMapArg(D, Args, CmdArgs); + addCoveragePrefixMapArg(D, Args, CmdArgs); + + Args.AddLastArg(CmdArgs, options::OPT_ffile_reproducible, + options::OPT_fno_file_reproducible); + + if (const char *Epoch = std::getenv("SOURCE_DATE_EPOCH")) { + CmdArgs.push_back("-source-date-epoch"); + CmdArgs.push_back(Args.MakeArgString(Epoch)); + } + + Args.addOptInFlag(CmdArgs, options::OPT_fdefine_target_os_macros, + options::OPT_fno_define_target_os_macros); } // FIXME: Move to target hook. @@ -1522,9 +1448,18 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, } } +void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) { + if (!Args.hasFlag(options::OPT_faapcs_bitfield_width, + options::OPT_fno_aapcs_bitfield_width, true)) + CmdArgs.push_back("-fno-aapcs-bitfield-width"); + + if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad)) + CmdArgs.push_back("-faapcs-bitfield-load"); +} + namespace { -void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args, - ArgStringList &CmdArgs) { +void RenderARMABI(const Driver &D, const llvm::Triple &Triple, + const ArgList &Args, ArgStringList &CmdArgs) { // Select the ABI to use. // FIXME: Support -meabi. // FIXME: Parts of this are duplicated in the backend, unify this somehow. @@ -1532,18 +1467,155 @@ void RenderARMABI(const llvm::Triple &Triple, const ArgList &Args, if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { ABIName = A->getValue(); } else { - std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false); + std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data(); } CmdArgs.push_back("-target-abi"); CmdArgs.push_back(ABIName); } + +void AddUnalignedAccessWarning(ArgStringList &CmdArgs) { + auto StrictAlignIter = + llvm::find_if(llvm::reverse(CmdArgs), [](StringRef Arg) { + return Arg == "+strict-align" || Arg == "-strict-align"; + }); + if (StrictAlignIter != CmdArgs.rend() && + StringRef(*StrictAlignIter) == "+strict-align") + CmdArgs.push_back("-Wunaligned-access"); +} +} + +// Each combination of options here forms a signing schema, and in most cases +// each signing schema is its own incompatible ABI. The default values of the +// options represent the default signing schema. +static void handlePAuthABI(const ArgList &DriverArgs, ArgStringList &CC1Args) { + if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics, + options::OPT_fno_ptrauth_intrinsics)) + CC1Args.push_back("-fptrauth-intrinsics"); + + if (!DriverArgs.hasArg(options::OPT_fptrauth_calls, + options::OPT_fno_ptrauth_calls)) + CC1Args.push_back("-fptrauth-calls"); + + if (!DriverArgs.hasArg(options::OPT_fptrauth_returns, + options::OPT_fno_ptrauth_returns)) + CC1Args.push_back("-fptrauth-returns"); + + if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps, + options::OPT_fno_ptrauth_auth_traps)) + CC1Args.push_back("-fptrauth-auth-traps"); + + if (!DriverArgs.hasArg( + options::OPT_fptrauth_vtable_pointer_address_discrimination, + options::OPT_fno_ptrauth_vtable_pointer_address_discrimination)) + CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination"); + + if (!DriverArgs.hasArg( + options::OPT_fptrauth_vtable_pointer_type_discrimination, + options::OPT_fno_ptrauth_vtable_pointer_type_discrimination)) + CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination"); + + if (!DriverArgs.hasArg(options::OPT_fptrauth_indirect_gotos, + options::OPT_fno_ptrauth_indirect_gotos)) + CC1Args.push_back("-fptrauth-indirect-gotos"); + + if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini, + options::OPT_fno_ptrauth_init_fini)) + CC1Args.push_back("-fptrauth-init-fini"); +} + +static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, + ArgStringList &CmdArgs, bool isAArch64) { + const Arg *A = isAArch64 + ? Args.getLastArg(options::OPT_msign_return_address_EQ, + options::OPT_mbranch_protection_EQ) + : Args.getLastArg(options::OPT_mbranch_protection_EQ); + if (!A) + return; + + const Driver &D = TC.getDriver(); + const llvm::Triple &Triple = TC.getEffectiveTriple(); + if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass()))) + D.Diag(diag::warn_incompatible_branch_protection_option) + << Triple.getArchName(); + + StringRef Scope, Key; + bool IndirectBranches, BranchProtectionPAuthLR, GuardedControlStack; + + if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { + Scope = A->getValue(); + if (Scope != "none" && Scope != "non-leaf" && Scope != "all") + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << Scope; + Key = "a_key"; + IndirectBranches = false; + BranchProtectionPAuthLR = false; + GuardedControlStack = false; + } else { + StringRef DiagMsg; + llvm::ARM::ParsedBranchProtection PBP; + bool EnablePAuthLR = false; + + // To know if we need to enable PAuth-LR As part of the standard branch + // protection option, it needs to be determined if the feature has been + // activated in the `march` argument. This information is stored within the + // CmdArgs variable and can be found using a search. + if (isAArch64) { + auto isPAuthLR = [](const char *member) { + llvm::AArch64::ExtensionInfo pauthlr_extension = + llvm::AArch64::getExtensionByID(llvm::AArch64::AEK_PAUTHLR); + return pauthlr_extension.PosTargetFeature == member; + }; + + if (std::any_of(CmdArgs.begin(), CmdArgs.end(), isPAuthLR)) + EnablePAuthLR = true; + } + if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg, + EnablePAuthLR)) + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << DiagMsg; + if (!isAArch64 && PBP.Key == "b_key") + D.Diag(diag::warn_unsupported_branch_protection) + << "b-key" << A->getAsString(Args); + Scope = PBP.Scope; + Key = PBP.Key; + BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR; + IndirectBranches = PBP.BranchTargetEnforcement; + GuardedControlStack = PBP.GuardedControlStack; + } + + CmdArgs.push_back( + Args.MakeArgString(Twine("-msign-return-address=") + Scope)); + if (Scope != "none") { + if (Triple.getEnvironment() == llvm::Triple::PAuthTest) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << Triple.getTriple(); + CmdArgs.push_back( + Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); + } + if (BranchProtectionPAuthLR) { + if (Triple.getEnvironment() == llvm::Triple::PAuthTest) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << Triple.getTriple(); + CmdArgs.push_back( + Args.MakeArgString(Twine("-mbranch-protection-pauth-lr"))); + } + if (IndirectBranches) + CmdArgs.push_back("-mbranch-target-enforce"); + // GCS is currently untested with PAuthABI, but enabling this could be allowed + // in future after testing with a suitable system. + if (GuardedControlStack) { + if (Triple.getEnvironment() == llvm::Triple::PAuthTest) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << Triple.getTriple(); + CmdArgs.push_back("-mguarded-control-stack"); + } } void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, ArgStringList &CmdArgs, bool KernelOrKext) const { - RenderARMABI(Triple, Args, CmdArgs); + RenderARMABI(getToolChain().getDriver(), Triple, Args, CmdArgs); // Determine floating point ABI from the options & target defaults. arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); @@ -1580,6 +1652,13 @@ void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, if (Args.getLastArg(options::OPT_mcmse)) CmdArgs.push_back("-mcmse"); + + AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); + + // Enable/disable return address signing and indirect branch targets. + CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); + + AddUnalignedAccessWarning(CmdArgs); } void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, @@ -1601,14 +1680,17 @@ void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, case llvm::Triple::thumbeb: // Use the effective triple, which takes into account the deployment target. AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext); - CmdArgs.push_back("-fallow-half-arguments-and-returns"); break; case llvm::Triple::aarch64: case llvm::Triple::aarch64_32: case llvm::Triple::aarch64_be: AddAArch64TargetArgs(Args, CmdArgs); - CmdArgs.push_back("-fallow-half-arguments-and-returns"); + break; + + case llvm::Triple::loongarch32: + case llvm::Triple::loongarch64: + AddLoongArchTargetArgs(Args, CmdArgs); break; case llvm::Triple::mips: @@ -1672,6 +1754,8 @@ void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args, ABIName = A->getValue(); else if (Triple.isOSDarwin()) ABIName = "darwinpcs"; + else if (Triple.getEnvironment() == llvm::Triple::PAuthTest) + ABIName = "pauthtest"; else ABIName = "aapcs"; @@ -1695,19 +1779,6 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args, RenderAArch64ABI(Triple, Args, CmdArgs); - if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769, - options::OPT_mno_fix_cortex_a53_835769)) { - CmdArgs.push_back("-mllvm"); - if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769)) - CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1"); - else - CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0"); - } else if (Triple.isAndroid()) { - // Enabled A53 errata (835769) workaround by default on android - CmdArgs.push_back("-mllvm"); - CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1"); - } - // Forward the -mglobal-merge option for explicit control over the pass. if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, options::OPT_mno_global_merge)) { @@ -1719,54 +1790,91 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args, } // Enable/disable return address signing and indirect branch targets. - if (Arg *A = Args.getLastArg(options::OPT_msign_return_address_EQ, - options::OPT_mbranch_protection_EQ)) { - - const Driver &D = getToolChain().getDriver(); - - StringRef Scope, Key; - bool IndirectBranches; - - if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { - Scope = A->getValue(); - if (!Scope.equals("none") && !Scope.equals("non-leaf") && - !Scope.equals("all")) - D.Diag(diag::err_invalid_branch_protection) - << Scope << A->getAsString(Args); - Key = "a_key"; - IndirectBranches = false; - } else { - StringRef Err; - llvm::AArch64::ParsedBranchProtection PBP; - if (!llvm::AArch64::parseBranchProtection(A->getValue(), PBP, Err)) - D.Diag(diag::err_invalid_branch_protection) - << Err << A->getAsString(Args); - Scope = PBP.Scope; - Key = PBP.Key; - IndirectBranches = PBP.BranchTargetEnforcement; - } + CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, true /*isAArch64*/); - CmdArgs.push_back( - Args.MakeArgString(Twine("-msign-return-address=") + Scope)); - CmdArgs.push_back( - Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); - if (IndirectBranches) - CmdArgs.push_back("-mbranch-target-enforce"); - } + if (Triple.getEnvironment() == llvm::Triple::PAuthTest) + handlePAuthABI(Args, CmdArgs); // Handle -msve_vector_bits=<bits> if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) { StringRef Val = A->getValue(); const Driver &D = getToolChain().getDriver(); - if (Val.equals("128") || Val.equals("256") || Val.equals("512") || - Val.equals("1024") || Val.equals("2048")) + if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" || + Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" || + Val == "1024+" || Val == "2048+") { + unsigned Bits = 0; + if (!Val.consume_back("+")) { + bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; + assert(!Invalid && "Failed to parse value"); + CmdArgs.push_back( + Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128))); + } + + bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; + assert(!Invalid && "Failed to parse value"); CmdArgs.push_back( - Args.MakeArgString(llvm::Twine("-msve-vector-bits=") + Val)); + Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128))); // Silently drop requests for vector-length agnostic code as it's implied. - else if (!Val.equals("scalable")) + } else if (Val != "scalable") // Handle the unsupported values passed to msve-vector-bits. D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; + << A->getSpelling() << Val; + } + + AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); + + if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { + CmdArgs.push_back("-tune-cpu"); + if (strcmp(A->getValue(), "native") == 0) + CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); + else + CmdArgs.push_back(A->getValue()); + } + + AddUnalignedAccessWarning(CmdArgs); + + Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_intrinsics, + options::OPT_fno_ptrauth_intrinsics); + Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_calls, + options::OPT_fno_ptrauth_calls); + Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_returns, + options::OPT_fno_ptrauth_returns); + Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_auth_traps, + options::OPT_fno_ptrauth_auth_traps); + Args.addOptInFlag( + CmdArgs, options::OPT_fptrauth_vtable_pointer_address_discrimination, + options::OPT_fno_ptrauth_vtable_pointer_address_discrimination); + Args.addOptInFlag( + CmdArgs, options::OPT_fptrauth_vtable_pointer_type_discrimination, + options::OPT_fno_ptrauth_vtable_pointer_type_discrimination); + Args.addOptInFlag( + CmdArgs, options::OPT_fptrauth_type_info_vtable_pointer_discrimination, + options::OPT_fno_ptrauth_type_info_vtable_pointer_discrimination); + Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini, + options::OPT_fno_ptrauth_init_fini); + Args.addOptInFlag( + CmdArgs, options::OPT_fptrauth_function_pointer_type_discrimination, + options::OPT_fno_ptrauth_function_pointer_type_discrimination); + + Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_indirect_gotos, + options::OPT_fno_ptrauth_indirect_gotos); +} + +void Clang::AddLoongArchTargetArgs(const ArgList &Args, + ArgStringList &CmdArgs) const { + const llvm::Triple &Triple = getToolChain().getTriple(); + + CmdArgs.push_back("-target-abi"); + CmdArgs.push_back( + loongarch::getLoongArchABI(getToolChain().getDriver(), Args, Triple) + .data()); + + // Handle -mtune. + if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { + std::string TuneCPU = A->getValue(); + TuneCPU = loongarch::postProcessTargetCPUString(TuneCPU, Triple); + CmdArgs.push_back("-tune-cpu"); + CmdArgs.push_back(Args.MakeArgString(TuneCPU)); } } @@ -1810,6 +1918,11 @@ void Clang::AddMIPSTargetArgs(const ArgList &Args, } } + if (Args.getLastArg(options::OPT_mfix4300)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("-mfix4300"); + } + if (Arg *A = Args.getLastArg(options::OPT_G)) { StringRef v = A->getValue(); CmdArgs.push_back("-mllvm"); @@ -1899,7 +2012,7 @@ void Clang::AddMIPSTargetArgs(const ArgList &Args, CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val)); } else D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; + << A->getSpelling() << Val; } else D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName; } @@ -1915,14 +2028,20 @@ void Clang::AddMIPSTargetArgs(const ArgList &Args, void Clang::AddPPCTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { + const Driver &D = getToolChain().getDriver(); + const llvm::Triple &T = getToolChain().getTriple(); + if (Args.getLastArg(options::OPT_mtune_EQ)) { + CmdArgs.push_back("-tune-cpu"); + std::string CPU = ppc::getPPCTuneCPU(Args, T); + CmdArgs.push_back(Args.MakeArgString(CPU)); + } + // Select the ABI to use. const char *ABIName = nullptr; - const llvm::Triple &T = getToolChain().getTriple(); if (T.isOSBinFormatELF()) { switch (getToolChain().getArch()) { case llvm::Triple::ppc64: { - if ((T.isOSFreeBSD() && T.getOSMajorVersion() >= 13) || - T.isOSOpenBSD() || T.isMusl()) + if (T.isPPC64ELFv2ABI()) ABIName = "elfv2"; else ABIName = "elfv1"; @@ -1936,14 +2055,29 @@ void Clang::AddPPCTargetArgs(const ArgList &Args, } } - bool IEEELongDouble = false; + bool IEEELongDouble = getToolChain().defaultToIEEELongDouble(); + bool VecExtabi = false; for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) { StringRef V = A->getValue(); - if (V == "ieeelongdouble") + if (V == "ieeelongdouble") { IEEELongDouble = true; - else if (V == "ibmlongdouble") + A->claim(); + } else if (V == "ibmlongdouble") { IEEELongDouble = false; - else if (V != "altivec") + A->claim(); + } else if (V == "vec-default") { + VecExtabi = false; + A->claim(); + } else if (V == "vec-extabi") { + VecExtabi = true; + A->claim(); + } else if (V == "elfv1") { + ABIName = "elfv1"; + A->claim(); + } else if (V == "elfv2") { + ABIName = "elfv2"; + A->claim(); + } else if (V != "altivec") // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore // the option if given as we don't have backend support for any targets // that don't use the altivec abi. @@ -1951,10 +2085,14 @@ void Clang::AddPPCTargetArgs(const ArgList &Args, } if (IEEELongDouble) CmdArgs.push_back("-mabi=ieeelongdouble"); + if (VecExtabi) { + if (!T.isOSAIX()) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << "-mabi=vec-extabi" << T.str(); + CmdArgs.push_back("-mabi=vec-extabi"); + } - ppc::FloatABI FloatABI = - ppc::getPPCFloatABI(getToolChain().getDriver(), Args); - + ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args); if (FloatABI == ppc::FloatABI::Soft) { // Floating point operations and argument passing are soft. CmdArgs.push_back("-msoft-float"); @@ -1988,13 +2126,19 @@ static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args, D.Diag(diag::warn_drv_unsupported_sdata); } } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ) - .equals_lower("large") && + .equals_insensitive("large") && (Triple.getArch() == llvm::Triple::riscv64)) { // Not support linker relaxation for RV64 with large code model. SmallDataLimit = "0"; if (Args.hasArg(options::OPT_G)) { D.Diag(diag::warn_drv_unsupported_sdata); } + } else if (Triple.isAndroid()) { + // GP relaxation is not supported on Android. + SmallDataLimit = "0"; + if (Args.hasArg(options::OPT_G)) { + D.Diag(diag::warn_drv_unsupported_sdata); + } } else if (Arg *A = Args.getLastArg(options::OPT_G)) { SmallDataLimit = A->getValue(); } @@ -2013,18 +2157,57 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args, SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs); - std::string TuneCPU; - - if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { - StringRef Name = A->getValue(); + if (!Args.hasFlag(options::OPT_mimplicit_float, + options::OPT_mno_implicit_float, true)) + CmdArgs.push_back("-no-implicit-float"); - Name = llvm::RISCV::resolveTuneCPUAlias(Name, Triple.isArch64Bit()); - TuneCPU = std::string(Name); + if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { + CmdArgs.push_back("-tune-cpu"); + if (strcmp(A->getValue(), "native") == 0) + CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); + else + CmdArgs.push_back(A->getValue()); } - if (!TuneCPU.empty()) { - CmdArgs.push_back("-tune-cpu"); - CmdArgs.push_back(Args.MakeArgString(TuneCPU)); + // Handle -mrvv-vector-bits=<bits> + if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) { + StringRef Val = A->getValue(); + const Driver &D = getToolChain().getDriver(); + + // Get minimum VLen from march. + unsigned MinVLen = 0; + std::string Arch = riscv::getRISCVArch(Args, Triple); + auto ISAInfo = llvm::RISCVISAInfo::parseArchString( + Arch, /*EnableExperimentalExtensions*/ true); + // Ignore parsing error. + if (!errorToBool(ISAInfo.takeError())) + MinVLen = (*ISAInfo)->getMinVLen(); + + // If the value is "zvl", use MinVLen from march. Otherwise, try to parse + // as integer as long as we have a MinVLen. + unsigned Bits = 0; + if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) { + Bits = MinVLen; + } else if (!Val.getAsInteger(10, Bits)) { + // Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that + // at least MinVLen. + if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock || + Bits > 65536 || !llvm::isPowerOf2_32(Bits)) + Bits = 0; + } + + // If we got a valid value try to use it. + if (Bits != 0) { + unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock; + CmdArgs.push_back( + Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin))); + CmdArgs.push_back( + Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin))); + } else if (Val != "scalable") { + // Handle the unsupported values passed to mrvv-vector-bits. + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << Val; + } } } @@ -2044,12 +2227,32 @@ void Clang::AddSparcTargetArgs(const ArgList &Args, CmdArgs.push_back("-mfloat-abi"); CmdArgs.push_back("hard"); } + + if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { + StringRef Name = A->getValue(); + std::string TuneCPU; + if (Name == "native") + TuneCPU = std::string(llvm::sys::getHostCPUName()); + else + TuneCPU = std::string(Name); + + CmdArgs.push_back("-tune-cpu"); + CmdArgs.push_back(Args.MakeArgString(TuneCPU)); + } } void Clang::AddSystemZTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { - bool HasBackchain = Args.hasFlag(options::OPT_mbackchain, - options::OPT_mno_backchain, false); + if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { + CmdArgs.push_back("-tune-cpu"); + if (strcmp(A->getValue(), "native") == 0) + CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); + else + CmdArgs.push_back(A->getValue()); + } + + bool HasBackchain = + Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false); bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack, options::OPT_mno_packed_stack, false); systemz::FloatABI FloatABI = @@ -2105,15 +2308,21 @@ void Clang::AddX86TargetArgs(const ArgList &Args, if (Value == "intel" || Value == "att") { CmdArgs.push_back("-mllvm"); CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); + CmdArgs.push_back(Args.MakeArgString("-inline-asm=" + Value)); } else { D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Value; + << A->getSpelling() << Value; } } else if (D.IsCLMode()) { CmdArgs.push_back("-mllvm"); CmdArgs.push_back("-x86-asm-syntax=intel"); } + if (Arg *A = Args.getLastArg(options::OPT_mskip_rax_setup, + options::OPT_mno_skip_rax_setup)) + if (A->getOption().matches(options::OPT_mskip_rax_setup)) + CmdArgs.push_back(Args.MakeArgString("-mskip-rax-setup")); + // Set flags to support MCU ABI. if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { CmdArgs.push_back("-mfloat-abi"); @@ -2123,10 +2332,10 @@ void Clang::AddX86TargetArgs(const ArgList &Args, // Handle -mtune. - // Default to "generic" unless -march is present or targetting the PS4. + // Default to "generic" unless -march is present or targetting the PS4/PS5. std::string TuneCPU; if (!Args.hasArg(clang::driver::options::OPT_march_EQ) && - !getToolChain().getTriple().isPS4CPU()) + !getToolChain().getTriple().isPS()) TuneCPU = "generic"; // Override based on -mtune. @@ -2154,8 +2363,8 @@ void Clang::AddHexagonTargetArgs(const ArgList &Args, if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { CmdArgs.push_back("-mllvm"); - CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" + - Twine(G.getValue()))); + CmdArgs.push_back( + Args.MakeArgString("-hexagon-small-data-threshold=" + Twine(*G))); } if (!Args.hasArg(options::OPT_fno_short_enums)) @@ -2185,7 +2394,7 @@ void Clang::AddLanaiTargetArgs(const ArgList &Args, if (Mregparm != 4) { getToolChain().getDriver().Diag( diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Value; + << A->getSpelling() << Value; } } } @@ -2195,10 +2404,8 @@ void Clang::AddWebAssemblyTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { // Default to "hidden" visibility. if (!Args.hasArg(options::OPT_fvisibility_EQ, - options::OPT_fvisibility_ms_compat)) { - CmdArgs.push_back("-fvisibility"); - CmdArgs.push_back("hidden"); - } + options::OPT_fvisibility_ms_compat)) + CmdArgs.push_back("-fvisibility=hidden"); } void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { @@ -2219,8 +2426,9 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, if (!CompilationDatabase) { std::error_code EC; - auto File = std::make_unique<llvm::raw_fd_ostream>(Filename, EC, - llvm::sys::fs::OF_Text); + auto File = std::make_unique<llvm::raw_fd_ostream>( + Filename, EC, + llvm::sys::fs::OF_TextWithCRLF | llvm::sys::fs::OF_Append); if (EC) { D.Diag(clang::diag::err_drv_compilationdatabase) << Filename << EC.message(); @@ -2234,7 +2442,8 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, CWD = "."; CDB << "{ \"directory\": \"" << escape(*CWD) << "\""; CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\""; - CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; + if (Output.isFilename()) + CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\""; SmallString<128> Buf; Buf = "-x"; @@ -2246,6 +2455,8 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, CDB << ", \"" << escape(Buf) << "\""; } CDB << ", \"" << escape(Input.getFilename()) << "\""; + if (Output.isFilename()) + CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\""; for (auto &A: Args) { auto &O = A->getOption(); // Skip language selection, which is positional. @@ -2259,6 +2470,9 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, // Skip inputs. if (O.getKind() == Option::InputClass) continue; + // Skip output. + if (O.getID() == options::OPT_o) + continue; // All other arguments are quoted and appended. ArgStringList ASL; A->render(Args, ASL); @@ -2294,7 +2508,8 @@ void Clang::DumpCompilationDatabaseFragmentToDir( Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json"); int FD; SmallString<256> TempPath; - Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath); + Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath, + llvm::sys::fs::OF_Text); if (Err) { Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message(); return; @@ -2304,12 +2519,31 @@ void Clang::DumpCompilationDatabaseFragmentToDir( DumpCompilationDatabase(C, "", Target, Output, Input, Args); } +static bool CheckARMImplicitITArg(StringRef Value) { + return Value == "always" || Value == "never" || Value == "arm" || + Value == "thumb"; +} + +static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs, + StringRef Value) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); +} + static void CollectArgsForIntegratedAssembler(Compilation &C, const ArgList &Args, ArgStringList &CmdArgs, const Driver &D) { - if (UseRelaxAll(C, Args)) - CmdArgs.push_back("-mrelax-all"); + // Default to -mno-relax-all. + // + // Note: RISC-V requires an indirect jump for offsets larger than 1MiB. This + // cannot be done by assembler branch relaxation as it needs a free temporary + // register. Because of this, branch relaxation is handled by a MachineIR pass + // before the assembler. Forcing assembler branch relaxation for -O0 makes the + // MachineIR branch relaxation inaccurate and it will miss cases where an + // indirect branch is necessary. + Args.addOptInFlag(CmdArgs, options::OPT_mrelax_all, + options::OPT_mno_relax_all); // Only default to -mincremental-linker-compatible if we think we are // targeting the MSVC linker. @@ -2320,26 +2554,10 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, DefaultIncrementalLinkerCompatible)) CmdArgs.push_back("-mincremental-linker-compatible"); - switch (C.getDefaultToolChain().getArch()) { - case llvm::Triple::arm: - case llvm::Triple::armeb: - case llvm::Triple::thumb: - case llvm::Triple::thumbeb: - if (Arg *A = Args.getLastArg(options::OPT_mimplicit_it_EQ)) { - StringRef Value = A->getValue(); - if (Value == "always" || Value == "never" || Value == "arm" || - Value == "thumb") { - CmdArgs.push_back("-mllvm"); - CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); - } else { - D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Value; - } - } - break; - default: - break; - } + Args.AddLastArg(CmdArgs, options::OPT_femit_dwarf_unwind_EQ); + + Args.addOptInFlag(CmdArgs, options::OPT_femit_compact_unwind_non_canonical, + options::OPT_fno_emit_compact_unwind_non_canonical); // If you add more args here, also add them to the block below that // starts with "// If CollectArgsForIntegratedAssembler() isn't called below". @@ -2351,13 +2569,34 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, // arg after parsing the '-I' arg. bool TakeNextArg = false; + const llvm::Triple &Triple = C.getDefaultToolChain().getTriple(); + bool Crel = false, ExperimentalCrel = false; bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations(); - bool UseNoExecStack = C.getDefaultToolChain().isNoExecStackDefault(); + bool UseNoExecStack = false; const char *MipsTargetFeature = nullptr; + StringRef ImplicitIt; for (const Arg *A : - Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) { + Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler, + options::OPT_mimplicit_it_EQ)) { A->claim(); + if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) { + switch (C.getDefaultToolChain().getArch()) { + case llvm::Triple::arm: + case llvm::Triple::armeb: + case llvm::Triple::thumb: + case llvm::Triple::thumbeb: + // Only store the value; the last value set takes effect. + ImplicitIt = A->getValue(); + if (!CheckARMImplicitITArg(ImplicitIt)) + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << ImplicitIt; + continue; + default: + break; + } + } + for (StringRef Value : A->getValues()) { if (TakeNextArg) { CmdArgs.push_back(Value.data()); @@ -2372,10 +2611,30 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, switch (C.getDefaultToolChain().getArch()) { default: break; + case llvm::Triple::x86: + case llvm::Triple::x86_64: + if (Value == "-msse2avx") { + CmdArgs.push_back("-msse2avx"); + continue; + } + break; + case llvm::Triple::wasm32: + case llvm::Triple::wasm64: + if (Value == "--no-type-check") { + CmdArgs.push_back("-mno-type-check"); + continue; + } + break; case llvm::Triple::thumb: case llvm::Triple::thumbeb: case llvm::Triple::arm: case llvm::Triple::armeb: + if (Value.starts_with("-mimplicit-it=")) { + // Only store the value; the last value set takes effect. + ImplicitIt = Value.split("=").second; + if (CheckARMImplicitITArg(ImplicitIt)) + continue; + } if (Value == "-mthumb") // -mthumb has already been processed in ComputeLLVMTriple() // recognize but skip over here. @@ -2395,12 +2654,12 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, CmdArgs.push_back("-use-tcc-in-div"); continue; } - if (Value.startswith("-msoft-float")) { + if (Value.starts_with("-msoft-float")) { CmdArgs.push_back("-target-feature"); CmdArgs.push_back("+soft-float"); continue; } - if (Value.startswith("-mhard-float")) { + if (Value.starts_with("-mhard-float")) { CmdArgs.push_back("-target-feature"); CmdArgs.push_back("-soft-float"); continue; @@ -2437,57 +2696,63 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, CmdArgs.push_back("-massembler-no-warn"); } else if (Value == "--noexecstack") { UseNoExecStack = true; - } else if (Value.startswith("-compress-debug-sections") || - Value.startswith("--compress-debug-sections") || + } else if (Value.starts_with("-compress-debug-sections") || + Value.starts_with("--compress-debug-sections") || Value == "-nocompress-debug-sections" || Value == "--nocompress-debug-sections") { CmdArgs.push_back(Value.data()); + } else if (Value == "--crel") { + Crel = true; + } else if (Value == "--no-crel") { + Crel = false; + } else if (Value == "--allow-experimental-crel") { + ExperimentalCrel = true; } else if (Value == "-mrelax-relocations=yes" || Value == "--mrelax-relocations=yes") { UseRelaxRelocations = true; } else if (Value == "-mrelax-relocations=no" || Value == "--mrelax-relocations=no") { UseRelaxRelocations = false; - } else if (Value.startswith("-I")) { + } else if (Value.starts_with("-I")) { CmdArgs.push_back(Value.data()); // We need to consume the next argument if the current arg is a plain // -I. The next arg will be the include directory. if (Value == "-I") TakeNextArg = true; - } else if (Value.startswith("-gdwarf-")) { + } else if (Value.starts_with("-gdwarf-")) { // "-gdwarf-N" options are not cc1as options. unsigned DwarfVersion = DwarfVersionNum(Value); if (DwarfVersion == 0) { // Send it onward, and let cc1as complain. CmdArgs.push_back(Value.data()); } else { RenderDebugEnablingArgs(Args, CmdArgs, - codegenoptions::LimitedDebugInfo, + llvm::codegenoptions::DebugInfoConstructor, DwarfVersion, llvm::DebuggerKind::Default); } - } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") || - Value.startswith("-mhwdiv") || Value.startswith("-march")) { + } else if (Value.starts_with("-mcpu") || Value.starts_with("-mfpu") || + Value.starts_with("-mhwdiv") || Value.starts_with("-march")) { // Do nothing, we'll validate it later. - } else if (Value == "-defsym") { - if (A->getNumValues() != 2) { - D.Diag(diag::err_drv_defsym_invalid_format) << Value; - break; - } - const char *S = A->getValue(1); - auto Pair = StringRef(S).split('='); - auto Sym = Pair.first; - auto SVal = Pair.second; - - if (Sym.empty() || SVal.empty()) { - D.Diag(diag::err_drv_defsym_invalid_format) << S; - break; - } - int64_t IVal; - if (SVal.getAsInteger(0, IVal)) { - D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; - break; - } - CmdArgs.push_back(Value.data()); - TakeNextArg = true; + } else if (Value == "-defsym" || Value == "--defsym") { + if (A->getNumValues() != 2) { + D.Diag(diag::err_drv_defsym_invalid_format) << Value; + break; + } + const char *S = A->getValue(1); + auto Pair = StringRef(S).split('='); + auto Sym = Pair.first; + auto SVal = Pair.second; + + if (Sym.empty() || SVal.empty()) { + D.Diag(diag::err_drv_defsym_invalid_format) << S; + break; + } + int64_t IVal; + if (SVal.getAsInteger(0, IVal)) { + D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; + break; + } + CmdArgs.push_back("--defsym"); + TakeNextArg = true; } else if (Value == "-fdebug-compilation-dir") { CmdArgs.push_back("-fdebug-compilation-dir"); TakeNextArg = true; @@ -2497,14 +2762,28 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, // -fdebug-compilation-dir (without '=') here. CmdArgs.push_back("-fdebug-compilation-dir"); CmdArgs.push_back(Value.data()); + } else if (Value == "--version") { + D.PrintVersion(C, llvm::outs()); } else { D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Value; + << A->getSpelling() << Value; } } } - if (UseRelaxRelocations) - CmdArgs.push_back("--mrelax-relocations"); + if (ImplicitIt.size()) + AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt); + if (Crel) { + if (!ExperimentalCrel) + D.Diag(diag::err_drv_experimental_crel); + if (Triple.isOSBinFormatELF() && !Triple.isMIPS()) { + CmdArgs.push_back("--crel"); + } else { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << "-Wa,--crel" << D.getTargetTriple(); + } + } + if (!UseRelaxRelocations) + CmdArgs.push_back("-mrelax-relocations=no"); if (UseNoExecStack) CmdArgs.push_back("-mnoexecstack"); if (MipsTargetFeature != nullptr) { @@ -2516,6 +2795,51 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, if (C.getDriver().embedBitcodeEnabled() || C.getDriver().embedBitcodeMarkerOnly()) Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); + + if (const char *AsSecureLogFile = getenv("AS_SECURE_LOG_FILE")) { + CmdArgs.push_back("-as-secure-log-file"); + CmdArgs.push_back(Args.MakeArgString(AsSecureLogFile)); + } +} + +static std::string ComplexRangeKindToStr(LangOptions::ComplexRangeKind Range) { + switch (Range) { + case LangOptions::ComplexRangeKind::CX_Full: + return "full"; + break; + case LangOptions::ComplexRangeKind::CX_Basic: + return "basic"; + break; + case LangOptions::ComplexRangeKind::CX_Improved: + return "improved"; + break; + case LangOptions::ComplexRangeKind::CX_Promoted: + return "promoted"; + break; + default: + return ""; + } +} + +static std::string ComplexArithmeticStr(LangOptions::ComplexRangeKind Range) { + return (Range == LangOptions::ComplexRangeKind::CX_None) + ? "" + : "-fcomplex-arithmetic=" + ComplexRangeKindToStr(Range); +} + +static void EmitComplexRangeDiag(const Driver &D, std::string str1, + std::string str2) { + if ((str1.compare(str2) != 0) && !str2.empty() && !str1.empty()) { + D.Diag(clang::diag::warn_drv_overriding_option) << str1 << str2; + } +} + +static std::string +RenderComplexRangeOption(LangOptions::ComplexRangeKind Range) { + std::string ComplexRangeStr = ComplexRangeKindToStr(Range); + if (!ComplexRangeStr.empty()) + return "-complex-range=" + ComplexRangeStr; + return ComplexRangeStr; } static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, @@ -2529,6 +2853,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, // LLVM flags based on the final state. bool HonorINFs = true; bool HonorNaNs = true; + bool ApproxFunc = false; // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. bool MathErrno = TC.IsMathErrnoDefault(); bool AssociativeMath = false; @@ -2538,21 +2863,61 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, bool TrappingMathPresent = false; // Is trapping-math in args, and not // overriden by ffp-exception-behavior? bool RoundingFPMath = false; - bool RoundingMathPresent = false; // Is rounding-math in args? // -ffp-model values: strict, fast, precise StringRef FPModel = ""; // -ffp-exception-behavior options: strict, maytrap, ignore StringRef FPExceptionBehavior = ""; - const llvm::DenormalMode DefaultDenormalFPMath = + // -ffp-eval-method options: double, extended, source + StringRef FPEvalMethod = ""; + llvm::DenormalMode DenormalFPMath = TC.getDefaultDenormalModeForType(Args, JA); - const llvm::DenormalMode DefaultDenormalFP32Math = + llvm::DenormalMode DenormalFP32Math = TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle()); - llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath; - llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math; - StringRef FPContract = ""; + // CUDA and HIP don't rely on the frontend to pass an ffp-contract option. + // If one wasn't given by the user, don't pass it here. + StringRef FPContract; + StringRef LastSeenFfpContractOption; + bool SeenUnsafeMathModeOption = false; + if (!JA.isDeviceOffloading(Action::OFK_Cuda) && + !JA.isOffloading(Action::OFK_HIP)) + FPContract = "on"; bool StrictFPModel = false; - + StringRef Float16ExcessPrecision = ""; + StringRef BFloat16ExcessPrecision = ""; + LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_None; + std::string ComplexRangeStr = ""; + std::string GccRangeComplexOption = ""; + + // Lambda to set fast-math options. This is also used by -ffp-model=fast + auto applyFastMath = [&]() { + HonorINFs = false; + HonorNaNs = false; + MathErrno = false; + AssociativeMath = true; + ReciprocalMath = true; + ApproxFunc = true; + SignedZeros = false; + TrappingMath = false; + RoundingFPMath = false; + FPExceptionBehavior = ""; + // If fast-math is set then set the fp-contract mode to fast. + FPContract = "fast"; + // ffast-math enables basic range rules for complex multiplication and + // division. + // Warn if user expects to perform full implementation of complex + // multiplication or division in the presence of nan or ninf flags. + if (Range == LangOptions::ComplexRangeKind::CX_Full || + Range == LangOptions::ComplexRangeKind::CX_Improved || + Range == LangOptions::ComplexRangeKind::CX_Promoted) + EmitComplexRangeDiag( + D, ComplexArithmeticStr(Range), + !GccRangeComplexOption.empty() + ? GccRangeComplexOption + : ComplexArithmeticStr(LangOptions::ComplexRangeKind::CX_Basic)); + Range = LangOptions::ComplexRangeKind::CX_Basic; + SeenUnsafeMathModeOption = true; + }; if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { CmdArgs.push_back("-mlimit-float-precision"); @@ -2560,79 +2925,141 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, } for (const Arg *A : Args) { - auto optID = A->getOption().getID(); - bool PreciseFPModel = false; - switch (optID) { - default: + switch (A->getOption().getID()) { + // If this isn't an FP option skip the claim below + default: continue; + + case options::OPT_fcx_limited_range: + if (GccRangeComplexOption.empty()) { + if (Range != LangOptions::ComplexRangeKind::CX_Basic) + EmitComplexRangeDiag(D, RenderComplexRangeOption(Range), + "-fcx-limited-range"); + } else { + if (GccRangeComplexOption != "-fno-cx-limited-range") + EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-limited-range"); + } + GccRangeComplexOption = "-fcx-limited-range"; + Range = LangOptions::ComplexRangeKind::CX_Basic; break; + case options::OPT_fno_cx_limited_range: + if (GccRangeComplexOption.empty()) { + EmitComplexRangeDiag(D, RenderComplexRangeOption(Range), + "-fno-cx-limited-range"); + } else { + if (GccRangeComplexOption.compare("-fcx-limited-range") != 0 && + GccRangeComplexOption.compare("-fno-cx-fortran-rules") != 0) + EmitComplexRangeDiag(D, GccRangeComplexOption, + "-fno-cx-limited-range"); + } + GccRangeComplexOption = "-fno-cx-limited-range"; + Range = LangOptions::ComplexRangeKind::CX_Full; + break; + case options::OPT_fcx_fortran_rules: + if (GccRangeComplexOption.empty()) + EmitComplexRangeDiag(D, RenderComplexRangeOption(Range), + "-fcx-fortran-rules"); + else + EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-fortran-rules"); + GccRangeComplexOption = "-fcx-fortran-rules"; + Range = LangOptions::ComplexRangeKind::CX_Improved; + break; + case options::OPT_fno_cx_fortran_rules: + if (GccRangeComplexOption.empty()) { + EmitComplexRangeDiag(D, RenderComplexRangeOption(Range), + "-fno-cx-fortran-rules"); + } else { + if (GccRangeComplexOption != "-fno-cx-limited-range") + EmitComplexRangeDiag(D, GccRangeComplexOption, + "-fno-cx-fortran-rules"); + } + GccRangeComplexOption = "-fno-cx-fortran-rules"; + Range = LangOptions::ComplexRangeKind::CX_Full; + break; + case options::OPT_fcomplex_arithmetic_EQ: { + LangOptions::ComplexRangeKind RangeVal; + StringRef Val = A->getValue(); + if (Val == "full") + RangeVal = LangOptions::ComplexRangeKind::CX_Full; + else if (Val == "improved") + RangeVal = LangOptions::ComplexRangeKind::CX_Improved; + else if (Val == "promoted") + RangeVal = LangOptions::ComplexRangeKind::CX_Promoted; + else if (Val == "basic") + RangeVal = LangOptions::ComplexRangeKind::CX_Basic; + else { + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << Val; + break; + } + if (!GccRangeComplexOption.empty()) { + if (GccRangeComplexOption.compare("-fcx-limited-range") != 0) { + if (GccRangeComplexOption.compare("-fcx-fortran-rules") != 0) { + if (RangeVal != LangOptions::ComplexRangeKind::CX_Improved) + EmitComplexRangeDiag(D, GccRangeComplexOption, + ComplexArithmeticStr(RangeVal)); + } else { + EmitComplexRangeDiag(D, GccRangeComplexOption, + ComplexArithmeticStr(RangeVal)); + } + } else { + if (RangeVal != LangOptions::ComplexRangeKind::CX_Basic) + EmitComplexRangeDiag(D, GccRangeComplexOption, + ComplexArithmeticStr(RangeVal)); + } + } + Range = RangeVal; + break; + } case options::OPT_ffp_model_EQ: { // If -ffp-model= is seen, reset to fno-fast-math HonorINFs = true; HonorNaNs = true; + ApproxFunc = false; // Turning *off* -ffast-math restores the toolchain default. MathErrno = TC.IsMathErrnoDefault(); AssociativeMath = false; ReciprocalMath = false; SignedZeros = true; - // -fno_fast_math restores default denormal and fpcontract handling - FPContract = ""; - DenormalFPMath = llvm::DenormalMode::getIEEE(); - - // FIXME: The target may have picked a non-IEEE default mode here based on - // -cl-denorms-are-zero. Should the target consider -fp-model interaction? - DenormalFP32Math = llvm::DenormalMode::getIEEE(); + FPContract = "on"; StringRef Val = A->getValue(); - if (OFastEnabled && !Val.equals("fast")) { - // Only -ffp-model=fast is compatible with OFast, ignore. - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << Args.MakeArgString("-ffp-model=" + Val) - << "-Ofast"; + if (OFastEnabled && Val != "fast") { + // Only -ffp-model=fast is compatible with OFast, ignore. + D.Diag(clang::diag::warn_drv_overriding_option) + << Args.MakeArgString("-ffp-model=" + Val) << "-Ofast"; break; } StrictFPModel = false; - PreciseFPModel = true; - // ffp-model= is a Driver option, it is entirely rewritten into more - // granular options before being passed into cc1. - // Use the gcc option in the switch below. - if (!FPModel.empty() && !FPModel.equals(Val)) { - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << Args.MakeArgString("-ffp-model=" + FPModel) - << Args.MakeArgString("-ffp-model=" + Val); - FPContract = ""; - } - if (Val.equals("fast")) { - optID = options::OPT_ffast_math; + if (!FPModel.empty() && FPModel != Val) + D.Diag(clang::diag::warn_drv_overriding_option) + << Args.MakeArgString("-ffp-model=" + FPModel) + << Args.MakeArgString("-ffp-model=" + Val); + if (Val == "fast") { FPModel = Val; - FPContract = "fast"; - } else if (Val.equals("precise")) { - optID = options::OPT_ffp_contract; + applyFastMath(); + } else if (Val == "precise") { FPModel = Val; - FPContract = "fast"; - PreciseFPModel = true; - } else if (Val.equals("strict")) { + FPContract = "on"; + } else if (Val == "strict") { StrictFPModel = true; - optID = options::OPT_frounding_math; FPExceptionBehavior = "strict"; FPModel = Val; FPContract = "off"; TrappingMath = true; + RoundingFPMath = true; } else D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; + << A->getSpelling() << Val; break; - } } - switch (optID) { - // If this isn't an FP option skip the claim below - default: continue; - // Options controlling individual features case options::OPT_fhonor_infinities: HonorINFs = true; break; case options::OPT_fno_honor_infinities: HonorINFs = false; break; case options::OPT_fhonor_nans: HonorNaNs = true; break; case options::OPT_fno_honor_nans: HonorNaNs = false; break; + case options::OPT_fapprox_func: ApproxFunc = true; break; + case options::OPT_fno_approx_func: ApproxFunc = false; break; case options::OPT_fmath_errno: MathErrno = true; break; case options::OPT_fno_math_errno: MathErrno = false; break; case options::OPT_fassociative_math: AssociativeMath = true; break; @@ -2643,22 +3070,24 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, case options::OPT_fno_signed_zeros: SignedZeros = false; break; case options::OPT_ftrapping_math: if (!TrappingMathPresent && !FPExceptionBehavior.empty() && - !FPExceptionBehavior.equals("strict")) + FPExceptionBehavior != "strict") // Warn that previous value of option is overridden. - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) - << "-ftrapping-math"; + D.Diag(clang::diag::warn_drv_overriding_option) + << Args.MakeArgString("-ffp-exception-behavior=" + + FPExceptionBehavior) + << "-ftrapping-math"; TrappingMath = true; TrappingMathPresent = true; FPExceptionBehavior = "strict"; break; case options::OPT_fno_trapping_math: if (!TrappingMathPresent && !FPExceptionBehavior.empty() && - !FPExceptionBehavior.equals("ignore")) + FPExceptionBehavior != "ignore") // Warn that previous value of option is overridden. - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) - << "-fno-trapping-math"; + D.Diag(clang::diag::warn_drv_overriding_option) + << Args.MakeArgString("-ffp-exception-behavior=" + + FPExceptionBehavior) + << "-fno-trapping-math"; TrappingMath = false; TrappingMathPresent = true; FPExceptionBehavior = "ignore"; @@ -2666,16 +3095,15 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, case options::OPT_frounding_math: RoundingFPMath = true; - RoundingMathPresent = true; break; case options::OPT_fno_rounding_math: RoundingFPMath = false; - RoundingMathPresent = false; break; case options::OPT_fdenormal_fp_math_EQ: DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue()); + DenormalFP32Math = DenormalFPMath; if (!DenormalFPMath.isValid()) { D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << A->getValue(); @@ -2693,47 +3121,71 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, // Validate and pass through -ffp-contract option. case options::OPT_ffp_contract: { StringRef Val = A->getValue(); - if (PreciseFPModel) { - // -ffp-model=precise enables ffp-contract=fast as a side effect - // the FPContract value has already been set to a string literal - // and the Val string isn't a pertinent value. - ; - } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off")) + if (Val == "fast" || Val == "on" || Val == "off" || + Val == "fast-honor-pragmas") { FPContract = Val; - else + LastSeenFfpContractOption = Val; + } else D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; + << A->getSpelling() << Val; break; } - // Validate and pass through -ffp-model option. - case options::OPT_ffp_model_EQ: - // This should only occur in the error case - // since the optID has been replaced by a more granular - // floating point option. - break; - // Validate and pass through -ffp-exception-behavior option. case options::OPT_ffp_exception_behavior_EQ: { StringRef Val = A->getValue(); if (!TrappingMathPresent && !FPExceptionBehavior.empty() && - !FPExceptionBehavior.equals(Val)) + FPExceptionBehavior != Val) // Warn that previous value of option is overridden. - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) - << Args.MakeArgString("-ffp-exception-behavior=" + Val); + D.Diag(clang::diag::warn_drv_overriding_option) + << Args.MakeArgString("-ffp-exception-behavior=" + + FPExceptionBehavior) + << Args.MakeArgString("-ffp-exception-behavior=" + Val); TrappingMath = TrappingMathPresent = false; - if (Val.equals("ignore") || Val.equals("maytrap")) + if (Val == "ignore" || Val == "maytrap") FPExceptionBehavior = Val; - else if (Val.equals("strict")) { + else if (Val == "strict") { FPExceptionBehavior = Val; TrappingMath = TrappingMathPresent = true; } else D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; + << A->getSpelling() << Val; break; } + // Validate and pass through -ffp-eval-method option. + case options::OPT_ffp_eval_method_EQ: { + StringRef Val = A->getValue(); + if (Val == "double" || Val == "extended" || Val == "source") + FPEvalMethod = Val; + else + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << Val; + break; + } + + case options::OPT_fexcess_precision_EQ: { + StringRef Val = A->getValue(); + const llvm::Triple::ArchType Arch = TC.getArch(); + if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { + if (Val == "standard" || Val == "fast") + Float16ExcessPrecision = Val; + // To make it GCC compatible, allow the value of "16" which + // means disable excess precision, the same meaning than clang's + // equivalent value "none". + else if (Val == "16") + Float16ExcessPrecision = "none"; + else + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << Val; + } else { + if (!(Val == "standard" || Val == "fast")) + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << Val; + } + BFloat16ExcessPrecision = Float16ExcessPrecision; + break; + } case options::OPT_ffinite_math_only: HonorINFs = false; HonorNaNs = false; @@ -2747,38 +3199,36 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, AssociativeMath = true; ReciprocalMath = true; SignedZeros = false; + ApproxFunc = true; TrappingMath = false; FPExceptionBehavior = ""; + FPContract = "fast"; + SeenUnsafeMathModeOption = true; break; case options::OPT_fno_unsafe_math_optimizations: AssociativeMath = false; ReciprocalMath = false; SignedZeros = true; - TrappingMath = true; - FPExceptionBehavior = "strict"; - - // The target may have opted to flush by default, so force IEEE. - DenormalFPMath = llvm::DenormalMode::getIEEE(); - DenormalFP32Math = llvm::DenormalMode::getIEEE(); + ApproxFunc = false; + + if (!JA.isDeviceOffloading(Action::OFK_Cuda) && + !JA.isOffloading(Action::OFK_HIP)) { + if (LastSeenFfpContractOption != "") { + FPContract = LastSeenFfpContractOption; + } else if (SeenUnsafeMathModeOption) + FPContract = "on"; + } break; case options::OPT_Ofast: // If -Ofast is the optimization level, then -ffast-math should be enabled if (!OFastEnabled) continue; - LLVM_FALLTHROUGH; - case options::OPT_ffast_math: - HonorINFs = false; - HonorNaNs = false; - MathErrno = false; - AssociativeMath = true; - ReciprocalMath = true; - SignedZeros = false; - TrappingMath = false; - RoundingFPMath = false; - // If fast-math is set then set the fp-contract mode to fast. - FPContract = "fast"; + [[fallthrough]]; + case options::OPT_ffast_math: { + applyFastMath(); break; + } case options::OPT_fno_fast_math: HonorINFs = true; HonorNaNs = true; @@ -2788,33 +3238,41 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, MathErrno = TC.IsMathErrnoDefault(); AssociativeMath = false; ReciprocalMath = false; + ApproxFunc = false; SignedZeros = true; - TrappingMath = false; - RoundingFPMath = false; - // -fno_fast_math restores default denormal and fpcontract handling - DenormalFPMath = DefaultDenormalFPMath; - DenormalFP32Math = llvm::DenormalMode::getIEEE(); - FPContract = ""; + // -fno_fast_math restores default fpcontract handling + if (!JA.isDeviceOffloading(Action::OFK_Cuda) && + !JA.isOffloading(Action::OFK_HIP)) { + if (LastSeenFfpContractOption != "") { + FPContract = LastSeenFfpContractOption; + } else if (SeenUnsafeMathModeOption) + FPContract = "on"; + } break; } + // The StrictFPModel local variable is needed to report warnings + // in the way we intend. If -ffp-model=strict has been used, we + // want to report a warning for the next option encountered that + // takes us out of the settings described by fp-model=strict, but + // we don't want to continue issuing warnings for other conflicting + // options after that. if (StrictFPModel) { // If -ffp-model=strict has been specified on command line but // subsequent options conflict then emit warning diagnostic. - if (HonorINFs && HonorNaNs && - !AssociativeMath && !ReciprocalMath && - SignedZeros && TrappingMath && RoundingFPMath && - (FPContract.equals("off") || FPContract.empty()) && - DenormalFPMath == llvm::DenormalMode::getIEEE() && - DenormalFP32Math == llvm::DenormalMode::getIEEE()) + if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath && + SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc && + FPContract == "off") // OK: Current Arg doesn't conflict with -ffp-model=strict ; else { StrictFPModel = false; FPModel = ""; - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << "-ffp-model=strict" << - ((A->getNumValues() == 0) ? A->getSpelling() - : Args.MakeArgString(A->getSpelling() + A->getValue())); + auto RHS = (A->getNumValues() == 0) + ? A->getSpelling() + : Args.MakeArgString(A->getSpelling() + A->getValue()); + if (RHS != "-ffp-model=strict") + D.Diag(clang::diag::warn_drv_overriding_option) + << "-ffp-model=strict" << RHS; } } @@ -2828,12 +3286,15 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, if (!HonorNaNs) CmdArgs.push_back("-menable-no-nans"); + if (ApproxFunc) + CmdArgs.push_back("-fapprox-func"); + if (MathErrno) CmdArgs.push_back("-fmath-errno"); - if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros && - !TrappingMath) - CmdArgs.push_back("-menable-unsafe-fp-math"); + if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc && + !TrappingMath) + CmdArgs.push_back("-funsafe-math-optimizations"); if (!SignedZeros) CmdArgs.push_back("-fno-signed-zeros"); @@ -2846,7 +3307,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, if (TrappingMath) { // FP Exception Behavior is also set to strict - assert(FPExceptionBehavior.equals("strict")); + assert(FPExceptionBehavior == "strict"); } // The default is IEEE. @@ -2868,35 +3329,44 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, if (!FPContract.empty()) CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract)); - if (!RoundingFPMath) - CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); - - if (RoundingFPMath && RoundingMathPresent) + if (RoundingFPMath) CmdArgs.push_back(Args.MakeArgString("-frounding-math")); + else + CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); if (!FPExceptionBehavior.empty()) CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior)); + if (!FPEvalMethod.empty()) + CmdArgs.push_back(Args.MakeArgString("-ffp-eval-method=" + FPEvalMethod)); + + if (!Float16ExcessPrecision.empty()) + CmdArgs.push_back(Args.MakeArgString("-ffloat16-excess-precision=" + + Float16ExcessPrecision)); + if (!BFloat16ExcessPrecision.empty()) + CmdArgs.push_back(Args.MakeArgString("-fbfloat16-excess-precision=" + + BFloat16ExcessPrecision)); + ParseMRecip(D, Args, CmdArgs); // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the // individual features enabled by -ffast-math instead of the option itself as // that's consistent with gcc's behaviour. - if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && + if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc && ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) { CmdArgs.push_back("-ffast-math"); - if (FPModel.equals("fast")) { - if (FPContract.equals("fast")) + if (FPModel == "fast") { + if (FPContract == "fast") // All set, do nothing. ; else if (FPContract.empty()) // Enable -ffp-contract=fast CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); else - D.Diag(clang::diag::warn_drv_overriding_flag_option) - << "-ffp-model=fast" - << Args.MakeArgString("-ffp-contract=" + FPContract); + D.Diag(clang::diag::warn_drv_overriding_option) + << "-ffp-model=fast" + << Args.MakeArgString("-ffp-contract=" + FPContract); } } @@ -2913,17 +3383,28 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow, options::OPT_fstrict_float_cast_overflow, false)) CmdArgs.push_back("-fno-strict-float-cast-overflow"); + + if (Range != LangOptions::ComplexRangeKind::CX_None) + ComplexRangeStr = RenderComplexRangeOption(Range); + if (!ComplexRangeStr.empty()) { + CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr)); + if (Args.hasArg(options::OPT_fcomplex_arithmetic_EQ)) + CmdArgs.push_back(Args.MakeArgString("-fcomplex-arithmetic=" + + ComplexRangeKindToStr(Range))); + } + if (Args.hasArg(options::OPT_fcx_limited_range)) + CmdArgs.push_back("-fcx-limited-range"); + if (Args.hasArg(options::OPT_fcx_fortran_rules)) + CmdArgs.push_back("-fcx-fortran-rules"); + if (Args.hasArg(options::OPT_fno_cx_limited_range)) + CmdArgs.push_back("-fno-cx-limited-range"); + if (Args.hasArg(options::OPT_fno_cx_fortran_rules)) + CmdArgs.push_back("-fno-cx-fortran-rules"); } static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, const llvm::Triple &Triple, const InputInfo &Input) { - // Enable region store model by default. - CmdArgs.push_back("-analyzer-store=region"); - - // Treat blocks as analysis entry points. - CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks"); - // Add default argument set. if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { CmdArgs.push_back("-analyzer-checker=core"); @@ -2941,8 +3422,8 @@ static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg"); } - // Disable some unix checkers for PS4. - if (Triple.isPS4CPU()) { + // Disable some unix checkers for PS4/PS5. + if (Triple.isPS()) { CmdArgs.push_back("-analyzer-disable-checker=unix.API"); CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork"); } @@ -2960,7 +3441,7 @@ static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, if (types::isCXX(Input.getType())) CmdArgs.push_back("-analyzer-checker=cplusplus"); - if (!Triple.isPS4CPU()) { + if (!Triple.isPS()) { CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); @@ -2990,6 +3471,16 @@ static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); } +static bool isValidSymbolName(StringRef S) { + if (S.empty()) + return false; + + if (std::isdigit(S[0])) + return false; + + return llvm::all_of(S, [](char C) { return std::isalnum(C) || C == '_'; }); +} + static void RenderSSPOptions(const Driver &D, const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs, bool KernelOrKext) { @@ -3016,6 +3507,12 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC, StackProtectorLevel = LangOptions::SSPStrong; else if (A->getOption().matches(options::OPT_fstack_protector_all)) StackProtectorLevel = LangOptions::SSPReq; + + if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) { + D.Diag(diag::warn_drv_unsupported_option_for_target) + << A->getSpelling() << EffectiveTriple.getTriple(); + StackProtectorLevel = DefaultStackProtectorLevel; + } } else { StackProtectorLevel = DefaultStackProtectorLevel; } @@ -3028,7 +3525,7 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC, // --param ssp-buffer-size= for (const Arg *A : Args.filtered(options::OPT__param)) { StringRef Str(A->getValue()); - if (Str.startswith("ssp-buffer-size=")) { + if (Str.starts_with("ssp-buffer-size=")) { if (StackProtectorLevel) { CmdArgs.push_back("-stack-protector-buffer-size"); // FIXME: Verify the argument is a valid integer. @@ -3038,18 +3535,48 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC, } } - // First support "tls" and "global" for X86 target. - // TODO: Support "sysreg" for AArch64. const std::string &TripleStr = EffectiveTriple.getTriple(); if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) { StringRef Value = A->getValue(); - if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) + if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && + !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) << TripleStr; - if (Value != "tls" && Value != "global") { + if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() || + EffectiveTriple.isThumb()) && + Value != "tls" && Value != "global") { D.Diag(diag::err_drv_invalid_value_with_suggestion) - << A->getOption().getName() << Value - << "valid arguments to '-mstack-protector-guard=' are:tls global"; + << A->getOption().getName() << Value << "tls global"; + return; + } + if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && + Value == "tls") { + if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) { + D.Diag(diag::err_drv_ssp_missing_offset_argument) + << A->getAsString(Args); + return; + } + // Check whether the target subarch supports the hardware TLS register + if (!arm::isHardTPSupported(EffectiveTriple)) { + D.Diag(diag::err_target_unsupported_tp_hard) + << EffectiveTriple.getArchName(); + return; + } + // Check whether the user asked for something other than -mtp=cp15 + if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { + StringRef Value = A->getValue(); + if (Value != "cp15") { + D.Diag(diag::err_drv_argument_not_allowed_with) + << A->getAsString(Args) << "-mstack-protector-guard=tls"; + return; + } + } + CmdArgs.push_back("-target-feature"); + CmdArgs.push_back("+read-tp-tpidruro"); + } + if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") { + D.Diag(diag::err_drv_invalid_value_with_suggestion) + << A->getOption().getName() << Value << "sysreg global"; return; } A->render(Args, CmdArgs); @@ -3057,26 +3584,46 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC, if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) { StringRef Value = A->getValue(); - if (!EffectiveTriple.isX86()) + if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && + !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) << TripleStr; - unsigned Offset; + int Offset; if (Value.getAsInteger(10, Offset)) { D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; return; } + if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && + (Offset < 0 || Offset > 0xfffff)) { + D.Diag(diag::err_drv_invalid_int_value) + << A->getOption().getName() << Value; + return; + } A->render(Args, CmdArgs); } if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) { StringRef Value = A->getValue(); - if (!EffectiveTriple.isX86()) + if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) << TripleStr; if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) { D.Diag(diag::err_drv_invalid_value_with_suggestion) - << A->getOption().getName() << Value - << "for X86, valid arguments to '-mstack-protector-guard-reg=' are:fs gs"; + << A->getOption().getName() << Value << "fs gs"; + return; + } + if (EffectiveTriple.isAArch64() && Value != "sp_el0") { + D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; + return; + } + A->render(Args, CmdArgs); + } + + if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_symbol_EQ)) { + StringRef Value = A->getValue(); + if (!isValidSymbolName(Value)) { + D.Diag(diag::err_drv_argument_only_allowed_with) + << A->getOption().getName() << "legal symbol name"; return; } A->render(Args, CmdArgs); @@ -3091,12 +3638,11 @@ static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, return; if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && - !EffectiveTriple.isPPC64()) + !EffectiveTriple.isPPC64() && !EffectiveTriple.isAArch64()) return; - if (Args.hasFlag(options::OPT_fstack_clash_protection, - options::OPT_fno_stack_clash_protection, false)) - CmdArgs.push_back("-fstack-clash-protection"); + Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection, + options::OPT_fno_stack_clash_protection); } static void RenderTrivialAutoVarInitOptions(const Driver &D, @@ -3117,7 +3663,7 @@ static void RenderTrivialAutoVarInitOptions(const Driver &D, TrivialAutoVarInit = Val; else D.Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; + << A->getSpelling() << Val; break; } } @@ -3136,8 +3682,6 @@ static void RenderTrivialAutoVarInitOptions(const Driver &D, } if (!TrivialAutoVarInit.empty()) { - if (TrivialAutoVarInit == "zero" && !Args.hasArg(options::OPT_enable_trivial_var_init_zero)) - D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled); CmdArgs.push_back( Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit)); } @@ -3156,9 +3700,24 @@ static void RenderTrivialAutoVarInitOptions(const Driver &D, CmdArgs.push_back( Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val)); } + + if (Arg *A = Args.getLastArg(options::OPT_ftrivial_auto_var_init_max_size)) { + if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) || + StringRef( + Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) == + "uninitialized") + D.Diag(diag::err_drv_trivial_auto_var_init_max_size_missing_dependency); + A->claim(); + StringRef Val = A->getValue(); + if (std::stoi(Val.str()) <= 0) + D.Diag(diag::err_drv_trivial_auto_var_init_max_size_invalid_value); + CmdArgs.push_back( + Args.MakeArgString("-ftrivial-auto-var-init-max-size=" + Val)); + } } -static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) { +static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, + types::ID InputType) { // cl-denorms-are-zero is not forwarded. It is translated into a generic flag // for denormal flushing handling based on the target. const unsigned ForwardedArguments[] = { @@ -3178,11 +3737,61 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) { if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) { std::string CLStdStr = std::string("-cl-std=") + A->getValue(); CmdArgs.push_back(Args.MakeArgString(CLStdStr)); + } else if (Arg *A = Args.getLastArg(options::OPT_cl_ext_EQ)) { + std::string CLExtStr = std::string("-cl-ext=") + A->getValue(); + CmdArgs.push_back(Args.MakeArgString(CLExtStr)); } for (const auto &Arg : ForwardedArguments) if (const auto *A = Args.getLastArg(Arg)) CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); + + // Only add the default headers if we are compiling OpenCL sources. + if ((types::isOpenCL(InputType) || + (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) && + !Args.hasArg(options::OPT_cl_no_stdinc)) { + CmdArgs.push_back("-finclude-default-header"); + CmdArgs.push_back("-fdeclare-opencl-builtins"); + } +} + +static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs, + types::ID InputType) { + const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version, + options::OPT_D, + options::OPT_I, + options::OPT_O, + options::OPT_emit_llvm, + options::OPT_emit_obj, + options::OPT_disable_llvm_passes, + options::OPT_fnative_half_type, + options::OPT_hlsl_entrypoint}; + if (!types::isHLSL(InputType)) + return; + for (const auto &Arg : ForwardedArguments) + if (const auto *A = Args.getLastArg(Arg)) + A->renderAsInput(Args, CmdArgs); + // Add the default headers if dxc_no_stdinc is not set. + if (!Args.hasArg(options::OPT_dxc_no_stdinc) && + !Args.hasArg(options::OPT_nostdinc)) + CmdArgs.push_back("-finclude-default-header"); +} + +static void RenderOpenACCOptions(const Driver &D, const ArgList &Args, + ArgStringList &CmdArgs, types::ID InputType) { + if (!Args.hasArg(options::OPT_fopenacc)) + return; + + CmdArgs.push_back("-fopenacc"); + + if (Arg *A = Args.getLastArg(options::OPT_openacc_macro_override)) { + StringRef Value = A->getValue(); + int Version; + if (!Value.getAsInteger(10, Version)) + A->renderAsInput(Args, CmdArgs); + else + D.Diag(diag::err_drv_clang_unsupported) << Value; + } } static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, @@ -3253,7 +3862,7 @@ static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property); Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly); Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init); - Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path); + Args.AddLastArg(CmdArgs, options::OPT_objcmt_allowlist_dir_path); } } @@ -3271,20 +3880,13 @@ static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, UseBuiltins = false; // Process the -fno-builtin-* options. - for (const auto &Arg : Args) { - const Option &O = Arg->getOption(); - if (!O.matches(options::OPT_fno_builtin_)) - continue; - - Arg->claim(); + for (const Arg *A : Args.filtered(options::OPT_fno_builtin_)) { + A->claim(); // If -fno-builtin is specified, then there's no need to pass the option to // the frontend. - if (!UseBuiltins) - continue; - - StringRef FuncName = Arg->getValue(); - CmdArgs.push_back(Args.MakeArgString("-fno-builtin-" + FuncName)); + if (UseBuiltins) + A->render(Args, CmdArgs); } // le32-specific flags: @@ -3295,6 +3897,11 @@ static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, } bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { + if (const char *Str = std::getenv("CLANG_MODULE_CACHE_PATH")) { + Twine Path{Str}; + Path.toVector(Result); + return Path.getSingleStringRef() != ""; + } if (llvm::sys::path::cache_directory(Result)) { llvm::sys::path::append(Result, "clang"); llvm::sys::path::append(Result, "ModuleCache"); @@ -3303,10 +3910,32 @@ bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { return false; } -static void RenderModulesOptions(Compilation &C, const Driver &D, +llvm::SmallString<256> +clang::driver::tools::getCXX20NamedModuleOutputPath(const ArgList &Args, + const char *BaseInput) { + if (Arg *ModuleOutputEQ = Args.getLastArg(options::OPT_fmodule_output_EQ)) + return StringRef(ModuleOutputEQ->getValue()); + + SmallString<256> OutputPath; + if (Arg *FinalOutput = Args.getLastArg(options::OPT_o); + FinalOutput && Args.hasArg(options::OPT_c)) + OutputPath = FinalOutput->getValue(); + else + OutputPath = BaseInput; + + const char *Extension = types::getTypeTempSuffix(types::TY_ModuleFile); + llvm::sys::path::replace_extension(OutputPath, Extension); + return OutputPath; +} + +static bool RenderModulesOptions(Compilation &C, const Driver &D, const ArgList &Args, const InputInfo &Input, - const InputInfo &Output, - ArgStringList &CmdArgs, bool &HaveModules) { + const InputInfo &Output, bool HaveStd20, + ArgStringList &CmdArgs) { + bool IsCXX = types::isCXX(Input.getType()); + bool HaveStdCXXModules = IsCXX && HaveStd20; + bool HaveModules = HaveStdCXXModules; + // -fmodules enables the use of precompiled modules (off by default). // Users can pass -fno-cxx-modules to turn off modules support for // C++/Objective-C++ programs. @@ -3314,17 +3943,13 @@ static void RenderModulesOptions(Compilation &C, const Driver &D, if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, options::OPT_fno_cxx_modules, true); - if (AllowedInCXX || !types::isCXX(Input.getType())) { + if (AllowedInCXX || !IsCXX) { CmdArgs.push_back("-fmodules"); HaveClangModules = true; } } HaveModules |= HaveClangModules; - if (Args.hasArg(options::OPT_fmodules_ts)) { - CmdArgs.push_back("-fmodules-ts"); - HaveModules = true; - } // -fmodule-maps enables implicit reading of module map files. By default, // this is enabled if we are using Clang's flavor of precompiled modules. @@ -3333,9 +3958,8 @@ static void RenderModulesOptions(Compilation &C, const Driver &D, CmdArgs.push_back("-fimplicit-module-maps"); // -fmodules-decluse checks that modules used are declared so (off by default) - if (Args.hasFlag(options::OPT_fmodules_decluse, - options::OPT_fno_modules_decluse, false)) - CmdArgs.push_back("-fmodules-decluse"); + Args.addOptInFlag(CmdArgs, options::OPT_fmodules_decluse, + options::OPT_fno_modules_decluse); // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that // all #included headers are part of modules. @@ -3379,12 +4003,6 @@ static void RenderModulesOptions(Compilation &C, const Driver &D, } if (HaveModules) { - // -fprebuilt-module-path specifies where to load the prebuilt module files. - for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { - CmdArgs.push_back(Args.MakeArgString( - std::string("-fprebuilt-module-path=") + A->getValue())); - A->claim(); - } if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules, options::OPT_fno_prebuilt_implicit_modules, false)) CmdArgs.push_back("-fprebuilt-implicit-modules"); @@ -3417,9 +4035,16 @@ static void RenderModulesOptions(Compilation &C, const Driver &D, // names to precompiled module files (the module is loaded only if used). // The -fmodule-file=<file> form can be used to unconditionally load // precompiled module files (whether used or not). - if (HaveModules) + if (HaveModules || Input.getType() == clang::driver::types::TY_ModuleFile) { Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file); - else + + // -fprebuilt-module-path specifies where to load the prebuilt module files. + for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { + CmdArgs.push_back(Args.MakeArgString( + std::string("-fprebuilt-module-path=") + A->getValue())); + A->claim(); + } + } else Args.ClaimAllArgs(options::OPT_fmodule_file); // When building modules and generating crashdumps, we need to dump a module @@ -3443,38 +4068,86 @@ static void RenderModulesOptions(Compilation &C, const Driver &D, Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); - Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); + if (HaveClangModules) { + Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); - if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { - if (Args.hasArg(options::OPT_fbuild_session_timestamp)) - D.Diag(diag::err_drv_argument_not_allowed_with) - << A->getAsString(Args) << "-fbuild-session-timestamp"; + if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { + if (Args.hasArg(options::OPT_fbuild_session_timestamp)) + D.Diag(diag::err_drv_argument_not_allowed_with) + << A->getAsString(Args) << "-fbuild-session-timestamp"; - llvm::sys::fs::file_status Status; - if (llvm::sys::fs::status(A->getValue(), Status)) - D.Diag(diag::err_drv_no_such_file) << A->getValue(); - CmdArgs.push_back( - Args.MakeArgString("-fbuild-session-timestamp=" + - Twine((uint64_t)Status.getLastModificationTime() - .time_since_epoch() - .count()))); - } + llvm::sys::fs::file_status Status; + if (llvm::sys::fs::status(A->getValue(), Status)) + D.Diag(diag::err_drv_no_such_file) << A->getValue(); + CmdArgs.push_back(Args.MakeArgString( + "-fbuild-session-timestamp=" + + Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>( + Status.getLastModificationTime().time_since_epoch()) + .count()))); + } + + if (Args.getLastArg( + options::OPT_fmodules_validate_once_per_build_session)) { + if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, + options::OPT_fbuild_session_file)) + D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); - if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) { - if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, - options::OPT_fbuild_session_file)) - D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); + Args.AddLastArg(CmdArgs, + options::OPT_fmodules_validate_once_per_build_session); + } + + if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, + options::OPT_fno_modules_validate_system_headers, + ImplicitModules)) + CmdArgs.push_back("-fmodules-validate-system-headers"); Args.AddLastArg(CmdArgs, - options::OPT_fmodules_validate_once_per_build_session); + options::OPT_fmodules_disable_diagnostic_validation); + } else { + Args.ClaimAllArgs(options::OPT_fbuild_session_timestamp); + Args.ClaimAllArgs(options::OPT_fbuild_session_file); + Args.ClaimAllArgs(options::OPT_fmodules_validate_once_per_build_session); + Args.ClaimAllArgs(options::OPT_fmodules_validate_system_headers); + Args.ClaimAllArgs(options::OPT_fno_modules_validate_system_headers); + Args.ClaimAllArgs(options::OPT_fmodules_disable_diagnostic_validation); + } + + // FIXME: We provisionally don't check ODR violations for decls in the global + // module fragment. + CmdArgs.push_back("-fskip-odr-check-in-gmf"); + + if (Args.hasArg(options::OPT_modules_reduced_bmi) && + (Input.getType() == driver::types::TY_CXXModule || + Input.getType() == driver::types::TY_PP_CXXModule)) { + CmdArgs.push_back("-fexperimental-modules-reduced-bmi"); + + if (Args.hasArg(options::OPT_fmodule_output_EQ)) + Args.AddLastArg(CmdArgs, options::OPT_fmodule_output_EQ); + else + CmdArgs.push_back(Args.MakeArgString( + "-fmodule-output=" + + getCXX20NamedModuleOutputPath(Args, Input.getBaseInput()))); } - if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, - options::OPT_fno_modules_validate_system_headers, - ImplicitModules)) - CmdArgs.push_back("-fmodules-validate-system-headers"); + // Noop if we see '-fexperimental-modules-reduced-bmi' with other translation + // units than module units. This is more user friendly to allow end uers to + // enable this feature without asking for help from build systems. + Args.ClaimAllArgs(options::OPT_modules_reduced_bmi); + + // We need to include the case the input file is a module file here. + // Since the default compilation model for C++ module interface unit will + // create temporary module file and compile the temporary module file + // to get the object file. Then the `-fmodule-output` flag will be + // brought to the second compilation process. So we have to claim it for + // the case too. + if (Input.getType() == driver::types::TY_CXXModule || + Input.getType() == driver::types::TY_PP_CXXModule || + Input.getType() == driver::types::TY_ModuleFile) { + Args.ClaimAllArgs(options::OPT_fmodule_output); + Args.ClaimAllArgs(options::OPT_fmodule_output_EQ); + } - Args.AddLastArg(CmdArgs, options::OPT_fmodules_disable_diagnostic_validation); + return HaveModules; } static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, @@ -3509,7 +4182,8 @@ static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, else CmdArgs.push_back("-fsigned-wchar"); } - } + } else if (T.isOSzOS()) + CmdArgs.push_back("-fno-signed-wchar"); } static void RenderObjCOptions(const ToolChain &TC, const Driver &D, @@ -3600,6 +4274,9 @@ static void RenderObjCOptions(const ToolChain &TC, const Driver &D, WeakArg->render(Args, CmdArgs); } } + + if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing)) + CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing"); } static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, @@ -3631,15 +4308,10 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, options::OPT_fno_caret_diagnostics, CaretDefault)) CmdArgs.push_back("-fno-caret-diagnostics"); - // -fdiagnostics-fixit-info is default, only pass non-default. - if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info, - options::OPT_fno_diagnostics_fixit_info)) - CmdArgs.push_back("-fno-diagnostics-fixit-info"); - - // Enable -fdiagnostics-show-option by default. - if (!Args.hasFlag(options::OPT_fdiagnostics_show_option, - options::OPT_fno_diagnostics_show_option, true)) - CmdArgs.push_back("-fno-diagnostics-show-option"); + Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_fixit_info, + options::OPT_fno_diagnostics_fixit_info); + Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_option, + options::OPT_fno_diagnostics_show_option); if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { @@ -3647,9 +4319,8 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, CmdArgs.push_back(A->getValue()); } - if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness, - options::OPT_fno_diagnostics_show_hotness, false)) - CmdArgs.push_back("-fdiagnostics-show-hotness"); + Args.addOptInFlag(CmdArgs, options::OPT_fdiagnostics_show_hotness, + options::OPT_fno_diagnostics_show_hotness); if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) { @@ -3658,9 +4329,19 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, CmdArgs.push_back(Args.MakeArgString(Opt)); } + if (const Arg *A = + Args.getLastArg(options::OPT_fdiagnostics_misexpect_tolerance_EQ)) { + std::string Opt = + std::string("-fdiagnostics-misexpect-tolerance=") + A->getValue(); + CmdArgs.push_back(Args.MakeArgString(Opt)); + } + if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { CmdArgs.push_back("-fdiagnostics-format"); CmdArgs.push_back(A->getValue()); + if (StringRef(A->getValue()) == "sarif" || + StringRef(A->getValue()) == "SARIF") + D.Diag(diag::warn_drv_sarif_format_unstable); } if (const Arg *A = Args.getLastArg( @@ -3677,22 +4358,13 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, // re-parsed to construct this job; claim any possible color diagnostic here // to avoid warn_drv_unused_argument and diagnose bad // OPT_fdiagnostics_color_EQ values. - for (const Arg *A : Args) { - const Option &O = A->getOption(); - if (!O.matches(options::OPT_fcolor_diagnostics) && - !O.matches(options::OPT_fdiagnostics_color) && - !O.matches(options::OPT_fno_color_diagnostics) && - !O.matches(options::OPT_fno_diagnostics_color) && - !O.matches(options::OPT_fdiagnostics_color_EQ)) - continue; - - if (O.matches(options::OPT_fdiagnostics_color_EQ)) { - StringRef Value(A->getValue()); - if (Value != "always" && Value != "never" && Value != "auto") - D.Diag(diag::err_drv_clang_unsupported) - << ("-fdiagnostics-color=" + Value).str(); - } - A->claim(); + Args.getLastArg(options::OPT_fcolor_diagnostics, + options::OPT_fno_color_diagnostics); + if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_color_EQ)) { + StringRef Value(A->getValue()); + if (Value != "always" && Value != "never" && Value != "auto") + D.Diag(diag::err_drv_invalid_argument_to_option) + << Value << A->getOption().getName(); } if (D.getDiags().getDiagnosticOptions().ShowColors) @@ -3701,9 +4373,11 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, if (Args.hasArg(options::OPT_fansi_escape_codes)) CmdArgs.push_back("-fansi-escape-codes"); - if (!Args.hasFlag(options::OPT_fshow_source_location, - options::OPT_fno_show_source_location)) - CmdArgs.push_back("-fno-show-source-location"); + Args.addOptOutFlag(CmdArgs, options::OPT_fshow_source_location, + options::OPT_fno_show_source_location); + + Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_line_numbers, + options::OPT_fno_diagnostics_show_line_numbers); if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths)) CmdArgs.push_back("-fdiagnostics-absolute-paths"); @@ -3712,14 +4386,11 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, ColumnDefault)) CmdArgs.push_back("-fno-show-column"); - if (!Args.hasFlag(options::OPT_fspell_checking, - options::OPT_fno_spell_checking)) - CmdArgs.push_back("-fno-spell-checking"); + Args.addOptOutFlag(CmdArgs, options::OPT_fspell_checking, + options::OPT_fno_spell_checking); } -enum class DwarfFissionKind { None, Split, Single }; - -static DwarfFissionKind getDebugFissionKind(const Driver &D, +DwarfFissionKind tools::getDebugFissionKind(const Driver &D, const ArgList &Args, Arg *&Arg) { Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ, options::OPT_gno_split_dwarf); @@ -3736,16 +4407,39 @@ static DwarfFissionKind getDebugFissionKind(const Driver &D, return DwarfFissionKind::Single; D.Diag(diag::err_drv_unsupported_option_argument) - << Arg->getOption().getName() << Arg->getValue(); + << Arg->getSpelling() << Arg->getValue(); return DwarfFissionKind::None; } -static void renderDebugOptions(const ToolChain &TC, const Driver &D, - const llvm::Triple &T, const ArgList &Args, - bool EmitCodeView, bool IRInput, - ArgStringList &CmdArgs, - codegenoptions::DebugInfoKind &DebugInfoKind, - DwarfFissionKind &DwarfFission) { +static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, + const ArgList &Args, ArgStringList &CmdArgs, + unsigned DwarfVersion) { + auto *DwarfFormatArg = + Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); + if (!DwarfFormatArg) + return; + + if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { + if (DwarfVersion < 3) + D.Diag(diag::err_drv_argument_only_allowed_with) + << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; + else if (!T.isArch64Bit()) + D.Diag(diag::err_drv_argument_only_allowed_with) + << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; + else if (!T.isOSBinFormatELF()) + D.Diag(diag::err_drv_argument_only_allowed_with) + << DwarfFormatArg->getAsString(Args) << "ELF platforms"; + } + + DwarfFormatArg->render(Args, CmdArgs); +} + +static void +renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T, + const ArgList &Args, bool IRInput, ArgStringList &CmdArgs, + const InputInfo &Output, + llvm::codegenoptions::DebugInfoKind &DebugInfoKind, + DwarfFissionKind &DwarfFission) { if (Args.hasFlag(options::OPT_fdebug_info_for_profiling, options::OPT_fno_debug_info_for_profiling, false) && checkDebugInfoOption( @@ -3780,63 +4474,59 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, } } if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { - DebugInfoKind = codegenoptions::LimitedDebugInfo; + DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; // If the last option explicitly specified a debug-info level, use it. if (checkDebugInfoOption(A, Args, D, TC) && A->getOption().matches(options::OPT_gN_Group)) { - DebugInfoKind = DebugLevelToInfoKind(*A); + DebugInfoKind = debugLevelToInfoKind(*A); // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more // complicated if you've disabled inline info in the skeleton CUs // (SplitDWARFInlining) - then there's value in composing split-dwarf and // line-tables-only, so let those compose naturally in that case. - if (DebugInfoKind == codegenoptions::NoDebugInfo || - DebugInfoKind == codegenoptions::DebugDirectivesOnly || - (DebugInfoKind == codegenoptions::DebugLineTablesOnly && + if (DebugInfoKind == llvm::codegenoptions::NoDebugInfo || + DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly || + (DebugInfoKind == llvm::codegenoptions::DebugLineTablesOnly && SplitDWARFInlining)) DwarfFission = DwarfFissionKind::None; } } // If a debugger tuning argument appeared, remember it. + bool HasDebuggerTuning = false; if (const Arg *A = Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { + HasDebuggerTuning = true; if (checkDebugInfoOption(A, Args, D, TC)) { if (A->getOption().matches(options::OPT_glldb)) DebuggerTuning = llvm::DebuggerKind::LLDB; else if (A->getOption().matches(options::OPT_gsce)) DebuggerTuning = llvm::DebuggerKind::SCE; + else if (A->getOption().matches(options::OPT_gdbx)) + DebuggerTuning = llvm::DebuggerKind::DBX; else DebuggerTuning = llvm::DebuggerKind::GDB; } } // If a -gdwarf argument appeared, remember it. - const Arg *GDwarfN = Args.getLastArg( - options::OPT_gdwarf_2, options::OPT_gdwarf_3, options::OPT_gdwarf_4, - options::OPT_gdwarf_5, options::OPT_gdwarf); bool EmitDwarf = false; - if (GDwarfN) { - if (checkDebugInfoOption(GDwarfN, Args, D, TC)) - EmitDwarf = true; - else - GDwarfN = nullptr; - } + if (const Arg *A = getDwarfNArg(Args)) + EmitDwarf = checkDebugInfoOption(A, Args, D, TC); - if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) { - if (checkDebugInfoOption(A, Args, D, TC)) - EmitCodeView = true; - } + bool EmitCodeView = false; + if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) + EmitCodeView = checkDebugInfoOption(A, Args, D, TC); // If the user asked for debug info but did not explicitly specify -gcodeview // or -gdwarf, ask the toolchain for the default format. if (!EmitCodeView && !EmitDwarf && - DebugInfoKind != codegenoptions::NoDebugInfo) { + DebugInfoKind != llvm::codegenoptions::NoDebugInfo) { switch (TC.getDefaultDebugFormat()) { - case codegenoptions::DIF_CodeView: + case llvm::codegenoptions::DIF_CodeView: EmitCodeView = true; break; - case codegenoptions::DIF_DWARF: + case llvm::codegenoptions::DIF_DWARF: EmitDwarf = true; break; } @@ -3845,61 +4535,62 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may // be lower than what the user wanted. - unsigned DefaultDWARFVersion = ParseDebugDefaultVersion(TC, Args); if (EmitDwarf) { - // Start with the platform default DWARF version - RequestedDWARFVersion = TC.GetDefaultDwarfVersion(); - assert(RequestedDWARFVersion && - "toolchain default DWARF version must be nonzero"); - - // If the user specified a default DWARF version, that takes precedence - // over the platform default. - if (DefaultDWARFVersion) - RequestedDWARFVersion = DefaultDWARFVersion; - - // Override with a user-specified DWARF version - if (GDwarfN) - if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling())) - RequestedDWARFVersion = ExplicitVersion; + RequestedDWARFVersion = getDwarfVersion(TC, Args); // Clamp effective DWARF version to the max supported by the toolchain. EffectiveDWARFVersion = std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion()); + } else { + Args.ClaimAllArgs(options::OPT_fdebug_default_version); } // -gline-directives-only supported only for the DWARF debug info. if (RequestedDWARFVersion == 0 && - DebugInfoKind == codegenoptions::DebugDirectivesOnly) - DebugInfoKind = codegenoptions::NoDebugInfo; + DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly) + DebugInfoKind = llvm::codegenoptions::NoDebugInfo; + + // strict DWARF is set to false by default. But for DBX, we need it to be set + // as true by default. + if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf)) + (void)checkDebugInfoOption(A, Args, D, TC); + if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf, + DebuggerTuning == llvm::DebuggerKind::DBX)) + CmdArgs.push_back("-gstrict-dwarf"); - // We ignore flag -gstrict-dwarf for now. // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags. Args.ClaimAllArgs(options::OPT_g_flags_Group); // Column info is included by default for everything except SCE and // CodeView. Clang doesn't track end columns, just starting columns, which, // in theory, is fine for CodeView (and PDB). In practice, however, the - // Microsoft debuggers don't handle missing end columns well, so it's better - // not to include any column info. + // Microsoft debuggers don't handle missing end columns well, and the AIX + // debugger DBX also doesn't handle the columns well, so it's better not to + // include any column info. if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info)) (void)checkDebugInfoOption(A, Args, D, TC); if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info, - !EmitCodeView && DebuggerTuning != llvm::DebuggerKind::SCE)) + !EmitCodeView && + (DebuggerTuning != llvm::DebuggerKind::SCE && + DebuggerTuning != llvm::DebuggerKind::DBX))) CmdArgs.push_back("-gno-column-info"); // FIXME: Move backend command line options to the module. - // If -gline-tables-only or -gline-directives-only is the last option it wins. - if (const Arg *A = Args.getLastArg(options::OPT_gmodules)) - if (checkDebugInfoOption(A, Args, D, TC)) { - if (DebugInfoKind != codegenoptions::DebugLineTablesOnly && - DebugInfoKind != codegenoptions::DebugDirectivesOnly) { - DebugInfoKind = codegenoptions::LimitedDebugInfo; + if (Args.hasFlag(options::OPT_gmodules, options::OPT_gno_modules, false)) { + // If -gline-tables-only or -gline-directives-only is the last option it + // wins. + if (checkDebugInfoOption(Args.getLastArg(options::OPT_gmodules), Args, D, + TC)) { + if (DebugInfoKind != llvm::codegenoptions::DebugLineTablesOnly && + DebugInfoKind != llvm::codegenoptions::DebugDirectivesOnly) { + DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; CmdArgs.push_back("-dwarf-ext-refs"); CmdArgs.push_back("-fmodule-format=obj"); } } + } - if (T.isOSBinFormatELF() && !SplitDWARFInlining) - CmdArgs.push_back("-fno-split-dwarf-inlining"); + if (T.isOSBinFormatELF() && SplitDWARFInlining) + CmdArgs.push_back("-fsplit-dwarf-inlining"); // After we've dealt with all combinations of things that could // make DebugInfoKind be other than None or DebugLineTablesOnly, @@ -3913,12 +4604,13 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug)) (void)checkDebugInfoOption(A, Args, D, TC); - if (DebugInfoKind == codegenoptions::LimitedDebugInfo) { + if (DebugInfoKind == llvm::codegenoptions::LimitedDebugInfo || + DebugInfoKind == llvm::codegenoptions::DebugInfoConstructor) { if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types, options::OPT_feliminate_unused_debug_types, false)) - DebugInfoKind = codegenoptions::UnusedTypeInfo; + DebugInfoKind = llvm::codegenoptions::UnusedTypeInfo; else if (NeedFullDebug) - DebugInfoKind = codegenoptions::FullDebugInfo; + DebugInfoKind = llvm::codegenoptions::FullDebugInfo; } if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, @@ -3944,29 +4636,30 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, if (EmitCodeView) { CmdArgs.push_back("-gcodeview"); - // Emit codeview type hashes if requested. - if (Args.hasFlag(options::OPT_gcodeview_ghash, - options::OPT_gno_codeview_ghash, false)) { - CmdArgs.push_back("-gcodeview-ghash"); - } - } + Args.addOptInFlag(CmdArgs, options::OPT_gcodeview_ghash, + options::OPT_gno_codeview_ghash); - // Omit inline line tables if requested. - if (Args.hasFlag(options::OPT_gno_inline_line_tables, - options::OPT_ginline_line_tables, false)) { - CmdArgs.push_back("-gno-inline-line-tables"); + Args.addOptOutFlag(CmdArgs, options::OPT_gcodeview_command_line, + options::OPT_gno_codeview_command_line); } + Args.addOptOutFlag(CmdArgs, options::OPT_ginline_line_tables, + options::OPT_gno_inline_line_tables); + // When emitting remarks, we need at least debug lines in the output. if (willEmitRemarks(Args) && - DebugInfoKind <= codegenoptions::DebugDirectivesOnly) - DebugInfoKind = codegenoptions::DebugLineTablesOnly; + DebugInfoKind <= llvm::codegenoptions::DebugDirectivesOnly) + DebugInfoKind = llvm::codegenoptions::DebugLineTablesOnly; // Adjust the debug info kind for the given toolchain. TC.adjustDebugInfoKind(DebugInfoKind, Args); + // On AIX, the debugger tuning option can be omitted if it is not explicitly + // set. RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion, - DebuggerTuning); + T.isOSAIX() && !HasDebuggerTuning + ? llvm::DebuggerKind::Default + : DebuggerTuning); // -fdebug-macro turns on macro debug info generation. if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro, @@ -3980,20 +4673,56 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames, options::OPT_gpubnames, options::OPT_gno_pubnames); if (DwarfFission != DwarfFissionKind::None || - (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) - if (!PubnamesArg || - (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && - !PubnamesArg->getOption().matches(options::OPT_gno_pubnames))) + (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) { + const bool OptionSet = + (PubnamesArg && + (PubnamesArg->getOption().matches(options::OPT_gpubnames) || + PubnamesArg->getOption().matches(options::OPT_ggnu_pubnames))); + if ((DebuggerTuning != llvm::DebuggerKind::LLDB || OptionSet) && + (!PubnamesArg || + (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && + !PubnamesArg->getOption().matches(options::OPT_gno_pubnames)))) CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches( options::OPT_gpubnames) ? "-gpubnames" : "-ggnu-pubnames"); + } + const auto *SimpleTemplateNamesArg = + Args.getLastArg(options::OPT_gsimple_template_names, + options::OPT_gno_simple_template_names); + bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; + if (SimpleTemplateNamesArg && + checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { + const auto &Opt = SimpleTemplateNamesArg->getOption(); + if (Opt.matches(options::OPT_gsimple_template_names)) { + ForwardTemplateParams = true; + CmdArgs.push_back("-gsimple-template-names=simple"); + } + } + + // Emit DW_TAG_template_alias for template aliases? True by default for SCE. + bool UseDebugTemplateAlias = + DebuggerTuning == llvm::DebuggerKind::SCE && RequestedDWARFVersion >= 4; + if (const auto *DebugTemplateAlias = Args.getLastArg( + options::OPT_gtemplate_alias, options::OPT_gno_template_alias)) { + // DW_TAG_template_alias is only supported from DWARFv5 but if a user + // asks for it we should let them have it (if the target supports it). + if (checkDebugInfoOption(DebugTemplateAlias, Args, D, TC)) { + const auto &Opt = DebugTemplateAlias->getOption(); + UseDebugTemplateAlias = Opt.matches(options::OPT_gtemplate_alias); + } + } + if (UseDebugTemplateAlias) + CmdArgs.push_back("-gtemplate-alias"); - if (Args.hasFlag(options::OPT_fdebug_ranges_base_address, - options::OPT_fno_debug_ranges_base_address, false)) { - CmdArgs.push_back("-fdebug-ranges-base-address"); + if (const Arg *A = Args.getLastArg(options::OPT_gsrc_hash_EQ)) { + StringRef v = A->getValue(); + CmdArgs.push_back(Args.MakeArgString("-gsrc-hash=" + v)); } + Args.addOptInFlag(CmdArgs, options::OPT_fdebug_ranges_base_address, + options::OPT_fno_debug_ranges_base_address); + // -gdwarf-aranges turns on the emission of the aranges section in the // backend. // Always enabled for SCE tuning. @@ -4005,10 +4734,10 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, CmdArgs.push_back("-generate-arange-section"); } - if (Args.hasFlag(options::OPT_fforce_dwarf_frame, - options::OPT_fno_force_dwarf_frame, false)) - CmdArgs.push_back("-fforce-dwarf-frame"); + Args.addOptInFlag(CmdArgs, options::OPT_fforce_dwarf_frame, + options::OPT_fno_force_dwarf_frame); + bool EnableTypeUnits = false; if (Args.hasFlag(options::OPT_fdebug_types_section, options::OPT_fno_debug_types_section, false)) { if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) { @@ -4019,14 +4748,35 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, } else if (checkDebugInfoOption( Args.getLastArg(options::OPT_fdebug_types_section), Args, D, TC)) { + EnableTypeUnits = true; CmdArgs.push_back("-mllvm"); CmdArgs.push_back("-generate-type-units"); } } + if (const Arg *A = + Args.getLastArg(options::OPT_gomit_unreferenced_methods, + options::OPT_gno_omit_unreferenced_methods)) + (void)checkDebugInfoOption(A, Args, D, TC); + if (Args.hasFlag(options::OPT_gomit_unreferenced_methods, + options::OPT_gno_omit_unreferenced_methods, false) && + (DebugInfoKind == llvm::codegenoptions::DebugInfoConstructor || + DebugInfoKind == llvm::codegenoptions::LimitedDebugInfo) && + !EnableTypeUnits) { + CmdArgs.push_back("-gomit-unreferenced-methods"); + } + + // To avoid join/split of directory+filename, the integrated assembler prefers + // the directory form of .file on all DWARF versions. GNU as doesn't allow the + // form before DWARF v5. + if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, + options::OPT_fno_dwarf_directory_asm, + TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) + CmdArgs.push_back("-fno-dwarf-directory-asm"); + // Decide how to render forward declarations of template instantiations. // SCE wants full descriptions, others just get them in the name. - if (DebuggerTuning == llvm::DebuggerKind::SCE) + if (ForwardTemplateParams) CmdArgs.push_back("-debug-forward-template-params"); // Do we need to explicitly import anonymous namespaces into the parent @@ -4034,26 +4784,106 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, if (DebuggerTuning == llvm::DebuggerKind::SCE) CmdArgs.push_back("-dwarf-explicit-import"); - auto *DwarfFormatArg = - Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); - if (DwarfFormatArg && - DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { - const llvm::Triple &RawTriple = TC.getTriple(); - if (EffectiveDWARFVersion < 3) - D.Diag(diag::err_drv_argument_only_allowed_with) - << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; - else if (!RawTriple.isArch64Bit()) - D.Diag(diag::err_drv_argument_only_allowed_with) - << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; - else if (!RawTriple.isOSBinFormatELF()) - D.Diag(diag::err_drv_argument_only_allowed_with) - << DwarfFormatArg->getAsString(Args) << "ELF platforms"; + renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion); + RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); + + // This controls whether or not we perform JustMyCode instrumentation. + if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) { + if (TC.getTriple().isOSBinFormatELF() || D.IsCLMode()) { + if (DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor) + CmdArgs.push_back("-fjmc"); + else if (D.IsCLMode()) + D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC" + << "'/Zi', '/Z7'"; + else + D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc" + << "-g"; + } else { + D.Diag(clang::diag::warn_drv_fjmc_for_elf_only); + } } - if (DwarfFormatArg) - DwarfFormatArg->render(Args, CmdArgs); + // Add in -fdebug-compilation-dir if necessary. + const char *DebugCompilationDir = + addDebugCompDirArg(Args, CmdArgs, D.getVFS()); + + addDebugPrefixMapArg(D, TC, Args, CmdArgs); - RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); + // Add the output path to the object file for CodeView debug infos. + if (EmitCodeView && Output.isFilename()) + addDebugObjectName(Args, CmdArgs, DebugCompilationDir, + Output.getFilename()); +} + +static void ProcessVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args, + ArgStringList &CmdArgs) { + unsigned RTOptionID = options::OPT__SLASH_MT; + + if (Args.hasArg(options::OPT__SLASH_LDd)) + // The /LDd option implies /MTd. The dependent lib part can be overridden, + // but defining _DEBUG is sticky. + RTOptionID = options::OPT__SLASH_MTd; + + if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) + RTOptionID = A->getOption().getID(); + + if (Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch<unsigned>(A->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); + } + + StringRef FlagForCRT; + switch (RTOptionID) { + case options::OPT__SLASH_MD: + if (Args.hasArg(options::OPT__SLASH_LDd)) + CmdArgs.push_back("-D_DEBUG"); + CmdArgs.push_back("-D_MT"); + CmdArgs.push_back("-D_DLL"); + FlagForCRT = "--dependent-lib=msvcrt"; + break; + case options::OPT__SLASH_MDd: + CmdArgs.push_back("-D_DEBUG"); + CmdArgs.push_back("-D_MT"); + CmdArgs.push_back("-D_DLL"); + FlagForCRT = "--dependent-lib=msvcrtd"; + break; + case options::OPT__SLASH_MT: + if (Args.hasArg(options::OPT__SLASH_LDd)) + CmdArgs.push_back("-D_DEBUG"); + CmdArgs.push_back("-D_MT"); + CmdArgs.push_back("-flto-visibility-public-std"); + FlagForCRT = "--dependent-lib=libcmt"; + break; + case options::OPT__SLASH_MTd: + CmdArgs.push_back("-D_DEBUG"); + CmdArgs.push_back("-D_MT"); + CmdArgs.push_back("-flto-visibility-public-std"); + FlagForCRT = "--dependent-lib=libcmtd"; + break; + default: + llvm_unreachable("Unexpected option ID."); + } + + if (Args.hasArg(options::OPT_fms_omit_default_lib)) { + CmdArgs.push_back("-D_VC_NODEFAULTLIB"); + } else { + CmdArgs.push_back(FlagForCRT.data()); + + // This provides POSIX compatibility (maps 'open' to '_open'), which most + // users want. The /Za flag to cl.exe turns this off, but it's not + // implemented in clang. + CmdArgs.push_back("--dependent-lib=oldnames"); + } + + // All Arm64EC object files implicitly add softintrin.lib. This is necessary + // even if the file doesn't actually refer to any of the routines because + // the CRT itself has incomplete dependency markings. + if (TC.getTriple().isWindowsArm64EC()) + CmdArgs.push_back("--dependent-lib=softintrin"); } void Clang::ConstructJob(Compilation &C, const JobAction &JA, @@ -4069,44 +4899,57 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, const Driver &D = TC.getDriver(); ArgStringList CmdArgs; - // Check number of inputs for sanity. We need at least one input. assert(Inputs.size() >= 1 && "Must have at least one input."); // CUDA/HIP compilation may have multiple inputs (source file + results of // device-side compilations). OpenMP device jobs also take the host IR as a // second input. Module precompilation accepts a list of header files to - // include as part of the module. All other jobs are expected to have exactly - // one input. + // include as part of the module. API extraction accepts a list of header + // files whose API information is emitted in the output. All other jobs are + // expected to have exactly one input. bool IsCuda = JA.isOffloading(Action::OFK_Cuda); + bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda); bool IsHIP = JA.isOffloading(Action::OFK_HIP); + bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP); bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); - bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA); - - // A header module compilation doesn't have a main input file, so invent a - // fake one as a placeholder. - const char *ModuleName = [&]{ - auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ); - return ModuleNameArg ? ModuleNameArg->getValue() : ""; - }(); - InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName); + bool IsExtractAPI = isa<ExtractAPIJobAction>(JA); + bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || + JA.isDeviceOffloading(Action::OFK_Host)); + bool IsHostOffloadingAction = + JA.isHostOffloading(Action::OFK_OpenMP) || + (JA.isHostOffloading(C.getActiveOffloadKinds()) && + Args.hasFlag(options::OPT_offload_new_driver, + options::OPT_no_offload_new_driver, false)); + + bool IsRDCMode = + Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false); + bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction); + auto LTOMode = D.getLTOMode(IsDeviceOffloadAction); + + // Extract API doesn't have a main input file, so invent a fake one as a + // placeholder. + InputInfo ExtractAPIPlaceholderInput(Inputs[0].getType(), "extract-api", + "extract-api"); const InputInfo &Input = - IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0]; + IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0]; - InputInfoList ModuleHeaderInputs; + InputInfoList ExtractAPIInputs; + InputInfoList HostOffloadingInputs; const InputInfo *CudaDeviceInput = nullptr; const InputInfo *OpenMPDeviceInput = nullptr; for (const InputInfo &I : Inputs) { - if (&I == &Input) { - // This is the primary input. - } else if (IsHeaderModulePrecompile && - types::getPrecompiledType(I.getType()) == types::TY_PCH) { - types::ID Expected = HeaderModuleInput.getType(); - if (I.getType() != Expected) { - D.Diag(diag::err_drv_module_header_wrong_kind) + if (&I == &Input || I.getType() == types::TY_Nothing) { + // This is the primary input or contains nothing. + } else if (IsExtractAPI) { + auto ExpectedInputType = ExtractAPIPlaceholderInput.getType(); + if (I.getType() != ExpectedInputType) { + D.Diag(diag::err_drv_extract_api_wrong_kind) << I.getFilename() << types::getTypeName(I.getType()) - << types::getTypeName(Expected); + << types::getTypeName(ExpectedInputType); } - ModuleHeaderInputs.push_back(I); + ExtractAPIInputs.push_back(I); + } else if (IsHostOffloadingAction) { + HostOffloadingInputs.push_back(I); } else if ((IsCuda || IsHIP) && !CudaDeviceInput) { CudaDeviceInput = &I; } else if (IsOpenMPDevice && !OpenMPDeviceInput) { @@ -4176,21 +5019,46 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(Args.MakeArgString( Twine("-target-sdk-version=") + CudaVersionToString(CTC->CudaInstallation.version()))); + // Unsized function arguments used for variadics were introduced in + // CUDA-9.0. We still do not support generating code that actually uses + // variadic arguments yet, but we do need to allow parsing them as + // recent CUDA headers rely on that. + // https://github.com/llvm/llvm-project/issues/58410 + if (CTC->CudaInstallation.version() >= CudaVersion::CUDA_90) + CmdArgs.push_back("-fcuda-allow-variadic-functions"); } } CmdArgs.push_back("-aux-triple"); CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); + + if (JA.isDeviceOffloading(Action::OFK_HIP) && + (getToolChain().getTriple().isAMDGPU() || + (getToolChain().getTriple().isSPIRV() && + getToolChain().getTriple().getVendor() == llvm::Triple::AMD))) { + // Device side compilation printf + if (Args.getLastArg(options::OPT_mprintf_kind_EQ)) { + CmdArgs.push_back(Args.MakeArgString( + "-mprintf-kind=" + + Args.getLastArgValue(options::OPT_mprintf_kind_EQ))); + // Force compiler error on invalid conversion specifiers + CmdArgs.push_back( + Args.MakeArgString("-Werror=format-invalid-specifier")); + } + } } + // Unconditionally claim the printf option now to avoid unused diagnostic. + if (const Arg *PF = Args.getLastArg(options::OPT_mprintf_kind_EQ)) + PF->claim(); + if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { - CmdArgs.push_back("-fsycl"); CmdArgs.push_back("-fsycl-is-device"); if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { A->render(Args, CmdArgs); } else { - // Ensure the default version in SYCL mode is 1.2.1 (aka 2017) - CmdArgs.push_back("-sycl-std=2017"); + // Ensure the default version in SYCL mode is 2020. + CmdArgs.push_back("-sycl-std=2020"); } } @@ -4220,9 +5088,21 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // are provided. TC.addClangWarningOptions(CmdArgs); + // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions. + if (Triple.isSPIR() || Triple.isSPIRV()) + CmdArgs.push_back("-Wspir-compat"); + // Select the appropriate action. RewriteKind rewriteKind = RK_None; + bool UnifiedLTO = false; + if (IsUsingLTO) { + UnifiedLTO = Args.hasFlag(options::OPT_funified_lto, + options::OPT_fno_unified_lto, Triple.isPS()); + if (UnifiedLTO) + CmdArgs.push_back("-funified-lto"); + } + // If CollectArgsForIntegratedAssembler() isn't called below, claim the args // it claims when not running an assembler. Otherwise, clang would emit // "argument unused" warnings for assembler flags when e.g. adding "-E" to @@ -4251,6 +5131,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } Args.ClaimAllArgs(options::OPT_Wa_COMMA); Args.ClaimAllArgs(options::OPT_Xassembler); + Args.ClaimAllArgs(options::OPT_femit_dwarf_unwind_EQ); } if (isa<AnalyzeJobAction>(JA)) { @@ -4266,6 +5147,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (Args.hasArg(options::OPT_rewrite_objc) && !Args.hasArg(options::OPT_g_Group)) CmdArgs.push_back("-P"); + else if (JA.getType() == types::TY_PP_CXXHeaderUnit) + CmdArgs.push_back("-fdirectives-only"); } } else if (isa<AssembleJobAction>(JA)) { CmdArgs.push_back("-emit-obj"); @@ -4278,13 +5161,37 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (JA.getType() == types::TY_Nothing) CmdArgs.push_back("-fsyntax-only"); else if (JA.getType() == types::TY_ModuleFile) - CmdArgs.push_back(IsHeaderModulePrecompile - ? "-emit-header-module" - : "-emit-module-interface"); + CmdArgs.push_back("-emit-module-interface"); + else if (JA.getType() == types::TY_HeaderUnit) + CmdArgs.push_back("-emit-header-unit"); else CmdArgs.push_back("-emit-pch"); } else if (isa<VerifyPCHJobAction>(JA)) { CmdArgs.push_back("-verify-pch"); + } else if (isa<ExtractAPIJobAction>(JA)) { + assert(JA.getType() == types::TY_API_INFO && + "Extract API actions must generate a API information."); + CmdArgs.push_back("-extract-api"); + + if (Arg *PrettySGFArg = Args.getLastArg(options::OPT_emit_pretty_sgf)) + PrettySGFArg->render(Args, CmdArgs); + + Arg *SymbolGraphDirArg = Args.getLastArg(options::OPT_symbol_graph_dir_EQ); + + if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ)) + ProductNameArg->render(Args, CmdArgs); + if (Arg *ExtractAPIIgnoresFileArg = + Args.getLastArg(options::OPT_extract_api_ignores_EQ)) + ExtractAPIIgnoresFileArg->render(Args, CmdArgs); + if (Arg *EmitExtensionSymbolGraphs = + Args.getLastArg(options::OPT_emit_extension_symbol_graphs)) { + if (!SymbolGraphDirArg) + D.Diag(diag::err_drv_missing_symbol_graph_dir); + + EmitExtensionSymbolGraphs->render(Args, CmdArgs); + } + if (SymbolGraphDirArg) + SymbolGraphDirArg->render(Args, CmdArgs); } else { assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) && "Invalid action for clang tool."); @@ -4295,13 +5202,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-emit-llvm"); } else if (JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC) { - CmdArgs.push_back("-emit-llvm-bc"); + // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S + if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) && + Args.hasArg(options::OPT_emit_llvm)) { + CmdArgs.push_back("-emit-llvm"); + } else { + CmdArgs.push_back("-emit-llvm-bc"); + } } else if (JA.getType() == types::TY_IFS || JA.getType() == types::TY_IFS_CPP) { StringRef ArgStr = Args.hasArg(options::OPT_interface_stub_version_EQ) ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ) - : "experimental-ifs-v2"; + : "ifs-v1"; CmdArgs.push_back("-emit-interface-stubs"); CmdArgs.push_back( Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str())); @@ -4328,22 +5241,48 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (JA.getType() == types::TY_LLVM_BC) CmdArgs.push_back("-emit-llvm-uselists"); - // Device-side jobs do not support LTO. - bool isDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || - JA.isDeviceOffloading(Action::OFK_Host)); - - if (D.isUsingLTO() && !isDeviceOffloadAction) { - Args.AddLastArg(CmdArgs, options::OPT_flto, options::OPT_flto_EQ); - CmdArgs.push_back("-flto-unit"); + if (IsUsingLTO) { + if (IsDeviceOffloadAction && !JA.isDeviceOffloading(Action::OFK_OpenMP) && + !Args.hasFlag(options::OPT_offload_new_driver, + options::OPT_no_offload_new_driver, false) && + !Triple.isAMDGPU()) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << Args.getLastArg(options::OPT_foffload_lto, + options::OPT_foffload_lto_EQ) + ->getAsString(Args) + << Triple.getTriple(); + } else if (Triple.isNVPTX() && !IsRDCMode && + JA.isDeviceOffloading(Action::OFK_Cuda)) { + D.Diag(diag::err_drv_unsupported_opt_for_language_mode) + << Args.getLastArg(options::OPT_foffload_lto, + options::OPT_foffload_lto_EQ) + ->getAsString(Args) + << "-fno-gpu-rdc"; + } else { + assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); + CmdArgs.push_back(Args.MakeArgString( + Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); + // PS4 uses the legacy LTO API, which does not support some of the + // features enabled by -flto-unit. + if (!RawTriple.isPS4() || + (D.getLTOMode() == LTOK_Full) || !UnifiedLTO) + CmdArgs.push_back("-flto-unit"); + } } } + Args.AddLastArg(CmdArgs, options::OPT_dumpdir); + if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) { if (!types::isLLVMIR(Input.getType())) D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args); Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ); } + if (Triple.isPPC()) + Args.addOptInFlag(CmdArgs, options::OPT_mregnames, + options::OPT_mno_regnames); + if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ)) Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ); @@ -4357,9 +5296,21 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, !MemProfArg->getOption().matches(options::OPT_fno_memory_profile)) MemProfArg->render(Args, CmdArgs); + if (auto *MemProfUseArg = + Args.getLastArg(options::OPT_fmemory_profile_use_EQ)) { + if (MemProfArg) + D.Diag(diag::err_drv_argument_not_allowed_with) + << MemProfUseArg->getAsString(Args) << MemProfArg->getAsString(Args); + if (auto *PGOInstrArg = Args.getLastArg(options::OPT_fprofile_generate, + options::OPT_fprofile_generate_EQ)) + D.Diag(diag::err_drv_argument_not_allowed_with) + << MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args); + MemProfUseArg->render(Args, CmdArgs); + } + // Embed-bitcode option. // Only white-listed flags below are allowed to be embedded. - if (C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO() && + if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO && (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) { // Add flags implied by -fembed-bitcode. Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); @@ -4371,7 +5322,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // reject options that shouldn't be supported in bitcode // also reject kernel/kext - static const constexpr unsigned kBitcodeOptionBlacklist[] = { + static const constexpr unsigned kBitcodeOptionIgnorelist[] = { options::OPT_mkernel, options::OPT_fapple_kext, options::OPT_ffunction_sections, @@ -4415,14 +5366,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_mllvm, }; for (const auto &A : Args) - if (llvm::find(kBitcodeOptionBlacklist, A->getOption().getID()) != - std::end(kBitcodeOptionBlacklist)) + if (llvm::is_contained(kBitcodeOptionIgnorelist, A->getOption().getID())) D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling(); // Render the CodeGen options that need to be passed. - if (!Args.hasFlag(options::OPT_foptimize_sibling_calls, - options::OPT_fno_optimize_sibling_calls)) - CmdArgs.push_back("-mdisable-tail-calls"); + Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, + options::OPT_fno_optimize_sibling_calls); RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args, CmdArgs, JA); @@ -4433,7 +5382,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, case llvm::Triple::arm: case llvm::Triple::armeb: case llvm::Triple::thumbeb: - RenderARMABI(Triple, Args, CmdArgs); + RenderARMABI(D, Triple, Args, CmdArgs); break; case llvm::Triple::aarch64: case llvm::Triple::aarch64_32: @@ -4472,11 +5421,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, C.addCommand(std::make_unique<Command>( JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(), - CmdArgs, Inputs, Output)); + CmdArgs, Inputs, Output, D.getPrependArg())); return; } - if (C.getDriver().embedBitcodeMarkerOnly() && !C.getDriver().isUsingLTO()) + if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO) CmdArgs.push_back("-fembed-bitcode=marker"); // We normally speed up the clang process a bit by skipping destructors at @@ -4484,6 +5433,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // cleanup. if (!C.isForDiagnostics()) CmdArgs.push_back("-disable-free"); + CmdArgs.push_back("-clear-ast-before-backend"); #ifdef NDEBUG const bool IsAssertBuild = false; @@ -4491,18 +5441,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, const bool IsAssertBuild = true; #endif - // Disable the verification pass in -asserts builds. - if (!IsAssertBuild) + // Disable the verification pass in asserts builds unless otherwise specified. + if (Args.hasFlag(options::OPT_fno_verify_intermediate_code, + options::OPT_fverify_intermediate_code, !IsAssertBuild)) { CmdArgs.push_back("-disable-llvm-verifier"); + } // Discard value names in assert builds unless otherwise specified. if (Args.hasFlag(options::OPT_fdiscard_value_names, options::OPT_fno_discard_value_names, !IsAssertBuild)) { if (Args.hasArg(options::OPT_fdiscard_value_names) && - (std::any_of(Inputs.begin(), Inputs.end(), - [](const clang::driver::InputInfo &II) { - return types::isLLVMIR(II.getType()); - }))) { + llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) { + return types::isLLVMIR(II.getType()); + })) { D.Diag(diag::warn_ignoring_fdiscard_for_bitcode); } CmdArgs.push_back("-discard-value-names"); @@ -4532,13 +5483,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // Since we can't access frontend flags through hasArg, let's manually iterate // through them. bool FoundAnalyzerConfig = false; - for (auto Arg : Args.filtered(options::OPT_Xclang)) + for (auto *Arg : Args.filtered(options::OPT_Xclang)) if (StringRef(Arg->getValue()) == "-analyzer-config") { FoundAnalyzerConfig = true; break; } if (!FoundAnalyzerConfig) - for (auto Arg : Args.filtered(options::OPT_Xanalyzer)) + for (auto *Arg : Args.filtered(options::OPT_Xanalyzer)) if (StringRef(Arg->getValue()) == "-analyzer-config") { FoundAnalyzerConfig = true; break; @@ -4555,10 +5506,74 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment))); } + // We support -falign-loops=N where N is a power of 2. GCC supports more + // forms. + if (const Arg *A = Args.getLastArg(options::OPT_falign_loops_EQ)) { + unsigned Value = 0; + if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) + TC.getDriver().Diag(diag::err_drv_invalid_int_value) + << A->getAsString(Args) << A->getValue(); + else if (Value & (Value - 1)) + TC.getDriver().Diag(diag::err_drv_alignment_not_power_of_two) + << A->getAsString(Args) << A->getValue(); + // Treat =0 as unspecified (use the target preference). + if (Value) + CmdArgs.push_back(Args.MakeArgString("-falign-loops=" + + Twine(std::min(Value, 65536u)))); + } + + if (Triple.isOSzOS()) { + // On z/OS some of the system header feature macros need to + // be defined to enable most cross platform projects to build + // successfully. Ths include the libc++ library. A + // complicating factor is that users can define these + // macros to the same or different values. We need to add + // the definition for these macros to the compilation command + // if the user hasn't already defined them. + + auto findMacroDefinition = [&](const std::string &Macro) { + auto MacroDefs = Args.getAllArgValues(options::OPT_D); + return llvm::any_of(MacroDefs, [&](const std::string &M) { + return M == Macro || M.find(Macro + '=') != std::string::npos; + }); + }; + + // _UNIX03_WITHDRAWN is required for libcxx & porting. + if (!findMacroDefinition("_UNIX03_WITHDRAWN")) + CmdArgs.push_back("-D_UNIX03_WITHDRAWN"); + // _OPEN_DEFAULT is required for XL compat + if (!findMacroDefinition("_OPEN_DEFAULT")) + CmdArgs.push_back("-D_OPEN_DEFAULT"); + if (D.CCCIsCXX() || types::isCXX(Input.getType())) { + // _XOPEN_SOURCE=600 is required for libcxx. + if (!findMacroDefinition("_XOPEN_SOURCE")) + CmdArgs.push_back("-D_XOPEN_SOURCE=600"); + } + } + llvm::Reloc::Model RelocationModel; unsigned PICLevel; bool IsPIE; std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); + Arg *LastPICDataRelArg = + Args.getLastArg(options::OPT_mno_pic_data_is_text_relative, + options::OPT_mpic_data_is_text_relative); + bool NoPICDataIsTextRelative = false; + if (LastPICDataRelArg) { + if (LastPICDataRelArg->getOption().matches( + options::OPT_mno_pic_data_is_text_relative)) { + NoPICDataIsTextRelative = true; + if (!PICLevel) + D.Diag(diag::err_drv_argument_only_allowed_with) + << "-mno-pic-data-is-text-relative" + << "-fpic/-fpie"; + } + if (!Triple.isSystemZ()) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << (NoPICDataIsTextRelative ? "-mno-pic-data-is-text-relative" + : "-mpic-data-is-text-relative") + << RawTriple.str(); + } bool IsROPI = RelocationModel == llvm::Reloc::ROPI || RelocationModel == llvm::Reloc::ROPI_RWPI; @@ -4587,6 +5602,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); if (IsPIE) CmdArgs.push_back("-pic-is-pie"); + if (NoPICDataIsTextRelative) + CmdArgs.push_back("-mcmodel=medium"); } if (RelocationModel == llvm::Reloc::ROPI || @@ -4617,7 +5634,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_semantic_interposition); if (RelocationModel != llvm::Reloc::Static && !IsPIE) { // The supported targets need to call AsmPrinter::getSymbolPreferLocal. - bool SupportsLocalAlias = Triple.isX86(); + bool SupportsLocalAlias = + Triple.isAArch64() || Triple.isRISCV() || Triple.isX86(); if (!A) CmdArgs.push_back("-fhalf-no-semantic-interposition"); else if (A->getOption().matches(options::OPT_fsemantic_interposition)) @@ -4642,62 +5660,67 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } - Args.AddLastArg(CmdArgs, options::OPT_fveclib); + if (Arg *A = Args.getLastArg(options::OPT_fveclib)) { + StringRef Name = A->getValue(); + if (Name == "SVML") { + if (Triple.getArch() != llvm::Triple::x86 && + Triple.getArch() != llvm::Triple::x86_64) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << Name << Triple.getArchName(); + } else if (Name == "LIBMVEC-X86") { + if (Triple.getArch() != llvm::Triple::x86 && + Triple.getArch() != llvm::Triple::x86_64) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << Name << Triple.getArchName(); + } else if (Name == "SLEEF" || Name == "ArmPL") { + if (Triple.getArch() != llvm::Triple::aarch64 && + Triple.getArch() != llvm::Triple::aarch64_be) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << Name << Triple.getArchName(); + } + A->render(Args, CmdArgs); + } if (Args.hasFlag(options::OPT_fmerge_all_constants, options::OPT_fno_merge_all_constants, false)) CmdArgs.push_back("-fmerge-all-constants"); - if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks, - options::OPT_fdelete_null_pointer_checks, false)) - CmdArgs.push_back("-fno-delete-null-pointer-checks"); + Args.addOptOutFlag(CmdArgs, options::OPT_fdelete_null_pointer_checks, + options::OPT_fno_delete_null_pointer_checks); // LLVM Code Generator Options. - if (Args.hasArg(options::OPT_frewrite_map_file) || - Args.hasArg(options::OPT_frewrite_map_file_EQ)) { - for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file, - options::OPT_frewrite_map_file_EQ)) { - StringRef Map = A->getValue(); - if (!llvm::sys::fs::exists(Map)) { - D.Diag(diag::err_drv_no_such_file) << Map; - } else { - CmdArgs.push_back("-frewrite-map-file"); - CmdArgs.push_back(A->getValue()); - A->claim(); - } - } + if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_quadword_atomics)) { + if (!Triple.isOSAIX() || Triple.isPPC32()) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getSpelling() << RawTriple.str(); + CmdArgs.push_back("-mabi=quadword-atomics"); } - if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_vec_extabi, - options::OPT_mabi_EQ_vec_default)) { - if (!Triple.isOSAIX()) + if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) { + // Emit the unsupported option error until the Clang's library integration + // support for 128-bit long double is available for AIX. + if (Triple.isOSAIX()) D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getSpelling() << RawTriple.str(); - if (A->getOption().getID() == options::OPT_mabi_EQ_vec_extabi) - CmdArgs.push_back("-mabi=vec-extabi"); - else - D.Diag(diag::err_aix_default_altivec_abi); } if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) { - StringRef v = A->getValue(); - CmdArgs.push_back("-mllvm"); - CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v)); - A->claim(); + StringRef V = A->getValue(), V1 = V; + unsigned Size; + if (V1.consumeInteger(10, Size) || !V1.empty()) + D.Diag(diag::err_drv_invalid_argument_to_option) + << V << A->getOption().getName(); + else + CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + V)); } - if (!Args.hasFlag(options::OPT_fjump_tables, options::OPT_fno_jump_tables, - true)) - CmdArgs.push_back("-fno-jump-tables"); - - if (Args.hasFlag(options::OPT_fprofile_sample_accurate, - options::OPT_fno_profile_sample_accurate, false)) - CmdArgs.push_back("-fprofile-sample-accurate"); - - if (!Args.hasFlag(options::OPT_fpreserve_as_comments, - options::OPT_fno_preserve_as_comments, true)) - CmdArgs.push_back("-fno-preserve-as-comments"); + Args.addOptOutFlag(CmdArgs, options::OPT_fjump_tables, + options::OPT_fno_jump_tables); + Args.addOptInFlag(CmdArgs, options::OPT_fprofile_sample_accurate, + options::OPT_fno_profile_sample_accurate); + Args.addOptOutFlag(CmdArgs, options::OPT_fpreserve_as_comments, + options::OPT_fno_preserve_as_comments); if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { CmdArgs.push_back("-mregparm"); @@ -4730,8 +5753,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } - if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) - CmdArgs.push_back("-fdefault-calling-conv=stdcall"); + if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) { + if (Triple.getArch() == llvm::Triple::m68k) + CmdArgs.push_back("-fdefault-calling-conv=rtdcall"); + else + CmdArgs.push_back("-fdefault-calling-conv=stdcall"); + } if (Args.hasArg(options::OPT_fenable_matrix)) { // enable-matrix is needed by both the LangOpts and by LLVM. @@ -4747,6 +5774,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, case CodeGenOptions::FramePointerKind::None: FPKeepKindStr = "-mframe-pointer=none"; break; + case CodeGenOptions::FramePointerKind::Reserved: + FPKeepKindStr = "-mframe-pointer=reserved"; + break; case CodeGenOptions::FramePointerKind::NonLeaf: FPKeepKindStr = "-mframe-pointer=non-leaf"; break; @@ -4757,57 +5787,79 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, assert(FPKeepKindStr && "unknown FramePointerKind"); CmdArgs.push_back(FPKeepKindStr); - if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, - options::OPT_fno_zero_initialized_in_bss, true)) - CmdArgs.push_back("-fno-zero-initialized-in-bss"); + Args.addOptOutFlag(CmdArgs, options::OPT_fzero_initialized_in_bss, + options::OPT_fno_zero_initialized_in_bss); bool OFastEnabled = isOptimizationLevelFast(Args); + if (OFastEnabled) + D.Diag(diag::warn_drv_deprecated_arg_ofast); // If -Ofast is the optimization level, then -fstrict-aliasing should be // enabled. This alias option is being used to simplify the hasFlag logic. OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing; - // We turn strict aliasing off by default if we're in CL mode, since MSVC + // We turn strict aliasing off by default if we're Windows MSVC since MSVC // doesn't do any TBAA. - bool TBAAOnByDefault = !D.IsCLMode(); if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, - options::OPT_fno_strict_aliasing, TBAAOnByDefault)) + options::OPT_fno_strict_aliasing, !IsWindowsMSVC)) CmdArgs.push_back("-relaxed-aliasing"); + if (Args.hasFlag(options::OPT_fpointer_tbaa, options::OPT_fno_pointer_tbaa, + false)) + CmdArgs.push_back("-pointer-tbaa"); if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, - options::OPT_fno_struct_path_tbaa)) + options::OPT_fno_struct_path_tbaa, true)) CmdArgs.push_back("-no-struct-path-tbaa"); - if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums, - false)) - CmdArgs.push_back("-fstrict-enums"); - if (!Args.hasFlag(options::OPT_fstrict_return, options::OPT_fno_strict_return, - true)) - CmdArgs.push_back("-fno-strict-return"); - if (Args.hasFlag(options::OPT_fallow_editor_placeholders, - options::OPT_fno_allow_editor_placeholders, false)) - CmdArgs.push_back("-fallow-editor-placeholders"); - if (Args.hasFlag(options::OPT_fstrict_vtable_pointers, - options::OPT_fno_strict_vtable_pointers, - false)) - CmdArgs.push_back("-fstrict-vtable-pointers"); - if (Args.hasFlag(options::OPT_fforce_emit_vtables, - options::OPT_fno_force_emit_vtables, - false)) - CmdArgs.push_back("-fforce-emit-vtables"); - if (!Args.hasFlag(options::OPT_foptimize_sibling_calls, - options::OPT_fno_optimize_sibling_calls)) - CmdArgs.push_back("-mdisable-tail-calls"); - if (Args.hasFlag(options::OPT_fno_escaping_block_tail_calls, - options::OPT_fescaping_block_tail_calls, false)) - CmdArgs.push_back("-fno-escaping-block-tail-calls"); + Args.addOptInFlag(CmdArgs, options::OPT_fstrict_enums, + options::OPT_fno_strict_enums); + Args.addOptOutFlag(CmdArgs, options::OPT_fstrict_return, + options::OPT_fno_strict_return); + Args.addOptInFlag(CmdArgs, options::OPT_fallow_editor_placeholders, + options::OPT_fno_allow_editor_placeholders); + Args.addOptInFlag(CmdArgs, options::OPT_fstrict_vtable_pointers, + options::OPT_fno_strict_vtable_pointers); + Args.addOptInFlag(CmdArgs, options::OPT_fforce_emit_vtables, + options::OPT_fno_force_emit_vtables); + Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, + options::OPT_fno_optimize_sibling_calls); + Args.addOptOutFlag(CmdArgs, options::OPT_fescaping_block_tail_calls, + options::OPT_fno_escaping_block_tail_calls); Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses, options::OPT_fno_fine_grained_bitfield_accesses); + Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, + options::OPT_fno_experimental_relative_cxx_abi_vtables); + + Args.AddLastArg(CmdArgs, options::OPT_fexperimental_omit_vtable_rtti, + options::OPT_fno_experimental_omit_vtable_rtti); + + Args.AddLastArg(CmdArgs, options::OPT_fdisable_block_signature_string, + options::OPT_fno_disable_block_signature_string); + // Handle segmented stacks. - if (Args.hasArg(options::OPT_fsplit_stack)) - CmdArgs.push_back("-split-stacks"); + Args.addOptInFlag(CmdArgs, options::OPT_fsplit_stack, + options::OPT_fno_split_stack); + + // -fprotect-parens=0 is default. + if (Args.hasFlag(options::OPT_fprotect_parens, + options::OPT_fno_protect_parens, false)) + CmdArgs.push_back("-fprotect-parens"); RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA); + if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) { + const llvm::Triple::ArchType Arch = TC.getArch(); + if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { + StringRef V = A->getValue(); + if (V == "64") + CmdArgs.push_back("-fextend-arguments=64"); + else if (V != "32") + D.Diag(diag::err_drv_invalid_argument_to_option) + << A->getValue() << A->getOption().getName(); + } else + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getOption().getName() << TripleStr; + } + if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) { if (TC.getArch() == llvm::Triple::avr) A->render(Args, CmdArgs); @@ -4850,7 +5902,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, << A->getValue() << A->getOption().getName(); } - if (!TC.useIntegratedAs()) + // If toolchain choose to use MCAsmParser for inline asm don't pass the + // option to disable integrated-as explicitly. + if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser()) CmdArgs.push_back("-no-integrated-as"); if (Args.hasArg(options::OPT_fdebug_pass_structure)) { @@ -4863,9 +5917,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } // Enable -mconstructor-aliases except on darwin, where we have to work around - // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where - // aliases aren't supported. Similarly, aliases aren't yet supported for AIX. - if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX() && !RawTriple.isOSAIX()) + // a linker bug (see https://openradar.appspot.com/7198997), and CUDA device + // code, where aliases aren't supported. + if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX()) CmdArgs.push_back("-mconstructor-aliases"); // Darwin's kernel doesn't support guard variables; just die if we @@ -4878,14 +5932,28 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-mms-bitfields"); } + if (Triple.isWindowsGNUEnvironment()) { + Args.addOptOutFlag(CmdArgs, options::OPT_fauto_import, + options::OPT_fno_auto_import); + } + + if (Args.hasFlag(options::OPT_fms_volatile, options::OPT_fno_ms_volatile, + Triple.isX86() && D.IsCLMode())) + CmdArgs.push_back("-fms-volatile"); + // Non-PIC code defaults to -fdirect-access-external-data while PIC code // defaults to -fno-direct-access-external-data. Pass the option if different // from the default. if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, - options::OPT_fno_direct_access_external_data)) + options::OPT_fno_direct_access_external_data)) { if (A->getOption().matches(options::OPT_fdirect_access_external_data) != (PICLevel == 0)) A->render(Args, CmdArgs); + } else if (PICLevel == 0 && Triple.isLoongArch()) { + // Some targets default to -fno-direct-access-external-data even for + // -fno-pic. + CmdArgs.push_back("-fno-direct-access-external-data"); + } if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { CmdArgs.push_back("-fno-plt"); @@ -4900,28 +5968,39 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (Freestanding) CmdArgs.push_back("-ffreestanding"); + Args.AddLastArg(CmdArgs, options::OPT_fno_knr_functions); + // This is a coarse approximation of what llvm-gcc actually does, both // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more // complicated ways. - bool AsynchronousUnwindTables = - Args.hasFlag(options::OPT_fasynchronous_unwind_tables, - options::OPT_fno_asynchronous_unwind_tables, - (TC.IsUnwindTablesDefault(Args) || - TC.getSanitizerArgs().needsUnwindTables()) && - !Freestanding); - if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, - AsynchronousUnwindTables)) - CmdArgs.push_back("-munwind-tables"); + auto SanitizeArgs = TC.getSanitizerArgs(Args); + + bool IsAsyncUnwindTablesDefault = + TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Asynchronous; + bool IsSyncUnwindTablesDefault = + TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Synchronous; + + bool AsyncUnwindTables = Args.hasFlag( + options::OPT_fasynchronous_unwind_tables, + options::OPT_fno_asynchronous_unwind_tables, + (IsAsyncUnwindTablesDefault || SanitizeArgs.needsUnwindTables()) && + !Freestanding); + bool UnwindTables = + Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, + IsSyncUnwindTablesDefault && !Freestanding); + if (AsyncUnwindTables) + CmdArgs.push_back("-funwind-tables=2"); + else if (UnwindTables) + CmdArgs.push_back("-funwind-tables=1"); // Prepare `-aux-target-cpu` and `-aux-target-feature` unless // `--gpu-use-aux-triple-only` is specified. if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) && - ((IsCuda && JA.isDeviceOffloading(Action::OFK_Cuda)) || - (IsHIP && JA.isDeviceOffloading(Action::OFK_HIP)))) { + (IsCudaDevice || IsHIPDevice)) { const ArgList &HostArgs = C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None); std::string HostCPU = - getCPUName(HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); + getCPUName(D, HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); if (!HostCPU.empty()) { CmdArgs.push_back("-aux-target-cpu"); CmdArgs.push_back(Args.MakeArgString(HostCPU)); @@ -4932,18 +6011,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); - // FIXME: Handle -mtune=. - (void)Args.hasArg(options::OPT_mtune_EQ); - - if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { - StringRef CM = A->getValue(); - if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" || - CM == "tiny") - A->render(Args, CmdArgs); - else - D.Diag(diag::err_drv_invalid_argument_to_option) - << CM << A->getOption().getName(); - } + addMCModel(D, Args, Triple, RelocationModel, CmdArgs); if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { StringRef Value = A->getValue(); @@ -4958,8 +6026,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ); } + if (isTLSDESCEnabled(TC, Args)) + CmdArgs.push_back("-enable-tlsdesc"); + // Add the target cpu - std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false); + std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); if (!CPU.empty()) { CmdArgs.push_back("-target-cpu"); CmdArgs.push_back(Args.MakeArgString(CPU)); @@ -4967,25 +6038,23 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs); - // These two are potentially updated by AddClangCLArgs. - codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; - bool EmitCodeView = false; - // Add clang-cl arguments. types::ID InputType = Input.getType(); if (D.IsCLMode()) - AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView); + AddClangCLArgs(Args, InputType, CmdArgs); + llvm::codegenoptions::DebugInfoKind DebugInfoKind = + llvm::codegenoptions::NoDebugInfo; DwarfFissionKind DwarfFission = DwarfFissionKind::None; - renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, - types::isLLVMIR(InputType), CmdArgs, DebugInfoKind, - DwarfFission); + renderDebugOptions(TC, D, RawTriple, Args, types::isLLVMIR(InputType), + CmdArgs, Output, DebugInfoKind, DwarfFission); // Add the split debug info name to the command lines here so we // can propagate it to the backend. bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) && (TC.getTriple().isOSBinFormatELF() || - TC.getTriple().isOSBinFormatWasm()) && + TC.getTriple().isOSBinFormatWasm() || + TC.getTriple().isOSBinFormatCOFF()) && (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)); if (SplitDWARF) { @@ -5031,20 +6100,30 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-H"); CmdArgs.push_back("-sys-header-deps"); } + Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes); - if (D.CCPrintHeaders && !D.CCGenDiagnostics) { + if (D.CCPrintHeadersFormat && !D.CCGenDiagnostics) { CmdArgs.push_back("-header-include-file"); - CmdArgs.push_back(D.CCPrintHeadersFilename ? D.CCPrintHeadersFilename - : "-"); + CmdArgs.push_back(!D.CCPrintHeadersFilename.empty() + ? D.CCPrintHeadersFilename.c_str() + : "-"); CmdArgs.push_back("-sys-header-deps"); + CmdArgs.push_back(Args.MakeArgString( + "-header-include-format=" + + std::string(headerIncludeFormatKindToString(D.CCPrintHeadersFormat)))); + CmdArgs.push_back( + Args.MakeArgString("-header-include-filtering=" + + std::string(headerIncludeFilteringKindToString( + D.CCPrintHeadersFiltering)))); } Args.AddLastArg(CmdArgs, options::OPT_P); Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { CmdArgs.push_back("-diagnostic-log-file"); - CmdArgs.push_back(D.CCLogDiagnosticsFilename ? D.CCLogDiagnosticsFilename - : "-"); + CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty() + ? D.CCLogDiagnosticsFilename.c_str() + : "-"); } // Give the gen diagnostics more chances to succeed, by avoiding intentional @@ -5052,6 +6131,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (D.CCGenDiagnostics) CmdArgs.push_back("-disable-pragma-debug-crash"); + // Allow backend to put its diagnostic files in the same place as frontend + // crash diagnostics files. + if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) { + StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir); + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir)); + } + bool UseSeparateSections = isUseSeparateSections(Triple); if (Args.hasFlag(options::OPT_ffunction_sections, @@ -5059,16 +6146,40 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-ffunction-sections"); } + if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_address_map, + options::OPT_fno_basic_block_address_map)) { + if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF()) { + if (A->getOption().matches(options::OPT_fbasic_block_address_map)) + A->render(Args, CmdArgs); + } else { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; + } + } + if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) { + StringRef Val = A->getValue(); if (Triple.isX86() && Triple.isOSBinFormatELF()) { - StringRef Val = A->getValue(); if (Val != "all" && Val != "labels" && Val != "none" && - !Val.startswith("list=")) + !Val.starts_with("list=")) D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << A->getValue(); else A->render(Args, CmdArgs); - } else { + } else if (Triple.isAArch64() && Triple.isOSBinFormatELF()) { + // "all" is not supported on AArch64 since branch relaxation creates new + // basic blocks for some cross-section branches. + if (Val != "labels" && Val != "none" && !Val.starts_with("list=")) + D.Diag(diag::err_drv_invalid_value) + << A->getAsString(Args) << A->getValue(); + else + A->render(Args, CmdArgs); + } else if (Triple.isNVPTX()) { + // Do not pass the option to the GPU compilation. We still want it enabled + // for the host-side compilation, so seeing it here is not an error. + } else if (Val != "none") { + // =none is allowed everywhere. It's useful for overriding the option + // and is the same as not specifying the option. D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) << TripleStr; } @@ -5080,27 +6191,26 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fdata-sections"); } - if (!Args.hasFlag(options::OPT_funique_section_names, - options::OPT_fno_unique_section_names, true)) - CmdArgs.push_back("-fno-unique-section-names"); - - if (Args.hasFlag(options::OPT_funique_internal_linkage_names, - options::OPT_fno_unique_internal_linkage_names, false)) - CmdArgs.push_back("-funique-internal-linkage-names"); - - if (Args.hasFlag(options::OPT_funique_basic_block_section_names, - options::OPT_fno_unique_basic_block_section_names, false)) - CmdArgs.push_back("-funique-basic-block-section-names"); + Args.addOptOutFlag(CmdArgs, options::OPT_funique_section_names, + options::OPT_fno_unique_section_names); + Args.addOptInFlag(CmdArgs, options::OPT_fseparate_named_sections, + options::OPT_fno_separate_named_sections); + Args.addOptInFlag(CmdArgs, options::OPT_funique_internal_linkage_names, + options::OPT_fno_unique_internal_linkage_names); + Args.addOptInFlag(CmdArgs, options::OPT_funique_basic_block_section_names, + options::OPT_fno_unique_basic_block_section_names); + Args.addOptInFlag(CmdArgs, options::OPT_fconvergent_functions, + options::OPT_fno_convergent_functions); if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, options::OPT_fno_split_machine_functions)) { - // This codegen pass is only available on x86-elf targets. - if (Triple.isX86() && Triple.isOSBinFormatELF()) { - if (A->getOption().matches(options::OPT_fsplit_machine_functions)) + if (!A->getOption().matches(options::OPT_fno_split_machine_functions)) { + // This codegen pass is only available on x86 and AArch64 ELF targets. + if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF()) A->render(Args, CmdArgs); - } else { - D.Diag(diag::err_drv_unsupported_opt_for_target) - << A->getAsString(Args) << TripleStr; + else + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; } } @@ -5112,15 +6222,21 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // for sampling, overhead of call arc collection is way too high and there's // no way to collect the output. if (!Triple.isNVPTX() && !Triple.isAMDGCN()) - addPGOAndCoverageFlags(TC, C, D, Output, Args, CmdArgs); + addPGOAndCoverageFlags(TC, C, JA, Output, Args, SanitizeArgs, CmdArgs); Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); - // Add runtime flag for PS4 when PGO, coverage, or sanitizers are enabled. - if (RawTriple.isPS4CPU() && + if (getLastProfileSampleUseArg(Args) && + Args.hasArg(options::OPT_fsample_profile_use_profi)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("-sample-profile-use-profi"); + } + + // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled. + if (RawTriple.isPS() && !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { - PS4cpu::addProfileRTArgs(TC, Args, CmdArgs); - PS4cpu::addSanitizerArgs(TC, CmdArgs); + PScpu::addProfileRTArgs(TC, Args, CmdArgs); + PScpu::addSanitizerArgs(TC, Args, CmdArgs); } // Pass options for controlling the default header search paths. @@ -5182,22 +6298,54 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddAllArgs(CmdArgs, options::OPT_R_Group); - Args.AddAllArgs(CmdArgs, options::OPT_W_Group); + for (const Arg *A : + Args.filtered(options::OPT_W_Group, options::OPT__SLASH_wd)) { + A->claim(); + if (A->getOption().getID() == options::OPT__SLASH_wd) { + unsigned WarningNumber; + if (StringRef(A->getValue()).getAsInteger(10, WarningNumber)) { + D.Diag(diag::err_drv_invalid_int_value) + << A->getAsString(Args) << A->getValue(); + continue; + } + + if (auto Group = diagGroupFromCLWarningID(WarningNumber)) { + CmdArgs.push_back(Args.MakeArgString( + "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group))); + } + continue; + } + A->render(Args, CmdArgs); + } + + Args.AddAllArgs(CmdArgs, options::OPT_Wsystem_headers_in_module_EQ); + if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) CmdArgs.push_back("-pedantic"); Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); Args.AddLastArg(CmdArgs, options::OPT_w); - // Fixed point flags - if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point, - /*Default=*/false)) - Args.AddLastArg(CmdArgs, options::OPT_ffixed_point); + Args.addOptInFlag(CmdArgs, options::OPT_ffixed_point, + options::OPT_fno_fixed_point); + + if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ)) + A->render(Args, CmdArgs); + + Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, + options::OPT_fno_experimental_relative_cxx_abi_vtables); + + Args.AddLastArg(CmdArgs, options::OPT_fexperimental_omit_vtable_rtti, + options::OPT_fno_experimental_omit_vtable_rtti); + + if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) + A->render(Args, CmdArgs); // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} // (-ansi is equivalent to -std=c89 or -std=c++98). // // If a std is supplied, only add -trigraphs if it follows the // option. + bool ImplyVCPPCVer = false; bool ImplyVCPPCXXVer = false; const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); if (Std) { @@ -5222,19 +6370,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // doesn't match. For the time being just ignore this for C++ inputs; // eventually we want to do all the standard defaulting here instead of // splitting it between the driver and clang -cc1. - if (!types::isCXX(InputType)) - Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", - /*Joined=*/true); + if (!types::isCXX(InputType)) { + if (!Args.hasArg(options::OPT__SLASH_std)) { + Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", + /*Joined=*/true); + } else + ImplyVCPPCVer = true; + } else if (IsWindowsMSVC) ImplyVCPPCXXVer = true; Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs, options::OPT_fno_trigraphs); - - // HIP headers has minimum C++ standard requirements. Therefore set the - // default language standard. - if (IsHIP) - CmdArgs.push_back(IsWindowsMSVC ? "-std=c++14" : "-std=c++11"); } // GCC's behavior for -Wwrite-strings is a bit strange: @@ -5274,37 +6421,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fno-gnu-keywords"); } - if (ShouldDisableDwarfDirectory(Args, TC)) - CmdArgs.push_back("-fno-dwarf-directory-asm"); - if (!ShouldEnableAutolink(Args, TC, JA)) CmdArgs.push_back("-fno-autolink"); - // Add in -fdebug-compilation-dir if necessary. - addDebugCompDirArg(Args, CmdArgs, D.getVFS()); - - addDebugPrefixMapArg(D, Args, CmdArgs); + Args.AddLastArg(CmdArgs, options::OPT_ftemplate_depth_EQ); + Args.AddLastArg(CmdArgs, options::OPT_foperator_arrow_depth_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_depth_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_steps_EQ); - if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, - options::OPT_ftemplate_depth_EQ)) { - CmdArgs.push_back("-ftemplate-depth"); - CmdArgs.push_back(A->getValue()); - } - - if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) { - CmdArgs.push_back("-foperator-arrow-depth"); - CmdArgs.push_back(A->getValue()); - } - - if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) { - CmdArgs.push_back("-fconstexpr-depth"); - CmdArgs.push_back(A->getValue()); - } - - if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) { - CmdArgs.push_back("-fconstexpr-steps"); - CmdArgs.push_back(A->getValue()); - } + Args.AddLastArg(CmdArgs, options::OPT_fexperimental_library); if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter)) CmdArgs.push_back("-fexperimental-new-constant-interpreter"); @@ -5331,7 +6456,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1", }; - if (find(kCFABIs, StringRef(A->getValue())) == std::end(kCFABIs)) + if (!llvm::is_contained(kCFABIs, StringRef(A->getValue()))) D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue(); else A->render(Args, CmdArgs); @@ -5347,9 +6472,20 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(A->getValue()); } - if (Args.hasFlag(options::OPT_fstack_size_section, - options::OPT_fno_stack_size_section, RawTriple.isPS4())) - CmdArgs.push_back("-fstack-size-section"); + Args.addOptInFlag(CmdArgs, options::OPT_fstack_size_section, + options::OPT_fno_stack_size_section); + + if (Args.hasArg(options::OPT_fstack_usage)) { + CmdArgs.push_back("-stack-usage-file"); + + if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { + SmallString<128> OutputFilename(OutputOpt->getValue()); + llvm::sys::path::replace_extension(OutputFilename, "su"); + CmdArgs.push_back(Args.MakeArgString(OutputFilename)); + } else + CmdArgs.push_back( + Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su")); + } CmdArgs.push_back("-ferror-limit"); if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) @@ -5357,25 +6493,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, else CmdArgs.push_back("19"); - if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) { - CmdArgs.push_back("-fmacro-backtrace-limit"); - CmdArgs.push_back(A->getValue()); - } - - if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) { - CmdArgs.push_back("-ftemplate-backtrace-limit"); - CmdArgs.push_back(A->getValue()); - } - - if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) { - CmdArgs.push_back("-fconstexpr-backtrace-limit"); - CmdArgs.push_back(A->getValue()); - } - - if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) { - CmdArgs.push_back("-fspell-checking-limit"); - CmdArgs.push_back(A->getValue()); - } + Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_backtrace_limit_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fmacro_backtrace_limit_EQ); + Args.AddLastArg(CmdArgs, options::OPT_ftemplate_backtrace_limit_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fspell_checking_limit_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fcaret_diagnostics_max_lines_EQ); // Pass -fmessage-length=. unsigned MessageLength = 0; @@ -5393,22 +6515,33 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back( Args.MakeArgString("-fmessage-length=" + Twine(MessageLength))); + if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_EQ)) + CmdArgs.push_back( + Args.MakeArgString("-frandomize-layout-seed=" + Twine(A->getValue(0)))); + + if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_file_EQ)) + CmdArgs.push_back(Args.MakeArgString("-frandomize-layout-seed-file=" + + Twine(A->getValue(0)))); + // -fvisibility= and -fvisibility-ms-compat are of a piece. if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, options::OPT_fvisibility_ms_compat)) { if (A->getOption().matches(options::OPT_fvisibility_EQ)) { - CmdArgs.push_back("-fvisibility"); - CmdArgs.push_back(A->getValue()); + A->render(Args, CmdArgs); } else { assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); - CmdArgs.push_back("-fvisibility"); - CmdArgs.push_back("hidden"); - CmdArgs.push_back("-ftype-visibility"); - CmdArgs.push_back("default"); + CmdArgs.push_back("-fvisibility=hidden"); + CmdArgs.push_back("-ftype-visibility=default"); } + } else if (IsOpenMPDevice) { + // When compiling for the OpenMP device we want protected visibility by + // default. This prevents the device from accidentally preempting code on + // the host, makes the system more robust, and improves performance. + CmdArgs.push_back("-fvisibility=protected"); } - if (!RawTriple.isPS4()) + // PS4/PS5 process these options in addClangTargetOptions. + if (!RawTriple.isPS()) { if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass, options::OPT_fno_visibility_from_dllstorageclass)) { @@ -5422,29 +6555,69 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fvisibility_externs_nodllstorageclass_EQ); } } - - if (const Arg *A = Args.getLastArg(options::OPT_mignore_xcoff_visibility)) { - if (Triple.isOSAIX()) - CmdArgs.push_back("-mignore-xcoff-visibility"); - else - D.Diag(diag::err_drv_unsupported_opt_for_target) - << A->getAsString(Args) << TripleStr; } - Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden); + if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden, + options::OPT_fno_visibility_inlines_hidden, false)) + CmdArgs.push_back("-fvisibility-inlines-hidden"); + Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, options::OPT_fno_visibility_inlines_hidden_static_local_var); - Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); + + // -fvisibility-global-new-delete-hidden is a deprecated spelling of + // -fvisibility-global-new-delete=force-hidden. + if (const Arg *A = + Args.getLastArg(options::OPT_fvisibility_global_new_delete_hidden)) { + D.Diag(diag::warn_drv_deprecated_arg) + << A->getAsString(Args) << /*hasReplacement=*/true + << "-fvisibility-global-new-delete=force-hidden"; + } + + if (const Arg *A = + Args.getLastArg(options::OPT_fvisibility_global_new_delete_EQ, + options::OPT_fvisibility_global_new_delete_hidden)) { + if (A->getOption().matches(options::OPT_fvisibility_global_new_delete_EQ)) { + A->render(Args, CmdArgs); + } else { + assert(A->getOption().matches( + options::OPT_fvisibility_global_new_delete_hidden)); + CmdArgs.push_back("-fvisibility-global-new-delete=force-hidden"); + } + } Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); + if (Args.hasFlag(options::OPT_fnew_infallible, + options::OPT_fno_new_infallible, false)) + CmdArgs.push_back("-fnew-infallible"); + + if (Args.hasFlag(options::OPT_fno_operator_names, + options::OPT_foperator_names, false)) + CmdArgs.push_back("-fno-operator-names"); + // Forward -f (flag) options which we can pass directly. Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); - Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names); - Args.AddLastArg(CmdArgs, options::OPT_femulated_tls, - options::OPT_fno_emulated_tls); + Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fraw_string_literals, + options::OPT_fno_raw_string_literals); + + if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls, + Triple.hasDefaultEmulatedTLS())) + CmdArgs.push_back("-femulated-tls"); + + Args.addOptInFlag(CmdArgs, options::OPT_fcheck_new, + options::OPT_fno_check_new); + + if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { + // FIXME: There's no reason for this to be restricted to X86. The backend + // code needs to be changed to include the appropriate function calls + // automatically. + if (!Triple.isX86() && !Triple.isAArch64()) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; + } // AltiVec-like language extensions aren't relevant for assembling. if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) @@ -5475,6 +6648,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_openmp_simd); Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder); Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); + if (!Args.hasFlag(options::OPT_fopenmp_extensions, + options::OPT_fno_openmp_extensions, /*Default=*/true)) + CmdArgs.push_back("-fno-openmp-extensions"); Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ); Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ); Args.AddAllArgs(CmdArgs, @@ -5490,19 +6666,30 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_openmp_cuda_mode, /*Default=*/false)) CmdArgs.push_back("-fopenmp-cuda-mode"); - // When in OpenMP offloading mode with NVPTX target, forward - // cuda-parallel-target-regions flag - if (Args.hasFlag(options::OPT_fopenmp_cuda_parallel_target_regions, - options::OPT_fno_openmp_cuda_parallel_target_regions, - /*Default=*/true)) - CmdArgs.push_back("-fopenmp-cuda-parallel-target-regions"); - - // When in OpenMP offloading mode with NVPTX target, check if full runtime - // is required. - if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime, - options::OPT_fno_openmp_cuda_force_full_runtime, + // When in OpenMP offloading mode, enable debugging on the device. + Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ); + if (Args.hasFlag(options::OPT_fopenmp_target_debug, + options::OPT_fno_openmp_target_debug, /*Default=*/false)) + CmdArgs.push_back("-fopenmp-target-debug"); + + // When in OpenMP offloading mode, forward assumptions information about + // thread and team counts in the device. + if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription, + options::OPT_fno_openmp_assume_teams_oversubscription, + /*Default=*/false)) + CmdArgs.push_back("-fopenmp-assume-teams-oversubscription"); + if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription, + options::OPT_fno_openmp_assume_threads_oversubscription, /*Default=*/false)) - CmdArgs.push_back("-fopenmp-cuda-force-full-runtime"); + CmdArgs.push_back("-fopenmp-assume-threads-oversubscription"); + if (Args.hasArg(options::OPT_fopenmp_assume_no_thread_state)) + CmdArgs.push_back("-fopenmp-assume-no-thread-state"); + if (Args.hasArg(options::OPT_fopenmp_assume_no_nested_parallelism)) + CmdArgs.push_back("-fopenmp-assume-no-nested-parallelism"); + if (Args.hasArg(options::OPT_fopenmp_offload_mandatory)) + CmdArgs.push_back("-fopenmp-offload-mandatory"); + if (Args.hasArg(options::OPT_fopenmp_force_usm)) + CmdArgs.push_back("-fopenmp-force-usm"); break; default: // By default, if Clang doesn't know how to generate useful OpenMP code @@ -5517,10 +6704,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, options::OPT_fno_openmp_simd); Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); + Args.addOptOutFlag(CmdArgs, options::OPT_fopenmp_extensions, + options::OPT_fno_openmp_extensions); } - const SanitizerArgs &Sanitize = TC.getSanitizerArgs(); - Sanitize.addArgs(TC, Args, CmdArgs, InputType); + // Forward the new driver to change offloading code generation. + if (Args.hasFlag(options::OPT_offload_new_driver, + options::OPT_no_offload_new_driver, false)) + CmdArgs.push_back("--offload-new-driver"); + + SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); const XRayArgs &XRay = TC.getXRayArgs(); XRay.addArgs(TC, Args, CmdArgs, InputType); @@ -5536,8 +6729,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) { StringRef S0 = A->getValue(), S = S0; unsigned Size, Offset = 0; - if (!Triple.isAArch64() && Triple.getArch() != llvm::Triple::x86 && - Triple.getArch() != llvm::Triple::x86_64) + if (!Triple.isAArch64() && !Triple.isLoongArch() && !Triple.isRISCV() && + !Triple.isX86() && + !(!Triple.isOSAIX() && (Triple.getArch() == llvm::Triple::ppc || + Triple.getArch() == llvm::Triple::ppc64))) D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) << TripleStr; else if (S.consumeInteger(10, Size) || @@ -5554,6 +6749,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } + Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch); + if (TC.SupportsProfiling()) { Args.AddLastArg(CmdArgs, options::OPT_pg); @@ -5581,22 +6778,56 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } + if (Arg *A = Args.getLastArgNoClaim(options::OPT_pg)) { + if (TC.getTriple().isOSzOS()) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; + } + } + if (Arg *A = Args.getLastArgNoClaim(options::OPT_p)) { + if (!(TC.getTriple().isOSAIX() || TC.getTriple().isOSOpenBSD())) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; + } + } + if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) { + if (A->getOption().matches(options::OPT_p)) { + A->claim(); + if (TC.getTriple().isOSAIX() && !Args.hasArgNoClaim(options::OPT_pg)) + CmdArgs.push_back("-pg"); + } + } + + // Reject AIX-specific link options on other targets. + if (!TC.getTriple().isOSAIX()) { + for (const Arg *A : Args.filtered(options::OPT_b, options::OPT_K, + options::OPT_mxcoff_build_id_EQ)) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getSpelling() << TripleStr; + } + } + if (Args.getLastArg(options::OPT_fapple_kext) || (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) CmdArgs.push_back("-fapple-kext"); + Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat); Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ); Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); Args.AddLastArg(CmdArgs, options::OPT_ftime_report); Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ); - Args.AddLastArg(CmdArgs, options::OPT_ftime_trace); - Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); Args.AddLastArg(CmdArgs, options::OPT_ftrapv); Args.AddLastArg(CmdArgs, options::OPT_malign_double); Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file); + if (const char *Name = C.getTimeTraceFile(&JA)) { + CmdArgs.push_back(Args.MakeArgString("-ftime-trace=" + Twine(Name))); + Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); + Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_verbose); + } + if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { CmdArgs.push_back("-ftrapv-handler"); CmdArgs.push_back(A->getValue()); @@ -5615,11 +6846,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fwrapv"); } - if (Arg *A = Args.getLastArg(options::OPT_freroll_loops, - options::OPT_fno_reroll_loops)) - if (A->getOption().matches(options::OPT_freroll_loops)) - CmdArgs.push_back("-freroll-loops"); - Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops, options::OPT_fno_finite_loops); @@ -5627,20 +6853,21 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, options::OPT_fno_unroll_loops); + Args.AddLastArg(CmdArgs, options::OPT_fstrict_flex_arrays_EQ); + Args.AddLastArg(CmdArgs, options::OPT_pthread); - if (Args.hasFlag(options::OPT_mspeculative_load_hardening, - options::OPT_mno_speculative_load_hardening, false)) - CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening")); + Args.addOptInFlag(CmdArgs, options::OPT_mspeculative_load_hardening, + options::OPT_mno_speculative_load_hardening); RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext); RenderSCPOptions(TC, Args, CmdArgs); RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs); - // Translate -mstackrealign - if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign, - false)) - CmdArgs.push_back(Args.MakeArgString("-mstackrealign")); + Args.AddLastArg(CmdArgs, options::OPT_fswift_async_fp_EQ); + + Args.addOptInFlag(CmdArgs, options::OPT_mstackrealign, + options::OPT_mno_stackrealign); if (Args.hasArg(options::OPT_mstack_alignment)) { StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); @@ -5656,9 +6883,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-mstack-probe-size=0"); } - if (!Args.hasFlag(options::OPT_mstack_arg_probe, - options::OPT_mno_stack_arg_probe, true)) - CmdArgs.push_back(Args.MakeArgString("-mno-stack-arg-probe")); + Args.addOptOutFlag(CmdArgs, options::OPT_mstack_arg_probe, + options::OPT_mno_stack_arg_probe); if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it, options::OPT_mno_restrict_it)) { @@ -5667,32 +6893,36 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-arm-restrict-it"); } else { CmdArgs.push_back("-mllvm"); - CmdArgs.push_back("-arm-no-restrict-it"); + CmdArgs.push_back("-arm-default-it"); } - } else if (Triple.isOSWindows() && - (Triple.getArch() == llvm::Triple::arm || - Triple.getArch() == llvm::Triple::thumb)) { - // Windows on ARM expects restricted IT blocks - CmdArgs.push_back("-mllvm"); - CmdArgs.push_back("-arm-restrict-it"); } // Forward -cl options to -cc1 - RenderOpenCLOptions(Args, CmdArgs); + RenderOpenCLOptions(Args, CmdArgs, InputType); + + // Forward hlsl options to -cc1 + RenderHLSLOptions(Args, CmdArgs, InputType); + + // Forward OpenACC options to -cc1 + RenderOpenACCOptions(D, Args, CmdArgs, InputType); if (IsHIP) { if (Args.hasFlag(options::OPT_fhip_new_launch_api, options::OPT_fno_hip_new_launch_api, true)) CmdArgs.push_back("-fhip-new-launch-api"); - if (Args.hasFlag(options::OPT_fgpu_allow_device_init, - options::OPT_fno_gpu_allow_device_init, false)) - CmdArgs.push_back("-fgpu-allow-device-init"); + Args.addOptInFlag(CmdArgs, options::OPT_fgpu_allow_device_init, + options::OPT_fno_gpu_allow_device_init); + Args.AddLastArg(CmdArgs, options::OPT_hipstdpar); + Args.AddLastArg(CmdArgs, options::OPT_hipstdpar_interpose_alloc); + Args.addOptInFlag(CmdArgs, options::OPT_fhip_kernel_arg_name, + options::OPT_fno_hip_kernel_arg_name); } if (IsCuda || IsHIP) { - if (Args.hasFlag(options::OPT_fgpu_defer_diag, - options::OPT_fno_gpu_defer_diag, false)) - CmdArgs.push_back("-fgpu-defer-diag"); + if (IsRDCMode) + CmdArgs.push_back("-fgpu-rdc"); + Args.addOptInFlag(CmdArgs, options::OPT_fgpu_defer_diag, + options::OPT_fno_gpu_defer_diag); if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads, options::OPT_fno_gpu_exclude_wrong_side_overloads, false)) { @@ -5701,39 +6931,64 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } + // Forward -nogpulib to -cc1. + if (Args.hasArg(options::OPT_nogpulib)) + CmdArgs.push_back("-nogpulib"); + if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { CmdArgs.push_back( Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); } - // Forward -f options with positive and negative forms; we translate - // these by hand. - if (Arg *A = getLastProfileSampleUseArg(Args)) { - auto *PGOArg = Args.getLastArg( - options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, - options::OPT_fcs_profile_generate, options::OPT_fcs_profile_generate_EQ, - options::OPT_fprofile_use, options::OPT_fprofile_use_EQ); - if (PGOArg) - D.Diag(diag::err_drv_argument_not_allowed_with) - << "SampleUse with PGO options"; + if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ)) + CmdArgs.push_back( + Args.MakeArgString(Twine("-mfunction-return=") + A->getValue())); + + Args.AddLastArg(CmdArgs, options::OPT_mindirect_branch_cs_prefix); + + // Forward -f options with positive and negative forms; we translate these by + // hand. Do not propagate PGO options to the GPU-side compilations as the + // profile info is for the host-side compilation only. + if (!(IsCudaDevice || IsHIPDevice)) { + if (Arg *A = getLastProfileSampleUseArg(Args)) { + auto *PGOArg = Args.getLastArg( + options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, + options::OPT_fcs_profile_generate, + options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use, + options::OPT_fprofile_use_EQ); + if (PGOArg) + D.Diag(diag::err_drv_argument_not_allowed_with) + << "SampleUse with PGO options"; + + StringRef fname = A->getValue(); + if (!llvm::sys::fs::exists(fname)) + D.Diag(diag::err_drv_no_such_file) << fname; + else + A->render(Args, CmdArgs); + } + Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); - StringRef fname = A->getValue(); - if (!llvm::sys::fs::exists(fname)) - D.Diag(diag::err_drv_no_such_file) << fname; - else - A->render(Args, CmdArgs); + if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, + options::OPT_fno_pseudo_probe_for_profiling, false)) { + CmdArgs.push_back("-fpseudo-probe-for-profiling"); + // Enforce -funique-internal-linkage-names if it's not explicitly turned + // off. + if (Args.hasFlag(options::OPT_funique_internal_linkage_names, + options::OPT_fno_unique_internal_linkage_names, true)) + CmdArgs.push_back("-funique-internal-linkage-names"); + } } - Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); - - if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, - options::OPT_fno_pseudo_probe_for_profiling, false)) - CmdArgs.push_back("-fpseudo-probe-for-profiling"); - RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs); - if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, - options::OPT_fno_assume_sane_operator_new)) - CmdArgs.push_back("-fno-assume-sane-operator-new"); + Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, + options::OPT_fno_assume_sane_operator_new); + + if (Args.hasFlag(options::OPT_fapinotes, options::OPT_fno_apinotes, false)) + CmdArgs.push_back("-fapinotes"); + if (Args.hasFlag(options::OPT_fapinotes_modules, + options::OPT_fno_apinotes_modules, false)) + CmdArgs.push_back("-fapinotes-modules"); + Args.AddLastArg(CmdArgs, options::OPT_fapinotes_swift_version); // -fblocks=0 is default. if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, @@ -5751,24 +7006,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (TC.IsEncodeExtendedBlockSignatureDefault()) CmdArgs.push_back("-fencode-extended-block-signature"); - if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts, - false) && - types::isCXX(InputType)) { - CmdArgs.push_back("-fcoroutines-ts"); - } + if (Args.hasFlag(options::OPT_fcoro_aligned_allocation, + options::OPT_fno_coro_aligned_allocation, false) && + types::isCXX(InputType)) + CmdArgs.push_back("-fcoro-aligned-allocation"); Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes, options::OPT_fno_double_square_bracket_attributes); - // -faccess-control is default. - if (Args.hasFlag(options::OPT_fno_access_control, - options::OPT_faccess_control, false)) - CmdArgs.push_back("-fno-access-control"); - - // -felide-constructors is the default. - if (Args.hasFlag(options::OPT_fno_elide_constructors, - options::OPT_felide_constructors, false)) - CmdArgs.push_back("-fno-elide-constructors"); + Args.addOptOutFlag(CmdArgs, options::OPT_faccess_control, + options::OPT_fno_access_control); + Args.addOptOutFlag(CmdArgs, options::OPT_felide_constructors, + options::OPT_fno_elide_constructors); ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); @@ -5787,7 +7036,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (!Args.hasFlag( options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit, !RawTriple.isOSAIX() && !RawTriple.isOSWindows() && - TC.getArch() != llvm::Triple::xcore && ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) || RawTriple.hasEnvironment())) || KernelOrKext) @@ -5798,10 +7046,28 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, RawTriple.isOSDarwin() && !KernelOrKext)) CmdArgs.push_back("-fregister-global-dtors-with-atexit"); - // -fno-use-line-directives is default. - if (Args.hasFlag(options::OPT_fuse_line_directives, - options::OPT_fno_use_line_directives, false)) - CmdArgs.push_back("-fuse-line-directives"); + Args.addOptInFlag(CmdArgs, options::OPT_fuse_line_directives, + options::OPT_fno_use_line_directives); + + // -fno-minimize-whitespace is default. + if (Args.hasFlag(options::OPT_fminimize_whitespace, + options::OPT_fno_minimize_whitespace, false)) { + types::ID InputType = Inputs[0].getType(); + if (!isDerivedFromC(InputType)) + D.Diag(diag::err_drv_opt_unsupported_input_type) + << "-fminimize-whitespace" << types::getTypeName(InputType); + CmdArgs.push_back("-fminimize-whitespace"); + } + + // -fno-keep-system-includes is default. + if (Args.hasFlag(options::OPT_fkeep_system_includes, + options::OPT_fno_keep_system_includes, false)) { + types::ID InputType = Inputs[0].getType(); + if (!isDerivedFromC(InputType)) + D.Diag(diag::err_drv_opt_unsupported_input_type) + << "-fkeep-system-includes" << types::getTypeName(InputType); + CmdArgs.push_back("-fkeep-system-includes"); + } // -fms-extensions=0 is default. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, @@ -5813,8 +7079,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, true))); - if (IsMSVCCompat) + if (IsMSVCCompat) { CmdArgs.push_back("-fms-compatibility"); + if (!types::isCXX(Input.getType()) && + Args.hasArg(options::OPT_fms_define_stdc)) + CmdArgs.push_back("-fms-define-stdc"); + } + + if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() && + Args.hasArg(options::OPT_fms_runtime_lib_EQ)) + ProcessVSRuntimeLibrary(getToolChain(), Args, CmdArgs); // Handle -fgcc-version, if present. VersionTuple GNUCVer; @@ -5824,8 +7098,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, StringRef Val = A->getValue(); Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable. bool Invalid = GNUCVer.tryParse(Val); - unsigned Minor = GNUCVer.getMinor().getValueOr(0); - unsigned Patch = GNUCVer.getSubminor().getValueOr(0); + unsigned Minor = GNUCVer.getMinor().value_or(0); + unsigned Patch = GNUCVer.getSubminor().value_or(0); if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) { D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << A->getValue(); @@ -5845,6 +7119,20 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString())); bool IsMSVC2015Compatible = MSVT.getMajor() >= 19; + if (ImplyVCPPCVer) { + StringRef LanguageStandard; + if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { + Std = StdArg; + LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) + .Case("c11", "-std=c11") + .Case("c17", "-std=c17") + .Default(""); + if (LanguageStandard.empty()) + D.Diag(clang::diag::warn_drv_unused_argument) + << StdArg->getAsString(Args); + } + CmdArgs.push_back(LanguageStandard.data()); + } if (ImplyVCPPCXXVer) { StringRef LanguageStandard; if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { @@ -5852,7 +7140,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) .Case("c++14", "-std=c++14") .Case("c++17", "-std=c++17") - .Case("c++latest", "-std=c++20") + .Case("c++20", "-std=c++20") + // TODO add c++23 and c++26 when MSVC supports it. + .Case("c++latest", "-std=c++26") .Default(""); if (LanguageStandard.empty()) D.Diag(clang::diag::warn_drv_unused_argument) @@ -5869,14 +7159,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(LanguageStandard.data()); } - // -fno-borland-extensions is default. - if (Args.hasFlag(options::OPT_fborland_extensions, - options::OPT_fno_borland_extensions, false)) - CmdArgs.push_back("-fborland-extensions"); + Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions, + options::OPT_fno_borland_extensions); - // -fno-declspec is default, except for PS4. + // -fno-declspec is default, except for PS4/PS5. if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec, - RawTriple.isPS4())) + RawTriple.isPS())) CmdArgs.push_back("-fdeclspec"); else if (Args.hasArg(options::OPT_fno_declspec)) CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec. @@ -5885,39 +7173,67 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // than 19. if (!Args.hasFlag(options::OPT_fthreadsafe_statics, options::OPT_fno_threadsafe_statics, - !IsWindowsMSVC || IsMSVC2015Compatible)) + !types::isOpenCL(InputType) && + (!IsWindowsMSVC || IsMSVC2015Compatible))) CmdArgs.push_back("-fno-threadsafe-statics"); - // -fno-delayed-template-parsing is default, except when targeting MSVC. - // Many old Windows SDK versions require this to parse. - // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their - // compiler. We should be able to disable this by default at some point. - if (Args.hasFlag(options::OPT_fdelayed_template_parsing, - options::OPT_fno_delayed_template_parsing, IsWindowsMSVC)) - CmdArgs.push_back("-fdelayed-template-parsing"); + // Add -fno-assumptions, if it was specified. + if (!Args.hasFlag(options::OPT_fassumptions, options::OPT_fno_assumptions, + true)) + CmdArgs.push_back("-fno-assumptions"); // -fgnu-keywords default varies depending on language; only pass if // specified. Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords, options::OPT_fno_gnu_keywords); - if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline, - false)) - CmdArgs.push_back("-fgnu89-inline"); + Args.addOptInFlag(CmdArgs, options::OPT_fgnu89_inline, + options::OPT_fno_gnu89_inline); - if (Args.hasArg(options::OPT_fno_inline)) - CmdArgs.push_back("-fno-inline"); + const Arg *InlineArg = Args.getLastArg(options::OPT_finline_functions, + options::OPT_finline_hint_functions, + options::OPT_fno_inline_functions); + if (Arg *A = Args.getLastArg(options::OPT_finline, options::OPT_fno_inline)) { + if (A->getOption().matches(options::OPT_fno_inline)) + A->render(Args, CmdArgs); + } else if (InlineArg) { + InlineArg->render(Args, CmdArgs); + } + + Args.AddLastArg(CmdArgs, options::OPT_finline_max_stacksize_EQ); + + // FIXME: Find a better way to determine whether we are in C++20. + bool HaveCxx20 = + Std && + (Std->containsValue("c++2a") || Std->containsValue("gnu++2a") || + Std->containsValue("c++20") || Std->containsValue("gnu++20") || + Std->containsValue("c++2b") || Std->containsValue("gnu++2b") || + Std->containsValue("c++23") || Std->containsValue("gnu++23") || + Std->containsValue("c++2c") || Std->containsValue("gnu++2c") || + Std->containsValue("c++26") || Std->containsValue("gnu++26") || + Std->containsValue("c++latest") || Std->containsValue("gnu++latest")); + bool HaveModules = + RenderModulesOptions(C, D, Args, Input, Output, HaveCxx20, CmdArgs); - Args.AddLastArg(CmdArgs, options::OPT_finline_functions, - options::OPT_finline_hint_functions, - options::OPT_fno_inline_functions); + // -fdelayed-template-parsing is default when targeting MSVC. + // Many old Windows SDK versions require this to parse. + // + // According to + // https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-170, + // MSVC actually defaults to -fno-delayed-template-parsing (/Zc:twoPhase- + // with MSVC CLI) if using C++20. So we match the behavior with MSVC here to + // not enable -fdelayed-template-parsing by default after C++20. + // + // FIXME: Given -fdelayed-template-parsing is a source of bugs, we should be + // able to disable this by default at some point. + if (Args.hasFlag(options::OPT_fdelayed_template_parsing, + options::OPT_fno_delayed_template_parsing, + IsWindowsMSVC && !HaveCxx20)) { + if (HaveCxx20) + D.Diag(clang::diag::warn_drv_delayed_template_parsing_after_cxx20); - // FIXME: Find a better way to determine whether the language has modules - // support by default, or just assume that all languages do. - bool HaveModules = - Std && (Std->containsValue("c++2a") || Std->containsValue("c++20") || - Std->containsValue("c++latest")); - RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules); + CmdArgs.push_back("-fdelayed-template-parsing"); + } if (Args.hasFlag(options::OPT_fpch_validate_input_files_content, options::OPT_fno_pch_validate_input_files_content, false)) @@ -5932,20 +7248,24 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, false)) CmdArgs.push_back("-fmodules-debuginfo"); - Args.AddLastArg(CmdArgs, options::OPT_flegacy_pass_manager, - options::OPT_fno_legacy_pass_manager); - ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, Input, CmdArgs); + if (types::isObjC(Input.getType()) && + Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec, + options::OPT_fno_objc_encode_cxx_class_template_spec, + !Runtime.isNeXTFamily())) + CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec"); + if (Args.hasFlag(options::OPT_fapplication_extension, options::OPT_fno_application_extension, false)) CmdArgs.push_back("-fapplication-extension"); // Handle GCC-style exception args. + bool EH = false; if (!C.getDriver().IsCLMode()) - addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); + EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); // Handle exception personalities Arg *A = Args.getLastArg( @@ -5978,22 +7298,31 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } // C++ "sane" operator new. - if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, - options::OPT_fno_assume_sane_operator_new)) - CmdArgs.push_back("-fno-assume-sane-operator-new"); - - // -frelaxed-template-template-args is off by default, as it is a severe - // breaking change until a corresponding change to template partial ordering - // is provided. - if (Args.hasFlag(options::OPT_frelaxed_template_template_args, - options::OPT_fno_relaxed_template_template_args, false)) - CmdArgs.push_back("-frelaxed-template-template-args"); - - // -fsized-deallocation is off by default, as it is an ABI-breaking change for - // most platforms. - if (Args.hasFlag(options::OPT_fsized_deallocation, - options::OPT_fno_sized_deallocation, false)) - CmdArgs.push_back("-fsized-deallocation"); + Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, + options::OPT_fno_assume_sane_operator_new); + + // -fassume-unique-vtables is on by default. + Args.addOptOutFlag(CmdArgs, options::OPT_fassume_unique_vtables, + options::OPT_fno_assume_unique_vtables); + + // -frelaxed-template-template-args is deprecated. + if (Arg *A = + Args.getLastArg(options::OPT_frelaxed_template_template_args, + options::OPT_fno_relaxed_template_template_args)) { + if (A->getOption().matches( + options::OPT_fno_relaxed_template_template_args)) { + D.Diag(diag::warn_drv_deprecated_arg_no_relaxed_template_template_args); + CmdArgs.push_back("-fno-relaxed-template-template-args"); + } else { + D.Diag(diag::warn_drv_deprecated_arg) + << A->getAsString(Args) << /*hasReplacement=*/false; + } + } + + // -fsized-deallocation is on by default in C++14 onwards and otherwise off + // by default. + Args.addLastArg(CmdArgs, options::OPT_fsized_deallocation, + options::OPT_fno_sized_deallocation); // -faligned-allocation is on by default in C++17 onwards and otherwise off // by default. @@ -6016,15 +7345,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // -fconstant-cfstrings is default, and may be subject to argument translation // on Darwin. if (!Args.hasFlag(options::OPT_fconstant_cfstrings, - options::OPT_fno_constant_cfstrings) || + options::OPT_fno_constant_cfstrings, true) || !Args.hasFlag(options::OPT_mconstant_cfstrings, - options::OPT_mno_constant_cfstrings)) + options::OPT_mno_constant_cfstrings, true)) CmdArgs.push_back("-fno-constant-cfstrings"); - // -fno-pascal-strings is default, only pass non-default. - if (Args.hasFlag(options::OPT_fpascal_strings, - options::OPT_fno_pascal_strings, false)) - CmdArgs.push_back("-fpascal-strings"); + Args.addOptInFlag(CmdArgs, options::OPT_fpascal_strings, + options::OPT_fno_pascal_strings); // Honor -fpack-struct= and -fpack-struct, if given. Note that // -fno-pack-struct doesn't apply to -fpack-struct=. @@ -6056,25 +7383,24 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-Qn"); // -fno-common is the default, set -fcommon only when that flag is set. - if (Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common, false)) - CmdArgs.push_back("-fcommon"); + Args.addOptInFlag(CmdArgs, options::OPT_fcommon, options::OPT_fno_common); // -fsigned-bitfields is default, and clang doesn't yet support // -funsigned-bitfields. if (!Args.hasFlag(options::OPT_fsigned_bitfields, - options::OPT_funsigned_bitfields)) + options::OPT_funsigned_bitfields, true)) D.Diag(diag::warn_drv_clang_unsupported) << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. - if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope)) + if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope, true)) D.Diag(diag::err_drv_clang_unsupported) << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); // -finput_charset=UTF-8 is default. Reject others if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) { StringRef value = inputCharset->getValue(); - if (!value.equals_lower("utf-8")) + if (!value.equals_insensitive("utf-8")) D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) << value; } @@ -6082,22 +7408,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // -fexec_charset=UTF-8 is default. Reject others if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) { StringRef value = execCharset->getValue(); - if (!value.equals_lower("utf-8")) + if (!value.equals_insensitive("utf-8")) D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) << value; } RenderDiagnosticsOptions(D, Args, CmdArgs); - // -fno-asm-blocks is default. - if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks, - false)) - CmdArgs.push_back("-fasm-blocks"); + Args.addOptInFlag(CmdArgs, options::OPT_fasm_blocks, + options::OPT_fno_asm_blocks); - // -fgnu-inline-asm is default. - if (!Args.hasFlag(options::OPT_fgnu_inline_asm, - options::OPT_fno_gnu_inline_asm, true)) - CmdArgs.push_back("-fno-gnu-inline-asm"); + Args.addOptOutFlag(CmdArgs, options::OPT_fgnu_inline_asm, + options::OPT_fno_gnu_inline_asm); // Enable vectorization per default according to the optimization level // selected. For optimization levels that want vectorization we use the alias @@ -6133,21 +7455,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fno-dollars-in-identifiers"); } - // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for - // practical purposes. - if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time, - options::OPT_fno_unit_at_a_time)) { - if (A->getOption().matches(options::OPT_fno_unit_at_a_time)) - D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args); - } - - if (Args.hasFlag(options::OPT_fapple_pragma_pack, - options::OPT_fno_apple_pragma_pack, false)) - CmdArgs.push_back("-fapple-pragma-pack"); - - if (Args.hasFlag(options::OPT_fxl_pragma_pack, - options::OPT_fno_xl_pragma_pack, RawTriple.isOSAIX())) - CmdArgs.push_back("-fxl-pragma-pack"); + Args.addOptInFlag(CmdArgs, options::OPT_fapple_pragma_pack, + options::OPT_fno_apple_pragma_pack); // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple)) @@ -6158,6 +7467,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (RewriteImports) CmdArgs.push_back("-frewrite-imports"); + Args.addOptInFlag(CmdArgs, options::OPT_fdirectives_only, + options::OPT_fno_directives_only); + // Enable rewrite includes if the user's asked for it or if we're generating // diagnostics. // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be @@ -6178,6 +7490,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_dM); Args.AddLastArg(CmdArgs, options::OPT_dD); + Args.AddLastArg(CmdArgs, options::OPT_dI); Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ); @@ -6202,6 +7515,35 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, A->claim(); } + // Turn -fplugin-arg-pluginname-key=value into + // -plugin-arg-pluginname key=value + // GCC has an actual plugin_argument struct with key/value pairs that it + // passes to its plugins, but we don't, so just pass it on as-is. + // + // The syntax for -fplugin-arg- is ambiguous if both plugin name and + // argument key are allowed to contain dashes. GCC therefore only + // allows dashes in the key. We do the same. + for (const Arg *A : Args.filtered(options::OPT_fplugin_arg)) { + auto ArgValue = StringRef(A->getValue()); + auto FirstDashIndex = ArgValue.find('-'); + StringRef PluginName = ArgValue.substr(0, FirstDashIndex); + StringRef Arg = ArgValue.substr(FirstDashIndex + 1); + + A->claim(); + if (FirstDashIndex == StringRef::npos || Arg.empty()) { + if (PluginName.empty()) { + D.Diag(diag::warn_drv_missing_plugin_name) << A->getAsString(Args); + } else { + D.Diag(diag::warn_drv_missing_plugin_arg) + << PluginName << A->getAsString(Args); + } + continue; + } + + CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-arg-") + PluginName)); + CmdArgs.push_back(Args.MakeArgString(Arg)); + } + // Forward -fpass-plugin=name.so to -cc1. for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { CmdArgs.push_back( @@ -6209,25 +7551,39 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, A->claim(); } + // Forward --vfsoverlay to -cc1. + for (const Arg *A : Args.filtered(options::OPT_vfsoverlay)) { + CmdArgs.push_back("--vfsoverlay"); + CmdArgs.push_back(A->getValue()); + A->claim(); + } + + Args.addOptInFlag(CmdArgs, options::OPT_fsafe_buffer_usage_suggestions, + options::OPT_fno_safe_buffer_usage_suggestions); + + Args.addOptInFlag(CmdArgs, options::OPT_fexperimental_late_parse_attributes, + options::OPT_fno_experimental_late_parse_attributes); + // Setup statistics file output. SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); - if (!StatsFile.empty()) + if (!StatsFile.empty()) { CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile)); + if (D.CCPrintInternalStats) + CmdArgs.push_back("-stats-file-append"); + } // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option // parser. - // -finclude-default-header flag is for preprocessor, - // do not pass it to other cc1 commands when save-temps is enabled - if (C.getDriver().isSaveTempsEnabled() && - !isa<PreprocessJobAction>(JA)) { - for (auto Arg : Args.filtered(options::OPT_Xclang)) { - Arg->claim(); - if (StringRef(Arg->getValue()) != "-finclude-default-header") - CmdArgs.push_back(Arg->getValue()); + for (auto Arg : Args.filtered(options::OPT_Xclang)) { + Arg->claim(); + // -finclude-default-header flag is for preprocessor, + // do not pass it to other cc1 commands when save-temps is enabled + if (C.getDriver().isSaveTempsEnabled() && + !isa<PreprocessJobAction>(JA)) { + if (StringRef(Arg->getValue()) == "-finclude-default-header") + continue; } - } - else { - Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); + CmdArgs.push_back(Arg->getValue()); } for (const Arg *A : Args.filtered(options::OPT_mllvm)) { A->claim(); @@ -6252,7 +7608,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // be added so both IR can be captured. if ((C.getDriver().isSaveTempsEnabled() || JA.isHostOffloading(Action::OFK_OpenMP)) && - !(C.getDriver().embedBitcodeInObject() && !C.getDriver().isUsingLTO()) && + !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) && isa<CompileJobAction>(JA)) CmdArgs.push_back("-disable-llvm-passes"); @@ -6271,7 +7627,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, auto FRecordSwitches = Args.hasFlag(options::OPT_frecord_command_line, options::OPT_fno_record_command_line, false); - if (FRecordSwitches && !Triple.isOSBinFormatELF()) + if (FRecordSwitches && !Triple.isOSBinFormatELF() && + !Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO()) D.Diag(diag::err_drv_unsupported_opt_for_target) << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args) << TripleStr; @@ -6299,13 +7656,22 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } } - // Host-side cuda compilation receives all device-side outputs in a single - // fatbin as Inputs[1]. Include the binary with -fcuda-include-gpubinary. + // Host-side offloading compilation receives all device-side outputs. Include + // them in the host compilation depending on the target. If the host inputs + // are not empty we use the new-driver scheme, otherwise use the old scheme. if ((IsCuda || IsHIP) && CudaDeviceInput) { + CmdArgs.push_back("-fcuda-include-gpubinary"); + CmdArgs.push_back(CudaDeviceInput->getFilename()); + } else if (!HostOffloadingInputs.empty()) { + if ((IsCuda || IsHIP) && !IsRDCMode) { + assert(HostOffloadingInputs.size() == 1 && "Only one input expected"); CmdArgs.push_back("-fcuda-include-gpubinary"); - CmdArgs.push_back(CudaDeviceInput->getFilename()); - if (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false)) - CmdArgs.push_back("-fgpu-rdc"); + CmdArgs.push_back(HostOffloadingInputs.front().getFilename()); + } else { + for (const InputInfo Input : HostOffloadingInputs) + CmdArgs.push_back(Args.MakeArgString("-fembed-offload-object=" + + TC.getInputFilename(Input))); + } } if (IsCuda) { @@ -6314,16 +7680,63 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fcuda-short-ptr"); } - if (IsHIP) + if (IsCuda || IsHIP) { + // Determine the original source input. + const Action *SourceAction = &JA; + while (SourceAction->getKind() != Action::InputClass) { + assert(!SourceAction->getInputs().empty() && "unexpected root action!"); + SourceAction = SourceAction->getInputs()[0]; + } + auto CUID = cast<InputAction>(SourceAction)->getId(); + if (!CUID.empty()) + CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); + + // -ffast-math turns on -fgpu-approx-transcendentals implicitly, but will + // be overriden by -fno-gpu-approx-transcendentals. + bool UseApproxTranscendentals = Args.hasFlag( + options::OPT_ffast_math, options::OPT_fno_fast_math, false); + if (Args.hasFlag(options::OPT_fgpu_approx_transcendentals, + options::OPT_fno_gpu_approx_transcendentals, + UseApproxTranscendentals)) + CmdArgs.push_back("-fgpu-approx-transcendentals"); + } else { + Args.claimAllArgs(options::OPT_fgpu_approx_transcendentals, + options::OPT_fno_gpu_approx_transcendentals); + } + + if (IsHIP) { CmdArgs.push_back("-fcuda-allow-variadic-functions"); + Args.AddLastArg(CmdArgs, options::OPT_fgpu_default_stream_EQ); + } + + Args.AddLastArg(CmdArgs, options::OPT_foffload_uniform_block, + options::OPT_fno_offload_uniform_block); + + Args.AddLastArg(CmdArgs, options::OPT_foffload_implicit_host_device_templates, + options::OPT_fno_offload_implicit_host_device_templates); + + if (IsCudaDevice || IsHIPDevice) { + StringRef InlineThresh = + Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ); + if (!InlineThresh.empty()) { + std::string ArgStr = + std::string("-inline-threshold=") + InlineThresh.str(); + CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)}); + } + } + + if (IsHIPDevice) + Args.addOptOutFlag(CmdArgs, + options::OPT_fhip_fp32_correctly_rounded_divide_sqrt, + options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt); // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path // to specify the result of the compile phase on the host, so the meaningful - // device declarations can be identified. Also, -fopenmp-is-device is passed - // along to tell the frontend that it is generating code for a device, so that - // only the relevant declarations are emitted. + // device declarations can be identified. Also, -fopenmp-is-target-device is + // passed along to tell the frontend that it is generating code for a device, + // so that only the relevant declarations are emitted. if (IsOpenMPDevice) { - CmdArgs.push_back("-fopenmp-is-device"); + CmdArgs.push_back("-fopenmp-is-target-device"); if (OpenMPDeviceInput) { CmdArgs.push_back("-fopenmp-host-ir-file-path"); CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename())); @@ -6333,28 +7746,22 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (Triple.isAMDGPU()) { handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); - if (Args.hasFlag(options::OPT_munsafe_fp_atomics, - options::OPT_mno_unsafe_fp_atomics)) - CmdArgs.push_back("-munsafe-fp-atomics"); + Args.addOptInFlag(CmdArgs, options::OPT_munsafe_fp_atomics, + options::OPT_mno_unsafe_fp_atomics); + Args.addOptOutFlag(CmdArgs, options::OPT_mamdgpu_ieee, + options::OPT_mno_amdgpu_ieee); } // For all the host OpenMP offloading compile jobs we need to pass the targets // information using -fopenmp-targets= option. if (JA.isHostOffloading(Action::OFK_OpenMP)) { - SmallString<128> TargetInfo("-fopenmp-targets="); + SmallString<128> Targets("-fopenmp-targets="); - Arg *Tgts = Args.getLastArg(options::OPT_fopenmp_targets_EQ); - assert(Tgts && Tgts->getNumValues() && - "OpenMP offloading has to have targets specified."); - for (unsigned i = 0; i < Tgts->getNumValues(); ++i) { - if (i) - TargetInfo += ','; - // We need to get the string from the triple because it may be not exactly - // the same as the one we get directly from the arguments. - llvm::Triple T(Tgts->getValue(i)); - TargetInfo += T.getTriple(); - } - CmdArgs.push_back(Args.MakeArgString(TargetInfo.str())); + SmallVector<std::string, 4> Triples; + auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); + std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples), + [](auto TC) { return TC.second->getTripleString(); }); + CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ","))); } bool VirtualFunctionElimination = @@ -6363,7 +7770,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (VirtualFunctionElimination) { // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO // in the future). - if (D.getLTOMode() != LTOK_Full) + if (LTOMode != LTOK_Full) D.Diag(diag::err_drv_argument_only_allowed_with) << "-fvirtual-function-elimination" << "-flto=full"; @@ -6382,25 +7789,55 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } if (WholeProgramVTables) { - if (!D.isUsingLTO()) + // PS4 uses the legacy LTO API, which does not support this feature in + // ThinLTO mode. + bool IsPS4 = getToolChain().getTriple().isPS4(); + + // Check if we passed LTO options but they were suppressed because this is a + // device offloading action, or we passed device offload LTO options which + // were suppressed because this is not the device offload action. + // Check if we are using PS4 in regular LTO mode. + // Otherwise, issue an error. + if ((!IsUsingLTO && !D.isUsingLTO(!IsDeviceOffloadAction)) || + (IsPS4 && !UnifiedLTO && (D.getLTOMode() != LTOK_Full))) D.Diag(diag::err_drv_argument_only_allowed_with) << "-fwhole-program-vtables" - << "-flto"; - CmdArgs.push_back("-fwhole-program-vtables"); + << ((IsPS4 && !UnifiedLTO) ? "-flto=full" : "-flto"); + + // Propagate -fwhole-program-vtables if this is an LTO compile. + if (IsUsingLTO) + CmdArgs.push_back("-fwhole-program-vtables"); } bool DefaultsSplitLTOUnit = - (WholeProgramVTables || Sanitize.needsLTO()) && - (D.getLTOMode() == LTOK_Full || TC.canSplitThinLTOUnit()); + ((WholeProgramVTables || SanitizeArgs.needsLTO()) && + (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit())) || + (!Triple.isPS4() && UnifiedLTO); bool SplitLTOUnit = Args.hasFlag(options::OPT_fsplit_lto_unit, options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); - if (Sanitize.needsLTO() && !SplitLTOUnit) + if (SanitizeArgs.needsLTO() && !SplitLTOUnit) D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" << "-fsanitize=cfi"; if (SplitLTOUnit) CmdArgs.push_back("-fsplit-lto-unit"); + if (Arg *A = Args.getLastArg(options::OPT_ffat_lto_objects, + options::OPT_fno_fat_lto_objects)) { + if (IsUsingLTO && A->getOption().matches(options::OPT_ffat_lto_objects)) { + assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); + if (!Triple.isOSBinFormatELF()) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TC.getTripleString(); + } + CmdArgs.push_back(Args.MakeArgString( + Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); + CmdArgs.push_back("-flto-unit"); + CmdArgs.push_back("-ffat-lto-objects"); + A->render(Args, CmdArgs); + } + } + if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, options::OPT_fno_global_isel)) { CmdArgs.push_back("-mllvm"); @@ -6437,7 +7874,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is // on, we need to pass these flags as linker flags and that will be handled // outside of the compiler. - if (!D.isUsingLTO()) { + if (!IsUsingLTO) { CmdArgs.push_back("-mllvm"); CmdArgs.push_back("-enable-order-file-instrumentation"); } @@ -6449,35 +7886,26 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fforce-enable-int128"); } - if (Args.hasFlag(options::OPT_fkeep_static_consts, - options::OPT_fno_keep_static_consts, false)) - CmdArgs.push_back("-fkeep-static-consts"); - - if (Args.hasFlag(options::OPT_fcomplete_member_pointers, - options::OPT_fno_complete_member_pointers, false)) - CmdArgs.push_back("-fcomplete-member-pointers"); - - if (!Args.hasFlag(options::OPT_fcxx_static_destructors, - options::OPT_fno_cxx_static_destructors, true)) - CmdArgs.push_back("-fno-c++-static-destructors"); + Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_consts, + options::OPT_fno_keep_static_consts); + Args.addOptInFlag(CmdArgs, options::OPT_fkeep_persistent_storage_variables, + options::OPT_fno_keep_persistent_storage_variables); + Args.addOptInFlag(CmdArgs, options::OPT_fcomplete_member_pointers, + options::OPT_fno_complete_member_pointers); + Args.addOptOutFlag(CmdArgs, options::OPT_fcxx_static_destructors, + options::OPT_fno_cxx_static_destructors); addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false); - if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics, - options::OPT_mno_outline_atomics)) { - if (A->getOption().matches(options::OPT_moutline_atomics)) { - // Option -moutline-atomics supported for AArch64 target only. - if (!Triple.isAArch64()) { - D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt) - << Triple.getArchName(); - } else { - CmdArgs.push_back("-target-feature"); - CmdArgs.push_back("+outline-atomics"); - } - } else { - CmdArgs.push_back("-target-feature"); - CmdArgs.push_back("-outline-atomics"); - } + addOutlineAtomicsArgs(D, getToolChain(), Args, CmdArgs, Triple); + + if (Triple.isAArch64() && + (Args.hasArg(options::OPT_mno_fmv) || + (Triple.isAndroid() && Triple.isAndroidVersionLT(23)) || + getToolChain().GetRuntimeLibType(Args) != ToolChain::RLT_CompilerRT)) { + // Disable Function Multiversioning on AArch64 target. + CmdArgs.push_back("-target-feature"); + CmdArgs.push_back("-fmv"); } if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig, @@ -6489,6 +7917,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, !TC.getTriple().isAndroid() && TC.useIntegratedAs())) CmdArgs.push_back("-faddrsig"); + if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && + (EH || UnwindTables || AsyncUnwindTables || + DebugInfoKind != llvm::codegenoptions::NoDebugInfo)) + CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); + if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { std::string Str = A->getAsString(Args); if (!TC.getTriple().isOSBinFormatELF()) @@ -6519,8 +7952,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, addDashXForInput(Args, Input, CmdArgs); ArrayRef<InputInfo> FrontendInputs = Input; - if (IsHeaderModulePrecompile) - FrontendInputs = ModuleHeaderInputs; + if (IsExtractAPI) + FrontendInputs = ExtractAPIInputs; else if (Input.isNothing()) FrontendInputs = {}; @@ -6531,31 +7964,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Input.getInputArg().renderAsInput(Args, CmdArgs); } - // Finally add the compile command to the compilation. - if (Args.hasArg(options::OPT__SLASH_fallback) && - Output.getType() == types::TY_Object && - (InputType == types::TY_C || InputType == types::TY_CXX)) { - auto CLCommand = - getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput); - C.addCommand(std::make_unique<FallbackCommand>( - JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, - Output, std::move(CLCommand))); - } else if (Args.hasArg(options::OPT__SLASH_fallback) && - isa<PrecompileJobAction>(JA)) { - // In /fallback builds, run the main compilation even if the pch generation - // fails, so that the main compilation's fallback to cl.exe runs. - C.addCommand(std::make_unique<ForceSuccessCommand>( - JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, - Output)); - } else if (D.CC1Main && !D.CCGenDiagnostics) { + if (D.CC1Main && !D.CCGenDiagnostics) { // Invoke the CC1 directly in this process - C.addCommand(std::make_unique<CC1Command>(JA, *this, - ResponseFileSupport::AtFileUTF8(), - Exec, CmdArgs, Inputs, Output)); + C.addCommand(std::make_unique<CC1Command>( + JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, + Output, D.getPrependArg())); } else { - C.addCommand(std::make_unique<Command>(JA, *this, - ResponseFileSupport::AtFileUTF8(), - Exec, CmdArgs, Inputs, Output)); + C.addCommand(std::make_unique<Command>( + JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, + Output, D.getPrependArg())); } // Make the compile command echo its inputs for /showFilenames. @@ -6586,11 +8003,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.ClaimAllArgs(options::OPT_emit_llvm); } -Clang::Clang(const ToolChain &TC) +Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend) // CAUTION! The first constructor argument ("clang") is not arbitrary, // as it is for other tools. Some operations on a Tool actually test // whether that tool is Clang based on the Tool's Name as a string. - : Tool("clang", "clang frontend", TC) {} + : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {} Clang::~Clang() {} @@ -6751,18 +8168,26 @@ struct EHFlags { /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR. /// - c: Assume that extern "C" functions are implicitly nounwind. /// The default is /EHs-c-, meaning cleanups are disabled. -static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { +static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args, + bool isWindowsMSVC) { EHFlags EH; std::vector<std::string> EHArgs = Args.getAllArgValues(options::OPT__SLASH_EH); - for (auto EHVal : EHArgs) { + for (const auto &EHVal : EHArgs) { for (size_t I = 0, E = EHVal.size(); I != E; ++I) { switch (EHVal[I]) { case 'a': EH.Asynch = maybeConsumeDash(EHVal, I); - if (EH.Asynch) + if (EH.Asynch) { + // Async exceptions are Windows MSVC only. + if (!isWindowsMSVC) { + EH.Asynch = false; + D.Diag(clang::diag::warn_drv_unused_argument) << "/EHa" << EHVal; + continue; + } EH.Synch = false; + } continue; case 'c': EH.NoUnwindC = maybeConsumeDash(EHVal, I); @@ -6788,66 +8213,20 @@ static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { EH.NoUnwindC = true; } + if (Args.hasArg(options::OPT__SLASH_kernel)) { + EH.Synch = false; + EH.NoUnwindC = false; + EH.Asynch = false; + } + return EH; } void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, - ArgStringList &CmdArgs, - codegenoptions::DebugInfoKind *DebugInfoKind, - bool *EmitCodeView) const { - unsigned RTOptionID = options::OPT__SLASH_MT; + ArgStringList &CmdArgs) const { bool isNVPTX = getToolChain().getTriple().isNVPTX(); - if (Args.hasArg(options::OPT__SLASH_LDd)) - // The /LDd option implies /MTd. The dependent lib part can be overridden, - // but defining _DEBUG is sticky. - RTOptionID = options::OPT__SLASH_MTd; - - if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) - RTOptionID = A->getOption().getID(); - - StringRef FlagForCRT; - switch (RTOptionID) { - case options::OPT__SLASH_MD: - if (Args.hasArg(options::OPT__SLASH_LDd)) - CmdArgs.push_back("-D_DEBUG"); - CmdArgs.push_back("-D_MT"); - CmdArgs.push_back("-D_DLL"); - FlagForCRT = "--dependent-lib=msvcrt"; - break; - case options::OPT__SLASH_MDd: - CmdArgs.push_back("-D_DEBUG"); - CmdArgs.push_back("-D_MT"); - CmdArgs.push_back("-D_DLL"); - FlagForCRT = "--dependent-lib=msvcrtd"; - break; - case options::OPT__SLASH_MT: - if (Args.hasArg(options::OPT__SLASH_LDd)) - CmdArgs.push_back("-D_DEBUG"); - CmdArgs.push_back("-D_MT"); - CmdArgs.push_back("-flto-visibility-public-std"); - FlagForCRT = "--dependent-lib=libcmt"; - break; - case options::OPT__SLASH_MTd: - CmdArgs.push_back("-D_DEBUG"); - CmdArgs.push_back("-D_MT"); - CmdArgs.push_back("-flto-visibility-public-std"); - FlagForCRT = "--dependent-lib=libcmtd"; - break; - default: - llvm_unreachable("Unexpected option ID."); - } - - if (Args.hasArg(options::OPT__SLASH_Zl)) { - CmdArgs.push_back("-D_VC_NODEFAULTLIB"); - } else { - CmdArgs.push_back(FlagForCRT.data()); - - // This provides POSIX compatibility (maps 'open' to '_open'), which most - // users want. The /Za flag to cl.exe turns this off, but it's not - // implemented in clang. - CmdArgs.push_back("--dependent-lib=oldnames"); - } + ProcessVSRuntimeLibrary(getToolChain(), Args, CmdArgs); if (Arg *ShowIncludes = Args.getLastArg(options::OPT__SLASH_showIncludes, @@ -6870,24 +8249,16 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong))); } - // Emit CodeView if -Z7 or -gline-tables-only are present. - if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7, - options::OPT_gline_tables_only)) { - *EmitCodeView = true; - if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7)) - *DebugInfoKind = codegenoptions::LimitedDebugInfo; - else - *DebugInfoKind = codegenoptions::DebugLineTablesOnly; - } else { - *EmitCodeView = false; - } - const Driver &D = getToolChain().getDriver(); - EHFlags EH = parseClangCLEHFlags(D, Args); + + bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment(); + EHFlags EH = parseClangCLEHFlags(D, Args, IsWindowsMSVC); if (!isNVPTX && (EH.Synch || EH.Asynch)) { if (types::isCXX(InputType)) CmdArgs.push_back("-fcxx-exceptions"); CmdArgs.push_back("-fexceptions"); + if (EH.Asynch) + CmdArgs.push_back("-fasync-exceptions"); } if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC) CmdArgs.push_back("-fexternc-nounwind"); @@ -6898,26 +8269,36 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back("-P"); } - unsigned VolatileOptionID; - if (getToolChain().getTriple().isX86()) - VolatileOptionID = options::OPT__SLASH_volatile_ms; - else - VolatileOptionID = options::OPT__SLASH_volatile_iso; - - if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group)) - VolatileOptionID = A->getOption().getID(); - - if (VolatileOptionID == options::OPT__SLASH_volatile_ms) - CmdArgs.push_back("-fms-volatile"); - if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_, options::OPT__SLASH_Zc_dllexportInlines, false)) { - if (Args.hasArg(options::OPT__SLASH_fallback)) { - D.Diag(clang::diag::err_drv_dllexport_inlines_and_fallback); - } else { - CmdArgs.push_back("-fno-dllexport-inlines"); + CmdArgs.push_back("-fno-dllexport-inlines"); + } + + if (Args.hasFlag(options::OPT__SLASH_Zc_wchar_t_, + options::OPT__SLASH_Zc_wchar_t, false)) { + CmdArgs.push_back("-fno-wchar"); + } + + if (Args.hasArg(options::OPT__SLASH_kernel)) { + llvm::Triple::ArchType Arch = getToolChain().getArch(); + std::vector<std::string> Values = + Args.getAllArgValues(options::OPT__SLASH_arch); + if (!Values.empty()) { + llvm::SmallSet<std::string, 4> SupportedArches; + if (Arch == llvm::Triple::x86) + SupportedArches.insert("IA32"); + + for (auto &V : Values) + if (!SupportedArches.contains(V)) + D.Diag(diag::err_drv_argument_not_allowed_with) + << std::string("/arch:").append(V) << "/kernel"; } + + CmdArgs.push_back("-fno-rtti"); + if (Args.hasFlag(options::OPT__SLASH_GR, options::OPT__SLASH_GR_, false)) + D.Diag(diag::err_drv_argument_not_allowed_with) << "/GR" + << "/kernel"; } Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); @@ -6946,6 +8327,9 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back("-fms-memptr-rep=virtual"); } + if (Args.hasArg(options::OPT_regcall4)) + CmdArgs.push_back("-regcall4"); + // Parse the default calling convention options. if (Arg *CCArg = Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr, @@ -6982,40 +8366,42 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back(DCCFlag); } + if (Args.hasArg(options::OPT__SLASH_Gregcall4)) + CmdArgs.push_back("-regcall4"); + Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ); if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { CmdArgs.push_back("-fdiagnostics-format"); - if (Args.hasArg(options::OPT__SLASH_fallback)) - CmdArgs.push_back("msvc-fallback"); - else - CmdArgs.push_back("msvc"); + CmdArgs.push_back("msvc"); } - if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) { + if (Args.hasArg(options::OPT__SLASH_kernel)) + CmdArgs.push_back("-fms-kernel"); + + for (const Arg *A : Args.filtered(options::OPT__SLASH_guard)) { StringRef GuardArgs = A->getValue(); - // The only valid options are "cf", "cf,nochecks", and "cf-". - if (GuardArgs.equals_lower("cf")) { + // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and + // "ehcont-". + if (GuardArgs.equals_insensitive("cf")) { // Emit CFG instrumentation and the table of address-taken functions. CmdArgs.push_back("-cfguard"); - } else if (GuardArgs.equals_lower("cf,nochecks")) { + } else if (GuardArgs.equals_insensitive("cf,nochecks")) { // Emit only the table of address-taken functions. CmdArgs.push_back("-cfguard-no-checks"); - } else if (GuardArgs.equals_lower("cf-")) { + } else if (GuardArgs.equals_insensitive("ehcont")) { + // Emit EH continuation table. + CmdArgs.push_back("-ehcontguard"); + } else if (GuardArgs.equals_insensitive("cf-") || + GuardArgs.equals_insensitive("ehcont-")) { // Do nothing, but we might want to emit a security warning in future. } else { D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs; } + A->claim(); } } -visualstudio::Compiler *Clang::getCLFallback() const { - if (!CLFallback) - CLFallback.reset(new visualstudio::Compiler(getToolChain())); - return CLFallback.get(); -} - - const char *Clang::getBaseInputName(const ArgList &Args, const InputInfo &Input) { return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())); @@ -7069,11 +8455,19 @@ void ClangAs::AddX86TargetArgs(const ArgList &Args, CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); } else { getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Value; + << A->getSpelling() << Value; } } } +void ClangAs::AddLoongArchTargetArgs(const ArgList &Args, + ArgStringList &CmdArgs) const { + CmdArgs.push_back("-target-abi"); + CmdArgs.push_back(loongarch::getLoongArchABI(getToolChain().getDriver(), Args, + getToolChain().getTriple()) + .data()); +} + void ClangAs::AddRISCVTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { const llvm::Triple &Triple = getToolChain().getTriple(); @@ -7081,6 +8475,12 @@ void ClangAs::AddRISCVTargetArgs(const ArgList &Args, CmdArgs.push_back("-target-abi"); CmdArgs.push_back(ABIName.data()); + + if (Args.hasFlag(options::OPT_mdefault_build_attributes, + options::OPT_mno_default_build_attributes, true)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("-riscv-add-build-attributes"); + } } void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, @@ -7112,6 +8512,8 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-triple"); CmdArgs.push_back(Args.MakeArgString(TripleStr)); + getToolChain().addClangCC1ASTargetOptions(Args, CmdArgs); + // Set the output mode, we currently only expect to be used as a real // assembler. CmdArgs.push_back("-filetype"); @@ -7123,7 +8525,7 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(Clang::getBaseInputName(Args, Input)); // Add the target cpu - std::string CPU = getCPUName(Args, Triple, /*FromAs*/ true); + std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ true); if (!CPU.empty()) { CmdArgs.push_back("-target-cpu"); CmdArgs.push_back(Args.MakeArgString(CPU)); @@ -7138,33 +8540,59 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, // Pass along any -I options so we get proper .include search paths. Args.AddAllArgs(CmdArgs, options::OPT_I_Group); + // Pass along any --embed-dir or similar options so we get proper embed paths. + Args.AddAllArgs(CmdArgs, options::OPT_embed_dir_EQ); + // Determine the original source input. - const Action *SourceAction = &JA; - while (SourceAction->getKind() != Action::InputClass) { - assert(!SourceAction->getInputs().empty() && "unexpected root action!"); - SourceAction = SourceAction->getInputs()[0]; - } + auto FindSource = [](const Action *S) -> const Action * { + while (S->getKind() != Action::InputClass) { + assert(!S->getInputs().empty() && "unexpected root action!"); + S = S->getInputs()[0]; + } + return S; + }; + const Action *SourceAction = FindSource(&JA); // Forward -g and handle debug info related flags, assuming we are dealing // with an actual assembly file. bool WantDebug = false; - unsigned DwarfVersion = 0; Args.ClaimAllArgs(options::OPT_g_Group); - if (Arg *A = Args.getLastArg(options::OPT_g_Group)) { + if (Arg *A = Args.getLastArg(options::OPT_g_Group)) WantDebug = !A->getOption().matches(options::OPT_g0) && !A->getOption().matches(options::OPT_ggdb0); - if (WantDebug) - DwarfVersion = DwarfVersionNum(A->getSpelling()); + + // If a -gdwarf argument appeared, remember it. + bool EmitDwarf = false; + if (const Arg *A = getDwarfNArg(Args)) + EmitDwarf = checkDebugInfoOption(A, Args, D, getToolChain()); + + bool EmitCodeView = false; + if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) + EmitCodeView = checkDebugInfoOption(A, Args, D, getToolChain()); + + // If the user asked for debug info but did not explicitly specify -gcodeview + // or -gdwarf, ask the toolchain for the default format. + if (!EmitCodeView && !EmitDwarf && WantDebug) { + switch (getToolChain().getDefaultDebugFormat()) { + case llvm::codegenoptions::DIF_CodeView: + EmitCodeView = true; + break; + case llvm::codegenoptions::DIF_DWARF: + EmitDwarf = true; + break; + } } - unsigned DefaultDwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args); - if (DwarfVersion == 0) - DwarfVersion = DefaultDwarfVersion; + // If the arguments don't imply DWARF, don't emit any debug info here. + if (!EmitDwarf) + WantDebug = false; - if (DwarfVersion == 0) - DwarfVersion = getToolChain().GetDefaultDwarfVersion(); + llvm::codegenoptions::DebugInfoKind DebugInfoKind = + llvm::codegenoptions::NoDebugInfo; - codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; + // Add the -fdebug-compilation-dir flag if needed. + const char *DebugCompilationDir = + addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); if (SourceAction->getType() == types::TY_Asm || SourceAction->getType() == types::TY_PP_Asm) { @@ -7172,12 +8600,11 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, // the guard for source type, however there is a test which asserts // that some assembler invocation receives no -debug-info-kind, // and it's not clear whether that test is just overly restrictive. - DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo - : codegenoptions::NoDebugInfo); - // Add the -fdebug-compilation-dir flag if needed. - addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); + DebugInfoKind = (WantDebug ? llvm::codegenoptions::DebugInfoConstructor + : llvm::codegenoptions::NoDebugInfo); - addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs); + addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args, + CmdArgs); // Set the AT_producer to the clang version when using the integrated // assembler on assembly source files. @@ -7187,11 +8614,12 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, // And pass along -I options Args.AddAllArgs(CmdArgs, options::OPT_I); } + const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion, llvm::DebuggerKind::Default); + renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion); RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain()); - // Handle -fPIC et al -- the relocation-model affects the assembler // for some targets. llvm::Reloc::Model RelocationModel; @@ -7267,10 +8695,23 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, } break; + case llvm::Triple::loongarch32: + case llvm::Triple::loongarch64: + AddLoongArchTargetArgs(Args, CmdArgs); + break; + case llvm::Triple::riscv32: case llvm::Triple::riscv64: AddRISCVTargetArgs(Args, CmdArgs); break; + + case llvm::Triple::hexagon: + if (Args.hasFlag(options::OPT_mdefault_build_attributes, + options::OPT_mno_default_build_attributes, true)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("-hexagon-add-build-attributes"); + } + break; } // Consume all the warning flags. Usually this would be handled more @@ -7285,6 +8726,29 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, Args.AddAllArgs(CmdArgs, options::OPT_mllvm); + if (DebugInfoKind > llvm::codegenoptions::NoDebugInfo && Output.isFilename()) + addDebugObjectName(Args, CmdArgs, DebugCompilationDir, + Output.getFilename()); + + // Fixup any previous commands that use -object-file-name because when we + // generated them, the final .obj name wasn't yet known. + for (Command &J : C.getJobs()) { + if (SourceAction != FindSource(&J.getSource())) + continue; + auto &JArgs = J.getArguments(); + for (unsigned I = 0; I < JArgs.size(); ++I) { + if (StringRef(JArgs[I]).starts_with("-object-file-name=") && + Output.isFilename()) { + ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); + addDebugObjectName(Args, NewArgs, DebugCompilationDir, + Output.getFilename()); + NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); + J.replaceArguments(NewArgs); + break; + } + } + } + assert(Output.isFilename() && "Unexpected lipo output."); CmdArgs.push_back("-o"); CmdArgs.push_back(Output.getFilename()); @@ -7298,7 +8762,7 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, } if (Triple.isAMDGPU()) - handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); + handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true); assert(Input.isFilename() && "Invalid input."); CmdArgs.push_back(Input.getFilename()); @@ -7306,18 +8770,17 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, const char *Exec = getToolChain().getDriver().getClangProgramPath(); if (D.CC1Main && !D.CCGenDiagnostics) { // Invoke cc1as directly in this process. - C.addCommand(std::make_unique<CC1Command>(JA, *this, - ResponseFileSupport::AtFileUTF8(), - Exec, CmdArgs, Inputs, Output)); + C.addCommand(std::make_unique<CC1Command>( + JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, + Output, D.getPrependArg())); } else { - C.addCommand(std::make_unique<Command>(JA, *this, - ResponseFileSupport::AtFileUTF8(), - Exec, CmdArgs, Inputs, Output)); + C.addCommand(std::make_unique<Command>( + JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, + Output, D.getPrependArg())); } } // Begin OffloadBundler - void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, @@ -7329,8 +8792,10 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, // The bundling command looks like this: // clang-offload-bundler -type=bc // -targets=host-triple,openmp-triple1,openmp-triple2 - // -outputs=input_file - // -inputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2" + // -output=output_file + // -input=unbundle_file_host + // -input=unbundle_file_tgt1 + // -input=unbundle_file_tgt2 ArgStringList CmdArgs; @@ -7364,23 +8829,39 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, Triples += Action::GetOffloadKindName(CurKind); Triples += '-'; Triples += CurTC->getTriple().normalize(); - if (CurKind == Action::OFK_HIP && CurDep->getOffloadingArch()) { + if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) && + !StringRef(CurDep->getOffloadingArch()).empty()) { Triples += '-'; Triples += CurDep->getOffloadingArch(); } + + // TODO: Replace parsing of -march flag. Can be done by storing GPUArch + // with each toolchain. + StringRef GPUArchName; + if (CurKind == Action::OFK_OpenMP) { + // Extract GPUArch from -march argument in TC argument list. + for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { + auto ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); + auto Arch = ArchStr.starts_with_insensitive("-march="); + if (Arch) { + GPUArchName = ArchStr.substr(7); + Triples += "-"; + break; + } + } + Triples += GPUArchName.str(); + } } CmdArgs.push_back(TCArgs.MakeArgString(Triples)); // Get bundled file command. CmdArgs.push_back( - TCArgs.MakeArgString(Twine("-outputs=") + Output.getFilename())); + TCArgs.MakeArgString(Twine("-output=") + Output.getFilename())); // Get unbundled files command. - SmallString<128> UB; - UB += "-inputs="; for (unsigned I = 0; I < Inputs.size(); ++I) { - if (I) - UB += ','; + SmallString<128> UB; + UB += "-input="; // Find ToolChain for this input. const ToolChain *CurTC = &getToolChain(); @@ -7390,16 +8871,19 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, assert(CurTC == nullptr && "Expected one dependence!"); CurTC = TC; }); + UB += C.addTempFile( + C.getArgs().MakeArgString(CurTC->getInputFilename(Inputs[I]))); + } else { + UB += CurTC->getInputFilename(Inputs[I]); } - UB += CurTC->getInputFilename(Inputs[I]); + CmdArgs.push_back(TCArgs.MakeArgString(UB)); } - CmdArgs.push_back(TCArgs.MakeArgString(UB)); - + addOffloadCompressArgs(TCArgs, CmdArgs); // All the inputs are encoded as commands. C.addCommand(std::make_unique<Command>( JA, *this, ResponseFileSupport::None(), TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), - CmdArgs, None, Output)); + CmdArgs, std::nullopt, Output)); } void OffloadBundler::ConstructJobMultipleOutputs( @@ -7412,8 +8896,10 @@ void OffloadBundler::ConstructJobMultipleOutputs( // The unbundling command looks like this: // clang-offload-bundler -type=bc // -targets=host-triple,openmp-triple1,openmp-triple2 - // -inputs=input_file - // -outputs=unbundle_file_host,unbundle_file_tgt1,unbundle_file_tgt2" + // -input=input_file + // -output=unbundle_file_host + // -output=unbundle_file_tgt1 + // -output=unbundle_file_tgt2 // -unbundle ArgStringList CmdArgs; @@ -7437,60 +8923,110 @@ void OffloadBundler::ConstructJobMultipleOutputs( Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind); Triples += '-'; Triples += Dep.DependentToolChain->getTriple().normalize(); - if (Dep.DependentOffloadKind == Action::OFK_HIP && + if ((Dep.DependentOffloadKind == Action::OFK_HIP || + Dep.DependentOffloadKind == Action::OFK_Cuda) && !Dep.DependentBoundArch.empty()) { Triples += '-'; Triples += Dep.DependentBoundArch; } + // TODO: Replace parsing of -march flag. Can be done by storing GPUArch + // with each toolchain. + StringRef GPUArchName; + if (Dep.DependentOffloadKind == Action::OFK_OpenMP) { + // Extract GPUArch from -march argument in TC argument list. + for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { + StringRef ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); + auto Arch = ArchStr.starts_with_insensitive("-march="); + if (Arch) { + GPUArchName = ArchStr.substr(7); + Triples += "-"; + break; + } + } + Triples += GPUArchName.str(); + } } CmdArgs.push_back(TCArgs.MakeArgString(Triples)); // Get bundled file command. CmdArgs.push_back( - TCArgs.MakeArgString(Twine("-inputs=") + Input.getFilename())); + TCArgs.MakeArgString(Twine("-input=") + Input.getFilename())); // Get unbundled files command. - SmallString<128> UB; - UB += "-outputs="; for (unsigned I = 0; I < Outputs.size(); ++I) { - if (I) - UB += ','; + SmallString<128> UB; + UB += "-output="; UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]); + CmdArgs.push_back(TCArgs.MakeArgString(UB)); } - CmdArgs.push_back(TCArgs.MakeArgString(UB)); CmdArgs.push_back("-unbundle"); CmdArgs.push_back("-allow-missing-bundles"); + if (TCArgs.hasArg(options::OPT_v)) + CmdArgs.push_back("-verbose"); // All the inputs are encoded as commands. C.addCommand(std::make_unique<Command>( JA, *this, ResponseFileSupport::None(), TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), - CmdArgs, None, Outputs)); + CmdArgs, std::nullopt, Outputs)); } -void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA, - const InputInfo &Output, - const InputInfoList &Inputs, - const ArgList &Args, - const char *LinkingOutput) const { +void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA, + const InputInfo &Output, + const InputInfoList &Inputs, + const llvm::opt::ArgList &Args, + const char *LinkingOutput) const { ArgStringList CmdArgs; - const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); - - // Add the "effective" target triple. - CmdArgs.push_back("-target"); - CmdArgs.push_back(Args.MakeArgString(Triple.getTriple())); - // Add the output file name. assert(Output.isFilename() && "Invalid output."); CmdArgs.push_back("-o"); CmdArgs.push_back(Output.getFilename()); - // Add inputs. - for (const InputInfo &I : Inputs) { - assert(I.isFilename() && "Invalid input."); - CmdArgs.push_back(I.getFilename()); + // Create the inputs to bundle the needed metadata. + for (const InputInfo &Input : Inputs) { + const Action *OffloadAction = Input.getAction(); + const ToolChain *TC = OffloadAction->getOffloadingToolChain(); + const ArgList &TCArgs = + C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(), + OffloadAction->getOffloadingDeviceKind()); + StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input)); + StringRef Arch = OffloadAction->getOffloadingArch() + ? OffloadAction->getOffloadingArch() + : TCArgs.getLastArgValue(options::OPT_march_EQ); + StringRef Kind = + Action::GetOffloadKindName(OffloadAction->getOffloadingDeviceKind()); + + ArgStringList Features; + SmallVector<StringRef> FeatureArgs; + getTargetFeatures(TC->getDriver(), TC->getTriple(), TCArgs, Features, + false); + llvm::copy_if(Features, std::back_inserter(FeatureArgs), + [](StringRef Arg) { return !Arg.starts_with("-target"); }); + + if (TC->getTriple().isAMDGPU()) { + for (StringRef Feature : llvm::split(Arch.split(':').second, ':')) { + FeatureArgs.emplace_back( + Args.MakeArgString(Feature.take_back() + Feature.drop_back())); + } + } + + // TODO: We need to pass in the full target-id and handle it properly in the + // linker wrapper. + SmallVector<std::string> Parts{ + "file=" + File.str(), + "triple=" + TC->getTripleString(), + "arch=" + Arch.str(), + "kind=" + Kind.str(), + }; + + if (TC->getDriver().isUsingLTO(/* IsOffload */ true) || + TC->getTriple().isAMDGPU()) + for (StringRef Feature : FeatureArgs) + Parts.emplace_back("feature=" + Feature.str()); + + CmdArgs.push_back(Args.MakeArgString("--image=" + llvm::join(Parts, ","))); } C.addCommand(std::make_unique<Command>( @@ -7498,3 +9034,126 @@ void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA, Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), CmdArgs, Inputs, Output)); } + +void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, + const InputInfo &Output, + const InputInfoList &Inputs, + const ArgList &Args, + const char *LinkingOutput) const { + const Driver &D = getToolChain().getDriver(); + const llvm::Triple TheTriple = getToolChain().getTriple(); + ArgStringList CmdArgs; + + // Pass the CUDA path to the linker wrapper tool. + for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP}) { + auto TCRange = C.getOffloadToolChains(Kind); + for (auto &I : llvm::make_range(TCRange.first, TCRange.second)) { + const ToolChain *TC = I.second; + if (TC->getTriple().isNVPTX()) { + CudaInstallationDetector CudaInstallation(D, TheTriple, Args); + if (CudaInstallation.isValid()) + CmdArgs.push_back(Args.MakeArgString( + "--cuda-path=" + CudaInstallation.getInstallPath())); + break; + } + } + } + + // Pass in the optimization level to use for LTO. + if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { + StringRef OOpt; + if (A->getOption().matches(options::OPT_O4) || + A->getOption().matches(options::OPT_Ofast)) + OOpt = "3"; + else if (A->getOption().matches(options::OPT_O)) { + OOpt = A->getValue(); + if (OOpt == "g") + OOpt = "1"; + else if (OOpt == "s" || OOpt == "z") + OOpt = "2"; + } else if (A->getOption().matches(options::OPT_O0)) + OOpt = "0"; + if (!OOpt.empty()) + CmdArgs.push_back(Args.MakeArgString(Twine("--opt-level=O") + OOpt)); + } + + CmdArgs.push_back( + Args.MakeArgString("--host-triple=" + TheTriple.getTriple())); + if (Args.hasArg(options::OPT_v)) + CmdArgs.push_back("--wrapper-verbose"); + + if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { + if (!A->getOption().matches(options::OPT_g0)) + CmdArgs.push_back("--device-debug"); + } + + // code-object-version=X needs to be passed to clang-linker-wrapper to ensure + // that it is used by lld. + if (const Arg *A = Args.getLastArg(options::OPT_mcode_object_version_EQ)) { + CmdArgs.push_back(Args.MakeArgString("-mllvm")); + CmdArgs.push_back(Args.MakeArgString( + Twine("--amdhsa-code-object-version=") + A->getValue())); + } + + for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) + CmdArgs.push_back(Args.MakeArgString("--ptxas-arg=" + A)); + + // Forward remarks passes to the LLVM backend in the wrapper. + if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) + CmdArgs.push_back(Args.MakeArgString(Twine("--offload-opt=-pass-remarks=") + + A->getValue())); + if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) + CmdArgs.push_back(Args.MakeArgString( + Twine("--offload-opt=-pass-remarks-missed=") + A->getValue())); + if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) + CmdArgs.push_back(Args.MakeArgString( + Twine("--offload-opt=-pass-remarks-analysis=") + A->getValue())); + if (Args.getLastArg(options::OPT_save_temps_EQ)) + CmdArgs.push_back("--save-temps"); + + // Construct the link job so we can wrap around it. + Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); + const auto &LinkCommand = C.getJobs().getJobs().back(); + + // Forward -Xoffload-linker<-triple> arguments to the device link job. + for (Arg *A : Args.filtered(options::OPT_Xoffload_linker)) { + StringRef Val = A->getValue(0); + if (Val.empty()) + CmdArgs.push_back( + Args.MakeArgString(Twine("--device-linker=") + A->getValue(1))); + else + CmdArgs.push_back(Args.MakeArgString( + "--device-linker=" + + ToolChain::getOpenMPTriple(Val.drop_front()).getTriple() + "=" + + A->getValue(1))); + } + Args.ClaimAllArgs(options::OPT_Xoffload_linker); + + // Embed bitcode instead of an object in JIT mode. + if (Args.hasFlag(options::OPT_fopenmp_target_jit, + options::OPT_fno_openmp_target_jit, false)) + CmdArgs.push_back("--embed-bitcode"); + + // Forward `-mllvm` arguments to the LLVM invocations if present. + for (Arg *A : Args.filtered(options::OPT_mllvm)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back(A->getValue()); + A->claim(); + } + + // Add the linker arguments to be forwarded by the wrapper. + CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") + + LinkCommand->getExecutable())); + for (const char *LinkArg : LinkCommand->getArguments()) + CmdArgs.push_back(LinkArg); + + addOffloadCompressArgs(Args, CmdArgs); + + const char *Exec = + Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper")); + + // Replace the executable and arguments of the link job with the + // wrapper. + LinkCommand->replaceExecutable(Exec); + LinkCommand->replaceArguments(CmdArgs); +} |