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, 71349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace)) { 720b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) && 730b57cec5SDimitry Andric !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) { 740b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 750b57cec5SDimitry Andric << A->getBaseArg().getAsString(Args) 760b57cec5SDimitry Andric << (D.IsCLMode() ? "/E, /P or /EP" : "-E"); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 820b57cec5SDimitry Andric // In gcc, only ARM checks this, but it seems reasonable to check universally. 830b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 840b57cec5SDimitry Andric if (const Arg *A = 850b57cec5SDimitry Andric Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic)) 860b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args) 870b57cec5SDimitry Andric << "-static"; 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes. 910b57cec5SDimitry Andric // This is used for the space-separated argument list specified with 920b57cec5SDimitry Andric // the -dwarf-debug-flags option. 930b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg, 940b57cec5SDimitry Andric SmallVectorImpl<char> &Res) { 950b57cec5SDimitry Andric for (; *Arg; ++Arg) { 960b57cec5SDimitry Andric switch (*Arg) { 970b57cec5SDimitry Andric default: 980b57cec5SDimitry Andric break; 990b57cec5SDimitry Andric case ' ': 1000b57cec5SDimitry Andric case '\\': 1010b57cec5SDimitry Andric Res.push_back('\\'); 1020b57cec5SDimitry Andric break; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric Res.push_back(*Arg); 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other 1090b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA. 1100b57cec5SDimitry Andric static void 1110b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA, 1120b57cec5SDimitry Andric const ToolChain &RegularToolChain, 1130b57cec5SDimitry Andric llvm::function_ref<void(const ToolChain &)> Work) { 1140b57cec5SDimitry Andric // Apply Work on the current/regular tool chain. 1150b57cec5SDimitry Andric Work(RegularToolChain); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric // Apply Work on all the offloading tool chains associated with the current 1180b57cec5SDimitry Andric // action. 1190b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_Cuda)) 1200b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 1210b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_Cuda)) 1220b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1230b57cec5SDimitry Andric else if (JA.isHostOffloading(Action::OFK_HIP)) 1240b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>()); 1250b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_HIP)) 1260b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 1290b57cec5SDimitry Andric auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>(); 1300b57cec5SDimitry Andric for (auto II = TCs.first, IE = TCs.second; II != IE; ++II) 1310b57cec5SDimitry Andric Work(*II->second); 1320b57cec5SDimitry Andric } else if (JA.isDeviceOffloading(Action::OFK_OpenMP)) 1330b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric // 1360b57cec5SDimitry Andric // TODO: Add support for other offloading programming models here. 1370b57cec5SDimitry Andric // 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step 1410b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error 1420b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position 1430b57cec5SDimitry Andric /// of the refinement step in the input string. 1440b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D, 1450b57cec5SDimitry Andric const Arg &A, size_t &Position) { 1460b57cec5SDimitry Andric const char RefinementStepToken = ':'; 1470b57cec5SDimitry Andric Position = In.find(RefinementStepToken); 1480b57cec5SDimitry Andric if (Position != StringRef::npos) { 1490b57cec5SDimitry Andric StringRef Option = A.getOption().getName(); 1500b57cec5SDimitry Andric StringRef RefStep = In.substr(Position + 1); 1510b57cec5SDimitry Andric // Allow exactly one numeric character for the additional refinement 1520b57cec5SDimitry Andric // step parameter. This is reasonable for all currently-supported 1530b57cec5SDimitry Andric // operations and architectures because we would expect that a larger value 1540b57cec5SDimitry Andric // of refinement steps would cause the estimate "optimization" to 1550b57cec5SDimitry Andric // under-perform the native operation. Also, if the estimate does not 1560b57cec5SDimitry Andric // converge quickly, it probably will not ever converge, so further 1570b57cec5SDimitry Andric // refinement steps will not produce a better answer. 1580b57cec5SDimitry Andric if (RefStep.size() != 1) { 1590b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1600b57cec5SDimitry Andric return false; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric char RefStepChar = RefStep[0]; 1630b57cec5SDimitry Andric if (RefStepChar < '0' || RefStepChar > '9') { 1640b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1650b57cec5SDimitry Andric return false; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric return true; 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters. 1720b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args, 1730b57cec5SDimitry Andric ArgStringList &OutStrings) { 1740b57cec5SDimitry Andric StringRef DisabledPrefixIn = "!"; 1750b57cec5SDimitry Andric StringRef DisabledPrefixOut = "!"; 1760b57cec5SDimitry Andric StringRef EnabledPrefixOut = ""; 1770b57cec5SDimitry Andric StringRef Out = "-mrecip="; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ); 1800b57cec5SDimitry Andric if (!A) 1810b57cec5SDimitry Andric return; 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric unsigned NumOptions = A->getNumValues(); 1840b57cec5SDimitry Andric if (NumOptions == 0) { 1850b57cec5SDimitry Andric // No option is the same as "all". 1860b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + "all")); 1870b57cec5SDimitry Andric return; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric // Pass through "all", "none", or "default" with an optional refinement step. 1910b57cec5SDimitry Andric if (NumOptions == 1) { 1920b57cec5SDimitry Andric StringRef Val = A->getValue(0); 1930b57cec5SDimitry Andric size_t RefStepLoc; 1940b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStepLoc)) 1950b57cec5SDimitry Andric return; 1960b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStepLoc); 1970b57cec5SDimitry Andric if (ValBase == "all" || ValBase == "none" || ValBase == "default") { 1980b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + Val)); 1990b57cec5SDimitry Andric return; 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric // Each reciprocal type may be enabled or disabled individually. 2040b57cec5SDimitry Andric // Check each input value for validity, concatenate them all back together, 2050b57cec5SDimitry Andric // and pass through. 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric llvm::StringMap<bool> OptionStrings; 2080b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divd", false)); 2090b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divf", false)); 21081ad6265SDimitry Andric OptionStrings.insert(std::make_pair("divh", false)); 2110b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divd", false)); 2120b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divf", false)); 21381ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-divh", false)); 2140b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtd", false)); 2150b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtf", false)); 21681ad6265SDimitry Andric OptionStrings.insert(std::make_pair("sqrth", false)); 2170b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtd", false)); 2180b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtf", false)); 21981ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrth", false)); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric for (unsigned i = 0; i != NumOptions; ++i) { 2220b57cec5SDimitry Andric StringRef Val = A->getValue(i); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric bool IsDisabled = Val.startswith(DisabledPrefixIn); 2250b57cec5SDimitry Andric // Ignore the disablement token for string matching. 2260b57cec5SDimitry Andric if (IsDisabled) 2270b57cec5SDimitry Andric Val = Val.substr(1); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric size_t RefStep; 2300b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStep)) 2310b57cec5SDimitry Andric return; 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStep); 2340b57cec5SDimitry Andric llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase); 2350b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2360b57cec5SDimitry Andric // Try again specifying float suffix. 2370b57cec5SDimitry Andric OptionIter = OptionStrings.find(ValBase.str() + 'f'); 2380b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2390b57cec5SDimitry Andric // The input name did not match any known option string. 2400b57cec5SDimitry Andric D.Diag(diag::err_drv_unknown_argument) << Val; 2410b57cec5SDimitry Andric return; 2420b57cec5SDimitry Andric } 24381ad6265SDimitry Andric // The option was specified without a half or float or double suffix. 24481ad6265SDimitry Andric // Make sure that the double or half entry was not already specified. 2450b57cec5SDimitry Andric // The float entry will be checked below. 24681ad6265SDimitry Andric if (OptionStrings[ValBase.str() + 'd'] || 24781ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h']) { 2480b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2490b57cec5SDimitry Andric return; 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric if (OptionIter->second == true) { 2540b57cec5SDimitry Andric // Duplicate option specified. 2550b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2560b57cec5SDimitry Andric return; 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // Mark the matched option as found. Do not allow duplicate specifiers. 2600b57cec5SDimitry Andric OptionIter->second = true; 2610b57cec5SDimitry Andric 26281ad6265SDimitry Andric // If the precision was not specified, also mark the double and half entry 26381ad6265SDimitry Andric // as found. 26481ad6265SDimitry Andric if (ValBase.back() != 'f' && ValBase.back() != 'd' && ValBase.back() != 'h') { 2650b57cec5SDimitry Andric OptionStrings[ValBase.str() + 'd'] = true; 26681ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h'] = true; 26781ad6265SDimitry Andric } 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric // Build the output string. 2700b57cec5SDimitry Andric StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut; 2710b57cec5SDimitry Andric Out = Args.MakeArgString(Out + Prefix + Val); 2720b57cec5SDimitry Andric if (i != NumOptions - 1) 2730b57cec5SDimitry Andric Out = Args.MakeArgString(Out + ","); 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out)); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer 2800b57cec5SDimitry Andric /// or the string "none". 2810b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args, 2820b57cec5SDimitry Andric ArgStringList &CmdArgs) { 2830b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ); 2840b57cec5SDimitry Andric if (!A) 2850b57cec5SDimitry Andric return; 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric StringRef Value = A->getValue(); 2880b57cec5SDimitry Andric if (Value == "none") { 2890b57cec5SDimitry Andric CmdArgs.push_back("-mprefer-vector-width=none"); 2900b57cec5SDimitry Andric } else { 2910b57cec5SDimitry Andric unsigned Width; 2920b57cec5SDimitry Andric if (Value.getAsInteger(10, Width)) { 2930b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 2940b57cec5SDimitry Andric return; 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value)); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric static bool 3010b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 3020b57cec5SDimitry Andric const llvm::Triple &Triple) { 3030b57cec5SDimitry Andric // We use the zero-cost exception tables for Objective-C if the non-fragile 3040b57cec5SDimitry Andric // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 3050b57cec5SDimitry Andric // later. 3060b57cec5SDimitry Andric if (runtime.isNonFragile()) 3070b57cec5SDimitry Andric return true; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric if (!Triple.isMacOSX()) 3100b57cec5SDimitry Andric return false; 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric return (!Triple.isMacOSXVersionLT(10, 5) && 3130b57cec5SDimitry Andric (Triple.getArch() == llvm::Triple::x86_64 || 3140b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::arm)); 3150b57cec5SDimitry Andric } 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a 3184824e7fdSDimitry Andric /// main flag, -fexceptions and also language specific flags to enable/disable 3190b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example 3200b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions. 321fe6060f1SDimitry Andric static bool addExceptionArgs(const ArgList &Args, types::ID InputType, 3220b57cec5SDimitry Andric const ToolChain &TC, bool KernelOrKext, 3230b57cec5SDimitry Andric const ObjCRuntime &objcRuntime, 3240b57cec5SDimitry Andric ArgStringList &CmdArgs) { 3250b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric if (KernelOrKext) { 3280b57cec5SDimitry Andric // -mkernel and -fapple-kext imply no exceptions, so claim exception related 3290b57cec5SDimitry Andric // arguments now to avoid warnings about unused arguments. 3300b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fexceptions); 3310b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_exceptions); 3320b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 3330b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 3340b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 3350b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 336fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fasync_exceptions); 337fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_async_exceptions); 338fe6060f1SDimitry Andric return false; 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // See if the user explicitly enabled exceptions. 3420b57cec5SDimitry Andric bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, 3430b57cec5SDimitry Andric false); 3440b57cec5SDimitry Andric 345fe6060f1SDimitry Andric bool EHa = Args.hasFlag(options::OPT_fasync_exceptions, 346fe6060f1SDimitry Andric options::OPT_fno_async_exceptions, false); 347fe6060f1SDimitry Andric if (EHa) { 348fe6060f1SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 349fe6060f1SDimitry Andric EH = true; 350fe6060f1SDimitry Andric } 351fe6060f1SDimitry Andric 3520b57cec5SDimitry Andric // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 3530b57cec5SDimitry Andric // is not necessarily sensible, but follows GCC. 3540b57cec5SDimitry Andric if (types::isObjC(InputType) && 3550b57cec5SDimitry Andric Args.hasFlag(options::OPT_fobjc_exceptions, 3560b57cec5SDimitry Andric options::OPT_fno_objc_exceptions, true)) { 3570b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-exceptions"); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric if (types::isCXX(InputType)) { 36381ad6265SDimitry Andric // Disable C++ EH by default on XCore and PS4/PS5. 36481ad6265SDimitry Andric bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore && 36581ad6265SDimitry Andric !Triple.isPS() && !Triple.isDriverKit(); 3660b57cec5SDimitry Andric Arg *ExceptionArg = Args.getLastArg( 3670b57cec5SDimitry Andric options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 3680b57cec5SDimitry Andric options::OPT_fexceptions, options::OPT_fno_exceptions); 3690b57cec5SDimitry Andric if (ExceptionArg) 3700b57cec5SDimitry Andric CXXExceptionsEnabled = 3710b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) || 3720b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fexceptions); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric if (CXXExceptionsEnabled) { 3750b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric EH = true; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3815ffd83dbSDimitry Andric // OPT_fignore_exceptions means exception could still be thrown, 3825ffd83dbSDimitry Andric // but no clean up or catch would happen in current module. 3835ffd83dbSDimitry Andric // So we do not set EH to false. 3845ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions); 3855ffd83dbSDimitry Andric 3860b57cec5SDimitry Andric if (EH) 3870b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 388fe6060f1SDimitry Andric return EH; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 391480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, 392480093f4SDimitry Andric const JobAction &JA) { 3930b57cec5SDimitry Andric bool Default = true; 3940b57cec5SDimitry Andric if (TC.getTriple().isOSDarwin()) { 3950b57cec5SDimitry Andric // The native darwin assembler doesn't support the linker_option directives, 3960b57cec5SDimitry Andric // so we disable them if we think the .s file will be passed to it. 3970b57cec5SDimitry Andric Default = TC.useIntegratedAs(); 3980b57cec5SDimitry Andric } 399480093f4SDimitry Andric // The linker_option directives are intended for host compilation. 400480093f4SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 401480093f4SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 402480093f4SDimitry Andric Default = false; 403480093f4SDimitry Andric return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 4040b57cec5SDimitry Andric Default); 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) { 4080b57cec5SDimitry Andric switch (Triple.getArch()){ 4090b57cec5SDimitry Andric default: 4100b57cec5SDimitry Andric return false; 4110b57cec5SDimitry Andric case llvm::Triple::arm: 4120b57cec5SDimitry Andric case llvm::Triple::thumb: 4130b57cec5SDimitry Andric // ARM Darwin targets require a frame pointer to be always present to aid 4140b57cec5SDimitry Andric // offline debugging via backtraces. 4150b57cec5SDimitry Andric return Triple.isOSDarwin(); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric static bool useFramePointerForTargetByDefault(const ArgList &Args, 4200b57cec5SDimitry Andric const llvm::Triple &Triple) { 4215ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry)) 422a7dea167SDimitry Andric return true; 423a7dea167SDimitry Andric 42406c3fb27SDimitry Andric if (Triple.isAndroid()) { 42506c3fb27SDimitry Andric switch (Triple.getArch()) { 42606c3fb27SDimitry Andric case llvm::Triple::aarch64: 42706c3fb27SDimitry Andric case llvm::Triple::arm: 42806c3fb27SDimitry Andric case llvm::Triple::armeb: 42906c3fb27SDimitry Andric case llvm::Triple::thumb: 43006c3fb27SDimitry Andric case llvm::Triple::thumbeb: 43106c3fb27SDimitry Andric case llvm::Triple::riscv64: 43206c3fb27SDimitry Andric return true; 43306c3fb27SDimitry Andric default: 43406c3fb27SDimitry Andric break; 43506c3fb27SDimitry Andric } 43606c3fb27SDimitry Andric } 43706c3fb27SDimitry Andric 4380b57cec5SDimitry Andric switch (Triple.getArch()) { 4390b57cec5SDimitry Andric case llvm::Triple::xcore: 4400b57cec5SDimitry Andric case llvm::Triple::wasm32: 4410b57cec5SDimitry Andric case llvm::Triple::wasm64: 4420b57cec5SDimitry Andric case llvm::Triple::msp430: 4430b57cec5SDimitry Andric // XCore never wants frame pointers, regardless of OS. 4440b57cec5SDimitry Andric // WebAssembly never wants frame pointers. 4450b57cec5SDimitry Andric return false; 4460b57cec5SDimitry Andric case llvm::Triple::ppc: 447e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 4480b57cec5SDimitry Andric case llvm::Triple::ppc64: 4490b57cec5SDimitry Andric case llvm::Triple::ppc64le: 4500b57cec5SDimitry Andric case llvm::Triple::riscv32: 4510b57cec5SDimitry Andric case llvm::Triple::riscv64: 452bdd1243dSDimitry Andric case llvm::Triple::sparc: 453bdd1243dSDimitry Andric case llvm::Triple::sparcel: 454bdd1243dSDimitry Andric case llvm::Triple::sparcv9: 455480093f4SDimitry Andric case llvm::Triple::amdgcn: 456480093f4SDimitry Andric case llvm::Triple::r600: 45781ad6265SDimitry Andric case llvm::Triple::csky: 458bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 459bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 4600b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4610b57cec5SDimitry Andric default: 4620b57cec5SDimitry Andric break; 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 465753f127fSDimitry Andric if (Triple.isOSFuchsia() || Triple.isOSNetBSD()) { 4660b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI || 4700b57cec5SDimitry Andric Triple.isOSHurd()) { 4710b57cec5SDimitry Andric switch (Triple.getArch()) { 4720b57cec5SDimitry Andric // Don't use a frame pointer on linux if optimizing for certain targets. 4735ffd83dbSDimitry Andric case llvm::Triple::arm: 4745ffd83dbSDimitry Andric case llvm::Triple::armeb: 4755ffd83dbSDimitry Andric case llvm::Triple::thumb: 4765ffd83dbSDimitry Andric case llvm::Triple::thumbeb: 4770b57cec5SDimitry Andric case llvm::Triple::mips64: 4780b57cec5SDimitry Andric case llvm::Triple::mips64el: 4790b57cec5SDimitry Andric case llvm::Triple::mips: 4800b57cec5SDimitry Andric case llvm::Triple::mipsel: 4810b57cec5SDimitry Andric case llvm::Triple::systemz: 4820b57cec5SDimitry Andric case llvm::Triple::x86: 4830b57cec5SDimitry Andric case llvm::Triple::x86_64: 4840b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4850b57cec5SDimitry Andric default: 4860b57cec5SDimitry Andric return true; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric if (Triple.isOSWindows()) { 4910b57cec5SDimitry Andric switch (Triple.getArch()) { 4920b57cec5SDimitry Andric case llvm::Triple::x86: 4930b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4940b57cec5SDimitry Andric case llvm::Triple::x86_64: 4950b57cec5SDimitry Andric return Triple.isOSBinFormatMachO(); 4960b57cec5SDimitry Andric case llvm::Triple::arm: 4970b57cec5SDimitry Andric case llvm::Triple::thumb: 4980b57cec5SDimitry Andric // Windows on ARM builds with FPO disabled to aid fast stack walking 4990b57cec5SDimitry Andric return true; 5000b57cec5SDimitry Andric default: 5010b57cec5SDimitry Andric // All other supported Windows ISAs use xdata unwind information, so frame 5020b57cec5SDimitry Andric // pointers are not generally useful. 5030b57cec5SDimitry Andric return false; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric return true; 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric 510a7dea167SDimitry Andric static CodeGenOptions::FramePointerKind 511a7dea167SDimitry Andric getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) { 512a7dea167SDimitry Andric // We have 4 states: 513a7dea167SDimitry Andric // 514a7dea167SDimitry Andric // 00) leaf retained, non-leaf retained 515a7dea167SDimitry Andric // 01) leaf retained, non-leaf omitted (this is invalid) 516a7dea167SDimitry Andric // 10) leaf omitted, non-leaf retained 517a7dea167SDimitry Andric // (what -momit-leaf-frame-pointer was designed for) 518a7dea167SDimitry Andric // 11) leaf omitted, non-leaf omitted 519a7dea167SDimitry Andric // 520a7dea167SDimitry Andric // "omit" options taking precedence over "no-omit" options is the only way 521a7dea167SDimitry Andric // to make 3 valid states representable 522a7dea167SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer, 523a7dea167SDimitry Andric options::OPT_fno_omit_frame_pointer); 524a7dea167SDimitry Andric bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer); 525a7dea167SDimitry Andric bool NoOmitFP = 526a7dea167SDimitry Andric A && A->getOption().matches(options::OPT_fno_omit_frame_pointer); 52781ad6265SDimitry Andric bool OmitLeafFP = 52881ad6265SDimitry Andric Args.hasFlag(options::OPT_momit_leaf_frame_pointer, 529480093f4SDimitry Andric options::OPT_mno_omit_leaf_frame_pointer, 53006c3fb27SDimitry Andric Triple.isAArch64() || Triple.isPS() || Triple.isVE() || 53106c3fb27SDimitry Andric (Triple.isAndroid() && Triple.isRISCV64())); 532a7dea167SDimitry Andric if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) || 533a7dea167SDimitry Andric (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) { 534e8d8bef9SDimitry Andric if (OmitLeafFP) 535a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::NonLeaf; 536a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::All; 5370b57cec5SDimitry Andric } 538a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::None; 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric /// Add a CC1 option to specify the debug compilation directory. 5420eae32dcSDimitry Andric static const char *addDebugCompDirArg(const ArgList &Args, 5430eae32dcSDimitry Andric ArgStringList &CmdArgs, 544a7dea167SDimitry Andric const llvm::vfs::FileSystem &VFS) { 545fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 546fe6060f1SDimitry Andric options::OPT_fdebug_compilation_dir_EQ)) { 547fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 548fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") + 549fe6060f1SDimitry Andric A->getValue())); 550fe6060f1SDimitry Andric else 551fe6060f1SDimitry Andric A->render(Args, CmdArgs); 552a7dea167SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 553a7dea167SDimitry Andric VFS.getCurrentWorkingDirectory()) { 554fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD)); 5550b57cec5SDimitry Andric } 5560eae32dcSDimitry Andric StringRef Path(CmdArgs.back()); 5570eae32dcSDimitry Andric return Path.substr(Path.find('=') + 1).data(); 5580eae32dcSDimitry Andric } 5590eae32dcSDimitry Andric 5600eae32dcSDimitry Andric static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs, 5610eae32dcSDimitry Andric const char *DebugCompilationDir, 5620eae32dcSDimitry Andric const char *OutputFileName) { 5630eae32dcSDimitry Andric // No need to generate a value for -object-file-name if it was provided. 5640eae32dcSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 5650eae32dcSDimitry Andric if (StringRef(Arg->getValue()).startswith("-object-file-name")) 5660eae32dcSDimitry Andric return; 5670eae32dcSDimitry Andric 5680eae32dcSDimitry Andric if (Args.hasArg(options::OPT_object_file_name_EQ)) 5690eae32dcSDimitry Andric return; 5700eae32dcSDimitry Andric 5710eae32dcSDimitry Andric SmallString<128> ObjFileNameForDebug(OutputFileName); 5720eae32dcSDimitry Andric if (ObjFileNameForDebug != "-" && 5730eae32dcSDimitry Andric !llvm::sys::path::is_absolute(ObjFileNameForDebug) && 5740eae32dcSDimitry Andric (!DebugCompilationDir || 5750eae32dcSDimitry Andric llvm::sys::path::is_absolute(DebugCompilationDir))) { 5760eae32dcSDimitry Andric // Make the path absolute in the debug infos like MSVC does. 5770eae32dcSDimitry Andric llvm::sys::fs::make_absolute(ObjFileNameForDebug); 5780eae32dcSDimitry Andric } 57906c3fb27SDimitry Andric // If the object file name is a relative path, then always use Windows 58006c3fb27SDimitry Andric // backslash style as -object-file-name is used for embedding object file path 58106c3fb27SDimitry Andric // in codeview and it can only be generated when targeting on Windows. 58206c3fb27SDimitry Andric // Otherwise, just use native absolute path. 58306c3fb27SDimitry Andric llvm::sys::path::Style Style = 58406c3fb27SDimitry Andric llvm::sys::path::is_absolute(ObjFileNameForDebug) 58506c3fb27SDimitry Andric ? llvm::sys::path::Style::native 58606c3fb27SDimitry Andric : llvm::sys::path::Style::windows_backslash; 58706c3fb27SDimitry Andric llvm::sys::path::remove_dots(ObjFileNameForDebug, /*remove_dot_dot=*/true, 58806c3fb27SDimitry Andric Style); 5890eae32dcSDimitry Andric CmdArgs.push_back( 5900eae32dcSDimitry Andric Args.MakeArgString(Twine("-object-file-name=") + ObjFileNameForDebug)); 5910b57cec5SDimitry Andric } 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map. 59481ad6265SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC, 59581ad6265SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 59681ad6265SDimitry Andric auto AddOneArg = [&](StringRef Map, StringRef Name) { 597349cc55cSDimitry Andric if (!Map.contains('=')) 59881ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) << Map << Name; 5990b57cec5SDimitry Andric else 6000b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); 60181ad6265SDimitry Andric }; 60281ad6265SDimitry Andric 60381ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 60481ad6265SDimitry Andric options::OPT_fdebug_prefix_map_EQ)) { 60581ad6265SDimitry Andric AddOneArg(A->getValue(), A->getOption().getName()); 6060b57cec5SDimitry Andric A->claim(); 6070b57cec5SDimitry Andric } 60881ad6265SDimitry Andric std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping(); 60981ad6265SDimitry Andric if (GlobalRemapEntry.empty()) 61081ad6265SDimitry Andric return; 61181ad6265SDimitry Andric AddOneArg(GlobalRemapEntry, "environment"); 6120b57cec5SDimitry Andric } 6130b57cec5SDimitry Andric 614480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map. 615480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, 616480093f4SDimitry Andric ArgStringList &CmdArgs) { 617480093f4SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 618480093f4SDimitry Andric options::OPT_fmacro_prefix_map_EQ)) { 619480093f4SDimitry Andric StringRef Map = A->getValue(); 620349cc55cSDimitry Andric if (!Map.contains('=')) 621480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 622480093f4SDimitry Andric << Map << A->getOption().getName(); 623480093f4SDimitry Andric else 624480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map)); 625480093f4SDimitry Andric A->claim(); 626480093f4SDimitry Andric } 627480093f4SDimitry Andric } 628480093f4SDimitry Andric 629e8d8bef9SDimitry Andric /// Add a CC1 and CC1AS option to specify the coverage file path prefix map. 630fe6060f1SDimitry Andric static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args, 631e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 632e8d8bef9SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 633fe6060f1SDimitry Andric options::OPT_fcoverage_prefix_map_EQ)) { 634e8d8bef9SDimitry Andric StringRef Map = A->getValue(); 635349cc55cSDimitry Andric if (!Map.contains('=')) 636e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 637e8d8bef9SDimitry Andric << Map << A->getOption().getName(); 638e8d8bef9SDimitry Andric else 639fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map)); 640e8d8bef9SDimitry Andric A->claim(); 641e8d8bef9SDimitry Andric } 642e8d8bef9SDimitry Andric } 643e8d8bef9SDimitry Andric 6440b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz. 6450b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is 6460b57cec5SDimitry Andric /// enabled. 6470b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) { 6480b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 6490b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 6500b57cec5SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 6510b57cec5SDimitry Andric return true; 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O0)) 6540b57cec5SDimitry Andric return false; 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric // Vectorize -Os. 6590b57cec5SDimitry Andric StringRef S(A->getValue()); 6600b57cec5SDimitry Andric if (S == "s") 6610b57cec5SDimitry Andric return true; 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric // Don't vectorize -Oz, unless it's the slp vectorizer. 6640b57cec5SDimitry Andric if (S == "z") 6650b57cec5SDimitry Andric return isSlpVec; 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric unsigned OptLevel = 0; 6680b57cec5SDimitry Andric if (S.getAsInteger(10, OptLevel)) 6690b57cec5SDimitry Andric return false; 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric return OptLevel > 1; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric return false; 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input. 6780b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 6790b57cec5SDimitry Andric ArgStringList &CmdArgs) { 6800b57cec5SDimitry Andric // When using -verify-pch, we don't want to provide the type 6810b57cec5SDimitry Andric // 'precompiled-header' if it was inferred from the file extension 6820b57cec5SDimitry Andric if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH) 6830b57cec5SDimitry Andric return; 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric CmdArgs.push_back("-x"); 6860b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc)) 6870b57cec5SDimitry Andric CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 6880b57cec5SDimitry Andric else { 6890b57cec5SDimitry Andric // Map the driver type to the frontend type. This is mostly an identity 6900b57cec5SDimitry Andric // mapping, except that the distinction between module interface units 6910b57cec5SDimitry Andric // and other source files does not exist at the frontend layer. 6920b57cec5SDimitry Andric const char *ClangType; 6930b57cec5SDimitry Andric switch (Input.getType()) { 6940b57cec5SDimitry Andric case types::TY_CXXModule: 6950b57cec5SDimitry Andric ClangType = "c++"; 6960b57cec5SDimitry Andric break; 6970b57cec5SDimitry Andric case types::TY_PP_CXXModule: 6980b57cec5SDimitry Andric ClangType = "c++-cpp-output"; 6990b57cec5SDimitry Andric break; 7000b57cec5SDimitry Andric default: 7010b57cec5SDimitry Andric ClangType = types::getTypeName(Input.getType()); 7020b57cec5SDimitry Andric break; 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric CmdArgs.push_back(ClangType); 7050b57cec5SDimitry Andric } 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, 70906c3fb27SDimitry Andric const JobAction &JA, const InputInfo &Output, 710349cc55cSDimitry Andric const ArgList &Args, SanitizerArgs &SanArgs, 7110b57cec5SDimitry Andric ArgStringList &CmdArgs) { 71206c3fb27SDimitry Andric const Driver &D = TC.getDriver(); 7130b57cec5SDimitry Andric auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, 7140b57cec5SDimitry Andric options::OPT_fprofile_generate_EQ, 7150b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7160b57cec5SDimitry Andric if (PGOGenerateArg && 7170b57cec5SDimitry Andric PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7180b57cec5SDimitry Andric PGOGenerateArg = nullptr; 7190b57cec5SDimitry Andric 72006c3fb27SDimitry Andric auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args); 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric auto *ProfileGenerateArg = Args.getLastArg( 7230b57cec5SDimitry Andric options::OPT_fprofile_instr_generate, 7240b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ, 7250b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate); 7260b57cec5SDimitry Andric if (ProfileGenerateArg && 7270b57cec5SDimitry Andric ProfileGenerateArg->getOption().matches( 7280b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate)) 7290b57cec5SDimitry Andric ProfileGenerateArg = nullptr; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric if (PGOGenerateArg && ProfileGenerateArg) 7320b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7330b57cec5SDimitry Andric << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling(); 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric auto *ProfileUseArg = getLastProfileUseArg(Args); 7360b57cec5SDimitry Andric 7370b57cec5SDimitry Andric if (PGOGenerateArg && ProfileUseArg) 7380b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7390b57cec5SDimitry Andric << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling(); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric if (ProfileGenerateArg && ProfileUseArg) 7420b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7430b57cec5SDimitry Andric << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling(); 7440b57cec5SDimitry Andric 745e8d8bef9SDimitry Andric if (CSPGOGenerateArg && PGOGenerateArg) { 7460b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7470b57cec5SDimitry Andric << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling(); 748e8d8bef9SDimitry Andric PGOGenerateArg = nullptr; 749e8d8bef9SDimitry Andric } 7500b57cec5SDimitry Andric 751fe6060f1SDimitry Andric if (TC.getTriple().isOSAIX()) { 752fe6060f1SDimitry Andric if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args)) 753fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 754fe6060f1SDimitry Andric << ProfileSampleUseArg->getSpelling() << TC.getTriple().str(); 755fe6060f1SDimitry Andric } 756fe6060f1SDimitry Andric 7570b57cec5SDimitry Andric if (ProfileGenerateArg) { 7580b57cec5SDimitry Andric if (ProfileGenerateArg->getOption().matches( 7590b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ)) 7600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") + 7610b57cec5SDimitry Andric ProfileGenerateArg->getValue())); 7620b57cec5SDimitry Andric // The default is to use Clang Instrumentation. 7630b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=clang"); 7640b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 7650b57cec5SDimitry Andric // Add dependent lib for clang_rt.profile 7665ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 7675ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 7680b57cec5SDimitry Andric } 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric Arg *PGOGenArg = nullptr; 7720b57cec5SDimitry Andric if (PGOGenerateArg) { 7730b57cec5SDimitry Andric assert(!CSPGOGenerateArg); 7740b57cec5SDimitry Andric PGOGenArg = PGOGenerateArg; 7750b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=llvm"); 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric if (CSPGOGenerateArg) { 7780b57cec5SDimitry Andric assert(!PGOGenerateArg); 7790b57cec5SDimitry Andric PGOGenArg = CSPGOGenerateArg; 7800b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=csllvm"); 7810b57cec5SDimitry Andric } 7820b57cec5SDimitry Andric if (PGOGenArg) { 7830b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 7845ffd83dbSDimitry Andric // Add dependent lib for clang_rt.profile 7855ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 7865ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric if (PGOGenArg->getOption().matches( 7890b57cec5SDimitry Andric PGOGenerateArg ? options::OPT_fprofile_generate_EQ 7900b57cec5SDimitry Andric : options::OPT_fcs_profile_generate_EQ)) { 7910b57cec5SDimitry Andric SmallString<128> Path(PGOGenArg->getValue()); 7920b57cec5SDimitry Andric llvm::sys::path::append(Path, "default_%m.profraw"); 7930b57cec5SDimitry Andric CmdArgs.push_back( 7940b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path)); 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric if (ProfileUseArg) { 7990b57cec5SDimitry Andric if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ)) 8000b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8010b57cec5SDimitry Andric Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue())); 8020b57cec5SDimitry Andric else if ((ProfileUseArg->getOption().matches( 8030b57cec5SDimitry Andric options::OPT_fprofile_use_EQ) || 8040b57cec5SDimitry Andric ProfileUseArg->getOption().matches( 8050b57cec5SDimitry Andric options::OPT_fprofile_instr_use))) { 8060b57cec5SDimitry Andric SmallString<128> Path( 8070b57cec5SDimitry Andric ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); 8080b57cec5SDimitry Andric if (Path.empty() || llvm::sys::fs::is_directory(Path)) 8090b57cec5SDimitry Andric llvm::sys::path::append(Path, "default.profdata"); 8100b57cec5SDimitry Andric CmdArgs.push_back( 8110b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path)); 8120b57cec5SDimitry Andric } 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric 8155ffd83dbSDimitry Andric bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage, 8165ffd83dbSDimitry Andric options::OPT_fno_test_coverage, false) || 817a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage); 8185ffd83dbSDimitry Andric bool EmitCovData = TC.needsGCovInstrumentation(Args); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoverage_mapping, 8210b57cec5SDimitry Andric options::OPT_fno_coverage_mapping, false)) { 8220b57cec5SDimitry Andric if (!ProfileGenerateArg) 8230b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 8240b57cec5SDimitry Andric << "-fcoverage-mapping" 8250b57cec5SDimitry Andric << "-fprofile-instr-generate"; 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric CmdArgs.push_back("-fcoverage-mapping"); 8280b57cec5SDimitry Andric } 8290b57cec5SDimitry Andric 830fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 831fe6060f1SDimitry Andric options::OPT_fcoverage_compilation_dir_EQ)) { 832fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 833fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 834fe6060f1SDimitry Andric Twine("-fcoverage-compilation-dir=") + A->getValue())); 835fe6060f1SDimitry Andric else 836fe6060f1SDimitry Andric A->render(Args, CmdArgs); 837fe6060f1SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 838fe6060f1SDimitry Andric D.getVFS().getCurrentWorkingDirectory()) { 839fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD)); 840fe6060f1SDimitry Andric } 841fe6060f1SDimitry Andric 8420b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) { 8430b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ); 8440b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 8450b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 8460b57cec5SDimitry Andric << "-fprofile-exclude-files=" 8470b57cec5SDimitry Andric << "--coverage"; 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric StringRef v = Arg->getValue(); 8500b57cec5SDimitry Andric CmdArgs.push_back( 8510b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-exclude-files=" + v))); 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) { 8550b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ); 8560b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 8570b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 8580b57cec5SDimitry Andric << "-fprofile-filter-files=" 8590b57cec5SDimitry Andric << "--coverage"; 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric StringRef v = Arg->getValue(); 8620b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v))); 8630b57cec5SDimitry Andric } 8640b57cec5SDimitry Andric 865e8d8bef9SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) { 866e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 867e8d8bef9SDimitry Andric if (Val == "atomic" || Val == "prefer-atomic") 868e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 869e8d8bef9SDimitry Andric else if (Val != "single") 870e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 871bdd1243dSDimitry Andric << A->getSpelling() << Val; 872349cc55cSDimitry Andric } else if (SanArgs.needsTsanRt()) { 873e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 874e8d8bef9SDimitry Andric } 875e8d8bef9SDimitry Andric 876fcaf7f86SDimitry Andric int FunctionGroups = 1; 877fcaf7f86SDimitry Andric int SelectedFunctionGroup = 0; 878fcaf7f86SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_function_groups)) { 879fcaf7f86SDimitry Andric StringRef Val = A->getValue(); 880fcaf7f86SDimitry Andric if (Val.getAsInteger(0, FunctionGroups) || FunctionGroups < 1) 881fcaf7f86SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; 882fcaf7f86SDimitry Andric } 883fcaf7f86SDimitry Andric if (const auto *A = 884fcaf7f86SDimitry Andric Args.getLastArg(options::OPT_fprofile_selected_function_group)) { 885fcaf7f86SDimitry Andric StringRef Val = A->getValue(); 886fcaf7f86SDimitry Andric if (Val.getAsInteger(0, SelectedFunctionGroup) || 887fcaf7f86SDimitry Andric SelectedFunctionGroup < 0 || SelectedFunctionGroup >= FunctionGroups) 888fcaf7f86SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; 889fcaf7f86SDimitry Andric } 890fcaf7f86SDimitry Andric if (FunctionGroups != 1) 891fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-function-groups=" + 892fcaf7f86SDimitry Andric Twine(FunctionGroups))); 893fcaf7f86SDimitry Andric if (SelectedFunctionGroup != 0) 894fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-selected-function-group=" + 895fcaf7f86SDimitry Andric Twine(SelectedFunctionGroup))); 896fcaf7f86SDimitry Andric 897a7dea167SDimitry Andric // Leave -fprofile-dir= an unused argument unless .gcda emission is 898a7dea167SDimitry Andric // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider 899a7dea167SDimitry Andric // the flag used. There is no -fno-profile-dir, so the user has no 900a7dea167SDimitry Andric // targeted way to suppress the warning. 901a7dea167SDimitry Andric Arg *FProfileDir = nullptr; 902a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fprofile_arcs) || 903a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage)) 904a7dea167SDimitry Andric FProfileDir = Args.getLastArg(options::OPT_fprofile_dir); 905a7dea167SDimitry Andric 90606c3fb27SDimitry Andric // TODO: Don't claim -c/-S to warn about -fsyntax-only -c/-S, -E -c/-S, 90706c3fb27SDimitry Andric // like we warn about -fsyntax-only -E. 90806c3fb27SDimitry Andric (void)(Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)); 90906c3fb27SDimitry Andric 91006c3fb27SDimitry Andric // Put the .gcno and .gcda files (if needed) next to the primary output file, 91106c3fb27SDimitry Andric // or fall back to a file in the current directory for `clang -c --coverage 91206c3fb27SDimitry Andric // d/a.c` in the absence of -o. 91306c3fb27SDimitry Andric if (EmitCovNotes || EmitCovData) { 91406c3fb27SDimitry Andric SmallString<128> CoverageFilename; 91506c3fb27SDimitry Andric if (Arg *DumpDir = Args.getLastArgNoClaim(options::OPT_dumpdir)) { 91606c3fb27SDimitry Andric // Form ${dumpdir}${basename}.gcno. Note that dumpdir may not end with a 91706c3fb27SDimitry Andric // path separator. 91806c3fb27SDimitry Andric CoverageFilename = DumpDir->getValue(); 91906c3fb27SDimitry Andric CoverageFilename += llvm::sys::path::filename(Output.getBaseInput()); 92006c3fb27SDimitry Andric } else if (Arg *FinalOutput = 92106c3fb27SDimitry Andric C.getArgs().getLastArg(options::OPT__SLASH_Fo)) { 92206c3fb27SDimitry Andric CoverageFilename = FinalOutput->getValue(); 92306c3fb27SDimitry Andric } else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) { 92406c3fb27SDimitry Andric CoverageFilename = FinalOutput->getValue(); 92506c3fb27SDimitry Andric } else { 92606c3fb27SDimitry Andric CoverageFilename = llvm::sys::path::filename(Output.getBaseInput()); 92706c3fb27SDimitry Andric } 928a7dea167SDimitry Andric if (llvm::sys::path::is_relative(CoverageFilename)) 929a7dea167SDimitry Andric (void)D.getVFS().makeAbsolute(CoverageFilename); 9300b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcno"); 93106c3fb27SDimitry Andric if (EmitCovNotes) { 932a7dea167SDimitry Andric CmdArgs.push_back("-coverage-notes-file"); 9330b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 93406c3fb27SDimitry Andric } 9350b57cec5SDimitry Andric 936a7dea167SDimitry Andric if (EmitCovData) { 937a7dea167SDimitry Andric if (FProfileDir) { 93806c3fb27SDimitry Andric SmallString<128> Gcno = std::move(CoverageFilename); 9390b57cec5SDimitry Andric CoverageFilename = FProfileDir->getValue(); 94006c3fb27SDimitry Andric llvm::sys::path::append(CoverageFilename, Gcno); 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcda"); 943a7dea167SDimitry Andric CmdArgs.push_back("-coverage-data-file"); 9440b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9450b57cec5SDimitry Andric } 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions. 9500b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) { 9510b57cec5SDimitry Andric if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A)) 9520b57cec5SDimitry Andric return true; 9530b57cec5SDimitry Andric 9540eae32dcSDimitry Andric return llvm::any_of(A->inputs(), ContainsCompileAction); 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler. 9580b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0. 9590b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 9600b57cec5SDimitry Andric bool RelaxDefault = true; 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 9630b57cec5SDimitry Andric RelaxDefault = A->getOption().matches(options::OPT_O0); 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric if (RelaxDefault) { 9660b57cec5SDimitry Andric RelaxDefault = false; 9670b57cec5SDimitry Andric for (const auto &Act : C.getActions()) { 9680b57cec5SDimitry Andric if (ContainsCompileAction(Act)) { 9690b57cec5SDimitry Andric RelaxDefault = true; 9700b57cec5SDimitry Andric break; 9710b57cec5SDimitry Andric } 9720b57cec5SDimitry Andric } 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 9760b57cec5SDimitry Andric RelaxDefault); 9770b57cec5SDimitry Andric } 9780b57cec5SDimitry Andric 97906c3fb27SDimitry Andric static void 98006c3fb27SDimitry Andric RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, 98106c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind DebugInfoKind, 9820b57cec5SDimitry Andric unsigned DwarfVersion, 9830b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning) { 98406c3fb27SDimitry Andric addDebugInfoKind(CmdArgs, DebugInfoKind); 9850b57cec5SDimitry Andric if (DwarfVersion > 0) 9860b57cec5SDimitry Andric CmdArgs.push_back( 9870b57cec5SDimitry Andric Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); 9880b57cec5SDimitry Andric switch (DebuggerTuning) { 9890b57cec5SDimitry Andric case llvm::DebuggerKind::GDB: 9900b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=gdb"); 9910b57cec5SDimitry Andric break; 9920b57cec5SDimitry Andric case llvm::DebuggerKind::LLDB: 9930b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=lldb"); 9940b57cec5SDimitry Andric break; 9950b57cec5SDimitry Andric case llvm::DebuggerKind::SCE: 9960b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=sce"); 9970b57cec5SDimitry Andric break; 998fe6060f1SDimitry Andric case llvm::DebuggerKind::DBX: 999fe6060f1SDimitry Andric CmdArgs.push_back("-debugger-tuning=dbx"); 1000fe6060f1SDimitry Andric break; 10010b57cec5SDimitry Andric default: 10020b57cec5SDimitry Andric break; 10030b57cec5SDimitry Andric } 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args, 10070b57cec5SDimitry Andric const Driver &D, const ToolChain &TC) { 10080b57cec5SDimitry Andric assert(A && "Expected non-nullptr argument."); 10090b57cec5SDimitry Andric if (TC.supportsDebugInfoOption(A)) 10100b57cec5SDimitry Andric return true; 10110b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target) 10120b57cec5SDimitry Andric << A->getAsString(Args) << TC.getTripleString(); 10130b57cec5SDimitry Andric return false; 10140b57cec5SDimitry Andric } 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args, 10170b57cec5SDimitry Andric ArgStringList &CmdArgs, 10180b57cec5SDimitry Andric const Driver &D, 10190b57cec5SDimitry Andric const ToolChain &TC) { 1020e8d8bef9SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gz_EQ); 10210b57cec5SDimitry Andric if (!A) 10220b57cec5SDimitry Andric return; 10230b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 10240b57cec5SDimitry Andric StringRef Value = A->getValue(); 10250b57cec5SDimitry Andric if (Value == "none") { 10260b57cec5SDimitry Andric CmdArgs.push_back("--compress-debug-sections=none"); 102704eeddc0SDimitry Andric } else if (Value == "zlib") { 1028753f127fSDimitry Andric if (llvm::compression::zlib::isAvailable()) { 10290b57cec5SDimitry Andric CmdArgs.push_back( 10300b57cec5SDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 10310b57cec5SDimitry Andric } else { 1032bdd1243dSDimitry Andric D.Diag(diag::warn_debug_compression_unavailable) << "zlib"; 1033bdd1243dSDimitry Andric } 1034bdd1243dSDimitry Andric } else if (Value == "zstd") { 1035bdd1243dSDimitry Andric if (llvm::compression::zstd::isAvailable()) { 1036bdd1243dSDimitry Andric CmdArgs.push_back( 1037bdd1243dSDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 1038bdd1243dSDimitry Andric } else { 1039bdd1243dSDimitry Andric D.Diag(diag::warn_debug_compression_unavailable) << "zstd"; 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric } else { 10420b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1043bdd1243dSDimitry Andric << A->getSpelling() << Value; 10440b57cec5SDimitry Andric } 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric } 10470b57cec5SDimitry Andric 1048e8d8bef9SDimitry Andric static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, 1049e8d8bef9SDimitry Andric const ArgList &Args, 105081ad6265SDimitry Andric ArgStringList &CmdArgs, 105181ad6265SDimitry Andric bool IsCC1As = false) { 1052fe6060f1SDimitry Andric // If no version was requested by the user, use the default value from the 1053fe6060f1SDimitry Andric // back end. This is consistent with the value returned from 1054fe6060f1SDimitry Andric // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without 1055fe6060f1SDimitry Andric // requiring the corresponding llvm to have the AMDGPU target enabled, 1056fe6060f1SDimitry Andric // provided the user (e.g. front end tests) can use the default. 1057fe6060f1SDimitry Andric if (haveAMDGPUCodeObjectVersionArgument(D, Args)) { 1058fe6060f1SDimitry Andric unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args); 1059e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 1060e8d8bef9SDimitry Andric Args.MakeArgString(Twine("--amdhsa-code-object-version=") + 1061e8d8bef9SDimitry Andric Twine(CodeObjVer))); 1062e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); 106381ad6265SDimitry Andric // -cc1as does not accept -mcode-object-version option. 106481ad6265SDimitry Andric if (!IsCC1As) 106581ad6265SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 106681ad6265SDimitry Andric Args.MakeArgString(Twine("-mcode-object-version=") + 106781ad6265SDimitry Andric Twine(CodeObjVer))); 1068e8d8bef9SDimitry Andric } 1069fe6060f1SDimitry Andric } 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, 10720b57cec5SDimitry Andric const Driver &D, const ArgList &Args, 10730b57cec5SDimitry Andric ArgStringList &CmdArgs, 10740b57cec5SDimitry Andric const InputInfo &Output, 10750b57cec5SDimitry Andric const InputInfoList &Inputs) const { 10760b57cec5SDimitry Andric const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU(); 10770b57cec5SDimitry Andric 10780b57cec5SDimitry Andric CheckPreprocessingOptions(D, Args); 10790b57cec5SDimitry Andric 10800b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_C); 10810b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_CC); 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric // Handle dependency file generation. 1084a7dea167SDimitry Andric Arg *ArgM = Args.getLastArg(options::OPT_MM); 1085a7dea167SDimitry Andric if (!ArgM) 1086a7dea167SDimitry Andric ArgM = Args.getLastArg(options::OPT_M); 1087a7dea167SDimitry Andric Arg *ArgMD = Args.getLastArg(options::OPT_MMD); 1088a7dea167SDimitry Andric if (!ArgMD) 1089a7dea167SDimitry Andric ArgMD = Args.getLastArg(options::OPT_MD); 1090a7dea167SDimitry Andric 1091a7dea167SDimitry Andric // -M and -MM imply -w. 1092a7dea167SDimitry Andric if (ArgM) 1093a7dea167SDimitry Andric CmdArgs.push_back("-w"); 1094a7dea167SDimitry Andric else 1095a7dea167SDimitry Andric ArgM = ArgMD; 1096a7dea167SDimitry Andric 1097a7dea167SDimitry Andric if (ArgM) { 10980b57cec5SDimitry Andric // Determine the output location. 10990b57cec5SDimitry Andric const char *DepFile; 11000b57cec5SDimitry Andric if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 11010b57cec5SDimitry Andric DepFile = MF->getValue(); 11020b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 11030b57cec5SDimitry Andric } else if (Output.getType() == types::TY_Dependencies) { 11040b57cec5SDimitry Andric DepFile = Output.getFilename(); 1105a7dea167SDimitry Andric } else if (!ArgMD) { 11060b57cec5SDimitry Andric DepFile = "-"; 11070b57cec5SDimitry Andric } else { 11080b57cec5SDimitry Andric DepFile = getDependencyFileName(Args, Inputs); 11090b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 11100b57cec5SDimitry Andric } 11110b57cec5SDimitry Andric CmdArgs.push_back("-dependency-file"); 11120b57cec5SDimitry Andric CmdArgs.push_back(DepFile); 11130b57cec5SDimitry Andric 1114a7dea167SDimitry Andric bool HasTarget = false; 1115a7dea167SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) { 1116a7dea167SDimitry Andric HasTarget = true; 1117a7dea167SDimitry Andric A->claim(); 1118a7dea167SDimitry Andric if (A->getOption().matches(options::OPT_MT)) { 1119a7dea167SDimitry Andric A->render(Args, CmdArgs); 1120a7dea167SDimitry Andric } else { 1121a7dea167SDimitry Andric CmdArgs.push_back("-MT"); 1122a7dea167SDimitry Andric SmallString<128> Quoted; 1123753f127fSDimitry Andric quoteMakeTarget(A->getValue(), Quoted); 1124a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 1125a7dea167SDimitry Andric } 1126a7dea167SDimitry Andric } 1127a7dea167SDimitry Andric 11280b57cec5SDimitry Andric // Add a default target if one wasn't specified. 1129a7dea167SDimitry Andric if (!HasTarget) { 11300b57cec5SDimitry Andric const char *DepTarget; 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric // If user provided -o, that is the dependency target, except 11330b57cec5SDimitry Andric // when we are only generating a dependency file. 11340b57cec5SDimitry Andric Arg *OutputOpt = Args.getLastArg(options::OPT_o); 11350b57cec5SDimitry Andric if (OutputOpt && Output.getType() != types::TY_Dependencies) { 11360b57cec5SDimitry Andric DepTarget = OutputOpt->getValue(); 11370b57cec5SDimitry Andric } else { 11380b57cec5SDimitry Andric // Otherwise derive from the base input. 11390b57cec5SDimitry Andric // 11400b57cec5SDimitry Andric // FIXME: This should use the computed output file location. 11410b57cec5SDimitry Andric SmallString<128> P(Inputs[0].getBaseInput()); 11420b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "o"); 11430b57cec5SDimitry Andric DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric 11460b57cec5SDimitry Andric CmdArgs.push_back("-MT"); 11470b57cec5SDimitry Andric SmallString<128> Quoted; 1148753f127fSDimitry Andric quoteMakeTarget(DepTarget, Quoted); 11490b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 11500b57cec5SDimitry Andric } 11510b57cec5SDimitry Andric 1152a7dea167SDimitry Andric if (ArgM->getOption().matches(options::OPT_M) || 1153a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MD)) 11540b57cec5SDimitry Andric CmdArgs.push_back("-sys-header-deps"); 115506c3fb27SDimitry Andric if (Args.hasFlag(options::OPT_canonical_prefixes, 115606c3fb27SDimitry Andric options::OPT_no_canonical_prefixes, true)) 115706c3fb27SDimitry Andric CmdArgs.push_back("-canonical-system-headers"); 11580b57cec5SDimitry Andric if ((isa<PrecompileJobAction>(JA) && 11590b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_module_file_deps)) || 11600b57cec5SDimitry Andric Args.hasArg(options::OPT_fmodule_file_deps)) 11610b57cec5SDimitry Andric CmdArgs.push_back("-module-file-deps"); 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric if (Args.hasArg(options::OPT_MG)) { 1165a7dea167SDimitry Andric if (!ArgM || ArgM->getOption().matches(options::OPT_MD) || 1166a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MMD)) 11670b57cec5SDimitry Andric D.Diag(diag::err_drv_mg_requires_m_or_mm); 11680b57cec5SDimitry Andric CmdArgs.push_back("-MG"); 11690b57cec5SDimitry Andric } 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MP); 11720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MV); 11730b57cec5SDimitry Andric 11745ffd83dbSDimitry Andric // Add offload include arguments specific for CUDA/HIP. This must happen 11755ffd83dbSDimitry Andric // before we -I or -include anything else, because we must pick up the 11765ffd83dbSDimitry Andric // CUDA/HIP headers from the particular CUDA/ROCm installation, rather than 11775ffd83dbSDimitry Andric // from e.g. /usr/local/include. 11780b57cec5SDimitry Andric if (JA.isOffloading(Action::OFK_Cuda)) 11790b57cec5SDimitry Andric getToolChain().AddCudaIncludeArgs(Args, CmdArgs); 11805ffd83dbSDimitry Andric if (JA.isOffloading(Action::OFK_HIP)) 11815ffd83dbSDimitry Andric getToolChain().AddHIPIncludeArgs(Args, CmdArgs); 11820b57cec5SDimitry Andric 118306c3fb27SDimitry Andric // If we are compiling for a GPU target we want to override the system headers 118406c3fb27SDimitry Andric // with ones created by the 'libc' project if present. 118506c3fb27SDimitry Andric if (!Args.hasArg(options::OPT_nostdinc) && 118606c3fb27SDimitry Andric !Args.hasArg(options::OPT_nogpuinc) && 118706c3fb27SDimitry Andric !Args.hasArg(options::OPT_nobuiltininc) && 118806c3fb27SDimitry Andric (getToolChain().getTriple().isNVPTX() || 118906c3fb27SDimitry Andric getToolChain().getTriple().isAMDGCN())) { 119006c3fb27SDimitry Andric 119106c3fb27SDimitry Andric // Without an offloading language we will include these headers directly. 119206c3fb27SDimitry Andric // Offloading languages will instead only use the declarations stored in 119306c3fb27SDimitry Andric // the resource directory at clang/lib/Headers/llvm_libc_wrappers. 119406c3fb27SDimitry Andric if (C.getActiveOffloadKinds() == Action::OFK_None) { 119506c3fb27SDimitry Andric SmallString<128> P(llvm::sys::path::parent_path(D.InstalledDir)); 119606c3fb27SDimitry Andric llvm::sys::path::append(P, "include"); 119706c3fb27SDimitry Andric llvm::sys::path::append(P, "gpu-none-llvm"); 119806c3fb27SDimitry Andric CmdArgs.push_back("-c-isystem"); 119906c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 120006c3fb27SDimitry Andric } else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) { 120106c3fb27SDimitry Andric // TODO: CUDA / HIP include their own headers for some common functions 120206c3fb27SDimitry Andric // implemented here. We'll need to clean those up so they do not conflict. 120306c3fb27SDimitry Andric SmallString<128> P(D.ResourceDir); 120406c3fb27SDimitry Andric llvm::sys::path::append(P, "include"); 120506c3fb27SDimitry Andric llvm::sys::path::append(P, "llvm_libc_wrappers"); 120606c3fb27SDimitry Andric CmdArgs.push_back("-internal-isystem"); 120706c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 120806c3fb27SDimitry Andric } 120906c3fb27SDimitry Andric } 121006c3fb27SDimitry Andric 12110b57cec5SDimitry Andric // If we are offloading to a target via OpenMP we need to include the 12120b57cec5SDimitry Andric // openmp_wrappers folder which contains alternative system headers. 12130b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_OpenMP) && 121481ad6265SDimitry Andric !Args.hasArg(options::OPT_nostdinc) && 1215bdd1243dSDimitry Andric !Args.hasArg(options::OPT_nogpuinc) && 121669ade1e0SDimitry Andric (getToolChain().getTriple().isNVPTX() || 121769ade1e0SDimitry Andric getToolChain().getTriple().isAMDGCN())) { 12180b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nobuiltininc)) { 12190b57cec5SDimitry Andric // Add openmp_wrappers/* to our system include path. This lets us wrap 12200b57cec5SDimitry Andric // standard library headers. 12210b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 12220b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 12230b57cec5SDimitry Andric llvm::sys::path::append(P, "openmp_wrappers"); 12240b57cec5SDimitry Andric CmdArgs.push_back("-internal-isystem"); 12250b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric 12280b57cec5SDimitry Andric CmdArgs.push_back("-include"); 12295ffd83dbSDimitry Andric CmdArgs.push_back("__clang_openmp_device_functions.h"); 12300b57cec5SDimitry Andric } 12310b57cec5SDimitry Andric 12320b57cec5SDimitry Andric // Add -i* options, and automatically translate to 12330b57cec5SDimitry Andric // -include-pch/-include-pth for transparent PCH support. It's 12340b57cec5SDimitry Andric // wonky, but we include looking for .gch so we can support seamless 12350b57cec5SDimitry Andric // replacement into a build system already set up to be generating 12360b57cec5SDimitry Andric // .gch files. 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric if (getToolChain().getDriver().IsCLMode()) { 12390b57cec5SDimitry Andric const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); 12400b57cec5SDimitry Andric const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); 12410b57cec5SDimitry Andric if (YcArg && JA.getKind() >= Action::PrecompileJobClass && 12420b57cec5SDimitry Andric JA.getKind() <= Action::AssembleJobClass) { 12430b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj")); 1244f91b0c1cSDimitry Andric // -fpch-instantiate-templates is the default when creating 1245f91b0c1cSDimitry Andric // precomp using /Yc 1246f91b0c1cSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 1247f91b0c1cSDimitry Andric options::OPT_fno_pch_instantiate_templates, true)) 12485ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates")); 12490b57cec5SDimitry Andric } 12500b57cec5SDimitry Andric if (YcArg || YuArg) { 12510b57cec5SDimitry Andric StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue(); 12520b57cec5SDimitry Andric if (!isa<PrecompileJobAction>(JA)) { 12530b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 12540b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath( 12550b57cec5SDimitry Andric C, !ThroughHeader.empty() 12560b57cec5SDimitry Andric ? ThroughHeader 12570b57cec5SDimitry Andric : llvm::sys::path::filename(Inputs[0].getBaseInput())))); 12580b57cec5SDimitry Andric } 12590b57cec5SDimitry Andric 12600b57cec5SDimitry Andric if (ThroughHeader.empty()) { 12610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 12620b57cec5SDimitry Andric Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use"))); 12630b57cec5SDimitry Andric } else { 12640b57cec5SDimitry Andric CmdArgs.push_back( 12650b57cec5SDimitry Andric Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader)); 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric 12700b57cec5SDimitry Andric bool RenderedImplicitInclude = false; 12710b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) { 127281ad6265SDimitry Andric if (A->getOption().matches(options::OPT_include) && 127381ad6265SDimitry Andric D.getProbePrecompiled()) { 12740b57cec5SDimitry Andric // Handling of gcc-style gch precompiled headers. 12750b57cec5SDimitry Andric bool IsFirstImplicitInclude = !RenderedImplicitInclude; 12760b57cec5SDimitry Andric RenderedImplicitInclude = true; 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric bool FoundPCH = false; 12790b57cec5SDimitry Andric SmallString<128> P(A->getValue()); 12800b57cec5SDimitry Andric // We want the files to have a name like foo.h.pch. Add a dummy extension 12810b57cec5SDimitry Andric // so that replace_extension does the right thing. 12820b57cec5SDimitry Andric P += ".dummy"; 12830b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "pch"); 128481ad6265SDimitry Andric if (D.getVFS().exists(P)) 12850b57cec5SDimitry Andric FoundPCH = true; 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric if (!FoundPCH) { 12880b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "gch"); 128981ad6265SDimitry Andric if (D.getVFS().exists(P)) { 12900b57cec5SDimitry Andric FoundPCH = true; 12910b57cec5SDimitry Andric } 12920b57cec5SDimitry Andric } 12930b57cec5SDimitry Andric 12940b57cec5SDimitry Andric if (FoundPCH) { 12950b57cec5SDimitry Andric if (IsFirstImplicitInclude) { 12960b57cec5SDimitry Andric A->claim(); 12970b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 12980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12990b57cec5SDimitry Andric continue; 13000b57cec5SDimitry Andric } else { 13010b57cec5SDimitry Andric // Ignore the PCH if not first on command line and emit warning. 13020b57cec5SDimitry Andric D.Diag(diag::warn_drv_pch_not_first_include) << P 13030b57cec5SDimitry Andric << A->getAsString(Args); 13040b57cec5SDimitry Andric } 13050b57cec5SDimitry Andric } 13060b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_isystem_after)) { 13070b57cec5SDimitry Andric // Handling of paths which must come late. These entries are handled by 13080b57cec5SDimitry Andric // the toolchain itself after the resource dir is inserted in the right 13090b57cec5SDimitry Andric // search order. 13100b57cec5SDimitry Andric // Do not claim the argument so that the use of the argument does not 13110b57cec5SDimitry Andric // silently go unnoticed on toolchains which do not honour the option. 13120b57cec5SDimitry Andric continue; 1313a7dea167SDimitry Andric } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) { 1314a7dea167SDimitry Andric // Translated to -internal-isystem by the driver, no need to pass to cc1. 1315a7dea167SDimitry Andric continue; 131606c3fb27SDimitry Andric } else if (A->getOption().matches(options::OPT_ibuiltininc)) { 131706c3fb27SDimitry Andric // This is used only by the driver. No need to pass to cc1. 131806c3fb27SDimitry Andric continue; 13190b57cec5SDimitry Andric } 13200b57cec5SDimitry Andric 13210b57cec5SDimitry Andric // Not translated, render as usual. 13220b57cec5SDimitry Andric A->claim(); 13230b57cec5SDimitry Andric A->render(Args, CmdArgs); 13240b57cec5SDimitry Andric } 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 13270b57cec5SDimitry Andric {options::OPT_D, options::OPT_U, options::OPT_I_Group, 13280b57cec5SDimitry Andric options::OPT_F, options::OPT_index_header_map}); 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric // Add -Wp, and -Xpreprocessor if using the preprocessor. 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric // FIXME: There is a very unfortunate problem here, some troubled 13330b57cec5SDimitry Andric // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 13340b57cec5SDimitry Andric // really support that we would have to parse and then translate 13350b57cec5SDimitry Andric // those options. :( 13360b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 13370b57cec5SDimitry Andric options::OPT_Xpreprocessor); 13380b57cec5SDimitry Andric 13390b57cec5SDimitry Andric // -I- is a deprecated GCC feature, reject it. 13400b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_I_)) 13410b57cec5SDimitry Andric D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 13440b57cec5SDimitry Andric // -isysroot to the CC1 invocation. 13450b57cec5SDimitry Andric StringRef sysroot = C.getSysRoot(); 13460b57cec5SDimitry Andric if (sysroot != "") { 13470b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_isysroot)) { 13480b57cec5SDimitry Andric CmdArgs.push_back("-isysroot"); 13490b57cec5SDimitry Andric CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 13500b57cec5SDimitry Andric } 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric // Parse additional include paths from environment variables. 13540b57cec5SDimitry Andric // FIXME: We should probably sink the logic for handling these from the 13550b57cec5SDimitry Andric // frontend into the driver. It will allow deleting 4 otherwise unused flags. 13560b57cec5SDimitry Andric // CPATH - included following the user specified includes (but prior to 13570b57cec5SDimitry Andric // builtin and standard includes). 13580b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 13590b57cec5SDimitry Andric // C_INCLUDE_PATH - system includes enabled when compiling C. 13600b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 13610b57cec5SDimitry Andric // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 13620b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 13630b57cec5SDimitry Andric // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 13640b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 13650b57cec5SDimitry Andric // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 13660b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric // While adding the include arguments, we also attempt to retrieve the 13690b57cec5SDimitry Andric // arguments of related offloading toolchains or arguments that are specific 13700b57cec5SDimitry Andric // of an offloading programming model. 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric // Add C++ include arguments, if needed. 1373a7dea167SDimitry Andric if (types::isCXX(Inputs[0].getType())) { 1374a7dea167SDimitry Andric bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem); 1375a7dea167SDimitry Andric forAllAssociatedToolChains( 1376a7dea167SDimitry Andric C, JA, getToolChain(), 1377a7dea167SDimitry Andric [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) { 1378a7dea167SDimitry Andric HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs) 1379a7dea167SDimitry Andric : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 13800b57cec5SDimitry Andric }); 1381a7dea167SDimitry Andric } 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric // Add system include arguments for all targets but IAMCU. 13840b57cec5SDimitry Andric if (!IsIAMCU) 13850b57cec5SDimitry Andric forAllAssociatedToolChains(C, JA, getToolChain(), 13860b57cec5SDimitry Andric [&Args, &CmdArgs](const ToolChain &TC) { 13870b57cec5SDimitry Andric TC.AddClangSystemIncludeArgs(Args, CmdArgs); 13880b57cec5SDimitry Andric }); 13890b57cec5SDimitry Andric else { 13900b57cec5SDimitry Andric // For IAMCU add special include arguments. 13910b57cec5SDimitry Andric getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs); 13920b57cec5SDimitry Andric } 1393480093f4SDimitry Andric 1394480093f4SDimitry Andric addMacroPrefixMapArg(D, Args, CmdArgs); 1395fe6060f1SDimitry Andric addCoveragePrefixMapArg(D, Args, CmdArgs); 139681ad6265SDimitry Andric 139781ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffile_reproducible, 139881ad6265SDimitry Andric options::OPT_fno_file_reproducible); 1399bdd1243dSDimitry Andric 1400bdd1243dSDimitry Andric if (const char *Epoch = std::getenv("SOURCE_DATE_EPOCH")) { 1401bdd1243dSDimitry Andric CmdArgs.push_back("-source-date-epoch"); 1402bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Epoch)); 1403bdd1243dSDimitry Andric } 14040b57cec5SDimitry Andric } 14050b57cec5SDimitry Andric 14060b57cec5SDimitry Andric // FIXME: Move to target hook. 14070b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) { 14080b57cec5SDimitry Andric switch (Triple.getArch()) { 14090b57cec5SDimitry Andric default: 14100b57cec5SDimitry Andric return true; 14110b57cec5SDimitry Andric 14120b57cec5SDimitry Andric case llvm::Triple::aarch64: 1413480093f4SDimitry Andric case llvm::Triple::aarch64_32: 14140b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 14150b57cec5SDimitry Andric case llvm::Triple::arm: 14160b57cec5SDimitry Andric case llvm::Triple::armeb: 14170b57cec5SDimitry Andric case llvm::Triple::thumb: 14180b57cec5SDimitry Andric case llvm::Triple::thumbeb: 14190b57cec5SDimitry Andric if (Triple.isOSDarwin() || Triple.isOSWindows()) 14200b57cec5SDimitry Andric return true; 14210b57cec5SDimitry Andric return false; 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric case llvm::Triple::ppc: 14240b57cec5SDimitry Andric case llvm::Triple::ppc64: 14250b57cec5SDimitry Andric if (Triple.isOSDarwin()) 14260b57cec5SDimitry Andric return true; 14270b57cec5SDimitry Andric return false; 14280b57cec5SDimitry Andric 14290b57cec5SDimitry Andric case llvm::Triple::hexagon: 1430e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 14310b57cec5SDimitry Andric case llvm::Triple::ppc64le: 14320b57cec5SDimitry Andric case llvm::Triple::riscv32: 14330b57cec5SDimitry Andric case llvm::Triple::riscv64: 14340b57cec5SDimitry Andric case llvm::Triple::systemz: 14350b57cec5SDimitry Andric case llvm::Triple::xcore: 14360b57cec5SDimitry Andric return false; 14370b57cec5SDimitry Andric } 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 1440480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple, 1441480093f4SDimitry Andric const ArgList &Args) { 1442480093f4SDimitry Andric // Supported only on Darwin where we invoke the compiler multiple times 1443480093f4SDimitry Andric // followed by an invocation to lipo. 1444480093f4SDimitry Andric if (!Triple.isOSDarwin()) 1445480093f4SDimitry Andric return false; 1446480093f4SDimitry Andric // If more than one "-arch <arch>" is specified, we're targeting multiple 1447480093f4SDimitry Andric // architectures resulting in a fat binary. 1448480093f4SDimitry Andric return Args.getAllArgValues(options::OPT_arch).size() > 1; 1449480093f4SDimitry Andric } 1450480093f4SDimitry Andric 1451480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args, 1452480093f4SDimitry Andric const llvm::Triple &Triple) { 1453480093f4SDimitry Andric // When enabling remarks, we need to error if: 1454480093f4SDimitry Andric // * The remark file is specified but we're targeting multiple architectures, 1455480093f4SDimitry Andric // which means more than one remark file is being generated. 1456480093f4SDimitry Andric bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args); 1457480093f4SDimitry Andric bool hasExplicitOutputFile = 1458480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1459480093f4SDimitry Andric if (hasMultipleInvocations && hasExplicitOutputFile) { 1460480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_output_with_multiple_archs) 1461480093f4SDimitry Andric << "-foptimization-record-file"; 1462480093f4SDimitry Andric return false; 1463480093f4SDimitry Andric } 1464480093f4SDimitry Andric return true; 1465480093f4SDimitry Andric } 1466480093f4SDimitry Andric 1467480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, 1468480093f4SDimitry Andric const llvm::Triple &Triple, 1469480093f4SDimitry Andric const InputInfo &Input, 1470480093f4SDimitry Andric const InputInfo &Output, const JobAction &JA) { 1471480093f4SDimitry Andric StringRef Format = "yaml"; 1472480093f4SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) 1473480093f4SDimitry Andric Format = A->getValue(); 1474480093f4SDimitry Andric 1475480093f4SDimitry Andric CmdArgs.push_back("-opt-record-file"); 1476480093f4SDimitry Andric 1477480093f4SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1478480093f4SDimitry Andric if (A) { 1479480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1480480093f4SDimitry Andric } else { 1481480093f4SDimitry Andric bool hasMultipleArchs = 1482480093f4SDimitry Andric Triple.isOSDarwin() && // Only supported on Darwin platforms. 1483480093f4SDimitry Andric Args.getAllArgValues(options::OPT_arch).size() > 1; 1484480093f4SDimitry Andric 1485480093f4SDimitry Andric SmallString<128> F; 1486480093f4SDimitry Andric 1487480093f4SDimitry Andric if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) { 1488480093f4SDimitry Andric if (Arg *FinalOutput = Args.getLastArg(options::OPT_o)) 1489480093f4SDimitry Andric F = FinalOutput->getValue(); 1490480093f4SDimitry Andric } else { 1491480093f4SDimitry Andric if (Format != "yaml" && // For YAML, keep the original behavior. 1492480093f4SDimitry Andric Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles. 1493480093f4SDimitry Andric Output.isFilename()) 1494480093f4SDimitry Andric F = Output.getFilename(); 1495480093f4SDimitry Andric } 1496480093f4SDimitry Andric 1497480093f4SDimitry Andric if (F.empty()) { 1498480093f4SDimitry Andric // Use the input filename. 1499480093f4SDimitry Andric F = llvm::sys::path::stem(Input.getBaseInput()); 1500480093f4SDimitry Andric 1501480093f4SDimitry Andric // If we're compiling for an offload architecture (i.e. a CUDA device), 1502480093f4SDimitry Andric // we need to make the file name for the device compilation different 1503480093f4SDimitry Andric // from the host compilation. 1504480093f4SDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_None) && 1505480093f4SDimitry Andric !JA.isDeviceOffloading(Action::OFK_Host)) { 1506480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1507480093f4SDimitry Andric F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(), 1508480093f4SDimitry Andric Triple.normalize()); 1509480093f4SDimitry Andric F += "-"; 1510480093f4SDimitry Andric F += JA.getOffloadingArch(); 1511480093f4SDimitry Andric } 1512480093f4SDimitry Andric } 1513480093f4SDimitry Andric 1514480093f4SDimitry Andric // If we're having more than one "-arch", we should name the files 1515480093f4SDimitry Andric // differently so that every cc1 invocation writes to a different file. 1516480093f4SDimitry Andric // We're doing that by appending "-<arch>" with "<arch>" being the arch 1517480093f4SDimitry Andric // name from the triple. 1518480093f4SDimitry Andric if (hasMultipleArchs) { 1519480093f4SDimitry Andric // First, remember the extension. 1520480093f4SDimitry Andric SmallString<64> OldExtension = llvm::sys::path::extension(F); 1521480093f4SDimitry Andric // then, remove it. 1522480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1523480093f4SDimitry Andric // attach -<arch> to it. 1524480093f4SDimitry Andric F += "-"; 1525480093f4SDimitry Andric F += Triple.getArchName(); 1526480093f4SDimitry Andric // put back the extension. 1527480093f4SDimitry Andric llvm::sys::path::replace_extension(F, OldExtension); 1528480093f4SDimitry Andric } 1529480093f4SDimitry Andric 1530480093f4SDimitry Andric SmallString<32> Extension; 1531480093f4SDimitry Andric Extension += "opt."; 1532480093f4SDimitry Andric Extension += Format; 1533480093f4SDimitry Andric 1534480093f4SDimitry Andric llvm::sys::path::replace_extension(F, Extension); 1535480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(F)); 1536480093f4SDimitry Andric } 1537480093f4SDimitry Andric 1538480093f4SDimitry Andric if (const Arg *A = 1539480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) { 1540480093f4SDimitry Andric CmdArgs.push_back("-opt-record-passes"); 1541480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1542480093f4SDimitry Andric } 1543480093f4SDimitry Andric 1544480093f4SDimitry Andric if (!Format.empty()) { 1545480093f4SDimitry Andric CmdArgs.push_back("-opt-record-format"); 1546480093f4SDimitry Andric CmdArgs.push_back(Format.data()); 1547480093f4SDimitry Andric } 1548480093f4SDimitry Andric } 1549480093f4SDimitry Andric 1550fe6060f1SDimitry Andric void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) { 1551fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_faapcs_bitfield_width, 1552fe6060f1SDimitry Andric options::OPT_fno_aapcs_bitfield_width, true)) 1553fe6060f1SDimitry Andric CmdArgs.push_back("-fno-aapcs-bitfield-width"); 1554fe6060f1SDimitry Andric 1555fe6060f1SDimitry Andric if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad)) 1556fe6060f1SDimitry Andric CmdArgs.push_back("-faapcs-bitfield-load"); 1557fe6060f1SDimitry Andric } 1558fe6060f1SDimitry Andric 15590b57cec5SDimitry Andric namespace { 1560349cc55cSDimitry Andric void RenderARMABI(const Driver &D, const llvm::Triple &Triple, 1561349cc55cSDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 15620b57cec5SDimitry Andric // Select the ABI to use. 15630b57cec5SDimitry Andric // FIXME: Support -meabi. 15640b57cec5SDimitry Andric // FIXME: Parts of this are duplicated in the backend, unify this somehow. 15650b57cec5SDimitry Andric const char *ABIName = nullptr; 15660b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 15670b57cec5SDimitry Andric ABIName = A->getValue(); 15680b57cec5SDimitry Andric } else { 1569349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 15700b57cec5SDimitry Andric ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data(); 15710b57cec5SDimitry Andric } 15720b57cec5SDimitry Andric 15730b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 15740b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 15750b57cec5SDimitry Andric } 157681ad6265SDimitry Andric 157781ad6265SDimitry Andric void AddUnalignedAccessWarning(ArgStringList &CmdArgs) { 157881ad6265SDimitry Andric auto StrictAlignIter = 1579bdd1243dSDimitry Andric llvm::find_if(llvm::reverse(CmdArgs), [](StringRef Arg) { 158081ad6265SDimitry Andric return Arg == "+strict-align" || Arg == "-strict-align"; 158181ad6265SDimitry Andric }); 158281ad6265SDimitry Andric if (StrictAlignIter != CmdArgs.rend() && 158381ad6265SDimitry Andric StringRef(*StrictAlignIter) == "+strict-align") 158481ad6265SDimitry Andric CmdArgs.push_back("-Wunaligned-access"); 158581ad6265SDimitry Andric } 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric 15881fd87a68SDimitry Andric static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, 15894824e7fdSDimitry Andric ArgStringList &CmdArgs, bool isAArch64) { 15904824e7fdSDimitry Andric const Arg *A = isAArch64 15914824e7fdSDimitry Andric ? Args.getLastArg(options::OPT_msign_return_address_EQ, 15924824e7fdSDimitry Andric options::OPT_mbranch_protection_EQ) 15934824e7fdSDimitry Andric : Args.getLastArg(options::OPT_mbranch_protection_EQ); 15944824e7fdSDimitry Andric if (!A) 15954824e7fdSDimitry Andric return; 15964824e7fdSDimitry Andric 15971fd87a68SDimitry Andric const Driver &D = TC.getDriver(); 15981fd87a68SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 15991fd87a68SDimitry Andric if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass()))) 1600d56accc7SDimitry Andric D.Diag(diag::warn_incompatible_branch_protection_option) 16011fd87a68SDimitry Andric << Triple.getArchName(); 16021fd87a68SDimitry Andric 16034824e7fdSDimitry Andric StringRef Scope, Key; 16044824e7fdSDimitry Andric bool IndirectBranches; 16054824e7fdSDimitry Andric 16064824e7fdSDimitry Andric if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { 16074824e7fdSDimitry Andric Scope = A->getValue(); 160881ad6265SDimitry Andric if (Scope != "none" && Scope != "non-leaf" && Scope != "all") 160981ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1610bdd1243dSDimitry Andric << A->getSpelling() << Scope; 16114824e7fdSDimitry Andric Key = "a_key"; 16124824e7fdSDimitry Andric IndirectBranches = false; 16134824e7fdSDimitry Andric } else { 16144824e7fdSDimitry Andric StringRef DiagMsg; 16154824e7fdSDimitry Andric llvm::ARM::ParsedBranchProtection PBP; 16164824e7fdSDimitry Andric if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg)) 161781ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1618bdd1243dSDimitry Andric << A->getSpelling() << DiagMsg; 16194824e7fdSDimitry Andric if (!isAArch64 && PBP.Key == "b_key") 16204824e7fdSDimitry Andric D.Diag(diag::warn_unsupported_branch_protection) 16214824e7fdSDimitry Andric << "b-key" << A->getAsString(Args); 16224824e7fdSDimitry Andric Scope = PBP.Scope; 16234824e7fdSDimitry Andric Key = PBP.Key; 16244824e7fdSDimitry Andric IndirectBranches = PBP.BranchTargetEnforcement; 16254824e7fdSDimitry Andric } 16264824e7fdSDimitry Andric 16274824e7fdSDimitry Andric CmdArgs.push_back( 16284824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address=") + Scope)); 16294824e7fdSDimitry Andric if (!Scope.equals("none")) 16304824e7fdSDimitry Andric CmdArgs.push_back( 16314824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); 16324824e7fdSDimitry Andric if (IndirectBranches) 16334824e7fdSDimitry Andric CmdArgs.push_back("-mbranch-target-enforce"); 16344824e7fdSDimitry Andric } 16354824e7fdSDimitry Andric 16360b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, 16370b57cec5SDimitry Andric ArgStringList &CmdArgs, bool KernelOrKext) const { 1638349cc55cSDimitry Andric RenderARMABI(getToolChain().getDriver(), Triple, Args, CmdArgs); 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andric // Determine floating point ABI from the options & target defaults. 16410b57cec5SDimitry Andric arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); 16420b57cec5SDimitry Andric if (ABI == arm::FloatABI::Soft) { 16430b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 16440b57cec5SDimitry Andric // FIXME: This changes CPP defines, we need -target-soft-float. 16450b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 16460b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16470b57cec5SDimitry Andric CmdArgs.push_back("soft"); 16480b57cec5SDimitry Andric } else if (ABI == arm::FloatABI::SoftFP) { 16490b57cec5SDimitry Andric // Floating point operations are hard, but argument passing is soft. 16500b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16510b57cec5SDimitry Andric CmdArgs.push_back("soft"); 16520b57cec5SDimitry Andric } else { 16530b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 16540b57cec5SDimitry Andric assert(ABI == arm::FloatABI::Hard && "Invalid float abi!"); 16550b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16560b57cec5SDimitry Andric CmdArgs.push_back("hard"); 16570b57cec5SDimitry Andric } 16580b57cec5SDimitry Andric 16590b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 16600b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 16610b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 16620b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 16630b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 16640b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=false"); 16650b57cec5SDimitry Andric else 16660b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=true"); 16670b57cec5SDimitry Andric } 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 16700b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 16710b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 16720b57cec5SDimitry Andric 16730b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mcmse)) 16740b57cec5SDimitry Andric CmdArgs.push_back("-mcmse"); 1675fe6060f1SDimitry Andric 1676fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 16774824e7fdSDimitry Andric 16784824e7fdSDimitry Andric // Enable/disable return address signing and indirect branch targets. 16791fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); 168081ad6265SDimitry Andric 168181ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 16820b57cec5SDimitry Andric } 16830b57cec5SDimitry Andric 16840b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, 16850b57cec5SDimitry Andric const ArgList &Args, bool KernelOrKext, 16860b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 16870b57cec5SDimitry Andric const ToolChain &TC = getToolChain(); 16880b57cec5SDimitry Andric 16890b57cec5SDimitry Andric // Add the target features 16905ffd83dbSDimitry Andric getTargetFeatures(TC.getDriver(), EffectiveTriple, Args, CmdArgs, false); 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric // Add target specific flags. 16930b57cec5SDimitry Andric switch (TC.getArch()) { 16940b57cec5SDimitry Andric default: 16950b57cec5SDimitry Andric break; 16960b57cec5SDimitry Andric 16970b57cec5SDimitry Andric case llvm::Triple::arm: 16980b57cec5SDimitry Andric case llvm::Triple::armeb: 16990b57cec5SDimitry Andric case llvm::Triple::thumb: 17000b57cec5SDimitry Andric case llvm::Triple::thumbeb: 17010b57cec5SDimitry Andric // Use the effective triple, which takes into account the deployment target. 17020b57cec5SDimitry Andric AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext); 17030b57cec5SDimitry Andric break; 17040b57cec5SDimitry Andric 17050b57cec5SDimitry Andric case llvm::Triple::aarch64: 1706480093f4SDimitry Andric case llvm::Triple::aarch64_32: 17070b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 17080b57cec5SDimitry Andric AddAArch64TargetArgs(Args, CmdArgs); 1709bdd1243dSDimitry Andric break; 1710bdd1243dSDimitry Andric 1711bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 1712bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 1713bdd1243dSDimitry Andric AddLoongArchTargetArgs(Args, CmdArgs); 17140b57cec5SDimitry Andric break; 17150b57cec5SDimitry Andric 17160b57cec5SDimitry Andric case llvm::Triple::mips: 17170b57cec5SDimitry Andric case llvm::Triple::mipsel: 17180b57cec5SDimitry Andric case llvm::Triple::mips64: 17190b57cec5SDimitry Andric case llvm::Triple::mips64el: 17200b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 17210b57cec5SDimitry Andric break; 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric case llvm::Triple::ppc: 1724e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 17250b57cec5SDimitry Andric case llvm::Triple::ppc64: 17260b57cec5SDimitry Andric case llvm::Triple::ppc64le: 17270b57cec5SDimitry Andric AddPPCTargetArgs(Args, CmdArgs); 17280b57cec5SDimitry Andric break; 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andric case llvm::Triple::riscv32: 17310b57cec5SDimitry Andric case llvm::Triple::riscv64: 17320b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 17330b57cec5SDimitry Andric break; 17340b57cec5SDimitry Andric 17350b57cec5SDimitry Andric case llvm::Triple::sparc: 17360b57cec5SDimitry Andric case llvm::Triple::sparcel: 17370b57cec5SDimitry Andric case llvm::Triple::sparcv9: 17380b57cec5SDimitry Andric AddSparcTargetArgs(Args, CmdArgs); 17390b57cec5SDimitry Andric break; 17400b57cec5SDimitry Andric 17410b57cec5SDimitry Andric case llvm::Triple::systemz: 17420b57cec5SDimitry Andric AddSystemZTargetArgs(Args, CmdArgs); 17430b57cec5SDimitry Andric break; 17440b57cec5SDimitry Andric 17450b57cec5SDimitry Andric case llvm::Triple::x86: 17460b57cec5SDimitry Andric case llvm::Triple::x86_64: 17470b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 17480b57cec5SDimitry Andric break; 17490b57cec5SDimitry Andric 17500b57cec5SDimitry Andric case llvm::Triple::lanai: 17510b57cec5SDimitry Andric AddLanaiTargetArgs(Args, CmdArgs); 17520b57cec5SDimitry Andric break; 17530b57cec5SDimitry Andric 17540b57cec5SDimitry Andric case llvm::Triple::hexagon: 17550b57cec5SDimitry Andric AddHexagonTargetArgs(Args, CmdArgs); 17560b57cec5SDimitry Andric break; 17570b57cec5SDimitry Andric 17580b57cec5SDimitry Andric case llvm::Triple::wasm32: 17590b57cec5SDimitry Andric case llvm::Triple::wasm64: 17600b57cec5SDimitry Andric AddWebAssemblyTargetArgs(Args, CmdArgs); 17610b57cec5SDimitry Andric break; 17625ffd83dbSDimitry Andric 17635ffd83dbSDimitry Andric case llvm::Triple::ve: 17645ffd83dbSDimitry Andric AddVETargetArgs(Args, CmdArgs); 17655ffd83dbSDimitry Andric break; 17660b57cec5SDimitry Andric } 17670b57cec5SDimitry Andric } 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric namespace { 17700b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args, 17710b57cec5SDimitry Andric ArgStringList &CmdArgs) { 17720b57cec5SDimitry Andric const char *ABIName = nullptr; 17730b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) 17740b57cec5SDimitry Andric ABIName = A->getValue(); 17750b57cec5SDimitry Andric else if (Triple.isOSDarwin()) 17760b57cec5SDimitry Andric ABIName = "darwinpcs"; 17770b57cec5SDimitry Andric else 17780b57cec5SDimitry Andric ABIName = "aapcs"; 17790b57cec5SDimitry Andric 17800b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 17810b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 17820b57cec5SDimitry Andric } 17830b57cec5SDimitry Andric } 17840b57cec5SDimitry Andric 17850b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args, 17860b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 17870b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 17880b57cec5SDimitry Andric 17890b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 17900b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 17910b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 17920b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 17930b57cec5SDimitry Andric 17940b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 17950b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 17960b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 17970b57cec5SDimitry Andric 17980b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 17990b57cec5SDimitry Andric 18000b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 18010b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 18020b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 18030b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18040b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 18050b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=false"); 18060b57cec5SDimitry Andric else 18070b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=true"); 18080b57cec5SDimitry Andric } 18090b57cec5SDimitry Andric 18100b57cec5SDimitry Andric // Enable/disable return address signing and indirect branch targets. 18111fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, true /*isAArch64*/); 1812e8d8bef9SDimitry Andric 1813e8d8bef9SDimitry Andric // Handle -msve_vector_bits=<bits> 1814e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) { 1815e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 1816e8d8bef9SDimitry Andric const Driver &D = getToolChain().getDriver(); 1817e8d8bef9SDimitry Andric if (Val.equals("128") || Val.equals("256") || Val.equals("512") || 1818349cc55cSDimitry Andric Val.equals("1024") || Val.equals("2048") || Val.equals("128+") || 1819349cc55cSDimitry Andric Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") || 1820349cc55cSDimitry Andric Val.equals("2048+")) { 1821349cc55cSDimitry Andric unsigned Bits = 0; 1822349cc55cSDimitry Andric if (Val.endswith("+")) 1823349cc55cSDimitry Andric Val = Val.substr(0, Val.size() - 1); 1824349cc55cSDimitry Andric else { 1825349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1826349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1827e8d8bef9SDimitry Andric CmdArgs.push_back( 1828349cc55cSDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128))); 1829349cc55cSDimitry Andric } 1830349cc55cSDimitry Andric 1831349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1832349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1833349cc55cSDimitry Andric CmdArgs.push_back( 1834349cc55cSDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128))); 1835e8d8bef9SDimitry Andric // Silently drop requests for vector-length agnostic code as it's implied. 1836349cc55cSDimitry Andric } else if (!Val.equals("scalable")) 1837e8d8bef9SDimitry Andric // Handle the unsupported values passed to msve-vector-bits. 1838e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1839bdd1243dSDimitry Andric << A->getSpelling() << Val; 1840e8d8bef9SDimitry Andric } 1841fe6060f1SDimitry Andric 1842fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 1843349cc55cSDimitry Andric 1844349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 1845349cc55cSDimitry Andric CmdArgs.push_back("-tune-cpu"); 1846fcaf7f86SDimitry Andric if (strcmp(A->getValue(), "native") == 0) 1847fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 1848fcaf7f86SDimitry Andric else 1849fcaf7f86SDimitry Andric CmdArgs.push_back(A->getValue()); 1850349cc55cSDimitry Andric } 185181ad6265SDimitry Andric 185281ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 18530b57cec5SDimitry Andric } 18540b57cec5SDimitry Andric 1855bdd1243dSDimitry Andric void Clang::AddLoongArchTargetArgs(const ArgList &Args, 1856bdd1243dSDimitry Andric ArgStringList &CmdArgs) const { 18578a4dda33SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 18588a4dda33SDimitry Andric 1859bdd1243dSDimitry Andric CmdArgs.push_back("-target-abi"); 18608a4dda33SDimitry Andric CmdArgs.push_back( 18618a4dda33SDimitry Andric loongarch::getLoongArchABI(getToolChain().getDriver(), Args, Triple) 1862bdd1243dSDimitry Andric .data()); 18638a4dda33SDimitry Andric 18648a4dda33SDimitry Andric // Handle -mtune. 18658a4dda33SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 18668a4dda33SDimitry Andric std::string TuneCPU = A->getValue(); 18678a4dda33SDimitry Andric TuneCPU = loongarch::postProcessTargetCPUString(TuneCPU, Triple); 18688a4dda33SDimitry Andric CmdArgs.push_back("-tune-cpu"); 18698a4dda33SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 18708a4dda33SDimitry Andric } 1871bdd1243dSDimitry Andric } 1872bdd1243dSDimitry Andric 18730b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args, 18740b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 18750b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 18760b57cec5SDimitry Andric StringRef CPUName; 18770b57cec5SDimitry Andric StringRef ABIName; 18780b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 18790b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 18800b57cec5SDimitry Andric 18810b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 18820b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 18830b57cec5SDimitry Andric 18840b57cec5SDimitry Andric mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple); 18850b57cec5SDimitry Andric if (ABI == mips::FloatABI::Soft) { 18860b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 18870b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 18880b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 18890b57cec5SDimitry Andric CmdArgs.push_back("soft"); 18900b57cec5SDimitry Andric } else { 18910b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 18920b57cec5SDimitry Andric assert(ABI == mips::FloatABI::Hard && "Invalid float abi!"); 18930b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 18940b57cec5SDimitry Andric CmdArgs.push_back("hard"); 18950b57cec5SDimitry Andric } 18960b57cec5SDimitry Andric 18970b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 18980b57cec5SDimitry Andric options::OPT_mno_ldc1_sdc1)) { 18990b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 19000b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19010b57cec5SDimitry Andric CmdArgs.push_back("-mno-ldc1-sdc1"); 19020b57cec5SDimitry Andric } 19030b57cec5SDimitry Andric } 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 19060b57cec5SDimitry Andric options::OPT_mno_check_zero_division)) { 19070b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 19080b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19090b57cec5SDimitry Andric CmdArgs.push_back("-mno-check-zero-division"); 19100b57cec5SDimitry Andric } 19110b57cec5SDimitry Andric } 19120b57cec5SDimitry Andric 191304eeddc0SDimitry Andric if (Args.getLastArg(options::OPT_mfix4300)) { 191404eeddc0SDimitry Andric CmdArgs.push_back("-mllvm"); 191504eeddc0SDimitry Andric CmdArgs.push_back("-mfix4300"); 191604eeddc0SDimitry Andric } 191704eeddc0SDimitry Andric 19180b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_G)) { 19190b57cec5SDimitry Andric StringRef v = A->getValue(); 19200b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19210b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 19220b57cec5SDimitry Andric A->claim(); 19230b57cec5SDimitry Andric } 19240b57cec5SDimitry Andric 19250b57cec5SDimitry Andric Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt); 19260b57cec5SDimitry Andric Arg *ABICalls = 19270b57cec5SDimitry Andric Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls); 19280b57cec5SDimitry Andric 19290b57cec5SDimitry Andric // -mabicalls is the default for many MIPS environments, even with -fno-pic. 19300b57cec5SDimitry Andric // -mgpopt is the default for static, -fno-pic environments but these two 19310b57cec5SDimitry Andric // options conflict. We want to be certain that -mno-abicalls -mgpopt is 19320b57cec5SDimitry Andric // the only case where -mllvm -mgpopt is passed. 19330b57cec5SDimitry Andric // NOTE: We need a warning here or in the backend to warn when -mgpopt is 19340b57cec5SDimitry Andric // passed explicitly when compiling something with -mabicalls 19350b57cec5SDimitry Andric // (implictly) in affect. Currently the warning is in the backend. 19360b57cec5SDimitry Andric // 19370b57cec5SDimitry Andric // When the ABI in use is N64, we also need to determine the PIC mode that 19380b57cec5SDimitry Andric // is in use, as -fno-pic for N64 implies -mno-abicalls. 19390b57cec5SDimitry Andric bool NoABICalls = 19400b57cec5SDimitry Andric ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls); 19410b57cec5SDimitry Andric 19420b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 19430b57cec5SDimitry Andric unsigned PICLevel; 19440b57cec5SDimitry Andric bool IsPIE; 19450b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 19460b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 19470b57cec5SDimitry Andric 19480b57cec5SDimitry Andric NoABICalls = NoABICalls || 19490b57cec5SDimitry Andric (RelocationModel == llvm::Reloc::Static && ABIName == "n64"); 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt); 19520b57cec5SDimitry Andric // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt. 19530b57cec5SDimitry Andric if (NoABICalls && (!GPOpt || WantGPOpt)) { 19540b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19550b57cec5SDimitry Andric CmdArgs.push_back("-mgpopt"); 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata, 19580b57cec5SDimitry Andric options::OPT_mno_local_sdata); 19590b57cec5SDimitry Andric Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata, 19600b57cec5SDimitry Andric options::OPT_mno_extern_sdata); 19610b57cec5SDimitry Andric Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data, 19620b57cec5SDimitry Andric options::OPT_mno_embedded_data); 19630b57cec5SDimitry Andric if (LocalSData) { 19640b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19650b57cec5SDimitry Andric if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) { 19660b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=1"); 19670b57cec5SDimitry Andric } else { 19680b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=0"); 19690b57cec5SDimitry Andric } 19700b57cec5SDimitry Andric LocalSData->claim(); 19710b57cec5SDimitry Andric } 19720b57cec5SDimitry Andric 19730b57cec5SDimitry Andric if (ExternSData) { 19740b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19750b57cec5SDimitry Andric if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) { 19760b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=1"); 19770b57cec5SDimitry Andric } else { 19780b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=0"); 19790b57cec5SDimitry Andric } 19800b57cec5SDimitry Andric ExternSData->claim(); 19810b57cec5SDimitry Andric } 19820b57cec5SDimitry Andric 19830b57cec5SDimitry Andric if (EmbeddedData) { 19840b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19850b57cec5SDimitry Andric if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) { 19860b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=1"); 19870b57cec5SDimitry Andric } else { 19880b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=0"); 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric EmbeddedData->claim(); 19910b57cec5SDimitry Andric } 19920b57cec5SDimitry Andric 19930b57cec5SDimitry Andric } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt) 19940b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1); 19950b57cec5SDimitry Andric 19960b57cec5SDimitry Andric if (GPOpt) 19970b57cec5SDimitry Andric GPOpt->claim(); 19980b57cec5SDimitry Andric 19990b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) { 20000b57cec5SDimitry Andric StringRef Val = StringRef(A->getValue()); 20010b57cec5SDimitry Andric if (mips::hasCompactBranches(CPUName)) { 20020b57cec5SDimitry Andric if (Val == "never" || Val == "always" || Val == "optimal") { 20030b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20040b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val)); 20050b57cec5SDimitry Andric } else 20060b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2007bdd1243dSDimitry Andric << A->getSpelling() << Val; 20080b57cec5SDimitry Andric } else 20090b57cec5SDimitry Andric D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName; 20100b57cec5SDimitry Andric } 20110b57cec5SDimitry Andric 20120b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls, 20130b57cec5SDimitry Andric options::OPT_mno_relax_pic_calls)) { 20140b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) { 20150b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20160b57cec5SDimitry Andric CmdArgs.push_back("-mips-jalr-reloc=0"); 20170b57cec5SDimitry Andric } 20180b57cec5SDimitry Andric } 20190b57cec5SDimitry Andric } 20200b57cec5SDimitry Andric 20210b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args, 20220b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 202306c3fb27SDimitry Andric const Driver &D = getToolChain().getDriver(); 20241ac55f4cSDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 202506c3fb27SDimitry Andric if (Args.getLastArg(options::OPT_mtune_EQ)) { 2026bdd1243dSDimitry Andric CmdArgs.push_back("-tune-cpu"); 20271ac55f4cSDimitry Andric std::string CPU = ppc::getPPCTuneCPU(Args, T); 20281ac55f4cSDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 2029bdd1243dSDimitry Andric } 2030bdd1243dSDimitry Andric 20310b57cec5SDimitry Andric // Select the ABI to use. 20320b57cec5SDimitry Andric const char *ABIName = nullptr; 2033480093f4SDimitry Andric if (T.isOSBinFormatELF()) { 20340b57cec5SDimitry Andric switch (getToolChain().getArch()) { 20350b57cec5SDimitry Andric case llvm::Triple::ppc64: { 20361ac55f4cSDimitry Andric if (T.isPPC64ELFv2ABI()) 2037480093f4SDimitry Andric ABIName = "elfv2"; 2038480093f4SDimitry Andric else 20390b57cec5SDimitry Andric ABIName = "elfv1"; 20400b57cec5SDimitry Andric break; 20410b57cec5SDimitry Andric } 20420b57cec5SDimitry Andric case llvm::Triple::ppc64le: 20430b57cec5SDimitry Andric ABIName = "elfv2"; 20440b57cec5SDimitry Andric break; 20450b57cec5SDimitry Andric default: 20460b57cec5SDimitry Andric break; 20470b57cec5SDimitry Andric } 2048480093f4SDimitry Andric } 20490b57cec5SDimitry Andric 205004eeddc0SDimitry Andric bool IEEELongDouble = getToolChain().defaultToIEEELongDouble(); 205106c3fb27SDimitry Andric bool VecExtabi = false; 20520b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) { 20530b57cec5SDimitry Andric StringRef V = A->getValue(); 205406c3fb27SDimitry Andric if (V == "ieeelongdouble") { 20550b57cec5SDimitry Andric IEEELongDouble = true; 205606c3fb27SDimitry Andric A->claim(); 205706c3fb27SDimitry Andric } else if (V == "ibmlongdouble") { 20580b57cec5SDimitry Andric IEEELongDouble = false; 205906c3fb27SDimitry Andric A->claim(); 206006c3fb27SDimitry Andric } else if (V == "vec-default") { 206106c3fb27SDimitry Andric VecExtabi = false; 206206c3fb27SDimitry Andric A->claim(); 206306c3fb27SDimitry Andric } else if (V == "vec-extabi") { 206406c3fb27SDimitry Andric VecExtabi = true; 206506c3fb27SDimitry Andric A->claim(); 20668a4dda33SDimitry Andric } else if (V == "elfv1") { 20678a4dda33SDimitry Andric ABIName = "elfv1"; 20688a4dda33SDimitry Andric A->claim(); 20698a4dda33SDimitry Andric } else if (V == "elfv2") { 20708a4dda33SDimitry Andric ABIName = "elfv2"; 20718a4dda33SDimitry Andric A->claim(); 207206c3fb27SDimitry Andric } else if (V != "altivec") 20730b57cec5SDimitry Andric // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore 20740b57cec5SDimitry Andric // the option if given as we don't have backend support for any targets 20750b57cec5SDimitry Andric // that don't use the altivec abi. 20760b57cec5SDimitry Andric ABIName = A->getValue(); 20770b57cec5SDimitry Andric } 20780b57cec5SDimitry Andric if (IEEELongDouble) 20790b57cec5SDimitry Andric CmdArgs.push_back("-mabi=ieeelongdouble"); 208006c3fb27SDimitry Andric if (VecExtabi) { 208106c3fb27SDimitry Andric if (!T.isOSAIX()) 208206c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 208306c3fb27SDimitry Andric << "-mabi=vec-extabi" << T.str(); 208406c3fb27SDimitry Andric CmdArgs.push_back("-mabi=vec-extabi"); 208506c3fb27SDimitry Andric } 20860b57cec5SDimitry Andric 208706c3fb27SDimitry Andric ppc::FloatABI FloatABI = ppc::getPPCFloatABI(D, Args); 20880b57cec5SDimitry Andric if (FloatABI == ppc::FloatABI::Soft) { 20890b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 20900b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 20910b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20920b57cec5SDimitry Andric CmdArgs.push_back("soft"); 20930b57cec5SDimitry Andric } else { 20940b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 20950b57cec5SDimitry Andric assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!"); 20960b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20970b57cec5SDimitry Andric CmdArgs.push_back("hard"); 20980b57cec5SDimitry Andric } 20990b57cec5SDimitry Andric 21000b57cec5SDimitry Andric if (ABIName) { 21010b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 21020b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 21030b57cec5SDimitry Andric } 21040b57cec5SDimitry Andric } 21050b57cec5SDimitry Andric 21065ffd83dbSDimitry Andric static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args, 21075ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 21085ffd83dbSDimitry Andric const Driver &D = TC.getDriver(); 21095ffd83dbSDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 21105ffd83dbSDimitry Andric // Default small data limitation is eight. 21115ffd83dbSDimitry Andric const char *SmallDataLimit = "8"; 21125ffd83dbSDimitry Andric // Get small data limitation. 21135ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_shared, options::OPT_fpic, 21145ffd83dbSDimitry Andric options::OPT_fPIC)) { 21155ffd83dbSDimitry Andric // Not support linker relaxation for PIC. 21165ffd83dbSDimitry Andric SmallDataLimit = "0"; 21175ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 21185ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 21195ffd83dbSDimitry Andric } 21205ffd83dbSDimitry Andric } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ) 2121fe6060f1SDimitry Andric .equals_insensitive("large") && 21225ffd83dbSDimitry Andric (Triple.getArch() == llvm::Triple::riscv64)) { 21235ffd83dbSDimitry Andric // Not support linker relaxation for RV64 with large code model. 21245ffd83dbSDimitry Andric SmallDataLimit = "0"; 21255ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 21265ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 21275ffd83dbSDimitry Andric } 212806c3fb27SDimitry Andric } else if (Triple.isAndroid()) { 212906c3fb27SDimitry Andric // GP relaxation is not supported on Android. 213006c3fb27SDimitry Andric SmallDataLimit = "0"; 213106c3fb27SDimitry Andric if (Args.hasArg(options::OPT_G)) { 213206c3fb27SDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 213306c3fb27SDimitry Andric } 21345ffd83dbSDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_G)) { 21355ffd83dbSDimitry Andric SmallDataLimit = A->getValue(); 21365ffd83dbSDimitry Andric } 21375ffd83dbSDimitry Andric // Forward the -msmall-data-limit= option. 21385ffd83dbSDimitry Andric CmdArgs.push_back("-msmall-data-limit"); 21395ffd83dbSDimitry Andric CmdArgs.push_back(SmallDataLimit); 21405ffd83dbSDimitry Andric } 21415ffd83dbSDimitry Andric 21420b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args, 21430b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21440b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 2145a7dea167SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 21460b57cec5SDimitry Andric 21470b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 2148a7dea167SDimitry Andric CmdArgs.push_back(ABIName.data()); 21495ffd83dbSDimitry Andric 21505ffd83dbSDimitry Andric SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs); 2151e8d8bef9SDimitry Andric 2152bdd1243dSDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 2153bdd1243dSDimitry Andric options::OPT_mno_implicit_float, true)) 2154bdd1243dSDimitry Andric CmdArgs.push_back("-no-implicit-float"); 2155bdd1243dSDimitry Andric 2156fcaf7f86SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 2157e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2158bdd1243dSDimitry Andric if (strcmp(A->getValue(), "native") == 0) 2159bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 2160bdd1243dSDimitry Andric else 2161bdd1243dSDimitry Andric CmdArgs.push_back(A->getValue()); 2162e8d8bef9SDimitry Andric } 216306c3fb27SDimitry Andric 216406c3fb27SDimitry Andric // Handle -mrvv-vector-bits=<bits> 216506c3fb27SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) { 216606c3fb27SDimitry Andric StringRef Val = A->getValue(); 216706c3fb27SDimitry Andric const Driver &D = getToolChain().getDriver(); 216806c3fb27SDimitry Andric 216906c3fb27SDimitry Andric // Get minimum VLen from march. 217006c3fb27SDimitry Andric unsigned MinVLen = 0; 217106c3fb27SDimitry Andric StringRef Arch = riscv::getRISCVArch(Args, Triple); 217206c3fb27SDimitry Andric auto ISAInfo = llvm::RISCVISAInfo::parseArchString( 217306c3fb27SDimitry Andric Arch, /*EnableExperimentalExtensions*/ true); 217406c3fb27SDimitry Andric if (!ISAInfo) { 217506c3fb27SDimitry Andric // Ignore parsing error. 217606c3fb27SDimitry Andric consumeError(ISAInfo.takeError()); 217706c3fb27SDimitry Andric } else { 217806c3fb27SDimitry Andric MinVLen = (*ISAInfo)->getMinVLen(); 217906c3fb27SDimitry Andric } 218006c3fb27SDimitry Andric 218106c3fb27SDimitry Andric // If the value is "zvl", use MinVLen from march. Otherwise, try to parse 218206c3fb27SDimitry Andric // as integer as long as we have a MinVLen. 218306c3fb27SDimitry Andric unsigned Bits = 0; 218406c3fb27SDimitry Andric if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) { 218506c3fb27SDimitry Andric Bits = MinVLen; 218606c3fb27SDimitry Andric } else if (!Val.getAsInteger(10, Bits)) { 218706c3fb27SDimitry Andric // Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that 218806c3fb27SDimitry Andric // at least MinVLen. 218906c3fb27SDimitry Andric if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock || 219006c3fb27SDimitry Andric Bits > 65536 || !llvm::isPowerOf2_32(Bits)) 219106c3fb27SDimitry Andric Bits = 0; 219206c3fb27SDimitry Andric } 219306c3fb27SDimitry Andric 219406c3fb27SDimitry Andric // If we got a valid value try to use it. 219506c3fb27SDimitry Andric if (Bits != 0) { 219606c3fb27SDimitry Andric unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock; 219706c3fb27SDimitry Andric CmdArgs.push_back( 219806c3fb27SDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin))); 219906c3fb27SDimitry Andric CmdArgs.push_back( 220006c3fb27SDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin))); 220106c3fb27SDimitry Andric } else if (!Val.equals("scalable")) { 220206c3fb27SDimitry Andric // Handle the unsupported values passed to mrvv-vector-bits. 220306c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 220406c3fb27SDimitry Andric << A->getSpelling() << Val; 220506c3fb27SDimitry Andric } 220606c3fb27SDimitry Andric } 22070b57cec5SDimitry Andric } 22080b57cec5SDimitry Andric 22090b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args, 22100b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22110b57cec5SDimitry Andric sparc::FloatABI FloatABI = 22120b57cec5SDimitry Andric sparc::getSparcFloatABI(getToolChain().getDriver(), Args); 22130b57cec5SDimitry Andric 22140b57cec5SDimitry Andric if (FloatABI == sparc::FloatABI::Soft) { 22150b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 22160b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 22170b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22180b57cec5SDimitry Andric CmdArgs.push_back("soft"); 22190b57cec5SDimitry Andric } else { 22200b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 22210b57cec5SDimitry Andric assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!"); 22220b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22230b57cec5SDimitry Andric CmdArgs.push_back("hard"); 22240b57cec5SDimitry Andric } 222561cfbce3SDimitry Andric 222661cfbce3SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 222761cfbce3SDimitry Andric StringRef Name = A->getValue(); 222861cfbce3SDimitry Andric std::string TuneCPU; 222961cfbce3SDimitry Andric if (Name == "native") 223061cfbce3SDimitry Andric TuneCPU = std::string(llvm::sys::getHostCPUName()); 223161cfbce3SDimitry Andric else 223261cfbce3SDimitry Andric TuneCPU = std::string(Name); 223361cfbce3SDimitry Andric 223461cfbce3SDimitry Andric CmdArgs.push_back("-tune-cpu"); 223561cfbce3SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 223661cfbce3SDimitry Andric } 22370b57cec5SDimitry Andric } 22380b57cec5SDimitry Andric 22390b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args, 22400b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2241fcaf7f86SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 2242753f127fSDimitry Andric CmdArgs.push_back("-tune-cpu"); 2243fcaf7f86SDimitry Andric if (strcmp(A->getValue(), "native") == 0) 2244fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 2245fcaf7f86SDimitry Andric else 2246fcaf7f86SDimitry Andric CmdArgs.push_back(A->getValue()); 2247753f127fSDimitry Andric } 2248753f127fSDimitry Andric 2249753f127fSDimitry Andric bool HasBackchain = 2250753f127fSDimitry Andric Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false); 2251480093f4SDimitry Andric bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack, 2252480093f4SDimitry Andric options::OPT_mno_packed_stack, false); 22535ffd83dbSDimitry Andric systemz::FloatABI FloatABI = 22545ffd83dbSDimitry Andric systemz::getSystemZFloatABI(getToolChain().getDriver(), Args); 22555ffd83dbSDimitry Andric bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft); 22565ffd83dbSDimitry Andric if (HasBackchain && HasPackedStack && !HasSoftFloat) { 2257480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 2258480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt) 22595ffd83dbSDimitry Andric << "-mpacked-stack -mbackchain -mhard-float"; 2260480093f4SDimitry Andric } 2261480093f4SDimitry Andric if (HasBackchain) 22620b57cec5SDimitry Andric CmdArgs.push_back("-mbackchain"); 2263480093f4SDimitry Andric if (HasPackedStack) 2264480093f4SDimitry Andric CmdArgs.push_back("-mpacked-stack"); 22655ffd83dbSDimitry Andric if (HasSoftFloat) { 22665ffd83dbSDimitry Andric // Floating point operations and argument passing are soft. 22675ffd83dbSDimitry Andric CmdArgs.push_back("-msoft-float"); 22685ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22695ffd83dbSDimitry Andric CmdArgs.push_back("soft"); 2270480093f4SDimitry Andric } 22710b57cec5SDimitry Andric } 22720b57cec5SDimitry Andric 22730b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args, 22740b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2275480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 22765ffd83dbSDimitry Andric addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false); 2277480093f4SDimitry Andric 22780b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 22790b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 22800b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 22810b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 22820b57cec5SDimitry Andric 22830b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs, 22840b57cec5SDimitry Andric options::OPT_mno_tls_direct_seg_refs, true)) 22850b57cec5SDimitry Andric CmdArgs.push_back("-mno-tls-direct-seg-refs"); 22860b57cec5SDimitry Andric 22870b57cec5SDimitry Andric // Default to avoid implicit floating-point for kernel/kext code, but allow 22880b57cec5SDimitry Andric // that to be overridden with -mno-soft-float. 22890b57cec5SDimitry Andric bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 22900b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)); 22910b57cec5SDimitry Andric if (Arg *A = Args.getLastArg( 22920b57cec5SDimitry Andric options::OPT_msoft_float, options::OPT_mno_soft_float, 22930b57cec5SDimitry Andric options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) { 22940b57cec5SDimitry Andric const Option &O = A->getOption(); 22950b57cec5SDimitry Andric NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 22960b57cec5SDimitry Andric O.matches(options::OPT_msoft_float)); 22970b57cec5SDimitry Andric } 22980b57cec5SDimitry Andric if (NoImplicitFloat) 22990b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 23000b57cec5SDimitry Andric 23010b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 23020b57cec5SDimitry Andric StringRef Value = A->getValue(); 23030b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 23040b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23050b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 2306349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-inline-asm=" + Value)); 23070b57cec5SDimitry Andric } else { 2308480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2309bdd1243dSDimitry Andric << A->getSpelling() << Value; 23100b57cec5SDimitry Andric } 2311480093f4SDimitry Andric } else if (D.IsCLMode()) { 23120b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23130b57cec5SDimitry Andric CmdArgs.push_back("-x86-asm-syntax=intel"); 23140b57cec5SDimitry Andric } 23150b57cec5SDimitry Andric 2316349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mskip_rax_setup, 2317349cc55cSDimitry Andric options::OPT_mno_skip_rax_setup)) 2318349cc55cSDimitry Andric if (A->getOption().matches(options::OPT_mskip_rax_setup)) 2319349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mskip-rax-setup")); 2320349cc55cSDimitry Andric 23210b57cec5SDimitry Andric // Set flags to support MCU ABI. 23220b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { 23230b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 23240b57cec5SDimitry Andric CmdArgs.push_back("soft"); 23250b57cec5SDimitry Andric CmdArgs.push_back("-mstack-alignment=4"); 23260b57cec5SDimitry Andric } 2327e8d8bef9SDimitry Andric 2328e8d8bef9SDimitry Andric // Handle -mtune. 2329e8d8bef9SDimitry Andric 233081ad6265SDimitry Andric // Default to "generic" unless -march is present or targetting the PS4/PS5. 2331e8d8bef9SDimitry Andric std::string TuneCPU; 2332e8d8bef9SDimitry Andric if (!Args.hasArg(clang::driver::options::OPT_march_EQ) && 233381ad6265SDimitry Andric !getToolChain().getTriple().isPS()) 2334e8d8bef9SDimitry Andric TuneCPU = "generic"; 2335e8d8bef9SDimitry Andric 2336e8d8bef9SDimitry Andric // Override based on -mtune. 2337e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2338e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2339e8d8bef9SDimitry Andric 2340e8d8bef9SDimitry Andric if (Name == "native") { 2341e8d8bef9SDimitry Andric Name = llvm::sys::getHostCPUName(); 2342e8d8bef9SDimitry Andric if (!Name.empty()) 2343e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2344e8d8bef9SDimitry Andric } else 2345e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2346e8d8bef9SDimitry Andric } 2347e8d8bef9SDimitry Andric 2348e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2349e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2350e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2351e8d8bef9SDimitry Andric } 23520b57cec5SDimitry Andric } 23530b57cec5SDimitry Andric 23540b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args, 23550b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23560b57cec5SDimitry Andric CmdArgs.push_back("-mqdsp6-compat"); 23570b57cec5SDimitry Andric CmdArgs.push_back("-Wreturn-type"); 23580b57cec5SDimitry Andric 23590b57cec5SDimitry Andric if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { 23600b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 2361bdd1243dSDimitry Andric CmdArgs.push_back( 2362bdd1243dSDimitry Andric Args.MakeArgString("-hexagon-small-data-threshold=" + Twine(*G))); 23630b57cec5SDimitry Andric } 23640b57cec5SDimitry Andric 23650b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_short_enums)) 23660b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 23670b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 23680b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23690b57cec5SDimitry Andric CmdArgs.push_back("-enable-hexagon-ieee-rnd-near"); 23700b57cec5SDimitry Andric } 23710b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23720b57cec5SDimitry Andric CmdArgs.push_back("-machine-sink-split=0"); 23730b57cec5SDimitry Andric } 23740b57cec5SDimitry Andric 23750b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args, 23760b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23770b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 23780b57cec5SDimitry Andric StringRef CPUName = A->getValue(); 23790b57cec5SDimitry Andric 23800b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 23810b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPUName)); 23820b57cec5SDimitry Andric } 23830b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 23840b57cec5SDimitry Andric StringRef Value = A->getValue(); 23850b57cec5SDimitry Andric // Only support mregparm=4 to support old usage. Report error for all other 23860b57cec5SDimitry Andric // cases. 23870b57cec5SDimitry Andric int Mregparm; 23880b57cec5SDimitry Andric if (Value.getAsInteger(10, Mregparm)) { 23890b57cec5SDimitry Andric if (Mregparm != 4) { 23900b57cec5SDimitry Andric getToolChain().getDriver().Diag( 23910b57cec5SDimitry Andric diag::err_drv_unsupported_option_argument) 2392bdd1243dSDimitry Andric << A->getSpelling() << Value; 23930b57cec5SDimitry Andric } 23940b57cec5SDimitry Andric } 23950b57cec5SDimitry Andric } 23960b57cec5SDimitry Andric } 23970b57cec5SDimitry Andric 23980b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args, 23990b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 24000b57cec5SDimitry Andric // Default to "hidden" visibility. 24010b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fvisibility_EQ, 2402bdd1243dSDimitry Andric options::OPT_fvisibility_ms_compat)) 2403bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=hidden"); 24040b57cec5SDimitry Andric } 24050b57cec5SDimitry Andric 24065ffd83dbSDimitry Andric void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 24075ffd83dbSDimitry Andric // Floating point operations and argument passing are hard. 24085ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 24095ffd83dbSDimitry Andric CmdArgs.push_back("hard"); 24105ffd83dbSDimitry Andric } 24115ffd83dbSDimitry Andric 24120b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, 24130b57cec5SDimitry Andric StringRef Target, const InputInfo &Output, 24140b57cec5SDimitry Andric const InputInfo &Input, const ArgList &Args) const { 24150b57cec5SDimitry Andric // If this is a dry run, do not create the compilation database file. 24160b57cec5SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 24170b57cec5SDimitry Andric return; 24180b57cec5SDimitry Andric 24190b57cec5SDimitry Andric using llvm::yaml::escape; 24200b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 24210b57cec5SDimitry Andric 24220b57cec5SDimitry Andric if (!CompilationDatabase) { 24230b57cec5SDimitry Andric std::error_code EC; 2424fe6060f1SDimitry Andric auto File = std::make_unique<llvm::raw_fd_ostream>( 242581ad6265SDimitry Andric Filename, EC, 242681ad6265SDimitry Andric llvm::sys::fs::OF_TextWithCRLF | llvm::sys::fs::OF_Append); 24270b57cec5SDimitry Andric if (EC) { 24280b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_compilationdatabase) << Filename 24290b57cec5SDimitry Andric << EC.message(); 24300b57cec5SDimitry Andric return; 24310b57cec5SDimitry Andric } 24320b57cec5SDimitry Andric CompilationDatabase = std::move(File); 24330b57cec5SDimitry Andric } 24340b57cec5SDimitry Andric auto &CDB = *CompilationDatabase; 2435a7dea167SDimitry Andric auto CWD = D.getVFS().getCurrentWorkingDirectory(); 2436a7dea167SDimitry Andric if (!CWD) 2437a7dea167SDimitry Andric CWD = "."; 2438a7dea167SDimitry Andric CDB << "{ \"directory\": \"" << escape(*CWD) << "\""; 24390b57cec5SDimitry Andric CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\""; 24400b57cec5SDimitry Andric CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; 24410b57cec5SDimitry Andric CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\""; 2442a7dea167SDimitry Andric SmallString<128> Buf; 24430b57cec5SDimitry Andric Buf = "-x"; 24440b57cec5SDimitry Andric Buf += types::getTypeName(Input.getType()); 24450b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 24460b57cec5SDimitry Andric if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) { 24470b57cec5SDimitry Andric Buf = "--sysroot="; 24480b57cec5SDimitry Andric Buf += D.SysRoot; 24490b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 24500b57cec5SDimitry Andric } 24510b57cec5SDimitry Andric CDB << ", \"" << escape(Input.getFilename()) << "\""; 245281ad6265SDimitry Andric CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\""; 24530b57cec5SDimitry Andric for (auto &A: Args) { 24540b57cec5SDimitry Andric auto &O = A->getOption(); 24550b57cec5SDimitry Andric // Skip language selection, which is positional. 24560b57cec5SDimitry Andric if (O.getID() == options::OPT_x) 24570b57cec5SDimitry Andric continue; 24580b57cec5SDimitry Andric // Skip writing dependency output and the compilation database itself. 24590b57cec5SDimitry Andric if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group) 24600b57cec5SDimitry Andric continue; 2461a7dea167SDimitry Andric if (O.getID() == options::OPT_gen_cdb_fragment_path) 2462a7dea167SDimitry Andric continue; 24630b57cec5SDimitry Andric // Skip inputs. 24640b57cec5SDimitry Andric if (O.getKind() == Option::InputClass) 24650b57cec5SDimitry Andric continue; 246681ad6265SDimitry Andric // Skip output. 246781ad6265SDimitry Andric if (O.getID() == options::OPT_o) 246881ad6265SDimitry Andric continue; 24690b57cec5SDimitry Andric // All other arguments are quoted and appended. 24700b57cec5SDimitry Andric ArgStringList ASL; 24710b57cec5SDimitry Andric A->render(Args, ASL); 24720b57cec5SDimitry Andric for (auto &it: ASL) 24730b57cec5SDimitry Andric CDB << ", \"" << escape(it) << "\""; 24740b57cec5SDimitry Andric } 24750b57cec5SDimitry Andric Buf = "--target="; 24760b57cec5SDimitry Andric Buf += Target; 24770b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\"]},\n"; 24780b57cec5SDimitry Andric } 24790b57cec5SDimitry Andric 2480a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir( 2481a7dea167SDimitry Andric StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output, 2482a7dea167SDimitry Andric const InputInfo &Input, const llvm::opt::ArgList &Args) const { 2483a7dea167SDimitry Andric // If this is a dry run, do not create the compilation database file. 2484a7dea167SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 2485a7dea167SDimitry Andric return; 2486a7dea167SDimitry Andric 2487a7dea167SDimitry Andric if (CompilationDatabase) 2488a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2489a7dea167SDimitry Andric 2490a7dea167SDimitry Andric SmallString<256> Path = Dir; 2491a7dea167SDimitry Andric const auto &Driver = C.getDriver(); 2492a7dea167SDimitry Andric Driver.getVFS().makeAbsolute(Path); 2493a7dea167SDimitry Andric auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true); 2494a7dea167SDimitry Andric if (Err) { 2495a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message(); 2496a7dea167SDimitry Andric return; 2497a7dea167SDimitry Andric } 2498a7dea167SDimitry Andric 2499a7dea167SDimitry Andric llvm::sys::path::append( 2500a7dea167SDimitry Andric Path, 2501a7dea167SDimitry Andric Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json"); 2502a7dea167SDimitry Andric int FD; 2503a7dea167SDimitry Andric SmallString<256> TempPath; 2504fe6060f1SDimitry Andric Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath, 2505fe6060f1SDimitry Andric llvm::sys::fs::OF_Text); 2506a7dea167SDimitry Andric if (Err) { 2507a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message(); 2508a7dea167SDimitry Andric return; 2509a7dea167SDimitry Andric } 2510a7dea167SDimitry Andric CompilationDatabase = 2511a7dea167SDimitry Andric std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true); 2512a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2513a7dea167SDimitry Andric } 2514a7dea167SDimitry Andric 2515fe6060f1SDimitry Andric static bool CheckARMImplicitITArg(StringRef Value) { 2516fe6060f1SDimitry Andric return Value == "always" || Value == "never" || Value == "arm" || 2517fe6060f1SDimitry Andric Value == "thumb"; 2518fe6060f1SDimitry Andric } 2519fe6060f1SDimitry Andric 2520fe6060f1SDimitry Andric static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs, 2521fe6060f1SDimitry Andric StringRef Value) { 2522fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 2523fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); 2524fe6060f1SDimitry Andric } 2525fe6060f1SDimitry Andric 25260b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C, 25270b57cec5SDimitry Andric const ArgList &Args, 25280b57cec5SDimitry Andric ArgStringList &CmdArgs, 25290b57cec5SDimitry Andric const Driver &D) { 25300b57cec5SDimitry Andric if (UseRelaxAll(C, Args)) 25310b57cec5SDimitry Andric CmdArgs.push_back("-mrelax-all"); 25320b57cec5SDimitry Andric 25330b57cec5SDimitry Andric // Only default to -mincremental-linker-compatible if we think we are 25340b57cec5SDimitry Andric // targeting the MSVC linker. 25350b57cec5SDimitry Andric bool DefaultIncrementalLinkerCompatible = 25360b57cec5SDimitry Andric C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); 25370b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mincremental_linker_compatible, 25380b57cec5SDimitry Andric options::OPT_mno_incremental_linker_compatible, 25390b57cec5SDimitry Andric DefaultIncrementalLinkerCompatible)) 25400b57cec5SDimitry Andric CmdArgs.push_back("-mincremental-linker-compatible"); 25410b57cec5SDimitry Andric 254281ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_dwarf_unwind_EQ); 254381ad6265SDimitry Andric 254406c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_femit_compact_unwind_non_canonical, 254506c3fb27SDimitry Andric options::OPT_fno_emit_compact_unwind_non_canonical); 254606c3fb27SDimitry Andric 2547a7dea167SDimitry Andric // If you add more args here, also add them to the block below that 2548a7dea167SDimitry Andric // starts with "// If CollectArgsForIntegratedAssembler() isn't called below". 2549a7dea167SDimitry Andric 25500b57cec5SDimitry Andric // When passing -I arguments to the assembler we sometimes need to 25510b57cec5SDimitry Andric // unconditionally take the next argument. For example, when parsing 25520b57cec5SDimitry Andric // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the 25530b57cec5SDimitry Andric // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo' 25540b57cec5SDimitry Andric // arg after parsing the '-I' arg. 25550b57cec5SDimitry Andric bool TakeNextArg = false; 25560b57cec5SDimitry Andric 25570b57cec5SDimitry Andric bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations(); 2558349cc55cSDimitry Andric bool UseNoExecStack = false; 25590b57cec5SDimitry Andric const char *MipsTargetFeature = nullptr; 2560fe6060f1SDimitry Andric StringRef ImplicitIt; 25610b57cec5SDimitry Andric for (const Arg *A : 2562fe6060f1SDimitry Andric Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler, 2563fe6060f1SDimitry Andric options::OPT_mimplicit_it_EQ)) { 25640b57cec5SDimitry Andric A->claim(); 25650b57cec5SDimitry Andric 2566fe6060f1SDimitry Andric if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) { 2567fe6060f1SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 2568fe6060f1SDimitry Andric case llvm::Triple::arm: 2569fe6060f1SDimitry Andric case llvm::Triple::armeb: 2570fe6060f1SDimitry Andric case llvm::Triple::thumb: 2571fe6060f1SDimitry Andric case llvm::Triple::thumbeb: 2572fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2573fe6060f1SDimitry Andric ImplicitIt = A->getValue(); 2574fe6060f1SDimitry Andric if (!CheckARMImplicitITArg(ImplicitIt)) 2575fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2576bdd1243dSDimitry Andric << A->getSpelling() << ImplicitIt; 2577fe6060f1SDimitry Andric continue; 2578fe6060f1SDimitry Andric default: 2579fe6060f1SDimitry Andric break; 2580fe6060f1SDimitry Andric } 2581fe6060f1SDimitry Andric } 2582fe6060f1SDimitry Andric 25830b57cec5SDimitry Andric for (StringRef Value : A->getValues()) { 25840b57cec5SDimitry Andric if (TakeNextArg) { 25850b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25860b57cec5SDimitry Andric TakeNextArg = false; 25870b57cec5SDimitry Andric continue; 25880b57cec5SDimitry Andric } 25890b57cec5SDimitry Andric 25900b57cec5SDimitry Andric if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() && 25910b57cec5SDimitry Andric Value == "-mbig-obj") 25920b57cec5SDimitry Andric continue; // LLVM handles bigobj automatically 25930b57cec5SDimitry Andric 25940b57cec5SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 25950b57cec5SDimitry Andric default: 25960b57cec5SDimitry Andric break; 2597bdd1243dSDimitry Andric case llvm::Triple::wasm32: 2598bdd1243dSDimitry Andric case llvm::Triple::wasm64: 2599bdd1243dSDimitry Andric if (Value == "--no-type-check") { 2600bdd1243dSDimitry Andric CmdArgs.push_back("-mno-type-check"); 2601bdd1243dSDimitry Andric continue; 2602bdd1243dSDimitry Andric } 2603bdd1243dSDimitry Andric break; 26040b57cec5SDimitry Andric case llvm::Triple::thumb: 26050b57cec5SDimitry Andric case llvm::Triple::thumbeb: 26060b57cec5SDimitry Andric case llvm::Triple::arm: 26070b57cec5SDimitry Andric case llvm::Triple::armeb: 2608fe6060f1SDimitry Andric if (Value.startswith("-mimplicit-it=")) { 2609fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2610fe6060f1SDimitry Andric ImplicitIt = Value.split("=").second; 2611fe6060f1SDimitry Andric if (CheckARMImplicitITArg(ImplicitIt)) 2612fe6060f1SDimitry Andric continue; 2613fe6060f1SDimitry Andric } 26140b57cec5SDimitry Andric if (Value == "-mthumb") 26150b57cec5SDimitry Andric // -mthumb has already been processed in ComputeLLVMTriple() 26160b57cec5SDimitry Andric // recognize but skip over here. 26170b57cec5SDimitry Andric continue; 26180b57cec5SDimitry Andric break; 26190b57cec5SDimitry Andric case llvm::Triple::mips: 26200b57cec5SDimitry Andric case llvm::Triple::mipsel: 26210b57cec5SDimitry Andric case llvm::Triple::mips64: 26220b57cec5SDimitry Andric case llvm::Triple::mips64el: 26230b57cec5SDimitry Andric if (Value == "--trap") { 26240b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26250b57cec5SDimitry Andric CmdArgs.push_back("+use-tcc-in-div"); 26260b57cec5SDimitry Andric continue; 26270b57cec5SDimitry Andric } 26280b57cec5SDimitry Andric if (Value == "--break") { 26290b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26300b57cec5SDimitry Andric CmdArgs.push_back("-use-tcc-in-div"); 26310b57cec5SDimitry Andric continue; 26320b57cec5SDimitry Andric } 26330b57cec5SDimitry Andric if (Value.startswith("-msoft-float")) { 26340b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26350b57cec5SDimitry Andric CmdArgs.push_back("+soft-float"); 26360b57cec5SDimitry Andric continue; 26370b57cec5SDimitry Andric } 26380b57cec5SDimitry Andric if (Value.startswith("-mhard-float")) { 26390b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26400b57cec5SDimitry Andric CmdArgs.push_back("-soft-float"); 26410b57cec5SDimitry Andric continue; 26420b57cec5SDimitry Andric } 26430b57cec5SDimitry Andric 26440b57cec5SDimitry Andric MipsTargetFeature = llvm::StringSwitch<const char *>(Value) 26450b57cec5SDimitry Andric .Case("-mips1", "+mips1") 26460b57cec5SDimitry Andric .Case("-mips2", "+mips2") 26470b57cec5SDimitry Andric .Case("-mips3", "+mips3") 26480b57cec5SDimitry Andric .Case("-mips4", "+mips4") 26490b57cec5SDimitry Andric .Case("-mips5", "+mips5") 26500b57cec5SDimitry Andric .Case("-mips32", "+mips32") 26510b57cec5SDimitry Andric .Case("-mips32r2", "+mips32r2") 26520b57cec5SDimitry Andric .Case("-mips32r3", "+mips32r3") 26530b57cec5SDimitry Andric .Case("-mips32r5", "+mips32r5") 26540b57cec5SDimitry Andric .Case("-mips32r6", "+mips32r6") 26550b57cec5SDimitry Andric .Case("-mips64", "+mips64") 26560b57cec5SDimitry Andric .Case("-mips64r2", "+mips64r2") 26570b57cec5SDimitry Andric .Case("-mips64r3", "+mips64r3") 26580b57cec5SDimitry Andric .Case("-mips64r5", "+mips64r5") 26590b57cec5SDimitry Andric .Case("-mips64r6", "+mips64r6") 26600b57cec5SDimitry Andric .Default(nullptr); 26610b57cec5SDimitry Andric if (MipsTargetFeature) 26620b57cec5SDimitry Andric continue; 26630b57cec5SDimitry Andric } 26640b57cec5SDimitry Andric 26650b57cec5SDimitry Andric if (Value == "-force_cpusubtype_ALL") { 26660b57cec5SDimitry Andric // Do nothing, this is the default and we don't support anything else. 26670b57cec5SDimitry Andric } else if (Value == "-L") { 26680b57cec5SDimitry Andric CmdArgs.push_back("-msave-temp-labels"); 26690b57cec5SDimitry Andric } else if (Value == "--fatal-warnings") { 26700b57cec5SDimitry Andric CmdArgs.push_back("-massembler-fatal-warnings"); 2671a7dea167SDimitry Andric } else if (Value == "--no-warn" || Value == "-W") { 2672a7dea167SDimitry Andric CmdArgs.push_back("-massembler-no-warn"); 26730b57cec5SDimitry Andric } else if (Value == "--noexecstack") { 26740b57cec5SDimitry Andric UseNoExecStack = true; 26750b57cec5SDimitry Andric } else if (Value.startswith("-compress-debug-sections") || 26760b57cec5SDimitry Andric Value.startswith("--compress-debug-sections") || 26770b57cec5SDimitry Andric Value == "-nocompress-debug-sections" || 26780b57cec5SDimitry Andric Value == "--nocompress-debug-sections") { 26790b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26800b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=yes" || 26810b57cec5SDimitry Andric Value == "--mrelax-relocations=yes") { 26820b57cec5SDimitry Andric UseRelaxRelocations = true; 26830b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=no" || 26840b57cec5SDimitry Andric Value == "--mrelax-relocations=no") { 26850b57cec5SDimitry Andric UseRelaxRelocations = false; 26860b57cec5SDimitry Andric } else if (Value.startswith("-I")) { 26870b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26880b57cec5SDimitry Andric // We need to consume the next argument if the current arg is a plain 26890b57cec5SDimitry Andric // -I. The next arg will be the include directory. 26900b57cec5SDimitry Andric if (Value == "-I") 26910b57cec5SDimitry Andric TakeNextArg = true; 26920b57cec5SDimitry Andric } else if (Value.startswith("-gdwarf-")) { 26930b57cec5SDimitry Andric // "-gdwarf-N" options are not cc1as options. 26940b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNum(Value); 26950b57cec5SDimitry Andric if (DwarfVersion == 0) { // Send it onward, and let cc1as complain. 26960b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26970b57cec5SDimitry Andric } else { 26980b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, 269906c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoConstructor, 27000b57cec5SDimitry Andric DwarfVersion, llvm::DebuggerKind::Default); 27010b57cec5SDimitry Andric } 27020b57cec5SDimitry Andric } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") || 27030b57cec5SDimitry Andric Value.startswith("-mhwdiv") || Value.startswith("-march")) { 27040b57cec5SDimitry Andric // Do nothing, we'll validate it later. 27050b57cec5SDimitry Andric } else if (Value == "-defsym") { 27060b57cec5SDimitry Andric if (A->getNumValues() != 2) { 27070b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << Value; 27080b57cec5SDimitry Andric break; 27090b57cec5SDimitry Andric } 27100b57cec5SDimitry Andric const char *S = A->getValue(1); 27110b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 27120b57cec5SDimitry Andric auto Sym = Pair.first; 27130b57cec5SDimitry Andric auto SVal = Pair.second; 27140b57cec5SDimitry Andric 27150b57cec5SDimitry Andric if (Sym.empty() || SVal.empty()) { 27160b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << S; 27170b57cec5SDimitry Andric break; 27180b57cec5SDimitry Andric } 27190b57cec5SDimitry Andric int64_t IVal; 27200b57cec5SDimitry Andric if (SVal.getAsInteger(0, IVal)) { 27210b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; 27220b57cec5SDimitry Andric break; 27230b57cec5SDimitry Andric } 27240b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 27250b57cec5SDimitry Andric TakeNextArg = true; 27260b57cec5SDimitry Andric } else if (Value == "-fdebug-compilation-dir") { 27270b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 27280b57cec5SDimitry Andric TakeNextArg = true; 2729480093f4SDimitry Andric } else if (Value.consume_front("-fdebug-compilation-dir=")) { 2730480093f4SDimitry Andric // The flag is a -Wa / -Xassembler argument and Options doesn't 2731480093f4SDimitry Andric // parse the argument, so this isn't automatically aliased to 2732480093f4SDimitry Andric // -fdebug-compilation-dir (without '=') here. 2733480093f4SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 2734480093f4SDimitry Andric CmdArgs.push_back(Value.data()); 2735fe6060f1SDimitry Andric } else if (Value == "--version") { 2736fe6060f1SDimitry Andric D.PrintVersion(C, llvm::outs()); 27370b57cec5SDimitry Andric } else { 27380b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2739bdd1243dSDimitry Andric << A->getSpelling() << Value; 27400b57cec5SDimitry Andric } 27410b57cec5SDimitry Andric } 27420b57cec5SDimitry Andric } 2743fe6060f1SDimitry Andric if (ImplicitIt.size()) 2744fe6060f1SDimitry Andric AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt); 2745bdd1243dSDimitry Andric if (!UseRelaxRelocations) 2746bdd1243dSDimitry Andric CmdArgs.push_back("-mrelax-relocations=no"); 27470b57cec5SDimitry Andric if (UseNoExecStack) 27480b57cec5SDimitry Andric CmdArgs.push_back("-mnoexecstack"); 27490b57cec5SDimitry Andric if (MipsTargetFeature != nullptr) { 27500b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 27510b57cec5SDimitry Andric CmdArgs.push_back(MipsTargetFeature); 27520b57cec5SDimitry Andric } 27530b57cec5SDimitry Andric 27540b57cec5SDimitry Andric // forward -fembed-bitcode to assmebler 27550b57cec5SDimitry Andric if (C.getDriver().embedBitcodeEnabled() || 27560b57cec5SDimitry Andric C.getDriver().embedBitcodeMarkerOnly()) 27570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 2758bdd1243dSDimitry Andric 2759bdd1243dSDimitry Andric if (const char *AsSecureLogFile = getenv("AS_SECURE_LOG_FILE")) { 2760bdd1243dSDimitry Andric CmdArgs.push_back("-as-secure-log-file"); 2761bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(AsSecureLogFile)); 2762bdd1243dSDimitry Andric } 27630b57cec5SDimitry Andric } 27640b57cec5SDimitry Andric 27650b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, 27660b57cec5SDimitry Andric bool OFastEnabled, const ArgList &Args, 27675ffd83dbSDimitry Andric ArgStringList &CmdArgs, 27685ffd83dbSDimitry Andric const JobAction &JA) { 27690b57cec5SDimitry Andric // Handle various floating point optimization flags, mapping them to the 27700b57cec5SDimitry Andric // appropriate LLVM code generation flags. This is complicated by several 27710b57cec5SDimitry Andric // "umbrella" flags, so we do this by stepping through the flags incrementally 27720b57cec5SDimitry Andric // adjusting what we think is enabled/disabled, then at the end setting the 27730b57cec5SDimitry Andric // LLVM flags based on the final state. 27740b57cec5SDimitry Andric bool HonorINFs = true; 27750b57cec5SDimitry Andric bool HonorNaNs = true; 2776349cc55cSDimitry Andric bool ApproxFunc = false; 27770b57cec5SDimitry Andric // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 27780b57cec5SDimitry Andric bool MathErrno = TC.IsMathErrnoDefault(); 27790b57cec5SDimitry Andric bool AssociativeMath = false; 27800b57cec5SDimitry Andric bool ReciprocalMath = false; 27810b57cec5SDimitry Andric bool SignedZeros = true; 2782480093f4SDimitry Andric bool TrappingMath = false; // Implemented via -ffp-exception-behavior 2783480093f4SDimitry Andric bool TrappingMathPresent = false; // Is trapping-math in args, and not 2784480093f4SDimitry Andric // overriden by ffp-exception-behavior? 2785480093f4SDimitry Andric bool RoundingFPMath = false; 2786480093f4SDimitry Andric bool RoundingMathPresent = false; // Is rounding-math in args? 2787480093f4SDimitry Andric // -ffp-model values: strict, fast, precise 2788480093f4SDimitry Andric StringRef FPModel = ""; 2789480093f4SDimitry Andric // -ffp-exception-behavior options: strict, maytrap, ignore 2790480093f4SDimitry Andric StringRef FPExceptionBehavior = ""; 279181ad6265SDimitry Andric // -ffp-eval-method options: double, extended, source 279281ad6265SDimitry Andric StringRef FPEvalMethod = ""; 27935ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFPMath = 27945ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA); 27955ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFP32Math = 27965ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle()); 27975ffd83dbSDimitry Andric 27985ffd83dbSDimitry Andric llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath; 27995ffd83dbSDimitry Andric llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math; 2800349cc55cSDimitry Andric // CUDA and HIP don't rely on the frontend to pass an ffp-contract option. 2801349cc55cSDimitry Andric // If one wasn't given by the user, don't pass it here. 2802349cc55cSDimitry Andric StringRef FPContract; 2803bdd1243dSDimitry Andric StringRef LastSeenFfpContractOption; 2804bdd1243dSDimitry Andric bool SeenUnsafeMathModeOption = false; 2805349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2806349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 2807349cc55cSDimitry Andric FPContract = "on"; 2808480093f4SDimitry Andric bool StrictFPModel = false; 2809bdd1243dSDimitry Andric StringRef Float16ExcessPrecision = ""; 281006c3fb27SDimitry Andric StringRef BFloat16ExcessPrecision = ""; 28110b57cec5SDimitry Andric 28120b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 28130b57cec5SDimitry Andric CmdArgs.push_back("-mlimit-float-precision"); 28140b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 28150b57cec5SDimitry Andric } 28160b57cec5SDimitry Andric 28170b57cec5SDimitry Andric for (const Arg *A : Args) { 2818480093f4SDimitry Andric auto optID = A->getOption().getID(); 2819480093f4SDimitry Andric bool PreciseFPModel = false; 2820480093f4SDimitry Andric switch (optID) { 2821480093f4SDimitry Andric default: 2822480093f4SDimitry Andric break; 2823480093f4SDimitry Andric case options::OPT_ffp_model_EQ: { 2824480093f4SDimitry Andric // If -ffp-model= is seen, reset to fno-fast-math 2825480093f4SDimitry Andric HonorINFs = true; 2826480093f4SDimitry Andric HonorNaNs = true; 2827bdd1243dSDimitry Andric ApproxFunc = false; 2828480093f4SDimitry Andric // Turning *off* -ffast-math restores the toolchain default. 2829480093f4SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 2830480093f4SDimitry Andric AssociativeMath = false; 2831480093f4SDimitry Andric ReciprocalMath = false; 2832480093f4SDimitry Andric SignedZeros = true; 2833480093f4SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 2834349cc55cSDimitry Andric FPContract = "on"; 28355ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 28365ffd83dbSDimitry Andric 28375ffd83dbSDimitry Andric // FIXME: The target may have picked a non-IEEE default mode here based on 28385ffd83dbSDimitry Andric // -cl-denorms-are-zero. Should the target consider -fp-model interaction? 28395ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 28405ffd83dbSDimitry Andric 2841480093f4SDimitry Andric StringRef Val = A->getValue(); 2842480093f4SDimitry Andric if (OFastEnabled && !Val.equals("fast")) { 2843480093f4SDimitry Andric // Only -ffp-model=fast is compatible with OFast, ignore. 2844480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2845480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val) 2846480093f4SDimitry Andric << "-Ofast"; 2847480093f4SDimitry Andric break; 2848480093f4SDimitry Andric } 2849480093f4SDimitry Andric StrictFPModel = false; 2850480093f4SDimitry Andric PreciseFPModel = true; 2851480093f4SDimitry Andric // ffp-model= is a Driver option, it is entirely rewritten into more 2852480093f4SDimitry Andric // granular options before being passed into cc1. 2853480093f4SDimitry Andric // Use the gcc option in the switch below. 2854349cc55cSDimitry Andric if (!FPModel.empty() && !FPModel.equals(Val)) 2855480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2856480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + FPModel) 2857480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val); 2858480093f4SDimitry Andric if (Val.equals("fast")) { 2859480093f4SDimitry Andric optID = options::OPT_ffast_math; 2860480093f4SDimitry Andric FPModel = Val; 28616e75b2fbSDimitry Andric FPContract = "fast"; 2862480093f4SDimitry Andric } else if (Val.equals("precise")) { 2863480093f4SDimitry Andric optID = options::OPT_ffp_contract; 2864480093f4SDimitry Andric FPModel = Val; 2865349cc55cSDimitry Andric FPContract = "on"; 2866480093f4SDimitry Andric PreciseFPModel = true; 2867480093f4SDimitry Andric } else if (Val.equals("strict")) { 2868480093f4SDimitry Andric StrictFPModel = true; 2869480093f4SDimitry Andric optID = options::OPT_frounding_math; 2870480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2871480093f4SDimitry Andric FPModel = Val; 28725ffd83dbSDimitry Andric FPContract = "off"; 2873480093f4SDimitry Andric TrappingMath = true; 2874480093f4SDimitry Andric } else 2875480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2876bdd1243dSDimitry Andric << A->getSpelling() << Val; 2877480093f4SDimitry Andric break; 2878480093f4SDimitry Andric } 2879480093f4SDimitry Andric } 2880480093f4SDimitry Andric 2881480093f4SDimitry Andric switch (optID) { 28820b57cec5SDimitry Andric // If this isn't an FP option skip the claim below 28830b57cec5SDimitry Andric default: continue; 28840b57cec5SDimitry Andric 28850b57cec5SDimitry Andric // Options controlling individual features 28860b57cec5SDimitry Andric case options::OPT_fhonor_infinities: HonorINFs = true; break; 28870b57cec5SDimitry Andric case options::OPT_fno_honor_infinities: HonorINFs = false; break; 28880b57cec5SDimitry Andric case options::OPT_fhonor_nans: HonorNaNs = true; break; 28890b57cec5SDimitry Andric case options::OPT_fno_honor_nans: HonorNaNs = false; break; 2890349cc55cSDimitry Andric case options::OPT_fapprox_func: ApproxFunc = true; break; 2891349cc55cSDimitry Andric case options::OPT_fno_approx_func: ApproxFunc = false; break; 28920b57cec5SDimitry Andric case options::OPT_fmath_errno: MathErrno = true; break; 28930b57cec5SDimitry Andric case options::OPT_fno_math_errno: MathErrno = false; break; 28940b57cec5SDimitry Andric case options::OPT_fassociative_math: AssociativeMath = true; break; 28950b57cec5SDimitry Andric case options::OPT_fno_associative_math: AssociativeMath = false; break; 28960b57cec5SDimitry Andric case options::OPT_freciprocal_math: ReciprocalMath = true; break; 28970b57cec5SDimitry Andric case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break; 28980b57cec5SDimitry Andric case options::OPT_fsigned_zeros: SignedZeros = true; break; 28990b57cec5SDimitry Andric case options::OPT_fno_signed_zeros: SignedZeros = false; break; 2900480093f4SDimitry Andric case options::OPT_ftrapping_math: 2901480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2902480093f4SDimitry Andric !FPExceptionBehavior.equals("strict")) 2903480093f4SDimitry Andric // Warn that previous value of option is overridden. 2904480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2905480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2906480093f4SDimitry Andric << "-ftrapping-math"; 2907480093f4SDimitry Andric TrappingMath = true; 2908480093f4SDimitry Andric TrappingMathPresent = true; 2909480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2910480093f4SDimitry Andric break; 2911480093f4SDimitry Andric case options::OPT_fno_trapping_math: 2912480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2913480093f4SDimitry Andric !FPExceptionBehavior.equals("ignore")) 2914480093f4SDimitry Andric // Warn that previous value of option is overridden. 2915480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2916480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2917480093f4SDimitry Andric << "-fno-trapping-math"; 2918480093f4SDimitry Andric TrappingMath = false; 2919480093f4SDimitry Andric TrappingMathPresent = true; 2920480093f4SDimitry Andric FPExceptionBehavior = "ignore"; 2921480093f4SDimitry Andric break; 2922480093f4SDimitry Andric 2923480093f4SDimitry Andric case options::OPT_frounding_math: 2924480093f4SDimitry Andric RoundingFPMath = true; 2925480093f4SDimitry Andric RoundingMathPresent = true; 2926480093f4SDimitry Andric break; 2927480093f4SDimitry Andric 2928480093f4SDimitry Andric case options::OPT_fno_rounding_math: 2929480093f4SDimitry Andric RoundingFPMath = false; 2930480093f4SDimitry Andric RoundingMathPresent = false; 2931480093f4SDimitry Andric break; 29320b57cec5SDimitry Andric 29330b57cec5SDimitry Andric case options::OPT_fdenormal_fp_math_EQ: 29345ffd83dbSDimitry Andric DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue()); 293581ad6265SDimitry Andric DenormalFP32Math = DenormalFPMath; 29365ffd83dbSDimitry Andric if (!DenormalFPMath.isValid()) { 29375ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 29385ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 29395ffd83dbSDimitry Andric } 29405ffd83dbSDimitry Andric break; 29415ffd83dbSDimitry Andric 29425ffd83dbSDimitry Andric case options::OPT_fdenormal_fp_math_f32_EQ: 29435ffd83dbSDimitry Andric DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue()); 29445ffd83dbSDimitry Andric if (!DenormalFP32Math.isValid()) { 29455ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 29465ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 29475ffd83dbSDimitry Andric } 29480b57cec5SDimitry Andric break; 29490b57cec5SDimitry Andric 2950480093f4SDimitry Andric // Validate and pass through -ffp-contract option. 29510b57cec5SDimitry Andric case options::OPT_ffp_contract: { 29520b57cec5SDimitry Andric StringRef Val = A->getValue(); 2953480093f4SDimitry Andric if (PreciseFPModel) { 2954349cc55cSDimitry Andric // -ffp-model=precise enables ffp-contract=on. 2955349cc55cSDimitry Andric // -ffp-model=precise sets PreciseFPModel to on and Val to 2956349cc55cSDimitry Andric // "precise". FPContract is set. 2957480093f4SDimitry Andric ; 2958bdd1243dSDimitry Andric } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off")) { 29590b57cec5SDimitry Andric FPContract = Val; 2960bdd1243dSDimitry Andric LastSeenFfpContractOption = Val; 2961bdd1243dSDimitry Andric } else 29620b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2963bdd1243dSDimitry Andric << A->getSpelling() << Val; 29640b57cec5SDimitry Andric break; 29650b57cec5SDimitry Andric } 29660b57cec5SDimitry Andric 2967480093f4SDimitry Andric // Validate and pass through -ffp-model option. 2968480093f4SDimitry Andric case options::OPT_ffp_model_EQ: 2969480093f4SDimitry Andric // This should only occur in the error case 2970480093f4SDimitry Andric // since the optID has been replaced by a more granular 2971480093f4SDimitry Andric // floating point option. 2972480093f4SDimitry Andric break; 2973480093f4SDimitry Andric 2974480093f4SDimitry Andric // Validate and pass through -ffp-exception-behavior option. 2975480093f4SDimitry Andric case options::OPT_ffp_exception_behavior_EQ: { 2976480093f4SDimitry Andric StringRef Val = A->getValue(); 2977480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2978480093f4SDimitry Andric !FPExceptionBehavior.equals(Val)) 2979480093f4SDimitry Andric // Warn that previous value of option is overridden. 2980480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2981480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2982480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + Val); 2983480093f4SDimitry Andric TrappingMath = TrappingMathPresent = false; 2984480093f4SDimitry Andric if (Val.equals("ignore") || Val.equals("maytrap")) 2985480093f4SDimitry Andric FPExceptionBehavior = Val; 2986480093f4SDimitry Andric else if (Val.equals("strict")) { 2987480093f4SDimitry Andric FPExceptionBehavior = Val; 2988480093f4SDimitry Andric TrappingMath = TrappingMathPresent = true; 2989480093f4SDimitry Andric } else 2990480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2991bdd1243dSDimitry Andric << A->getSpelling() << Val; 2992480093f4SDimitry Andric break; 2993480093f4SDimitry Andric } 2994480093f4SDimitry Andric 299581ad6265SDimitry Andric // Validate and pass through -ffp-eval-method option. 299681ad6265SDimitry Andric case options::OPT_ffp_eval_method_EQ: { 299781ad6265SDimitry Andric StringRef Val = A->getValue(); 299881ad6265SDimitry Andric if (Val.equals("double") || Val.equals("extended") || 299981ad6265SDimitry Andric Val.equals("source")) 300081ad6265SDimitry Andric FPEvalMethod = Val; 300181ad6265SDimitry Andric else 300281ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 3003bdd1243dSDimitry Andric << A->getSpelling() << Val; 300481ad6265SDimitry Andric break; 300581ad6265SDimitry Andric } 300681ad6265SDimitry Andric 3007bdd1243dSDimitry Andric case options::OPT_fexcess_precision_EQ: { 3008bdd1243dSDimitry Andric StringRef Val = A->getValue(); 3009bdd1243dSDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 3010bdd1243dSDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 3011bdd1243dSDimitry Andric if (Val.equals("standard") || Val.equals("fast")) 3012bdd1243dSDimitry Andric Float16ExcessPrecision = Val; 3013bdd1243dSDimitry Andric // To make it GCC compatible, allow the value of "16" which 3014bdd1243dSDimitry Andric // means disable excess precision, the same meaning than clang's 3015bdd1243dSDimitry Andric // equivalent value "none". 3016bdd1243dSDimitry Andric else if (Val.equals("16")) 3017bdd1243dSDimitry Andric Float16ExcessPrecision = "none"; 3018bdd1243dSDimitry Andric else 3019bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 3020bdd1243dSDimitry Andric << A->getSpelling() << Val; 3021bdd1243dSDimitry Andric } else { 3022bdd1243dSDimitry Andric if (!(Val.equals("standard") || Val.equals("fast"))) 3023bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 3024bdd1243dSDimitry Andric << A->getSpelling() << Val; 3025bdd1243dSDimitry Andric } 302606c3fb27SDimitry Andric BFloat16ExcessPrecision = Float16ExcessPrecision; 3027bdd1243dSDimitry Andric break; 3028bdd1243dSDimitry Andric } 30290b57cec5SDimitry Andric case options::OPT_ffinite_math_only: 30300b57cec5SDimitry Andric HonorINFs = false; 30310b57cec5SDimitry Andric HonorNaNs = false; 30320b57cec5SDimitry Andric break; 30330b57cec5SDimitry Andric case options::OPT_fno_finite_math_only: 30340b57cec5SDimitry Andric HonorINFs = true; 30350b57cec5SDimitry Andric HonorNaNs = true; 30360b57cec5SDimitry Andric break; 30370b57cec5SDimitry Andric 30380b57cec5SDimitry Andric case options::OPT_funsafe_math_optimizations: 30390b57cec5SDimitry Andric AssociativeMath = true; 30400b57cec5SDimitry Andric ReciprocalMath = true; 30410b57cec5SDimitry Andric SignedZeros = false; 304204eeddc0SDimitry Andric ApproxFunc = true; 30430b57cec5SDimitry Andric TrappingMath = false; 3044480093f4SDimitry Andric FPExceptionBehavior = ""; 3045bdd1243dSDimitry Andric FPContract = "fast"; 3046bdd1243dSDimitry Andric SeenUnsafeMathModeOption = true; 30470b57cec5SDimitry Andric break; 30480b57cec5SDimitry Andric case options::OPT_fno_unsafe_math_optimizations: 30490b57cec5SDimitry Andric AssociativeMath = false; 30500b57cec5SDimitry Andric ReciprocalMath = false; 30510b57cec5SDimitry Andric SignedZeros = true; 305204eeddc0SDimitry Andric ApproxFunc = false; 30530b57cec5SDimitry Andric TrappingMath = true; 3054480093f4SDimitry Andric FPExceptionBehavior = "strict"; 30555ffd83dbSDimitry Andric 30565ffd83dbSDimitry Andric // The target may have opted to flush by default, so force IEEE. 30575ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 30585ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 3059bdd1243dSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 3060bdd1243dSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) { 3061bdd1243dSDimitry Andric if (LastSeenFfpContractOption != "") { 3062bdd1243dSDimitry Andric FPContract = LastSeenFfpContractOption; 3063bdd1243dSDimitry Andric } else if (SeenUnsafeMathModeOption) 3064bdd1243dSDimitry Andric FPContract = "on"; 3065bdd1243dSDimitry Andric } 30660b57cec5SDimitry Andric break; 30670b57cec5SDimitry Andric 30680b57cec5SDimitry Andric case options::OPT_Ofast: 30690b57cec5SDimitry Andric // If -Ofast is the optimization level, then -ffast-math should be enabled 30700b57cec5SDimitry Andric if (!OFastEnabled) 30710b57cec5SDimitry Andric continue; 3072bdd1243dSDimitry Andric [[fallthrough]]; 30730b57cec5SDimitry Andric case options::OPT_ffast_math: 30740b57cec5SDimitry Andric HonorINFs = false; 30750b57cec5SDimitry Andric HonorNaNs = false; 30760b57cec5SDimitry Andric MathErrno = false; 30770b57cec5SDimitry Andric AssociativeMath = true; 30780b57cec5SDimitry Andric ReciprocalMath = true; 307904eeddc0SDimitry Andric ApproxFunc = true; 30800b57cec5SDimitry Andric SignedZeros = false; 30810b57cec5SDimitry Andric TrappingMath = false; 3082480093f4SDimitry Andric RoundingFPMath = false; 3083bdd1243dSDimitry Andric FPExceptionBehavior = ""; 30840b57cec5SDimitry Andric // If fast-math is set then set the fp-contract mode to fast. 30850b57cec5SDimitry Andric FPContract = "fast"; 3086bdd1243dSDimitry Andric SeenUnsafeMathModeOption = true; 30870b57cec5SDimitry Andric break; 30880b57cec5SDimitry Andric case options::OPT_fno_fast_math: 30890b57cec5SDimitry Andric HonorINFs = true; 30900b57cec5SDimitry Andric HonorNaNs = true; 30910b57cec5SDimitry Andric // Turning on -ffast-math (with either flag) removes the need for 30920b57cec5SDimitry Andric // MathErrno. However, turning *off* -ffast-math merely restores the 30930b57cec5SDimitry Andric // toolchain default (which may be false). 30940b57cec5SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 30950b57cec5SDimitry Andric AssociativeMath = false; 30960b57cec5SDimitry Andric ReciprocalMath = false; 309704eeddc0SDimitry Andric ApproxFunc = false; 30980b57cec5SDimitry Andric SignedZeros = true; 30990b57cec5SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 31005ffd83dbSDimitry Andric DenormalFPMath = DefaultDenormalFPMath; 31015ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 3102349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 3103bdd1243dSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) { 3104bdd1243dSDimitry Andric if (LastSeenFfpContractOption != "") { 3105bdd1243dSDimitry Andric FPContract = LastSeenFfpContractOption; 3106bdd1243dSDimitry Andric } else if (SeenUnsafeMathModeOption) 3107349cc55cSDimitry Andric FPContract = "on"; 3108349cc55cSDimitry Andric } 31090b57cec5SDimitry Andric break; 31100b57cec5SDimitry Andric } 3111480093f4SDimitry Andric if (StrictFPModel) { 3112480093f4SDimitry Andric // If -ffp-model=strict has been specified on command line but 3113480093f4SDimitry Andric // subsequent options conflict then emit warning diagnostic. 3114349cc55cSDimitry Andric if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath && 311504eeddc0SDimitry Andric SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc && 31165ffd83dbSDimitry Andric DenormalFPMath == llvm::DenormalMode::getIEEE() && 3117349cc55cSDimitry Andric DenormalFP32Math == llvm::DenormalMode::getIEEE() && 3118349cc55cSDimitry Andric FPContract.equals("off")) 3119480093f4SDimitry Andric // OK: Current Arg doesn't conflict with -ffp-model=strict 3120480093f4SDimitry Andric ; 3121480093f4SDimitry Andric else { 3122480093f4SDimitry Andric StrictFPModel = false; 3123480093f4SDimitry Andric FPModel = ""; 3124bdd1243dSDimitry Andric auto RHS = (A->getNumValues() == 0) 3125bdd1243dSDimitry Andric ? A->getSpelling() 3126bdd1243dSDimitry Andric : Args.MakeArgString(A->getSpelling() + A->getValue()); 3127bdd1243dSDimitry Andric if (RHS != "-ffp-model=strict") 3128480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3129bdd1243dSDimitry Andric << "-ffp-model=strict" << RHS; 3130480093f4SDimitry Andric } 3131480093f4SDimitry Andric } 31320b57cec5SDimitry Andric 31330b57cec5SDimitry Andric // If we handled this option claim it 31340b57cec5SDimitry Andric A->claim(); 31350b57cec5SDimitry Andric } 31360b57cec5SDimitry Andric 31370b57cec5SDimitry Andric if (!HonorINFs) 31380b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-infs"); 31390b57cec5SDimitry Andric 31400b57cec5SDimitry Andric if (!HonorNaNs) 31410b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-nans"); 31420b57cec5SDimitry Andric 3143349cc55cSDimitry Andric if (ApproxFunc) 3144349cc55cSDimitry Andric CmdArgs.push_back("-fapprox-func"); 3145349cc55cSDimitry Andric 31460b57cec5SDimitry Andric if (MathErrno) 31470b57cec5SDimitry Andric CmdArgs.push_back("-fmath-errno"); 31480b57cec5SDimitry Andric 3149bdd1243dSDimitry Andric if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc && 3150bdd1243dSDimitry Andric !TrappingMath) 3151bdd1243dSDimitry Andric CmdArgs.push_back("-funsafe-math-optimizations"); 31520b57cec5SDimitry Andric 31530b57cec5SDimitry Andric if (!SignedZeros) 31540b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-zeros"); 31550b57cec5SDimitry Andric 31560b57cec5SDimitry Andric if (AssociativeMath && !SignedZeros && !TrappingMath) 31570b57cec5SDimitry Andric CmdArgs.push_back("-mreassociate"); 31580b57cec5SDimitry Andric 31590b57cec5SDimitry Andric if (ReciprocalMath) 31600b57cec5SDimitry Andric CmdArgs.push_back("-freciprocal-math"); 31610b57cec5SDimitry Andric 3162480093f4SDimitry Andric if (TrappingMath) { 3163480093f4SDimitry Andric // FP Exception Behavior is also set to strict 3164480093f4SDimitry Andric assert(FPExceptionBehavior.equals("strict")); 3165e8d8bef9SDimitry Andric } 31660b57cec5SDimitry Andric 31675ffd83dbSDimitry Andric // The default is IEEE. 31685ffd83dbSDimitry Andric if (DenormalFPMath != llvm::DenormalMode::getIEEE()) { 31695ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 31705ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 31715ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math=" << DenormalFPMath; 31725ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 31735ffd83dbSDimitry Andric } 31745ffd83dbSDimitry Andric 31755ffd83dbSDimitry Andric // Add f32 specific denormal mode flag if it's different. 31765ffd83dbSDimitry Andric if (DenormalFP32Math != DenormalFPMath) { 31775ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 31785ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 31795ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math; 31805ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 31815ffd83dbSDimitry Andric } 31820b57cec5SDimitry Andric 31830b57cec5SDimitry Andric if (!FPContract.empty()) 31840b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract)); 31850b57cec5SDimitry Andric 3186480093f4SDimitry Andric if (!RoundingFPMath) 3187480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); 3188480093f4SDimitry Andric 3189480093f4SDimitry Andric if (RoundingFPMath && RoundingMathPresent) 3190480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frounding-math")); 3191480093f4SDimitry Andric 3192480093f4SDimitry Andric if (!FPExceptionBehavior.empty()) 3193480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" + 3194480093f4SDimitry Andric FPExceptionBehavior)); 3195480093f4SDimitry Andric 319681ad6265SDimitry Andric if (!FPEvalMethod.empty()) 319781ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-eval-method=" + FPEvalMethod)); 319881ad6265SDimitry Andric 3199bdd1243dSDimitry Andric if (!Float16ExcessPrecision.empty()) 3200bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffloat16-excess-precision=" + 3201bdd1243dSDimitry Andric Float16ExcessPrecision)); 320206c3fb27SDimitry Andric if (!BFloat16ExcessPrecision.empty()) 320306c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fbfloat16-excess-precision=" + 320406c3fb27SDimitry Andric BFloat16ExcessPrecision)); 3205bdd1243dSDimitry Andric 32060b57cec5SDimitry Andric ParseMRecip(D, Args, CmdArgs); 32070b57cec5SDimitry Andric 32080b57cec5SDimitry Andric // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the 32090b57cec5SDimitry Andric // individual features enabled by -ffast-math instead of the option itself as 32100b57cec5SDimitry Andric // that's consistent with gcc's behaviour. 321104eeddc0SDimitry Andric if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc && 3212480093f4SDimitry Andric ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) { 32130b57cec5SDimitry Andric CmdArgs.push_back("-ffast-math"); 3214480093f4SDimitry Andric if (FPModel.equals("fast")) { 3215480093f4SDimitry Andric if (FPContract.equals("fast")) 3216480093f4SDimitry Andric // All set, do nothing. 3217480093f4SDimitry Andric ; 3218480093f4SDimitry Andric else if (FPContract.empty()) 3219480093f4SDimitry Andric // Enable -ffp-contract=fast 3220480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 3221480093f4SDimitry Andric else 3222480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3223480093f4SDimitry Andric << "-ffp-model=fast" 3224480093f4SDimitry Andric << Args.MakeArgString("-ffp-contract=" + FPContract); 3225480093f4SDimitry Andric } 3226480093f4SDimitry Andric } 32270b57cec5SDimitry Andric 32280b57cec5SDimitry Andric // Handle __FINITE_MATH_ONLY__ similarly. 32290b57cec5SDimitry Andric if (!HonorINFs && !HonorNaNs) 32300b57cec5SDimitry Andric CmdArgs.push_back("-ffinite-math-only"); 32310b57cec5SDimitry Andric 32320b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) { 32330b57cec5SDimitry Andric CmdArgs.push_back("-mfpmath"); 32340b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 32350b57cec5SDimitry Andric } 32360b57cec5SDimitry Andric 32370b57cec5SDimitry Andric // Disable a codegen optimization for floating-point casts. 32380b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow, 32390b57cec5SDimitry Andric options::OPT_fstrict_float_cast_overflow, false)) 32400b57cec5SDimitry Andric CmdArgs.push_back("-fno-strict-float-cast-overflow"); 32410b57cec5SDimitry Andric } 32420b57cec5SDimitry Andric 32430b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, 32440b57cec5SDimitry Andric const llvm::Triple &Triple, 32450b57cec5SDimitry Andric const InputInfo &Input) { 32460b57cec5SDimitry Andric // Add default argument set. 32470b57cec5SDimitry Andric if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 32480b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=core"); 32490b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=apiModeling"); 32500b57cec5SDimitry Andric 32510b57cec5SDimitry Andric if (!Triple.isWindowsMSVCEnvironment()) { 32520b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix"); 32530b57cec5SDimitry Andric } else { 32540b57cec5SDimitry Andric // Enable "unix" checkers that also work on Windows. 32550b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.API"); 32560b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.Malloc"); 32570b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof"); 32580b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator"); 32590b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg"); 32600b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg"); 32610b57cec5SDimitry Andric } 32620b57cec5SDimitry Andric 326381ad6265SDimitry Andric // Disable some unix checkers for PS4/PS5. 326481ad6265SDimitry Andric if (Triple.isPS()) { 32650b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.API"); 32660b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork"); 32670b57cec5SDimitry Andric } 32680b57cec5SDimitry Andric 3269480093f4SDimitry Andric if (Triple.isOSDarwin()) { 32700b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=osx"); 3271480093f4SDimitry Andric CmdArgs.push_back( 3272480093f4SDimitry Andric "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType"); 3273480093f4SDimitry Andric } 3274480093f4SDimitry Andric else if (Triple.isOSFuchsia()) 3275480093f4SDimitry Andric CmdArgs.push_back("-analyzer-checker=fuchsia"); 32760b57cec5SDimitry Andric 32770b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=deadcode"); 32780b57cec5SDimitry Andric 32790b57cec5SDimitry Andric if (types::isCXX(Input.getType())) 32800b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=cplusplus"); 32810b57cec5SDimitry Andric 328281ad6265SDimitry Andric if (!Triple.isPS()) { 32830b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 32840b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 32850b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 32860b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 32870b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 32880b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 32890b57cec5SDimitry Andric } 32900b57cec5SDimitry Andric 32910b57cec5SDimitry Andric // Default nullability checks. 32920b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull"); 32930b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull"); 32940b57cec5SDimitry Andric } 32950b57cec5SDimitry Andric 32960b57cec5SDimitry Andric // Set the output format. The default is plist, for (lame) historical reasons. 32970b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-output"); 32980b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 32990b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 33000b57cec5SDimitry Andric else 33010b57cec5SDimitry Andric CmdArgs.push_back("plist"); 33020b57cec5SDimitry Andric 33030b57cec5SDimitry Andric // Disable the presentation of standard compiler warnings when using 33040b57cec5SDimitry Andric // --analyze. We only want to show static analyzer diagnostics or frontend 33050b57cec5SDimitry Andric // errors. 33060b57cec5SDimitry Andric CmdArgs.push_back("-w"); 33070b57cec5SDimitry Andric 33080b57cec5SDimitry Andric // Add -Xanalyzer arguments when running as analyzer. 33090b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 33100b57cec5SDimitry Andric } 33110b57cec5SDimitry Andric 3312753f127fSDimitry Andric static bool isValidSymbolName(StringRef S) { 3313753f127fSDimitry Andric if (S.empty()) 3314753f127fSDimitry Andric return false; 3315753f127fSDimitry Andric 3316753f127fSDimitry Andric if (std::isdigit(S[0])) 3317753f127fSDimitry Andric return false; 3318753f127fSDimitry Andric 3319753f127fSDimitry Andric return llvm::all_of(S, [](char C) { return std::isalnum(C) || C == '_'; }); 3320753f127fSDimitry Andric } 3321753f127fSDimitry Andric 3322e8d8bef9SDimitry Andric static void RenderSSPOptions(const Driver &D, const ToolChain &TC, 3323e8d8bef9SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3324e8d8bef9SDimitry Andric bool KernelOrKext) { 33250b57cec5SDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 33260b57cec5SDimitry Andric 33270b57cec5SDimitry Andric // NVPTX doesn't support stack protectors; from the compiler's perspective, it 33280b57cec5SDimitry Andric // doesn't even have a stack! 33290b57cec5SDimitry Andric if (EffectiveTriple.isNVPTX()) 33300b57cec5SDimitry Andric return; 33310b57cec5SDimitry Andric 33320b57cec5SDimitry Andric // -stack-protector=0 is default. 3333e8d8bef9SDimitry Andric LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff; 3334e8d8bef9SDimitry Andric LangOptions::StackProtectorMode DefaultStackProtectorLevel = 33350b57cec5SDimitry Andric TC.GetDefaultStackProtectorLevel(KernelOrKext); 33360b57cec5SDimitry Andric 33370b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 33380b57cec5SDimitry Andric options::OPT_fstack_protector_all, 33390b57cec5SDimitry Andric options::OPT_fstack_protector_strong, 33400b57cec5SDimitry Andric options::OPT_fstack_protector)) { 33410b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fstack_protector)) 33420b57cec5SDimitry Andric StackProtectorLevel = 3343e8d8bef9SDimitry Andric std::max<>(LangOptions::SSPOn, DefaultStackProtectorLevel); 33440b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_strong)) 33450b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPStrong; 33460b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_all)) 33470b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPReq; 3348bdd1243dSDimitry Andric 3349bdd1243dSDimitry Andric if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) { 3350bdd1243dSDimitry Andric D.Diag(diag::warn_drv_unsupported_option_for_target) 3351bdd1243dSDimitry Andric << A->getSpelling() << EffectiveTriple.getTriple(); 3352bdd1243dSDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 3353bdd1243dSDimitry Andric } 33540b57cec5SDimitry Andric } else { 33550b57cec5SDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 33560b57cec5SDimitry Andric } 33570b57cec5SDimitry Andric 33580b57cec5SDimitry Andric if (StackProtectorLevel) { 33590b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 33600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 33610b57cec5SDimitry Andric } 33620b57cec5SDimitry Andric 33630b57cec5SDimitry Andric // --param ssp-buffer-size= 33640b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__param)) { 33650b57cec5SDimitry Andric StringRef Str(A->getValue()); 33660b57cec5SDimitry Andric if (Str.startswith("ssp-buffer-size=")) { 33670b57cec5SDimitry Andric if (StackProtectorLevel) { 33680b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector-buffer-size"); 33690b57cec5SDimitry Andric // FIXME: Verify the argument is a valid integer. 33700b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 33710b57cec5SDimitry Andric } 33720b57cec5SDimitry Andric A->claim(); 33730b57cec5SDimitry Andric } 33740b57cec5SDimitry Andric } 3375e8d8bef9SDimitry Andric 3376e8d8bef9SDimitry Andric const std::string &TripleStr = EffectiveTriple.getTriple(); 3377e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) { 3378e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3379349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3380349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3381e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3382e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3383349cc55cSDimitry Andric if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() || 3384349cc55cSDimitry Andric EffectiveTriple.isThumb()) && 3385349cc55cSDimitry Andric Value != "tls" && Value != "global") { 3386e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3387fe6060f1SDimitry Andric << A->getOption().getName() << Value << "tls global"; 3388fe6060f1SDimitry Andric return; 3389fe6060f1SDimitry Andric } 3390349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3391349cc55cSDimitry Andric Value == "tls") { 3392349cc55cSDimitry Andric if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3393349cc55cSDimitry Andric D.Diag(diag::err_drv_ssp_missing_offset_argument) 3394349cc55cSDimitry Andric << A->getAsString(Args); 3395349cc55cSDimitry Andric return; 3396349cc55cSDimitry Andric } 3397349cc55cSDimitry Andric // Check whether the target subarch supports the hardware TLS register 339804eeddc0SDimitry Andric if (!arm::isHardTPSupported(EffectiveTriple)) { 3399349cc55cSDimitry Andric D.Diag(diag::err_target_unsupported_tp_hard) 3400349cc55cSDimitry Andric << EffectiveTriple.getArchName(); 3401349cc55cSDimitry Andric return; 3402349cc55cSDimitry Andric } 3403349cc55cSDimitry Andric // Check whether the user asked for something other than -mtp=cp15 3404349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 3405349cc55cSDimitry Andric StringRef Value = A->getValue(); 3406349cc55cSDimitry Andric if (Value != "cp15") { 3407349cc55cSDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 3408349cc55cSDimitry Andric << A->getAsString(Args) << "-mstack-protector-guard=tls"; 3409349cc55cSDimitry Andric return; 3410349cc55cSDimitry Andric } 3411349cc55cSDimitry Andric } 3412349cc55cSDimitry Andric CmdArgs.push_back("-target-feature"); 341306c3fb27SDimitry Andric CmdArgs.push_back("+read-tp-tpidruro"); 3414349cc55cSDimitry Andric } 3415fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") { 3416fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3417fe6060f1SDimitry Andric << A->getOption().getName() << Value << "sysreg global"; 3418e8d8bef9SDimitry Andric return; 3419e8d8bef9SDimitry Andric } 3420e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3421e8d8bef9SDimitry Andric } 3422e8d8bef9SDimitry Andric 3423e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3424e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3425349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3426349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3427e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3428e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3429fe6060f1SDimitry Andric int Offset; 3430e8d8bef9SDimitry Andric if (Value.getAsInteger(10, Offset)) { 3431e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3432e8d8bef9SDimitry Andric return; 3433e8d8bef9SDimitry Andric } 3434349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3435349cc55cSDimitry Andric (Offset < 0 || Offset > 0xfffff)) { 3436349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 3437349cc55cSDimitry Andric << A->getOption().getName() << Value; 3438349cc55cSDimitry Andric return; 3439349cc55cSDimitry Andric } 3440e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3441e8d8bef9SDimitry Andric } 3442e8d8bef9SDimitry Andric 3443e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) { 3444e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3445fe6060f1SDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) 3446e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3447e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3448e8d8bef9SDimitry Andric if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) { 3449e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3450fe6060f1SDimitry Andric << A->getOption().getName() << Value << "fs gs"; 3451fe6060f1SDimitry Andric return; 3452fe6060f1SDimitry Andric } 3453fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sp_el0") { 3454fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3455e8d8bef9SDimitry Andric return; 3456e8d8bef9SDimitry Andric } 3457e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3458e8d8bef9SDimitry Andric } 3459753f127fSDimitry Andric 3460753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_symbol_EQ)) { 3461753f127fSDimitry Andric StringRef Value = A->getValue(); 3462753f127fSDimitry Andric if (!isValidSymbolName(Value)) { 3463753f127fSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3464753f127fSDimitry Andric << A->getOption().getName() << "legal symbol name"; 3465753f127fSDimitry Andric return; 3466753f127fSDimitry Andric } 3467753f127fSDimitry Andric A->render(Args, CmdArgs); 3468753f127fSDimitry Andric } 34690b57cec5SDimitry Andric } 34700b57cec5SDimitry Andric 34715ffd83dbSDimitry Andric static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, 34725ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 34735ffd83dbSDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 34745ffd83dbSDimitry Andric 34753063e1e5SEd Maste if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux()) 34765ffd83dbSDimitry Andric return; 34775ffd83dbSDimitry Andric 34785ffd83dbSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && 34795ffd83dbSDimitry Andric !EffectiveTriple.isPPC64()) 34805ffd83dbSDimitry Andric return; 34815ffd83dbSDimitry Andric 348281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection, 348381ad6265SDimitry Andric options::OPT_fno_stack_clash_protection); 34845ffd83dbSDimitry Andric } 34855ffd83dbSDimitry Andric 34860b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D, 34870b57cec5SDimitry Andric const ToolChain &TC, 34880b57cec5SDimitry Andric const ArgList &Args, 34890b57cec5SDimitry Andric ArgStringList &CmdArgs) { 34900b57cec5SDimitry Andric auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit(); 34910b57cec5SDimitry Andric StringRef TrivialAutoVarInit = ""; 34920b57cec5SDimitry Andric 34930b57cec5SDimitry Andric for (const Arg *A : Args) { 34940b57cec5SDimitry Andric switch (A->getOption().getID()) { 34950b57cec5SDimitry Andric default: 34960b57cec5SDimitry Andric continue; 34970b57cec5SDimitry Andric case options::OPT_ftrivial_auto_var_init: { 34980b57cec5SDimitry Andric A->claim(); 34990b57cec5SDimitry Andric StringRef Val = A->getValue(); 35000b57cec5SDimitry Andric if (Val == "uninitialized" || Val == "zero" || Val == "pattern") 35010b57cec5SDimitry Andric TrivialAutoVarInit = Val; 35020b57cec5SDimitry Andric else 35030b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 3504bdd1243dSDimitry Andric << A->getSpelling() << Val; 35050b57cec5SDimitry Andric break; 35060b57cec5SDimitry Andric } 35070b57cec5SDimitry Andric } 35080b57cec5SDimitry Andric } 35090b57cec5SDimitry Andric 35100b57cec5SDimitry Andric if (TrivialAutoVarInit.empty()) 35110b57cec5SDimitry Andric switch (DefaultTrivialAutoVarInit) { 35120b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Uninitialized: 35130b57cec5SDimitry Andric break; 35140b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Pattern: 35150b57cec5SDimitry Andric TrivialAutoVarInit = "pattern"; 35160b57cec5SDimitry Andric break; 35170b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Zero: 35180b57cec5SDimitry Andric TrivialAutoVarInit = "zero"; 35190b57cec5SDimitry Andric break; 35200b57cec5SDimitry Andric } 35210b57cec5SDimitry Andric 35220b57cec5SDimitry Andric if (!TrivialAutoVarInit.empty()) { 35230b57cec5SDimitry Andric CmdArgs.push_back( 35240b57cec5SDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit)); 35250b57cec5SDimitry Andric } 35265ffd83dbSDimitry Andric 35275ffd83dbSDimitry Andric if (Arg *A = 35285ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) { 35295ffd83dbSDimitry Andric if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) || 35305ffd83dbSDimitry Andric StringRef( 35315ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) == 35325ffd83dbSDimitry Andric "uninitialized") 35335ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency); 35345ffd83dbSDimitry Andric A->claim(); 35355ffd83dbSDimitry Andric StringRef Val = A->getValue(); 35365ffd83dbSDimitry Andric if (std::stoi(Val.str()) <= 0) 35375ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_invalid_value); 35385ffd83dbSDimitry Andric CmdArgs.push_back( 35395ffd83dbSDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val)); 35405ffd83dbSDimitry Andric } 35410b57cec5SDimitry Andric } 35420b57cec5SDimitry Andric 3543fe6060f1SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, 3544fe6060f1SDimitry Andric types::ID InputType) { 35455ffd83dbSDimitry Andric // cl-denorms-are-zero is not forwarded. It is translated into a generic flag 35465ffd83dbSDimitry Andric // for denormal flushing handling based on the target. 35470b57cec5SDimitry Andric const unsigned ForwardedArguments[] = { 35480b57cec5SDimitry Andric options::OPT_cl_opt_disable, 35490b57cec5SDimitry Andric options::OPT_cl_strict_aliasing, 35500b57cec5SDimitry Andric options::OPT_cl_single_precision_constant, 35510b57cec5SDimitry Andric options::OPT_cl_finite_math_only, 35520b57cec5SDimitry Andric options::OPT_cl_kernel_arg_info, 35530b57cec5SDimitry Andric options::OPT_cl_unsafe_math_optimizations, 35540b57cec5SDimitry Andric options::OPT_cl_fast_relaxed_math, 35550b57cec5SDimitry Andric options::OPT_cl_mad_enable, 35560b57cec5SDimitry Andric options::OPT_cl_no_signed_zeros, 35570b57cec5SDimitry Andric options::OPT_cl_fp32_correctly_rounded_divide_sqrt, 35580b57cec5SDimitry Andric options::OPT_cl_uniform_work_group_size 35590b57cec5SDimitry Andric }; 35600b57cec5SDimitry Andric 35610b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) { 35620b57cec5SDimitry Andric std::string CLStdStr = std::string("-cl-std=") + A->getValue(); 35630b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLStdStr)); 356481ad6265SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_cl_ext_EQ)) { 356581ad6265SDimitry Andric std::string CLExtStr = std::string("-cl-ext=") + A->getValue(); 356681ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLExtStr)); 35670b57cec5SDimitry Andric } 35680b57cec5SDimitry Andric 35690b57cec5SDimitry Andric for (const auto &Arg : ForwardedArguments) 35700b57cec5SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 35710b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); 3572fe6060f1SDimitry Andric 3573fe6060f1SDimitry Andric // Only add the default headers if we are compiling OpenCL sources. 3574fe6060f1SDimitry Andric if ((types::isOpenCL(InputType) || 3575fe6060f1SDimitry Andric (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) && 3576fe6060f1SDimitry Andric !Args.hasArg(options::OPT_cl_no_stdinc)) { 3577fe6060f1SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 3578fe6060f1SDimitry Andric CmdArgs.push_back("-fdeclare-opencl-builtins"); 3579fe6060f1SDimitry Andric } 35800b57cec5SDimitry Andric } 35810b57cec5SDimitry Andric 358281ad6265SDimitry Andric static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs, 358381ad6265SDimitry Andric types::ID InputType) { 358481ad6265SDimitry Andric const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version, 358581ad6265SDimitry Andric options::OPT_D, 3586fcaf7f86SDimitry Andric options::OPT_I, 358781ad6265SDimitry Andric options::OPT_S, 3588bdd1243dSDimitry Andric options::OPT_O, 358981ad6265SDimitry Andric options::OPT_emit_llvm, 3590bdd1243dSDimitry Andric options::OPT_emit_obj, 359181ad6265SDimitry Andric options::OPT_disable_llvm_passes, 3592bdd1243dSDimitry Andric options::OPT_fnative_half_type, 3593bdd1243dSDimitry Andric options::OPT_hlsl_entrypoint}; 3594bdd1243dSDimitry Andric if (!types::isHLSL(InputType)) 3595bdd1243dSDimitry Andric return; 359681ad6265SDimitry Andric for (const auto &Arg : ForwardedArguments) 359781ad6265SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 359881ad6265SDimitry Andric A->renderAsInput(Args, CmdArgs); 359981ad6265SDimitry Andric // Add the default headers if dxc_no_stdinc is not set. 3600bdd1243dSDimitry Andric if (!Args.hasArg(options::OPT_dxc_no_stdinc) && 3601bdd1243dSDimitry Andric !Args.hasArg(options::OPT_nostdinc)) 360281ad6265SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 360381ad6265SDimitry Andric } 360481ad6265SDimitry Andric 36050b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, 36060b57cec5SDimitry Andric ArgStringList &CmdArgs) { 36070b57cec5SDimitry Andric bool ARCMTEnabled = false; 36080b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) { 36090b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 36100b57cec5SDimitry Andric options::OPT_ccc_arcmt_modify, 36110b57cec5SDimitry Andric options::OPT_ccc_arcmt_migrate)) { 36120b57cec5SDimitry Andric ARCMTEnabled = true; 36130b57cec5SDimitry Andric switch (A->getOption().getID()) { 36140b57cec5SDimitry Andric default: llvm_unreachable("missed a case"); 36150b57cec5SDimitry Andric case options::OPT_ccc_arcmt_check: 3616e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=check"); 36170b57cec5SDimitry Andric break; 36180b57cec5SDimitry Andric case options::OPT_ccc_arcmt_modify: 3619e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=modify"); 36200b57cec5SDimitry Andric break; 36210b57cec5SDimitry Andric case options::OPT_ccc_arcmt_migrate: 3622e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=migrate"); 36230b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 36240b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 36250b57cec5SDimitry Andric 36260b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 36270b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 36280b57cec5SDimitry Andric break; 36290b57cec5SDimitry Andric } 36300b57cec5SDimitry Andric } 36310b57cec5SDimitry Andric } else { 36320b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 36330b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 36340b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 36350b57cec5SDimitry Andric } 36360b57cec5SDimitry Andric 36370b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 36380b57cec5SDimitry Andric if (ARCMTEnabled) 36390b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 36400b57cec5SDimitry Andric << A->getAsString(Args) << "-ccc-arcmt-migrate"; 36410b57cec5SDimitry Andric 36420b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 36430b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 36440b57cec5SDimitry Andric 36450b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 36460b57cec5SDimitry Andric options::OPT_objcmt_migrate_subscripting, 36470b57cec5SDimitry Andric options::OPT_objcmt_migrate_property)) { 36480b57cec5SDimitry Andric // None specified, means enable them all. 36490b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-literals"); 36500b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-subscripting"); 36510b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-property"); 36520b57cec5SDimitry Andric } else { 36530b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 36540b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 36550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 36560b57cec5SDimitry Andric } 36570b57cec5SDimitry Andric } else { 36580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 36590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 36600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 36610b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all); 36620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property); 36630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property); 36640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax); 36650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation); 36660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype); 36670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros); 36680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance); 36690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property); 36700b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property); 36710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly); 36720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init); 3673349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_allowlist_dir_path); 36740b57cec5SDimitry Andric } 36750b57cec5SDimitry Andric } 36760b57cec5SDimitry Andric 36770b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, 36780b57cec5SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 36790b57cec5SDimitry Andric // -fbuiltin is default unless -mkernel is used. 36800b57cec5SDimitry Andric bool UseBuiltins = 36810b57cec5SDimitry Andric Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin, 36820b57cec5SDimitry Andric !Args.hasArg(options::OPT_mkernel)); 36830b57cec5SDimitry Andric if (!UseBuiltins) 36840b57cec5SDimitry Andric CmdArgs.push_back("-fno-builtin"); 36850b57cec5SDimitry Andric 36860b57cec5SDimitry Andric // -ffreestanding implies -fno-builtin. 36870b57cec5SDimitry Andric if (Args.hasArg(options::OPT_ffreestanding)) 36880b57cec5SDimitry Andric UseBuiltins = false; 36890b57cec5SDimitry Andric 36900b57cec5SDimitry Andric // Process the -fno-builtin-* options. 369181ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fno_builtin_)) { 369281ad6265SDimitry Andric A->claim(); 36930b57cec5SDimitry Andric 36940b57cec5SDimitry Andric // If -fno-builtin is specified, then there's no need to pass the option to 36950b57cec5SDimitry Andric // the frontend. 369681ad6265SDimitry Andric if (UseBuiltins) 369781ad6265SDimitry Andric A->render(Args, CmdArgs); 36980b57cec5SDimitry Andric } 36990b57cec5SDimitry Andric 37000b57cec5SDimitry Andric // le32-specific flags: 37010b57cec5SDimitry Andric // -fno-math-builtin: clang should not convert math builtins to intrinsics 37020b57cec5SDimitry Andric // by default. 37030b57cec5SDimitry Andric if (TC.getArch() == llvm::Triple::le32) 37040b57cec5SDimitry Andric CmdArgs.push_back("-fno-math-builtin"); 37050b57cec5SDimitry Andric } 37060b57cec5SDimitry Andric 37075ffd83dbSDimitry Andric bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { 370881ad6265SDimitry Andric if (const char *Str = std::getenv("CLANG_MODULE_CACHE_PATH")) { 370981ad6265SDimitry Andric Twine Path{Str}; 371081ad6265SDimitry Andric Path.toVector(Result); 371181ad6265SDimitry Andric return Path.getSingleStringRef() != ""; 371281ad6265SDimitry Andric } 37135ffd83dbSDimitry Andric if (llvm::sys::path::cache_directory(Result)) { 37145ffd83dbSDimitry Andric llvm::sys::path::append(Result, "clang"); 37150b57cec5SDimitry Andric llvm::sys::path::append(Result, "ModuleCache"); 37165ffd83dbSDimitry Andric return true; 37175ffd83dbSDimitry Andric } 37185ffd83dbSDimitry Andric return false; 37190b57cec5SDimitry Andric } 37200b57cec5SDimitry Andric 3721bdd1243dSDimitry Andric static bool RenderModulesOptions(Compilation &C, const Driver &D, 37220b57cec5SDimitry Andric const ArgList &Args, const InputInfo &Input, 3723bdd1243dSDimitry Andric const InputInfo &Output, const Arg *Std, 3724bdd1243dSDimitry Andric ArgStringList &CmdArgs) { 3725bdd1243dSDimitry Andric bool IsCXX = types::isCXX(Input.getType()); 3726bdd1243dSDimitry Andric // FIXME: Find a better way to determine whether the input has standard c++ 3727bdd1243dSDimitry Andric // modules support by default. 3728bdd1243dSDimitry Andric bool HaveStdCXXModules = 3729bdd1243dSDimitry Andric IsCXX && Std && 373006c3fb27SDimitry Andric (Std->containsValue("c++2a") || Std->containsValue("gnu++2a") || 373106c3fb27SDimitry Andric Std->containsValue("c++20") || Std->containsValue("gnu++20") || 373206c3fb27SDimitry Andric Std->containsValue("c++2b") || Std->containsValue("gnu++2b") || 373306c3fb27SDimitry Andric Std->containsValue("c++23") || Std->containsValue("gnu++23") || 373406c3fb27SDimitry Andric Std->containsValue("c++2c") || Std->containsValue("gnu++2c") || 373506c3fb27SDimitry Andric Std->containsValue("c++26") || Std->containsValue("gnu++26") || 373606c3fb27SDimitry Andric Std->containsValue("c++latest") || Std->containsValue("gnu++latest")); 3737bdd1243dSDimitry Andric bool HaveModules = HaveStdCXXModules; 3738bdd1243dSDimitry Andric 37390b57cec5SDimitry Andric // -fmodules enables the use of precompiled modules (off by default). 37400b57cec5SDimitry Andric // Users can pass -fno-cxx-modules to turn off modules support for 37410b57cec5SDimitry Andric // C++/Objective-C++ programs. 37420b57cec5SDimitry Andric bool HaveClangModules = false; 37430b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 37440b57cec5SDimitry Andric bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 37450b57cec5SDimitry Andric options::OPT_fno_cxx_modules, true); 3746bdd1243dSDimitry Andric if (AllowedInCXX || !IsCXX) { 37470b57cec5SDimitry Andric CmdArgs.push_back("-fmodules"); 37480b57cec5SDimitry Andric HaveClangModules = true; 37490b57cec5SDimitry Andric } 37500b57cec5SDimitry Andric } 37510b57cec5SDimitry Andric 37520b57cec5SDimitry Andric HaveModules |= HaveClangModules; 37530b57cec5SDimitry Andric 37540b57cec5SDimitry Andric // -fmodule-maps enables implicit reading of module map files. By default, 37550b57cec5SDimitry Andric // this is enabled if we are using Clang's flavor of precompiled modules. 37560b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fimplicit_module_maps, 37570b57cec5SDimitry Andric options::OPT_fno_implicit_module_maps, HaveClangModules)) 37580b57cec5SDimitry Andric CmdArgs.push_back("-fimplicit-module-maps"); 37590b57cec5SDimitry Andric 37600b57cec5SDimitry Andric // -fmodules-decluse checks that modules used are declared so (off by default) 376181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fmodules_decluse, 376281ad6265SDimitry Andric options::OPT_fno_modules_decluse); 37630b57cec5SDimitry Andric 37640b57cec5SDimitry Andric // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that 37650b57cec5SDimitry Andric // all #included headers are part of modules. 37660b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_strict_decluse, 37670b57cec5SDimitry Andric options::OPT_fno_modules_strict_decluse, false)) 37680b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-strict-decluse"); 37690b57cec5SDimitry Andric 37700b57cec5SDimitry Andric // -fno-implicit-modules turns off implicitly compiling modules on demand. 37710b57cec5SDimitry Andric bool ImplicitModules = false; 37720b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fimplicit_modules, 37730b57cec5SDimitry Andric options::OPT_fno_implicit_modules, HaveClangModules)) { 37740b57cec5SDimitry Andric if (HaveModules) 37750b57cec5SDimitry Andric CmdArgs.push_back("-fno-implicit-modules"); 37760b57cec5SDimitry Andric } else if (HaveModules) { 37770b57cec5SDimitry Andric ImplicitModules = true; 37780b57cec5SDimitry Andric // -fmodule-cache-path specifies where our implicitly-built module files 37790b57cec5SDimitry Andric // should be written. 37800b57cec5SDimitry Andric SmallString<128> Path; 37810b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) 37820b57cec5SDimitry Andric Path = A->getValue(); 37830b57cec5SDimitry Andric 37845ffd83dbSDimitry Andric bool HasPath = true; 37850b57cec5SDimitry Andric if (C.isForDiagnostics()) { 37860b57cec5SDimitry Andric // When generating crash reports, we want to emit the modules along with 37870b57cec5SDimitry Andric // the reproduction sources, so we ignore any provided module path. 37880b57cec5SDimitry Andric Path = Output.getFilename(); 37890b57cec5SDimitry Andric llvm::sys::path::replace_extension(Path, ".cache"); 37900b57cec5SDimitry Andric llvm::sys::path::append(Path, "modules"); 37910b57cec5SDimitry Andric } else if (Path.empty()) { 37920b57cec5SDimitry Andric // No module path was provided: use the default. 37935ffd83dbSDimitry Andric HasPath = Driver::getDefaultModuleCachePath(Path); 37940b57cec5SDimitry Andric } 37950b57cec5SDimitry Andric 37965ffd83dbSDimitry Andric // `HasPath` will only be false if getDefaultModuleCachePath() fails. 37975ffd83dbSDimitry Andric // That being said, that failure is unlikely and not caching is harmless. 37985ffd83dbSDimitry Andric if (HasPath) { 37990b57cec5SDimitry Andric const char Arg[] = "-fmodules-cache-path="; 38000b57cec5SDimitry Andric Path.insert(Path.begin(), Arg, Arg + strlen(Arg)); 38010b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Path)); 38020b57cec5SDimitry Andric } 38035ffd83dbSDimitry Andric } 38040b57cec5SDimitry Andric 38050b57cec5SDimitry Andric if (HaveModules) { 3806e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules, 3807e8d8bef9SDimitry Andric options::OPT_fno_prebuilt_implicit_modules, false)) 3808e8d8bef9SDimitry Andric CmdArgs.push_back("-fprebuilt-implicit-modules"); 3809a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content, 3810a7dea167SDimitry Andric options::OPT_fno_modules_validate_input_files_content, 3811a7dea167SDimitry Andric false)) 3812a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 38130b57cec5SDimitry Andric } 38140b57cec5SDimitry Andric 38150b57cec5SDimitry Andric // -fmodule-name specifies the module that is currently being built (or 38160b57cec5SDimitry Andric // used for header checking by -fmodule-maps). 38170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ); 38180b57cec5SDimitry Andric 38190b57cec5SDimitry Andric // -fmodule-map-file can be used to specify files containing module 38200b57cec5SDimitry Andric // definitions. 38210b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file); 38220b57cec5SDimitry Andric 38230b57cec5SDimitry Andric // -fbuiltin-module-map can be used to load the clang 38240b57cec5SDimitry Andric // builtin headers modulemap file. 38250b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuiltin_module_map)) { 38260b57cec5SDimitry Andric SmallString<128> BuiltinModuleMap(D.ResourceDir); 38270b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "include"); 38280b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "module.modulemap"); 38290b57cec5SDimitry Andric if (llvm::sys::fs::exists(BuiltinModuleMap)) 38300b57cec5SDimitry Andric CmdArgs.push_back( 38310b57cec5SDimitry Andric Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap)); 38320b57cec5SDimitry Andric } 38330b57cec5SDimitry Andric 38340b57cec5SDimitry Andric // The -fmodule-file=<name>=<file> form specifies the mapping of module 38350b57cec5SDimitry Andric // names to precompiled module files (the module is loaded only if used). 38360b57cec5SDimitry Andric // The -fmodule-file=<file> form can be used to unconditionally load 38370b57cec5SDimitry Andric // precompiled module files (whether used or not). 383806c3fb27SDimitry Andric if (HaveModules || Input.getType() == clang::driver::types::TY_ModuleFile) { 38390b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file); 384006c3fb27SDimitry Andric 384106c3fb27SDimitry Andric // -fprebuilt-module-path specifies where to load the prebuilt module files. 384206c3fb27SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { 384306c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 384406c3fb27SDimitry Andric std::string("-fprebuilt-module-path=") + A->getValue())); 384506c3fb27SDimitry Andric A->claim(); 384606c3fb27SDimitry Andric } 384706c3fb27SDimitry Andric } else 38480b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_file); 38490b57cec5SDimitry Andric 38500b57cec5SDimitry Andric // When building modules and generating crashdumps, we need to dump a module 38510b57cec5SDimitry Andric // dependency VFS alongside the output. 38520b57cec5SDimitry Andric if (HaveClangModules && C.isForDiagnostics()) { 38530b57cec5SDimitry Andric SmallString<128> VFSDir(Output.getFilename()); 38540b57cec5SDimitry Andric llvm::sys::path::replace_extension(VFSDir, ".cache"); 38550b57cec5SDimitry Andric // Add the cache directory as a temp so the crash diagnostics pick it up. 38560b57cec5SDimitry Andric C.addTempFile(Args.MakeArgString(VFSDir)); 38570b57cec5SDimitry Andric 38580b57cec5SDimitry Andric llvm::sys::path::append(VFSDir, "vfs"); 38590b57cec5SDimitry Andric CmdArgs.push_back("-module-dependency-dir"); 38600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(VFSDir)); 38610b57cec5SDimitry Andric } 38620b57cec5SDimitry Andric 38630b57cec5SDimitry Andric if (HaveClangModules) 38640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path); 38650b57cec5SDimitry Andric 38660b57cec5SDimitry Andric // Pass through all -fmodules-ignore-macro arguments. 38670b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 38680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 38690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 38700b57cec5SDimitry Andric 3871753f127fSDimitry Andric if (HaveClangModules) { 38720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); 38730b57cec5SDimitry Andric 38740b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { 38750b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuild_session_timestamp)) 38760b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 38770b57cec5SDimitry Andric << A->getAsString(Args) << "-fbuild-session-timestamp"; 38780b57cec5SDimitry Andric 38790b57cec5SDimitry Andric llvm::sys::fs::file_status Status; 38800b57cec5SDimitry Andric if (llvm::sys::fs::status(A->getValue(), Status)) 38810b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << A->getValue(); 3882349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 3883349cc55cSDimitry Andric "-fbuild-session-timestamp=" + 3884349cc55cSDimitry Andric Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>( 3885349cc55cSDimitry Andric Status.getLastModificationTime().time_since_epoch()) 38860b57cec5SDimitry Andric .count()))); 38870b57cec5SDimitry Andric } 38880b57cec5SDimitry Andric 3889753f127fSDimitry Andric if (Args.getLastArg( 3890753f127fSDimitry Andric options::OPT_fmodules_validate_once_per_build_session)) { 38910b57cec5SDimitry Andric if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, 38920b57cec5SDimitry Andric options::OPT_fbuild_session_file)) 38930b57cec5SDimitry Andric D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); 38940b57cec5SDimitry Andric 38950b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 38960b57cec5SDimitry Andric options::OPT_fmodules_validate_once_per_build_session); 38970b57cec5SDimitry Andric } 38980b57cec5SDimitry Andric 38990b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, 39000b57cec5SDimitry Andric options::OPT_fno_modules_validate_system_headers, 39010b57cec5SDimitry Andric ImplicitModules)) 39020b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-validate-system-headers"); 39030b57cec5SDimitry Andric 3904753f127fSDimitry Andric Args.AddLastArg(CmdArgs, 3905753f127fSDimitry Andric options::OPT_fmodules_disable_diagnostic_validation); 3906753f127fSDimitry Andric } else { 3907753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_timestamp); 3908753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_file); 3909753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_once_per_build_session); 3910753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_system_headers); 3911753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fno_modules_validate_system_headers); 3912753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_disable_diagnostic_validation); 3913753f127fSDimitry Andric } 3914bdd1243dSDimitry Andric 3915bdd1243dSDimitry Andric // Claim `-fmodule-output` and `-fmodule-output=` to avoid unused warnings. 3916bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_output); 3917bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_output_EQ); 3918bdd1243dSDimitry Andric 3919bdd1243dSDimitry Andric return HaveModules; 39200b57cec5SDimitry Andric } 39210b57cec5SDimitry Andric 39220b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, 39230b57cec5SDimitry Andric ArgStringList &CmdArgs) { 39240b57cec5SDimitry Andric // -fsigned-char is default. 39250b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char, 39260b57cec5SDimitry Andric options::OPT_fno_signed_char, 39270b57cec5SDimitry Andric options::OPT_funsigned_char, 39280b57cec5SDimitry Andric options::OPT_fno_unsigned_char)) { 39290b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_funsigned_char) || 39300b57cec5SDimitry Andric A->getOption().matches(options::OPT_fno_signed_char)) { 39310b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 39320b57cec5SDimitry Andric } 39330b57cec5SDimitry Andric } else if (!isSignedCharDefault(T)) { 39340b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 39350b57cec5SDimitry Andric } 39360b57cec5SDimitry Andric 39370b57cec5SDimitry Andric // The default depends on the language standard. 39380b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t); 39390b57cec5SDimitry Andric 39400b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar, 39410b57cec5SDimitry Andric options::OPT_fno_short_wchar)) { 39420b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fshort_wchar)) { 39430b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=short"); 39440b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 39450b57cec5SDimitry Andric } else { 39460b57cec5SDimitry Andric bool IsARM = T.isARM() || T.isThumb() || T.isAArch64(); 39470b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=int"); 3948e8d8bef9SDimitry Andric if (T.isOSzOS() || 3949e8d8bef9SDimitry Andric (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD()))) 39500b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 39510b57cec5SDimitry Andric else 39520b57cec5SDimitry Andric CmdArgs.push_back("-fsigned-wchar"); 39530b57cec5SDimitry Andric } 395406c3fb27SDimitry Andric } else if (T.isOSzOS()) 395506c3fb27SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 39560b57cec5SDimitry Andric } 39570b57cec5SDimitry Andric 39580b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D, 39590b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 39600b57cec5SDimitry Andric ObjCRuntime &Runtime, bool InferCovariantReturns, 39610b57cec5SDimitry Andric const InputInfo &Input, ArgStringList &CmdArgs) { 39620b57cec5SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 39630b57cec5SDimitry Andric 39640b57cec5SDimitry Andric // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy 39650b57cec5SDimitry Andric // is the default. Except for deployment target of 10.5, next runtime is 39660b57cec5SDimitry Andric // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently. 39670b57cec5SDimitry Andric if (Runtime.isNonFragile()) { 39680b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 39690b57cec5SDimitry Andric options::OPT_fno_objc_legacy_dispatch, 39700b57cec5SDimitry Andric Runtime.isLegacyDispatchDefaultForArch(Arch))) { 39710b57cec5SDimitry Andric if (TC.UseObjCMixedDispatch()) 39720b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 39730b57cec5SDimitry Andric else 39740b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 39750b57cec5SDimitry Andric } 39760b57cec5SDimitry Andric } 39770b57cec5SDimitry Andric 39780b57cec5SDimitry Andric // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option 39790b57cec5SDimitry Andric // to do Array/Dictionary subscripting by default. 39800b57cec5SDimitry Andric if (Arch == llvm::Triple::x86 && T.isMacOSX() && 39810b57cec5SDimitry Andric Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily()) 39820b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-subscripting-legacy-runtime"); 39830b57cec5SDimitry Andric 39840b57cec5SDimitry Andric // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 39850b57cec5SDimitry Andric // NOTE: This logic is duplicated in ToolChains.cpp. 39860b57cec5SDimitry Andric if (isObjCAutoRefCount(Args)) { 39870b57cec5SDimitry Andric TC.CheckObjCARC(); 39880b57cec5SDimitry Andric 39890b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc"); 39900b57cec5SDimitry Andric 39910b57cec5SDimitry Andric // FIXME: It seems like this entire block, and several around it should be 39920b57cec5SDimitry Andric // wrapped in isObjC, but for now we just use it here as this is where it 39930b57cec5SDimitry Andric // was being used previously. 39940b57cec5SDimitry Andric if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) { 39950b57cec5SDimitry Andric if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 39960b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 39970b57cec5SDimitry Andric else 39980b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 39990b57cec5SDimitry Andric } 40000b57cec5SDimitry Andric 40010b57cec5SDimitry Andric // Allow the user to enable full exceptions code emission. 40020b57cec5SDimitry Andric // We default off for Objective-C, on for Objective-C++. 40030b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 40040b57cec5SDimitry Andric options::OPT_fno_objc_arc_exceptions, 40050b57cec5SDimitry Andric /*Default=*/types::isCXX(Input.getType()))) 40060b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-exceptions"); 40070b57cec5SDimitry Andric } 40080b57cec5SDimitry Andric 40090b57cec5SDimitry Andric // Silence warning for full exception code emission options when explicitly 40100b57cec5SDimitry Andric // set to use no ARC. 40110b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fno_objc_arc)) { 40120b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions); 40130b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions); 40140b57cec5SDimitry Andric } 40150b57cec5SDimitry Andric 40160b57cec5SDimitry Andric // Allow the user to control whether messages can be converted to runtime 40170b57cec5SDimitry Andric // functions. 40180b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 40190b57cec5SDimitry Andric auto *Arg = Args.getLastArg( 40200b57cec5SDimitry Andric options::OPT_fobjc_convert_messages_to_runtime_calls, 40210b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls); 40220b57cec5SDimitry Andric if (Arg && 40230b57cec5SDimitry Andric Arg->getOption().matches( 40240b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls)) 40250b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls"); 40260b57cec5SDimitry Andric } 40270b57cec5SDimitry Andric 40280b57cec5SDimitry Andric // -fobjc-infer-related-result-type is the default, except in the Objective-C 40290b57cec5SDimitry Andric // rewriter. 40300b57cec5SDimitry Andric if (InferCovariantReturns) 40310b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-infer-related-result-type"); 40320b57cec5SDimitry Andric 40330b57cec5SDimitry Andric // Pass down -fobjc-weak or -fno-objc-weak if present. 40340b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 40350b57cec5SDimitry Andric auto WeakArg = 40360b57cec5SDimitry Andric Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak); 40370b57cec5SDimitry Andric if (!WeakArg) { 40380b57cec5SDimitry Andric // nothing to do 40390b57cec5SDimitry Andric } else if (!Runtime.allowsWeak()) { 40400b57cec5SDimitry Andric if (WeakArg->getOption().matches(options::OPT_fobjc_weak)) 40410b57cec5SDimitry Andric D.Diag(diag::err_objc_weak_unsupported); 40420b57cec5SDimitry Andric } else { 40430b57cec5SDimitry Andric WeakArg->render(Args, CmdArgs); 40440b57cec5SDimitry Andric } 40450b57cec5SDimitry Andric } 4046fe6060f1SDimitry Andric 4047fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing)) 4048fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing"); 40490b57cec5SDimitry Andric } 40500b57cec5SDimitry Andric 40510b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, 40520b57cec5SDimitry Andric ArgStringList &CmdArgs) { 40530b57cec5SDimitry Andric bool CaretDefault = true; 40540b57cec5SDimitry Andric bool ColumnDefault = true; 40550b57cec5SDimitry Andric 40560b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic, 40570b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_column, 40580b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_caret)) { 40590b57cec5SDimitry Andric switch (A->getOption().getID()) { 40600b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_caret: 40610b57cec5SDimitry Andric CaretDefault = true; 40620b57cec5SDimitry Andric ColumnDefault = true; 40630b57cec5SDimitry Andric break; 40640b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_column: 40650b57cec5SDimitry Andric CaretDefault = false; 40660b57cec5SDimitry Andric ColumnDefault = true; 40670b57cec5SDimitry Andric break; 40680b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_classic: 40690b57cec5SDimitry Andric CaretDefault = false; 40700b57cec5SDimitry Andric ColumnDefault = false; 40710b57cec5SDimitry Andric break; 40720b57cec5SDimitry Andric } 40730b57cec5SDimitry Andric } 40740b57cec5SDimitry Andric 40750b57cec5SDimitry Andric // -fcaret-diagnostics is default. 40760b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 40770b57cec5SDimitry Andric options::OPT_fno_caret_diagnostics, CaretDefault)) 40780b57cec5SDimitry Andric CmdArgs.push_back("-fno-caret-diagnostics"); 40790b57cec5SDimitry Andric 408081ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_fixit_info, 408181ad6265SDimitry Andric options::OPT_fno_diagnostics_fixit_info); 408281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_option, 408381ad6265SDimitry Andric options::OPT_fno_diagnostics_show_option); 40840b57cec5SDimitry Andric 40850b57cec5SDimitry Andric if (const Arg *A = 40860b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 40870b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-category"); 40880b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 40890b57cec5SDimitry Andric } 40900b57cec5SDimitry Andric 409181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdiagnostics_show_hotness, 409281ad6265SDimitry Andric options::OPT_fno_diagnostics_show_hotness); 40930b57cec5SDimitry Andric 40940b57cec5SDimitry Andric if (const Arg *A = 40950b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) { 40960b57cec5SDimitry Andric std::string Opt = 40970b57cec5SDimitry Andric std::string("-fdiagnostics-hotness-threshold=") + A->getValue(); 40980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 40990b57cec5SDimitry Andric } 41000b57cec5SDimitry Andric 410106c3fb27SDimitry Andric if (const Arg *A = 410206c3fb27SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_misexpect_tolerance_EQ)) { 410306c3fb27SDimitry Andric std::string Opt = 410406c3fb27SDimitry Andric std::string("-fdiagnostics-misexpect-tolerance=") + A->getValue(); 410506c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 410606c3fb27SDimitry Andric } 410706c3fb27SDimitry Andric 41080b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 41090b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 41100b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 4111fcaf7f86SDimitry Andric if (StringRef(A->getValue()) == "sarif" || 4112fcaf7f86SDimitry Andric StringRef(A->getValue()) == "SARIF") 4113fcaf7f86SDimitry Andric D.Diag(diag::warn_drv_sarif_format_unstable); 41140b57cec5SDimitry Andric } 41150b57cec5SDimitry Andric 41160b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg( 41170b57cec5SDimitry Andric options::OPT_fdiagnostics_show_note_include_stack, 41180b57cec5SDimitry Andric options::OPT_fno_diagnostics_show_note_include_stack)) { 41190b57cec5SDimitry Andric const Option &O = A->getOption(); 41200b57cec5SDimitry Andric if (O.matches(options::OPT_fdiagnostics_show_note_include_stack)) 41210b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 41220b57cec5SDimitry Andric else 41230b57cec5SDimitry Andric CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 41240b57cec5SDimitry Andric } 41250b57cec5SDimitry Andric 41260b57cec5SDimitry Andric // Color diagnostics are parsed by the driver directly from argv and later 41270b57cec5SDimitry Andric // re-parsed to construct this job; claim any possible color diagnostic here 41280b57cec5SDimitry Andric // to avoid warn_drv_unused_argument and diagnose bad 41290b57cec5SDimitry Andric // OPT_fdiagnostics_color_EQ values. 413081ad6265SDimitry Andric Args.getLastArg(options::OPT_fcolor_diagnostics, 413181ad6265SDimitry Andric options::OPT_fno_color_diagnostics); 413281ad6265SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_color_EQ)) { 41330b57cec5SDimitry Andric StringRef Value(A->getValue()); 41340b57cec5SDimitry Andric if (Value != "always" && Value != "never" && Value != "auto") 413581ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 413681ad6265SDimitry Andric << Value << A->getOption().getName(); 41370b57cec5SDimitry Andric } 41380b57cec5SDimitry Andric 41390b57cec5SDimitry Andric if (D.getDiags().getDiagnosticOptions().ShowColors) 41400b57cec5SDimitry Andric CmdArgs.push_back("-fcolor-diagnostics"); 41410b57cec5SDimitry Andric 41420b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fansi_escape_codes)) 41430b57cec5SDimitry Andric CmdArgs.push_back("-fansi-escape-codes"); 41440b57cec5SDimitry Andric 414581ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fshow_source_location, 414681ad6265SDimitry Andric options::OPT_fno_show_source_location); 41470b57cec5SDimitry Andric 414806c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_line_numbers, 414906c3fb27SDimitry Andric options::OPT_fno_diagnostics_show_line_numbers); 415006c3fb27SDimitry Andric 41510b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths)) 41520b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-absolute-paths"); 41530b57cec5SDimitry Andric 41540b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column, 41550b57cec5SDimitry Andric ColumnDefault)) 41560b57cec5SDimitry Andric CmdArgs.push_back("-fno-show-column"); 41570b57cec5SDimitry Andric 415881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fspell_checking, 415981ad6265SDimitry Andric options::OPT_fno_spell_checking); 41600b57cec5SDimitry Andric } 41610b57cec5SDimitry Andric 4162fcaf7f86SDimitry Andric DwarfFissionKind tools::getDebugFissionKind(const Driver &D, 41630b57cec5SDimitry Andric const ArgList &Args, Arg *&Arg) { 4164e8d8bef9SDimitry Andric Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ, 4165e8d8bef9SDimitry Andric options::OPT_gno_split_dwarf); 4166e8d8bef9SDimitry Andric if (!Arg || Arg->getOption().matches(options::OPT_gno_split_dwarf)) 41670b57cec5SDimitry Andric return DwarfFissionKind::None; 41680b57cec5SDimitry Andric 41690b57cec5SDimitry Andric if (Arg->getOption().matches(options::OPT_gsplit_dwarf)) 41700b57cec5SDimitry Andric return DwarfFissionKind::Split; 41710b57cec5SDimitry Andric 41720b57cec5SDimitry Andric StringRef Value = Arg->getValue(); 41730b57cec5SDimitry Andric if (Value == "split") 41740b57cec5SDimitry Andric return DwarfFissionKind::Split; 41750b57cec5SDimitry Andric if (Value == "single") 41760b57cec5SDimitry Andric return DwarfFissionKind::Single; 41770b57cec5SDimitry Andric 41780b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 4179bdd1243dSDimitry Andric << Arg->getSpelling() << Arg->getValue(); 41800b57cec5SDimitry Andric return DwarfFissionKind::None; 41810b57cec5SDimitry Andric } 41820b57cec5SDimitry Andric 4183fe6060f1SDimitry Andric static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, 4184fe6060f1SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 4185fe6060f1SDimitry Andric unsigned DwarfVersion) { 4186fe6060f1SDimitry Andric auto *DwarfFormatArg = 4187fe6060f1SDimitry Andric Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); 4188fe6060f1SDimitry Andric if (!DwarfFormatArg) 4189fe6060f1SDimitry Andric return; 4190fe6060f1SDimitry Andric 4191fe6060f1SDimitry Andric if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { 4192fe6060f1SDimitry Andric if (DwarfVersion < 3) 4193fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4194fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; 4195fe6060f1SDimitry Andric else if (!T.isArch64Bit()) 4196fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4197fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; 4198fe6060f1SDimitry Andric else if (!T.isOSBinFormatELF()) 4199fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4200fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "ELF platforms"; 4201fe6060f1SDimitry Andric } 4202fe6060f1SDimitry Andric 4203fe6060f1SDimitry Andric DwarfFormatArg->render(Args, CmdArgs); 4204fe6060f1SDimitry Andric } 4205fe6060f1SDimitry Andric 420606c3fb27SDimitry Andric static void 420706c3fb27SDimitry Andric renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T, 420806c3fb27SDimitry Andric const ArgList &Args, bool EmitCodeView, bool IRInput, 4209e8d8bef9SDimitry Andric ArgStringList &CmdArgs, 421006c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind &DebugInfoKind, 42110b57cec5SDimitry Andric DwarfFissionKind &DwarfFission) { 42120b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_info_for_profiling, 42130b57cec5SDimitry Andric options::OPT_fno_debug_info_for_profiling, false) && 42140b57cec5SDimitry Andric checkDebugInfoOption( 42150b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC)) 42160b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-info-for-profiling"); 42170b57cec5SDimitry Andric 42180b57cec5SDimitry Andric // The 'g' groups options involve a somewhat intricate sequence of decisions 42190b57cec5SDimitry Andric // about what to pass from the driver to the frontend, but by the time they 42200b57cec5SDimitry Andric // reach cc1 they've been factored into three well-defined orthogonal choices: 42210b57cec5SDimitry Andric // * what level of debug info to generate 42220b57cec5SDimitry Andric // * what dwarf version to write 42230b57cec5SDimitry Andric // * what debugger tuning to use 42240b57cec5SDimitry Andric // This avoids having to monkey around further in cc1 other than to disable 42250b57cec5SDimitry Andric // codeview if not running in a Windows environment. Perhaps even that 42260b57cec5SDimitry Andric // decision should be made in the driver as well though. 42270b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning(); 42280b57cec5SDimitry Andric 42290b57cec5SDimitry Andric bool SplitDWARFInlining = 42300b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_dwarf_inlining, 4231480093f4SDimitry Andric options::OPT_fno_split_dwarf_inlining, false); 42320b57cec5SDimitry Andric 4233e8d8bef9SDimitry Andric // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does 4234e8d8bef9SDimitry Andric // object file generation and no IR generation, -gN should not be needed. So 4235e8d8bef9SDimitry Andric // allow -gsplit-dwarf with either -gN or IR input. 4236e8d8bef9SDimitry Andric if (IRInput || Args.hasArg(options::OPT_g_Group)) { 42370b57cec5SDimitry Andric Arg *SplitDWARFArg; 42380b57cec5SDimitry Andric DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg); 42390b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None && 42400b57cec5SDimitry Andric !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) { 42410b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 42420b57cec5SDimitry Andric SplitDWARFInlining = false; 42430b57cec5SDimitry Andric } 4244e8d8bef9SDimitry Andric } 4245e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 424606c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; 42470b57cec5SDimitry Andric 42480b57cec5SDimitry Andric // If the last option explicitly specified a debug-info level, use it. 42490b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC) && 42500b57cec5SDimitry Andric A->getOption().matches(options::OPT_gN_Group)) { 425106c3fb27SDimitry Andric DebugInfoKind = debugLevelToInfoKind(*A); 42520b57cec5SDimitry Andric // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more 42530b57cec5SDimitry Andric // complicated if you've disabled inline info in the skeleton CUs 42540b57cec5SDimitry Andric // (SplitDWARFInlining) - then there's value in composing split-dwarf and 42550b57cec5SDimitry Andric // line-tables-only, so let those compose naturally in that case. 425606c3fb27SDimitry Andric if (DebugInfoKind == llvm::codegenoptions::NoDebugInfo || 425706c3fb27SDimitry Andric DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly || 425806c3fb27SDimitry Andric (DebugInfoKind == llvm::codegenoptions::DebugLineTablesOnly && 42590b57cec5SDimitry Andric SplitDWARFInlining)) 42600b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 42610b57cec5SDimitry Andric } 42620b57cec5SDimitry Andric } 42630b57cec5SDimitry Andric 42640b57cec5SDimitry Andric // If a debugger tuning argument appeared, remember it. 4265bdd1243dSDimitry Andric bool HasDebuggerTuning = false; 42660b57cec5SDimitry Andric if (const Arg *A = 42670b57cec5SDimitry Andric Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { 4268bdd1243dSDimitry Andric HasDebuggerTuning = true; 42690b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 42700b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_glldb)) 42710b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::LLDB; 42720b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_gsce)) 42730b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::SCE; 4274fe6060f1SDimitry Andric else if (A->getOption().matches(options::OPT_gdbx)) 4275fe6060f1SDimitry Andric DebuggerTuning = llvm::DebuggerKind::DBX; 42760b57cec5SDimitry Andric else 42770b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::GDB; 42780b57cec5SDimitry Andric } 42790b57cec5SDimitry Andric } 42800b57cec5SDimitry Andric 42810b57cec5SDimitry Andric // If a -gdwarf argument appeared, remember it. 4282480093f4SDimitry Andric bool EmitDwarf = false; 4283bdd1243dSDimitry Andric if (const Arg *A = getDwarfNArg(Args)) 4284bdd1243dSDimitry Andric EmitDwarf = checkDebugInfoOption(A, Args, D, TC); 42850b57cec5SDimitry Andric 4286bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) 4287bdd1243dSDimitry Andric EmitCodeView = checkDebugInfoOption(A, Args, D, TC); 42880b57cec5SDimitry Andric 42890b57cec5SDimitry Andric // If the user asked for debug info but did not explicitly specify -gcodeview 42900b57cec5SDimitry Andric // or -gdwarf, ask the toolchain for the default format. 4291480093f4SDimitry Andric if (!EmitCodeView && !EmitDwarf && 429206c3fb27SDimitry Andric DebugInfoKind != llvm::codegenoptions::NoDebugInfo) { 42930b57cec5SDimitry Andric switch (TC.getDefaultDebugFormat()) { 429406c3fb27SDimitry Andric case llvm::codegenoptions::DIF_CodeView: 42950b57cec5SDimitry Andric EmitCodeView = true; 42960b57cec5SDimitry Andric break; 429706c3fb27SDimitry Andric case llvm::codegenoptions::DIF_DWARF: 4298480093f4SDimitry Andric EmitDwarf = true; 42990b57cec5SDimitry Andric break; 43000b57cec5SDimitry Andric } 43010b57cec5SDimitry Andric } 43020b57cec5SDimitry Andric 4303e8d8bef9SDimitry Andric unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user 4304e8d8bef9SDimitry Andric unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may 4305e8d8bef9SDimitry Andric // be lower than what the user wanted. 4306480093f4SDimitry Andric if (EmitDwarf) { 4307bdd1243dSDimitry Andric RequestedDWARFVersion = getDwarfVersion(TC, Args); 4308e8d8bef9SDimitry Andric // Clamp effective DWARF version to the max supported by the toolchain. 4309e8d8bef9SDimitry Andric EffectiveDWARFVersion = 4310e8d8bef9SDimitry Andric std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion()); 4311bdd1243dSDimitry Andric } else { 4312bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fdebug_default_version); 4313480093f4SDimitry Andric } 4314480093f4SDimitry Andric 43150b57cec5SDimitry Andric // -gline-directives-only supported only for the DWARF debug info. 4316e8d8bef9SDimitry Andric if (RequestedDWARFVersion == 0 && 431706c3fb27SDimitry Andric DebugInfoKind == llvm::codegenoptions::DebugDirectivesOnly) 431806c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::NoDebugInfo; 43190b57cec5SDimitry Andric 4320fe6060f1SDimitry Andric // strict DWARF is set to false by default. But for DBX, we need it to be set 4321fe6060f1SDimitry Andric // as true by default. 4322fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf)) 4323fe6060f1SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4324fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf, 4325fe6060f1SDimitry Andric DebuggerTuning == llvm::DebuggerKind::DBX)) 4326fe6060f1SDimitry Andric CmdArgs.push_back("-gstrict-dwarf"); 4327fe6060f1SDimitry Andric 43280b57cec5SDimitry Andric // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags. 43290b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_flags_Group); 43300b57cec5SDimitry Andric 43310b57cec5SDimitry Andric // Column info is included by default for everything except SCE and 43320b57cec5SDimitry Andric // CodeView. Clang doesn't track end columns, just starting columns, which, 43330b57cec5SDimitry Andric // in theory, is fine for CodeView (and PDB). In practice, however, the 4334fe6060f1SDimitry Andric // Microsoft debuggers don't handle missing end columns well, and the AIX 4335fe6060f1SDimitry Andric // debugger DBX also doesn't handle the columns well, so it's better not to 4336fe6060f1SDimitry Andric // include any column info. 43370b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info)) 43380b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 43395ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info, 4340fe6060f1SDimitry Andric !EmitCodeView && 4341fe6060f1SDimitry Andric (DebuggerTuning != llvm::DebuggerKind::SCE && 4342fe6060f1SDimitry Andric DebuggerTuning != llvm::DebuggerKind::DBX))) 43435ffd83dbSDimitry Andric CmdArgs.push_back("-gno-column-info"); 43440b57cec5SDimitry Andric 43450b57cec5SDimitry Andric // FIXME: Move backend command line options to the module. 4346bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_gmodules, options::OPT_gno_modules, false)) { 4347bdd1243dSDimitry Andric // If -gline-tables-only or -gline-directives-only is the last option it 4348bdd1243dSDimitry Andric // wins. 4349bdd1243dSDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_gmodules), Args, D, 4350bdd1243dSDimitry Andric TC)) { 435106c3fb27SDimitry Andric if (DebugInfoKind != llvm::codegenoptions::DebugLineTablesOnly && 435206c3fb27SDimitry Andric DebugInfoKind != llvm::codegenoptions::DebugDirectivesOnly) { 435306c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; 43540b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-ext-refs"); 43550b57cec5SDimitry Andric CmdArgs.push_back("-fmodule-format=obj"); 43560b57cec5SDimitry Andric } 43570b57cec5SDimitry Andric } 4358bdd1243dSDimitry Andric } 43590b57cec5SDimitry Andric 4360fe6060f1SDimitry Andric if (T.isOSBinFormatELF() && SplitDWARFInlining) 4361fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-dwarf-inlining"); 43620b57cec5SDimitry Andric 43630b57cec5SDimitry Andric // After we've dealt with all combinations of things that could 43640b57cec5SDimitry Andric // make DebugInfoKind be other than None or DebugLineTablesOnly, 43650b57cec5SDimitry Andric // figure out if we need to "upgrade" it to standalone debug info. 43660b57cec5SDimitry Andric // We parse these two '-f' options whether or not they will be used, 43670b57cec5SDimitry Andric // to claim them even if you wrote "-fstandalone-debug -gline-tables-only" 43680b57cec5SDimitry Andric bool NeedFullDebug = Args.hasFlag( 43690b57cec5SDimitry Andric options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug, 43700b57cec5SDimitry Andric DebuggerTuning == llvm::DebuggerKind::LLDB || 43710b57cec5SDimitry Andric TC.GetDefaultStandaloneDebug()); 43720b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug)) 43730b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4374e8d8bef9SDimitry Andric 437506c3fb27SDimitry Andric if (DebugInfoKind == llvm::codegenoptions::LimitedDebugInfo || 437606c3fb27SDimitry Andric DebugInfoKind == llvm::codegenoptions::DebugInfoConstructor) { 4377e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types, 4378e8d8bef9SDimitry Andric options::OPT_feliminate_unused_debug_types, false)) 437906c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::UnusedTypeInfo; 4380e8d8bef9SDimitry Andric else if (NeedFullDebug) 438106c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::FullDebugInfo; 4382e8d8bef9SDimitry Andric } 43830b57cec5SDimitry Andric 43840b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, 43850b57cec5SDimitry Andric false)) { 43860b57cec5SDimitry Andric // Source embedding is a vendor extension to DWARF v5. By now we have 43870b57cec5SDimitry Andric // checked if a DWARF version was stated explicitly, and have otherwise 43880b57cec5SDimitry Andric // fallen back to the target default, so if this is still not at least 5 43890b57cec5SDimitry Andric // we emit an error. 43900b57cec5SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gembed_source); 4391e8d8bef9SDimitry Andric if (RequestedDWARFVersion < 5) 43920b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 43930b57cec5SDimitry Andric << A->getAsString(Args) << "-gdwarf-5"; 4394e8d8bef9SDimitry Andric else if (EffectiveDWARFVersion < 5) 4395e8d8bef9SDimitry Andric // The toolchain has reduced allowed dwarf version, so we can't enable 4396e8d8bef9SDimitry Andric // -gembed-source. 4397e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_dwarf_version_limited_by_target) 4398e8d8bef9SDimitry Andric << A->getAsString(Args) << TC.getTripleString() << 5 4399e8d8bef9SDimitry Andric << EffectiveDWARFVersion; 44000b57cec5SDimitry Andric else if (checkDebugInfoOption(A, Args, D, TC)) 44010b57cec5SDimitry Andric CmdArgs.push_back("-gembed-source"); 44020b57cec5SDimitry Andric } 44030b57cec5SDimitry Andric 44040b57cec5SDimitry Andric if (EmitCodeView) { 44050b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview"); 44060b57cec5SDimitry Andric 4407bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_gcodeview_ghash, 4408bdd1243dSDimitry Andric options::OPT_gno_codeview_ghash); 4409bdd1243dSDimitry Andric 4410bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_gcodeview_command_line, 4411bdd1243dSDimitry Andric options::OPT_gno_codeview_command_line); 44120b57cec5SDimitry Andric } 44130b57cec5SDimitry Andric 4414bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_ginline_line_tables, 4415bdd1243dSDimitry Andric options::OPT_gno_inline_line_tables); 4416480093f4SDimitry Andric 4417480093f4SDimitry Andric // When emitting remarks, we need at least debug lines in the output. 4418480093f4SDimitry Andric if (willEmitRemarks(Args) && 441906c3fb27SDimitry Andric DebugInfoKind <= llvm::codegenoptions::DebugDirectivesOnly) 442006c3fb27SDimitry Andric DebugInfoKind = llvm::codegenoptions::DebugLineTablesOnly; 4421480093f4SDimitry Andric 4422e8d8bef9SDimitry Andric // Adjust the debug info kind for the given toolchain. 4423e8d8bef9SDimitry Andric TC.adjustDebugInfoKind(DebugInfoKind, Args); 4424e8d8bef9SDimitry Andric 4425bdd1243dSDimitry Andric // On AIX, the debugger tuning option can be omitted if it is not explicitly 4426bdd1243dSDimitry Andric // set. 4427e8d8bef9SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion, 4428bdd1243dSDimitry Andric T.isOSAIX() && !HasDebuggerTuning 4429bdd1243dSDimitry Andric ? llvm::DebuggerKind::Default 4430bdd1243dSDimitry Andric : DebuggerTuning); 44310b57cec5SDimitry Andric 44320b57cec5SDimitry Andric // -fdebug-macro turns on macro debug info generation. 44330b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro, 44340b57cec5SDimitry Andric false)) 44350b57cec5SDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args, 44360b57cec5SDimitry Andric D, TC)) 44370b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-macro"); 44380b57cec5SDimitry Andric 44390b57cec5SDimitry Andric // -ggnu-pubnames turns on gnu style pubnames in the backend. 44400b57cec5SDimitry Andric const auto *PubnamesArg = 44410b57cec5SDimitry Andric Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames, 44420b57cec5SDimitry Andric options::OPT_gpubnames, options::OPT_gno_pubnames); 44430b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None || 44440b57cec5SDimitry Andric (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) 44450b57cec5SDimitry Andric if (!PubnamesArg || 44460b57cec5SDimitry Andric (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && 44470b57cec5SDimitry Andric !PubnamesArg->getOption().matches(options::OPT_gno_pubnames))) 44480b57cec5SDimitry Andric CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches( 44490b57cec5SDimitry Andric options::OPT_gpubnames) 44500b57cec5SDimitry Andric ? "-gpubnames" 44510b57cec5SDimitry Andric : "-ggnu-pubnames"); 4452349cc55cSDimitry Andric const auto *SimpleTemplateNamesArg = 445381ad6265SDimitry Andric Args.getLastArg(options::OPT_gsimple_template_names, 445481ad6265SDimitry Andric options::OPT_gno_simple_template_names); 4455349cc55cSDimitry Andric bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; 4456349cc55cSDimitry Andric if (SimpleTemplateNamesArg && 4457349cc55cSDimitry Andric checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { 4458349cc55cSDimitry Andric const auto &Opt = SimpleTemplateNamesArg->getOption(); 4459349cc55cSDimitry Andric if (Opt.matches(options::OPT_gsimple_template_names)) { 4460349cc55cSDimitry Andric ForwardTemplateParams = true; 4461349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=simple"); 4462349cc55cSDimitry Andric } 4463349cc55cSDimitry Andric } 44640b57cec5SDimitry Andric 4465bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gsrc_hash_EQ)) { 4466bdd1243dSDimitry Andric StringRef v = A->getValue(); 4467bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-gsrc-hash=" + v)); 44680b57cec5SDimitry Andric } 44690b57cec5SDimitry Andric 4470bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdebug_ranges_base_address, 4471bdd1243dSDimitry Andric options::OPT_fno_debug_ranges_base_address); 4472bdd1243dSDimitry Andric 44730b57cec5SDimitry Andric // -gdwarf-aranges turns on the emission of the aranges section in the 44740b57cec5SDimitry Andric // backend. 44750b57cec5SDimitry Andric // Always enabled for SCE tuning. 44760b57cec5SDimitry Andric bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE; 44770b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges)) 44780b57cec5SDimitry Andric NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges; 44790b57cec5SDimitry Andric if (NeedAranges) { 44800b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 44810b57cec5SDimitry Andric CmdArgs.push_back("-generate-arange-section"); 44820b57cec5SDimitry Andric } 44830b57cec5SDimitry Andric 4484bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_dwarf_frame, 4485bdd1243dSDimitry Andric options::OPT_fno_force_dwarf_frame); 4486480093f4SDimitry Andric 44870b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_types_section, 44880b57cec5SDimitry Andric options::OPT_fno_debug_types_section, false)) { 4489e8d8bef9SDimitry Andric if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) { 44900b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 44910b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fdebug_types_section) 44920b57cec5SDimitry Andric ->getAsString(Args) 44930b57cec5SDimitry Andric << T.getTriple(); 44940b57cec5SDimitry Andric } else if (checkDebugInfoOption( 44950b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_types_section), Args, D, 44960b57cec5SDimitry Andric TC)) { 44970b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 44980b57cec5SDimitry Andric CmdArgs.push_back("-generate-type-units"); 44990b57cec5SDimitry Andric } 45000b57cec5SDimitry Andric } 45010b57cec5SDimitry Andric 4502fe6060f1SDimitry Andric // To avoid join/split of directory+filename, the integrated assembler prefers 4503fe6060f1SDimitry Andric // the directory form of .file on all DWARF versions. GNU as doesn't allow the 4504fe6060f1SDimitry Andric // form before DWARF v5. 4505fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, 4506fe6060f1SDimitry Andric options::OPT_fno_dwarf_directory_asm, 4507fe6060f1SDimitry Andric TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) 4508fe6060f1SDimitry Andric CmdArgs.push_back("-fno-dwarf-directory-asm"); 4509fe6060f1SDimitry Andric 45100b57cec5SDimitry Andric // Decide how to render forward declarations of template instantiations. 45110b57cec5SDimitry Andric // SCE wants full descriptions, others just get them in the name. 4512349cc55cSDimitry Andric if (ForwardTemplateParams) 45130b57cec5SDimitry Andric CmdArgs.push_back("-debug-forward-template-params"); 45140b57cec5SDimitry Andric 45150b57cec5SDimitry Andric // Do we need to explicitly import anonymous namespaces into the parent 45160b57cec5SDimitry Andric // scope? 45170b57cec5SDimitry Andric if (DebuggerTuning == llvm::DebuggerKind::SCE) 45180b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-explicit-import"); 45190b57cec5SDimitry Andric 4520fe6060f1SDimitry Andric renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion); 45210b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); 45220b57cec5SDimitry Andric } 45230b57cec5SDimitry Andric 4524bdd1243dSDimitry Andric static void ProcessVSRuntimeLibrary(const ArgList &Args, 4525bdd1243dSDimitry Andric ArgStringList &CmdArgs) { 4526bdd1243dSDimitry Andric unsigned RTOptionID = options::OPT__SLASH_MT; 4527bdd1243dSDimitry Andric 4528bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4529bdd1243dSDimitry Andric // The /LDd option implies /MTd. The dependent lib part can be overridden, 4530bdd1243dSDimitry Andric // but defining _DEBUG is sticky. 4531bdd1243dSDimitry Andric RTOptionID = options::OPT__SLASH_MTd; 4532bdd1243dSDimitry Andric 4533bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) 4534bdd1243dSDimitry Andric RTOptionID = A->getOption().getID(); 4535bdd1243dSDimitry Andric 4536bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { 4537bdd1243dSDimitry Andric RTOptionID = llvm::StringSwitch<unsigned>(A->getValue()) 4538bdd1243dSDimitry Andric .Case("static", options::OPT__SLASH_MT) 4539bdd1243dSDimitry Andric .Case("static_dbg", options::OPT__SLASH_MTd) 4540bdd1243dSDimitry Andric .Case("dll", options::OPT__SLASH_MD) 4541bdd1243dSDimitry Andric .Case("dll_dbg", options::OPT__SLASH_MDd) 4542bdd1243dSDimitry Andric .Default(options::OPT__SLASH_MT); 4543bdd1243dSDimitry Andric } 4544bdd1243dSDimitry Andric 4545bdd1243dSDimitry Andric StringRef FlagForCRT; 4546bdd1243dSDimitry Andric switch (RTOptionID) { 4547bdd1243dSDimitry Andric case options::OPT__SLASH_MD: 4548bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4549bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4550bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4551bdd1243dSDimitry Andric CmdArgs.push_back("-D_DLL"); 4552bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=msvcrt"; 4553bdd1243dSDimitry Andric break; 4554bdd1243dSDimitry Andric case options::OPT__SLASH_MDd: 4555bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4556bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4557bdd1243dSDimitry Andric CmdArgs.push_back("-D_DLL"); 4558bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=msvcrtd"; 4559bdd1243dSDimitry Andric break; 4560bdd1243dSDimitry Andric case options::OPT__SLASH_MT: 4561bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4562bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4563bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4564bdd1243dSDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 4565bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=libcmt"; 4566bdd1243dSDimitry Andric break; 4567bdd1243dSDimitry Andric case options::OPT__SLASH_MTd: 4568bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4569bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4570bdd1243dSDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 4571bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=libcmtd"; 4572bdd1243dSDimitry Andric break; 4573bdd1243dSDimitry Andric default: 4574bdd1243dSDimitry Andric llvm_unreachable("Unexpected option ID."); 4575bdd1243dSDimitry Andric } 4576bdd1243dSDimitry Andric 4577bdd1243dSDimitry Andric if (Args.hasArg(options::OPT_fms_omit_default_lib)) { 4578bdd1243dSDimitry Andric CmdArgs.push_back("-D_VC_NODEFAULTLIB"); 4579bdd1243dSDimitry Andric } else { 4580bdd1243dSDimitry Andric CmdArgs.push_back(FlagForCRT.data()); 4581bdd1243dSDimitry Andric 4582bdd1243dSDimitry Andric // This provides POSIX compatibility (maps 'open' to '_open'), which most 4583bdd1243dSDimitry Andric // users want. The /Za flag to cl.exe turns this off, but it's not 4584bdd1243dSDimitry Andric // implemented in clang. 4585bdd1243dSDimitry Andric CmdArgs.push_back("--dependent-lib=oldnames"); 4586bdd1243dSDimitry Andric } 4587bdd1243dSDimitry Andric } 4588bdd1243dSDimitry Andric 45890b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA, 45900b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 45910b57cec5SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 45920b57cec5SDimitry Andric const auto &TC = getToolChain(); 45930b57cec5SDimitry Andric const llvm::Triple &RawTriple = TC.getTriple(); 45940b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 45950b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 45960b57cec5SDimitry Andric 45970b57cec5SDimitry Andric bool KernelOrKext = 45980b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 45990b57cec5SDimitry Andric const Driver &D = TC.getDriver(); 46000b57cec5SDimitry Andric ArgStringList CmdArgs; 46010b57cec5SDimitry Andric 46020b57cec5SDimitry Andric assert(Inputs.size() >= 1 && "Must have at least one input."); 46030b57cec5SDimitry Andric // CUDA/HIP compilation may have multiple inputs (source file + results of 46040b57cec5SDimitry Andric // device-side compilations). OpenMP device jobs also take the host IR as a 46050b57cec5SDimitry Andric // second input. Module precompilation accepts a list of header files to 460681ad6265SDimitry Andric // include as part of the module. API extraction accepts a list of header 460781ad6265SDimitry Andric // files whose API information is emitted in the output. All other jobs are 460881ad6265SDimitry Andric // expected to have exactly one input. 46090b57cec5SDimitry Andric bool IsCuda = JA.isOffloading(Action::OFK_Cuda); 4610fe6060f1SDimitry Andric bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda); 46110b57cec5SDimitry Andric bool IsHIP = JA.isOffloading(Action::OFK_HIP); 4612fe6060f1SDimitry Andric bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP); 46130b57cec5SDimitry Andric bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); 461481ad6265SDimitry Andric bool IsExtractAPI = isa<ExtractAPIJobAction>(JA); 4615fe6060f1SDimitry Andric bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || 4616fe6060f1SDimitry Andric JA.isDeviceOffloading(Action::OFK_Host)); 461781ad6265SDimitry Andric bool IsHostOffloadingAction = 4618bdd1243dSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP) || 461981ad6265SDimitry Andric (JA.isHostOffloading(C.getActiveOffloadKinds()) && 462081ad6265SDimitry Andric Args.hasFlag(options::OPT_offload_new_driver, 462181ad6265SDimitry Andric options::OPT_no_offload_new_driver, false)); 462281ad6265SDimitry Andric 4623753f127fSDimitry Andric bool IsRDCMode = 4624753f127fSDimitry Andric Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false); 4625fe6060f1SDimitry Andric bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction); 4626fe6060f1SDimitry Andric auto LTOMode = D.getLTOMode(IsDeviceOffloadAction); 46270b57cec5SDimitry Andric 462881ad6265SDimitry Andric // Extract API doesn't have a main input file, so invent a fake one as a 462981ad6265SDimitry Andric // placeholder. 463081ad6265SDimitry Andric InputInfo ExtractAPIPlaceholderInput(Inputs[0].getType(), "extract-api", 463181ad6265SDimitry Andric "extract-api"); 463281ad6265SDimitry Andric 4633bdd1243dSDimitry Andric const InputInfo &Input = 4634bdd1243dSDimitry Andric IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0]; 46350b57cec5SDimitry Andric 463681ad6265SDimitry Andric InputInfoList ExtractAPIInputs; 463781ad6265SDimitry Andric InputInfoList HostOffloadingInputs; 46380b57cec5SDimitry Andric const InputInfo *CudaDeviceInput = nullptr; 46390b57cec5SDimitry Andric const InputInfo *OpenMPDeviceInput = nullptr; 46400b57cec5SDimitry Andric for (const InputInfo &I : Inputs) { 4641bdd1243dSDimitry Andric if (&I == &Input || I.getType() == types::TY_Nothing) { 4642bdd1243dSDimitry Andric // This is the primary input or contains nothing. 464381ad6265SDimitry Andric } else if (IsExtractAPI) { 464481ad6265SDimitry Andric auto ExpectedInputType = ExtractAPIPlaceholderInput.getType(); 464581ad6265SDimitry Andric if (I.getType() != ExpectedInputType) { 464681ad6265SDimitry Andric D.Diag(diag::err_drv_extract_api_wrong_kind) 464781ad6265SDimitry Andric << I.getFilename() << types::getTypeName(I.getType()) 464881ad6265SDimitry Andric << types::getTypeName(ExpectedInputType); 464981ad6265SDimitry Andric } 465081ad6265SDimitry Andric ExtractAPIInputs.push_back(I); 465181ad6265SDimitry Andric } else if (IsHostOffloadingAction) { 465281ad6265SDimitry Andric HostOffloadingInputs.push_back(I); 46530b57cec5SDimitry Andric } else if ((IsCuda || IsHIP) && !CudaDeviceInput) { 46540b57cec5SDimitry Andric CudaDeviceInput = &I; 46550b57cec5SDimitry Andric } else if (IsOpenMPDevice && !OpenMPDeviceInput) { 46560b57cec5SDimitry Andric OpenMPDeviceInput = &I; 46570b57cec5SDimitry Andric } else { 46580b57cec5SDimitry Andric llvm_unreachable("unexpectedly given multiple inputs"); 46590b57cec5SDimitry Andric } 46600b57cec5SDimitry Andric } 46610b57cec5SDimitry Andric 46625ffd83dbSDimitry Andric const llvm::Triple *AuxTriple = 46635ffd83dbSDimitry Andric (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr; 46640b57cec5SDimitry Andric bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment(); 46650b57cec5SDimitry Andric bool IsIAMCU = RawTriple.isOSIAMCU(); 46660b57cec5SDimitry Andric 46670b57cec5SDimitry Andric // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in 46680b57cec5SDimitry Andric // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not 46690b57cec5SDimitry Andric // Windows), we need to pass Windows-specific flags to cc1. 46700b57cec5SDimitry Andric if (IsCuda || IsHIP) 46710b57cec5SDimitry Andric IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment(); 46720b57cec5SDimitry Andric 46730b57cec5SDimitry Andric // C++ is not supported for IAMCU. 46740b57cec5SDimitry Andric if (IsIAMCU && types::isCXX(Input.getType())) 46750b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU"; 46760b57cec5SDimitry Andric 46770b57cec5SDimitry Andric // Invoke ourselves in -cc1 mode. 46780b57cec5SDimitry Andric // 46790b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 46800b57cec5SDimitry Andric CmdArgs.push_back("-cc1"); 46810b57cec5SDimitry Andric 46820b57cec5SDimitry Andric // Add the "effective" target triple. 46830b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 46840b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 46850b57cec5SDimitry Andric 46860b57cec5SDimitry Andric if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) { 46870b57cec5SDimitry Andric DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args); 46880b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_MJ); 4689a7dea167SDimitry Andric } else if (const Arg *GenCDBFragment = 4690a7dea167SDimitry Andric Args.getLastArg(options::OPT_gen_cdb_fragment_path)) { 4691a7dea167SDimitry Andric DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C, 4692a7dea167SDimitry Andric TripleStr, Output, Input, Args); 4693a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path); 46940b57cec5SDimitry Andric } 46950b57cec5SDimitry Andric 46960b57cec5SDimitry Andric if (IsCuda || IsHIP) { 46970b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for a CUDA/HIP device 46980b57cec5SDimitry Andric // and vice-versa. 46990b57cec5SDimitry Andric std::string NormalizedTriple; 47000b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 47010b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 47020b57cec5SDimitry Andric NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>() 47030b57cec5SDimitry Andric ->getTriple() 47040b57cec5SDimitry Andric .normalize(); 47050b57cec5SDimitry Andric else { 47060b57cec5SDimitry Andric // Host-side compilation. 47070b57cec5SDimitry Andric NormalizedTriple = 47080b57cec5SDimitry Andric (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() 47090b57cec5SDimitry Andric : C.getSingleOffloadToolChain<Action::OFK_HIP>()) 47100b57cec5SDimitry Andric ->getTriple() 47110b57cec5SDimitry Andric .normalize(); 47120b57cec5SDimitry Andric if (IsCuda) { 47130b57cec5SDimitry Andric // We need to figure out which CUDA version we're compiling for, as that 47140b57cec5SDimitry Andric // determines how we load and launch GPU kernels. 47150b57cec5SDimitry Andric auto *CTC = static_cast<const toolchains::CudaToolChain *>( 47160b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 47170b57cec5SDimitry Andric assert(CTC && "Expected valid CUDA Toolchain."); 47180b57cec5SDimitry Andric if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN) 47190b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 47200b57cec5SDimitry Andric Twine("-target-sdk-version=") + 47210b57cec5SDimitry Andric CudaVersionToString(CTC->CudaInstallation.version()))); 472206c3fb27SDimitry Andric // Unsized function arguments used for variadics were introduced in 472306c3fb27SDimitry Andric // CUDA-9.0. We still do not support generating code that actually uses 472406c3fb27SDimitry Andric // variadic arguments yet, but we do need to allow parsing them as 472506c3fb27SDimitry Andric // recent CUDA headers rely on that. 472606c3fb27SDimitry Andric // https://github.com/llvm/llvm-project/issues/58410 472706c3fb27SDimitry Andric if (CTC->CudaInstallation.version() >= CudaVersion::CUDA_90) 472806c3fb27SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 47290b57cec5SDimitry Andric } 47300b57cec5SDimitry Andric } 47310b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 47320b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 473306c3fb27SDimitry Andric 473406c3fb27SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_HIP) && 473506c3fb27SDimitry Andric getToolChain().getTriple().isAMDGPU()) { 473606c3fb27SDimitry Andric // Device side compilation printf 473706c3fb27SDimitry Andric if (Args.getLastArg(options::OPT_mprintf_kind_EQ)) { 473806c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 473906c3fb27SDimitry Andric "-mprintf-kind=" + 474006c3fb27SDimitry Andric Args.getLastArgValue(options::OPT_mprintf_kind_EQ))); 474106c3fb27SDimitry Andric // Force compiler error on invalid conversion specifiers 474206c3fb27SDimitry Andric CmdArgs.push_back( 474306c3fb27SDimitry Andric Args.MakeArgString("-Werror=format-invalid-specifier")); 47440b57cec5SDimitry Andric } 474506c3fb27SDimitry Andric } 474606c3fb27SDimitry Andric } 474706c3fb27SDimitry Andric 474806c3fb27SDimitry Andric // Unconditionally claim the printf option now to avoid unused diagnostic. 474906c3fb27SDimitry Andric if (const Arg *PF = Args.getLastArg(options::OPT_mprintf_kind_EQ)) 475006c3fb27SDimitry Andric PF->claim(); 47510b57cec5SDimitry Andric 47525ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { 47535ffd83dbSDimitry Andric CmdArgs.push_back("-fsycl-is-device"); 47545ffd83dbSDimitry Andric 47555ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { 47565ffd83dbSDimitry Andric A->render(Args, CmdArgs); 47575ffd83dbSDimitry Andric } else { 4758fe6060f1SDimitry Andric // Ensure the default version in SYCL mode is 2020. 4759fe6060f1SDimitry Andric CmdArgs.push_back("-sycl-std=2020"); 47605ffd83dbSDimitry Andric } 47615ffd83dbSDimitry Andric } 47625ffd83dbSDimitry Andric 47630b57cec5SDimitry Andric if (IsOpenMPDevice) { 47640b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for an OpenMP device. 47650b57cec5SDimitry Andric std::string NormalizedTriple = 47660b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Host>() 47670b57cec5SDimitry Andric ->getTriple() 47680b57cec5SDimitry Andric .normalize(); 47690b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 47700b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 47710b57cec5SDimitry Andric } 47720b57cec5SDimitry Andric 47730b57cec5SDimitry Andric if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm || 47740b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::thumb)) { 47750b57cec5SDimitry Andric unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6; 47765ffd83dbSDimitry Andric unsigned Version = 0; 47775ffd83dbSDimitry Andric bool Failure = 47785ffd83dbSDimitry Andric Triple.getArchName().substr(Offset).consumeInteger(10, Version); 47795ffd83dbSDimitry Andric if (Failure || Version < 7) 47800b57cec5SDimitry Andric D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName() 47810b57cec5SDimitry Andric << TripleStr; 47820b57cec5SDimitry Andric } 47830b57cec5SDimitry Andric 47840b57cec5SDimitry Andric // Push all default warning arguments that are specific to 47850b57cec5SDimitry Andric // the given target. These come before user provided warning options 47860b57cec5SDimitry Andric // are provided. 47870b57cec5SDimitry Andric TC.addClangWarningOptions(CmdArgs); 47880b57cec5SDimitry Andric 4789fe6060f1SDimitry Andric // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions. 4790bdd1243dSDimitry Andric if (Triple.isSPIR() || Triple.isSPIRV()) 4791fe6060f1SDimitry Andric CmdArgs.push_back("-Wspir-compat"); 4792fe6060f1SDimitry Andric 47930b57cec5SDimitry Andric // Select the appropriate action. 47940b57cec5SDimitry Andric RewriteKind rewriteKind = RK_None; 47950b57cec5SDimitry Andric 479606c3fb27SDimitry Andric bool UnifiedLTO = false; 479706c3fb27SDimitry Andric if (IsUsingLTO) { 479806c3fb27SDimitry Andric UnifiedLTO = Args.hasFlag(options::OPT_funified_lto, 479906c3fb27SDimitry Andric options::OPT_fno_unified_lto, Triple.isPS()); 480006c3fb27SDimitry Andric if (UnifiedLTO) 480106c3fb27SDimitry Andric CmdArgs.push_back("-funified-lto"); 480206c3fb27SDimitry Andric } 480306c3fb27SDimitry Andric 4804a7dea167SDimitry Andric // If CollectArgsForIntegratedAssembler() isn't called below, claim the args 4805a7dea167SDimitry Andric // it claims when not running an assembler. Otherwise, clang would emit 4806a7dea167SDimitry Andric // "argument unused" warnings for assembler flags when e.g. adding "-E" to 4807a7dea167SDimitry Andric // flags while debugging something. That'd be somewhat inconvenient, and it's 4808a7dea167SDimitry Andric // also inconsistent with most other flags -- we don't warn on 4809a7dea167SDimitry Andric // -ffunction-sections not being used in -E mode either for example, even 4810a7dea167SDimitry Andric // though it's not really used either. 4811a7dea167SDimitry Andric if (!isa<AssembleJobAction>(JA)) { 4812a7dea167SDimitry Andric // The args claimed here should match the args used in 4813a7dea167SDimitry Andric // CollectArgsForIntegratedAssembler(). 4814a7dea167SDimitry Andric if (TC.useIntegratedAs()) { 4815a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mrelax_all); 4816a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_relax_all); 4817a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible); 4818a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible); 4819a7dea167SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 4820a7dea167SDimitry Andric case llvm::Triple::arm: 4821a7dea167SDimitry Andric case llvm::Triple::armeb: 4822a7dea167SDimitry Andric case llvm::Triple::thumb: 4823a7dea167SDimitry Andric case llvm::Triple::thumbeb: 4824a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ); 4825a7dea167SDimitry Andric break; 4826a7dea167SDimitry Andric default: 4827a7dea167SDimitry Andric break; 4828a7dea167SDimitry Andric } 4829a7dea167SDimitry Andric } 4830a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Wa_COMMA); 4831a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Xassembler); 483281ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_femit_dwarf_unwind_EQ); 4833a7dea167SDimitry Andric } 4834a7dea167SDimitry Andric 48350b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) { 48360b57cec5SDimitry Andric assert(JA.getType() == types::TY_Plist && "Invalid output type."); 48370b57cec5SDimitry Andric CmdArgs.push_back("-analyze"); 48380b57cec5SDimitry Andric } else if (isa<MigrateJobAction>(JA)) { 48390b57cec5SDimitry Andric CmdArgs.push_back("-migrate"); 48400b57cec5SDimitry Andric } else if (isa<PreprocessJobAction>(JA)) { 48410b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) 48420b57cec5SDimitry Andric CmdArgs.push_back("-Eonly"); 48430b57cec5SDimitry Andric else { 48440b57cec5SDimitry Andric CmdArgs.push_back("-E"); 48450b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc) && 48460b57cec5SDimitry Andric !Args.hasArg(options::OPT_g_Group)) 48470b57cec5SDimitry Andric CmdArgs.push_back("-P"); 484881ad6265SDimitry Andric else if (JA.getType() == types::TY_PP_CXXHeaderUnit) 484981ad6265SDimitry Andric CmdArgs.push_back("-fdirectives-only"); 48500b57cec5SDimitry Andric } 48510b57cec5SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 48520b57cec5SDimitry Andric CmdArgs.push_back("-emit-obj"); 48530b57cec5SDimitry Andric 48540b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 48550b57cec5SDimitry Andric 48560b57cec5SDimitry Andric // Also ignore explicit -force_cpusubtype_ALL option. 48570b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 48580b57cec5SDimitry Andric } else if (isa<PrecompileJobAction>(JA)) { 48590b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) 48600b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 48610b57cec5SDimitry Andric else if (JA.getType() == types::TY_ModuleFile) 4862bdd1243dSDimitry Andric CmdArgs.push_back("-emit-module-interface"); 486381ad6265SDimitry Andric else if (JA.getType() == types::TY_HeaderUnit) 486481ad6265SDimitry Andric CmdArgs.push_back("-emit-header-unit"); 48650b57cec5SDimitry Andric else 48660b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 48670b57cec5SDimitry Andric } else if (isa<VerifyPCHJobAction>(JA)) { 48680b57cec5SDimitry Andric CmdArgs.push_back("-verify-pch"); 486981ad6265SDimitry Andric } else if (isa<ExtractAPIJobAction>(JA)) { 487081ad6265SDimitry Andric assert(JA.getType() == types::TY_API_INFO && 487181ad6265SDimitry Andric "Extract API actions must generate a API information."); 487281ad6265SDimitry Andric CmdArgs.push_back("-extract-api"); 487381ad6265SDimitry Andric if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ)) 487481ad6265SDimitry Andric ProductNameArg->render(Args, CmdArgs); 4875bdd1243dSDimitry Andric if (Arg *ExtractAPIIgnoresFileArg = 4876bdd1243dSDimitry Andric Args.getLastArg(options::OPT_extract_api_ignores_EQ)) 4877bdd1243dSDimitry Andric ExtractAPIIgnoresFileArg->render(Args, CmdArgs); 48780b57cec5SDimitry Andric } else { 48790b57cec5SDimitry Andric assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) && 48800b57cec5SDimitry Andric "Invalid action for clang tool."); 48810b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) { 48820b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 48830b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 48840b57cec5SDimitry Andric JA.getType() == types::TY_LTO_IR) { 48850b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm"); 48860b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 48870b57cec5SDimitry Andric JA.getType() == types::TY_LTO_BC) { 4888fe6060f1SDimitry Andric // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S 4889fe6060f1SDimitry Andric if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) && 4890fe6060f1SDimitry Andric Args.hasArg(options::OPT_emit_llvm)) { 4891fe6060f1SDimitry Andric CmdArgs.push_back("-emit-llvm"); 4892fe6060f1SDimitry Andric } else { 48930b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 4894fe6060f1SDimitry Andric } 4895a7dea167SDimitry Andric } else if (JA.getType() == types::TY_IFS || 4896a7dea167SDimitry Andric JA.getType() == types::TY_IFS_CPP) { 4897a7dea167SDimitry Andric StringRef ArgStr = 4898a7dea167SDimitry Andric Args.hasArg(options::OPT_interface_stub_version_EQ) 4899a7dea167SDimitry Andric ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ) 4900fe6060f1SDimitry Andric : "ifs-v1"; 49010b57cec5SDimitry Andric CmdArgs.push_back("-emit-interface-stubs"); 49020b57cec5SDimitry Andric CmdArgs.push_back( 4903a7dea167SDimitry Andric Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str())); 49040b57cec5SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 49050b57cec5SDimitry Andric CmdArgs.push_back("-S"); 49060b57cec5SDimitry Andric } else if (JA.getType() == types::TY_AST) { 49070b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 49080b57cec5SDimitry Andric } else if (JA.getType() == types::TY_ModuleFile) { 49090b57cec5SDimitry Andric CmdArgs.push_back("-module-file-info"); 49100b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenObjC) { 49110b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 49120b57cec5SDimitry Andric rewriteKind = RK_NonFragile; 49130b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 49140b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 49150b57cec5SDimitry Andric rewriteKind = RK_Fragile; 49160b57cec5SDimitry Andric } else { 49170b57cec5SDimitry Andric assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!"); 49180b57cec5SDimitry Andric } 49190b57cec5SDimitry Andric 49200b57cec5SDimitry Andric // Preserve use-list order by default when emitting bitcode, so that 49210b57cec5SDimitry Andric // loading the bitcode up in 'opt' or 'llc' and running passes gives the 49220b57cec5SDimitry Andric // same result as running passes here. For LTO, we don't need to preserve 49230b57cec5SDimitry Andric // the use-list order, since serialization to bitcode is part of the flow. 49240b57cec5SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 49250b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-uselists"); 49260b57cec5SDimitry Andric 492781ad6265SDimitry Andric if (IsUsingLTO) { 4928bdd1243dSDimitry Andric if (IsDeviceOffloadAction && !JA.isDeviceOffloading(Action::OFK_OpenMP) && 492981ad6265SDimitry Andric !Args.hasFlag(options::OPT_offload_new_driver, 493081ad6265SDimitry Andric options::OPT_no_offload_new_driver, false) && 493181ad6265SDimitry Andric !Triple.isAMDGPU()) { 4932fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4933fe6060f1SDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4934fe6060f1SDimitry Andric options::OPT_foffload_lto_EQ) 4935fe6060f1SDimitry Andric ->getAsString(Args) 4936fe6060f1SDimitry Andric << Triple.getTriple(); 4937bdd1243dSDimitry Andric } else if (Triple.isNVPTX() && !IsRDCMode && 4938bdd1243dSDimitry Andric JA.isDeviceOffloading(Action::OFK_Cuda)) { 4939bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_language_mode) 4940bdd1243dSDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4941bdd1243dSDimitry Andric options::OPT_foffload_lto_EQ) 4942bdd1243dSDimitry Andric ->getAsString(Args) 4943bdd1243dSDimitry Andric << "-fno-gpu-rdc"; 4944349cc55cSDimitry Andric } else { 4945349cc55cSDimitry Andric assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); 4946349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 4947349cc55cSDimitry Andric Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 494806c3fb27SDimitry Andric // PS4 uses the legacy LTO API, which does not support some of the 494906c3fb27SDimitry Andric // features enabled by -flto-unit. 495006c3fb27SDimitry Andric if (!RawTriple.isPS4() || 495106c3fb27SDimitry Andric (D.getLTOMode() == LTOK_Full) || !UnifiedLTO) 4952349cc55cSDimitry Andric CmdArgs.push_back("-flto-unit"); 4953fe6060f1SDimitry Andric } 49540b57cec5SDimitry Andric } 49550b57cec5SDimitry Andric } 49560b57cec5SDimitry Andric 495706c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dumpdir); 495806c3fb27SDimitry Andric 49590b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) { 49600b57cec5SDimitry Andric if (!types::isLLVMIR(Input.getType())) 49610b57cec5SDimitry Andric D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args); 49620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ); 49630b57cec5SDimitry Andric } 49640b57cec5SDimitry Andric 4965480093f4SDimitry Andric if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ)) 4966480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ); 4967480093f4SDimitry Andric 49680b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 49690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ); 49700b57cec5SDimitry Andric 4971e8d8bef9SDimitry Andric auto *MemProfArg = Args.getLastArg(options::OPT_fmemory_profile, 4972e8d8bef9SDimitry Andric options::OPT_fmemory_profile_EQ, 4973e8d8bef9SDimitry Andric options::OPT_fno_memory_profile); 4974e8d8bef9SDimitry Andric if (MemProfArg && 4975e8d8bef9SDimitry Andric !MemProfArg->getOption().matches(options::OPT_fno_memory_profile)) 4976e8d8bef9SDimitry Andric MemProfArg->render(Args, CmdArgs); 4977e8d8bef9SDimitry Andric 497806c3fb27SDimitry Andric if (auto *MemProfUseArg = 497906c3fb27SDimitry Andric Args.getLastArg(options::OPT_fmemory_profile_use_EQ)) { 498006c3fb27SDimitry Andric if (MemProfArg) 498106c3fb27SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 498206c3fb27SDimitry Andric << MemProfUseArg->getAsString(Args) << MemProfArg->getAsString(Args); 498306c3fb27SDimitry Andric if (auto *PGOInstrArg = Args.getLastArg(options::OPT_fprofile_generate, 498406c3fb27SDimitry Andric options::OPT_fprofile_generate_EQ)) 498506c3fb27SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 498606c3fb27SDimitry Andric << MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args); 498706c3fb27SDimitry Andric MemProfUseArg->render(Args, CmdArgs); 498806c3fb27SDimitry Andric } 498906c3fb27SDimitry Andric 49900b57cec5SDimitry Andric // Embed-bitcode option. 49910b57cec5SDimitry Andric // Only white-listed flags below are allowed to be embedded. 4992fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO && 49930b57cec5SDimitry Andric (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) { 49940b57cec5SDimitry Andric // Add flags implied by -fembed-bitcode. 49950b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 49960b57cec5SDimitry Andric // Disable all llvm IR level optimizations. 49970b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 49980b57cec5SDimitry Andric 4999480093f4SDimitry Andric // Render target options. 50000b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 50010b57cec5SDimitry Andric 50020b57cec5SDimitry Andric // reject options that shouldn't be supported in bitcode 50030b57cec5SDimitry Andric // also reject kernel/kext 5004349cc55cSDimitry Andric static const constexpr unsigned kBitcodeOptionIgnorelist[] = { 50050b57cec5SDimitry Andric options::OPT_mkernel, 50060b57cec5SDimitry Andric options::OPT_fapple_kext, 50070b57cec5SDimitry Andric options::OPT_ffunction_sections, 50080b57cec5SDimitry Andric options::OPT_fno_function_sections, 50090b57cec5SDimitry Andric options::OPT_fdata_sections, 50100b57cec5SDimitry Andric options::OPT_fno_data_sections, 50115ffd83dbSDimitry Andric options::OPT_fbasic_block_sections_EQ, 50125ffd83dbSDimitry Andric options::OPT_funique_internal_linkage_names, 50135ffd83dbSDimitry Andric options::OPT_fno_unique_internal_linkage_names, 50140b57cec5SDimitry Andric options::OPT_funique_section_names, 50150b57cec5SDimitry Andric options::OPT_fno_unique_section_names, 50165ffd83dbSDimitry Andric options::OPT_funique_basic_block_section_names, 50175ffd83dbSDimitry Andric options::OPT_fno_unique_basic_block_section_names, 50180b57cec5SDimitry Andric options::OPT_mrestrict_it, 50190b57cec5SDimitry Andric options::OPT_mno_restrict_it, 50200b57cec5SDimitry Andric options::OPT_mstackrealign, 50210b57cec5SDimitry Andric options::OPT_mno_stackrealign, 50220b57cec5SDimitry Andric options::OPT_mstack_alignment, 50230b57cec5SDimitry Andric options::OPT_mcmodel_EQ, 50240b57cec5SDimitry Andric options::OPT_mlong_calls, 50250b57cec5SDimitry Andric options::OPT_mno_long_calls, 50260b57cec5SDimitry Andric options::OPT_ggnu_pubnames, 50270b57cec5SDimitry Andric options::OPT_gdwarf_aranges, 50280b57cec5SDimitry Andric options::OPT_fdebug_types_section, 50290b57cec5SDimitry Andric options::OPT_fno_debug_types_section, 50300b57cec5SDimitry Andric options::OPT_fdwarf_directory_asm, 50310b57cec5SDimitry Andric options::OPT_fno_dwarf_directory_asm, 50320b57cec5SDimitry Andric options::OPT_mrelax_all, 50330b57cec5SDimitry Andric options::OPT_mno_relax_all, 50340b57cec5SDimitry Andric options::OPT_ftrap_function_EQ, 50350b57cec5SDimitry Andric options::OPT_ffixed_r9, 50360b57cec5SDimitry Andric options::OPT_mfix_cortex_a53_835769, 50370b57cec5SDimitry Andric options::OPT_mno_fix_cortex_a53_835769, 50380b57cec5SDimitry Andric options::OPT_ffixed_x18, 50390b57cec5SDimitry Andric options::OPT_mglobal_merge, 50400b57cec5SDimitry Andric options::OPT_mno_global_merge, 50410b57cec5SDimitry Andric options::OPT_mred_zone, 50420b57cec5SDimitry Andric options::OPT_mno_red_zone, 50430b57cec5SDimitry Andric options::OPT_Wa_COMMA, 50440b57cec5SDimitry Andric options::OPT_Xassembler, 50450b57cec5SDimitry Andric options::OPT_mllvm, 50460b57cec5SDimitry Andric }; 50470b57cec5SDimitry Andric for (const auto &A : Args) 5048349cc55cSDimitry Andric if (llvm::is_contained(kBitcodeOptionIgnorelist, A->getOption().getID())) 50490b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling(); 50500b57cec5SDimitry Andric 50510b57cec5SDimitry Andric // Render the CodeGen options that need to be passed. 505281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 505381ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 50540b57cec5SDimitry Andric 50550b57cec5SDimitry Andric RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args, 50565ffd83dbSDimitry Andric CmdArgs, JA); 50570b57cec5SDimitry Andric 50580b57cec5SDimitry Andric // Render ABI arguments 50590b57cec5SDimitry Andric switch (TC.getArch()) { 50600b57cec5SDimitry Andric default: break; 50610b57cec5SDimitry Andric case llvm::Triple::arm: 50620b57cec5SDimitry Andric case llvm::Triple::armeb: 50630b57cec5SDimitry Andric case llvm::Triple::thumbeb: 5064349cc55cSDimitry Andric RenderARMABI(D, Triple, Args, CmdArgs); 50650b57cec5SDimitry Andric break; 50660b57cec5SDimitry Andric case llvm::Triple::aarch64: 5067480093f4SDimitry Andric case llvm::Triple::aarch64_32: 50680b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 50690b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 50700b57cec5SDimitry Andric break; 50710b57cec5SDimitry Andric } 50720b57cec5SDimitry Andric 50730b57cec5SDimitry Andric // Optimization level for CodeGen. 50740b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 50750b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 50760b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 50770b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 50780b57cec5SDimitry Andric } else { 50790b57cec5SDimitry Andric A->render(Args, CmdArgs); 50800b57cec5SDimitry Andric } 50810b57cec5SDimitry Andric } 50820b57cec5SDimitry Andric 50830b57cec5SDimitry Andric // Input/Output file. 50840b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 50850b57cec5SDimitry Andric // Handled with other dependency code. 50860b57cec5SDimitry Andric } else if (Output.isFilename()) { 50870b57cec5SDimitry Andric CmdArgs.push_back("-o"); 50880b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 50890b57cec5SDimitry Andric } else { 50900b57cec5SDimitry Andric assert(Output.isNothing() && "Input output."); 50910b57cec5SDimitry Andric } 50920b57cec5SDimitry Andric 50930b57cec5SDimitry Andric for (const auto &II : Inputs) { 50940b57cec5SDimitry Andric addDashXForInput(Args, II, CmdArgs); 50950b57cec5SDimitry Andric if (II.isFilename()) 50960b57cec5SDimitry Andric CmdArgs.push_back(II.getFilename()); 50970b57cec5SDimitry Andric else 50980b57cec5SDimitry Andric II.getInputArg().renderAsInput(Args, CmdArgs); 50990b57cec5SDimitry Andric } 51000b57cec5SDimitry Andric 5101e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>( 5102e8d8bef9SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(), 510306c3fb27SDimitry Andric CmdArgs, Inputs, Output, D.getPrependArg())); 51040b57cec5SDimitry Andric return; 51050b57cec5SDimitry Andric } 51060b57cec5SDimitry Andric 5107fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO) 51080b57cec5SDimitry Andric CmdArgs.push_back("-fembed-bitcode=marker"); 51090b57cec5SDimitry Andric 51100b57cec5SDimitry Andric // We normally speed up the clang process a bit by skipping destructors at 51110b57cec5SDimitry Andric // exit, but when we're generating diagnostics we can rely on some of the 51120b57cec5SDimitry Andric // cleanup. 51130b57cec5SDimitry Andric if (!C.isForDiagnostics()) 51140b57cec5SDimitry Andric CmdArgs.push_back("-disable-free"); 5115349cc55cSDimitry Andric CmdArgs.push_back("-clear-ast-before-backend"); 51160b57cec5SDimitry Andric 51170b57cec5SDimitry Andric #ifdef NDEBUG 51180b57cec5SDimitry Andric const bool IsAssertBuild = false; 51190b57cec5SDimitry Andric #else 51200b57cec5SDimitry Andric const bool IsAssertBuild = true; 51210b57cec5SDimitry Andric #endif 51220b57cec5SDimitry Andric 51230b57cec5SDimitry Andric // Disable the verification pass in -asserts builds. 51240b57cec5SDimitry Andric if (!IsAssertBuild) 51250b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-verifier"); 51260b57cec5SDimitry Andric 51270b57cec5SDimitry Andric // Discard value names in assert builds unless otherwise specified. 51280b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdiscard_value_names, 512947395794SDimitry Andric options::OPT_fno_discard_value_names, !IsAssertBuild)) { 513047395794SDimitry Andric if (Args.hasArg(options::OPT_fdiscard_value_names) && 5131349cc55cSDimitry Andric llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) { 513247395794SDimitry Andric return types::isLLVMIR(II.getType()); 5133349cc55cSDimitry Andric })) { 513447395794SDimitry Andric D.Diag(diag::warn_ignoring_fdiscard_for_bitcode); 513547395794SDimitry Andric } 51360b57cec5SDimitry Andric CmdArgs.push_back("-discard-value-names"); 513747395794SDimitry Andric } 51380b57cec5SDimitry Andric 51390b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 51400b57cec5SDimitry Andric // -save-temps. 51410b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 51420b57cec5SDimitry Andric CmdArgs.push_back(getBaseInputName(Args, Input)); 51430b57cec5SDimitry Andric 51440b57cec5SDimitry Andric // Some flags which affect the language (via preprocessor 51450b57cec5SDimitry Andric // defines). 51460b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 51470b57cec5SDimitry Andric CmdArgs.push_back("-static-define"); 51480b57cec5SDimitry Andric 51490b57cec5SDimitry Andric if (Args.hasArg(options::OPT_municode)) 51500b57cec5SDimitry Andric CmdArgs.push_back("-DUNICODE"); 51510b57cec5SDimitry Andric 51520b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) 51530b57cec5SDimitry Andric RenderAnalyzerOptions(Args, CmdArgs, Triple, Input); 51540b57cec5SDimitry Andric 5155a7dea167SDimitry Andric if (isa<AnalyzeJobAction>(JA) || 5156a7dea167SDimitry Andric (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze))) 5157a7dea167SDimitry Andric CmdArgs.push_back("-setup-static-analyzer"); 5158a7dea167SDimitry Andric 51590b57cec5SDimitry Andric // Enable compatilibily mode to avoid analyzer-config related errors. 51600b57cec5SDimitry Andric // Since we can't access frontend flags through hasArg, let's manually iterate 51610b57cec5SDimitry Andric // through them. 51620b57cec5SDimitry Andric bool FoundAnalyzerConfig = false; 5163bdd1243dSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 51640b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 51650b57cec5SDimitry Andric FoundAnalyzerConfig = true; 51660b57cec5SDimitry Andric break; 51670b57cec5SDimitry Andric } 51680b57cec5SDimitry Andric if (!FoundAnalyzerConfig) 5169bdd1243dSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xanalyzer)) 51700b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 51710b57cec5SDimitry Andric FoundAnalyzerConfig = true; 51720b57cec5SDimitry Andric break; 51730b57cec5SDimitry Andric } 51740b57cec5SDimitry Andric if (FoundAnalyzerConfig) 51750b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-config-compatibility-mode=true"); 51760b57cec5SDimitry Andric 51770b57cec5SDimitry Andric CheckCodeGenerationOptions(D, Args); 51780b57cec5SDimitry Andric 51790b57cec5SDimitry Andric unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args); 51800b57cec5SDimitry Andric assert(FunctionAlignment <= 31 && "function alignment will be truncated!"); 51810b57cec5SDimitry Andric if (FunctionAlignment) { 51820b57cec5SDimitry Andric CmdArgs.push_back("-function-alignment"); 51830b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment))); 51840b57cec5SDimitry Andric } 51850b57cec5SDimitry Andric 5186349cc55cSDimitry Andric // We support -falign-loops=N where N is a power of 2. GCC supports more 5187349cc55cSDimitry Andric // forms. 5188349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_falign_loops_EQ)) { 5189349cc55cSDimitry Andric unsigned Value = 0; 5190349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) 5191349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_invalid_int_value) 5192349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5193349cc55cSDimitry Andric else if (Value & (Value - 1)) 5194349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_alignment_not_power_of_two) 5195349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5196349cc55cSDimitry Andric // Treat =0 as unspecified (use the target preference). 5197349cc55cSDimitry Andric if (Value) 5198349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-falign-loops=" + 5199349cc55cSDimitry Andric Twine(std::min(Value, 65536u)))); 5200349cc55cSDimitry Andric } 5201349cc55cSDimitry Andric 520206c3fb27SDimitry Andric if (Triple.isOSzOS()) { 520306c3fb27SDimitry Andric // On z/OS some of the system header feature macros need to 520406c3fb27SDimitry Andric // be defined to enable most cross platform projects to build 520506c3fb27SDimitry Andric // successfully. Ths include the libc++ library. A 520606c3fb27SDimitry Andric // complicating factor is that users can define these 520706c3fb27SDimitry Andric // macros to the same or different values. We need to add 520806c3fb27SDimitry Andric // the definition for these macros to the compilation command 520906c3fb27SDimitry Andric // if the user hasn't already defined them. 521006c3fb27SDimitry Andric 521106c3fb27SDimitry Andric auto findMacroDefinition = [&](const std::string &Macro) { 521206c3fb27SDimitry Andric auto MacroDefs = Args.getAllArgValues(options::OPT_D); 521306c3fb27SDimitry Andric return std::find_if(MacroDefs.begin(), MacroDefs.end(), 521406c3fb27SDimitry Andric [&](const std::string &M) { 521506c3fb27SDimitry Andric return M == Macro || 521606c3fb27SDimitry Andric M.find(Macro + '=') != std::string::npos; 521706c3fb27SDimitry Andric }) != MacroDefs.end(); 521806c3fb27SDimitry Andric }; 521906c3fb27SDimitry Andric 522006c3fb27SDimitry Andric // _UNIX03_WITHDRAWN is required for libcxx & porting. 522106c3fb27SDimitry Andric if (!findMacroDefinition("_UNIX03_WITHDRAWN")) 522206c3fb27SDimitry Andric CmdArgs.push_back("-D_UNIX03_WITHDRAWN"); 522306c3fb27SDimitry Andric // _OPEN_DEFAULT is required for XL compat 522406c3fb27SDimitry Andric if (!findMacroDefinition("_OPEN_DEFAULT")) 522506c3fb27SDimitry Andric CmdArgs.push_back("-D_OPEN_DEFAULT"); 522606c3fb27SDimitry Andric if (D.CCCIsCXX() || types::isCXX(Input.getType())) { 522706c3fb27SDimitry Andric // _XOPEN_SOURCE=600 is required for libcxx. 522806c3fb27SDimitry Andric if (!findMacroDefinition("_XOPEN_SOURCE")) 522906c3fb27SDimitry Andric CmdArgs.push_back("-D_XOPEN_SOURCE=600"); 523006c3fb27SDimitry Andric } 523106c3fb27SDimitry Andric } 523206c3fb27SDimitry Andric 52330b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 52340b57cec5SDimitry Andric unsigned PICLevel; 52350b57cec5SDimitry Andric bool IsPIE; 52360b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); 5237bdd1243dSDimitry Andric Arg *LastPICDataRelArg = 5238bdd1243dSDimitry Andric Args.getLastArg(options::OPT_mno_pic_data_is_text_relative, 5239bdd1243dSDimitry Andric options::OPT_mpic_data_is_text_relative); 5240bdd1243dSDimitry Andric bool NoPICDataIsTextRelative = false; 5241bdd1243dSDimitry Andric if (LastPICDataRelArg) { 5242bdd1243dSDimitry Andric if (LastPICDataRelArg->getOption().matches( 5243bdd1243dSDimitry Andric options::OPT_mno_pic_data_is_text_relative)) { 5244bdd1243dSDimitry Andric NoPICDataIsTextRelative = true; 5245bdd1243dSDimitry Andric if (!PICLevel) 5246bdd1243dSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 5247bdd1243dSDimitry Andric << "-mno-pic-data-is-text-relative" 5248bdd1243dSDimitry Andric << "-fpic/-fpie"; 5249bdd1243dSDimitry Andric } 5250bdd1243dSDimitry Andric if (!Triple.isSystemZ()) 5251bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5252bdd1243dSDimitry Andric << (NoPICDataIsTextRelative ? "-mno-pic-data-is-text-relative" 5253bdd1243dSDimitry Andric : "-mpic-data-is-text-relative") 5254bdd1243dSDimitry Andric << RawTriple.str(); 5255bdd1243dSDimitry Andric } 52560b57cec5SDimitry Andric 52575ffd83dbSDimitry Andric bool IsROPI = RelocationModel == llvm::Reloc::ROPI || 52585ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 52595ffd83dbSDimitry Andric bool IsRWPI = RelocationModel == llvm::Reloc::RWPI || 52605ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 52610b57cec5SDimitry Andric 52625ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_mcmse) && 52635ffd83dbSDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) { 52645ffd83dbSDimitry Andric if (IsROPI) 52655ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << IsROPI; 52665ffd83dbSDimitry Andric if (IsRWPI) 52675ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << !IsRWPI; 52685ffd83dbSDimitry Andric } 52695ffd83dbSDimitry Andric 52705ffd83dbSDimitry Andric if (IsROPI && types::isCXX(Input.getType()) && 52710b57cec5SDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) 52720b57cec5SDimitry Andric D.Diag(diag::err_drv_ropi_incompatible_with_cxx); 52730b57cec5SDimitry Andric 52745ffd83dbSDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 52750b57cec5SDimitry Andric if (RMName) { 52760b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 52770b57cec5SDimitry Andric CmdArgs.push_back(RMName); 52780b57cec5SDimitry Andric } 52790b57cec5SDimitry Andric if (PICLevel > 0) { 52800b57cec5SDimitry Andric CmdArgs.push_back("-pic-level"); 52810b57cec5SDimitry Andric CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); 52820b57cec5SDimitry Andric if (IsPIE) 52830b57cec5SDimitry Andric CmdArgs.push_back("-pic-is-pie"); 5284bdd1243dSDimitry Andric if (NoPICDataIsTextRelative) 5285bdd1243dSDimitry Andric CmdArgs.push_back("-mcmodel=medium"); 52860b57cec5SDimitry Andric } 52870b57cec5SDimitry Andric 52880b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::ROPI || 52890b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 52900b57cec5SDimitry Andric CmdArgs.push_back("-fropi"); 52910b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::RWPI || 52920b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 52930b57cec5SDimitry Andric CmdArgs.push_back("-frwpi"); 52940b57cec5SDimitry Andric 52950b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_meabi)) { 52960b57cec5SDimitry Andric CmdArgs.push_back("-meabi"); 52970b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 52980b57cec5SDimitry Andric } 52990b57cec5SDimitry Andric 5300e8d8bef9SDimitry Andric // -fsemantic-interposition is forwarded to CC1: set the 5301e8d8bef9SDimitry Andric // "SemanticInterposition" metadata to 1 (make some linkages interposable) and 5302e8d8bef9SDimitry Andric // make default visibility external linkage definitions dso_preemptable. 5303e8d8bef9SDimitry Andric // 5304e8d8bef9SDimitry Andric // -fno-semantic-interposition: if the target supports .Lfoo$local local 5305e8d8bef9SDimitry Andric // aliases (make default visibility external linkage definitions dso_local). 5306e8d8bef9SDimitry Andric // This is the CC1 default for ELF to match COFF/Mach-O. 5307e8d8bef9SDimitry Andric // 5308e8d8bef9SDimitry Andric // Otherwise use Clang's traditional behavior: like 5309e8d8bef9SDimitry Andric // -fno-semantic-interposition but local aliases are not used. So references 5310e8d8bef9SDimitry Andric // can be interposed if not optimized out. 5311e8d8bef9SDimitry Andric if (Triple.isOSBinFormatELF()) { 5312e8d8bef9SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fsemantic_interposition, 5313e8d8bef9SDimitry Andric options::OPT_fno_semantic_interposition); 5314e8d8bef9SDimitry Andric if (RelocationModel != llvm::Reloc::Static && !IsPIE) { 5315e8d8bef9SDimitry Andric // The supported targets need to call AsmPrinter::getSymbolPreferLocal. 5316fe6060f1SDimitry Andric bool SupportsLocalAlias = 5317fe6060f1SDimitry Andric Triple.isAArch64() || Triple.isRISCV() || Triple.isX86(); 5318e8d8bef9SDimitry Andric if (!A) 5319e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5320e8d8bef9SDimitry Andric else if (A->getOption().matches(options::OPT_fsemantic_interposition)) 53215ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5322e8d8bef9SDimitry Andric else if (!SupportsLocalAlias) 5323e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5324e8d8bef9SDimitry Andric } 5325e8d8bef9SDimitry Andric } 53265ffd83dbSDimitry Andric 53275ffd83dbSDimitry Andric { 53285ffd83dbSDimitry Andric std::string Model; 53290b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) { 53300b57cec5SDimitry Andric if (!TC.isThreadModelSupported(A->getValue())) 53310b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_thread_model_for_target) 53320b57cec5SDimitry Andric << A->getValue() << A->getAsString(Args); 53335ffd83dbSDimitry Andric Model = A->getValue(); 53345ffd83dbSDimitry Andric } else 53355ffd83dbSDimitry Andric Model = TC.getThreadModel(); 53365ffd83dbSDimitry Andric if (Model != "posix") { 53375ffd83dbSDimitry Andric CmdArgs.push_back("-mthread-model"); 53385ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Model)); 53390b57cec5SDimitry Andric } 53405ffd83dbSDimitry Andric } 53410b57cec5SDimitry Andric 5342bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fveclib)) { 5343bdd1243dSDimitry Andric StringRef Name = A->getValue(); 5344bdd1243dSDimitry Andric if (Name == "SVML") { 5345bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::x86 && 5346bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::x86_64) 5347bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5348bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5349bdd1243dSDimitry Andric } else if (Name == "LIBMVEC-X86") { 5350bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::x86 && 5351bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::x86_64) 5352bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5353bdd1243dSDimitry Andric << Name << Triple.getArchName(); 535406c3fb27SDimitry Andric } else if (Name == "SLEEF" || Name == "ArmPL") { 5355bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::aarch64 && 5356bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::aarch64_be) 5357bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5358bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5359bdd1243dSDimitry Andric } 5360bdd1243dSDimitry Andric A->render(Args, CmdArgs); 5361bdd1243dSDimitry Andric } 53620b57cec5SDimitry Andric 53630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmerge_all_constants, 53640b57cec5SDimitry Andric options::OPT_fno_merge_all_constants, false)) 53650b57cec5SDimitry Andric CmdArgs.push_back("-fmerge-all-constants"); 53660b57cec5SDimitry Andric 5367bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdelete_null_pointer_checks, 5368bdd1243dSDimitry Andric options::OPT_fno_delete_null_pointer_checks); 53690b57cec5SDimitry Andric 53700b57cec5SDimitry Andric // LLVM Code Generator Options. 53710b57cec5SDimitry Andric 5372972a253aSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_quadword_atomics)) { 5373972a253aSDimitry Andric if (!Triple.isOSAIX() || Triple.isPPC32()) 5374972a253aSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5375972a253aSDimitry Andric << A->getSpelling() << RawTriple.str(); 5376972a253aSDimitry Andric CmdArgs.push_back("-mabi=quadword-atomics"); 5377972a253aSDimitry Andric } 5378972a253aSDimitry Andric 5379fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) { 5380fe6060f1SDimitry Andric // Emit the unsupported option error until the Clang's library integration 5381fe6060f1SDimitry Andric // support for 128-bit long double is available for AIX. 5382fe6060f1SDimitry Andric if (Triple.isOSAIX()) 5383fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5384fe6060f1SDimitry Andric << A->getSpelling() << RawTriple.str(); 5385e8d8bef9SDimitry Andric } 5386e8d8bef9SDimitry Andric 53870b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) { 538806c3fb27SDimitry Andric StringRef V = A->getValue(), V1 = V; 538906c3fb27SDimitry Andric unsigned Size; 539006c3fb27SDimitry Andric if (V1.consumeInteger(10, Size) || !V1.empty()) 539106c3fb27SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 539206c3fb27SDimitry Andric << V << A->getOption().getName(); 5393fe6060f1SDimitry Andric else 539406c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + V)); 53950b57cec5SDimitry Andric } 53960b57cec5SDimitry Andric 539781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fjump_tables, 539881ad6265SDimitry Andric options::OPT_fno_jump_tables); 539981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fprofile_sample_accurate, 540081ad6265SDimitry Andric options::OPT_fno_profile_sample_accurate); 540181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fpreserve_as_comments, 540281ad6265SDimitry Andric options::OPT_fno_preserve_as_comments); 54030b57cec5SDimitry Andric 54040b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 54050b57cec5SDimitry Andric CmdArgs.push_back("-mregparm"); 54060b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 54070b57cec5SDimitry Andric } 54080b57cec5SDimitry Andric 5409e86cf8adSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_maix_struct_return, 5410e86cf8adSDimitry Andric options::OPT_msvr4_struct_return)) { 5411e8d8bef9SDimitry Andric if (!TC.getTriple().isPPC32()) { 5412e86cf8adSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5413e86cf8adSDimitry Andric << A->getSpelling() << RawTriple.str(); 5414e86cf8adSDimitry Andric } else if (A->getOption().matches(options::OPT_maix_struct_return)) { 5415e86cf8adSDimitry Andric CmdArgs.push_back("-maix-struct-return"); 5416e86cf8adSDimitry Andric } else { 5417e86cf8adSDimitry Andric assert(A->getOption().matches(options::OPT_msvr4_struct_return)); 5418e86cf8adSDimitry Andric CmdArgs.push_back("-msvr4-struct-return"); 5419e86cf8adSDimitry Andric } 5420e86cf8adSDimitry Andric } 5421e86cf8adSDimitry Andric 54220b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 54230b57cec5SDimitry Andric options::OPT_freg_struct_return)) { 54240b57cec5SDimitry Andric if (TC.getArch() != llvm::Triple::x86) { 54250b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 54260b57cec5SDimitry Andric << A->getSpelling() << RawTriple.str(); 54270b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 54280b57cec5SDimitry Andric CmdArgs.push_back("-fpcc-struct-return"); 54290b57cec5SDimitry Andric } else { 54300b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_freg_struct_return)); 54310b57cec5SDimitry Andric CmdArgs.push_back("-freg-struct-return"); 54320b57cec5SDimitry Andric } 54330b57cec5SDimitry Andric } 54340b57cec5SDimitry Andric 54350b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) 54360b57cec5SDimitry Andric CmdArgs.push_back("-fdefault-calling-conv=stdcall"); 54370b57cec5SDimitry Andric 54385ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_fenable_matrix)) { 54395ffd83dbSDimitry Andric // enable-matrix is needed by both the LangOpts and by LLVM. 54405ffd83dbSDimitry Andric CmdArgs.push_back("-fenable-matrix"); 54415ffd83dbSDimitry Andric CmdArgs.push_back("-mllvm"); 54425ffd83dbSDimitry Andric CmdArgs.push_back("-enable-matrix"); 54435ffd83dbSDimitry Andric } 54445ffd83dbSDimitry Andric 5445a7dea167SDimitry Andric CodeGenOptions::FramePointerKind FPKeepKind = 5446a7dea167SDimitry Andric getFramePointerKind(Args, RawTriple); 5447a7dea167SDimitry Andric const char *FPKeepKindStr = nullptr; 5448a7dea167SDimitry Andric switch (FPKeepKind) { 5449a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::None: 5450a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=none"; 5451a7dea167SDimitry Andric break; 5452a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::NonLeaf: 5453a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=non-leaf"; 5454a7dea167SDimitry Andric break; 5455a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::All: 5456a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=all"; 5457a7dea167SDimitry Andric break; 5458a7dea167SDimitry Andric } 5459a7dea167SDimitry Andric assert(FPKeepKindStr && "unknown FramePointerKind"); 5460a7dea167SDimitry Andric CmdArgs.push_back(FPKeepKindStr); 5461a7dea167SDimitry Andric 546281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fzero_initialized_in_bss, 546381ad6265SDimitry Andric options::OPT_fno_zero_initialized_in_bss); 54640b57cec5SDimitry Andric 54650b57cec5SDimitry Andric bool OFastEnabled = isOptimizationLevelFast(Args); 54660b57cec5SDimitry Andric // If -Ofast is the optimization level, then -fstrict-aliasing should be 54670b57cec5SDimitry Andric // enabled. This alias option is being used to simplify the hasFlag logic. 54680b57cec5SDimitry Andric OptSpecifier StrictAliasingAliasOption = 54690b57cec5SDimitry Andric OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing; 54700b57cec5SDimitry Andric // We turn strict aliasing off by default if we're in CL mode, since MSVC 54710b57cec5SDimitry Andric // doesn't do any TBAA. 54720b57cec5SDimitry Andric bool TBAAOnByDefault = !D.IsCLMode(); 54730b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 54740b57cec5SDimitry Andric options::OPT_fno_strict_aliasing, TBAAOnByDefault)) 54750b57cec5SDimitry Andric CmdArgs.push_back("-relaxed-aliasing"); 54760b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, 547781ad6265SDimitry Andric options::OPT_fno_struct_path_tbaa, true)) 54780b57cec5SDimitry Andric CmdArgs.push_back("-no-struct-path-tbaa"); 547981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_enums, 548081ad6265SDimitry Andric options::OPT_fno_strict_enums); 548181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fstrict_return, 548281ad6265SDimitry Andric options::OPT_fno_strict_return); 548381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fallow_editor_placeholders, 548481ad6265SDimitry Andric options::OPT_fno_allow_editor_placeholders); 548581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_vtable_pointers, 548681ad6265SDimitry Andric options::OPT_fno_strict_vtable_pointers); 548781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_emit_vtables, 548881ad6265SDimitry Andric options::OPT_fno_force_emit_vtables); 548981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 549081ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 549181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fescaping_block_tail_calls, 549281ad6265SDimitry Andric options::OPT_fno_escaping_block_tail_calls); 54930b57cec5SDimitry Andric 54940b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses, 54950b57cec5SDimitry Andric options::OPT_fno_fine_grained_bitfield_accesses); 54960b57cec5SDimitry Andric 5497fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5498fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5499fe6060f1SDimitry Andric 55000b57cec5SDimitry Andric // Handle segmented stacks. 5501bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsplit_stack, 5502bdd1243dSDimitry Andric options::OPT_fno_split_stack); 5503fe6060f1SDimitry Andric 5504fe6060f1SDimitry Andric // -fprotect-parens=0 is default. 5505fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fprotect_parens, 5506fe6060f1SDimitry Andric options::OPT_fno_protect_parens, false)) 5507fe6060f1SDimitry Andric CmdArgs.push_back("-fprotect-parens"); 55080b57cec5SDimitry Andric 55095ffd83dbSDimitry Andric RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA); 55105ffd83dbSDimitry Andric 5511fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) { 5512fe6060f1SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 5513fe6060f1SDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5514fe6060f1SDimitry Andric StringRef V = A->getValue(); 5515fe6060f1SDimitry Andric if (V == "64") 5516fe6060f1SDimitry Andric CmdArgs.push_back("-fextend-arguments=64"); 5517fe6060f1SDimitry Andric else if (V != "32") 5518fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5519fe6060f1SDimitry Andric << A->getValue() << A->getOption().getName(); 5520fe6060f1SDimitry Andric } else 5521fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5522fe6060f1SDimitry Andric << A->getOption().getName() << TripleStr; 5523fe6060f1SDimitry Andric } 5524fe6060f1SDimitry Andric 55255ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) { 55265ffd83dbSDimitry Andric if (TC.getArch() == llvm::Triple::avr) 55275ffd83dbSDimitry Andric A->render(Args, CmdArgs); 55285ffd83dbSDimitry Andric else 55295ffd83dbSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 55305ffd83dbSDimitry Andric << A->getAsString(Args) << TripleStr; 55315ffd83dbSDimitry Andric } 55320b57cec5SDimitry Andric 5533a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) { 5534480093f4SDimitry Andric if (TC.getTriple().isX86()) 5535a7dea167SDimitry Andric A->render(Args, CmdArgs); 5536e8d8bef9SDimitry Andric else if (TC.getTriple().isPPC() && 5537a7dea167SDimitry Andric (A->getOption().getID() != options::OPT_mlong_double_80)) 55380b57cec5SDimitry Andric A->render(Args, CmdArgs); 55390b57cec5SDimitry Andric else 55400b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 55410b57cec5SDimitry Andric << A->getAsString(Args) << TripleStr; 55420b57cec5SDimitry Andric } 55430b57cec5SDimitry Andric 55440b57cec5SDimitry Andric // Decide whether to use verbose asm. Verbose assembly is the default on 55450b57cec5SDimitry Andric // toolchains which have the integrated assembler on by default. 55460b57cec5SDimitry Andric bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault(); 55475ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 5548a7dea167SDimitry Andric IsIntegratedAssemblerDefault)) 55495ffd83dbSDimitry Andric CmdArgs.push_back("-fno-verbose-asm"); 55500b57cec5SDimitry Andric 5551e8d8bef9SDimitry Andric // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we 5552e8d8bef9SDimitry Andric // use that to indicate the MC default in the backend. 5553e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbinutils_version_EQ)) { 5554e8d8bef9SDimitry Andric StringRef V = A->getValue(); 5555e8d8bef9SDimitry Andric unsigned Num; 5556e8d8bef9SDimitry Andric if (V == "none") 5557e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5558e8d8bef9SDimitry Andric else if (!V.consumeInteger(10, Num) && Num > 0 && 5559e8d8bef9SDimitry Andric (V.empty() || (V.consume_front(".") && 5560e8d8bef9SDimitry Andric !V.consumeInteger(10, Num) && V.empty()))) 5561e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5562e8d8bef9SDimitry Andric else 5563e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5564e8d8bef9SDimitry Andric << A->getValue() << A->getOption().getName(); 5565e8d8bef9SDimitry Andric } 5566e8d8bef9SDimitry Andric 5567fe6060f1SDimitry Andric // If toolchain choose to use MCAsmParser for inline asm don't pass the 5568fe6060f1SDimitry Andric // option to disable integrated-as explictly. 5569fe6060f1SDimitry Andric if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser()) 55700b57cec5SDimitry Andric CmdArgs.push_back("-no-integrated-as"); 55710b57cec5SDimitry Andric 55720b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 55730b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 55740b57cec5SDimitry Andric CmdArgs.push_back("Structure"); 55750b57cec5SDimitry Andric } 55760b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 55770b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 55780b57cec5SDimitry Andric CmdArgs.push_back("Arguments"); 55790b57cec5SDimitry Andric } 55800b57cec5SDimitry Andric 55810b57cec5SDimitry Andric // Enable -mconstructor-aliases except on darwin, where we have to work around 5582349cc55cSDimitry Andric // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where 5583349cc55cSDimitry Andric // aliases aren't supported. 5584349cc55cSDimitry Andric if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX()) 55850b57cec5SDimitry Andric CmdArgs.push_back("-mconstructor-aliases"); 55860b57cec5SDimitry Andric 55870b57cec5SDimitry Andric // Darwin's kernel doesn't support guard variables; just die if we 55880b57cec5SDimitry Andric // try to use them. 55890b57cec5SDimitry Andric if (KernelOrKext && RawTriple.isOSDarwin()) 55900b57cec5SDimitry Andric CmdArgs.push_back("-fforbid-guard-variables"); 55910b57cec5SDimitry Andric 55920b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields, 55935ffd83dbSDimitry Andric Triple.isWindowsGNUEnvironment())) { 55940b57cec5SDimitry Andric CmdArgs.push_back("-mms-bitfields"); 55950b57cec5SDimitry Andric } 55960b57cec5SDimitry Andric 5597e8d8bef9SDimitry Andric // Non-PIC code defaults to -fdirect-access-external-data while PIC code 5598e8d8bef9SDimitry Andric // defaults to -fno-direct-access-external-data. Pass the option if different 5599e8d8bef9SDimitry Andric // from the default. 5600e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, 5601e8d8bef9SDimitry Andric options::OPT_fno_direct_access_external_data)) 5602e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fdirect_access_external_data) != 5603e8d8bef9SDimitry Andric (PICLevel == 0)) 5604e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 56050b57cec5SDimitry Andric 56060b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { 56070b57cec5SDimitry Andric CmdArgs.push_back("-fno-plt"); 56080b57cec5SDimitry Andric } 56090b57cec5SDimitry Andric 56100b57cec5SDimitry Andric // -fhosted is default. 56110b57cec5SDimitry Andric // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to 56120b57cec5SDimitry Andric // use Freestanding. 56130b57cec5SDimitry Andric bool Freestanding = 56140b57cec5SDimitry Andric Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 56150b57cec5SDimitry Andric KernelOrKext; 56160b57cec5SDimitry Andric if (Freestanding) 56170b57cec5SDimitry Andric CmdArgs.push_back("-ffreestanding"); 56180b57cec5SDimitry Andric 561981ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_knr_functions); 562081ad6265SDimitry Andric 56210b57cec5SDimitry Andric // This is a coarse approximation of what llvm-gcc actually does, both 56220b57cec5SDimitry Andric // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 56230b57cec5SDimitry Andric // complicated ways. 5624349cc55cSDimitry Andric auto SanitizeArgs = TC.getSanitizerArgs(Args); 5625bdd1243dSDimitry Andric 5626bdd1243dSDimitry Andric bool IsAsyncUnwindTablesDefault = 5627bdd1243dSDimitry Andric TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Asynchronous; 5628bdd1243dSDimitry Andric bool IsSyncUnwindTablesDefault = 5629bdd1243dSDimitry Andric TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Synchronous; 5630bdd1243dSDimitry Andric 5631349cc55cSDimitry Andric bool AsyncUnwindTables = Args.hasFlag( 5632349cc55cSDimitry Andric options::OPT_fasynchronous_unwind_tables, 56330b57cec5SDimitry Andric options::OPT_fno_asynchronous_unwind_tables, 5634bdd1243dSDimitry Andric (IsAsyncUnwindTablesDefault || SanitizeArgs.needsUnwindTables()) && 56350b57cec5SDimitry Andric !Freestanding); 5636bdd1243dSDimitry Andric bool UnwindTables = 5637bdd1243dSDimitry Andric Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, 5638bdd1243dSDimitry Andric IsSyncUnwindTablesDefault && !Freestanding); 5639349cc55cSDimitry Andric if (AsyncUnwindTables) 5640349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=2"); 5641349cc55cSDimitry Andric else if (UnwindTables) 5642349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=1"); 56430b57cec5SDimitry Andric 56445ffd83dbSDimitry Andric // Prepare `-aux-target-cpu` and `-aux-target-feature` unless 56455ffd83dbSDimitry Andric // `--gpu-use-aux-triple-only` is specified. 56465ffd83dbSDimitry Andric if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) && 5647fe6060f1SDimitry Andric (IsCudaDevice || IsHIPDevice)) { 56485ffd83dbSDimitry Andric const ArgList &HostArgs = 56495ffd83dbSDimitry Andric C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None); 56505ffd83dbSDimitry Andric std::string HostCPU = 5651349cc55cSDimitry Andric getCPUName(D, HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); 56525ffd83dbSDimitry Andric if (!HostCPU.empty()) { 56535ffd83dbSDimitry Andric CmdArgs.push_back("-aux-target-cpu"); 56545ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(HostCPU)); 56555ffd83dbSDimitry Andric } 56565ffd83dbSDimitry Andric getTargetFeatures(D, *TC.getAuxTriple(), HostArgs, CmdArgs, 56575ffd83dbSDimitry Andric /*ForAS*/ false, /*IsAux*/ true); 56585ffd83dbSDimitry Andric } 56595ffd83dbSDimitry Andric 56600b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 56610b57cec5SDimitry Andric 56620b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 56635ffd83dbSDimitry Andric StringRef CM = A->getValue(); 56645ffd83dbSDimitry Andric if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" || 5665fe6060f1SDimitry Andric CM == "tiny") { 5666fe6060f1SDimitry Andric if (Triple.isOSAIX() && CM == "medium") 5667fe6060f1SDimitry Andric CmdArgs.push_back("-mcmodel=large"); 5668bdd1243dSDimitry Andric else if (Triple.isAArch64() && (CM == "kernel" || CM == "medium")) 5669bdd1243dSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5670bdd1243dSDimitry Andric << CM << A->getOption().getName(); 56715ffd83dbSDimitry Andric else 5672fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5673fe6060f1SDimitry Andric } else { 56745ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 56755ffd83dbSDimitry Andric << CM << A->getOption().getName(); 56760b57cec5SDimitry Andric } 5677fe6060f1SDimitry Andric } 56780b57cec5SDimitry Andric 5679480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { 5680480093f4SDimitry Andric StringRef Value = A->getValue(); 5681480093f4SDimitry Andric unsigned TLSSize = 0; 5682480093f4SDimitry Andric Value.getAsInteger(10, TLSSize); 5683480093f4SDimitry Andric if (!Triple.isAArch64() || !Triple.isOSBinFormatELF()) 5684480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5685480093f4SDimitry Andric << A->getOption().getName() << TripleStr; 5686480093f4SDimitry Andric if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48) 5687480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5688480093f4SDimitry Andric << A->getOption().getName() << Value; 5689480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ); 5690480093f4SDimitry Andric } 5691480093f4SDimitry Andric 56920b57cec5SDimitry Andric // Add the target cpu 5693349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 56940b57cec5SDimitry Andric if (!CPU.empty()) { 56950b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 56960b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 56970b57cec5SDimitry Andric } 56980b57cec5SDimitry Andric 56990b57cec5SDimitry Andric RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs); 57000b57cec5SDimitry Andric 57010b57cec5SDimitry Andric // These two are potentially updated by AddClangCLArgs. 570206c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind DebugInfoKind = 570306c3fb27SDimitry Andric llvm::codegenoptions::NoDebugInfo; 57040b57cec5SDimitry Andric bool EmitCodeView = false; 57050b57cec5SDimitry Andric 57060b57cec5SDimitry Andric // Add clang-cl arguments. 57070b57cec5SDimitry Andric types::ID InputType = Input.getType(); 57080b57cec5SDimitry Andric if (D.IsCLMode()) 57090b57cec5SDimitry Andric AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView); 57100b57cec5SDimitry Andric 5711e8d8bef9SDimitry Andric DwarfFissionKind DwarfFission = DwarfFissionKind::None; 5712e8d8bef9SDimitry Andric renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, 5713e8d8bef9SDimitry Andric types::isLLVMIR(InputType), CmdArgs, DebugInfoKind, 5714e8d8bef9SDimitry Andric DwarfFission); 57150b57cec5SDimitry Andric 571681ad6265SDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 571781ad6265SDimitry Andric if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) { 571881ad6265SDimitry Andric if (TC.getTriple().isOSBinFormatELF()) { 571906c3fb27SDimitry Andric if (DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor) 572081ad6265SDimitry Andric CmdArgs.push_back("-fjmc"); 572181ad6265SDimitry Andric else 572281ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc" 572381ad6265SDimitry Andric << "-g"; 572481ad6265SDimitry Andric } else { 572581ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_fjmc_for_elf_only); 572681ad6265SDimitry Andric } 572781ad6265SDimitry Andric } 572881ad6265SDimitry Andric 57290b57cec5SDimitry Andric // Add the split debug info name to the command lines here so we 57300b57cec5SDimitry Andric // can propagate it to the backend. 57310b57cec5SDimitry Andric bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) && 5732e8d8bef9SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 573306c3fb27SDimitry Andric TC.getTriple().isOSBinFormatWasm() || 573406c3fb27SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 57350b57cec5SDimitry Andric (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 57360b57cec5SDimitry Andric isa<BackendJobAction>(JA)); 57370b57cec5SDimitry Andric if (SplitDWARF) { 5738e8d8bef9SDimitry Andric const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output); 57390b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-file"); 57400b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 57410b57cec5SDimitry Andric if (DwarfFission == DwarfFissionKind::Split) { 57420b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 57430b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 57440b57cec5SDimitry Andric } 57450b57cec5SDimitry Andric } 57460b57cec5SDimitry Andric 57470b57cec5SDimitry Andric // Pass the linker version in use. 57480b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 57490b57cec5SDimitry Andric CmdArgs.push_back("-target-linker-version"); 57500b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 57510b57cec5SDimitry Andric } 57520b57cec5SDimitry Andric 57530b57cec5SDimitry Andric // Explicitly error on some things we know we don't support and can't just 57540b57cec5SDimitry Andric // ignore. 57550b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fallow_unsupported)) { 57560b57cec5SDimitry Andric Arg *Unsupported; 57570b57cec5SDimitry Andric if (types::isCXX(InputType) && RawTriple.isOSDarwin() && 57580b57cec5SDimitry Andric TC.getArch() == llvm::Triple::x86) { 57590b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 57600b57cec5SDimitry Andric (Unsupported = Args.getLastArg(options::OPT_mkernel))) 57610b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 57620b57cec5SDimitry Andric << Unsupported->getOption().getName(); 57630b57cec5SDimitry Andric } 57640b57cec5SDimitry Andric // The faltivec option has been superseded by the maltivec option. 57650b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_faltivec))) 57660b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 57670b57cec5SDimitry Andric << Unsupported->getOption().getName() 57680b57cec5SDimitry Andric << "please use -maltivec and include altivec.h explicitly"; 57690b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec))) 57700b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 57710b57cec5SDimitry Andric << Unsupported->getOption().getName() << "please use -mno-altivec"; 57720b57cec5SDimitry Andric } 57730b57cec5SDimitry Andric 57740b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_v); 57755ffd83dbSDimitry Andric 57765ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_H)) { 57775ffd83dbSDimitry Andric CmdArgs.push_back("-H"); 57785ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 57795ffd83dbSDimitry Andric } 5780fe6060f1SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes); 57815ffd83dbSDimitry Andric 5782bdd1243dSDimitry Andric if (D.CCPrintHeadersFormat && !D.CCGenDiagnostics) { 57830b57cec5SDimitry Andric CmdArgs.push_back("-header-include-file"); 5784fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCPrintHeadersFilename.empty() 5785fe6060f1SDimitry Andric ? D.CCPrintHeadersFilename.c_str() 57860b57cec5SDimitry Andric : "-"); 57875ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 5788bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5789bdd1243dSDimitry Andric "-header-include-format=" + 5790bdd1243dSDimitry Andric std::string(headerIncludeFormatKindToString(D.CCPrintHeadersFormat)))); 5791bdd1243dSDimitry Andric CmdArgs.push_back( 5792bdd1243dSDimitry Andric Args.MakeArgString("-header-include-filtering=" + 5793bdd1243dSDimitry Andric std::string(headerIncludeFilteringKindToString( 5794bdd1243dSDimitry Andric D.CCPrintHeadersFiltering)))); 57950b57cec5SDimitry Andric } 57960b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_P); 57970b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 57980b57cec5SDimitry Andric 57990b57cec5SDimitry Andric if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 58000b57cec5SDimitry Andric CmdArgs.push_back("-diagnostic-log-file"); 5801fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty() 5802fe6060f1SDimitry Andric ? D.CCLogDiagnosticsFilename.c_str() 58030b57cec5SDimitry Andric : "-"); 58040b57cec5SDimitry Andric } 58050b57cec5SDimitry Andric 580613138422SDimitry Andric // Give the gen diagnostics more chances to succeed, by avoiding intentional 580713138422SDimitry Andric // crashes. 580813138422SDimitry Andric if (D.CCGenDiagnostics) 580913138422SDimitry Andric CmdArgs.push_back("-disable-pragma-debug-crash"); 581013138422SDimitry Andric 5811fe6060f1SDimitry Andric // Allow backend to put its diagnostic files in the same place as frontend 5812fe6060f1SDimitry Andric // crash diagnostics files. 5813fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) { 5814fe6060f1SDimitry Andric StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir); 5815fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5816fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir)); 5817fe6060f1SDimitry Andric } 5818fe6060f1SDimitry Andric 58190b57cec5SDimitry Andric bool UseSeparateSections = isUseSeparateSections(Triple); 58200b57cec5SDimitry Andric 58210b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffunction_sections, 58220b57cec5SDimitry Andric options::OPT_fno_function_sections, UseSeparateSections)) { 58230b57cec5SDimitry Andric CmdArgs.push_back("-ffunction-sections"); 58240b57cec5SDimitry Andric } 58250b57cec5SDimitry Andric 58265ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) { 58275ffd83dbSDimitry Andric StringRef Val = A->getValue(); 5828fe6060f1SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 58295ffd83dbSDimitry Andric if (Val != "all" && Val != "labels" && Val != "none" && 5830e8d8bef9SDimitry Andric !Val.startswith("list=")) 58315ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 58325ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 58335ffd83dbSDimitry Andric else 58345ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5835fe6060f1SDimitry Andric } else if (Triple.isNVPTX()) { 5836fe6060f1SDimitry Andric // Do not pass the option to the GPU compilation. We still want it enabled 5837fe6060f1SDimitry Andric // for the host-side compilation, so seeing it here is not an error. 5838fe6060f1SDimitry Andric } else if (Val != "none") { 5839fe6060f1SDimitry Andric // =none is allowed everywhere. It's useful for overriding the option 5840fe6060f1SDimitry Andric // and is the same as not specifying the option. 5841e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5842e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5843e8d8bef9SDimitry Andric } 58445ffd83dbSDimitry Andric } 58455ffd83dbSDimitry Andric 5846e8d8bef9SDimitry Andric bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF(); 58470b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, 5848e8d8bef9SDimitry Andric UseSeparateSections || HasDefaultDataSections)) { 58490b57cec5SDimitry Andric CmdArgs.push_back("-fdata-sections"); 58500b57cec5SDimitry Andric } 58510b57cec5SDimitry Andric 585281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_funique_section_names, 585381ad6265SDimitry Andric options::OPT_fno_unique_section_names); 585481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_internal_linkage_names, 585581ad6265SDimitry Andric options::OPT_fno_unique_internal_linkage_names); 585681ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_basic_block_section_names, 585781ad6265SDimitry Andric options::OPT_fno_unique_basic_block_section_names); 585806c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fconvergent_functions, 585906c3fb27SDimitry Andric options::OPT_fno_convergent_functions); 58605ffd83dbSDimitry Andric 5861e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, 5862e8d8bef9SDimitry Andric options::OPT_fno_split_machine_functions)) { 5863e8d8bef9SDimitry Andric // This codegen pass is only available on x86-elf targets. 5864e8d8bef9SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 5865e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fsplit_machine_functions)) 5866e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5867e8d8bef9SDimitry Andric } else { 5868e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5869e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5870e8d8bef9SDimitry Andric } 5871e8d8bef9SDimitry Andric } 5872e8d8bef9SDimitry Andric 58730b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, 58740b57cec5SDimitry Andric options::OPT_finstrument_functions_after_inlining, 58750b57cec5SDimitry Andric options::OPT_finstrument_function_entry_bare); 58760b57cec5SDimitry Andric 58775ffd83dbSDimitry Andric // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support 58785ffd83dbSDimitry Andric // for sampling, overhead of call arc collection is way too high and there's 58795ffd83dbSDimitry Andric // no way to collect the output. 58805ffd83dbSDimitry Andric if (!Triple.isNVPTX() && !Triple.isAMDGCN()) 588106c3fb27SDimitry Andric addPGOAndCoverageFlags(TC, C, JA, Output, Args, SanitizeArgs, CmdArgs); 58820b57cec5SDimitry Andric 58830b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); 58840b57cec5SDimitry Andric 5885bdd1243dSDimitry Andric if (getLastProfileSampleUseArg(Args) && 5886bdd1243dSDimitry Andric Args.hasArg(options::OPT_fsample_profile_use_profi)) { 5887bdd1243dSDimitry Andric CmdArgs.push_back("-mllvm"); 5888bdd1243dSDimitry Andric CmdArgs.push_back("-sample-profile-use-profi"); 5889bdd1243dSDimitry Andric } 5890bdd1243dSDimitry Andric 589181ad6265SDimitry Andric // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled. 589281ad6265SDimitry Andric if (RawTriple.isPS() && 58930b57cec5SDimitry Andric !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 589481ad6265SDimitry Andric PScpu::addProfileRTArgs(TC, Args, CmdArgs); 589581ad6265SDimitry Andric PScpu::addSanitizerArgs(TC, Args, CmdArgs); 58960b57cec5SDimitry Andric } 58970b57cec5SDimitry Andric 58980b57cec5SDimitry Andric // Pass options for controlling the default header search paths. 58990b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdinc)) { 59000b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 59010b57cec5SDimitry Andric CmdArgs.push_back("-nobuiltininc"); 59020b57cec5SDimitry Andric } else { 59030b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdlibinc)) 59040b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 59050b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 59060b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 59070b57cec5SDimitry Andric } 59080b57cec5SDimitry Andric 59090b57cec5SDimitry Andric // Pass the path to compiler resource files. 59100b57cec5SDimitry Andric CmdArgs.push_back("-resource-dir"); 59110b57cec5SDimitry Andric CmdArgs.push_back(D.ResourceDir.c_str()); 59120b57cec5SDimitry Andric 59130b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_working_directory); 59140b57cec5SDimitry Andric 59150b57cec5SDimitry Andric RenderARCMigrateToolOptions(D, Args, CmdArgs); 59160b57cec5SDimitry Andric 59170b57cec5SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 59180b57cec5SDimitry Andric // preprocessor. 59190b57cec5SDimitry Andric // 59200b57cec5SDimitry Andric // FIXME: Support -fpreprocessed 59210b57cec5SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 59220b57cec5SDimitry Andric AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 59230b57cec5SDimitry Andric 59240b57cec5SDimitry Andric // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 59250b57cec5SDimitry Andric // that "The compiler can only warn and ignore the option if not recognized". 59260b57cec5SDimitry Andric // When building with ccache, it will pass -D options to clang even on 59270b57cec5SDimitry Andric // preprocessed inputs and configure concludes that -fPIC is not supported. 59280b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_D); 59290b57cec5SDimitry Andric 59300b57cec5SDimitry Andric // Manually translate -O4 to -O3; let clang reject others. 59310b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 59320b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 59330b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 59340b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 59350b57cec5SDimitry Andric } else { 59360b57cec5SDimitry Andric A->render(Args, CmdArgs); 59370b57cec5SDimitry Andric } 59380b57cec5SDimitry Andric } 59390b57cec5SDimitry Andric 59400b57cec5SDimitry Andric // Warn about ignored options to clang. 59410b57cec5SDimitry Andric for (const Arg *A : 59420b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) { 59430b57cec5SDimitry Andric D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args); 59440b57cec5SDimitry Andric A->claim(); 59450b57cec5SDimitry Andric } 59460b57cec5SDimitry Andric 59470b57cec5SDimitry Andric for (const Arg *A : 59480b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) { 59490b57cec5SDimitry Andric D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args); 59500b57cec5SDimitry Andric A->claim(); 59510b57cec5SDimitry Andric } 59520b57cec5SDimitry Andric 59530b57cec5SDimitry Andric claimNoWarnArgs(Args); 59540b57cec5SDimitry Andric 59550b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_R_Group); 59560b57cec5SDimitry Andric 5957349cc55cSDimitry Andric for (const Arg *A : 5958349cc55cSDimitry Andric Args.filtered(options::OPT_W_Group, options::OPT__SLASH_wd)) { 5959349cc55cSDimitry Andric A->claim(); 5960349cc55cSDimitry Andric if (A->getOption().getID() == options::OPT__SLASH_wd) { 5961349cc55cSDimitry Andric unsigned WarningNumber; 5962349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, WarningNumber)) { 5963349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5964349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5965349cc55cSDimitry Andric continue; 5966349cc55cSDimitry Andric } 5967349cc55cSDimitry Andric 5968349cc55cSDimitry Andric if (auto Group = diagGroupFromCLWarningID(WarningNumber)) { 5969349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5970349cc55cSDimitry Andric "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group))); 5971349cc55cSDimitry Andric } 5972349cc55cSDimitry Andric continue; 5973349cc55cSDimitry Andric } 5974349cc55cSDimitry Andric A->render(Args, CmdArgs); 5975349cc55cSDimitry Andric } 5976349cc55cSDimitry Andric 59770b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 59780b57cec5SDimitry Andric CmdArgs.push_back("-pedantic"); 59790b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 59800b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_w); 59810b57cec5SDimitry Andric 5982bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_ffixed_point, 5983bdd1243dSDimitry Andric options::OPT_fno_fixed_point); 59840b57cec5SDimitry Andric 5985fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ)) 5986fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5987fe6060f1SDimitry Andric 5988fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5989fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5990fe6060f1SDimitry Andric 5991349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) 5992349cc55cSDimitry Andric A->render(Args, CmdArgs); 5993349cc55cSDimitry Andric 59940b57cec5SDimitry Andric // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 59950b57cec5SDimitry Andric // (-ansi is equivalent to -std=c89 or -std=c++98). 59960b57cec5SDimitry Andric // 59970b57cec5SDimitry Andric // If a std is supplied, only add -trigraphs if it follows the 59980b57cec5SDimitry Andric // option. 5999fe6060f1SDimitry Andric bool ImplyVCPPCVer = false; 60000b57cec5SDimitry Andric bool ImplyVCPPCXXVer = false; 60010b57cec5SDimitry Andric const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); 60020b57cec5SDimitry Andric if (Std) { 60030b57cec5SDimitry Andric if (Std->getOption().matches(options::OPT_ansi)) 60040b57cec5SDimitry Andric if (types::isCXX(InputType)) 60050b57cec5SDimitry Andric CmdArgs.push_back("-std=c++98"); 60060b57cec5SDimitry Andric else 60070b57cec5SDimitry Andric CmdArgs.push_back("-std=c89"); 60080b57cec5SDimitry Andric else 60090b57cec5SDimitry Andric Std->render(Args, CmdArgs); 60100b57cec5SDimitry Andric 60110b57cec5SDimitry Andric // If -f(no-)trigraphs appears after the language standard flag, honor it. 60120b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 60130b57cec5SDimitry Andric options::OPT_ftrigraphs, 60140b57cec5SDimitry Andric options::OPT_fno_trigraphs)) 60150b57cec5SDimitry Andric if (A != Std) 60160b57cec5SDimitry Andric A->render(Args, CmdArgs); 60170b57cec5SDimitry Andric } else { 60180b57cec5SDimitry Andric // Honor -std-default. 60190b57cec5SDimitry Andric // 60200b57cec5SDimitry Andric // FIXME: Clang doesn't correctly handle -std= when the input language 60210b57cec5SDimitry Andric // doesn't match. For the time being just ignore this for C++ inputs; 60220b57cec5SDimitry Andric // eventually we want to do all the standard defaulting here instead of 60230b57cec5SDimitry Andric // splitting it between the driver and clang -cc1. 6024fe6060f1SDimitry Andric if (!types::isCXX(InputType)) { 6025fe6060f1SDimitry Andric if (!Args.hasArg(options::OPT__SLASH_std)) { 60260b57cec5SDimitry Andric Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", 60270b57cec5SDimitry Andric /*Joined=*/true); 6028fe6060f1SDimitry Andric } else 6029fe6060f1SDimitry Andric ImplyVCPPCVer = true; 6030fe6060f1SDimitry Andric } 60310b57cec5SDimitry Andric else if (IsWindowsMSVC) 60320b57cec5SDimitry Andric ImplyVCPPCXXVer = true; 60330b57cec5SDimitry Andric 60340b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs, 60350b57cec5SDimitry Andric options::OPT_fno_trigraphs); 60360b57cec5SDimitry Andric } 60370b57cec5SDimitry Andric 60380b57cec5SDimitry Andric // GCC's behavior for -Wwrite-strings is a bit strange: 60390b57cec5SDimitry Andric // * In C, this "warning flag" changes the types of string literals from 60400b57cec5SDimitry Andric // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning 60410b57cec5SDimitry Andric // for the discarded qualifier. 60420b57cec5SDimitry Andric // * In C++, this is just a normal warning flag. 60430b57cec5SDimitry Andric // 60440b57cec5SDimitry Andric // Implementing this warning correctly in C is hard, so we follow GCC's 60450b57cec5SDimitry Andric // behavior for now. FIXME: Directly diagnose uses of a string literal as 60460b57cec5SDimitry Andric // a non-const char* in C, rather than using this crude hack. 60470b57cec5SDimitry Andric if (!types::isCXX(InputType)) { 60480b57cec5SDimitry Andric // FIXME: This should behave just like a warning flag, and thus should also 60490b57cec5SDimitry Andric // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on. 60500b57cec5SDimitry Andric Arg *WriteStrings = 60510b57cec5SDimitry Andric Args.getLastArg(options::OPT_Wwrite_strings, 60520b57cec5SDimitry Andric options::OPT_Wno_write_strings, options::OPT_w); 60530b57cec5SDimitry Andric if (WriteStrings && 60540b57cec5SDimitry Andric WriteStrings->getOption().matches(options::OPT_Wwrite_strings)) 60550b57cec5SDimitry Andric CmdArgs.push_back("-fconst-strings"); 60560b57cec5SDimitry Andric } 60570b57cec5SDimitry Andric 60580b57cec5SDimitry Andric // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 60590b57cec5SDimitry Andric // during C++ compilation, which it is by default. GCC keeps this define even 60600b57cec5SDimitry Andric // in the presence of '-w', match this behavior bug-for-bug. 60610b57cec5SDimitry Andric if (types::isCXX(InputType) && 60620b57cec5SDimitry Andric Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 60630b57cec5SDimitry Andric true)) { 60640b57cec5SDimitry Andric CmdArgs.push_back("-fdeprecated-macro"); 60650b57cec5SDimitry Andric } 60660b57cec5SDimitry Andric 60670b57cec5SDimitry Andric // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 60680b57cec5SDimitry Andric if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 60690b57cec5SDimitry Andric if (Asm->getOption().matches(options::OPT_fasm)) 60700b57cec5SDimitry Andric CmdArgs.push_back("-fgnu-keywords"); 60710b57cec5SDimitry Andric else 60720b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-keywords"); 60730b57cec5SDimitry Andric } 60740b57cec5SDimitry Andric 6075480093f4SDimitry Andric if (!ShouldEnableAutolink(Args, TC, JA)) 60760b57cec5SDimitry Andric CmdArgs.push_back("-fno-autolink"); 60770b57cec5SDimitry Andric 60780b57cec5SDimitry Andric // Add in -fdebug-compilation-dir if necessary. 60790eae32dcSDimitry Andric const char *DebugCompilationDir = 6080a7dea167SDimitry Andric addDebugCompDirArg(Args, CmdArgs, D.getVFS()); 60810b57cec5SDimitry Andric 608281ad6265SDimitry Andric addDebugPrefixMapArg(D, TC, Args, CmdArgs); 60830b57cec5SDimitry Andric 608406c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftemplate_depth_EQ); 608506c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_foperator_arrow_depth_EQ); 608606c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_depth_EQ); 608706c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_steps_EQ); 60880b57cec5SDimitry Andric 6089fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_library); 609081ad6265SDimitry Andric 6091a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter)) 6092a7dea167SDimitry Andric CmdArgs.push_back("-fexperimental-new-constant-interpreter"); 6093a7dea167SDimitry Andric 60940b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 60950b57cec5SDimitry Andric CmdArgs.push_back("-fbracket-depth"); 60960b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 60970b57cec5SDimitry Andric } 60980b57cec5SDimitry Andric 60990b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 61000b57cec5SDimitry Andric options::OPT_Wlarge_by_value_copy_def)) { 61010b57cec5SDimitry Andric if (A->getNumValues()) { 61020b57cec5SDimitry Andric StringRef bytes = A->getValue(); 61030b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 61040b57cec5SDimitry Andric } else 61050b57cec5SDimitry Andric CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 61060b57cec5SDimitry Andric } 61070b57cec5SDimitry Andric 61080b57cec5SDimitry Andric if (Args.hasArg(options::OPT_relocatable_pch)) 61090b57cec5SDimitry Andric CmdArgs.push_back("-relocatable-pch"); 61100b57cec5SDimitry Andric 61110b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) { 61120b57cec5SDimitry Andric static const char *kCFABIs[] = { 61130b57cec5SDimitry Andric "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1", 61140b57cec5SDimitry Andric }; 61150b57cec5SDimitry Andric 611681ad6265SDimitry Andric if (!llvm::is_contained(kCFABIs, StringRef(A->getValue()))) 61170b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue(); 61180b57cec5SDimitry Andric else 61190b57cec5SDimitry Andric A->render(Args, CmdArgs); 61200b57cec5SDimitry Andric } 61210b57cec5SDimitry Andric 61220b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 61230b57cec5SDimitry Andric CmdArgs.push_back("-fconstant-string-class"); 61240b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 61250b57cec5SDimitry Andric } 61260b57cec5SDimitry Andric 61270b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 61280b57cec5SDimitry Andric CmdArgs.push_back("-ftabstop"); 61290b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 61300b57cec5SDimitry Andric } 61310b57cec5SDimitry Andric 6132bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_size_section, 6133bdd1243dSDimitry Andric options::OPT_fno_stack_size_section); 61340b57cec5SDimitry Andric 6135fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fstack_usage)) { 6136fe6060f1SDimitry Andric CmdArgs.push_back("-stack-usage-file"); 6137fe6060f1SDimitry Andric 6138fe6060f1SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 6139fe6060f1SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 6140fe6060f1SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "su"); 6141fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 6142fe6060f1SDimitry Andric } else 6143fe6060f1SDimitry Andric CmdArgs.push_back( 6144fe6060f1SDimitry Andric Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su")); 6145fe6060f1SDimitry Andric } 6146fe6060f1SDimitry Andric 61470b57cec5SDimitry Andric CmdArgs.push_back("-ferror-limit"); 61480b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 61490b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 61500b57cec5SDimitry Andric else 61510b57cec5SDimitry Andric CmdArgs.push_back("19"); 61520b57cec5SDimitry Andric 615306c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_backtrace_limit_EQ); 615406c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmacro_backtrace_limit_EQ); 615506c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftemplate_backtrace_limit_EQ); 615606c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fspell_checking_limit_EQ); 615706c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fcaret_diagnostics_max_lines_EQ); 61580b57cec5SDimitry Andric 61590b57cec5SDimitry Andric // Pass -fmessage-length=. 61605ffd83dbSDimitry Andric unsigned MessageLength = 0; 61610b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 61625ffd83dbSDimitry Andric StringRef V(A->getValue()); 61635ffd83dbSDimitry Andric if (V.getAsInteger(0, MessageLength)) 61645ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 61655ffd83dbSDimitry Andric << V << A->getOption().getName(); 61660b57cec5SDimitry Andric } else { 61670b57cec5SDimitry Andric // If -fmessage-length=N was not specified, determine whether this is a 61680b57cec5SDimitry Andric // terminal and, if so, implicitly define -fmessage-length appropriately. 61695ffd83dbSDimitry Andric MessageLength = llvm::sys::Process::StandardErrColumns(); 61700b57cec5SDimitry Andric } 61715ffd83dbSDimitry Andric if (MessageLength != 0) 61725ffd83dbSDimitry Andric CmdArgs.push_back( 61735ffd83dbSDimitry Andric Args.MakeArgString("-fmessage-length=" + Twine(MessageLength))); 61740b57cec5SDimitry Andric 617581ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_EQ)) 617681ad6265SDimitry Andric CmdArgs.push_back( 617781ad6265SDimitry Andric Args.MakeArgString("-frandomize-layout-seed=" + Twine(A->getValue(0)))); 617881ad6265SDimitry Andric 617981ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_file_EQ)) 618081ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frandomize-layout-seed-file=" + 618181ad6265SDimitry Andric Twine(A->getValue(0)))); 618281ad6265SDimitry Andric 61830b57cec5SDimitry Andric // -fvisibility= and -fvisibility-ms-compat are of a piece. 61840b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 61850b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 61860b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 6187bdd1243dSDimitry Andric A->render(Args, CmdArgs); 61880b57cec5SDimitry Andric } else { 61890b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 6190bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=hidden"); 6191bdd1243dSDimitry Andric CmdArgs.push_back("-ftype-visibility=default"); 61920b57cec5SDimitry Andric } 619304eeddc0SDimitry Andric } else if (IsOpenMPDevice) { 619404eeddc0SDimitry Andric // When compiling for the OpenMP device we want protected visibility by 6195bdd1243dSDimitry Andric // default. This prevents the device from accidentally preempting code on 6196bdd1243dSDimitry Andric // the host, makes the system more robust, and improves performance. 6197bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=protected"); 61980b57cec5SDimitry Andric } 61990b57cec5SDimitry Andric 6200bdd1243dSDimitry Andric // PS4/PS5 process these options in addClangTargetOptions. 6201bdd1243dSDimitry Andric if (!RawTriple.isPS()) { 6202e8d8bef9SDimitry Andric if (const Arg *A = 6203e8d8bef9SDimitry Andric Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass, 6204e8d8bef9SDimitry Andric options::OPT_fno_visibility_from_dllstorageclass)) { 6205e8d8bef9SDimitry Andric if (A->getOption().matches( 6206e8d8bef9SDimitry Andric options::OPT_fvisibility_from_dllstorageclass)) { 6207e8d8bef9SDimitry Andric CmdArgs.push_back("-fvisibility-from-dllstorageclass"); 6208e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_dllexport_EQ); 6209e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_nodllstorageclass_EQ); 6210e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_externs_dllimport_EQ); 6211e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, 6212e8d8bef9SDimitry Andric options::OPT_fvisibility_externs_nodllstorageclass_EQ); 6213e8d8bef9SDimitry Andric } 6214e8d8bef9SDimitry Andric } 6215bdd1243dSDimitry Andric } 6216e8d8bef9SDimitry Andric 6217fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden, 6218fe6060f1SDimitry Andric options::OPT_fno_visibility_inlines_hidden, false)) 6219fe6060f1SDimitry Andric CmdArgs.push_back("-fvisibility-inlines-hidden"); 6220fe6060f1SDimitry Andric 6221e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, 6222e8d8bef9SDimitry Andric options::OPT_fno_visibility_inlines_hidden_static_local_var); 62230b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); 62240b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 62250b57cec5SDimitry Andric 62264824e7fdSDimitry Andric if (Args.hasFlag(options::OPT_fnew_infallible, 62274824e7fdSDimitry Andric options::OPT_fno_new_infallible, false)) 62284824e7fdSDimitry Andric CmdArgs.push_back("-fnew-infallible"); 62294824e7fdSDimitry Andric 6230fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fno_operator_names, 6231fe6060f1SDimitry Andric options::OPT_foperator_names, false)) 6232fe6060f1SDimitry Andric CmdArgs.push_back("-fno-operator-names"); 6233fe6060f1SDimitry Andric 62340b57cec5SDimitry Andric // Forward -f (flag) options which we can pass directly. 62350b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 62360b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 62370b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); 623881ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); 623981ad6265SDimitry Andric 624006c3fb27SDimitry Andric if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls, 624106c3fb27SDimitry Andric Triple.hasDefaultEmulatedTLS())) 624206c3fb27SDimitry Andric CmdArgs.push_back("-femulated-tls"); 624306c3fb27SDimitry Andric 624406c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcheck_new, 624506c3fb27SDimitry Andric options::OPT_fno_check_new); 624606c3fb27SDimitry Andric 624781ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { 624881ad6265SDimitry Andric // FIXME: There's no reason for this to be restricted to X86. The backend 624981ad6265SDimitry Andric // code needs to be changed to include the appropriate function calls 625081ad6265SDimitry Andric // automatically. 625181ad6265SDimitry Andric if (!Triple.isX86() && !Triple.isAArch64()) 625281ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 625381ad6265SDimitry Andric << A->getAsString(Args) << TripleStr; 625481ad6265SDimitry Andric } 62550b57cec5SDimitry Andric 62560b57cec5SDimitry Andric // AltiVec-like language extensions aren't relevant for assembling. 62570b57cec5SDimitry Andric if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) 62580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzvector); 62590b57cec5SDimitry Andric 62600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 62610b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 62620b57cec5SDimitry Andric 62630b57cec5SDimitry Andric // Forward flags for OpenMP. We don't do this if the current action is an 62640b57cec5SDimitry Andric // device offloading action other than OpenMP. 62650b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, 62660b57cec5SDimitry Andric options::OPT_fno_openmp, false) && 62670b57cec5SDimitry Andric (JA.isDeviceOffloading(Action::OFK_None) || 62680b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_OpenMP))) { 62690b57cec5SDimitry Andric switch (D.getOpenMPRuntime(Args)) { 62700b57cec5SDimitry Andric case Driver::OMPRT_OMP: 62710b57cec5SDimitry Andric case Driver::OMPRT_IOMP5: 62720b57cec5SDimitry Andric // Clang can generate useful OpenMP code for these two runtime libraries. 62730b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp"); 62740b57cec5SDimitry Andric 62750b57cec5SDimitry Andric // If no option regarding the use of TLS in OpenMP codegeneration is 62760b57cec5SDimitry Andric // given, decide a default based on the target. Otherwise rely on the 62770b57cec5SDimitry Andric // options and pass the right information to the frontend. 62780b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_use_tls, 62790b57cec5SDimitry Andric options::OPT_fnoopenmp_use_tls, /*Default=*/true)) 62800b57cec5SDimitry Andric CmdArgs.push_back("-fnoopenmp-use-tls"); 62810b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 62820b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 6283480093f4SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder); 62840b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 6285349cc55cSDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_extensions, 6286349cc55cSDimitry Andric options::OPT_fno_openmp_extensions, /*Default=*/true)) 6287349cc55cSDimitry Andric CmdArgs.push_back("-fno-openmp-extensions"); 62880b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ); 62890b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ); 62900b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 62910b57cec5SDimitry Andric options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ); 62920b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse, 62930b57cec5SDimitry Andric options::OPT_fno_openmp_optimistic_collapse, 62940b57cec5SDimitry Andric /*Default=*/false)) 62950b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-optimistic-collapse"); 62960b57cec5SDimitry Andric 62970b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, forward 62980b57cec5SDimitry Andric // cuda-mode flag 62990b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_mode, 63000b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_mode, /*Default=*/false)) 63010b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-mode"); 63020b57cec5SDimitry Andric 6303349cc55cSDimitry Andric // When in OpenMP offloading mode, enable debugging on the device. 6304349cc55cSDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ); 6305349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_debug, 6306349cc55cSDimitry Andric options::OPT_fno_openmp_target_debug, /*Default=*/false)) 6307349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-target-debug"); 6308349cc55cSDimitry Andric 6309349cc55cSDimitry Andric // When in OpenMP offloading mode, forward assumptions information about 6310349cc55cSDimitry Andric // thread and team counts in the device. 6311349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription, 6312349cc55cSDimitry Andric options::OPT_fno_openmp_assume_teams_oversubscription, 6313349cc55cSDimitry Andric /*Default=*/false)) 6314349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-teams-oversubscription"); 6315349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription, 6316349cc55cSDimitry Andric options::OPT_fno_openmp_assume_threads_oversubscription, 6317349cc55cSDimitry Andric /*Default=*/false)) 6318349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-threads-oversubscription"); 631981ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_thread_state)) 632081ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-thread-state"); 6321bdd1243dSDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_nested_parallelism)) 6322bdd1243dSDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-nested-parallelism"); 632381ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_offload_mandatory)) 632481ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-offload-mandatory"); 63250b57cec5SDimitry Andric break; 63260b57cec5SDimitry Andric default: 63270b57cec5SDimitry Andric // By default, if Clang doesn't know how to generate useful OpenMP code 63280b57cec5SDimitry Andric // for a specific runtime library, we just don't pass the '-fopenmp' flag 63290b57cec5SDimitry Andric // down to the actual compilation. 63300b57cec5SDimitry Andric // FIXME: It would be better to have a mode which *only* omits IR 63310b57cec5SDimitry Andric // generation based on the OpenMP support so that we get consistent 63320b57cec5SDimitry Andric // semantic analysis, etc. 63330b57cec5SDimitry Andric break; 63340b57cec5SDimitry Andric } 63350b57cec5SDimitry Andric } else { 63360b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 63370b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 63380b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 633981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fopenmp_extensions, 634081ad6265SDimitry Andric options::OPT_fno_openmp_extensions); 63410b57cec5SDimitry Andric } 63420b57cec5SDimitry Andric 634381ad6265SDimitry Andric // Forward the new driver to change offloading code generation. 6344bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_offload_new_driver, 6345bdd1243dSDimitry Andric options::OPT_no_offload_new_driver, false)) 634681ad6265SDimitry Andric CmdArgs.push_back("--offload-new-driver"); 634781ad6265SDimitry Andric 6348349cc55cSDimitry Andric SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); 63490b57cec5SDimitry Andric 63500b57cec5SDimitry Andric const XRayArgs &XRay = TC.getXRayArgs(); 63510b57cec5SDimitry Andric XRay.addArgs(TC, Args, CmdArgs, InputType); 63520b57cec5SDimitry Andric 6353e8d8bef9SDimitry Andric for (const auto &Filename : 6354e8d8bef9SDimitry Andric Args.getAllArgValues(options::OPT_fprofile_list_EQ)) { 6355e8d8bef9SDimitry Andric if (D.getVFS().exists(Filename)) 6356e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-list=" + Filename)); 6357e8d8bef9SDimitry Andric else 6358e8d8bef9SDimitry Andric D.Diag(clang::diag::err_drv_no_such_file) << Filename; 6359e8d8bef9SDimitry Andric } 6360e8d8bef9SDimitry Andric 6361480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) { 6362480093f4SDimitry Andric StringRef S0 = A->getValue(), S = S0; 636355e4f9d5SDimitry Andric unsigned Size, Offset = 0; 636406c3fb27SDimitry Andric if (!Triple.isAArch64() && !Triple.isLoongArch() && !Triple.isRISCV() && 636506c3fb27SDimitry Andric !Triple.isX86()) 6366480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6367480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6368480093f4SDimitry Andric else if (S.consumeInteger(10, Size) || 6369480093f4SDimitry Andric (!S.empty() && (!S.consume_front(",") || 637055e4f9d5SDimitry Andric S.consumeInteger(10, Offset) || !S.empty()))) 6371480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 6372480093f4SDimitry Andric << S0 << A->getOption().getName(); 637355e4f9d5SDimitry Andric else if (Size < Offset) 6374480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument); 637555e4f9d5SDimitry Andric else { 6376480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size))); 637755e4f9d5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 637855e4f9d5SDimitry Andric "-fpatchable-function-entry-offset=" + Twine(Offset))); 637955e4f9d5SDimitry Andric } 6380480093f4SDimitry Andric } 6381480093f4SDimitry Andric 638204eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch); 638304eeddc0SDimitry Andric 6384480093f4SDimitry Andric if (TC.SupportsProfiling()) { 63850b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pg); 63860b57cec5SDimitry Andric 6387480093f4SDimitry Andric llvm::Triple::ArchType Arch = TC.getArch(); 6388480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfentry)) { 6389480093f4SDimitry Andric if (Arch == llvm::Triple::systemz || TC.getTriple().isX86()) 6390480093f4SDimitry Andric A->render(Args, CmdArgs); 6391480093f4SDimitry Andric else 6392480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6393480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6394480093f4SDimitry Andric } 6395480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) { 6396480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6397480093f4SDimitry Andric A->render(Args, CmdArgs); 6398480093f4SDimitry Andric else 6399480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6400480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6401480093f4SDimitry Andric } 6402480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) { 6403480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6404480093f4SDimitry Andric A->render(Args, CmdArgs); 6405480093f4SDimitry Andric else 6406480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6407480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6408480093f4SDimitry Andric } 6409480093f4SDimitry Andric } 641006c3fb27SDimitry Andric 641106c3fb27SDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_pg)) { 641206c3fb27SDimitry Andric if (TC.getTriple().isOSzOS()) { 6413bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6414bdd1243dSDimitry Andric << A->getAsString(Args) << TripleStr; 6415bdd1243dSDimitry Andric } 6416bdd1243dSDimitry Andric } 641706c3fb27SDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_p)) { 641806c3fb27SDimitry Andric if (!(TC.getTriple().isOSAIX() || TC.getTriple().isOSOpenBSD())) { 641906c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 642006c3fb27SDimitry Andric << A->getAsString(Args) << TripleStr; 642106c3fb27SDimitry Andric } 642206c3fb27SDimitry Andric } 642306c3fb27SDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) { 642406c3fb27SDimitry Andric if (A->getOption().matches(options::OPT_p)) { 642506c3fb27SDimitry Andric A->claim(); 642606c3fb27SDimitry Andric if (TC.getTriple().isOSAIX() && !Args.hasArgNoClaim(options::OPT_pg)) 642706c3fb27SDimitry Andric CmdArgs.push_back("-pg"); 642806c3fb27SDimitry Andric } 642906c3fb27SDimitry Andric } 643006c3fb27SDimitry Andric 643106c3fb27SDimitry Andric // Reject AIX-specific link options on other targets. 643206c3fb27SDimitry Andric if (!TC.getTriple().isOSAIX()) { 643306c3fb27SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_b, options::OPT_K, 643406c3fb27SDimitry Andric options::OPT_mxcoff_build_id_EQ)) { 643506c3fb27SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 643606c3fb27SDimitry Andric << A->getSpelling() << TripleStr; 643706c3fb27SDimitry Andric } 643806c3fb27SDimitry Andric } 64390b57cec5SDimitry Andric 64400b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_fapple_kext) || 64410b57cec5SDimitry Andric (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) 64420b57cec5SDimitry Andric CmdArgs.push_back("-fapple-kext"); 64430b57cec5SDimitry Andric 6444fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat); 6445a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ); 64460b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 64470b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 64480b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 64490b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 6450e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ); 64510b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 64520b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_malign_double); 6453480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file); 64540b57cec5SDimitry Andric 645506c3fb27SDimitry Andric if (const char *Name = C.getTimeTraceFile(&JA)) { 645606c3fb27SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ftime-trace=" + Twine(Name))); 645706c3fb27SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); 645806c3fb27SDimitry Andric } 645906c3fb27SDimitry Andric 64600b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 64610b57cec5SDimitry Andric CmdArgs.push_back("-ftrapv-handler"); 64620b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 64630b57cec5SDimitry Andric } 64640b57cec5SDimitry Andric 64650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 64660b57cec5SDimitry Andric 64670b57cec5SDimitry Andric // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 64680b57cec5SDimitry Andric // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 64690b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) { 64700b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fwrapv)) 64710b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 64720b57cec5SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 64730b57cec5SDimitry Andric options::OPT_fno_strict_overflow)) { 64740b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_strict_overflow)) 64750b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 64760b57cec5SDimitry Andric } 64770b57cec5SDimitry Andric 64780b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_freroll_loops, 64790b57cec5SDimitry Andric options::OPT_fno_reroll_loops)) 64800b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_freroll_loops)) 64810b57cec5SDimitry Andric CmdArgs.push_back("-freroll-loops"); 64820b57cec5SDimitry Andric 6483d409305fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops, 6484d409305fSDimitry Andric options::OPT_fno_finite_loops); 6485d409305fSDimitry Andric 64860b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 64870b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 64880b57cec5SDimitry Andric options::OPT_fno_unroll_loops); 64890b57cec5SDimitry Andric 6490fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fstrict_flex_arrays_EQ); 6491fcaf7f86SDimitry Andric 64920b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pthread); 64930b57cec5SDimitry Andric 6494bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mspeculative_load_hardening, 6495bdd1243dSDimitry Andric options::OPT_mno_speculative_load_hardening); 64960b57cec5SDimitry Andric 6497e8d8bef9SDimitry Andric RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext); 64985ffd83dbSDimitry Andric RenderSCPOptions(TC, Args, CmdArgs); 64990b57cec5SDimitry Andric RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs); 65000b57cec5SDimitry Andric 6501349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fswift_async_fp_EQ); 6502349cc55cSDimitry Andric 6503bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mstackrealign, 6504bdd1243dSDimitry Andric options::OPT_mno_stackrealign); 65050b57cec5SDimitry Andric 65060b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_alignment)) { 65070b57cec5SDimitry Andric StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 65080b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 65090b57cec5SDimitry Andric } 65100b57cec5SDimitry Andric 65110b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_probe_size)) { 65120b57cec5SDimitry Andric StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size); 65130b57cec5SDimitry Andric 65140b57cec5SDimitry Andric if (!Size.empty()) 65150b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size)); 65160b57cec5SDimitry Andric else 65170b57cec5SDimitry Andric CmdArgs.push_back("-mstack-probe-size=0"); 65180b57cec5SDimitry Andric } 65190b57cec5SDimitry Andric 652081ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_mstack_arg_probe, 652181ad6265SDimitry Andric options::OPT_mno_stack_arg_probe); 65220b57cec5SDimitry Andric 65230b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it, 65240b57cec5SDimitry Andric options::OPT_mno_restrict_it)) { 65250b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mrestrict_it)) { 65260b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 65270b57cec5SDimitry Andric CmdArgs.push_back("-arm-restrict-it"); 65280b57cec5SDimitry Andric } else { 65290b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 653081ad6265SDimitry Andric CmdArgs.push_back("-arm-default-it"); 65310b57cec5SDimitry Andric } 65320b57cec5SDimitry Andric } 65330b57cec5SDimitry Andric 65340b57cec5SDimitry Andric // Forward -cl options to -cc1 6535fe6060f1SDimitry Andric RenderOpenCLOptions(Args, CmdArgs, InputType); 65360b57cec5SDimitry Andric 653781ad6265SDimitry Andric // Forward hlsl options to -cc1 653881ad6265SDimitry Andric RenderHLSLOptions(Args, CmdArgs, InputType); 653981ad6265SDimitry Andric 6540e8d8bef9SDimitry Andric if (IsHIP) { 6541e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fhip_new_launch_api, 65425ffd83dbSDimitry Andric options::OPT_fno_hip_new_launch_api, true)) 6543a7dea167SDimitry Andric CmdArgs.push_back("-fhip-new-launch-api"); 6544e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_allow_device_init, 6545e8d8bef9SDimitry Andric options::OPT_fno_gpu_allow_device_init, false)) 6546e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-allow-device-init"); 654781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fhip_kernel_arg_name, 654881ad6265SDimitry Andric options::OPT_fno_hip_kernel_arg_name); 6549e8d8bef9SDimitry Andric } 6550e8d8bef9SDimitry Andric 6551e8d8bef9SDimitry Andric if (IsCuda || IsHIP) { 6552753f127fSDimitry Andric if (IsRDCMode) 6553fe6060f1SDimitry Andric CmdArgs.push_back("-fgpu-rdc"); 6554e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_defer_diag, 6555e8d8bef9SDimitry Andric options::OPT_fno_gpu_defer_diag, false)) 6556e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6557e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads, 6558e8d8bef9SDimitry Andric options::OPT_fno_gpu_exclude_wrong_side_overloads, 6559e8d8bef9SDimitry Andric false)) { 6560e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-exclude-wrong-side-overloads"); 6561e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6562e8d8bef9SDimitry Andric } 6563e8d8bef9SDimitry Andric } 6564a7dea167SDimitry Andric 656581ad6265SDimitry Andric // Forward -nogpulib to -cc1. 656681ad6265SDimitry Andric if (Args.hasArg(options::OPT_nogpulib)) 656781ad6265SDimitry Andric CmdArgs.push_back("-nogpulib"); 656881ad6265SDimitry Andric 65690b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { 65700b57cec5SDimitry Andric CmdArgs.push_back( 65710b57cec5SDimitry Andric Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); 65720b57cec5SDimitry Andric } 65730b57cec5SDimitry Andric 6574753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ)) 6575753f127fSDimitry Andric CmdArgs.push_back( 6576753f127fSDimitry Andric Args.MakeArgString(Twine("-mfunction-return=") + A->getValue())); 6577753f127fSDimitry Andric 6578bdd1243dSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mindirect_branch_cs_prefix); 6579bdd1243dSDimitry Andric 6580fe6060f1SDimitry Andric // Forward -f options with positive and negative forms; we translate these by 6581fe6060f1SDimitry Andric // hand. Do not propagate PGO options to the GPU-side compilations as the 6582fe6060f1SDimitry Andric // profile info is for the host-side compilation only. 6583fe6060f1SDimitry Andric if (!(IsCudaDevice || IsHIPDevice)) { 65840b57cec5SDimitry Andric if (Arg *A = getLastProfileSampleUseArg(Args)) { 65850b57cec5SDimitry Andric auto *PGOArg = Args.getLastArg( 65860b57cec5SDimitry Andric options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, 6587fe6060f1SDimitry Andric options::OPT_fcs_profile_generate, 6588fe6060f1SDimitry Andric options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use, 6589fe6060f1SDimitry Andric options::OPT_fprofile_use_EQ); 65900b57cec5SDimitry Andric if (PGOArg) 65910b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 65920b57cec5SDimitry Andric << "SampleUse with PGO options"; 65930b57cec5SDimitry Andric 65940b57cec5SDimitry Andric StringRef fname = A->getValue(); 65950b57cec5SDimitry Andric if (!llvm::sys::fs::exists(fname)) 65960b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << fname; 65970b57cec5SDimitry Andric else 65980b57cec5SDimitry Andric A->render(Args, CmdArgs); 65990b57cec5SDimitry Andric } 66000b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); 66010b57cec5SDimitry Andric 6602e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, 6603fe6060f1SDimitry Andric options::OPT_fno_pseudo_probe_for_profiling, false)) { 6604e8d8bef9SDimitry Andric CmdArgs.push_back("-fpseudo-probe-for-profiling"); 6605fe6060f1SDimitry Andric // Enforce -funique-internal-linkage-names if it's not explicitly turned 6606fe6060f1SDimitry Andric // off. 6607fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_funique_internal_linkage_names, 6608fe6060f1SDimitry Andric options::OPT_fno_unique_internal_linkage_names, true)) 6609fe6060f1SDimitry Andric CmdArgs.push_back("-funique-internal-linkage-names"); 6610fe6060f1SDimitry Andric } 6611fe6060f1SDimitry Andric } 66120b57cec5SDimitry Andric RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs); 66130b57cec5SDimitry Andric 661481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 661581ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 66160b57cec5SDimitry Andric 66170b57cec5SDimitry Andric // -fblocks=0 is default. 66180b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 66190b57cec5SDimitry Andric TC.IsBlocksDefault()) || 66200b57cec5SDimitry Andric (Args.hasArg(options::OPT_fgnu_runtime) && 66210b57cec5SDimitry Andric Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 66220b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_blocks))) { 66230b57cec5SDimitry Andric CmdArgs.push_back("-fblocks"); 66240b57cec5SDimitry Andric 66250b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime()) 66260b57cec5SDimitry Andric CmdArgs.push_back("-fblocks-runtime-optional"); 66270b57cec5SDimitry Andric } 66280b57cec5SDimitry Andric 66290b57cec5SDimitry Andric // -fencode-extended-block-signature=1 is default. 66300b57cec5SDimitry Andric if (TC.IsEncodeExtendedBlockSignatureDefault()) 66310b57cec5SDimitry Andric CmdArgs.push_back("-fencode-extended-block-signature"); 66320b57cec5SDimitry Andric 6633bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_fcoro_aligned_allocation, 6634bdd1243dSDimitry Andric options::OPT_fno_coro_aligned_allocation, false) && 6635bdd1243dSDimitry Andric types::isCXX(InputType)) 6636bdd1243dSDimitry Andric CmdArgs.push_back("-fcoro-aligned-allocation"); 6637bdd1243dSDimitry Andric 66380b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes, 66390b57cec5SDimitry Andric options::OPT_fno_double_square_bracket_attributes); 66400b57cec5SDimitry Andric 664181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_faccess_control, 664281ad6265SDimitry Andric options::OPT_fno_access_control); 664381ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_felide_constructors, 664481ad6265SDimitry Andric options::OPT_fno_elide_constructors); 66450b57cec5SDimitry Andric 66460b57cec5SDimitry Andric ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); 66470b57cec5SDimitry Andric 66480b57cec5SDimitry Andric if (KernelOrKext || (types::isCXX(InputType) && 66490b57cec5SDimitry Andric (RTTIMode == ToolChain::RM_Disabled))) 66500b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti"); 66510b57cec5SDimitry Andric 6652e8d8bef9SDimitry Andric // -fshort-enums=0 is default for all architectures except Hexagon and z/OS. 66530b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums, 6654e8d8bef9SDimitry Andric TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS())) 66550b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 66560b57cec5SDimitry Andric 66570b57cec5SDimitry Andric RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs); 66580b57cec5SDimitry Andric 66590b57cec5SDimitry Andric // -fuse-cxa-atexit is default. 66600b57cec5SDimitry Andric if (!Args.hasFlag( 66610b57cec5SDimitry Andric options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit, 66625ffd83dbSDimitry Andric !RawTriple.isOSAIX() && !RawTriple.isOSWindows() && 66630b57cec5SDimitry Andric ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) || 66640b57cec5SDimitry Andric RawTriple.hasEnvironment())) || 66650b57cec5SDimitry Andric KernelOrKext) 66660b57cec5SDimitry Andric CmdArgs.push_back("-fno-use-cxa-atexit"); 66670b57cec5SDimitry Andric 66680b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit, 66690b57cec5SDimitry Andric options::OPT_fno_register_global_dtors_with_atexit, 66700b57cec5SDimitry Andric RawTriple.isOSDarwin() && !KernelOrKext)) 66710b57cec5SDimitry Andric CmdArgs.push_back("-fregister-global-dtors-with-atexit"); 66720b57cec5SDimitry Andric 667381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fuse_line_directives, 667481ad6265SDimitry Andric options::OPT_fno_use_line_directives); 66750b57cec5SDimitry Andric 6676349cc55cSDimitry Andric // -fno-minimize-whitespace is default. 6677349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fminimize_whitespace, 6678349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace, false)) { 6679349cc55cSDimitry Andric types::ID InputType = Inputs[0].getType(); 6680349cc55cSDimitry Andric if (!isDerivedFromC(InputType)) 6681349cc55cSDimitry Andric D.Diag(diag::err_drv_minws_unsupported_input_type) 6682349cc55cSDimitry Andric << types::getTypeName(InputType); 6683349cc55cSDimitry Andric CmdArgs.push_back("-fminimize-whitespace"); 6684349cc55cSDimitry Andric } 6685349cc55cSDimitry Andric 66865ffd83dbSDimitry Andric // -fms-extensions=0 is default. 66875ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 66885ffd83dbSDimitry Andric IsWindowsMSVC)) 66895ffd83dbSDimitry Andric CmdArgs.push_back("-fms-extensions"); 66905ffd83dbSDimitry Andric 66910b57cec5SDimitry Andric // -fms-compatibility=0 is default. 6692a7dea167SDimitry Andric bool IsMSVCCompat = Args.hasFlag( 6693a7dea167SDimitry Andric options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, 6694a7dea167SDimitry Andric (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, 6695a7dea167SDimitry Andric options::OPT_fno_ms_extensions, true))); 6696a7dea167SDimitry Andric if (IsMSVCCompat) 66970b57cec5SDimitry Andric CmdArgs.push_back("-fms-compatibility"); 66980b57cec5SDimitry Andric 6699bdd1243dSDimitry Andric if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() && 6700bdd1243dSDimitry Andric Args.hasArg(options::OPT_fms_runtime_lib_EQ)) 6701bdd1243dSDimitry Andric ProcessVSRuntimeLibrary(Args, CmdArgs); 6702bdd1243dSDimitry Andric 6703a7dea167SDimitry Andric // Handle -fgcc-version, if present. 6704a7dea167SDimitry Andric VersionTuple GNUCVer; 6705a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) { 6706a7dea167SDimitry Andric // Check that the version has 1 to 3 components and the minor and patch 6707a7dea167SDimitry Andric // versions fit in two decimal digits. 6708a7dea167SDimitry Andric StringRef Val = A->getValue(); 6709a7dea167SDimitry Andric Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable. 6710a7dea167SDimitry Andric bool Invalid = GNUCVer.tryParse(Val); 671181ad6265SDimitry Andric unsigned Minor = GNUCVer.getMinor().value_or(0); 671281ad6265SDimitry Andric unsigned Patch = GNUCVer.getSubminor().value_or(0); 6713a7dea167SDimitry Andric if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) { 6714a7dea167SDimitry Andric D.Diag(diag::err_drv_invalid_value) 6715a7dea167SDimitry Andric << A->getAsString(Args) << A->getValue(); 6716a7dea167SDimitry Andric } 6717a7dea167SDimitry Andric } else if (!IsMSVCCompat) { 6718a7dea167SDimitry Andric // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect. 6719a7dea167SDimitry Andric GNUCVer = VersionTuple(4, 2, 1); 6720a7dea167SDimitry Andric } 6721a7dea167SDimitry Andric if (!GNUCVer.empty()) { 6722a7dea167SDimitry Andric CmdArgs.push_back( 6723a7dea167SDimitry Andric Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString())); 6724a7dea167SDimitry Andric } 6725a7dea167SDimitry Andric 67260b57cec5SDimitry Andric VersionTuple MSVT = TC.computeMSVCVersion(&D, Args); 67270b57cec5SDimitry Andric if (!MSVT.empty()) 67280b57cec5SDimitry Andric CmdArgs.push_back( 67290b57cec5SDimitry Andric Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString())); 67300b57cec5SDimitry Andric 67310b57cec5SDimitry Andric bool IsMSVC2015Compatible = MSVT.getMajor() >= 19; 6732fe6060f1SDimitry Andric if (ImplyVCPPCVer) { 6733fe6060f1SDimitry Andric StringRef LanguageStandard; 6734fe6060f1SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 6735fe6060f1SDimitry Andric Std = StdArg; 6736fe6060f1SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 6737fe6060f1SDimitry Andric .Case("c11", "-std=c11") 6738fe6060f1SDimitry Andric .Case("c17", "-std=c17") 6739fe6060f1SDimitry Andric .Default(""); 6740fe6060f1SDimitry Andric if (LanguageStandard.empty()) 6741fe6060f1SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 6742fe6060f1SDimitry Andric << StdArg->getAsString(Args); 6743fe6060f1SDimitry Andric } 6744fe6060f1SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 6745fe6060f1SDimitry Andric } 67460b57cec5SDimitry Andric if (ImplyVCPPCXXVer) { 67470b57cec5SDimitry Andric StringRef LanguageStandard; 67480b57cec5SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 67490b57cec5SDimitry Andric Std = StdArg; 67500b57cec5SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 67510b57cec5SDimitry Andric .Case("c++14", "-std=c++14") 67520b57cec5SDimitry Andric .Case("c++17", "-std=c++17") 6753fe6060f1SDimitry Andric .Case("c++20", "-std=c++20") 675406c3fb27SDimitry Andric // TODO add c++23 and c++26 when MSVC supports it. 675506c3fb27SDimitry Andric .Case("c++latest", "-std=c++26") 67560b57cec5SDimitry Andric .Default(""); 67570b57cec5SDimitry Andric if (LanguageStandard.empty()) 67580b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 67590b57cec5SDimitry Andric << StdArg->getAsString(Args); 67600b57cec5SDimitry Andric } 67610b57cec5SDimitry Andric 67620b57cec5SDimitry Andric if (LanguageStandard.empty()) { 67630b57cec5SDimitry Andric if (IsMSVC2015Compatible) 67640b57cec5SDimitry Andric LanguageStandard = "-std=c++14"; 67650b57cec5SDimitry Andric else 67660b57cec5SDimitry Andric LanguageStandard = "-std=c++11"; 67670b57cec5SDimitry Andric } 67680b57cec5SDimitry Andric 67690b57cec5SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 67700b57cec5SDimitry Andric } 67710b57cec5SDimitry Andric 677281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions, 677381ad6265SDimitry Andric options::OPT_fno_borland_extensions); 67740b57cec5SDimitry Andric 677581ad6265SDimitry Andric // -fno-declspec is default, except for PS4/PS5. 67760b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec, 677781ad6265SDimitry Andric RawTriple.isPS())) 67780b57cec5SDimitry Andric CmdArgs.push_back("-fdeclspec"); 67790b57cec5SDimitry Andric else if (Args.hasArg(options::OPT_fno_declspec)) 67800b57cec5SDimitry Andric CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec. 67810b57cec5SDimitry Andric 67820b57cec5SDimitry Andric // -fthreadsafe-static is default, except for MSVC compatibility versions less 67830b57cec5SDimitry Andric // than 19. 67840b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 67850b57cec5SDimitry Andric options::OPT_fno_threadsafe_statics, 6786349cc55cSDimitry Andric !types::isOpenCL(InputType) && 6787349cc55cSDimitry Andric (!IsWindowsMSVC || IsMSVC2015Compatible))) 67880b57cec5SDimitry Andric CmdArgs.push_back("-fno-threadsafe-statics"); 67890b57cec5SDimitry Andric 67900b57cec5SDimitry Andric // -fno-delayed-template-parsing is default, except when targeting MSVC. 67910b57cec5SDimitry Andric // Many old Windows SDK versions require this to parse. 67920b57cec5SDimitry Andric // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their 67930b57cec5SDimitry Andric // compiler. We should be able to disable this by default at some point. 67940b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 67950b57cec5SDimitry Andric options::OPT_fno_delayed_template_parsing, IsWindowsMSVC)) 67960b57cec5SDimitry Andric CmdArgs.push_back("-fdelayed-template-parsing"); 67970b57cec5SDimitry Andric 67980b57cec5SDimitry Andric // -fgnu-keywords default varies depending on language; only pass if 67990b57cec5SDimitry Andric // specified. 68000b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords, 68010b57cec5SDimitry Andric options::OPT_fno_gnu_keywords); 68020b57cec5SDimitry Andric 680381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fgnu89_inline, 680481ad6265SDimitry Andric options::OPT_fno_gnu89_inline); 68050b57cec5SDimitry Andric 680681ad6265SDimitry Andric const Arg *InlineArg = Args.getLastArg(options::OPT_finline_functions, 68070b57cec5SDimitry Andric options::OPT_finline_hint_functions, 68080b57cec5SDimitry Andric options::OPT_fno_inline_functions); 680981ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_finline, options::OPT_fno_inline)) { 681081ad6265SDimitry Andric if (A->getOption().matches(options::OPT_fno_inline)) 681181ad6265SDimitry Andric A->render(Args, CmdArgs); 681281ad6265SDimitry Andric } else if (InlineArg) { 681381ad6265SDimitry Andric InlineArg->render(Args, CmdArgs); 681481ad6265SDimitry Andric } 68150b57cec5SDimitry Andric 6816bdd1243dSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finline_max_stacksize_EQ); 6817bdd1243dSDimitry Andric 68180b57cec5SDimitry Andric bool HaveModules = 6819bdd1243dSDimitry Andric RenderModulesOptions(C, D, Args, Input, Output, Std, CmdArgs); 68200b57cec5SDimitry Andric 6821a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fpch_validate_input_files_content, 6822a7dea167SDimitry Andric options::OPT_fno_pch_validate_input_files_content, false)) 6823a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 68245ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 68255ffd83dbSDimitry Andric options::OPT_fno_pch_instantiate_templates, false)) 68265ffd83dbSDimitry Andric CmdArgs.push_back("-fpch-instantiate-templates"); 6827590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen, 6828590d96feSDimitry Andric false)) 6829590d96feSDimitry Andric CmdArgs.push_back("-fmodules-codegen"); 6830590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo, 6831590d96feSDimitry Andric false)) 6832590d96feSDimitry Andric CmdArgs.push_back("-fmodules-debuginfo"); 6833a7dea167SDimitry Andric 68345ffd83dbSDimitry Andric ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); 68350b57cec5SDimitry Andric RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, 68360b57cec5SDimitry Andric Input, CmdArgs); 68370b57cec5SDimitry Andric 6838fe6060f1SDimitry Andric if (types::isObjC(Input.getType()) && 6839fe6060f1SDimitry Andric Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec, 6840fe6060f1SDimitry Andric options::OPT_fno_objc_encode_cxx_class_template_spec, 6841fe6060f1SDimitry Andric !Runtime.isNeXTFamily())) 6842fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec"); 6843fe6060f1SDimitry Andric 68440b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fapplication_extension, 68450b57cec5SDimitry Andric options::OPT_fno_application_extension, false)) 68460b57cec5SDimitry Andric CmdArgs.push_back("-fapplication-extension"); 68470b57cec5SDimitry Andric 68480b57cec5SDimitry Andric // Handle GCC-style exception args. 6849fe6060f1SDimitry Andric bool EH = false; 68500b57cec5SDimitry Andric if (!C.getDriver().IsCLMode()) 6851fe6060f1SDimitry Andric EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); 68520b57cec5SDimitry Andric 68530b57cec5SDimitry Andric // Handle exception personalities 6854a7dea167SDimitry Andric Arg *A = Args.getLastArg( 6855a7dea167SDimitry Andric options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions, 6856a7dea167SDimitry Andric options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions); 68570b57cec5SDimitry Andric if (A) { 68580b57cec5SDimitry Andric const Option &Opt = A->getOption(); 68590b57cec5SDimitry Andric if (Opt.matches(options::OPT_fsjlj_exceptions)) 6860e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 68610b57cec5SDimitry Andric if (Opt.matches(options::OPT_fseh_exceptions)) 6862e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 68630b57cec5SDimitry Andric if (Opt.matches(options::OPT_fdwarf_exceptions)) 6864e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 6865a7dea167SDimitry Andric if (Opt.matches(options::OPT_fwasm_exceptions)) 6866e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=wasm"); 68670b57cec5SDimitry Andric } else { 68680b57cec5SDimitry Andric switch (TC.GetExceptionModel(Args)) { 68690b57cec5SDimitry Andric default: 68700b57cec5SDimitry Andric break; 68710b57cec5SDimitry Andric case llvm::ExceptionHandling::DwarfCFI: 6872e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 68730b57cec5SDimitry Andric break; 68740b57cec5SDimitry Andric case llvm::ExceptionHandling::SjLj: 6875e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 68760b57cec5SDimitry Andric break; 68770b57cec5SDimitry Andric case llvm::ExceptionHandling::WinEH: 6878e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 68790b57cec5SDimitry Andric break; 68800b57cec5SDimitry Andric } 68810b57cec5SDimitry Andric } 68820b57cec5SDimitry Andric 68830b57cec5SDimitry Andric // C++ "sane" operator new. 688481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 688581ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 68860b57cec5SDimitry Andric 688706c3fb27SDimitry Andric // -fassume-unique-vtables is on by default. 688806c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_unique_vtables, 688906c3fb27SDimitry Andric options::OPT_fno_assume_unique_vtables); 689006c3fb27SDimitry Andric 68910b57cec5SDimitry Andric // -frelaxed-template-template-args is off by default, as it is a severe 68920b57cec5SDimitry Andric // breaking change until a corresponding change to template partial ordering 68930b57cec5SDimitry Andric // is provided. 689481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_frelaxed_template_template_args, 689581ad6265SDimitry Andric options::OPT_fno_relaxed_template_template_args); 68960b57cec5SDimitry Andric 68970b57cec5SDimitry Andric // -fsized-deallocation is off by default, as it is an ABI-breaking change for 68980b57cec5SDimitry Andric // most platforms. 689981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsized_deallocation, 690081ad6265SDimitry Andric options::OPT_fno_sized_deallocation); 69010b57cec5SDimitry Andric 69020b57cec5SDimitry Andric // -faligned-allocation is on by default in C++17 onwards and otherwise off 69030b57cec5SDimitry Andric // by default. 69040b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation, 69050b57cec5SDimitry Andric options::OPT_fno_aligned_allocation, 69060b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) { 69070b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_aligned_allocation)) 69080b57cec5SDimitry Andric CmdArgs.push_back("-fno-aligned-allocation"); 69090b57cec5SDimitry Andric else 69100b57cec5SDimitry Andric CmdArgs.push_back("-faligned-allocation"); 69110b57cec5SDimitry Andric } 69120b57cec5SDimitry Andric 69130b57cec5SDimitry Andric // The default new alignment can be specified using a dedicated option or via 69140b57cec5SDimitry Andric // a GCC-compatible option that also turns on aligned allocation. 69150b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ, 69160b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) 69170b57cec5SDimitry Andric CmdArgs.push_back( 69180b57cec5SDimitry Andric Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue())); 69190b57cec5SDimitry Andric 69200b57cec5SDimitry Andric // -fconstant-cfstrings is default, and may be subject to argument translation 69210b57cec5SDimitry Andric // on Darwin. 69220b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 692381ad6265SDimitry Andric options::OPT_fno_constant_cfstrings, true) || 69240b57cec5SDimitry Andric !Args.hasFlag(options::OPT_mconstant_cfstrings, 692581ad6265SDimitry Andric options::OPT_mno_constant_cfstrings, true)) 69260b57cec5SDimitry Andric CmdArgs.push_back("-fno-constant-cfstrings"); 69270b57cec5SDimitry Andric 692881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fpascal_strings, 692981ad6265SDimitry Andric options::OPT_fno_pascal_strings); 69300b57cec5SDimitry Andric 69310b57cec5SDimitry Andric // Honor -fpack-struct= and -fpack-struct, if given. Note that 69320b57cec5SDimitry Andric // -fno-pack-struct doesn't apply to -fpack-struct=. 69330b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 69340b57cec5SDimitry Andric std::string PackStructStr = "-fpack-struct="; 69350b57cec5SDimitry Andric PackStructStr += A->getValue(); 69360b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 69370b57cec5SDimitry Andric } else if (Args.hasFlag(options::OPT_fpack_struct, 69380b57cec5SDimitry Andric options::OPT_fno_pack_struct, false)) { 69390b57cec5SDimitry Andric CmdArgs.push_back("-fpack-struct=1"); 69400b57cec5SDimitry Andric } 69410b57cec5SDimitry Andric 69420b57cec5SDimitry Andric // Handle -fmax-type-align=N and -fno-type-align 69430b57cec5SDimitry Andric bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align); 69440b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) { 69450b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 69460b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align="; 69470b57cec5SDimitry Andric MaxTypeAlignStr += A->getValue(); 69480b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 69490b57cec5SDimitry Andric } 69500b57cec5SDimitry Andric } else if (RawTriple.isOSDarwin()) { 69510b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 69520b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align=16"; 69530b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 69540b57cec5SDimitry Andric } 69550b57cec5SDimitry Andric } 69560b57cec5SDimitry Andric 69570b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true)) 69580b57cec5SDimitry Andric CmdArgs.push_back("-Qn"); 69590b57cec5SDimitry Andric 69605ffd83dbSDimitry Andric // -fno-common is the default, set -fcommon only when that flag is set. 696181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcommon, options::OPT_fno_common); 69620b57cec5SDimitry Andric 69630b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't yet support 69640b57cec5SDimitry Andric // -funsigned-bitfields. 69650b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fsigned_bitfields, 696681ad6265SDimitry Andric options::OPT_funsigned_bitfields, true)) 69670b57cec5SDimitry Andric D.Diag(diag::warn_drv_clang_unsupported) 69680b57cec5SDimitry Andric << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 69690b57cec5SDimitry Andric 69700b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 697181ad6265SDimitry Andric if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope, true)) 69720b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) 69730b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 69740b57cec5SDimitry Andric 69750b57cec5SDimitry Andric // -finput_charset=UTF-8 is default. Reject others 69760b57cec5SDimitry Andric if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) { 69770b57cec5SDimitry Andric StringRef value = inputCharset->getValue(); 6978fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 69790b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) 69800b57cec5SDimitry Andric << value; 69810b57cec5SDimitry Andric } 69820b57cec5SDimitry Andric 69830b57cec5SDimitry Andric // -fexec_charset=UTF-8 is default. Reject others 69840b57cec5SDimitry Andric if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) { 69850b57cec5SDimitry Andric StringRef value = execCharset->getValue(); 6986fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 69870b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) 69880b57cec5SDimitry Andric << value; 69890b57cec5SDimitry Andric } 69900b57cec5SDimitry Andric 69910b57cec5SDimitry Andric RenderDiagnosticsOptions(D, Args, CmdArgs); 69920b57cec5SDimitry Andric 699381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fasm_blocks, 699481ad6265SDimitry Andric options::OPT_fno_asm_blocks); 69950b57cec5SDimitry Andric 6996bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fgnu_inline_asm, 6997bdd1243dSDimitry Andric options::OPT_fno_gnu_inline_asm); 69980b57cec5SDimitry Andric 69990b57cec5SDimitry Andric // Enable vectorization per default according to the optimization level 70000b57cec5SDimitry Andric // selected. For optimization levels that want vectorization we use the alias 70010b57cec5SDimitry Andric // option to simplify the hasFlag logic. 70020b57cec5SDimitry Andric bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false); 70030b57cec5SDimitry Andric OptSpecifier VectorizeAliasOption = 70040b57cec5SDimitry Andric EnableVec ? options::OPT_O_Group : options::OPT_fvectorize; 70050b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 70060b57cec5SDimitry Andric options::OPT_fno_vectorize, EnableVec)) 70070b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-loops"); 70080b57cec5SDimitry Andric 70090b57cec5SDimitry Andric // -fslp-vectorize is enabled based on the optimization level selected. 70100b57cec5SDimitry Andric bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true); 70110b57cec5SDimitry Andric OptSpecifier SLPVectAliasOption = 70120b57cec5SDimitry Andric EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize; 70130b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption, 70140b57cec5SDimitry Andric options::OPT_fno_slp_vectorize, EnableSLPVec)) 70150b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-slp"); 70160b57cec5SDimitry Andric 70170b57cec5SDimitry Andric ParseMPreferVectorWidth(D, Args, CmdArgs); 70180b57cec5SDimitry Andric 70190b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ); 70200b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 70210b57cec5SDimitry Andric options::OPT_fsanitize_undefined_strip_path_components_EQ); 70220b57cec5SDimitry Andric 70230b57cec5SDimitry Andric // -fdollars-in-identifiers default varies depending on platform and 70240b57cec5SDimitry Andric // language; only pass if specified. 70250b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 70260b57cec5SDimitry Andric options::OPT_fno_dollars_in_identifiers)) { 70270b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 70280b57cec5SDimitry Andric CmdArgs.push_back("-fdollars-in-identifiers"); 70290b57cec5SDimitry Andric else 70300b57cec5SDimitry Andric CmdArgs.push_back("-fno-dollars-in-identifiers"); 70310b57cec5SDimitry Andric } 70320b57cec5SDimitry Andric 703381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fapple_pragma_pack, 703481ad6265SDimitry Andric options::OPT_fno_apple_pragma_pack); 70350b57cec5SDimitry Andric 70360b57cec5SDimitry Andric // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. 7037480093f4SDimitry Andric if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple)) 7038480093f4SDimitry Andric renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA); 70390b57cec5SDimitry Andric 70400b57cec5SDimitry Andric bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports, 70410b57cec5SDimitry Andric options::OPT_fno_rewrite_imports, false); 70420b57cec5SDimitry Andric if (RewriteImports) 70430b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-imports"); 70440b57cec5SDimitry Andric 7045bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdirectives_only, 7046bdd1243dSDimitry Andric options::OPT_fno_directives_only); 704781ad6265SDimitry Andric 70480b57cec5SDimitry Andric // Enable rewrite includes if the user's asked for it or if we're generating 70490b57cec5SDimitry Andric // diagnostics. 70500b57cec5SDimitry Andric // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be 70510b57cec5SDimitry Andric // nice to enable this when doing a crashdump for modules as well. 70520b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_frewrite_includes, 70530b57cec5SDimitry Andric options::OPT_fno_rewrite_includes, false) || 70540b57cec5SDimitry Andric (C.isForDiagnostics() && !HaveModules)) 70550b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-includes"); 70560b57cec5SDimitry Andric 70570b57cec5SDimitry Andric // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 70580b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_traditional, 70590b57cec5SDimitry Andric options::OPT_traditional_cpp)) { 70600b57cec5SDimitry Andric if (isa<PreprocessJobAction>(JA)) 70610b57cec5SDimitry Andric CmdArgs.push_back("-traditional-cpp"); 70620b57cec5SDimitry Andric else 70630b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 70640b57cec5SDimitry Andric } 70650b57cec5SDimitry Andric 70660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dM); 70670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dD); 706804eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dI); 70690b57cec5SDimitry Andric 70705ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ); 70715ffd83dbSDimitry Andric 70720b57cec5SDimitry Andric // Handle serialized diagnostics. 70730b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 70740b57cec5SDimitry Andric CmdArgs.push_back("-serialize-diagnostic-file"); 70750b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getValue())); 70760b57cec5SDimitry Andric } 70770b57cec5SDimitry Andric 70780b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 70790b57cec5SDimitry Andric CmdArgs.push_back("-fretain-comments-from-system-headers"); 70800b57cec5SDimitry Andric 70810b57cec5SDimitry Andric // Forward -fcomment-block-commands to -cc1. 70820b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 70830b57cec5SDimitry Andric // Forward -fparse-all-comments to -cc1. 70840b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 70850b57cec5SDimitry Andric 70860b57cec5SDimitry Andric // Turn -fplugin=name.so into -load name.so 70870b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) { 70880b57cec5SDimitry Andric CmdArgs.push_back("-load"); 70890b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 70900b57cec5SDimitry Andric A->claim(); 70910b57cec5SDimitry Andric } 70920b57cec5SDimitry Andric 70934824e7fdSDimitry Andric // Turn -fplugin-arg-pluginname-key=value into 70944824e7fdSDimitry Andric // -plugin-arg-pluginname key=value 70954824e7fdSDimitry Andric // GCC has an actual plugin_argument struct with key/value pairs that it 70964824e7fdSDimitry Andric // passes to its plugins, but we don't, so just pass it on as-is. 70974824e7fdSDimitry Andric // 70984824e7fdSDimitry Andric // The syntax for -fplugin-arg- is ambiguous if both plugin name and 70994824e7fdSDimitry Andric // argument key are allowed to contain dashes. GCC therefore only 71004824e7fdSDimitry Andric // allows dashes in the key. We do the same. 71014824e7fdSDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_arg)) { 71024824e7fdSDimitry Andric auto ArgValue = StringRef(A->getValue()); 71034824e7fdSDimitry Andric auto FirstDashIndex = ArgValue.find('-'); 71044824e7fdSDimitry Andric StringRef PluginName = ArgValue.substr(0, FirstDashIndex); 71054824e7fdSDimitry Andric StringRef Arg = ArgValue.substr(FirstDashIndex + 1); 71064824e7fdSDimitry Andric 71074824e7fdSDimitry Andric A->claim(); 71084824e7fdSDimitry Andric if (FirstDashIndex == StringRef::npos || Arg.empty()) { 71094824e7fdSDimitry Andric if (PluginName.empty()) { 71104824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_name) << A->getAsString(Args); 71114824e7fdSDimitry Andric } else { 71124824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_arg) 71134824e7fdSDimitry Andric << PluginName << A->getAsString(Args); 71144824e7fdSDimitry Andric } 71154824e7fdSDimitry Andric continue; 71164824e7fdSDimitry Andric } 71174824e7fdSDimitry Andric 71184824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-arg-") + PluginName)); 71194824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Arg)); 71204824e7fdSDimitry Andric } 71214824e7fdSDimitry Andric 71220b57cec5SDimitry Andric // Forward -fpass-plugin=name.so to -cc1. 71230b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { 71240b57cec5SDimitry Andric CmdArgs.push_back( 71250b57cec5SDimitry Andric Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); 71260b57cec5SDimitry Andric A->claim(); 71270b57cec5SDimitry Andric } 71280b57cec5SDimitry Andric 712906c3fb27SDimitry Andric // Forward --vfsoverlay to -cc1. 713006c3fb27SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_vfsoverlay)) { 713106c3fb27SDimitry Andric CmdArgs.push_back("--vfsoverlay"); 713206c3fb27SDimitry Andric CmdArgs.push_back(A->getValue()); 713306c3fb27SDimitry Andric A->claim(); 713406c3fb27SDimitry Andric } 713506c3fb27SDimitry Andric 713606c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsafe_buffer_usage_suggestions, 713706c3fb27SDimitry Andric options::OPT_fno_safe_buffer_usage_suggestions); 713806c3fb27SDimitry Andric 71390b57cec5SDimitry Andric // Setup statistics file output. 71400b57cec5SDimitry Andric SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); 714106c3fb27SDimitry Andric if (!StatsFile.empty()) { 71420b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile)); 714306c3fb27SDimitry Andric if (D.CCPrintInternalStats) 714406c3fb27SDimitry Andric CmdArgs.push_back("-stats-file-append"); 714506c3fb27SDimitry Andric } 71460b57cec5SDimitry Andric 71470b57cec5SDimitry Andric // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 71480b57cec5SDimitry Andric // parser. 7149bdd1243dSDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) { 7150bdd1243dSDimitry Andric Arg->claim(); 71510b57cec5SDimitry Andric // -finclude-default-header flag is for preprocessor, 71520b57cec5SDimitry Andric // do not pass it to other cc1 commands when save-temps is enabled 71530b57cec5SDimitry Andric if (C.getDriver().isSaveTempsEnabled() && 71540b57cec5SDimitry Andric !isa<PreprocessJobAction>(JA)) { 7155bdd1243dSDimitry Andric if (StringRef(Arg->getValue()) == "-finclude-default-header") 7156bdd1243dSDimitry Andric continue; 7157bdd1243dSDimitry Andric } 71580b57cec5SDimitry Andric CmdArgs.push_back(Arg->getValue()); 71590b57cec5SDimitry Andric } 71600b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 71610b57cec5SDimitry Andric A->claim(); 71620b57cec5SDimitry Andric 71630b57cec5SDimitry Andric // We translate this by hand to the -cc1 argument, since nightly test uses 71640b57cec5SDimitry Andric // it and developers have been trained to spell it with -mllvm. Both 71650b57cec5SDimitry Andric // spellings are now deprecated and should be removed. 71660b57cec5SDimitry Andric if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") { 71670b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-optzns"); 71680b57cec5SDimitry Andric } else { 71690b57cec5SDimitry Andric A->render(Args, CmdArgs); 71700b57cec5SDimitry Andric } 71710b57cec5SDimitry Andric } 71720b57cec5SDimitry Andric 71730b57cec5SDimitry Andric // With -save-temps, we want to save the unoptimized bitcode output from the 71740b57cec5SDimitry Andric // CompileJobAction, use -disable-llvm-passes to get pristine IR generated 71750b57cec5SDimitry Andric // by the frontend. 71760b57cec5SDimitry Andric // When -fembed-bitcode is enabled, optimized bitcode is emitted because it 71770b57cec5SDimitry Andric // has slightly different breakdown between stages. 71780b57cec5SDimitry Andric // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of 71790b57cec5SDimitry Andric // pristine IR generated by the frontend. Ideally, a new compile action should 71800b57cec5SDimitry Andric // be added so both IR can be captured. 71815ffd83dbSDimitry Andric if ((C.getDriver().isSaveTempsEnabled() || 71825ffd83dbSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP)) && 7183fe6060f1SDimitry Andric !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) && 71840b57cec5SDimitry Andric isa<CompileJobAction>(JA)) 71850b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 71860b57cec5SDimitry Andric 71870b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_undef); 71880b57cec5SDimitry Andric 71890b57cec5SDimitry Andric const char *Exec = D.getClangProgramPath(); 71900b57cec5SDimitry Andric 71910b57cec5SDimitry Andric // Optionally embed the -cc1 level arguments into the debug info or a 71920b57cec5SDimitry Andric // section, for build analysis. 71930b57cec5SDimitry Andric // Also record command line arguments into the debug info if 71940b57cec5SDimitry Andric // -grecord-gcc-switches options is set on. 71950b57cec5SDimitry Andric // By default, -gno-record-gcc-switches is set on and no recording. 71960b57cec5SDimitry Andric auto GRecordSwitches = 71970b57cec5SDimitry Andric Args.hasFlag(options::OPT_grecord_command_line, 71980b57cec5SDimitry Andric options::OPT_gno_record_command_line, false); 71990b57cec5SDimitry Andric auto FRecordSwitches = 72000b57cec5SDimitry Andric Args.hasFlag(options::OPT_frecord_command_line, 72010b57cec5SDimitry Andric options::OPT_fno_record_command_line, false); 720206c3fb27SDimitry Andric if (FRecordSwitches && !Triple.isOSBinFormatELF() && 720306c3fb27SDimitry Andric !Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO()) 72040b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 72050b57cec5SDimitry Andric << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args) 72060b57cec5SDimitry Andric << TripleStr; 72070b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) { 72080b57cec5SDimitry Andric ArgStringList OriginalArgs; 72090b57cec5SDimitry Andric for (const auto &Arg : Args) 72100b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 72110b57cec5SDimitry Andric 72120b57cec5SDimitry Andric SmallString<256> Flags; 72135ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 72140b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 72150b57cec5SDimitry Andric SmallString<128> EscapedArg; 72160b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 72170b57cec5SDimitry Andric Flags += " "; 72180b57cec5SDimitry Andric Flags += EscapedArg; 72190b57cec5SDimitry Andric } 72200b57cec5SDimitry Andric auto FlagsArgString = Args.MakeArgString(Flags); 72210b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches) { 72220b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 72230b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 72240b57cec5SDimitry Andric } 72250b57cec5SDimitry Andric if (FRecordSwitches) { 72260b57cec5SDimitry Andric CmdArgs.push_back("-record-command-line"); 72270b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 72280b57cec5SDimitry Andric } 72290b57cec5SDimitry Andric } 72300b57cec5SDimitry Andric 7231753f127fSDimitry Andric // Host-side offloading compilation receives all device-side outputs. Include 7232753f127fSDimitry Andric // them in the host compilation depending on the target. If the host inputs 7233753f127fSDimitry Andric // are not empty we use the new-driver scheme, otherwise use the old scheme. 72340b57cec5SDimitry Andric if ((IsCuda || IsHIP) && CudaDeviceInput) { 72350b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 72360b57cec5SDimitry Andric CmdArgs.push_back(CudaDeviceInput->getFilename()); 7237753f127fSDimitry Andric } else if (!HostOffloadingInputs.empty()) { 7238fcaf7f86SDimitry Andric if ((IsCuda || IsHIP) && !IsRDCMode) { 7239753f127fSDimitry Andric assert(HostOffloadingInputs.size() == 1 && "Only one input expected"); 7240753f127fSDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 7241753f127fSDimitry Andric CmdArgs.push_back(HostOffloadingInputs.front().getFilename()); 7242753f127fSDimitry Andric } else { 7243753f127fSDimitry Andric for (const InputInfo Input : HostOffloadingInputs) 7244753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fembed-offload-object=" + 7245753f127fSDimitry Andric TC.getInputFilename(Input))); 7246753f127fSDimitry Andric } 72470b57cec5SDimitry Andric } 72480b57cec5SDimitry Andric 72490b57cec5SDimitry Andric if (IsCuda) { 72500b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcuda_short_ptr, 72510b57cec5SDimitry Andric options::OPT_fno_cuda_short_ptr, false)) 72520b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-short-ptr"); 72530b57cec5SDimitry Andric } 72540b57cec5SDimitry Andric 7255fe6060f1SDimitry Andric if (IsCuda || IsHIP) { 7256fe6060f1SDimitry Andric // Determine the original source input. 7257fe6060f1SDimitry Andric const Action *SourceAction = &JA; 7258fe6060f1SDimitry Andric while (SourceAction->getKind() != Action::InputClass) { 7259fe6060f1SDimitry Andric assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 7260fe6060f1SDimitry Andric SourceAction = SourceAction->getInputs()[0]; 7261fe6060f1SDimitry Andric } 7262fe6060f1SDimitry Andric auto CUID = cast<InputAction>(SourceAction)->getId(); 7263fe6060f1SDimitry Andric if (!CUID.empty()) 7264fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); 7265fe6060f1SDimitry Andric } 7266fe6060f1SDimitry Andric 726781ad6265SDimitry Andric if (IsHIP) { 7268480093f4SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 726981ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgpu_default_stream_EQ); 727081ad6265SDimitry Andric } 7271480093f4SDimitry Andric 7272fe6060f1SDimitry Andric if (IsCudaDevice || IsHIPDevice) { 7273fe6060f1SDimitry Andric StringRef InlineThresh = 7274fe6060f1SDimitry Andric Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ); 7275fe6060f1SDimitry Andric if (!InlineThresh.empty()) { 7276fe6060f1SDimitry Andric std::string ArgStr = 7277fe6060f1SDimitry Andric std::string("-inline-threshold=") + InlineThresh.str(); 7278fe6060f1SDimitry Andric CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)}); 7279fe6060f1SDimitry Andric } 7280fe6060f1SDimitry Andric } 7281fe6060f1SDimitry Andric 728206c3fb27SDimitry Andric if (IsHIPDevice) 728306c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, 728406c3fb27SDimitry Andric options::OPT_fhip_fp32_correctly_rounded_divide_sqrt, 728506c3fb27SDimitry Andric options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt); 728606c3fb27SDimitry Andric 72870b57cec5SDimitry Andric // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path 72880b57cec5SDimitry Andric // to specify the result of the compile phase on the host, so the meaningful 728906c3fb27SDimitry Andric // device declarations can be identified. Also, -fopenmp-is-target-device is 729006c3fb27SDimitry Andric // passed along to tell the frontend that it is generating code for a device, 729106c3fb27SDimitry Andric // so that only the relevant declarations are emitted. 72920b57cec5SDimitry Andric if (IsOpenMPDevice) { 729306c3fb27SDimitry Andric CmdArgs.push_back("-fopenmp-is-target-device"); 72940b57cec5SDimitry Andric if (OpenMPDeviceInput) { 72950b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-host-ir-file-path"); 72960b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename())); 72970b57cec5SDimitry Andric } 72980b57cec5SDimitry Andric } 72990b57cec5SDimitry Andric 7300e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) { 7301e8d8bef9SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); 7302e8d8bef9SDimitry Andric 730381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_munsafe_fp_atomics, 730481ad6265SDimitry Andric options::OPT_mno_unsafe_fp_atomics); 730506c3fb27SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_mamdgpu_ieee, 730606c3fb27SDimitry Andric options::OPT_mno_amdgpu_ieee); 7307e8d8bef9SDimitry Andric } 7308e8d8bef9SDimitry Andric 73090b57cec5SDimitry Andric // For all the host OpenMP offloading compile jobs we need to pass the targets 73100b57cec5SDimitry Andric // information using -fopenmp-targets= option. 73110b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 731281ad6265SDimitry Andric SmallString<128> Targets("-fopenmp-targets="); 73130b57cec5SDimitry Andric 731481ad6265SDimitry Andric SmallVector<std::string, 4> Triples; 731581ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); 731681ad6265SDimitry Andric std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples), 731781ad6265SDimitry Andric [](auto TC) { return TC.second->getTripleString(); }); 731881ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ","))); 73190b57cec5SDimitry Andric } 73200b57cec5SDimitry Andric 7321a7dea167SDimitry Andric bool VirtualFunctionElimination = 7322a7dea167SDimitry Andric Args.hasFlag(options::OPT_fvirtual_function_elimination, 7323a7dea167SDimitry Andric options::OPT_fno_virtual_function_elimination, false); 7324a7dea167SDimitry Andric if (VirtualFunctionElimination) { 7325a7dea167SDimitry Andric // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO 7326a7dea167SDimitry Andric // in the future). 7327fe6060f1SDimitry Andric if (LTOMode != LTOK_Full) 7328a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 7329a7dea167SDimitry Andric << "-fvirtual-function-elimination" 7330a7dea167SDimitry Andric << "-flto=full"; 7331a7dea167SDimitry Andric 7332a7dea167SDimitry Andric CmdArgs.push_back("-fvirtual-function-elimination"); 7333a7dea167SDimitry Andric } 7334a7dea167SDimitry Andric 7335a7dea167SDimitry Andric // VFE requires whole-program-vtables, and enables it by default. 7336a7dea167SDimitry Andric bool WholeProgramVTables = Args.hasFlag( 7337a7dea167SDimitry Andric options::OPT_fwhole_program_vtables, 7338a7dea167SDimitry Andric options::OPT_fno_whole_program_vtables, VirtualFunctionElimination); 7339a7dea167SDimitry Andric if (VirtualFunctionElimination && !WholeProgramVTables) { 7340a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7341a7dea167SDimitry Andric << "-fno-whole-program-vtables" 7342a7dea167SDimitry Andric << "-fvirtual-function-elimination"; 7343a7dea167SDimitry Andric } 7344a7dea167SDimitry Andric 73450b57cec5SDimitry Andric if (WholeProgramVTables) { 734606c3fb27SDimitry Andric // PS4 uses the legacy LTO API, which does not support this feature in 734706c3fb27SDimitry Andric // ThinLTO mode. 734806c3fb27SDimitry Andric bool IsPS4 = getToolChain().getTriple().isPS4(); 734906c3fb27SDimitry Andric 7350fe6060f1SDimitry Andric // Check if we passed LTO options but they were suppressed because this is a 7351fe6060f1SDimitry Andric // device offloading action, or we passed device offload LTO options which 7352fe6060f1SDimitry Andric // were suppressed because this is not the device offload action. 735306c3fb27SDimitry Andric // Check if we are using PS4 in regular LTO mode. 7354fe6060f1SDimitry Andric // Otherwise, issue an error. 735506c3fb27SDimitry Andric if ((!IsUsingLTO && !D.isUsingLTO(!IsDeviceOffloadAction)) || 735606c3fb27SDimitry Andric (IsPS4 && !UnifiedLTO && (D.getLTOMode() != LTOK_Full))) 73570b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 73580b57cec5SDimitry Andric << "-fwhole-program-vtables" 735906c3fb27SDimitry Andric << ((IsPS4 && !UnifiedLTO) ? "-flto=full" : "-flto"); 736006c3fb27SDimitry Andric 736106c3fb27SDimitry Andric // Propagate -fwhole-program-vtables if this is an LTO compile. 736206c3fb27SDimitry Andric if (IsUsingLTO) 736306c3fb27SDimitry Andric CmdArgs.push_back("-fwhole-program-vtables"); 73640b57cec5SDimitry Andric } 73650b57cec5SDimitry Andric 7366480093f4SDimitry Andric bool DefaultsSplitLTOUnit = 736706c3fb27SDimitry Andric ((WholeProgramVTables || SanitizeArgs.needsLTO()) && 736806c3fb27SDimitry Andric (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit())) || 736906c3fb27SDimitry Andric (!Triple.isPS4() && UnifiedLTO); 73700b57cec5SDimitry Andric bool SplitLTOUnit = 73710b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_lto_unit, 7372a7dea167SDimitry Andric options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); 7373349cc55cSDimitry Andric if (SanitizeArgs.needsLTO() && !SplitLTOUnit) 7374a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" 7375a7dea167SDimitry Andric << "-fsanitize=cfi"; 73760b57cec5SDimitry Andric if (SplitLTOUnit) 73770b57cec5SDimitry Andric CmdArgs.push_back("-fsplit-lto-unit"); 73780b57cec5SDimitry Andric 73795ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, 73805ffd83dbSDimitry Andric options::OPT_fno_global_isel)) { 73810b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 73825ffd83dbSDimitry Andric if (A->getOption().matches(options::OPT_fglobal_isel)) { 73830b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=1"); 73840b57cec5SDimitry Andric 73850b57cec5SDimitry Andric // GISel is on by default on AArch64 -O0, so don't bother adding 73860b57cec5SDimitry Andric // the fallback remarks for it. Other combinations will add a warning of 73870b57cec5SDimitry Andric // some kind. 73880b57cec5SDimitry Andric bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64; 73890b57cec5SDimitry Andric bool IsOptLevelSupported = false; 73900b57cec5SDimitry Andric 73910b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_O_Group); 73920b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::aarch64) { 73930b57cec5SDimitry Andric if (!A || A->getOption().matches(options::OPT_O0)) 73940b57cec5SDimitry Andric IsOptLevelSupported = true; 73950b57cec5SDimitry Andric } 73960b57cec5SDimitry Andric if (!IsArchSupported || !IsOptLevelSupported) { 73970b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 73980b57cec5SDimitry Andric CmdArgs.push_back("-global-isel-abort=2"); 73990b57cec5SDimitry Andric 74000b57cec5SDimitry Andric if (!IsArchSupported) 74015ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete) << Triple.getArchName(); 74020b57cec5SDimitry Andric else 74035ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete_opt); 74040b57cec5SDimitry Andric } 74050b57cec5SDimitry Andric } else { 74060b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=0"); 74070b57cec5SDimitry Andric } 74080b57cec5SDimitry Andric } 74090b57cec5SDimitry Andric 74100b57cec5SDimitry Andric if (Args.hasArg(options::OPT_forder_file_instrumentation)) { 74110b57cec5SDimitry Andric CmdArgs.push_back("-forder-file-instrumentation"); 74120b57cec5SDimitry Andric // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is 74130b57cec5SDimitry Andric // on, we need to pass these flags as linker flags and that will be handled 74140b57cec5SDimitry Andric // outside of the compiler. 7415fe6060f1SDimitry Andric if (!IsUsingLTO) { 74160b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 74170b57cec5SDimitry Andric CmdArgs.push_back("-enable-order-file-instrumentation"); 74180b57cec5SDimitry Andric } 74190b57cec5SDimitry Andric } 74200b57cec5SDimitry Andric 74210b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128, 74220b57cec5SDimitry Andric options::OPT_fno_force_enable_int128)) { 74230b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fforce_enable_int128)) 74240b57cec5SDimitry Andric CmdArgs.push_back("-fforce-enable-int128"); 74250b57cec5SDimitry Andric } 74260b57cec5SDimitry Andric 742781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_consts, 742881ad6265SDimitry Andric options::OPT_fno_keep_static_consts); 742906c3fb27SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fkeep_persistent_storage_variables, 743006c3fb27SDimitry Andric options::OPT_fno_keep_persistent_storage_variables); 743181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcomplete_member_pointers, 743281ad6265SDimitry Andric options::OPT_fno_complete_member_pointers); 7433bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fcxx_static_destructors, 7434bdd1243dSDimitry Andric options::OPT_fno_cxx_static_destructors); 74350b57cec5SDimitry Andric 7436e8d8bef9SDimitry Andric addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false); 7437e8d8bef9SDimitry Andric 7438e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics, 7439e8d8bef9SDimitry Andric options::OPT_mno_outline_atomics)) { 7440e8d8bef9SDimitry Andric // Option -moutline-atomics supported for AArch64 target only. 7441e8d8bef9SDimitry Andric if (!Triple.isAArch64()) { 7442e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt) 74430eae32dcSDimitry Andric << Triple.getArchName() << A->getOption().getName(); 74440b57cec5SDimitry Andric } else { 74450eae32dcSDimitry Andric if (A->getOption().matches(options::OPT_moutline_atomics)) { 7446e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7447e8d8bef9SDimitry Andric CmdArgs.push_back("+outline-atomics"); 74480b57cec5SDimitry Andric } else { 7449e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7450e8d8bef9SDimitry Andric CmdArgs.push_back("-outline-atomics"); 74510b57cec5SDimitry Andric } 74520eae32dcSDimitry Andric } 7453fe6060f1SDimitry Andric } else if (Triple.isAArch64() && 7454fe6060f1SDimitry Andric getToolChain().IsAArch64OutlineAtomicsDefault(Args)) { 7455fe6060f1SDimitry Andric CmdArgs.push_back("-target-feature"); 7456fe6060f1SDimitry Andric CmdArgs.push_back("+outline-atomics"); 74570b57cec5SDimitry Andric } 74580b57cec5SDimitry Andric 7459bdd1243dSDimitry Andric if (Triple.isAArch64() && 7460bdd1243dSDimitry Andric (Args.hasArg(options::OPT_mno_fmv) || 7461bdd1243dSDimitry Andric getToolChain().GetRuntimeLibType(Args) != ToolChain::RLT_CompilerRT)) { 7462bdd1243dSDimitry Andric // Disable Function Multiversioning on AArch64 target. 7463bdd1243dSDimitry Andric CmdArgs.push_back("-target-feature"); 7464bdd1243dSDimitry Andric CmdArgs.push_back("-fmv"); 7465bdd1243dSDimitry Andric } 7466bdd1243dSDimitry Andric 74670b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig, 74680b57cec5SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 74690b57cec5SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 7470e8d8bef9SDimitry Andric !TC.getTriple().isPS4() && !TC.getTriple().isVE() && 74710b57cec5SDimitry Andric !TC.getTriple().isOSNetBSD() && 7472480093f4SDimitry Andric !Distro(D.getVFS(), TC.getTriple()).IsGentoo() && 7473e8d8bef9SDimitry Andric !TC.getTriple().isAndroid() && TC.useIntegratedAs())) 74740b57cec5SDimitry Andric CmdArgs.push_back("-faddrsig"); 74750b57cec5SDimitry Andric 7476fe6060f1SDimitry Andric if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && 7477bdd1243dSDimitry Andric (EH || UnwindTables || AsyncUnwindTables || 747806c3fb27SDimitry Andric DebugInfoKind != llvm::codegenoptions::NoDebugInfo)) 7479fe6060f1SDimitry Andric CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); 7480fe6060f1SDimitry Andric 74810b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { 74820b57cec5SDimitry Andric std::string Str = A->getAsString(Args); 74830b57cec5SDimitry Andric if (!TC.getTriple().isOSBinFormatELF()) 74840b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 74850b57cec5SDimitry Andric << Str << TC.getTripleString(); 74860b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str)); 74870b57cec5SDimitry Andric } 74880b57cec5SDimitry Andric 74890eae32dcSDimitry Andric // Add the output path to the object file for CodeView debug infos. 74900eae32dcSDimitry Andric if (EmitCodeView && Output.isFilename()) 74910eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 74920eae32dcSDimitry Andric Output.getFilename()); 74930eae32dcSDimitry Andric 74940b57cec5SDimitry Andric // Add the "-o out -x type src.c" flags last. This is done primarily to make 74950b57cec5SDimitry Andric // the -cc1 command easier to edit when reproducing compiler crashes. 74960b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 74970b57cec5SDimitry Andric // Handled with other dependency code. 74980b57cec5SDimitry Andric } else if (Output.isFilename()) { 7499480093f4SDimitry Andric if (Output.getType() == clang::driver::types::TY_IFS_CPP || 7500480093f4SDimitry Andric Output.getType() == clang::driver::types::TY_IFS) { 7501480093f4SDimitry Andric SmallString<128> OutputFilename(Output.getFilename()); 7502480093f4SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "ifs"); 7503480093f4SDimitry Andric CmdArgs.push_back("-o"); 7504480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 7505480093f4SDimitry Andric } else { 75060b57cec5SDimitry Andric CmdArgs.push_back("-o"); 75070b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 7508480093f4SDimitry Andric } 75090b57cec5SDimitry Andric } else { 75100b57cec5SDimitry Andric assert(Output.isNothing() && "Invalid output."); 75110b57cec5SDimitry Andric } 75120b57cec5SDimitry Andric 75130b57cec5SDimitry Andric addDashXForInput(Args, Input, CmdArgs); 75140b57cec5SDimitry Andric 75150b57cec5SDimitry Andric ArrayRef<InputInfo> FrontendInputs = Input; 7516bdd1243dSDimitry Andric if (IsExtractAPI) 751781ad6265SDimitry Andric FrontendInputs = ExtractAPIInputs; 75180b57cec5SDimitry Andric else if (Input.isNothing()) 75190b57cec5SDimitry Andric FrontendInputs = {}; 75200b57cec5SDimitry Andric 75210b57cec5SDimitry Andric for (const InputInfo &Input : FrontendInputs) { 75220b57cec5SDimitry Andric if (Input.isFilename()) 75230b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 75240b57cec5SDimitry Andric else 75250b57cec5SDimitry Andric Input.getInputArg().renderAsInput(Args, CmdArgs); 75260b57cec5SDimitry Andric } 75270b57cec5SDimitry Andric 7528fe6060f1SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 7529480093f4SDimitry Andric // Invoke the CC1 directly in this process 753006c3fb27SDimitry Andric C.addCommand(std::make_unique<CC1Command>( 753106c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 753206c3fb27SDimitry Andric Output, D.getPrependArg())); 75330b57cec5SDimitry Andric } else { 753406c3fb27SDimitry Andric C.addCommand(std::make_unique<Command>( 753506c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 753606c3fb27SDimitry Andric Output, D.getPrependArg())); 75370b57cec5SDimitry Andric } 75380b57cec5SDimitry Andric 75390b57cec5SDimitry Andric // Make the compile command echo its inputs for /showFilenames. 75400b57cec5SDimitry Andric if (Output.getType() == types::TY_Object && 75410b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_showFilenames, 75420b57cec5SDimitry Andric options::OPT__SLASH_showFilenames_, false)) { 754313138422SDimitry Andric C.getJobs().getJobs().back()->PrintInputFilenames = true; 75440b57cec5SDimitry Andric } 75450b57cec5SDimitry Andric 75460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_pg)) 75475ffd83dbSDimitry Andric if (FPKeepKind == CodeGenOptions::FramePointerKind::None && 75485ffd83dbSDimitry Andric !Args.hasArg(options::OPT_mfentry)) 75490b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer" 75500b57cec5SDimitry Andric << A->getAsString(Args); 75510b57cec5SDimitry Andric 75520b57cec5SDimitry Andric // Claim some arguments which clang supports automatically. 75530b57cec5SDimitry Andric 75540b57cec5SDimitry Andric // -fpch-preprocess is used with gcc to add a special marker in the output to 75550b57cec5SDimitry Andric // include the PCH file. 75560b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fpch_preprocess); 75570b57cec5SDimitry Andric 75580b57cec5SDimitry Andric // Claim some arguments which clang doesn't support, but we don't 75590b57cec5SDimitry Andric // care to warn the user about. 75600b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 75610b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 75620b57cec5SDimitry Andric 75630b57cec5SDimitry Andric // Disable warnings for clang -E -emit-llvm foo.c 75640b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 75650b57cec5SDimitry Andric } 75660b57cec5SDimitry Andric 75670eae32dcSDimitry Andric Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend) 75680b57cec5SDimitry Andric // CAUTION! The first constructor argument ("clang") is not arbitrary, 75690b57cec5SDimitry Andric // as it is for other tools. Some operations on a Tool actually test 75700b57cec5SDimitry Andric // whether that tool is Clang based on the Tool's Name as a string. 75710eae32dcSDimitry Andric : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {} 75720b57cec5SDimitry Andric 75730b57cec5SDimitry Andric Clang::~Clang() {} 75740b57cec5SDimitry Andric 75750b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI. 75760b57cec5SDimitry Andric /// 75770b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile. 75780b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 75795ffd83dbSDimitry Andric const InputInfoList &inputs, 75800b57cec5SDimitry Andric ArgStringList &cmdArgs, 75810b57cec5SDimitry Andric RewriteKind rewriteKind) const { 75820b57cec5SDimitry Andric // Look for the controlling runtime option. 75830b57cec5SDimitry Andric Arg *runtimeArg = 75840b57cec5SDimitry Andric args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime, 75850b57cec5SDimitry Andric options::OPT_fobjc_runtime_EQ); 75860b57cec5SDimitry Andric 75870b57cec5SDimitry Andric // Just forward -fobjc-runtime= to the frontend. This supercedes 75880b57cec5SDimitry Andric // options about fragility. 75890b57cec5SDimitry Andric if (runtimeArg && 75900b57cec5SDimitry Andric runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 75910b57cec5SDimitry Andric ObjCRuntime runtime; 75920b57cec5SDimitry Andric StringRef value = runtimeArg->getValue(); 75930b57cec5SDimitry Andric if (runtime.tryParse(value)) { 75940b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 75950b57cec5SDimitry Andric << value; 75960b57cec5SDimitry Andric } 75970b57cec5SDimitry Andric if ((runtime.getKind() == ObjCRuntime::GNUstep) && 75980b57cec5SDimitry Andric (runtime.getVersion() >= VersionTuple(2, 0))) 75990b57cec5SDimitry Andric if (!getToolChain().getTriple().isOSBinFormatELF() && 76000b57cec5SDimitry Andric !getToolChain().getTriple().isOSBinFormatCOFF()) { 76010b57cec5SDimitry Andric getToolChain().getDriver().Diag( 76020b57cec5SDimitry Andric diag::err_drv_gnustep_objc_runtime_incompatible_binary) 76030b57cec5SDimitry Andric << runtime.getVersion().getMajor(); 76040b57cec5SDimitry Andric } 76050b57cec5SDimitry Andric 76060b57cec5SDimitry Andric runtimeArg->render(args, cmdArgs); 76070b57cec5SDimitry Andric return runtime; 76080b57cec5SDimitry Andric } 76090b57cec5SDimitry Andric 76100b57cec5SDimitry Andric // Otherwise, we'll need the ABI "version". Version numbers are 76110b57cec5SDimitry Andric // slightly confusing for historical reasons: 76120b57cec5SDimitry Andric // 1 - Traditional "fragile" ABI 76130b57cec5SDimitry Andric // 2 - Non-fragile ABI, version 1 76140b57cec5SDimitry Andric // 3 - Non-fragile ABI, version 2 76150b57cec5SDimitry Andric unsigned objcABIVersion = 1; 76160b57cec5SDimitry Andric // If -fobjc-abi-version= is present, use that to set the version. 76170b57cec5SDimitry Andric if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 76180b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 76190b57cec5SDimitry Andric if (value == "1") 76200b57cec5SDimitry Andric objcABIVersion = 1; 76210b57cec5SDimitry Andric else if (value == "2") 76220b57cec5SDimitry Andric objcABIVersion = 2; 76230b57cec5SDimitry Andric else if (value == "3") 76240b57cec5SDimitry Andric objcABIVersion = 3; 76250b57cec5SDimitry Andric else 76260b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value; 76270b57cec5SDimitry Andric } else { 76280b57cec5SDimitry Andric // Otherwise, determine if we are using the non-fragile ABI. 76290b57cec5SDimitry Andric bool nonFragileABIIsDefault = 76300b57cec5SDimitry Andric (rewriteKind == RK_NonFragile || 76310b57cec5SDimitry Andric (rewriteKind == RK_None && 76320b57cec5SDimitry Andric getToolChain().IsObjCNonFragileABIDefault())); 76330b57cec5SDimitry Andric if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 76340b57cec5SDimitry Andric options::OPT_fno_objc_nonfragile_abi, 76350b57cec5SDimitry Andric nonFragileABIIsDefault)) { 76360b57cec5SDimitry Andric // Determine the non-fragile ABI version to use. 76370b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 76380b57cec5SDimitry Andric unsigned nonFragileABIVersion = 1; 76390b57cec5SDimitry Andric #else 76400b57cec5SDimitry Andric unsigned nonFragileABIVersion = 2; 76410b57cec5SDimitry Andric #endif 76420b57cec5SDimitry Andric 76430b57cec5SDimitry Andric if (Arg *abiArg = 76440b57cec5SDimitry Andric args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) { 76450b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 76460b57cec5SDimitry Andric if (value == "1") 76470b57cec5SDimitry Andric nonFragileABIVersion = 1; 76480b57cec5SDimitry Andric else if (value == "2") 76490b57cec5SDimitry Andric nonFragileABIVersion = 2; 76500b57cec5SDimitry Andric else 76510b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 76520b57cec5SDimitry Andric << value; 76530b57cec5SDimitry Andric } 76540b57cec5SDimitry Andric 76550b57cec5SDimitry Andric objcABIVersion = 1 + nonFragileABIVersion; 76560b57cec5SDimitry Andric } else { 76570b57cec5SDimitry Andric objcABIVersion = 1; 76580b57cec5SDimitry Andric } 76590b57cec5SDimitry Andric } 76600b57cec5SDimitry Andric 76610b57cec5SDimitry Andric // We don't actually care about the ABI version other than whether 76620b57cec5SDimitry Andric // it's non-fragile. 76630b57cec5SDimitry Andric bool isNonFragile = objcABIVersion != 1; 76640b57cec5SDimitry Andric 76650b57cec5SDimitry Andric // If we have no runtime argument, ask the toolchain for its default runtime. 76660b57cec5SDimitry Andric // However, the rewriter only really supports the Mac runtime, so assume that. 76670b57cec5SDimitry Andric ObjCRuntime runtime; 76680b57cec5SDimitry Andric if (!runtimeArg) { 76690b57cec5SDimitry Andric switch (rewriteKind) { 76700b57cec5SDimitry Andric case RK_None: 76710b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 76720b57cec5SDimitry Andric break; 76730b57cec5SDimitry Andric case RK_Fragile: 76740b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 76750b57cec5SDimitry Andric break; 76760b57cec5SDimitry Andric case RK_NonFragile: 76770b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 76780b57cec5SDimitry Andric break; 76790b57cec5SDimitry Andric } 76800b57cec5SDimitry Andric 76810b57cec5SDimitry Andric // -fnext-runtime 76820b57cec5SDimitry Andric } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 76830b57cec5SDimitry Andric // On Darwin, make this use the default behavior for the toolchain. 76840b57cec5SDimitry Andric if (getToolChain().getTriple().isOSDarwin()) { 76850b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 76860b57cec5SDimitry Andric 76870b57cec5SDimitry Andric // Otherwise, build for a generic macosx port. 76880b57cec5SDimitry Andric } else { 76890b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 76900b57cec5SDimitry Andric } 76910b57cec5SDimitry Andric 76920b57cec5SDimitry Andric // -fgnu-runtime 76930b57cec5SDimitry Andric } else { 76940b57cec5SDimitry Andric assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 76950b57cec5SDimitry Andric // Legacy behaviour is to target the gnustep runtime if we are in 76960b57cec5SDimitry Andric // non-fragile mode or the GCC runtime in fragile mode. 76970b57cec5SDimitry Andric if (isNonFragile) 76980b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0)); 76990b57cec5SDimitry Andric else 77000b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 77010b57cec5SDimitry Andric } 77020b57cec5SDimitry Andric 77035ffd83dbSDimitry Andric if (llvm::any_of(inputs, [](const InputInfo &input) { 77045ffd83dbSDimitry Andric return types::isObjC(input.getType()); 77055ffd83dbSDimitry Andric })) 77060b57cec5SDimitry Andric cmdArgs.push_back( 77070b57cec5SDimitry Andric args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); 77080b57cec5SDimitry Andric return runtime; 77090b57cec5SDimitry Andric } 77100b57cec5SDimitry Andric 77110b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) { 77120b57cec5SDimitry Andric bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-'); 77130b57cec5SDimitry Andric I += HaveDash; 77140b57cec5SDimitry Andric return !HaveDash; 77150b57cec5SDimitry Andric } 77160b57cec5SDimitry Andric 77170b57cec5SDimitry Andric namespace { 77180b57cec5SDimitry Andric struct EHFlags { 77190b57cec5SDimitry Andric bool Synch = false; 77200b57cec5SDimitry Andric bool Asynch = false; 77210b57cec5SDimitry Andric bool NoUnwindC = false; 77220b57cec5SDimitry Andric }; 77230b57cec5SDimitry Andric } // end anonymous namespace 77240b57cec5SDimitry Andric 77250b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are 77260b57cec5SDimitry Andric /// thrown. There are three modifiers: 77270b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions. 77280b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions. 77290b57cec5SDimitry Andric /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR. 77300b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind. 77310b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled. 77320b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { 77330b57cec5SDimitry Andric EHFlags EH; 77340b57cec5SDimitry Andric 77350b57cec5SDimitry Andric std::vector<std::string> EHArgs = 77360b57cec5SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_EH); 77370b57cec5SDimitry Andric for (auto EHVal : EHArgs) { 77380b57cec5SDimitry Andric for (size_t I = 0, E = EHVal.size(); I != E; ++I) { 77390b57cec5SDimitry Andric switch (EHVal[I]) { 77400b57cec5SDimitry Andric case 'a': 77410b57cec5SDimitry Andric EH.Asynch = maybeConsumeDash(EHVal, I); 77420b57cec5SDimitry Andric if (EH.Asynch) 77430b57cec5SDimitry Andric EH.Synch = false; 77440b57cec5SDimitry Andric continue; 77450b57cec5SDimitry Andric case 'c': 77460b57cec5SDimitry Andric EH.NoUnwindC = maybeConsumeDash(EHVal, I); 77470b57cec5SDimitry Andric continue; 77480b57cec5SDimitry Andric case 's': 77490b57cec5SDimitry Andric EH.Synch = maybeConsumeDash(EHVal, I); 77500b57cec5SDimitry Andric if (EH.Synch) 77510b57cec5SDimitry Andric EH.Asynch = false; 77520b57cec5SDimitry Andric continue; 77530b57cec5SDimitry Andric default: 77540b57cec5SDimitry Andric break; 77550b57cec5SDimitry Andric } 77560b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal; 77570b57cec5SDimitry Andric break; 77580b57cec5SDimitry Andric } 77590b57cec5SDimitry Andric } 77600b57cec5SDimitry Andric // The /GX, /GX- flags are only processed if there are not /EH flags. 77610b57cec5SDimitry Andric // The default is that /GX is not specified. 77620b57cec5SDimitry Andric if (EHArgs.empty() && 77630b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_, 77640b57cec5SDimitry Andric /*Default=*/false)) { 77650b57cec5SDimitry Andric EH.Synch = true; 77660b57cec5SDimitry Andric EH.NoUnwindC = true; 77670b57cec5SDimitry Andric } 77680b57cec5SDimitry Andric 776981ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 777081ad6265SDimitry Andric EH.Synch = false; 777181ad6265SDimitry Andric EH.NoUnwindC = false; 777281ad6265SDimitry Andric EH.Asynch = false; 777381ad6265SDimitry Andric } 777481ad6265SDimitry Andric 77750b57cec5SDimitry Andric return EH; 77760b57cec5SDimitry Andric } 77770b57cec5SDimitry Andric 77780b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, 77790b57cec5SDimitry Andric ArgStringList &CmdArgs, 778006c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind *DebugInfoKind, 77810b57cec5SDimitry Andric bool *EmitCodeView) const { 77825ffd83dbSDimitry Andric bool isNVPTX = getToolChain().getTriple().isNVPTX(); 77830b57cec5SDimitry Andric 7784bdd1243dSDimitry Andric ProcessVSRuntimeLibrary(Args, CmdArgs); 77850b57cec5SDimitry Andric 77865ffd83dbSDimitry Andric if (Arg *ShowIncludes = 77875ffd83dbSDimitry Andric Args.getLastArg(options::OPT__SLASH_showIncludes, 77885ffd83dbSDimitry Andric options::OPT__SLASH_showIncludes_user)) { 77895ffd83dbSDimitry Andric CmdArgs.push_back("--show-includes"); 77905ffd83dbSDimitry Andric if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes)) 77915ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 77925ffd83dbSDimitry Andric } 77930b57cec5SDimitry Andric 77940b57cec5SDimitry Andric // This controls whether or not we emit RTTI data for polymorphic types. 77950b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR, 77960b57cec5SDimitry Andric /*Default=*/false)) 77970b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti-data"); 77980b57cec5SDimitry Andric 77990b57cec5SDimitry Andric // This controls whether or not we emit stack-protector instrumentation. 78000b57cec5SDimitry Andric // In MSVC, Buffer Security Check (/GS) is on by default. 78015ffd83dbSDimitry Andric if (!isNVPTX && Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_, 78020b57cec5SDimitry Andric /*Default=*/true)) { 78030b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 78040b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong))); 78050b57cec5SDimitry Andric } 78060b57cec5SDimitry Andric 7807e8d8bef9SDimitry Andric // Emit CodeView if -Z7 or -gline-tables-only are present. 7808e8d8bef9SDimitry Andric if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7, 78090b57cec5SDimitry Andric options::OPT_gline_tables_only)) { 78100b57cec5SDimitry Andric *EmitCodeView = true; 78110b57cec5SDimitry Andric if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7)) 781206c3fb27SDimitry Andric *DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor; 78130b57cec5SDimitry Andric else 781406c3fb27SDimitry Andric *DebugInfoKind = llvm::codegenoptions::DebugLineTablesOnly; 78150b57cec5SDimitry Andric } else { 78160b57cec5SDimitry Andric *EmitCodeView = false; 78170b57cec5SDimitry Andric } 78180b57cec5SDimitry Andric 78190b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 782081ad6265SDimitry Andric 782181ad6265SDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 782281ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_JMC, options::OPT__SLASH_JMC_, 782381ad6265SDimitry Andric /*Default=*/false)) { 782406c3fb27SDimitry Andric if (*EmitCodeView && 782506c3fb27SDimitry Andric *DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor) 782681ad6265SDimitry Andric CmdArgs.push_back("-fjmc"); 782781ad6265SDimitry Andric else 782881ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC" 782981ad6265SDimitry Andric << "'/Zi', '/Z7'"; 783081ad6265SDimitry Andric } 783181ad6265SDimitry Andric 78320b57cec5SDimitry Andric EHFlags EH = parseClangCLEHFlags(D, Args); 78335ffd83dbSDimitry Andric if (!isNVPTX && (EH.Synch || EH.Asynch)) { 78340b57cec5SDimitry Andric if (types::isCXX(InputType)) 78350b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 78360b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 783706c3fb27SDimitry Andric if (EH.Asynch) 783806c3fb27SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 78390b57cec5SDimitry Andric } 78400b57cec5SDimitry Andric if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC) 78410b57cec5SDimitry Andric CmdArgs.push_back("-fexternc-nounwind"); 78420b57cec5SDimitry Andric 78430b57cec5SDimitry Andric // /EP should expand to -E -P. 78440b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_EP)) { 78450b57cec5SDimitry Andric CmdArgs.push_back("-E"); 78460b57cec5SDimitry Andric CmdArgs.push_back("-P"); 78470b57cec5SDimitry Andric } 78480b57cec5SDimitry Andric 78490b57cec5SDimitry Andric unsigned VolatileOptionID; 7850480093f4SDimitry Andric if (getToolChain().getTriple().isX86()) 78510b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_ms; 78520b57cec5SDimitry Andric else 78530b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_iso; 78540b57cec5SDimitry Andric 78550b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group)) 78560b57cec5SDimitry Andric VolatileOptionID = A->getOption().getID(); 78570b57cec5SDimitry Andric 78580b57cec5SDimitry Andric if (VolatileOptionID == options::OPT__SLASH_volatile_ms) 78590b57cec5SDimitry Andric CmdArgs.push_back("-fms-volatile"); 78600b57cec5SDimitry Andric 78610b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_, 78620b57cec5SDimitry Andric options::OPT__SLASH_Zc_dllexportInlines, 78630b57cec5SDimitry Andric false)) { 78640b57cec5SDimitry Andric CmdArgs.push_back("-fno-dllexport-inlines"); 78650b57cec5SDimitry Andric } 78660b57cec5SDimitry Andric 786781ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_wchar_t_, 786881ad6265SDimitry Andric options::OPT__SLASH_Zc_wchar_t, false)) { 786981ad6265SDimitry Andric CmdArgs.push_back("-fno-wchar"); 787081ad6265SDimitry Andric } 787181ad6265SDimitry Andric 787281ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 787381ad6265SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 787481ad6265SDimitry Andric std::vector<std::string> Values = 787581ad6265SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_arch); 787681ad6265SDimitry Andric if (!Values.empty()) { 787781ad6265SDimitry Andric llvm::SmallSet<std::string, 4> SupportedArches; 787881ad6265SDimitry Andric if (Arch == llvm::Triple::x86) 787981ad6265SDimitry Andric SupportedArches.insert("IA32"); 788081ad6265SDimitry Andric 788181ad6265SDimitry Andric for (auto &V : Values) 788281ad6265SDimitry Andric if (!SupportedArches.contains(V)) 788381ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 788481ad6265SDimitry Andric << std::string("/arch:").append(V) << "/kernel"; 788581ad6265SDimitry Andric } 788681ad6265SDimitry Andric 788781ad6265SDimitry Andric CmdArgs.push_back("-fno-rtti"); 788881ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR, options::OPT__SLASH_GR_, false)) 788981ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "/GR" 789081ad6265SDimitry Andric << "/kernel"; 789181ad6265SDimitry Andric } 789281ad6265SDimitry Andric 78930b57cec5SDimitry Andric Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); 78940b57cec5SDimitry Andric Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb); 78950b57cec5SDimitry Andric if (MostGeneralArg && BestCaseArg) 78960b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 78970b57cec5SDimitry Andric << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args); 78980b57cec5SDimitry Andric 78990b57cec5SDimitry Andric if (MostGeneralArg) { 79000b57cec5SDimitry Andric Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms); 79010b57cec5SDimitry Andric Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm); 79020b57cec5SDimitry Andric Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv); 79030b57cec5SDimitry Andric 79040b57cec5SDimitry Andric Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg; 79050b57cec5SDimitry Andric Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg; 79060b57cec5SDimitry Andric if (FirstConflict && SecondConflict && FirstConflict != SecondConflict) 79070b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 79080b57cec5SDimitry Andric << FirstConflict->getAsString(Args) 79090b57cec5SDimitry Andric << SecondConflict->getAsString(Args); 79100b57cec5SDimitry Andric 79110b57cec5SDimitry Andric if (SingleArg) 79120b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=single"); 79130b57cec5SDimitry Andric else if (MultipleArg) 79140b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=multiple"); 79150b57cec5SDimitry Andric else 79160b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=virtual"); 79170b57cec5SDimitry Andric } 79180b57cec5SDimitry Andric 79190b57cec5SDimitry Andric // Parse the default calling convention options. 79200b57cec5SDimitry Andric if (Arg *CCArg = 79210b57cec5SDimitry Andric Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr, 79220b57cec5SDimitry Andric options::OPT__SLASH_Gz, options::OPT__SLASH_Gv, 79230b57cec5SDimitry Andric options::OPT__SLASH_Gregcall)) { 79240b57cec5SDimitry Andric unsigned DCCOptId = CCArg->getOption().getID(); 79250b57cec5SDimitry Andric const char *DCCFlag = nullptr; 79265ffd83dbSDimitry Andric bool ArchSupported = !isNVPTX; 79270b57cec5SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 79280b57cec5SDimitry Andric switch (DCCOptId) { 79290b57cec5SDimitry Andric case options::OPT__SLASH_Gd: 79300b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=cdecl"; 79310b57cec5SDimitry Andric break; 79320b57cec5SDimitry Andric case options::OPT__SLASH_Gr: 79330b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 79340b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=fastcall"; 79350b57cec5SDimitry Andric break; 79360b57cec5SDimitry Andric case options::OPT__SLASH_Gz: 79370b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 79380b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=stdcall"; 79390b57cec5SDimitry Andric break; 79400b57cec5SDimitry Andric case options::OPT__SLASH_Gv: 79410b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 79420b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=vectorcall"; 79430b57cec5SDimitry Andric break; 79440b57cec5SDimitry Andric case options::OPT__SLASH_Gregcall: 79450b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 79460b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=regcall"; 79470b57cec5SDimitry Andric break; 79480b57cec5SDimitry Andric } 79490b57cec5SDimitry Andric 79500b57cec5SDimitry Andric // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either. 79510b57cec5SDimitry Andric if (ArchSupported && DCCFlag) 79520b57cec5SDimitry Andric CmdArgs.push_back(DCCFlag); 79530b57cec5SDimitry Andric } 79540b57cec5SDimitry Andric 79550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ); 79560b57cec5SDimitry Andric 79570b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { 79580b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 79590b57cec5SDimitry Andric CmdArgs.push_back("msvc"); 79600b57cec5SDimitry Andric } 79610b57cec5SDimitry Andric 796281ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) 796381ad6265SDimitry Andric CmdArgs.push_back("-fms-kernel"); 796481ad6265SDimitry Andric 7965a324c340SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__SLASH_guard)) { 7966480093f4SDimitry Andric StringRef GuardArgs = A->getValue(); 7967fe6060f1SDimitry Andric // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and 7968fe6060f1SDimitry Andric // "ehcont-". 7969fe6060f1SDimitry Andric if (GuardArgs.equals_insensitive("cf")) { 7970480093f4SDimitry Andric // Emit CFG instrumentation and the table of address-taken functions. 79710b57cec5SDimitry Andric CmdArgs.push_back("-cfguard"); 7972fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf,nochecks")) { 7973480093f4SDimitry Andric // Emit only the table of address-taken functions. 7974480093f4SDimitry Andric CmdArgs.push_back("-cfguard-no-checks"); 7975fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("ehcont")) { 7976fe6060f1SDimitry Andric // Emit EH continuation table. 7977fe6060f1SDimitry Andric CmdArgs.push_back("-ehcontguard"); 7978fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf-") || 7979fe6060f1SDimitry Andric GuardArgs.equals_insensitive("ehcont-")) { 7980480093f4SDimitry Andric // Do nothing, but we might want to emit a security warning in future. 7981480093f4SDimitry Andric } else { 7982480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs; 7983480093f4SDimitry Andric } 79842efbaac7SDimitry Andric A->claim(); 79850b57cec5SDimitry Andric } 79860b57cec5SDimitry Andric } 79870b57cec5SDimitry Andric 79880b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args, 79890b57cec5SDimitry Andric const InputInfo &Input) { 79900b57cec5SDimitry Andric return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())); 79910b57cec5SDimitry Andric } 79920b57cec5SDimitry Andric 79930b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args, 79940b57cec5SDimitry Andric const InputInfoList &Inputs) { 79950b57cec5SDimitry Andric const char *Str = getBaseInputName(Args, Inputs[0]); 79960b57cec5SDimitry Andric 79970b57cec5SDimitry Andric if (const char *End = strrchr(Str, '.')) 79980b57cec5SDimitry Andric return Args.MakeArgString(std::string(Str, End)); 79990b57cec5SDimitry Andric 80000b57cec5SDimitry Andric return Str; 80010b57cec5SDimitry Andric } 80020b57cec5SDimitry Andric 80030b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args, 80040b57cec5SDimitry Andric const InputInfoList &Inputs) { 80050b57cec5SDimitry Andric // FIXME: Think about this more. 80060b57cec5SDimitry Andric 80070b57cec5SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 8008a7dea167SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 8009a7dea167SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d')); 8010a7dea167SDimitry Andric return Args.MakeArgString(OutputFilename); 80110b57cec5SDimitry Andric } 8012a7dea167SDimitry Andric 8013a7dea167SDimitry Andric return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d"); 80140b57cec5SDimitry Andric } 80150b57cec5SDimitry Andric 80160b57cec5SDimitry Andric // Begin ClangAs 80170b57cec5SDimitry Andric 80180b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args, 80190b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 80200b57cec5SDimitry Andric StringRef CPUName; 80210b57cec5SDimitry Andric StringRef ABIName; 80220b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 80230b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 80240b57cec5SDimitry Andric 80250b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 80260b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 80270b57cec5SDimitry Andric } 80280b57cec5SDimitry Andric 80290b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args, 80300b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 80315ffd83dbSDimitry Andric addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs, 80325ffd83dbSDimitry Andric /*IsLTO=*/false); 8033480093f4SDimitry Andric 80340b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 80350b57cec5SDimitry Andric StringRef Value = A->getValue(); 80360b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 80370b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 80380b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 80390b57cec5SDimitry Andric } else { 80400b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) 8041bdd1243dSDimitry Andric << A->getSpelling() << Value; 80420b57cec5SDimitry Andric } 80430b57cec5SDimitry Andric } 80440b57cec5SDimitry Andric } 80450b57cec5SDimitry Andric 804606c3fb27SDimitry Andric void ClangAs::AddLoongArchTargetArgs(const ArgList &Args, 804706c3fb27SDimitry Andric ArgStringList &CmdArgs) const { 804806c3fb27SDimitry Andric CmdArgs.push_back("-target-abi"); 804906c3fb27SDimitry Andric CmdArgs.push_back(loongarch::getLoongArchABI(getToolChain().getDriver(), Args, 805006c3fb27SDimitry Andric getToolChain().getTriple()) 805106c3fb27SDimitry Andric .data()); 805206c3fb27SDimitry Andric } 805306c3fb27SDimitry Andric 80540b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args, 80550b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 80560b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 80570b57cec5SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 80580b57cec5SDimitry Andric 80590b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 80600b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 806106c3fb27SDimitry Andric 806206c3fb27SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 806306c3fb27SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 806406c3fb27SDimitry Andric CmdArgs.push_back("-mllvm"); 806506c3fb27SDimitry Andric CmdArgs.push_back("-riscv-add-build-attributes"); 806606c3fb27SDimitry Andric } 80670b57cec5SDimitry Andric } 80680b57cec5SDimitry Andric 80690b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 80700b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 80710b57cec5SDimitry Andric const ArgList &Args, 80720b57cec5SDimitry Andric const char *LinkingOutput) const { 80730b57cec5SDimitry Andric ArgStringList CmdArgs; 80740b57cec5SDimitry Andric 80750b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Unexpected number of inputs."); 80760b57cec5SDimitry Andric const InputInfo &Input = Inputs[0]; 80770b57cec5SDimitry Andric 80780b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 80790b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 80800b57cec5SDimitry Andric const auto &D = getToolChain().getDriver(); 80810b57cec5SDimitry Andric 80820b57cec5SDimitry Andric // Don't warn about "clang -w -c foo.s" 80830b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_w); 80840b57cec5SDimitry Andric // and "clang -emit-llvm -c foo.s" 80850b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 80860b57cec5SDimitry Andric 80870b57cec5SDimitry Andric claimNoWarnArgs(Args); 80880b57cec5SDimitry Andric 80890b57cec5SDimitry Andric // Invoke ourselves in -cc1as mode. 80900b57cec5SDimitry Andric // 80910b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 80920b57cec5SDimitry Andric CmdArgs.push_back("-cc1as"); 80930b57cec5SDimitry Andric 80940b57cec5SDimitry Andric // Add the "effective" target triple. 80950b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 80960b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 8097bdd1243dSDimitry Andric 8098bdd1243dSDimitry Andric getToolChain().addClangCC1ASTargetOptions(Args, CmdArgs); 80990b57cec5SDimitry Andric 81000b57cec5SDimitry Andric // Set the output mode, we currently only expect to be used as a real 81010b57cec5SDimitry Andric // assembler. 81020b57cec5SDimitry Andric CmdArgs.push_back("-filetype"); 81030b57cec5SDimitry Andric CmdArgs.push_back("obj"); 81040b57cec5SDimitry Andric 81050b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 81060b57cec5SDimitry Andric // -save-temps or preprocessed assembly. 81070b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 81080b57cec5SDimitry Andric CmdArgs.push_back(Clang::getBaseInputName(Args, Input)); 81090b57cec5SDimitry Andric 81100b57cec5SDimitry Andric // Add the target cpu 8111349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ true); 81120b57cec5SDimitry Andric if (!CPU.empty()) { 81130b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 81140b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 81150b57cec5SDimitry Andric } 81160b57cec5SDimitry Andric 81170b57cec5SDimitry Andric // Add the target features 81185ffd83dbSDimitry Andric getTargetFeatures(D, Triple, Args, CmdArgs, true); 81190b57cec5SDimitry Andric 81200b57cec5SDimitry Andric // Ignore explicit -force_cpusubtype_ALL option. 81210b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 81220b57cec5SDimitry Andric 81230b57cec5SDimitry Andric // Pass along any -I options so we get proper .include search paths. 81240b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I_Group); 81250b57cec5SDimitry Andric 81260b57cec5SDimitry Andric // Determine the original source input. 81270eae32dcSDimitry Andric auto FindSource = [](const Action *S) -> const Action * { 81280eae32dcSDimitry Andric while (S->getKind() != Action::InputClass) { 81290eae32dcSDimitry Andric assert(!S->getInputs().empty() && "unexpected root action!"); 81300eae32dcSDimitry Andric S = S->getInputs()[0]; 81310b57cec5SDimitry Andric } 81320eae32dcSDimitry Andric return S; 81330eae32dcSDimitry Andric }; 81340eae32dcSDimitry Andric const Action *SourceAction = FindSource(&JA); 81350b57cec5SDimitry Andric 81360b57cec5SDimitry Andric // Forward -g and handle debug info related flags, assuming we are dealing 81370b57cec5SDimitry Andric // with an actual assembly file. 81380b57cec5SDimitry Andric bool WantDebug = false; 81390b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_Group); 8140fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 81410b57cec5SDimitry Andric WantDebug = !A->getOption().matches(options::OPT_g0) && 81420b57cec5SDimitry Andric !A->getOption().matches(options::OPT_ggdb0); 8143480093f4SDimitry Andric 814406c3fb27SDimitry Andric llvm::codegenoptions::DebugInfoKind DebugInfoKind = 814506c3fb27SDimitry Andric llvm::codegenoptions::NoDebugInfo; 81460b57cec5SDimitry Andric 81470eae32dcSDimitry Andric // Add the -fdebug-compilation-dir flag if needed. 81480eae32dcSDimitry Andric const char *DebugCompilationDir = 81490eae32dcSDimitry Andric addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); 81500eae32dcSDimitry Andric 81510b57cec5SDimitry Andric if (SourceAction->getType() == types::TY_Asm || 81520b57cec5SDimitry Andric SourceAction->getType() == types::TY_PP_Asm) { 81530b57cec5SDimitry Andric // You might think that it would be ok to set DebugInfoKind outside of 81540b57cec5SDimitry Andric // the guard for source type, however there is a test which asserts 81550b57cec5SDimitry Andric // that some assembler invocation receives no -debug-info-kind, 81560b57cec5SDimitry Andric // and it's not clear whether that test is just overly restrictive. 815706c3fb27SDimitry Andric DebugInfoKind = (WantDebug ? llvm::codegenoptions::DebugInfoConstructor 815806c3fb27SDimitry Andric : llvm::codegenoptions::NoDebugInfo); 81590b57cec5SDimitry Andric 816081ad6265SDimitry Andric addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args, 816181ad6265SDimitry Andric CmdArgs); 81620b57cec5SDimitry Andric 81630b57cec5SDimitry Andric // Set the AT_producer to the clang version when using the integrated 81640b57cec5SDimitry Andric // assembler on assembly source files. 81650b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-producer"); 81660b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 81670b57cec5SDimitry Andric 81680b57cec5SDimitry Andric // And pass along -I options 81690b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I); 81700b57cec5SDimitry Andric } 8171bdd1243dSDimitry Andric const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); 81720b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion, 81730b57cec5SDimitry Andric llvm::DebuggerKind::Default); 8174fe6060f1SDimitry Andric renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion); 81750b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain()); 81760b57cec5SDimitry Andric 81770b57cec5SDimitry Andric // Handle -fPIC et al -- the relocation-model affects the assembler 81780b57cec5SDimitry Andric // for some targets. 81790b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 81800b57cec5SDimitry Andric unsigned PICLevel; 81810b57cec5SDimitry Andric bool IsPIE; 81820b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 81830b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 81840b57cec5SDimitry Andric 81850b57cec5SDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 81860b57cec5SDimitry Andric if (RMName) { 81870b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 81880b57cec5SDimitry Andric CmdArgs.push_back(RMName); 81890b57cec5SDimitry Andric } 81900b57cec5SDimitry Andric 81910b57cec5SDimitry Andric // Optionally embed the -cc1as level arguments into the debug info, for build 81920b57cec5SDimitry Andric // analysis. 81930b57cec5SDimitry Andric if (getToolChain().UseDwarfDebugFlags()) { 81940b57cec5SDimitry Andric ArgStringList OriginalArgs; 81950b57cec5SDimitry Andric for (const auto &Arg : Args) 81960b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 81970b57cec5SDimitry Andric 81980b57cec5SDimitry Andric SmallString<256> Flags; 81990b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 82005ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 82010b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 82020b57cec5SDimitry Andric SmallString<128> EscapedArg; 82030b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 82040b57cec5SDimitry Andric Flags += " "; 82050b57cec5SDimitry Andric Flags += EscapedArg; 82060b57cec5SDimitry Andric } 82070b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 82080b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Flags)); 82090b57cec5SDimitry Andric } 82100b57cec5SDimitry Andric 82110b57cec5SDimitry Andric // FIXME: Add -static support, once we have it. 82120b57cec5SDimitry Andric 82130b57cec5SDimitry Andric // Add target specific flags. 82140b57cec5SDimitry Andric switch (getToolChain().getArch()) { 82150b57cec5SDimitry Andric default: 82160b57cec5SDimitry Andric break; 82170b57cec5SDimitry Andric 82180b57cec5SDimitry Andric case llvm::Triple::mips: 82190b57cec5SDimitry Andric case llvm::Triple::mipsel: 82200b57cec5SDimitry Andric case llvm::Triple::mips64: 82210b57cec5SDimitry Andric case llvm::Triple::mips64el: 82220b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 82230b57cec5SDimitry Andric break; 82240b57cec5SDimitry Andric 82250b57cec5SDimitry Andric case llvm::Triple::x86: 82260b57cec5SDimitry Andric case llvm::Triple::x86_64: 82270b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 82280b57cec5SDimitry Andric break; 82290b57cec5SDimitry Andric 82300b57cec5SDimitry Andric case llvm::Triple::arm: 82310b57cec5SDimitry Andric case llvm::Triple::armeb: 82320b57cec5SDimitry Andric case llvm::Triple::thumb: 82330b57cec5SDimitry Andric case llvm::Triple::thumbeb: 82340b57cec5SDimitry Andric // This isn't in AddARMTargetArgs because we want to do this for assembly 82350b57cec5SDimitry Andric // only, not C/C++. 82360b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 82370b57cec5SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 82380b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 82390b57cec5SDimitry Andric CmdArgs.push_back("-arm-add-build-attributes"); 82400b57cec5SDimitry Andric } 82410b57cec5SDimitry Andric break; 82420b57cec5SDimitry Andric 8243e8d8bef9SDimitry Andric case llvm::Triple::aarch64: 8244e8d8bef9SDimitry Andric case llvm::Triple::aarch64_32: 8245e8d8bef9SDimitry Andric case llvm::Triple::aarch64_be: 8246e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_mmark_bti_property)) { 8247e8d8bef9SDimitry Andric CmdArgs.push_back("-mllvm"); 8248e8d8bef9SDimitry Andric CmdArgs.push_back("-aarch64-mark-bti-property"); 8249e8d8bef9SDimitry Andric } 8250e8d8bef9SDimitry Andric break; 8251e8d8bef9SDimitry Andric 825206c3fb27SDimitry Andric case llvm::Triple::loongarch32: 825306c3fb27SDimitry Andric case llvm::Triple::loongarch64: 825406c3fb27SDimitry Andric AddLoongArchTargetArgs(Args, CmdArgs); 825506c3fb27SDimitry Andric break; 825606c3fb27SDimitry Andric 82570b57cec5SDimitry Andric case llvm::Triple::riscv32: 82580b57cec5SDimitry Andric case llvm::Triple::riscv64: 82590b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 82600b57cec5SDimitry Andric break; 82610b57cec5SDimitry Andric } 82620b57cec5SDimitry Andric 82630b57cec5SDimitry Andric // Consume all the warning flags. Usually this would be handled more 82640b57cec5SDimitry Andric // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as 82650b57cec5SDimitry Andric // doesn't handle that so rather than warning about unused flags that are 82660b57cec5SDimitry Andric // actually used, we'll lie by omission instead. 82670b57cec5SDimitry Andric // FIXME: Stop lying and consume only the appropriate driver flags 82680b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_W_Group); 82690b57cec5SDimitry Andric 82700b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 82710b57cec5SDimitry Andric getToolChain().getDriver()); 82720b57cec5SDimitry Andric 82730b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 82740b57cec5SDimitry Andric 827506c3fb27SDimitry Andric if (DebugInfoKind > llvm::codegenoptions::NoDebugInfo && Output.isFilename()) 82760eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 82770eae32dcSDimitry Andric Output.getFilename()); 82780eae32dcSDimitry Andric 82790eae32dcSDimitry Andric // Fixup any previous commands that use -object-file-name because when we 82800eae32dcSDimitry Andric // generated them, the final .obj name wasn't yet known. 82810eae32dcSDimitry Andric for (Command &J : C.getJobs()) { 82820eae32dcSDimitry Andric if (SourceAction != FindSource(&J.getSource())) 82830eae32dcSDimitry Andric continue; 82840eae32dcSDimitry Andric auto &JArgs = J.getArguments(); 82850eae32dcSDimitry Andric for (unsigned I = 0; I < JArgs.size(); ++I) { 82860eae32dcSDimitry Andric if (StringRef(JArgs[I]).startswith("-object-file-name=") && 82870eae32dcSDimitry Andric Output.isFilename()) { 82880eae32dcSDimitry Andric ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); 82890eae32dcSDimitry Andric addDebugObjectName(Args, NewArgs, DebugCompilationDir, 82900eae32dcSDimitry Andric Output.getFilename()); 82910eae32dcSDimitry Andric NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); 82920eae32dcSDimitry Andric J.replaceArguments(NewArgs); 82930eae32dcSDimitry Andric break; 82940eae32dcSDimitry Andric } 82950eae32dcSDimitry Andric } 82960eae32dcSDimitry Andric } 82970eae32dcSDimitry Andric 82980b57cec5SDimitry Andric assert(Output.isFilename() && "Unexpected lipo output."); 82990b57cec5SDimitry Andric CmdArgs.push_back("-o"); 83000b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 83010b57cec5SDimitry Andric 83020b57cec5SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 83030b57cec5SDimitry Andric Arg *A; 83040b57cec5SDimitry Andric if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split && 83050b57cec5SDimitry Andric T.isOSBinFormatELF()) { 83060b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 8307e8d8bef9SDimitry Andric CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output)); 83080b57cec5SDimitry Andric } 83090b57cec5SDimitry Andric 8310e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) 831181ad6265SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true); 8312e8d8bef9SDimitry Andric 83130b57cec5SDimitry Andric assert(Input.isFilename() && "Invalid input."); 83140b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 83150b57cec5SDimitry Andric 83160b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 8317e8d8bef9SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 8318e8d8bef9SDimitry Andric // Invoke cc1as directly in this process. 831906c3fb27SDimitry Andric C.addCommand(std::make_unique<CC1Command>( 832006c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 832106c3fb27SDimitry Andric Output, D.getPrependArg())); 8322e8d8bef9SDimitry Andric } else { 832306c3fb27SDimitry Andric C.addCommand(std::make_unique<Command>( 832406c3fb27SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs, 832506c3fb27SDimitry Andric Output, D.getPrependArg())); 8326e8d8bef9SDimitry Andric } 83270b57cec5SDimitry Andric } 83280b57cec5SDimitry Andric 83290b57cec5SDimitry Andric // Begin OffloadBundler 83300b57cec5SDimitry Andric 83310b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, 83320b57cec5SDimitry Andric const InputInfo &Output, 83330b57cec5SDimitry Andric const InputInfoList &Inputs, 83340b57cec5SDimitry Andric const llvm::opt::ArgList &TCArgs, 83350b57cec5SDimitry Andric const char *LinkingOutput) const { 83360b57cec5SDimitry Andric // The version with only one output is expected to refer to a bundling job. 83370b57cec5SDimitry Andric assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!"); 83380b57cec5SDimitry Andric 83390b57cec5SDimitry Andric // The bundling command looks like this: 83400b57cec5SDimitry Andric // clang-offload-bundler -type=bc 83410b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 834281ad6265SDimitry Andric // -output=output_file 834381ad6265SDimitry Andric // -input=unbundle_file_host 834481ad6265SDimitry Andric // -input=unbundle_file_tgt1 834581ad6265SDimitry Andric // -input=unbundle_file_tgt2 83460b57cec5SDimitry Andric 83470b57cec5SDimitry Andric ArgStringList CmdArgs; 83480b57cec5SDimitry Andric 83490b57cec5SDimitry Andric // Get the type. 83500b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 83510b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Output.getType()))); 83520b57cec5SDimitry Andric 83530b57cec5SDimitry Andric assert(JA.getInputs().size() == Inputs.size() && 83540b57cec5SDimitry Andric "Not have inputs for all dependence actions??"); 83550b57cec5SDimitry Andric 83560b57cec5SDimitry Andric // Get the targets. 83570b57cec5SDimitry Andric SmallString<128> Triples; 83580b57cec5SDimitry Andric Triples += "-targets="; 83590b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 83600b57cec5SDimitry Andric if (I) 83610b57cec5SDimitry Andric Triples += ','; 83620b57cec5SDimitry Andric 83630b57cec5SDimitry Andric // Find ToolChain for this input. 83640b57cec5SDimitry Andric Action::OffloadKind CurKind = Action::OFK_Host; 83650b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 83660b57cec5SDimitry Andric const Action *CurDep = JA.getInputs()[I]; 83670b57cec5SDimitry Andric 83680b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) { 83690b57cec5SDimitry Andric CurTC = nullptr; 83700b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) { 83710b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 83720b57cec5SDimitry Andric CurKind = A->getOffloadingDeviceKind(); 83730b57cec5SDimitry Andric CurTC = TC; 83740b57cec5SDimitry Andric }); 83750b57cec5SDimitry Andric } 83760b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(CurKind); 8377349cc55cSDimitry Andric Triples += '-'; 8378349cc55cSDimitry Andric Triples += CurTC->getTriple().normalize(); 8379349cc55cSDimitry Andric if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) && 83800eae32dcSDimitry Andric !StringRef(CurDep->getOffloadingArch()).empty()) { 8381349cc55cSDimitry Andric Triples += '-'; 83820b57cec5SDimitry Andric Triples += CurDep->getOffloadingArch(); 83830b57cec5SDimitry Andric } 8384349cc55cSDimitry Andric 8385349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8386349cc55cSDimitry Andric // with each toolchain. 8387349cc55cSDimitry Andric StringRef GPUArchName; 8388349cc55cSDimitry Andric if (CurKind == Action::OFK_OpenMP) { 8389349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8390349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8391349cc55cSDimitry Andric auto ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 839206c3fb27SDimitry Andric auto Arch = ArchStr.starts_with_insensitive("-march="); 8393349cc55cSDimitry Andric if (Arch) { 8394349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8395349cc55cSDimitry Andric Triples += "-"; 8396349cc55cSDimitry Andric break; 8397349cc55cSDimitry Andric } 8398349cc55cSDimitry Andric } 8399349cc55cSDimitry Andric Triples += GPUArchName.str(); 8400349cc55cSDimitry Andric } 84010b57cec5SDimitry Andric } 84020b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 84030b57cec5SDimitry Andric 84040b57cec5SDimitry Andric // Get bundled file command. 84050b57cec5SDimitry Andric CmdArgs.push_back( 840681ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-output=") + Output.getFilename())); 84070b57cec5SDimitry Andric 84080b57cec5SDimitry Andric // Get unbundled files command. 84090b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 841081ad6265SDimitry Andric SmallString<128> UB; 841181ad6265SDimitry Andric UB += "-input="; 84120b57cec5SDimitry Andric 84130b57cec5SDimitry Andric // Find ToolChain for this input. 84140b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 84150b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) { 84160b57cec5SDimitry Andric CurTC = nullptr; 84170b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) { 84180b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 84190b57cec5SDimitry Andric CurTC = TC; 84200b57cec5SDimitry Andric }); 84216e75b2fbSDimitry Andric UB += C.addTempFile( 84226e75b2fbSDimitry Andric C.getArgs().MakeArgString(CurTC->getInputFilename(Inputs[I]))); 84236e75b2fbSDimitry Andric } else { 84240b57cec5SDimitry Andric UB += CurTC->getInputFilename(Inputs[I]); 84250b57cec5SDimitry Andric } 84260b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 842781ad6265SDimitry Andric } 84280b57cec5SDimitry Andric // All the inputs are encoded as commands. 8429a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 84305ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 84310b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8432bdd1243dSDimitry Andric CmdArgs, std::nullopt, Output)); 84330b57cec5SDimitry Andric } 84340b57cec5SDimitry Andric 84350b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs( 84360b57cec5SDimitry Andric Compilation &C, const JobAction &JA, const InputInfoList &Outputs, 84370b57cec5SDimitry Andric const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, 84380b57cec5SDimitry Andric const char *LinkingOutput) const { 84390b57cec5SDimitry Andric // The version with multiple outputs is expected to refer to a unbundling job. 84400b57cec5SDimitry Andric auto &UA = cast<OffloadUnbundlingJobAction>(JA); 84410b57cec5SDimitry Andric 84420b57cec5SDimitry Andric // The unbundling command looks like this: 84430b57cec5SDimitry Andric // clang-offload-bundler -type=bc 84440b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 844581ad6265SDimitry Andric // -input=input_file 844681ad6265SDimitry Andric // -output=unbundle_file_host 844781ad6265SDimitry Andric // -output=unbundle_file_tgt1 844881ad6265SDimitry Andric // -output=unbundle_file_tgt2 84490b57cec5SDimitry Andric // -unbundle 84500b57cec5SDimitry Andric 84510b57cec5SDimitry Andric ArgStringList CmdArgs; 84520b57cec5SDimitry Andric 84530b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Expecting to unbundle a single file!"); 84540b57cec5SDimitry Andric InputInfo Input = Inputs.front(); 84550b57cec5SDimitry Andric 84560b57cec5SDimitry Andric // Get the type. 84570b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 84580b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Input.getType()))); 84590b57cec5SDimitry Andric 84600b57cec5SDimitry Andric // Get the targets. 84610b57cec5SDimitry Andric SmallString<128> Triples; 84620b57cec5SDimitry Andric Triples += "-targets="; 84630b57cec5SDimitry Andric auto DepInfo = UA.getDependentActionsInfo(); 84640b57cec5SDimitry Andric for (unsigned I = 0; I < DepInfo.size(); ++I) { 84650b57cec5SDimitry Andric if (I) 84660b57cec5SDimitry Andric Triples += ','; 84670b57cec5SDimitry Andric 84680b57cec5SDimitry Andric auto &Dep = DepInfo[I]; 84690b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind); 8470349cc55cSDimitry Andric Triples += '-'; 8471349cc55cSDimitry Andric Triples += Dep.DependentToolChain->getTriple().normalize(); 8472349cc55cSDimitry Andric if ((Dep.DependentOffloadKind == Action::OFK_HIP || 8473349cc55cSDimitry Andric Dep.DependentOffloadKind == Action::OFK_Cuda) && 8474349cc55cSDimitry Andric !Dep.DependentBoundArch.empty()) { 8475349cc55cSDimitry Andric Triples += '-'; 84760b57cec5SDimitry Andric Triples += Dep.DependentBoundArch; 84770b57cec5SDimitry Andric } 8478349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8479349cc55cSDimitry Andric // with each toolchain. 8480349cc55cSDimitry Andric StringRef GPUArchName; 8481349cc55cSDimitry Andric if (Dep.DependentOffloadKind == Action::OFK_OpenMP) { 8482349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8483349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8484349cc55cSDimitry Andric StringRef ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 848506c3fb27SDimitry Andric auto Arch = ArchStr.starts_with_insensitive("-march="); 8486349cc55cSDimitry Andric if (Arch) { 8487349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8488349cc55cSDimitry Andric Triples += "-"; 8489349cc55cSDimitry Andric break; 8490349cc55cSDimitry Andric } 8491349cc55cSDimitry Andric } 8492349cc55cSDimitry Andric Triples += GPUArchName.str(); 8493349cc55cSDimitry Andric } 84940b57cec5SDimitry Andric } 84950b57cec5SDimitry Andric 84960b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 84970b57cec5SDimitry Andric 84980b57cec5SDimitry Andric // Get bundled file command. 84990b57cec5SDimitry Andric CmdArgs.push_back( 850081ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-input=") + Input.getFilename())); 85010b57cec5SDimitry Andric 85020b57cec5SDimitry Andric // Get unbundled files command. 85030b57cec5SDimitry Andric for (unsigned I = 0; I < Outputs.size(); ++I) { 850481ad6265SDimitry Andric SmallString<128> UB; 850581ad6265SDimitry Andric UB += "-output="; 85060b57cec5SDimitry Andric UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]); 85070b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 850881ad6265SDimitry Andric } 85090b57cec5SDimitry Andric CmdArgs.push_back("-unbundle"); 8510e8d8bef9SDimitry Andric CmdArgs.push_back("-allow-missing-bundles"); 85110b57cec5SDimitry Andric 85120b57cec5SDimitry Andric // All the inputs are encoded as commands. 8513a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 85145ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 85150b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8516bdd1243dSDimitry Andric CmdArgs, std::nullopt, Outputs)); 8517a7dea167SDimitry Andric } 85181fd87a68SDimitry Andric 851981ad6265SDimitry Andric void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA, 852081ad6265SDimitry Andric const InputInfo &Output, 852181ad6265SDimitry Andric const InputInfoList &Inputs, 852281ad6265SDimitry Andric const llvm::opt::ArgList &Args, 852381ad6265SDimitry Andric const char *LinkingOutput) const { 852481ad6265SDimitry Andric ArgStringList CmdArgs; 852581ad6265SDimitry Andric 852681ad6265SDimitry Andric // Add the output file name. 852781ad6265SDimitry Andric assert(Output.isFilename() && "Invalid output."); 852881ad6265SDimitry Andric CmdArgs.push_back("-o"); 852981ad6265SDimitry Andric CmdArgs.push_back(Output.getFilename()); 853081ad6265SDimitry Andric 853181ad6265SDimitry Andric // Create the inputs to bundle the needed metadata. 853281ad6265SDimitry Andric for (const InputInfo &Input : Inputs) { 853381ad6265SDimitry Andric const Action *OffloadAction = Input.getAction(); 853481ad6265SDimitry Andric const ToolChain *TC = OffloadAction->getOffloadingToolChain(); 853581ad6265SDimitry Andric const ArgList &TCArgs = 853681ad6265SDimitry Andric C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(), 853781ad6265SDimitry Andric OffloadAction->getOffloadingDeviceKind()); 853881ad6265SDimitry Andric StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input)); 853906c3fb27SDimitry Andric StringRef Arch = OffloadAction->getOffloadingArch() 854081ad6265SDimitry Andric ? OffloadAction->getOffloadingArch() 854181ad6265SDimitry Andric : TCArgs.getLastArgValue(options::OPT_march_EQ); 854281ad6265SDimitry Andric StringRef Kind = 854381ad6265SDimitry Andric Action::GetOffloadKindName(OffloadAction->getOffloadingDeviceKind()); 854481ad6265SDimitry Andric 854581ad6265SDimitry Andric ArgStringList Features; 854681ad6265SDimitry Andric SmallVector<StringRef> FeatureArgs; 8547753f127fSDimitry Andric getTargetFeatures(TC->getDriver(), TC->getTriple(), TCArgs, Features, 8548753f127fSDimitry Andric false); 854981ad6265SDimitry Andric llvm::copy_if(Features, std::back_inserter(FeatureArgs), 855081ad6265SDimitry Andric [](StringRef Arg) { return !Arg.startswith("-target"); }); 855181ad6265SDimitry Andric 855206c3fb27SDimitry Andric if (TC->getTriple().isAMDGPU()) { 855306c3fb27SDimitry Andric for (StringRef Feature : llvm::split(Arch.split(':').second, ':')) { 855406c3fb27SDimitry Andric FeatureArgs.emplace_back( 855506c3fb27SDimitry Andric Args.MakeArgString(Feature.take_back() + Feature.drop_back())); 855606c3fb27SDimitry Andric } 855706c3fb27SDimitry Andric } 855806c3fb27SDimitry Andric 855906c3fb27SDimitry Andric // TODO: We need to pass in the full target-id and handle it properly in the 856006c3fb27SDimitry Andric // linker wrapper. 856181ad6265SDimitry Andric SmallVector<std::string> Parts{ 856281ad6265SDimitry Andric "file=" + File.str(), 856381ad6265SDimitry Andric "triple=" + TC->getTripleString(), 856406c3fb27SDimitry Andric "arch=" + getProcessorFromTargetID(TC->getTriple(), Arch).str(), 856581ad6265SDimitry Andric "kind=" + Kind.str(), 856681ad6265SDimitry Andric }; 856781ad6265SDimitry Andric 856806c3fb27SDimitry Andric if (TC->getDriver().isUsingLTO(/* IsOffload */ true) || 856906c3fb27SDimitry Andric TC->getTriple().isAMDGPU()) 857081ad6265SDimitry Andric for (StringRef Feature : FeatureArgs) 857181ad6265SDimitry Andric Parts.emplace_back("feature=" + Feature.str()); 857281ad6265SDimitry Andric 857381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("--image=" + llvm::join(Parts, ","))); 857481ad6265SDimitry Andric } 857581ad6265SDimitry Andric 857681ad6265SDimitry Andric C.addCommand(std::make_unique<Command>( 857781ad6265SDimitry Andric JA, *this, ResponseFileSupport::None(), 857881ad6265SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), 857981ad6265SDimitry Andric CmdArgs, Inputs, Output)); 858081ad6265SDimitry Andric } 858181ad6265SDimitry Andric 85821fd87a68SDimitry Andric void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, 85831fd87a68SDimitry Andric const InputInfo &Output, 85841fd87a68SDimitry Andric const InputInfoList &Inputs, 85851fd87a68SDimitry Andric const ArgList &Args, 85861fd87a68SDimitry Andric const char *LinkingOutput) const { 8587d56accc7SDimitry Andric const Driver &D = getToolChain().getDriver(); 8588d56accc7SDimitry Andric const llvm::Triple TheTriple = getToolChain().getTriple(); 85891fd87a68SDimitry Andric ArgStringList CmdArgs; 85901fd87a68SDimitry Andric 8591d56accc7SDimitry Andric // Pass the CUDA path to the linker wrapper tool. 859281ad6265SDimitry Andric for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP}) { 859381ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains(Kind); 859481ad6265SDimitry Andric for (auto &I : llvm::make_range(TCRange.first, TCRange.second)) { 8595d56accc7SDimitry Andric const ToolChain *TC = I.second; 8596d56accc7SDimitry Andric if (TC->getTriple().isNVPTX()) { 8597d56accc7SDimitry Andric CudaInstallationDetector CudaInstallation(D, TheTriple, Args); 8598d56accc7SDimitry Andric if (CudaInstallation.isValid()) 8599d56accc7SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8600d56accc7SDimitry Andric "--cuda-path=" + CudaInstallation.getInstallPath())); 8601d56accc7SDimitry Andric break; 8602d56accc7SDimitry Andric } 8603d56accc7SDimitry Andric } 86041fd87a68SDimitry Andric } 86051fd87a68SDimitry Andric 860681ad6265SDimitry Andric if (D.isUsingLTO(/* IsOffload */ true)) { 86071fd87a68SDimitry Andric // Pass in the optimization level to use for LTO. 86081fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 86091fd87a68SDimitry Andric StringRef OOpt; 86101fd87a68SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 86111fd87a68SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 86121fd87a68SDimitry Andric OOpt = "3"; 86131fd87a68SDimitry Andric else if (A->getOption().matches(options::OPT_O)) { 86141fd87a68SDimitry Andric OOpt = A->getValue(); 86151fd87a68SDimitry Andric if (OOpt == "g") 86161fd87a68SDimitry Andric OOpt = "1"; 86171fd87a68SDimitry Andric else if (OOpt == "s" || OOpt == "z") 86181fd87a68SDimitry Andric OOpt = "2"; 86191fd87a68SDimitry Andric } else if (A->getOption().matches(options::OPT_O0)) 86201fd87a68SDimitry Andric OOpt = "0"; 86211fd87a68SDimitry Andric if (!OOpt.empty()) 8622753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--opt-level=O") + OOpt)); 86231fd87a68SDimitry Andric } 86241fd87a68SDimitry Andric } 86251fd87a68SDimitry Andric 8626753f127fSDimitry Andric CmdArgs.push_back( 8627753f127fSDimitry Andric Args.MakeArgString("--host-triple=" + TheTriple.getTriple())); 86281fd87a68SDimitry Andric if (Args.hasArg(options::OPT_v)) 8629bdd1243dSDimitry Andric CmdArgs.push_back("--wrapper-verbose"); 86301fd87a68SDimitry Andric 86311fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 8632753f127fSDimitry Andric if (!A->getOption().matches(options::OPT_g0)) 8633753f127fSDimitry Andric CmdArgs.push_back("--device-debug"); 86341fd87a68SDimitry Andric } 86351fd87a68SDimitry Andric 86361fd87a68SDimitry Andric for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 8637bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("--ptxas-arg=" + A)); 86381fd87a68SDimitry Andric 86391fd87a68SDimitry Andric // Forward remarks passes to the LLVM backend in the wrapper. 86401fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) 8641fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--offload-opt=-pass-remarks=") + 8642fcaf7f86SDimitry Andric A->getValue())); 86431fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) 8644fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8645fcaf7f86SDimitry Andric Twine("--offload-opt=-pass-remarks-missed=") + A->getValue())); 86461fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) 8647fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8648fcaf7f86SDimitry Andric Twine("--offload-opt=-pass-remarks-analysis=") + A->getValue())); 86491fd87a68SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 8650753f127fSDimitry Andric CmdArgs.push_back("--save-temps"); 86511fd87a68SDimitry Andric 8652d56accc7SDimitry Andric // Construct the link job so we can wrap around it. 8653d56accc7SDimitry Andric Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); 8654d56accc7SDimitry Andric const auto &LinkCommand = C.getJobs().getJobs().back(); 8655d56accc7SDimitry Andric 865681ad6265SDimitry Andric // Forward -Xoffload-linker<-triple> arguments to the device link job. 8657753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_Xoffload_linker)) { 8658753f127fSDimitry Andric StringRef Val = A->getValue(0); 865981ad6265SDimitry Andric if (Val.empty()) 866081ad6265SDimitry Andric CmdArgs.push_back( 8661753f127fSDimitry Andric Args.MakeArgString(Twine("--device-linker=") + A->getValue(1))); 866281ad6265SDimitry Andric else 866381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8664753f127fSDimitry Andric "--device-linker=" + 866581ad6265SDimitry Andric ToolChain::getOpenMPTriple(Val.drop_front()).getTriple() + "=" + 8666753f127fSDimitry Andric A->getValue(1))); 866781ad6265SDimitry Andric } 866881ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_Xoffload_linker); 866981ad6265SDimitry Andric 8670bdd1243dSDimitry Andric // Embed bitcode instead of an object in JIT mode. 8671bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_jit, 8672bdd1243dSDimitry Andric options::OPT_fno_openmp_target_jit, false)) 8673bdd1243dSDimitry Andric CmdArgs.push_back("--embed-bitcode"); 8674bdd1243dSDimitry Andric 8675753f127fSDimitry Andric // Forward `-mllvm` arguments to the LLVM invocations if present. 8676753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_mllvm)) { 8677753f127fSDimitry Andric CmdArgs.push_back("-mllvm"); 8678753f127fSDimitry Andric CmdArgs.push_back(A->getValue()); 8679753f127fSDimitry Andric A->claim(); 8680753f127fSDimitry Andric } 8681753f127fSDimitry Andric 86821fd87a68SDimitry Andric // Add the linker arguments to be forwarded by the wrapper. 8683753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") + 8684753f127fSDimitry Andric LinkCommand->getExecutable())); 86851fd87a68SDimitry Andric CmdArgs.push_back("--"); 86861fd87a68SDimitry Andric for (const char *LinkArg : LinkCommand->getArguments()) 86871fd87a68SDimitry Andric CmdArgs.push_back(LinkArg); 86881fd87a68SDimitry Andric 86891fd87a68SDimitry Andric const char *Exec = 86901fd87a68SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper")); 86911fd87a68SDimitry Andric 86921fd87a68SDimitry Andric // Replace the executable and arguments of the link job with the 86931fd87a68SDimitry Andric // wrapper. 86941fd87a68SDimitry Andric LinkCommand->replaceExecutable(Exec); 86951fd87a68SDimitry Andric LinkCommand->replaceArguments(CmdArgs); 86961fd87a68SDimitry Andric } 8697