10b57cec5SDimitry Andric //===-- Clang.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "Clang.h" 105ffd83dbSDimitry Andric #include "AMDGPU.h" 110b57cec5SDimitry Andric #include "Arch/AArch64.h" 120b57cec5SDimitry Andric #include "Arch/ARM.h" 1381ad6265SDimitry Andric #include "Arch/CSKY.h" 14bdd1243dSDimitry Andric #include "Arch/LoongArch.h" 15fe6060f1SDimitry Andric #include "Arch/M68k.h" 160b57cec5SDimitry Andric #include "Arch/Mips.h" 170b57cec5SDimitry Andric #include "Arch/PPC.h" 180b57cec5SDimitry Andric #include "Arch/RISCV.h" 190b57cec5SDimitry Andric #include "Arch/Sparc.h" 200b57cec5SDimitry Andric #include "Arch/SystemZ.h" 215ffd83dbSDimitry Andric #include "Arch/VE.h" 220b57cec5SDimitry Andric #include "Arch/X86.h" 230b57cec5SDimitry Andric #include "CommonArgs.h" 240b57cec5SDimitry Andric #include "Hexagon.h" 255ffd83dbSDimitry Andric #include "MSP430.h" 260b57cec5SDimitry Andric #include "PS4CPU.h" 27349cc55cSDimitry Andric #include "clang/Basic/CLWarnings.h" 280b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h" 29a7dea167SDimitry Andric #include "clang/Basic/CodeGenOptions.h" 30bdd1243dSDimitry Andric #include "clang/Basic/HeaderInclude.h" 310b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 32753f127fSDimitry Andric #include "clang/Basic/MakeSupport.h" 330b57cec5SDimitry Andric #include "clang/Basic/ObjCRuntime.h" 340b57cec5SDimitry Andric #include "clang/Basic/Version.h" 3581ad6265SDimitry Andric #include "clang/Config/config.h" 3681ad6265SDimitry Andric #include "clang/Driver/Action.h" 370b57cec5SDimitry Andric #include "clang/Driver/Distro.h" 380b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h" 39fe6060f1SDimitry Andric #include "clang/Driver/InputInfo.h" 400b57cec5SDimitry Andric #include "clang/Driver/Options.h" 410b57cec5SDimitry Andric #include "clang/Driver/SanitizerArgs.h" 4204eeddc0SDimitry Andric #include "clang/Driver/Types.h" 430b57cec5SDimitry Andric #include "clang/Driver/XRayArgs.h" 4481ad6265SDimitry Andric #include "llvm/ADT/SmallSet.h" 450b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 460b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 470b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 480b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 495ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h" 500b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 5106c3fb27SDimitry Andric #include "llvm/Support/Error.h" 520b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 530b57cec5SDimitry Andric #include "llvm/Support/Path.h" 540b57cec5SDimitry Andric #include "llvm/Support/Process.h" 5506c3fb27SDimitry Andric #include "llvm/Support/RISCVISAInfo.h" 560b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h" 5706c3fb27SDimitry Andric #include "llvm/TargetParser/ARMTargetParserCommon.h" 5806c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h" 598a4dda33SDimitry Andric #include "llvm/TargetParser/LoongArchTargetParser.h" 6006c3fb27SDimitry Andric #include "llvm/TargetParser/RISCVTargetParser.h" 61753f127fSDimitry Andric #include <cctype> 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric using namespace clang::driver; 640b57cec5SDimitry Andric using namespace clang::driver::tools; 650b57cec5SDimitry Andric using namespace clang; 660b57cec5SDimitry Andric using namespace llvm::opt; 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { 69349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC, 70349cc55cSDimitry Andric options::OPT_fminimize_whitespace, 715f757f3fSDimitry Andric options::OPT_fno_minimize_whitespace, 725f757f3fSDimitry Andric options::OPT_fkeep_system_includes, 735f757f3fSDimitry Andric options::OPT_fno_keep_system_includes)) { 740b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) && 750b57cec5SDimitry Andric !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) { 760b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 770b57cec5SDimitry Andric << A->getBaseArg().getAsString(Args) 780b57cec5SDimitry Andric << (D.IsCLMode() ? "/E, /P or /EP" : "-E"); 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 840b57cec5SDimitry Andric // In gcc, only ARM checks this, but it seems reasonable to check universally. 850b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 860b57cec5SDimitry Andric if (const Arg *A = 870b57cec5SDimitry Andric Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic)) 880b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args) 890b57cec5SDimitry Andric << "-static"; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes. 930b57cec5SDimitry Andric // This is used for the space-separated argument list specified with 940b57cec5SDimitry Andric // the -dwarf-debug-flags option. 950b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg, 960b57cec5SDimitry Andric SmallVectorImpl<char> &Res) { 970b57cec5SDimitry Andric for (; *Arg; ++Arg) { 980b57cec5SDimitry Andric switch (*Arg) { 990b57cec5SDimitry Andric default: 1000b57cec5SDimitry Andric break; 1010b57cec5SDimitry Andric case ' ': 1020b57cec5SDimitry Andric case '\\': 1030b57cec5SDimitry Andric Res.push_back('\\'); 1040b57cec5SDimitry Andric break; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric Res.push_back(*Arg); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other 1110b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA. 1120b57cec5SDimitry Andric static void 1130b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA, 1140b57cec5SDimitry Andric const ToolChain &RegularToolChain, 1150b57cec5SDimitry Andric llvm::function_ref<void(const ToolChain &)> Work) { 1160b57cec5SDimitry Andric // Apply Work on the current/regular tool chain. 1170b57cec5SDimitry Andric Work(RegularToolChain); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // Apply Work on all the offloading tool chains associated with the current 1200b57cec5SDimitry Andric // action. 1210b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_Cuda)) 1220b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 1230b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_Cuda)) 1240b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1250b57cec5SDimitry Andric else if (JA.isHostOffloading(Action::OFK_HIP)) 1260b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>()); 1270b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_HIP)) 1280b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 1310b57cec5SDimitry Andric auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>(); 1320b57cec5SDimitry Andric for (auto II = TCs.first, IE = TCs.second; II != IE; ++II) 1330b57cec5SDimitry Andric Work(*II->second); 1340b57cec5SDimitry Andric } else if (JA.isDeviceOffloading(Action::OFK_OpenMP)) 1350b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric // 1380b57cec5SDimitry Andric // TODO: Add support for other offloading programming models here. 1390b57cec5SDimitry Andric // 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step 1430b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error 1440b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position 1450b57cec5SDimitry Andric /// of the refinement step in the input string. 1460b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D, 1470b57cec5SDimitry Andric const Arg &A, size_t &Position) { 1480b57cec5SDimitry Andric const char RefinementStepToken = ':'; 1490b57cec5SDimitry Andric Position = In.find(RefinementStepToken); 1500b57cec5SDimitry Andric if (Position != StringRef::npos) { 1510b57cec5SDimitry Andric StringRef Option = A.getOption().getName(); 1520b57cec5SDimitry Andric StringRef RefStep = In.substr(Position + 1); 1530b57cec5SDimitry Andric // Allow exactly one numeric character for the additional refinement 1540b57cec5SDimitry Andric // step parameter. This is reasonable for all currently-supported 1550b57cec5SDimitry Andric // operations and architectures because we would expect that a larger value 1560b57cec5SDimitry Andric // of refinement steps would cause the estimate "optimization" to 1570b57cec5SDimitry Andric // under-perform the native operation. Also, if the estimate does not 1580b57cec5SDimitry Andric // converge quickly, it probably will not ever converge, so further 1590b57cec5SDimitry Andric // refinement steps will not produce a better answer. 1600b57cec5SDimitry Andric if (RefStep.size() != 1) { 1610b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1620b57cec5SDimitry Andric return false; 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric char RefStepChar = RefStep[0]; 1650b57cec5SDimitry Andric if (RefStepChar < '0' || RefStepChar > '9') { 1660b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1670b57cec5SDimitry Andric return false; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric return true; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters. 1740b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args, 1750b57cec5SDimitry Andric ArgStringList &OutStrings) { 1760b57cec5SDimitry Andric StringRef DisabledPrefixIn = "!"; 1770b57cec5SDimitry Andric StringRef DisabledPrefixOut = "!"; 1780b57cec5SDimitry Andric StringRef EnabledPrefixOut = ""; 1790b57cec5SDimitry Andric StringRef Out = "-mrecip="; 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ); 1820b57cec5SDimitry Andric if (!A) 1830b57cec5SDimitry Andric return; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric unsigned NumOptions = A->getNumValues(); 1860b57cec5SDimitry Andric if (NumOptions == 0) { 1870b57cec5SDimitry Andric // No option is the same as "all". 1880b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + "all")); 1890b57cec5SDimitry Andric return; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric // Pass through "all", "none", or "default" with an optional refinement step. 1930b57cec5SDimitry Andric if (NumOptions == 1) { 1940b57cec5SDimitry Andric StringRef Val = A->getValue(0); 1950b57cec5SDimitry Andric size_t RefStepLoc; 1960b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStepLoc)) 1970b57cec5SDimitry Andric return; 1980b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStepLoc); 1990b57cec5SDimitry Andric if (ValBase == "all" || ValBase == "none" || ValBase == "default") { 2000b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + Val)); 2010b57cec5SDimitry Andric return; 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Each reciprocal type may be enabled or disabled individually. 2060b57cec5SDimitry Andric // Check each input value for validity, concatenate them all back together, 2070b57cec5SDimitry Andric // and pass through. 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric llvm::StringMap<bool> OptionStrings; 2100b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divd", false)); 2110b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divf", false)); 21281ad6265SDimitry Andric OptionStrings.insert(std::make_pair("divh", false)); 2130b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divd", false)); 2140b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divf", false)); 21581ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-divh", false)); 2160b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtd", false)); 2170b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtf", false)); 21881ad6265SDimitry Andric OptionStrings.insert(std::make_pair("sqrth", false)); 2190b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtd", false)); 2200b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtf", false)); 22181ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrth", false)); 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric for (unsigned i = 0; i != NumOptions; ++i) { 2240b57cec5SDimitry Andric StringRef Val = A->getValue(i); 2250b57cec5SDimitry Andric 2265f757f3fSDimitry Andric bool IsDisabled = Val.starts_with(DisabledPrefixIn); 2270b57cec5SDimitry Andric // Ignore the disablement token for string matching. 2280b57cec5SDimitry Andric if (IsDisabled) 2290b57cec5SDimitry Andric Val = Val.substr(1); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric size_t RefStep; 2320b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStep)) 2330b57cec5SDimitry Andric return; 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStep); 2360b57cec5SDimitry Andric llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase); 2370b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2380b57cec5SDimitry Andric // Try again specifying float suffix. 2390b57cec5SDimitry Andric OptionIter = OptionStrings.find(ValBase.str() + 'f'); 2400b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2410b57cec5SDimitry Andric // The input name did not match any known option string. 2420b57cec5SDimitry Andric D.Diag(diag::err_drv_unknown_argument) << Val; 2430b57cec5SDimitry Andric return; 2440b57cec5SDimitry Andric } 24581ad6265SDimitry Andric // The option was specified without a half or float or double suffix. 24681ad6265SDimitry Andric // Make sure that the double or half entry was not already specified. 2470b57cec5SDimitry Andric // The float entry will be checked below. 24881ad6265SDimitry Andric if (OptionStrings[ValBase.str() + 'd'] || 24981ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h']) { 2500b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2510b57cec5SDimitry Andric return; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric if (OptionIter->second == true) { 2560b57cec5SDimitry Andric // Duplicate option specified. 2570b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2580b57cec5SDimitry Andric return; 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric // Mark the matched option as found. Do not allow duplicate specifiers. 2620b57cec5SDimitry Andric OptionIter->second = true; 2630b57cec5SDimitry Andric 26481ad6265SDimitry Andric // If the precision was not specified, also mark the double and half entry 26581ad6265SDimitry Andric // as found. 26681ad6265SDimitry Andric if (ValBase.back() != 'f' && ValBase.back() != 'd' && ValBase.back() != 'h') { 2670b57cec5SDimitry Andric OptionStrings[ValBase.str() + 'd'] = true; 26881ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h'] = true; 26981ad6265SDimitry Andric } 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // Build the output string. 2720b57cec5SDimitry Andric StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut; 2730b57cec5SDimitry Andric Out = Args.MakeArgString(Out + Prefix + Val); 2740b57cec5SDimitry Andric if (i != NumOptions - 1) 2750b57cec5SDimitry Andric Out = Args.MakeArgString(Out + ","); 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out)); 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer 2820b57cec5SDimitry Andric /// or the string "none". 2830b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args, 2840b57cec5SDimitry Andric ArgStringList &CmdArgs) { 2850b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ); 2860b57cec5SDimitry Andric if (!A) 2870b57cec5SDimitry Andric return; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric StringRef Value = A->getValue(); 2900b57cec5SDimitry Andric if (Value == "none") { 2910b57cec5SDimitry Andric CmdArgs.push_back("-mprefer-vector-width=none"); 2920b57cec5SDimitry Andric } else { 2930b57cec5SDimitry Andric unsigned Width; 2940b57cec5SDimitry Andric if (Value.getAsInteger(10, Width)) { 2950b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 2960b57cec5SDimitry Andric return; 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value)); 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric static bool 3030b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 3040b57cec5SDimitry Andric const llvm::Triple &Triple) { 3050b57cec5SDimitry Andric // We use the zero-cost exception tables for Objective-C if the non-fragile 3060b57cec5SDimitry Andric // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 3070b57cec5SDimitry Andric // later. 3080b57cec5SDimitry Andric if (runtime.isNonFragile()) 3090b57cec5SDimitry Andric return true; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric if (!Triple.isMacOSX()) 3120b57cec5SDimitry Andric return false; 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric return (!Triple.isMacOSXVersionLT(10, 5) && 3150b57cec5SDimitry Andric (Triple.getArch() == llvm::Triple::x86_64 || 3160b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::arm)); 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a 3204824e7fdSDimitry Andric /// main flag, -fexceptions and also language specific flags to enable/disable 3210b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example 3220b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions. 323fe6060f1SDimitry Andric static bool addExceptionArgs(const ArgList &Args, types::ID InputType, 3240b57cec5SDimitry Andric const ToolChain &TC, bool KernelOrKext, 3250b57cec5SDimitry Andric const ObjCRuntime &objcRuntime, 3260b57cec5SDimitry Andric ArgStringList &CmdArgs) { 3270b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric if (KernelOrKext) { 3300b57cec5SDimitry Andric // -mkernel and -fapple-kext imply no exceptions, so claim exception related 3310b57cec5SDimitry Andric // arguments now to avoid warnings about unused arguments. 3320b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fexceptions); 3330b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_exceptions); 3340b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 3350b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 3360b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 3370b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 338fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fasync_exceptions); 339fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_async_exceptions); 340fe6060f1SDimitry Andric return false; 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric // See if the user explicitly enabled exceptions. 3440b57cec5SDimitry Andric bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, 3450b57cec5SDimitry Andric false); 3460b57cec5SDimitry Andric 347fe6060f1SDimitry Andric bool EHa = Args.hasFlag(options::OPT_fasync_exceptions, 348fe6060f1SDimitry Andric options::OPT_fno_async_exceptions, false); 349fe6060f1SDimitry Andric if (EHa) { 350fe6060f1SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 351fe6060f1SDimitry Andric EH = true; 352fe6060f1SDimitry Andric } 353fe6060f1SDimitry Andric 3540b57cec5SDimitry Andric // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 3550b57cec5SDimitry Andric // is not necessarily sensible, but follows GCC. 3560b57cec5SDimitry Andric if (types::isObjC(InputType) && 3570b57cec5SDimitry Andric Args.hasFlag(options::OPT_fobjc_exceptions, 3580b57cec5SDimitry Andric options::OPT_fno_objc_exceptions, true)) { 3590b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-exceptions"); 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric if (types::isCXX(InputType)) { 36581ad6265SDimitry Andric // Disable C++ EH by default on XCore and PS4/PS5. 36681ad6265SDimitry Andric bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore && 36781ad6265SDimitry Andric !Triple.isPS() && !Triple.isDriverKit(); 3680b57cec5SDimitry Andric Arg *ExceptionArg = Args.getLastArg( 3690b57cec5SDimitry Andric options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 3700b57cec5SDimitry Andric options::OPT_fexceptions, options::OPT_fno_exceptions); 3710b57cec5SDimitry Andric if (ExceptionArg) 3720b57cec5SDimitry Andric CXXExceptionsEnabled = 3730b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) || 3740b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fexceptions); 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric if (CXXExceptionsEnabled) { 3770b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric EH = true; 3800b57cec5SDimitry Andric } 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3835ffd83dbSDimitry Andric // OPT_fignore_exceptions means exception could still be thrown, 3845ffd83dbSDimitry Andric // but no clean up or catch would happen in current module. 3855ffd83dbSDimitry Andric // So we do not set EH to false. 3865ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions); 3875ffd83dbSDimitry Andric 3885f757f3fSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fassume_nothrow_exception_dtor, 3895f757f3fSDimitry Andric options::OPT_fno_assume_nothrow_exception_dtor); 3905f757f3fSDimitry Andric 3910b57cec5SDimitry Andric if (EH) 3920b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 393fe6060f1SDimitry Andric return EH; 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 396480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, 397480093f4SDimitry Andric const JobAction &JA) { 3980b57cec5SDimitry Andric bool Default = true; 3990b57cec5SDimitry Andric if (TC.getTriple().isOSDarwin()) { 4000b57cec5SDimitry Andric // The native darwin assembler doesn't support the linker_option directives, 4010b57cec5SDimitry Andric // so we disable them if we think the .s file will be passed to it. 4020b57cec5SDimitry Andric Default = TC.useIntegratedAs(); 4030b57cec5SDimitry Andric } 404480093f4SDimitry Andric // The linker_option directives are intended for host compilation. 405480093f4SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 406480093f4SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 407480093f4SDimitry Andric Default = false; 408480093f4SDimitry Andric return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 4090b57cec5SDimitry Andric Default); 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric /// Add a CC1 option to specify the debug compilation directory. 4130eae32dcSDimitry Andric static const char *addDebugCompDirArg(const ArgList &Args, 4140eae32dcSDimitry Andric ArgStringList &CmdArgs, 415a7dea167SDimitry Andric const llvm::vfs::FileSystem &VFS) { 416fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 417fe6060f1SDimitry Andric options::OPT_fdebug_compilation_dir_EQ)) { 418fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 419fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") + 420fe6060f1SDimitry Andric A->getValue())); 421fe6060f1SDimitry Andric else 422fe6060f1SDimitry Andric A->render(Args, CmdArgs); 423a7dea167SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 424a7dea167SDimitry Andric VFS.getCurrentWorkingDirectory()) { 425fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD)); 4260b57cec5SDimitry Andric } 4270eae32dcSDimitry Andric StringRef Path(CmdArgs.back()); 4280eae32dcSDimitry Andric return Path.substr(Path.find('=') + 1).data(); 4290eae32dcSDimitry Andric } 4300eae32dcSDimitry Andric 4310eae32dcSDimitry Andric static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs, 4320eae32dcSDimitry Andric const char *DebugCompilationDir, 4330eae32dcSDimitry Andric const char *OutputFileName) { 4340eae32dcSDimitry Andric // No need to generate a value for -object-file-name if it was provided. 4350eae32dcSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 4365f757f3fSDimitry Andric if (StringRef(Arg->getValue()).starts_with("-object-file-name")) 4370eae32dcSDimitry Andric return; 4380eae32dcSDimitry Andric 4390eae32dcSDimitry Andric if (Args.hasArg(options::OPT_object_file_name_EQ)) 4400eae32dcSDimitry Andric return; 4410eae32dcSDimitry Andric 4420eae32dcSDimitry Andric SmallString<128> ObjFileNameForDebug(OutputFileName); 4430eae32dcSDimitry Andric if (ObjFileNameForDebug != "-" && 4440eae32dcSDimitry Andric !llvm::sys::path::is_absolute(ObjFileNameForDebug) && 4450eae32dcSDimitry Andric (!DebugCompilationDir || 4460eae32dcSDimitry Andric llvm::sys::path::is_absolute(DebugCompilationDir))) { 4470eae32dcSDimitry Andric // Make the path absolute in the debug infos like MSVC does. 4480eae32dcSDimitry Andric llvm::sys::fs::make_absolute(ObjFileNameForDebug); 4490eae32dcSDimitry Andric } 45006c3fb27SDimitry Andric // If the object file name is a relative path, then always use Windows 45106c3fb27SDimitry Andric // backslash style as -object-file-name is used for embedding object file path 45206c3fb27SDimitry Andric // in codeview and it can only be generated when targeting on Windows. 45306c3fb27SDimitry Andric // Otherwise, just use native absolute path. 45406c3fb27SDimitry Andric llvm::sys::path::Style Style = 45506c3fb27SDimitry Andric llvm::sys::path::is_absolute(ObjFileNameForDebug) 45606c3fb27SDimitry Andric ? llvm::sys::path::Style::native 45706c3fb27SDimitry Andric : llvm::sys::path::Style::windows_backslash; 45806c3fb27SDimitry Andric llvm::sys::path::remove_dots(ObjFileNameForDebug, /*remove_dot_dot=*/true, 45906c3fb27SDimitry Andric Style); 4600eae32dcSDimitry Andric CmdArgs.push_back( 4610eae32dcSDimitry Andric Args.MakeArgString(Twine("-object-file-name=") + ObjFileNameForDebug)); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map. 46581ad6265SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC, 46681ad6265SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 46781ad6265SDimitry Andric auto AddOneArg = [&](StringRef Map, StringRef Name) { 468349cc55cSDimitry Andric if (!Map.contains('=')) 46981ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) << Map << Name; 4700b57cec5SDimitry Andric else 4710b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); 47281ad6265SDimitry Andric }; 47381ad6265SDimitry Andric 47481ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 47581ad6265SDimitry Andric options::OPT_fdebug_prefix_map_EQ)) { 47681ad6265SDimitry Andric AddOneArg(A->getValue(), A->getOption().getName()); 4770b57cec5SDimitry Andric A->claim(); 4780b57cec5SDimitry Andric } 47981ad6265SDimitry Andric std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping(); 48081ad6265SDimitry Andric if (GlobalRemapEntry.empty()) 48181ad6265SDimitry Andric return; 48281ad6265SDimitry Andric AddOneArg(GlobalRemapEntry, "environment"); 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric 485480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map. 486480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, 487480093f4SDimitry Andric ArgStringList &CmdArgs) { 488480093f4SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 489480093f4SDimitry Andric options::OPT_fmacro_prefix_map_EQ)) { 490480093f4SDimitry Andric StringRef Map = A->getValue(); 491349cc55cSDimitry Andric if (!Map.contains('=')) 492480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 493480093f4SDimitry Andric << Map << A->getOption().getName(); 494480093f4SDimitry Andric else 495480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map)); 496480093f4SDimitry Andric A->claim(); 497480093f4SDimitry Andric } 498480093f4SDimitry Andric } 499480093f4SDimitry Andric 500e8d8bef9SDimitry Andric /// Add a CC1 and CC1AS option to specify the coverage file path prefix map. 501fe6060f1SDimitry Andric static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args, 502e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 503e8d8bef9SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 504fe6060f1SDimitry Andric options::OPT_fcoverage_prefix_map_EQ)) { 505e8d8bef9SDimitry Andric StringRef Map = A->getValue(); 506349cc55cSDimitry Andric if (!Map.contains('=')) 507e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 508e8d8bef9SDimitry Andric << Map << A->getOption().getName(); 509e8d8bef9SDimitry Andric else 510fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map)); 511e8d8bef9SDimitry Andric A->claim(); 512e8d8bef9SDimitry Andric } 513e8d8bef9SDimitry Andric } 514e8d8bef9SDimitry Andric 5150b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz. 5160b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is 5170b57cec5SDimitry Andric /// enabled. 5180b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) { 5190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 5200b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 5210b57cec5SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 5220b57cec5SDimitry Andric return true; 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O0)) 5250b57cec5SDimitry Andric return false; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric // Vectorize -Os. 5300b57cec5SDimitry Andric StringRef S(A->getValue()); 5310b57cec5SDimitry Andric if (S == "s") 5320b57cec5SDimitry Andric return true; 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric // Don't vectorize -Oz, unless it's the slp vectorizer. 5350b57cec5SDimitry Andric if (S == "z") 5360b57cec5SDimitry Andric return isSlpVec; 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric unsigned OptLevel = 0; 5390b57cec5SDimitry Andric if (S.getAsInteger(10, OptLevel)) 5400b57cec5SDimitry Andric return false; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric return OptLevel > 1; 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric return false; 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input. 5490b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 5500b57cec5SDimitry Andric ArgStringList &CmdArgs) { 5510b57cec5SDimitry Andric // When using -verify-pch, we don't want to provide the type 5520b57cec5SDimitry Andric // 'precompiled-header' if it was inferred from the file extension 5530b57cec5SDimitry Andric if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH) 5540b57cec5SDimitry Andric return; 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric CmdArgs.push_back("-x"); 5570b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc)) 5580b57cec5SDimitry Andric CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 5590b57cec5SDimitry Andric else { 5600b57cec5SDimitry Andric // Map the driver type to the frontend type. This is mostly an identity 5610b57cec5SDimitry Andric // mapping, except that the distinction between module interface units 5620b57cec5SDimitry Andric // and other source files does not exist at the frontend layer. 5630b57cec5SDimitry Andric const char *ClangType; 5640b57cec5SDimitry Andric switch (Input.getType()) { 5650b57cec5SDimitry Andric case types::TY_CXXModule: 5660b57cec5SDimitry Andric ClangType = "c++"; 5670b57cec5SDimitry Andric break; 5680b57cec5SDimitry Andric case types::TY_PP_CXXModule: 5690b57cec5SDimitry Andric ClangType = "c++-cpp-output"; 5700b57cec5SDimitry Andric break; 5710b57cec5SDimitry Andric default: 5720b57cec5SDimitry Andric ClangType = types::getTypeName(Input.getType()); 5730b57cec5SDimitry Andric break; 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric CmdArgs.push_back(ClangType); 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, 58006c3fb27SDimitry Andric const JobAction &JA, const InputInfo &Output, 581349cc55cSDimitry Andric const ArgList &Args, SanitizerArgs &SanArgs, 5820b57cec5SDimitry Andric ArgStringList &CmdArgs) { 58306c3fb27SDimitry Andric const Driver &D = TC.getDriver(); 5840b57cec5SDimitry Andric auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, 5850b57cec5SDimitry Andric options::OPT_fprofile_generate_EQ, 5860b57cec5SDimitry Andric options::OPT_fno_profile_generate); 5870b57cec5SDimitry Andric if (PGOGenerateArg && 5880b57cec5SDimitry Andric PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 5890b57cec5SDimitry Andric PGOGenerateArg = nullptr; 5900b57cec5SDimitry Andric 59106c3fb27SDimitry Andric auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args); 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric auto *ProfileGenerateArg = Args.getLastArg( 5940b57cec5SDimitry Andric options::OPT_fprofile_instr_generate, 5950b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ, 5960b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate); 5970b57cec5SDimitry Andric if (ProfileGenerateArg && 5980b57cec5SDimitry Andric ProfileGenerateArg->getOption().matches( 5990b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate)) 6000b57cec5SDimitry Andric ProfileGenerateArg = nullptr; 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric if (PGOGenerateArg && ProfileGenerateArg) 6030b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 6040b57cec5SDimitry Andric << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling(); 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric auto *ProfileUseArg = getLastProfileUseArg(Args); 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric if (PGOGenerateArg && ProfileUseArg) 6090b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 6100b57cec5SDimitry Andric << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling(); 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric if (ProfileGenerateArg && ProfileUseArg) 6130b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 6140b57cec5SDimitry Andric << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling(); 6150b57cec5SDimitry Andric 616e8d8bef9SDimitry Andric if (CSPGOGenerateArg && PGOGenerateArg) { 6170b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 6180b57cec5SDimitry Andric << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling(); 619e8d8bef9SDimitry Andric PGOGenerateArg = nullptr; 620e8d8bef9SDimitry Andric } 6210b57cec5SDimitry Andric 622fe6060f1SDimitry Andric if (TC.getTriple().isOSAIX()) { 623fe6060f1SDimitry Andric if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args)) 624fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 625fe6060f1SDimitry Andric << ProfileSampleUseArg->getSpelling() << TC.getTriple().str(); 626fe6060f1SDimitry Andric } 627fe6060f1SDimitry Andric 6280b57cec5SDimitry Andric if (ProfileGenerateArg) { 6290b57cec5SDimitry Andric if (ProfileGenerateArg->getOption().matches( 6300b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ)) 6310b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") + 6320b57cec5SDimitry Andric ProfileGenerateArg->getValue())); 6330b57cec5SDimitry Andric // The default is to use Clang Instrumentation. 6340b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=clang"); 6350b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 6360b57cec5SDimitry Andric // Add dependent lib for clang_rt.profile 6375ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 6385ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric Arg *PGOGenArg = nullptr; 6430b57cec5SDimitry Andric if (PGOGenerateArg) { 6440b57cec5SDimitry Andric assert(!CSPGOGenerateArg); 6450b57cec5SDimitry Andric PGOGenArg = PGOGenerateArg; 6460b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=llvm"); 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric if (CSPGOGenerateArg) { 6490b57cec5SDimitry Andric assert(!PGOGenerateArg); 6500b57cec5SDimitry Andric PGOGenArg = CSPGOGenerateArg; 6510b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=csllvm"); 6520b57cec5SDimitry Andric } 6530b57cec5SDimitry Andric if (PGOGenArg) { 6540b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 6555ffd83dbSDimitry Andric // Add dependent lib for clang_rt.profile 6565ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 6575ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric if (PGOGenArg->getOption().matches( 6600b57cec5SDimitry Andric PGOGenerateArg ? options::OPT_fprofile_generate_EQ 6610b57cec5SDimitry Andric : options::OPT_fcs_profile_generate_EQ)) { 6620b57cec5SDimitry Andric SmallString<128> Path(PGOGenArg->getValue()); 6630b57cec5SDimitry Andric llvm::sys::path::append(Path, "default_%m.profraw"); 6640b57cec5SDimitry Andric CmdArgs.push_back( 6650b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path)); 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric if (ProfileUseArg) { 6700b57cec5SDimitry Andric if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ)) 6710b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 6720b57cec5SDimitry Andric Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue())); 6730b57cec5SDimitry Andric else if ((ProfileUseArg->getOption().matches( 6740b57cec5SDimitry Andric options::OPT_fprofile_use_EQ) || 6750b57cec5SDimitry Andric ProfileUseArg->getOption().matches( 6760b57cec5SDimitry Andric options::OPT_fprofile_instr_use))) { 6770b57cec5SDimitry Andric SmallString<128> Path( 6780b57cec5SDimitry Andric ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); 6790b57cec5SDimitry Andric if (Path.empty() || llvm::sys::fs::is_directory(Path)) 6800b57cec5SDimitry Andric llvm::sys::path::append(Path, "default.profdata"); 6810b57cec5SDimitry Andric CmdArgs.push_back( 6820b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path)); 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric 6865ffd83dbSDimitry Andric bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage, 6875ffd83dbSDimitry Andric options::OPT_fno_test_coverage, false) || 688a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage); 6895ffd83dbSDimitry Andric bool EmitCovData = TC.needsGCovInstrumentation(Args); 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoverage_mapping, 6920b57cec5SDimitry Andric options::OPT_fno_coverage_mapping, false)) { 6930b57cec5SDimitry Andric if (!ProfileGenerateArg) 6940b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 6950b57cec5SDimitry Andric << "-fcoverage-mapping" 6960b57cec5SDimitry Andric << "-fprofile-instr-generate"; 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric CmdArgs.push_back("-fcoverage-mapping"); 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric 7011db9f3b2SDimitry Andric if (Args.hasFlag(options::OPT_fmcdc_coverage, options::OPT_fno_mcdc_coverage, 7021db9f3b2SDimitry Andric false)) { 7031db9f3b2SDimitry Andric if (!Args.hasFlag(options::OPT_fcoverage_mapping, 7041db9f3b2SDimitry Andric options::OPT_fno_coverage_mapping, false)) 7051db9f3b2SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 7061db9f3b2SDimitry Andric << "-fcoverage-mcdc" 7071db9f3b2SDimitry Andric << "-fcoverage-mapping"; 7081db9f3b2SDimitry Andric 7091db9f3b2SDimitry Andric CmdArgs.push_back("-fcoverage-mcdc"); 7101db9f3b2SDimitry Andric } 7111db9f3b2SDimitry Andric 712fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 713fe6060f1SDimitry Andric options::OPT_fcoverage_compilation_dir_EQ)) { 714fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 715fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 716fe6060f1SDimitry Andric Twine("-fcoverage-compilation-dir=") + A->getValue())); 717fe6060f1SDimitry Andric else 718fe6060f1SDimitry Andric A->render(Args, CmdArgs); 719fe6060f1SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 720fe6060f1SDimitry Andric D.getVFS().getCurrentWorkingDirectory()) { 721fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD)); 722fe6060f1SDimitry Andric } 723fe6060f1SDimitry Andric 7240b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) { 7250b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ); 7260b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 7270b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 7280b57cec5SDimitry Andric << "-fprofile-exclude-files=" 7290b57cec5SDimitry Andric << "--coverage"; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric StringRef v = Arg->getValue(); 7320b57cec5SDimitry Andric CmdArgs.push_back( 7330b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-exclude-files=" + v))); 7340b57cec5SDimitry Andric } 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) { 7370b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ); 7380b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 7390b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 7400b57cec5SDimitry Andric << "-fprofile-filter-files=" 7410b57cec5SDimitry Andric << "--coverage"; 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric StringRef v = Arg->getValue(); 7440b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v))); 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 747e8d8bef9SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) { 748e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 749e8d8bef9SDimitry Andric if (Val == "atomic" || Val == "prefer-atomic") 750e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 751e8d8bef9SDimitry Andric else if (Val != "single") 752e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 753bdd1243dSDimitry Andric << A->getSpelling() << Val; 754e8d8bef9SDimitry Andric } 755e8d8bef9SDimitry Andric 756fcaf7f86SDimitry Andric int FunctionGroups = 1; 757fcaf7f86SDimitry Andric int SelectedFunctionGroup = 0; 758fcaf7f86SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_function_groups)) { 759fcaf7f86SDimitry Andric StringRef Val = A->getValue(); 760fcaf7f86SDimitry Andric if (Val.getAsInteger(0, FunctionGroups) || FunctionGroups < 1) 761fcaf7f86SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; 762fcaf7f86SDimitry Andric } 763fcaf7f86SDimitry Andric if (const auto *A = 764fcaf7f86SDimitry Andric Args.getLastArg(options::OPT_fprofile_selected_function_group)) { 765fcaf7f86SDimitry Andric StringRef Val = A->getValue(); 766fcaf7f86SDimitry Andric if (Val.getAsInteger(0, SelectedFunctionGroup) || 767fcaf7f86SDimitry Andric SelectedFunctionGroup < 0 || SelectedFunctionGroup >= FunctionGroups) 768fcaf7f86SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; 769fcaf7f86SDimitry Andric } 770fcaf7f86SDimitry Andric if (FunctionGroups != 1) 771fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-function-groups=" + 772fcaf7f86SDimitry Andric Twine(FunctionGroups))); 773fcaf7f86SDimitry Andric if (SelectedFunctionGroup != 0) 774fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-selected-function-group=" + 775fcaf7f86SDimitry Andric Twine(SelectedFunctionGroup))); 776fcaf7f86SDimitry Andric 777a7dea167SDimitry Andric // Leave -fprofile-dir= an unused argument unless .gcda emission is 778a7dea167SDimitry Andric // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider 779a7dea167SDimitry Andric // the flag used. There is no -fno-profile-dir, so the user has no 780a7dea167SDimitry Andric // targeted way to suppress the warning. 781a7dea167SDimitry Andric Arg *FProfileDir = nullptr; 782a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fprofile_arcs) || 783a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage)) 784a7dea167SDimitry Andric FProfileDir = Args.getLastArg(options::OPT_fprofile_dir); 785a7dea167SDimitry Andric 78606c3fb27SDimitry Andric // TODO: Don't claim -c/-S to warn about -fsyntax-only -c/-S, -E -c/-S, 78706c3fb27SDimitry Andric // like we warn about -fsyntax-only -E. 78806c3fb27SDimitry Andric (void)(Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)); 78906c3fb27SDimitry Andric 79006c3fb27SDimitry Andric // Put the .gcno and .gcda files (if needed) next to the primary output file, 79106c3fb27SDimitry Andric // or fall back to a file in the current directory for `clang -c --coverage 79206c3fb27SDimitry Andric // d/a.c` in the absence of -o. 79306c3fb27SDimitry Andric if (EmitCovNotes || EmitCovData) { 79406c3fb27SDimitry Andric SmallString<128> CoverageFilename; 79506c3fb27SDimitry Andric if (Arg *DumpDir = Args.getLastArgNoClaim(options::OPT_dumpdir)) { 79606c3fb27SDimitry Andric // Form ${dumpdir}${basename}.gcno. Note that dumpdir may not end with a 79706c3fb27SDimitry Andric // path separator. 79806c3fb27SDimitry Andric CoverageFilename = DumpDir->getValue(); 79906c3fb27SDimitry Andric CoverageFilename += llvm::sys::path::filename(Output.getBaseInput()); 80006c3fb27SDimitry Andric } else if (Arg *FinalOutput = 80106c3fb27SDimitry Andric C.getArgs().getLastArg(options::OPT__SLASH_Fo)) { 80206c3fb27SDimitry Andric CoverageFilename = FinalOutput->getValue(); 80306c3fb27SDimitry Andric } else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) { 80406c3fb27SDimitry Andric CoverageFilename = FinalOutput->getValue(); 80506c3fb27SDimitry Andric } else { 80606c3fb27SDimitry Andric CoverageFilename = llvm::sys::path::filename(Output.getBaseInput()); 80706c3fb27SDimitry Andric } 808a7dea167SDimitry Andric if (llvm::sys::path::is_relative(CoverageFilename)) 809a7dea167SDimitry Andric (void)D.getVFS().makeAbsolute(CoverageFilename); 8100b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcno"); 81106c3fb27SDimitry Andric if (EmitCovNotes) { 8125f757f3fSDimitry Andric CmdArgs.push_back( 8135f757f3fSDimitry Andric Args.MakeArgString("-coverage-notes-file=" + CoverageFilename)); 81406c3fb27SDimitry Andric } 8150b57cec5SDimitry Andric 816a7dea167SDimitry Andric if (EmitCovData) { 817a7dea167SDimitry Andric if (FProfileDir) { 81806c3fb27SDimitry Andric SmallString<128> Gcno = std::move(CoverageFilename); 8190b57cec5SDimitry Andric CoverageFilename = FProfileDir->getValue(); 82006c3fb27SDimitry Andric llvm::sys::path::append(CoverageFilename, Gcno); 8210b57cec5SDimitry Andric } 8220b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcda"); 8235f757f3fSDimitry Andric CmdArgs.push_back( 8245f757f3fSDimitry Andric Args.MakeArgString("-coverage-data-file=" + CoverageFilename)); 8250b57cec5SDimitry Andric } 8260b57cec5SDimitry Andric } 8270b57cec5SDimitry Andric } 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions. 8300b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) { 8310b57cec5SDimitry Andric if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A)) 8320b57cec5SDimitry Andric return true; 8330b57cec5SDimitry Andric 8340eae32dcSDimitry Andric return llvm::any_of(A->inputs(), ContainsCompileAction); 8350b57cec5SDimitry Andric } 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler. 8380b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0. 8390b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 8400b57cec5SDimitry Andric bool RelaxDefault = true; 8410b57cec5SDimitry Andric 8420b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 8430b57cec5SDimitry Andric RelaxDefault = A->getOption().matches(options::OPT_O0); 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric if (RelaxDefault) { 8460b57cec5SDimitry Andric RelaxDefault = false; 8470b57cec5SDimitry Andric for (const auto &Act : C.getActions()) { 8480b57cec5SDimitry Andric if (ContainsCompileAction(Act)) { 8490b57cec5SDimitry Andric RelaxDefault = true; 8500b57cec5SDimitry Andric break; 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 8560b57cec5SDimitry Andric RelaxDefault); 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 85906c3fb27SDimitry Andric static void 86006c3fb27SDimitry Andric RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, 86106c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind DebugInfoKind, 8620b57cec5SDimitry Andric unsigned DwarfVersion, 8630b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning) { 86406c3fb27SDimitry Andric addDebugInfoKind(CmdArgs, DebugInfoKind); 8650b57cec5SDimitry Andric if (DwarfVersion > 0) 8660b57cec5SDimitry Andric CmdArgs.push_back( 8670b57cec5SDimitry Andric Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); 8680b57cec5SDimitry Andric switch (DebuggerTuning) { 8690b57cec5SDimitry Andric case llvm::DebuggerKind::GDB: 8700b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=gdb"); 8710b57cec5SDimitry Andric break; 8720b57cec5SDimitry Andric case llvm::DebuggerKind::LLDB: 8730b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=lldb"); 8740b57cec5SDimitry Andric break; 8750b57cec5SDimitry Andric case llvm::DebuggerKind::SCE: 8760b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=sce"); 8770b57cec5SDimitry Andric break; 878fe6060f1SDimitry Andric case llvm::DebuggerKind::DBX: 879fe6060f1SDimitry Andric CmdArgs.push_back("-debugger-tuning=dbx"); 880fe6060f1SDimitry Andric break; 8810b57cec5SDimitry Andric default: 8820b57cec5SDimitry Andric break; 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args, 8870b57cec5SDimitry Andric const Driver &D, const ToolChain &TC) { 8880b57cec5SDimitry Andric assert(A && "Expected non-nullptr argument."); 8890b57cec5SDimitry Andric if (TC.supportsDebugInfoOption(A)) 8900b57cec5SDimitry Andric return true; 8910b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target) 8920b57cec5SDimitry Andric << A->getAsString(Args) << TC.getTripleString(); 8930b57cec5SDimitry Andric return false; 8940b57cec5SDimitry Andric } 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args, 8970b57cec5SDimitry Andric ArgStringList &CmdArgs, 8980b57cec5SDimitry Andric const Driver &D, 8990b57cec5SDimitry Andric const ToolChain &TC) { 900e8d8bef9SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gz_EQ); 9010b57cec5SDimitry Andric if (!A) 9020b57cec5SDimitry Andric return; 9030b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 9040b57cec5SDimitry Andric StringRef Value = A->getValue(); 9050b57cec5SDimitry Andric if (Value == "none") { 9060b57cec5SDimitry Andric CmdArgs.push_back("--compress-debug-sections=none"); 90704eeddc0SDimitry Andric } else if (Value == "zlib") { 908753f127fSDimitry Andric if (llvm::compression::zlib::isAvailable()) { 9090b57cec5SDimitry Andric CmdArgs.push_back( 9100b57cec5SDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 9110b57cec5SDimitry Andric } else { 912bdd1243dSDimitry Andric D.Diag(diag::warn_debug_compression_unavailable) << "zlib"; 913bdd1243dSDimitry Andric } 914bdd1243dSDimitry Andric } else if (Value == "zstd") { 915bdd1243dSDimitry Andric if (llvm::compression::zstd::isAvailable()) { 916bdd1243dSDimitry Andric CmdArgs.push_back( 917bdd1243dSDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 918bdd1243dSDimitry Andric } else { 919bdd1243dSDimitry Andric D.Diag(diag::warn_debug_compression_unavailable) << "zstd"; 9200b57cec5SDimitry Andric } 9210b57cec5SDimitry Andric } else { 9220b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 923bdd1243dSDimitry Andric << A->getSpelling() << Value; 9240b57cec5SDimitry Andric } 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric } 9270b57cec5SDimitry Andric 928e8d8bef9SDimitry Andric static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, 929e8d8bef9SDimitry Andric const ArgList &Args, 93081ad6265SDimitry Andric ArgStringList &CmdArgs, 93181ad6265SDimitry Andric bool IsCC1As = false) { 932fe6060f1SDimitry Andric // If no version was requested by the user, use the default value from the 933fe6060f1SDimitry Andric // back end. This is consistent with the value returned from 934fe6060f1SDimitry Andric // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without 935fe6060f1SDimitry Andric // requiring the corresponding llvm to have the AMDGPU target enabled, 936fe6060f1SDimitry Andric // provided the user (e.g. front end tests) can use the default. 937fe6060f1SDimitry Andric if (haveAMDGPUCodeObjectVersionArgument(D, Args)) { 938fe6060f1SDimitry Andric unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args); 939e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 940e8d8bef9SDimitry Andric Args.MakeArgString(Twine("--amdhsa-code-object-version=") + 941e8d8bef9SDimitry Andric Twine(CodeObjVer))); 942e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); 94381ad6265SDimitry Andric // -cc1as does not accept -mcode-object-version option. 94481ad6265SDimitry Andric if (!IsCC1As) 94581ad6265SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 94681ad6265SDimitry Andric Args.MakeArgString(Twine("-mcode-object-version=") + 94781ad6265SDimitry Andric Twine(CodeObjVer))); 948e8d8bef9SDimitry Andric } 949fe6060f1SDimitry Andric } 9500b57cec5SDimitry Andric 9515f757f3fSDimitry Andric static bool hasClangPchSignature(const Driver &D, StringRef Path) { 9525f757f3fSDimitry Andric if (llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MemBuf = 9535f757f3fSDimitry Andric D.getVFS().getBufferForFile(Path)) 9545f757f3fSDimitry Andric return (*MemBuf)->getBuffer().starts_with("CPCH"); 9555f757f3fSDimitry Andric return false; 9565f757f3fSDimitry Andric } 9575f757f3fSDimitry Andric 9585f757f3fSDimitry Andric static bool gchProbe(const Driver &D, StringRef Path) { 9595f757f3fSDimitry Andric llvm::ErrorOr<llvm::vfs::Status> Status = D.getVFS().status(Path); 9605f757f3fSDimitry Andric if (!Status) 9615f757f3fSDimitry Andric return false; 9625f757f3fSDimitry Andric 9635f757f3fSDimitry Andric if (Status->isDirectory()) { 9645f757f3fSDimitry Andric std::error_code EC; 9655f757f3fSDimitry Andric for (llvm::vfs::directory_iterator DI = D.getVFS().dir_begin(Path, EC), DE; 9665f757f3fSDimitry Andric !EC && DI != DE; DI = DI.increment(EC)) { 9675f757f3fSDimitry Andric if (hasClangPchSignature(D, DI->path())) 9685f757f3fSDimitry Andric return true; 9695f757f3fSDimitry Andric } 9705f757f3fSDimitry Andric D.Diag(diag::warn_drv_pch_ignoring_gch_dir) << Path; 9715f757f3fSDimitry Andric return false; 9725f757f3fSDimitry Andric } 9735f757f3fSDimitry Andric 9745f757f3fSDimitry Andric if (hasClangPchSignature(D, Path)) 9755f757f3fSDimitry Andric return true; 9765f757f3fSDimitry Andric D.Diag(diag::warn_drv_pch_ignoring_gch_file) << Path; 9775f757f3fSDimitry Andric return false; 9785f757f3fSDimitry Andric } 9795f757f3fSDimitry Andric 9800b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, 9810b57cec5SDimitry Andric const Driver &D, const ArgList &Args, 9820b57cec5SDimitry Andric ArgStringList &CmdArgs, 9830b57cec5SDimitry Andric const InputInfo &Output, 9840b57cec5SDimitry Andric const InputInfoList &Inputs) const { 9850b57cec5SDimitry Andric const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU(); 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric CheckPreprocessingOptions(D, Args); 9880b57cec5SDimitry Andric 9890b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_C); 9900b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_CC); 9910b57cec5SDimitry Andric 9920b57cec5SDimitry Andric // Handle dependency file generation. 993a7dea167SDimitry Andric Arg *ArgM = Args.getLastArg(options::OPT_MM); 994a7dea167SDimitry Andric if (!ArgM) 995a7dea167SDimitry Andric ArgM = Args.getLastArg(options::OPT_M); 996a7dea167SDimitry Andric Arg *ArgMD = Args.getLastArg(options::OPT_MMD); 997a7dea167SDimitry Andric if (!ArgMD) 998a7dea167SDimitry Andric ArgMD = Args.getLastArg(options::OPT_MD); 999a7dea167SDimitry Andric 1000a7dea167SDimitry Andric // -M and -MM imply -w. 1001a7dea167SDimitry Andric if (ArgM) 1002a7dea167SDimitry Andric CmdArgs.push_back("-w"); 1003a7dea167SDimitry Andric else 1004a7dea167SDimitry Andric ArgM = ArgMD; 1005a7dea167SDimitry Andric 1006a7dea167SDimitry Andric if (ArgM) { 10070b57cec5SDimitry Andric // Determine the output location. 10080b57cec5SDimitry Andric const char *DepFile; 10090b57cec5SDimitry Andric if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 10100b57cec5SDimitry Andric DepFile = MF->getValue(); 10110b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 10120b57cec5SDimitry Andric } else if (Output.getType() == types::TY_Dependencies) { 10130b57cec5SDimitry Andric DepFile = Output.getFilename(); 1014a7dea167SDimitry Andric } else if (!ArgMD) { 10150b57cec5SDimitry Andric DepFile = "-"; 10160b57cec5SDimitry Andric } else { 10170b57cec5SDimitry Andric DepFile = getDependencyFileName(Args, Inputs); 10180b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric CmdArgs.push_back("-dependency-file"); 10210b57cec5SDimitry Andric CmdArgs.push_back(DepFile); 10220b57cec5SDimitry Andric 1023a7dea167SDimitry Andric bool HasTarget = false; 1024a7dea167SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) { 1025a7dea167SDimitry Andric HasTarget = true; 1026a7dea167SDimitry Andric A->claim(); 1027a7dea167SDimitry Andric if (A->getOption().matches(options::OPT_MT)) { 1028a7dea167SDimitry Andric A->render(Args, CmdArgs); 1029a7dea167SDimitry Andric } else { 1030a7dea167SDimitry Andric CmdArgs.push_back("-MT"); 1031a7dea167SDimitry Andric SmallString<128> Quoted; 1032753f127fSDimitry Andric quoteMakeTarget(A->getValue(), Quoted); 1033a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 1034a7dea167SDimitry Andric } 1035a7dea167SDimitry Andric } 1036a7dea167SDimitry Andric 10370b57cec5SDimitry Andric // Add a default target if one wasn't specified. 1038a7dea167SDimitry Andric if (!HasTarget) { 10390b57cec5SDimitry Andric const char *DepTarget; 10400b57cec5SDimitry Andric 10410b57cec5SDimitry Andric // If user provided -o, that is the dependency target, except 10420b57cec5SDimitry Andric // when we are only generating a dependency file. 10430b57cec5SDimitry Andric Arg *OutputOpt = Args.getLastArg(options::OPT_o); 10440b57cec5SDimitry Andric if (OutputOpt && Output.getType() != types::TY_Dependencies) { 10450b57cec5SDimitry Andric DepTarget = OutputOpt->getValue(); 10460b57cec5SDimitry Andric } else { 10470b57cec5SDimitry Andric // Otherwise derive from the base input. 10480b57cec5SDimitry Andric // 10490b57cec5SDimitry Andric // FIXME: This should use the computed output file location. 10500b57cec5SDimitry Andric SmallString<128> P(Inputs[0].getBaseInput()); 10510b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "o"); 10520b57cec5SDimitry Andric DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric CmdArgs.push_back("-MT"); 10560b57cec5SDimitry Andric SmallString<128> Quoted; 1057753f127fSDimitry Andric quoteMakeTarget(DepTarget, Quoted); 10580b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric 1061a7dea167SDimitry Andric if (ArgM->getOption().matches(options::OPT_M) || 1062a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MD)) 10630b57cec5SDimitry Andric CmdArgs.push_back("-sys-header-deps"); 10640b57cec5SDimitry Andric if ((isa<PrecompileJobAction>(JA) && 10650b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_module_file_deps)) || 10660b57cec5SDimitry Andric Args.hasArg(options::OPT_fmodule_file_deps)) 10670b57cec5SDimitry Andric CmdArgs.push_back("-module-file-deps"); 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric if (Args.hasArg(options::OPT_MG)) { 1071a7dea167SDimitry Andric if (!ArgM || ArgM->getOption().matches(options::OPT_MD) || 1072a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MMD)) 10730b57cec5SDimitry Andric D.Diag(diag::err_drv_mg_requires_m_or_mm); 10740b57cec5SDimitry Andric CmdArgs.push_back("-MG"); 10750b57cec5SDimitry Andric } 10760b57cec5SDimitry Andric 10770b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MP); 10780b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MV); 10790b57cec5SDimitry Andric 10805ffd83dbSDimitry Andric // Add offload include arguments specific for CUDA/HIP. This must happen 10815ffd83dbSDimitry Andric // before we -I or -include anything else, because we must pick up the 10825ffd83dbSDimitry Andric // CUDA/HIP headers from the particular CUDA/ROCm installation, rather than 10835ffd83dbSDimitry Andric // from e.g. /usr/local/include. 10840b57cec5SDimitry Andric if (JA.isOffloading(Action::OFK_Cuda)) 10850b57cec5SDimitry Andric getToolChain().AddCudaIncludeArgs(Args, CmdArgs); 10865ffd83dbSDimitry Andric if (JA.isOffloading(Action::OFK_HIP)) 10875ffd83dbSDimitry Andric getToolChain().AddHIPIncludeArgs(Args, CmdArgs); 10880b57cec5SDimitry Andric 108906c3fb27SDimitry Andric // If we are compiling for a GPU target we want to override the system headers 109006c3fb27SDimitry Andric // with ones created by the 'libc' project if present. 109106c3fb27SDimitry Andric if (!Args.hasArg(options::OPT_nostdinc) && 109206c3fb27SDimitry Andric !Args.hasArg(options::OPT_nogpuinc) && 10935f757f3fSDimitry Andric !Args.hasArg(options::OPT_nobuiltininc)) { 109406c3fb27SDimitry Andric // Without an offloading language we will include these headers directly. 109506c3fb27SDimitry Andric // Offloading languages will instead only use the declarations stored in 109606c3fb27SDimitry Andric // the resource directory at clang/lib/Headers/llvm_libc_wrappers. 10975f757f3fSDimitry Andric if ((getToolChain().getTriple().isNVPTX() || 10985f757f3fSDimitry Andric getToolChain().getTriple().isAMDGCN()) && 10995f757f3fSDimitry Andric C.getActiveOffloadKinds() == Action::OFK_None) { 110006c3fb27SDimitry Andric SmallString<128> P(llvm::sys::path::parent_path(D.InstalledDir)); 110106c3fb27SDimitry Andric llvm::sys::path::append(P, "include"); 110206c3fb27SDimitry Andric llvm::sys::path::append(P, "gpu-none-llvm"); 110306c3fb27SDimitry Andric CmdArgs.push_back("-c-isystem"); 110406c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 110506c3fb27SDimitry Andric } else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) { 110606c3fb27SDimitry Andric // TODO: CUDA / HIP include their own headers for some common functions 110706c3fb27SDimitry Andric // implemented here. We'll need to clean those up so they do not conflict. 110806c3fb27SDimitry Andric SmallString<128> P(D.ResourceDir); 110906c3fb27SDimitry Andric llvm::sys::path::append(P, "include"); 111006c3fb27SDimitry Andric llvm::sys::path::append(P, "llvm_libc_wrappers"); 111106c3fb27SDimitry Andric CmdArgs.push_back("-internal-isystem"); 111206c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 111306c3fb27SDimitry Andric } 111406c3fb27SDimitry Andric } 111506c3fb27SDimitry Andric 11160b57cec5SDimitry Andric // If we are offloading to a target via OpenMP we need to include the 11170b57cec5SDimitry Andric // openmp_wrappers folder which contains alternative system headers. 11180b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_OpenMP) && 111981ad6265SDimitry Andric !Args.hasArg(options::OPT_nostdinc) && 1120bdd1243dSDimitry Andric !Args.hasArg(options::OPT_nogpuinc) && 112169ade1e0SDimitry Andric (getToolChain().getTriple().isNVPTX() || 112269ade1e0SDimitry Andric getToolChain().getTriple().isAMDGCN())) { 11230b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nobuiltininc)) { 11240b57cec5SDimitry Andric // Add openmp_wrappers/* to our system include path. This lets us wrap 11250b57cec5SDimitry Andric // standard library headers. 11260b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 11270b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 11280b57cec5SDimitry Andric llvm::sys::path::append(P, "openmp_wrappers"); 11290b57cec5SDimitry Andric CmdArgs.push_back("-internal-isystem"); 11300b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric CmdArgs.push_back("-include"); 11345ffd83dbSDimitry Andric CmdArgs.push_back("__clang_openmp_device_functions.h"); 11350b57cec5SDimitry Andric } 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric // Add -i* options, and automatically translate to 11380b57cec5SDimitry Andric // -include-pch/-include-pth for transparent PCH support. It's 11390b57cec5SDimitry Andric // wonky, but we include looking for .gch so we can support seamless 11400b57cec5SDimitry Andric // replacement into a build system already set up to be generating 11410b57cec5SDimitry Andric // .gch files. 11420b57cec5SDimitry Andric 11430b57cec5SDimitry Andric if (getToolChain().getDriver().IsCLMode()) { 11440b57cec5SDimitry Andric const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); 11450b57cec5SDimitry Andric const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); 11460b57cec5SDimitry Andric if (YcArg && JA.getKind() >= Action::PrecompileJobClass && 11470b57cec5SDimitry Andric JA.getKind() <= Action::AssembleJobClass) { 11480b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj")); 1149f91b0c1cSDimitry Andric // -fpch-instantiate-templates is the default when creating 1150f91b0c1cSDimitry Andric // precomp using /Yc 1151f91b0c1cSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 1152f91b0c1cSDimitry Andric options::OPT_fno_pch_instantiate_templates, true)) 11535ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates")); 11540b57cec5SDimitry Andric } 11550b57cec5SDimitry Andric if (YcArg || YuArg) { 11560b57cec5SDimitry Andric StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue(); 11570b57cec5SDimitry Andric if (!isa<PrecompileJobAction>(JA)) { 11580b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 11590b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath( 11600b57cec5SDimitry Andric C, !ThroughHeader.empty() 11610b57cec5SDimitry Andric ? ThroughHeader 11620b57cec5SDimitry Andric : llvm::sys::path::filename(Inputs[0].getBaseInput())))); 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 11650b57cec5SDimitry Andric if (ThroughHeader.empty()) { 11660b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 11670b57cec5SDimitry Andric Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use"))); 11680b57cec5SDimitry Andric } else { 11690b57cec5SDimitry Andric CmdArgs.push_back( 11700b57cec5SDimitry Andric Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader)); 11710b57cec5SDimitry Andric } 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric } 11740b57cec5SDimitry Andric 11750b57cec5SDimitry Andric bool RenderedImplicitInclude = false; 11760b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) { 117781ad6265SDimitry Andric if (A->getOption().matches(options::OPT_include) && 117881ad6265SDimitry Andric D.getProbePrecompiled()) { 11790b57cec5SDimitry Andric // Handling of gcc-style gch precompiled headers. 11800b57cec5SDimitry Andric bool IsFirstImplicitInclude = !RenderedImplicitInclude; 11810b57cec5SDimitry Andric RenderedImplicitInclude = true; 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric bool FoundPCH = false; 11840b57cec5SDimitry Andric SmallString<128> P(A->getValue()); 11850b57cec5SDimitry Andric // We want the files to have a name like foo.h.pch. Add a dummy extension 11860b57cec5SDimitry Andric // so that replace_extension does the right thing. 11870b57cec5SDimitry Andric P += ".dummy"; 11880b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "pch"); 118981ad6265SDimitry Andric if (D.getVFS().exists(P)) 11900b57cec5SDimitry Andric FoundPCH = true; 11910b57cec5SDimitry Andric 11920b57cec5SDimitry Andric if (!FoundPCH) { 11935f757f3fSDimitry Andric // For GCC compat, probe for a file or directory ending in .gch instead. 11940b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "gch"); 11955f757f3fSDimitry Andric FoundPCH = gchProbe(D, P.str()); 11960b57cec5SDimitry Andric } 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric if (FoundPCH) { 11990b57cec5SDimitry Andric if (IsFirstImplicitInclude) { 12000b57cec5SDimitry Andric A->claim(); 12010b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 12020b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12030b57cec5SDimitry Andric continue; 12040b57cec5SDimitry Andric } else { 12050b57cec5SDimitry Andric // Ignore the PCH if not first on command line and emit warning. 12060b57cec5SDimitry Andric D.Diag(diag::warn_drv_pch_not_first_include) << P 12070b57cec5SDimitry Andric << A->getAsString(Args); 12080b57cec5SDimitry Andric } 12090b57cec5SDimitry Andric } 12100b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_isystem_after)) { 12110b57cec5SDimitry Andric // Handling of paths which must come late. These entries are handled by 12120b57cec5SDimitry Andric // the toolchain itself after the resource dir is inserted in the right 12130b57cec5SDimitry Andric // search order. 12140b57cec5SDimitry Andric // Do not claim the argument so that the use of the argument does not 12150b57cec5SDimitry Andric // silently go unnoticed on toolchains which do not honour the option. 12160b57cec5SDimitry Andric continue; 1217a7dea167SDimitry Andric } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) { 1218a7dea167SDimitry Andric // Translated to -internal-isystem by the driver, no need to pass to cc1. 1219a7dea167SDimitry Andric continue; 122006c3fb27SDimitry Andric } else if (A->getOption().matches(options::OPT_ibuiltininc)) { 122106c3fb27SDimitry Andric // This is used only by the driver. No need to pass to cc1. 122206c3fb27SDimitry Andric continue; 12230b57cec5SDimitry Andric } 12240b57cec5SDimitry Andric 12250b57cec5SDimitry Andric // Not translated, render as usual. 12260b57cec5SDimitry Andric A->claim(); 12270b57cec5SDimitry Andric A->render(Args, CmdArgs); 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 12305f757f3fSDimitry Andric Args.addAllArgs(CmdArgs, 12310b57cec5SDimitry Andric {options::OPT_D, options::OPT_U, options::OPT_I_Group, 12320b57cec5SDimitry Andric options::OPT_F, options::OPT_index_header_map}); 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric // Add -Wp, and -Xpreprocessor if using the preprocessor. 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric // FIXME: There is a very unfortunate problem here, some troubled 12370b57cec5SDimitry Andric // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 12380b57cec5SDimitry Andric // really support that we would have to parse and then translate 12390b57cec5SDimitry Andric // those options. :( 12400b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 12410b57cec5SDimitry Andric options::OPT_Xpreprocessor); 12420b57cec5SDimitry Andric 12430b57cec5SDimitry Andric // -I- is a deprecated GCC feature, reject it. 12440b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_I_)) 12450b57cec5SDimitry Andric D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 12460b57cec5SDimitry Andric 12470b57cec5SDimitry Andric // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 12480b57cec5SDimitry Andric // -isysroot to the CC1 invocation. 12490b57cec5SDimitry Andric StringRef sysroot = C.getSysRoot(); 12500b57cec5SDimitry Andric if (sysroot != "") { 12510b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_isysroot)) { 12520b57cec5SDimitry Andric CmdArgs.push_back("-isysroot"); 12530b57cec5SDimitry Andric CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 12540b57cec5SDimitry Andric } 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric // Parse additional include paths from environment variables. 12580b57cec5SDimitry Andric // FIXME: We should probably sink the logic for handling these from the 12590b57cec5SDimitry Andric // frontend into the driver. It will allow deleting 4 otherwise unused flags. 12600b57cec5SDimitry Andric // CPATH - included following the user specified includes (but prior to 12610b57cec5SDimitry Andric // builtin and standard includes). 12620b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 12630b57cec5SDimitry Andric // C_INCLUDE_PATH - system includes enabled when compiling C. 12640b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 12650b57cec5SDimitry Andric // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 12660b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 12670b57cec5SDimitry Andric // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 12680b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 12690b57cec5SDimitry Andric // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 12700b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric // While adding the include arguments, we also attempt to retrieve the 12730b57cec5SDimitry Andric // arguments of related offloading toolchains or arguments that are specific 12740b57cec5SDimitry Andric // of an offloading programming model. 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric // Add C++ include arguments, if needed. 1277a7dea167SDimitry Andric if (types::isCXX(Inputs[0].getType())) { 1278a7dea167SDimitry Andric bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem); 1279a7dea167SDimitry Andric forAllAssociatedToolChains( 1280a7dea167SDimitry Andric C, JA, getToolChain(), 1281a7dea167SDimitry Andric [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) { 1282a7dea167SDimitry Andric HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs) 1283a7dea167SDimitry Andric : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 12840b57cec5SDimitry Andric }); 1285a7dea167SDimitry Andric } 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric // Add system include arguments for all targets but IAMCU. 12880b57cec5SDimitry Andric if (!IsIAMCU) 12890b57cec5SDimitry Andric forAllAssociatedToolChains(C, JA, getToolChain(), 12900b57cec5SDimitry Andric [&Args, &CmdArgs](const ToolChain &TC) { 12910b57cec5SDimitry Andric TC.AddClangSystemIncludeArgs(Args, CmdArgs); 12920b57cec5SDimitry Andric }); 12930b57cec5SDimitry Andric else { 12940b57cec5SDimitry Andric // For IAMCU add special include arguments. 12950b57cec5SDimitry Andric getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs); 12960b57cec5SDimitry Andric } 1297480093f4SDimitry Andric 1298480093f4SDimitry Andric addMacroPrefixMapArg(D, Args, CmdArgs); 1299fe6060f1SDimitry Andric addCoveragePrefixMapArg(D, Args, CmdArgs); 130081ad6265SDimitry Andric 130181ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffile_reproducible, 130281ad6265SDimitry Andric options::OPT_fno_file_reproducible); 1303bdd1243dSDimitry Andric 1304bdd1243dSDimitry Andric if (const char *Epoch = std::getenv("SOURCE_DATE_EPOCH")) { 1305bdd1243dSDimitry Andric CmdArgs.push_back("-source-date-epoch"); 1306bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Epoch)); 1307bdd1243dSDimitry Andric } 13085f757f3fSDimitry Andric 13095f757f3fSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdefine_target_os_macros, 13105f757f3fSDimitry Andric options::OPT_fno_define_target_os_macros); 13110b57cec5SDimitry Andric } 13120b57cec5SDimitry Andric 13130b57cec5SDimitry Andric // FIXME: Move to target hook. 13140b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) { 13150b57cec5SDimitry Andric switch (Triple.getArch()) { 13160b57cec5SDimitry Andric default: 13170b57cec5SDimitry Andric return true; 13180b57cec5SDimitry Andric 13190b57cec5SDimitry Andric case llvm::Triple::aarch64: 1320480093f4SDimitry Andric case llvm::Triple::aarch64_32: 13210b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 13220b57cec5SDimitry Andric case llvm::Triple::arm: 13230b57cec5SDimitry Andric case llvm::Triple::armeb: 13240b57cec5SDimitry Andric case llvm::Triple::thumb: 13250b57cec5SDimitry Andric case llvm::Triple::thumbeb: 13260b57cec5SDimitry Andric if (Triple.isOSDarwin() || Triple.isOSWindows()) 13270b57cec5SDimitry Andric return true; 13280b57cec5SDimitry Andric return false; 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric case llvm::Triple::ppc: 13310b57cec5SDimitry Andric case llvm::Triple::ppc64: 13320b57cec5SDimitry Andric if (Triple.isOSDarwin()) 13330b57cec5SDimitry Andric return true; 13340b57cec5SDimitry Andric return false; 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric case llvm::Triple::hexagon: 1337e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 13380b57cec5SDimitry Andric case llvm::Triple::ppc64le: 13390b57cec5SDimitry Andric case llvm::Triple::riscv32: 13400b57cec5SDimitry Andric case llvm::Triple::riscv64: 13410b57cec5SDimitry Andric case llvm::Triple::systemz: 13420b57cec5SDimitry Andric case llvm::Triple::xcore: 13430b57cec5SDimitry Andric return false; 13440b57cec5SDimitry Andric } 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric 1347480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple, 1348480093f4SDimitry Andric const ArgList &Args) { 1349480093f4SDimitry Andric // Supported only on Darwin where we invoke the compiler multiple times 1350480093f4SDimitry Andric // followed by an invocation to lipo. 1351480093f4SDimitry Andric if (!Triple.isOSDarwin()) 1352480093f4SDimitry Andric return false; 1353480093f4SDimitry Andric // If more than one "-arch <arch>" is specified, we're targeting multiple 1354480093f4SDimitry Andric // architectures resulting in a fat binary. 1355480093f4SDimitry Andric return Args.getAllArgValues(options::OPT_arch).size() > 1; 1356480093f4SDimitry Andric } 1357480093f4SDimitry Andric 1358480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args, 1359480093f4SDimitry Andric const llvm::Triple &Triple) { 1360480093f4SDimitry Andric // When enabling remarks, we need to error if: 1361480093f4SDimitry Andric // * The remark file is specified but we're targeting multiple architectures, 1362480093f4SDimitry Andric // which means more than one remark file is being generated. 1363480093f4SDimitry Andric bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args); 1364480093f4SDimitry Andric bool hasExplicitOutputFile = 1365480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1366480093f4SDimitry Andric if (hasMultipleInvocations && hasExplicitOutputFile) { 1367480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_output_with_multiple_archs) 1368480093f4SDimitry Andric << "-foptimization-record-file"; 1369480093f4SDimitry Andric return false; 1370480093f4SDimitry Andric } 1371480093f4SDimitry Andric return true; 1372480093f4SDimitry Andric } 1373480093f4SDimitry Andric 1374480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, 1375480093f4SDimitry Andric const llvm::Triple &Triple, 1376480093f4SDimitry Andric const InputInfo &Input, 1377480093f4SDimitry Andric const InputInfo &Output, const JobAction &JA) { 1378480093f4SDimitry Andric StringRef Format = "yaml"; 1379480093f4SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) 1380480093f4SDimitry Andric Format = A->getValue(); 1381480093f4SDimitry Andric 1382480093f4SDimitry Andric CmdArgs.push_back("-opt-record-file"); 1383480093f4SDimitry Andric 1384480093f4SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1385480093f4SDimitry Andric if (A) { 1386480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1387480093f4SDimitry Andric } else { 1388480093f4SDimitry Andric bool hasMultipleArchs = 1389480093f4SDimitry Andric Triple.isOSDarwin() && // Only supported on Darwin platforms. 1390480093f4SDimitry Andric Args.getAllArgValues(options::OPT_arch).size() > 1; 1391480093f4SDimitry Andric 1392480093f4SDimitry Andric SmallString<128> F; 1393480093f4SDimitry Andric 1394480093f4SDimitry Andric if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) { 1395480093f4SDimitry Andric if (Arg *FinalOutput = Args.getLastArg(options::OPT_o)) 1396480093f4SDimitry Andric F = FinalOutput->getValue(); 1397480093f4SDimitry Andric } else { 1398480093f4SDimitry Andric if (Format != "yaml" && // For YAML, keep the original behavior. 1399480093f4SDimitry Andric Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles. 1400480093f4SDimitry Andric Output.isFilename()) 1401480093f4SDimitry Andric F = Output.getFilename(); 1402480093f4SDimitry Andric } 1403480093f4SDimitry Andric 1404480093f4SDimitry Andric if (F.empty()) { 1405480093f4SDimitry Andric // Use the input filename. 1406480093f4SDimitry Andric F = llvm::sys::path::stem(Input.getBaseInput()); 1407480093f4SDimitry Andric 1408480093f4SDimitry Andric // If we're compiling for an offload architecture (i.e. a CUDA device), 1409480093f4SDimitry Andric // we need to make the file name for the device compilation different 1410480093f4SDimitry Andric // from the host compilation. 1411480093f4SDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_None) && 1412480093f4SDimitry Andric !JA.isDeviceOffloading(Action::OFK_Host)) { 1413480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1414480093f4SDimitry Andric F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(), 1415480093f4SDimitry Andric Triple.normalize()); 1416480093f4SDimitry Andric F += "-"; 1417480093f4SDimitry Andric F += JA.getOffloadingArch(); 1418480093f4SDimitry Andric } 1419480093f4SDimitry Andric } 1420480093f4SDimitry Andric 1421480093f4SDimitry Andric // If we're having more than one "-arch", we should name the files 1422480093f4SDimitry Andric // differently so that every cc1 invocation writes to a different file. 1423480093f4SDimitry Andric // We're doing that by appending "-<arch>" with "<arch>" being the arch 1424480093f4SDimitry Andric // name from the triple. 1425480093f4SDimitry Andric if (hasMultipleArchs) { 1426480093f4SDimitry Andric // First, remember the extension. 1427480093f4SDimitry Andric SmallString<64> OldExtension = llvm::sys::path::extension(F); 1428480093f4SDimitry Andric // then, remove it. 1429480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1430480093f4SDimitry Andric // attach -<arch> to it. 1431480093f4SDimitry Andric F += "-"; 1432480093f4SDimitry Andric F += Triple.getArchName(); 1433480093f4SDimitry Andric // put back the extension. 1434480093f4SDimitry Andric llvm::sys::path::replace_extension(F, OldExtension); 1435480093f4SDimitry Andric } 1436480093f4SDimitry Andric 1437480093f4SDimitry Andric SmallString<32> Extension; 1438480093f4SDimitry Andric Extension += "opt."; 1439480093f4SDimitry Andric Extension += Format; 1440480093f4SDimitry Andric 1441480093f4SDimitry Andric llvm::sys::path::replace_extension(F, Extension); 1442480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(F)); 1443480093f4SDimitry Andric } 1444480093f4SDimitry Andric 1445480093f4SDimitry Andric if (const Arg *A = 1446480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) { 1447480093f4SDimitry Andric CmdArgs.push_back("-opt-record-passes"); 1448480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1449480093f4SDimitry Andric } 1450480093f4SDimitry Andric 1451480093f4SDimitry Andric if (!Format.empty()) { 1452480093f4SDimitry Andric CmdArgs.push_back("-opt-record-format"); 1453480093f4SDimitry Andric CmdArgs.push_back(Format.data()); 1454480093f4SDimitry Andric } 1455480093f4SDimitry Andric } 1456480093f4SDimitry Andric 1457fe6060f1SDimitry Andric void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) { 1458fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_faapcs_bitfield_width, 1459fe6060f1SDimitry Andric options::OPT_fno_aapcs_bitfield_width, true)) 1460fe6060f1SDimitry Andric CmdArgs.push_back("-fno-aapcs-bitfield-width"); 1461fe6060f1SDimitry Andric 1462fe6060f1SDimitry Andric if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad)) 1463fe6060f1SDimitry Andric CmdArgs.push_back("-faapcs-bitfield-load"); 1464fe6060f1SDimitry Andric } 1465fe6060f1SDimitry Andric 14660b57cec5SDimitry Andric namespace { 1467349cc55cSDimitry Andric void RenderARMABI(const Driver &D, const llvm::Triple &Triple, 1468349cc55cSDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 14690b57cec5SDimitry Andric // Select the ABI to use. 14700b57cec5SDimitry Andric // FIXME: Support -meabi. 14710b57cec5SDimitry Andric // FIXME: Parts of this are duplicated in the backend, unify this somehow. 14720b57cec5SDimitry Andric const char *ABIName = nullptr; 14730b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 14740b57cec5SDimitry Andric ABIName = A->getValue(); 14750b57cec5SDimitry Andric } else { 1476349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 14770b57cec5SDimitry Andric ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data(); 14780b57cec5SDimitry Andric } 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 14810b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 14820b57cec5SDimitry Andric } 148381ad6265SDimitry Andric 148481ad6265SDimitry Andric void AddUnalignedAccessWarning(ArgStringList &CmdArgs) { 148581ad6265SDimitry Andric auto StrictAlignIter = 1486bdd1243dSDimitry Andric llvm::find_if(llvm::reverse(CmdArgs), [](StringRef Arg) { 148781ad6265SDimitry Andric return Arg == "+strict-align" || Arg == "-strict-align"; 148881ad6265SDimitry Andric }); 148981ad6265SDimitry Andric if (StrictAlignIter != CmdArgs.rend() && 149081ad6265SDimitry Andric StringRef(*StrictAlignIter) == "+strict-align") 149181ad6265SDimitry Andric CmdArgs.push_back("-Wunaligned-access"); 149281ad6265SDimitry Andric } 14930b57cec5SDimitry Andric } 14940b57cec5SDimitry Andric 14951fd87a68SDimitry Andric static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, 14964824e7fdSDimitry Andric ArgStringList &CmdArgs, bool isAArch64) { 14974824e7fdSDimitry Andric const Arg *A = isAArch64 14984824e7fdSDimitry Andric ? Args.getLastArg(options::OPT_msign_return_address_EQ, 14994824e7fdSDimitry Andric options::OPT_mbranch_protection_EQ) 15004824e7fdSDimitry Andric : Args.getLastArg(options::OPT_mbranch_protection_EQ); 15014824e7fdSDimitry Andric if (!A) 15024824e7fdSDimitry Andric return; 15034824e7fdSDimitry Andric 15041fd87a68SDimitry Andric const Driver &D = TC.getDriver(); 15051fd87a68SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 15061fd87a68SDimitry Andric if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass()))) 1507d56accc7SDimitry Andric D.Diag(diag::warn_incompatible_branch_protection_option) 15081fd87a68SDimitry Andric << Triple.getArchName(); 15091fd87a68SDimitry Andric 15104824e7fdSDimitry Andric StringRef Scope, Key; 1511cb14a3feSDimitry Andric bool IndirectBranches, BranchProtectionPAuthLR; 15124824e7fdSDimitry Andric 15134824e7fdSDimitry Andric if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { 15144824e7fdSDimitry Andric Scope = A->getValue(); 151581ad6265SDimitry Andric if (Scope != "none" && Scope != "non-leaf" && Scope != "all") 151681ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1517bdd1243dSDimitry Andric << A->getSpelling() << Scope; 15184824e7fdSDimitry Andric Key = "a_key"; 15194824e7fdSDimitry Andric IndirectBranches = false; 1520cb14a3feSDimitry Andric BranchProtectionPAuthLR = false; 15214824e7fdSDimitry Andric } else { 15224824e7fdSDimitry Andric StringRef DiagMsg; 15234824e7fdSDimitry Andric llvm::ARM::ParsedBranchProtection PBP; 15244824e7fdSDimitry Andric if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg)) 152581ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1526bdd1243dSDimitry Andric << A->getSpelling() << DiagMsg; 15274824e7fdSDimitry Andric if (!isAArch64 && PBP.Key == "b_key") 15284824e7fdSDimitry Andric D.Diag(diag::warn_unsupported_branch_protection) 15294824e7fdSDimitry Andric << "b-key" << A->getAsString(Args); 15304824e7fdSDimitry Andric Scope = PBP.Scope; 15314824e7fdSDimitry Andric Key = PBP.Key; 1532cb14a3feSDimitry Andric BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR; 15334824e7fdSDimitry Andric IndirectBranches = PBP.BranchTargetEnforcement; 15344824e7fdSDimitry Andric } 15354824e7fdSDimitry Andric 15364824e7fdSDimitry Andric CmdArgs.push_back( 15374824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address=") + Scope)); 15384824e7fdSDimitry Andric if (!Scope.equals("none")) 15394824e7fdSDimitry Andric CmdArgs.push_back( 15404824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); 1541cb14a3feSDimitry Andric if (BranchProtectionPAuthLR) 1542cb14a3feSDimitry Andric CmdArgs.push_back( 1543cb14a3feSDimitry Andric Args.MakeArgString(Twine("-mbranch-protection-pauth-lr"))); 15444824e7fdSDimitry Andric if (IndirectBranches) 15454824e7fdSDimitry Andric CmdArgs.push_back("-mbranch-target-enforce"); 15464824e7fdSDimitry Andric } 15474824e7fdSDimitry Andric 15480b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, 15490b57cec5SDimitry Andric ArgStringList &CmdArgs, bool KernelOrKext) const { 1550349cc55cSDimitry Andric RenderARMABI(getToolChain().getDriver(), Triple, Args, CmdArgs); 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric // Determine floating point ABI from the options & target defaults. 15530b57cec5SDimitry Andric arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); 15540b57cec5SDimitry Andric if (ABI == arm::FloatABI::Soft) { 15550b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 15560b57cec5SDimitry Andric // FIXME: This changes CPP defines, we need -target-soft-float. 15570b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 15580b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 15590b57cec5SDimitry Andric CmdArgs.push_back("soft"); 15600b57cec5SDimitry Andric } else if (ABI == arm::FloatABI::SoftFP) { 15610b57cec5SDimitry Andric // Floating point operations are hard, but argument passing is soft. 15620b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 15630b57cec5SDimitry Andric CmdArgs.push_back("soft"); 15640b57cec5SDimitry Andric } else { 15650b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 15660b57cec5SDimitry Andric assert(ABI == arm::FloatABI::Hard && "Invalid float abi!"); 15670b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 15680b57cec5SDimitry Andric CmdArgs.push_back("hard"); 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 15720b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 15730b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 15740b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 15750b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 15760b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=false"); 15770b57cec5SDimitry Andric else 15780b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=true"); 15790b57cec5SDimitry Andric } 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 15820b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 15830b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 15840b57cec5SDimitry Andric 15850b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mcmse)) 15860b57cec5SDimitry Andric CmdArgs.push_back("-mcmse"); 1587fe6060f1SDimitry Andric 1588fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 15894824e7fdSDimitry Andric 15904824e7fdSDimitry Andric // Enable/disable return address signing and indirect branch targets. 15911fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); 159281ad6265SDimitry Andric 159381ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 15940b57cec5SDimitry Andric } 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, 15970b57cec5SDimitry Andric const ArgList &Args, bool KernelOrKext, 15980b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 15990b57cec5SDimitry Andric const ToolChain &TC = getToolChain(); 16000b57cec5SDimitry Andric 16010b57cec5SDimitry Andric // Add the target features 16025ffd83dbSDimitry Andric getTargetFeatures(TC.getDriver(), EffectiveTriple, Args, CmdArgs, false); 16030b57cec5SDimitry Andric 16040b57cec5SDimitry Andric // Add target specific flags. 16050b57cec5SDimitry Andric switch (TC.getArch()) { 16060b57cec5SDimitry Andric default: 16070b57cec5SDimitry Andric break; 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric case llvm::Triple::arm: 16100b57cec5SDimitry Andric case llvm::Triple::armeb: 16110b57cec5SDimitry Andric case llvm::Triple::thumb: 16120b57cec5SDimitry Andric case llvm::Triple::thumbeb: 16130b57cec5SDimitry Andric // Use the effective triple, which takes into account the deployment target. 16140b57cec5SDimitry Andric AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext); 16150b57cec5SDimitry Andric break; 16160b57cec5SDimitry Andric 16170b57cec5SDimitry Andric case llvm::Triple::aarch64: 1618480093f4SDimitry Andric case llvm::Triple::aarch64_32: 16190b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 16200b57cec5SDimitry Andric AddAArch64TargetArgs(Args, CmdArgs); 1621bdd1243dSDimitry Andric break; 1622bdd1243dSDimitry Andric 1623bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 1624bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 1625bdd1243dSDimitry Andric AddLoongArchTargetArgs(Args, CmdArgs); 16260b57cec5SDimitry Andric break; 16270b57cec5SDimitry Andric 16280b57cec5SDimitry Andric case llvm::Triple::mips: 16290b57cec5SDimitry Andric case llvm::Triple::mipsel: 16300b57cec5SDimitry Andric case llvm::Triple::mips64: 16310b57cec5SDimitry Andric case llvm::Triple::mips64el: 16320b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 16330b57cec5SDimitry Andric break; 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric case llvm::Triple::ppc: 1636e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 16370b57cec5SDimitry Andric case llvm::Triple::ppc64: 16380b57cec5SDimitry Andric case llvm::Triple::ppc64le: 16390b57cec5SDimitry Andric AddPPCTargetArgs(Args, CmdArgs); 16400b57cec5SDimitry Andric break; 16410b57cec5SDimitry Andric 16420b57cec5SDimitry Andric case llvm::Triple::riscv32: 16430b57cec5SDimitry Andric case llvm::Triple::riscv64: 16440b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 16450b57cec5SDimitry Andric break; 16460b57cec5SDimitry Andric 16470b57cec5SDimitry Andric case llvm::Triple::sparc: 16480b57cec5SDimitry Andric case llvm::Triple::sparcel: 16490b57cec5SDimitry Andric case llvm::Triple::sparcv9: 16500b57cec5SDimitry Andric AddSparcTargetArgs(Args, CmdArgs); 16510b57cec5SDimitry Andric break; 16520b57cec5SDimitry Andric 16530b57cec5SDimitry Andric case llvm::Triple::systemz: 16540b57cec5SDimitry Andric AddSystemZTargetArgs(Args, CmdArgs); 16550b57cec5SDimitry Andric break; 16560b57cec5SDimitry Andric 16570b57cec5SDimitry Andric case llvm::Triple::x86: 16580b57cec5SDimitry Andric case llvm::Triple::x86_64: 16590b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 16600b57cec5SDimitry Andric break; 16610b57cec5SDimitry Andric 16620b57cec5SDimitry Andric case llvm::Triple::lanai: 16630b57cec5SDimitry Andric AddLanaiTargetArgs(Args, CmdArgs); 16640b57cec5SDimitry Andric break; 16650b57cec5SDimitry Andric 16660b57cec5SDimitry Andric case llvm::Triple::hexagon: 16670b57cec5SDimitry Andric AddHexagonTargetArgs(Args, CmdArgs); 16680b57cec5SDimitry Andric break; 16690b57cec5SDimitry Andric 16700b57cec5SDimitry Andric case llvm::Triple::wasm32: 16710b57cec5SDimitry Andric case llvm::Triple::wasm64: 16720b57cec5SDimitry Andric AddWebAssemblyTargetArgs(Args, CmdArgs); 16730b57cec5SDimitry Andric break; 16745ffd83dbSDimitry Andric 16755ffd83dbSDimitry Andric case llvm::Triple::ve: 16765ffd83dbSDimitry Andric AddVETargetArgs(Args, CmdArgs); 16775ffd83dbSDimitry Andric break; 16780b57cec5SDimitry Andric } 16790b57cec5SDimitry Andric } 16800b57cec5SDimitry Andric 16810b57cec5SDimitry Andric namespace { 16820b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args, 16830b57cec5SDimitry Andric ArgStringList &CmdArgs) { 16840b57cec5SDimitry Andric const char *ABIName = nullptr; 16850b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) 16860b57cec5SDimitry Andric ABIName = A->getValue(); 16870b57cec5SDimitry Andric else if (Triple.isOSDarwin()) 16880b57cec5SDimitry Andric ABIName = "darwinpcs"; 16890b57cec5SDimitry Andric else 16900b57cec5SDimitry Andric ABIName = "aapcs"; 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 16930b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 16940b57cec5SDimitry Andric } 16950b57cec5SDimitry Andric } 16960b57cec5SDimitry Andric 16970b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args, 16980b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 16990b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 17000b57cec5SDimitry Andric 17010b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 17020b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 17030b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 17040b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 17070b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 17080b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 17090b57cec5SDimitry Andric 17100b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 17110b57cec5SDimitry Andric 17120b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 17130b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 17140b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 17150b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 17160b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 17170b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=false"); 17180b57cec5SDimitry Andric else 17190b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=true"); 17200b57cec5SDimitry Andric } 17210b57cec5SDimitry Andric 17220b57cec5SDimitry Andric // Enable/disable return address signing and indirect branch targets. 17231fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, true /*isAArch64*/); 1724e8d8bef9SDimitry Andric 1725e8d8bef9SDimitry Andric // Handle -msve_vector_bits=<bits> 1726e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) { 1727e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 1728e8d8bef9SDimitry Andric const Driver &D = getToolChain().getDriver(); 1729e8d8bef9SDimitry Andric if (Val.equals("128") || Val.equals("256") || Val.equals("512") || 1730349cc55cSDimitry Andric Val.equals("1024") || Val.equals("2048") || Val.equals("128+") || 1731349cc55cSDimitry Andric Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") || 1732349cc55cSDimitry Andric Val.equals("2048+")) { 1733349cc55cSDimitry Andric unsigned Bits = 0; 17345f757f3fSDimitry Andric if (Val.ends_with("+")) 1735349cc55cSDimitry Andric Val = Val.substr(0, Val.size() - 1); 1736349cc55cSDimitry Andric else { 1737349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1738349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1739e8d8bef9SDimitry Andric CmdArgs.push_back( 1740349cc55cSDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128))); 1741349cc55cSDimitry Andric } 1742349cc55cSDimitry Andric 1743349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1744349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1745349cc55cSDimitry Andric CmdArgs.push_back( 1746349cc55cSDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128))); 1747e8d8bef9SDimitry Andric // Silently drop requests for vector-length agnostic code as it's implied. 1748349cc55cSDimitry Andric } else if (!Val.equals("scalable")) 1749e8d8bef9SDimitry Andric // Handle the unsupported values passed to msve-vector-bits. 1750e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1751bdd1243dSDimitry Andric << A->getSpelling() << Val; 1752e8d8bef9SDimitry Andric } 1753fe6060f1SDimitry Andric 1754fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 1755349cc55cSDimitry Andric 1756349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 1757349cc55cSDimitry Andric CmdArgs.push_back("-tune-cpu"); 1758fcaf7f86SDimitry Andric if (strcmp(A->getValue(), "native") == 0) 1759fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 1760fcaf7f86SDimitry Andric else 1761fcaf7f86SDimitry Andric CmdArgs.push_back(A->getValue()); 1762349cc55cSDimitry Andric } 176381ad6265SDimitry Andric 176481ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 17650b57cec5SDimitry Andric } 17660b57cec5SDimitry Andric 1767bdd1243dSDimitry Andric void Clang::AddLoongArchTargetArgs(const ArgList &Args, 1768bdd1243dSDimitry Andric ArgStringList &CmdArgs) const { 17698a4dda33SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 17708a4dda33SDimitry Andric 1771bdd1243dSDimitry Andric CmdArgs.push_back("-target-abi"); 17728a4dda33SDimitry Andric CmdArgs.push_back( 17738a4dda33SDimitry Andric loongarch::getLoongArchABI(getToolChain().getDriver(), Args, Triple) 1774bdd1243dSDimitry Andric .data()); 17758a4dda33SDimitry Andric 17768a4dda33SDimitry Andric // Handle -mtune. 17778a4dda33SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 17788a4dda33SDimitry Andric std::string TuneCPU = A->getValue(); 17798a4dda33SDimitry Andric TuneCPU = loongarch::postProcessTargetCPUString(TuneCPU, Triple); 17808a4dda33SDimitry Andric CmdArgs.push_back("-tune-cpu"); 17818a4dda33SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 17828a4dda33SDimitry Andric } 1783bdd1243dSDimitry Andric } 1784bdd1243dSDimitry Andric 17850b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args, 17860b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 17870b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 17880b57cec5SDimitry Andric StringRef CPUName; 17890b57cec5SDimitry Andric StringRef ABIName; 17900b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 17910b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 17920b57cec5SDimitry Andric 17930b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 17940b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple); 17970b57cec5SDimitry Andric if (ABI == mips::FloatABI::Soft) { 17980b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 17990b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 18000b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 18010b57cec5SDimitry Andric CmdArgs.push_back("soft"); 18020b57cec5SDimitry Andric } else { 18030b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 18040b57cec5SDimitry Andric assert(ABI == mips::FloatABI::Hard && "Invalid float abi!"); 18050b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 18060b57cec5SDimitry Andric CmdArgs.push_back("hard"); 18070b57cec5SDimitry Andric } 18080b57cec5SDimitry Andric 18090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 18100b57cec5SDimitry Andric options::OPT_mno_ldc1_sdc1)) { 18110b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 18120b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18130b57cec5SDimitry Andric CmdArgs.push_back("-mno-ldc1-sdc1"); 18140b57cec5SDimitry Andric } 18150b57cec5SDimitry Andric } 18160b57cec5SDimitry Andric 18170b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 18180b57cec5SDimitry Andric options::OPT_mno_check_zero_division)) { 18190b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 18200b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18210b57cec5SDimitry Andric CmdArgs.push_back("-mno-check-zero-division"); 18220b57cec5SDimitry Andric } 18230b57cec5SDimitry Andric } 18240b57cec5SDimitry Andric 182504eeddc0SDimitry Andric if (Args.getLastArg(options::OPT_mfix4300)) { 182604eeddc0SDimitry Andric CmdArgs.push_back("-mllvm"); 182704eeddc0SDimitry Andric CmdArgs.push_back("-mfix4300"); 182804eeddc0SDimitry Andric } 182904eeddc0SDimitry Andric 18300b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_G)) { 18310b57cec5SDimitry Andric StringRef v = A->getValue(); 18320b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18330b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 18340b57cec5SDimitry Andric A->claim(); 18350b57cec5SDimitry Andric } 18360b57cec5SDimitry Andric 18370b57cec5SDimitry Andric Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt); 18380b57cec5SDimitry Andric Arg *ABICalls = 18390b57cec5SDimitry Andric Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls); 18400b57cec5SDimitry Andric 18410b57cec5SDimitry Andric // -mabicalls is the default for many MIPS environments, even with -fno-pic. 18420b57cec5SDimitry Andric // -mgpopt is the default for static, -fno-pic environments but these two 18430b57cec5SDimitry Andric // options conflict. We want to be certain that -mno-abicalls -mgpopt is 18440b57cec5SDimitry Andric // the only case where -mllvm -mgpopt is passed. 18450b57cec5SDimitry Andric // NOTE: We need a warning here or in the backend to warn when -mgpopt is 18460b57cec5SDimitry Andric // passed explicitly when compiling something with -mabicalls 18470b57cec5SDimitry Andric // (implictly) in affect. Currently the warning is in the backend. 18480b57cec5SDimitry Andric // 18490b57cec5SDimitry Andric // When the ABI in use is N64, we also need to determine the PIC mode that 18500b57cec5SDimitry Andric // is in use, as -fno-pic for N64 implies -mno-abicalls. 18510b57cec5SDimitry Andric bool NoABICalls = 18520b57cec5SDimitry Andric ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls); 18530b57cec5SDimitry Andric 18540b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 18550b57cec5SDimitry Andric unsigned PICLevel; 18560b57cec5SDimitry Andric bool IsPIE; 18570b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 18580b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 18590b57cec5SDimitry Andric 18600b57cec5SDimitry Andric NoABICalls = NoABICalls || 18610b57cec5SDimitry Andric (RelocationModel == llvm::Reloc::Static && ABIName == "n64"); 18620b57cec5SDimitry Andric 18630b57cec5SDimitry Andric bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt); 18640b57cec5SDimitry Andric // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt. 18650b57cec5SDimitry Andric if (NoABICalls && (!GPOpt || WantGPOpt)) { 18660b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18670b57cec5SDimitry Andric CmdArgs.push_back("-mgpopt"); 18680b57cec5SDimitry Andric 18690b57cec5SDimitry Andric Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata, 18700b57cec5SDimitry Andric options::OPT_mno_local_sdata); 18710b57cec5SDimitry Andric Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata, 18720b57cec5SDimitry Andric options::OPT_mno_extern_sdata); 18730b57cec5SDimitry Andric Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data, 18740b57cec5SDimitry Andric options::OPT_mno_embedded_data); 18750b57cec5SDimitry Andric if (LocalSData) { 18760b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18770b57cec5SDimitry Andric if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) { 18780b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=1"); 18790b57cec5SDimitry Andric } else { 18800b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=0"); 18810b57cec5SDimitry Andric } 18820b57cec5SDimitry Andric LocalSData->claim(); 18830b57cec5SDimitry Andric } 18840b57cec5SDimitry Andric 18850b57cec5SDimitry Andric if (ExternSData) { 18860b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18870b57cec5SDimitry Andric if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) { 18880b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=1"); 18890b57cec5SDimitry Andric } else { 18900b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=0"); 18910b57cec5SDimitry Andric } 18920b57cec5SDimitry Andric ExternSData->claim(); 18930b57cec5SDimitry Andric } 18940b57cec5SDimitry Andric 18950b57cec5SDimitry Andric if (EmbeddedData) { 18960b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18970b57cec5SDimitry Andric if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) { 18980b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=1"); 18990b57cec5SDimitry Andric } else { 19000b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=0"); 19010b57cec5SDimitry Andric } 19020b57cec5SDimitry Andric EmbeddedData->claim(); 19030b57cec5SDimitry Andric } 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt) 19060b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1); 19070b57cec5SDimitry Andric 19080b57cec5SDimitry Andric if (GPOpt) 19090b57cec5SDimitry Andric GPOpt->claim(); 19100b57cec5SDimitry Andric 19110b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) { 19120b57cec5SDimitry Andric StringRef Val = StringRef(A->getValue()); 19130b57cec5SDimitry Andric if (mips::hasCompactBranches(CPUName)) { 19140b57cec5SDimitry Andric if (Val == "never" || Val == "always" || Val == "optimal") { 19150b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19160b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val)); 19170b57cec5SDimitry Andric } else 19180b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1919bdd1243dSDimitry Andric << A->getSpelling() << Val; 19200b57cec5SDimitry Andric } else 19210b57cec5SDimitry Andric D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName; 19220b57cec5SDimitry Andric } 19230b57cec5SDimitry Andric 19240b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls, 19250b57cec5SDimitry Andric options::OPT_mno_relax_pic_calls)) { 19260b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) { 19270b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19280b57cec5SDimitry Andric CmdArgs.push_back("-mips-jalr-reloc=0"); 19290b57cec5SDimitry Andric } 19300b57cec5SDimitry Andric } 19310b57cec5SDimitry Andric } 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args, 19340b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 193506c3fb27SDimitry Andric const Driver &D = getToolChain().getDriver(); 19361ac55f4cSDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 193706c3fb27SDimitry Andric if (Args.getLastArg(options::OPT_mtune_EQ)) { 1938bdd1243dSDimitry Andric CmdArgs.push_back("-tune-cpu"); 19391ac55f4cSDimitry Andric std::string CPU = ppc::getPPCTuneCPU(Args, T); 19401ac55f4cSDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 1941bdd1243dSDimitry Andric } 1942bdd1243dSDimitry Andric 19430b57cec5SDimitry Andric // Select the ABI to use. 19440b57cec5SDimitry Andric const char *ABIName = nullptr; 1945480093f4SDimitry Andric if (T.isOSBinFormatELF()) { 19460b57cec5SDimitry Andric switch (getToolChain().getArch()) { 19470b57cec5SDimitry Andric case llvm::Triple::ppc64: { 19481ac55f4cSDimitry Andric if (T.isPPC64ELFv2ABI()) 1949480093f4SDimitry Andric ABIName = "elfv2"; 1950480093f4SDimitry Andric else 19510b57cec5SDimitry Andric ABIName = "elfv1"; 19520b57cec5SDimitry Andric break; 19530b57cec5SDimitry Andric } 19540b57cec5SDimitry Andric case llvm::Triple::ppc64le: 19550b57cec5SDimitry Andric ABIName = "elfv2"; 19560b57cec5SDimitry Andric break; 19570b57cec5SDimitry Andric default: 19580b57cec5SDimitry Andric break; 19590b57cec5SDimitry Andric } 1960480093f4SDimitry Andric } 19610b57cec5SDimitry Andric 196204eeddc0SDimitry Andric bool IEEELongDouble = getToolChain().defaultToIEEELongDouble(); 196306c3fb27SDimitry Andric bool VecExtabi = false; 19640b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) { 19650b57cec5SDimitry Andric StringRef V = A->getValue(); 196606c3fb27SDimitry Andric if (V == "ieeelongdouble") { 19670b57cec5SDimitry Andric IEEELongDouble = true; 196806c3fb27SDimitry Andric A->claim(); 196906c3fb27SDimitry Andric } else if (V == "ibmlongdouble") { 19700b57cec5SDimitry Andric IEEELongDouble = false; 197106c3fb27SDimitry Andric A->claim(); 197206c3fb27SDimitry Andric } else if (V == "vec-default") { 197306c3fb27SDimitry Andric VecExtabi = false; 197406c3fb27SDimitry Andric A->claim(); 197506c3fb27SDimitry Andric } else if (V == "vec-extabi") { 197606c3fb27SDimitry Andric VecExtabi = true; 197706c3fb27SDimitry Andric A->claim(); 19788a4dda33SDimitry Andric } else if (V == "elfv1") { 19798a4dda33SDimitry Andric ABIName = "elfv1"; 19808a4dda33SDimitry Andric A->claim(); 19818a4dda33SDimitry Andric } else if (V == "elfv2") { 19828a4dda33SDimitry Andric ABIName = "elfv2"; 19838a4dda33SDimitry Andric A->claim(); 198406c3fb27SDimitry Andric } else if (V != "altivec") 19850b57cec5SDimitry Andric // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore 19860b57cec5SDimitry Andric // the option if given as we don't have backend support for any targets 19870b57cec5SDimitry Andric // that don't use the altivec abi. 19880b57cec5SDimitry Andric ABIName = A->getValue(); 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric if (IEEELongDouble) 19910b57cec5SDimitry Andric CmdArgs.push_back("-mabi=ieeelongdouble"); 199206c3fb27SDimitry Andric if (VecExtabi) { 199306c3fb27SDimitry Andric if (!T.isOSAIX()) 199406c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 199506c3fb27SDimitry Andric << "-mabi=vec-extabi" << T.str(); 199606c3fb27SDimitry Andric CmdArgs.push_back("-mabi=vec-extabi"); 199706c3fb27SDimitry Andric } 19980b57cec5SDimitry Andric 199906c3fb27SDimitry Andric ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args); 20000b57cec5SDimitry Andric if (FloatABI == ppc::FloatABI::Soft) { 20010b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 20020b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 20030b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20040b57cec5SDimitry Andric CmdArgs.push_back("soft"); 20050b57cec5SDimitry Andric } else { 20060b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 20070b57cec5SDimitry Andric assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!"); 20080b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20090b57cec5SDimitry Andric CmdArgs.push_back("hard"); 20100b57cec5SDimitry Andric } 20110b57cec5SDimitry Andric 20120b57cec5SDimitry Andric if (ABIName) { 20130b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 20140b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 20150b57cec5SDimitry Andric } 20160b57cec5SDimitry Andric } 20170b57cec5SDimitry Andric 20185ffd83dbSDimitry Andric static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args, 20195ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 20205ffd83dbSDimitry Andric const Driver &D = TC.getDriver(); 20215ffd83dbSDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 20225ffd83dbSDimitry Andric // Default small data limitation is eight. 20235ffd83dbSDimitry Andric const char *SmallDataLimit = "8"; 20245ffd83dbSDimitry Andric // Get small data limitation. 20255ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_shared, options::OPT_fpic, 20265ffd83dbSDimitry Andric options::OPT_fPIC)) { 20275ffd83dbSDimitry Andric // Not support linker relaxation for PIC. 20285ffd83dbSDimitry Andric SmallDataLimit = "0"; 20295ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 20305ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 20315ffd83dbSDimitry Andric } 20325ffd83dbSDimitry Andric } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ) 2033fe6060f1SDimitry Andric .equals_insensitive("large") && 20345ffd83dbSDimitry Andric (Triple.getArch() == llvm::Triple::riscv64)) { 20355ffd83dbSDimitry Andric // Not support linker relaxation for RV64 with large code model. 20365ffd83dbSDimitry Andric SmallDataLimit = "0"; 20375ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 20385ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 20395ffd83dbSDimitry Andric } 204006c3fb27SDimitry Andric } else if (Triple.isAndroid()) { 204106c3fb27SDimitry Andric // GP relaxation is not supported on Android. 204206c3fb27SDimitry Andric SmallDataLimit = "0"; 204306c3fb27SDimitry Andric if (Args.hasArg(options::OPT_G)) { 204406c3fb27SDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 204506c3fb27SDimitry Andric } 20465ffd83dbSDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_G)) { 20475ffd83dbSDimitry Andric SmallDataLimit = A->getValue(); 20485ffd83dbSDimitry Andric } 20495ffd83dbSDimitry Andric // Forward the -msmall-data-limit= option. 20505ffd83dbSDimitry Andric CmdArgs.push_back("-msmall-data-limit"); 20515ffd83dbSDimitry Andric CmdArgs.push_back(SmallDataLimit); 20525ffd83dbSDimitry Andric } 20535ffd83dbSDimitry Andric 20540b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args, 20550b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 20560b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 2057a7dea167SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 20580b57cec5SDimitry Andric 20590b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 2060a7dea167SDimitry Andric CmdArgs.push_back(ABIName.data()); 20615ffd83dbSDimitry Andric 20625ffd83dbSDimitry Andric SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs); 2063e8d8bef9SDimitry Andric 2064bdd1243dSDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 2065bdd1243dSDimitry Andric options::OPT_mno_implicit_float, true)) 2066bdd1243dSDimitry Andric CmdArgs.push_back("-no-implicit-float"); 2067bdd1243dSDimitry Andric 2068fcaf7f86SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 2069e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2070bdd1243dSDimitry Andric if (strcmp(A->getValue(), "native") == 0) 2071bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 2072bdd1243dSDimitry Andric else 2073bdd1243dSDimitry Andric CmdArgs.push_back(A->getValue()); 2074e8d8bef9SDimitry Andric } 207506c3fb27SDimitry Andric 207606c3fb27SDimitry Andric // Handle -mrvv-vector-bits=<bits> 207706c3fb27SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) { 207806c3fb27SDimitry Andric StringRef Val = A->getValue(); 207906c3fb27SDimitry Andric const Driver &D = getToolChain().getDriver(); 208006c3fb27SDimitry Andric 208106c3fb27SDimitry Andric // Get minimum VLen from march. 208206c3fb27SDimitry Andric unsigned MinVLen = 0; 208306c3fb27SDimitry Andric StringRef Arch = riscv::getRISCVArch(Args, Triple); 208406c3fb27SDimitry Andric auto ISAInfo = llvm::RISCVISAInfo::parseArchString( 208506c3fb27SDimitry Andric Arch, /*EnableExperimentalExtensions*/ true); 208606c3fb27SDimitry Andric // Ignore parsing error. 2087647cbc5dSDimitry Andric if (!errorToBool(ISAInfo.takeError())) 208806c3fb27SDimitry Andric MinVLen = (*ISAInfo)->getMinVLen(); 208906c3fb27SDimitry Andric 209006c3fb27SDimitry Andric // If the value is "zvl", use MinVLen from march. Otherwise, try to parse 209106c3fb27SDimitry Andric // as integer as long as we have a MinVLen. 209206c3fb27SDimitry Andric unsigned Bits = 0; 209306c3fb27SDimitry Andric if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) { 209406c3fb27SDimitry Andric Bits = MinVLen; 209506c3fb27SDimitry Andric } else if (!Val.getAsInteger(10, Bits)) { 209606c3fb27SDimitry Andric // Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that 209706c3fb27SDimitry Andric // at least MinVLen. 209806c3fb27SDimitry Andric if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock || 209906c3fb27SDimitry Andric Bits > 65536 || !llvm::isPowerOf2_32(Bits)) 210006c3fb27SDimitry Andric Bits = 0; 210106c3fb27SDimitry Andric } 210206c3fb27SDimitry Andric 210306c3fb27SDimitry Andric // If we got a valid value try to use it. 210406c3fb27SDimitry Andric if (Bits != 0) { 210506c3fb27SDimitry Andric unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock; 210606c3fb27SDimitry Andric CmdArgs.push_back( 210706c3fb27SDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin))); 210806c3fb27SDimitry Andric CmdArgs.push_back( 210906c3fb27SDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin))); 211006c3fb27SDimitry Andric } else if (!Val.equals("scalable")) { 211106c3fb27SDimitry Andric // Handle the unsupported values passed to mrvv-vector-bits. 211206c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 211306c3fb27SDimitry Andric << A->getSpelling() << Val; 211406c3fb27SDimitry Andric } 211506c3fb27SDimitry Andric } 21160b57cec5SDimitry Andric } 21170b57cec5SDimitry Andric 21180b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args, 21190b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21200b57cec5SDimitry Andric sparc::FloatABI FloatABI = 21210b57cec5SDimitry Andric sparc::getSparcFloatABI(getToolChain().getDriver(), Args); 21220b57cec5SDimitry Andric 21230b57cec5SDimitry Andric if (FloatABI == sparc::FloatABI::Soft) { 21240b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 21250b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 21260b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21270b57cec5SDimitry Andric CmdArgs.push_back("soft"); 21280b57cec5SDimitry Andric } else { 21290b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 21300b57cec5SDimitry Andric assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!"); 21310b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21320b57cec5SDimitry Andric CmdArgs.push_back("hard"); 21330b57cec5SDimitry Andric } 213461cfbce3SDimitry Andric 213561cfbce3SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 213661cfbce3SDimitry Andric StringRef Name = A->getValue(); 213761cfbce3SDimitry Andric std::string TuneCPU; 213861cfbce3SDimitry Andric if (Name == "native") 213961cfbce3SDimitry Andric TuneCPU = std::string(llvm::sys::getHostCPUName()); 214061cfbce3SDimitry Andric else 214161cfbce3SDimitry Andric TuneCPU = std::string(Name); 214261cfbce3SDimitry Andric 214361cfbce3SDimitry Andric CmdArgs.push_back("-tune-cpu"); 214461cfbce3SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 214561cfbce3SDimitry Andric } 21460b57cec5SDimitry Andric } 21470b57cec5SDimitry Andric 21480b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args, 21490b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2150fcaf7f86SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 2151753f127fSDimitry Andric CmdArgs.push_back("-tune-cpu"); 2152fcaf7f86SDimitry Andric if (strcmp(A->getValue(), "native") == 0) 2153fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 2154fcaf7f86SDimitry Andric else 2155fcaf7f86SDimitry Andric CmdArgs.push_back(A->getValue()); 2156753f127fSDimitry Andric } 2157753f127fSDimitry Andric 2158753f127fSDimitry Andric bool HasBackchain = 2159753f127fSDimitry Andric Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false); 2160480093f4SDimitry Andric bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack, 2161480093f4SDimitry Andric options::OPT_mno_packed_stack, false); 21625ffd83dbSDimitry Andric systemz::FloatABI FloatABI = 21635ffd83dbSDimitry Andric systemz::getSystemZFloatABI(getToolChain().getDriver(), Args); 21645ffd83dbSDimitry Andric bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft); 21655ffd83dbSDimitry Andric if (HasBackchain && HasPackedStack && !HasSoftFloat) { 2166480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 2167480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt) 21685ffd83dbSDimitry Andric << "-mpacked-stack -mbackchain -mhard-float"; 2169480093f4SDimitry Andric } 2170480093f4SDimitry Andric if (HasBackchain) 21710b57cec5SDimitry Andric CmdArgs.push_back("-mbackchain"); 2172480093f4SDimitry Andric if (HasPackedStack) 2173480093f4SDimitry Andric CmdArgs.push_back("-mpacked-stack"); 21745ffd83dbSDimitry Andric if (HasSoftFloat) { 21755ffd83dbSDimitry Andric // Floating point operations and argument passing are soft. 21765ffd83dbSDimitry Andric CmdArgs.push_back("-msoft-float"); 21775ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21785ffd83dbSDimitry Andric CmdArgs.push_back("soft"); 2179480093f4SDimitry Andric } 21800b57cec5SDimitry Andric } 21810b57cec5SDimitry Andric 21820b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args, 21830b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2184480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 21855ffd83dbSDimitry Andric addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false); 2186480093f4SDimitry Andric 21870b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 21880b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 21890b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 21900b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 21910b57cec5SDimitry Andric 21920b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs, 21930b57cec5SDimitry Andric options::OPT_mno_tls_direct_seg_refs, true)) 21940b57cec5SDimitry Andric CmdArgs.push_back("-mno-tls-direct-seg-refs"); 21950b57cec5SDimitry Andric 21960b57cec5SDimitry Andric // Default to avoid implicit floating-point for kernel/kext code, but allow 21970b57cec5SDimitry Andric // that to be overridden with -mno-soft-float. 21980b57cec5SDimitry Andric bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 21990b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)); 22000b57cec5SDimitry Andric if (Arg *A = Args.getLastArg( 22010b57cec5SDimitry Andric options::OPT_msoft_float, options::OPT_mno_soft_float, 22020b57cec5SDimitry Andric options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) { 22030b57cec5SDimitry Andric const Option &O = A->getOption(); 22040b57cec5SDimitry Andric NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 22050b57cec5SDimitry Andric O.matches(options::OPT_msoft_float)); 22060b57cec5SDimitry Andric } 22070b57cec5SDimitry Andric if (NoImplicitFloat) 22080b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 22090b57cec5SDimitry Andric 22100b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 22110b57cec5SDimitry Andric StringRef Value = A->getValue(); 22120b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 22130b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22140b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 2215349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-inline-asm=" + Value)); 22160b57cec5SDimitry Andric } else { 2217480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2218bdd1243dSDimitry Andric << A->getSpelling() << Value; 22190b57cec5SDimitry Andric } 2220480093f4SDimitry Andric } else if (D.IsCLMode()) { 22210b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22220b57cec5SDimitry Andric CmdArgs.push_back("-x86-asm-syntax=intel"); 22230b57cec5SDimitry Andric } 22240b57cec5SDimitry Andric 2225349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mskip_rax_setup, 2226349cc55cSDimitry Andric options::OPT_mno_skip_rax_setup)) 2227349cc55cSDimitry Andric if (A->getOption().matches(options::OPT_mskip_rax_setup)) 2228349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mskip-rax-setup")); 2229349cc55cSDimitry Andric 22300b57cec5SDimitry Andric // Set flags to support MCU ABI. 22310b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { 22320b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22330b57cec5SDimitry Andric CmdArgs.push_back("soft"); 22340b57cec5SDimitry Andric CmdArgs.push_back("-mstack-alignment=4"); 22350b57cec5SDimitry Andric } 2236e8d8bef9SDimitry Andric 2237e8d8bef9SDimitry Andric // Handle -mtune. 2238e8d8bef9SDimitry Andric 223981ad6265SDimitry Andric // Default to "generic" unless -march is present or targetting the PS4/PS5. 2240e8d8bef9SDimitry Andric std::string TuneCPU; 2241e8d8bef9SDimitry Andric if (!Args.hasArg(clang::driver::options::OPT_march_EQ) && 224281ad6265SDimitry Andric !getToolChain().getTriple().isPS()) 2243e8d8bef9SDimitry Andric TuneCPU = "generic"; 2244e8d8bef9SDimitry Andric 2245e8d8bef9SDimitry Andric // Override based on -mtune. 2246e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2247e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2248e8d8bef9SDimitry Andric 2249e8d8bef9SDimitry Andric if (Name == "native") { 2250e8d8bef9SDimitry Andric Name = llvm::sys::getHostCPUName(); 2251e8d8bef9SDimitry Andric if (!Name.empty()) 2252e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2253e8d8bef9SDimitry Andric } else 2254e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2255e8d8bef9SDimitry Andric } 2256e8d8bef9SDimitry Andric 2257e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2258e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2259e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2260e8d8bef9SDimitry Andric } 22610b57cec5SDimitry Andric } 22620b57cec5SDimitry Andric 22630b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args, 22640b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22650b57cec5SDimitry Andric CmdArgs.push_back("-mqdsp6-compat"); 22660b57cec5SDimitry Andric CmdArgs.push_back("-Wreturn-type"); 22670b57cec5SDimitry Andric 22680b57cec5SDimitry Andric if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { 22690b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 2270bdd1243dSDimitry Andric CmdArgs.push_back( 2271bdd1243dSDimitry Andric Args.MakeArgString("-hexagon-small-data-threshold=" + Twine(*G))); 22720b57cec5SDimitry Andric } 22730b57cec5SDimitry Andric 22740b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_short_enums)) 22750b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 22760b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 22770b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22780b57cec5SDimitry Andric CmdArgs.push_back("-enable-hexagon-ieee-rnd-near"); 22790b57cec5SDimitry Andric } 22800b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22810b57cec5SDimitry Andric CmdArgs.push_back("-machine-sink-split=0"); 22820b57cec5SDimitry Andric } 22830b57cec5SDimitry Andric 22840b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args, 22850b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22860b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 22870b57cec5SDimitry Andric StringRef CPUName = A->getValue(); 22880b57cec5SDimitry Andric 22890b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 22900b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPUName)); 22910b57cec5SDimitry Andric } 22920b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 22930b57cec5SDimitry Andric StringRef Value = A->getValue(); 22940b57cec5SDimitry Andric // Only support mregparm=4 to support old usage. Report error for all other 22950b57cec5SDimitry Andric // cases. 22960b57cec5SDimitry Andric int Mregparm; 22970b57cec5SDimitry Andric if (Value.getAsInteger(10, Mregparm)) { 22980b57cec5SDimitry Andric if (Mregparm != 4) { 22990b57cec5SDimitry Andric getToolChain().getDriver().Diag( 23000b57cec5SDimitry Andric diag::err_drv_unsupported_option_argument) 2301bdd1243dSDimitry Andric << A->getSpelling() << Value; 23020b57cec5SDimitry Andric } 23030b57cec5SDimitry Andric } 23040b57cec5SDimitry Andric } 23050b57cec5SDimitry Andric } 23060b57cec5SDimitry Andric 23070b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args, 23080b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23090b57cec5SDimitry Andric // Default to "hidden" visibility. 23100b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fvisibility_EQ, 2311bdd1243dSDimitry Andric options::OPT_fvisibility_ms_compat)) 2312bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=hidden"); 23130b57cec5SDimitry Andric } 23140b57cec5SDimitry Andric 23155ffd83dbSDimitry Andric void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 23165ffd83dbSDimitry Andric // Floating point operations and argument passing are hard. 23175ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 23185ffd83dbSDimitry Andric CmdArgs.push_back("hard"); 23195ffd83dbSDimitry Andric } 23205ffd83dbSDimitry Andric 23210b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, 23220b57cec5SDimitry Andric StringRef Target, const InputInfo &Output, 23230b57cec5SDimitry Andric const InputInfo &Input, const ArgList &Args) const { 23240b57cec5SDimitry Andric // If this is a dry run, do not create the compilation database file. 23250b57cec5SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 23260b57cec5SDimitry Andric return; 23270b57cec5SDimitry Andric 23280b57cec5SDimitry Andric using llvm::yaml::escape; 23290b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 23300b57cec5SDimitry Andric 23310b57cec5SDimitry Andric if (!CompilationDatabase) { 23320b57cec5SDimitry Andric std::error_code EC; 2333fe6060f1SDimitry Andric auto File = std::make_unique<llvm::raw_fd_ostream>( 233481ad6265SDimitry Andric Filename, EC, 233581ad6265SDimitry Andric llvm::sys::fs::OF_TextWithCRLF | llvm::sys::fs::OF_Append); 23360b57cec5SDimitry Andric if (EC) { 23370b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_compilationdatabase) << Filename 23380b57cec5SDimitry Andric << EC.message(); 23390b57cec5SDimitry Andric return; 23400b57cec5SDimitry Andric } 23410b57cec5SDimitry Andric CompilationDatabase = std::move(File); 23420b57cec5SDimitry Andric } 23430b57cec5SDimitry Andric auto &CDB = *CompilationDatabase; 2344a7dea167SDimitry Andric auto CWD = D.getVFS().getCurrentWorkingDirectory(); 2345a7dea167SDimitry Andric if (!CWD) 2346a7dea167SDimitry Andric CWD = "."; 2347a7dea167SDimitry Andric CDB << "{ \"directory\": \"" << escape(*CWD) << "\""; 23480b57cec5SDimitry Andric CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\""; 23495f757f3fSDimitry Andric if (Output.isFilename()) 23500b57cec5SDimitry Andric CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; 23510b57cec5SDimitry Andric CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\""; 2352a7dea167SDimitry Andric SmallString<128> Buf; 23530b57cec5SDimitry Andric Buf = "-x"; 23540b57cec5SDimitry Andric Buf += types::getTypeName(Input.getType()); 23550b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 23560b57cec5SDimitry Andric if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) { 23570b57cec5SDimitry Andric Buf = "--sysroot="; 23580b57cec5SDimitry Andric Buf += D.SysRoot; 23590b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 23600b57cec5SDimitry Andric } 23610b57cec5SDimitry Andric CDB << ", \"" << escape(Input.getFilename()) << "\""; 23625f757f3fSDimitry Andric if (Output.isFilename()) 236381ad6265SDimitry Andric CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\""; 23640b57cec5SDimitry Andric for (auto &A: Args) { 23650b57cec5SDimitry Andric auto &O = A->getOption(); 23660b57cec5SDimitry Andric // Skip language selection, which is positional. 23670b57cec5SDimitry Andric if (O.getID() == options::OPT_x) 23680b57cec5SDimitry Andric continue; 23690b57cec5SDimitry Andric // Skip writing dependency output and the compilation database itself. 23700b57cec5SDimitry Andric if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group) 23710b57cec5SDimitry Andric continue; 2372a7dea167SDimitry Andric if (O.getID() == options::OPT_gen_cdb_fragment_path) 2373a7dea167SDimitry Andric continue; 23740b57cec5SDimitry Andric // Skip inputs. 23750b57cec5SDimitry Andric if (O.getKind() == Option::InputClass) 23760b57cec5SDimitry Andric continue; 237781ad6265SDimitry Andric // Skip output. 237881ad6265SDimitry Andric if (O.getID() == options::OPT_o) 237981ad6265SDimitry Andric continue; 23800b57cec5SDimitry Andric // All other arguments are quoted and appended. 23810b57cec5SDimitry Andric ArgStringList ASL; 23820b57cec5SDimitry Andric A->render(Args, ASL); 23830b57cec5SDimitry Andric for (auto &it: ASL) 23840b57cec5SDimitry Andric CDB << ", \"" << escape(it) << "\""; 23850b57cec5SDimitry Andric } 23860b57cec5SDimitry Andric Buf = "--target="; 23870b57cec5SDimitry Andric Buf += Target; 23880b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\"]},\n"; 23890b57cec5SDimitry Andric } 23900b57cec5SDimitry Andric 2391a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir( 2392a7dea167SDimitry Andric StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output, 2393a7dea167SDimitry Andric const InputInfo &Input, const llvm::opt::ArgList &Args) const { 2394a7dea167SDimitry Andric // If this is a dry run, do not create the compilation database file. 2395a7dea167SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 2396a7dea167SDimitry Andric return; 2397a7dea167SDimitry Andric 2398a7dea167SDimitry Andric if (CompilationDatabase) 2399a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2400a7dea167SDimitry Andric 2401a7dea167SDimitry Andric SmallString<256> Path = Dir; 2402a7dea167SDimitry Andric const auto &Driver = C.getDriver(); 2403a7dea167SDimitry Andric Driver.getVFS().makeAbsolute(Path); 2404a7dea167SDimitry Andric auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true); 2405a7dea167SDimitry Andric if (Err) { 2406a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message(); 2407a7dea167SDimitry Andric return; 2408a7dea167SDimitry Andric } 2409a7dea167SDimitry Andric 2410a7dea167SDimitry Andric llvm::sys::path::append( 2411a7dea167SDimitry Andric Path, 2412a7dea167SDimitry Andric Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json"); 2413a7dea167SDimitry Andric int FD; 2414a7dea167SDimitry Andric SmallString<256> TempPath; 2415fe6060f1SDimitry Andric Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath, 2416fe6060f1SDimitry Andric llvm::sys::fs::OF_Text); 2417a7dea167SDimitry Andric if (Err) { 2418a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message(); 2419a7dea167SDimitry Andric return; 2420a7dea167SDimitry Andric } 2421a7dea167SDimitry Andric CompilationDatabase = 2422a7dea167SDimitry Andric std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true); 2423a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2424a7dea167SDimitry Andric } 2425a7dea167SDimitry Andric 2426fe6060f1SDimitry Andric static bool CheckARMImplicitITArg(StringRef Value) { 2427fe6060f1SDimitry Andric return Value == "always" || Value == "never" || Value == "arm" || 2428fe6060f1SDimitry Andric Value == "thumb"; 2429fe6060f1SDimitry Andric } 2430fe6060f1SDimitry Andric 2431fe6060f1SDimitry Andric static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs, 2432fe6060f1SDimitry Andric StringRef Value) { 2433fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 2434fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); 2435fe6060f1SDimitry Andric } 2436fe6060f1SDimitry Andric 24370b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C, 24380b57cec5SDimitry Andric const ArgList &Args, 24390b57cec5SDimitry Andric ArgStringList &CmdArgs, 24400b57cec5SDimitry Andric const Driver &D) { 24410b57cec5SDimitry Andric if (UseRelaxAll(C, Args)) 24420b57cec5SDimitry Andric CmdArgs.push_back("-mrelax-all"); 24430b57cec5SDimitry Andric 24440b57cec5SDimitry Andric // Only default to -mincremental-linker-compatible if we think we are 24450b57cec5SDimitry Andric // targeting the MSVC linker. 24460b57cec5SDimitry Andric bool DefaultIncrementalLinkerCompatible = 24470b57cec5SDimitry Andric C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); 24480b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mincremental_linker_compatible, 24490b57cec5SDimitry Andric options::OPT_mno_incremental_linker_compatible, 24500b57cec5SDimitry Andric DefaultIncrementalLinkerCompatible)) 24510b57cec5SDimitry Andric CmdArgs.push_back("-mincremental-linker-compatible"); 24520b57cec5SDimitry Andric 245381ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_dwarf_unwind_EQ); 245481ad6265SDimitry Andric 245506c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_femit_compact_unwind_non_canonical, 245606c3fb27SDimitry Andric options::OPT_fno_emit_compact_unwind_non_canonical); 245706c3fb27SDimitry Andric 2458a7dea167SDimitry Andric // If you add more args here, also add them to the block below that 2459a7dea167SDimitry Andric // starts with "// If CollectArgsForIntegratedAssembler() isn't called below". 2460a7dea167SDimitry Andric 24610b57cec5SDimitry Andric // When passing -I arguments to the assembler we sometimes need to 24620b57cec5SDimitry Andric // unconditionally take the next argument. For example, when parsing 24630b57cec5SDimitry Andric // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the 24640b57cec5SDimitry Andric // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo' 24650b57cec5SDimitry Andric // arg after parsing the '-I' arg. 24660b57cec5SDimitry Andric bool TakeNextArg = false; 24670b57cec5SDimitry Andric 24680b57cec5SDimitry Andric bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations(); 2469349cc55cSDimitry Andric bool UseNoExecStack = false; 24700b57cec5SDimitry Andric const char *MipsTargetFeature = nullptr; 2471fe6060f1SDimitry Andric StringRef ImplicitIt; 24720b57cec5SDimitry Andric for (const Arg *A : 2473fe6060f1SDimitry Andric Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler, 2474fe6060f1SDimitry Andric options::OPT_mimplicit_it_EQ)) { 24750b57cec5SDimitry Andric A->claim(); 24760b57cec5SDimitry Andric 2477fe6060f1SDimitry Andric if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) { 2478fe6060f1SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 2479fe6060f1SDimitry Andric case llvm::Triple::arm: 2480fe6060f1SDimitry Andric case llvm::Triple::armeb: 2481fe6060f1SDimitry Andric case llvm::Triple::thumb: 2482fe6060f1SDimitry Andric case llvm::Triple::thumbeb: 2483fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2484fe6060f1SDimitry Andric ImplicitIt = A->getValue(); 2485fe6060f1SDimitry Andric if (!CheckARMImplicitITArg(ImplicitIt)) 2486fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2487bdd1243dSDimitry Andric << A->getSpelling() << ImplicitIt; 2488fe6060f1SDimitry Andric continue; 2489fe6060f1SDimitry Andric default: 2490fe6060f1SDimitry Andric break; 2491fe6060f1SDimitry Andric } 2492fe6060f1SDimitry Andric } 2493fe6060f1SDimitry Andric 24940b57cec5SDimitry Andric for (StringRef Value : A->getValues()) { 24950b57cec5SDimitry Andric if (TakeNextArg) { 24960b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 24970b57cec5SDimitry Andric TakeNextArg = false; 24980b57cec5SDimitry Andric continue; 24990b57cec5SDimitry Andric } 25000b57cec5SDimitry Andric 25010b57cec5SDimitry Andric if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() && 25020b57cec5SDimitry Andric Value == "-mbig-obj") 25030b57cec5SDimitry Andric continue; // LLVM handles bigobj automatically 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 25060b57cec5SDimitry Andric default: 25070b57cec5SDimitry Andric break; 2508bdd1243dSDimitry Andric case llvm::Triple::wasm32: 2509bdd1243dSDimitry Andric case llvm::Triple::wasm64: 2510bdd1243dSDimitry Andric if (Value == "--no-type-check") { 2511bdd1243dSDimitry Andric CmdArgs.push_back("-mno-type-check"); 2512bdd1243dSDimitry Andric continue; 2513bdd1243dSDimitry Andric } 2514bdd1243dSDimitry Andric break; 25150b57cec5SDimitry Andric case llvm::Triple::thumb: 25160b57cec5SDimitry Andric case llvm::Triple::thumbeb: 25170b57cec5SDimitry Andric case llvm::Triple::arm: 25180b57cec5SDimitry Andric case llvm::Triple::armeb: 25195f757f3fSDimitry Andric if (Value.starts_with("-mimplicit-it=")) { 2520fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2521fe6060f1SDimitry Andric ImplicitIt = Value.split("=").second; 2522fe6060f1SDimitry Andric if (CheckARMImplicitITArg(ImplicitIt)) 2523fe6060f1SDimitry Andric continue; 2524fe6060f1SDimitry Andric } 25250b57cec5SDimitry Andric if (Value == "-mthumb") 25260b57cec5SDimitry Andric // -mthumb has already been processed in ComputeLLVMTriple() 25270b57cec5SDimitry Andric // recognize but skip over here. 25280b57cec5SDimitry Andric continue; 25290b57cec5SDimitry Andric break; 25300b57cec5SDimitry Andric case llvm::Triple::mips: 25310b57cec5SDimitry Andric case llvm::Triple::mipsel: 25320b57cec5SDimitry Andric case llvm::Triple::mips64: 25330b57cec5SDimitry Andric case llvm::Triple::mips64el: 25340b57cec5SDimitry Andric if (Value == "--trap") { 25350b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25360b57cec5SDimitry Andric CmdArgs.push_back("+use-tcc-in-div"); 25370b57cec5SDimitry Andric continue; 25380b57cec5SDimitry Andric } 25390b57cec5SDimitry Andric if (Value == "--break") { 25400b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25410b57cec5SDimitry Andric CmdArgs.push_back("-use-tcc-in-div"); 25420b57cec5SDimitry Andric continue; 25430b57cec5SDimitry Andric } 25445f757f3fSDimitry Andric if (Value.starts_with("-msoft-float")) { 25450b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25460b57cec5SDimitry Andric CmdArgs.push_back("+soft-float"); 25470b57cec5SDimitry Andric continue; 25480b57cec5SDimitry Andric } 25495f757f3fSDimitry Andric if (Value.starts_with("-mhard-float")) { 25500b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25510b57cec5SDimitry Andric CmdArgs.push_back("-soft-float"); 25520b57cec5SDimitry Andric continue; 25530b57cec5SDimitry Andric } 25540b57cec5SDimitry Andric 25550b57cec5SDimitry Andric MipsTargetFeature = llvm::StringSwitch<const char *>(Value) 25560b57cec5SDimitry Andric .Case("-mips1", "+mips1") 25570b57cec5SDimitry Andric .Case("-mips2", "+mips2") 25580b57cec5SDimitry Andric .Case("-mips3", "+mips3") 25590b57cec5SDimitry Andric .Case("-mips4", "+mips4") 25600b57cec5SDimitry Andric .Case("-mips5", "+mips5") 25610b57cec5SDimitry Andric .Case("-mips32", "+mips32") 25620b57cec5SDimitry Andric .Case("-mips32r2", "+mips32r2") 25630b57cec5SDimitry Andric .Case("-mips32r3", "+mips32r3") 25640b57cec5SDimitry Andric .Case("-mips32r5", "+mips32r5") 25650b57cec5SDimitry Andric .Case("-mips32r6", "+mips32r6") 25660b57cec5SDimitry Andric .Case("-mips64", "+mips64") 25670b57cec5SDimitry Andric .Case("-mips64r2", "+mips64r2") 25680b57cec5SDimitry Andric .Case("-mips64r3", "+mips64r3") 25690b57cec5SDimitry Andric .Case("-mips64r5", "+mips64r5") 25700b57cec5SDimitry Andric .Case("-mips64r6", "+mips64r6") 25710b57cec5SDimitry Andric .Default(nullptr); 25720b57cec5SDimitry Andric if (MipsTargetFeature) 25730b57cec5SDimitry Andric continue; 25740b57cec5SDimitry Andric } 25750b57cec5SDimitry Andric 25760b57cec5SDimitry Andric if (Value == "-force_cpusubtype_ALL") { 25770b57cec5SDimitry Andric // Do nothing, this is the default and we don't support anything else. 25780b57cec5SDimitry Andric } else if (Value == "-L") { 25790b57cec5SDimitry Andric CmdArgs.push_back("-msave-temp-labels"); 25800b57cec5SDimitry Andric } else if (Value == "--fatal-warnings") { 25810b57cec5SDimitry Andric CmdArgs.push_back("-massembler-fatal-warnings"); 2582a7dea167SDimitry Andric } else if (Value == "--no-warn" || Value == "-W") { 2583a7dea167SDimitry Andric CmdArgs.push_back("-massembler-no-warn"); 25840b57cec5SDimitry Andric } else if (Value == "--noexecstack") { 25850b57cec5SDimitry Andric UseNoExecStack = true; 25865f757f3fSDimitry Andric } else if (Value.starts_with("-compress-debug-sections") || 25875f757f3fSDimitry Andric Value.starts_with("--compress-debug-sections") || 25880b57cec5SDimitry Andric Value == "-nocompress-debug-sections" || 25890b57cec5SDimitry Andric Value == "--nocompress-debug-sections") { 25900b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25910b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=yes" || 25920b57cec5SDimitry Andric Value == "--mrelax-relocations=yes") { 25930b57cec5SDimitry Andric UseRelaxRelocations = true; 25940b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=no" || 25950b57cec5SDimitry Andric Value == "--mrelax-relocations=no") { 25960b57cec5SDimitry Andric UseRelaxRelocations = false; 25975f757f3fSDimitry Andric } else if (Value.starts_with("-I")) { 25980b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25990b57cec5SDimitry Andric // We need to consume the next argument if the current arg is a plain 26000b57cec5SDimitry Andric // -I. The next arg will be the include directory. 26010b57cec5SDimitry Andric if (Value == "-I") 26020b57cec5SDimitry Andric TakeNextArg = true; 26035f757f3fSDimitry Andric } else if (Value.starts_with("-gdwarf-")) { 26040b57cec5SDimitry Andric // "-gdwarf-N" options are not cc1as options. 26050b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNum(Value); 26060b57cec5SDimitry Andric if (DwarfVersion == 0) { // Send it onward, and let cc1as complain. 26070b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26080b57cec5SDimitry Andric } else { 26090b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, 261006c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoConstructor, 26110b57cec5SDimitry Andric DwarfVersion, llvm::DebuggerKind::Default); 26120b57cec5SDimitry Andric } 26135f757f3fSDimitry Andric } else if (Value.starts_with("-mcpu") || Value.starts_with("-mfpu") || 26145f757f3fSDimitry Andric Value.starts_with("-mhwdiv") || Value.starts_with("-march")) { 26150b57cec5SDimitry Andric // Do nothing, we'll validate it later. 26160b57cec5SDimitry Andric } else if (Value == "-defsym") { 26170b57cec5SDimitry Andric if (A->getNumValues() != 2) { 26180b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << Value; 26190b57cec5SDimitry Andric break; 26200b57cec5SDimitry Andric } 26210b57cec5SDimitry Andric const char *S = A->getValue(1); 26220b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 26230b57cec5SDimitry Andric auto Sym = Pair.first; 26240b57cec5SDimitry Andric auto SVal = Pair.second; 26250b57cec5SDimitry Andric 26260b57cec5SDimitry Andric if (Sym.empty() || SVal.empty()) { 26270b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << S; 26280b57cec5SDimitry Andric break; 26290b57cec5SDimitry Andric } 26300b57cec5SDimitry Andric int64_t IVal; 26310b57cec5SDimitry Andric if (SVal.getAsInteger(0, IVal)) { 26320b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; 26330b57cec5SDimitry Andric break; 26340b57cec5SDimitry Andric } 26350b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26360b57cec5SDimitry Andric TakeNextArg = true; 26370b57cec5SDimitry Andric } else if (Value == "-fdebug-compilation-dir") { 26380b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 26390b57cec5SDimitry Andric TakeNextArg = true; 2640480093f4SDimitry Andric } else if (Value.consume_front("-fdebug-compilation-dir=")) { 2641480093f4SDimitry Andric // The flag is a -Wa / -Xassembler argument and Options doesn't 2642480093f4SDimitry Andric // parse the argument, so this isn't automatically aliased to 2643480093f4SDimitry Andric // -fdebug-compilation-dir (without '=') here. 2644480093f4SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 2645480093f4SDimitry Andric CmdArgs.push_back(Value.data()); 2646fe6060f1SDimitry Andric } else if (Value == "--version") { 2647fe6060f1SDimitry Andric D.PrintVersion(C, llvm::outs()); 26480b57cec5SDimitry Andric } else { 26490b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2650bdd1243dSDimitry Andric << A->getSpelling() << Value; 26510b57cec5SDimitry Andric } 26520b57cec5SDimitry Andric } 26530b57cec5SDimitry Andric } 2654fe6060f1SDimitry Andric if (ImplicitIt.size()) 2655fe6060f1SDimitry Andric AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt); 2656bdd1243dSDimitry Andric if (!UseRelaxRelocations) 2657bdd1243dSDimitry Andric CmdArgs.push_back("-mrelax-relocations=no"); 26580b57cec5SDimitry Andric if (UseNoExecStack) 26590b57cec5SDimitry Andric CmdArgs.push_back("-mnoexecstack"); 26600b57cec5SDimitry Andric if (MipsTargetFeature != nullptr) { 26610b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26620b57cec5SDimitry Andric CmdArgs.push_back(MipsTargetFeature); 26630b57cec5SDimitry Andric } 26640b57cec5SDimitry Andric 26650b57cec5SDimitry Andric // forward -fembed-bitcode to assmebler 26660b57cec5SDimitry Andric if (C.getDriver().embedBitcodeEnabled() || 26670b57cec5SDimitry Andric C.getDriver().embedBitcodeMarkerOnly()) 26680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 2669bdd1243dSDimitry Andric 2670bdd1243dSDimitry Andric if (const char *AsSecureLogFile = getenv("AS_SECURE_LOG_FILE")) { 2671bdd1243dSDimitry Andric CmdArgs.push_back("-as-secure-log-file"); 2672bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(AsSecureLogFile)); 2673bdd1243dSDimitry Andric } 26740b57cec5SDimitry Andric } 26750b57cec5SDimitry Andric 26765f757f3fSDimitry Andric static StringRef EnumComplexRangeToStr(LangOptions::ComplexRangeKind Range) { 26775f757f3fSDimitry Andric StringRef RangeStr = ""; 26785f757f3fSDimitry Andric switch (Range) { 26795f757f3fSDimitry Andric case LangOptions::ComplexRangeKind::CX_Limited: 26805f757f3fSDimitry Andric return "-fcx-limited-range"; 26815f757f3fSDimitry Andric break; 26825f757f3fSDimitry Andric case LangOptions::ComplexRangeKind::CX_Fortran: 26835f757f3fSDimitry Andric return "-fcx-fortran-rules"; 26845f757f3fSDimitry Andric break; 26855f757f3fSDimitry Andric default: 26865f757f3fSDimitry Andric return RangeStr; 26875f757f3fSDimitry Andric break; 26885f757f3fSDimitry Andric } 26895f757f3fSDimitry Andric } 26905f757f3fSDimitry Andric 26915f757f3fSDimitry Andric static void EmitComplexRangeDiag(const Driver &D, 26925f757f3fSDimitry Andric LangOptions::ComplexRangeKind Range1, 26935f757f3fSDimitry Andric LangOptions::ComplexRangeKind Range2) { 26945f757f3fSDimitry Andric if (Range1 != LangOptions::ComplexRangeKind::CX_Full) 26955f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 26965f757f3fSDimitry Andric << EnumComplexRangeToStr(Range1) << EnumComplexRangeToStr(Range2); 26975f757f3fSDimitry Andric } 26985f757f3fSDimitry Andric 26995f757f3fSDimitry Andric static std::string RenderComplexRangeOption(std::string Range) { 27005f757f3fSDimitry Andric std::string ComplexRangeStr = "-complex-range="; 27015f757f3fSDimitry Andric ComplexRangeStr += Range; 27025f757f3fSDimitry Andric return ComplexRangeStr; 27035f757f3fSDimitry Andric } 27045f757f3fSDimitry Andric 27050b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, 27060b57cec5SDimitry Andric bool OFastEnabled, const ArgList &Args, 27075ffd83dbSDimitry Andric ArgStringList &CmdArgs, 27085ffd83dbSDimitry Andric const JobAction &JA) { 27090b57cec5SDimitry Andric // Handle various floating point optimization flags, mapping them to the 27100b57cec5SDimitry Andric // appropriate LLVM code generation flags. This is complicated by several 27110b57cec5SDimitry Andric // "umbrella" flags, so we do this by stepping through the flags incrementally 27120b57cec5SDimitry Andric // adjusting what we think is enabled/disabled, then at the end setting the 27130b57cec5SDimitry Andric // LLVM flags based on the final state. 27140b57cec5SDimitry Andric bool HonorINFs = true; 27150b57cec5SDimitry Andric bool HonorNaNs = true; 2716349cc55cSDimitry Andric bool ApproxFunc = false; 27170b57cec5SDimitry Andric // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 27180b57cec5SDimitry Andric bool MathErrno = TC.IsMathErrnoDefault(); 27190b57cec5SDimitry Andric bool AssociativeMath = false; 27200b57cec5SDimitry Andric bool ReciprocalMath = false; 27210b57cec5SDimitry Andric bool SignedZeros = true; 2722480093f4SDimitry Andric bool TrappingMath = false; // Implemented via -ffp-exception-behavior 2723480093f4SDimitry Andric bool TrappingMathPresent = false; // Is trapping-math in args, and not 2724480093f4SDimitry Andric // overriden by ffp-exception-behavior? 2725480093f4SDimitry Andric bool RoundingFPMath = false; 2726480093f4SDimitry Andric bool RoundingMathPresent = false; // Is rounding-math in args? 2727480093f4SDimitry Andric // -ffp-model values: strict, fast, precise 2728480093f4SDimitry Andric StringRef FPModel = ""; 2729480093f4SDimitry Andric // -ffp-exception-behavior options: strict, maytrap, ignore 2730480093f4SDimitry Andric StringRef FPExceptionBehavior = ""; 273181ad6265SDimitry Andric // -ffp-eval-method options: double, extended, source 273281ad6265SDimitry Andric StringRef FPEvalMethod = ""; 27335ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFPMath = 27345ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA); 27355ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFP32Math = 27365ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle()); 27375ffd83dbSDimitry Andric 27385ffd83dbSDimitry Andric llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath; 27395ffd83dbSDimitry Andric llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math; 2740349cc55cSDimitry Andric // CUDA and HIP don't rely on the frontend to pass an ffp-contract option. 2741349cc55cSDimitry Andric // If one wasn't given by the user, don't pass it here. 2742349cc55cSDimitry Andric StringRef FPContract; 2743bdd1243dSDimitry Andric StringRef LastSeenFfpContractOption; 2744bdd1243dSDimitry Andric bool SeenUnsafeMathModeOption = false; 2745349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2746349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 2747349cc55cSDimitry Andric FPContract = "on"; 2748480093f4SDimitry Andric bool StrictFPModel = false; 2749bdd1243dSDimitry Andric StringRef Float16ExcessPrecision = ""; 275006c3fb27SDimitry Andric StringRef BFloat16ExcessPrecision = ""; 27515f757f3fSDimitry Andric LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_Full; 27520b57cec5SDimitry Andric 27530b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 27540b57cec5SDimitry Andric CmdArgs.push_back("-mlimit-float-precision"); 27550b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 27560b57cec5SDimitry Andric } 27570b57cec5SDimitry Andric 27580b57cec5SDimitry Andric for (const Arg *A : Args) { 2759480093f4SDimitry Andric auto optID = A->getOption().getID(); 2760480093f4SDimitry Andric bool PreciseFPModel = false; 2761480093f4SDimitry Andric switch (optID) { 2762480093f4SDimitry Andric default: 2763480093f4SDimitry Andric break; 27645f757f3fSDimitry Andric case options::OPT_fcx_limited_range: { 27655f757f3fSDimitry Andric EmitComplexRangeDiag(D, Range, LangOptions::ComplexRangeKind::CX_Limited); 27665f757f3fSDimitry Andric Range = LangOptions::ComplexRangeKind::CX_Limited; 27675f757f3fSDimitry Andric std::string ComplexRangeStr = RenderComplexRangeOption("limited"); 27685f757f3fSDimitry Andric if (!ComplexRangeStr.empty()) 27695f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr)); 27705f757f3fSDimitry Andric break; 27715f757f3fSDimitry Andric } 27725f757f3fSDimitry Andric case options::OPT_fno_cx_limited_range: 27735f757f3fSDimitry Andric Range = LangOptions::ComplexRangeKind::CX_Full; 27745f757f3fSDimitry Andric break; 27755f757f3fSDimitry Andric case options::OPT_fcx_fortran_rules: { 27765f757f3fSDimitry Andric EmitComplexRangeDiag(D, Range, LangOptions::ComplexRangeKind::CX_Fortran); 27775f757f3fSDimitry Andric Range = LangOptions::ComplexRangeKind::CX_Fortran; 27785f757f3fSDimitry Andric std::string ComplexRangeStr = RenderComplexRangeOption("fortran"); 27795f757f3fSDimitry Andric if (!ComplexRangeStr.empty()) 27805f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr)); 27815f757f3fSDimitry Andric break; 27825f757f3fSDimitry Andric } 27835f757f3fSDimitry Andric case options::OPT_fno_cx_fortran_rules: 27845f757f3fSDimitry Andric Range = LangOptions::ComplexRangeKind::CX_Full; 27855f757f3fSDimitry Andric break; 2786480093f4SDimitry Andric case options::OPT_ffp_model_EQ: { 2787480093f4SDimitry Andric // If -ffp-model= is seen, reset to fno-fast-math 2788480093f4SDimitry Andric HonorINFs = true; 2789480093f4SDimitry Andric HonorNaNs = true; 2790bdd1243dSDimitry Andric ApproxFunc = false; 2791480093f4SDimitry Andric // Turning *off* -ffast-math restores the toolchain default. 2792480093f4SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 2793480093f4SDimitry Andric AssociativeMath = false; 2794480093f4SDimitry Andric ReciprocalMath = false; 2795480093f4SDimitry Andric SignedZeros = true; 2796480093f4SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 2797349cc55cSDimitry Andric FPContract = "on"; 27985ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 27995ffd83dbSDimitry Andric 28005ffd83dbSDimitry Andric // FIXME: The target may have picked a non-IEEE default mode here based on 28015ffd83dbSDimitry Andric // -cl-denorms-are-zero. Should the target consider -fp-model interaction? 28025ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 28035ffd83dbSDimitry Andric 2804480093f4SDimitry Andric StringRef Val = A->getValue(); 2805480093f4SDimitry Andric if (OFastEnabled && !Val.equals("fast")) { 2806480093f4SDimitry Andric // Only -ffp-model=fast is compatible with OFast, ignore. 28075f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 28085f757f3fSDimitry Andric << Args.MakeArgString("-ffp-model=" + Val) << "-Ofast"; 2809480093f4SDimitry Andric break; 2810480093f4SDimitry Andric } 2811480093f4SDimitry Andric StrictFPModel = false; 2812480093f4SDimitry Andric PreciseFPModel = true; 2813480093f4SDimitry Andric // ffp-model= is a Driver option, it is entirely rewritten into more 2814480093f4SDimitry Andric // granular options before being passed into cc1. 2815480093f4SDimitry Andric // Use the gcc option in the switch below. 2816349cc55cSDimitry Andric if (!FPModel.empty() && !FPModel.equals(Val)) 28175f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 2818480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + FPModel) 2819480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val); 2820480093f4SDimitry Andric if (Val.equals("fast")) { 2821480093f4SDimitry Andric optID = options::OPT_ffast_math; 2822480093f4SDimitry Andric FPModel = Val; 28236e75b2fbSDimitry Andric FPContract = "fast"; 2824480093f4SDimitry Andric } else if (Val.equals("precise")) { 2825480093f4SDimitry Andric optID = options::OPT_ffp_contract; 2826480093f4SDimitry Andric FPModel = Val; 2827349cc55cSDimitry Andric FPContract = "on"; 2828480093f4SDimitry Andric PreciseFPModel = true; 2829480093f4SDimitry Andric } else if (Val.equals("strict")) { 2830480093f4SDimitry Andric StrictFPModel = true; 2831480093f4SDimitry Andric optID = options::OPT_frounding_math; 2832480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2833480093f4SDimitry Andric FPModel = Val; 28345ffd83dbSDimitry Andric FPContract = "off"; 2835480093f4SDimitry Andric TrappingMath = true; 2836480093f4SDimitry Andric } else 2837480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2838bdd1243dSDimitry Andric << A->getSpelling() << Val; 2839480093f4SDimitry Andric break; 2840480093f4SDimitry Andric } 2841480093f4SDimitry Andric } 2842480093f4SDimitry Andric 2843480093f4SDimitry Andric switch (optID) { 28440b57cec5SDimitry Andric // If this isn't an FP option skip the claim below 28450b57cec5SDimitry Andric default: continue; 28460b57cec5SDimitry Andric 28470b57cec5SDimitry Andric // Options controlling individual features 28480b57cec5SDimitry Andric case options::OPT_fhonor_infinities: HonorINFs = true; break; 28490b57cec5SDimitry Andric case options::OPT_fno_honor_infinities: HonorINFs = false; break; 28500b57cec5SDimitry Andric case options::OPT_fhonor_nans: HonorNaNs = true; break; 28510b57cec5SDimitry Andric case options::OPT_fno_honor_nans: HonorNaNs = false; break; 2852349cc55cSDimitry Andric case options::OPT_fapprox_func: ApproxFunc = true; break; 2853349cc55cSDimitry Andric case options::OPT_fno_approx_func: ApproxFunc = false; break; 28540b57cec5SDimitry Andric case options::OPT_fmath_errno: MathErrno = true; break; 28550b57cec5SDimitry Andric case options::OPT_fno_math_errno: MathErrno = false; break; 28560b57cec5SDimitry Andric case options::OPT_fassociative_math: AssociativeMath = true; break; 28570b57cec5SDimitry Andric case options::OPT_fno_associative_math: AssociativeMath = false; break; 28580b57cec5SDimitry Andric case options::OPT_freciprocal_math: ReciprocalMath = true; break; 28590b57cec5SDimitry Andric case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break; 28600b57cec5SDimitry Andric case options::OPT_fsigned_zeros: SignedZeros = true; break; 28610b57cec5SDimitry Andric case options::OPT_fno_signed_zeros: SignedZeros = false; break; 2862480093f4SDimitry Andric case options::OPT_ftrapping_math: 2863480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2864480093f4SDimitry Andric !FPExceptionBehavior.equals("strict")) 2865480093f4SDimitry Andric // Warn that previous value of option is overridden. 28665f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 28675f757f3fSDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + 28685f757f3fSDimitry Andric FPExceptionBehavior) 2869480093f4SDimitry Andric << "-ftrapping-math"; 2870480093f4SDimitry Andric TrappingMath = true; 2871480093f4SDimitry Andric TrappingMathPresent = true; 2872480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2873480093f4SDimitry Andric break; 2874480093f4SDimitry Andric case options::OPT_fno_trapping_math: 2875480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2876480093f4SDimitry Andric !FPExceptionBehavior.equals("ignore")) 2877480093f4SDimitry Andric // Warn that previous value of option is overridden. 28785f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 28795f757f3fSDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + 28805f757f3fSDimitry Andric FPExceptionBehavior) 2881480093f4SDimitry Andric << "-fno-trapping-math"; 2882480093f4SDimitry Andric TrappingMath = false; 2883480093f4SDimitry Andric TrappingMathPresent = true; 2884480093f4SDimitry Andric FPExceptionBehavior = "ignore"; 2885480093f4SDimitry Andric break; 2886480093f4SDimitry Andric 2887480093f4SDimitry Andric case options::OPT_frounding_math: 2888480093f4SDimitry Andric RoundingFPMath = true; 2889480093f4SDimitry Andric RoundingMathPresent = true; 2890480093f4SDimitry Andric break; 2891480093f4SDimitry Andric 2892480093f4SDimitry Andric case options::OPT_fno_rounding_math: 2893480093f4SDimitry Andric RoundingFPMath = false; 2894480093f4SDimitry Andric RoundingMathPresent = false; 2895480093f4SDimitry Andric break; 28960b57cec5SDimitry Andric 28970b57cec5SDimitry Andric case options::OPT_fdenormal_fp_math_EQ: 28985ffd83dbSDimitry Andric DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue()); 289981ad6265SDimitry Andric DenormalFP32Math = DenormalFPMath; 29005ffd83dbSDimitry Andric if (!DenormalFPMath.isValid()) { 29015ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 29025ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 29035ffd83dbSDimitry Andric } 29045ffd83dbSDimitry Andric break; 29055ffd83dbSDimitry Andric 29065ffd83dbSDimitry Andric case options::OPT_fdenormal_fp_math_f32_EQ: 29075ffd83dbSDimitry Andric DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue()); 29085ffd83dbSDimitry Andric if (!DenormalFP32Math.isValid()) { 29095ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 29105ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 29115ffd83dbSDimitry Andric } 29120b57cec5SDimitry Andric break; 29130b57cec5SDimitry Andric 2914480093f4SDimitry Andric // Validate and pass through -ffp-contract option. 29150b57cec5SDimitry Andric case options::OPT_ffp_contract: { 29160b57cec5SDimitry Andric StringRef Val = A->getValue(); 2917480093f4SDimitry Andric if (PreciseFPModel) { 2918349cc55cSDimitry Andric // -ffp-model=precise enables ffp-contract=on. 2919349cc55cSDimitry Andric // -ffp-model=precise sets PreciseFPModel to on and Val to 2920349cc55cSDimitry Andric // "precise". FPContract is set. 2921480093f4SDimitry Andric ; 29225f757f3fSDimitry Andric } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off") || 29235f757f3fSDimitry Andric Val.equals("fast-honor-pragmas")) { 29240b57cec5SDimitry Andric FPContract = Val; 2925bdd1243dSDimitry Andric LastSeenFfpContractOption = Val; 2926bdd1243dSDimitry Andric } else 29270b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2928bdd1243dSDimitry Andric << A->getSpelling() << Val; 29290b57cec5SDimitry Andric break; 29300b57cec5SDimitry Andric } 29310b57cec5SDimitry Andric 2932480093f4SDimitry Andric // Validate and pass through -ffp-model option. 2933480093f4SDimitry Andric case options::OPT_ffp_model_EQ: 2934480093f4SDimitry Andric // This should only occur in the error case 2935480093f4SDimitry Andric // since the optID has been replaced by a more granular 2936480093f4SDimitry Andric // floating point option. 2937480093f4SDimitry Andric break; 2938480093f4SDimitry Andric 2939480093f4SDimitry Andric // Validate and pass through -ffp-exception-behavior option. 2940480093f4SDimitry Andric case options::OPT_ffp_exception_behavior_EQ: { 2941480093f4SDimitry Andric StringRef Val = A->getValue(); 2942480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2943480093f4SDimitry Andric !FPExceptionBehavior.equals(Val)) 2944480093f4SDimitry Andric // Warn that previous value of option is overridden. 29455f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 29465f757f3fSDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + 29475f757f3fSDimitry Andric FPExceptionBehavior) 2948480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + Val); 2949480093f4SDimitry Andric TrappingMath = TrappingMathPresent = false; 2950480093f4SDimitry Andric if (Val.equals("ignore") || Val.equals("maytrap")) 2951480093f4SDimitry Andric FPExceptionBehavior = Val; 2952480093f4SDimitry Andric else if (Val.equals("strict")) { 2953480093f4SDimitry Andric FPExceptionBehavior = Val; 2954480093f4SDimitry Andric TrappingMath = TrappingMathPresent = true; 2955480093f4SDimitry Andric } else 2956480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2957bdd1243dSDimitry Andric << A->getSpelling() << Val; 2958480093f4SDimitry Andric break; 2959480093f4SDimitry Andric } 2960480093f4SDimitry Andric 296181ad6265SDimitry Andric // Validate and pass through -ffp-eval-method option. 296281ad6265SDimitry Andric case options::OPT_ffp_eval_method_EQ: { 296381ad6265SDimitry Andric StringRef Val = A->getValue(); 296481ad6265SDimitry Andric if (Val.equals("double") || Val.equals("extended") || 296581ad6265SDimitry Andric Val.equals("source")) 296681ad6265SDimitry Andric FPEvalMethod = Val; 296781ad6265SDimitry Andric else 296881ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2969bdd1243dSDimitry Andric << A->getSpelling() << Val; 297081ad6265SDimitry Andric break; 297181ad6265SDimitry Andric } 297281ad6265SDimitry Andric 2973bdd1243dSDimitry Andric case options::OPT_fexcess_precision_EQ: { 2974bdd1243dSDimitry Andric StringRef Val = A->getValue(); 2975bdd1243dSDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 2976bdd1243dSDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 2977bdd1243dSDimitry Andric if (Val.equals("standard") || Val.equals("fast")) 2978bdd1243dSDimitry Andric Float16ExcessPrecision = Val; 2979bdd1243dSDimitry Andric // To make it GCC compatible, allow the value of "16" which 2980bdd1243dSDimitry Andric // means disable excess precision, the same meaning than clang's 2981bdd1243dSDimitry Andric // equivalent value "none". 2982bdd1243dSDimitry Andric else if (Val.equals("16")) 2983bdd1243dSDimitry Andric Float16ExcessPrecision = "none"; 2984bdd1243dSDimitry Andric else 2985bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2986bdd1243dSDimitry Andric << A->getSpelling() << Val; 2987bdd1243dSDimitry Andric } else { 2988bdd1243dSDimitry Andric if (!(Val.equals("standard") || Val.equals("fast"))) 2989bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2990bdd1243dSDimitry Andric << A->getSpelling() << Val; 2991bdd1243dSDimitry Andric } 299206c3fb27SDimitry Andric BFloat16ExcessPrecision = Float16ExcessPrecision; 2993bdd1243dSDimitry Andric break; 2994bdd1243dSDimitry Andric } 29950b57cec5SDimitry Andric case options::OPT_ffinite_math_only: 29960b57cec5SDimitry Andric HonorINFs = false; 29970b57cec5SDimitry Andric HonorNaNs = false; 29980b57cec5SDimitry Andric break; 29990b57cec5SDimitry Andric case options::OPT_fno_finite_math_only: 30000b57cec5SDimitry Andric HonorINFs = true; 30010b57cec5SDimitry Andric HonorNaNs = true; 30020b57cec5SDimitry Andric break; 30030b57cec5SDimitry Andric 30040b57cec5SDimitry Andric case options::OPT_funsafe_math_optimizations: 30050b57cec5SDimitry Andric AssociativeMath = true; 30060b57cec5SDimitry Andric ReciprocalMath = true; 30070b57cec5SDimitry Andric SignedZeros = false; 300804eeddc0SDimitry Andric ApproxFunc = true; 30090b57cec5SDimitry Andric TrappingMath = false; 3010480093f4SDimitry Andric FPExceptionBehavior = ""; 3011bdd1243dSDimitry Andric FPContract = "fast"; 3012bdd1243dSDimitry Andric SeenUnsafeMathModeOption = true; 30130b57cec5SDimitry Andric break; 30140b57cec5SDimitry Andric case options::OPT_fno_unsafe_math_optimizations: 30150b57cec5SDimitry Andric AssociativeMath = false; 30160b57cec5SDimitry Andric ReciprocalMath = false; 30170b57cec5SDimitry Andric SignedZeros = true; 301804eeddc0SDimitry Andric ApproxFunc = false; 30190b57cec5SDimitry Andric TrappingMath = true; 3020480093f4SDimitry Andric FPExceptionBehavior = "strict"; 30215ffd83dbSDimitry Andric 30225ffd83dbSDimitry Andric // The target may have opted to flush by default, so force IEEE. 30235ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 30245ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 3025bdd1243dSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 3026bdd1243dSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) { 3027bdd1243dSDimitry Andric if (LastSeenFfpContractOption != "") { 3028bdd1243dSDimitry Andric FPContract = LastSeenFfpContractOption; 3029bdd1243dSDimitry Andric } else if (SeenUnsafeMathModeOption) 3030bdd1243dSDimitry Andric FPContract = "on"; 3031bdd1243dSDimitry Andric } 30320b57cec5SDimitry Andric break; 30330b57cec5SDimitry Andric 30340b57cec5SDimitry Andric case options::OPT_Ofast: 30350b57cec5SDimitry Andric // If -Ofast is the optimization level, then -ffast-math should be enabled 30360b57cec5SDimitry Andric if (!OFastEnabled) 30370b57cec5SDimitry Andric continue; 3038bdd1243dSDimitry Andric [[fallthrough]]; 30395f757f3fSDimitry Andric case options::OPT_ffast_math: { 30400b57cec5SDimitry Andric HonorINFs = false; 30410b57cec5SDimitry Andric HonorNaNs = false; 30420b57cec5SDimitry Andric MathErrno = false; 30430b57cec5SDimitry Andric AssociativeMath = true; 30440b57cec5SDimitry Andric ReciprocalMath = true; 304504eeddc0SDimitry Andric ApproxFunc = true; 30460b57cec5SDimitry Andric SignedZeros = false; 30470b57cec5SDimitry Andric TrappingMath = false; 3048480093f4SDimitry Andric RoundingFPMath = false; 3049bdd1243dSDimitry Andric FPExceptionBehavior = ""; 30500b57cec5SDimitry Andric // If fast-math is set then set the fp-contract mode to fast. 30510b57cec5SDimitry Andric FPContract = "fast"; 3052bdd1243dSDimitry Andric SeenUnsafeMathModeOption = true; 30535f757f3fSDimitry Andric // ffast-math enables fortran rules for complex multiplication and 30545f757f3fSDimitry Andric // division. 30555f757f3fSDimitry Andric std::string ComplexRangeStr = RenderComplexRangeOption("limited"); 30565f757f3fSDimitry Andric if (!ComplexRangeStr.empty()) 30575f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr)); 30580b57cec5SDimitry Andric break; 30595f757f3fSDimitry Andric } 30600b57cec5SDimitry Andric case options::OPT_fno_fast_math: 30610b57cec5SDimitry Andric HonorINFs = true; 30620b57cec5SDimitry Andric HonorNaNs = true; 30630b57cec5SDimitry Andric // Turning on -ffast-math (with either flag) removes the need for 30640b57cec5SDimitry Andric // MathErrno. However, turning *off* -ffast-math merely restores the 30650b57cec5SDimitry Andric // toolchain default (which may be false). 30660b57cec5SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 30670b57cec5SDimitry Andric AssociativeMath = false; 30680b57cec5SDimitry Andric ReciprocalMath = false; 306904eeddc0SDimitry Andric ApproxFunc = false; 30700b57cec5SDimitry Andric SignedZeros = true; 30710b57cec5SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 30725ffd83dbSDimitry Andric DenormalFPMath = DefaultDenormalFPMath; 30735ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 3074349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 3075bdd1243dSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) { 3076bdd1243dSDimitry Andric if (LastSeenFfpContractOption != "") { 3077bdd1243dSDimitry Andric FPContract = LastSeenFfpContractOption; 3078bdd1243dSDimitry Andric } else if (SeenUnsafeMathModeOption) 3079349cc55cSDimitry Andric FPContract = "on"; 3080349cc55cSDimitry Andric } 30810b57cec5SDimitry Andric break; 30820b57cec5SDimitry Andric } 3083480093f4SDimitry Andric if (StrictFPModel) { 3084480093f4SDimitry Andric // If -ffp-model=strict has been specified on command line but 3085480093f4SDimitry Andric // subsequent options conflict then emit warning diagnostic. 3086349cc55cSDimitry Andric if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath && 308704eeddc0SDimitry Andric SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc && 30885ffd83dbSDimitry Andric DenormalFPMath == llvm::DenormalMode::getIEEE() && 3089349cc55cSDimitry Andric DenormalFP32Math == llvm::DenormalMode::getIEEE() && 3090349cc55cSDimitry Andric FPContract.equals("off")) 3091480093f4SDimitry Andric // OK: Current Arg doesn't conflict with -ffp-model=strict 3092480093f4SDimitry Andric ; 3093480093f4SDimitry Andric else { 3094480093f4SDimitry Andric StrictFPModel = false; 3095480093f4SDimitry Andric FPModel = ""; 3096bdd1243dSDimitry Andric auto RHS = (A->getNumValues() == 0) 3097bdd1243dSDimitry Andric ? A->getSpelling() 3098bdd1243dSDimitry Andric : Args.MakeArgString(A->getSpelling() + A->getValue()); 3099bdd1243dSDimitry Andric if (RHS != "-ffp-model=strict") 31005f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 3101bdd1243dSDimitry Andric << "-ffp-model=strict" << RHS; 3102480093f4SDimitry Andric } 3103480093f4SDimitry Andric } 31040b57cec5SDimitry Andric 31050b57cec5SDimitry Andric // If we handled this option claim it 31060b57cec5SDimitry Andric A->claim(); 31070b57cec5SDimitry Andric } 31080b57cec5SDimitry Andric 31090b57cec5SDimitry Andric if (!HonorINFs) 31100b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-infs"); 31110b57cec5SDimitry Andric 31120b57cec5SDimitry Andric if (!HonorNaNs) 31130b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-nans"); 31140b57cec5SDimitry Andric 3115349cc55cSDimitry Andric if (ApproxFunc) 3116349cc55cSDimitry Andric CmdArgs.push_back("-fapprox-func"); 3117349cc55cSDimitry Andric 31180b57cec5SDimitry Andric if (MathErrno) 31190b57cec5SDimitry Andric CmdArgs.push_back("-fmath-errno"); 31200b57cec5SDimitry Andric 3121bdd1243dSDimitry Andric if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc && 3122bdd1243dSDimitry Andric !TrappingMath) 3123bdd1243dSDimitry Andric CmdArgs.push_back("-funsafe-math-optimizations"); 31240b57cec5SDimitry Andric 31250b57cec5SDimitry Andric if (!SignedZeros) 31260b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-zeros"); 31270b57cec5SDimitry Andric 31280b57cec5SDimitry Andric if (AssociativeMath && !SignedZeros && !TrappingMath) 31290b57cec5SDimitry Andric CmdArgs.push_back("-mreassociate"); 31300b57cec5SDimitry Andric 31310b57cec5SDimitry Andric if (ReciprocalMath) 31320b57cec5SDimitry Andric CmdArgs.push_back("-freciprocal-math"); 31330b57cec5SDimitry Andric 3134480093f4SDimitry Andric if (TrappingMath) { 3135480093f4SDimitry Andric // FP Exception Behavior is also set to strict 3136480093f4SDimitry Andric assert(FPExceptionBehavior.equals("strict")); 3137e8d8bef9SDimitry Andric } 31380b57cec5SDimitry Andric 31395ffd83dbSDimitry Andric // The default is IEEE. 31405ffd83dbSDimitry Andric if (DenormalFPMath != llvm::DenormalMode::getIEEE()) { 31415ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 31425ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 31435ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math=" << DenormalFPMath; 31445ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 31455ffd83dbSDimitry Andric } 31465ffd83dbSDimitry Andric 31475ffd83dbSDimitry Andric // Add f32 specific denormal mode flag if it's different. 31485ffd83dbSDimitry Andric if (DenormalFP32Math != DenormalFPMath) { 31495ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 31505ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 31515ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math; 31525ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 31535ffd83dbSDimitry Andric } 31540b57cec5SDimitry Andric 31550b57cec5SDimitry Andric if (!FPContract.empty()) 31560b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract)); 31570b57cec5SDimitry Andric 3158480093f4SDimitry Andric if (!RoundingFPMath) 3159480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); 3160480093f4SDimitry Andric 3161480093f4SDimitry Andric if (RoundingFPMath && RoundingMathPresent) 3162480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frounding-math")); 3163480093f4SDimitry Andric 3164480093f4SDimitry Andric if (!FPExceptionBehavior.empty()) 3165480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" + 3166480093f4SDimitry Andric FPExceptionBehavior)); 3167480093f4SDimitry Andric 316881ad6265SDimitry Andric if (!FPEvalMethod.empty()) 316981ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-eval-method=" + FPEvalMethod)); 317081ad6265SDimitry Andric 3171bdd1243dSDimitry Andric if (!Float16ExcessPrecision.empty()) 3172bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffloat16-excess-precision=" + 3173bdd1243dSDimitry Andric Float16ExcessPrecision)); 317406c3fb27SDimitry Andric if (!BFloat16ExcessPrecision.empty()) 317506c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fbfloat16-excess-precision=" + 317606c3fb27SDimitry Andric BFloat16ExcessPrecision)); 3177bdd1243dSDimitry Andric 31780b57cec5SDimitry Andric ParseMRecip(D, Args, CmdArgs); 31790b57cec5SDimitry Andric 31800b57cec5SDimitry Andric // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the 31810b57cec5SDimitry Andric // individual features enabled by -ffast-math instead of the option itself as 31820b57cec5SDimitry Andric // that's consistent with gcc's behaviour. 318304eeddc0SDimitry Andric if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc && 3184480093f4SDimitry Andric ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) { 31850b57cec5SDimitry Andric CmdArgs.push_back("-ffast-math"); 3186480093f4SDimitry Andric if (FPModel.equals("fast")) { 3187480093f4SDimitry Andric if (FPContract.equals("fast")) 3188480093f4SDimitry Andric // All set, do nothing. 3189480093f4SDimitry Andric ; 3190480093f4SDimitry Andric else if (FPContract.empty()) 3191480093f4SDimitry Andric // Enable -ffp-contract=fast 3192480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 3193480093f4SDimitry Andric else 31945f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_option) 3195480093f4SDimitry Andric << "-ffp-model=fast" 3196480093f4SDimitry Andric << Args.MakeArgString("-ffp-contract=" + FPContract); 3197480093f4SDimitry Andric } 3198480093f4SDimitry Andric } 31990b57cec5SDimitry Andric 32000b57cec5SDimitry Andric // Handle __FINITE_MATH_ONLY__ similarly. 32010b57cec5SDimitry Andric if (!HonorINFs && !HonorNaNs) 32020b57cec5SDimitry Andric CmdArgs.push_back("-ffinite-math-only"); 32030b57cec5SDimitry Andric 32040b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) { 32050b57cec5SDimitry Andric CmdArgs.push_back("-mfpmath"); 32060b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 32070b57cec5SDimitry Andric } 32080b57cec5SDimitry Andric 32090b57cec5SDimitry Andric // Disable a codegen optimization for floating-point casts. 32100b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow, 32110b57cec5SDimitry Andric options::OPT_fstrict_float_cast_overflow, false)) 32120b57cec5SDimitry Andric CmdArgs.push_back("-fno-strict-float-cast-overflow"); 32135f757f3fSDimitry Andric 3214cb14a3feSDimitry Andric if (Args.hasArg(options::OPT_fcx_limited_range)) 32155f757f3fSDimitry Andric CmdArgs.push_back("-fcx-limited-range"); 3216cb14a3feSDimitry Andric if (Args.hasArg(options::OPT_fcx_fortran_rules)) 32175f757f3fSDimitry Andric CmdArgs.push_back("-fcx-fortran-rules"); 3218cb14a3feSDimitry Andric if (Args.hasArg(options::OPT_fno_cx_limited_range)) 32195f757f3fSDimitry Andric CmdArgs.push_back("-fno-cx-limited-range"); 3220cb14a3feSDimitry Andric if (Args.hasArg(options::OPT_fno_cx_fortran_rules)) 32215f757f3fSDimitry Andric CmdArgs.push_back("-fno-cx-fortran-rules"); 32220b57cec5SDimitry Andric } 32230b57cec5SDimitry Andric 32240b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, 32250b57cec5SDimitry Andric const llvm::Triple &Triple, 32260b57cec5SDimitry Andric const InputInfo &Input) { 32270b57cec5SDimitry Andric // Add default argument set. 32280b57cec5SDimitry Andric if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 32290b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=core"); 32300b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=apiModeling"); 32310b57cec5SDimitry Andric 32320b57cec5SDimitry Andric if (!Triple.isWindowsMSVCEnvironment()) { 32330b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix"); 32340b57cec5SDimitry Andric } else { 32350b57cec5SDimitry Andric // Enable "unix" checkers that also work on Windows. 32360b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.API"); 32370b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.Malloc"); 32380b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof"); 32390b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator"); 32400b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg"); 32410b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg"); 32420b57cec5SDimitry Andric } 32430b57cec5SDimitry Andric 324481ad6265SDimitry Andric // Disable some unix checkers for PS4/PS5. 324581ad6265SDimitry Andric if (Triple.isPS()) { 32460b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.API"); 32470b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork"); 32480b57cec5SDimitry Andric } 32490b57cec5SDimitry Andric 3250480093f4SDimitry Andric if (Triple.isOSDarwin()) { 32510b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=osx"); 3252480093f4SDimitry Andric CmdArgs.push_back( 3253480093f4SDimitry Andric "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType"); 3254480093f4SDimitry Andric } 3255480093f4SDimitry Andric else if (Triple.isOSFuchsia()) 3256480093f4SDimitry Andric CmdArgs.push_back("-analyzer-checker=fuchsia"); 32570b57cec5SDimitry Andric 32580b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=deadcode"); 32590b57cec5SDimitry Andric 32600b57cec5SDimitry Andric if (types::isCXX(Input.getType())) 32610b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=cplusplus"); 32620b57cec5SDimitry Andric 326381ad6265SDimitry Andric if (!Triple.isPS()) { 32640b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 32650b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 32660b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 32670b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 32680b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 32690b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 32700b57cec5SDimitry Andric } 32710b57cec5SDimitry Andric 32720b57cec5SDimitry Andric // Default nullability checks. 32730b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull"); 32740b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull"); 32750b57cec5SDimitry Andric } 32760b57cec5SDimitry Andric 32770b57cec5SDimitry Andric // Set the output format. The default is plist, for (lame) historical reasons. 32780b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-output"); 32790b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 32800b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 32810b57cec5SDimitry Andric else 32820b57cec5SDimitry Andric CmdArgs.push_back("plist"); 32830b57cec5SDimitry Andric 32840b57cec5SDimitry Andric // Disable the presentation of standard compiler warnings when using 32850b57cec5SDimitry Andric // --analyze. We only want to show static analyzer diagnostics or frontend 32860b57cec5SDimitry Andric // errors. 32870b57cec5SDimitry Andric CmdArgs.push_back("-w"); 32880b57cec5SDimitry Andric 32890b57cec5SDimitry Andric // Add -Xanalyzer arguments when running as analyzer. 32900b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 32910b57cec5SDimitry Andric } 32920b57cec5SDimitry Andric 3293753f127fSDimitry Andric static bool isValidSymbolName(StringRef S) { 3294753f127fSDimitry Andric if (S.empty()) 3295753f127fSDimitry Andric return false; 3296753f127fSDimitry Andric 3297753f127fSDimitry Andric if (std::isdigit(S[0])) 3298753f127fSDimitry Andric return false; 3299753f127fSDimitry Andric 3300753f127fSDimitry Andric return llvm::all_of(S, [](char C) { return std::isalnum(C) || C == '_'; }); 3301753f127fSDimitry Andric } 3302753f127fSDimitry Andric 3303e8d8bef9SDimitry Andric static void RenderSSPOptions(const Driver &D, const ToolChain &TC, 3304e8d8bef9SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3305e8d8bef9SDimitry Andric bool KernelOrKext) { 33060b57cec5SDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 33070b57cec5SDimitry Andric 33080b57cec5SDimitry Andric // NVPTX doesn't support stack protectors; from the compiler's perspective, it 33090b57cec5SDimitry Andric // doesn't even have a stack! 33100b57cec5SDimitry Andric if (EffectiveTriple.isNVPTX()) 33110b57cec5SDimitry Andric return; 33120b57cec5SDimitry Andric 33130b57cec5SDimitry Andric // -stack-protector=0 is default. 3314e8d8bef9SDimitry Andric LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff; 3315e8d8bef9SDimitry Andric LangOptions::StackProtectorMode DefaultStackProtectorLevel = 33160b57cec5SDimitry Andric TC.GetDefaultStackProtectorLevel(KernelOrKext); 33170b57cec5SDimitry Andric 33180b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 33190b57cec5SDimitry Andric options::OPT_fstack_protector_all, 33200b57cec5SDimitry Andric options::OPT_fstack_protector_strong, 33210b57cec5SDimitry Andric options::OPT_fstack_protector)) { 33220b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fstack_protector)) 33230b57cec5SDimitry Andric StackProtectorLevel = 3324e8d8bef9SDimitry Andric std::max<>(LangOptions::SSPOn, DefaultStackProtectorLevel); 33250b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_strong)) 33260b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPStrong; 33270b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_all)) 33280b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPReq; 3329bdd1243dSDimitry Andric 3330bdd1243dSDimitry Andric if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) { 3331bdd1243dSDimitry Andric D.Diag(diag::warn_drv_unsupported_option_for_target) 3332bdd1243dSDimitry Andric << A->getSpelling() << EffectiveTriple.getTriple(); 3333bdd1243dSDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 3334bdd1243dSDimitry Andric } 33350b57cec5SDimitry Andric } else { 33360b57cec5SDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 33370b57cec5SDimitry Andric } 33380b57cec5SDimitry Andric 33390b57cec5SDimitry Andric if (StackProtectorLevel) { 33400b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 33410b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 33420b57cec5SDimitry Andric } 33430b57cec5SDimitry Andric 33440b57cec5SDimitry Andric // --param ssp-buffer-size= 33450b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__param)) { 33460b57cec5SDimitry Andric StringRef Str(A->getValue()); 33475f757f3fSDimitry Andric if (Str.starts_with("ssp-buffer-size=")) { 33480b57cec5SDimitry Andric if (StackProtectorLevel) { 33490b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector-buffer-size"); 33500b57cec5SDimitry Andric // FIXME: Verify the argument is a valid integer. 33510b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 33520b57cec5SDimitry Andric } 33530b57cec5SDimitry Andric A->claim(); 33540b57cec5SDimitry Andric } 33550b57cec5SDimitry Andric } 3356e8d8bef9SDimitry Andric 3357e8d8bef9SDimitry Andric const std::string &TripleStr = EffectiveTriple.getTriple(); 3358e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) { 3359e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3360349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3361349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3362e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3363e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3364349cc55cSDimitry Andric if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() || 3365349cc55cSDimitry Andric EffectiveTriple.isThumb()) && 3366349cc55cSDimitry Andric Value != "tls" && Value != "global") { 3367e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3368fe6060f1SDimitry Andric << A->getOption().getName() << Value << "tls global"; 3369fe6060f1SDimitry Andric return; 3370fe6060f1SDimitry Andric } 3371349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3372349cc55cSDimitry Andric Value == "tls") { 3373349cc55cSDimitry Andric if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3374349cc55cSDimitry Andric D.Diag(diag::err_drv_ssp_missing_offset_argument) 3375349cc55cSDimitry Andric << A->getAsString(Args); 3376349cc55cSDimitry Andric return; 3377349cc55cSDimitry Andric } 3378349cc55cSDimitry Andric // Check whether the target subarch supports the hardware TLS register 337904eeddc0SDimitry Andric if (!arm::isHardTPSupported(EffectiveTriple)) { 3380349cc55cSDimitry Andric D.Diag(diag::err_target_unsupported_tp_hard) 3381349cc55cSDimitry Andric << EffectiveTriple.getArchName(); 3382349cc55cSDimitry Andric return; 3383349cc55cSDimitry Andric } 3384349cc55cSDimitry Andric // Check whether the user asked for something other than -mtp=cp15 3385349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 3386349cc55cSDimitry Andric StringRef Value = A->getValue(); 3387349cc55cSDimitry Andric if (Value != "cp15") { 3388349cc55cSDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 3389349cc55cSDimitry Andric << A->getAsString(Args) << "-mstack-protector-guard=tls"; 3390349cc55cSDimitry Andric return; 3391349cc55cSDimitry Andric } 3392349cc55cSDimitry Andric } 3393349cc55cSDimitry Andric CmdArgs.push_back("-target-feature"); 339406c3fb27SDimitry Andric CmdArgs.push_back("+read-tp-tpidruro"); 3395349cc55cSDimitry Andric } 3396fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") { 3397fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3398fe6060f1SDimitry Andric << A->getOption().getName() << Value << "sysreg global"; 3399e8d8bef9SDimitry Andric return; 3400e8d8bef9SDimitry Andric } 3401e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3402e8d8bef9SDimitry Andric } 3403e8d8bef9SDimitry Andric 3404e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3405e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3406349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3407349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3408e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3409e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3410fe6060f1SDimitry Andric int Offset; 3411e8d8bef9SDimitry Andric if (Value.getAsInteger(10, Offset)) { 3412e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3413e8d8bef9SDimitry Andric return; 3414e8d8bef9SDimitry Andric } 3415349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3416349cc55cSDimitry Andric (Offset < 0 || Offset > 0xfffff)) { 3417349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 3418349cc55cSDimitry Andric << A->getOption().getName() << Value; 3419349cc55cSDimitry Andric return; 3420349cc55cSDimitry Andric } 3421e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3422e8d8bef9SDimitry Andric } 3423e8d8bef9SDimitry Andric 3424e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) { 3425e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3426fe6060f1SDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) 3427e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3428e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3429e8d8bef9SDimitry Andric if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) { 3430e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3431fe6060f1SDimitry Andric << A->getOption().getName() << Value << "fs gs"; 3432fe6060f1SDimitry Andric return; 3433fe6060f1SDimitry Andric } 3434fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sp_el0") { 3435fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3436e8d8bef9SDimitry Andric return; 3437e8d8bef9SDimitry Andric } 3438e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3439e8d8bef9SDimitry Andric } 3440753f127fSDimitry Andric 3441753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_symbol_EQ)) { 3442753f127fSDimitry Andric StringRef Value = A->getValue(); 3443753f127fSDimitry Andric if (!isValidSymbolName(Value)) { 3444753f127fSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3445753f127fSDimitry Andric << A->getOption().getName() << "legal symbol name"; 3446753f127fSDimitry Andric return; 3447753f127fSDimitry Andric } 3448753f127fSDimitry Andric A->render(Args, CmdArgs); 3449753f127fSDimitry Andric } 34500b57cec5SDimitry Andric } 34510b57cec5SDimitry Andric 34525ffd83dbSDimitry Andric static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, 34535ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 34545ffd83dbSDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 34555ffd83dbSDimitry Andric 34563063e1e5SEd Maste if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux()) 34575ffd83dbSDimitry Andric return; 34585ffd83dbSDimitry Andric 34595ffd83dbSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && 34605f757f3fSDimitry Andric !EffectiveTriple.isPPC64() && !EffectiveTriple.isAArch64()) 34615ffd83dbSDimitry Andric return; 34625ffd83dbSDimitry Andric 346381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection, 346481ad6265SDimitry Andric options::OPT_fno_stack_clash_protection); 34655ffd83dbSDimitry Andric } 34665ffd83dbSDimitry Andric 34670b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D, 34680b57cec5SDimitry Andric const ToolChain &TC, 34690b57cec5SDimitry Andric const ArgList &Args, 34700b57cec5SDimitry Andric ArgStringList &CmdArgs) { 34710b57cec5SDimitry Andric auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit(); 34720b57cec5SDimitry Andric StringRef TrivialAutoVarInit = ""; 34730b57cec5SDimitry Andric 34740b57cec5SDimitry Andric for (const Arg *A : Args) { 34750b57cec5SDimitry Andric switch (A->getOption().getID()) { 34760b57cec5SDimitry Andric default: 34770b57cec5SDimitry Andric continue; 34780b57cec5SDimitry Andric case options::OPT_ftrivial_auto_var_init: { 34790b57cec5SDimitry Andric A->claim(); 34800b57cec5SDimitry Andric StringRef Val = A->getValue(); 34810b57cec5SDimitry Andric if (Val == "uninitialized" || Val == "zero" || Val == "pattern") 34820b57cec5SDimitry Andric TrivialAutoVarInit = Val; 34830b57cec5SDimitry Andric else 34840b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 3485bdd1243dSDimitry Andric << A->getSpelling() << Val; 34860b57cec5SDimitry Andric break; 34870b57cec5SDimitry Andric } 34880b57cec5SDimitry Andric } 34890b57cec5SDimitry Andric } 34900b57cec5SDimitry Andric 34910b57cec5SDimitry Andric if (TrivialAutoVarInit.empty()) 34920b57cec5SDimitry Andric switch (DefaultTrivialAutoVarInit) { 34930b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Uninitialized: 34940b57cec5SDimitry Andric break; 34950b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Pattern: 34960b57cec5SDimitry Andric TrivialAutoVarInit = "pattern"; 34970b57cec5SDimitry Andric break; 34980b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Zero: 34990b57cec5SDimitry Andric TrivialAutoVarInit = "zero"; 35000b57cec5SDimitry Andric break; 35010b57cec5SDimitry Andric } 35020b57cec5SDimitry Andric 35030b57cec5SDimitry Andric if (!TrivialAutoVarInit.empty()) { 35040b57cec5SDimitry Andric CmdArgs.push_back( 35050b57cec5SDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit)); 35060b57cec5SDimitry Andric } 35075ffd83dbSDimitry Andric 35085ffd83dbSDimitry Andric if (Arg *A = 35095ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) { 35105ffd83dbSDimitry Andric if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) || 35115ffd83dbSDimitry Andric StringRef( 35125ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) == 35135ffd83dbSDimitry Andric "uninitialized") 35145ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency); 35155ffd83dbSDimitry Andric A->claim(); 35165ffd83dbSDimitry Andric StringRef Val = A->getValue(); 35175ffd83dbSDimitry Andric if (std::stoi(Val.str()) <= 0) 35185ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_invalid_value); 35195ffd83dbSDimitry Andric CmdArgs.push_back( 35205ffd83dbSDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val)); 35215ffd83dbSDimitry Andric } 35220b57cec5SDimitry Andric } 35230b57cec5SDimitry Andric 3524fe6060f1SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, 3525fe6060f1SDimitry Andric types::ID InputType) { 35265ffd83dbSDimitry Andric // cl-denorms-are-zero is not forwarded. It is translated into a generic flag 35275ffd83dbSDimitry Andric // for denormal flushing handling based on the target. 35280b57cec5SDimitry Andric const unsigned ForwardedArguments[] = { 35290b57cec5SDimitry Andric options::OPT_cl_opt_disable, 35300b57cec5SDimitry Andric options::OPT_cl_strict_aliasing, 35310b57cec5SDimitry Andric options::OPT_cl_single_precision_constant, 35320b57cec5SDimitry Andric options::OPT_cl_finite_math_only, 35330b57cec5SDimitry Andric options::OPT_cl_kernel_arg_info, 35340b57cec5SDimitry Andric options::OPT_cl_unsafe_math_optimizations, 35350b57cec5SDimitry Andric options::OPT_cl_fast_relaxed_math, 35360b57cec5SDimitry Andric options::OPT_cl_mad_enable, 35370b57cec5SDimitry Andric options::OPT_cl_no_signed_zeros, 35380b57cec5SDimitry Andric options::OPT_cl_fp32_correctly_rounded_divide_sqrt, 35390b57cec5SDimitry Andric options::OPT_cl_uniform_work_group_size 35400b57cec5SDimitry Andric }; 35410b57cec5SDimitry Andric 35420b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) { 35430b57cec5SDimitry Andric std::string CLStdStr = std::string("-cl-std=") + A->getValue(); 35440b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLStdStr)); 354581ad6265SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_cl_ext_EQ)) { 354681ad6265SDimitry Andric std::string CLExtStr = std::string("-cl-ext=") + A->getValue(); 354781ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLExtStr)); 35480b57cec5SDimitry Andric } 35490b57cec5SDimitry Andric 35500b57cec5SDimitry Andric for (const auto &Arg : ForwardedArguments) 35510b57cec5SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 35520b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); 3553fe6060f1SDimitry Andric 3554fe6060f1SDimitry Andric // Only add the default headers if we are compiling OpenCL sources. 3555fe6060f1SDimitry Andric if ((types::isOpenCL(InputType) || 3556fe6060f1SDimitry Andric (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) && 3557fe6060f1SDimitry Andric !Args.hasArg(options::OPT_cl_no_stdinc)) { 3558fe6060f1SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 3559fe6060f1SDimitry Andric CmdArgs.push_back("-fdeclare-opencl-builtins"); 3560fe6060f1SDimitry Andric } 35610b57cec5SDimitry Andric } 35620b57cec5SDimitry Andric 356381ad6265SDimitry Andric static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs, 356481ad6265SDimitry Andric types::ID InputType) { 356581ad6265SDimitry Andric const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version, 356681ad6265SDimitry Andric options::OPT_D, 3567fcaf7f86SDimitry Andric options::OPT_I, 356881ad6265SDimitry Andric options::OPT_S, 3569bdd1243dSDimitry Andric options::OPT_O, 357081ad6265SDimitry Andric options::OPT_emit_llvm, 3571bdd1243dSDimitry Andric options::OPT_emit_obj, 357281ad6265SDimitry Andric options::OPT_disable_llvm_passes, 3573bdd1243dSDimitry Andric options::OPT_fnative_half_type, 3574bdd1243dSDimitry Andric options::OPT_hlsl_entrypoint}; 3575bdd1243dSDimitry Andric if (!types::isHLSL(InputType)) 3576bdd1243dSDimitry Andric return; 357781ad6265SDimitry Andric for (const auto &Arg : ForwardedArguments) 357881ad6265SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 357981ad6265SDimitry Andric A->renderAsInput(Args, CmdArgs); 358081ad6265SDimitry Andric // Add the default headers if dxc_no_stdinc is not set. 3581bdd1243dSDimitry Andric if (!Args.hasArg(options::OPT_dxc_no_stdinc) && 3582bdd1243dSDimitry Andric !Args.hasArg(options::OPT_nostdinc)) 358381ad6265SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 358481ad6265SDimitry Andric } 358581ad6265SDimitry Andric 35865f757f3fSDimitry Andric static void RenderOpenACCOptions(const Driver &D, const ArgList &Args, 35875f757f3fSDimitry Andric ArgStringList &CmdArgs, types::ID InputType) { 35885f757f3fSDimitry Andric if (!Args.hasArg(options::OPT_fopenacc)) 35895f757f3fSDimitry Andric return; 35905f757f3fSDimitry Andric 35915f757f3fSDimitry Andric CmdArgs.push_back("-fopenacc"); 35925f757f3fSDimitry Andric 35935f757f3fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_openacc_macro_override)) { 35945f757f3fSDimitry Andric StringRef Value = A->getValue(); 35955f757f3fSDimitry Andric int Version; 35965f757f3fSDimitry Andric if (!Value.getAsInteger(10, Version)) 35975f757f3fSDimitry Andric A->renderAsInput(Args, CmdArgs); 35985f757f3fSDimitry Andric else 35995f757f3fSDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << Value; 36005f757f3fSDimitry Andric } 36015f757f3fSDimitry Andric } 36025f757f3fSDimitry Andric 36030b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, 36040b57cec5SDimitry Andric ArgStringList &CmdArgs) { 36050b57cec5SDimitry Andric bool ARCMTEnabled = false; 36060b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) { 36070b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 36080b57cec5SDimitry Andric options::OPT_ccc_arcmt_modify, 36090b57cec5SDimitry Andric options::OPT_ccc_arcmt_migrate)) { 36100b57cec5SDimitry Andric ARCMTEnabled = true; 36110b57cec5SDimitry Andric switch (A->getOption().getID()) { 36120b57cec5SDimitry Andric default: llvm_unreachable("missed a case"); 36130b57cec5SDimitry Andric case options::OPT_ccc_arcmt_check: 3614e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=check"); 36150b57cec5SDimitry Andric break; 36160b57cec5SDimitry Andric case options::OPT_ccc_arcmt_modify: 3617e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=modify"); 36180b57cec5SDimitry Andric break; 36190b57cec5SDimitry Andric case options::OPT_ccc_arcmt_migrate: 3620e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=migrate"); 36210b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 36220b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 36230b57cec5SDimitry Andric 36240b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 36250b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 36260b57cec5SDimitry Andric break; 36270b57cec5SDimitry Andric } 36280b57cec5SDimitry Andric } 36290b57cec5SDimitry Andric } else { 36300b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 36310b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 36320b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 36330b57cec5SDimitry Andric } 36340b57cec5SDimitry Andric 36350b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 36360b57cec5SDimitry Andric if (ARCMTEnabled) 36370b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 36380b57cec5SDimitry Andric << A->getAsString(Args) << "-ccc-arcmt-migrate"; 36390b57cec5SDimitry Andric 36400b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 36410b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 36420b57cec5SDimitry Andric 36430b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 36440b57cec5SDimitry Andric options::OPT_objcmt_migrate_subscripting, 36450b57cec5SDimitry Andric options::OPT_objcmt_migrate_property)) { 36460b57cec5SDimitry Andric // None specified, means enable them all. 36470b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-literals"); 36480b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-subscripting"); 36490b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-property"); 36500b57cec5SDimitry Andric } else { 36510b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 36520b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 36530b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 36540b57cec5SDimitry Andric } 36550b57cec5SDimitry Andric } else { 36560b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 36570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 36580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 36590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all); 36600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property); 36610b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property); 36620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax); 36630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation); 36640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype); 36650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros); 36660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance); 36670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property); 36680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property); 36690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly); 36700b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init); 3671349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_allowlist_dir_path); 36720b57cec5SDimitry Andric } 36730b57cec5SDimitry Andric } 36740b57cec5SDimitry Andric 36750b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, 36760b57cec5SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 36770b57cec5SDimitry Andric // -fbuiltin is default unless -mkernel is used. 36780b57cec5SDimitry Andric bool UseBuiltins = 36790b57cec5SDimitry Andric Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin, 36800b57cec5SDimitry Andric !Args.hasArg(options::OPT_mkernel)); 36810b57cec5SDimitry Andric if (!UseBuiltins) 36820b57cec5SDimitry Andric CmdArgs.push_back("-fno-builtin"); 36830b57cec5SDimitry Andric 36840b57cec5SDimitry Andric // -ffreestanding implies -fno-builtin. 36850b57cec5SDimitry Andric if (Args.hasArg(options::OPT_ffreestanding)) 36860b57cec5SDimitry Andric UseBuiltins = false; 36870b57cec5SDimitry Andric 36880b57cec5SDimitry Andric // Process the -fno-builtin-* options. 368981ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fno_builtin_)) { 369081ad6265SDimitry Andric A->claim(); 36910b57cec5SDimitry Andric 36920b57cec5SDimitry Andric // If -fno-builtin is specified, then there's no need to pass the option to 36930b57cec5SDimitry Andric // the frontend. 369481ad6265SDimitry Andric if (UseBuiltins) 369581ad6265SDimitry Andric A->render(Args, CmdArgs); 36960b57cec5SDimitry Andric } 36970b57cec5SDimitry Andric 36980b57cec5SDimitry Andric // le32-specific flags: 36990b57cec5SDimitry Andric // -fno-math-builtin: clang should not convert math builtins to intrinsics 37000b57cec5SDimitry Andric // by default. 37010b57cec5SDimitry Andric if (TC.getArch() == llvm::Triple::le32) 37020b57cec5SDimitry Andric CmdArgs.push_back("-fno-math-builtin"); 37030b57cec5SDimitry Andric } 37040b57cec5SDimitry Andric 37055ffd83dbSDimitry Andric bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { 370681ad6265SDimitry Andric if (const char *Str = std::getenv("CLANG_MODULE_CACHE_PATH")) { 370781ad6265SDimitry Andric Twine Path{Str}; 370881ad6265SDimitry Andric Path.toVector(Result); 370981ad6265SDimitry Andric return Path.getSingleStringRef() != ""; 371081ad6265SDimitry Andric } 37115ffd83dbSDimitry Andric if (llvm::sys::path::cache_directory(Result)) { 37125ffd83dbSDimitry Andric llvm::sys::path::append(Result, "clang"); 37130b57cec5SDimitry Andric llvm::sys::path::append(Result, "ModuleCache"); 37145ffd83dbSDimitry Andric return true; 37155ffd83dbSDimitry Andric } 37165ffd83dbSDimitry Andric return false; 37170b57cec5SDimitry Andric } 37180b57cec5SDimitry Andric 3719bdd1243dSDimitry Andric static bool RenderModulesOptions(Compilation &C, const Driver &D, 37200b57cec5SDimitry Andric const ArgList &Args, const InputInfo &Input, 37215f757f3fSDimitry Andric const InputInfo &Output, bool HaveStd20, 3722bdd1243dSDimitry Andric ArgStringList &CmdArgs) { 3723bdd1243dSDimitry Andric bool IsCXX = types::isCXX(Input.getType()); 37245f757f3fSDimitry Andric bool HaveStdCXXModules = IsCXX && HaveStd20; 3725bdd1243dSDimitry Andric bool HaveModules = HaveStdCXXModules; 3726bdd1243dSDimitry Andric 37270b57cec5SDimitry Andric // -fmodules enables the use of precompiled modules (off by default). 37280b57cec5SDimitry Andric // Users can pass -fno-cxx-modules to turn off modules support for 37290b57cec5SDimitry Andric // C++/Objective-C++ programs. 37300b57cec5SDimitry Andric bool HaveClangModules = false; 37310b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 37320b57cec5SDimitry Andric bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 37330b57cec5SDimitry Andric options::OPT_fno_cxx_modules, true); 3734bdd1243dSDimitry Andric if (AllowedInCXX || !IsCXX) { 37350b57cec5SDimitry Andric CmdArgs.push_back("-fmodules"); 37360b57cec5SDimitry Andric HaveClangModules = true; 37370b57cec5SDimitry Andric } 37380b57cec5SDimitry Andric } 37390b57cec5SDimitry Andric 37400b57cec5SDimitry Andric HaveModules |= HaveClangModules; 37410b57cec5SDimitry Andric 37420b57cec5SDimitry Andric // -fmodule-maps enables implicit reading of module map files. By default, 37430b57cec5SDimitry Andric // this is enabled if we are using Clang's flavor of precompiled modules. 37440b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fimplicit_module_maps, 37450b57cec5SDimitry Andric options::OPT_fno_implicit_module_maps, HaveClangModules)) 37460b57cec5SDimitry Andric CmdArgs.push_back("-fimplicit-module-maps"); 37470b57cec5SDimitry Andric 37480b57cec5SDimitry Andric // -fmodules-decluse checks that modules used are declared so (off by default) 374981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fmodules_decluse, 375081ad6265SDimitry Andric options::OPT_fno_modules_decluse); 37510b57cec5SDimitry Andric 37520b57cec5SDimitry Andric // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that 37530b57cec5SDimitry Andric // all #included headers are part of modules. 37540b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_strict_decluse, 37550b57cec5SDimitry Andric options::OPT_fno_modules_strict_decluse, false)) 37560b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-strict-decluse"); 37570b57cec5SDimitry Andric 37580b57cec5SDimitry Andric // -fno-implicit-modules turns off implicitly compiling modules on demand. 37590b57cec5SDimitry Andric bool ImplicitModules = false; 37600b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fimplicit_modules, 37610b57cec5SDimitry Andric options::OPT_fno_implicit_modules, HaveClangModules)) { 37620b57cec5SDimitry Andric if (HaveModules) 37630b57cec5SDimitry Andric CmdArgs.push_back("-fno-implicit-modules"); 37640b57cec5SDimitry Andric } else if (HaveModules) { 37650b57cec5SDimitry Andric ImplicitModules = true; 37660b57cec5SDimitry Andric // -fmodule-cache-path specifies where our implicitly-built module files 37670b57cec5SDimitry Andric // should be written. 37680b57cec5SDimitry Andric SmallString<128> Path; 37690b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) 37700b57cec5SDimitry Andric Path = A->getValue(); 37710b57cec5SDimitry Andric 37725ffd83dbSDimitry Andric bool HasPath = true; 37730b57cec5SDimitry Andric if (C.isForDiagnostics()) { 37740b57cec5SDimitry Andric // When generating crash reports, we want to emit the modules along with 37750b57cec5SDimitry Andric // the reproduction sources, so we ignore any provided module path. 37760b57cec5SDimitry Andric Path = Output.getFilename(); 37770b57cec5SDimitry Andric llvm::sys::path::replace_extension(Path, ".cache"); 37780b57cec5SDimitry Andric llvm::sys::path::append(Path, "modules"); 37790b57cec5SDimitry Andric } else if (Path.empty()) { 37800b57cec5SDimitry Andric // No module path was provided: use the default. 37815ffd83dbSDimitry Andric HasPath = Driver::getDefaultModuleCachePath(Path); 37820b57cec5SDimitry Andric } 37830b57cec5SDimitry Andric 37845ffd83dbSDimitry Andric // `HasPath` will only be false if getDefaultModuleCachePath() fails. 37855ffd83dbSDimitry Andric // That being said, that failure is unlikely and not caching is harmless. 37865ffd83dbSDimitry Andric if (HasPath) { 37870b57cec5SDimitry Andric const char Arg[] = "-fmodules-cache-path="; 37880b57cec5SDimitry Andric Path.insert(Path.begin(), Arg, Arg + strlen(Arg)); 37890b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Path)); 37900b57cec5SDimitry Andric } 37915ffd83dbSDimitry Andric } 37920b57cec5SDimitry Andric 37930b57cec5SDimitry Andric if (HaveModules) { 3794e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules, 3795e8d8bef9SDimitry Andric options::OPT_fno_prebuilt_implicit_modules, false)) 3796e8d8bef9SDimitry Andric CmdArgs.push_back("-fprebuilt-implicit-modules"); 3797a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content, 3798a7dea167SDimitry Andric options::OPT_fno_modules_validate_input_files_content, 3799a7dea167SDimitry Andric false)) 3800a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 38010b57cec5SDimitry Andric } 38020b57cec5SDimitry Andric 38030b57cec5SDimitry Andric // -fmodule-name specifies the module that is currently being built (or 38040b57cec5SDimitry Andric // used for header checking by -fmodule-maps). 38050b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ); 38060b57cec5SDimitry Andric 38070b57cec5SDimitry Andric // -fmodule-map-file can be used to specify files containing module 38080b57cec5SDimitry Andric // definitions. 38090b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file); 38100b57cec5SDimitry Andric 38110b57cec5SDimitry Andric // -fbuiltin-module-map can be used to load the clang 38120b57cec5SDimitry Andric // builtin headers modulemap file. 38130b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuiltin_module_map)) { 38140b57cec5SDimitry Andric SmallString<128> BuiltinModuleMap(D.ResourceDir); 38150b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "include"); 38160b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "module.modulemap"); 38170b57cec5SDimitry Andric if (llvm::sys::fs::exists(BuiltinModuleMap)) 38180b57cec5SDimitry Andric CmdArgs.push_back( 38190b57cec5SDimitry Andric Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap)); 38200b57cec5SDimitry Andric } 38210b57cec5SDimitry Andric 38220b57cec5SDimitry Andric // The -fmodule-file=<name>=<file> form specifies the mapping of module 38230b57cec5SDimitry Andric // names to precompiled module files (the module is loaded only if used). 38240b57cec5SDimitry Andric // The -fmodule-file=<file> form can be used to unconditionally load 38250b57cec5SDimitry Andric // precompiled module files (whether used or not). 382606c3fb27SDimitry Andric if (HaveModules || Input.getType() == clang::driver::types::TY_ModuleFile) { 38270b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file); 382806c3fb27SDimitry Andric 382906c3fb27SDimitry Andric // -fprebuilt-module-path specifies where to load the prebuilt module files. 383006c3fb27SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { 383106c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 383206c3fb27SDimitry Andric std::string("-fprebuilt-module-path=") + A->getValue())); 383306c3fb27SDimitry Andric A->claim(); 383406c3fb27SDimitry Andric } 383506c3fb27SDimitry Andric } else 38360b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_file); 38370b57cec5SDimitry Andric 38380b57cec5SDimitry Andric // When building modules and generating crashdumps, we need to dump a module 38390b57cec5SDimitry Andric // dependency VFS alongside the output. 38400b57cec5SDimitry Andric if (HaveClangModules && C.isForDiagnostics()) { 38410b57cec5SDimitry Andric SmallString<128> VFSDir(Output.getFilename()); 38420b57cec5SDimitry Andric llvm::sys::path::replace_extension(VFSDir, ".cache"); 38430b57cec5SDimitry Andric // Add the cache directory as a temp so the crash diagnostics pick it up. 38440b57cec5SDimitry Andric C.addTempFile(Args.MakeArgString(VFSDir)); 38450b57cec5SDimitry Andric 38460b57cec5SDimitry Andric llvm::sys::path::append(VFSDir, "vfs"); 38470b57cec5SDimitry Andric CmdArgs.push_back("-module-dependency-dir"); 38480b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(VFSDir)); 38490b57cec5SDimitry Andric } 38500b57cec5SDimitry Andric 38510b57cec5SDimitry Andric if (HaveClangModules) 38520b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path); 38530b57cec5SDimitry Andric 38540b57cec5SDimitry Andric // Pass through all -fmodules-ignore-macro arguments. 38550b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 38560b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 38570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 38580b57cec5SDimitry Andric 3859753f127fSDimitry Andric if (HaveClangModules) { 38600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); 38610b57cec5SDimitry Andric 38620b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { 38630b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuild_session_timestamp)) 38640b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 38650b57cec5SDimitry Andric << A->getAsString(Args) << "-fbuild-session-timestamp"; 38660b57cec5SDimitry Andric 38670b57cec5SDimitry Andric llvm::sys::fs::file_status Status; 38680b57cec5SDimitry Andric if (llvm::sys::fs::status(A->getValue(), Status)) 38690b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << A->getValue(); 3870349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 3871349cc55cSDimitry Andric "-fbuild-session-timestamp=" + 3872349cc55cSDimitry Andric Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>( 3873349cc55cSDimitry Andric Status.getLastModificationTime().time_since_epoch()) 38740b57cec5SDimitry Andric .count()))); 38750b57cec5SDimitry Andric } 38760b57cec5SDimitry Andric 3877753f127fSDimitry Andric if (Args.getLastArg( 3878753f127fSDimitry Andric options::OPT_fmodules_validate_once_per_build_session)) { 38790b57cec5SDimitry Andric if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, 38800b57cec5SDimitry Andric options::OPT_fbuild_session_file)) 38810b57cec5SDimitry Andric D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); 38820b57cec5SDimitry Andric 38830b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 38840b57cec5SDimitry Andric options::OPT_fmodules_validate_once_per_build_session); 38850b57cec5SDimitry Andric } 38860b57cec5SDimitry Andric 38870b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, 38880b57cec5SDimitry Andric options::OPT_fno_modules_validate_system_headers, 38890b57cec5SDimitry Andric ImplicitModules)) 38900b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-validate-system-headers"); 38910b57cec5SDimitry Andric 3892753f127fSDimitry Andric Args.AddLastArg(CmdArgs, 3893753f127fSDimitry Andric options::OPT_fmodules_disable_diagnostic_validation); 3894753f127fSDimitry Andric } else { 3895753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_timestamp); 3896753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_file); 3897753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_once_per_build_session); 3898753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_system_headers); 3899753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fno_modules_validate_system_headers); 3900753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_disable_diagnostic_validation); 3901753f127fSDimitry Andric } 3902bdd1243dSDimitry Andric 3903bdd1243dSDimitry Andric // Claim `-fmodule-output` and `-fmodule-output=` to avoid unused warnings. 3904bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_output); 3905bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_output_EQ); 3906bdd1243dSDimitry Andric 3907bdd1243dSDimitry Andric return HaveModules; 39080b57cec5SDimitry Andric } 39090b57cec5SDimitry Andric 39100b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, 39110b57cec5SDimitry Andric ArgStringList &CmdArgs) { 39120b57cec5SDimitry Andric // -fsigned-char is default. 39130b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char, 39140b57cec5SDimitry Andric options::OPT_fno_signed_char, 39150b57cec5SDimitry Andric options::OPT_funsigned_char, 39160b57cec5SDimitry Andric options::OPT_fno_unsigned_char)) { 39170b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_funsigned_char) || 39180b57cec5SDimitry Andric A->getOption().matches(options::OPT_fno_signed_char)) { 39190b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 39200b57cec5SDimitry Andric } 39210b57cec5SDimitry Andric } else if (!isSignedCharDefault(T)) { 39220b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 39230b57cec5SDimitry Andric } 39240b57cec5SDimitry Andric 39250b57cec5SDimitry Andric // The default depends on the language standard. 39260b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t); 39270b57cec5SDimitry Andric 39280b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar, 39290b57cec5SDimitry Andric options::OPT_fno_short_wchar)) { 39300b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fshort_wchar)) { 39310b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=short"); 39320b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 39330b57cec5SDimitry Andric } else { 39340b57cec5SDimitry Andric bool IsARM = T.isARM() || T.isThumb() || T.isAArch64(); 39350b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=int"); 3936e8d8bef9SDimitry Andric if (T.isOSzOS() || 3937e8d8bef9SDimitry Andric (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD()))) 39380b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 39390b57cec5SDimitry Andric else 39400b57cec5SDimitry Andric CmdArgs.push_back("-fsigned-wchar"); 39410b57cec5SDimitry Andric } 394206c3fb27SDimitry Andric } else if (T.isOSzOS()) 394306c3fb27SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 39440b57cec5SDimitry Andric } 39450b57cec5SDimitry Andric 39460b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D, 39470b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 39480b57cec5SDimitry Andric ObjCRuntime &Runtime, bool InferCovariantReturns, 39490b57cec5SDimitry Andric const InputInfo &Input, ArgStringList &CmdArgs) { 39500b57cec5SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 39510b57cec5SDimitry Andric 39520b57cec5SDimitry Andric // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy 39530b57cec5SDimitry Andric // is the default. Except for deployment target of 10.5, next runtime is 39540b57cec5SDimitry Andric // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently. 39550b57cec5SDimitry Andric if (Runtime.isNonFragile()) { 39560b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 39570b57cec5SDimitry Andric options::OPT_fno_objc_legacy_dispatch, 39580b57cec5SDimitry Andric Runtime.isLegacyDispatchDefaultForArch(Arch))) { 39590b57cec5SDimitry Andric if (TC.UseObjCMixedDispatch()) 39600b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 39610b57cec5SDimitry Andric else 39620b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 39630b57cec5SDimitry Andric } 39640b57cec5SDimitry Andric } 39650b57cec5SDimitry Andric 39660b57cec5SDimitry Andric // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option 39670b57cec5SDimitry Andric // to do Array/Dictionary subscripting by default. 39680b57cec5SDimitry Andric if (Arch == llvm::Triple::x86 && T.isMacOSX() && 39690b57cec5SDimitry Andric Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily()) 39700b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-subscripting-legacy-runtime"); 39710b57cec5SDimitry Andric 39720b57cec5SDimitry Andric // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 39730b57cec5SDimitry Andric // NOTE: This logic is duplicated in ToolChains.cpp. 39740b57cec5SDimitry Andric if (isObjCAutoRefCount(Args)) { 39750b57cec5SDimitry Andric TC.CheckObjCARC(); 39760b57cec5SDimitry Andric 39770b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc"); 39780b57cec5SDimitry Andric 39790b57cec5SDimitry Andric // FIXME: It seems like this entire block, and several around it should be 39800b57cec5SDimitry Andric // wrapped in isObjC, but for now we just use it here as this is where it 39810b57cec5SDimitry Andric // was being used previously. 39820b57cec5SDimitry Andric if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) { 39830b57cec5SDimitry Andric if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 39840b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 39850b57cec5SDimitry Andric else 39860b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 39870b57cec5SDimitry Andric } 39880b57cec5SDimitry Andric 39890b57cec5SDimitry Andric // Allow the user to enable full exceptions code emission. 39900b57cec5SDimitry Andric // We default off for Objective-C, on for Objective-C++. 39910b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 39920b57cec5SDimitry Andric options::OPT_fno_objc_arc_exceptions, 39930b57cec5SDimitry Andric /*Default=*/types::isCXX(Input.getType()))) 39940b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-exceptions"); 39950b57cec5SDimitry Andric } 39960b57cec5SDimitry Andric 39970b57cec5SDimitry Andric // Silence warning for full exception code emission options when explicitly 39980b57cec5SDimitry Andric // set to use no ARC. 39990b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fno_objc_arc)) { 40000b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions); 40010b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions); 40020b57cec5SDimitry Andric } 40030b57cec5SDimitry Andric 40040b57cec5SDimitry Andric // Allow the user to control whether messages can be converted to runtime 40050b57cec5SDimitry Andric // functions. 40060b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 40070b57cec5SDimitry Andric auto *Arg = Args.getLastArg( 40080b57cec5SDimitry Andric options::OPT_fobjc_convert_messages_to_runtime_calls, 40090b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls); 40100b57cec5SDimitry Andric if (Arg && 40110b57cec5SDimitry Andric Arg->getOption().matches( 40120b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls)) 40130b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls"); 40140b57cec5SDimitry Andric } 40150b57cec5SDimitry Andric 40160b57cec5SDimitry Andric // -fobjc-infer-related-result-type is the default, except in the Objective-C 40170b57cec5SDimitry Andric // rewriter. 40180b57cec5SDimitry Andric if (InferCovariantReturns) 40190b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-infer-related-result-type"); 40200b57cec5SDimitry Andric 40210b57cec5SDimitry Andric // Pass down -fobjc-weak or -fno-objc-weak if present. 40220b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 40230b57cec5SDimitry Andric auto WeakArg = 40240b57cec5SDimitry Andric Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak); 40250b57cec5SDimitry Andric if (!WeakArg) { 40260b57cec5SDimitry Andric // nothing to do 40270b57cec5SDimitry Andric } else if (!Runtime.allowsWeak()) { 40280b57cec5SDimitry Andric if (WeakArg->getOption().matches(options::OPT_fobjc_weak)) 40290b57cec5SDimitry Andric D.Diag(diag::err_objc_weak_unsupported); 40300b57cec5SDimitry Andric } else { 40310b57cec5SDimitry Andric WeakArg->render(Args, CmdArgs); 40320b57cec5SDimitry Andric } 40330b57cec5SDimitry Andric } 4034fe6060f1SDimitry Andric 4035fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing)) 4036fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing"); 40370b57cec5SDimitry Andric } 40380b57cec5SDimitry Andric 40390b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, 40400b57cec5SDimitry Andric ArgStringList &CmdArgs) { 40410b57cec5SDimitry Andric bool CaretDefault = true; 40420b57cec5SDimitry Andric bool ColumnDefault = true; 40430b57cec5SDimitry Andric 40440b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic, 40450b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_column, 40460b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_caret)) { 40470b57cec5SDimitry Andric switch (A->getOption().getID()) { 40480b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_caret: 40490b57cec5SDimitry Andric CaretDefault = true; 40500b57cec5SDimitry Andric ColumnDefault = true; 40510b57cec5SDimitry Andric break; 40520b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_column: 40530b57cec5SDimitry Andric CaretDefault = false; 40540b57cec5SDimitry Andric ColumnDefault = true; 40550b57cec5SDimitry Andric break; 40560b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_classic: 40570b57cec5SDimitry Andric CaretDefault = false; 40580b57cec5SDimitry Andric ColumnDefault = false; 40590b57cec5SDimitry Andric break; 40600b57cec5SDimitry Andric } 40610b57cec5SDimitry Andric } 40620b57cec5SDimitry Andric 40630b57cec5SDimitry Andric // -fcaret-diagnostics is default. 40640b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 40650b57cec5SDimitry Andric options::OPT_fno_caret_diagnostics, CaretDefault)) 40660b57cec5SDimitry Andric CmdArgs.push_back("-fno-caret-diagnostics"); 40670b57cec5SDimitry Andric 406881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_fixit_info, 406981ad6265SDimitry Andric options::OPT_fno_diagnostics_fixit_info); 407081ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_option, 407181ad6265SDimitry Andric options::OPT_fno_diagnostics_show_option); 40720b57cec5SDimitry Andric 40730b57cec5SDimitry Andric if (const Arg *A = 40740b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 40750b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-category"); 40760b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 40770b57cec5SDimitry Andric } 40780b57cec5SDimitry Andric 407981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdiagnostics_show_hotness, 408081ad6265SDimitry Andric options::OPT_fno_diagnostics_show_hotness); 40810b57cec5SDimitry Andric 40820b57cec5SDimitry Andric if (const Arg *A = 40830b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) { 40840b57cec5SDimitry Andric std::string Opt = 40850b57cec5SDimitry Andric std::string("-fdiagnostics-hotness-threshold=") + A->getValue(); 40860b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 40870b57cec5SDimitry Andric } 40880b57cec5SDimitry Andric 408906c3fb27SDimitry Andric if (const Arg *A = 409006c3fb27SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_misexpect_tolerance_EQ)) { 409106c3fb27SDimitry Andric std::string Opt = 409206c3fb27SDimitry Andric std::string("-fdiagnostics-misexpect-tolerance=") + A->getValue(); 409306c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 409406c3fb27SDimitry Andric } 409506c3fb27SDimitry Andric 40960b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 40970b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 40980b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 4099fcaf7f86SDimitry Andric if (StringRef(A->getValue()) == "sarif" || 4100fcaf7f86SDimitry Andric StringRef(A->getValue()) == "SARIF") 4101fcaf7f86SDimitry Andric D.Diag(diag::warn_drv_sarif_format_unstable); 41020b57cec5SDimitry Andric } 41030b57cec5SDimitry Andric 41040b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg( 41050b57cec5SDimitry Andric options::OPT_fdiagnostics_show_note_include_stack, 41060b57cec5SDimitry Andric options::OPT_fno_diagnostics_show_note_include_stack)) { 41070b57cec5SDimitry Andric const Option &O = A->getOption(); 41080b57cec5SDimitry Andric if (O.matches(options::OPT_fdiagnostics_show_note_include_stack)) 41090b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 41100b57cec5SDimitry Andric else 41110b57cec5SDimitry Andric CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 41120b57cec5SDimitry Andric } 41130b57cec5SDimitry Andric 41140b57cec5SDimitry Andric // Color diagnostics are parsed by the driver directly from argv and later 41150b57cec5SDimitry Andric // re-parsed to construct this job; claim any possible color diagnostic here 41160b57cec5SDimitry Andric // to avoid warn_drv_unused_argument and diagnose bad 41170b57cec5SDimitry Andric // OPT_fdiagnostics_color_EQ values. 411881ad6265SDimitry Andric Args.getLastArg(options::OPT_fcolor_diagnostics, 411981ad6265SDimitry Andric options::OPT_fno_color_diagnostics); 412081ad6265SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_color_EQ)) { 41210b57cec5SDimitry Andric StringRef Value(A->getValue()); 41220b57cec5SDimitry Andric if (Value != "always" && Value != "never" && Value != "auto") 412381ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 412481ad6265SDimitry Andric << Value << A->getOption().getName(); 41250b57cec5SDimitry Andric } 41260b57cec5SDimitry Andric 41270b57cec5SDimitry Andric if (D.getDiags().getDiagnosticOptions().ShowColors) 41280b57cec5SDimitry Andric CmdArgs.push_back("-fcolor-diagnostics"); 41290b57cec5SDimitry Andric 41300b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fansi_escape_codes)) 41310b57cec5SDimitry Andric CmdArgs.push_back("-fansi-escape-codes"); 41320b57cec5SDimitry Andric 413381ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fshow_source_location, 413481ad6265SDimitry Andric options::OPT_fno_show_source_location); 41350b57cec5SDimitry Andric 413606c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_line_numbers, 413706c3fb27SDimitry Andric options::OPT_fno_diagnostics_show_line_numbers); 413806c3fb27SDimitry Andric 41390b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths)) 41400b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-absolute-paths"); 41410b57cec5SDimitry Andric 41420b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column, 41430b57cec5SDimitry Andric ColumnDefault)) 41440b57cec5SDimitry Andric CmdArgs.push_back("-fno-show-column"); 41450b57cec5SDimitry Andric 414681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fspell_checking, 414781ad6265SDimitry Andric options::OPT_fno_spell_checking); 41480b57cec5SDimitry Andric } 41490b57cec5SDimitry Andric 4150fcaf7f86SDimitry Andric DwarfFissionKind tools::getDebugFissionKind(const Driver &D, 41510b57cec5SDimitry Andric const ArgList &Args, Arg *&Arg) { 4152e8d8bef9SDimitry Andric Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ, 4153e8d8bef9SDimitry Andric options::OPT_gno_split_dwarf); 4154e8d8bef9SDimitry Andric if (!Arg || Arg->getOption().matches(options::OPT_gno_split_dwarf)) 41550b57cec5SDimitry Andric return DwarfFissionKind::None; 41560b57cec5SDimitry Andric 41570b57cec5SDimitry Andric if (Arg->getOption().matches(options::OPT_gsplit_dwarf)) 41580b57cec5SDimitry Andric return DwarfFissionKind::Split; 41590b57cec5SDimitry Andric 41600b57cec5SDimitry Andric StringRef Value = Arg->getValue(); 41610b57cec5SDimitry Andric if (Value == "split") 41620b57cec5SDimitry Andric return DwarfFissionKind::Split; 41630b57cec5SDimitry Andric if (Value == "single") 41640b57cec5SDimitry Andric return DwarfFissionKind::Single; 41650b57cec5SDimitry Andric 41660b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 4167bdd1243dSDimitry Andric << Arg->getSpelling() << Arg->getValue(); 41680b57cec5SDimitry Andric return DwarfFissionKind::None; 41690b57cec5SDimitry Andric } 41700b57cec5SDimitry Andric 4171fe6060f1SDimitry Andric static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, 4172fe6060f1SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 4173fe6060f1SDimitry Andric unsigned DwarfVersion) { 4174fe6060f1SDimitry Andric auto *DwarfFormatArg = 4175fe6060f1SDimitry Andric Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); 4176fe6060f1SDimitry Andric if (!DwarfFormatArg) 4177fe6060f1SDimitry Andric return; 4178fe6060f1SDimitry Andric 4179fe6060f1SDimitry Andric if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { 4180fe6060f1SDimitry Andric if (DwarfVersion < 3) 4181fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4182fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; 4183fe6060f1SDimitry Andric else if (!T.isArch64Bit()) 4184fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4185fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; 4186fe6060f1SDimitry Andric else if (!T.isOSBinFormatELF()) 4187fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4188fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "ELF platforms"; 4189fe6060f1SDimitry Andric } 4190fe6060f1SDimitry Andric 4191fe6060f1SDimitry Andric DwarfFormatArg->render(Args, CmdArgs); 4192fe6060f1SDimitry Andric } 4193fe6060f1SDimitry Andric 419406c3fb27SDimitry Andric static void 419506c3fb27SDimitry Andric renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T, 41965f757f3fSDimitry Andric const ArgList &Args, bool IRInput, ArgStringList &CmdArgs, 41975f757f3fSDimitry Andric const InputInfo &Output, 419806c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind &DebugInfoKind, 41990b57cec5SDimitry Andric DwarfFissionKind &DwarfFission) { 42000b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_info_for_profiling, 42010b57cec5SDimitry Andric options::OPT_fno_debug_info_for_profiling, false) && 42020b57cec5SDimitry Andric checkDebugInfoOption( 42030b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC)) 42040b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-info-for-profiling"); 42050b57cec5SDimitry Andric 42060b57cec5SDimitry Andric // The 'g' groups options involve a somewhat intricate sequence of decisions 42070b57cec5SDimitry Andric // about what to pass from the driver to the frontend, but by the time they 42080b57cec5SDimitry Andric // reach cc1 they've been factored into three well-defined orthogonal choices: 42090b57cec5SDimitry Andric // * what level of debug info to generate 42100b57cec5SDimitry Andric // * what dwarf version to write 42110b57cec5SDimitry Andric // * what debugger tuning to use 42120b57cec5SDimitry Andric // This avoids having to monkey around further in cc1 other than to disable 42130b57cec5SDimitry Andric // codeview if not running in a Windows environment. Perhaps even that 42140b57cec5SDimitry Andric // decision should be made in the driver as well though. 42150b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning(); 42160b57cec5SDimitry Andric 42170b57cec5SDimitry Andric bool SplitDWARFInlining = 42180b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_dwarf_inlining, 4219480093f4SDimitry Andric options::OPT_fno_split_dwarf_inlining, false); 42200b57cec5SDimitry Andric 4221e8d8bef9SDimitry Andric // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does 4222e8d8bef9SDimitry Andric // object file generation and no IR generation, -gN should not be needed. So 4223e8d8bef9SDimitry Andric // allow -gsplit-dwarf with either -gN or IR input. 4224e8d8bef9SDimitry Andric if (IRInput || Args.hasArg(options::OPT_g_Group)) { 42250b57cec5SDimitry Andric Arg *SplitDWARFArg; 42260b57cec5SDimitry Andric DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg); 42270b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None && 42280b57cec5SDimitry Andric !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) { 42290b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 42300b57cec5SDimitry Andric SplitDWARFInlining = false; 42310b57cec5SDimitry Andric } 4232e8d8bef9SDimitry Andric } 4233e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 423406c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; 42350b57cec5SDimitry Andric 42360b57cec5SDimitry Andric // If the last option explicitly specified a debug-info level, use it. 42370b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC) && 42380b57cec5SDimitry Andric A->getOption().matches(options::OPT_gN_Group)) { 423906c3fb27SDimitry Andric DebugInfoKind = debugLevelToInfoKind(*A); 42400b57cec5SDimitry Andric // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more 42410b57cec5SDimitry Andric // complicated if you've disabled inline info in the skeleton CUs 42420b57cec5SDimitry Andric // (SplitDWARFInlining) - then there's value in composing split-dwarf and 42430b57cec5SDimitry Andric // line-tables-only, so let those compose naturally in that case. 424406c3fb27SDimitry Andric if (DebugInfoKind == llvm::codegenoptions::NoDebugInfo || 424506c3fb27SDimitry Andric DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly || 424606c3fb27SDimitry Andric (DebugInfoKind == llvm::codegenoptions::DebugLineTablesOnly && 42470b57cec5SDimitry Andric SplitDWARFInlining)) 42480b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 42490b57cec5SDimitry Andric } 42500b57cec5SDimitry Andric } 42510b57cec5SDimitry Andric 42520b57cec5SDimitry Andric // If a debugger tuning argument appeared, remember it. 4253bdd1243dSDimitry Andric bool HasDebuggerTuning = false; 42540b57cec5SDimitry Andric if (const Arg *A = 42550b57cec5SDimitry Andric Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { 4256bdd1243dSDimitry Andric HasDebuggerTuning = true; 42570b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 42580b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_glldb)) 42590b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::LLDB; 42600b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_gsce)) 42610b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::SCE; 4262fe6060f1SDimitry Andric else if (A->getOption().matches(options::OPT_gdbx)) 4263fe6060f1SDimitry Andric DebuggerTuning = llvm::DebuggerKind::DBX; 42640b57cec5SDimitry Andric else 42650b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::GDB; 42660b57cec5SDimitry Andric } 42670b57cec5SDimitry Andric } 42680b57cec5SDimitry Andric 42690b57cec5SDimitry Andric // If a -gdwarf argument appeared, remember it. 4270480093f4SDimitry Andric bool EmitDwarf = false; 4271bdd1243dSDimitry Andric if (const Arg *A = getDwarfNArg(Args)) 4272bdd1243dSDimitry Andric EmitDwarf = checkDebugInfoOption(A, Args, D, TC); 42730b57cec5SDimitry Andric 42745f757f3fSDimitry Andric bool EmitCodeView = false; 4275bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) 4276bdd1243dSDimitry Andric EmitCodeView = checkDebugInfoOption(A, Args, D, TC); 42770b57cec5SDimitry Andric 42780b57cec5SDimitry Andric // If the user asked for debug info but did not explicitly specify -gcodeview 42790b57cec5SDimitry Andric // or -gdwarf, ask the toolchain for the default format. 4280480093f4SDimitry Andric if (!EmitCodeView && !EmitDwarf && 428106c3fb27SDimitry Andric DebugInfoKind != llvm::codegenoptions::NoDebugInfo) { 42820b57cec5SDimitry Andric switch (TC.getDefaultDebugFormat()) { 428306c3fb27SDimitry Andric case llvm::codegenoptions::DIF_CodeView: 42840b57cec5SDimitry Andric EmitCodeView = true; 42850b57cec5SDimitry Andric break; 428606c3fb27SDimitry Andric case llvm::codegenoptions::DIF_DWARF: 4287480093f4SDimitry Andric EmitDwarf = true; 42880b57cec5SDimitry Andric break; 42890b57cec5SDimitry Andric } 42900b57cec5SDimitry Andric } 42910b57cec5SDimitry Andric 4292e8d8bef9SDimitry Andric unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user 4293e8d8bef9SDimitry Andric unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may 4294e8d8bef9SDimitry Andric // be lower than what the user wanted. 4295480093f4SDimitry Andric if (EmitDwarf) { 4296bdd1243dSDimitry Andric RequestedDWARFVersion = getDwarfVersion(TC, Args); 4297e8d8bef9SDimitry Andric // Clamp effective DWARF version to the max supported by the toolchain. 4298e8d8bef9SDimitry Andric EffectiveDWARFVersion = 4299e8d8bef9SDimitry Andric std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion()); 4300bdd1243dSDimitry Andric } else { 4301bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fdebug_default_version); 4302480093f4SDimitry Andric } 4303480093f4SDimitry Andric 43040b57cec5SDimitry Andric // -gline-directives-only supported only for the DWARF debug info. 4305e8d8bef9SDimitry Andric if (RequestedDWARFVersion == 0 && 430606c3fb27SDimitry Andric DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly) 430706c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::NoDebugInfo; 43080b57cec5SDimitry Andric 4309fe6060f1SDimitry Andric // strict DWARF is set to false by default. But for DBX, we need it to be set 4310fe6060f1SDimitry Andric // as true by default. 4311fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf)) 4312fe6060f1SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4313fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf, 4314fe6060f1SDimitry Andric DebuggerTuning == llvm::DebuggerKind::DBX)) 4315fe6060f1SDimitry Andric CmdArgs.push_back("-gstrict-dwarf"); 4316fe6060f1SDimitry Andric 43170b57cec5SDimitry Andric // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags. 43180b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_flags_Group); 43190b57cec5SDimitry Andric 43200b57cec5SDimitry Andric // Column info is included by default for everything except SCE and 43210b57cec5SDimitry Andric // CodeView. Clang doesn't track end columns, just starting columns, which, 43220b57cec5SDimitry Andric // in theory, is fine for CodeView (and PDB). In practice, however, the 4323fe6060f1SDimitry Andric // Microsoft debuggers don't handle missing end columns well, and the AIX 4324fe6060f1SDimitry Andric // debugger DBX also doesn't handle the columns well, so it's better not to 4325fe6060f1SDimitry Andric // include any column info. 43260b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info)) 43270b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 43285ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info, 4329fe6060f1SDimitry Andric !EmitCodeView && 4330fe6060f1SDimitry Andric (DebuggerTuning != llvm::DebuggerKind::SCE && 4331fe6060f1SDimitry Andric DebuggerTuning != llvm::DebuggerKind::DBX))) 43325ffd83dbSDimitry Andric CmdArgs.push_back("-gno-column-info"); 43330b57cec5SDimitry Andric 43340b57cec5SDimitry Andric // FIXME: Move backend command line options to the module. 4335bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_gmodules, options::OPT_gno_modules, false)) { 4336bdd1243dSDimitry Andric // If -gline-tables-only or -gline-directives-only is the last option it 4337bdd1243dSDimitry Andric // wins. 4338bdd1243dSDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_gmodules), Args, D, 4339bdd1243dSDimitry Andric TC)) { 434006c3fb27SDimitry Andric if (DebugInfoKind != llvm::codegenoptions::DebugLineTablesOnly && 434106c3fb27SDimitry Andric DebugInfoKind != llvm::codegenoptions::DebugDirectivesOnly) { 434206c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; 43430b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-ext-refs"); 43440b57cec5SDimitry Andric CmdArgs.push_back("-fmodule-format=obj"); 43450b57cec5SDimitry Andric } 43460b57cec5SDimitry Andric } 4347bdd1243dSDimitry Andric } 43480b57cec5SDimitry Andric 4349fe6060f1SDimitry Andric if (T.isOSBinFormatELF() && SplitDWARFInlining) 4350fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-dwarf-inlining"); 43510b57cec5SDimitry Andric 43520b57cec5SDimitry Andric // After we've dealt with all combinations of things that could 43530b57cec5SDimitry Andric // make DebugInfoKind be other than None or DebugLineTablesOnly, 43540b57cec5SDimitry Andric // figure out if we need to "upgrade" it to standalone debug info. 43550b57cec5SDimitry Andric // We parse these two '-f' options whether or not they will be used, 43560b57cec5SDimitry Andric // to claim them even if you wrote "-fstandalone-debug -gline-tables-only" 43570b57cec5SDimitry Andric bool NeedFullDebug = Args.hasFlag( 43580b57cec5SDimitry Andric options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug, 43590b57cec5SDimitry Andric DebuggerTuning == llvm::DebuggerKind::LLDB || 43600b57cec5SDimitry Andric TC.GetDefaultStandaloneDebug()); 43610b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug)) 43620b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4363e8d8bef9SDimitry Andric 436406c3fb27SDimitry Andric if (DebugInfoKind == llvm::codegenoptions::LimitedDebugInfo || 436506c3fb27SDimitry Andric DebugInfoKind == llvm::codegenoptions::DebugInfoConstructor) { 4366e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types, 4367e8d8bef9SDimitry Andric options::OPT_feliminate_unused_debug_types, false)) 436806c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::UnusedTypeInfo; 4369e8d8bef9SDimitry Andric else if (NeedFullDebug) 437006c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::FullDebugInfo; 4371e8d8bef9SDimitry Andric } 43720b57cec5SDimitry Andric 43730b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, 43740b57cec5SDimitry Andric false)) { 43750b57cec5SDimitry Andric // Source embedding is a vendor extension to DWARF v5. By now we have 43760b57cec5SDimitry Andric // checked if a DWARF version was stated explicitly, and have otherwise 43770b57cec5SDimitry Andric // fallen back to the target default, so if this is still not at least 5 43780b57cec5SDimitry Andric // we emit an error. 43790b57cec5SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gembed_source); 4380e8d8bef9SDimitry Andric if (RequestedDWARFVersion < 5) 43810b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 43820b57cec5SDimitry Andric << A->getAsString(Args) << "-gdwarf-5"; 4383e8d8bef9SDimitry Andric else if (EffectiveDWARFVersion < 5) 4384e8d8bef9SDimitry Andric // The toolchain has reduced allowed dwarf version, so we can't enable 4385e8d8bef9SDimitry Andric // -gembed-source. 4386e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_dwarf_version_limited_by_target) 4387e8d8bef9SDimitry Andric << A->getAsString(Args) << TC.getTripleString() << 5 4388e8d8bef9SDimitry Andric << EffectiveDWARFVersion; 43890b57cec5SDimitry Andric else if (checkDebugInfoOption(A, Args, D, TC)) 43900b57cec5SDimitry Andric CmdArgs.push_back("-gembed-source"); 43910b57cec5SDimitry Andric } 43920b57cec5SDimitry Andric 43930b57cec5SDimitry Andric if (EmitCodeView) { 43940b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview"); 43950b57cec5SDimitry Andric 4396bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_gcodeview_ghash, 4397bdd1243dSDimitry Andric options::OPT_gno_codeview_ghash); 4398bdd1243dSDimitry Andric 4399bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_gcodeview_command_line, 4400bdd1243dSDimitry Andric options::OPT_gno_codeview_command_line); 44010b57cec5SDimitry Andric } 44020b57cec5SDimitry Andric 4403bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_ginline_line_tables, 4404bdd1243dSDimitry Andric options::OPT_gno_inline_line_tables); 4405480093f4SDimitry Andric 4406480093f4SDimitry Andric // When emitting remarks, we need at least debug lines in the output. 4407480093f4SDimitry Andric if (willEmitRemarks(Args) && 440806c3fb27SDimitry Andric DebugInfoKind <= llvm::codegenoptions::DebugDirectivesOnly) 440906c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::DebugLineTablesOnly; 4410480093f4SDimitry Andric 4411e8d8bef9SDimitry Andric // Adjust the debug info kind for the given toolchain. 4412e8d8bef9SDimitry Andric TC.adjustDebugInfoKind(DebugInfoKind, Args); 4413e8d8bef9SDimitry Andric 4414bdd1243dSDimitry Andric // On AIX, the debugger tuning option can be omitted if it is not explicitly 4415bdd1243dSDimitry Andric // set. 4416e8d8bef9SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion, 4417bdd1243dSDimitry Andric T.isOSAIX() && !HasDebuggerTuning 4418bdd1243dSDimitry Andric ? llvm::DebuggerKind::Default 4419bdd1243dSDimitry Andric : DebuggerTuning); 44200b57cec5SDimitry Andric 44210b57cec5SDimitry Andric // -fdebug-macro turns on macro debug info generation. 44220b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro, 44230b57cec5SDimitry Andric false)) 44240b57cec5SDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args, 44250b57cec5SDimitry Andric D, TC)) 44260b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-macro"); 44270b57cec5SDimitry Andric 44280b57cec5SDimitry Andric // -ggnu-pubnames turns on gnu style pubnames in the backend. 44290b57cec5SDimitry Andric const auto *PubnamesArg = 44300b57cec5SDimitry Andric Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames, 44310b57cec5SDimitry Andric options::OPT_gpubnames, options::OPT_gno_pubnames); 44320b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None || 44330b57cec5SDimitry Andric (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) 44340b57cec5SDimitry Andric if (!PubnamesArg || 44350b57cec5SDimitry Andric (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && 44360b57cec5SDimitry Andric !PubnamesArg->getOption().matches(options::OPT_gno_pubnames))) 44370b57cec5SDimitry Andric CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches( 44380b57cec5SDimitry Andric options::OPT_gpubnames) 44390b57cec5SDimitry Andric ? "-gpubnames" 44400b57cec5SDimitry Andric : "-ggnu-pubnames"); 4441349cc55cSDimitry Andric const auto *SimpleTemplateNamesArg = 444281ad6265SDimitry Andric Args.getLastArg(options::OPT_gsimple_template_names, 444381ad6265SDimitry Andric options::OPT_gno_simple_template_names); 4444349cc55cSDimitry Andric bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; 4445349cc55cSDimitry Andric if (SimpleTemplateNamesArg && 4446349cc55cSDimitry Andric checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { 4447349cc55cSDimitry Andric const auto &Opt = SimpleTemplateNamesArg->getOption(); 4448349cc55cSDimitry Andric if (Opt.matches(options::OPT_gsimple_template_names)) { 4449349cc55cSDimitry Andric ForwardTemplateParams = true; 4450349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=simple"); 4451349cc55cSDimitry Andric } 4452349cc55cSDimitry Andric } 44530b57cec5SDimitry Andric 4454bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gsrc_hash_EQ)) { 4455bdd1243dSDimitry Andric StringRef v = A->getValue(); 4456bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-gsrc-hash=" + v)); 44570b57cec5SDimitry Andric } 44580b57cec5SDimitry Andric 4459bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdebug_ranges_base_address, 4460bdd1243dSDimitry Andric options::OPT_fno_debug_ranges_base_address); 4461bdd1243dSDimitry Andric 44620b57cec5SDimitry Andric // -gdwarf-aranges turns on the emission of the aranges section in the 44630b57cec5SDimitry Andric // backend. 44640b57cec5SDimitry Andric // Always enabled for SCE tuning. 44650b57cec5SDimitry Andric bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE; 44660b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges)) 44670b57cec5SDimitry Andric NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges; 44680b57cec5SDimitry Andric if (NeedAranges) { 44690b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 44700b57cec5SDimitry Andric CmdArgs.push_back("-generate-arange-section"); 44710b57cec5SDimitry Andric } 44720b57cec5SDimitry Andric 4473bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_dwarf_frame, 4474bdd1243dSDimitry Andric options::OPT_fno_force_dwarf_frame); 4475480093f4SDimitry Andric 44760b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_types_section, 44770b57cec5SDimitry Andric options::OPT_fno_debug_types_section, false)) { 4478e8d8bef9SDimitry Andric if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) { 44790b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 44800b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fdebug_types_section) 44810b57cec5SDimitry Andric ->getAsString(Args) 44820b57cec5SDimitry Andric << T.getTriple(); 44830b57cec5SDimitry Andric } else if (checkDebugInfoOption( 44840b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_types_section), Args, D, 44850b57cec5SDimitry Andric TC)) { 44860b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 44870b57cec5SDimitry Andric CmdArgs.push_back("-generate-type-units"); 44880b57cec5SDimitry Andric } 44890b57cec5SDimitry Andric } 44900b57cec5SDimitry Andric 4491fe6060f1SDimitry Andric // To avoid join/split of directory+filename, the integrated assembler prefers 4492fe6060f1SDimitry Andric // the directory form of .file on all DWARF versions. GNU as doesn't allow the 4493fe6060f1SDimitry Andric // form before DWARF v5. 4494fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, 4495fe6060f1SDimitry Andric options::OPT_fno_dwarf_directory_asm, 4496fe6060f1SDimitry Andric TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) 4497fe6060f1SDimitry Andric CmdArgs.push_back("-fno-dwarf-directory-asm"); 4498fe6060f1SDimitry Andric 44990b57cec5SDimitry Andric // Decide how to render forward declarations of template instantiations. 45000b57cec5SDimitry Andric // SCE wants full descriptions, others just get them in the name. 4501349cc55cSDimitry Andric if (ForwardTemplateParams) 45020b57cec5SDimitry Andric CmdArgs.push_back("-debug-forward-template-params"); 45030b57cec5SDimitry Andric 45040b57cec5SDimitry Andric // Do we need to explicitly import anonymous namespaces into the parent 45050b57cec5SDimitry Andric // scope? 45060b57cec5SDimitry Andric if (DebuggerTuning == llvm::DebuggerKind::SCE) 45070b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-explicit-import"); 45080b57cec5SDimitry Andric 4509fe6060f1SDimitry Andric renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion); 45100b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); 45115f757f3fSDimitry Andric 45125f757f3fSDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 45135f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) { 45145f757f3fSDimitry Andric if (TC.getTriple().isOSBinFormatELF() || D.IsCLMode()) { 45155f757f3fSDimitry Andric if (DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor) 45165f757f3fSDimitry Andric CmdArgs.push_back("-fjmc"); 45175f757f3fSDimitry Andric else if (D.IsCLMode()) 45185f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC" 45195f757f3fSDimitry Andric << "'/Zi', '/Z7'"; 45205f757f3fSDimitry Andric else 45215f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc" 45225f757f3fSDimitry Andric << "-g"; 45235f757f3fSDimitry Andric } else { 45245f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_fjmc_for_elf_only); 45255f757f3fSDimitry Andric } 45265f757f3fSDimitry Andric } 45275f757f3fSDimitry Andric 45285f757f3fSDimitry Andric // Add in -fdebug-compilation-dir if necessary. 45295f757f3fSDimitry Andric const char *DebugCompilationDir = 45305f757f3fSDimitry Andric addDebugCompDirArg(Args, CmdArgs, D.getVFS()); 45315f757f3fSDimitry Andric 45325f757f3fSDimitry Andric addDebugPrefixMapArg(D, TC, Args, CmdArgs); 45335f757f3fSDimitry Andric 45345f757f3fSDimitry Andric // Add the output path to the object file for CodeView debug infos. 45355f757f3fSDimitry Andric if (EmitCodeView && Output.isFilename()) 45365f757f3fSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 45375f757f3fSDimitry Andric Output.getFilename()); 45380b57cec5SDimitry Andric } 45390b57cec5SDimitry Andric 4540bdd1243dSDimitry Andric static void ProcessVSRuntimeLibrary(const ArgList &Args, 4541bdd1243dSDimitry Andric ArgStringList &CmdArgs) { 4542bdd1243dSDimitry Andric unsigned RTOptionID = options::OPT__SLASH_MT; 4543bdd1243dSDimitry Andric 4544bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4545bdd1243dSDimitry Andric // The /LDd option implies /MTd. The dependent lib part can be overridden, 4546bdd1243dSDimitry Andric // but defining _DEBUG is sticky. 4547bdd1243dSDimitry Andric RTOptionID = options::OPT__SLASH_MTd; 4548bdd1243dSDimitry Andric 4549bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) 4550bdd1243dSDimitry Andric RTOptionID = A->getOption().getID(); 4551bdd1243dSDimitry Andric 4552bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { 4553bdd1243dSDimitry Andric RTOptionID = llvm::StringSwitch<unsigned>(A->getValue()) 4554bdd1243dSDimitry Andric .Case("static", options::OPT__SLASH_MT) 4555bdd1243dSDimitry Andric .Case("static_dbg", options::OPT__SLASH_MTd) 4556bdd1243dSDimitry Andric .Case("dll", options::OPT__SLASH_MD) 4557bdd1243dSDimitry Andric .Case("dll_dbg", options::OPT__SLASH_MDd) 4558bdd1243dSDimitry Andric .Default(options::OPT__SLASH_MT); 4559bdd1243dSDimitry Andric } 4560bdd1243dSDimitry Andric 4561bdd1243dSDimitry Andric StringRef FlagForCRT; 4562bdd1243dSDimitry Andric switch (RTOptionID) { 4563bdd1243dSDimitry Andric case options::OPT__SLASH_MD: 4564bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4565bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4566bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4567bdd1243dSDimitry Andric CmdArgs.push_back("-D_DLL"); 4568bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=msvcrt"; 4569bdd1243dSDimitry Andric break; 4570bdd1243dSDimitry Andric case options::OPT__SLASH_MDd: 4571bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4572bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4573bdd1243dSDimitry Andric CmdArgs.push_back("-D_DLL"); 4574bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=msvcrtd"; 4575bdd1243dSDimitry Andric break; 4576bdd1243dSDimitry Andric case options::OPT__SLASH_MT: 4577bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4578bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4579bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4580bdd1243dSDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 4581bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=libcmt"; 4582bdd1243dSDimitry Andric break; 4583bdd1243dSDimitry Andric case options::OPT__SLASH_MTd: 4584bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4585bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4586bdd1243dSDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 4587bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=libcmtd"; 4588bdd1243dSDimitry Andric break; 4589bdd1243dSDimitry Andric default: 4590bdd1243dSDimitry Andric llvm_unreachable("Unexpected option ID."); 4591bdd1243dSDimitry Andric } 4592bdd1243dSDimitry Andric 4593bdd1243dSDimitry Andric if (Args.hasArg(options::OPT_fms_omit_default_lib)) { 4594bdd1243dSDimitry Andric CmdArgs.push_back("-D_VC_NODEFAULTLIB"); 4595bdd1243dSDimitry Andric } else { 4596bdd1243dSDimitry Andric CmdArgs.push_back(FlagForCRT.data()); 4597bdd1243dSDimitry Andric 4598bdd1243dSDimitry Andric // This provides POSIX compatibility (maps 'open' to '_open'), which most 4599bdd1243dSDimitry Andric // users want. The /Za flag to cl.exe turns this off, but it's not 4600bdd1243dSDimitry Andric // implemented in clang. 4601bdd1243dSDimitry Andric CmdArgs.push_back("--dependent-lib=oldnames"); 4602bdd1243dSDimitry Andric } 4603bdd1243dSDimitry Andric } 4604bdd1243dSDimitry Andric 46050b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA, 46060b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 46070b57cec5SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 46080b57cec5SDimitry Andric const auto &TC = getToolChain(); 46090b57cec5SDimitry Andric const llvm::Triple &RawTriple = TC.getTriple(); 46100b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 46110b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 46120b57cec5SDimitry Andric 46130b57cec5SDimitry Andric bool KernelOrKext = 46140b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 46150b57cec5SDimitry Andric const Driver &D = TC.getDriver(); 46160b57cec5SDimitry Andric ArgStringList CmdArgs; 46170b57cec5SDimitry Andric 46180b57cec5SDimitry Andric assert(Inputs.size() >= 1 && "Must have at least one input."); 46190b57cec5SDimitry Andric // CUDA/HIP compilation may have multiple inputs (source file + results of 46200b57cec5SDimitry Andric // device-side compilations). OpenMP device jobs also take the host IR as a 46210b57cec5SDimitry Andric // second input. Module precompilation accepts a list of header files to 462281ad6265SDimitry Andric // include as part of the module. API extraction accepts a list of header 462381ad6265SDimitry Andric // files whose API information is emitted in the output. All other jobs are 462481ad6265SDimitry Andric // expected to have exactly one input. 46250b57cec5SDimitry Andric bool IsCuda = JA.isOffloading(Action::OFK_Cuda); 4626fe6060f1SDimitry Andric bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda); 46270b57cec5SDimitry Andric bool IsHIP = JA.isOffloading(Action::OFK_HIP); 4628fe6060f1SDimitry Andric bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP); 46290b57cec5SDimitry Andric bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); 463081ad6265SDimitry Andric bool IsExtractAPI = isa<ExtractAPIJobAction>(JA); 4631fe6060f1SDimitry Andric bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || 4632fe6060f1SDimitry Andric JA.isDeviceOffloading(Action::OFK_Host)); 463381ad6265SDimitry Andric bool IsHostOffloadingAction = 4634bdd1243dSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP) || 463581ad6265SDimitry Andric (JA.isHostOffloading(C.getActiveOffloadKinds()) && 463681ad6265SDimitry Andric Args.hasFlag(options::OPT_offload_new_driver, 463781ad6265SDimitry Andric options::OPT_no_offload_new_driver, false)); 463881ad6265SDimitry Andric 4639753f127fSDimitry Andric bool IsRDCMode = 4640753f127fSDimitry Andric Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false); 4641fe6060f1SDimitry Andric bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction); 4642fe6060f1SDimitry Andric auto LTOMode = D.getLTOMode(IsDeviceOffloadAction); 46430b57cec5SDimitry Andric 464481ad6265SDimitry Andric // Extract API doesn't have a main input file, so invent a fake one as a 464581ad6265SDimitry Andric // placeholder. 464681ad6265SDimitry Andric InputInfo ExtractAPIPlaceholderInput(Inputs[0].getType(), "extract-api", 464781ad6265SDimitry Andric "extract-api"); 464881ad6265SDimitry Andric 4649bdd1243dSDimitry Andric const InputInfo &Input = 4650bdd1243dSDimitry Andric IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0]; 46510b57cec5SDimitry Andric 465281ad6265SDimitry Andric InputInfoList ExtractAPIInputs; 465381ad6265SDimitry Andric InputInfoList HostOffloadingInputs; 46540b57cec5SDimitry Andric const InputInfo *CudaDeviceInput = nullptr; 46550b57cec5SDimitry Andric const InputInfo *OpenMPDeviceInput = nullptr; 46560b57cec5SDimitry Andric for (const InputInfo &I : Inputs) { 4657bdd1243dSDimitry Andric if (&I == &Input || I.getType() == types::TY_Nothing) { 4658bdd1243dSDimitry Andric // This is the primary input or contains nothing. 465981ad6265SDimitry Andric } else if (IsExtractAPI) { 466081ad6265SDimitry Andric auto ExpectedInputType = ExtractAPIPlaceholderInput.getType(); 466181ad6265SDimitry Andric if (I.getType() != ExpectedInputType) { 466281ad6265SDimitry Andric D.Diag(diag::err_drv_extract_api_wrong_kind) 466381ad6265SDimitry Andric << I.getFilename() << types::getTypeName(I.getType()) 466481ad6265SDimitry Andric << types::getTypeName(ExpectedInputType); 466581ad6265SDimitry Andric } 466681ad6265SDimitry Andric ExtractAPIInputs.push_back(I); 466781ad6265SDimitry Andric } else if (IsHostOffloadingAction) { 466881ad6265SDimitry Andric HostOffloadingInputs.push_back(I); 46690b57cec5SDimitry Andric } else if ((IsCuda || IsHIP) && !CudaDeviceInput) { 46700b57cec5SDimitry Andric CudaDeviceInput = &I; 46710b57cec5SDimitry Andric } else if (IsOpenMPDevice && !OpenMPDeviceInput) { 46720b57cec5SDimitry Andric OpenMPDeviceInput = &I; 46730b57cec5SDimitry Andric } else { 46740b57cec5SDimitry Andric llvm_unreachable("unexpectedly given multiple inputs"); 46750b57cec5SDimitry Andric } 46760b57cec5SDimitry Andric } 46770b57cec5SDimitry Andric 46785ffd83dbSDimitry Andric const llvm::Triple *AuxTriple = 46795ffd83dbSDimitry Andric (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr; 46800b57cec5SDimitry Andric bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment(); 46810b57cec5SDimitry Andric bool IsIAMCU = RawTriple.isOSIAMCU(); 46820b57cec5SDimitry Andric 46830b57cec5SDimitry Andric // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in 46840b57cec5SDimitry Andric // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not 46850b57cec5SDimitry Andric // Windows), we need to pass Windows-specific flags to cc1. 46860b57cec5SDimitry Andric if (IsCuda || IsHIP) 46870b57cec5SDimitry Andric IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment(); 46880b57cec5SDimitry Andric 46890b57cec5SDimitry Andric // C++ is not supported for IAMCU. 46900b57cec5SDimitry Andric if (IsIAMCU && types::isCXX(Input.getType())) 46910b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU"; 46920b57cec5SDimitry Andric 46930b57cec5SDimitry Andric // Invoke ourselves in -cc1 mode. 46940b57cec5SDimitry Andric // 46950b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 46960b57cec5SDimitry Andric CmdArgs.push_back("-cc1"); 46970b57cec5SDimitry Andric 46980b57cec5SDimitry Andric // Add the "effective" target triple. 46990b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 47000b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 47010b57cec5SDimitry Andric 47020b57cec5SDimitry Andric if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) { 47030b57cec5SDimitry Andric DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args); 47040b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_MJ); 4705a7dea167SDimitry Andric } else if (const Arg *GenCDBFragment = 4706a7dea167SDimitry Andric Args.getLastArg(options::OPT_gen_cdb_fragment_path)) { 4707a7dea167SDimitry Andric DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C, 4708a7dea167SDimitry Andric TripleStr, Output, Input, Args); 4709a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path); 47100b57cec5SDimitry Andric } 47110b57cec5SDimitry Andric 47120b57cec5SDimitry Andric if (IsCuda || IsHIP) { 47130b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for a CUDA/HIP device 47140b57cec5SDimitry Andric // and vice-versa. 47150b57cec5SDimitry Andric std::string NormalizedTriple; 47160b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 47170b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 47180b57cec5SDimitry Andric NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>() 47190b57cec5SDimitry Andric ->getTriple() 47200b57cec5SDimitry Andric .normalize(); 47210b57cec5SDimitry Andric else { 47220b57cec5SDimitry Andric // Host-side compilation. 47230b57cec5SDimitry Andric NormalizedTriple = 47240b57cec5SDimitry Andric (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() 47250b57cec5SDimitry Andric : C.getSingleOffloadToolChain<Action::OFK_HIP>()) 47260b57cec5SDimitry Andric ->getTriple() 47270b57cec5SDimitry Andric .normalize(); 47280b57cec5SDimitry Andric if (IsCuda) { 47290b57cec5SDimitry Andric // We need to figure out which CUDA version we're compiling for, as that 47300b57cec5SDimitry Andric // determines how we load and launch GPU kernels. 47310b57cec5SDimitry Andric auto *CTC = static_cast<const toolchains::CudaToolChain *>( 47320b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 47330b57cec5SDimitry Andric assert(CTC && "Expected valid CUDA Toolchain."); 47340b57cec5SDimitry Andric if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN) 47350b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 47360b57cec5SDimitry Andric Twine("-target-sdk-version=") + 47370b57cec5SDimitry Andric CudaVersionToString(CTC->CudaInstallation.version()))); 473806c3fb27SDimitry Andric // Unsized function arguments used for variadics were introduced in 473906c3fb27SDimitry Andric // CUDA-9.0. We still do not support generating code that actually uses 474006c3fb27SDimitry Andric // variadic arguments yet, but we do need to allow parsing them as 474106c3fb27SDimitry Andric // recent CUDA headers rely on that. 474206c3fb27SDimitry Andric // https://github.com/llvm/llvm-project/issues/58410 474306c3fb27SDimitry Andric if (CTC->CudaInstallation.version() >= CudaVersion::CUDA_90) 474406c3fb27SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 47450b57cec5SDimitry Andric } 47460b57cec5SDimitry Andric } 47470b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 47480b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 474906c3fb27SDimitry Andric 475006c3fb27SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_HIP) && 475106c3fb27SDimitry Andric getToolChain().getTriple().isAMDGPU()) { 475206c3fb27SDimitry Andric // Device side compilation printf 475306c3fb27SDimitry Andric if (Args.getLastArg(options::OPT_mprintf_kind_EQ)) { 475406c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 475506c3fb27SDimitry Andric "-mprintf-kind=" + 475606c3fb27SDimitry Andric Args.getLastArgValue(options::OPT_mprintf_kind_EQ))); 475706c3fb27SDimitry Andric // Force compiler error on invalid conversion specifiers 475806c3fb27SDimitry Andric CmdArgs.push_back( 475906c3fb27SDimitry Andric Args.MakeArgString("-Werror=format-invalid-specifier")); 47600b57cec5SDimitry Andric } 476106c3fb27SDimitry Andric } 476206c3fb27SDimitry Andric } 476306c3fb27SDimitry Andric 476406c3fb27SDimitry Andric // Unconditionally claim the printf option now to avoid unused diagnostic. 476506c3fb27SDimitry Andric if (const Arg *PF = Args.getLastArg(options::OPT_mprintf_kind_EQ)) 476606c3fb27SDimitry Andric PF->claim(); 47670b57cec5SDimitry Andric 47685ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { 47695ffd83dbSDimitry Andric CmdArgs.push_back("-fsycl-is-device"); 47705ffd83dbSDimitry Andric 47715ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { 47725ffd83dbSDimitry Andric A->render(Args, CmdArgs); 47735ffd83dbSDimitry Andric } else { 4774fe6060f1SDimitry Andric // Ensure the default version in SYCL mode is 2020. 4775fe6060f1SDimitry Andric CmdArgs.push_back("-sycl-std=2020"); 47765ffd83dbSDimitry Andric } 47775ffd83dbSDimitry Andric } 47785ffd83dbSDimitry Andric 47790b57cec5SDimitry Andric if (IsOpenMPDevice) { 47800b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for an OpenMP device. 47810b57cec5SDimitry Andric std::string NormalizedTriple = 47820b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Host>() 47830b57cec5SDimitry Andric ->getTriple() 47840b57cec5SDimitry Andric .normalize(); 47850b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 47860b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 47870b57cec5SDimitry Andric } 47880b57cec5SDimitry Andric 47890b57cec5SDimitry Andric if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm || 47900b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::thumb)) { 47910b57cec5SDimitry Andric unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6; 47925ffd83dbSDimitry Andric unsigned Version = 0; 47935ffd83dbSDimitry Andric bool Failure = 47945ffd83dbSDimitry Andric Triple.getArchName().substr(Offset).consumeInteger(10, Version); 47955ffd83dbSDimitry Andric if (Failure || Version < 7) 47960b57cec5SDimitry Andric D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName() 47970b57cec5SDimitry Andric << TripleStr; 47980b57cec5SDimitry Andric } 47990b57cec5SDimitry Andric 48000b57cec5SDimitry Andric // Push all default warning arguments that are specific to 48010b57cec5SDimitry Andric // the given target. These come before user provided warning options 48020b57cec5SDimitry Andric // are provided. 48030b57cec5SDimitry Andric TC.addClangWarningOptions(CmdArgs); 48040b57cec5SDimitry Andric 4805fe6060f1SDimitry Andric // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions. 4806bdd1243dSDimitry Andric if (Triple.isSPIR() || Triple.isSPIRV()) 4807fe6060f1SDimitry Andric CmdArgs.push_back("-Wspir-compat"); 4808fe6060f1SDimitry Andric 48090b57cec5SDimitry Andric // Select the appropriate action. 48100b57cec5SDimitry Andric RewriteKind rewriteKind = RK_None; 48110b57cec5SDimitry Andric 481206c3fb27SDimitry Andric bool UnifiedLTO = false; 481306c3fb27SDimitry Andric if (IsUsingLTO) { 481406c3fb27SDimitry Andric UnifiedLTO = Args.hasFlag(options::OPT_funified_lto, 48155f757f3fSDimitry Andric options::OPT_fno_unified_lto, Triple.isPS()) || 48165f757f3fSDimitry Andric Args.hasFlag(options::OPT_ffat_lto_objects, 48175f757f3fSDimitry Andric options::OPT_fno_fat_lto_objects, false); 481806c3fb27SDimitry Andric if (UnifiedLTO) 481906c3fb27SDimitry Andric CmdArgs.push_back("-funified-lto"); 482006c3fb27SDimitry Andric } 482106c3fb27SDimitry Andric 4822a7dea167SDimitry Andric // If CollectArgsForIntegratedAssembler() isn't called below, claim the args 4823a7dea167SDimitry Andric // it claims when not running an assembler. Otherwise, clang would emit 4824a7dea167SDimitry Andric // "argument unused" warnings for assembler flags when e.g. adding "-E" to 4825a7dea167SDimitry Andric // flags while debugging something. That'd be somewhat inconvenient, and it's 4826a7dea167SDimitry Andric // also inconsistent with most other flags -- we don't warn on 4827a7dea167SDimitry Andric // -ffunction-sections not being used in -E mode either for example, even 4828a7dea167SDimitry Andric // though it's not really used either. 4829a7dea167SDimitry Andric if (!isa<AssembleJobAction>(JA)) { 4830a7dea167SDimitry Andric // The args claimed here should match the args used in 4831a7dea167SDimitry Andric // CollectArgsForIntegratedAssembler(). 4832a7dea167SDimitry Andric if (TC.useIntegratedAs()) { 4833a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mrelax_all); 4834a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_relax_all); 4835a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible); 4836a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible); 4837a7dea167SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 4838a7dea167SDimitry Andric case llvm::Triple::arm: 4839a7dea167SDimitry Andric case llvm::Triple::armeb: 4840a7dea167SDimitry Andric case llvm::Triple::thumb: 4841a7dea167SDimitry Andric case llvm::Triple::thumbeb: 4842a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ); 4843a7dea167SDimitry Andric break; 4844a7dea167SDimitry Andric default: 4845a7dea167SDimitry Andric break; 4846a7dea167SDimitry Andric } 4847a7dea167SDimitry Andric } 4848a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Wa_COMMA); 4849a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Xassembler); 485081ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_femit_dwarf_unwind_EQ); 4851a7dea167SDimitry Andric } 4852a7dea167SDimitry Andric 48530b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) { 48540b57cec5SDimitry Andric assert(JA.getType() == types::TY_Plist && "Invalid output type."); 48550b57cec5SDimitry Andric CmdArgs.push_back("-analyze"); 48560b57cec5SDimitry Andric } else if (isa<MigrateJobAction>(JA)) { 48570b57cec5SDimitry Andric CmdArgs.push_back("-migrate"); 48580b57cec5SDimitry Andric } else if (isa<PreprocessJobAction>(JA)) { 48590b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) 48600b57cec5SDimitry Andric CmdArgs.push_back("-Eonly"); 48610b57cec5SDimitry Andric else { 48620b57cec5SDimitry Andric CmdArgs.push_back("-E"); 48630b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc) && 48640b57cec5SDimitry Andric !Args.hasArg(options::OPT_g_Group)) 48650b57cec5SDimitry Andric CmdArgs.push_back("-P"); 486681ad6265SDimitry Andric else if (JA.getType() == types::TY_PP_CXXHeaderUnit) 486781ad6265SDimitry Andric CmdArgs.push_back("-fdirectives-only"); 48680b57cec5SDimitry Andric } 48690b57cec5SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 48700b57cec5SDimitry Andric CmdArgs.push_back("-emit-obj"); 48710b57cec5SDimitry Andric 48720b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 48730b57cec5SDimitry Andric 48740b57cec5SDimitry Andric // Also ignore explicit -force_cpusubtype_ALL option. 48750b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 48760b57cec5SDimitry Andric } else if (isa<PrecompileJobAction>(JA)) { 48770b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) 48780b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 48790b57cec5SDimitry Andric else if (JA.getType() == types::TY_ModuleFile) 4880bdd1243dSDimitry Andric CmdArgs.push_back("-emit-module-interface"); 488181ad6265SDimitry Andric else if (JA.getType() == types::TY_HeaderUnit) 488281ad6265SDimitry Andric CmdArgs.push_back("-emit-header-unit"); 48830b57cec5SDimitry Andric else 48840b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 48850b57cec5SDimitry Andric } else if (isa<VerifyPCHJobAction>(JA)) { 48860b57cec5SDimitry Andric CmdArgs.push_back("-verify-pch"); 488781ad6265SDimitry Andric } else if (isa<ExtractAPIJobAction>(JA)) { 488881ad6265SDimitry Andric assert(JA.getType() == types::TY_API_INFO && 488981ad6265SDimitry Andric "Extract API actions must generate a API information."); 489081ad6265SDimitry Andric CmdArgs.push_back("-extract-api"); 489181ad6265SDimitry Andric if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ)) 489281ad6265SDimitry Andric ProductNameArg->render(Args, CmdArgs); 4893bdd1243dSDimitry Andric if (Arg *ExtractAPIIgnoresFileArg = 4894bdd1243dSDimitry Andric Args.getLastArg(options::OPT_extract_api_ignores_EQ)) 4895bdd1243dSDimitry Andric ExtractAPIIgnoresFileArg->render(Args, CmdArgs); 48960b57cec5SDimitry Andric } else { 48970b57cec5SDimitry Andric assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) && 48980b57cec5SDimitry Andric "Invalid action for clang tool."); 48990b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) { 49000b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 49010b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 49020b57cec5SDimitry Andric JA.getType() == types::TY_LTO_IR) { 49030b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm"); 49040b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 49050b57cec5SDimitry Andric JA.getType() == types::TY_LTO_BC) { 4906fe6060f1SDimitry Andric // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S 4907fe6060f1SDimitry Andric if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) && 4908fe6060f1SDimitry Andric Args.hasArg(options::OPT_emit_llvm)) { 4909fe6060f1SDimitry Andric CmdArgs.push_back("-emit-llvm"); 4910fe6060f1SDimitry Andric } else { 49110b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 4912fe6060f1SDimitry Andric } 4913a7dea167SDimitry Andric } else if (JA.getType() == types::TY_IFS || 4914a7dea167SDimitry Andric JA.getType() == types::TY_IFS_CPP) { 4915a7dea167SDimitry Andric StringRef ArgStr = 4916a7dea167SDimitry Andric Args.hasArg(options::OPT_interface_stub_version_EQ) 4917a7dea167SDimitry Andric ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ) 4918fe6060f1SDimitry Andric : "ifs-v1"; 49190b57cec5SDimitry Andric CmdArgs.push_back("-emit-interface-stubs"); 49200b57cec5SDimitry Andric CmdArgs.push_back( 4921a7dea167SDimitry Andric Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str())); 49220b57cec5SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 49230b57cec5SDimitry Andric CmdArgs.push_back("-S"); 49240b57cec5SDimitry Andric } else if (JA.getType() == types::TY_AST) { 49250b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 49260b57cec5SDimitry Andric } else if (JA.getType() == types::TY_ModuleFile) { 49270b57cec5SDimitry Andric CmdArgs.push_back("-module-file-info"); 49280b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenObjC) { 49290b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 49300b57cec5SDimitry Andric rewriteKind = RK_NonFragile; 49310b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 49320b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 49330b57cec5SDimitry Andric rewriteKind = RK_Fragile; 49340b57cec5SDimitry Andric } else { 49350b57cec5SDimitry Andric assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!"); 49360b57cec5SDimitry Andric } 49370b57cec5SDimitry Andric 49380b57cec5SDimitry Andric // Preserve use-list order by default when emitting bitcode, so that 49390b57cec5SDimitry Andric // loading the bitcode up in 'opt' or 'llc' and running passes gives the 49400b57cec5SDimitry Andric // same result as running passes here. For LTO, we don't need to preserve 49410b57cec5SDimitry Andric // the use-list order, since serialization to bitcode is part of the flow. 49420b57cec5SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 49430b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-uselists"); 49440b57cec5SDimitry Andric 494581ad6265SDimitry Andric if (IsUsingLTO) { 4946bdd1243dSDimitry Andric if (IsDeviceOffloadAction && !JA.isDeviceOffloading(Action::OFK_OpenMP) && 494781ad6265SDimitry Andric !Args.hasFlag(options::OPT_offload_new_driver, 494881ad6265SDimitry Andric options::OPT_no_offload_new_driver, false) && 494981ad6265SDimitry Andric !Triple.isAMDGPU()) { 4950fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4951fe6060f1SDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4952fe6060f1SDimitry Andric options::OPT_foffload_lto_EQ) 4953fe6060f1SDimitry Andric ->getAsString(Args) 4954fe6060f1SDimitry Andric << Triple.getTriple(); 4955bdd1243dSDimitry Andric } else if (Triple.isNVPTX() && !IsRDCMode && 4956bdd1243dSDimitry Andric JA.isDeviceOffloading(Action::OFK_Cuda)) { 4957bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_language_mode) 4958bdd1243dSDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4959bdd1243dSDimitry Andric options::OPT_foffload_lto_EQ) 4960bdd1243dSDimitry Andric ->getAsString(Args) 4961bdd1243dSDimitry Andric << "-fno-gpu-rdc"; 4962349cc55cSDimitry Andric } else { 4963349cc55cSDimitry Andric assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); 4964349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 4965349cc55cSDimitry Andric Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 496606c3fb27SDimitry Andric // PS4 uses the legacy LTO API, which does not support some of the 496706c3fb27SDimitry Andric // features enabled by -flto-unit. 496806c3fb27SDimitry Andric if (!RawTriple.isPS4() || 496906c3fb27SDimitry Andric (D.getLTOMode() == LTOK_Full) || !UnifiedLTO) 4970349cc55cSDimitry Andric CmdArgs.push_back("-flto-unit"); 4971fe6060f1SDimitry Andric } 49720b57cec5SDimitry Andric } 49730b57cec5SDimitry Andric } 49740b57cec5SDimitry Andric 497506c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dumpdir); 497606c3fb27SDimitry Andric 49770b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) { 49780b57cec5SDimitry Andric if (!types::isLLVMIR(Input.getType())) 49790b57cec5SDimitry Andric D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args); 49800b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ); 49810b57cec5SDimitry Andric } 49820b57cec5SDimitry Andric 49835f757f3fSDimitry Andric if (Triple.isPPC()) 49845f757f3fSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mregnames, 49855f757f3fSDimitry Andric options::OPT_mno_regnames); 49865f757f3fSDimitry Andric 4987480093f4SDimitry Andric if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ)) 4988480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ); 4989480093f4SDimitry Andric 49900b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 49910b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ); 49920b57cec5SDimitry Andric 4993e8d8bef9SDimitry Andric auto *MemProfArg = Args.getLastArg(options::OPT_fmemory_profile, 4994e8d8bef9SDimitry Andric options::OPT_fmemory_profile_EQ, 4995e8d8bef9SDimitry Andric options::OPT_fno_memory_profile); 4996e8d8bef9SDimitry Andric if (MemProfArg && 4997e8d8bef9SDimitry Andric !MemProfArg->getOption().matches(options::OPT_fno_memory_profile)) 4998e8d8bef9SDimitry Andric MemProfArg->render(Args, CmdArgs); 4999e8d8bef9SDimitry Andric 500006c3fb27SDimitry Andric if (auto *MemProfUseArg = 500106c3fb27SDimitry Andric Args.getLastArg(options::OPT_fmemory_profile_use_EQ)) { 500206c3fb27SDimitry Andric if (MemProfArg) 500306c3fb27SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 500406c3fb27SDimitry Andric << MemProfUseArg->getAsString(Args) << MemProfArg->getAsString(Args); 500506c3fb27SDimitry Andric if (auto *PGOInstrArg = Args.getLastArg(options::OPT_fprofile_generate, 500606c3fb27SDimitry Andric options::OPT_fprofile_generate_EQ)) 500706c3fb27SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 500806c3fb27SDimitry Andric << MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args); 500906c3fb27SDimitry Andric MemProfUseArg->render(Args, CmdArgs); 501006c3fb27SDimitry Andric } 501106c3fb27SDimitry Andric 50120b57cec5SDimitry Andric // Embed-bitcode option. 50130b57cec5SDimitry Andric // Only white-listed flags below are allowed to be embedded. 5014fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO && 50150b57cec5SDimitry Andric (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) { 50160b57cec5SDimitry Andric // Add flags implied by -fembed-bitcode. 50170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 50180b57cec5SDimitry Andric // Disable all llvm IR level optimizations. 50190b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 50200b57cec5SDimitry Andric 5021480093f4SDimitry Andric // Render target options. 50220b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 50230b57cec5SDimitry Andric 50240b57cec5SDimitry Andric // reject options that shouldn't be supported in bitcode 50250b57cec5SDimitry Andric // also reject kernel/kext 5026349cc55cSDimitry Andric static const constexpr unsigned kBitcodeOptionIgnorelist[] = { 50270b57cec5SDimitry Andric options::OPT_mkernel, 50280b57cec5SDimitry Andric options::OPT_fapple_kext, 50290b57cec5SDimitry Andric options::OPT_ffunction_sections, 50300b57cec5SDimitry Andric options::OPT_fno_function_sections, 50310b57cec5SDimitry Andric options::OPT_fdata_sections, 50320b57cec5SDimitry Andric options::OPT_fno_data_sections, 50335ffd83dbSDimitry Andric options::OPT_fbasic_block_sections_EQ, 50345ffd83dbSDimitry Andric options::OPT_funique_internal_linkage_names, 50355ffd83dbSDimitry Andric options::OPT_fno_unique_internal_linkage_names, 50360b57cec5SDimitry Andric options::OPT_funique_section_names, 50370b57cec5SDimitry Andric options::OPT_fno_unique_section_names, 50385ffd83dbSDimitry Andric options::OPT_funique_basic_block_section_names, 50395ffd83dbSDimitry Andric options::OPT_fno_unique_basic_block_section_names, 50400b57cec5SDimitry Andric options::OPT_mrestrict_it, 50410b57cec5SDimitry Andric options::OPT_mno_restrict_it, 50420b57cec5SDimitry Andric options::OPT_mstackrealign, 50430b57cec5SDimitry Andric options::OPT_mno_stackrealign, 50440b57cec5SDimitry Andric options::OPT_mstack_alignment, 50450b57cec5SDimitry Andric options::OPT_mcmodel_EQ, 50460b57cec5SDimitry Andric options::OPT_mlong_calls, 50470b57cec5SDimitry Andric options::OPT_mno_long_calls, 50480b57cec5SDimitry Andric options::OPT_ggnu_pubnames, 50490b57cec5SDimitry Andric options::OPT_gdwarf_aranges, 50500b57cec5SDimitry Andric options::OPT_fdebug_types_section, 50510b57cec5SDimitry Andric options::OPT_fno_debug_types_section, 50520b57cec5SDimitry Andric options::OPT_fdwarf_directory_asm, 50530b57cec5SDimitry Andric options::OPT_fno_dwarf_directory_asm, 50540b57cec5SDimitry Andric options::OPT_mrelax_all, 50550b57cec5SDimitry Andric options::OPT_mno_relax_all, 50560b57cec5SDimitry Andric options::OPT_ftrap_function_EQ, 50570b57cec5SDimitry Andric options::OPT_ffixed_r9, 50580b57cec5SDimitry Andric options::OPT_mfix_cortex_a53_835769, 50590b57cec5SDimitry Andric options::OPT_mno_fix_cortex_a53_835769, 50600b57cec5SDimitry Andric options::OPT_ffixed_x18, 50610b57cec5SDimitry Andric options::OPT_mglobal_merge, 50620b57cec5SDimitry Andric options::OPT_mno_global_merge, 50630b57cec5SDimitry Andric options::OPT_mred_zone, 50640b57cec5SDimitry Andric options::OPT_mno_red_zone, 50650b57cec5SDimitry Andric options::OPT_Wa_COMMA, 50660b57cec5SDimitry Andric options::OPT_Xassembler, 50670b57cec5SDimitry Andric options::OPT_mllvm, 50680b57cec5SDimitry Andric }; 50690b57cec5SDimitry Andric for (const auto &A : Args) 5070349cc55cSDimitry Andric if (llvm::is_contained(kBitcodeOptionIgnorelist, A->getOption().getID())) 50710b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling(); 50720b57cec5SDimitry Andric 50730b57cec5SDimitry Andric // Render the CodeGen options that need to be passed. 507481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 507581ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 50760b57cec5SDimitry Andric 50770b57cec5SDimitry Andric RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args, 50785ffd83dbSDimitry Andric CmdArgs, JA); 50790b57cec5SDimitry Andric 50800b57cec5SDimitry Andric // Render ABI arguments 50810b57cec5SDimitry Andric switch (TC.getArch()) { 50820b57cec5SDimitry Andric default: break; 50830b57cec5SDimitry Andric case llvm::Triple::arm: 50840b57cec5SDimitry Andric case llvm::Triple::armeb: 50850b57cec5SDimitry Andric case llvm::Triple::thumbeb: 5086349cc55cSDimitry Andric RenderARMABI(D, Triple, Args, CmdArgs); 50870b57cec5SDimitry Andric break; 50880b57cec5SDimitry Andric case llvm::Triple::aarch64: 5089480093f4SDimitry Andric case llvm::Triple::aarch64_32: 50900b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 50910b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 50920b57cec5SDimitry Andric break; 50930b57cec5SDimitry Andric } 50940b57cec5SDimitry Andric 50950b57cec5SDimitry Andric // Optimization level for CodeGen. 50960b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 50970b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 50980b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 50990b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 51000b57cec5SDimitry Andric } else { 51010b57cec5SDimitry Andric A->render(Args, CmdArgs); 51020b57cec5SDimitry Andric } 51030b57cec5SDimitry Andric } 51040b57cec5SDimitry Andric 51050b57cec5SDimitry Andric // Input/Output file. 51060b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 51070b57cec5SDimitry Andric // Handled with other dependency code. 51080b57cec5SDimitry Andric } else if (Output.isFilename()) { 51090b57cec5SDimitry Andric CmdArgs.push_back("-o"); 51100b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 51110b57cec5SDimitry Andric } else { 51120b57cec5SDimitry Andric assert(Output.isNothing() && "Input output."); 51130b57cec5SDimitry Andric } 51140b57cec5SDimitry Andric 51150b57cec5SDimitry Andric for (const auto &II : Inputs) { 51160b57cec5SDimitry Andric addDashXForInput(Args, II, CmdArgs); 51170b57cec5SDimitry Andric if (II.isFilename()) 51180b57cec5SDimitry Andric CmdArgs.push_back(II.getFilename()); 51190b57cec5SDimitry Andric else 51200b57cec5SDimitry Andric II.getInputArg().renderAsInput(Args, CmdArgs); 51210b57cec5SDimitry Andric } 51220b57cec5SDimitry Andric 5123e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>( 5124e8d8bef9SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(), 512506c3fb27SDimitry Andric CmdArgs, Inputs, Output, D.getPrependArg())); 51260b57cec5SDimitry Andric return; 51270b57cec5SDimitry Andric } 51280b57cec5SDimitry Andric 5129fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO) 51300b57cec5SDimitry Andric CmdArgs.push_back("-fembed-bitcode=marker"); 51310b57cec5SDimitry Andric 51320b57cec5SDimitry Andric // We normally speed up the clang process a bit by skipping destructors at 51330b57cec5SDimitry Andric // exit, but when we're generating diagnostics we can rely on some of the 51340b57cec5SDimitry Andric // cleanup. 51350b57cec5SDimitry Andric if (!C.isForDiagnostics()) 51360b57cec5SDimitry Andric CmdArgs.push_back("-disable-free"); 5137349cc55cSDimitry Andric CmdArgs.push_back("-clear-ast-before-backend"); 51380b57cec5SDimitry Andric 51390b57cec5SDimitry Andric #ifdef NDEBUG 51400b57cec5SDimitry Andric const bool IsAssertBuild = false; 51410b57cec5SDimitry Andric #else 51420b57cec5SDimitry Andric const bool IsAssertBuild = true; 51430b57cec5SDimitry Andric #endif 51440b57cec5SDimitry Andric 51455f757f3fSDimitry Andric // Disable the verification pass in asserts builds unless otherwise specified. 51465f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fno_verify_intermediate_code, 51475f757f3fSDimitry Andric options::OPT_fverify_intermediate_code, !IsAssertBuild)) { 51480b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-verifier"); 51495f757f3fSDimitry Andric } 51500b57cec5SDimitry Andric 51510b57cec5SDimitry Andric // Discard value names in assert builds unless otherwise specified. 51520b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdiscard_value_names, 515347395794SDimitry Andric options::OPT_fno_discard_value_names, !IsAssertBuild)) { 515447395794SDimitry Andric if (Args.hasArg(options::OPT_fdiscard_value_names) && 5155349cc55cSDimitry Andric llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) { 515647395794SDimitry Andric return types::isLLVMIR(II.getType()); 5157349cc55cSDimitry Andric })) { 515847395794SDimitry Andric D.Diag(diag::warn_ignoring_fdiscard_for_bitcode); 515947395794SDimitry Andric } 51600b57cec5SDimitry Andric CmdArgs.push_back("-discard-value-names"); 516147395794SDimitry Andric } 51620b57cec5SDimitry Andric 51630b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 51640b57cec5SDimitry Andric // -save-temps. 51650b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 51660b57cec5SDimitry Andric CmdArgs.push_back(getBaseInputName(Args, Input)); 51670b57cec5SDimitry Andric 51680b57cec5SDimitry Andric // Some flags which affect the language (via preprocessor 51690b57cec5SDimitry Andric // defines). 51700b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 51710b57cec5SDimitry Andric CmdArgs.push_back("-static-define"); 51720b57cec5SDimitry Andric 51730b57cec5SDimitry Andric if (Args.hasArg(options::OPT_municode)) 51740b57cec5SDimitry Andric CmdArgs.push_back("-DUNICODE"); 51750b57cec5SDimitry Andric 51760b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) 51770b57cec5SDimitry Andric RenderAnalyzerOptions(Args, CmdArgs, Triple, Input); 51780b57cec5SDimitry Andric 5179a7dea167SDimitry Andric if (isa<AnalyzeJobAction>(JA) || 5180a7dea167SDimitry Andric (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze))) 5181a7dea167SDimitry Andric CmdArgs.push_back("-setup-static-analyzer"); 5182a7dea167SDimitry Andric 51830b57cec5SDimitry Andric // Enable compatilibily mode to avoid analyzer-config related errors. 51840b57cec5SDimitry Andric // Since we can't access frontend flags through hasArg, let's manually iterate 51850b57cec5SDimitry Andric // through them. 51860b57cec5SDimitry Andric bool FoundAnalyzerConfig = false; 5187bdd1243dSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 51880b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 51890b57cec5SDimitry Andric FoundAnalyzerConfig = true; 51900b57cec5SDimitry Andric break; 51910b57cec5SDimitry Andric } 51920b57cec5SDimitry Andric if (!FoundAnalyzerConfig) 5193bdd1243dSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xanalyzer)) 51940b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 51950b57cec5SDimitry Andric FoundAnalyzerConfig = true; 51960b57cec5SDimitry Andric break; 51970b57cec5SDimitry Andric } 51980b57cec5SDimitry Andric if (FoundAnalyzerConfig) 51990b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-config-compatibility-mode=true"); 52000b57cec5SDimitry Andric 52010b57cec5SDimitry Andric CheckCodeGenerationOptions(D, Args); 52020b57cec5SDimitry Andric 52030b57cec5SDimitry Andric unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args); 52040b57cec5SDimitry Andric assert(FunctionAlignment <= 31 && "function alignment will be truncated!"); 52050b57cec5SDimitry Andric if (FunctionAlignment) { 52060b57cec5SDimitry Andric CmdArgs.push_back("-function-alignment"); 52070b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment))); 52080b57cec5SDimitry Andric } 52090b57cec5SDimitry Andric 5210349cc55cSDimitry Andric // We support -falign-loops=N where N is a power of 2. GCC supports more 5211349cc55cSDimitry Andric // forms. 5212349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_falign_loops_EQ)) { 5213349cc55cSDimitry Andric unsigned Value = 0; 5214349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) 5215349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_invalid_int_value) 5216349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5217349cc55cSDimitry Andric else if (Value & (Value - 1)) 5218349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_alignment_not_power_of_two) 5219349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5220349cc55cSDimitry Andric // Treat =0 as unspecified (use the target preference). 5221349cc55cSDimitry Andric if (Value) 5222349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-falign-loops=" + 5223349cc55cSDimitry Andric Twine(std::min(Value, 65536u)))); 5224349cc55cSDimitry Andric } 5225349cc55cSDimitry Andric 522606c3fb27SDimitry Andric if (Triple.isOSzOS()) { 522706c3fb27SDimitry Andric // On z/OS some of the system header feature macros need to 522806c3fb27SDimitry Andric // be defined to enable most cross platform projects to build 522906c3fb27SDimitry Andric // successfully. Ths include the libc++ library. A 523006c3fb27SDimitry Andric // complicating factor is that users can define these 523106c3fb27SDimitry Andric // macros to the same or different values. We need to add 523206c3fb27SDimitry Andric // the definition for these macros to the compilation command 523306c3fb27SDimitry Andric // if the user hasn't already defined them. 523406c3fb27SDimitry Andric 523506c3fb27SDimitry Andric auto findMacroDefinition = [&](const std::string &Macro) { 523606c3fb27SDimitry Andric auto MacroDefs = Args.getAllArgValues(options::OPT_D); 52375f757f3fSDimitry Andric return llvm::any_of(MacroDefs, [&](const std::string &M) { 52385f757f3fSDimitry Andric return M == Macro || M.find(Macro + '=') != std::string::npos; 52395f757f3fSDimitry Andric }); 524006c3fb27SDimitry Andric }; 524106c3fb27SDimitry Andric 524206c3fb27SDimitry Andric // _UNIX03_WITHDRAWN is required for libcxx & porting. 524306c3fb27SDimitry Andric if (!findMacroDefinition("_UNIX03_WITHDRAWN")) 524406c3fb27SDimitry Andric CmdArgs.push_back("-D_UNIX03_WITHDRAWN"); 524506c3fb27SDimitry Andric // _OPEN_DEFAULT is required for XL compat 524606c3fb27SDimitry Andric if (!findMacroDefinition("_OPEN_DEFAULT")) 524706c3fb27SDimitry Andric CmdArgs.push_back("-D_OPEN_DEFAULT"); 524806c3fb27SDimitry Andric if (D.CCCIsCXX() || types::isCXX(Input.getType())) { 524906c3fb27SDimitry Andric // _XOPEN_SOURCE=600 is required for libcxx. 525006c3fb27SDimitry Andric if (!findMacroDefinition("_XOPEN_SOURCE")) 525106c3fb27SDimitry Andric CmdArgs.push_back("-D_XOPEN_SOURCE=600"); 525206c3fb27SDimitry Andric } 525306c3fb27SDimitry Andric } 525406c3fb27SDimitry Andric 52550b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 52560b57cec5SDimitry Andric unsigned PICLevel; 52570b57cec5SDimitry Andric bool IsPIE; 52580b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); 5259bdd1243dSDimitry Andric Arg *LastPICDataRelArg = 5260bdd1243dSDimitry Andric Args.getLastArg(options::OPT_mno_pic_data_is_text_relative, 5261bdd1243dSDimitry Andric options::OPT_mpic_data_is_text_relative); 5262bdd1243dSDimitry Andric bool NoPICDataIsTextRelative = false; 5263bdd1243dSDimitry Andric if (LastPICDataRelArg) { 5264bdd1243dSDimitry Andric if (LastPICDataRelArg->getOption().matches( 5265bdd1243dSDimitry Andric options::OPT_mno_pic_data_is_text_relative)) { 5266bdd1243dSDimitry Andric NoPICDataIsTextRelative = true; 5267bdd1243dSDimitry Andric if (!PICLevel) 5268bdd1243dSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 5269bdd1243dSDimitry Andric << "-mno-pic-data-is-text-relative" 5270bdd1243dSDimitry Andric << "-fpic/-fpie"; 5271bdd1243dSDimitry Andric } 5272bdd1243dSDimitry Andric if (!Triple.isSystemZ()) 5273bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5274bdd1243dSDimitry Andric << (NoPICDataIsTextRelative ? "-mno-pic-data-is-text-relative" 5275bdd1243dSDimitry Andric : "-mpic-data-is-text-relative") 5276bdd1243dSDimitry Andric << RawTriple.str(); 5277bdd1243dSDimitry Andric } 52780b57cec5SDimitry Andric 52795ffd83dbSDimitry Andric bool IsROPI = RelocationModel == llvm::Reloc::ROPI || 52805ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 52815ffd83dbSDimitry Andric bool IsRWPI = RelocationModel == llvm::Reloc::RWPI || 52825ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 52830b57cec5SDimitry Andric 52845ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_mcmse) && 52855ffd83dbSDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) { 52865ffd83dbSDimitry Andric if (IsROPI) 52875ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << IsROPI; 52885ffd83dbSDimitry Andric if (IsRWPI) 52895ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << !IsRWPI; 52905ffd83dbSDimitry Andric } 52915ffd83dbSDimitry Andric 52925ffd83dbSDimitry Andric if (IsROPI && types::isCXX(Input.getType()) && 52930b57cec5SDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) 52940b57cec5SDimitry Andric D.Diag(diag::err_drv_ropi_incompatible_with_cxx); 52950b57cec5SDimitry Andric 52965ffd83dbSDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 52970b57cec5SDimitry Andric if (RMName) { 52980b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 52990b57cec5SDimitry Andric CmdArgs.push_back(RMName); 53000b57cec5SDimitry Andric } 53010b57cec5SDimitry Andric if (PICLevel > 0) { 53020b57cec5SDimitry Andric CmdArgs.push_back("-pic-level"); 53030b57cec5SDimitry Andric CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); 53040b57cec5SDimitry Andric if (IsPIE) 53050b57cec5SDimitry Andric CmdArgs.push_back("-pic-is-pie"); 5306bdd1243dSDimitry Andric if (NoPICDataIsTextRelative) 5307bdd1243dSDimitry Andric CmdArgs.push_back("-mcmodel=medium"); 53080b57cec5SDimitry Andric } 53090b57cec5SDimitry Andric 53100b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::ROPI || 53110b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 53120b57cec5SDimitry Andric CmdArgs.push_back("-fropi"); 53130b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::RWPI || 53140b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 53150b57cec5SDimitry Andric CmdArgs.push_back("-frwpi"); 53160b57cec5SDimitry Andric 53170b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_meabi)) { 53180b57cec5SDimitry Andric CmdArgs.push_back("-meabi"); 53190b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 53200b57cec5SDimitry Andric } 53210b57cec5SDimitry Andric 5322e8d8bef9SDimitry Andric // -fsemantic-interposition is forwarded to CC1: set the 5323e8d8bef9SDimitry Andric // "SemanticInterposition" metadata to 1 (make some linkages interposable) and 5324e8d8bef9SDimitry Andric // make default visibility external linkage definitions dso_preemptable. 5325e8d8bef9SDimitry Andric // 5326e8d8bef9SDimitry Andric // -fno-semantic-interposition: if the target supports .Lfoo$local local 5327e8d8bef9SDimitry Andric // aliases (make default visibility external linkage definitions dso_local). 5328e8d8bef9SDimitry Andric // This is the CC1 default for ELF to match COFF/Mach-O. 5329e8d8bef9SDimitry Andric // 5330e8d8bef9SDimitry Andric // Otherwise use Clang's traditional behavior: like 5331e8d8bef9SDimitry Andric // -fno-semantic-interposition but local aliases are not used. So references 5332e8d8bef9SDimitry Andric // can be interposed if not optimized out. 5333e8d8bef9SDimitry Andric if (Triple.isOSBinFormatELF()) { 5334e8d8bef9SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fsemantic_interposition, 5335e8d8bef9SDimitry Andric options::OPT_fno_semantic_interposition); 5336e8d8bef9SDimitry Andric if (RelocationModel != llvm::Reloc::Static && !IsPIE) { 5337e8d8bef9SDimitry Andric // The supported targets need to call AsmPrinter::getSymbolPreferLocal. 5338fe6060f1SDimitry Andric bool SupportsLocalAlias = 5339fe6060f1SDimitry Andric Triple.isAArch64() || Triple.isRISCV() || Triple.isX86(); 5340e8d8bef9SDimitry Andric if (!A) 5341e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5342e8d8bef9SDimitry Andric else if (A->getOption().matches(options::OPT_fsemantic_interposition)) 53435ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5344e8d8bef9SDimitry Andric else if (!SupportsLocalAlias) 5345e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5346e8d8bef9SDimitry Andric } 5347e8d8bef9SDimitry Andric } 53485ffd83dbSDimitry Andric 53495ffd83dbSDimitry Andric { 53505ffd83dbSDimitry Andric std::string Model; 53510b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) { 53520b57cec5SDimitry Andric if (!TC.isThreadModelSupported(A->getValue())) 53530b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_thread_model_for_target) 53540b57cec5SDimitry Andric << A->getValue() << A->getAsString(Args); 53555ffd83dbSDimitry Andric Model = A->getValue(); 53565ffd83dbSDimitry Andric } else 53575ffd83dbSDimitry Andric Model = TC.getThreadModel(); 53585ffd83dbSDimitry Andric if (Model != "posix") { 53595ffd83dbSDimitry Andric CmdArgs.push_back("-mthread-model"); 53605ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Model)); 53610b57cec5SDimitry Andric } 53625ffd83dbSDimitry Andric } 53630b57cec5SDimitry Andric 5364bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fveclib)) { 5365bdd1243dSDimitry Andric StringRef Name = A->getValue(); 5366bdd1243dSDimitry Andric if (Name == "SVML") { 5367bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::x86 && 5368bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::x86_64) 5369bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5370bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5371bdd1243dSDimitry Andric } else if (Name == "LIBMVEC-X86") { 5372bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::x86 && 5373bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::x86_64) 5374bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5375bdd1243dSDimitry Andric << Name << Triple.getArchName(); 537606c3fb27SDimitry Andric } else if (Name == "SLEEF" || Name == "ArmPL") { 5377bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::aarch64 && 5378bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::aarch64_be) 5379bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5380bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5381bdd1243dSDimitry Andric } 5382bdd1243dSDimitry Andric A->render(Args, CmdArgs); 5383bdd1243dSDimitry Andric } 53840b57cec5SDimitry Andric 53850b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmerge_all_constants, 53860b57cec5SDimitry Andric options::OPT_fno_merge_all_constants, false)) 53870b57cec5SDimitry Andric CmdArgs.push_back("-fmerge-all-constants"); 53880b57cec5SDimitry Andric 5389bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdelete_null_pointer_checks, 5390bdd1243dSDimitry Andric options::OPT_fno_delete_null_pointer_checks); 53910b57cec5SDimitry Andric 53920b57cec5SDimitry Andric // LLVM Code Generator Options. 53930b57cec5SDimitry Andric 5394972a253aSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_quadword_atomics)) { 5395972a253aSDimitry Andric if (!Triple.isOSAIX() || Triple.isPPC32()) 5396972a253aSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5397972a253aSDimitry Andric << A->getSpelling() << RawTriple.str(); 5398972a253aSDimitry Andric CmdArgs.push_back("-mabi=quadword-atomics"); 5399972a253aSDimitry Andric } 5400972a253aSDimitry Andric 5401fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) { 5402fe6060f1SDimitry Andric // Emit the unsupported option error until the Clang's library integration 5403fe6060f1SDimitry Andric // support for 128-bit long double is available for AIX. 5404fe6060f1SDimitry Andric if (Triple.isOSAIX()) 5405fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5406fe6060f1SDimitry Andric << A->getSpelling() << RawTriple.str(); 5407e8d8bef9SDimitry Andric } 5408e8d8bef9SDimitry Andric 54090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) { 541006c3fb27SDimitry Andric StringRef V = A->getValue(), V1 = V; 541106c3fb27SDimitry Andric unsigned Size; 541206c3fb27SDimitry Andric if (V1.consumeInteger(10, Size) || !V1.empty()) 541306c3fb27SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 541406c3fb27SDimitry Andric << V << A->getOption().getName(); 5415fe6060f1SDimitry Andric else 541606c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + V)); 54170b57cec5SDimitry Andric } 54180b57cec5SDimitry Andric 541981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fjump_tables, 542081ad6265SDimitry Andric options::OPT_fno_jump_tables); 542181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fprofile_sample_accurate, 542281ad6265SDimitry Andric options::OPT_fno_profile_sample_accurate); 542381ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fpreserve_as_comments, 542481ad6265SDimitry Andric options::OPT_fno_preserve_as_comments); 54250b57cec5SDimitry Andric 54260b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 54270b57cec5SDimitry Andric CmdArgs.push_back("-mregparm"); 54280b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 54290b57cec5SDimitry Andric } 54300b57cec5SDimitry Andric 5431e86cf8adSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_maix_struct_return, 5432e86cf8adSDimitry Andric options::OPT_msvr4_struct_return)) { 5433e8d8bef9SDimitry Andric if (!TC.getTriple().isPPC32()) { 5434e86cf8adSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5435e86cf8adSDimitry Andric << A->getSpelling() << RawTriple.str(); 5436e86cf8adSDimitry Andric } else if (A->getOption().matches(options::OPT_maix_struct_return)) { 5437e86cf8adSDimitry Andric CmdArgs.push_back("-maix-struct-return"); 5438e86cf8adSDimitry Andric } else { 5439e86cf8adSDimitry Andric assert(A->getOption().matches(options::OPT_msvr4_struct_return)); 5440e86cf8adSDimitry Andric CmdArgs.push_back("-msvr4-struct-return"); 5441e86cf8adSDimitry Andric } 5442e86cf8adSDimitry Andric } 5443e86cf8adSDimitry Andric 54440b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 54450b57cec5SDimitry Andric options::OPT_freg_struct_return)) { 54460b57cec5SDimitry Andric if (TC.getArch() != llvm::Triple::x86) { 54470b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 54480b57cec5SDimitry Andric << A->getSpelling() << RawTriple.str(); 54490b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 54500b57cec5SDimitry Andric CmdArgs.push_back("-fpcc-struct-return"); 54510b57cec5SDimitry Andric } else { 54520b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_freg_struct_return)); 54530b57cec5SDimitry Andric CmdArgs.push_back("-freg-struct-return"); 54540b57cec5SDimitry Andric } 54550b57cec5SDimitry Andric } 54560b57cec5SDimitry Andric 54575f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) { 54585f757f3fSDimitry Andric if (Triple.getArch() == llvm::Triple::m68k) 54595f757f3fSDimitry Andric CmdArgs.push_back("-fdefault-calling-conv=rtdcall"); 54605f757f3fSDimitry Andric else 54610b57cec5SDimitry Andric CmdArgs.push_back("-fdefault-calling-conv=stdcall"); 54625f757f3fSDimitry Andric } 54630b57cec5SDimitry Andric 54645ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_fenable_matrix)) { 54655ffd83dbSDimitry Andric // enable-matrix is needed by both the LangOpts and by LLVM. 54665ffd83dbSDimitry Andric CmdArgs.push_back("-fenable-matrix"); 54675ffd83dbSDimitry Andric CmdArgs.push_back("-mllvm"); 54685ffd83dbSDimitry Andric CmdArgs.push_back("-enable-matrix"); 54695ffd83dbSDimitry Andric } 54705ffd83dbSDimitry Andric 5471a7dea167SDimitry Andric CodeGenOptions::FramePointerKind FPKeepKind = 5472a7dea167SDimitry Andric getFramePointerKind(Args, RawTriple); 5473a7dea167SDimitry Andric const char *FPKeepKindStr = nullptr; 5474a7dea167SDimitry Andric switch (FPKeepKind) { 5475a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::None: 5476a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=none"; 5477a7dea167SDimitry Andric break; 5478a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::NonLeaf: 5479a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=non-leaf"; 5480a7dea167SDimitry Andric break; 5481a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::All: 5482a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=all"; 5483a7dea167SDimitry Andric break; 5484a7dea167SDimitry Andric } 5485a7dea167SDimitry Andric assert(FPKeepKindStr && "unknown FramePointerKind"); 5486a7dea167SDimitry Andric CmdArgs.push_back(FPKeepKindStr); 5487a7dea167SDimitry Andric 548881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fzero_initialized_in_bss, 548981ad6265SDimitry Andric options::OPT_fno_zero_initialized_in_bss); 54900b57cec5SDimitry Andric 54910b57cec5SDimitry Andric bool OFastEnabled = isOptimizationLevelFast(Args); 54920b57cec5SDimitry Andric // If -Ofast is the optimization level, then -fstrict-aliasing should be 54930b57cec5SDimitry Andric // enabled. This alias option is being used to simplify the hasFlag logic. 54940b57cec5SDimitry Andric OptSpecifier StrictAliasingAliasOption = 54950b57cec5SDimitry Andric OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing; 54960b57cec5SDimitry Andric // We turn strict aliasing off by default if we're in CL mode, since MSVC 54970b57cec5SDimitry Andric // doesn't do any TBAA. 54980b57cec5SDimitry Andric bool TBAAOnByDefault = !D.IsCLMode(); 54990b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 55000b57cec5SDimitry Andric options::OPT_fno_strict_aliasing, TBAAOnByDefault)) 55010b57cec5SDimitry Andric CmdArgs.push_back("-relaxed-aliasing"); 55020b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, 550381ad6265SDimitry Andric options::OPT_fno_struct_path_tbaa, true)) 55040b57cec5SDimitry Andric CmdArgs.push_back("-no-struct-path-tbaa"); 550581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_enums, 550681ad6265SDimitry Andric options::OPT_fno_strict_enums); 550781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fstrict_return, 550881ad6265SDimitry Andric options::OPT_fno_strict_return); 550981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fallow_editor_placeholders, 551081ad6265SDimitry Andric options::OPT_fno_allow_editor_placeholders); 551181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_vtable_pointers, 551281ad6265SDimitry Andric options::OPT_fno_strict_vtable_pointers); 551381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_emit_vtables, 551481ad6265SDimitry Andric options::OPT_fno_force_emit_vtables); 551581ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 551681ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 551781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fescaping_block_tail_calls, 551881ad6265SDimitry Andric options::OPT_fno_escaping_block_tail_calls); 55190b57cec5SDimitry Andric 55200b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses, 55210b57cec5SDimitry Andric options::OPT_fno_fine_grained_bitfield_accesses); 55220b57cec5SDimitry Andric 5523fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5524fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5525fe6060f1SDimitry Andric 55265f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_omit_vtable_rtti, 55275f757f3fSDimitry Andric options::OPT_fno_experimental_omit_vtable_rtti); 55285f757f3fSDimitry Andric 55290b57cec5SDimitry Andric // Handle segmented stacks. 5530bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsplit_stack, 5531bdd1243dSDimitry Andric options::OPT_fno_split_stack); 5532fe6060f1SDimitry Andric 5533fe6060f1SDimitry Andric // -fprotect-parens=0 is default. 5534fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fprotect_parens, 5535fe6060f1SDimitry Andric options::OPT_fno_protect_parens, false)) 5536fe6060f1SDimitry Andric CmdArgs.push_back("-fprotect-parens"); 55370b57cec5SDimitry Andric 55385ffd83dbSDimitry Andric RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA); 55395ffd83dbSDimitry Andric 5540fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) { 5541fe6060f1SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 5542fe6060f1SDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5543fe6060f1SDimitry Andric StringRef V = A->getValue(); 5544fe6060f1SDimitry Andric if (V == "64") 5545fe6060f1SDimitry Andric CmdArgs.push_back("-fextend-arguments=64"); 5546fe6060f1SDimitry Andric else if (V != "32") 5547fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5548fe6060f1SDimitry Andric << A->getValue() << A->getOption().getName(); 5549fe6060f1SDimitry Andric } else 5550fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5551fe6060f1SDimitry Andric << A->getOption().getName() << TripleStr; 5552fe6060f1SDimitry Andric } 5553fe6060f1SDimitry Andric 55545ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) { 55555ffd83dbSDimitry Andric if (TC.getArch() == llvm::Triple::avr) 55565ffd83dbSDimitry Andric A->render(Args, CmdArgs); 55575ffd83dbSDimitry Andric else 55585ffd83dbSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 55595ffd83dbSDimitry Andric << A->getAsString(Args) << TripleStr; 55605ffd83dbSDimitry Andric } 55610b57cec5SDimitry Andric 5562a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) { 5563480093f4SDimitry Andric if (TC.getTriple().isX86()) 5564a7dea167SDimitry Andric A->render(Args, CmdArgs); 5565e8d8bef9SDimitry Andric else if (TC.getTriple().isPPC() && 5566a7dea167SDimitry Andric (A->getOption().getID() != options::OPT_mlong_double_80)) 55670b57cec5SDimitry Andric A->render(Args, CmdArgs); 55680b57cec5SDimitry Andric else 55690b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 55700b57cec5SDimitry Andric << A->getAsString(Args) << TripleStr; 55710b57cec5SDimitry Andric } 55720b57cec5SDimitry Andric 55730b57cec5SDimitry Andric // Decide whether to use verbose asm. Verbose assembly is the default on 55740b57cec5SDimitry Andric // toolchains which have the integrated assembler on by default. 55750b57cec5SDimitry Andric bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault(); 55765ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 5577a7dea167SDimitry Andric IsIntegratedAssemblerDefault)) 55785ffd83dbSDimitry Andric CmdArgs.push_back("-fno-verbose-asm"); 55790b57cec5SDimitry Andric 5580e8d8bef9SDimitry Andric // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we 5581e8d8bef9SDimitry Andric // use that to indicate the MC default in the backend. 5582e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbinutils_version_EQ)) { 5583e8d8bef9SDimitry Andric StringRef V = A->getValue(); 5584e8d8bef9SDimitry Andric unsigned Num; 5585e8d8bef9SDimitry Andric if (V == "none") 5586e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5587e8d8bef9SDimitry Andric else if (!V.consumeInteger(10, Num) && Num > 0 && 5588e8d8bef9SDimitry Andric (V.empty() || (V.consume_front(".") && 5589e8d8bef9SDimitry Andric !V.consumeInteger(10, Num) && V.empty()))) 5590e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5591e8d8bef9SDimitry Andric else 5592e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5593e8d8bef9SDimitry Andric << A->getValue() << A->getOption().getName(); 5594e8d8bef9SDimitry Andric } 5595e8d8bef9SDimitry Andric 5596fe6060f1SDimitry Andric // If toolchain choose to use MCAsmParser for inline asm don't pass the 5597fe6060f1SDimitry Andric // option to disable integrated-as explictly. 5598fe6060f1SDimitry Andric if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser()) 55990b57cec5SDimitry Andric CmdArgs.push_back("-no-integrated-as"); 56000b57cec5SDimitry Andric 56010b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 56020b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 56030b57cec5SDimitry Andric CmdArgs.push_back("Structure"); 56040b57cec5SDimitry Andric } 56050b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 56060b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 56070b57cec5SDimitry Andric CmdArgs.push_back("Arguments"); 56080b57cec5SDimitry Andric } 56090b57cec5SDimitry Andric 56100b57cec5SDimitry Andric // Enable -mconstructor-aliases except on darwin, where we have to work around 56115f757f3fSDimitry Andric // a linker bug (see https://openradar.appspot.com/7198997), and CUDA device 56125f757f3fSDimitry Andric // code, where aliases aren't supported. 5613349cc55cSDimitry Andric if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX()) 56140b57cec5SDimitry Andric CmdArgs.push_back("-mconstructor-aliases"); 56150b57cec5SDimitry Andric 56160b57cec5SDimitry Andric // Darwin's kernel doesn't support guard variables; just die if we 56170b57cec5SDimitry Andric // try to use them. 56180b57cec5SDimitry Andric if (KernelOrKext && RawTriple.isOSDarwin()) 56190b57cec5SDimitry Andric CmdArgs.push_back("-fforbid-guard-variables"); 56200b57cec5SDimitry Andric 56210b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields, 56225ffd83dbSDimitry Andric Triple.isWindowsGNUEnvironment())) { 56230b57cec5SDimitry Andric CmdArgs.push_back("-mms-bitfields"); 56240b57cec5SDimitry Andric } 56250b57cec5SDimitry Andric 56265f757f3fSDimitry Andric if (Triple.isWindowsGNUEnvironment()) { 56275f757f3fSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fauto_import, 56285f757f3fSDimitry Andric options::OPT_fno_auto_import); 56295f757f3fSDimitry Andric } 56305f757f3fSDimitry Andric 56315f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fms_volatile, options::OPT_fno_ms_volatile, 56325f757f3fSDimitry Andric Triple.isX86() && D.IsCLMode())) 56335f757f3fSDimitry Andric CmdArgs.push_back("-fms-volatile"); 56345f757f3fSDimitry Andric 5635e8d8bef9SDimitry Andric // Non-PIC code defaults to -fdirect-access-external-data while PIC code 5636e8d8bef9SDimitry Andric // defaults to -fno-direct-access-external-data. Pass the option if different 5637e8d8bef9SDimitry Andric // from the default. 5638e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, 56395f757f3fSDimitry Andric options::OPT_fno_direct_access_external_data)) { 5640e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fdirect_access_external_data) != 5641e8d8bef9SDimitry Andric (PICLevel == 0)) 5642e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 56435f757f3fSDimitry Andric } else if (PICLevel == 0 && Triple.isLoongArch()) { 56445f757f3fSDimitry Andric // Some targets default to -fno-direct-access-external-data even for 56455f757f3fSDimitry Andric // -fno-pic. 56465f757f3fSDimitry Andric CmdArgs.push_back("-fno-direct-access-external-data"); 56475f757f3fSDimitry Andric } 56480b57cec5SDimitry Andric 56490b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { 56500b57cec5SDimitry Andric CmdArgs.push_back("-fno-plt"); 56510b57cec5SDimitry Andric } 56520b57cec5SDimitry Andric 56530b57cec5SDimitry Andric // -fhosted is default. 56540b57cec5SDimitry Andric // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to 56550b57cec5SDimitry Andric // use Freestanding. 56560b57cec5SDimitry Andric bool Freestanding = 56570b57cec5SDimitry Andric Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 56580b57cec5SDimitry Andric KernelOrKext; 56590b57cec5SDimitry Andric if (Freestanding) 56600b57cec5SDimitry Andric CmdArgs.push_back("-ffreestanding"); 56610b57cec5SDimitry Andric 566281ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_knr_functions); 566381ad6265SDimitry Andric 56640b57cec5SDimitry Andric // This is a coarse approximation of what llvm-gcc actually does, both 56650b57cec5SDimitry Andric // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 56660b57cec5SDimitry Andric // complicated ways. 5667349cc55cSDimitry Andric auto SanitizeArgs = TC.getSanitizerArgs(Args); 5668bdd1243dSDimitry Andric 5669bdd1243dSDimitry Andric bool IsAsyncUnwindTablesDefault = 5670bdd1243dSDimitry Andric TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Asynchronous; 5671bdd1243dSDimitry Andric bool IsSyncUnwindTablesDefault = 5672bdd1243dSDimitry Andric TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Synchronous; 5673bdd1243dSDimitry Andric 5674349cc55cSDimitry Andric bool AsyncUnwindTables = Args.hasFlag( 5675349cc55cSDimitry Andric options::OPT_fasynchronous_unwind_tables, 56760b57cec5SDimitry Andric options::OPT_fno_asynchronous_unwind_tables, 5677bdd1243dSDimitry Andric (IsAsyncUnwindTablesDefault || SanitizeArgs.needsUnwindTables()) && 56780b57cec5SDimitry Andric !Freestanding); 5679bdd1243dSDimitry Andric bool UnwindTables = 5680bdd1243dSDimitry Andric Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, 5681bdd1243dSDimitry Andric IsSyncUnwindTablesDefault && !Freestanding); 5682349cc55cSDimitry Andric if (AsyncUnwindTables) 5683349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=2"); 5684349cc55cSDimitry Andric else if (UnwindTables) 5685349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=1"); 56860b57cec5SDimitry Andric 56875ffd83dbSDimitry Andric // Prepare `-aux-target-cpu` and `-aux-target-feature` unless 56885ffd83dbSDimitry Andric // `--gpu-use-aux-triple-only` is specified. 56895ffd83dbSDimitry Andric if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) && 5690fe6060f1SDimitry Andric (IsCudaDevice || IsHIPDevice)) { 56915ffd83dbSDimitry Andric const ArgList &HostArgs = 56925ffd83dbSDimitry Andric C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None); 56935ffd83dbSDimitry Andric std::string HostCPU = 5694349cc55cSDimitry Andric getCPUName(D, HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); 56955ffd83dbSDimitry Andric if (!HostCPU.empty()) { 56965ffd83dbSDimitry Andric CmdArgs.push_back("-aux-target-cpu"); 56975ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(HostCPU)); 56985ffd83dbSDimitry Andric } 56995ffd83dbSDimitry Andric getTargetFeatures(D, *TC.getAuxTriple(), HostArgs, CmdArgs, 57005ffd83dbSDimitry Andric /*ForAS*/ false, /*IsAux*/ true); 57015ffd83dbSDimitry Andric } 57025ffd83dbSDimitry Andric 57030b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 57040b57cec5SDimitry Andric 57050b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 57065ffd83dbSDimitry Andric StringRef CM = A->getValue(); 57075f757f3fSDimitry Andric bool Ok = false; 5708fe6060f1SDimitry Andric if (Triple.isOSAIX() && CM == "medium") 57095f757f3fSDimitry Andric CM = "large"; 57105f757f3fSDimitry Andric if (Triple.isAArch64(64)) { 57115f757f3fSDimitry Andric Ok = CM == "tiny" || CM == "small" || CM == "large"; 57125f757f3fSDimitry Andric if (CM == "large" && RelocationModel != llvm::Reloc::Static) 57135f757f3fSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 57145f757f3fSDimitry Andric << A->getAsString(Args) << "-fno-pic"; 57155f757f3fSDimitry Andric } else if (Triple.isLoongArch()) { 57165f757f3fSDimitry Andric if (CM == "extreme" && 57175f757f3fSDimitry Andric Args.hasFlagNoClaim(options::OPT_fplt, options::OPT_fno_plt, false)) 57185f757f3fSDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 57195f757f3fSDimitry Andric << A->getAsString(Args) << "-fplt"; 57205f757f3fSDimitry Andric Ok = CM == "normal" || CM == "medium" || CM == "extreme"; 57215f757f3fSDimitry Andric // Convert to LLVM recognizable names. 57225f757f3fSDimitry Andric if (Ok) 57235f757f3fSDimitry Andric CM = llvm::StringSwitch<StringRef>(CM) 57245f757f3fSDimitry Andric .Case("normal", "small") 57255f757f3fSDimitry Andric .Case("extreme", "large") 57265f757f3fSDimitry Andric .Default(CM); 57275f757f3fSDimitry Andric } else if (Triple.isPPC64() || Triple.isOSAIX()) { 57285f757f3fSDimitry Andric Ok = CM == "small" || CM == "medium" || CM == "large"; 57295f757f3fSDimitry Andric } else if (Triple.isRISCV()) { 57305f757f3fSDimitry Andric if (CM == "medlow") 57315f757f3fSDimitry Andric CM = "small"; 57325f757f3fSDimitry Andric else if (CM == "medany") 57335f757f3fSDimitry Andric CM = "medium"; 57345f757f3fSDimitry Andric Ok = CM == "small" || CM == "medium"; 57355f757f3fSDimitry Andric } else if (Triple.getArch() == llvm::Triple::x86_64) { 57365f757f3fSDimitry Andric Ok = llvm::is_contained({"small", "kernel", "medium", "large", "tiny"}, 57375f757f3fSDimitry Andric CM); 57385f757f3fSDimitry Andric } else if (Triple.isNVPTX() || Triple.isAMDGPU()) { 57395f757f3fSDimitry Andric // NVPTX/AMDGPU does not care about the code model and will accept 57405f757f3fSDimitry Andric // whatever works for the host. 57415f757f3fSDimitry Andric Ok = true; 57425f757f3fSDimitry Andric } 57435f757f3fSDimitry Andric if (Ok) { 57445f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mcmodel=" + CM)); 5745fe6060f1SDimitry Andric } else { 57465f757f3fSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument_for_target) 57475f757f3fSDimitry Andric << A->getSpelling() << CM << TripleStr; 57485f757f3fSDimitry Andric } 57495f757f3fSDimitry Andric } 57505f757f3fSDimitry Andric 57515f757f3fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlarge_data_threshold_EQ)) { 57525f757f3fSDimitry Andric if (!Triple.isX86()) { 57535f757f3fSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 57545f757f3fSDimitry Andric << A->getOption().getName() << TripleStr; 57555f757f3fSDimitry Andric } else { 57565f757f3fSDimitry Andric bool IsMediumCM = false; 57575f757f3fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) 57585f757f3fSDimitry Andric IsMediumCM = StringRef(A->getValue()) == "medium"; 57595f757f3fSDimitry Andric if (!IsMediumCM) { 57605f757f3fSDimitry Andric D.Diag(diag::warn_drv_large_data_threshold_invalid_code_model) 57615f757f3fSDimitry Andric << A->getOption().getRenderName(); 57625f757f3fSDimitry Andric } else { 57635f757f3fSDimitry Andric A->render(Args, CmdArgs); 57645f757f3fSDimitry Andric } 57650b57cec5SDimitry Andric } 5766fe6060f1SDimitry Andric } 57670b57cec5SDimitry Andric 5768480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { 5769480093f4SDimitry Andric StringRef Value = A->getValue(); 5770480093f4SDimitry Andric unsigned TLSSize = 0; 5771480093f4SDimitry Andric Value.getAsInteger(10, TLSSize); 5772480093f4SDimitry Andric if (!Triple.isAArch64() || !Triple.isOSBinFormatELF()) 5773480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5774480093f4SDimitry Andric << A->getOption().getName() << TripleStr; 5775480093f4SDimitry Andric if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48) 5776480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5777480093f4SDimitry Andric << A->getOption().getName() << Value; 5778480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ); 5779480093f4SDimitry Andric } 5780480093f4SDimitry Andric 57810b57cec5SDimitry Andric // Add the target cpu 5782349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 57830b57cec5SDimitry Andric if (!CPU.empty()) { 57840b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 57850b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 57860b57cec5SDimitry Andric } 57870b57cec5SDimitry Andric 57880b57cec5SDimitry Andric RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs); 57890b57cec5SDimitry Andric 57900b57cec5SDimitry Andric // Add clang-cl arguments. 57910b57cec5SDimitry Andric types::ID InputType = Input.getType(); 57920b57cec5SDimitry Andric if (D.IsCLMode()) 57935f757f3fSDimitry Andric AddClangCLArgs(Args, InputType, CmdArgs); 57940b57cec5SDimitry Andric 57955f757f3fSDimitry Andric llvm::codegenoptions::DebugInfoKind DebugInfoKind = 57965f757f3fSDimitry Andric llvm::codegenoptions::NoDebugInfo; 5797e8d8bef9SDimitry Andric DwarfFissionKind DwarfFission = DwarfFissionKind::None; 57985f757f3fSDimitry Andric renderDebugOptions(TC, D, RawTriple, Args, types::isLLVMIR(InputType), 57995f757f3fSDimitry Andric CmdArgs, Output, DebugInfoKind, DwarfFission); 580081ad6265SDimitry Andric 58010b57cec5SDimitry Andric // Add the split debug info name to the command lines here so we 58020b57cec5SDimitry Andric // can propagate it to the backend. 58030b57cec5SDimitry Andric bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) && 5804e8d8bef9SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 580506c3fb27SDimitry Andric TC.getTriple().isOSBinFormatWasm() || 580606c3fb27SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 58070b57cec5SDimitry Andric (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 58080b57cec5SDimitry Andric isa<BackendJobAction>(JA)); 58090b57cec5SDimitry Andric if (SplitDWARF) { 5810e8d8bef9SDimitry Andric const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output); 58110b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-file"); 58120b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 58130b57cec5SDimitry Andric if (DwarfFission == DwarfFissionKind::Split) { 58140b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 58150b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 58160b57cec5SDimitry Andric } 58170b57cec5SDimitry Andric } 58180b57cec5SDimitry Andric 58190b57cec5SDimitry Andric // Pass the linker version in use. 58200b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 58210b57cec5SDimitry Andric CmdArgs.push_back("-target-linker-version"); 58220b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58230b57cec5SDimitry Andric } 58240b57cec5SDimitry Andric 58250b57cec5SDimitry Andric // Explicitly error on some things we know we don't support and can't just 58260b57cec5SDimitry Andric // ignore. 58270b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fallow_unsupported)) { 58280b57cec5SDimitry Andric Arg *Unsupported; 58290b57cec5SDimitry Andric if (types::isCXX(InputType) && RawTriple.isOSDarwin() && 58300b57cec5SDimitry Andric TC.getArch() == llvm::Triple::x86) { 58310b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 58320b57cec5SDimitry Andric (Unsupported = Args.getLastArg(options::OPT_mkernel))) 58330b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 58340b57cec5SDimitry Andric << Unsupported->getOption().getName(); 58350b57cec5SDimitry Andric } 58360b57cec5SDimitry Andric // The faltivec option has been superseded by the maltivec option. 58370b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_faltivec))) 58380b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 58390b57cec5SDimitry Andric << Unsupported->getOption().getName() 58400b57cec5SDimitry Andric << "please use -maltivec and include altivec.h explicitly"; 58410b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec))) 58420b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 58430b57cec5SDimitry Andric << Unsupported->getOption().getName() << "please use -mno-altivec"; 58440b57cec5SDimitry Andric } 58450b57cec5SDimitry Andric 58460b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_v); 58475ffd83dbSDimitry Andric 58485ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_H)) { 58495ffd83dbSDimitry Andric CmdArgs.push_back("-H"); 58505ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 58515ffd83dbSDimitry Andric } 5852fe6060f1SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes); 58535ffd83dbSDimitry Andric 5854bdd1243dSDimitry Andric if (D.CCPrintHeadersFormat && !D.CCGenDiagnostics) { 58550b57cec5SDimitry Andric CmdArgs.push_back("-header-include-file"); 5856fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCPrintHeadersFilename.empty() 5857fe6060f1SDimitry Andric ? D.CCPrintHeadersFilename.c_str() 58580b57cec5SDimitry Andric : "-"); 58595ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 5860bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5861bdd1243dSDimitry Andric "-header-include-format=" + 5862bdd1243dSDimitry Andric std::string(headerIncludeFormatKindToString(D.CCPrintHeadersFormat)))); 5863bdd1243dSDimitry Andric CmdArgs.push_back( 5864bdd1243dSDimitry Andric Args.MakeArgString("-header-include-filtering=" + 5865bdd1243dSDimitry Andric std::string(headerIncludeFilteringKindToString( 5866bdd1243dSDimitry Andric D.CCPrintHeadersFiltering)))); 58670b57cec5SDimitry Andric } 58680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_P); 58690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 58700b57cec5SDimitry Andric 58710b57cec5SDimitry Andric if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 58720b57cec5SDimitry Andric CmdArgs.push_back("-diagnostic-log-file"); 5873fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty() 5874fe6060f1SDimitry Andric ? D.CCLogDiagnosticsFilename.c_str() 58750b57cec5SDimitry Andric : "-"); 58760b57cec5SDimitry Andric } 58770b57cec5SDimitry Andric 587813138422SDimitry Andric // Give the gen diagnostics more chances to succeed, by avoiding intentional 587913138422SDimitry Andric // crashes. 588013138422SDimitry Andric if (D.CCGenDiagnostics) 588113138422SDimitry Andric CmdArgs.push_back("-disable-pragma-debug-crash"); 588213138422SDimitry Andric 5883fe6060f1SDimitry Andric // Allow backend to put its diagnostic files in the same place as frontend 5884fe6060f1SDimitry Andric // crash diagnostics files. 5885fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) { 5886fe6060f1SDimitry Andric StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir); 5887fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5888fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir)); 5889fe6060f1SDimitry Andric } 5890fe6060f1SDimitry Andric 58910b57cec5SDimitry Andric bool UseSeparateSections = isUseSeparateSections(Triple); 58920b57cec5SDimitry Andric 58930b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffunction_sections, 58940b57cec5SDimitry Andric options::OPT_fno_function_sections, UseSeparateSections)) { 58950b57cec5SDimitry Andric CmdArgs.push_back("-ffunction-sections"); 58960b57cec5SDimitry Andric } 58970b57cec5SDimitry Andric 58985ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) { 58995ffd83dbSDimitry Andric StringRef Val = A->getValue(); 5900fe6060f1SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 59015ffd83dbSDimitry Andric if (Val != "all" && Val != "labels" && Val != "none" && 59025f757f3fSDimitry Andric !Val.starts_with("list=")) 59035ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 59045ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 59055ffd83dbSDimitry Andric else 59065ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5907fe6060f1SDimitry Andric } else if (Triple.isNVPTX()) { 5908fe6060f1SDimitry Andric // Do not pass the option to the GPU compilation. We still want it enabled 5909fe6060f1SDimitry Andric // for the host-side compilation, so seeing it here is not an error. 5910fe6060f1SDimitry Andric } else if (Val != "none") { 5911fe6060f1SDimitry Andric // =none is allowed everywhere. It's useful for overriding the option 5912fe6060f1SDimitry Andric // and is the same as not specifying the option. 5913e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5914e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5915e8d8bef9SDimitry Andric } 59165ffd83dbSDimitry Andric } 59175ffd83dbSDimitry Andric 5918e8d8bef9SDimitry Andric bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF(); 59190b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, 5920e8d8bef9SDimitry Andric UseSeparateSections || HasDefaultDataSections)) { 59210b57cec5SDimitry Andric CmdArgs.push_back("-fdata-sections"); 59220b57cec5SDimitry Andric } 59230b57cec5SDimitry Andric 592481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_funique_section_names, 592581ad6265SDimitry Andric options::OPT_fno_unique_section_names); 592681ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_internal_linkage_names, 592781ad6265SDimitry Andric options::OPT_fno_unique_internal_linkage_names); 592881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_basic_block_section_names, 592981ad6265SDimitry Andric options::OPT_fno_unique_basic_block_section_names); 593006c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fconvergent_functions, 593106c3fb27SDimitry Andric options::OPT_fno_convergent_functions); 59325ffd83dbSDimitry Andric 5933e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, 5934e8d8bef9SDimitry Andric options::OPT_fno_split_machine_functions)) { 59355f757f3fSDimitry Andric if (!A->getOption().matches(options::OPT_fno_split_machine_functions)) { 59365f757f3fSDimitry Andric // This codegen pass is only available on x86 and AArch64 ELF targets. 59375f757f3fSDimitry Andric if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF()) 5938e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 59395f757f3fSDimitry Andric else 5940e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5941e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5942e8d8bef9SDimitry Andric } 5943e8d8bef9SDimitry Andric } 5944e8d8bef9SDimitry Andric 59450b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, 59460b57cec5SDimitry Andric options::OPT_finstrument_functions_after_inlining, 59470b57cec5SDimitry Andric options::OPT_finstrument_function_entry_bare); 59480b57cec5SDimitry Andric 59495ffd83dbSDimitry Andric // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support 59505ffd83dbSDimitry Andric // for sampling, overhead of call arc collection is way too high and there's 59515ffd83dbSDimitry Andric // no way to collect the output. 59525ffd83dbSDimitry Andric if (!Triple.isNVPTX() && !Triple.isAMDGCN()) 595306c3fb27SDimitry Andric addPGOAndCoverageFlags(TC, C, JA, Output, Args, SanitizeArgs, CmdArgs); 59540b57cec5SDimitry Andric 59550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); 59560b57cec5SDimitry Andric 5957bdd1243dSDimitry Andric if (getLastProfileSampleUseArg(Args) && 5958bdd1243dSDimitry Andric Args.hasArg(options::OPT_fsample_profile_use_profi)) { 5959bdd1243dSDimitry Andric CmdArgs.push_back("-mllvm"); 5960bdd1243dSDimitry Andric CmdArgs.push_back("-sample-profile-use-profi"); 5961bdd1243dSDimitry Andric } 5962bdd1243dSDimitry Andric 596381ad6265SDimitry Andric // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled. 596481ad6265SDimitry Andric if (RawTriple.isPS() && 59650b57cec5SDimitry Andric !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 596681ad6265SDimitry Andric PScpu::addProfileRTArgs(TC, Args, CmdArgs); 596781ad6265SDimitry Andric PScpu::addSanitizerArgs(TC, Args, CmdArgs); 59680b57cec5SDimitry Andric } 59690b57cec5SDimitry Andric 59700b57cec5SDimitry Andric // Pass options for controlling the default header search paths. 59710b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdinc)) { 59720b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 59730b57cec5SDimitry Andric CmdArgs.push_back("-nobuiltininc"); 59740b57cec5SDimitry Andric } else { 59750b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdlibinc)) 59760b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 59770b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 59780b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 59790b57cec5SDimitry Andric } 59800b57cec5SDimitry Andric 59810b57cec5SDimitry Andric // Pass the path to compiler resource files. 59820b57cec5SDimitry Andric CmdArgs.push_back("-resource-dir"); 59830b57cec5SDimitry Andric CmdArgs.push_back(D.ResourceDir.c_str()); 59840b57cec5SDimitry Andric 59850b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_working_directory); 59860b57cec5SDimitry Andric 59870b57cec5SDimitry Andric RenderARCMigrateToolOptions(D, Args, CmdArgs); 59880b57cec5SDimitry Andric 59890b57cec5SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 59900b57cec5SDimitry Andric // preprocessor. 59910b57cec5SDimitry Andric // 59920b57cec5SDimitry Andric // FIXME: Support -fpreprocessed 59930b57cec5SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 59940b57cec5SDimitry Andric AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 59950b57cec5SDimitry Andric 59960b57cec5SDimitry Andric // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 59970b57cec5SDimitry Andric // that "The compiler can only warn and ignore the option if not recognized". 59980b57cec5SDimitry Andric // When building with ccache, it will pass -D options to clang even on 59990b57cec5SDimitry Andric // preprocessed inputs and configure concludes that -fPIC is not supported. 60000b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_D); 60010b57cec5SDimitry Andric 60020b57cec5SDimitry Andric // Manually translate -O4 to -O3; let clang reject others. 60030b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 60040b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 60050b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 60060b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 60070b57cec5SDimitry Andric } else { 60080b57cec5SDimitry Andric A->render(Args, CmdArgs); 60090b57cec5SDimitry Andric } 60100b57cec5SDimitry Andric } 60110b57cec5SDimitry Andric 60120b57cec5SDimitry Andric // Warn about ignored options to clang. 60130b57cec5SDimitry Andric for (const Arg *A : 60140b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) { 60150b57cec5SDimitry Andric D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args); 60160b57cec5SDimitry Andric A->claim(); 60170b57cec5SDimitry Andric } 60180b57cec5SDimitry Andric 60190b57cec5SDimitry Andric for (const Arg *A : 60200b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) { 60210b57cec5SDimitry Andric D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args); 60220b57cec5SDimitry Andric A->claim(); 60230b57cec5SDimitry Andric } 60240b57cec5SDimitry Andric 60250b57cec5SDimitry Andric claimNoWarnArgs(Args); 60260b57cec5SDimitry Andric 60270b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_R_Group); 60280b57cec5SDimitry Andric 6029349cc55cSDimitry Andric for (const Arg *A : 6030349cc55cSDimitry Andric Args.filtered(options::OPT_W_Group, options::OPT__SLASH_wd)) { 6031349cc55cSDimitry Andric A->claim(); 6032349cc55cSDimitry Andric if (A->getOption().getID() == options::OPT__SLASH_wd) { 6033349cc55cSDimitry Andric unsigned WarningNumber; 6034349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, WarningNumber)) { 6035349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 6036349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 6037349cc55cSDimitry Andric continue; 6038349cc55cSDimitry Andric } 6039349cc55cSDimitry Andric 6040349cc55cSDimitry Andric if (auto Group = diagGroupFromCLWarningID(WarningNumber)) { 6041349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 6042349cc55cSDimitry Andric "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group))); 6043349cc55cSDimitry Andric } 6044349cc55cSDimitry Andric continue; 6045349cc55cSDimitry Andric } 6046349cc55cSDimitry Andric A->render(Args, CmdArgs); 6047349cc55cSDimitry Andric } 6048349cc55cSDimitry Andric 60495f757f3fSDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_Wsystem_headers_in_module_EQ); 60505f757f3fSDimitry Andric 60510b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 60520b57cec5SDimitry Andric CmdArgs.push_back("-pedantic"); 60530b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 60540b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_w); 60550b57cec5SDimitry Andric 6056bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_ffixed_point, 6057bdd1243dSDimitry Andric options::OPT_fno_fixed_point); 60580b57cec5SDimitry Andric 6059fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ)) 6060fe6060f1SDimitry Andric A->render(Args, CmdArgs); 6061fe6060f1SDimitry Andric 6062fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 6063fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 6064fe6060f1SDimitry Andric 60655f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_omit_vtable_rtti, 60665f757f3fSDimitry Andric options::OPT_fno_experimental_omit_vtable_rtti); 60675f757f3fSDimitry Andric 6068349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) 6069349cc55cSDimitry Andric A->render(Args, CmdArgs); 6070349cc55cSDimitry Andric 60710b57cec5SDimitry Andric // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 60720b57cec5SDimitry Andric // (-ansi is equivalent to -std=c89 or -std=c++98). 60730b57cec5SDimitry Andric // 60740b57cec5SDimitry Andric // If a std is supplied, only add -trigraphs if it follows the 60750b57cec5SDimitry Andric // option. 6076fe6060f1SDimitry Andric bool ImplyVCPPCVer = false; 60770b57cec5SDimitry Andric bool ImplyVCPPCXXVer = false; 60780b57cec5SDimitry Andric const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); 60790b57cec5SDimitry Andric if (Std) { 60800b57cec5SDimitry Andric if (Std->getOption().matches(options::OPT_ansi)) 60810b57cec5SDimitry Andric if (types::isCXX(InputType)) 60820b57cec5SDimitry Andric CmdArgs.push_back("-std=c++98"); 60830b57cec5SDimitry Andric else 60840b57cec5SDimitry Andric CmdArgs.push_back("-std=c89"); 60850b57cec5SDimitry Andric else 60860b57cec5SDimitry Andric Std->render(Args, CmdArgs); 60870b57cec5SDimitry Andric 60880b57cec5SDimitry Andric // If -f(no-)trigraphs appears after the language standard flag, honor it. 60890b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 60900b57cec5SDimitry Andric options::OPT_ftrigraphs, 60910b57cec5SDimitry Andric options::OPT_fno_trigraphs)) 60920b57cec5SDimitry Andric if (A != Std) 60930b57cec5SDimitry Andric A->render(Args, CmdArgs); 60940b57cec5SDimitry Andric } else { 60950b57cec5SDimitry Andric // Honor -std-default. 60960b57cec5SDimitry Andric // 60970b57cec5SDimitry Andric // FIXME: Clang doesn't correctly handle -std= when the input language 60980b57cec5SDimitry Andric // doesn't match. For the time being just ignore this for C++ inputs; 60990b57cec5SDimitry Andric // eventually we want to do all the standard defaulting here instead of 61000b57cec5SDimitry Andric // splitting it between the driver and clang -cc1. 6101fe6060f1SDimitry Andric if (!types::isCXX(InputType)) { 6102fe6060f1SDimitry Andric if (!Args.hasArg(options::OPT__SLASH_std)) { 61030b57cec5SDimitry Andric Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", 61040b57cec5SDimitry Andric /*Joined=*/true); 6105fe6060f1SDimitry Andric } else 6106fe6060f1SDimitry Andric ImplyVCPPCVer = true; 6107fe6060f1SDimitry Andric } 61080b57cec5SDimitry Andric else if (IsWindowsMSVC) 61090b57cec5SDimitry Andric ImplyVCPPCXXVer = true; 61100b57cec5SDimitry Andric 61110b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs, 61120b57cec5SDimitry Andric options::OPT_fno_trigraphs); 61130b57cec5SDimitry Andric } 61140b57cec5SDimitry Andric 61150b57cec5SDimitry Andric // GCC's behavior for -Wwrite-strings is a bit strange: 61160b57cec5SDimitry Andric // * In C, this "warning flag" changes the types of string literals from 61170b57cec5SDimitry Andric // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning 61180b57cec5SDimitry Andric // for the discarded qualifier. 61190b57cec5SDimitry Andric // * In C++, this is just a normal warning flag. 61200b57cec5SDimitry Andric // 61210b57cec5SDimitry Andric // Implementing this warning correctly in C is hard, so we follow GCC's 61220b57cec5SDimitry Andric // behavior for now. FIXME: Directly diagnose uses of a string literal as 61230b57cec5SDimitry Andric // a non-const char* in C, rather than using this crude hack. 61240b57cec5SDimitry Andric if (!types::isCXX(InputType)) { 61250b57cec5SDimitry Andric // FIXME: This should behave just like a warning flag, and thus should also 61260b57cec5SDimitry Andric // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on. 61270b57cec5SDimitry Andric Arg *WriteStrings = 61280b57cec5SDimitry Andric Args.getLastArg(options::OPT_Wwrite_strings, 61290b57cec5SDimitry Andric options::OPT_Wno_write_strings, options::OPT_w); 61300b57cec5SDimitry Andric if (WriteStrings && 61310b57cec5SDimitry Andric WriteStrings->getOption().matches(options::OPT_Wwrite_strings)) 61320b57cec5SDimitry Andric CmdArgs.push_back("-fconst-strings"); 61330b57cec5SDimitry Andric } 61340b57cec5SDimitry Andric 61350b57cec5SDimitry Andric // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 61360b57cec5SDimitry Andric // during C++ compilation, which it is by default. GCC keeps this define even 61370b57cec5SDimitry Andric // in the presence of '-w', match this behavior bug-for-bug. 61380b57cec5SDimitry Andric if (types::isCXX(InputType) && 61390b57cec5SDimitry Andric Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 61400b57cec5SDimitry Andric true)) { 61410b57cec5SDimitry Andric CmdArgs.push_back("-fdeprecated-macro"); 61420b57cec5SDimitry Andric } 61430b57cec5SDimitry Andric 61440b57cec5SDimitry Andric // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 61450b57cec5SDimitry Andric if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 61460b57cec5SDimitry Andric if (Asm->getOption().matches(options::OPT_fasm)) 61470b57cec5SDimitry Andric CmdArgs.push_back("-fgnu-keywords"); 61480b57cec5SDimitry Andric else 61490b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-keywords"); 61500b57cec5SDimitry Andric } 61510b57cec5SDimitry Andric 6152480093f4SDimitry Andric if (!ShouldEnableAutolink(Args, TC, JA)) 61530b57cec5SDimitry Andric CmdArgs.push_back("-fno-autolink"); 61540b57cec5SDimitry Andric 615506c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftemplate_depth_EQ); 615606c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_foperator_arrow_depth_EQ); 615706c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_depth_EQ); 615806c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_steps_EQ); 61590b57cec5SDimitry Andric 6160fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_library); 616181ad6265SDimitry Andric 6162a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter)) 6163a7dea167SDimitry Andric CmdArgs.push_back("-fexperimental-new-constant-interpreter"); 6164a7dea167SDimitry Andric 61650b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 61660b57cec5SDimitry Andric CmdArgs.push_back("-fbracket-depth"); 61670b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 61680b57cec5SDimitry Andric } 61690b57cec5SDimitry Andric 61700b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 61710b57cec5SDimitry Andric options::OPT_Wlarge_by_value_copy_def)) { 61720b57cec5SDimitry Andric if (A->getNumValues()) { 61730b57cec5SDimitry Andric StringRef bytes = A->getValue(); 61740b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 61750b57cec5SDimitry Andric } else 61760b57cec5SDimitry Andric CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 61770b57cec5SDimitry Andric } 61780b57cec5SDimitry Andric 61790b57cec5SDimitry Andric if (Args.hasArg(options::OPT_relocatable_pch)) 61800b57cec5SDimitry Andric CmdArgs.push_back("-relocatable-pch"); 61810b57cec5SDimitry Andric 61820b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) { 61830b57cec5SDimitry Andric static const char *kCFABIs[] = { 61840b57cec5SDimitry Andric "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1", 61850b57cec5SDimitry Andric }; 61860b57cec5SDimitry Andric 618781ad6265SDimitry Andric if (!llvm::is_contained(kCFABIs, StringRef(A->getValue()))) 61880b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue(); 61890b57cec5SDimitry Andric else 61900b57cec5SDimitry Andric A->render(Args, CmdArgs); 61910b57cec5SDimitry Andric } 61920b57cec5SDimitry Andric 61930b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 61940b57cec5SDimitry Andric CmdArgs.push_back("-fconstant-string-class"); 61950b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 61960b57cec5SDimitry Andric } 61970b57cec5SDimitry Andric 61980b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 61990b57cec5SDimitry Andric CmdArgs.push_back("-ftabstop"); 62000b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 62010b57cec5SDimitry Andric } 62020b57cec5SDimitry Andric 6203bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_size_section, 6204bdd1243dSDimitry Andric options::OPT_fno_stack_size_section); 62050b57cec5SDimitry Andric 6206fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fstack_usage)) { 6207fe6060f1SDimitry Andric CmdArgs.push_back("-stack-usage-file"); 6208fe6060f1SDimitry Andric 6209fe6060f1SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 6210fe6060f1SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 6211fe6060f1SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "su"); 6212fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 6213fe6060f1SDimitry Andric } else 6214fe6060f1SDimitry Andric CmdArgs.push_back( 6215fe6060f1SDimitry Andric Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su")); 6216fe6060f1SDimitry Andric } 6217fe6060f1SDimitry Andric 62180b57cec5SDimitry Andric CmdArgs.push_back("-ferror-limit"); 62190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 62200b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 62210b57cec5SDimitry Andric else 62220b57cec5SDimitry Andric CmdArgs.push_back("19"); 62230b57cec5SDimitry Andric 622406c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_backtrace_limit_EQ); 622506c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmacro_backtrace_limit_EQ); 622606c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftemplate_backtrace_limit_EQ); 622706c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fspell_checking_limit_EQ); 622806c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fcaret_diagnostics_max_lines_EQ); 62290b57cec5SDimitry Andric 62300b57cec5SDimitry Andric // Pass -fmessage-length=. 62315ffd83dbSDimitry Andric unsigned MessageLength = 0; 62320b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 62335ffd83dbSDimitry Andric StringRef V(A->getValue()); 62345ffd83dbSDimitry Andric if (V.getAsInteger(0, MessageLength)) 62355ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 62365ffd83dbSDimitry Andric << V << A->getOption().getName(); 62370b57cec5SDimitry Andric } else { 62380b57cec5SDimitry Andric // If -fmessage-length=N was not specified, determine whether this is a 62390b57cec5SDimitry Andric // terminal and, if so, implicitly define -fmessage-length appropriately. 62405ffd83dbSDimitry Andric MessageLength = llvm::sys::Process::StandardErrColumns(); 62410b57cec5SDimitry Andric } 62425ffd83dbSDimitry Andric if (MessageLength != 0) 62435ffd83dbSDimitry Andric CmdArgs.push_back( 62445ffd83dbSDimitry Andric Args.MakeArgString("-fmessage-length=" + Twine(MessageLength))); 62450b57cec5SDimitry Andric 624681ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_EQ)) 624781ad6265SDimitry Andric CmdArgs.push_back( 624881ad6265SDimitry Andric Args.MakeArgString("-frandomize-layout-seed=" + Twine(A->getValue(0)))); 624981ad6265SDimitry Andric 625081ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_file_EQ)) 625181ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frandomize-layout-seed-file=" + 625281ad6265SDimitry Andric Twine(A->getValue(0)))); 625381ad6265SDimitry Andric 62540b57cec5SDimitry Andric // -fvisibility= and -fvisibility-ms-compat are of a piece. 62550b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 62560b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 62570b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 6258bdd1243dSDimitry Andric A->render(Args, CmdArgs); 62590b57cec5SDimitry Andric } else { 62600b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 6261bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=hidden"); 6262bdd1243dSDimitry Andric CmdArgs.push_back("-ftype-visibility=default"); 62630b57cec5SDimitry Andric } 626404eeddc0SDimitry Andric } else if (IsOpenMPDevice) { 626504eeddc0SDimitry Andric // When compiling for the OpenMP device we want protected visibility by 6266bdd1243dSDimitry Andric // default. This prevents the device from accidentally preempting code on 6267bdd1243dSDimitry Andric // the host, makes the system more robust, and improves performance. 6268bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=protected"); 62690b57cec5SDimitry Andric } 62700b57cec5SDimitry Andric 6271bdd1243dSDimitry Andric // PS4/PS5 process these options in addClangTargetOptions. 6272bdd1243dSDimitry Andric if (!RawTriple.isPS()) { 6273e8d8bef9SDimitry Andric if (const Arg *A = 6274e8d8bef9SDimitry Andric Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass, 6275e8d8bef9SDimitry Andric options::OPT_fno_visibility_from_dllstorageclass)) { 6276e8d8bef9SDimitry Andric if (A->getOption().matches( 6277e8d8bef9SDimitry Andric options::OPT_fvisibility_from_dllstorageclass)) { 6278e8d8bef9SDimitry Andric CmdArgs.push_back("-fvisibility-from-dllstorageclass"); 6279e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_dllexport_EQ); 6280e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_nodllstorageclass_EQ); 6281e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_externs_dllimport_EQ); 6282e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, 6283e8d8bef9SDimitry Andric options::OPT_fvisibility_externs_nodllstorageclass_EQ); 6284e8d8bef9SDimitry Andric } 6285e8d8bef9SDimitry Andric } 6286bdd1243dSDimitry Andric } 6287e8d8bef9SDimitry Andric 6288fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden, 6289fe6060f1SDimitry Andric options::OPT_fno_visibility_inlines_hidden, false)) 6290fe6060f1SDimitry Andric CmdArgs.push_back("-fvisibility-inlines-hidden"); 6291fe6060f1SDimitry Andric 6292e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, 6293e8d8bef9SDimitry Andric options::OPT_fno_visibility_inlines_hidden_static_local_var); 62940b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); 62950b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 62960b57cec5SDimitry Andric 62974824e7fdSDimitry Andric if (Args.hasFlag(options::OPT_fnew_infallible, 62984824e7fdSDimitry Andric options::OPT_fno_new_infallible, false)) 62994824e7fdSDimitry Andric CmdArgs.push_back("-fnew-infallible"); 63004824e7fdSDimitry Andric 6301fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fno_operator_names, 6302fe6060f1SDimitry Andric options::OPT_foperator_names, false)) 6303fe6060f1SDimitry Andric CmdArgs.push_back("-fno-operator-names"); 6304fe6060f1SDimitry Andric 63050b57cec5SDimitry Andric // Forward -f (flag) options which we can pass directly. 63060b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 63070b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 63080b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); 630981ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); 631081ad6265SDimitry Andric 631106c3fb27SDimitry Andric if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls, 631206c3fb27SDimitry Andric Triple.hasDefaultEmulatedTLS())) 631306c3fb27SDimitry Andric CmdArgs.push_back("-femulated-tls"); 631406c3fb27SDimitry Andric 631506c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcheck_new, 631606c3fb27SDimitry Andric options::OPT_fno_check_new); 631706c3fb27SDimitry Andric 631881ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { 631981ad6265SDimitry Andric // FIXME: There's no reason for this to be restricted to X86. The backend 632081ad6265SDimitry Andric // code needs to be changed to include the appropriate function calls 632181ad6265SDimitry Andric // automatically. 632281ad6265SDimitry Andric if (!Triple.isX86() && !Triple.isAArch64()) 632381ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 632481ad6265SDimitry Andric << A->getAsString(Args) << TripleStr; 632581ad6265SDimitry Andric } 63260b57cec5SDimitry Andric 63270b57cec5SDimitry Andric // AltiVec-like language extensions aren't relevant for assembling. 63280b57cec5SDimitry Andric if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) 63290b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzvector); 63300b57cec5SDimitry Andric 63310b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 63320b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 63330b57cec5SDimitry Andric 63340b57cec5SDimitry Andric // Forward flags for OpenMP. We don't do this if the current action is an 63350b57cec5SDimitry Andric // device offloading action other than OpenMP. 63360b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, 63370b57cec5SDimitry Andric options::OPT_fno_openmp, false) && 63380b57cec5SDimitry Andric (JA.isDeviceOffloading(Action::OFK_None) || 63390b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_OpenMP))) { 63400b57cec5SDimitry Andric switch (D.getOpenMPRuntime(Args)) { 63410b57cec5SDimitry Andric case Driver::OMPRT_OMP: 63420b57cec5SDimitry Andric case Driver::OMPRT_IOMP5: 63430b57cec5SDimitry Andric // Clang can generate useful OpenMP code for these two runtime libraries. 63440b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp"); 63450b57cec5SDimitry Andric 63460b57cec5SDimitry Andric // If no option regarding the use of TLS in OpenMP codegeneration is 63470b57cec5SDimitry Andric // given, decide a default based on the target. Otherwise rely on the 63480b57cec5SDimitry Andric // options and pass the right information to the frontend. 63490b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_use_tls, 63500b57cec5SDimitry Andric options::OPT_fnoopenmp_use_tls, /*Default=*/true)) 63510b57cec5SDimitry Andric CmdArgs.push_back("-fnoopenmp-use-tls"); 63520b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 63530b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 6354480093f4SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder); 63550b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 6356349cc55cSDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_extensions, 6357349cc55cSDimitry Andric options::OPT_fno_openmp_extensions, /*Default=*/true)) 6358349cc55cSDimitry Andric CmdArgs.push_back("-fno-openmp-extensions"); 63590b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ); 63600b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ); 63610b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 63620b57cec5SDimitry Andric options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ); 63630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse, 63640b57cec5SDimitry Andric options::OPT_fno_openmp_optimistic_collapse, 63650b57cec5SDimitry Andric /*Default=*/false)) 63660b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-optimistic-collapse"); 63670b57cec5SDimitry Andric 63680b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, forward 63690b57cec5SDimitry Andric // cuda-mode flag 63700b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_mode, 63710b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_mode, /*Default=*/false)) 63720b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-mode"); 63730b57cec5SDimitry Andric 6374349cc55cSDimitry Andric // When in OpenMP offloading mode, enable debugging on the device. 6375349cc55cSDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ); 6376349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_debug, 6377349cc55cSDimitry Andric options::OPT_fno_openmp_target_debug, /*Default=*/false)) 6378349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-target-debug"); 6379349cc55cSDimitry Andric 6380349cc55cSDimitry Andric // When in OpenMP offloading mode, forward assumptions information about 6381349cc55cSDimitry Andric // thread and team counts in the device. 6382349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription, 6383349cc55cSDimitry Andric options::OPT_fno_openmp_assume_teams_oversubscription, 6384349cc55cSDimitry Andric /*Default=*/false)) 6385349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-teams-oversubscription"); 6386349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription, 6387349cc55cSDimitry Andric options::OPT_fno_openmp_assume_threads_oversubscription, 6388349cc55cSDimitry Andric /*Default=*/false)) 6389349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-threads-oversubscription"); 639081ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_thread_state)) 639181ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-thread-state"); 6392bdd1243dSDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_nested_parallelism)) 6393bdd1243dSDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-nested-parallelism"); 639481ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_offload_mandatory)) 639581ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-offload-mandatory"); 63960b57cec5SDimitry Andric break; 63970b57cec5SDimitry Andric default: 63980b57cec5SDimitry Andric // By default, if Clang doesn't know how to generate useful OpenMP code 63990b57cec5SDimitry Andric // for a specific runtime library, we just don't pass the '-fopenmp' flag 64000b57cec5SDimitry Andric // down to the actual compilation. 64010b57cec5SDimitry Andric // FIXME: It would be better to have a mode which *only* omits IR 64020b57cec5SDimitry Andric // generation based on the OpenMP support so that we get consistent 64030b57cec5SDimitry Andric // semantic analysis, etc. 64040b57cec5SDimitry Andric break; 64050b57cec5SDimitry Andric } 64060b57cec5SDimitry Andric } else { 64070b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 64080b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 64090b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 641081ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fopenmp_extensions, 641181ad6265SDimitry Andric options::OPT_fno_openmp_extensions); 64120b57cec5SDimitry Andric } 64130b57cec5SDimitry Andric 641481ad6265SDimitry Andric // Forward the new driver to change offloading code generation. 6415bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_offload_new_driver, 6416bdd1243dSDimitry Andric options::OPT_no_offload_new_driver, false)) 641781ad6265SDimitry Andric CmdArgs.push_back("--offload-new-driver"); 641881ad6265SDimitry Andric 6419349cc55cSDimitry Andric SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); 64200b57cec5SDimitry Andric 64210b57cec5SDimitry Andric const XRayArgs &XRay = TC.getXRayArgs(); 64220b57cec5SDimitry Andric XRay.addArgs(TC, Args, CmdArgs, InputType); 64230b57cec5SDimitry Andric 6424e8d8bef9SDimitry Andric for (const auto &Filename : 6425e8d8bef9SDimitry Andric Args.getAllArgValues(options::OPT_fprofile_list_EQ)) { 6426e8d8bef9SDimitry Andric if (D.getVFS().exists(Filename)) 6427e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-list=" + Filename)); 6428e8d8bef9SDimitry Andric else 6429e8d8bef9SDimitry Andric D.Diag(clang::diag::err_drv_no_such_file) << Filename; 6430e8d8bef9SDimitry Andric } 6431e8d8bef9SDimitry Andric 6432480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) { 6433480093f4SDimitry Andric StringRef S0 = A->getValue(), S = S0; 643455e4f9d5SDimitry Andric unsigned Size, Offset = 0; 643506c3fb27SDimitry Andric if (!Triple.isAArch64() && !Triple.isLoongArch() && !Triple.isRISCV() && 643606c3fb27SDimitry Andric !Triple.isX86()) 6437480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6438480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6439480093f4SDimitry Andric else if (S.consumeInteger(10, Size) || 6440480093f4SDimitry Andric (!S.empty() && (!S.consume_front(",") || 644155e4f9d5SDimitry Andric S.consumeInteger(10, Offset) || !S.empty()))) 6442480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 6443480093f4SDimitry Andric << S0 << A->getOption().getName(); 644455e4f9d5SDimitry Andric else if (Size < Offset) 6445480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument); 644655e4f9d5SDimitry Andric else { 6447480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size))); 644855e4f9d5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 644955e4f9d5SDimitry Andric "-fpatchable-function-entry-offset=" + Twine(Offset))); 645055e4f9d5SDimitry Andric } 6451480093f4SDimitry Andric } 6452480093f4SDimitry Andric 645304eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch); 645404eeddc0SDimitry Andric 6455480093f4SDimitry Andric if (TC.SupportsProfiling()) { 64560b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pg); 64570b57cec5SDimitry Andric 6458480093f4SDimitry Andric llvm::Triple::ArchType Arch = TC.getArch(); 6459480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfentry)) { 6460480093f4SDimitry Andric if (Arch == llvm::Triple::systemz || TC.getTriple().isX86()) 6461480093f4SDimitry Andric A->render(Args, CmdArgs); 6462480093f4SDimitry Andric else 6463480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6464480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6465480093f4SDimitry Andric } 6466480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) { 6467480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6468480093f4SDimitry Andric A->render(Args, CmdArgs); 6469480093f4SDimitry Andric else 6470480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6471480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6472480093f4SDimitry Andric } 6473480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) { 6474480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6475480093f4SDimitry Andric A->render(Args, CmdArgs); 6476480093f4SDimitry Andric else 6477480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6478480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6479480093f4SDimitry Andric } 6480480093f4SDimitry Andric } 648106c3fb27SDimitry Andric 648206c3fb27SDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_pg)) { 648306c3fb27SDimitry Andric if (TC.getTriple().isOSzOS()) { 6484bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6485bdd1243dSDimitry Andric << A->getAsString(Args) << TripleStr; 6486bdd1243dSDimitry Andric } 6487bdd1243dSDimitry Andric } 648806c3fb27SDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_p)) { 648906c3fb27SDimitry Andric if (!(TC.getTriple().isOSAIX() || TC.getTriple().isOSOpenBSD())) { 649006c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 649106c3fb27SDimitry Andric << A->getAsString(Args) << TripleStr; 649206c3fb27SDimitry Andric } 649306c3fb27SDimitry Andric } 649406c3fb27SDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) { 649506c3fb27SDimitry Andric if (A->getOption().matches(options::OPT_p)) { 649606c3fb27SDimitry Andric A->claim(); 649706c3fb27SDimitry Andric if (TC.getTriple().isOSAIX() && !Args.hasArgNoClaim(options::OPT_pg)) 649806c3fb27SDimitry Andric CmdArgs.push_back("-pg"); 649906c3fb27SDimitry Andric } 650006c3fb27SDimitry Andric } 650106c3fb27SDimitry Andric 650206c3fb27SDimitry Andric // Reject AIX-specific link options on other targets. 650306c3fb27SDimitry Andric if (!TC.getTriple().isOSAIX()) { 650406c3fb27SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_b, options::OPT_K, 650506c3fb27SDimitry Andric options::OPT_mxcoff_build_id_EQ)) { 650606c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 650706c3fb27SDimitry Andric << A->getSpelling() << TripleStr; 650806c3fb27SDimitry Andric } 650906c3fb27SDimitry Andric } 65100b57cec5SDimitry Andric 65110b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_fapple_kext) || 65120b57cec5SDimitry Andric (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) 65130b57cec5SDimitry Andric CmdArgs.push_back("-fapple-kext"); 65140b57cec5SDimitry Andric 6515fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat); 6516a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ); 65170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 65180b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 65190b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 65200b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 6521e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ); 65220b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 65230b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_malign_double); 6524480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file); 65250b57cec5SDimitry Andric 652606c3fb27SDimitry Andric if (const char *Name = C.getTimeTraceFile(&JA)) { 652706c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ftime-trace=" + Twine(Name))); 652806c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); 652906c3fb27SDimitry Andric } 653006c3fb27SDimitry Andric 65310b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 65320b57cec5SDimitry Andric CmdArgs.push_back("-ftrapv-handler"); 65330b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 65340b57cec5SDimitry Andric } 65350b57cec5SDimitry Andric 65360b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 65370b57cec5SDimitry Andric 65380b57cec5SDimitry Andric // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 65390b57cec5SDimitry Andric // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 65400b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) { 65410b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fwrapv)) 65420b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 65430b57cec5SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 65440b57cec5SDimitry Andric options::OPT_fno_strict_overflow)) { 65450b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_strict_overflow)) 65460b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 65470b57cec5SDimitry Andric } 65480b57cec5SDimitry Andric 65490b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_freroll_loops, 65500b57cec5SDimitry Andric options::OPT_fno_reroll_loops)) 65510b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_freroll_loops)) 65520b57cec5SDimitry Andric CmdArgs.push_back("-freroll-loops"); 65530b57cec5SDimitry Andric 6554d409305fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops, 6555d409305fSDimitry Andric options::OPT_fno_finite_loops); 6556d409305fSDimitry Andric 65570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 65580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 65590b57cec5SDimitry Andric options::OPT_fno_unroll_loops); 65600b57cec5SDimitry Andric 6561fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fstrict_flex_arrays_EQ); 6562fcaf7f86SDimitry Andric 65630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pthread); 65640b57cec5SDimitry Andric 6565bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mspeculative_load_hardening, 6566bdd1243dSDimitry Andric options::OPT_mno_speculative_load_hardening); 65670b57cec5SDimitry Andric 6568e8d8bef9SDimitry Andric RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext); 65695ffd83dbSDimitry Andric RenderSCPOptions(TC, Args, CmdArgs); 65700b57cec5SDimitry Andric RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs); 65710b57cec5SDimitry Andric 6572349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fswift_async_fp_EQ); 6573349cc55cSDimitry Andric 6574bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mstackrealign, 6575bdd1243dSDimitry Andric options::OPT_mno_stackrealign); 65760b57cec5SDimitry Andric 65770b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_alignment)) { 65780b57cec5SDimitry Andric StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 65790b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 65800b57cec5SDimitry Andric } 65810b57cec5SDimitry Andric 65820b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_probe_size)) { 65830b57cec5SDimitry Andric StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size); 65840b57cec5SDimitry Andric 65850b57cec5SDimitry Andric if (!Size.empty()) 65860b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size)); 65870b57cec5SDimitry Andric else 65880b57cec5SDimitry Andric CmdArgs.push_back("-mstack-probe-size=0"); 65890b57cec5SDimitry Andric } 65900b57cec5SDimitry Andric 659181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_mstack_arg_probe, 659281ad6265SDimitry Andric options::OPT_mno_stack_arg_probe); 65930b57cec5SDimitry Andric 65940b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it, 65950b57cec5SDimitry Andric options::OPT_mno_restrict_it)) { 65960b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mrestrict_it)) { 65970b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 65980b57cec5SDimitry Andric CmdArgs.push_back("-arm-restrict-it"); 65990b57cec5SDimitry Andric } else { 66000b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 660181ad6265SDimitry Andric CmdArgs.push_back("-arm-default-it"); 66020b57cec5SDimitry Andric } 66030b57cec5SDimitry Andric } 66040b57cec5SDimitry Andric 66050b57cec5SDimitry Andric // Forward -cl options to -cc1 6606fe6060f1SDimitry Andric RenderOpenCLOptions(Args, CmdArgs, InputType); 66070b57cec5SDimitry Andric 660881ad6265SDimitry Andric // Forward hlsl options to -cc1 660981ad6265SDimitry Andric RenderHLSLOptions(Args, CmdArgs, InputType); 661081ad6265SDimitry Andric 66115f757f3fSDimitry Andric // Forward OpenACC options to -cc1 66125f757f3fSDimitry Andric RenderOpenACCOptions(D, Args, CmdArgs, InputType); 66135f757f3fSDimitry Andric 6614e8d8bef9SDimitry Andric if (IsHIP) { 6615e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fhip_new_launch_api, 66165ffd83dbSDimitry Andric options::OPT_fno_hip_new_launch_api, true)) 6617a7dea167SDimitry Andric CmdArgs.push_back("-fhip-new-launch-api"); 66185f757f3fSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fgpu_allow_device_init, 66195f757f3fSDimitry Andric options::OPT_fno_gpu_allow_device_init); 66205f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_hipstdpar); 66215f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_hipstdpar_interpose_alloc); 662281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fhip_kernel_arg_name, 662381ad6265SDimitry Andric options::OPT_fno_hip_kernel_arg_name); 6624e8d8bef9SDimitry Andric } 6625e8d8bef9SDimitry Andric 6626e8d8bef9SDimitry Andric if (IsCuda || IsHIP) { 6627753f127fSDimitry Andric if (IsRDCMode) 6628fe6060f1SDimitry Andric CmdArgs.push_back("-fgpu-rdc"); 66295f757f3fSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fgpu_defer_diag, 66305f757f3fSDimitry Andric options::OPT_fno_gpu_defer_diag); 6631e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads, 6632e8d8bef9SDimitry Andric options::OPT_fno_gpu_exclude_wrong_side_overloads, 6633e8d8bef9SDimitry Andric false)) { 6634e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-exclude-wrong-side-overloads"); 6635e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6636e8d8bef9SDimitry Andric } 6637e8d8bef9SDimitry Andric } 6638a7dea167SDimitry Andric 663981ad6265SDimitry Andric // Forward -nogpulib to -cc1. 664081ad6265SDimitry Andric if (Args.hasArg(options::OPT_nogpulib)) 664181ad6265SDimitry Andric CmdArgs.push_back("-nogpulib"); 664281ad6265SDimitry Andric 66430b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { 66440b57cec5SDimitry Andric CmdArgs.push_back( 66450b57cec5SDimitry Andric Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); 66460b57cec5SDimitry Andric } 66470b57cec5SDimitry Andric 6648753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ)) 6649753f127fSDimitry Andric CmdArgs.push_back( 6650753f127fSDimitry Andric Args.MakeArgString(Twine("-mfunction-return=") + A->getValue())); 6651753f127fSDimitry Andric 6652bdd1243dSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mindirect_branch_cs_prefix); 6653bdd1243dSDimitry Andric 6654fe6060f1SDimitry Andric // Forward -f options with positive and negative forms; we translate these by 6655fe6060f1SDimitry Andric // hand. Do not propagate PGO options to the GPU-side compilations as the 6656fe6060f1SDimitry Andric // profile info is for the host-side compilation only. 6657fe6060f1SDimitry Andric if (!(IsCudaDevice || IsHIPDevice)) { 66580b57cec5SDimitry Andric if (Arg *A = getLastProfileSampleUseArg(Args)) { 66590b57cec5SDimitry Andric auto *PGOArg = Args.getLastArg( 66600b57cec5SDimitry Andric options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, 6661fe6060f1SDimitry Andric options::OPT_fcs_profile_generate, 6662fe6060f1SDimitry Andric options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use, 6663fe6060f1SDimitry Andric options::OPT_fprofile_use_EQ); 66640b57cec5SDimitry Andric if (PGOArg) 66650b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 66660b57cec5SDimitry Andric << "SampleUse with PGO options"; 66670b57cec5SDimitry Andric 66680b57cec5SDimitry Andric StringRef fname = A->getValue(); 66690b57cec5SDimitry Andric if (!llvm::sys::fs::exists(fname)) 66700b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << fname; 66710b57cec5SDimitry Andric else 66720b57cec5SDimitry Andric A->render(Args, CmdArgs); 66730b57cec5SDimitry Andric } 66740b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); 66750b57cec5SDimitry Andric 6676e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, 6677fe6060f1SDimitry Andric options::OPT_fno_pseudo_probe_for_profiling, false)) { 6678e8d8bef9SDimitry Andric CmdArgs.push_back("-fpseudo-probe-for-profiling"); 6679fe6060f1SDimitry Andric // Enforce -funique-internal-linkage-names if it's not explicitly turned 6680fe6060f1SDimitry Andric // off. 6681fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_funique_internal_linkage_names, 6682fe6060f1SDimitry Andric options::OPT_fno_unique_internal_linkage_names, true)) 6683fe6060f1SDimitry Andric CmdArgs.push_back("-funique-internal-linkage-names"); 6684fe6060f1SDimitry Andric } 6685fe6060f1SDimitry Andric } 66860b57cec5SDimitry Andric RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs); 66870b57cec5SDimitry Andric 668881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 668981ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 66900b57cec5SDimitry Andric 66915f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fapinotes, options::OPT_fno_apinotes, false)) 66925f757f3fSDimitry Andric CmdArgs.push_back("-fapinotes"); 66935f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fapinotes_modules, 66945f757f3fSDimitry Andric options::OPT_fno_apinotes_modules, false)) 66955f757f3fSDimitry Andric CmdArgs.push_back("-fapinotes-modules"); 66965f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fapinotes_swift_version); 66975f757f3fSDimitry Andric 66980b57cec5SDimitry Andric // -fblocks=0 is default. 66990b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 67000b57cec5SDimitry Andric TC.IsBlocksDefault()) || 67010b57cec5SDimitry Andric (Args.hasArg(options::OPT_fgnu_runtime) && 67020b57cec5SDimitry Andric Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 67030b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_blocks))) { 67040b57cec5SDimitry Andric CmdArgs.push_back("-fblocks"); 67050b57cec5SDimitry Andric 67060b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime()) 67070b57cec5SDimitry Andric CmdArgs.push_back("-fblocks-runtime-optional"); 67080b57cec5SDimitry Andric } 67090b57cec5SDimitry Andric 67100b57cec5SDimitry Andric // -fencode-extended-block-signature=1 is default. 67110b57cec5SDimitry Andric if (TC.IsEncodeExtendedBlockSignatureDefault()) 67120b57cec5SDimitry Andric CmdArgs.push_back("-fencode-extended-block-signature"); 67130b57cec5SDimitry Andric 6714bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_fcoro_aligned_allocation, 6715bdd1243dSDimitry Andric options::OPT_fno_coro_aligned_allocation, false) && 6716bdd1243dSDimitry Andric types::isCXX(InputType)) 6717bdd1243dSDimitry Andric CmdArgs.push_back("-fcoro-aligned-allocation"); 6718bdd1243dSDimitry Andric 67190b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes, 67200b57cec5SDimitry Andric options::OPT_fno_double_square_bracket_attributes); 67210b57cec5SDimitry Andric 672281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_faccess_control, 672381ad6265SDimitry Andric options::OPT_fno_access_control); 672481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_felide_constructors, 672581ad6265SDimitry Andric options::OPT_fno_elide_constructors); 67260b57cec5SDimitry Andric 67270b57cec5SDimitry Andric ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); 67280b57cec5SDimitry Andric 67290b57cec5SDimitry Andric if (KernelOrKext || (types::isCXX(InputType) && 67300b57cec5SDimitry Andric (RTTIMode == ToolChain::RM_Disabled))) 67310b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti"); 67320b57cec5SDimitry Andric 6733e8d8bef9SDimitry Andric // -fshort-enums=0 is default for all architectures except Hexagon and z/OS. 67340b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums, 6735e8d8bef9SDimitry Andric TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS())) 67360b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 67370b57cec5SDimitry Andric 67380b57cec5SDimitry Andric RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs); 67390b57cec5SDimitry Andric 67400b57cec5SDimitry Andric // -fuse-cxa-atexit is default. 67410b57cec5SDimitry Andric if (!Args.hasFlag( 67420b57cec5SDimitry Andric options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit, 67435ffd83dbSDimitry Andric !RawTriple.isOSAIX() && !RawTriple.isOSWindows() && 67440b57cec5SDimitry Andric ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) || 67450b57cec5SDimitry Andric RawTriple.hasEnvironment())) || 67460b57cec5SDimitry Andric KernelOrKext) 67470b57cec5SDimitry Andric CmdArgs.push_back("-fno-use-cxa-atexit"); 67480b57cec5SDimitry Andric 67490b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit, 67500b57cec5SDimitry Andric options::OPT_fno_register_global_dtors_with_atexit, 67510b57cec5SDimitry Andric RawTriple.isOSDarwin() && !KernelOrKext)) 67520b57cec5SDimitry Andric CmdArgs.push_back("-fregister-global-dtors-with-atexit"); 67530b57cec5SDimitry Andric 675481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fuse_line_directives, 675581ad6265SDimitry Andric options::OPT_fno_use_line_directives); 67560b57cec5SDimitry Andric 6757349cc55cSDimitry Andric // -fno-minimize-whitespace is default. 6758349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fminimize_whitespace, 6759349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace, false)) { 6760349cc55cSDimitry Andric types::ID InputType = Inputs[0].getType(); 6761349cc55cSDimitry Andric if (!isDerivedFromC(InputType)) 67625f757f3fSDimitry Andric D.Diag(diag::err_drv_opt_unsupported_input_type) 67635f757f3fSDimitry Andric << "-fminimize-whitespace" << types::getTypeName(InputType); 6764349cc55cSDimitry Andric CmdArgs.push_back("-fminimize-whitespace"); 6765349cc55cSDimitry Andric } 6766349cc55cSDimitry Andric 67675f757f3fSDimitry Andric // -fno-keep-system-includes is default. 67685f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fkeep_system_includes, 67695f757f3fSDimitry Andric options::OPT_fno_keep_system_includes, false)) { 67705f757f3fSDimitry Andric types::ID InputType = Inputs[0].getType(); 67715f757f3fSDimitry Andric if (!isDerivedFromC(InputType)) 67725f757f3fSDimitry Andric D.Diag(diag::err_drv_opt_unsupported_input_type) 67735f757f3fSDimitry Andric << "-fkeep-system-includes" << types::getTypeName(InputType); 67745f757f3fSDimitry Andric CmdArgs.push_back("-fkeep-system-includes"); 67755f757f3fSDimitry Andric } 67765f757f3fSDimitry Andric 67775ffd83dbSDimitry Andric // -fms-extensions=0 is default. 67785ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 67795ffd83dbSDimitry Andric IsWindowsMSVC)) 67805ffd83dbSDimitry Andric CmdArgs.push_back("-fms-extensions"); 67815ffd83dbSDimitry Andric 67820b57cec5SDimitry Andric // -fms-compatibility=0 is default. 6783a7dea167SDimitry Andric bool IsMSVCCompat = Args.hasFlag( 6784a7dea167SDimitry Andric options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, 6785a7dea167SDimitry Andric (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, 6786a7dea167SDimitry Andric options::OPT_fno_ms_extensions, true))); 6787a7dea167SDimitry Andric if (IsMSVCCompat) 67880b57cec5SDimitry Andric CmdArgs.push_back("-fms-compatibility"); 67890b57cec5SDimitry Andric 6790bdd1243dSDimitry Andric if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() && 6791bdd1243dSDimitry Andric Args.hasArg(options::OPT_fms_runtime_lib_EQ)) 6792bdd1243dSDimitry Andric ProcessVSRuntimeLibrary(Args, CmdArgs); 6793bdd1243dSDimitry Andric 6794a7dea167SDimitry Andric // Handle -fgcc-version, if present. 6795a7dea167SDimitry Andric VersionTuple GNUCVer; 6796a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) { 6797a7dea167SDimitry Andric // Check that the version has 1 to 3 components and the minor and patch 6798a7dea167SDimitry Andric // versions fit in two decimal digits. 6799a7dea167SDimitry Andric StringRef Val = A->getValue(); 6800a7dea167SDimitry Andric Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable. 6801a7dea167SDimitry Andric bool Invalid = GNUCVer.tryParse(Val); 680281ad6265SDimitry Andric unsigned Minor = GNUCVer.getMinor().value_or(0); 680381ad6265SDimitry Andric unsigned Patch = GNUCVer.getSubminor().value_or(0); 6804a7dea167SDimitry Andric if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) { 6805a7dea167SDimitry Andric D.Diag(diag::err_drv_invalid_value) 6806a7dea167SDimitry Andric << A->getAsString(Args) << A->getValue(); 6807a7dea167SDimitry Andric } 6808a7dea167SDimitry Andric } else if (!IsMSVCCompat) { 6809a7dea167SDimitry Andric // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect. 6810a7dea167SDimitry Andric GNUCVer = VersionTuple(4, 2, 1); 6811a7dea167SDimitry Andric } 6812a7dea167SDimitry Andric if (!GNUCVer.empty()) { 6813a7dea167SDimitry Andric CmdArgs.push_back( 6814a7dea167SDimitry Andric Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString())); 6815a7dea167SDimitry Andric } 6816a7dea167SDimitry Andric 68170b57cec5SDimitry Andric VersionTuple MSVT = TC.computeMSVCVersion(&D, Args); 68180b57cec5SDimitry Andric if (!MSVT.empty()) 68190b57cec5SDimitry Andric CmdArgs.push_back( 68200b57cec5SDimitry Andric Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString())); 68210b57cec5SDimitry Andric 68220b57cec5SDimitry Andric bool IsMSVC2015Compatible = MSVT.getMajor() >= 19; 6823fe6060f1SDimitry Andric if (ImplyVCPPCVer) { 6824fe6060f1SDimitry Andric StringRef LanguageStandard; 6825fe6060f1SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 6826fe6060f1SDimitry Andric Std = StdArg; 6827fe6060f1SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 6828fe6060f1SDimitry Andric .Case("c11", "-std=c11") 6829fe6060f1SDimitry Andric .Case("c17", "-std=c17") 6830fe6060f1SDimitry Andric .Default(""); 6831fe6060f1SDimitry Andric if (LanguageStandard.empty()) 6832fe6060f1SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 6833fe6060f1SDimitry Andric << StdArg->getAsString(Args); 6834fe6060f1SDimitry Andric } 6835fe6060f1SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 6836fe6060f1SDimitry Andric } 68370b57cec5SDimitry Andric if (ImplyVCPPCXXVer) { 68380b57cec5SDimitry Andric StringRef LanguageStandard; 68390b57cec5SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 68400b57cec5SDimitry Andric Std = StdArg; 68410b57cec5SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 68420b57cec5SDimitry Andric .Case("c++14", "-std=c++14") 68430b57cec5SDimitry Andric .Case("c++17", "-std=c++17") 6844fe6060f1SDimitry Andric .Case("c++20", "-std=c++20") 684506c3fb27SDimitry Andric // TODO add c++23 and c++26 when MSVC supports it. 684606c3fb27SDimitry Andric .Case("c++latest", "-std=c++26") 68470b57cec5SDimitry Andric .Default(""); 68480b57cec5SDimitry Andric if (LanguageStandard.empty()) 68490b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 68500b57cec5SDimitry Andric << StdArg->getAsString(Args); 68510b57cec5SDimitry Andric } 68520b57cec5SDimitry Andric 68530b57cec5SDimitry Andric if (LanguageStandard.empty()) { 68540b57cec5SDimitry Andric if (IsMSVC2015Compatible) 68550b57cec5SDimitry Andric LanguageStandard = "-std=c++14"; 68560b57cec5SDimitry Andric else 68570b57cec5SDimitry Andric LanguageStandard = "-std=c++11"; 68580b57cec5SDimitry Andric } 68590b57cec5SDimitry Andric 68600b57cec5SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 68610b57cec5SDimitry Andric } 68620b57cec5SDimitry Andric 686381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions, 686481ad6265SDimitry Andric options::OPT_fno_borland_extensions); 68650b57cec5SDimitry Andric 686681ad6265SDimitry Andric // -fno-declspec is default, except for PS4/PS5. 68670b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec, 686881ad6265SDimitry Andric RawTriple.isPS())) 68690b57cec5SDimitry Andric CmdArgs.push_back("-fdeclspec"); 68700b57cec5SDimitry Andric else if (Args.hasArg(options::OPT_fno_declspec)) 68710b57cec5SDimitry Andric CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec. 68720b57cec5SDimitry Andric 68730b57cec5SDimitry Andric // -fthreadsafe-static is default, except for MSVC compatibility versions less 68740b57cec5SDimitry Andric // than 19. 68750b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 68760b57cec5SDimitry Andric options::OPT_fno_threadsafe_statics, 6877349cc55cSDimitry Andric !types::isOpenCL(InputType) && 6878349cc55cSDimitry Andric (!IsWindowsMSVC || IsMSVC2015Compatible))) 68790b57cec5SDimitry Andric CmdArgs.push_back("-fno-threadsafe-statics"); 68800b57cec5SDimitry Andric 68810b57cec5SDimitry Andric // -fgnu-keywords default varies depending on language; only pass if 68820b57cec5SDimitry Andric // specified. 68830b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords, 68840b57cec5SDimitry Andric options::OPT_fno_gnu_keywords); 68850b57cec5SDimitry Andric 688681ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fgnu89_inline, 688781ad6265SDimitry Andric options::OPT_fno_gnu89_inline); 68880b57cec5SDimitry Andric 688981ad6265SDimitry Andric const Arg *InlineArg = Args.getLastArg(options::OPT_finline_functions, 68900b57cec5SDimitry Andric options::OPT_finline_hint_functions, 68910b57cec5SDimitry Andric options::OPT_fno_inline_functions); 689281ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_finline, options::OPT_fno_inline)) { 689381ad6265SDimitry Andric if (A->getOption().matches(options::OPT_fno_inline)) 689481ad6265SDimitry Andric A->render(Args, CmdArgs); 689581ad6265SDimitry Andric } else if (InlineArg) { 689681ad6265SDimitry Andric InlineArg->render(Args, CmdArgs); 689781ad6265SDimitry Andric } 68980b57cec5SDimitry Andric 6899bdd1243dSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finline_max_stacksize_EQ); 6900bdd1243dSDimitry Andric 69015f757f3fSDimitry Andric // FIXME: Find a better way to determine whether we are in C++20. 69025f757f3fSDimitry Andric bool HaveCxx20 = 69035f757f3fSDimitry Andric Std && 69045f757f3fSDimitry Andric (Std->containsValue("c++2a") || Std->containsValue("gnu++2a") || 69055f757f3fSDimitry Andric Std->containsValue("c++20") || Std->containsValue("gnu++20") || 69065f757f3fSDimitry Andric Std->containsValue("c++2b") || Std->containsValue("gnu++2b") || 69075f757f3fSDimitry Andric Std->containsValue("c++23") || Std->containsValue("gnu++23") || 69085f757f3fSDimitry Andric Std->containsValue("c++2c") || Std->containsValue("gnu++2c") || 69095f757f3fSDimitry Andric Std->containsValue("c++26") || Std->containsValue("gnu++26") || 69105f757f3fSDimitry Andric Std->containsValue("c++latest") || Std->containsValue("gnu++latest")); 69110b57cec5SDimitry Andric bool HaveModules = 69125f757f3fSDimitry Andric RenderModulesOptions(C, D, Args, Input, Output, HaveCxx20, CmdArgs); 69135f757f3fSDimitry Andric 69145f757f3fSDimitry Andric // -fdelayed-template-parsing is default when targeting MSVC. 69155f757f3fSDimitry Andric // Many old Windows SDK versions require this to parse. 69165f757f3fSDimitry Andric // 69175f757f3fSDimitry Andric // According to 69185f757f3fSDimitry Andric // https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-170, 69195f757f3fSDimitry Andric // MSVC actually defaults to -fno-delayed-template-parsing (/Zc:twoPhase- 69205f757f3fSDimitry Andric // with MSVC CLI) if using C++20. So we match the behavior with MSVC here to 69215f757f3fSDimitry Andric // not enable -fdelayed-template-parsing by default after C++20. 69225f757f3fSDimitry Andric // 69235f757f3fSDimitry Andric // FIXME: Given -fdelayed-template-parsing is a source of bugs, we should be 69245f757f3fSDimitry Andric // able to disable this by default at some point. 69255f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 69265f757f3fSDimitry Andric options::OPT_fno_delayed_template_parsing, 69275f757f3fSDimitry Andric IsWindowsMSVC && !HaveCxx20)) { 69285f757f3fSDimitry Andric if (HaveCxx20) 69295f757f3fSDimitry Andric D.Diag(clang::diag::warn_drv_delayed_template_parsing_after_cxx20); 69305f757f3fSDimitry Andric 69315f757f3fSDimitry Andric CmdArgs.push_back("-fdelayed-template-parsing"); 69325f757f3fSDimitry Andric } 69330b57cec5SDimitry Andric 6934a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fpch_validate_input_files_content, 6935a7dea167SDimitry Andric options::OPT_fno_pch_validate_input_files_content, false)) 6936a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 69375ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 69385ffd83dbSDimitry Andric options::OPT_fno_pch_instantiate_templates, false)) 69395ffd83dbSDimitry Andric CmdArgs.push_back("-fpch-instantiate-templates"); 6940590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen, 6941590d96feSDimitry Andric false)) 6942590d96feSDimitry Andric CmdArgs.push_back("-fmodules-codegen"); 6943590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo, 6944590d96feSDimitry Andric false)) 6945590d96feSDimitry Andric CmdArgs.push_back("-fmodules-debuginfo"); 6946a7dea167SDimitry Andric 69475ffd83dbSDimitry Andric ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); 69480b57cec5SDimitry Andric RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, 69490b57cec5SDimitry Andric Input, CmdArgs); 69500b57cec5SDimitry Andric 6951fe6060f1SDimitry Andric if (types::isObjC(Input.getType()) && 6952fe6060f1SDimitry Andric Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec, 6953fe6060f1SDimitry Andric options::OPT_fno_objc_encode_cxx_class_template_spec, 6954fe6060f1SDimitry Andric !Runtime.isNeXTFamily())) 6955fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec"); 6956fe6060f1SDimitry Andric 69570b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fapplication_extension, 69580b57cec5SDimitry Andric options::OPT_fno_application_extension, false)) 69590b57cec5SDimitry Andric CmdArgs.push_back("-fapplication-extension"); 69600b57cec5SDimitry Andric 69610b57cec5SDimitry Andric // Handle GCC-style exception args. 6962fe6060f1SDimitry Andric bool EH = false; 69630b57cec5SDimitry Andric if (!C.getDriver().IsCLMode()) 6964fe6060f1SDimitry Andric EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); 69650b57cec5SDimitry Andric 69660b57cec5SDimitry Andric // Handle exception personalities 6967a7dea167SDimitry Andric Arg *A = Args.getLastArg( 6968a7dea167SDimitry Andric options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions, 6969a7dea167SDimitry Andric options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions); 69700b57cec5SDimitry Andric if (A) { 69710b57cec5SDimitry Andric const Option &Opt = A->getOption(); 69720b57cec5SDimitry Andric if (Opt.matches(options::OPT_fsjlj_exceptions)) 6973e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 69740b57cec5SDimitry Andric if (Opt.matches(options::OPT_fseh_exceptions)) 6975e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 69760b57cec5SDimitry Andric if (Opt.matches(options::OPT_fdwarf_exceptions)) 6977e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 6978a7dea167SDimitry Andric if (Opt.matches(options::OPT_fwasm_exceptions)) 6979e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=wasm"); 69800b57cec5SDimitry Andric } else { 69810b57cec5SDimitry Andric switch (TC.GetExceptionModel(Args)) { 69820b57cec5SDimitry Andric default: 69830b57cec5SDimitry Andric break; 69840b57cec5SDimitry Andric case llvm::ExceptionHandling::DwarfCFI: 6985e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 69860b57cec5SDimitry Andric break; 69870b57cec5SDimitry Andric case llvm::ExceptionHandling::SjLj: 6988e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 69890b57cec5SDimitry Andric break; 69900b57cec5SDimitry Andric case llvm::ExceptionHandling::WinEH: 6991e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 69920b57cec5SDimitry Andric break; 69930b57cec5SDimitry Andric } 69940b57cec5SDimitry Andric } 69950b57cec5SDimitry Andric 69960b57cec5SDimitry Andric // C++ "sane" operator new. 699781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 699881ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 69990b57cec5SDimitry Andric 700006c3fb27SDimitry Andric // -fassume-unique-vtables is on by default. 700106c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_unique_vtables, 700206c3fb27SDimitry Andric options::OPT_fno_assume_unique_vtables); 700306c3fb27SDimitry Andric 70040b57cec5SDimitry Andric // -frelaxed-template-template-args is off by default, as it is a severe 70050b57cec5SDimitry Andric // breaking change until a corresponding change to template partial ordering 70060b57cec5SDimitry Andric // is provided. 700781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_frelaxed_template_template_args, 700881ad6265SDimitry Andric options::OPT_fno_relaxed_template_template_args); 70090b57cec5SDimitry Andric 70100b57cec5SDimitry Andric // -fsized-deallocation is off by default, as it is an ABI-breaking change for 70110b57cec5SDimitry Andric // most platforms. 701281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsized_deallocation, 701381ad6265SDimitry Andric options::OPT_fno_sized_deallocation); 70140b57cec5SDimitry Andric 70150b57cec5SDimitry Andric // -faligned-allocation is on by default in C++17 onwards and otherwise off 70160b57cec5SDimitry Andric // by default. 70170b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation, 70180b57cec5SDimitry Andric options::OPT_fno_aligned_allocation, 70190b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) { 70200b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_aligned_allocation)) 70210b57cec5SDimitry Andric CmdArgs.push_back("-fno-aligned-allocation"); 70220b57cec5SDimitry Andric else 70230b57cec5SDimitry Andric CmdArgs.push_back("-faligned-allocation"); 70240b57cec5SDimitry Andric } 70250b57cec5SDimitry Andric 70260b57cec5SDimitry Andric // The default new alignment can be specified using a dedicated option or via 70270b57cec5SDimitry Andric // a GCC-compatible option that also turns on aligned allocation. 70280b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ, 70290b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) 70300b57cec5SDimitry Andric CmdArgs.push_back( 70310b57cec5SDimitry Andric Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue())); 70320b57cec5SDimitry Andric 70330b57cec5SDimitry Andric // -fconstant-cfstrings is default, and may be subject to argument translation 70340b57cec5SDimitry Andric // on Darwin. 70350b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 703681ad6265SDimitry Andric options::OPT_fno_constant_cfstrings, true) || 70370b57cec5SDimitry Andric !Args.hasFlag(options::OPT_mconstant_cfstrings, 703881ad6265SDimitry Andric options::OPT_mno_constant_cfstrings, true)) 70390b57cec5SDimitry Andric CmdArgs.push_back("-fno-constant-cfstrings"); 70400b57cec5SDimitry Andric 704181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fpascal_strings, 704281ad6265SDimitry Andric options::OPT_fno_pascal_strings); 70430b57cec5SDimitry Andric 70440b57cec5SDimitry Andric // Honor -fpack-struct= and -fpack-struct, if given. Note that 70450b57cec5SDimitry Andric // -fno-pack-struct doesn't apply to -fpack-struct=. 70460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 70470b57cec5SDimitry Andric std::string PackStructStr = "-fpack-struct="; 70480b57cec5SDimitry Andric PackStructStr += A->getValue(); 70490b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 70500b57cec5SDimitry Andric } else if (Args.hasFlag(options::OPT_fpack_struct, 70510b57cec5SDimitry Andric options::OPT_fno_pack_struct, false)) { 70520b57cec5SDimitry Andric CmdArgs.push_back("-fpack-struct=1"); 70530b57cec5SDimitry Andric } 70540b57cec5SDimitry Andric 70550b57cec5SDimitry Andric // Handle -fmax-type-align=N and -fno-type-align 70560b57cec5SDimitry Andric bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align); 70570b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) { 70580b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 70590b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align="; 70600b57cec5SDimitry Andric MaxTypeAlignStr += A->getValue(); 70610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 70620b57cec5SDimitry Andric } 70630b57cec5SDimitry Andric } else if (RawTriple.isOSDarwin()) { 70640b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 70650b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align=16"; 70660b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 70670b57cec5SDimitry Andric } 70680b57cec5SDimitry Andric } 70690b57cec5SDimitry Andric 70700b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true)) 70710b57cec5SDimitry Andric CmdArgs.push_back("-Qn"); 70720b57cec5SDimitry Andric 70735ffd83dbSDimitry Andric // -fno-common is the default, set -fcommon only when that flag is set. 707481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcommon, options::OPT_fno_common); 70750b57cec5SDimitry Andric 70760b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't yet support 70770b57cec5SDimitry Andric // -funsigned-bitfields. 70780b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fsigned_bitfields, 707981ad6265SDimitry Andric options::OPT_funsigned_bitfields, true)) 70800b57cec5SDimitry Andric D.Diag(diag::warn_drv_clang_unsupported) 70810b57cec5SDimitry Andric << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 70820b57cec5SDimitry Andric 70830b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 708481ad6265SDimitry Andric if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope, true)) 70850b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) 70860b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 70870b57cec5SDimitry Andric 70880b57cec5SDimitry Andric // -finput_charset=UTF-8 is default. Reject others 70890b57cec5SDimitry Andric if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) { 70900b57cec5SDimitry Andric StringRef value = inputCharset->getValue(); 7091fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 70920b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) 70930b57cec5SDimitry Andric << value; 70940b57cec5SDimitry Andric } 70950b57cec5SDimitry Andric 70960b57cec5SDimitry Andric // -fexec_charset=UTF-8 is default. Reject others 70970b57cec5SDimitry Andric if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) { 70980b57cec5SDimitry Andric StringRef value = execCharset->getValue(); 7099fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 71000b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) 71010b57cec5SDimitry Andric << value; 71020b57cec5SDimitry Andric } 71030b57cec5SDimitry Andric 71040b57cec5SDimitry Andric RenderDiagnosticsOptions(D, Args, CmdArgs); 71050b57cec5SDimitry Andric 710681ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fasm_blocks, 710781ad6265SDimitry Andric options::OPT_fno_asm_blocks); 71080b57cec5SDimitry Andric 7109bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fgnu_inline_asm, 7110bdd1243dSDimitry Andric options::OPT_fno_gnu_inline_asm); 71110b57cec5SDimitry Andric 71120b57cec5SDimitry Andric // Enable vectorization per default according to the optimization level 71130b57cec5SDimitry Andric // selected. For optimization levels that want vectorization we use the alias 71140b57cec5SDimitry Andric // option to simplify the hasFlag logic. 71150b57cec5SDimitry Andric bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false); 71160b57cec5SDimitry Andric OptSpecifier VectorizeAliasOption = 71170b57cec5SDimitry Andric EnableVec ? options::OPT_O_Group : options::OPT_fvectorize; 71180b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 71190b57cec5SDimitry Andric options::OPT_fno_vectorize, EnableVec)) 71200b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-loops"); 71210b57cec5SDimitry Andric 71220b57cec5SDimitry Andric // -fslp-vectorize is enabled based on the optimization level selected. 71230b57cec5SDimitry Andric bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true); 71240b57cec5SDimitry Andric OptSpecifier SLPVectAliasOption = 71250b57cec5SDimitry Andric EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize; 71260b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption, 71270b57cec5SDimitry Andric options::OPT_fno_slp_vectorize, EnableSLPVec)) 71280b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-slp"); 71290b57cec5SDimitry Andric 71300b57cec5SDimitry Andric ParseMPreferVectorWidth(D, Args, CmdArgs); 71310b57cec5SDimitry Andric 71320b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ); 71330b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 71340b57cec5SDimitry Andric options::OPT_fsanitize_undefined_strip_path_components_EQ); 71350b57cec5SDimitry Andric 71360b57cec5SDimitry Andric // -fdollars-in-identifiers default varies depending on platform and 71370b57cec5SDimitry Andric // language; only pass if specified. 71380b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 71390b57cec5SDimitry Andric options::OPT_fno_dollars_in_identifiers)) { 71400b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 71410b57cec5SDimitry Andric CmdArgs.push_back("-fdollars-in-identifiers"); 71420b57cec5SDimitry Andric else 71430b57cec5SDimitry Andric CmdArgs.push_back("-fno-dollars-in-identifiers"); 71440b57cec5SDimitry Andric } 71450b57cec5SDimitry Andric 714681ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fapple_pragma_pack, 714781ad6265SDimitry Andric options::OPT_fno_apple_pragma_pack); 71480b57cec5SDimitry Andric 71490b57cec5SDimitry Andric // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. 7150480093f4SDimitry Andric if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple)) 7151480093f4SDimitry Andric renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA); 71520b57cec5SDimitry Andric 71530b57cec5SDimitry Andric bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports, 71540b57cec5SDimitry Andric options::OPT_fno_rewrite_imports, false); 71550b57cec5SDimitry Andric if (RewriteImports) 71560b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-imports"); 71570b57cec5SDimitry Andric 7158bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdirectives_only, 7159bdd1243dSDimitry Andric options::OPT_fno_directives_only); 716081ad6265SDimitry Andric 71610b57cec5SDimitry Andric // Enable rewrite includes if the user's asked for it or if we're generating 71620b57cec5SDimitry Andric // diagnostics. 71630b57cec5SDimitry Andric // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be 71640b57cec5SDimitry Andric // nice to enable this when doing a crashdump for modules as well. 71650b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_frewrite_includes, 71660b57cec5SDimitry Andric options::OPT_fno_rewrite_includes, false) || 71670b57cec5SDimitry Andric (C.isForDiagnostics() && !HaveModules)) 71680b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-includes"); 71690b57cec5SDimitry Andric 71700b57cec5SDimitry Andric // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 71710b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_traditional, 71720b57cec5SDimitry Andric options::OPT_traditional_cpp)) { 71730b57cec5SDimitry Andric if (isa<PreprocessJobAction>(JA)) 71740b57cec5SDimitry Andric CmdArgs.push_back("-traditional-cpp"); 71750b57cec5SDimitry Andric else 71760b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 71770b57cec5SDimitry Andric } 71780b57cec5SDimitry Andric 71790b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dM); 71800b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dD); 718104eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dI); 71820b57cec5SDimitry Andric 71835ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ); 71845ffd83dbSDimitry Andric 71850b57cec5SDimitry Andric // Handle serialized diagnostics. 71860b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 71870b57cec5SDimitry Andric CmdArgs.push_back("-serialize-diagnostic-file"); 71880b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getValue())); 71890b57cec5SDimitry Andric } 71900b57cec5SDimitry Andric 71910b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 71920b57cec5SDimitry Andric CmdArgs.push_back("-fretain-comments-from-system-headers"); 71930b57cec5SDimitry Andric 71940b57cec5SDimitry Andric // Forward -fcomment-block-commands to -cc1. 71950b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 71960b57cec5SDimitry Andric // Forward -fparse-all-comments to -cc1. 71970b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 71980b57cec5SDimitry Andric 71990b57cec5SDimitry Andric // Turn -fplugin=name.so into -load name.so 72000b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) { 72010b57cec5SDimitry Andric CmdArgs.push_back("-load"); 72020b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 72030b57cec5SDimitry Andric A->claim(); 72040b57cec5SDimitry Andric } 72050b57cec5SDimitry Andric 72064824e7fdSDimitry Andric // Turn -fplugin-arg-pluginname-key=value into 72074824e7fdSDimitry Andric // -plugin-arg-pluginname key=value 72084824e7fdSDimitry Andric // GCC has an actual plugin_argument struct with key/value pairs that it 72094824e7fdSDimitry Andric // passes to its plugins, but we don't, so just pass it on as-is. 72104824e7fdSDimitry Andric // 72114824e7fdSDimitry Andric // The syntax for -fplugin-arg- is ambiguous if both plugin name and 72124824e7fdSDimitry Andric // argument key are allowed to contain dashes. GCC therefore only 72134824e7fdSDimitry Andric // allows dashes in the key. We do the same. 72144824e7fdSDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_arg)) { 72154824e7fdSDimitry Andric auto ArgValue = StringRef(A->getValue()); 72164824e7fdSDimitry Andric auto FirstDashIndex = ArgValue.find('-'); 72174824e7fdSDimitry Andric StringRef PluginName = ArgValue.substr(0, FirstDashIndex); 72184824e7fdSDimitry Andric StringRef Arg = ArgValue.substr(FirstDashIndex + 1); 72194824e7fdSDimitry Andric 72204824e7fdSDimitry Andric A->claim(); 72214824e7fdSDimitry Andric if (FirstDashIndex == StringRef::npos || Arg.empty()) { 72224824e7fdSDimitry Andric if (PluginName.empty()) { 72234824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_name) << A->getAsString(Args); 72244824e7fdSDimitry Andric } else { 72254824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_arg) 72264824e7fdSDimitry Andric << PluginName << A->getAsString(Args); 72274824e7fdSDimitry Andric } 72284824e7fdSDimitry Andric continue; 72294824e7fdSDimitry Andric } 72304824e7fdSDimitry Andric 72314824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-arg-") + PluginName)); 72324824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Arg)); 72334824e7fdSDimitry Andric } 72344824e7fdSDimitry Andric 72350b57cec5SDimitry Andric // Forward -fpass-plugin=name.so to -cc1. 72360b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { 72370b57cec5SDimitry Andric CmdArgs.push_back( 72380b57cec5SDimitry Andric Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); 72390b57cec5SDimitry Andric A->claim(); 72400b57cec5SDimitry Andric } 72410b57cec5SDimitry Andric 724206c3fb27SDimitry Andric // Forward --vfsoverlay to -cc1. 724306c3fb27SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_vfsoverlay)) { 724406c3fb27SDimitry Andric CmdArgs.push_back("--vfsoverlay"); 724506c3fb27SDimitry Andric CmdArgs.push_back(A->getValue()); 724606c3fb27SDimitry Andric A->claim(); 724706c3fb27SDimitry Andric } 724806c3fb27SDimitry Andric 724906c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsafe_buffer_usage_suggestions, 725006c3fb27SDimitry Andric options::OPT_fno_safe_buffer_usage_suggestions); 725106c3fb27SDimitry Andric 72520b57cec5SDimitry Andric // Setup statistics file output. 72530b57cec5SDimitry Andric SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); 725406c3fb27SDimitry Andric if (!StatsFile.empty()) { 72550b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile)); 725606c3fb27SDimitry Andric if (D.CCPrintInternalStats) 725706c3fb27SDimitry Andric CmdArgs.push_back("-stats-file-append"); 725806c3fb27SDimitry Andric } 72590b57cec5SDimitry Andric 72600b57cec5SDimitry Andric // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 72610b57cec5SDimitry Andric // parser. 7262bdd1243dSDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) { 7263bdd1243dSDimitry Andric Arg->claim(); 72640b57cec5SDimitry Andric // -finclude-default-header flag is for preprocessor, 72650b57cec5SDimitry Andric // do not pass it to other cc1 commands when save-temps is enabled 72660b57cec5SDimitry Andric if (C.getDriver().isSaveTempsEnabled() && 72670b57cec5SDimitry Andric !isa<PreprocessJobAction>(JA)) { 7268bdd1243dSDimitry Andric if (StringRef(Arg->getValue()) == "-finclude-default-header") 7269bdd1243dSDimitry Andric continue; 7270bdd1243dSDimitry Andric } 72710b57cec5SDimitry Andric CmdArgs.push_back(Arg->getValue()); 72720b57cec5SDimitry Andric } 72730b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 72740b57cec5SDimitry Andric A->claim(); 72750b57cec5SDimitry Andric 72760b57cec5SDimitry Andric // We translate this by hand to the -cc1 argument, since nightly test uses 72770b57cec5SDimitry Andric // it and developers have been trained to spell it with -mllvm. Both 72780b57cec5SDimitry Andric // spellings are now deprecated and should be removed. 72790b57cec5SDimitry Andric if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") { 72800b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-optzns"); 72810b57cec5SDimitry Andric } else { 72820b57cec5SDimitry Andric A->render(Args, CmdArgs); 72830b57cec5SDimitry Andric } 72840b57cec5SDimitry Andric } 72850b57cec5SDimitry Andric 72860b57cec5SDimitry Andric // With -save-temps, we want to save the unoptimized bitcode output from the 72870b57cec5SDimitry Andric // CompileJobAction, use -disable-llvm-passes to get pristine IR generated 72880b57cec5SDimitry Andric // by the frontend. 72890b57cec5SDimitry Andric // When -fembed-bitcode is enabled, optimized bitcode is emitted because it 72900b57cec5SDimitry Andric // has slightly different breakdown between stages. 72910b57cec5SDimitry Andric // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of 72920b57cec5SDimitry Andric // pristine IR generated by the frontend. Ideally, a new compile action should 72930b57cec5SDimitry Andric // be added so both IR can be captured. 72945ffd83dbSDimitry Andric if ((C.getDriver().isSaveTempsEnabled() || 72955ffd83dbSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP)) && 7296fe6060f1SDimitry Andric !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) && 72970b57cec5SDimitry Andric isa<CompileJobAction>(JA)) 72980b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 72990b57cec5SDimitry Andric 73000b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_undef); 73010b57cec5SDimitry Andric 73020b57cec5SDimitry Andric const char *Exec = D.getClangProgramPath(); 73030b57cec5SDimitry Andric 73040b57cec5SDimitry Andric // Optionally embed the -cc1 level arguments into the debug info or a 73050b57cec5SDimitry Andric // section, for build analysis. 73060b57cec5SDimitry Andric // Also record command line arguments into the debug info if 73070b57cec5SDimitry Andric // -grecord-gcc-switches options is set on. 73080b57cec5SDimitry Andric // By default, -gno-record-gcc-switches is set on and no recording. 73090b57cec5SDimitry Andric auto GRecordSwitches = 73100b57cec5SDimitry Andric Args.hasFlag(options::OPT_grecord_command_line, 73110b57cec5SDimitry Andric options::OPT_gno_record_command_line, false); 73120b57cec5SDimitry Andric auto FRecordSwitches = 73130b57cec5SDimitry Andric Args.hasFlag(options::OPT_frecord_command_line, 73140b57cec5SDimitry Andric options::OPT_fno_record_command_line, false); 731506c3fb27SDimitry Andric if (FRecordSwitches && !Triple.isOSBinFormatELF() && 731606c3fb27SDimitry Andric !Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO()) 73170b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 73180b57cec5SDimitry Andric << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args) 73190b57cec5SDimitry Andric << TripleStr; 73200b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) { 73210b57cec5SDimitry Andric ArgStringList OriginalArgs; 73220b57cec5SDimitry Andric for (const auto &Arg : Args) 73230b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 73240b57cec5SDimitry Andric 73250b57cec5SDimitry Andric SmallString<256> Flags; 73265ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 73270b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 73280b57cec5SDimitry Andric SmallString<128> EscapedArg; 73290b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 73300b57cec5SDimitry Andric Flags += " "; 73310b57cec5SDimitry Andric Flags += EscapedArg; 73320b57cec5SDimitry Andric } 73330b57cec5SDimitry Andric auto FlagsArgString = Args.MakeArgString(Flags); 73340b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches) { 73350b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 73360b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 73370b57cec5SDimitry Andric } 73380b57cec5SDimitry Andric if (FRecordSwitches) { 73390b57cec5SDimitry Andric CmdArgs.push_back("-record-command-line"); 73400b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 73410b57cec5SDimitry Andric } 73420b57cec5SDimitry Andric } 73430b57cec5SDimitry Andric 7344753f127fSDimitry Andric // Host-side offloading compilation receives all device-side outputs. Include 7345753f127fSDimitry Andric // them in the host compilation depending on the target. If the host inputs 7346753f127fSDimitry Andric // are not empty we use the new-driver scheme, otherwise use the old scheme. 73470b57cec5SDimitry Andric if ((IsCuda || IsHIP) && CudaDeviceInput) { 73480b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 73490b57cec5SDimitry Andric CmdArgs.push_back(CudaDeviceInput->getFilename()); 7350753f127fSDimitry Andric } else if (!HostOffloadingInputs.empty()) { 7351fcaf7f86SDimitry Andric if ((IsCuda || IsHIP) && !IsRDCMode) { 7352753f127fSDimitry Andric assert(HostOffloadingInputs.size() == 1 && "Only one input expected"); 7353753f127fSDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 7354753f127fSDimitry Andric CmdArgs.push_back(HostOffloadingInputs.front().getFilename()); 7355753f127fSDimitry Andric } else { 7356753f127fSDimitry Andric for (const InputInfo Input : HostOffloadingInputs) 7357753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fembed-offload-object=" + 7358753f127fSDimitry Andric TC.getInputFilename(Input))); 7359753f127fSDimitry Andric } 73600b57cec5SDimitry Andric } 73610b57cec5SDimitry Andric 73620b57cec5SDimitry Andric if (IsCuda) { 73630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcuda_short_ptr, 73640b57cec5SDimitry Andric options::OPT_fno_cuda_short_ptr, false)) 73650b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-short-ptr"); 73660b57cec5SDimitry Andric } 73670b57cec5SDimitry Andric 7368fe6060f1SDimitry Andric if (IsCuda || IsHIP) { 7369fe6060f1SDimitry Andric // Determine the original source input. 7370fe6060f1SDimitry Andric const Action *SourceAction = &JA; 7371fe6060f1SDimitry Andric while (SourceAction->getKind() != Action::InputClass) { 7372fe6060f1SDimitry Andric assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 7373fe6060f1SDimitry Andric SourceAction = SourceAction->getInputs()[0]; 7374fe6060f1SDimitry Andric } 7375fe6060f1SDimitry Andric auto CUID = cast<InputAction>(SourceAction)->getId(); 7376fe6060f1SDimitry Andric if (!CUID.empty()) 7377fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); 73785f757f3fSDimitry Andric 73795f757f3fSDimitry Andric // -ffast-math turns on -fgpu-approx-transcendentals implicitly, but will 73805f757f3fSDimitry Andric // be overriden by -fno-gpu-approx-transcendentals. 73815f757f3fSDimitry Andric bool UseApproxTranscendentals = Args.hasFlag( 73825f757f3fSDimitry Andric options::OPT_ffast_math, options::OPT_fno_fast_math, false); 73835f757f3fSDimitry Andric if (Args.hasFlag(options::OPT_fgpu_approx_transcendentals, 73845f757f3fSDimitry Andric options::OPT_fno_gpu_approx_transcendentals, 73855f757f3fSDimitry Andric UseApproxTranscendentals)) 73865f757f3fSDimitry Andric CmdArgs.push_back("-fgpu-approx-transcendentals"); 73875f757f3fSDimitry Andric } else { 73885f757f3fSDimitry Andric Args.claimAllArgs(options::OPT_fgpu_approx_transcendentals, 73895f757f3fSDimitry Andric options::OPT_fno_gpu_approx_transcendentals); 7390fe6060f1SDimitry Andric } 7391fe6060f1SDimitry Andric 739281ad6265SDimitry Andric if (IsHIP) { 7393480093f4SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 739481ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgpu_default_stream_EQ); 739581ad6265SDimitry Andric } 7396480093f4SDimitry Andric 73975f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_foffload_uniform_block, 73985f757f3fSDimitry Andric options::OPT_fno_offload_uniform_block); 73995f757f3fSDimitry Andric 74005f757f3fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_foffload_implicit_host_device_templates, 74015f757f3fSDimitry Andric options::OPT_fno_offload_implicit_host_device_templates); 74025f757f3fSDimitry Andric 7403fe6060f1SDimitry Andric if (IsCudaDevice || IsHIPDevice) { 7404fe6060f1SDimitry Andric StringRef InlineThresh = 7405fe6060f1SDimitry Andric Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ); 7406fe6060f1SDimitry Andric if (!InlineThresh.empty()) { 7407fe6060f1SDimitry Andric std::string ArgStr = 7408fe6060f1SDimitry Andric std::string("-inline-threshold=") + InlineThresh.str(); 7409fe6060f1SDimitry Andric CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)}); 7410fe6060f1SDimitry Andric } 7411fe6060f1SDimitry Andric } 7412fe6060f1SDimitry Andric 741306c3fb27SDimitry Andric if (IsHIPDevice) 741406c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, 741506c3fb27SDimitry Andric options::OPT_fhip_fp32_correctly_rounded_divide_sqrt, 741606c3fb27SDimitry Andric options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt); 741706c3fb27SDimitry Andric 74180b57cec5SDimitry Andric // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path 74190b57cec5SDimitry Andric // to specify the result of the compile phase on the host, so the meaningful 742006c3fb27SDimitry Andric // device declarations can be identified. Also, -fopenmp-is-target-device is 742106c3fb27SDimitry Andric // passed along to tell the frontend that it is generating code for a device, 742206c3fb27SDimitry Andric // so that only the relevant declarations are emitted. 74230b57cec5SDimitry Andric if (IsOpenMPDevice) { 742406c3fb27SDimitry Andric CmdArgs.push_back("-fopenmp-is-target-device"); 74250b57cec5SDimitry Andric if (OpenMPDeviceInput) { 74260b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-host-ir-file-path"); 74270b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename())); 74280b57cec5SDimitry Andric } 74290b57cec5SDimitry Andric } 74300b57cec5SDimitry Andric 7431e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) { 7432e8d8bef9SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); 7433e8d8bef9SDimitry Andric 743481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_munsafe_fp_atomics, 743581ad6265SDimitry Andric options::OPT_mno_unsafe_fp_atomics); 743606c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_mamdgpu_ieee, 743706c3fb27SDimitry Andric options::OPT_mno_amdgpu_ieee); 7438e8d8bef9SDimitry Andric } 7439e8d8bef9SDimitry Andric 74400b57cec5SDimitry Andric // For all the host OpenMP offloading compile jobs we need to pass the targets 74410b57cec5SDimitry Andric // information using -fopenmp-targets= option. 74420b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 744381ad6265SDimitry Andric SmallString<128> Targets("-fopenmp-targets="); 74440b57cec5SDimitry Andric 744581ad6265SDimitry Andric SmallVector<std::string, 4> Triples; 744681ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); 744781ad6265SDimitry Andric std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples), 744881ad6265SDimitry Andric [](auto TC) { return TC.second->getTripleString(); }); 744981ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ","))); 74500b57cec5SDimitry Andric } 74510b57cec5SDimitry Andric 7452a7dea167SDimitry Andric bool VirtualFunctionElimination = 7453a7dea167SDimitry Andric Args.hasFlag(options::OPT_fvirtual_function_elimination, 7454a7dea167SDimitry Andric options::OPT_fno_virtual_function_elimination, false); 7455a7dea167SDimitry Andric if (VirtualFunctionElimination) { 7456a7dea167SDimitry Andric // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO 7457a7dea167SDimitry Andric // in the future). 7458fe6060f1SDimitry Andric if (LTOMode != LTOK_Full) 7459a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 7460a7dea167SDimitry Andric << "-fvirtual-function-elimination" 7461a7dea167SDimitry Andric << "-flto=full"; 7462a7dea167SDimitry Andric 7463a7dea167SDimitry Andric CmdArgs.push_back("-fvirtual-function-elimination"); 7464a7dea167SDimitry Andric } 7465a7dea167SDimitry Andric 7466a7dea167SDimitry Andric // VFE requires whole-program-vtables, and enables it by default. 7467a7dea167SDimitry Andric bool WholeProgramVTables = Args.hasFlag( 7468a7dea167SDimitry Andric options::OPT_fwhole_program_vtables, 7469a7dea167SDimitry Andric options::OPT_fno_whole_program_vtables, VirtualFunctionElimination); 7470a7dea167SDimitry Andric if (VirtualFunctionElimination && !WholeProgramVTables) { 7471a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7472a7dea167SDimitry Andric << "-fno-whole-program-vtables" 7473a7dea167SDimitry Andric << "-fvirtual-function-elimination"; 7474a7dea167SDimitry Andric } 7475a7dea167SDimitry Andric 74760b57cec5SDimitry Andric if (WholeProgramVTables) { 747706c3fb27SDimitry Andric // PS4 uses the legacy LTO API, which does not support this feature in 747806c3fb27SDimitry Andric // ThinLTO mode. 747906c3fb27SDimitry Andric bool IsPS4 = getToolChain().getTriple().isPS4(); 748006c3fb27SDimitry Andric 7481fe6060f1SDimitry Andric // Check if we passed LTO options but they were suppressed because this is a 7482fe6060f1SDimitry Andric // device offloading action, or we passed device offload LTO options which 7483fe6060f1SDimitry Andric // were suppressed because this is not the device offload action. 748406c3fb27SDimitry Andric // Check if we are using PS4 in regular LTO mode. 7485fe6060f1SDimitry Andric // Otherwise, issue an error. 748606c3fb27SDimitry Andric if ((!IsUsingLTO && !D.isUsingLTO(!IsDeviceOffloadAction)) || 748706c3fb27SDimitry Andric (IsPS4 && !UnifiedLTO && (D.getLTOMode() != LTOK_Full))) 74880b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 74890b57cec5SDimitry Andric << "-fwhole-program-vtables" 749006c3fb27SDimitry Andric << ((IsPS4 && !UnifiedLTO) ? "-flto=full" : "-flto"); 749106c3fb27SDimitry Andric 749206c3fb27SDimitry Andric // Propagate -fwhole-program-vtables if this is an LTO compile. 749306c3fb27SDimitry Andric if (IsUsingLTO) 749406c3fb27SDimitry Andric CmdArgs.push_back("-fwhole-program-vtables"); 74950b57cec5SDimitry Andric } 74960b57cec5SDimitry Andric 7497480093f4SDimitry Andric bool DefaultsSplitLTOUnit = 749806c3fb27SDimitry Andric ((WholeProgramVTables || SanitizeArgs.needsLTO()) && 749906c3fb27SDimitry Andric (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit())) || 750006c3fb27SDimitry Andric (!Triple.isPS4() && UnifiedLTO); 75010b57cec5SDimitry Andric bool SplitLTOUnit = 75020b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_lto_unit, 7503a7dea167SDimitry Andric options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); 7504349cc55cSDimitry Andric if (SanitizeArgs.needsLTO() && !SplitLTOUnit) 7505a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" 7506a7dea167SDimitry Andric << "-fsanitize=cfi"; 75070b57cec5SDimitry Andric if (SplitLTOUnit) 75080b57cec5SDimitry Andric CmdArgs.push_back("-fsplit-lto-unit"); 75090b57cec5SDimitry Andric 75105f757f3fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffat_lto_objects, 75115f757f3fSDimitry Andric options::OPT_fno_fat_lto_objects)) { 75125f757f3fSDimitry Andric if (IsUsingLTO && A->getOption().matches(options::OPT_ffat_lto_objects)) { 75135f757f3fSDimitry Andric assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); 75145f757f3fSDimitry Andric if (!Triple.isOSBinFormatELF()) { 75155f757f3fSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 75165f757f3fSDimitry Andric << A->getAsString(Args) << TC.getTripleString(); 75175f757f3fSDimitry Andric } 75185f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 75195f757f3fSDimitry Andric Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 75205f757f3fSDimitry Andric CmdArgs.push_back("-flto-unit"); 75215f757f3fSDimitry Andric CmdArgs.push_back("-ffat-lto-objects"); 75225f757f3fSDimitry Andric A->render(Args, CmdArgs); 75235f757f3fSDimitry Andric } 75245f757f3fSDimitry Andric } 75255f757f3fSDimitry Andric 75265ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, 75275ffd83dbSDimitry Andric options::OPT_fno_global_isel)) { 75280b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 75295ffd83dbSDimitry Andric if (A->getOption().matches(options::OPT_fglobal_isel)) { 75300b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=1"); 75310b57cec5SDimitry Andric 75320b57cec5SDimitry Andric // GISel is on by default on AArch64 -O0, so don't bother adding 75330b57cec5SDimitry Andric // the fallback remarks for it. Other combinations will add a warning of 75340b57cec5SDimitry Andric // some kind. 75350b57cec5SDimitry Andric bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64; 75360b57cec5SDimitry Andric bool IsOptLevelSupported = false; 75370b57cec5SDimitry Andric 75380b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_O_Group); 75390b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::aarch64) { 75400b57cec5SDimitry Andric if (!A || A->getOption().matches(options::OPT_O0)) 75410b57cec5SDimitry Andric IsOptLevelSupported = true; 75420b57cec5SDimitry Andric } 75430b57cec5SDimitry Andric if (!IsArchSupported || !IsOptLevelSupported) { 75440b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 75450b57cec5SDimitry Andric CmdArgs.push_back("-global-isel-abort=2"); 75460b57cec5SDimitry Andric 75470b57cec5SDimitry Andric if (!IsArchSupported) 75485ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete) << Triple.getArchName(); 75490b57cec5SDimitry Andric else 75505ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete_opt); 75510b57cec5SDimitry Andric } 75520b57cec5SDimitry Andric } else { 75530b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=0"); 75540b57cec5SDimitry Andric } 75550b57cec5SDimitry Andric } 75560b57cec5SDimitry Andric 75570b57cec5SDimitry Andric if (Args.hasArg(options::OPT_forder_file_instrumentation)) { 75580b57cec5SDimitry Andric CmdArgs.push_back("-forder-file-instrumentation"); 75590b57cec5SDimitry Andric // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is 75600b57cec5SDimitry Andric // on, we need to pass these flags as linker flags and that will be handled 75610b57cec5SDimitry Andric // outside of the compiler. 7562fe6060f1SDimitry Andric if (!IsUsingLTO) { 75630b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 75640b57cec5SDimitry Andric CmdArgs.push_back("-enable-order-file-instrumentation"); 75650b57cec5SDimitry Andric } 75660b57cec5SDimitry Andric } 75670b57cec5SDimitry Andric 75680b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128, 75690b57cec5SDimitry Andric options::OPT_fno_force_enable_int128)) { 75700b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fforce_enable_int128)) 75710b57cec5SDimitry Andric CmdArgs.push_back("-fforce-enable-int128"); 75720b57cec5SDimitry Andric } 75730b57cec5SDimitry Andric 757481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_consts, 757581ad6265SDimitry Andric options::OPT_fno_keep_static_consts); 757606c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fkeep_persistent_storage_variables, 757706c3fb27SDimitry Andric options::OPT_fno_keep_persistent_storage_variables); 757881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcomplete_member_pointers, 757981ad6265SDimitry Andric options::OPT_fno_complete_member_pointers); 7580bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fcxx_static_destructors, 7581bdd1243dSDimitry Andric options::OPT_fno_cxx_static_destructors); 75820b57cec5SDimitry Andric 7583e8d8bef9SDimitry Andric addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false); 7584e8d8bef9SDimitry Andric 7585e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics, 7586e8d8bef9SDimitry Andric options::OPT_mno_outline_atomics)) { 7587e8d8bef9SDimitry Andric // Option -moutline-atomics supported for AArch64 target only. 7588e8d8bef9SDimitry Andric if (!Triple.isAArch64()) { 7589e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt) 75900eae32dcSDimitry Andric << Triple.getArchName() << A->getOption().getName(); 75910b57cec5SDimitry Andric } else { 75920eae32dcSDimitry Andric if (A->getOption().matches(options::OPT_moutline_atomics)) { 7593e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7594e8d8bef9SDimitry Andric CmdArgs.push_back("+outline-atomics"); 75950b57cec5SDimitry Andric } else { 7596e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7597e8d8bef9SDimitry Andric CmdArgs.push_back("-outline-atomics"); 75980b57cec5SDimitry Andric } 75990eae32dcSDimitry Andric } 7600fe6060f1SDimitry Andric } else if (Triple.isAArch64() && 7601fe6060f1SDimitry Andric getToolChain().IsAArch64OutlineAtomicsDefault(Args)) { 7602fe6060f1SDimitry Andric CmdArgs.push_back("-target-feature"); 7603fe6060f1SDimitry Andric CmdArgs.push_back("+outline-atomics"); 76040b57cec5SDimitry Andric } 76050b57cec5SDimitry Andric 7606bdd1243dSDimitry Andric if (Triple.isAArch64() && 7607bdd1243dSDimitry Andric (Args.hasArg(options::OPT_mno_fmv) || 76085f757f3fSDimitry Andric (Triple.isAndroid() && Triple.isAndroidVersionLT(23)) || 7609bdd1243dSDimitry Andric getToolChain().GetRuntimeLibType(Args) != ToolChain::RLT_CompilerRT)) { 7610bdd1243dSDimitry Andric // Disable Function Multiversioning on AArch64 target. 7611bdd1243dSDimitry Andric CmdArgs.push_back("-target-feature"); 7612bdd1243dSDimitry Andric CmdArgs.push_back("-fmv"); 7613bdd1243dSDimitry Andric } 7614bdd1243dSDimitry Andric 76150b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig, 76160b57cec5SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 76170b57cec5SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 7618e8d8bef9SDimitry Andric !TC.getTriple().isPS4() && !TC.getTriple().isVE() && 76190b57cec5SDimitry Andric !TC.getTriple().isOSNetBSD() && 7620480093f4SDimitry Andric !Distro(D.getVFS(), TC.getTriple()).IsGentoo() && 7621e8d8bef9SDimitry Andric !TC.getTriple().isAndroid() && TC.useIntegratedAs())) 76220b57cec5SDimitry Andric CmdArgs.push_back("-faddrsig"); 76230b57cec5SDimitry Andric 7624fe6060f1SDimitry Andric if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && 7625bdd1243dSDimitry Andric (EH || UnwindTables || AsyncUnwindTables || 762606c3fb27SDimitry Andric DebugInfoKind != llvm::codegenoptions::NoDebugInfo)) 7627fe6060f1SDimitry Andric CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); 7628fe6060f1SDimitry Andric 76290b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { 76300b57cec5SDimitry Andric std::string Str = A->getAsString(Args); 76310b57cec5SDimitry Andric if (!TC.getTriple().isOSBinFormatELF()) 76320b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 76330b57cec5SDimitry Andric << Str << TC.getTripleString(); 76340b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str)); 76350b57cec5SDimitry Andric } 76360b57cec5SDimitry Andric 76370b57cec5SDimitry Andric // Add the "-o out -x type src.c" flags last. This is done primarily to make 76380b57cec5SDimitry Andric // the -cc1 command easier to edit when reproducing compiler crashes. 76390b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 76400b57cec5SDimitry Andric // Handled with other dependency code. 76410b57cec5SDimitry Andric } else if (Output.isFilename()) { 7642480093f4SDimitry Andric if (Output.getType() == clang::driver::types::TY_IFS_CPP || 7643480093f4SDimitry Andric Output.getType() == clang::driver::types::TY_IFS) { 7644480093f4SDimitry Andric SmallString<128> OutputFilename(Output.getFilename()); 7645480093f4SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "ifs"); 7646480093f4SDimitry Andric CmdArgs.push_back("-o"); 7647480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 7648480093f4SDimitry Andric } else { 76490b57cec5SDimitry Andric CmdArgs.push_back("-o"); 76500b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 7651480093f4SDimitry Andric } 76520b57cec5SDimitry Andric } else { 76530b57cec5SDimitry Andric assert(Output.isNothing() && "Invalid output."); 76540b57cec5SDimitry Andric } 76550b57cec5SDimitry Andric 76560b57cec5SDimitry Andric addDashXForInput(Args, Input, CmdArgs); 76570b57cec5SDimitry Andric 76580b57cec5SDimitry Andric ArrayRef<InputInfo> FrontendInputs = Input; 7659bdd1243dSDimitry Andric if (IsExtractAPI) 766081ad6265SDimitry Andric FrontendInputs = ExtractAPIInputs; 76610b57cec5SDimitry Andric else if (Input.isNothing()) 76620b57cec5SDimitry Andric FrontendInputs = {}; 76630b57cec5SDimitry Andric 76640b57cec5SDimitry Andric for (const InputInfo &Input : FrontendInputs) { 76650b57cec5SDimitry Andric if (Input.isFilename()) 76660b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 76670b57cec5SDimitry Andric else 76680b57cec5SDimitry Andric Input.getInputArg().renderAsInput(Args, CmdArgs); 76690b57cec5SDimitry Andric } 76700b57cec5SDimitry Andric 7671fe6060f1SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 7672480093f4SDimitry Andric // Invoke the CC1 directly in this process 767306c3fb27SDimitry Andric C.addCommand(std::make_unique<CC1Command>( 767406c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 767506c3fb27SDimitry Andric Output, D.getPrependArg())); 76760b57cec5SDimitry Andric } else { 767706c3fb27SDimitry Andric C.addCommand(std::make_unique<Command>( 767806c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 767906c3fb27SDimitry Andric Output, D.getPrependArg())); 76800b57cec5SDimitry Andric } 76810b57cec5SDimitry Andric 76820b57cec5SDimitry Andric // Make the compile command echo its inputs for /showFilenames. 76830b57cec5SDimitry Andric if (Output.getType() == types::TY_Object && 76840b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_showFilenames, 76850b57cec5SDimitry Andric options::OPT__SLASH_showFilenames_, false)) { 768613138422SDimitry Andric C.getJobs().getJobs().back()->PrintInputFilenames = true; 76870b57cec5SDimitry Andric } 76880b57cec5SDimitry Andric 76890b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_pg)) 76905ffd83dbSDimitry Andric if (FPKeepKind == CodeGenOptions::FramePointerKind::None && 76915ffd83dbSDimitry Andric !Args.hasArg(options::OPT_mfentry)) 76920b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer" 76930b57cec5SDimitry Andric << A->getAsString(Args); 76940b57cec5SDimitry Andric 76950b57cec5SDimitry Andric // Claim some arguments which clang supports automatically. 76960b57cec5SDimitry Andric 76970b57cec5SDimitry Andric // -fpch-preprocess is used with gcc to add a special marker in the output to 76980b57cec5SDimitry Andric // include the PCH file. 76990b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fpch_preprocess); 77000b57cec5SDimitry Andric 77010b57cec5SDimitry Andric // Claim some arguments which clang doesn't support, but we don't 77020b57cec5SDimitry Andric // care to warn the user about. 77030b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 77040b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 77050b57cec5SDimitry Andric 77060b57cec5SDimitry Andric // Disable warnings for clang -E -emit-llvm foo.c 77070b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 77080b57cec5SDimitry Andric } 77090b57cec5SDimitry Andric 77100eae32dcSDimitry Andric Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend) 77110b57cec5SDimitry Andric // CAUTION! The first constructor argument ("clang") is not arbitrary, 77120b57cec5SDimitry Andric // as it is for other tools. Some operations on a Tool actually test 77130b57cec5SDimitry Andric // whether that tool is Clang based on the Tool's Name as a string. 77140eae32dcSDimitry Andric : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {} 77150b57cec5SDimitry Andric 77160b57cec5SDimitry Andric Clang::~Clang() {} 77170b57cec5SDimitry Andric 77180b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI. 77190b57cec5SDimitry Andric /// 77200b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile. 77210b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 77225ffd83dbSDimitry Andric const InputInfoList &inputs, 77230b57cec5SDimitry Andric ArgStringList &cmdArgs, 77240b57cec5SDimitry Andric RewriteKind rewriteKind) const { 77250b57cec5SDimitry Andric // Look for the controlling runtime option. 77260b57cec5SDimitry Andric Arg *runtimeArg = 77270b57cec5SDimitry Andric args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime, 77280b57cec5SDimitry Andric options::OPT_fobjc_runtime_EQ); 77290b57cec5SDimitry Andric 77300b57cec5SDimitry Andric // Just forward -fobjc-runtime= to the frontend. This supercedes 77310b57cec5SDimitry Andric // options about fragility. 77320b57cec5SDimitry Andric if (runtimeArg && 77330b57cec5SDimitry Andric runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 77340b57cec5SDimitry Andric ObjCRuntime runtime; 77350b57cec5SDimitry Andric StringRef value = runtimeArg->getValue(); 77360b57cec5SDimitry Andric if (runtime.tryParse(value)) { 77370b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 77380b57cec5SDimitry Andric << value; 77390b57cec5SDimitry Andric } 77400b57cec5SDimitry Andric if ((runtime.getKind() == ObjCRuntime::GNUstep) && 77410b57cec5SDimitry Andric (runtime.getVersion() >= VersionTuple(2, 0))) 77420b57cec5SDimitry Andric if (!getToolChain().getTriple().isOSBinFormatELF() && 77430b57cec5SDimitry Andric !getToolChain().getTriple().isOSBinFormatCOFF()) { 77440b57cec5SDimitry Andric getToolChain().getDriver().Diag( 77450b57cec5SDimitry Andric diag::err_drv_gnustep_objc_runtime_incompatible_binary) 77460b57cec5SDimitry Andric << runtime.getVersion().getMajor(); 77470b57cec5SDimitry Andric } 77480b57cec5SDimitry Andric 77490b57cec5SDimitry Andric runtimeArg->render(args, cmdArgs); 77500b57cec5SDimitry Andric return runtime; 77510b57cec5SDimitry Andric } 77520b57cec5SDimitry Andric 77530b57cec5SDimitry Andric // Otherwise, we'll need the ABI "version". Version numbers are 77540b57cec5SDimitry Andric // slightly confusing for historical reasons: 77550b57cec5SDimitry Andric // 1 - Traditional "fragile" ABI 77560b57cec5SDimitry Andric // 2 - Non-fragile ABI, version 1 77570b57cec5SDimitry Andric // 3 - Non-fragile ABI, version 2 77580b57cec5SDimitry Andric unsigned objcABIVersion = 1; 77590b57cec5SDimitry Andric // If -fobjc-abi-version= is present, use that to set the version. 77600b57cec5SDimitry Andric if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 77610b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 77620b57cec5SDimitry Andric if (value == "1") 77630b57cec5SDimitry Andric objcABIVersion = 1; 77640b57cec5SDimitry Andric else if (value == "2") 77650b57cec5SDimitry Andric objcABIVersion = 2; 77660b57cec5SDimitry Andric else if (value == "3") 77670b57cec5SDimitry Andric objcABIVersion = 3; 77680b57cec5SDimitry Andric else 77690b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value; 77700b57cec5SDimitry Andric } else { 77710b57cec5SDimitry Andric // Otherwise, determine if we are using the non-fragile ABI. 77720b57cec5SDimitry Andric bool nonFragileABIIsDefault = 77730b57cec5SDimitry Andric (rewriteKind == RK_NonFragile || 77740b57cec5SDimitry Andric (rewriteKind == RK_None && 77750b57cec5SDimitry Andric getToolChain().IsObjCNonFragileABIDefault())); 77760b57cec5SDimitry Andric if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 77770b57cec5SDimitry Andric options::OPT_fno_objc_nonfragile_abi, 77780b57cec5SDimitry Andric nonFragileABIIsDefault)) { 77790b57cec5SDimitry Andric // Determine the non-fragile ABI version to use. 77800b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 77810b57cec5SDimitry Andric unsigned nonFragileABIVersion = 1; 77820b57cec5SDimitry Andric #else 77830b57cec5SDimitry Andric unsigned nonFragileABIVersion = 2; 77840b57cec5SDimitry Andric #endif 77850b57cec5SDimitry Andric 77860b57cec5SDimitry Andric if (Arg *abiArg = 77870b57cec5SDimitry Andric args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) { 77880b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 77890b57cec5SDimitry Andric if (value == "1") 77900b57cec5SDimitry Andric nonFragileABIVersion = 1; 77910b57cec5SDimitry Andric else if (value == "2") 77920b57cec5SDimitry Andric nonFragileABIVersion = 2; 77930b57cec5SDimitry Andric else 77940b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 77950b57cec5SDimitry Andric << value; 77960b57cec5SDimitry Andric } 77970b57cec5SDimitry Andric 77980b57cec5SDimitry Andric objcABIVersion = 1 + nonFragileABIVersion; 77990b57cec5SDimitry Andric } else { 78000b57cec5SDimitry Andric objcABIVersion = 1; 78010b57cec5SDimitry Andric } 78020b57cec5SDimitry Andric } 78030b57cec5SDimitry Andric 78040b57cec5SDimitry Andric // We don't actually care about the ABI version other than whether 78050b57cec5SDimitry Andric // it's non-fragile. 78060b57cec5SDimitry Andric bool isNonFragile = objcABIVersion != 1; 78070b57cec5SDimitry Andric 78080b57cec5SDimitry Andric // If we have no runtime argument, ask the toolchain for its default runtime. 78090b57cec5SDimitry Andric // However, the rewriter only really supports the Mac runtime, so assume that. 78100b57cec5SDimitry Andric ObjCRuntime runtime; 78110b57cec5SDimitry Andric if (!runtimeArg) { 78120b57cec5SDimitry Andric switch (rewriteKind) { 78130b57cec5SDimitry Andric case RK_None: 78140b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 78150b57cec5SDimitry Andric break; 78160b57cec5SDimitry Andric case RK_Fragile: 78170b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 78180b57cec5SDimitry Andric break; 78190b57cec5SDimitry Andric case RK_NonFragile: 78200b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 78210b57cec5SDimitry Andric break; 78220b57cec5SDimitry Andric } 78230b57cec5SDimitry Andric 78240b57cec5SDimitry Andric // -fnext-runtime 78250b57cec5SDimitry Andric } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 78260b57cec5SDimitry Andric // On Darwin, make this use the default behavior for the toolchain. 78270b57cec5SDimitry Andric if (getToolChain().getTriple().isOSDarwin()) { 78280b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 78290b57cec5SDimitry Andric 78300b57cec5SDimitry Andric // Otherwise, build for a generic macosx port. 78310b57cec5SDimitry Andric } else { 78320b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 78330b57cec5SDimitry Andric } 78340b57cec5SDimitry Andric 78350b57cec5SDimitry Andric // -fgnu-runtime 78360b57cec5SDimitry Andric } else { 78370b57cec5SDimitry Andric assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 78380b57cec5SDimitry Andric // Legacy behaviour is to target the gnustep runtime if we are in 78390b57cec5SDimitry Andric // non-fragile mode or the GCC runtime in fragile mode. 78400b57cec5SDimitry Andric if (isNonFragile) 78410b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0)); 78420b57cec5SDimitry Andric else 78430b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 78440b57cec5SDimitry Andric } 78450b57cec5SDimitry Andric 78465ffd83dbSDimitry Andric if (llvm::any_of(inputs, [](const InputInfo &input) { 78475ffd83dbSDimitry Andric return types::isObjC(input.getType()); 78485ffd83dbSDimitry Andric })) 78490b57cec5SDimitry Andric cmdArgs.push_back( 78500b57cec5SDimitry Andric args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); 78510b57cec5SDimitry Andric return runtime; 78520b57cec5SDimitry Andric } 78530b57cec5SDimitry Andric 78540b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) { 78550b57cec5SDimitry Andric bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-'); 78560b57cec5SDimitry Andric I += HaveDash; 78570b57cec5SDimitry Andric return !HaveDash; 78580b57cec5SDimitry Andric } 78590b57cec5SDimitry Andric 78600b57cec5SDimitry Andric namespace { 78610b57cec5SDimitry Andric struct EHFlags { 78620b57cec5SDimitry Andric bool Synch = false; 78630b57cec5SDimitry Andric bool Asynch = false; 78640b57cec5SDimitry Andric bool NoUnwindC = false; 78650b57cec5SDimitry Andric }; 78660b57cec5SDimitry Andric } // end anonymous namespace 78670b57cec5SDimitry Andric 78680b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are 78690b57cec5SDimitry Andric /// thrown. There are three modifiers: 78700b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions. 78710b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions. 78720b57cec5SDimitry Andric /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR. 78730b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind. 78740b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled. 78750b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { 78760b57cec5SDimitry Andric EHFlags EH; 78770b57cec5SDimitry Andric 78780b57cec5SDimitry Andric std::vector<std::string> EHArgs = 78790b57cec5SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_EH); 78800b57cec5SDimitry Andric for (auto EHVal : EHArgs) { 78810b57cec5SDimitry Andric for (size_t I = 0, E = EHVal.size(); I != E; ++I) { 78820b57cec5SDimitry Andric switch (EHVal[I]) { 78830b57cec5SDimitry Andric case 'a': 78840b57cec5SDimitry Andric EH.Asynch = maybeConsumeDash(EHVal, I); 78850b57cec5SDimitry Andric if (EH.Asynch) 78860b57cec5SDimitry Andric EH.Synch = false; 78870b57cec5SDimitry Andric continue; 78880b57cec5SDimitry Andric case 'c': 78890b57cec5SDimitry Andric EH.NoUnwindC = maybeConsumeDash(EHVal, I); 78900b57cec5SDimitry Andric continue; 78910b57cec5SDimitry Andric case 's': 78920b57cec5SDimitry Andric EH.Synch = maybeConsumeDash(EHVal, I); 78930b57cec5SDimitry Andric if (EH.Synch) 78940b57cec5SDimitry Andric EH.Asynch = false; 78950b57cec5SDimitry Andric continue; 78960b57cec5SDimitry Andric default: 78970b57cec5SDimitry Andric break; 78980b57cec5SDimitry Andric } 78990b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal; 79000b57cec5SDimitry Andric break; 79010b57cec5SDimitry Andric } 79020b57cec5SDimitry Andric } 79030b57cec5SDimitry Andric // The /GX, /GX- flags are only processed if there are not /EH flags. 79040b57cec5SDimitry Andric // The default is that /GX is not specified. 79050b57cec5SDimitry Andric if (EHArgs.empty() && 79060b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_, 79070b57cec5SDimitry Andric /*Default=*/false)) { 79080b57cec5SDimitry Andric EH.Synch = true; 79090b57cec5SDimitry Andric EH.NoUnwindC = true; 79100b57cec5SDimitry Andric } 79110b57cec5SDimitry Andric 791281ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 791381ad6265SDimitry Andric EH.Synch = false; 791481ad6265SDimitry Andric EH.NoUnwindC = false; 791581ad6265SDimitry Andric EH.Asynch = false; 791681ad6265SDimitry Andric } 791781ad6265SDimitry Andric 79180b57cec5SDimitry Andric return EH; 79190b57cec5SDimitry Andric } 79200b57cec5SDimitry Andric 79210b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, 79225f757f3fSDimitry Andric ArgStringList &CmdArgs) const { 79235ffd83dbSDimitry Andric bool isNVPTX = getToolChain().getTriple().isNVPTX(); 79240b57cec5SDimitry Andric 7925bdd1243dSDimitry Andric ProcessVSRuntimeLibrary(Args, CmdArgs); 79260b57cec5SDimitry Andric 79275ffd83dbSDimitry Andric if (Arg *ShowIncludes = 79285ffd83dbSDimitry Andric Args.getLastArg(options::OPT__SLASH_showIncludes, 79295ffd83dbSDimitry Andric options::OPT__SLASH_showIncludes_user)) { 79305ffd83dbSDimitry Andric CmdArgs.push_back("--show-includes"); 79315ffd83dbSDimitry Andric if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes)) 79325ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 79335ffd83dbSDimitry Andric } 79340b57cec5SDimitry Andric 79350b57cec5SDimitry Andric // This controls whether or not we emit RTTI data for polymorphic types. 79360b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR, 79370b57cec5SDimitry Andric /*Default=*/false)) 79380b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti-data"); 79390b57cec5SDimitry Andric 79400b57cec5SDimitry Andric // This controls whether or not we emit stack-protector instrumentation. 79410b57cec5SDimitry Andric // In MSVC, Buffer Security Check (/GS) is on by default. 79425ffd83dbSDimitry Andric if (!isNVPTX && Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_, 79430b57cec5SDimitry Andric /*Default=*/true)) { 79440b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 79450b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong))); 79460b57cec5SDimitry Andric } 79470b57cec5SDimitry Andric 79480b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 794981ad6265SDimitry Andric 79500b57cec5SDimitry Andric EHFlags EH = parseClangCLEHFlags(D, Args); 79515ffd83dbSDimitry Andric if (!isNVPTX && (EH.Synch || EH.Asynch)) { 79520b57cec5SDimitry Andric if (types::isCXX(InputType)) 79530b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 79540b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 795506c3fb27SDimitry Andric if (EH.Asynch) 795606c3fb27SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 79570b57cec5SDimitry Andric } 79580b57cec5SDimitry Andric if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC) 79590b57cec5SDimitry Andric CmdArgs.push_back("-fexternc-nounwind"); 79600b57cec5SDimitry Andric 79610b57cec5SDimitry Andric // /EP should expand to -E -P. 79620b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_EP)) { 79630b57cec5SDimitry Andric CmdArgs.push_back("-E"); 79640b57cec5SDimitry Andric CmdArgs.push_back("-P"); 79650b57cec5SDimitry Andric } 79660b57cec5SDimitry Andric 79670b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_, 79680b57cec5SDimitry Andric options::OPT__SLASH_Zc_dllexportInlines, 79690b57cec5SDimitry Andric false)) { 79700b57cec5SDimitry Andric CmdArgs.push_back("-fno-dllexport-inlines"); 79710b57cec5SDimitry Andric } 79720b57cec5SDimitry Andric 797381ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_wchar_t_, 797481ad6265SDimitry Andric options::OPT__SLASH_Zc_wchar_t, false)) { 797581ad6265SDimitry Andric CmdArgs.push_back("-fno-wchar"); 797681ad6265SDimitry Andric } 797781ad6265SDimitry Andric 797881ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 797981ad6265SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 798081ad6265SDimitry Andric std::vector<std::string> Values = 798181ad6265SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_arch); 798281ad6265SDimitry Andric if (!Values.empty()) { 798381ad6265SDimitry Andric llvm::SmallSet<std::string, 4> SupportedArches; 798481ad6265SDimitry Andric if (Arch == llvm::Triple::x86) 798581ad6265SDimitry Andric SupportedArches.insert("IA32"); 798681ad6265SDimitry Andric 798781ad6265SDimitry Andric for (auto &V : Values) 798881ad6265SDimitry Andric if (!SupportedArches.contains(V)) 798981ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 799081ad6265SDimitry Andric << std::string("/arch:").append(V) << "/kernel"; 799181ad6265SDimitry Andric } 799281ad6265SDimitry Andric 799381ad6265SDimitry Andric CmdArgs.push_back("-fno-rtti"); 799481ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR, options::OPT__SLASH_GR_, false)) 799581ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "/GR" 799681ad6265SDimitry Andric << "/kernel"; 799781ad6265SDimitry Andric } 799881ad6265SDimitry Andric 79990b57cec5SDimitry Andric Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); 80000b57cec5SDimitry Andric Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb); 80010b57cec5SDimitry Andric if (MostGeneralArg && BestCaseArg) 80020b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 80030b57cec5SDimitry Andric << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args); 80040b57cec5SDimitry Andric 80050b57cec5SDimitry Andric if (MostGeneralArg) { 80060b57cec5SDimitry Andric Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms); 80070b57cec5SDimitry Andric Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm); 80080b57cec5SDimitry Andric Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv); 80090b57cec5SDimitry Andric 80100b57cec5SDimitry Andric Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg; 80110b57cec5SDimitry Andric Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg; 80120b57cec5SDimitry Andric if (FirstConflict && SecondConflict && FirstConflict != SecondConflict) 80130b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 80140b57cec5SDimitry Andric << FirstConflict->getAsString(Args) 80150b57cec5SDimitry Andric << SecondConflict->getAsString(Args); 80160b57cec5SDimitry Andric 80170b57cec5SDimitry Andric if (SingleArg) 80180b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=single"); 80190b57cec5SDimitry Andric else if (MultipleArg) 80200b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=multiple"); 80210b57cec5SDimitry Andric else 80220b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=virtual"); 80230b57cec5SDimitry Andric } 80240b57cec5SDimitry Andric 80255f757f3fSDimitry Andric if (Args.hasArg(options::OPT_regcall4)) 80265f757f3fSDimitry Andric CmdArgs.push_back("-regcall4"); 80275f757f3fSDimitry Andric 80280b57cec5SDimitry Andric // Parse the default calling convention options. 80290b57cec5SDimitry Andric if (Arg *CCArg = 80300b57cec5SDimitry Andric Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr, 80310b57cec5SDimitry Andric options::OPT__SLASH_Gz, options::OPT__SLASH_Gv, 80320b57cec5SDimitry Andric options::OPT__SLASH_Gregcall)) { 80330b57cec5SDimitry Andric unsigned DCCOptId = CCArg->getOption().getID(); 80340b57cec5SDimitry Andric const char *DCCFlag = nullptr; 80355ffd83dbSDimitry Andric bool ArchSupported = !isNVPTX; 80360b57cec5SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 80370b57cec5SDimitry Andric switch (DCCOptId) { 80380b57cec5SDimitry Andric case options::OPT__SLASH_Gd: 80390b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=cdecl"; 80400b57cec5SDimitry Andric break; 80410b57cec5SDimitry Andric case options::OPT__SLASH_Gr: 80420b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 80430b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=fastcall"; 80440b57cec5SDimitry Andric break; 80450b57cec5SDimitry Andric case options::OPT__SLASH_Gz: 80460b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 80470b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=stdcall"; 80480b57cec5SDimitry Andric break; 80490b57cec5SDimitry Andric case options::OPT__SLASH_Gv: 80500b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 80510b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=vectorcall"; 80520b57cec5SDimitry Andric break; 80530b57cec5SDimitry Andric case options::OPT__SLASH_Gregcall: 80540b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 80550b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=regcall"; 80560b57cec5SDimitry Andric break; 80570b57cec5SDimitry Andric } 80580b57cec5SDimitry Andric 80590b57cec5SDimitry Andric // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either. 80600b57cec5SDimitry Andric if (ArchSupported && DCCFlag) 80610b57cec5SDimitry Andric CmdArgs.push_back(DCCFlag); 80620b57cec5SDimitry Andric } 80630b57cec5SDimitry Andric 80645f757f3fSDimitry Andric if (Args.hasArg(options::OPT__SLASH_Gregcall4)) 80655f757f3fSDimitry Andric CmdArgs.push_back("-regcall4"); 80665f757f3fSDimitry Andric 80670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ); 80680b57cec5SDimitry Andric 80690b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { 80700b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 80710b57cec5SDimitry Andric CmdArgs.push_back("msvc"); 80720b57cec5SDimitry Andric } 80730b57cec5SDimitry Andric 807481ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) 807581ad6265SDimitry Andric CmdArgs.push_back("-fms-kernel"); 807681ad6265SDimitry Andric 8077a324c340SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__SLASH_guard)) { 8078480093f4SDimitry Andric StringRef GuardArgs = A->getValue(); 8079fe6060f1SDimitry Andric // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and 8080fe6060f1SDimitry Andric // "ehcont-". 8081fe6060f1SDimitry Andric if (GuardArgs.equals_insensitive("cf")) { 8082480093f4SDimitry Andric // Emit CFG instrumentation and the table of address-taken functions. 80830b57cec5SDimitry Andric CmdArgs.push_back("-cfguard"); 8084fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf,nochecks")) { 8085480093f4SDimitry Andric // Emit only the table of address-taken functions. 8086480093f4SDimitry Andric CmdArgs.push_back("-cfguard-no-checks"); 8087fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("ehcont")) { 8088fe6060f1SDimitry Andric // Emit EH continuation table. 8089fe6060f1SDimitry Andric CmdArgs.push_back("-ehcontguard"); 8090fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf-") || 8091fe6060f1SDimitry Andric GuardArgs.equals_insensitive("ehcont-")) { 8092480093f4SDimitry Andric // Do nothing, but we might want to emit a security warning in future. 8093480093f4SDimitry Andric } else { 8094480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs; 8095480093f4SDimitry Andric } 80962efbaac7SDimitry Andric A->claim(); 80970b57cec5SDimitry Andric } 80980b57cec5SDimitry Andric } 80990b57cec5SDimitry Andric 81000b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args, 81010b57cec5SDimitry Andric const InputInfo &Input) { 81020b57cec5SDimitry Andric return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())); 81030b57cec5SDimitry Andric } 81040b57cec5SDimitry Andric 81050b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args, 81060b57cec5SDimitry Andric const InputInfoList &Inputs) { 81070b57cec5SDimitry Andric const char *Str = getBaseInputName(Args, Inputs[0]); 81080b57cec5SDimitry Andric 81090b57cec5SDimitry Andric if (const char *End = strrchr(Str, '.')) 81100b57cec5SDimitry Andric return Args.MakeArgString(std::string(Str, End)); 81110b57cec5SDimitry Andric 81120b57cec5SDimitry Andric return Str; 81130b57cec5SDimitry Andric } 81140b57cec5SDimitry Andric 81150b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args, 81160b57cec5SDimitry Andric const InputInfoList &Inputs) { 81170b57cec5SDimitry Andric // FIXME: Think about this more. 81180b57cec5SDimitry Andric 81190b57cec5SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 8120a7dea167SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 8121a7dea167SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d')); 8122a7dea167SDimitry Andric return Args.MakeArgString(OutputFilename); 81230b57cec5SDimitry Andric } 8124a7dea167SDimitry Andric 8125a7dea167SDimitry Andric return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d"); 81260b57cec5SDimitry Andric } 81270b57cec5SDimitry Andric 81280b57cec5SDimitry Andric // Begin ClangAs 81290b57cec5SDimitry Andric 81300b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args, 81310b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 81320b57cec5SDimitry Andric StringRef CPUName; 81330b57cec5SDimitry Andric StringRef ABIName; 81340b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 81350b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 81360b57cec5SDimitry Andric 81370b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 81380b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 81390b57cec5SDimitry Andric } 81400b57cec5SDimitry Andric 81410b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args, 81420b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 81435ffd83dbSDimitry Andric addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs, 81445ffd83dbSDimitry Andric /*IsLTO=*/false); 8145480093f4SDimitry Andric 81460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 81470b57cec5SDimitry Andric StringRef Value = A->getValue(); 81480b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 81490b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 81500b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 81510b57cec5SDimitry Andric } else { 81520b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) 8153bdd1243dSDimitry Andric << A->getSpelling() << Value; 81540b57cec5SDimitry Andric } 81550b57cec5SDimitry Andric } 81560b57cec5SDimitry Andric } 81570b57cec5SDimitry Andric 815806c3fb27SDimitry Andric void ClangAs::AddLoongArchTargetArgs(const ArgList &Args, 815906c3fb27SDimitry Andric ArgStringList &CmdArgs) const { 816006c3fb27SDimitry Andric CmdArgs.push_back("-target-abi"); 816106c3fb27SDimitry Andric CmdArgs.push_back(loongarch::getLoongArchABI(getToolChain().getDriver(), Args, 816206c3fb27SDimitry Andric getToolChain().getTriple()) 816306c3fb27SDimitry Andric .data()); 816406c3fb27SDimitry Andric } 816506c3fb27SDimitry Andric 81660b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args, 81670b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 81680b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 81690b57cec5SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 81700b57cec5SDimitry Andric 81710b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 81720b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 817306c3fb27SDimitry Andric 817406c3fb27SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 817506c3fb27SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 817606c3fb27SDimitry Andric CmdArgs.push_back("-mllvm"); 817706c3fb27SDimitry Andric CmdArgs.push_back("-riscv-add-build-attributes"); 817806c3fb27SDimitry Andric } 81790b57cec5SDimitry Andric } 81800b57cec5SDimitry Andric 81810b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 81820b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 81830b57cec5SDimitry Andric const ArgList &Args, 81840b57cec5SDimitry Andric const char *LinkingOutput) const { 81850b57cec5SDimitry Andric ArgStringList CmdArgs; 81860b57cec5SDimitry Andric 81870b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Unexpected number of inputs."); 81880b57cec5SDimitry Andric const InputInfo &Input = Inputs[0]; 81890b57cec5SDimitry Andric 81900b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 81910b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 81920b57cec5SDimitry Andric const auto &D = getToolChain().getDriver(); 81930b57cec5SDimitry Andric 81940b57cec5SDimitry Andric // Don't warn about "clang -w -c foo.s" 81950b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_w); 81960b57cec5SDimitry Andric // and "clang -emit-llvm -c foo.s" 81970b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 81980b57cec5SDimitry Andric 81990b57cec5SDimitry Andric claimNoWarnArgs(Args); 82000b57cec5SDimitry Andric 82010b57cec5SDimitry Andric // Invoke ourselves in -cc1as mode. 82020b57cec5SDimitry Andric // 82030b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 82040b57cec5SDimitry Andric CmdArgs.push_back("-cc1as"); 82050b57cec5SDimitry Andric 82060b57cec5SDimitry Andric // Add the "effective" target triple. 82070b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 82080b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 8209bdd1243dSDimitry Andric 8210bdd1243dSDimitry Andric getToolChain().addClangCC1ASTargetOptions(Args, CmdArgs); 82110b57cec5SDimitry Andric 82120b57cec5SDimitry Andric // Set the output mode, we currently only expect to be used as a real 82130b57cec5SDimitry Andric // assembler. 82140b57cec5SDimitry Andric CmdArgs.push_back("-filetype"); 82150b57cec5SDimitry Andric CmdArgs.push_back("obj"); 82160b57cec5SDimitry Andric 82170b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 82180b57cec5SDimitry Andric // -save-temps or preprocessed assembly. 82190b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 82200b57cec5SDimitry Andric CmdArgs.push_back(Clang::getBaseInputName(Args, Input)); 82210b57cec5SDimitry Andric 82220b57cec5SDimitry Andric // Add the target cpu 8223349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ true); 82240b57cec5SDimitry Andric if (!CPU.empty()) { 82250b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 82260b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 82270b57cec5SDimitry Andric } 82280b57cec5SDimitry Andric 82290b57cec5SDimitry Andric // Add the target features 82305ffd83dbSDimitry Andric getTargetFeatures(D, Triple, Args, CmdArgs, true); 82310b57cec5SDimitry Andric 82320b57cec5SDimitry Andric // Ignore explicit -force_cpusubtype_ALL option. 82330b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 82340b57cec5SDimitry Andric 82350b57cec5SDimitry Andric // Pass along any -I options so we get proper .include search paths. 82360b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I_Group); 82370b57cec5SDimitry Andric 82380b57cec5SDimitry Andric // Determine the original source input. 82390eae32dcSDimitry Andric auto FindSource = [](const Action *S) -> const Action * { 82400eae32dcSDimitry Andric while (S->getKind() != Action::InputClass) { 82410eae32dcSDimitry Andric assert(!S->getInputs().empty() && "unexpected root action!"); 82420eae32dcSDimitry Andric S = S->getInputs()[0]; 82430b57cec5SDimitry Andric } 82440eae32dcSDimitry Andric return S; 82450eae32dcSDimitry Andric }; 82460eae32dcSDimitry Andric const Action *SourceAction = FindSource(&JA); 82470b57cec5SDimitry Andric 82480b57cec5SDimitry Andric // Forward -g and handle debug info related flags, assuming we are dealing 82490b57cec5SDimitry Andric // with an actual assembly file. 82500b57cec5SDimitry Andric bool WantDebug = false; 82510b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_Group); 8252fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 82530b57cec5SDimitry Andric WantDebug = !A->getOption().matches(options::OPT_g0) && 82540b57cec5SDimitry Andric !A->getOption().matches(options::OPT_ggdb0); 8255480093f4SDimitry Andric 825606c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind DebugInfoKind = 825706c3fb27SDimitry Andric llvm::codegenoptions::NoDebugInfo; 82580b57cec5SDimitry Andric 82590eae32dcSDimitry Andric // Add the -fdebug-compilation-dir flag if needed. 82600eae32dcSDimitry Andric const char *DebugCompilationDir = 82610eae32dcSDimitry Andric addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); 82620eae32dcSDimitry Andric 82630b57cec5SDimitry Andric if (SourceAction->getType() == types::TY_Asm || 82640b57cec5SDimitry Andric SourceAction->getType() == types::TY_PP_Asm) { 82650b57cec5SDimitry Andric // You might think that it would be ok to set DebugInfoKind outside of 82660b57cec5SDimitry Andric // the guard for source type, however there is a test which asserts 82670b57cec5SDimitry Andric // that some assembler invocation receives no -debug-info-kind, 82680b57cec5SDimitry Andric // and it's not clear whether that test is just overly restrictive. 826906c3fb27SDimitry Andric DebugInfoKind = (WantDebug ? llvm::codegenoptions::DebugInfoConstructor 827006c3fb27SDimitry Andric : llvm::codegenoptions::NoDebugInfo); 82710b57cec5SDimitry Andric 827281ad6265SDimitry Andric addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args, 827381ad6265SDimitry Andric CmdArgs); 82740b57cec5SDimitry Andric 82750b57cec5SDimitry Andric // Set the AT_producer to the clang version when using the integrated 82760b57cec5SDimitry Andric // assembler on assembly source files. 82770b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-producer"); 82780b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 82790b57cec5SDimitry Andric 82800b57cec5SDimitry Andric // And pass along -I options 82810b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I); 82820b57cec5SDimitry Andric } 8283bdd1243dSDimitry Andric const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); 82840b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion, 82850b57cec5SDimitry Andric llvm::DebuggerKind::Default); 8286fe6060f1SDimitry Andric renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion); 82870b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain()); 82880b57cec5SDimitry Andric 82890b57cec5SDimitry Andric // Handle -fPIC et al -- the relocation-model affects the assembler 82900b57cec5SDimitry Andric // for some targets. 82910b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 82920b57cec5SDimitry Andric unsigned PICLevel; 82930b57cec5SDimitry Andric bool IsPIE; 82940b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 82950b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 82960b57cec5SDimitry Andric 82970b57cec5SDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 82980b57cec5SDimitry Andric if (RMName) { 82990b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 83000b57cec5SDimitry Andric CmdArgs.push_back(RMName); 83010b57cec5SDimitry Andric } 83020b57cec5SDimitry Andric 83030b57cec5SDimitry Andric // Optionally embed the -cc1as level arguments into the debug info, for build 83040b57cec5SDimitry Andric // analysis. 83050b57cec5SDimitry Andric if (getToolChain().UseDwarfDebugFlags()) { 83060b57cec5SDimitry Andric ArgStringList OriginalArgs; 83070b57cec5SDimitry Andric for (const auto &Arg : Args) 83080b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 83090b57cec5SDimitry Andric 83100b57cec5SDimitry Andric SmallString<256> Flags; 83110b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 83125ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 83130b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 83140b57cec5SDimitry Andric SmallString<128> EscapedArg; 83150b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 83160b57cec5SDimitry Andric Flags += " "; 83170b57cec5SDimitry Andric Flags += EscapedArg; 83180b57cec5SDimitry Andric } 83190b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 83200b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Flags)); 83210b57cec5SDimitry Andric } 83220b57cec5SDimitry Andric 83230b57cec5SDimitry Andric // FIXME: Add -static support, once we have it. 83240b57cec5SDimitry Andric 83250b57cec5SDimitry Andric // Add target specific flags. 83260b57cec5SDimitry Andric switch (getToolChain().getArch()) { 83270b57cec5SDimitry Andric default: 83280b57cec5SDimitry Andric break; 83290b57cec5SDimitry Andric 83300b57cec5SDimitry Andric case llvm::Triple::mips: 83310b57cec5SDimitry Andric case llvm::Triple::mipsel: 83320b57cec5SDimitry Andric case llvm::Triple::mips64: 83330b57cec5SDimitry Andric case llvm::Triple::mips64el: 83340b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 83350b57cec5SDimitry Andric break; 83360b57cec5SDimitry Andric 83370b57cec5SDimitry Andric case llvm::Triple::x86: 83380b57cec5SDimitry Andric case llvm::Triple::x86_64: 83390b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 83400b57cec5SDimitry Andric break; 83410b57cec5SDimitry Andric 83420b57cec5SDimitry Andric case llvm::Triple::arm: 83430b57cec5SDimitry Andric case llvm::Triple::armeb: 83440b57cec5SDimitry Andric case llvm::Triple::thumb: 83450b57cec5SDimitry Andric case llvm::Triple::thumbeb: 83460b57cec5SDimitry Andric // This isn't in AddARMTargetArgs because we want to do this for assembly 83470b57cec5SDimitry Andric // only, not C/C++. 83480b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 83490b57cec5SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 83500b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 83510b57cec5SDimitry Andric CmdArgs.push_back("-arm-add-build-attributes"); 83520b57cec5SDimitry Andric } 83530b57cec5SDimitry Andric break; 83540b57cec5SDimitry Andric 8355e8d8bef9SDimitry Andric case llvm::Triple::aarch64: 8356e8d8bef9SDimitry Andric case llvm::Triple::aarch64_32: 8357e8d8bef9SDimitry Andric case llvm::Triple::aarch64_be: 8358e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_mmark_bti_property)) { 8359e8d8bef9SDimitry Andric CmdArgs.push_back("-mllvm"); 8360e8d8bef9SDimitry Andric CmdArgs.push_back("-aarch64-mark-bti-property"); 8361e8d8bef9SDimitry Andric } 8362e8d8bef9SDimitry Andric break; 8363e8d8bef9SDimitry Andric 836406c3fb27SDimitry Andric case llvm::Triple::loongarch32: 836506c3fb27SDimitry Andric case llvm::Triple::loongarch64: 836606c3fb27SDimitry Andric AddLoongArchTargetArgs(Args, CmdArgs); 836706c3fb27SDimitry Andric break; 836806c3fb27SDimitry Andric 83690b57cec5SDimitry Andric case llvm::Triple::riscv32: 83700b57cec5SDimitry Andric case llvm::Triple::riscv64: 83710b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 83720b57cec5SDimitry Andric break; 83730b57cec5SDimitry Andric } 83740b57cec5SDimitry Andric 83750b57cec5SDimitry Andric // Consume all the warning flags. Usually this would be handled more 83760b57cec5SDimitry Andric // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as 83770b57cec5SDimitry Andric // doesn't handle that so rather than warning about unused flags that are 83780b57cec5SDimitry Andric // actually used, we'll lie by omission instead. 83790b57cec5SDimitry Andric // FIXME: Stop lying and consume only the appropriate driver flags 83800b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_W_Group); 83810b57cec5SDimitry Andric 83820b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 83830b57cec5SDimitry Andric getToolChain().getDriver()); 83840b57cec5SDimitry Andric 83850b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 83860b57cec5SDimitry Andric 838706c3fb27SDimitry Andric if (DebugInfoKind > llvm::codegenoptions::NoDebugInfo && Output.isFilename()) 83880eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 83890eae32dcSDimitry Andric Output.getFilename()); 83900eae32dcSDimitry Andric 83910eae32dcSDimitry Andric // Fixup any previous commands that use -object-file-name because when we 83920eae32dcSDimitry Andric // generated them, the final .obj name wasn't yet known. 83930eae32dcSDimitry Andric for (Command &J : C.getJobs()) { 83940eae32dcSDimitry Andric if (SourceAction != FindSource(&J.getSource())) 83950eae32dcSDimitry Andric continue; 83960eae32dcSDimitry Andric auto &JArgs = J.getArguments(); 83970eae32dcSDimitry Andric for (unsigned I = 0; I < JArgs.size(); ++I) { 83985f757f3fSDimitry Andric if (StringRef(JArgs[I]).starts_with("-object-file-name=") && 83990eae32dcSDimitry Andric Output.isFilename()) { 84000eae32dcSDimitry Andric ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); 84010eae32dcSDimitry Andric addDebugObjectName(Args, NewArgs, DebugCompilationDir, 84020eae32dcSDimitry Andric Output.getFilename()); 84030eae32dcSDimitry Andric NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); 84040eae32dcSDimitry Andric J.replaceArguments(NewArgs); 84050eae32dcSDimitry Andric break; 84060eae32dcSDimitry Andric } 84070eae32dcSDimitry Andric } 84080eae32dcSDimitry Andric } 84090eae32dcSDimitry Andric 84100b57cec5SDimitry Andric assert(Output.isFilename() && "Unexpected lipo output."); 84110b57cec5SDimitry Andric CmdArgs.push_back("-o"); 84120b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 84130b57cec5SDimitry Andric 84140b57cec5SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 84150b57cec5SDimitry Andric Arg *A; 84160b57cec5SDimitry Andric if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split && 84170b57cec5SDimitry Andric T.isOSBinFormatELF()) { 84180b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 8419e8d8bef9SDimitry Andric CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output)); 84200b57cec5SDimitry Andric } 84210b57cec5SDimitry Andric 8422e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) 842381ad6265SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true); 8424e8d8bef9SDimitry Andric 84250b57cec5SDimitry Andric assert(Input.isFilename() && "Invalid input."); 84260b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 84270b57cec5SDimitry Andric 84280b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 8429e8d8bef9SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 8430e8d8bef9SDimitry Andric // Invoke cc1as directly in this process. 843106c3fb27SDimitry Andric C.addCommand(std::make_unique<CC1Command>( 843206c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 843306c3fb27SDimitry Andric Output, D.getPrependArg())); 8434e8d8bef9SDimitry Andric } else { 843506c3fb27SDimitry Andric C.addCommand(std::make_unique<Command>( 843606c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 843706c3fb27SDimitry Andric Output, D.getPrependArg())); 8438e8d8bef9SDimitry Andric } 84390b57cec5SDimitry Andric } 84400b57cec5SDimitry Andric 84410b57cec5SDimitry Andric // Begin OffloadBundler 84420b57cec5SDimitry Andric 84430b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, 84440b57cec5SDimitry Andric const InputInfo &Output, 84450b57cec5SDimitry Andric const InputInfoList &Inputs, 84460b57cec5SDimitry Andric const llvm::opt::ArgList &TCArgs, 84470b57cec5SDimitry Andric const char *LinkingOutput) const { 84480b57cec5SDimitry Andric // The version with only one output is expected to refer to a bundling job. 84490b57cec5SDimitry Andric assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!"); 84500b57cec5SDimitry Andric 84510b57cec5SDimitry Andric // The bundling command looks like this: 84520b57cec5SDimitry Andric // clang-offload-bundler -type=bc 84530b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 845481ad6265SDimitry Andric // -output=output_file 845581ad6265SDimitry Andric // -input=unbundle_file_host 845681ad6265SDimitry Andric // -input=unbundle_file_tgt1 845781ad6265SDimitry Andric // -input=unbundle_file_tgt2 84580b57cec5SDimitry Andric 84590b57cec5SDimitry Andric ArgStringList CmdArgs; 84600b57cec5SDimitry Andric 84610b57cec5SDimitry Andric // Get the type. 84620b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 84630b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Output.getType()))); 84640b57cec5SDimitry Andric 84650b57cec5SDimitry Andric assert(JA.getInputs().size() == Inputs.size() && 84660b57cec5SDimitry Andric "Not have inputs for all dependence actions??"); 84670b57cec5SDimitry Andric 84680b57cec5SDimitry Andric // Get the targets. 84690b57cec5SDimitry Andric SmallString<128> Triples; 84700b57cec5SDimitry Andric Triples += "-targets="; 84710b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 84720b57cec5SDimitry Andric if (I) 84730b57cec5SDimitry Andric Triples += ','; 84740b57cec5SDimitry Andric 84750b57cec5SDimitry Andric // Find ToolChain for this input. 84760b57cec5SDimitry Andric Action::OffloadKind CurKind = Action::OFK_Host; 84770b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 84780b57cec5SDimitry Andric const Action *CurDep = JA.getInputs()[I]; 84790b57cec5SDimitry Andric 84800b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) { 84810b57cec5SDimitry Andric CurTC = nullptr; 84820b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) { 84830b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 84840b57cec5SDimitry Andric CurKind = A->getOffloadingDeviceKind(); 84850b57cec5SDimitry Andric CurTC = TC; 84860b57cec5SDimitry Andric }); 84870b57cec5SDimitry Andric } 84880b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(CurKind); 8489349cc55cSDimitry Andric Triples += '-'; 8490349cc55cSDimitry Andric Triples += CurTC->getTriple().normalize(); 8491349cc55cSDimitry Andric if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) && 84920eae32dcSDimitry Andric !StringRef(CurDep->getOffloadingArch()).empty()) { 8493349cc55cSDimitry Andric Triples += '-'; 84940b57cec5SDimitry Andric Triples += CurDep->getOffloadingArch(); 84950b57cec5SDimitry Andric } 8496349cc55cSDimitry Andric 8497349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8498349cc55cSDimitry Andric // with each toolchain. 8499349cc55cSDimitry Andric StringRef GPUArchName; 8500349cc55cSDimitry Andric if (CurKind == Action::OFK_OpenMP) { 8501349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8502349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8503349cc55cSDimitry Andric auto ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 850406c3fb27SDimitry Andric auto Arch = ArchStr.starts_with_insensitive("-march="); 8505349cc55cSDimitry Andric if (Arch) { 8506349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8507349cc55cSDimitry Andric Triples += "-"; 8508349cc55cSDimitry Andric break; 8509349cc55cSDimitry Andric } 8510349cc55cSDimitry Andric } 8511349cc55cSDimitry Andric Triples += GPUArchName.str(); 8512349cc55cSDimitry Andric } 85130b57cec5SDimitry Andric } 85140b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 85150b57cec5SDimitry Andric 85160b57cec5SDimitry Andric // Get bundled file command. 85170b57cec5SDimitry Andric CmdArgs.push_back( 851881ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-output=") + Output.getFilename())); 85190b57cec5SDimitry Andric 85200b57cec5SDimitry Andric // Get unbundled files command. 85210b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 852281ad6265SDimitry Andric SmallString<128> UB; 852381ad6265SDimitry Andric UB += "-input="; 85240b57cec5SDimitry Andric 85250b57cec5SDimitry Andric // Find ToolChain for this input. 85260b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 85270b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) { 85280b57cec5SDimitry Andric CurTC = nullptr; 85290b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) { 85300b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 85310b57cec5SDimitry Andric CurTC = TC; 85320b57cec5SDimitry Andric }); 85336e75b2fbSDimitry Andric UB += C.addTempFile( 85346e75b2fbSDimitry Andric C.getArgs().MakeArgString(CurTC->getInputFilename(Inputs[I]))); 85356e75b2fbSDimitry Andric } else { 85360b57cec5SDimitry Andric UB += CurTC->getInputFilename(Inputs[I]); 85370b57cec5SDimitry Andric } 85380b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 853981ad6265SDimitry Andric } 85405f757f3fSDimitry Andric if (TCArgs.hasFlag(options::OPT_offload_compress, 85415f757f3fSDimitry Andric options::OPT_no_offload_compress, false)) 85425f757f3fSDimitry Andric CmdArgs.push_back("-compress"); 85435f757f3fSDimitry Andric if (TCArgs.hasArg(options::OPT_v)) 85445f757f3fSDimitry Andric CmdArgs.push_back("-verbose"); 85450b57cec5SDimitry Andric // All the inputs are encoded as commands. 8546a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 85475ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 85480b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8549bdd1243dSDimitry Andric CmdArgs, std::nullopt, Output)); 85500b57cec5SDimitry Andric } 85510b57cec5SDimitry Andric 85520b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs( 85530b57cec5SDimitry Andric Compilation &C, const JobAction &JA, const InputInfoList &Outputs, 85540b57cec5SDimitry Andric const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, 85550b57cec5SDimitry Andric const char *LinkingOutput) const { 85560b57cec5SDimitry Andric // The version with multiple outputs is expected to refer to a unbundling job. 85570b57cec5SDimitry Andric auto &UA = cast<OffloadUnbundlingJobAction>(JA); 85580b57cec5SDimitry Andric 85590b57cec5SDimitry Andric // The unbundling command looks like this: 85600b57cec5SDimitry Andric // clang-offload-bundler -type=bc 85610b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 856281ad6265SDimitry Andric // -input=input_file 856381ad6265SDimitry Andric // -output=unbundle_file_host 856481ad6265SDimitry Andric // -output=unbundle_file_tgt1 856581ad6265SDimitry Andric // -output=unbundle_file_tgt2 85660b57cec5SDimitry Andric // -unbundle 85670b57cec5SDimitry Andric 85680b57cec5SDimitry Andric ArgStringList CmdArgs; 85690b57cec5SDimitry Andric 85700b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Expecting to unbundle a single file!"); 85710b57cec5SDimitry Andric InputInfo Input = Inputs.front(); 85720b57cec5SDimitry Andric 85730b57cec5SDimitry Andric // Get the type. 85740b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 85750b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Input.getType()))); 85760b57cec5SDimitry Andric 85770b57cec5SDimitry Andric // Get the targets. 85780b57cec5SDimitry Andric SmallString<128> Triples; 85790b57cec5SDimitry Andric Triples += "-targets="; 85800b57cec5SDimitry Andric auto DepInfo = UA.getDependentActionsInfo(); 85810b57cec5SDimitry Andric for (unsigned I = 0; I < DepInfo.size(); ++I) { 85820b57cec5SDimitry Andric if (I) 85830b57cec5SDimitry Andric Triples += ','; 85840b57cec5SDimitry Andric 85850b57cec5SDimitry Andric auto &Dep = DepInfo[I]; 85860b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind); 8587349cc55cSDimitry Andric Triples += '-'; 8588349cc55cSDimitry Andric Triples += Dep.DependentToolChain->getTriple().normalize(); 8589349cc55cSDimitry Andric if ((Dep.DependentOffloadKind == Action::OFK_HIP || 8590349cc55cSDimitry Andric Dep.DependentOffloadKind == Action::OFK_Cuda) && 8591349cc55cSDimitry Andric !Dep.DependentBoundArch.empty()) { 8592349cc55cSDimitry Andric Triples += '-'; 85930b57cec5SDimitry Andric Triples += Dep.DependentBoundArch; 85940b57cec5SDimitry Andric } 8595349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8596349cc55cSDimitry Andric // with each toolchain. 8597349cc55cSDimitry Andric StringRef GPUArchName; 8598349cc55cSDimitry Andric if (Dep.DependentOffloadKind == Action::OFK_OpenMP) { 8599349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8600349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8601349cc55cSDimitry Andric StringRef ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 860206c3fb27SDimitry Andric auto Arch = ArchStr.starts_with_insensitive("-march="); 8603349cc55cSDimitry Andric if (Arch) { 8604349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8605349cc55cSDimitry Andric Triples += "-"; 8606349cc55cSDimitry Andric break; 8607349cc55cSDimitry Andric } 8608349cc55cSDimitry Andric } 8609349cc55cSDimitry Andric Triples += GPUArchName.str(); 8610349cc55cSDimitry Andric } 86110b57cec5SDimitry Andric } 86120b57cec5SDimitry Andric 86130b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 86140b57cec5SDimitry Andric 86150b57cec5SDimitry Andric // Get bundled file command. 86160b57cec5SDimitry Andric CmdArgs.push_back( 861781ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-input=") + Input.getFilename())); 86180b57cec5SDimitry Andric 86190b57cec5SDimitry Andric // Get unbundled files command. 86200b57cec5SDimitry Andric for (unsigned I = 0; I < Outputs.size(); ++I) { 862181ad6265SDimitry Andric SmallString<128> UB; 862281ad6265SDimitry Andric UB += "-output="; 86230b57cec5SDimitry Andric UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]); 86240b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 862581ad6265SDimitry Andric } 86260b57cec5SDimitry Andric CmdArgs.push_back("-unbundle"); 8627e8d8bef9SDimitry Andric CmdArgs.push_back("-allow-missing-bundles"); 86285f757f3fSDimitry Andric if (TCArgs.hasArg(options::OPT_v)) 86295f757f3fSDimitry Andric CmdArgs.push_back("-verbose"); 86300b57cec5SDimitry Andric 86310b57cec5SDimitry Andric // All the inputs are encoded as commands. 8632a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 86335ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 86340b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8635bdd1243dSDimitry Andric CmdArgs, std::nullopt, Outputs)); 8636a7dea167SDimitry Andric } 86371fd87a68SDimitry Andric 863881ad6265SDimitry Andric void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA, 863981ad6265SDimitry Andric const InputInfo &Output, 864081ad6265SDimitry Andric const InputInfoList &Inputs, 864181ad6265SDimitry Andric const llvm::opt::ArgList &Args, 864281ad6265SDimitry Andric const char *LinkingOutput) const { 864381ad6265SDimitry Andric ArgStringList CmdArgs; 864481ad6265SDimitry Andric 864581ad6265SDimitry Andric // Add the output file name. 864681ad6265SDimitry Andric assert(Output.isFilename() && "Invalid output."); 864781ad6265SDimitry Andric CmdArgs.push_back("-o"); 864881ad6265SDimitry Andric CmdArgs.push_back(Output.getFilename()); 864981ad6265SDimitry Andric 865081ad6265SDimitry Andric // Create the inputs to bundle the needed metadata. 865181ad6265SDimitry Andric for (const InputInfo &Input : Inputs) { 865281ad6265SDimitry Andric const Action *OffloadAction = Input.getAction(); 865381ad6265SDimitry Andric const ToolChain *TC = OffloadAction->getOffloadingToolChain(); 865481ad6265SDimitry Andric const ArgList &TCArgs = 865581ad6265SDimitry Andric C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(), 865681ad6265SDimitry Andric OffloadAction->getOffloadingDeviceKind()); 865781ad6265SDimitry Andric StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input)); 865806c3fb27SDimitry Andric StringRef Arch = OffloadAction->getOffloadingArch() 865981ad6265SDimitry Andric ? OffloadAction->getOffloadingArch() 866081ad6265SDimitry Andric : TCArgs.getLastArgValue(options::OPT_march_EQ); 866181ad6265SDimitry Andric StringRef Kind = 866281ad6265SDimitry Andric Action::GetOffloadKindName(OffloadAction->getOffloadingDeviceKind()); 866381ad6265SDimitry Andric 866481ad6265SDimitry Andric ArgStringList Features; 866581ad6265SDimitry Andric SmallVector<StringRef> FeatureArgs; 8666753f127fSDimitry Andric getTargetFeatures(TC->getDriver(), TC->getTriple(), TCArgs, Features, 8667753f127fSDimitry Andric false); 866881ad6265SDimitry Andric llvm::copy_if(Features, std::back_inserter(FeatureArgs), 86695f757f3fSDimitry Andric [](StringRef Arg) { return !Arg.starts_with("-target"); }); 867081ad6265SDimitry Andric 867106c3fb27SDimitry Andric if (TC->getTriple().isAMDGPU()) { 867206c3fb27SDimitry Andric for (StringRef Feature : llvm::split(Arch.split(':').second, ':')) { 867306c3fb27SDimitry Andric FeatureArgs.emplace_back( 867406c3fb27SDimitry Andric Args.MakeArgString(Feature.take_back() + Feature.drop_back())); 867506c3fb27SDimitry Andric } 867606c3fb27SDimitry Andric } 867706c3fb27SDimitry Andric 867806c3fb27SDimitry Andric // TODO: We need to pass in the full target-id and handle it properly in the 867906c3fb27SDimitry Andric // linker wrapper. 868081ad6265SDimitry Andric SmallVector<std::string> Parts{ 868181ad6265SDimitry Andric "file=" + File.str(), 868281ad6265SDimitry Andric "triple=" + TC->getTripleString(), 868306c3fb27SDimitry Andric "arch=" + getProcessorFromTargetID(TC->getTriple(), Arch).str(), 868481ad6265SDimitry Andric "kind=" + Kind.str(), 868581ad6265SDimitry Andric }; 868681ad6265SDimitry Andric 868706c3fb27SDimitry Andric if (TC->getDriver().isUsingLTO(/* IsOffload */ true) || 868806c3fb27SDimitry Andric TC->getTriple().isAMDGPU()) 868981ad6265SDimitry Andric for (StringRef Feature : FeatureArgs) 869081ad6265SDimitry Andric Parts.emplace_back("feature=" + Feature.str()); 869181ad6265SDimitry Andric 869281ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("--image=" + llvm::join(Parts, ","))); 869381ad6265SDimitry Andric } 869481ad6265SDimitry Andric 869581ad6265SDimitry Andric C.addCommand(std::make_unique<Command>( 869681ad6265SDimitry Andric JA, *this, ResponseFileSupport::None(), 869781ad6265SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), 869881ad6265SDimitry Andric CmdArgs, Inputs, Output)); 869981ad6265SDimitry Andric } 870081ad6265SDimitry Andric 87011fd87a68SDimitry Andric void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, 87021fd87a68SDimitry Andric const InputInfo &Output, 87031fd87a68SDimitry Andric const InputInfoList &Inputs, 87041fd87a68SDimitry Andric const ArgList &Args, 87051fd87a68SDimitry Andric const char *LinkingOutput) const { 8706d56accc7SDimitry Andric const Driver &D = getToolChain().getDriver(); 8707d56accc7SDimitry Andric const llvm::Triple TheTriple = getToolChain().getTriple(); 87081fd87a68SDimitry Andric ArgStringList CmdArgs; 87091fd87a68SDimitry Andric 8710d56accc7SDimitry Andric // Pass the CUDA path to the linker wrapper tool. 871181ad6265SDimitry Andric for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP}) { 871281ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains(Kind); 871381ad6265SDimitry Andric for (auto &I : llvm::make_range(TCRange.first, TCRange.second)) { 8714d56accc7SDimitry Andric const ToolChain *TC = I.second; 8715d56accc7SDimitry Andric if (TC->getTriple().isNVPTX()) { 8716d56accc7SDimitry Andric CudaInstallationDetector CudaInstallation(D, TheTriple, Args); 8717d56accc7SDimitry Andric if (CudaInstallation.isValid()) 8718d56accc7SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8719d56accc7SDimitry Andric "--cuda-path=" + CudaInstallation.getInstallPath())); 8720d56accc7SDimitry Andric break; 8721d56accc7SDimitry Andric } 8722d56accc7SDimitry Andric } 87231fd87a68SDimitry Andric } 87241fd87a68SDimitry Andric 87251fd87a68SDimitry Andric // Pass in the optimization level to use for LTO. 87261fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 87271fd87a68SDimitry Andric StringRef OOpt; 87281fd87a68SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 87291fd87a68SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 87301fd87a68SDimitry Andric OOpt = "3"; 87311fd87a68SDimitry Andric else if (A->getOption().matches(options::OPT_O)) { 87321fd87a68SDimitry Andric OOpt = A->getValue(); 87331fd87a68SDimitry Andric if (OOpt == "g") 87341fd87a68SDimitry Andric OOpt = "1"; 87351fd87a68SDimitry Andric else if (OOpt == "s" || OOpt == "z") 87361fd87a68SDimitry Andric OOpt = "2"; 87371fd87a68SDimitry Andric } else if (A->getOption().matches(options::OPT_O0)) 87381fd87a68SDimitry Andric OOpt = "0"; 87391fd87a68SDimitry Andric if (!OOpt.empty()) 8740753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--opt-level=O") + OOpt)); 87411fd87a68SDimitry Andric } 87421fd87a68SDimitry Andric 8743753f127fSDimitry Andric CmdArgs.push_back( 8744753f127fSDimitry Andric Args.MakeArgString("--host-triple=" + TheTriple.getTriple())); 87451fd87a68SDimitry Andric if (Args.hasArg(options::OPT_v)) 8746bdd1243dSDimitry Andric CmdArgs.push_back("--wrapper-verbose"); 87471fd87a68SDimitry Andric 87481fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 8749753f127fSDimitry Andric if (!A->getOption().matches(options::OPT_g0)) 8750753f127fSDimitry Andric CmdArgs.push_back("--device-debug"); 87511fd87a68SDimitry Andric } 87521fd87a68SDimitry Andric 87535f757f3fSDimitry Andric // code-object-version=X needs to be passed to clang-linker-wrapper to ensure 87545f757f3fSDimitry Andric // that it is used by lld. 87555f757f3fSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mcode_object_version_EQ)) { 87565f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mllvm")); 87575f757f3fSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 87585f757f3fSDimitry Andric Twine("--amdhsa-code-object-version=") + A->getValue())); 87595f757f3fSDimitry Andric } 87605f757f3fSDimitry Andric 87611fd87a68SDimitry Andric for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 8762bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("--ptxas-arg=" + A)); 87631fd87a68SDimitry Andric 87641fd87a68SDimitry Andric // Forward remarks passes to the LLVM backend in the wrapper. 87651fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) 8766fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--offload-opt=-pass-remarks=") + 8767fcaf7f86SDimitry Andric A->getValue())); 87681fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) 8769fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8770fcaf7f86SDimitry Andric Twine("--offload-opt=-pass-remarks-missed=") + A->getValue())); 87711fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) 8772fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8773fcaf7f86SDimitry Andric Twine("--offload-opt=-pass-remarks-analysis=") + A->getValue())); 87741fd87a68SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 8775753f127fSDimitry Andric CmdArgs.push_back("--save-temps"); 87761fd87a68SDimitry Andric 8777d56accc7SDimitry Andric // Construct the link job so we can wrap around it. 8778d56accc7SDimitry Andric Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); 8779d56accc7SDimitry Andric const auto &LinkCommand = C.getJobs().getJobs().back(); 8780d56accc7SDimitry Andric 878181ad6265SDimitry Andric // Forward -Xoffload-linker<-triple> arguments to the device link job. 8782753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_Xoffload_linker)) { 8783753f127fSDimitry Andric StringRef Val = A->getValue(0); 878481ad6265SDimitry Andric if (Val.empty()) 878581ad6265SDimitry Andric CmdArgs.push_back( 8786753f127fSDimitry Andric Args.MakeArgString(Twine("--device-linker=") + A->getValue(1))); 878781ad6265SDimitry Andric else 878881ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8789753f127fSDimitry Andric "--device-linker=" + 879081ad6265SDimitry Andric ToolChain::getOpenMPTriple(Val.drop_front()).getTriple() + "=" + 8791753f127fSDimitry Andric A->getValue(1))); 879281ad6265SDimitry Andric } 879381ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_Xoffload_linker); 879481ad6265SDimitry Andric 8795bdd1243dSDimitry Andric // Embed bitcode instead of an object in JIT mode. 8796bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_jit, 8797bdd1243dSDimitry Andric options::OPT_fno_openmp_target_jit, false)) 8798bdd1243dSDimitry Andric CmdArgs.push_back("--embed-bitcode"); 8799bdd1243dSDimitry Andric 8800753f127fSDimitry Andric // Forward `-mllvm` arguments to the LLVM invocations if present. 8801753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_mllvm)) { 8802753f127fSDimitry Andric CmdArgs.push_back("-mllvm"); 8803753f127fSDimitry Andric CmdArgs.push_back(A->getValue()); 8804753f127fSDimitry Andric A->claim(); 8805753f127fSDimitry Andric } 8806753f127fSDimitry Andric 88071fd87a68SDimitry Andric // Add the linker arguments to be forwarded by the wrapper. 8808753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") + 8809753f127fSDimitry Andric LinkCommand->getExecutable())); 88101fd87a68SDimitry Andric CmdArgs.push_back("--"); 88111fd87a68SDimitry Andric for (const char *LinkArg : LinkCommand->getArguments()) 88121fd87a68SDimitry Andric CmdArgs.push_back(LinkArg); 88131fd87a68SDimitry Andric 88141fd87a68SDimitry Andric const char *Exec = 88151fd87a68SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper")); 88161fd87a68SDimitry Andric 88171fd87a68SDimitry Andric // Replace the executable and arguments of the link job with the 88181fd87a68SDimitry Andric // wrapper. 88191fd87a68SDimitry Andric LinkCommand->replaceExecutable(Exec); 88201fd87a68SDimitry Andric LinkCommand->replaceArguments(CmdArgs); 88211fd87a68SDimitry Andric } 8822