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" 48bdd1243dSDimitry Andric #include "llvm/Support/ARMTargetParserCommon.h" 490b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 505ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h" 510b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 520b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 53e8d8bef9SDimitry Andric #include "llvm/Support/Host.h" 540b57cec5SDimitry Andric #include "llvm/Support/Path.h" 550b57cec5SDimitry Andric #include "llvm/Support/Process.h" 560b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h" 57753f127fSDimitry Andric #include <cctype> 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric using namespace clang::driver; 600b57cec5SDimitry Andric using namespace clang::driver::tools; 610b57cec5SDimitry Andric using namespace clang; 620b57cec5SDimitry Andric using namespace llvm::opt; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { 65349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC, 66349cc55cSDimitry Andric options::OPT_fminimize_whitespace, 67349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace)) { 680b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) && 690b57cec5SDimitry Andric !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) { 700b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 710b57cec5SDimitry Andric << A->getBaseArg().getAsString(Args) 720b57cec5SDimitry Andric << (D.IsCLMode() ? "/E, /P or /EP" : "-E"); 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 780b57cec5SDimitry Andric // In gcc, only ARM checks this, but it seems reasonable to check universally. 790b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 800b57cec5SDimitry Andric if (const Arg *A = 810b57cec5SDimitry Andric Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic)) 820b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args) 830b57cec5SDimitry Andric << "-static"; 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes. 870b57cec5SDimitry Andric // This is used for the space-separated argument list specified with 880b57cec5SDimitry Andric // the -dwarf-debug-flags option. 890b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg, 900b57cec5SDimitry Andric SmallVectorImpl<char> &Res) { 910b57cec5SDimitry Andric for (; *Arg; ++Arg) { 920b57cec5SDimitry Andric switch (*Arg) { 930b57cec5SDimitry Andric default: 940b57cec5SDimitry Andric break; 950b57cec5SDimitry Andric case ' ': 960b57cec5SDimitry Andric case '\\': 970b57cec5SDimitry Andric Res.push_back('\\'); 980b57cec5SDimitry Andric break; 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric Res.push_back(*Arg); 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other 1050b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA. 1060b57cec5SDimitry Andric static void 1070b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA, 1080b57cec5SDimitry Andric const ToolChain &RegularToolChain, 1090b57cec5SDimitry Andric llvm::function_ref<void(const ToolChain &)> Work) { 1100b57cec5SDimitry Andric // Apply Work on the current/regular tool chain. 1110b57cec5SDimitry Andric Work(RegularToolChain); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric // Apply Work on all the offloading tool chains associated with the current 1140b57cec5SDimitry Andric // action. 1150b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_Cuda)) 1160b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 1170b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_Cuda)) 1180b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1190b57cec5SDimitry Andric else if (JA.isHostOffloading(Action::OFK_HIP)) 1200b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>()); 1210b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_HIP)) 1220b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 1250b57cec5SDimitry Andric auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>(); 1260b57cec5SDimitry Andric for (auto II = TCs.first, IE = TCs.second; II != IE; ++II) 1270b57cec5SDimitry Andric Work(*II->second); 1280b57cec5SDimitry Andric } else if (JA.isDeviceOffloading(Action::OFK_OpenMP)) 1290b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // 1320b57cec5SDimitry Andric // TODO: Add support for other offloading programming models here. 1330b57cec5SDimitry Andric // 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step 1370b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error 1380b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position 1390b57cec5SDimitry Andric /// of the refinement step in the input string. 1400b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D, 1410b57cec5SDimitry Andric const Arg &A, size_t &Position) { 1420b57cec5SDimitry Andric const char RefinementStepToken = ':'; 1430b57cec5SDimitry Andric Position = In.find(RefinementStepToken); 1440b57cec5SDimitry Andric if (Position != StringRef::npos) { 1450b57cec5SDimitry Andric StringRef Option = A.getOption().getName(); 1460b57cec5SDimitry Andric StringRef RefStep = In.substr(Position + 1); 1470b57cec5SDimitry Andric // Allow exactly one numeric character for the additional refinement 1480b57cec5SDimitry Andric // step parameter. This is reasonable for all currently-supported 1490b57cec5SDimitry Andric // operations and architectures because we would expect that a larger value 1500b57cec5SDimitry Andric // of refinement steps would cause the estimate "optimization" to 1510b57cec5SDimitry Andric // under-perform the native operation. Also, if the estimate does not 1520b57cec5SDimitry Andric // converge quickly, it probably will not ever converge, so further 1530b57cec5SDimitry Andric // refinement steps will not produce a better answer. 1540b57cec5SDimitry Andric if (RefStep.size() != 1) { 1550b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1560b57cec5SDimitry Andric return false; 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric char RefStepChar = RefStep[0]; 1590b57cec5SDimitry Andric if (RefStepChar < '0' || RefStepChar > '9') { 1600b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1610b57cec5SDimitry Andric return false; 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric return true; 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters. 1680b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args, 1690b57cec5SDimitry Andric ArgStringList &OutStrings) { 1700b57cec5SDimitry Andric StringRef DisabledPrefixIn = "!"; 1710b57cec5SDimitry Andric StringRef DisabledPrefixOut = "!"; 1720b57cec5SDimitry Andric StringRef EnabledPrefixOut = ""; 1730b57cec5SDimitry Andric StringRef Out = "-mrecip="; 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ); 1760b57cec5SDimitry Andric if (!A) 1770b57cec5SDimitry Andric return; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric unsigned NumOptions = A->getNumValues(); 1800b57cec5SDimitry Andric if (NumOptions == 0) { 1810b57cec5SDimitry Andric // No option is the same as "all". 1820b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + "all")); 1830b57cec5SDimitry Andric return; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // Pass through "all", "none", or "default" with an optional refinement step. 1870b57cec5SDimitry Andric if (NumOptions == 1) { 1880b57cec5SDimitry Andric StringRef Val = A->getValue(0); 1890b57cec5SDimitry Andric size_t RefStepLoc; 1900b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStepLoc)) 1910b57cec5SDimitry Andric return; 1920b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStepLoc); 1930b57cec5SDimitry Andric if (ValBase == "all" || ValBase == "none" || ValBase == "default") { 1940b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + Val)); 1950b57cec5SDimitry Andric return; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // Each reciprocal type may be enabled or disabled individually. 2000b57cec5SDimitry Andric // Check each input value for validity, concatenate them all back together, 2010b57cec5SDimitry Andric // and pass through. 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric llvm::StringMap<bool> OptionStrings; 2040b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divd", false)); 2050b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divf", false)); 20681ad6265SDimitry Andric OptionStrings.insert(std::make_pair("divh", false)); 2070b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divd", false)); 2080b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divf", false)); 20981ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-divh", false)); 2100b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtd", false)); 2110b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtf", false)); 21281ad6265SDimitry Andric OptionStrings.insert(std::make_pair("sqrth", false)); 2130b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtd", false)); 2140b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtf", false)); 21581ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrth", false)); 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric for (unsigned i = 0; i != NumOptions; ++i) { 2180b57cec5SDimitry Andric StringRef Val = A->getValue(i); 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric bool IsDisabled = Val.startswith(DisabledPrefixIn); 2210b57cec5SDimitry Andric // Ignore the disablement token for string matching. 2220b57cec5SDimitry Andric if (IsDisabled) 2230b57cec5SDimitry Andric Val = Val.substr(1); 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric size_t RefStep; 2260b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStep)) 2270b57cec5SDimitry Andric return; 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStep); 2300b57cec5SDimitry Andric llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase); 2310b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2320b57cec5SDimitry Andric // Try again specifying float suffix. 2330b57cec5SDimitry Andric OptionIter = OptionStrings.find(ValBase.str() + 'f'); 2340b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2350b57cec5SDimitry Andric // The input name did not match any known option string. 2360b57cec5SDimitry Andric D.Diag(diag::err_drv_unknown_argument) << Val; 2370b57cec5SDimitry Andric return; 2380b57cec5SDimitry Andric } 23981ad6265SDimitry Andric // The option was specified without a half or float or double suffix. 24081ad6265SDimitry Andric // Make sure that the double or half entry was not already specified. 2410b57cec5SDimitry Andric // The float entry will be checked below. 24281ad6265SDimitry Andric if (OptionStrings[ValBase.str() + 'd'] || 24381ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h']) { 2440b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2450b57cec5SDimitry Andric return; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric if (OptionIter->second == true) { 2500b57cec5SDimitry Andric // Duplicate option specified. 2510b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2520b57cec5SDimitry Andric return; 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric // Mark the matched option as found. Do not allow duplicate specifiers. 2560b57cec5SDimitry Andric OptionIter->second = true; 2570b57cec5SDimitry Andric 25881ad6265SDimitry Andric // If the precision was not specified, also mark the double and half entry 25981ad6265SDimitry Andric // as found. 26081ad6265SDimitry Andric if (ValBase.back() != 'f' && ValBase.back() != 'd' && ValBase.back() != 'h') { 2610b57cec5SDimitry Andric OptionStrings[ValBase.str() + 'd'] = true; 26281ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h'] = true; 26381ad6265SDimitry Andric } 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric // Build the output string. 2660b57cec5SDimitry Andric StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut; 2670b57cec5SDimitry Andric Out = Args.MakeArgString(Out + Prefix + Val); 2680b57cec5SDimitry Andric if (i != NumOptions - 1) 2690b57cec5SDimitry Andric Out = Args.MakeArgString(Out + ","); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out)); 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer 2760b57cec5SDimitry Andric /// or the string "none". 2770b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args, 2780b57cec5SDimitry Andric ArgStringList &CmdArgs) { 2790b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ); 2800b57cec5SDimitry Andric if (!A) 2810b57cec5SDimitry Andric return; 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric StringRef Value = A->getValue(); 2840b57cec5SDimitry Andric if (Value == "none") { 2850b57cec5SDimitry Andric CmdArgs.push_back("-mprefer-vector-width=none"); 2860b57cec5SDimitry Andric } else { 2870b57cec5SDimitry Andric unsigned Width; 2880b57cec5SDimitry Andric if (Value.getAsInteger(10, Width)) { 2890b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 2900b57cec5SDimitry Andric return; 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value)); 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric static bool 2970b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 2980b57cec5SDimitry Andric const llvm::Triple &Triple) { 2990b57cec5SDimitry Andric // We use the zero-cost exception tables for Objective-C if the non-fragile 3000b57cec5SDimitry Andric // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 3010b57cec5SDimitry Andric // later. 3020b57cec5SDimitry Andric if (runtime.isNonFragile()) 3030b57cec5SDimitry Andric return true; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric if (!Triple.isMacOSX()) 3060b57cec5SDimitry Andric return false; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric return (!Triple.isMacOSXVersionLT(10, 5) && 3090b57cec5SDimitry Andric (Triple.getArch() == llvm::Triple::x86_64 || 3100b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::arm)); 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a 3144824e7fdSDimitry Andric /// main flag, -fexceptions and also language specific flags to enable/disable 3150b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example 3160b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions. 317fe6060f1SDimitry Andric static bool addExceptionArgs(const ArgList &Args, types::ID InputType, 3180b57cec5SDimitry Andric const ToolChain &TC, bool KernelOrKext, 3190b57cec5SDimitry Andric const ObjCRuntime &objcRuntime, 3200b57cec5SDimitry Andric ArgStringList &CmdArgs) { 3210b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric if (KernelOrKext) { 3240b57cec5SDimitry Andric // -mkernel and -fapple-kext imply no exceptions, so claim exception related 3250b57cec5SDimitry Andric // arguments now to avoid warnings about unused arguments. 3260b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fexceptions); 3270b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_exceptions); 3280b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 3290b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 3300b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 3310b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 332fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fasync_exceptions); 333fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_async_exceptions); 334fe6060f1SDimitry Andric return false; 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric // See if the user explicitly enabled exceptions. 3380b57cec5SDimitry Andric bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, 3390b57cec5SDimitry Andric false); 3400b57cec5SDimitry Andric 341fe6060f1SDimitry Andric bool EHa = Args.hasFlag(options::OPT_fasync_exceptions, 342fe6060f1SDimitry Andric options::OPT_fno_async_exceptions, false); 343fe6060f1SDimitry Andric if (EHa) { 344fe6060f1SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 345fe6060f1SDimitry Andric EH = true; 346fe6060f1SDimitry Andric } 347fe6060f1SDimitry Andric 3480b57cec5SDimitry Andric // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 3490b57cec5SDimitry Andric // is not necessarily sensible, but follows GCC. 3500b57cec5SDimitry Andric if (types::isObjC(InputType) && 3510b57cec5SDimitry Andric Args.hasFlag(options::OPT_fobjc_exceptions, 3520b57cec5SDimitry Andric options::OPT_fno_objc_exceptions, true)) { 3530b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-exceptions"); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric if (types::isCXX(InputType)) { 35981ad6265SDimitry Andric // Disable C++ EH by default on XCore and PS4/PS5. 36081ad6265SDimitry Andric bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore && 36181ad6265SDimitry Andric !Triple.isPS() && !Triple.isDriverKit(); 3620b57cec5SDimitry Andric Arg *ExceptionArg = Args.getLastArg( 3630b57cec5SDimitry Andric options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 3640b57cec5SDimitry Andric options::OPT_fexceptions, options::OPT_fno_exceptions); 3650b57cec5SDimitry Andric if (ExceptionArg) 3660b57cec5SDimitry Andric CXXExceptionsEnabled = 3670b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) || 3680b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fexceptions); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric if (CXXExceptionsEnabled) { 3710b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric EH = true; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3775ffd83dbSDimitry Andric // OPT_fignore_exceptions means exception could still be thrown, 3785ffd83dbSDimitry Andric // but no clean up or catch would happen in current module. 3795ffd83dbSDimitry Andric // So we do not set EH to false. 3805ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions); 3815ffd83dbSDimitry Andric 3820b57cec5SDimitry Andric if (EH) 3830b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 384fe6060f1SDimitry Andric return EH; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric 387480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, 388480093f4SDimitry Andric const JobAction &JA) { 3890b57cec5SDimitry Andric bool Default = true; 3900b57cec5SDimitry Andric if (TC.getTriple().isOSDarwin()) { 3910b57cec5SDimitry Andric // The native darwin assembler doesn't support the linker_option directives, 3920b57cec5SDimitry Andric // so we disable them if we think the .s file will be passed to it. 3930b57cec5SDimitry Andric Default = TC.useIntegratedAs(); 3940b57cec5SDimitry Andric } 395480093f4SDimitry Andric // The linker_option directives are intended for host compilation. 396480093f4SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 397480093f4SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 398480093f4SDimitry Andric Default = false; 399480093f4SDimitry Andric return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 4000b57cec5SDimitry Andric Default); 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases 4040b57cec5SDimitry Andric // to the corresponding DebugInfoKind. 4050b57cec5SDimitry Andric static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) { 4060b57cec5SDimitry Andric assert(A.getOption().matches(options::OPT_gN_Group) && 4070b57cec5SDimitry Andric "Not a -g option that specifies a debug-info level"); 4080b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_g0) || 4090b57cec5SDimitry Andric A.getOption().matches(options::OPT_ggdb0)) 4100b57cec5SDimitry Andric return codegenoptions::NoDebugInfo; 4110b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_gline_tables_only) || 4120b57cec5SDimitry Andric A.getOption().matches(options::OPT_ggdb1)) 4130b57cec5SDimitry Andric return codegenoptions::DebugLineTablesOnly; 4140b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_gline_directives_only)) 4150b57cec5SDimitry Andric return codegenoptions::DebugDirectivesOnly; 416fe6060f1SDimitry Andric return codegenoptions::DebugInfoConstructor; 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) { 4200b57cec5SDimitry Andric switch (Triple.getArch()){ 4210b57cec5SDimitry Andric default: 4220b57cec5SDimitry Andric return false; 4230b57cec5SDimitry Andric case llvm::Triple::arm: 4240b57cec5SDimitry Andric case llvm::Triple::thumb: 4250b57cec5SDimitry Andric // ARM Darwin targets require a frame pointer to be always present to aid 4260b57cec5SDimitry Andric // offline debugging via backtraces. 4270b57cec5SDimitry Andric return Triple.isOSDarwin(); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric static bool useFramePointerForTargetByDefault(const ArgList &Args, 4320b57cec5SDimitry Andric const llvm::Triple &Triple) { 4335ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry)) 434a7dea167SDimitry Andric return true; 435a7dea167SDimitry Andric 4360b57cec5SDimitry Andric switch (Triple.getArch()) { 4370b57cec5SDimitry Andric case llvm::Triple::xcore: 4380b57cec5SDimitry Andric case llvm::Triple::wasm32: 4390b57cec5SDimitry Andric case llvm::Triple::wasm64: 4400b57cec5SDimitry Andric case llvm::Triple::msp430: 4410b57cec5SDimitry Andric // XCore never wants frame pointers, regardless of OS. 4420b57cec5SDimitry Andric // WebAssembly never wants frame pointers. 4430b57cec5SDimitry Andric return false; 4440b57cec5SDimitry Andric case llvm::Triple::ppc: 445e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 4460b57cec5SDimitry Andric case llvm::Triple::ppc64: 4470b57cec5SDimitry Andric case llvm::Triple::ppc64le: 4480b57cec5SDimitry Andric case llvm::Triple::riscv32: 4490b57cec5SDimitry Andric case llvm::Triple::riscv64: 450bdd1243dSDimitry Andric case llvm::Triple::sparc: 451bdd1243dSDimitry Andric case llvm::Triple::sparcel: 452bdd1243dSDimitry Andric case llvm::Triple::sparcv9: 453480093f4SDimitry Andric case llvm::Triple::amdgcn: 454480093f4SDimitry Andric case llvm::Triple::r600: 45581ad6265SDimitry Andric case llvm::Triple::csky: 456bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 457bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 4580b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4590b57cec5SDimitry Andric default: 4600b57cec5SDimitry Andric break; 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric 463753f127fSDimitry Andric if (Triple.isOSFuchsia() || Triple.isOSNetBSD()) { 4640b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI || 4680b57cec5SDimitry Andric Triple.isOSHurd()) { 4690b57cec5SDimitry Andric switch (Triple.getArch()) { 4700b57cec5SDimitry Andric // Don't use a frame pointer on linux if optimizing for certain targets. 4715ffd83dbSDimitry Andric case llvm::Triple::arm: 4725ffd83dbSDimitry Andric case llvm::Triple::armeb: 4735ffd83dbSDimitry Andric case llvm::Triple::thumb: 4745ffd83dbSDimitry Andric case llvm::Triple::thumbeb: 4755ffd83dbSDimitry Andric if (Triple.isAndroid()) 4765ffd83dbSDimitry Andric return true; 477bdd1243dSDimitry Andric [[fallthrough]]; 4780b57cec5SDimitry Andric case llvm::Triple::mips64: 4790b57cec5SDimitry Andric case llvm::Triple::mips64el: 4800b57cec5SDimitry Andric case llvm::Triple::mips: 4810b57cec5SDimitry Andric case llvm::Triple::mipsel: 4820b57cec5SDimitry Andric case llvm::Triple::systemz: 4830b57cec5SDimitry Andric case llvm::Triple::x86: 4840b57cec5SDimitry Andric case llvm::Triple::x86_64: 4850b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4860b57cec5SDimitry Andric default: 4870b57cec5SDimitry Andric return true; 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric if (Triple.isOSWindows()) { 4920b57cec5SDimitry Andric switch (Triple.getArch()) { 4930b57cec5SDimitry Andric case llvm::Triple::x86: 4940b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 4950b57cec5SDimitry Andric case llvm::Triple::x86_64: 4960b57cec5SDimitry Andric return Triple.isOSBinFormatMachO(); 4970b57cec5SDimitry Andric case llvm::Triple::arm: 4980b57cec5SDimitry Andric case llvm::Triple::thumb: 4990b57cec5SDimitry Andric // Windows on ARM builds with FPO disabled to aid fast stack walking 5000b57cec5SDimitry Andric return true; 5010b57cec5SDimitry Andric default: 5020b57cec5SDimitry Andric // All other supported Windows ISAs use xdata unwind information, so frame 5030b57cec5SDimitry Andric // pointers are not generally useful. 5040b57cec5SDimitry Andric return false; 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric return true; 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 511a7dea167SDimitry Andric static CodeGenOptions::FramePointerKind 512a7dea167SDimitry Andric getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) { 513a7dea167SDimitry Andric // We have 4 states: 514a7dea167SDimitry Andric // 515a7dea167SDimitry Andric // 00) leaf retained, non-leaf retained 516a7dea167SDimitry Andric // 01) leaf retained, non-leaf omitted (this is invalid) 517a7dea167SDimitry Andric // 10) leaf omitted, non-leaf retained 518a7dea167SDimitry Andric // (what -momit-leaf-frame-pointer was designed for) 519a7dea167SDimitry Andric // 11) leaf omitted, non-leaf omitted 520a7dea167SDimitry Andric // 521a7dea167SDimitry Andric // "omit" options taking precedence over "no-omit" options is the only way 522a7dea167SDimitry Andric // to make 3 valid states representable 523a7dea167SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer, 524a7dea167SDimitry Andric options::OPT_fno_omit_frame_pointer); 525a7dea167SDimitry Andric bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer); 526a7dea167SDimitry Andric bool NoOmitFP = 527a7dea167SDimitry Andric A && A->getOption().matches(options::OPT_fno_omit_frame_pointer); 52881ad6265SDimitry Andric bool OmitLeafFP = 52981ad6265SDimitry Andric Args.hasFlag(options::OPT_momit_leaf_frame_pointer, 530480093f4SDimitry Andric options::OPT_mno_omit_leaf_frame_pointer, 53181ad6265SDimitry Andric Triple.isAArch64() || Triple.isPS() || Triple.isVE()); 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 } 5790eae32dcSDimitry Andric CmdArgs.push_back( 5800eae32dcSDimitry Andric Args.MakeArgString(Twine("-object-file-name=") + ObjFileNameForDebug)); 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map. 58481ad6265SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC, 58581ad6265SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 58681ad6265SDimitry Andric auto AddOneArg = [&](StringRef Map, StringRef Name) { 587349cc55cSDimitry Andric if (!Map.contains('=')) 58881ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) << Map << Name; 5890b57cec5SDimitry Andric else 5900b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); 59181ad6265SDimitry Andric }; 59281ad6265SDimitry Andric 59381ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 59481ad6265SDimitry Andric options::OPT_fdebug_prefix_map_EQ)) { 59581ad6265SDimitry Andric AddOneArg(A->getValue(), A->getOption().getName()); 5960b57cec5SDimitry Andric A->claim(); 5970b57cec5SDimitry Andric } 59881ad6265SDimitry Andric std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping(); 59981ad6265SDimitry Andric if (GlobalRemapEntry.empty()) 60081ad6265SDimitry Andric return; 60181ad6265SDimitry Andric AddOneArg(GlobalRemapEntry, "environment"); 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 604480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map. 605480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, 606480093f4SDimitry Andric ArgStringList &CmdArgs) { 607480093f4SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 608480093f4SDimitry Andric options::OPT_fmacro_prefix_map_EQ)) { 609480093f4SDimitry Andric StringRef Map = A->getValue(); 610349cc55cSDimitry Andric if (!Map.contains('=')) 611480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 612480093f4SDimitry Andric << Map << A->getOption().getName(); 613480093f4SDimitry Andric else 614480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map)); 615480093f4SDimitry Andric A->claim(); 616480093f4SDimitry Andric } 617480093f4SDimitry Andric } 618480093f4SDimitry Andric 619e8d8bef9SDimitry Andric /// Add a CC1 and CC1AS option to specify the coverage file path prefix map. 620fe6060f1SDimitry Andric static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args, 621e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 622e8d8bef9SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 623fe6060f1SDimitry Andric options::OPT_fcoverage_prefix_map_EQ)) { 624e8d8bef9SDimitry Andric StringRef Map = A->getValue(); 625349cc55cSDimitry Andric if (!Map.contains('=')) 626e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 627e8d8bef9SDimitry Andric << Map << A->getOption().getName(); 628e8d8bef9SDimitry Andric else 629fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map)); 630e8d8bef9SDimitry Andric A->claim(); 631e8d8bef9SDimitry Andric } 632e8d8bef9SDimitry Andric } 633e8d8bef9SDimitry Andric 6340b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz. 6350b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is 6360b57cec5SDimitry Andric /// enabled. 6370b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) { 6380b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 6390b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 6400b57cec5SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 6410b57cec5SDimitry Andric return true; 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O0)) 6440b57cec5SDimitry Andric return false; 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric // Vectorize -Os. 6490b57cec5SDimitry Andric StringRef S(A->getValue()); 6500b57cec5SDimitry Andric if (S == "s") 6510b57cec5SDimitry Andric return true; 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric // Don't vectorize -Oz, unless it's the slp vectorizer. 6540b57cec5SDimitry Andric if (S == "z") 6550b57cec5SDimitry Andric return isSlpVec; 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric unsigned OptLevel = 0; 6580b57cec5SDimitry Andric if (S.getAsInteger(10, OptLevel)) 6590b57cec5SDimitry Andric return false; 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric return OptLevel > 1; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric return false; 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input. 6680b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 6690b57cec5SDimitry Andric ArgStringList &CmdArgs) { 6700b57cec5SDimitry Andric // When using -verify-pch, we don't want to provide the type 6710b57cec5SDimitry Andric // 'precompiled-header' if it was inferred from the file extension 6720b57cec5SDimitry Andric if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH) 6730b57cec5SDimitry Andric return; 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric CmdArgs.push_back("-x"); 6760b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc)) 6770b57cec5SDimitry Andric CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 6780b57cec5SDimitry Andric else { 6790b57cec5SDimitry Andric // Map the driver type to the frontend type. This is mostly an identity 6800b57cec5SDimitry Andric // mapping, except that the distinction between module interface units 6810b57cec5SDimitry Andric // and other source files does not exist at the frontend layer. 6820b57cec5SDimitry Andric const char *ClangType; 6830b57cec5SDimitry Andric switch (Input.getType()) { 6840b57cec5SDimitry Andric case types::TY_CXXModule: 6850b57cec5SDimitry Andric ClangType = "c++"; 6860b57cec5SDimitry Andric break; 6870b57cec5SDimitry Andric case types::TY_PP_CXXModule: 6880b57cec5SDimitry Andric ClangType = "c++-cpp-output"; 6890b57cec5SDimitry Andric break; 6900b57cec5SDimitry Andric default: 6910b57cec5SDimitry Andric ClangType = types::getTypeName(Input.getType()); 6920b57cec5SDimitry Andric break; 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric CmdArgs.push_back(ClangType); 6950b57cec5SDimitry Andric } 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, 6990b57cec5SDimitry Andric const Driver &D, const InputInfo &Output, 700349cc55cSDimitry Andric const ArgList &Args, SanitizerArgs &SanArgs, 7010b57cec5SDimitry Andric ArgStringList &CmdArgs) { 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, 7040b57cec5SDimitry Andric options::OPT_fprofile_generate_EQ, 7050b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7060b57cec5SDimitry Andric if (PGOGenerateArg && 7070b57cec5SDimitry Andric PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7080b57cec5SDimitry Andric PGOGenerateArg = nullptr; 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate, 7110b57cec5SDimitry Andric options::OPT_fcs_profile_generate_EQ, 7120b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7130b57cec5SDimitry Andric if (CSPGOGenerateArg && 7140b57cec5SDimitry Andric CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7150b57cec5SDimitry Andric CSPGOGenerateArg = nullptr; 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric auto *ProfileGenerateArg = Args.getLastArg( 7180b57cec5SDimitry Andric options::OPT_fprofile_instr_generate, 7190b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ, 7200b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate); 7210b57cec5SDimitry Andric if (ProfileGenerateArg && 7220b57cec5SDimitry Andric ProfileGenerateArg->getOption().matches( 7230b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate)) 7240b57cec5SDimitry Andric ProfileGenerateArg = nullptr; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric if (PGOGenerateArg && ProfileGenerateArg) 7270b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7280b57cec5SDimitry Andric << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling(); 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric auto *ProfileUseArg = getLastProfileUseArg(Args); 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric if (PGOGenerateArg && ProfileUseArg) 7330b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7340b57cec5SDimitry Andric << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling(); 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric if (ProfileGenerateArg && ProfileUseArg) 7370b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7380b57cec5SDimitry Andric << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling(); 7390b57cec5SDimitry Andric 740e8d8bef9SDimitry Andric if (CSPGOGenerateArg && PGOGenerateArg) { 7410b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7420b57cec5SDimitry Andric << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling(); 743e8d8bef9SDimitry Andric PGOGenerateArg = nullptr; 744e8d8bef9SDimitry Andric } 7450b57cec5SDimitry Andric 746fe6060f1SDimitry Andric if (TC.getTriple().isOSAIX()) { 747fe6060f1SDimitry Andric if (ProfileGenerateArg) 748fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 749fe6060f1SDimitry Andric << ProfileGenerateArg->getSpelling() << TC.getTriple().str(); 750fe6060f1SDimitry Andric if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args)) 751fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 752fe6060f1SDimitry Andric << ProfileSampleUseArg->getSpelling() << TC.getTriple().str(); 753fe6060f1SDimitry Andric } 754fe6060f1SDimitry Andric 7550b57cec5SDimitry Andric if (ProfileGenerateArg) { 7560b57cec5SDimitry Andric if (ProfileGenerateArg->getOption().matches( 7570b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ)) 7580b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") + 7590b57cec5SDimitry Andric ProfileGenerateArg->getValue())); 7600b57cec5SDimitry Andric // The default is to use Clang Instrumentation. 7610b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=clang"); 7620b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 7630b57cec5SDimitry Andric // Add dependent lib for clang_rt.profile 7645ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 7655ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric Arg *PGOGenArg = nullptr; 7700b57cec5SDimitry Andric if (PGOGenerateArg) { 7710b57cec5SDimitry Andric assert(!CSPGOGenerateArg); 7720b57cec5SDimitry Andric PGOGenArg = PGOGenerateArg; 7730b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=llvm"); 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric if (CSPGOGenerateArg) { 7760b57cec5SDimitry Andric assert(!PGOGenerateArg); 7770b57cec5SDimitry Andric PGOGenArg = CSPGOGenerateArg; 7780b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=csllvm"); 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric if (PGOGenArg) { 7810b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 7825ffd83dbSDimitry Andric // Add dependent lib for clang_rt.profile 7835ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 7845ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 7850b57cec5SDimitry Andric } 7860b57cec5SDimitry Andric if (PGOGenArg->getOption().matches( 7870b57cec5SDimitry Andric PGOGenerateArg ? options::OPT_fprofile_generate_EQ 7880b57cec5SDimitry Andric : options::OPT_fcs_profile_generate_EQ)) { 7890b57cec5SDimitry Andric SmallString<128> Path(PGOGenArg->getValue()); 7900b57cec5SDimitry Andric llvm::sys::path::append(Path, "default_%m.profraw"); 7910b57cec5SDimitry Andric CmdArgs.push_back( 7920b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path)); 7930b57cec5SDimitry Andric } 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric if (ProfileUseArg) { 7970b57cec5SDimitry Andric if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ)) 7980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 7990b57cec5SDimitry Andric Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue())); 8000b57cec5SDimitry Andric else if ((ProfileUseArg->getOption().matches( 8010b57cec5SDimitry Andric options::OPT_fprofile_use_EQ) || 8020b57cec5SDimitry Andric ProfileUseArg->getOption().matches( 8030b57cec5SDimitry Andric options::OPT_fprofile_instr_use))) { 8040b57cec5SDimitry Andric SmallString<128> Path( 8050b57cec5SDimitry Andric ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); 8060b57cec5SDimitry Andric if (Path.empty() || llvm::sys::fs::is_directory(Path)) 8070b57cec5SDimitry Andric llvm::sys::path::append(Path, "default.profdata"); 8080b57cec5SDimitry Andric CmdArgs.push_back( 8090b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path)); 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric 8135ffd83dbSDimitry Andric bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage, 8145ffd83dbSDimitry Andric options::OPT_fno_test_coverage, false) || 815a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage); 8165ffd83dbSDimitry Andric bool EmitCovData = TC.needsGCovInstrumentation(Args); 817a7dea167SDimitry Andric if (EmitCovNotes) 818e8d8bef9SDimitry Andric CmdArgs.push_back("-ftest-coverage"); 819a7dea167SDimitry Andric if (EmitCovData) 820e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-arcs"); 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoverage_mapping, 8230b57cec5SDimitry Andric options::OPT_fno_coverage_mapping, false)) { 8240b57cec5SDimitry Andric if (!ProfileGenerateArg) 8250b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 8260b57cec5SDimitry Andric << "-fcoverage-mapping" 8270b57cec5SDimitry Andric << "-fprofile-instr-generate"; 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric CmdArgs.push_back("-fcoverage-mapping"); 8300b57cec5SDimitry Andric } 8310b57cec5SDimitry Andric 832fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 833fe6060f1SDimitry Andric options::OPT_fcoverage_compilation_dir_EQ)) { 834fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 835fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 836fe6060f1SDimitry Andric Twine("-fcoverage-compilation-dir=") + A->getValue())); 837fe6060f1SDimitry Andric else 838fe6060f1SDimitry Andric A->render(Args, CmdArgs); 839fe6060f1SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 840fe6060f1SDimitry Andric D.getVFS().getCurrentWorkingDirectory()) { 841fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD)); 842fe6060f1SDimitry Andric } 843fe6060f1SDimitry Andric 8440b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) { 8450b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ); 8460b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 8470b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 8480b57cec5SDimitry Andric << "-fprofile-exclude-files=" 8490b57cec5SDimitry Andric << "--coverage"; 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric StringRef v = Arg->getValue(); 8520b57cec5SDimitry Andric CmdArgs.push_back( 8530b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-exclude-files=" + v))); 8540b57cec5SDimitry Andric } 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) { 8570b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ); 8580b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 8590b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 8600b57cec5SDimitry Andric << "-fprofile-filter-files=" 8610b57cec5SDimitry Andric << "--coverage"; 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric StringRef v = Arg->getValue(); 8640b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v))); 8650b57cec5SDimitry Andric } 8660b57cec5SDimitry Andric 867e8d8bef9SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) { 868e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 869e8d8bef9SDimitry Andric if (Val == "atomic" || Val == "prefer-atomic") 870e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 871e8d8bef9SDimitry Andric else if (Val != "single") 872e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 873bdd1243dSDimitry Andric << A->getSpelling() << Val; 874349cc55cSDimitry Andric } else if (SanArgs.needsTsanRt()) { 875e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 876e8d8bef9SDimitry Andric } 877e8d8bef9SDimitry Andric 878fcaf7f86SDimitry Andric int FunctionGroups = 1; 879fcaf7f86SDimitry Andric int SelectedFunctionGroup = 0; 880fcaf7f86SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_function_groups)) { 881fcaf7f86SDimitry Andric StringRef Val = A->getValue(); 882fcaf7f86SDimitry Andric if (Val.getAsInteger(0, FunctionGroups) || FunctionGroups < 1) 883fcaf7f86SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; 884fcaf7f86SDimitry Andric } 885fcaf7f86SDimitry Andric if (const auto *A = 886fcaf7f86SDimitry Andric Args.getLastArg(options::OPT_fprofile_selected_function_group)) { 887fcaf7f86SDimitry Andric StringRef Val = A->getValue(); 888fcaf7f86SDimitry Andric if (Val.getAsInteger(0, SelectedFunctionGroup) || 889fcaf7f86SDimitry Andric SelectedFunctionGroup < 0 || SelectedFunctionGroup >= FunctionGroups) 890fcaf7f86SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) << A->getAsString(Args) << Val; 891fcaf7f86SDimitry Andric } 892fcaf7f86SDimitry Andric if (FunctionGroups != 1) 893fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-function-groups=" + 894fcaf7f86SDimitry Andric Twine(FunctionGroups))); 895fcaf7f86SDimitry Andric if (SelectedFunctionGroup != 0) 896fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-selected-function-group=" + 897fcaf7f86SDimitry Andric Twine(SelectedFunctionGroup))); 898fcaf7f86SDimitry Andric 899a7dea167SDimitry Andric // Leave -fprofile-dir= an unused argument unless .gcda emission is 900a7dea167SDimitry Andric // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider 901a7dea167SDimitry Andric // the flag used. There is no -fno-profile-dir, so the user has no 902a7dea167SDimitry Andric // targeted way to suppress the warning. 903a7dea167SDimitry Andric Arg *FProfileDir = nullptr; 904a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fprofile_arcs) || 905a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage)) 906a7dea167SDimitry Andric FProfileDir = Args.getLastArg(options::OPT_fprofile_dir); 907a7dea167SDimitry Andric 908a7dea167SDimitry Andric // Put the .gcno and .gcda files (if needed) next to the object file or 909a7dea167SDimitry Andric // bitcode file in the case of LTO. 910a7dea167SDimitry Andric // FIXME: There should be a simpler way to find the object file for this 911a7dea167SDimitry Andric // input, and this code probably does the wrong thing for commands that 912a7dea167SDimitry Andric // compile and link all at once. 913a7dea167SDimitry Andric if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) && 914a7dea167SDimitry Andric (EmitCovNotes || EmitCovData) && Output.isFilename()) { 9150b57cec5SDimitry Andric SmallString<128> OutputFilename; 916480093f4SDimitry Andric if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo)) 917480093f4SDimitry Andric OutputFilename = FinalOutput->getValue(); 918480093f4SDimitry Andric else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) 9190b57cec5SDimitry Andric OutputFilename = FinalOutput->getValue(); 9200b57cec5SDimitry Andric else 9210b57cec5SDimitry Andric OutputFilename = llvm::sys::path::filename(Output.getBaseInput()); 9220b57cec5SDimitry Andric SmallString<128> CoverageFilename = OutputFilename; 923a7dea167SDimitry Andric if (llvm::sys::path::is_relative(CoverageFilename)) 924a7dea167SDimitry Andric (void)D.getVFS().makeAbsolute(CoverageFilename); 9250b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcno"); 926a7dea167SDimitry Andric 927a7dea167SDimitry Andric CmdArgs.push_back("-coverage-notes-file"); 9280b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9290b57cec5SDimitry Andric 930a7dea167SDimitry Andric if (EmitCovData) { 931a7dea167SDimitry Andric if (FProfileDir) { 9320b57cec5SDimitry Andric CoverageFilename = FProfileDir->getValue(); 9330b57cec5SDimitry Andric llvm::sys::path::append(CoverageFilename, OutputFilename); 9340b57cec5SDimitry Andric } 9350b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcda"); 936a7dea167SDimitry Andric CmdArgs.push_back("-coverage-data-file"); 9370b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions. 9430b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) { 9440b57cec5SDimitry Andric if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A)) 9450b57cec5SDimitry Andric return true; 9460b57cec5SDimitry Andric 9470eae32dcSDimitry Andric return llvm::any_of(A->inputs(), ContainsCompileAction); 9480b57cec5SDimitry Andric } 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler. 9510b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0. 9520b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 9530b57cec5SDimitry Andric bool RelaxDefault = true; 9540b57cec5SDimitry Andric 9550b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 9560b57cec5SDimitry Andric RelaxDefault = A->getOption().matches(options::OPT_O0); 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric if (RelaxDefault) { 9590b57cec5SDimitry Andric RelaxDefault = false; 9600b57cec5SDimitry Andric for (const auto &Act : C.getActions()) { 9610b57cec5SDimitry Andric if (ContainsCompileAction(Act)) { 9620b57cec5SDimitry Andric RelaxDefault = true; 9630b57cec5SDimitry Andric break; 9640b57cec5SDimitry Andric } 9650b57cec5SDimitry Andric } 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric 9680b57cec5SDimitry Andric return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 9690b57cec5SDimitry Andric RelaxDefault); 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, 9730b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind, 9740b57cec5SDimitry Andric unsigned DwarfVersion, 9750b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning) { 9760b57cec5SDimitry Andric switch (DebugInfoKind) { 9770b57cec5SDimitry Andric case codegenoptions::DebugDirectivesOnly: 9780b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=line-directives-only"); 9790b57cec5SDimitry Andric break; 9800b57cec5SDimitry Andric case codegenoptions::DebugLineTablesOnly: 9810b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=line-tables-only"); 9820b57cec5SDimitry Andric break; 983480093f4SDimitry Andric case codegenoptions::DebugInfoConstructor: 984480093f4SDimitry Andric CmdArgs.push_back("-debug-info-kind=constructor"); 985480093f4SDimitry Andric break; 9860b57cec5SDimitry Andric case codegenoptions::LimitedDebugInfo: 9870b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=limited"); 9880b57cec5SDimitry Andric break; 9890b57cec5SDimitry Andric case codegenoptions::FullDebugInfo: 9900b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=standalone"); 9910b57cec5SDimitry Andric break; 992e8d8bef9SDimitry Andric case codegenoptions::UnusedTypeInfo: 993e8d8bef9SDimitry Andric CmdArgs.push_back("-debug-info-kind=unused-types"); 994e8d8bef9SDimitry Andric break; 9950b57cec5SDimitry Andric default: 9960b57cec5SDimitry Andric break; 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric if (DwarfVersion > 0) 9990b57cec5SDimitry Andric CmdArgs.push_back( 10000b57cec5SDimitry Andric Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); 10010b57cec5SDimitry Andric switch (DebuggerTuning) { 10020b57cec5SDimitry Andric case llvm::DebuggerKind::GDB: 10030b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=gdb"); 10040b57cec5SDimitry Andric break; 10050b57cec5SDimitry Andric case llvm::DebuggerKind::LLDB: 10060b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=lldb"); 10070b57cec5SDimitry Andric break; 10080b57cec5SDimitry Andric case llvm::DebuggerKind::SCE: 10090b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=sce"); 10100b57cec5SDimitry Andric break; 1011fe6060f1SDimitry Andric case llvm::DebuggerKind::DBX: 1012fe6060f1SDimitry Andric CmdArgs.push_back("-debugger-tuning=dbx"); 1013fe6060f1SDimitry Andric break; 10140b57cec5SDimitry Andric default: 10150b57cec5SDimitry Andric break; 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric } 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args, 10200b57cec5SDimitry Andric const Driver &D, const ToolChain &TC) { 10210b57cec5SDimitry Andric assert(A && "Expected non-nullptr argument."); 10220b57cec5SDimitry Andric if (TC.supportsDebugInfoOption(A)) 10230b57cec5SDimitry Andric return true; 10240b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target) 10250b57cec5SDimitry Andric << A->getAsString(Args) << TC.getTripleString(); 10260b57cec5SDimitry Andric return false; 10270b57cec5SDimitry Andric } 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args, 10300b57cec5SDimitry Andric ArgStringList &CmdArgs, 10310b57cec5SDimitry Andric const Driver &D, 10320b57cec5SDimitry Andric const ToolChain &TC) { 1033e8d8bef9SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gz_EQ); 10340b57cec5SDimitry Andric if (!A) 10350b57cec5SDimitry Andric return; 10360b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 10370b57cec5SDimitry Andric StringRef Value = A->getValue(); 10380b57cec5SDimitry Andric if (Value == "none") { 10390b57cec5SDimitry Andric CmdArgs.push_back("--compress-debug-sections=none"); 104004eeddc0SDimitry Andric } else if (Value == "zlib") { 1041753f127fSDimitry Andric if (llvm::compression::zlib::isAvailable()) { 10420b57cec5SDimitry Andric CmdArgs.push_back( 10430b57cec5SDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 10440b57cec5SDimitry Andric } else { 1045bdd1243dSDimitry Andric D.Diag(diag::warn_debug_compression_unavailable) << "zlib"; 1046bdd1243dSDimitry Andric } 1047bdd1243dSDimitry Andric } else if (Value == "zstd") { 1048bdd1243dSDimitry Andric if (llvm::compression::zstd::isAvailable()) { 1049bdd1243dSDimitry Andric CmdArgs.push_back( 1050bdd1243dSDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 1051bdd1243dSDimitry Andric } else { 1052bdd1243dSDimitry Andric D.Diag(diag::warn_debug_compression_unavailable) << "zstd"; 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric } else { 10550b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1056bdd1243dSDimitry Andric << A->getSpelling() << Value; 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric 1061e8d8bef9SDimitry Andric static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, 1062e8d8bef9SDimitry Andric const ArgList &Args, 106381ad6265SDimitry Andric ArgStringList &CmdArgs, 106481ad6265SDimitry Andric bool IsCC1As = false) { 1065fe6060f1SDimitry Andric // If no version was requested by the user, use the default value from the 1066fe6060f1SDimitry Andric // back end. This is consistent with the value returned from 1067fe6060f1SDimitry Andric // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without 1068fe6060f1SDimitry Andric // requiring the corresponding llvm to have the AMDGPU target enabled, 1069fe6060f1SDimitry Andric // provided the user (e.g. front end tests) can use the default. 1070fe6060f1SDimitry Andric if (haveAMDGPUCodeObjectVersionArgument(D, Args)) { 1071fe6060f1SDimitry Andric unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args); 1072e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 1073e8d8bef9SDimitry Andric Args.MakeArgString(Twine("--amdhsa-code-object-version=") + 1074e8d8bef9SDimitry Andric Twine(CodeObjVer))); 1075e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); 107681ad6265SDimitry Andric // -cc1as does not accept -mcode-object-version option. 107781ad6265SDimitry Andric if (!IsCC1As) 107881ad6265SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 107981ad6265SDimitry Andric Args.MakeArgString(Twine("-mcode-object-version=") + 108081ad6265SDimitry Andric Twine(CodeObjVer))); 1081e8d8bef9SDimitry Andric } 1082fe6060f1SDimitry Andric } 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, 10850b57cec5SDimitry Andric const Driver &D, const ArgList &Args, 10860b57cec5SDimitry Andric ArgStringList &CmdArgs, 10870b57cec5SDimitry Andric const InputInfo &Output, 10880b57cec5SDimitry Andric const InputInfoList &Inputs) const { 10890b57cec5SDimitry Andric const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU(); 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric CheckPreprocessingOptions(D, Args); 10920b57cec5SDimitry Andric 10930b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_C); 10940b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_CC); 10950b57cec5SDimitry Andric 10960b57cec5SDimitry Andric // Handle dependency file generation. 1097a7dea167SDimitry Andric Arg *ArgM = Args.getLastArg(options::OPT_MM); 1098a7dea167SDimitry Andric if (!ArgM) 1099a7dea167SDimitry Andric ArgM = Args.getLastArg(options::OPT_M); 1100a7dea167SDimitry Andric Arg *ArgMD = Args.getLastArg(options::OPT_MMD); 1101a7dea167SDimitry Andric if (!ArgMD) 1102a7dea167SDimitry Andric ArgMD = Args.getLastArg(options::OPT_MD); 1103a7dea167SDimitry Andric 1104a7dea167SDimitry Andric // -M and -MM imply -w. 1105a7dea167SDimitry Andric if (ArgM) 1106a7dea167SDimitry Andric CmdArgs.push_back("-w"); 1107a7dea167SDimitry Andric else 1108a7dea167SDimitry Andric ArgM = ArgMD; 1109a7dea167SDimitry Andric 1110a7dea167SDimitry Andric if (ArgM) { 11110b57cec5SDimitry Andric // Determine the output location. 11120b57cec5SDimitry Andric const char *DepFile; 11130b57cec5SDimitry Andric if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 11140b57cec5SDimitry Andric DepFile = MF->getValue(); 11150b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 11160b57cec5SDimitry Andric } else if (Output.getType() == types::TY_Dependencies) { 11170b57cec5SDimitry Andric DepFile = Output.getFilename(); 1118a7dea167SDimitry Andric } else if (!ArgMD) { 11190b57cec5SDimitry Andric DepFile = "-"; 11200b57cec5SDimitry Andric } else { 11210b57cec5SDimitry Andric DepFile = getDependencyFileName(Args, Inputs); 11220b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 11230b57cec5SDimitry Andric } 11240b57cec5SDimitry Andric CmdArgs.push_back("-dependency-file"); 11250b57cec5SDimitry Andric CmdArgs.push_back(DepFile); 11260b57cec5SDimitry Andric 1127a7dea167SDimitry Andric bool HasTarget = false; 1128a7dea167SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) { 1129a7dea167SDimitry Andric HasTarget = true; 1130a7dea167SDimitry Andric A->claim(); 1131a7dea167SDimitry Andric if (A->getOption().matches(options::OPT_MT)) { 1132a7dea167SDimitry Andric A->render(Args, CmdArgs); 1133a7dea167SDimitry Andric } else { 1134a7dea167SDimitry Andric CmdArgs.push_back("-MT"); 1135a7dea167SDimitry Andric SmallString<128> Quoted; 1136753f127fSDimitry Andric quoteMakeTarget(A->getValue(), Quoted); 1137a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 1138a7dea167SDimitry Andric } 1139a7dea167SDimitry Andric } 1140a7dea167SDimitry Andric 11410b57cec5SDimitry Andric // Add a default target if one wasn't specified. 1142a7dea167SDimitry Andric if (!HasTarget) { 11430b57cec5SDimitry Andric const char *DepTarget; 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric // If user provided -o, that is the dependency target, except 11460b57cec5SDimitry Andric // when we are only generating a dependency file. 11470b57cec5SDimitry Andric Arg *OutputOpt = Args.getLastArg(options::OPT_o); 11480b57cec5SDimitry Andric if (OutputOpt && Output.getType() != types::TY_Dependencies) { 11490b57cec5SDimitry Andric DepTarget = OutputOpt->getValue(); 11500b57cec5SDimitry Andric } else { 11510b57cec5SDimitry Andric // Otherwise derive from the base input. 11520b57cec5SDimitry Andric // 11530b57cec5SDimitry Andric // FIXME: This should use the computed output file location. 11540b57cec5SDimitry Andric SmallString<128> P(Inputs[0].getBaseInput()); 11550b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "o"); 11560b57cec5SDimitry Andric DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 11570b57cec5SDimitry Andric } 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andric CmdArgs.push_back("-MT"); 11600b57cec5SDimitry Andric SmallString<128> Quoted; 1161753f127fSDimitry Andric quoteMakeTarget(DepTarget, Quoted); 11620b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 1165a7dea167SDimitry Andric if (ArgM->getOption().matches(options::OPT_M) || 1166a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MD)) 11670b57cec5SDimitry Andric CmdArgs.push_back("-sys-header-deps"); 11680b57cec5SDimitry Andric if ((isa<PrecompileJobAction>(JA) && 11690b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_module_file_deps)) || 11700b57cec5SDimitry Andric Args.hasArg(options::OPT_fmodule_file_deps)) 11710b57cec5SDimitry Andric CmdArgs.push_back("-module-file-deps"); 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric if (Args.hasArg(options::OPT_MG)) { 1175a7dea167SDimitry Andric if (!ArgM || ArgM->getOption().matches(options::OPT_MD) || 1176a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MMD)) 11770b57cec5SDimitry Andric D.Diag(diag::err_drv_mg_requires_m_or_mm); 11780b57cec5SDimitry Andric CmdArgs.push_back("-MG"); 11790b57cec5SDimitry Andric } 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MP); 11820b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MV); 11830b57cec5SDimitry Andric 11845ffd83dbSDimitry Andric // Add offload include arguments specific for CUDA/HIP. This must happen 11855ffd83dbSDimitry Andric // before we -I or -include anything else, because we must pick up the 11865ffd83dbSDimitry Andric // CUDA/HIP headers from the particular CUDA/ROCm installation, rather than 11875ffd83dbSDimitry Andric // from e.g. /usr/local/include. 11880b57cec5SDimitry Andric if (JA.isOffloading(Action::OFK_Cuda)) 11890b57cec5SDimitry Andric getToolChain().AddCudaIncludeArgs(Args, CmdArgs); 11905ffd83dbSDimitry Andric if (JA.isOffloading(Action::OFK_HIP)) 11915ffd83dbSDimitry Andric getToolChain().AddHIPIncludeArgs(Args, CmdArgs); 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric // If we are offloading to a target via OpenMP we need to include the 11940b57cec5SDimitry Andric // openmp_wrappers folder which contains alternative system headers. 11950b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_OpenMP) && 119681ad6265SDimitry Andric !Args.hasArg(options::OPT_nostdinc) && 1197bdd1243dSDimitry Andric !Args.hasArg(options::OPT_nogpuinc) && 119869ade1e0SDimitry Andric (getToolChain().getTriple().isNVPTX() || 119969ade1e0SDimitry Andric getToolChain().getTriple().isAMDGCN())) { 12000b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nobuiltininc)) { 12010b57cec5SDimitry Andric // Add openmp_wrappers/* to our system include path. This lets us wrap 12020b57cec5SDimitry Andric // standard library headers. 12030b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 12040b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 12050b57cec5SDimitry Andric llvm::sys::path::append(P, "openmp_wrappers"); 12060b57cec5SDimitry Andric CmdArgs.push_back("-internal-isystem"); 12070b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12080b57cec5SDimitry Andric } 12090b57cec5SDimitry Andric 12100b57cec5SDimitry Andric CmdArgs.push_back("-include"); 12115ffd83dbSDimitry Andric CmdArgs.push_back("__clang_openmp_device_functions.h"); 12120b57cec5SDimitry Andric } 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric // Add -i* options, and automatically translate to 12150b57cec5SDimitry Andric // -include-pch/-include-pth for transparent PCH support. It's 12160b57cec5SDimitry Andric // wonky, but we include looking for .gch so we can support seamless 12170b57cec5SDimitry Andric // replacement into a build system already set up to be generating 12180b57cec5SDimitry Andric // .gch files. 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric if (getToolChain().getDriver().IsCLMode()) { 12210b57cec5SDimitry Andric const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); 12220b57cec5SDimitry Andric const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); 12230b57cec5SDimitry Andric if (YcArg && JA.getKind() >= Action::PrecompileJobClass && 12240b57cec5SDimitry Andric JA.getKind() <= Action::AssembleJobClass) { 12250b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj")); 1226f91b0c1cSDimitry Andric // -fpch-instantiate-templates is the default when creating 1227f91b0c1cSDimitry Andric // precomp using /Yc 1228f91b0c1cSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 1229f91b0c1cSDimitry Andric options::OPT_fno_pch_instantiate_templates, true)) 12305ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates")); 12310b57cec5SDimitry Andric } 12320b57cec5SDimitry Andric if (YcArg || YuArg) { 12330b57cec5SDimitry Andric StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue(); 12340b57cec5SDimitry Andric if (!isa<PrecompileJobAction>(JA)) { 12350b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 12360b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath( 12370b57cec5SDimitry Andric C, !ThroughHeader.empty() 12380b57cec5SDimitry Andric ? ThroughHeader 12390b57cec5SDimitry Andric : llvm::sys::path::filename(Inputs[0].getBaseInput())))); 12400b57cec5SDimitry Andric } 12410b57cec5SDimitry Andric 12420b57cec5SDimitry Andric if (ThroughHeader.empty()) { 12430b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 12440b57cec5SDimitry Andric Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use"))); 12450b57cec5SDimitry Andric } else { 12460b57cec5SDimitry Andric CmdArgs.push_back( 12470b57cec5SDimitry Andric Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader)); 12480b57cec5SDimitry Andric } 12490b57cec5SDimitry Andric } 12500b57cec5SDimitry Andric } 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric bool RenderedImplicitInclude = false; 12530b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) { 125481ad6265SDimitry Andric if (A->getOption().matches(options::OPT_include) && 125581ad6265SDimitry Andric D.getProbePrecompiled()) { 12560b57cec5SDimitry Andric // Handling of gcc-style gch precompiled headers. 12570b57cec5SDimitry Andric bool IsFirstImplicitInclude = !RenderedImplicitInclude; 12580b57cec5SDimitry Andric RenderedImplicitInclude = true; 12590b57cec5SDimitry Andric 12600b57cec5SDimitry Andric bool FoundPCH = false; 12610b57cec5SDimitry Andric SmallString<128> P(A->getValue()); 12620b57cec5SDimitry Andric // We want the files to have a name like foo.h.pch. Add a dummy extension 12630b57cec5SDimitry Andric // so that replace_extension does the right thing. 12640b57cec5SDimitry Andric P += ".dummy"; 12650b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "pch"); 126681ad6265SDimitry Andric if (D.getVFS().exists(P)) 12670b57cec5SDimitry Andric FoundPCH = true; 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric if (!FoundPCH) { 12700b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "gch"); 127181ad6265SDimitry Andric if (D.getVFS().exists(P)) { 12720b57cec5SDimitry Andric FoundPCH = true; 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric } 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric if (FoundPCH) { 12770b57cec5SDimitry Andric if (IsFirstImplicitInclude) { 12780b57cec5SDimitry Andric A->claim(); 12790b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 12800b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12810b57cec5SDimitry Andric continue; 12820b57cec5SDimitry Andric } else { 12830b57cec5SDimitry Andric // Ignore the PCH if not first on command line and emit warning. 12840b57cec5SDimitry Andric D.Diag(diag::warn_drv_pch_not_first_include) << P 12850b57cec5SDimitry Andric << A->getAsString(Args); 12860b57cec5SDimitry Andric } 12870b57cec5SDimitry Andric } 12880b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_isystem_after)) { 12890b57cec5SDimitry Andric // Handling of paths which must come late. These entries are handled by 12900b57cec5SDimitry Andric // the toolchain itself after the resource dir is inserted in the right 12910b57cec5SDimitry Andric // search order. 12920b57cec5SDimitry Andric // Do not claim the argument so that the use of the argument does not 12930b57cec5SDimitry Andric // silently go unnoticed on toolchains which do not honour the option. 12940b57cec5SDimitry Andric continue; 1295a7dea167SDimitry Andric } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) { 1296a7dea167SDimitry Andric // Translated to -internal-isystem by the driver, no need to pass to cc1. 1297a7dea167SDimitry Andric continue; 12980b57cec5SDimitry Andric } 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric // Not translated, render as usual. 13010b57cec5SDimitry Andric A->claim(); 13020b57cec5SDimitry Andric A->render(Args, CmdArgs); 13030b57cec5SDimitry Andric } 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 13060b57cec5SDimitry Andric {options::OPT_D, options::OPT_U, options::OPT_I_Group, 13070b57cec5SDimitry Andric options::OPT_F, options::OPT_index_header_map}); 13080b57cec5SDimitry Andric 13090b57cec5SDimitry Andric // Add -Wp, and -Xpreprocessor if using the preprocessor. 13100b57cec5SDimitry Andric 13110b57cec5SDimitry Andric // FIXME: There is a very unfortunate problem here, some troubled 13120b57cec5SDimitry Andric // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 13130b57cec5SDimitry Andric // really support that we would have to parse and then translate 13140b57cec5SDimitry Andric // those options. :( 13150b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 13160b57cec5SDimitry Andric options::OPT_Xpreprocessor); 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric // -I- is a deprecated GCC feature, reject it. 13190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_I_)) 13200b57cec5SDimitry Andric D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 13230b57cec5SDimitry Andric // -isysroot to the CC1 invocation. 13240b57cec5SDimitry Andric StringRef sysroot = C.getSysRoot(); 13250b57cec5SDimitry Andric if (sysroot != "") { 13260b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_isysroot)) { 13270b57cec5SDimitry Andric CmdArgs.push_back("-isysroot"); 13280b57cec5SDimitry Andric CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 13290b57cec5SDimitry Andric } 13300b57cec5SDimitry Andric } 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric // Parse additional include paths from environment variables. 13330b57cec5SDimitry Andric // FIXME: We should probably sink the logic for handling these from the 13340b57cec5SDimitry Andric // frontend into the driver. It will allow deleting 4 otherwise unused flags. 13350b57cec5SDimitry Andric // CPATH - included following the user specified includes (but prior to 13360b57cec5SDimitry Andric // builtin and standard includes). 13370b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 13380b57cec5SDimitry Andric // C_INCLUDE_PATH - system includes enabled when compiling C. 13390b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 13400b57cec5SDimitry Andric // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 13410b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 13420b57cec5SDimitry Andric // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 13430b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 13440b57cec5SDimitry Andric // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 13450b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric // While adding the include arguments, we also attempt to retrieve the 13480b57cec5SDimitry Andric // arguments of related offloading toolchains or arguments that are specific 13490b57cec5SDimitry Andric // of an offloading programming model. 13500b57cec5SDimitry Andric 13510b57cec5SDimitry Andric // Add C++ include arguments, if needed. 1352a7dea167SDimitry Andric if (types::isCXX(Inputs[0].getType())) { 1353a7dea167SDimitry Andric bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem); 1354a7dea167SDimitry Andric forAllAssociatedToolChains( 1355a7dea167SDimitry Andric C, JA, getToolChain(), 1356a7dea167SDimitry Andric [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) { 1357a7dea167SDimitry Andric HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs) 1358a7dea167SDimitry Andric : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 13590b57cec5SDimitry Andric }); 1360a7dea167SDimitry Andric } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric // Add system include arguments for all targets but IAMCU. 13630b57cec5SDimitry Andric if (!IsIAMCU) 13640b57cec5SDimitry Andric forAllAssociatedToolChains(C, JA, getToolChain(), 13650b57cec5SDimitry Andric [&Args, &CmdArgs](const ToolChain &TC) { 13660b57cec5SDimitry Andric TC.AddClangSystemIncludeArgs(Args, CmdArgs); 13670b57cec5SDimitry Andric }); 13680b57cec5SDimitry Andric else { 13690b57cec5SDimitry Andric // For IAMCU add special include arguments. 13700b57cec5SDimitry Andric getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs); 13710b57cec5SDimitry Andric } 1372480093f4SDimitry Andric 1373480093f4SDimitry Andric addMacroPrefixMapArg(D, Args, CmdArgs); 1374fe6060f1SDimitry Andric addCoveragePrefixMapArg(D, Args, CmdArgs); 137581ad6265SDimitry Andric 137681ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffile_reproducible, 137781ad6265SDimitry Andric options::OPT_fno_file_reproducible); 1378bdd1243dSDimitry Andric 1379bdd1243dSDimitry Andric if (const char *Epoch = std::getenv("SOURCE_DATE_EPOCH")) { 1380bdd1243dSDimitry Andric CmdArgs.push_back("-source-date-epoch"); 1381bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Epoch)); 1382bdd1243dSDimitry Andric } 13830b57cec5SDimitry Andric } 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric // FIXME: Move to target hook. 13860b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) { 13870b57cec5SDimitry Andric switch (Triple.getArch()) { 13880b57cec5SDimitry Andric default: 13890b57cec5SDimitry Andric return true; 13900b57cec5SDimitry Andric 13910b57cec5SDimitry Andric case llvm::Triple::aarch64: 1392480093f4SDimitry Andric case llvm::Triple::aarch64_32: 13930b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 13940b57cec5SDimitry Andric case llvm::Triple::arm: 13950b57cec5SDimitry Andric case llvm::Triple::armeb: 13960b57cec5SDimitry Andric case llvm::Triple::thumb: 13970b57cec5SDimitry Andric case llvm::Triple::thumbeb: 13980b57cec5SDimitry Andric if (Triple.isOSDarwin() || Triple.isOSWindows()) 13990b57cec5SDimitry Andric return true; 14000b57cec5SDimitry Andric return false; 14010b57cec5SDimitry Andric 14020b57cec5SDimitry Andric case llvm::Triple::ppc: 14030b57cec5SDimitry Andric case llvm::Triple::ppc64: 14040b57cec5SDimitry Andric if (Triple.isOSDarwin()) 14050b57cec5SDimitry Andric return true; 14060b57cec5SDimitry Andric return false; 14070b57cec5SDimitry Andric 14080b57cec5SDimitry Andric case llvm::Triple::hexagon: 1409e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 14100b57cec5SDimitry Andric case llvm::Triple::ppc64le: 14110b57cec5SDimitry Andric case llvm::Triple::riscv32: 14120b57cec5SDimitry Andric case llvm::Triple::riscv64: 14130b57cec5SDimitry Andric case llvm::Triple::systemz: 14140b57cec5SDimitry Andric case llvm::Triple::xcore: 14150b57cec5SDimitry Andric return false; 14160b57cec5SDimitry Andric } 14170b57cec5SDimitry Andric } 14180b57cec5SDimitry Andric 1419480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple, 1420480093f4SDimitry Andric const ArgList &Args) { 1421480093f4SDimitry Andric // Supported only on Darwin where we invoke the compiler multiple times 1422480093f4SDimitry Andric // followed by an invocation to lipo. 1423480093f4SDimitry Andric if (!Triple.isOSDarwin()) 1424480093f4SDimitry Andric return false; 1425480093f4SDimitry Andric // If more than one "-arch <arch>" is specified, we're targeting multiple 1426480093f4SDimitry Andric // architectures resulting in a fat binary. 1427480093f4SDimitry Andric return Args.getAllArgValues(options::OPT_arch).size() > 1; 1428480093f4SDimitry Andric } 1429480093f4SDimitry Andric 1430480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args, 1431480093f4SDimitry Andric const llvm::Triple &Triple) { 1432480093f4SDimitry Andric // When enabling remarks, we need to error if: 1433480093f4SDimitry Andric // * The remark file is specified but we're targeting multiple architectures, 1434480093f4SDimitry Andric // which means more than one remark file is being generated. 1435480093f4SDimitry Andric bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args); 1436480093f4SDimitry Andric bool hasExplicitOutputFile = 1437480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1438480093f4SDimitry Andric if (hasMultipleInvocations && hasExplicitOutputFile) { 1439480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_output_with_multiple_archs) 1440480093f4SDimitry Andric << "-foptimization-record-file"; 1441480093f4SDimitry Andric return false; 1442480093f4SDimitry Andric } 1443480093f4SDimitry Andric return true; 1444480093f4SDimitry Andric } 1445480093f4SDimitry Andric 1446480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, 1447480093f4SDimitry Andric const llvm::Triple &Triple, 1448480093f4SDimitry Andric const InputInfo &Input, 1449480093f4SDimitry Andric const InputInfo &Output, const JobAction &JA) { 1450480093f4SDimitry Andric StringRef Format = "yaml"; 1451480093f4SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) 1452480093f4SDimitry Andric Format = A->getValue(); 1453480093f4SDimitry Andric 1454480093f4SDimitry Andric CmdArgs.push_back("-opt-record-file"); 1455480093f4SDimitry Andric 1456480093f4SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1457480093f4SDimitry Andric if (A) { 1458480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1459480093f4SDimitry Andric } else { 1460480093f4SDimitry Andric bool hasMultipleArchs = 1461480093f4SDimitry Andric Triple.isOSDarwin() && // Only supported on Darwin platforms. 1462480093f4SDimitry Andric Args.getAllArgValues(options::OPT_arch).size() > 1; 1463480093f4SDimitry Andric 1464480093f4SDimitry Andric SmallString<128> F; 1465480093f4SDimitry Andric 1466480093f4SDimitry Andric if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) { 1467480093f4SDimitry Andric if (Arg *FinalOutput = Args.getLastArg(options::OPT_o)) 1468480093f4SDimitry Andric F = FinalOutput->getValue(); 1469480093f4SDimitry Andric } else { 1470480093f4SDimitry Andric if (Format != "yaml" && // For YAML, keep the original behavior. 1471480093f4SDimitry Andric Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles. 1472480093f4SDimitry Andric Output.isFilename()) 1473480093f4SDimitry Andric F = Output.getFilename(); 1474480093f4SDimitry Andric } 1475480093f4SDimitry Andric 1476480093f4SDimitry Andric if (F.empty()) { 1477480093f4SDimitry Andric // Use the input filename. 1478480093f4SDimitry Andric F = llvm::sys::path::stem(Input.getBaseInput()); 1479480093f4SDimitry Andric 1480480093f4SDimitry Andric // If we're compiling for an offload architecture (i.e. a CUDA device), 1481480093f4SDimitry Andric // we need to make the file name for the device compilation different 1482480093f4SDimitry Andric // from the host compilation. 1483480093f4SDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_None) && 1484480093f4SDimitry Andric !JA.isDeviceOffloading(Action::OFK_Host)) { 1485480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1486480093f4SDimitry Andric F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(), 1487480093f4SDimitry Andric Triple.normalize()); 1488480093f4SDimitry Andric F += "-"; 1489480093f4SDimitry Andric F += JA.getOffloadingArch(); 1490480093f4SDimitry Andric } 1491480093f4SDimitry Andric } 1492480093f4SDimitry Andric 1493480093f4SDimitry Andric // If we're having more than one "-arch", we should name the files 1494480093f4SDimitry Andric // differently so that every cc1 invocation writes to a different file. 1495480093f4SDimitry Andric // We're doing that by appending "-<arch>" with "<arch>" being the arch 1496480093f4SDimitry Andric // name from the triple. 1497480093f4SDimitry Andric if (hasMultipleArchs) { 1498480093f4SDimitry Andric // First, remember the extension. 1499480093f4SDimitry Andric SmallString<64> OldExtension = llvm::sys::path::extension(F); 1500480093f4SDimitry Andric // then, remove it. 1501480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1502480093f4SDimitry Andric // attach -<arch> to it. 1503480093f4SDimitry Andric F += "-"; 1504480093f4SDimitry Andric F += Triple.getArchName(); 1505480093f4SDimitry Andric // put back the extension. 1506480093f4SDimitry Andric llvm::sys::path::replace_extension(F, OldExtension); 1507480093f4SDimitry Andric } 1508480093f4SDimitry Andric 1509480093f4SDimitry Andric SmallString<32> Extension; 1510480093f4SDimitry Andric Extension += "opt."; 1511480093f4SDimitry Andric Extension += Format; 1512480093f4SDimitry Andric 1513480093f4SDimitry Andric llvm::sys::path::replace_extension(F, Extension); 1514480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(F)); 1515480093f4SDimitry Andric } 1516480093f4SDimitry Andric 1517480093f4SDimitry Andric if (const Arg *A = 1518480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) { 1519480093f4SDimitry Andric CmdArgs.push_back("-opt-record-passes"); 1520480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1521480093f4SDimitry Andric } 1522480093f4SDimitry Andric 1523480093f4SDimitry Andric if (!Format.empty()) { 1524480093f4SDimitry Andric CmdArgs.push_back("-opt-record-format"); 1525480093f4SDimitry Andric CmdArgs.push_back(Format.data()); 1526480093f4SDimitry Andric } 1527480093f4SDimitry Andric } 1528480093f4SDimitry Andric 1529fe6060f1SDimitry Andric void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) { 1530fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_faapcs_bitfield_width, 1531fe6060f1SDimitry Andric options::OPT_fno_aapcs_bitfield_width, true)) 1532fe6060f1SDimitry Andric CmdArgs.push_back("-fno-aapcs-bitfield-width"); 1533fe6060f1SDimitry Andric 1534fe6060f1SDimitry Andric if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad)) 1535fe6060f1SDimitry Andric CmdArgs.push_back("-faapcs-bitfield-load"); 1536fe6060f1SDimitry Andric } 1537fe6060f1SDimitry Andric 15380b57cec5SDimitry Andric namespace { 1539349cc55cSDimitry Andric void RenderARMABI(const Driver &D, const llvm::Triple &Triple, 1540349cc55cSDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 15410b57cec5SDimitry Andric // Select the ABI to use. 15420b57cec5SDimitry Andric // FIXME: Support -meabi. 15430b57cec5SDimitry Andric // FIXME: Parts of this are duplicated in the backend, unify this somehow. 15440b57cec5SDimitry Andric const char *ABIName = nullptr; 15450b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 15460b57cec5SDimitry Andric ABIName = A->getValue(); 15470b57cec5SDimitry Andric } else { 1548349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 15490b57cec5SDimitry Andric ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data(); 15500b57cec5SDimitry Andric } 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 15530b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 15540b57cec5SDimitry Andric } 155581ad6265SDimitry Andric 155681ad6265SDimitry Andric void AddUnalignedAccessWarning(ArgStringList &CmdArgs) { 155781ad6265SDimitry Andric auto StrictAlignIter = 1558bdd1243dSDimitry Andric llvm::find_if(llvm::reverse(CmdArgs), [](StringRef Arg) { 155981ad6265SDimitry Andric return Arg == "+strict-align" || Arg == "-strict-align"; 156081ad6265SDimitry Andric }); 156181ad6265SDimitry Andric if (StrictAlignIter != CmdArgs.rend() && 156281ad6265SDimitry Andric StringRef(*StrictAlignIter) == "+strict-align") 156381ad6265SDimitry Andric CmdArgs.push_back("-Wunaligned-access"); 156481ad6265SDimitry Andric } 15650b57cec5SDimitry Andric } 15660b57cec5SDimitry Andric 15671fd87a68SDimitry Andric static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, 15684824e7fdSDimitry Andric ArgStringList &CmdArgs, bool isAArch64) { 15694824e7fdSDimitry Andric const Arg *A = isAArch64 15704824e7fdSDimitry Andric ? Args.getLastArg(options::OPT_msign_return_address_EQ, 15714824e7fdSDimitry Andric options::OPT_mbranch_protection_EQ) 15724824e7fdSDimitry Andric : Args.getLastArg(options::OPT_mbranch_protection_EQ); 15734824e7fdSDimitry Andric if (!A) 15744824e7fdSDimitry Andric return; 15754824e7fdSDimitry Andric 15761fd87a68SDimitry Andric const Driver &D = TC.getDriver(); 15771fd87a68SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 15781fd87a68SDimitry Andric if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass()))) 1579d56accc7SDimitry Andric D.Diag(diag::warn_incompatible_branch_protection_option) 15801fd87a68SDimitry Andric << Triple.getArchName(); 15811fd87a68SDimitry Andric 15824824e7fdSDimitry Andric StringRef Scope, Key; 15834824e7fdSDimitry Andric bool IndirectBranches; 15844824e7fdSDimitry Andric 15854824e7fdSDimitry Andric if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { 15864824e7fdSDimitry Andric Scope = A->getValue(); 158781ad6265SDimitry Andric if (Scope != "none" && Scope != "non-leaf" && Scope != "all") 158881ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1589bdd1243dSDimitry Andric << A->getSpelling() << Scope; 15904824e7fdSDimitry Andric Key = "a_key"; 15914824e7fdSDimitry Andric IndirectBranches = false; 15924824e7fdSDimitry Andric } else { 15934824e7fdSDimitry Andric StringRef DiagMsg; 15944824e7fdSDimitry Andric llvm::ARM::ParsedBranchProtection PBP; 15954824e7fdSDimitry Andric if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg)) 159681ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1597bdd1243dSDimitry Andric << A->getSpelling() << DiagMsg; 15984824e7fdSDimitry Andric if (!isAArch64 && PBP.Key == "b_key") 15994824e7fdSDimitry Andric D.Diag(diag::warn_unsupported_branch_protection) 16004824e7fdSDimitry Andric << "b-key" << A->getAsString(Args); 16014824e7fdSDimitry Andric Scope = PBP.Scope; 16024824e7fdSDimitry Andric Key = PBP.Key; 16034824e7fdSDimitry Andric IndirectBranches = PBP.BranchTargetEnforcement; 16044824e7fdSDimitry Andric } 16054824e7fdSDimitry Andric 16064824e7fdSDimitry Andric CmdArgs.push_back( 16074824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address=") + Scope)); 16084824e7fdSDimitry Andric if (!Scope.equals("none")) 16094824e7fdSDimitry Andric CmdArgs.push_back( 16104824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); 16114824e7fdSDimitry Andric if (IndirectBranches) 16124824e7fdSDimitry Andric CmdArgs.push_back("-mbranch-target-enforce"); 16134824e7fdSDimitry Andric } 16144824e7fdSDimitry Andric 16150b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, 16160b57cec5SDimitry Andric ArgStringList &CmdArgs, bool KernelOrKext) const { 1617349cc55cSDimitry Andric RenderARMABI(getToolChain().getDriver(), Triple, Args, CmdArgs); 16180b57cec5SDimitry Andric 16190b57cec5SDimitry Andric // Determine floating point ABI from the options & target defaults. 16200b57cec5SDimitry Andric arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); 16210b57cec5SDimitry Andric if (ABI == arm::FloatABI::Soft) { 16220b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 16230b57cec5SDimitry Andric // FIXME: This changes CPP defines, we need -target-soft-float. 16240b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 16250b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16260b57cec5SDimitry Andric CmdArgs.push_back("soft"); 16270b57cec5SDimitry Andric } else if (ABI == arm::FloatABI::SoftFP) { 16280b57cec5SDimitry Andric // Floating point operations are hard, but argument passing is soft. 16290b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16300b57cec5SDimitry Andric CmdArgs.push_back("soft"); 16310b57cec5SDimitry Andric } else { 16320b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 16330b57cec5SDimitry Andric assert(ABI == arm::FloatABI::Hard && "Invalid float abi!"); 16340b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 16350b57cec5SDimitry Andric CmdArgs.push_back("hard"); 16360b57cec5SDimitry Andric } 16370b57cec5SDimitry Andric 16380b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 16390b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 16400b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 16410b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 16420b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 16430b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=false"); 16440b57cec5SDimitry Andric else 16450b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=true"); 16460b57cec5SDimitry Andric } 16470b57cec5SDimitry Andric 16480b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 16490b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 16500b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 16510b57cec5SDimitry Andric 16520b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mcmse)) 16530b57cec5SDimitry Andric CmdArgs.push_back("-mcmse"); 1654fe6060f1SDimitry Andric 1655fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 16564824e7fdSDimitry Andric 16574824e7fdSDimitry Andric // Enable/disable return address signing and indirect branch targets. 16581fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); 165981ad6265SDimitry Andric 166081ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 16610b57cec5SDimitry Andric } 16620b57cec5SDimitry Andric 16630b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, 16640b57cec5SDimitry Andric const ArgList &Args, bool KernelOrKext, 16650b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 16660b57cec5SDimitry Andric const ToolChain &TC = getToolChain(); 16670b57cec5SDimitry Andric 16680b57cec5SDimitry Andric // Add the target features 16695ffd83dbSDimitry Andric getTargetFeatures(TC.getDriver(), EffectiveTriple, Args, CmdArgs, false); 16700b57cec5SDimitry Andric 16710b57cec5SDimitry Andric // Add target specific flags. 16720b57cec5SDimitry Andric switch (TC.getArch()) { 16730b57cec5SDimitry Andric default: 16740b57cec5SDimitry Andric break; 16750b57cec5SDimitry Andric 16760b57cec5SDimitry Andric case llvm::Triple::arm: 16770b57cec5SDimitry Andric case llvm::Triple::armeb: 16780b57cec5SDimitry Andric case llvm::Triple::thumb: 16790b57cec5SDimitry Andric case llvm::Triple::thumbeb: 16800b57cec5SDimitry Andric // Use the effective triple, which takes into account the deployment target. 16810b57cec5SDimitry Andric AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext); 16820b57cec5SDimitry Andric break; 16830b57cec5SDimitry Andric 16840b57cec5SDimitry Andric case llvm::Triple::aarch64: 1685480093f4SDimitry Andric case llvm::Triple::aarch64_32: 16860b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 16870b57cec5SDimitry Andric AddAArch64TargetArgs(Args, CmdArgs); 1688bdd1243dSDimitry Andric break; 1689bdd1243dSDimitry Andric 1690bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 1691bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 1692bdd1243dSDimitry Andric AddLoongArchTargetArgs(Args, CmdArgs); 16930b57cec5SDimitry Andric break; 16940b57cec5SDimitry Andric 16950b57cec5SDimitry Andric case llvm::Triple::mips: 16960b57cec5SDimitry Andric case llvm::Triple::mipsel: 16970b57cec5SDimitry Andric case llvm::Triple::mips64: 16980b57cec5SDimitry Andric case llvm::Triple::mips64el: 16990b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 17000b57cec5SDimitry Andric break; 17010b57cec5SDimitry Andric 17020b57cec5SDimitry Andric case llvm::Triple::ppc: 1703e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 17040b57cec5SDimitry Andric case llvm::Triple::ppc64: 17050b57cec5SDimitry Andric case llvm::Triple::ppc64le: 17060b57cec5SDimitry Andric AddPPCTargetArgs(Args, CmdArgs); 17070b57cec5SDimitry Andric break; 17080b57cec5SDimitry Andric 17090b57cec5SDimitry Andric case llvm::Triple::riscv32: 17100b57cec5SDimitry Andric case llvm::Triple::riscv64: 17110b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 17120b57cec5SDimitry Andric break; 17130b57cec5SDimitry Andric 17140b57cec5SDimitry Andric case llvm::Triple::sparc: 17150b57cec5SDimitry Andric case llvm::Triple::sparcel: 17160b57cec5SDimitry Andric case llvm::Triple::sparcv9: 17170b57cec5SDimitry Andric AddSparcTargetArgs(Args, CmdArgs); 17180b57cec5SDimitry Andric break; 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric case llvm::Triple::systemz: 17210b57cec5SDimitry Andric AddSystemZTargetArgs(Args, CmdArgs); 17220b57cec5SDimitry Andric break; 17230b57cec5SDimitry Andric 17240b57cec5SDimitry Andric case llvm::Triple::x86: 17250b57cec5SDimitry Andric case llvm::Triple::x86_64: 17260b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 17270b57cec5SDimitry Andric break; 17280b57cec5SDimitry Andric 17290b57cec5SDimitry Andric case llvm::Triple::lanai: 17300b57cec5SDimitry Andric AddLanaiTargetArgs(Args, CmdArgs); 17310b57cec5SDimitry Andric break; 17320b57cec5SDimitry Andric 17330b57cec5SDimitry Andric case llvm::Triple::hexagon: 17340b57cec5SDimitry Andric AddHexagonTargetArgs(Args, CmdArgs); 17350b57cec5SDimitry Andric break; 17360b57cec5SDimitry Andric 17370b57cec5SDimitry Andric case llvm::Triple::wasm32: 17380b57cec5SDimitry Andric case llvm::Triple::wasm64: 17390b57cec5SDimitry Andric AddWebAssemblyTargetArgs(Args, CmdArgs); 17400b57cec5SDimitry Andric break; 17415ffd83dbSDimitry Andric 17425ffd83dbSDimitry Andric case llvm::Triple::ve: 17435ffd83dbSDimitry Andric AddVETargetArgs(Args, CmdArgs); 17445ffd83dbSDimitry Andric break; 17450b57cec5SDimitry Andric } 17460b57cec5SDimitry Andric } 17470b57cec5SDimitry Andric 17480b57cec5SDimitry Andric namespace { 17490b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args, 17500b57cec5SDimitry Andric ArgStringList &CmdArgs) { 17510b57cec5SDimitry Andric const char *ABIName = nullptr; 17520b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) 17530b57cec5SDimitry Andric ABIName = A->getValue(); 17540b57cec5SDimitry Andric else if (Triple.isOSDarwin()) 17550b57cec5SDimitry Andric ABIName = "darwinpcs"; 17560b57cec5SDimitry Andric else 17570b57cec5SDimitry Andric ABIName = "aapcs"; 17580b57cec5SDimitry Andric 17590b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 17600b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 17610b57cec5SDimitry Andric } 17620b57cec5SDimitry Andric } 17630b57cec5SDimitry Andric 17640b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args, 17650b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 17660b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 17670b57cec5SDimitry Andric 17680b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 17690b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 17700b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 17710b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 17740b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 17750b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 17780b57cec5SDimitry Andric 17790b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 17800b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 17810b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 17820b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 17830b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 17840b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=false"); 17850b57cec5SDimitry Andric else 17860b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=true"); 17870b57cec5SDimitry Andric } 17880b57cec5SDimitry Andric 17890b57cec5SDimitry Andric // Enable/disable return address signing and indirect branch targets. 17901fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, true /*isAArch64*/); 1791e8d8bef9SDimitry Andric 1792e8d8bef9SDimitry Andric // Handle -msve_vector_bits=<bits> 1793e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) { 1794e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 1795e8d8bef9SDimitry Andric const Driver &D = getToolChain().getDriver(); 1796e8d8bef9SDimitry Andric if (Val.equals("128") || Val.equals("256") || Val.equals("512") || 1797349cc55cSDimitry Andric Val.equals("1024") || Val.equals("2048") || Val.equals("128+") || 1798349cc55cSDimitry Andric Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") || 1799349cc55cSDimitry Andric Val.equals("2048+")) { 1800349cc55cSDimitry Andric unsigned Bits = 0; 1801349cc55cSDimitry Andric if (Val.endswith("+")) 1802349cc55cSDimitry Andric Val = Val.substr(0, Val.size() - 1); 1803349cc55cSDimitry Andric else { 1804349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1805349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1806e8d8bef9SDimitry Andric CmdArgs.push_back( 1807349cc55cSDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128))); 1808349cc55cSDimitry Andric } 1809349cc55cSDimitry Andric 1810349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1811349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1812349cc55cSDimitry Andric CmdArgs.push_back( 1813349cc55cSDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128))); 1814e8d8bef9SDimitry Andric // Silently drop requests for vector-length agnostic code as it's implied. 1815349cc55cSDimitry Andric } else if (!Val.equals("scalable")) 1816e8d8bef9SDimitry Andric // Handle the unsupported values passed to msve-vector-bits. 1817e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1818bdd1243dSDimitry Andric << A->getSpelling() << Val; 1819e8d8bef9SDimitry Andric } 1820fe6060f1SDimitry Andric 1821fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 1822349cc55cSDimitry Andric 1823349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 1824349cc55cSDimitry Andric CmdArgs.push_back("-tune-cpu"); 1825fcaf7f86SDimitry Andric if (strcmp(A->getValue(), "native") == 0) 1826fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 1827fcaf7f86SDimitry Andric else 1828fcaf7f86SDimitry Andric CmdArgs.push_back(A->getValue()); 1829349cc55cSDimitry Andric } 183081ad6265SDimitry Andric 183181ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 18320b57cec5SDimitry Andric } 18330b57cec5SDimitry Andric 1834bdd1243dSDimitry Andric void Clang::AddLoongArchTargetArgs(const ArgList &Args, 1835bdd1243dSDimitry Andric ArgStringList &CmdArgs) const { 1836bdd1243dSDimitry Andric CmdArgs.push_back("-target-abi"); 1837bdd1243dSDimitry Andric CmdArgs.push_back(loongarch::getLoongArchABI(getToolChain().getDriver(), Args, 1838bdd1243dSDimitry Andric getToolChain().getTriple()) 1839bdd1243dSDimitry Andric .data()); 1840bdd1243dSDimitry Andric } 1841bdd1243dSDimitry Andric 18420b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args, 18430b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 18440b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 18450b57cec5SDimitry Andric StringRef CPUName; 18460b57cec5SDimitry Andric StringRef ABIName; 18470b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 18480b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 18490b57cec5SDimitry Andric 18500b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 18510b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 18520b57cec5SDimitry Andric 18530b57cec5SDimitry Andric mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple); 18540b57cec5SDimitry Andric if (ABI == mips::FloatABI::Soft) { 18550b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 18560b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 18570b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 18580b57cec5SDimitry Andric CmdArgs.push_back("soft"); 18590b57cec5SDimitry Andric } else { 18600b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 18610b57cec5SDimitry Andric assert(ABI == mips::FloatABI::Hard && "Invalid float abi!"); 18620b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 18630b57cec5SDimitry Andric CmdArgs.push_back("hard"); 18640b57cec5SDimitry Andric } 18650b57cec5SDimitry Andric 18660b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 18670b57cec5SDimitry Andric options::OPT_mno_ldc1_sdc1)) { 18680b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 18690b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18700b57cec5SDimitry Andric CmdArgs.push_back("-mno-ldc1-sdc1"); 18710b57cec5SDimitry Andric } 18720b57cec5SDimitry Andric } 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 18750b57cec5SDimitry Andric options::OPT_mno_check_zero_division)) { 18760b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 18770b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18780b57cec5SDimitry Andric CmdArgs.push_back("-mno-check-zero-division"); 18790b57cec5SDimitry Andric } 18800b57cec5SDimitry Andric } 18810b57cec5SDimitry Andric 188204eeddc0SDimitry Andric if (Args.getLastArg(options::OPT_mfix4300)) { 188304eeddc0SDimitry Andric CmdArgs.push_back("-mllvm"); 188404eeddc0SDimitry Andric CmdArgs.push_back("-mfix4300"); 188504eeddc0SDimitry Andric } 188604eeddc0SDimitry Andric 18870b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_G)) { 18880b57cec5SDimitry Andric StringRef v = A->getValue(); 18890b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18900b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 18910b57cec5SDimitry Andric A->claim(); 18920b57cec5SDimitry Andric } 18930b57cec5SDimitry Andric 18940b57cec5SDimitry Andric Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt); 18950b57cec5SDimitry Andric Arg *ABICalls = 18960b57cec5SDimitry Andric Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls); 18970b57cec5SDimitry Andric 18980b57cec5SDimitry Andric // -mabicalls is the default for many MIPS environments, even with -fno-pic. 18990b57cec5SDimitry Andric // -mgpopt is the default for static, -fno-pic environments but these two 19000b57cec5SDimitry Andric // options conflict. We want to be certain that -mno-abicalls -mgpopt is 19010b57cec5SDimitry Andric // the only case where -mllvm -mgpopt is passed. 19020b57cec5SDimitry Andric // NOTE: We need a warning here or in the backend to warn when -mgpopt is 19030b57cec5SDimitry Andric // passed explicitly when compiling something with -mabicalls 19040b57cec5SDimitry Andric // (implictly) in affect. Currently the warning is in the backend. 19050b57cec5SDimitry Andric // 19060b57cec5SDimitry Andric // When the ABI in use is N64, we also need to determine the PIC mode that 19070b57cec5SDimitry Andric // is in use, as -fno-pic for N64 implies -mno-abicalls. 19080b57cec5SDimitry Andric bool NoABICalls = 19090b57cec5SDimitry Andric ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls); 19100b57cec5SDimitry Andric 19110b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 19120b57cec5SDimitry Andric unsigned PICLevel; 19130b57cec5SDimitry Andric bool IsPIE; 19140b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 19150b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric NoABICalls = NoABICalls || 19180b57cec5SDimitry Andric (RelocationModel == llvm::Reloc::Static && ABIName == "n64"); 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt); 19210b57cec5SDimitry Andric // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt. 19220b57cec5SDimitry Andric if (NoABICalls && (!GPOpt || WantGPOpt)) { 19230b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19240b57cec5SDimitry Andric CmdArgs.push_back("-mgpopt"); 19250b57cec5SDimitry Andric 19260b57cec5SDimitry Andric Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata, 19270b57cec5SDimitry Andric options::OPT_mno_local_sdata); 19280b57cec5SDimitry Andric Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata, 19290b57cec5SDimitry Andric options::OPT_mno_extern_sdata); 19300b57cec5SDimitry Andric Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data, 19310b57cec5SDimitry Andric options::OPT_mno_embedded_data); 19320b57cec5SDimitry Andric if (LocalSData) { 19330b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19340b57cec5SDimitry Andric if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) { 19350b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=1"); 19360b57cec5SDimitry Andric } else { 19370b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=0"); 19380b57cec5SDimitry Andric } 19390b57cec5SDimitry Andric LocalSData->claim(); 19400b57cec5SDimitry Andric } 19410b57cec5SDimitry Andric 19420b57cec5SDimitry Andric if (ExternSData) { 19430b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19440b57cec5SDimitry Andric if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) { 19450b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=1"); 19460b57cec5SDimitry Andric } else { 19470b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=0"); 19480b57cec5SDimitry Andric } 19490b57cec5SDimitry Andric ExternSData->claim(); 19500b57cec5SDimitry Andric } 19510b57cec5SDimitry Andric 19520b57cec5SDimitry Andric if (EmbeddedData) { 19530b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19540b57cec5SDimitry Andric if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) { 19550b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=1"); 19560b57cec5SDimitry Andric } else { 19570b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=0"); 19580b57cec5SDimitry Andric } 19590b57cec5SDimitry Andric EmbeddedData->claim(); 19600b57cec5SDimitry Andric } 19610b57cec5SDimitry Andric 19620b57cec5SDimitry Andric } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt) 19630b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1); 19640b57cec5SDimitry Andric 19650b57cec5SDimitry Andric if (GPOpt) 19660b57cec5SDimitry Andric GPOpt->claim(); 19670b57cec5SDimitry Andric 19680b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) { 19690b57cec5SDimitry Andric StringRef Val = StringRef(A->getValue()); 19700b57cec5SDimitry Andric if (mips::hasCompactBranches(CPUName)) { 19710b57cec5SDimitry Andric if (Val == "never" || Val == "always" || Val == "optimal") { 19720b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19730b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val)); 19740b57cec5SDimitry Andric } else 19750b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1976bdd1243dSDimitry Andric << A->getSpelling() << Val; 19770b57cec5SDimitry Andric } else 19780b57cec5SDimitry Andric D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName; 19790b57cec5SDimitry Andric } 19800b57cec5SDimitry Andric 19810b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls, 19820b57cec5SDimitry Andric options::OPT_mno_relax_pic_calls)) { 19830b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) { 19840b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19850b57cec5SDimitry Andric CmdArgs.push_back("-mips-jalr-reloc=0"); 19860b57cec5SDimitry Andric } 19870b57cec5SDimitry Andric } 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric 19900b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args, 19910b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 19921ac55f4cSDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 1993bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 1994bdd1243dSDimitry Andric CmdArgs.push_back("-tune-cpu"); 19951ac55f4cSDimitry Andric std::string CPU = ppc::getPPCTuneCPU(Args, T); 19961ac55f4cSDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 1997bdd1243dSDimitry Andric } 1998bdd1243dSDimitry Andric 19990b57cec5SDimitry Andric // Select the ABI to use. 20000b57cec5SDimitry Andric const char *ABIName = nullptr; 2001480093f4SDimitry Andric if (T.isOSBinFormatELF()) { 20020b57cec5SDimitry Andric switch (getToolChain().getArch()) { 20030b57cec5SDimitry Andric case llvm::Triple::ppc64: { 20041ac55f4cSDimitry Andric if (T.isPPC64ELFv2ABI()) 2005480093f4SDimitry Andric ABIName = "elfv2"; 2006480093f4SDimitry Andric else 20070b57cec5SDimitry Andric ABIName = "elfv1"; 20080b57cec5SDimitry Andric break; 20090b57cec5SDimitry Andric } 20100b57cec5SDimitry Andric case llvm::Triple::ppc64le: 20110b57cec5SDimitry Andric ABIName = "elfv2"; 20120b57cec5SDimitry Andric break; 20130b57cec5SDimitry Andric default: 20140b57cec5SDimitry Andric break; 20150b57cec5SDimitry Andric } 2016480093f4SDimitry Andric } 20170b57cec5SDimitry Andric 201804eeddc0SDimitry Andric bool IEEELongDouble = getToolChain().defaultToIEEELongDouble(); 20190b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) { 20200b57cec5SDimitry Andric StringRef V = A->getValue(); 20210b57cec5SDimitry Andric if (V == "ieeelongdouble") 20220b57cec5SDimitry Andric IEEELongDouble = true; 20230b57cec5SDimitry Andric else if (V == "ibmlongdouble") 20240b57cec5SDimitry Andric IEEELongDouble = false; 20250b57cec5SDimitry Andric else if (V != "altivec") 20260b57cec5SDimitry Andric // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore 20270b57cec5SDimitry Andric // the option if given as we don't have backend support for any targets 20280b57cec5SDimitry Andric // that don't use the altivec abi. 20290b57cec5SDimitry Andric ABIName = A->getValue(); 20300b57cec5SDimitry Andric } 20310b57cec5SDimitry Andric if (IEEELongDouble) 20320b57cec5SDimitry Andric CmdArgs.push_back("-mabi=ieeelongdouble"); 20330b57cec5SDimitry Andric 20340b57cec5SDimitry Andric ppc::FloatABI FloatABI = 20350b57cec5SDimitry Andric ppc::getPPCFloatABI(getToolChain().getDriver(), Args); 20360b57cec5SDimitry Andric 20370b57cec5SDimitry Andric if (FloatABI == ppc::FloatABI::Soft) { 20380b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 20390b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 20400b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20410b57cec5SDimitry Andric CmdArgs.push_back("soft"); 20420b57cec5SDimitry Andric } else { 20430b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 20440b57cec5SDimitry Andric assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!"); 20450b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 20460b57cec5SDimitry Andric CmdArgs.push_back("hard"); 20470b57cec5SDimitry Andric } 20480b57cec5SDimitry Andric 20490b57cec5SDimitry Andric if (ABIName) { 20500b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 20510b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 20520b57cec5SDimitry Andric } 20530b57cec5SDimitry Andric } 20540b57cec5SDimitry Andric 20555ffd83dbSDimitry Andric static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args, 20565ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 20575ffd83dbSDimitry Andric const Driver &D = TC.getDriver(); 20585ffd83dbSDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 20595ffd83dbSDimitry Andric // Default small data limitation is eight. 20605ffd83dbSDimitry Andric const char *SmallDataLimit = "8"; 20615ffd83dbSDimitry Andric // Get small data limitation. 20625ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_shared, options::OPT_fpic, 20635ffd83dbSDimitry Andric options::OPT_fPIC)) { 20645ffd83dbSDimitry Andric // Not support linker relaxation for PIC. 20655ffd83dbSDimitry Andric SmallDataLimit = "0"; 20665ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 20675ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 20685ffd83dbSDimitry Andric } 20695ffd83dbSDimitry Andric } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ) 2070fe6060f1SDimitry Andric .equals_insensitive("large") && 20715ffd83dbSDimitry Andric (Triple.getArch() == llvm::Triple::riscv64)) { 20725ffd83dbSDimitry Andric // Not support linker relaxation for RV64 with large code model. 20735ffd83dbSDimitry Andric SmallDataLimit = "0"; 20745ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 20755ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 20765ffd83dbSDimitry Andric } 20775ffd83dbSDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_G)) { 20785ffd83dbSDimitry Andric SmallDataLimit = A->getValue(); 20795ffd83dbSDimitry Andric } 20805ffd83dbSDimitry Andric // Forward the -msmall-data-limit= option. 20815ffd83dbSDimitry Andric CmdArgs.push_back("-msmall-data-limit"); 20825ffd83dbSDimitry Andric CmdArgs.push_back(SmallDataLimit); 20835ffd83dbSDimitry Andric } 20845ffd83dbSDimitry Andric 20850b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args, 20860b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 20870b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 2088a7dea167SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 20890b57cec5SDimitry Andric 20900b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 2091a7dea167SDimitry Andric CmdArgs.push_back(ABIName.data()); 20925ffd83dbSDimitry Andric 20935ffd83dbSDimitry Andric SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs); 2094e8d8bef9SDimitry Andric 2095bdd1243dSDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 2096bdd1243dSDimitry Andric options::OPT_mno_implicit_float, true)) 2097bdd1243dSDimitry Andric CmdArgs.push_back("-no-implicit-float"); 2098bdd1243dSDimitry Andric 2099fcaf7f86SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 2100e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2101bdd1243dSDimitry Andric if (strcmp(A->getValue(), "native") == 0) 2102bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 2103bdd1243dSDimitry Andric else 2104bdd1243dSDimitry Andric CmdArgs.push_back(A->getValue()); 2105e8d8bef9SDimitry Andric } 21060b57cec5SDimitry Andric } 21070b57cec5SDimitry Andric 21080b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args, 21090b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21100b57cec5SDimitry Andric sparc::FloatABI FloatABI = 21110b57cec5SDimitry Andric sparc::getSparcFloatABI(getToolChain().getDriver(), Args); 21120b57cec5SDimitry Andric 21130b57cec5SDimitry Andric if (FloatABI == sparc::FloatABI::Soft) { 21140b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 21150b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 21160b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21170b57cec5SDimitry Andric CmdArgs.push_back("soft"); 21180b57cec5SDimitry Andric } else { 21190b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 21200b57cec5SDimitry Andric assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!"); 21210b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21220b57cec5SDimitry Andric CmdArgs.push_back("hard"); 21230b57cec5SDimitry Andric } 212461cfbce3SDimitry Andric 212561cfbce3SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 212661cfbce3SDimitry Andric StringRef Name = A->getValue(); 212761cfbce3SDimitry Andric std::string TuneCPU; 212861cfbce3SDimitry Andric if (Name == "native") 212961cfbce3SDimitry Andric TuneCPU = std::string(llvm::sys::getHostCPUName()); 213061cfbce3SDimitry Andric else 213161cfbce3SDimitry Andric TuneCPU = std::string(Name); 213261cfbce3SDimitry Andric 213361cfbce3SDimitry Andric CmdArgs.push_back("-tune-cpu"); 213461cfbce3SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 213561cfbce3SDimitry Andric } 21360b57cec5SDimitry Andric } 21370b57cec5SDimitry Andric 21380b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args, 21390b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2140fcaf7f86SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) { 2141753f127fSDimitry Andric CmdArgs.push_back("-tune-cpu"); 2142fcaf7f86SDimitry Andric if (strcmp(A->getValue(), "native") == 0) 2143fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName())); 2144fcaf7f86SDimitry Andric else 2145fcaf7f86SDimitry Andric CmdArgs.push_back(A->getValue()); 2146753f127fSDimitry Andric } 2147753f127fSDimitry Andric 2148753f127fSDimitry Andric bool HasBackchain = 2149753f127fSDimitry Andric Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false); 2150480093f4SDimitry Andric bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack, 2151480093f4SDimitry Andric options::OPT_mno_packed_stack, false); 21525ffd83dbSDimitry Andric systemz::FloatABI FloatABI = 21535ffd83dbSDimitry Andric systemz::getSystemZFloatABI(getToolChain().getDriver(), Args); 21545ffd83dbSDimitry Andric bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft); 21555ffd83dbSDimitry Andric if (HasBackchain && HasPackedStack && !HasSoftFloat) { 2156480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 2157480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt) 21585ffd83dbSDimitry Andric << "-mpacked-stack -mbackchain -mhard-float"; 2159480093f4SDimitry Andric } 2160480093f4SDimitry Andric if (HasBackchain) 21610b57cec5SDimitry Andric CmdArgs.push_back("-mbackchain"); 2162480093f4SDimitry Andric if (HasPackedStack) 2163480093f4SDimitry Andric CmdArgs.push_back("-mpacked-stack"); 21645ffd83dbSDimitry Andric if (HasSoftFloat) { 21655ffd83dbSDimitry Andric // Floating point operations and argument passing are soft. 21665ffd83dbSDimitry Andric CmdArgs.push_back("-msoft-float"); 21675ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21685ffd83dbSDimitry Andric CmdArgs.push_back("soft"); 2169480093f4SDimitry Andric } 21700b57cec5SDimitry Andric } 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args, 21730b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2174480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 21755ffd83dbSDimitry Andric addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false); 2176480093f4SDimitry Andric 21770b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 21780b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 21790b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 21800b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 21810b57cec5SDimitry Andric 21820b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs, 21830b57cec5SDimitry Andric options::OPT_mno_tls_direct_seg_refs, true)) 21840b57cec5SDimitry Andric CmdArgs.push_back("-mno-tls-direct-seg-refs"); 21850b57cec5SDimitry Andric 21860b57cec5SDimitry Andric // Default to avoid implicit floating-point for kernel/kext code, but allow 21870b57cec5SDimitry Andric // that to be overridden with -mno-soft-float. 21880b57cec5SDimitry Andric bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 21890b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)); 21900b57cec5SDimitry Andric if (Arg *A = Args.getLastArg( 21910b57cec5SDimitry Andric options::OPT_msoft_float, options::OPT_mno_soft_float, 21920b57cec5SDimitry Andric options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) { 21930b57cec5SDimitry Andric const Option &O = A->getOption(); 21940b57cec5SDimitry Andric NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 21950b57cec5SDimitry Andric O.matches(options::OPT_msoft_float)); 21960b57cec5SDimitry Andric } 21970b57cec5SDimitry Andric if (NoImplicitFloat) 21980b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 21990b57cec5SDimitry Andric 22000b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 22010b57cec5SDimitry Andric StringRef Value = A->getValue(); 22020b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 22030b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22040b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 2205349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-inline-asm=" + Value)); 22060b57cec5SDimitry Andric } else { 2207480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2208bdd1243dSDimitry Andric << A->getSpelling() << Value; 22090b57cec5SDimitry Andric } 2210480093f4SDimitry Andric } else if (D.IsCLMode()) { 22110b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22120b57cec5SDimitry Andric CmdArgs.push_back("-x86-asm-syntax=intel"); 22130b57cec5SDimitry Andric } 22140b57cec5SDimitry Andric 2215349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mskip_rax_setup, 2216349cc55cSDimitry Andric options::OPT_mno_skip_rax_setup)) 2217349cc55cSDimitry Andric if (A->getOption().matches(options::OPT_mskip_rax_setup)) 2218349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mskip-rax-setup")); 2219349cc55cSDimitry Andric 22200b57cec5SDimitry Andric // Set flags to support MCU ABI. 22210b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { 22220b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22230b57cec5SDimitry Andric CmdArgs.push_back("soft"); 22240b57cec5SDimitry Andric CmdArgs.push_back("-mstack-alignment=4"); 22250b57cec5SDimitry Andric } 2226e8d8bef9SDimitry Andric 2227e8d8bef9SDimitry Andric // Handle -mtune. 2228e8d8bef9SDimitry Andric 222981ad6265SDimitry Andric // Default to "generic" unless -march is present or targetting the PS4/PS5. 2230e8d8bef9SDimitry Andric std::string TuneCPU; 2231e8d8bef9SDimitry Andric if (!Args.hasArg(clang::driver::options::OPT_march_EQ) && 223281ad6265SDimitry Andric !getToolChain().getTriple().isPS()) 2233e8d8bef9SDimitry Andric TuneCPU = "generic"; 2234e8d8bef9SDimitry Andric 2235e8d8bef9SDimitry Andric // Override based on -mtune. 2236e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2237e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2238e8d8bef9SDimitry Andric 2239e8d8bef9SDimitry Andric if (Name == "native") { 2240e8d8bef9SDimitry Andric Name = llvm::sys::getHostCPUName(); 2241e8d8bef9SDimitry Andric if (!Name.empty()) 2242e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2243e8d8bef9SDimitry Andric } else 2244e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2245e8d8bef9SDimitry Andric } 2246e8d8bef9SDimitry Andric 2247e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2248e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2249e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2250e8d8bef9SDimitry Andric } 22510b57cec5SDimitry Andric } 22520b57cec5SDimitry Andric 22530b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args, 22540b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22550b57cec5SDimitry Andric CmdArgs.push_back("-mqdsp6-compat"); 22560b57cec5SDimitry Andric CmdArgs.push_back("-Wreturn-type"); 22570b57cec5SDimitry Andric 22580b57cec5SDimitry Andric if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { 22590b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 2260bdd1243dSDimitry Andric CmdArgs.push_back( 2261bdd1243dSDimitry Andric Args.MakeArgString("-hexagon-small-data-threshold=" + Twine(*G))); 22620b57cec5SDimitry Andric } 22630b57cec5SDimitry Andric 22640b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_short_enums)) 22650b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 22660b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 22670b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22680b57cec5SDimitry Andric CmdArgs.push_back("-enable-hexagon-ieee-rnd-near"); 22690b57cec5SDimitry Andric } 22700b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22710b57cec5SDimitry Andric CmdArgs.push_back("-machine-sink-split=0"); 22720b57cec5SDimitry Andric } 22730b57cec5SDimitry Andric 22740b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args, 22750b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22760b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 22770b57cec5SDimitry Andric StringRef CPUName = A->getValue(); 22780b57cec5SDimitry Andric 22790b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 22800b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPUName)); 22810b57cec5SDimitry Andric } 22820b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 22830b57cec5SDimitry Andric StringRef Value = A->getValue(); 22840b57cec5SDimitry Andric // Only support mregparm=4 to support old usage. Report error for all other 22850b57cec5SDimitry Andric // cases. 22860b57cec5SDimitry Andric int Mregparm; 22870b57cec5SDimitry Andric if (Value.getAsInteger(10, Mregparm)) { 22880b57cec5SDimitry Andric if (Mregparm != 4) { 22890b57cec5SDimitry Andric getToolChain().getDriver().Diag( 22900b57cec5SDimitry Andric diag::err_drv_unsupported_option_argument) 2291bdd1243dSDimitry Andric << A->getSpelling() << Value; 22920b57cec5SDimitry Andric } 22930b57cec5SDimitry Andric } 22940b57cec5SDimitry Andric } 22950b57cec5SDimitry Andric } 22960b57cec5SDimitry Andric 22970b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args, 22980b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 22990b57cec5SDimitry Andric // Default to "hidden" visibility. 23000b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fvisibility_EQ, 2301bdd1243dSDimitry Andric options::OPT_fvisibility_ms_compat)) 2302bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=hidden"); 23030b57cec5SDimitry Andric } 23040b57cec5SDimitry Andric 23055ffd83dbSDimitry Andric void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 23065ffd83dbSDimitry Andric // Floating point operations and argument passing are hard. 23075ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 23085ffd83dbSDimitry Andric CmdArgs.push_back("hard"); 23095ffd83dbSDimitry Andric } 23105ffd83dbSDimitry Andric 23110b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, 23120b57cec5SDimitry Andric StringRef Target, const InputInfo &Output, 23130b57cec5SDimitry Andric const InputInfo &Input, const ArgList &Args) const { 23140b57cec5SDimitry Andric // If this is a dry run, do not create the compilation database file. 23150b57cec5SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 23160b57cec5SDimitry Andric return; 23170b57cec5SDimitry Andric 23180b57cec5SDimitry Andric using llvm::yaml::escape; 23190b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 23200b57cec5SDimitry Andric 23210b57cec5SDimitry Andric if (!CompilationDatabase) { 23220b57cec5SDimitry Andric std::error_code EC; 2323fe6060f1SDimitry Andric auto File = std::make_unique<llvm::raw_fd_ostream>( 232481ad6265SDimitry Andric Filename, EC, 232581ad6265SDimitry Andric llvm::sys::fs::OF_TextWithCRLF | llvm::sys::fs::OF_Append); 23260b57cec5SDimitry Andric if (EC) { 23270b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_compilationdatabase) << Filename 23280b57cec5SDimitry Andric << EC.message(); 23290b57cec5SDimitry Andric return; 23300b57cec5SDimitry Andric } 23310b57cec5SDimitry Andric CompilationDatabase = std::move(File); 23320b57cec5SDimitry Andric } 23330b57cec5SDimitry Andric auto &CDB = *CompilationDatabase; 2334a7dea167SDimitry Andric auto CWD = D.getVFS().getCurrentWorkingDirectory(); 2335a7dea167SDimitry Andric if (!CWD) 2336a7dea167SDimitry Andric CWD = "."; 2337a7dea167SDimitry Andric CDB << "{ \"directory\": \"" << escape(*CWD) << "\""; 23380b57cec5SDimitry Andric CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\""; 23390b57cec5SDimitry Andric CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; 23400b57cec5SDimitry Andric CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\""; 2341a7dea167SDimitry Andric SmallString<128> Buf; 23420b57cec5SDimitry Andric Buf = "-x"; 23430b57cec5SDimitry Andric Buf += types::getTypeName(Input.getType()); 23440b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 23450b57cec5SDimitry Andric if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) { 23460b57cec5SDimitry Andric Buf = "--sysroot="; 23470b57cec5SDimitry Andric Buf += D.SysRoot; 23480b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 23490b57cec5SDimitry Andric } 23500b57cec5SDimitry Andric CDB << ", \"" << escape(Input.getFilename()) << "\""; 235181ad6265SDimitry Andric CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\""; 23520b57cec5SDimitry Andric for (auto &A: Args) { 23530b57cec5SDimitry Andric auto &O = A->getOption(); 23540b57cec5SDimitry Andric // Skip language selection, which is positional. 23550b57cec5SDimitry Andric if (O.getID() == options::OPT_x) 23560b57cec5SDimitry Andric continue; 23570b57cec5SDimitry Andric // Skip writing dependency output and the compilation database itself. 23580b57cec5SDimitry Andric if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group) 23590b57cec5SDimitry Andric continue; 2360a7dea167SDimitry Andric if (O.getID() == options::OPT_gen_cdb_fragment_path) 2361a7dea167SDimitry Andric continue; 23620b57cec5SDimitry Andric // Skip inputs. 23630b57cec5SDimitry Andric if (O.getKind() == Option::InputClass) 23640b57cec5SDimitry Andric continue; 236581ad6265SDimitry Andric // Skip output. 236681ad6265SDimitry Andric if (O.getID() == options::OPT_o) 236781ad6265SDimitry Andric continue; 23680b57cec5SDimitry Andric // All other arguments are quoted and appended. 23690b57cec5SDimitry Andric ArgStringList ASL; 23700b57cec5SDimitry Andric A->render(Args, ASL); 23710b57cec5SDimitry Andric for (auto &it: ASL) 23720b57cec5SDimitry Andric CDB << ", \"" << escape(it) << "\""; 23730b57cec5SDimitry Andric } 23740b57cec5SDimitry Andric Buf = "--target="; 23750b57cec5SDimitry Andric Buf += Target; 23760b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\"]},\n"; 23770b57cec5SDimitry Andric } 23780b57cec5SDimitry Andric 2379a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir( 2380a7dea167SDimitry Andric StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output, 2381a7dea167SDimitry Andric const InputInfo &Input, const llvm::opt::ArgList &Args) const { 2382a7dea167SDimitry Andric // If this is a dry run, do not create the compilation database file. 2383a7dea167SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 2384a7dea167SDimitry Andric return; 2385a7dea167SDimitry Andric 2386a7dea167SDimitry Andric if (CompilationDatabase) 2387a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2388a7dea167SDimitry Andric 2389a7dea167SDimitry Andric SmallString<256> Path = Dir; 2390a7dea167SDimitry Andric const auto &Driver = C.getDriver(); 2391a7dea167SDimitry Andric Driver.getVFS().makeAbsolute(Path); 2392a7dea167SDimitry Andric auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true); 2393a7dea167SDimitry Andric if (Err) { 2394a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message(); 2395a7dea167SDimitry Andric return; 2396a7dea167SDimitry Andric } 2397a7dea167SDimitry Andric 2398a7dea167SDimitry Andric llvm::sys::path::append( 2399a7dea167SDimitry Andric Path, 2400a7dea167SDimitry Andric Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json"); 2401a7dea167SDimitry Andric int FD; 2402a7dea167SDimitry Andric SmallString<256> TempPath; 2403fe6060f1SDimitry Andric Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath, 2404fe6060f1SDimitry Andric llvm::sys::fs::OF_Text); 2405a7dea167SDimitry Andric if (Err) { 2406a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message(); 2407a7dea167SDimitry Andric return; 2408a7dea167SDimitry Andric } 2409a7dea167SDimitry Andric CompilationDatabase = 2410a7dea167SDimitry Andric std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true); 2411a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2412a7dea167SDimitry Andric } 2413a7dea167SDimitry Andric 2414fe6060f1SDimitry Andric static bool CheckARMImplicitITArg(StringRef Value) { 2415fe6060f1SDimitry Andric return Value == "always" || Value == "never" || Value == "arm" || 2416fe6060f1SDimitry Andric Value == "thumb"; 2417fe6060f1SDimitry Andric } 2418fe6060f1SDimitry Andric 2419fe6060f1SDimitry Andric static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs, 2420fe6060f1SDimitry Andric StringRef Value) { 2421fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 2422fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); 2423fe6060f1SDimitry Andric } 2424fe6060f1SDimitry Andric 24250b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C, 24260b57cec5SDimitry Andric const ArgList &Args, 24270b57cec5SDimitry Andric ArgStringList &CmdArgs, 24280b57cec5SDimitry Andric const Driver &D) { 24290b57cec5SDimitry Andric if (UseRelaxAll(C, Args)) 24300b57cec5SDimitry Andric CmdArgs.push_back("-mrelax-all"); 24310b57cec5SDimitry Andric 24320b57cec5SDimitry Andric // Only default to -mincremental-linker-compatible if we think we are 24330b57cec5SDimitry Andric // targeting the MSVC linker. 24340b57cec5SDimitry Andric bool DefaultIncrementalLinkerCompatible = 24350b57cec5SDimitry Andric C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); 24360b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mincremental_linker_compatible, 24370b57cec5SDimitry Andric options::OPT_mno_incremental_linker_compatible, 24380b57cec5SDimitry Andric DefaultIncrementalLinkerCompatible)) 24390b57cec5SDimitry Andric CmdArgs.push_back("-mincremental-linker-compatible"); 24400b57cec5SDimitry Andric 244181ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_dwarf_unwind_EQ); 244281ad6265SDimitry Andric 2443a7dea167SDimitry Andric // If you add more args here, also add them to the block below that 2444a7dea167SDimitry Andric // starts with "// If CollectArgsForIntegratedAssembler() isn't called below". 2445a7dea167SDimitry Andric 24460b57cec5SDimitry Andric // When passing -I arguments to the assembler we sometimes need to 24470b57cec5SDimitry Andric // unconditionally take the next argument. For example, when parsing 24480b57cec5SDimitry Andric // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the 24490b57cec5SDimitry Andric // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo' 24500b57cec5SDimitry Andric // arg after parsing the '-I' arg. 24510b57cec5SDimitry Andric bool TakeNextArg = false; 24520b57cec5SDimitry Andric 24530b57cec5SDimitry Andric bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations(); 2454349cc55cSDimitry Andric bool UseNoExecStack = false; 24550b57cec5SDimitry Andric const char *MipsTargetFeature = nullptr; 2456fe6060f1SDimitry Andric StringRef ImplicitIt; 24570b57cec5SDimitry Andric for (const Arg *A : 2458fe6060f1SDimitry Andric Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler, 2459fe6060f1SDimitry Andric options::OPT_mimplicit_it_EQ)) { 24600b57cec5SDimitry Andric A->claim(); 24610b57cec5SDimitry Andric 2462fe6060f1SDimitry Andric if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) { 2463fe6060f1SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 2464fe6060f1SDimitry Andric case llvm::Triple::arm: 2465fe6060f1SDimitry Andric case llvm::Triple::armeb: 2466fe6060f1SDimitry Andric case llvm::Triple::thumb: 2467fe6060f1SDimitry Andric case llvm::Triple::thumbeb: 2468fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2469fe6060f1SDimitry Andric ImplicitIt = A->getValue(); 2470fe6060f1SDimitry Andric if (!CheckARMImplicitITArg(ImplicitIt)) 2471fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2472bdd1243dSDimitry Andric << A->getSpelling() << ImplicitIt; 2473fe6060f1SDimitry Andric continue; 2474fe6060f1SDimitry Andric default: 2475fe6060f1SDimitry Andric break; 2476fe6060f1SDimitry Andric } 2477fe6060f1SDimitry Andric } 2478fe6060f1SDimitry Andric 24790b57cec5SDimitry Andric for (StringRef Value : A->getValues()) { 24800b57cec5SDimitry Andric if (TakeNextArg) { 24810b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 24820b57cec5SDimitry Andric TakeNextArg = false; 24830b57cec5SDimitry Andric continue; 24840b57cec5SDimitry Andric } 24850b57cec5SDimitry Andric 24860b57cec5SDimitry Andric if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() && 24870b57cec5SDimitry Andric Value == "-mbig-obj") 24880b57cec5SDimitry Andric continue; // LLVM handles bigobj automatically 24890b57cec5SDimitry Andric 24900b57cec5SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 24910b57cec5SDimitry Andric default: 24920b57cec5SDimitry Andric break; 2493bdd1243dSDimitry Andric case llvm::Triple::wasm32: 2494bdd1243dSDimitry Andric case llvm::Triple::wasm64: 2495bdd1243dSDimitry Andric if (Value == "--no-type-check") { 2496bdd1243dSDimitry Andric CmdArgs.push_back("-mno-type-check"); 2497bdd1243dSDimitry Andric continue; 2498bdd1243dSDimitry Andric } 2499bdd1243dSDimitry Andric break; 25000b57cec5SDimitry Andric case llvm::Triple::thumb: 25010b57cec5SDimitry Andric case llvm::Triple::thumbeb: 25020b57cec5SDimitry Andric case llvm::Triple::arm: 25030b57cec5SDimitry Andric case llvm::Triple::armeb: 2504fe6060f1SDimitry Andric if (Value.startswith("-mimplicit-it=")) { 2505fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2506fe6060f1SDimitry Andric ImplicitIt = Value.split("=").second; 2507fe6060f1SDimitry Andric if (CheckARMImplicitITArg(ImplicitIt)) 2508fe6060f1SDimitry Andric continue; 2509fe6060f1SDimitry Andric } 25100b57cec5SDimitry Andric if (Value == "-mthumb") 25110b57cec5SDimitry Andric // -mthumb has already been processed in ComputeLLVMTriple() 25120b57cec5SDimitry Andric // recognize but skip over here. 25130b57cec5SDimitry Andric continue; 25140b57cec5SDimitry Andric break; 25150b57cec5SDimitry Andric case llvm::Triple::mips: 25160b57cec5SDimitry Andric case llvm::Triple::mipsel: 25170b57cec5SDimitry Andric case llvm::Triple::mips64: 25180b57cec5SDimitry Andric case llvm::Triple::mips64el: 25190b57cec5SDimitry Andric if (Value == "--trap") { 25200b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25210b57cec5SDimitry Andric CmdArgs.push_back("+use-tcc-in-div"); 25220b57cec5SDimitry Andric continue; 25230b57cec5SDimitry Andric } 25240b57cec5SDimitry Andric if (Value == "--break") { 25250b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25260b57cec5SDimitry Andric CmdArgs.push_back("-use-tcc-in-div"); 25270b57cec5SDimitry Andric continue; 25280b57cec5SDimitry Andric } 25290b57cec5SDimitry Andric if (Value.startswith("-msoft-float")) { 25300b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25310b57cec5SDimitry Andric CmdArgs.push_back("+soft-float"); 25320b57cec5SDimitry Andric continue; 25330b57cec5SDimitry Andric } 25340b57cec5SDimitry Andric if (Value.startswith("-mhard-float")) { 25350b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25360b57cec5SDimitry Andric CmdArgs.push_back("-soft-float"); 25370b57cec5SDimitry Andric continue; 25380b57cec5SDimitry Andric } 25390b57cec5SDimitry Andric 25400b57cec5SDimitry Andric MipsTargetFeature = llvm::StringSwitch<const char *>(Value) 25410b57cec5SDimitry Andric .Case("-mips1", "+mips1") 25420b57cec5SDimitry Andric .Case("-mips2", "+mips2") 25430b57cec5SDimitry Andric .Case("-mips3", "+mips3") 25440b57cec5SDimitry Andric .Case("-mips4", "+mips4") 25450b57cec5SDimitry Andric .Case("-mips5", "+mips5") 25460b57cec5SDimitry Andric .Case("-mips32", "+mips32") 25470b57cec5SDimitry Andric .Case("-mips32r2", "+mips32r2") 25480b57cec5SDimitry Andric .Case("-mips32r3", "+mips32r3") 25490b57cec5SDimitry Andric .Case("-mips32r5", "+mips32r5") 25500b57cec5SDimitry Andric .Case("-mips32r6", "+mips32r6") 25510b57cec5SDimitry Andric .Case("-mips64", "+mips64") 25520b57cec5SDimitry Andric .Case("-mips64r2", "+mips64r2") 25530b57cec5SDimitry Andric .Case("-mips64r3", "+mips64r3") 25540b57cec5SDimitry Andric .Case("-mips64r5", "+mips64r5") 25550b57cec5SDimitry Andric .Case("-mips64r6", "+mips64r6") 25560b57cec5SDimitry Andric .Default(nullptr); 25570b57cec5SDimitry Andric if (MipsTargetFeature) 25580b57cec5SDimitry Andric continue; 25590b57cec5SDimitry Andric } 25600b57cec5SDimitry Andric 25610b57cec5SDimitry Andric if (Value == "-force_cpusubtype_ALL") { 25620b57cec5SDimitry Andric // Do nothing, this is the default and we don't support anything else. 25630b57cec5SDimitry Andric } else if (Value == "-L") { 25640b57cec5SDimitry Andric CmdArgs.push_back("-msave-temp-labels"); 25650b57cec5SDimitry Andric } else if (Value == "--fatal-warnings") { 25660b57cec5SDimitry Andric CmdArgs.push_back("-massembler-fatal-warnings"); 2567a7dea167SDimitry Andric } else if (Value == "--no-warn" || Value == "-W") { 2568a7dea167SDimitry Andric CmdArgs.push_back("-massembler-no-warn"); 25690b57cec5SDimitry Andric } else if (Value == "--noexecstack") { 25700b57cec5SDimitry Andric UseNoExecStack = true; 25710b57cec5SDimitry Andric } else if (Value.startswith("-compress-debug-sections") || 25720b57cec5SDimitry Andric Value.startswith("--compress-debug-sections") || 25730b57cec5SDimitry Andric Value == "-nocompress-debug-sections" || 25740b57cec5SDimitry Andric Value == "--nocompress-debug-sections") { 25750b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25760b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=yes" || 25770b57cec5SDimitry Andric Value == "--mrelax-relocations=yes") { 25780b57cec5SDimitry Andric UseRelaxRelocations = true; 25790b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=no" || 25800b57cec5SDimitry Andric Value == "--mrelax-relocations=no") { 25810b57cec5SDimitry Andric UseRelaxRelocations = false; 25820b57cec5SDimitry Andric } else if (Value.startswith("-I")) { 25830b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25840b57cec5SDimitry Andric // We need to consume the next argument if the current arg is a plain 25850b57cec5SDimitry Andric // -I. The next arg will be the include directory. 25860b57cec5SDimitry Andric if (Value == "-I") 25870b57cec5SDimitry Andric TakeNextArg = true; 25880b57cec5SDimitry Andric } else if (Value.startswith("-gdwarf-")) { 25890b57cec5SDimitry Andric // "-gdwarf-N" options are not cc1as options. 25900b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNum(Value); 25910b57cec5SDimitry Andric if (DwarfVersion == 0) { // Send it onward, and let cc1as complain. 25920b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25930b57cec5SDimitry Andric } else { 25940b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, 2595fe6060f1SDimitry Andric codegenoptions::DebugInfoConstructor, 25960b57cec5SDimitry Andric DwarfVersion, llvm::DebuggerKind::Default); 25970b57cec5SDimitry Andric } 25980b57cec5SDimitry Andric } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") || 25990b57cec5SDimitry Andric Value.startswith("-mhwdiv") || Value.startswith("-march")) { 26000b57cec5SDimitry Andric // Do nothing, we'll validate it later. 26010b57cec5SDimitry Andric } else if (Value == "-defsym") { 26020b57cec5SDimitry Andric if (A->getNumValues() != 2) { 26030b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << Value; 26040b57cec5SDimitry Andric break; 26050b57cec5SDimitry Andric } 26060b57cec5SDimitry Andric const char *S = A->getValue(1); 26070b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 26080b57cec5SDimitry Andric auto Sym = Pair.first; 26090b57cec5SDimitry Andric auto SVal = Pair.second; 26100b57cec5SDimitry Andric 26110b57cec5SDimitry Andric if (Sym.empty() || SVal.empty()) { 26120b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << S; 26130b57cec5SDimitry Andric break; 26140b57cec5SDimitry Andric } 26150b57cec5SDimitry Andric int64_t IVal; 26160b57cec5SDimitry Andric if (SVal.getAsInteger(0, IVal)) { 26170b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; 26180b57cec5SDimitry Andric break; 26190b57cec5SDimitry Andric } 26200b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26210b57cec5SDimitry Andric TakeNextArg = true; 26220b57cec5SDimitry Andric } else if (Value == "-fdebug-compilation-dir") { 26230b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 26240b57cec5SDimitry Andric TakeNextArg = true; 2625480093f4SDimitry Andric } else if (Value.consume_front("-fdebug-compilation-dir=")) { 2626480093f4SDimitry Andric // The flag is a -Wa / -Xassembler argument and Options doesn't 2627480093f4SDimitry Andric // parse the argument, so this isn't automatically aliased to 2628480093f4SDimitry Andric // -fdebug-compilation-dir (without '=') here. 2629480093f4SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 2630480093f4SDimitry Andric CmdArgs.push_back(Value.data()); 2631fe6060f1SDimitry Andric } else if (Value == "--version") { 2632fe6060f1SDimitry Andric D.PrintVersion(C, llvm::outs()); 26330b57cec5SDimitry Andric } else { 26340b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2635bdd1243dSDimitry Andric << A->getSpelling() << Value; 26360b57cec5SDimitry Andric } 26370b57cec5SDimitry Andric } 26380b57cec5SDimitry Andric } 2639fe6060f1SDimitry Andric if (ImplicitIt.size()) 2640fe6060f1SDimitry Andric AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt); 2641bdd1243dSDimitry Andric if (!UseRelaxRelocations) 2642bdd1243dSDimitry Andric CmdArgs.push_back("-mrelax-relocations=no"); 26430b57cec5SDimitry Andric if (UseNoExecStack) 26440b57cec5SDimitry Andric CmdArgs.push_back("-mnoexecstack"); 26450b57cec5SDimitry Andric if (MipsTargetFeature != nullptr) { 26460b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26470b57cec5SDimitry Andric CmdArgs.push_back(MipsTargetFeature); 26480b57cec5SDimitry Andric } 26490b57cec5SDimitry Andric 26500b57cec5SDimitry Andric // forward -fembed-bitcode to assmebler 26510b57cec5SDimitry Andric if (C.getDriver().embedBitcodeEnabled() || 26520b57cec5SDimitry Andric C.getDriver().embedBitcodeMarkerOnly()) 26530b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 2654bdd1243dSDimitry Andric 2655bdd1243dSDimitry Andric if (const char *AsSecureLogFile = getenv("AS_SECURE_LOG_FILE")) { 2656bdd1243dSDimitry Andric CmdArgs.push_back("-as-secure-log-file"); 2657bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString(AsSecureLogFile)); 2658bdd1243dSDimitry Andric } 26590b57cec5SDimitry Andric } 26600b57cec5SDimitry Andric 26610b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, 26620b57cec5SDimitry Andric bool OFastEnabled, const ArgList &Args, 26635ffd83dbSDimitry Andric ArgStringList &CmdArgs, 26645ffd83dbSDimitry Andric const JobAction &JA) { 26650b57cec5SDimitry Andric // Handle various floating point optimization flags, mapping them to the 26660b57cec5SDimitry Andric // appropriate LLVM code generation flags. This is complicated by several 26670b57cec5SDimitry Andric // "umbrella" flags, so we do this by stepping through the flags incrementally 26680b57cec5SDimitry Andric // adjusting what we think is enabled/disabled, then at the end setting the 26690b57cec5SDimitry Andric // LLVM flags based on the final state. 26700b57cec5SDimitry Andric bool HonorINFs = true; 26710b57cec5SDimitry Andric bool HonorNaNs = true; 2672349cc55cSDimitry Andric bool ApproxFunc = false; 26730b57cec5SDimitry Andric // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 26740b57cec5SDimitry Andric bool MathErrno = TC.IsMathErrnoDefault(); 26750b57cec5SDimitry Andric bool AssociativeMath = false; 26760b57cec5SDimitry Andric bool ReciprocalMath = false; 26770b57cec5SDimitry Andric bool SignedZeros = true; 2678480093f4SDimitry Andric bool TrappingMath = false; // Implemented via -ffp-exception-behavior 2679480093f4SDimitry Andric bool TrappingMathPresent = false; // Is trapping-math in args, and not 2680480093f4SDimitry Andric // overriden by ffp-exception-behavior? 2681480093f4SDimitry Andric bool RoundingFPMath = false; 2682480093f4SDimitry Andric bool RoundingMathPresent = false; // Is rounding-math in args? 2683480093f4SDimitry Andric // -ffp-model values: strict, fast, precise 2684480093f4SDimitry Andric StringRef FPModel = ""; 2685480093f4SDimitry Andric // -ffp-exception-behavior options: strict, maytrap, ignore 2686480093f4SDimitry Andric StringRef FPExceptionBehavior = ""; 268781ad6265SDimitry Andric // -ffp-eval-method options: double, extended, source 268881ad6265SDimitry Andric StringRef FPEvalMethod = ""; 26895ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFPMath = 26905ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA); 26915ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFP32Math = 26925ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle()); 26935ffd83dbSDimitry Andric 26945ffd83dbSDimitry Andric llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath; 26955ffd83dbSDimitry Andric llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math; 2696349cc55cSDimitry Andric // CUDA and HIP don't rely on the frontend to pass an ffp-contract option. 2697349cc55cSDimitry Andric // If one wasn't given by the user, don't pass it here. 2698349cc55cSDimitry Andric StringRef FPContract; 2699bdd1243dSDimitry Andric StringRef LastSeenFfpContractOption; 2700bdd1243dSDimitry Andric bool SeenUnsafeMathModeOption = false; 2701349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2702349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 2703349cc55cSDimitry Andric FPContract = "on"; 2704480093f4SDimitry Andric bool StrictFPModel = false; 2705bdd1243dSDimitry Andric StringRef Float16ExcessPrecision = ""; 27060b57cec5SDimitry Andric 27070b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 27080b57cec5SDimitry Andric CmdArgs.push_back("-mlimit-float-precision"); 27090b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 27100b57cec5SDimitry Andric } 27110b57cec5SDimitry Andric 27120b57cec5SDimitry Andric for (const Arg *A : Args) { 2713480093f4SDimitry Andric auto optID = A->getOption().getID(); 2714480093f4SDimitry Andric bool PreciseFPModel = false; 2715480093f4SDimitry Andric switch (optID) { 2716480093f4SDimitry Andric default: 2717480093f4SDimitry Andric break; 2718480093f4SDimitry Andric case options::OPT_ffp_model_EQ: { 2719480093f4SDimitry Andric // If -ffp-model= is seen, reset to fno-fast-math 2720480093f4SDimitry Andric HonorINFs = true; 2721480093f4SDimitry Andric HonorNaNs = true; 2722bdd1243dSDimitry Andric ApproxFunc = false; 2723480093f4SDimitry Andric // Turning *off* -ffast-math restores the toolchain default. 2724480093f4SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 2725480093f4SDimitry Andric AssociativeMath = false; 2726480093f4SDimitry Andric ReciprocalMath = false; 2727480093f4SDimitry Andric SignedZeros = true; 2728480093f4SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 2729349cc55cSDimitry Andric FPContract = "on"; 27305ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 27315ffd83dbSDimitry Andric 27325ffd83dbSDimitry Andric // FIXME: The target may have picked a non-IEEE default mode here based on 27335ffd83dbSDimitry Andric // -cl-denorms-are-zero. Should the target consider -fp-model interaction? 27345ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 27355ffd83dbSDimitry Andric 2736480093f4SDimitry Andric StringRef Val = A->getValue(); 2737480093f4SDimitry Andric if (OFastEnabled && !Val.equals("fast")) { 2738480093f4SDimitry Andric // Only -ffp-model=fast is compatible with OFast, ignore. 2739480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2740480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val) 2741480093f4SDimitry Andric << "-Ofast"; 2742480093f4SDimitry Andric break; 2743480093f4SDimitry Andric } 2744480093f4SDimitry Andric StrictFPModel = false; 2745480093f4SDimitry Andric PreciseFPModel = true; 2746480093f4SDimitry Andric // ffp-model= is a Driver option, it is entirely rewritten into more 2747480093f4SDimitry Andric // granular options before being passed into cc1. 2748480093f4SDimitry Andric // Use the gcc option in the switch below. 2749349cc55cSDimitry Andric if (!FPModel.empty() && !FPModel.equals(Val)) 2750480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2751480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + FPModel) 2752480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val); 2753480093f4SDimitry Andric if (Val.equals("fast")) { 2754480093f4SDimitry Andric optID = options::OPT_ffast_math; 2755480093f4SDimitry Andric FPModel = Val; 27566e75b2fbSDimitry Andric FPContract = "fast"; 2757480093f4SDimitry Andric } else if (Val.equals("precise")) { 2758480093f4SDimitry Andric optID = options::OPT_ffp_contract; 2759480093f4SDimitry Andric FPModel = Val; 2760349cc55cSDimitry Andric FPContract = "on"; 2761480093f4SDimitry Andric PreciseFPModel = true; 2762480093f4SDimitry Andric } else if (Val.equals("strict")) { 2763480093f4SDimitry Andric StrictFPModel = true; 2764480093f4SDimitry Andric optID = options::OPT_frounding_math; 2765480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2766480093f4SDimitry Andric FPModel = Val; 27675ffd83dbSDimitry Andric FPContract = "off"; 2768480093f4SDimitry Andric TrappingMath = true; 2769480093f4SDimitry Andric } else 2770480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2771bdd1243dSDimitry Andric << A->getSpelling() << Val; 2772480093f4SDimitry Andric break; 2773480093f4SDimitry Andric } 2774480093f4SDimitry Andric } 2775480093f4SDimitry Andric 2776480093f4SDimitry Andric switch (optID) { 27770b57cec5SDimitry Andric // If this isn't an FP option skip the claim below 27780b57cec5SDimitry Andric default: continue; 27790b57cec5SDimitry Andric 27800b57cec5SDimitry Andric // Options controlling individual features 27810b57cec5SDimitry Andric case options::OPT_fhonor_infinities: HonorINFs = true; break; 27820b57cec5SDimitry Andric case options::OPT_fno_honor_infinities: HonorINFs = false; break; 27830b57cec5SDimitry Andric case options::OPT_fhonor_nans: HonorNaNs = true; break; 27840b57cec5SDimitry Andric case options::OPT_fno_honor_nans: HonorNaNs = false; break; 2785349cc55cSDimitry Andric case options::OPT_fapprox_func: ApproxFunc = true; break; 2786349cc55cSDimitry Andric case options::OPT_fno_approx_func: ApproxFunc = false; break; 27870b57cec5SDimitry Andric case options::OPT_fmath_errno: MathErrno = true; break; 27880b57cec5SDimitry Andric case options::OPT_fno_math_errno: MathErrno = false; break; 27890b57cec5SDimitry Andric case options::OPT_fassociative_math: AssociativeMath = true; break; 27900b57cec5SDimitry Andric case options::OPT_fno_associative_math: AssociativeMath = false; break; 27910b57cec5SDimitry Andric case options::OPT_freciprocal_math: ReciprocalMath = true; break; 27920b57cec5SDimitry Andric case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break; 27930b57cec5SDimitry Andric case options::OPT_fsigned_zeros: SignedZeros = true; break; 27940b57cec5SDimitry Andric case options::OPT_fno_signed_zeros: SignedZeros = false; break; 2795480093f4SDimitry Andric case options::OPT_ftrapping_math: 2796480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2797480093f4SDimitry Andric !FPExceptionBehavior.equals("strict")) 2798480093f4SDimitry Andric // Warn that previous value of option is overridden. 2799480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2800480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2801480093f4SDimitry Andric << "-ftrapping-math"; 2802480093f4SDimitry Andric TrappingMath = true; 2803480093f4SDimitry Andric TrappingMathPresent = true; 2804480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2805480093f4SDimitry Andric break; 2806480093f4SDimitry Andric case options::OPT_fno_trapping_math: 2807480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2808480093f4SDimitry Andric !FPExceptionBehavior.equals("ignore")) 2809480093f4SDimitry Andric // Warn that previous value of option is overridden. 2810480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2811480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2812480093f4SDimitry Andric << "-fno-trapping-math"; 2813480093f4SDimitry Andric TrappingMath = false; 2814480093f4SDimitry Andric TrappingMathPresent = true; 2815480093f4SDimitry Andric FPExceptionBehavior = "ignore"; 2816480093f4SDimitry Andric break; 2817480093f4SDimitry Andric 2818480093f4SDimitry Andric case options::OPT_frounding_math: 2819480093f4SDimitry Andric RoundingFPMath = true; 2820480093f4SDimitry Andric RoundingMathPresent = true; 2821480093f4SDimitry Andric break; 2822480093f4SDimitry Andric 2823480093f4SDimitry Andric case options::OPT_fno_rounding_math: 2824480093f4SDimitry Andric RoundingFPMath = false; 2825480093f4SDimitry Andric RoundingMathPresent = false; 2826480093f4SDimitry Andric break; 28270b57cec5SDimitry Andric 28280b57cec5SDimitry Andric case options::OPT_fdenormal_fp_math_EQ: 28295ffd83dbSDimitry Andric DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue()); 283081ad6265SDimitry Andric DenormalFP32Math = DenormalFPMath; 28315ffd83dbSDimitry Andric if (!DenormalFPMath.isValid()) { 28325ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 28335ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 28345ffd83dbSDimitry Andric } 28355ffd83dbSDimitry Andric break; 28365ffd83dbSDimitry Andric 28375ffd83dbSDimitry Andric case options::OPT_fdenormal_fp_math_f32_EQ: 28385ffd83dbSDimitry Andric DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue()); 28395ffd83dbSDimitry Andric if (!DenormalFP32Math.isValid()) { 28405ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 28415ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 28425ffd83dbSDimitry Andric } 28430b57cec5SDimitry Andric break; 28440b57cec5SDimitry Andric 2845480093f4SDimitry Andric // Validate and pass through -ffp-contract option. 28460b57cec5SDimitry Andric case options::OPT_ffp_contract: { 28470b57cec5SDimitry Andric StringRef Val = A->getValue(); 2848480093f4SDimitry Andric if (PreciseFPModel) { 2849349cc55cSDimitry Andric // -ffp-model=precise enables ffp-contract=on. 2850349cc55cSDimitry Andric // -ffp-model=precise sets PreciseFPModel to on and Val to 2851349cc55cSDimitry Andric // "precise". FPContract is set. 2852480093f4SDimitry Andric ; 2853bdd1243dSDimitry Andric } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off")) { 28540b57cec5SDimitry Andric FPContract = Val; 2855bdd1243dSDimitry Andric LastSeenFfpContractOption = Val; 2856bdd1243dSDimitry Andric } else 28570b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2858bdd1243dSDimitry Andric << A->getSpelling() << Val; 28590b57cec5SDimitry Andric break; 28600b57cec5SDimitry Andric } 28610b57cec5SDimitry Andric 2862480093f4SDimitry Andric // Validate and pass through -ffp-model option. 2863480093f4SDimitry Andric case options::OPT_ffp_model_EQ: 2864480093f4SDimitry Andric // This should only occur in the error case 2865480093f4SDimitry Andric // since the optID has been replaced by a more granular 2866480093f4SDimitry Andric // floating point option. 2867480093f4SDimitry Andric break; 2868480093f4SDimitry Andric 2869480093f4SDimitry Andric // Validate and pass through -ffp-exception-behavior option. 2870480093f4SDimitry Andric case options::OPT_ffp_exception_behavior_EQ: { 2871480093f4SDimitry Andric StringRef Val = A->getValue(); 2872480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2873480093f4SDimitry Andric !FPExceptionBehavior.equals(Val)) 2874480093f4SDimitry Andric // Warn that previous value of option is overridden. 2875480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2876480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2877480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + Val); 2878480093f4SDimitry Andric TrappingMath = TrappingMathPresent = false; 2879480093f4SDimitry Andric if (Val.equals("ignore") || Val.equals("maytrap")) 2880480093f4SDimitry Andric FPExceptionBehavior = Val; 2881480093f4SDimitry Andric else if (Val.equals("strict")) { 2882480093f4SDimitry Andric FPExceptionBehavior = Val; 2883480093f4SDimitry Andric TrappingMath = TrappingMathPresent = true; 2884480093f4SDimitry Andric } else 2885480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2886bdd1243dSDimitry Andric << A->getSpelling() << Val; 2887480093f4SDimitry Andric break; 2888480093f4SDimitry Andric } 2889480093f4SDimitry Andric 289081ad6265SDimitry Andric // Validate and pass through -ffp-eval-method option. 289181ad6265SDimitry Andric case options::OPT_ffp_eval_method_EQ: { 289281ad6265SDimitry Andric StringRef Val = A->getValue(); 289381ad6265SDimitry Andric if (Val.equals("double") || Val.equals("extended") || 289481ad6265SDimitry Andric Val.equals("source")) 289581ad6265SDimitry Andric FPEvalMethod = Val; 289681ad6265SDimitry Andric else 289781ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2898bdd1243dSDimitry Andric << A->getSpelling() << Val; 289981ad6265SDimitry Andric break; 290081ad6265SDimitry Andric } 290181ad6265SDimitry Andric 2902bdd1243dSDimitry Andric case options::OPT_fexcess_precision_EQ: { 2903bdd1243dSDimitry Andric StringRef Val = A->getValue(); 2904bdd1243dSDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 2905bdd1243dSDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 2906bdd1243dSDimitry Andric if (Val.equals("standard") || Val.equals("fast")) 2907bdd1243dSDimitry Andric Float16ExcessPrecision = Val; 2908bdd1243dSDimitry Andric // To make it GCC compatible, allow the value of "16" which 2909bdd1243dSDimitry Andric // means disable excess precision, the same meaning than clang's 2910bdd1243dSDimitry Andric // equivalent value "none". 2911bdd1243dSDimitry Andric else if (Val.equals("16")) 2912bdd1243dSDimitry Andric Float16ExcessPrecision = "none"; 2913bdd1243dSDimitry Andric else 2914bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2915bdd1243dSDimitry Andric << A->getSpelling() << Val; 2916bdd1243dSDimitry Andric } else { 2917bdd1243dSDimitry Andric if (!(Val.equals("standard") || Val.equals("fast"))) 2918bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2919bdd1243dSDimitry Andric << A->getSpelling() << Val; 2920bdd1243dSDimitry Andric } 2921bdd1243dSDimitry Andric break; 2922bdd1243dSDimitry Andric } 29230b57cec5SDimitry Andric case options::OPT_ffinite_math_only: 29240b57cec5SDimitry Andric HonorINFs = false; 29250b57cec5SDimitry Andric HonorNaNs = false; 29260b57cec5SDimitry Andric break; 29270b57cec5SDimitry Andric case options::OPT_fno_finite_math_only: 29280b57cec5SDimitry Andric HonorINFs = true; 29290b57cec5SDimitry Andric HonorNaNs = true; 29300b57cec5SDimitry Andric break; 29310b57cec5SDimitry Andric 29320b57cec5SDimitry Andric case options::OPT_funsafe_math_optimizations: 29330b57cec5SDimitry Andric AssociativeMath = true; 29340b57cec5SDimitry Andric ReciprocalMath = true; 29350b57cec5SDimitry Andric SignedZeros = false; 293604eeddc0SDimitry Andric ApproxFunc = true; 29370b57cec5SDimitry Andric TrappingMath = false; 2938480093f4SDimitry Andric FPExceptionBehavior = ""; 2939bdd1243dSDimitry Andric FPContract = "fast"; 2940bdd1243dSDimitry Andric SeenUnsafeMathModeOption = true; 29410b57cec5SDimitry Andric break; 29420b57cec5SDimitry Andric case options::OPT_fno_unsafe_math_optimizations: 29430b57cec5SDimitry Andric AssociativeMath = false; 29440b57cec5SDimitry Andric ReciprocalMath = false; 29450b57cec5SDimitry Andric SignedZeros = true; 294604eeddc0SDimitry Andric ApproxFunc = false; 29470b57cec5SDimitry Andric TrappingMath = true; 2948480093f4SDimitry Andric FPExceptionBehavior = "strict"; 29495ffd83dbSDimitry Andric 29505ffd83dbSDimitry Andric // The target may have opted to flush by default, so force IEEE. 29515ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 29525ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 2953bdd1243dSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2954bdd1243dSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) { 2955bdd1243dSDimitry Andric if (LastSeenFfpContractOption != "") { 2956bdd1243dSDimitry Andric FPContract = LastSeenFfpContractOption; 2957bdd1243dSDimitry Andric } else if (SeenUnsafeMathModeOption) 2958bdd1243dSDimitry Andric FPContract = "on"; 2959bdd1243dSDimitry Andric } 29600b57cec5SDimitry Andric break; 29610b57cec5SDimitry Andric 29620b57cec5SDimitry Andric case options::OPT_Ofast: 29630b57cec5SDimitry Andric // If -Ofast is the optimization level, then -ffast-math should be enabled 29640b57cec5SDimitry Andric if (!OFastEnabled) 29650b57cec5SDimitry Andric continue; 2966bdd1243dSDimitry Andric [[fallthrough]]; 29670b57cec5SDimitry Andric case options::OPT_ffast_math: 29680b57cec5SDimitry Andric HonorINFs = false; 29690b57cec5SDimitry Andric HonorNaNs = false; 29700b57cec5SDimitry Andric MathErrno = false; 29710b57cec5SDimitry Andric AssociativeMath = true; 29720b57cec5SDimitry Andric ReciprocalMath = true; 297304eeddc0SDimitry Andric ApproxFunc = true; 29740b57cec5SDimitry Andric SignedZeros = false; 29750b57cec5SDimitry Andric TrappingMath = false; 2976480093f4SDimitry Andric RoundingFPMath = false; 2977bdd1243dSDimitry Andric FPExceptionBehavior = ""; 29780b57cec5SDimitry Andric // If fast-math is set then set the fp-contract mode to fast. 29790b57cec5SDimitry Andric FPContract = "fast"; 2980bdd1243dSDimitry Andric SeenUnsafeMathModeOption = true; 29810b57cec5SDimitry Andric break; 29820b57cec5SDimitry Andric case options::OPT_fno_fast_math: 29830b57cec5SDimitry Andric HonorINFs = true; 29840b57cec5SDimitry Andric HonorNaNs = true; 29850b57cec5SDimitry Andric // Turning on -ffast-math (with either flag) removes the need for 29860b57cec5SDimitry Andric // MathErrno. However, turning *off* -ffast-math merely restores the 29870b57cec5SDimitry Andric // toolchain default (which may be false). 29880b57cec5SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 29890b57cec5SDimitry Andric AssociativeMath = false; 29900b57cec5SDimitry Andric ReciprocalMath = false; 299104eeddc0SDimitry Andric ApproxFunc = false; 29920b57cec5SDimitry Andric SignedZeros = true; 29930b57cec5SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 29945ffd83dbSDimitry Andric DenormalFPMath = DefaultDenormalFPMath; 29955ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 2996349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2997bdd1243dSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) { 2998bdd1243dSDimitry Andric if (LastSeenFfpContractOption != "") { 2999bdd1243dSDimitry Andric FPContract = LastSeenFfpContractOption; 3000bdd1243dSDimitry Andric } else if (SeenUnsafeMathModeOption) 3001349cc55cSDimitry Andric FPContract = "on"; 3002349cc55cSDimitry Andric } 30030b57cec5SDimitry Andric break; 30040b57cec5SDimitry Andric } 3005480093f4SDimitry Andric if (StrictFPModel) { 3006480093f4SDimitry Andric // If -ffp-model=strict has been specified on command line but 3007480093f4SDimitry Andric // subsequent options conflict then emit warning diagnostic. 3008349cc55cSDimitry Andric if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath && 300904eeddc0SDimitry Andric SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc && 30105ffd83dbSDimitry Andric DenormalFPMath == llvm::DenormalMode::getIEEE() && 3011349cc55cSDimitry Andric DenormalFP32Math == llvm::DenormalMode::getIEEE() && 3012349cc55cSDimitry Andric FPContract.equals("off")) 3013480093f4SDimitry Andric // OK: Current Arg doesn't conflict with -ffp-model=strict 3014480093f4SDimitry Andric ; 3015480093f4SDimitry Andric else { 3016480093f4SDimitry Andric StrictFPModel = false; 3017480093f4SDimitry Andric FPModel = ""; 3018bdd1243dSDimitry Andric auto RHS = (A->getNumValues() == 0) 3019bdd1243dSDimitry Andric ? A->getSpelling() 3020bdd1243dSDimitry Andric : Args.MakeArgString(A->getSpelling() + A->getValue()); 3021bdd1243dSDimitry Andric if (RHS != "-ffp-model=strict") 3022480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3023bdd1243dSDimitry Andric << "-ffp-model=strict" << RHS; 3024480093f4SDimitry Andric } 3025480093f4SDimitry Andric } 30260b57cec5SDimitry Andric 30270b57cec5SDimitry Andric // If we handled this option claim it 30280b57cec5SDimitry Andric A->claim(); 30290b57cec5SDimitry Andric } 30300b57cec5SDimitry Andric 30310b57cec5SDimitry Andric if (!HonorINFs) 30320b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-infs"); 30330b57cec5SDimitry Andric 30340b57cec5SDimitry Andric if (!HonorNaNs) 30350b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-nans"); 30360b57cec5SDimitry Andric 3037349cc55cSDimitry Andric if (ApproxFunc) 3038349cc55cSDimitry Andric CmdArgs.push_back("-fapprox-func"); 3039349cc55cSDimitry Andric 30400b57cec5SDimitry Andric if (MathErrno) 30410b57cec5SDimitry Andric CmdArgs.push_back("-fmath-errno"); 30420b57cec5SDimitry Andric 3043bdd1243dSDimitry Andric if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc && 3044bdd1243dSDimitry Andric !TrappingMath) 3045bdd1243dSDimitry Andric CmdArgs.push_back("-funsafe-math-optimizations"); 30460b57cec5SDimitry Andric 30470b57cec5SDimitry Andric if (!SignedZeros) 30480b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-zeros"); 30490b57cec5SDimitry Andric 30500b57cec5SDimitry Andric if (AssociativeMath && !SignedZeros && !TrappingMath) 30510b57cec5SDimitry Andric CmdArgs.push_back("-mreassociate"); 30520b57cec5SDimitry Andric 30530b57cec5SDimitry Andric if (ReciprocalMath) 30540b57cec5SDimitry Andric CmdArgs.push_back("-freciprocal-math"); 30550b57cec5SDimitry Andric 3056480093f4SDimitry Andric if (TrappingMath) { 3057480093f4SDimitry Andric // FP Exception Behavior is also set to strict 3058480093f4SDimitry Andric assert(FPExceptionBehavior.equals("strict")); 3059e8d8bef9SDimitry Andric } 30600b57cec5SDimitry Andric 30615ffd83dbSDimitry Andric // The default is IEEE. 30625ffd83dbSDimitry Andric if (DenormalFPMath != llvm::DenormalMode::getIEEE()) { 30635ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 30645ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 30655ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math=" << DenormalFPMath; 30665ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 30675ffd83dbSDimitry Andric } 30685ffd83dbSDimitry Andric 30695ffd83dbSDimitry Andric // Add f32 specific denormal mode flag if it's different. 30705ffd83dbSDimitry Andric if (DenormalFP32Math != DenormalFPMath) { 30715ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 30725ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 30735ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math; 30745ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 30755ffd83dbSDimitry Andric } 30760b57cec5SDimitry Andric 30770b57cec5SDimitry Andric if (!FPContract.empty()) 30780b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract)); 30790b57cec5SDimitry Andric 3080480093f4SDimitry Andric if (!RoundingFPMath) 3081480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); 3082480093f4SDimitry Andric 3083480093f4SDimitry Andric if (RoundingFPMath && RoundingMathPresent) 3084480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frounding-math")); 3085480093f4SDimitry Andric 3086480093f4SDimitry Andric if (!FPExceptionBehavior.empty()) 3087480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" + 3088480093f4SDimitry Andric FPExceptionBehavior)); 3089480093f4SDimitry Andric 309081ad6265SDimitry Andric if (!FPEvalMethod.empty()) 309181ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-eval-method=" + FPEvalMethod)); 309281ad6265SDimitry Andric 3093bdd1243dSDimitry Andric if (!Float16ExcessPrecision.empty()) 3094bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffloat16-excess-precision=" + 3095bdd1243dSDimitry Andric Float16ExcessPrecision)); 3096bdd1243dSDimitry Andric 30970b57cec5SDimitry Andric ParseMRecip(D, Args, CmdArgs); 30980b57cec5SDimitry Andric 30990b57cec5SDimitry Andric // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the 31000b57cec5SDimitry Andric // individual features enabled by -ffast-math instead of the option itself as 31010b57cec5SDimitry Andric // that's consistent with gcc's behaviour. 310204eeddc0SDimitry Andric if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc && 3103480093f4SDimitry Andric ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) { 31040b57cec5SDimitry Andric CmdArgs.push_back("-ffast-math"); 3105480093f4SDimitry Andric if (FPModel.equals("fast")) { 3106480093f4SDimitry Andric if (FPContract.equals("fast")) 3107480093f4SDimitry Andric // All set, do nothing. 3108480093f4SDimitry Andric ; 3109480093f4SDimitry Andric else if (FPContract.empty()) 3110480093f4SDimitry Andric // Enable -ffp-contract=fast 3111480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 3112480093f4SDimitry Andric else 3113480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3114480093f4SDimitry Andric << "-ffp-model=fast" 3115480093f4SDimitry Andric << Args.MakeArgString("-ffp-contract=" + FPContract); 3116480093f4SDimitry Andric } 3117480093f4SDimitry Andric } 31180b57cec5SDimitry Andric 31190b57cec5SDimitry Andric // Handle __FINITE_MATH_ONLY__ similarly. 31200b57cec5SDimitry Andric if (!HonorINFs && !HonorNaNs) 31210b57cec5SDimitry Andric CmdArgs.push_back("-ffinite-math-only"); 31220b57cec5SDimitry Andric 31230b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) { 31240b57cec5SDimitry Andric CmdArgs.push_back("-mfpmath"); 31250b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 31260b57cec5SDimitry Andric } 31270b57cec5SDimitry Andric 31280b57cec5SDimitry Andric // Disable a codegen optimization for floating-point casts. 31290b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow, 31300b57cec5SDimitry Andric options::OPT_fstrict_float_cast_overflow, false)) 31310b57cec5SDimitry Andric CmdArgs.push_back("-fno-strict-float-cast-overflow"); 31320b57cec5SDimitry Andric } 31330b57cec5SDimitry Andric 31340b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, 31350b57cec5SDimitry Andric const llvm::Triple &Triple, 31360b57cec5SDimitry Andric const InputInfo &Input) { 31370b57cec5SDimitry Andric // Add default argument set. 31380b57cec5SDimitry Andric if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 31390b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=core"); 31400b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=apiModeling"); 31410b57cec5SDimitry Andric 31420b57cec5SDimitry Andric if (!Triple.isWindowsMSVCEnvironment()) { 31430b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix"); 31440b57cec5SDimitry Andric } else { 31450b57cec5SDimitry Andric // Enable "unix" checkers that also work on Windows. 31460b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.API"); 31470b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.Malloc"); 31480b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof"); 31490b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator"); 31500b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg"); 31510b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg"); 31520b57cec5SDimitry Andric } 31530b57cec5SDimitry Andric 315481ad6265SDimitry Andric // Disable some unix checkers for PS4/PS5. 315581ad6265SDimitry Andric if (Triple.isPS()) { 31560b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.API"); 31570b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork"); 31580b57cec5SDimitry Andric } 31590b57cec5SDimitry Andric 3160480093f4SDimitry Andric if (Triple.isOSDarwin()) { 31610b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=osx"); 3162480093f4SDimitry Andric CmdArgs.push_back( 3163480093f4SDimitry Andric "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType"); 3164480093f4SDimitry Andric } 3165480093f4SDimitry Andric else if (Triple.isOSFuchsia()) 3166480093f4SDimitry Andric CmdArgs.push_back("-analyzer-checker=fuchsia"); 31670b57cec5SDimitry Andric 31680b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=deadcode"); 31690b57cec5SDimitry Andric 31700b57cec5SDimitry Andric if (types::isCXX(Input.getType())) 31710b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=cplusplus"); 31720b57cec5SDimitry Andric 317381ad6265SDimitry Andric if (!Triple.isPS()) { 31740b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 31750b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 31760b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 31770b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 31780b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 31790b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 31800b57cec5SDimitry Andric } 31810b57cec5SDimitry Andric 31820b57cec5SDimitry Andric // Default nullability checks. 31830b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull"); 31840b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull"); 31850b57cec5SDimitry Andric } 31860b57cec5SDimitry Andric 31870b57cec5SDimitry Andric // Set the output format. The default is plist, for (lame) historical reasons. 31880b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-output"); 31890b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 31900b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 31910b57cec5SDimitry Andric else 31920b57cec5SDimitry Andric CmdArgs.push_back("plist"); 31930b57cec5SDimitry Andric 31940b57cec5SDimitry Andric // Disable the presentation of standard compiler warnings when using 31950b57cec5SDimitry Andric // --analyze. We only want to show static analyzer diagnostics or frontend 31960b57cec5SDimitry Andric // errors. 31970b57cec5SDimitry Andric CmdArgs.push_back("-w"); 31980b57cec5SDimitry Andric 31990b57cec5SDimitry Andric // Add -Xanalyzer arguments when running as analyzer. 32000b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 32010b57cec5SDimitry Andric } 32020b57cec5SDimitry Andric 3203753f127fSDimitry Andric static bool isValidSymbolName(StringRef S) { 3204753f127fSDimitry Andric if (S.empty()) 3205753f127fSDimitry Andric return false; 3206753f127fSDimitry Andric 3207753f127fSDimitry Andric if (std::isdigit(S[0])) 3208753f127fSDimitry Andric return false; 3209753f127fSDimitry Andric 3210753f127fSDimitry Andric return llvm::all_of(S, [](char C) { return std::isalnum(C) || C == '_'; }); 3211753f127fSDimitry Andric } 3212753f127fSDimitry Andric 3213e8d8bef9SDimitry Andric static void RenderSSPOptions(const Driver &D, const ToolChain &TC, 3214e8d8bef9SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3215e8d8bef9SDimitry Andric bool KernelOrKext) { 32160b57cec5SDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 32170b57cec5SDimitry Andric 32180b57cec5SDimitry Andric // NVPTX doesn't support stack protectors; from the compiler's perspective, it 32190b57cec5SDimitry Andric // doesn't even have a stack! 32200b57cec5SDimitry Andric if (EffectiveTriple.isNVPTX()) 32210b57cec5SDimitry Andric return; 32220b57cec5SDimitry Andric 32230b57cec5SDimitry Andric // -stack-protector=0 is default. 3224e8d8bef9SDimitry Andric LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff; 3225e8d8bef9SDimitry Andric LangOptions::StackProtectorMode DefaultStackProtectorLevel = 32260b57cec5SDimitry Andric TC.GetDefaultStackProtectorLevel(KernelOrKext); 32270b57cec5SDimitry Andric 32280b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 32290b57cec5SDimitry Andric options::OPT_fstack_protector_all, 32300b57cec5SDimitry Andric options::OPT_fstack_protector_strong, 32310b57cec5SDimitry Andric options::OPT_fstack_protector)) { 32320b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fstack_protector)) 32330b57cec5SDimitry Andric StackProtectorLevel = 3234e8d8bef9SDimitry Andric std::max<>(LangOptions::SSPOn, DefaultStackProtectorLevel); 32350b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_strong)) 32360b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPStrong; 32370b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_all)) 32380b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPReq; 3239bdd1243dSDimitry Andric 3240bdd1243dSDimitry Andric if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) { 3241bdd1243dSDimitry Andric D.Diag(diag::warn_drv_unsupported_option_for_target) 3242bdd1243dSDimitry Andric << A->getSpelling() << EffectiveTriple.getTriple(); 3243bdd1243dSDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 3244bdd1243dSDimitry Andric } 32450b57cec5SDimitry Andric } else { 32460b57cec5SDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 32470b57cec5SDimitry Andric } 32480b57cec5SDimitry Andric 32490b57cec5SDimitry Andric if (StackProtectorLevel) { 32500b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 32510b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 32520b57cec5SDimitry Andric } 32530b57cec5SDimitry Andric 32540b57cec5SDimitry Andric // --param ssp-buffer-size= 32550b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__param)) { 32560b57cec5SDimitry Andric StringRef Str(A->getValue()); 32570b57cec5SDimitry Andric if (Str.startswith("ssp-buffer-size=")) { 32580b57cec5SDimitry Andric if (StackProtectorLevel) { 32590b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector-buffer-size"); 32600b57cec5SDimitry Andric // FIXME: Verify the argument is a valid integer. 32610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 32620b57cec5SDimitry Andric } 32630b57cec5SDimitry Andric A->claim(); 32640b57cec5SDimitry Andric } 32650b57cec5SDimitry Andric } 3266e8d8bef9SDimitry Andric 3267e8d8bef9SDimitry Andric const std::string &TripleStr = EffectiveTriple.getTriple(); 3268e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) { 3269e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3270349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3271349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3272e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3273e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3274349cc55cSDimitry Andric if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() || 3275349cc55cSDimitry Andric EffectiveTriple.isThumb()) && 3276349cc55cSDimitry Andric Value != "tls" && Value != "global") { 3277e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3278fe6060f1SDimitry Andric << A->getOption().getName() << Value << "tls global"; 3279fe6060f1SDimitry Andric return; 3280fe6060f1SDimitry Andric } 3281349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3282349cc55cSDimitry Andric Value == "tls") { 3283349cc55cSDimitry Andric if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3284349cc55cSDimitry Andric D.Diag(diag::err_drv_ssp_missing_offset_argument) 3285349cc55cSDimitry Andric << A->getAsString(Args); 3286349cc55cSDimitry Andric return; 3287349cc55cSDimitry Andric } 3288349cc55cSDimitry Andric // Check whether the target subarch supports the hardware TLS register 328904eeddc0SDimitry Andric if (!arm::isHardTPSupported(EffectiveTriple)) { 3290349cc55cSDimitry Andric D.Diag(diag::err_target_unsupported_tp_hard) 3291349cc55cSDimitry Andric << EffectiveTriple.getArchName(); 3292349cc55cSDimitry Andric return; 3293349cc55cSDimitry Andric } 3294349cc55cSDimitry Andric // Check whether the user asked for something other than -mtp=cp15 3295349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 3296349cc55cSDimitry Andric StringRef Value = A->getValue(); 3297349cc55cSDimitry Andric if (Value != "cp15") { 3298349cc55cSDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 3299349cc55cSDimitry Andric << A->getAsString(Args) << "-mstack-protector-guard=tls"; 3300349cc55cSDimitry Andric return; 3301349cc55cSDimitry Andric } 3302349cc55cSDimitry Andric } 3303349cc55cSDimitry Andric CmdArgs.push_back("-target-feature"); 3304349cc55cSDimitry Andric CmdArgs.push_back("+read-tp-hard"); 3305349cc55cSDimitry Andric } 3306fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") { 3307fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3308fe6060f1SDimitry Andric << A->getOption().getName() << Value << "sysreg global"; 3309e8d8bef9SDimitry Andric return; 3310e8d8bef9SDimitry Andric } 3311e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3312e8d8bef9SDimitry Andric } 3313e8d8bef9SDimitry Andric 3314e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3315e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3316349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3317349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3318e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3319e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3320fe6060f1SDimitry Andric int Offset; 3321e8d8bef9SDimitry Andric if (Value.getAsInteger(10, Offset)) { 3322e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3323e8d8bef9SDimitry Andric return; 3324e8d8bef9SDimitry Andric } 3325349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3326349cc55cSDimitry Andric (Offset < 0 || Offset > 0xfffff)) { 3327349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 3328349cc55cSDimitry Andric << A->getOption().getName() << Value; 3329349cc55cSDimitry Andric return; 3330349cc55cSDimitry Andric } 3331e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3332e8d8bef9SDimitry Andric } 3333e8d8bef9SDimitry Andric 3334e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) { 3335e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3336fe6060f1SDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) 3337e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3338e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3339e8d8bef9SDimitry Andric if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) { 3340e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3341fe6060f1SDimitry Andric << A->getOption().getName() << Value << "fs gs"; 3342fe6060f1SDimitry Andric return; 3343fe6060f1SDimitry Andric } 3344fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sp_el0") { 3345fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3346e8d8bef9SDimitry Andric return; 3347e8d8bef9SDimitry Andric } 3348e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3349e8d8bef9SDimitry Andric } 3350753f127fSDimitry Andric 3351753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_symbol_EQ)) { 3352753f127fSDimitry Andric StringRef Value = A->getValue(); 3353753f127fSDimitry Andric if (!isValidSymbolName(Value)) { 3354753f127fSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3355753f127fSDimitry Andric << A->getOption().getName() << "legal symbol name"; 3356753f127fSDimitry Andric return; 3357753f127fSDimitry Andric } 3358753f127fSDimitry Andric A->render(Args, CmdArgs); 3359753f127fSDimitry Andric } 33600b57cec5SDimitry Andric } 33610b57cec5SDimitry Andric 33625ffd83dbSDimitry Andric static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, 33635ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 33645ffd83dbSDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 33655ffd83dbSDimitry Andric 33663063e1e5SEd Maste if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux()) 33675ffd83dbSDimitry Andric return; 33685ffd83dbSDimitry Andric 33695ffd83dbSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && 33705ffd83dbSDimitry Andric !EffectiveTriple.isPPC64()) 33715ffd83dbSDimitry Andric return; 33725ffd83dbSDimitry Andric 337381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection, 337481ad6265SDimitry Andric options::OPT_fno_stack_clash_protection); 33755ffd83dbSDimitry Andric } 33765ffd83dbSDimitry Andric 33770b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D, 33780b57cec5SDimitry Andric const ToolChain &TC, 33790b57cec5SDimitry Andric const ArgList &Args, 33800b57cec5SDimitry Andric ArgStringList &CmdArgs) { 33810b57cec5SDimitry Andric auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit(); 33820b57cec5SDimitry Andric StringRef TrivialAutoVarInit = ""; 33830b57cec5SDimitry Andric 33840b57cec5SDimitry Andric for (const Arg *A : Args) { 33850b57cec5SDimitry Andric switch (A->getOption().getID()) { 33860b57cec5SDimitry Andric default: 33870b57cec5SDimitry Andric continue; 33880b57cec5SDimitry Andric case options::OPT_ftrivial_auto_var_init: { 33890b57cec5SDimitry Andric A->claim(); 33900b57cec5SDimitry Andric StringRef Val = A->getValue(); 33910b57cec5SDimitry Andric if (Val == "uninitialized" || Val == "zero" || Val == "pattern") 33920b57cec5SDimitry Andric TrivialAutoVarInit = Val; 33930b57cec5SDimitry Andric else 33940b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 3395bdd1243dSDimitry Andric << A->getSpelling() << Val; 33960b57cec5SDimitry Andric break; 33970b57cec5SDimitry Andric } 33980b57cec5SDimitry Andric } 33990b57cec5SDimitry Andric } 34000b57cec5SDimitry Andric 34010b57cec5SDimitry Andric if (TrivialAutoVarInit.empty()) 34020b57cec5SDimitry Andric switch (DefaultTrivialAutoVarInit) { 34030b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Uninitialized: 34040b57cec5SDimitry Andric break; 34050b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Pattern: 34060b57cec5SDimitry Andric TrivialAutoVarInit = "pattern"; 34070b57cec5SDimitry Andric break; 34080b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Zero: 34090b57cec5SDimitry Andric TrivialAutoVarInit = "zero"; 34100b57cec5SDimitry Andric break; 34110b57cec5SDimitry Andric } 34120b57cec5SDimitry Andric 34130b57cec5SDimitry Andric if (!TrivialAutoVarInit.empty()) { 34140b57cec5SDimitry Andric CmdArgs.push_back( 34150b57cec5SDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit)); 34160b57cec5SDimitry Andric } 34175ffd83dbSDimitry Andric 34185ffd83dbSDimitry Andric if (Arg *A = 34195ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) { 34205ffd83dbSDimitry Andric if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) || 34215ffd83dbSDimitry Andric StringRef( 34225ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) == 34235ffd83dbSDimitry Andric "uninitialized") 34245ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency); 34255ffd83dbSDimitry Andric A->claim(); 34265ffd83dbSDimitry Andric StringRef Val = A->getValue(); 34275ffd83dbSDimitry Andric if (std::stoi(Val.str()) <= 0) 34285ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_invalid_value); 34295ffd83dbSDimitry Andric CmdArgs.push_back( 34305ffd83dbSDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val)); 34315ffd83dbSDimitry Andric } 34320b57cec5SDimitry Andric } 34330b57cec5SDimitry Andric 3434fe6060f1SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, 3435fe6060f1SDimitry Andric types::ID InputType) { 34365ffd83dbSDimitry Andric // cl-denorms-are-zero is not forwarded. It is translated into a generic flag 34375ffd83dbSDimitry Andric // for denormal flushing handling based on the target. 34380b57cec5SDimitry Andric const unsigned ForwardedArguments[] = { 34390b57cec5SDimitry Andric options::OPT_cl_opt_disable, 34400b57cec5SDimitry Andric options::OPT_cl_strict_aliasing, 34410b57cec5SDimitry Andric options::OPT_cl_single_precision_constant, 34420b57cec5SDimitry Andric options::OPT_cl_finite_math_only, 34430b57cec5SDimitry Andric options::OPT_cl_kernel_arg_info, 34440b57cec5SDimitry Andric options::OPT_cl_unsafe_math_optimizations, 34450b57cec5SDimitry Andric options::OPT_cl_fast_relaxed_math, 34460b57cec5SDimitry Andric options::OPT_cl_mad_enable, 34470b57cec5SDimitry Andric options::OPT_cl_no_signed_zeros, 34480b57cec5SDimitry Andric options::OPT_cl_fp32_correctly_rounded_divide_sqrt, 34490b57cec5SDimitry Andric options::OPT_cl_uniform_work_group_size 34500b57cec5SDimitry Andric }; 34510b57cec5SDimitry Andric 34520b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) { 34530b57cec5SDimitry Andric std::string CLStdStr = std::string("-cl-std=") + A->getValue(); 34540b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLStdStr)); 345581ad6265SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_cl_ext_EQ)) { 345681ad6265SDimitry Andric std::string CLExtStr = std::string("-cl-ext=") + A->getValue(); 345781ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLExtStr)); 34580b57cec5SDimitry Andric } 34590b57cec5SDimitry Andric 34600b57cec5SDimitry Andric for (const auto &Arg : ForwardedArguments) 34610b57cec5SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 34620b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); 3463fe6060f1SDimitry Andric 3464fe6060f1SDimitry Andric // Only add the default headers if we are compiling OpenCL sources. 3465fe6060f1SDimitry Andric if ((types::isOpenCL(InputType) || 3466fe6060f1SDimitry Andric (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) && 3467fe6060f1SDimitry Andric !Args.hasArg(options::OPT_cl_no_stdinc)) { 3468fe6060f1SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 3469fe6060f1SDimitry Andric CmdArgs.push_back("-fdeclare-opencl-builtins"); 3470fe6060f1SDimitry Andric } 34710b57cec5SDimitry Andric } 34720b57cec5SDimitry Andric 347381ad6265SDimitry Andric static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs, 347481ad6265SDimitry Andric types::ID InputType) { 347581ad6265SDimitry Andric const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version, 347681ad6265SDimitry Andric options::OPT_D, 3477fcaf7f86SDimitry Andric options::OPT_I, 347881ad6265SDimitry Andric options::OPT_S, 3479bdd1243dSDimitry Andric options::OPT_O, 348081ad6265SDimitry Andric options::OPT_emit_llvm, 3481bdd1243dSDimitry Andric options::OPT_emit_obj, 348281ad6265SDimitry Andric options::OPT_disable_llvm_passes, 3483bdd1243dSDimitry Andric options::OPT_fnative_half_type, 3484bdd1243dSDimitry Andric options::OPT_hlsl_entrypoint}; 3485bdd1243dSDimitry Andric if (!types::isHLSL(InputType)) 3486bdd1243dSDimitry Andric return; 348781ad6265SDimitry Andric for (const auto &Arg : ForwardedArguments) 348881ad6265SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 348981ad6265SDimitry Andric A->renderAsInput(Args, CmdArgs); 349081ad6265SDimitry Andric // Add the default headers if dxc_no_stdinc is not set. 3491bdd1243dSDimitry Andric if (!Args.hasArg(options::OPT_dxc_no_stdinc) && 3492bdd1243dSDimitry Andric !Args.hasArg(options::OPT_nostdinc)) 349381ad6265SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 349481ad6265SDimitry Andric } 349581ad6265SDimitry Andric 34960b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, 34970b57cec5SDimitry Andric ArgStringList &CmdArgs) { 34980b57cec5SDimitry Andric bool ARCMTEnabled = false; 34990b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) { 35000b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 35010b57cec5SDimitry Andric options::OPT_ccc_arcmt_modify, 35020b57cec5SDimitry Andric options::OPT_ccc_arcmt_migrate)) { 35030b57cec5SDimitry Andric ARCMTEnabled = true; 35040b57cec5SDimitry Andric switch (A->getOption().getID()) { 35050b57cec5SDimitry Andric default: llvm_unreachable("missed a case"); 35060b57cec5SDimitry Andric case options::OPT_ccc_arcmt_check: 3507e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=check"); 35080b57cec5SDimitry Andric break; 35090b57cec5SDimitry Andric case options::OPT_ccc_arcmt_modify: 3510e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=modify"); 35110b57cec5SDimitry Andric break; 35120b57cec5SDimitry Andric case options::OPT_ccc_arcmt_migrate: 3513e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=migrate"); 35140b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 35150b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 35160b57cec5SDimitry Andric 35170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 35180b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 35190b57cec5SDimitry Andric break; 35200b57cec5SDimitry Andric } 35210b57cec5SDimitry Andric } 35220b57cec5SDimitry Andric } else { 35230b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 35240b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 35250b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 35260b57cec5SDimitry Andric } 35270b57cec5SDimitry Andric 35280b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 35290b57cec5SDimitry Andric if (ARCMTEnabled) 35300b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 35310b57cec5SDimitry Andric << A->getAsString(Args) << "-ccc-arcmt-migrate"; 35320b57cec5SDimitry Andric 35330b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 35340b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 35350b57cec5SDimitry Andric 35360b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 35370b57cec5SDimitry Andric options::OPT_objcmt_migrate_subscripting, 35380b57cec5SDimitry Andric options::OPT_objcmt_migrate_property)) { 35390b57cec5SDimitry Andric // None specified, means enable them all. 35400b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-literals"); 35410b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-subscripting"); 35420b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-property"); 35430b57cec5SDimitry Andric } else { 35440b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 35450b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 35460b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 35470b57cec5SDimitry Andric } 35480b57cec5SDimitry Andric } else { 35490b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 35500b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 35510b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 35520b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all); 35530b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property); 35540b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property); 35550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax); 35560b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation); 35570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype); 35580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros); 35590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance); 35600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property); 35610b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property); 35620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly); 35630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init); 3564349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_allowlist_dir_path); 35650b57cec5SDimitry Andric } 35660b57cec5SDimitry Andric } 35670b57cec5SDimitry Andric 35680b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, 35690b57cec5SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 35700b57cec5SDimitry Andric // -fbuiltin is default unless -mkernel is used. 35710b57cec5SDimitry Andric bool UseBuiltins = 35720b57cec5SDimitry Andric Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin, 35730b57cec5SDimitry Andric !Args.hasArg(options::OPT_mkernel)); 35740b57cec5SDimitry Andric if (!UseBuiltins) 35750b57cec5SDimitry Andric CmdArgs.push_back("-fno-builtin"); 35760b57cec5SDimitry Andric 35770b57cec5SDimitry Andric // -ffreestanding implies -fno-builtin. 35780b57cec5SDimitry Andric if (Args.hasArg(options::OPT_ffreestanding)) 35790b57cec5SDimitry Andric UseBuiltins = false; 35800b57cec5SDimitry Andric 35810b57cec5SDimitry Andric // Process the -fno-builtin-* options. 358281ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fno_builtin_)) { 358381ad6265SDimitry Andric A->claim(); 35840b57cec5SDimitry Andric 35850b57cec5SDimitry Andric // If -fno-builtin is specified, then there's no need to pass the option to 35860b57cec5SDimitry Andric // the frontend. 358781ad6265SDimitry Andric if (UseBuiltins) 358881ad6265SDimitry Andric A->render(Args, CmdArgs); 35890b57cec5SDimitry Andric } 35900b57cec5SDimitry Andric 35910b57cec5SDimitry Andric // le32-specific flags: 35920b57cec5SDimitry Andric // -fno-math-builtin: clang should not convert math builtins to intrinsics 35930b57cec5SDimitry Andric // by default. 35940b57cec5SDimitry Andric if (TC.getArch() == llvm::Triple::le32) 35950b57cec5SDimitry Andric CmdArgs.push_back("-fno-math-builtin"); 35960b57cec5SDimitry Andric } 35970b57cec5SDimitry Andric 35985ffd83dbSDimitry Andric bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { 359981ad6265SDimitry Andric if (const char *Str = std::getenv("CLANG_MODULE_CACHE_PATH")) { 360081ad6265SDimitry Andric Twine Path{Str}; 360181ad6265SDimitry Andric Path.toVector(Result); 360281ad6265SDimitry Andric return Path.getSingleStringRef() != ""; 360381ad6265SDimitry Andric } 36045ffd83dbSDimitry Andric if (llvm::sys::path::cache_directory(Result)) { 36055ffd83dbSDimitry Andric llvm::sys::path::append(Result, "clang"); 36060b57cec5SDimitry Andric llvm::sys::path::append(Result, "ModuleCache"); 36075ffd83dbSDimitry Andric return true; 36085ffd83dbSDimitry Andric } 36095ffd83dbSDimitry Andric return false; 36100b57cec5SDimitry Andric } 36110b57cec5SDimitry Andric 3612bdd1243dSDimitry Andric static bool RenderModulesOptions(Compilation &C, const Driver &D, 36130b57cec5SDimitry Andric const ArgList &Args, const InputInfo &Input, 3614bdd1243dSDimitry Andric const InputInfo &Output, const Arg *Std, 3615bdd1243dSDimitry Andric ArgStringList &CmdArgs) { 3616bdd1243dSDimitry Andric bool IsCXX = types::isCXX(Input.getType()); 3617bdd1243dSDimitry Andric // FIXME: Find a better way to determine whether the input has standard c++ 3618bdd1243dSDimitry Andric // modules support by default. 3619bdd1243dSDimitry Andric bool HaveStdCXXModules = 3620bdd1243dSDimitry Andric IsCXX && Std && 3621bdd1243dSDimitry Andric (Std->containsValue("c++2a") || Std->containsValue("c++20") || 3622bdd1243dSDimitry Andric Std->containsValue("c++2b") || Std->containsValue("c++latest")); 3623bdd1243dSDimitry Andric bool HaveModules = HaveStdCXXModules; 3624bdd1243dSDimitry Andric 36250b57cec5SDimitry Andric // -fmodules enables the use of precompiled modules (off by default). 36260b57cec5SDimitry Andric // Users can pass -fno-cxx-modules to turn off modules support for 36270b57cec5SDimitry Andric // C++/Objective-C++ programs. 36280b57cec5SDimitry Andric bool HaveClangModules = false; 36290b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 36300b57cec5SDimitry Andric bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 36310b57cec5SDimitry Andric options::OPT_fno_cxx_modules, true); 3632bdd1243dSDimitry Andric if (AllowedInCXX || !IsCXX) { 36330b57cec5SDimitry Andric CmdArgs.push_back("-fmodules"); 36340b57cec5SDimitry Andric HaveClangModules = true; 36350b57cec5SDimitry Andric } 36360b57cec5SDimitry Andric } 36370b57cec5SDimitry Andric 36380b57cec5SDimitry Andric HaveModules |= HaveClangModules; 36390b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fmodules_ts)) { 3640bdd1243dSDimitry Andric D.Diag(diag::warn_deprecated_fmodules_ts_flag); 36410b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-ts"); 36420b57cec5SDimitry Andric HaveModules = true; 36430b57cec5SDimitry Andric } 36440b57cec5SDimitry Andric 36450b57cec5SDimitry Andric // -fmodule-maps enables implicit reading of module map files. By default, 36460b57cec5SDimitry Andric // this is enabled if we are using Clang's flavor of precompiled modules. 36470b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fimplicit_module_maps, 36480b57cec5SDimitry Andric options::OPT_fno_implicit_module_maps, HaveClangModules)) 36490b57cec5SDimitry Andric CmdArgs.push_back("-fimplicit-module-maps"); 36500b57cec5SDimitry Andric 36510b57cec5SDimitry Andric // -fmodules-decluse checks that modules used are declared so (off by default) 365281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fmodules_decluse, 365381ad6265SDimitry Andric options::OPT_fno_modules_decluse); 36540b57cec5SDimitry Andric 36550b57cec5SDimitry Andric // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that 36560b57cec5SDimitry Andric // all #included headers are part of modules. 36570b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_strict_decluse, 36580b57cec5SDimitry Andric options::OPT_fno_modules_strict_decluse, false)) 36590b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-strict-decluse"); 36600b57cec5SDimitry Andric 36610b57cec5SDimitry Andric // -fno-implicit-modules turns off implicitly compiling modules on demand. 36620b57cec5SDimitry Andric bool ImplicitModules = false; 36630b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fimplicit_modules, 36640b57cec5SDimitry Andric options::OPT_fno_implicit_modules, HaveClangModules)) { 36650b57cec5SDimitry Andric if (HaveModules) 36660b57cec5SDimitry Andric CmdArgs.push_back("-fno-implicit-modules"); 36670b57cec5SDimitry Andric } else if (HaveModules) { 36680b57cec5SDimitry Andric ImplicitModules = true; 36690b57cec5SDimitry Andric // -fmodule-cache-path specifies where our implicitly-built module files 36700b57cec5SDimitry Andric // should be written. 36710b57cec5SDimitry Andric SmallString<128> Path; 36720b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) 36730b57cec5SDimitry Andric Path = A->getValue(); 36740b57cec5SDimitry Andric 36755ffd83dbSDimitry Andric bool HasPath = true; 36760b57cec5SDimitry Andric if (C.isForDiagnostics()) { 36770b57cec5SDimitry Andric // When generating crash reports, we want to emit the modules along with 36780b57cec5SDimitry Andric // the reproduction sources, so we ignore any provided module path. 36790b57cec5SDimitry Andric Path = Output.getFilename(); 36800b57cec5SDimitry Andric llvm::sys::path::replace_extension(Path, ".cache"); 36810b57cec5SDimitry Andric llvm::sys::path::append(Path, "modules"); 36820b57cec5SDimitry Andric } else if (Path.empty()) { 36830b57cec5SDimitry Andric // No module path was provided: use the default. 36845ffd83dbSDimitry Andric HasPath = Driver::getDefaultModuleCachePath(Path); 36850b57cec5SDimitry Andric } 36860b57cec5SDimitry Andric 36875ffd83dbSDimitry Andric // `HasPath` will only be false if getDefaultModuleCachePath() fails. 36885ffd83dbSDimitry Andric // That being said, that failure is unlikely and not caching is harmless. 36895ffd83dbSDimitry Andric if (HasPath) { 36900b57cec5SDimitry Andric const char Arg[] = "-fmodules-cache-path="; 36910b57cec5SDimitry Andric Path.insert(Path.begin(), Arg, Arg + strlen(Arg)); 36920b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Path)); 36930b57cec5SDimitry Andric } 36945ffd83dbSDimitry Andric } 36950b57cec5SDimitry Andric 36960b57cec5SDimitry Andric if (HaveModules) { 36970b57cec5SDimitry Andric // -fprebuilt-module-path specifies where to load the prebuilt module files. 36980b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { 36990b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 37000b57cec5SDimitry Andric std::string("-fprebuilt-module-path=") + A->getValue())); 37010b57cec5SDimitry Andric A->claim(); 37020b57cec5SDimitry Andric } 3703e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules, 3704e8d8bef9SDimitry Andric options::OPT_fno_prebuilt_implicit_modules, false)) 3705e8d8bef9SDimitry Andric CmdArgs.push_back("-fprebuilt-implicit-modules"); 3706a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content, 3707a7dea167SDimitry Andric options::OPT_fno_modules_validate_input_files_content, 3708a7dea167SDimitry Andric false)) 3709a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 37100b57cec5SDimitry Andric } 37110b57cec5SDimitry Andric 37120b57cec5SDimitry Andric // -fmodule-name specifies the module that is currently being built (or 37130b57cec5SDimitry Andric // used for header checking by -fmodule-maps). 37140b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ); 37150b57cec5SDimitry Andric 37160b57cec5SDimitry Andric // -fmodule-map-file can be used to specify files containing module 37170b57cec5SDimitry Andric // definitions. 37180b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file); 37190b57cec5SDimitry Andric 37200b57cec5SDimitry Andric // -fbuiltin-module-map can be used to load the clang 37210b57cec5SDimitry Andric // builtin headers modulemap file. 37220b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuiltin_module_map)) { 37230b57cec5SDimitry Andric SmallString<128> BuiltinModuleMap(D.ResourceDir); 37240b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "include"); 37250b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "module.modulemap"); 37260b57cec5SDimitry Andric if (llvm::sys::fs::exists(BuiltinModuleMap)) 37270b57cec5SDimitry Andric CmdArgs.push_back( 37280b57cec5SDimitry Andric Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap)); 37290b57cec5SDimitry Andric } 37300b57cec5SDimitry Andric 37310b57cec5SDimitry Andric // The -fmodule-file=<name>=<file> form specifies the mapping of module 37320b57cec5SDimitry Andric // names to precompiled module files (the module is loaded only if used). 37330b57cec5SDimitry Andric // The -fmodule-file=<file> form can be used to unconditionally load 37340b57cec5SDimitry Andric // precompiled module files (whether used or not). 37350b57cec5SDimitry Andric if (HaveModules) 37360b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file); 37370b57cec5SDimitry Andric else 37380b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_file); 37390b57cec5SDimitry Andric 37400b57cec5SDimitry Andric // When building modules and generating crashdumps, we need to dump a module 37410b57cec5SDimitry Andric // dependency VFS alongside the output. 37420b57cec5SDimitry Andric if (HaveClangModules && C.isForDiagnostics()) { 37430b57cec5SDimitry Andric SmallString<128> VFSDir(Output.getFilename()); 37440b57cec5SDimitry Andric llvm::sys::path::replace_extension(VFSDir, ".cache"); 37450b57cec5SDimitry Andric // Add the cache directory as a temp so the crash diagnostics pick it up. 37460b57cec5SDimitry Andric C.addTempFile(Args.MakeArgString(VFSDir)); 37470b57cec5SDimitry Andric 37480b57cec5SDimitry Andric llvm::sys::path::append(VFSDir, "vfs"); 37490b57cec5SDimitry Andric CmdArgs.push_back("-module-dependency-dir"); 37500b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(VFSDir)); 37510b57cec5SDimitry Andric } 37520b57cec5SDimitry Andric 37530b57cec5SDimitry Andric if (HaveClangModules) 37540b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path); 37550b57cec5SDimitry Andric 37560b57cec5SDimitry Andric // Pass through all -fmodules-ignore-macro arguments. 37570b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 37580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 37590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 37600b57cec5SDimitry Andric 3761753f127fSDimitry Andric if (HaveClangModules) { 37620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); 37630b57cec5SDimitry Andric 37640b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { 37650b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuild_session_timestamp)) 37660b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 37670b57cec5SDimitry Andric << A->getAsString(Args) << "-fbuild-session-timestamp"; 37680b57cec5SDimitry Andric 37690b57cec5SDimitry Andric llvm::sys::fs::file_status Status; 37700b57cec5SDimitry Andric if (llvm::sys::fs::status(A->getValue(), Status)) 37710b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << A->getValue(); 3772349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 3773349cc55cSDimitry Andric "-fbuild-session-timestamp=" + 3774349cc55cSDimitry Andric Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>( 3775349cc55cSDimitry Andric Status.getLastModificationTime().time_since_epoch()) 37760b57cec5SDimitry Andric .count()))); 37770b57cec5SDimitry Andric } 37780b57cec5SDimitry Andric 3779753f127fSDimitry Andric if (Args.getLastArg( 3780753f127fSDimitry Andric options::OPT_fmodules_validate_once_per_build_session)) { 37810b57cec5SDimitry Andric if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, 37820b57cec5SDimitry Andric options::OPT_fbuild_session_file)) 37830b57cec5SDimitry Andric D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); 37840b57cec5SDimitry Andric 37850b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 37860b57cec5SDimitry Andric options::OPT_fmodules_validate_once_per_build_session); 37870b57cec5SDimitry Andric } 37880b57cec5SDimitry Andric 37890b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, 37900b57cec5SDimitry Andric options::OPT_fno_modules_validate_system_headers, 37910b57cec5SDimitry Andric ImplicitModules)) 37920b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-validate-system-headers"); 37930b57cec5SDimitry Andric 3794753f127fSDimitry Andric Args.AddLastArg(CmdArgs, 3795753f127fSDimitry Andric options::OPT_fmodules_disable_diagnostic_validation); 3796753f127fSDimitry Andric } else { 3797753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_timestamp); 3798753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_file); 3799753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_once_per_build_session); 3800753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_system_headers); 3801753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fno_modules_validate_system_headers); 3802753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_disable_diagnostic_validation); 3803753f127fSDimitry Andric } 3804bdd1243dSDimitry Andric 3805bdd1243dSDimitry Andric // Claim `-fmodule-output` and `-fmodule-output=` to avoid unused warnings. 3806bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_output); 3807bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_output_EQ); 3808bdd1243dSDimitry Andric 3809bdd1243dSDimitry Andric return HaveModules; 38100b57cec5SDimitry Andric } 38110b57cec5SDimitry Andric 38120b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, 38130b57cec5SDimitry Andric ArgStringList &CmdArgs) { 38140b57cec5SDimitry Andric // -fsigned-char is default. 38150b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char, 38160b57cec5SDimitry Andric options::OPT_fno_signed_char, 38170b57cec5SDimitry Andric options::OPT_funsigned_char, 38180b57cec5SDimitry Andric options::OPT_fno_unsigned_char)) { 38190b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_funsigned_char) || 38200b57cec5SDimitry Andric A->getOption().matches(options::OPT_fno_signed_char)) { 38210b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 38220b57cec5SDimitry Andric } 38230b57cec5SDimitry Andric } else if (!isSignedCharDefault(T)) { 38240b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 38250b57cec5SDimitry Andric } 38260b57cec5SDimitry Andric 38270b57cec5SDimitry Andric // The default depends on the language standard. 38280b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t); 38290b57cec5SDimitry Andric 38300b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar, 38310b57cec5SDimitry Andric options::OPT_fno_short_wchar)) { 38320b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fshort_wchar)) { 38330b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=short"); 38340b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 38350b57cec5SDimitry Andric } else { 38360b57cec5SDimitry Andric bool IsARM = T.isARM() || T.isThumb() || T.isAArch64(); 38370b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=int"); 3838e8d8bef9SDimitry Andric if (T.isOSzOS() || 3839e8d8bef9SDimitry Andric (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD()))) 38400b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 38410b57cec5SDimitry Andric else 38420b57cec5SDimitry Andric CmdArgs.push_back("-fsigned-wchar"); 38430b57cec5SDimitry Andric } 38440b57cec5SDimitry Andric } 38450b57cec5SDimitry Andric } 38460b57cec5SDimitry Andric 38470b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D, 38480b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 38490b57cec5SDimitry Andric ObjCRuntime &Runtime, bool InferCovariantReturns, 38500b57cec5SDimitry Andric const InputInfo &Input, ArgStringList &CmdArgs) { 38510b57cec5SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 38520b57cec5SDimitry Andric 38530b57cec5SDimitry Andric // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy 38540b57cec5SDimitry Andric // is the default. Except for deployment target of 10.5, next runtime is 38550b57cec5SDimitry Andric // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently. 38560b57cec5SDimitry Andric if (Runtime.isNonFragile()) { 38570b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 38580b57cec5SDimitry Andric options::OPT_fno_objc_legacy_dispatch, 38590b57cec5SDimitry Andric Runtime.isLegacyDispatchDefaultForArch(Arch))) { 38600b57cec5SDimitry Andric if (TC.UseObjCMixedDispatch()) 38610b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 38620b57cec5SDimitry Andric else 38630b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 38640b57cec5SDimitry Andric } 38650b57cec5SDimitry Andric } 38660b57cec5SDimitry Andric 38670b57cec5SDimitry Andric // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option 38680b57cec5SDimitry Andric // to do Array/Dictionary subscripting by default. 38690b57cec5SDimitry Andric if (Arch == llvm::Triple::x86 && T.isMacOSX() && 38700b57cec5SDimitry Andric Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily()) 38710b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-subscripting-legacy-runtime"); 38720b57cec5SDimitry Andric 38730b57cec5SDimitry Andric // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 38740b57cec5SDimitry Andric // NOTE: This logic is duplicated in ToolChains.cpp. 38750b57cec5SDimitry Andric if (isObjCAutoRefCount(Args)) { 38760b57cec5SDimitry Andric TC.CheckObjCARC(); 38770b57cec5SDimitry Andric 38780b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc"); 38790b57cec5SDimitry Andric 38800b57cec5SDimitry Andric // FIXME: It seems like this entire block, and several around it should be 38810b57cec5SDimitry Andric // wrapped in isObjC, but for now we just use it here as this is where it 38820b57cec5SDimitry Andric // was being used previously. 38830b57cec5SDimitry Andric if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) { 38840b57cec5SDimitry Andric if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 38850b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 38860b57cec5SDimitry Andric else 38870b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 38880b57cec5SDimitry Andric } 38890b57cec5SDimitry Andric 38900b57cec5SDimitry Andric // Allow the user to enable full exceptions code emission. 38910b57cec5SDimitry Andric // We default off for Objective-C, on for Objective-C++. 38920b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 38930b57cec5SDimitry Andric options::OPT_fno_objc_arc_exceptions, 38940b57cec5SDimitry Andric /*Default=*/types::isCXX(Input.getType()))) 38950b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-exceptions"); 38960b57cec5SDimitry Andric } 38970b57cec5SDimitry Andric 38980b57cec5SDimitry Andric // Silence warning for full exception code emission options when explicitly 38990b57cec5SDimitry Andric // set to use no ARC. 39000b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fno_objc_arc)) { 39010b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions); 39020b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions); 39030b57cec5SDimitry Andric } 39040b57cec5SDimitry Andric 39050b57cec5SDimitry Andric // Allow the user to control whether messages can be converted to runtime 39060b57cec5SDimitry Andric // functions. 39070b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 39080b57cec5SDimitry Andric auto *Arg = Args.getLastArg( 39090b57cec5SDimitry Andric options::OPT_fobjc_convert_messages_to_runtime_calls, 39100b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls); 39110b57cec5SDimitry Andric if (Arg && 39120b57cec5SDimitry Andric Arg->getOption().matches( 39130b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls)) 39140b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls"); 39150b57cec5SDimitry Andric } 39160b57cec5SDimitry Andric 39170b57cec5SDimitry Andric // -fobjc-infer-related-result-type is the default, except in the Objective-C 39180b57cec5SDimitry Andric // rewriter. 39190b57cec5SDimitry Andric if (InferCovariantReturns) 39200b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-infer-related-result-type"); 39210b57cec5SDimitry Andric 39220b57cec5SDimitry Andric // Pass down -fobjc-weak or -fno-objc-weak if present. 39230b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 39240b57cec5SDimitry Andric auto WeakArg = 39250b57cec5SDimitry Andric Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak); 39260b57cec5SDimitry Andric if (!WeakArg) { 39270b57cec5SDimitry Andric // nothing to do 39280b57cec5SDimitry Andric } else if (!Runtime.allowsWeak()) { 39290b57cec5SDimitry Andric if (WeakArg->getOption().matches(options::OPT_fobjc_weak)) 39300b57cec5SDimitry Andric D.Diag(diag::err_objc_weak_unsupported); 39310b57cec5SDimitry Andric } else { 39320b57cec5SDimitry Andric WeakArg->render(Args, CmdArgs); 39330b57cec5SDimitry Andric } 39340b57cec5SDimitry Andric } 3935fe6060f1SDimitry Andric 3936fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing)) 3937fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing"); 39380b57cec5SDimitry Andric } 39390b57cec5SDimitry Andric 39400b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, 39410b57cec5SDimitry Andric ArgStringList &CmdArgs) { 39420b57cec5SDimitry Andric bool CaretDefault = true; 39430b57cec5SDimitry Andric bool ColumnDefault = true; 39440b57cec5SDimitry Andric 39450b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic, 39460b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_column, 39470b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_caret)) { 39480b57cec5SDimitry Andric switch (A->getOption().getID()) { 39490b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_caret: 39500b57cec5SDimitry Andric CaretDefault = true; 39510b57cec5SDimitry Andric ColumnDefault = true; 39520b57cec5SDimitry Andric break; 39530b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_column: 39540b57cec5SDimitry Andric CaretDefault = false; 39550b57cec5SDimitry Andric ColumnDefault = true; 39560b57cec5SDimitry Andric break; 39570b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_classic: 39580b57cec5SDimitry Andric CaretDefault = false; 39590b57cec5SDimitry Andric ColumnDefault = false; 39600b57cec5SDimitry Andric break; 39610b57cec5SDimitry Andric } 39620b57cec5SDimitry Andric } 39630b57cec5SDimitry Andric 39640b57cec5SDimitry Andric // -fcaret-diagnostics is default. 39650b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 39660b57cec5SDimitry Andric options::OPT_fno_caret_diagnostics, CaretDefault)) 39670b57cec5SDimitry Andric CmdArgs.push_back("-fno-caret-diagnostics"); 39680b57cec5SDimitry Andric 396981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_fixit_info, 397081ad6265SDimitry Andric options::OPT_fno_diagnostics_fixit_info); 397181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_option, 397281ad6265SDimitry Andric options::OPT_fno_diagnostics_show_option); 39730b57cec5SDimitry Andric 39740b57cec5SDimitry Andric if (const Arg *A = 39750b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 39760b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-category"); 39770b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 39780b57cec5SDimitry Andric } 39790b57cec5SDimitry Andric 398081ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdiagnostics_show_hotness, 398181ad6265SDimitry Andric options::OPT_fno_diagnostics_show_hotness); 39820b57cec5SDimitry Andric 39830b57cec5SDimitry Andric if (const Arg *A = 39840b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) { 39850b57cec5SDimitry Andric std::string Opt = 39860b57cec5SDimitry Andric std::string("-fdiagnostics-hotness-threshold=") + A->getValue(); 39870b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 39880b57cec5SDimitry Andric } 39890b57cec5SDimitry Andric 39900b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 39910b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 39920b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 3993fcaf7f86SDimitry Andric if (StringRef(A->getValue()) == "sarif" || 3994fcaf7f86SDimitry Andric StringRef(A->getValue()) == "SARIF") 3995fcaf7f86SDimitry Andric D.Diag(diag::warn_drv_sarif_format_unstable); 39960b57cec5SDimitry Andric } 39970b57cec5SDimitry Andric 39980b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg( 39990b57cec5SDimitry Andric options::OPT_fdiagnostics_show_note_include_stack, 40000b57cec5SDimitry Andric options::OPT_fno_diagnostics_show_note_include_stack)) { 40010b57cec5SDimitry Andric const Option &O = A->getOption(); 40020b57cec5SDimitry Andric if (O.matches(options::OPT_fdiagnostics_show_note_include_stack)) 40030b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 40040b57cec5SDimitry Andric else 40050b57cec5SDimitry Andric CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 40060b57cec5SDimitry Andric } 40070b57cec5SDimitry Andric 40080b57cec5SDimitry Andric // Color diagnostics are parsed by the driver directly from argv and later 40090b57cec5SDimitry Andric // re-parsed to construct this job; claim any possible color diagnostic here 40100b57cec5SDimitry Andric // to avoid warn_drv_unused_argument and diagnose bad 40110b57cec5SDimitry Andric // OPT_fdiagnostics_color_EQ values. 401281ad6265SDimitry Andric Args.getLastArg(options::OPT_fcolor_diagnostics, 401381ad6265SDimitry Andric options::OPT_fno_color_diagnostics); 401481ad6265SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_color_EQ)) { 40150b57cec5SDimitry Andric StringRef Value(A->getValue()); 40160b57cec5SDimitry Andric if (Value != "always" && Value != "never" && Value != "auto") 401781ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 401881ad6265SDimitry Andric << Value << A->getOption().getName(); 40190b57cec5SDimitry Andric } 40200b57cec5SDimitry Andric 40210b57cec5SDimitry Andric if (D.getDiags().getDiagnosticOptions().ShowColors) 40220b57cec5SDimitry Andric CmdArgs.push_back("-fcolor-diagnostics"); 40230b57cec5SDimitry Andric 40240b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fansi_escape_codes)) 40250b57cec5SDimitry Andric CmdArgs.push_back("-fansi-escape-codes"); 40260b57cec5SDimitry Andric 402781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fshow_source_location, 402881ad6265SDimitry Andric options::OPT_fno_show_source_location); 40290b57cec5SDimitry Andric 40300b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths)) 40310b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-absolute-paths"); 40320b57cec5SDimitry Andric 40330b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column, 40340b57cec5SDimitry Andric ColumnDefault)) 40350b57cec5SDimitry Andric CmdArgs.push_back("-fno-show-column"); 40360b57cec5SDimitry Andric 403781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fspell_checking, 403881ad6265SDimitry Andric options::OPT_fno_spell_checking); 40390b57cec5SDimitry Andric } 40400b57cec5SDimitry Andric 4041fcaf7f86SDimitry Andric DwarfFissionKind tools::getDebugFissionKind(const Driver &D, 40420b57cec5SDimitry Andric const ArgList &Args, Arg *&Arg) { 4043e8d8bef9SDimitry Andric Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ, 4044e8d8bef9SDimitry Andric options::OPT_gno_split_dwarf); 4045e8d8bef9SDimitry Andric if (!Arg || Arg->getOption().matches(options::OPT_gno_split_dwarf)) 40460b57cec5SDimitry Andric return DwarfFissionKind::None; 40470b57cec5SDimitry Andric 40480b57cec5SDimitry Andric if (Arg->getOption().matches(options::OPT_gsplit_dwarf)) 40490b57cec5SDimitry Andric return DwarfFissionKind::Split; 40500b57cec5SDimitry Andric 40510b57cec5SDimitry Andric StringRef Value = Arg->getValue(); 40520b57cec5SDimitry Andric if (Value == "split") 40530b57cec5SDimitry Andric return DwarfFissionKind::Split; 40540b57cec5SDimitry Andric if (Value == "single") 40550b57cec5SDimitry Andric return DwarfFissionKind::Single; 40560b57cec5SDimitry Andric 40570b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 4058bdd1243dSDimitry Andric << Arg->getSpelling() << Arg->getValue(); 40590b57cec5SDimitry Andric return DwarfFissionKind::None; 40600b57cec5SDimitry Andric } 40610b57cec5SDimitry Andric 4062fe6060f1SDimitry Andric static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, 4063fe6060f1SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 4064fe6060f1SDimitry Andric unsigned DwarfVersion) { 4065fe6060f1SDimitry Andric auto *DwarfFormatArg = 4066fe6060f1SDimitry Andric Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); 4067fe6060f1SDimitry Andric if (!DwarfFormatArg) 4068fe6060f1SDimitry Andric return; 4069fe6060f1SDimitry Andric 4070fe6060f1SDimitry Andric if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { 4071fe6060f1SDimitry Andric if (DwarfVersion < 3) 4072fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4073fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; 4074fe6060f1SDimitry Andric else if (!T.isArch64Bit()) 4075fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4076fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; 4077fe6060f1SDimitry Andric else if (!T.isOSBinFormatELF()) 4078fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4079fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "ELF platforms"; 4080fe6060f1SDimitry Andric } 4081fe6060f1SDimitry Andric 4082fe6060f1SDimitry Andric DwarfFormatArg->render(Args, CmdArgs); 4083fe6060f1SDimitry Andric } 4084fe6060f1SDimitry Andric 4085e8d8bef9SDimitry Andric static void renderDebugOptions(const ToolChain &TC, const Driver &D, 40860b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 4087e8d8bef9SDimitry Andric bool EmitCodeView, bool IRInput, 4088e8d8bef9SDimitry Andric ArgStringList &CmdArgs, 40890b57cec5SDimitry Andric codegenoptions::DebugInfoKind &DebugInfoKind, 40900b57cec5SDimitry Andric DwarfFissionKind &DwarfFission) { 40910b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_info_for_profiling, 40920b57cec5SDimitry Andric options::OPT_fno_debug_info_for_profiling, false) && 40930b57cec5SDimitry Andric checkDebugInfoOption( 40940b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC)) 40950b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-info-for-profiling"); 40960b57cec5SDimitry Andric 40970b57cec5SDimitry Andric // The 'g' groups options involve a somewhat intricate sequence of decisions 40980b57cec5SDimitry Andric // about what to pass from the driver to the frontend, but by the time they 40990b57cec5SDimitry Andric // reach cc1 they've been factored into three well-defined orthogonal choices: 41000b57cec5SDimitry Andric // * what level of debug info to generate 41010b57cec5SDimitry Andric // * what dwarf version to write 41020b57cec5SDimitry Andric // * what debugger tuning to use 41030b57cec5SDimitry Andric // This avoids having to monkey around further in cc1 other than to disable 41040b57cec5SDimitry Andric // codeview if not running in a Windows environment. Perhaps even that 41050b57cec5SDimitry Andric // decision should be made in the driver as well though. 41060b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning(); 41070b57cec5SDimitry Andric 41080b57cec5SDimitry Andric bool SplitDWARFInlining = 41090b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_dwarf_inlining, 4110480093f4SDimitry Andric options::OPT_fno_split_dwarf_inlining, false); 41110b57cec5SDimitry Andric 4112e8d8bef9SDimitry Andric // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does 4113e8d8bef9SDimitry Andric // object file generation and no IR generation, -gN should not be needed. So 4114e8d8bef9SDimitry Andric // allow -gsplit-dwarf with either -gN or IR input. 4115e8d8bef9SDimitry Andric if (IRInput || Args.hasArg(options::OPT_g_Group)) { 41160b57cec5SDimitry Andric Arg *SplitDWARFArg; 41170b57cec5SDimitry Andric DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg); 41180b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None && 41190b57cec5SDimitry Andric !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) { 41200b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 41210b57cec5SDimitry Andric SplitDWARFInlining = false; 41220b57cec5SDimitry Andric } 4123e8d8bef9SDimitry Andric } 4124e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 4125fe6060f1SDimitry Andric DebugInfoKind = codegenoptions::DebugInfoConstructor; 41260b57cec5SDimitry Andric 41270b57cec5SDimitry Andric // If the last option explicitly specified a debug-info level, use it. 41280b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC) && 41290b57cec5SDimitry Andric A->getOption().matches(options::OPT_gN_Group)) { 41300b57cec5SDimitry Andric DebugInfoKind = DebugLevelToInfoKind(*A); 41310b57cec5SDimitry Andric // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more 41320b57cec5SDimitry Andric // complicated if you've disabled inline info in the skeleton CUs 41330b57cec5SDimitry Andric // (SplitDWARFInlining) - then there's value in composing split-dwarf and 41340b57cec5SDimitry Andric // line-tables-only, so let those compose naturally in that case. 41350b57cec5SDimitry Andric if (DebugInfoKind == codegenoptions::NoDebugInfo || 41360b57cec5SDimitry Andric DebugInfoKind == codegenoptions::DebugDirectivesOnly || 41370b57cec5SDimitry Andric (DebugInfoKind == codegenoptions::DebugLineTablesOnly && 41380b57cec5SDimitry Andric SplitDWARFInlining)) 41390b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 41400b57cec5SDimitry Andric } 41410b57cec5SDimitry Andric } 41420b57cec5SDimitry Andric 41430b57cec5SDimitry Andric // If a debugger tuning argument appeared, remember it. 4144bdd1243dSDimitry Andric bool HasDebuggerTuning = false; 41450b57cec5SDimitry Andric if (const Arg *A = 41460b57cec5SDimitry Andric Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { 4147bdd1243dSDimitry Andric HasDebuggerTuning = true; 41480b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 41490b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_glldb)) 41500b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::LLDB; 41510b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_gsce)) 41520b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::SCE; 4153fe6060f1SDimitry Andric else if (A->getOption().matches(options::OPT_gdbx)) 4154fe6060f1SDimitry Andric DebuggerTuning = llvm::DebuggerKind::DBX; 41550b57cec5SDimitry Andric else 41560b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::GDB; 41570b57cec5SDimitry Andric } 41580b57cec5SDimitry Andric } 41590b57cec5SDimitry Andric 41600b57cec5SDimitry Andric // If a -gdwarf argument appeared, remember it. 4161480093f4SDimitry Andric bool EmitDwarf = false; 4162bdd1243dSDimitry Andric if (const Arg *A = getDwarfNArg(Args)) 4163bdd1243dSDimitry Andric EmitDwarf = checkDebugInfoOption(A, Args, D, TC); 41640b57cec5SDimitry Andric 4165bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) 4166bdd1243dSDimitry Andric EmitCodeView = checkDebugInfoOption(A, Args, D, TC); 41670b57cec5SDimitry Andric 41680b57cec5SDimitry Andric // If the user asked for debug info but did not explicitly specify -gcodeview 41690b57cec5SDimitry Andric // or -gdwarf, ask the toolchain for the default format. 4170480093f4SDimitry Andric if (!EmitCodeView && !EmitDwarf && 41710b57cec5SDimitry Andric DebugInfoKind != codegenoptions::NoDebugInfo) { 41720b57cec5SDimitry Andric switch (TC.getDefaultDebugFormat()) { 41730b57cec5SDimitry Andric case codegenoptions::DIF_CodeView: 41740b57cec5SDimitry Andric EmitCodeView = true; 41750b57cec5SDimitry Andric break; 41760b57cec5SDimitry Andric case codegenoptions::DIF_DWARF: 4177480093f4SDimitry Andric EmitDwarf = true; 41780b57cec5SDimitry Andric break; 41790b57cec5SDimitry Andric } 41800b57cec5SDimitry Andric } 41810b57cec5SDimitry Andric 4182e8d8bef9SDimitry Andric unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user 4183e8d8bef9SDimitry Andric unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may 4184e8d8bef9SDimitry Andric // be lower than what the user wanted. 4185480093f4SDimitry Andric if (EmitDwarf) { 4186bdd1243dSDimitry Andric RequestedDWARFVersion = getDwarfVersion(TC, Args); 4187e8d8bef9SDimitry Andric // Clamp effective DWARF version to the max supported by the toolchain. 4188e8d8bef9SDimitry Andric EffectiveDWARFVersion = 4189e8d8bef9SDimitry Andric std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion()); 4190bdd1243dSDimitry Andric } else { 4191bdd1243dSDimitry Andric Args.ClaimAllArgs(options::OPT_fdebug_default_version); 4192480093f4SDimitry Andric } 4193480093f4SDimitry Andric 41940b57cec5SDimitry Andric // -gline-directives-only supported only for the DWARF debug info. 4195e8d8bef9SDimitry Andric if (RequestedDWARFVersion == 0 && 4196e8d8bef9SDimitry Andric DebugInfoKind == codegenoptions::DebugDirectivesOnly) 41970b57cec5SDimitry Andric DebugInfoKind = codegenoptions::NoDebugInfo; 41980b57cec5SDimitry Andric 4199fe6060f1SDimitry Andric // strict DWARF is set to false by default. But for DBX, we need it to be set 4200fe6060f1SDimitry Andric // as true by default. 4201fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf)) 4202fe6060f1SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4203fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf, 4204fe6060f1SDimitry Andric DebuggerTuning == llvm::DebuggerKind::DBX)) 4205fe6060f1SDimitry Andric CmdArgs.push_back("-gstrict-dwarf"); 4206fe6060f1SDimitry Andric 42070b57cec5SDimitry Andric // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags. 42080b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_flags_Group); 42090b57cec5SDimitry Andric 42100b57cec5SDimitry Andric // Column info is included by default for everything except SCE and 42110b57cec5SDimitry Andric // CodeView. Clang doesn't track end columns, just starting columns, which, 42120b57cec5SDimitry Andric // in theory, is fine for CodeView (and PDB). In practice, however, the 4213fe6060f1SDimitry Andric // Microsoft debuggers don't handle missing end columns well, and the AIX 4214fe6060f1SDimitry Andric // debugger DBX also doesn't handle the columns well, so it's better not to 4215fe6060f1SDimitry Andric // include any column info. 42160b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info)) 42170b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 42185ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info, 4219fe6060f1SDimitry Andric !EmitCodeView && 4220fe6060f1SDimitry Andric (DebuggerTuning != llvm::DebuggerKind::SCE && 4221fe6060f1SDimitry Andric DebuggerTuning != llvm::DebuggerKind::DBX))) 42225ffd83dbSDimitry Andric CmdArgs.push_back("-gno-column-info"); 42230b57cec5SDimitry Andric 42240b57cec5SDimitry Andric // FIXME: Move backend command line options to the module. 4225bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_gmodules, options::OPT_gno_modules, false)) { 4226bdd1243dSDimitry Andric // If -gline-tables-only or -gline-directives-only is the last option it 4227bdd1243dSDimitry Andric // wins. 4228bdd1243dSDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_gmodules), Args, D, 4229bdd1243dSDimitry Andric TC)) { 42300b57cec5SDimitry Andric if (DebugInfoKind != codegenoptions::DebugLineTablesOnly && 42310b57cec5SDimitry Andric DebugInfoKind != codegenoptions::DebugDirectivesOnly) { 4232fe6060f1SDimitry Andric DebugInfoKind = codegenoptions::DebugInfoConstructor; 42330b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-ext-refs"); 42340b57cec5SDimitry Andric CmdArgs.push_back("-fmodule-format=obj"); 42350b57cec5SDimitry Andric } 42360b57cec5SDimitry Andric } 4237bdd1243dSDimitry Andric } 42380b57cec5SDimitry Andric 4239fe6060f1SDimitry Andric if (T.isOSBinFormatELF() && SplitDWARFInlining) 4240fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-dwarf-inlining"); 42410b57cec5SDimitry Andric 42420b57cec5SDimitry Andric // After we've dealt with all combinations of things that could 42430b57cec5SDimitry Andric // make DebugInfoKind be other than None or DebugLineTablesOnly, 42440b57cec5SDimitry Andric // figure out if we need to "upgrade" it to standalone debug info. 42450b57cec5SDimitry Andric // We parse these two '-f' options whether or not they will be used, 42460b57cec5SDimitry Andric // to claim them even if you wrote "-fstandalone-debug -gline-tables-only" 42470b57cec5SDimitry Andric bool NeedFullDebug = Args.hasFlag( 42480b57cec5SDimitry Andric options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug, 42490b57cec5SDimitry Andric DebuggerTuning == llvm::DebuggerKind::LLDB || 42500b57cec5SDimitry Andric TC.GetDefaultStandaloneDebug()); 42510b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug)) 42520b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4253e8d8bef9SDimitry Andric 4254fe6060f1SDimitry Andric if (DebugInfoKind == codegenoptions::LimitedDebugInfo || 4255fe6060f1SDimitry Andric DebugInfoKind == codegenoptions::DebugInfoConstructor) { 4256e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types, 4257e8d8bef9SDimitry Andric options::OPT_feliminate_unused_debug_types, false)) 4258e8d8bef9SDimitry Andric DebugInfoKind = codegenoptions::UnusedTypeInfo; 4259e8d8bef9SDimitry Andric else if (NeedFullDebug) 42600b57cec5SDimitry Andric DebugInfoKind = codegenoptions::FullDebugInfo; 4261e8d8bef9SDimitry Andric } 42620b57cec5SDimitry Andric 42630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, 42640b57cec5SDimitry Andric false)) { 42650b57cec5SDimitry Andric // Source embedding is a vendor extension to DWARF v5. By now we have 42660b57cec5SDimitry Andric // checked if a DWARF version was stated explicitly, and have otherwise 42670b57cec5SDimitry Andric // fallen back to the target default, so if this is still not at least 5 42680b57cec5SDimitry Andric // we emit an error. 42690b57cec5SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gembed_source); 4270e8d8bef9SDimitry Andric if (RequestedDWARFVersion < 5) 42710b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 42720b57cec5SDimitry Andric << A->getAsString(Args) << "-gdwarf-5"; 4273e8d8bef9SDimitry Andric else if (EffectiveDWARFVersion < 5) 4274e8d8bef9SDimitry Andric // The toolchain has reduced allowed dwarf version, so we can't enable 4275e8d8bef9SDimitry Andric // -gembed-source. 4276e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_dwarf_version_limited_by_target) 4277e8d8bef9SDimitry Andric << A->getAsString(Args) << TC.getTripleString() << 5 4278e8d8bef9SDimitry Andric << EffectiveDWARFVersion; 42790b57cec5SDimitry Andric else if (checkDebugInfoOption(A, Args, D, TC)) 42800b57cec5SDimitry Andric CmdArgs.push_back("-gembed-source"); 42810b57cec5SDimitry Andric } 42820b57cec5SDimitry Andric 42830b57cec5SDimitry Andric if (EmitCodeView) { 42840b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview"); 42850b57cec5SDimitry Andric 4286bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_gcodeview_ghash, 4287bdd1243dSDimitry Andric options::OPT_gno_codeview_ghash); 4288bdd1243dSDimitry Andric 4289bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_gcodeview_command_line, 4290bdd1243dSDimitry Andric options::OPT_gno_codeview_command_line); 42910b57cec5SDimitry Andric } 42920b57cec5SDimitry Andric 4293bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_ginline_line_tables, 4294bdd1243dSDimitry Andric options::OPT_gno_inline_line_tables); 4295480093f4SDimitry Andric 4296480093f4SDimitry Andric // When emitting remarks, we need at least debug lines in the output. 4297480093f4SDimitry Andric if (willEmitRemarks(Args) && 4298480093f4SDimitry Andric DebugInfoKind <= codegenoptions::DebugDirectivesOnly) 4299480093f4SDimitry Andric DebugInfoKind = codegenoptions::DebugLineTablesOnly; 4300480093f4SDimitry Andric 4301e8d8bef9SDimitry Andric // Adjust the debug info kind for the given toolchain. 4302e8d8bef9SDimitry Andric TC.adjustDebugInfoKind(DebugInfoKind, Args); 4303e8d8bef9SDimitry Andric 4304bdd1243dSDimitry Andric // On AIX, the debugger tuning option can be omitted if it is not explicitly 4305bdd1243dSDimitry Andric // set. 4306e8d8bef9SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion, 4307bdd1243dSDimitry Andric T.isOSAIX() && !HasDebuggerTuning 4308bdd1243dSDimitry Andric ? llvm::DebuggerKind::Default 4309bdd1243dSDimitry Andric : DebuggerTuning); 43100b57cec5SDimitry Andric 43110b57cec5SDimitry Andric // -fdebug-macro turns on macro debug info generation. 43120b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro, 43130b57cec5SDimitry Andric false)) 43140b57cec5SDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args, 43150b57cec5SDimitry Andric D, TC)) 43160b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-macro"); 43170b57cec5SDimitry Andric 43180b57cec5SDimitry Andric // -ggnu-pubnames turns on gnu style pubnames in the backend. 43190b57cec5SDimitry Andric const auto *PubnamesArg = 43200b57cec5SDimitry Andric Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames, 43210b57cec5SDimitry Andric options::OPT_gpubnames, options::OPT_gno_pubnames); 43220b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None || 43230b57cec5SDimitry Andric (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) 43240b57cec5SDimitry Andric if (!PubnamesArg || 43250b57cec5SDimitry Andric (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && 43260b57cec5SDimitry Andric !PubnamesArg->getOption().matches(options::OPT_gno_pubnames))) 43270b57cec5SDimitry Andric CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches( 43280b57cec5SDimitry Andric options::OPT_gpubnames) 43290b57cec5SDimitry Andric ? "-gpubnames" 43300b57cec5SDimitry Andric : "-ggnu-pubnames"); 4331349cc55cSDimitry Andric const auto *SimpleTemplateNamesArg = 433281ad6265SDimitry Andric Args.getLastArg(options::OPT_gsimple_template_names, 433381ad6265SDimitry Andric options::OPT_gno_simple_template_names); 4334349cc55cSDimitry Andric bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; 4335349cc55cSDimitry Andric if (SimpleTemplateNamesArg && 4336349cc55cSDimitry Andric checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { 4337349cc55cSDimitry Andric const auto &Opt = SimpleTemplateNamesArg->getOption(); 4338349cc55cSDimitry Andric if (Opt.matches(options::OPT_gsimple_template_names)) { 4339349cc55cSDimitry Andric ForwardTemplateParams = true; 4340349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=simple"); 4341349cc55cSDimitry Andric } 4342349cc55cSDimitry Andric } 43430b57cec5SDimitry Andric 4344bdd1243dSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gsrc_hash_EQ)) { 4345bdd1243dSDimitry Andric StringRef v = A->getValue(); 4346bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-gsrc-hash=" + v)); 43470b57cec5SDimitry Andric } 43480b57cec5SDimitry Andric 4349bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdebug_ranges_base_address, 4350bdd1243dSDimitry Andric options::OPT_fno_debug_ranges_base_address); 4351bdd1243dSDimitry Andric 43520b57cec5SDimitry Andric // -gdwarf-aranges turns on the emission of the aranges section in the 43530b57cec5SDimitry Andric // backend. 43540b57cec5SDimitry Andric // Always enabled for SCE tuning. 43550b57cec5SDimitry Andric bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE; 43560b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges)) 43570b57cec5SDimitry Andric NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges; 43580b57cec5SDimitry Andric if (NeedAranges) { 43590b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 43600b57cec5SDimitry Andric CmdArgs.push_back("-generate-arange-section"); 43610b57cec5SDimitry Andric } 43620b57cec5SDimitry Andric 4363bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_dwarf_frame, 4364bdd1243dSDimitry Andric options::OPT_fno_force_dwarf_frame); 4365480093f4SDimitry Andric 43660b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_types_section, 43670b57cec5SDimitry Andric options::OPT_fno_debug_types_section, false)) { 4368e8d8bef9SDimitry Andric if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) { 43690b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 43700b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fdebug_types_section) 43710b57cec5SDimitry Andric ->getAsString(Args) 43720b57cec5SDimitry Andric << T.getTriple(); 43730b57cec5SDimitry Andric } else if (checkDebugInfoOption( 43740b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_types_section), Args, D, 43750b57cec5SDimitry Andric TC)) { 43760b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 43770b57cec5SDimitry Andric CmdArgs.push_back("-generate-type-units"); 43780b57cec5SDimitry Andric } 43790b57cec5SDimitry Andric } 43800b57cec5SDimitry Andric 4381fe6060f1SDimitry Andric // To avoid join/split of directory+filename, the integrated assembler prefers 4382fe6060f1SDimitry Andric // the directory form of .file on all DWARF versions. GNU as doesn't allow the 4383fe6060f1SDimitry Andric // form before DWARF v5. 4384fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, 4385fe6060f1SDimitry Andric options::OPT_fno_dwarf_directory_asm, 4386fe6060f1SDimitry Andric TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) 4387fe6060f1SDimitry Andric CmdArgs.push_back("-fno-dwarf-directory-asm"); 4388fe6060f1SDimitry Andric 43890b57cec5SDimitry Andric // Decide how to render forward declarations of template instantiations. 43900b57cec5SDimitry Andric // SCE wants full descriptions, others just get them in the name. 4391349cc55cSDimitry Andric if (ForwardTemplateParams) 43920b57cec5SDimitry Andric CmdArgs.push_back("-debug-forward-template-params"); 43930b57cec5SDimitry Andric 43940b57cec5SDimitry Andric // Do we need to explicitly import anonymous namespaces into the parent 43950b57cec5SDimitry Andric // scope? 43960b57cec5SDimitry Andric if (DebuggerTuning == llvm::DebuggerKind::SCE) 43970b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-explicit-import"); 43980b57cec5SDimitry Andric 4399fe6060f1SDimitry Andric renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion); 44000b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); 44010b57cec5SDimitry Andric } 44020b57cec5SDimitry Andric 4403bdd1243dSDimitry Andric static void ProcessVSRuntimeLibrary(const ArgList &Args, 4404bdd1243dSDimitry Andric ArgStringList &CmdArgs) { 4405bdd1243dSDimitry Andric unsigned RTOptionID = options::OPT__SLASH_MT; 4406bdd1243dSDimitry Andric 4407bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4408bdd1243dSDimitry Andric // The /LDd option implies /MTd. The dependent lib part can be overridden, 4409bdd1243dSDimitry Andric // but defining _DEBUG is sticky. 4410bdd1243dSDimitry Andric RTOptionID = options::OPT__SLASH_MTd; 4411bdd1243dSDimitry Andric 4412bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) 4413bdd1243dSDimitry Andric RTOptionID = A->getOption().getID(); 4414bdd1243dSDimitry Andric 4415bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { 4416bdd1243dSDimitry Andric RTOptionID = llvm::StringSwitch<unsigned>(A->getValue()) 4417bdd1243dSDimitry Andric .Case("static", options::OPT__SLASH_MT) 4418bdd1243dSDimitry Andric .Case("static_dbg", options::OPT__SLASH_MTd) 4419bdd1243dSDimitry Andric .Case("dll", options::OPT__SLASH_MD) 4420bdd1243dSDimitry Andric .Case("dll_dbg", options::OPT__SLASH_MDd) 4421bdd1243dSDimitry Andric .Default(options::OPT__SLASH_MT); 4422bdd1243dSDimitry Andric } 4423bdd1243dSDimitry Andric 4424bdd1243dSDimitry Andric StringRef FlagForCRT; 4425bdd1243dSDimitry Andric switch (RTOptionID) { 4426bdd1243dSDimitry Andric case options::OPT__SLASH_MD: 4427bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4428bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4429bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4430bdd1243dSDimitry Andric CmdArgs.push_back("-D_DLL"); 4431bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=msvcrt"; 4432bdd1243dSDimitry Andric break; 4433bdd1243dSDimitry Andric case options::OPT__SLASH_MDd: 4434bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4435bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4436bdd1243dSDimitry Andric CmdArgs.push_back("-D_DLL"); 4437bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=msvcrtd"; 4438bdd1243dSDimitry Andric break; 4439bdd1243dSDimitry Andric case options::OPT__SLASH_MT: 4440bdd1243dSDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 4441bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4442bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4443bdd1243dSDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 4444bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=libcmt"; 4445bdd1243dSDimitry Andric break; 4446bdd1243dSDimitry Andric case options::OPT__SLASH_MTd: 4447bdd1243dSDimitry Andric CmdArgs.push_back("-D_DEBUG"); 4448bdd1243dSDimitry Andric CmdArgs.push_back("-D_MT"); 4449bdd1243dSDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 4450bdd1243dSDimitry Andric FlagForCRT = "--dependent-lib=libcmtd"; 4451bdd1243dSDimitry Andric break; 4452bdd1243dSDimitry Andric default: 4453bdd1243dSDimitry Andric llvm_unreachable("Unexpected option ID."); 4454bdd1243dSDimitry Andric } 4455bdd1243dSDimitry Andric 4456bdd1243dSDimitry Andric if (Args.hasArg(options::OPT_fms_omit_default_lib)) { 4457bdd1243dSDimitry Andric CmdArgs.push_back("-D_VC_NODEFAULTLIB"); 4458bdd1243dSDimitry Andric } else { 4459bdd1243dSDimitry Andric CmdArgs.push_back(FlagForCRT.data()); 4460bdd1243dSDimitry Andric 4461bdd1243dSDimitry Andric // This provides POSIX compatibility (maps 'open' to '_open'), which most 4462bdd1243dSDimitry Andric // users want. The /Za flag to cl.exe turns this off, but it's not 4463bdd1243dSDimitry Andric // implemented in clang. 4464bdd1243dSDimitry Andric CmdArgs.push_back("--dependent-lib=oldnames"); 4465bdd1243dSDimitry Andric } 4466bdd1243dSDimitry Andric } 4467bdd1243dSDimitry Andric 44680b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA, 44690b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 44700b57cec5SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 44710b57cec5SDimitry Andric const auto &TC = getToolChain(); 44720b57cec5SDimitry Andric const llvm::Triple &RawTriple = TC.getTriple(); 44730b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 44740b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 44750b57cec5SDimitry Andric 44760b57cec5SDimitry Andric bool KernelOrKext = 44770b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 44780b57cec5SDimitry Andric const Driver &D = TC.getDriver(); 44790b57cec5SDimitry Andric ArgStringList CmdArgs; 44800b57cec5SDimitry Andric 44810b57cec5SDimitry Andric assert(Inputs.size() >= 1 && "Must have at least one input."); 44820b57cec5SDimitry Andric // CUDA/HIP compilation may have multiple inputs (source file + results of 44830b57cec5SDimitry Andric // device-side compilations). OpenMP device jobs also take the host IR as a 44840b57cec5SDimitry Andric // second input. Module precompilation accepts a list of header files to 448581ad6265SDimitry Andric // include as part of the module. API extraction accepts a list of header 448681ad6265SDimitry Andric // files whose API information is emitted in the output. All other jobs are 448781ad6265SDimitry Andric // expected to have exactly one input. 44880b57cec5SDimitry Andric bool IsCuda = JA.isOffloading(Action::OFK_Cuda); 4489fe6060f1SDimitry Andric bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda); 44900b57cec5SDimitry Andric bool IsHIP = JA.isOffloading(Action::OFK_HIP); 4491fe6060f1SDimitry Andric bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP); 44920b57cec5SDimitry Andric bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); 449381ad6265SDimitry Andric bool IsExtractAPI = isa<ExtractAPIJobAction>(JA); 4494fe6060f1SDimitry Andric bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || 4495fe6060f1SDimitry Andric JA.isDeviceOffloading(Action::OFK_Host)); 449681ad6265SDimitry Andric bool IsHostOffloadingAction = 4497bdd1243dSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP) || 449881ad6265SDimitry Andric (JA.isHostOffloading(C.getActiveOffloadKinds()) && 449981ad6265SDimitry Andric Args.hasFlag(options::OPT_offload_new_driver, 450081ad6265SDimitry Andric options::OPT_no_offload_new_driver, false)); 450181ad6265SDimitry Andric 4502753f127fSDimitry Andric bool IsRDCMode = 4503753f127fSDimitry Andric Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false); 4504fe6060f1SDimitry Andric bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction); 4505fe6060f1SDimitry Andric auto LTOMode = D.getLTOMode(IsDeviceOffloadAction); 45060b57cec5SDimitry Andric 450781ad6265SDimitry Andric // Extract API doesn't have a main input file, so invent a fake one as a 450881ad6265SDimitry Andric // placeholder. 450981ad6265SDimitry Andric InputInfo ExtractAPIPlaceholderInput(Inputs[0].getType(), "extract-api", 451081ad6265SDimitry Andric "extract-api"); 451181ad6265SDimitry Andric 4512bdd1243dSDimitry Andric const InputInfo &Input = 4513bdd1243dSDimitry Andric IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0]; 45140b57cec5SDimitry Andric 451581ad6265SDimitry Andric InputInfoList ExtractAPIInputs; 451681ad6265SDimitry Andric InputInfoList HostOffloadingInputs; 45170b57cec5SDimitry Andric const InputInfo *CudaDeviceInput = nullptr; 45180b57cec5SDimitry Andric const InputInfo *OpenMPDeviceInput = nullptr; 45190b57cec5SDimitry Andric for (const InputInfo &I : Inputs) { 4520bdd1243dSDimitry Andric if (&I == &Input || I.getType() == types::TY_Nothing) { 4521bdd1243dSDimitry Andric // This is the primary input or contains nothing. 452281ad6265SDimitry Andric } else if (IsExtractAPI) { 452381ad6265SDimitry Andric auto ExpectedInputType = ExtractAPIPlaceholderInput.getType(); 452481ad6265SDimitry Andric if (I.getType() != ExpectedInputType) { 452581ad6265SDimitry Andric D.Diag(diag::err_drv_extract_api_wrong_kind) 452681ad6265SDimitry Andric << I.getFilename() << types::getTypeName(I.getType()) 452781ad6265SDimitry Andric << types::getTypeName(ExpectedInputType); 452881ad6265SDimitry Andric } 452981ad6265SDimitry Andric ExtractAPIInputs.push_back(I); 453081ad6265SDimitry Andric } else if (IsHostOffloadingAction) { 453181ad6265SDimitry Andric HostOffloadingInputs.push_back(I); 45320b57cec5SDimitry Andric } else if ((IsCuda || IsHIP) && !CudaDeviceInput) { 45330b57cec5SDimitry Andric CudaDeviceInput = &I; 45340b57cec5SDimitry Andric } else if (IsOpenMPDevice && !OpenMPDeviceInput) { 45350b57cec5SDimitry Andric OpenMPDeviceInput = &I; 45360b57cec5SDimitry Andric } else { 45370b57cec5SDimitry Andric llvm_unreachable("unexpectedly given multiple inputs"); 45380b57cec5SDimitry Andric } 45390b57cec5SDimitry Andric } 45400b57cec5SDimitry Andric 45415ffd83dbSDimitry Andric const llvm::Triple *AuxTriple = 45425ffd83dbSDimitry Andric (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr; 45430b57cec5SDimitry Andric bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment(); 45440b57cec5SDimitry Andric bool IsIAMCU = RawTriple.isOSIAMCU(); 45450b57cec5SDimitry Andric 45460b57cec5SDimitry Andric // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in 45470b57cec5SDimitry Andric // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not 45480b57cec5SDimitry Andric // Windows), we need to pass Windows-specific flags to cc1. 45490b57cec5SDimitry Andric if (IsCuda || IsHIP) 45500b57cec5SDimitry Andric IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment(); 45510b57cec5SDimitry Andric 45520b57cec5SDimitry Andric // C++ is not supported for IAMCU. 45530b57cec5SDimitry Andric if (IsIAMCU && types::isCXX(Input.getType())) 45540b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU"; 45550b57cec5SDimitry Andric 45560b57cec5SDimitry Andric // Invoke ourselves in -cc1 mode. 45570b57cec5SDimitry Andric // 45580b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 45590b57cec5SDimitry Andric CmdArgs.push_back("-cc1"); 45600b57cec5SDimitry Andric 45610b57cec5SDimitry Andric // Add the "effective" target triple. 45620b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 45630b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 45640b57cec5SDimitry Andric 45650b57cec5SDimitry Andric if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) { 45660b57cec5SDimitry Andric DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args); 45670b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_MJ); 4568a7dea167SDimitry Andric } else if (const Arg *GenCDBFragment = 4569a7dea167SDimitry Andric Args.getLastArg(options::OPT_gen_cdb_fragment_path)) { 4570a7dea167SDimitry Andric DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C, 4571a7dea167SDimitry Andric TripleStr, Output, Input, Args); 4572a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path); 45730b57cec5SDimitry Andric } 45740b57cec5SDimitry Andric 45750b57cec5SDimitry Andric if (IsCuda || IsHIP) { 45760b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for a CUDA/HIP device 45770b57cec5SDimitry Andric // and vice-versa. 45780b57cec5SDimitry Andric std::string NormalizedTriple; 45790b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 45800b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 45810b57cec5SDimitry Andric NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>() 45820b57cec5SDimitry Andric ->getTriple() 45830b57cec5SDimitry Andric .normalize(); 45840b57cec5SDimitry Andric else { 45850b57cec5SDimitry Andric // Host-side compilation. 45860b57cec5SDimitry Andric NormalizedTriple = 45870b57cec5SDimitry Andric (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() 45880b57cec5SDimitry Andric : C.getSingleOffloadToolChain<Action::OFK_HIP>()) 45890b57cec5SDimitry Andric ->getTriple() 45900b57cec5SDimitry Andric .normalize(); 45910b57cec5SDimitry Andric if (IsCuda) { 45920b57cec5SDimitry Andric // We need to figure out which CUDA version we're compiling for, as that 45930b57cec5SDimitry Andric // determines how we load and launch GPU kernels. 45940b57cec5SDimitry Andric auto *CTC = static_cast<const toolchains::CudaToolChain *>( 45950b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 45960b57cec5SDimitry Andric assert(CTC && "Expected valid CUDA Toolchain."); 45970b57cec5SDimitry Andric if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN) 45980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 45990b57cec5SDimitry Andric Twine("-target-sdk-version=") + 46000b57cec5SDimitry Andric CudaVersionToString(CTC->CudaInstallation.version()))); 46010b57cec5SDimitry Andric } 46020b57cec5SDimitry Andric } 46030b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 46040b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 46050b57cec5SDimitry Andric } 46060b57cec5SDimitry Andric 46075ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { 46085ffd83dbSDimitry Andric CmdArgs.push_back("-fsycl-is-device"); 46095ffd83dbSDimitry Andric 46105ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { 46115ffd83dbSDimitry Andric A->render(Args, CmdArgs); 46125ffd83dbSDimitry Andric } else { 4613fe6060f1SDimitry Andric // Ensure the default version in SYCL mode is 2020. 4614fe6060f1SDimitry Andric CmdArgs.push_back("-sycl-std=2020"); 46155ffd83dbSDimitry Andric } 46165ffd83dbSDimitry Andric } 46175ffd83dbSDimitry Andric 46180b57cec5SDimitry Andric if (IsOpenMPDevice) { 46190b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for an OpenMP device. 46200b57cec5SDimitry Andric std::string NormalizedTriple = 46210b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Host>() 46220b57cec5SDimitry Andric ->getTriple() 46230b57cec5SDimitry Andric .normalize(); 46240b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 46250b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 46260b57cec5SDimitry Andric } 46270b57cec5SDimitry Andric 46280b57cec5SDimitry Andric if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm || 46290b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::thumb)) { 46300b57cec5SDimitry Andric unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6; 46315ffd83dbSDimitry Andric unsigned Version = 0; 46325ffd83dbSDimitry Andric bool Failure = 46335ffd83dbSDimitry Andric Triple.getArchName().substr(Offset).consumeInteger(10, Version); 46345ffd83dbSDimitry Andric if (Failure || Version < 7) 46350b57cec5SDimitry Andric D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName() 46360b57cec5SDimitry Andric << TripleStr; 46370b57cec5SDimitry Andric } 46380b57cec5SDimitry Andric 46390b57cec5SDimitry Andric // Push all default warning arguments that are specific to 46400b57cec5SDimitry Andric // the given target. These come before user provided warning options 46410b57cec5SDimitry Andric // are provided. 46420b57cec5SDimitry Andric TC.addClangWarningOptions(CmdArgs); 46430b57cec5SDimitry Andric 4644fe6060f1SDimitry Andric // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions. 4645bdd1243dSDimitry Andric if (Triple.isSPIR() || Triple.isSPIRV()) 4646fe6060f1SDimitry Andric CmdArgs.push_back("-Wspir-compat"); 4647fe6060f1SDimitry Andric 46480b57cec5SDimitry Andric // Select the appropriate action. 46490b57cec5SDimitry Andric RewriteKind rewriteKind = RK_None; 46500b57cec5SDimitry Andric 4651a7dea167SDimitry Andric // If CollectArgsForIntegratedAssembler() isn't called below, claim the args 4652a7dea167SDimitry Andric // it claims when not running an assembler. Otherwise, clang would emit 4653a7dea167SDimitry Andric // "argument unused" warnings for assembler flags when e.g. adding "-E" to 4654a7dea167SDimitry Andric // flags while debugging something. That'd be somewhat inconvenient, and it's 4655a7dea167SDimitry Andric // also inconsistent with most other flags -- we don't warn on 4656a7dea167SDimitry Andric // -ffunction-sections not being used in -E mode either for example, even 4657a7dea167SDimitry Andric // though it's not really used either. 4658a7dea167SDimitry Andric if (!isa<AssembleJobAction>(JA)) { 4659a7dea167SDimitry Andric // The args claimed here should match the args used in 4660a7dea167SDimitry Andric // CollectArgsForIntegratedAssembler(). 4661a7dea167SDimitry Andric if (TC.useIntegratedAs()) { 4662a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mrelax_all); 4663a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_relax_all); 4664a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible); 4665a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible); 4666a7dea167SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 4667a7dea167SDimitry Andric case llvm::Triple::arm: 4668a7dea167SDimitry Andric case llvm::Triple::armeb: 4669a7dea167SDimitry Andric case llvm::Triple::thumb: 4670a7dea167SDimitry Andric case llvm::Triple::thumbeb: 4671a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ); 4672a7dea167SDimitry Andric break; 4673a7dea167SDimitry Andric default: 4674a7dea167SDimitry Andric break; 4675a7dea167SDimitry Andric } 4676a7dea167SDimitry Andric } 4677a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Wa_COMMA); 4678a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Xassembler); 467981ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_femit_dwarf_unwind_EQ); 4680a7dea167SDimitry Andric } 4681a7dea167SDimitry Andric 46820b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) { 46830b57cec5SDimitry Andric assert(JA.getType() == types::TY_Plist && "Invalid output type."); 46840b57cec5SDimitry Andric CmdArgs.push_back("-analyze"); 46850b57cec5SDimitry Andric } else if (isa<MigrateJobAction>(JA)) { 46860b57cec5SDimitry Andric CmdArgs.push_back("-migrate"); 46870b57cec5SDimitry Andric } else if (isa<PreprocessJobAction>(JA)) { 46880b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) 46890b57cec5SDimitry Andric CmdArgs.push_back("-Eonly"); 46900b57cec5SDimitry Andric else { 46910b57cec5SDimitry Andric CmdArgs.push_back("-E"); 46920b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc) && 46930b57cec5SDimitry Andric !Args.hasArg(options::OPT_g_Group)) 46940b57cec5SDimitry Andric CmdArgs.push_back("-P"); 469581ad6265SDimitry Andric else if (JA.getType() == types::TY_PP_CXXHeaderUnit) 469681ad6265SDimitry Andric CmdArgs.push_back("-fdirectives-only"); 46970b57cec5SDimitry Andric } 46980b57cec5SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 46990b57cec5SDimitry Andric CmdArgs.push_back("-emit-obj"); 47000b57cec5SDimitry Andric 47010b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 47020b57cec5SDimitry Andric 47030b57cec5SDimitry Andric // Also ignore explicit -force_cpusubtype_ALL option. 47040b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 47050b57cec5SDimitry Andric } else if (isa<PrecompileJobAction>(JA)) { 47060b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) 47070b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 47080b57cec5SDimitry Andric else if (JA.getType() == types::TY_ModuleFile) 4709bdd1243dSDimitry Andric CmdArgs.push_back("-emit-module-interface"); 471081ad6265SDimitry Andric else if (JA.getType() == types::TY_HeaderUnit) 471181ad6265SDimitry Andric CmdArgs.push_back("-emit-header-unit"); 47120b57cec5SDimitry Andric else 47130b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 47140b57cec5SDimitry Andric } else if (isa<VerifyPCHJobAction>(JA)) { 47150b57cec5SDimitry Andric CmdArgs.push_back("-verify-pch"); 471681ad6265SDimitry Andric } else if (isa<ExtractAPIJobAction>(JA)) { 471781ad6265SDimitry Andric assert(JA.getType() == types::TY_API_INFO && 471881ad6265SDimitry Andric "Extract API actions must generate a API information."); 471981ad6265SDimitry Andric CmdArgs.push_back("-extract-api"); 472081ad6265SDimitry Andric if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ)) 472181ad6265SDimitry Andric ProductNameArg->render(Args, CmdArgs); 4722bdd1243dSDimitry Andric if (Arg *ExtractAPIIgnoresFileArg = 4723bdd1243dSDimitry Andric Args.getLastArg(options::OPT_extract_api_ignores_EQ)) 4724bdd1243dSDimitry Andric ExtractAPIIgnoresFileArg->render(Args, CmdArgs); 47250b57cec5SDimitry Andric } else { 47260b57cec5SDimitry Andric assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) && 47270b57cec5SDimitry Andric "Invalid action for clang tool."); 47280b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) { 47290b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 47300b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 47310b57cec5SDimitry Andric JA.getType() == types::TY_LTO_IR) { 47320b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm"); 47330b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 47340b57cec5SDimitry Andric JA.getType() == types::TY_LTO_BC) { 4735fe6060f1SDimitry Andric // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S 4736fe6060f1SDimitry Andric if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) && 4737fe6060f1SDimitry Andric Args.hasArg(options::OPT_emit_llvm)) { 4738fe6060f1SDimitry Andric CmdArgs.push_back("-emit-llvm"); 4739fe6060f1SDimitry Andric } else { 47400b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 4741fe6060f1SDimitry Andric } 4742a7dea167SDimitry Andric } else if (JA.getType() == types::TY_IFS || 4743a7dea167SDimitry Andric JA.getType() == types::TY_IFS_CPP) { 4744a7dea167SDimitry Andric StringRef ArgStr = 4745a7dea167SDimitry Andric Args.hasArg(options::OPT_interface_stub_version_EQ) 4746a7dea167SDimitry Andric ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ) 4747fe6060f1SDimitry Andric : "ifs-v1"; 47480b57cec5SDimitry Andric CmdArgs.push_back("-emit-interface-stubs"); 47490b57cec5SDimitry Andric CmdArgs.push_back( 4750a7dea167SDimitry Andric Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str())); 47510b57cec5SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 47520b57cec5SDimitry Andric CmdArgs.push_back("-S"); 47530b57cec5SDimitry Andric } else if (JA.getType() == types::TY_AST) { 47540b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 47550b57cec5SDimitry Andric } else if (JA.getType() == types::TY_ModuleFile) { 47560b57cec5SDimitry Andric CmdArgs.push_back("-module-file-info"); 47570b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenObjC) { 47580b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 47590b57cec5SDimitry Andric rewriteKind = RK_NonFragile; 47600b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 47610b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 47620b57cec5SDimitry Andric rewriteKind = RK_Fragile; 47630b57cec5SDimitry Andric } else { 47640b57cec5SDimitry Andric assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!"); 47650b57cec5SDimitry Andric } 47660b57cec5SDimitry Andric 47670b57cec5SDimitry Andric // Preserve use-list order by default when emitting bitcode, so that 47680b57cec5SDimitry Andric // loading the bitcode up in 'opt' or 'llc' and running passes gives the 47690b57cec5SDimitry Andric // same result as running passes here. For LTO, we don't need to preserve 47700b57cec5SDimitry Andric // the use-list order, since serialization to bitcode is part of the flow. 47710b57cec5SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 47720b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-uselists"); 47730b57cec5SDimitry Andric 477481ad6265SDimitry Andric if (IsUsingLTO) { 4775bdd1243dSDimitry Andric if (IsDeviceOffloadAction && !JA.isDeviceOffloading(Action::OFK_OpenMP) && 477681ad6265SDimitry Andric !Args.hasFlag(options::OPT_offload_new_driver, 477781ad6265SDimitry Andric options::OPT_no_offload_new_driver, false) && 477881ad6265SDimitry Andric !Triple.isAMDGPU()) { 4779fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4780fe6060f1SDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4781fe6060f1SDimitry Andric options::OPT_foffload_lto_EQ) 4782fe6060f1SDimitry Andric ->getAsString(Args) 4783fe6060f1SDimitry Andric << Triple.getTriple(); 4784bdd1243dSDimitry Andric } else if (Triple.isNVPTX() && !IsRDCMode && 4785bdd1243dSDimitry Andric JA.isDeviceOffloading(Action::OFK_Cuda)) { 4786bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_language_mode) 4787bdd1243dSDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4788bdd1243dSDimitry Andric options::OPT_foffload_lto_EQ) 4789bdd1243dSDimitry Andric ->getAsString(Args) 4790bdd1243dSDimitry Andric << "-fno-gpu-rdc"; 4791349cc55cSDimitry Andric } else { 4792349cc55cSDimitry Andric assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); 4793349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 4794349cc55cSDimitry Andric Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 4795349cc55cSDimitry Andric CmdArgs.push_back("-flto-unit"); 4796fe6060f1SDimitry Andric } 47970b57cec5SDimitry Andric } 47980b57cec5SDimitry Andric } 47990b57cec5SDimitry Andric 48000b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) { 48010b57cec5SDimitry Andric if (!types::isLLVMIR(Input.getType())) 48020b57cec5SDimitry Andric D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args); 48030b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ); 48040b57cec5SDimitry Andric } 48050b57cec5SDimitry Andric 4806480093f4SDimitry Andric if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ)) 4807480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ); 4808480093f4SDimitry Andric 48090b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 48100b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ); 48110b57cec5SDimitry Andric 4812e8d8bef9SDimitry Andric auto *MemProfArg = Args.getLastArg(options::OPT_fmemory_profile, 4813e8d8bef9SDimitry Andric options::OPT_fmemory_profile_EQ, 4814e8d8bef9SDimitry Andric options::OPT_fno_memory_profile); 4815e8d8bef9SDimitry Andric if (MemProfArg && 4816e8d8bef9SDimitry Andric !MemProfArg->getOption().matches(options::OPT_fno_memory_profile)) 4817e8d8bef9SDimitry Andric MemProfArg->render(Args, CmdArgs); 4818e8d8bef9SDimitry Andric 48190b57cec5SDimitry Andric // Embed-bitcode option. 48200b57cec5SDimitry Andric // Only white-listed flags below are allowed to be embedded. 4821fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO && 48220b57cec5SDimitry Andric (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) { 48230b57cec5SDimitry Andric // Add flags implied by -fembed-bitcode. 48240b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 48250b57cec5SDimitry Andric // Disable all llvm IR level optimizations. 48260b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 48270b57cec5SDimitry Andric 4828480093f4SDimitry Andric // Render target options. 48290b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 48300b57cec5SDimitry Andric 48310b57cec5SDimitry Andric // reject options that shouldn't be supported in bitcode 48320b57cec5SDimitry Andric // also reject kernel/kext 4833349cc55cSDimitry Andric static const constexpr unsigned kBitcodeOptionIgnorelist[] = { 48340b57cec5SDimitry Andric options::OPT_mkernel, 48350b57cec5SDimitry Andric options::OPT_fapple_kext, 48360b57cec5SDimitry Andric options::OPT_ffunction_sections, 48370b57cec5SDimitry Andric options::OPT_fno_function_sections, 48380b57cec5SDimitry Andric options::OPT_fdata_sections, 48390b57cec5SDimitry Andric options::OPT_fno_data_sections, 48405ffd83dbSDimitry Andric options::OPT_fbasic_block_sections_EQ, 48415ffd83dbSDimitry Andric options::OPT_funique_internal_linkage_names, 48425ffd83dbSDimitry Andric options::OPT_fno_unique_internal_linkage_names, 48430b57cec5SDimitry Andric options::OPT_funique_section_names, 48440b57cec5SDimitry Andric options::OPT_fno_unique_section_names, 48455ffd83dbSDimitry Andric options::OPT_funique_basic_block_section_names, 48465ffd83dbSDimitry Andric options::OPT_fno_unique_basic_block_section_names, 48470b57cec5SDimitry Andric options::OPT_mrestrict_it, 48480b57cec5SDimitry Andric options::OPT_mno_restrict_it, 48490b57cec5SDimitry Andric options::OPT_mstackrealign, 48500b57cec5SDimitry Andric options::OPT_mno_stackrealign, 48510b57cec5SDimitry Andric options::OPT_mstack_alignment, 48520b57cec5SDimitry Andric options::OPT_mcmodel_EQ, 48530b57cec5SDimitry Andric options::OPT_mlong_calls, 48540b57cec5SDimitry Andric options::OPT_mno_long_calls, 48550b57cec5SDimitry Andric options::OPT_ggnu_pubnames, 48560b57cec5SDimitry Andric options::OPT_gdwarf_aranges, 48570b57cec5SDimitry Andric options::OPT_fdebug_types_section, 48580b57cec5SDimitry Andric options::OPT_fno_debug_types_section, 48590b57cec5SDimitry Andric options::OPT_fdwarf_directory_asm, 48600b57cec5SDimitry Andric options::OPT_fno_dwarf_directory_asm, 48610b57cec5SDimitry Andric options::OPT_mrelax_all, 48620b57cec5SDimitry Andric options::OPT_mno_relax_all, 48630b57cec5SDimitry Andric options::OPT_ftrap_function_EQ, 48640b57cec5SDimitry Andric options::OPT_ffixed_r9, 48650b57cec5SDimitry Andric options::OPT_mfix_cortex_a53_835769, 48660b57cec5SDimitry Andric options::OPT_mno_fix_cortex_a53_835769, 48670b57cec5SDimitry Andric options::OPT_ffixed_x18, 48680b57cec5SDimitry Andric options::OPT_mglobal_merge, 48690b57cec5SDimitry Andric options::OPT_mno_global_merge, 48700b57cec5SDimitry Andric options::OPT_mred_zone, 48710b57cec5SDimitry Andric options::OPT_mno_red_zone, 48720b57cec5SDimitry Andric options::OPT_Wa_COMMA, 48730b57cec5SDimitry Andric options::OPT_Xassembler, 48740b57cec5SDimitry Andric options::OPT_mllvm, 48750b57cec5SDimitry Andric }; 48760b57cec5SDimitry Andric for (const auto &A : Args) 4877349cc55cSDimitry Andric if (llvm::is_contained(kBitcodeOptionIgnorelist, A->getOption().getID())) 48780b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling(); 48790b57cec5SDimitry Andric 48800b57cec5SDimitry Andric // Render the CodeGen options that need to be passed. 488181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 488281ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 48830b57cec5SDimitry Andric 48840b57cec5SDimitry Andric RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args, 48855ffd83dbSDimitry Andric CmdArgs, JA); 48860b57cec5SDimitry Andric 48870b57cec5SDimitry Andric // Render ABI arguments 48880b57cec5SDimitry Andric switch (TC.getArch()) { 48890b57cec5SDimitry Andric default: break; 48900b57cec5SDimitry Andric case llvm::Triple::arm: 48910b57cec5SDimitry Andric case llvm::Triple::armeb: 48920b57cec5SDimitry Andric case llvm::Triple::thumbeb: 4893349cc55cSDimitry Andric RenderARMABI(D, Triple, Args, CmdArgs); 48940b57cec5SDimitry Andric break; 48950b57cec5SDimitry Andric case llvm::Triple::aarch64: 4896480093f4SDimitry Andric case llvm::Triple::aarch64_32: 48970b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 48980b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 48990b57cec5SDimitry Andric break; 49000b57cec5SDimitry Andric } 49010b57cec5SDimitry Andric 49020b57cec5SDimitry Andric // Optimization level for CodeGen. 49030b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 49040b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 49050b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 49060b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 49070b57cec5SDimitry Andric } else { 49080b57cec5SDimitry Andric A->render(Args, CmdArgs); 49090b57cec5SDimitry Andric } 49100b57cec5SDimitry Andric } 49110b57cec5SDimitry Andric 49120b57cec5SDimitry Andric // Input/Output file. 49130b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 49140b57cec5SDimitry Andric // Handled with other dependency code. 49150b57cec5SDimitry Andric } else if (Output.isFilename()) { 49160b57cec5SDimitry Andric CmdArgs.push_back("-o"); 49170b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 49180b57cec5SDimitry Andric } else { 49190b57cec5SDimitry Andric assert(Output.isNothing() && "Input output."); 49200b57cec5SDimitry Andric } 49210b57cec5SDimitry Andric 49220b57cec5SDimitry Andric for (const auto &II : Inputs) { 49230b57cec5SDimitry Andric addDashXForInput(Args, II, CmdArgs); 49240b57cec5SDimitry Andric if (II.isFilename()) 49250b57cec5SDimitry Andric CmdArgs.push_back(II.getFilename()); 49260b57cec5SDimitry Andric else 49270b57cec5SDimitry Andric II.getInputArg().renderAsInput(Args, CmdArgs); 49280b57cec5SDimitry Andric } 49290b57cec5SDimitry Andric 4930e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>( 4931e8d8bef9SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(), 4932e8d8bef9SDimitry Andric CmdArgs, Inputs, Output)); 49330b57cec5SDimitry Andric return; 49340b57cec5SDimitry Andric } 49350b57cec5SDimitry Andric 4936fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO) 49370b57cec5SDimitry Andric CmdArgs.push_back("-fembed-bitcode=marker"); 49380b57cec5SDimitry Andric 49390b57cec5SDimitry Andric // We normally speed up the clang process a bit by skipping destructors at 49400b57cec5SDimitry Andric // exit, but when we're generating diagnostics we can rely on some of the 49410b57cec5SDimitry Andric // cleanup. 49420b57cec5SDimitry Andric if (!C.isForDiagnostics()) 49430b57cec5SDimitry Andric CmdArgs.push_back("-disable-free"); 4944349cc55cSDimitry Andric CmdArgs.push_back("-clear-ast-before-backend"); 49450b57cec5SDimitry Andric 49460b57cec5SDimitry Andric #ifdef NDEBUG 49470b57cec5SDimitry Andric const bool IsAssertBuild = false; 49480b57cec5SDimitry Andric #else 49490b57cec5SDimitry Andric const bool IsAssertBuild = true; 49500b57cec5SDimitry Andric #endif 49510b57cec5SDimitry Andric 49520b57cec5SDimitry Andric // Disable the verification pass in -asserts builds. 49530b57cec5SDimitry Andric if (!IsAssertBuild) 49540b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-verifier"); 49550b57cec5SDimitry Andric 49560b57cec5SDimitry Andric // Discard value names in assert builds unless otherwise specified. 49570b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdiscard_value_names, 495847395794SDimitry Andric options::OPT_fno_discard_value_names, !IsAssertBuild)) { 495947395794SDimitry Andric if (Args.hasArg(options::OPT_fdiscard_value_names) && 4960349cc55cSDimitry Andric llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) { 496147395794SDimitry Andric return types::isLLVMIR(II.getType()); 4962349cc55cSDimitry Andric })) { 496347395794SDimitry Andric D.Diag(diag::warn_ignoring_fdiscard_for_bitcode); 496447395794SDimitry Andric } 49650b57cec5SDimitry Andric CmdArgs.push_back("-discard-value-names"); 496647395794SDimitry Andric } 49670b57cec5SDimitry Andric 49680b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 49690b57cec5SDimitry Andric // -save-temps. 49700b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 49710b57cec5SDimitry Andric CmdArgs.push_back(getBaseInputName(Args, Input)); 49720b57cec5SDimitry Andric 49730b57cec5SDimitry Andric // Some flags which affect the language (via preprocessor 49740b57cec5SDimitry Andric // defines). 49750b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 49760b57cec5SDimitry Andric CmdArgs.push_back("-static-define"); 49770b57cec5SDimitry Andric 49780b57cec5SDimitry Andric if (Args.hasArg(options::OPT_municode)) 49790b57cec5SDimitry Andric CmdArgs.push_back("-DUNICODE"); 49800b57cec5SDimitry Andric 49810b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) 49820b57cec5SDimitry Andric RenderAnalyzerOptions(Args, CmdArgs, Triple, Input); 49830b57cec5SDimitry Andric 4984a7dea167SDimitry Andric if (isa<AnalyzeJobAction>(JA) || 4985a7dea167SDimitry Andric (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze))) 4986a7dea167SDimitry Andric CmdArgs.push_back("-setup-static-analyzer"); 4987a7dea167SDimitry Andric 49880b57cec5SDimitry Andric // Enable compatilibily mode to avoid analyzer-config related errors. 49890b57cec5SDimitry Andric // Since we can't access frontend flags through hasArg, let's manually iterate 49900b57cec5SDimitry Andric // through them. 49910b57cec5SDimitry Andric bool FoundAnalyzerConfig = false; 4992bdd1243dSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 49930b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 49940b57cec5SDimitry Andric FoundAnalyzerConfig = true; 49950b57cec5SDimitry Andric break; 49960b57cec5SDimitry Andric } 49970b57cec5SDimitry Andric if (!FoundAnalyzerConfig) 4998bdd1243dSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xanalyzer)) 49990b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 50000b57cec5SDimitry Andric FoundAnalyzerConfig = true; 50010b57cec5SDimitry Andric break; 50020b57cec5SDimitry Andric } 50030b57cec5SDimitry Andric if (FoundAnalyzerConfig) 50040b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-config-compatibility-mode=true"); 50050b57cec5SDimitry Andric 50060b57cec5SDimitry Andric CheckCodeGenerationOptions(D, Args); 50070b57cec5SDimitry Andric 50080b57cec5SDimitry Andric unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args); 50090b57cec5SDimitry Andric assert(FunctionAlignment <= 31 && "function alignment will be truncated!"); 50100b57cec5SDimitry Andric if (FunctionAlignment) { 50110b57cec5SDimitry Andric CmdArgs.push_back("-function-alignment"); 50120b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment))); 50130b57cec5SDimitry Andric } 50140b57cec5SDimitry Andric 5015349cc55cSDimitry Andric // We support -falign-loops=N where N is a power of 2. GCC supports more 5016349cc55cSDimitry Andric // forms. 5017349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_falign_loops_EQ)) { 5018349cc55cSDimitry Andric unsigned Value = 0; 5019349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) 5020349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_invalid_int_value) 5021349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5022349cc55cSDimitry Andric else if (Value & (Value - 1)) 5023349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_alignment_not_power_of_two) 5024349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5025349cc55cSDimitry Andric // Treat =0 as unspecified (use the target preference). 5026349cc55cSDimitry Andric if (Value) 5027349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-falign-loops=" + 5028349cc55cSDimitry Andric Twine(std::min(Value, 65536u)))); 5029349cc55cSDimitry Andric } 5030349cc55cSDimitry Andric 50310b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 50320b57cec5SDimitry Andric unsigned PICLevel; 50330b57cec5SDimitry Andric bool IsPIE; 50340b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); 5035bdd1243dSDimitry Andric Arg *LastPICDataRelArg = 5036bdd1243dSDimitry Andric Args.getLastArg(options::OPT_mno_pic_data_is_text_relative, 5037bdd1243dSDimitry Andric options::OPT_mpic_data_is_text_relative); 5038bdd1243dSDimitry Andric bool NoPICDataIsTextRelative = false; 5039bdd1243dSDimitry Andric if (LastPICDataRelArg) { 5040bdd1243dSDimitry Andric if (LastPICDataRelArg->getOption().matches( 5041bdd1243dSDimitry Andric options::OPT_mno_pic_data_is_text_relative)) { 5042bdd1243dSDimitry Andric NoPICDataIsTextRelative = true; 5043bdd1243dSDimitry Andric if (!PICLevel) 5044bdd1243dSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 5045bdd1243dSDimitry Andric << "-mno-pic-data-is-text-relative" 5046bdd1243dSDimitry Andric << "-fpic/-fpie"; 5047bdd1243dSDimitry Andric } 5048bdd1243dSDimitry Andric if (!Triple.isSystemZ()) 5049bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5050bdd1243dSDimitry Andric << (NoPICDataIsTextRelative ? "-mno-pic-data-is-text-relative" 5051bdd1243dSDimitry Andric : "-mpic-data-is-text-relative") 5052bdd1243dSDimitry Andric << RawTriple.str(); 5053bdd1243dSDimitry Andric } 50540b57cec5SDimitry Andric 50555ffd83dbSDimitry Andric bool IsROPI = RelocationModel == llvm::Reloc::ROPI || 50565ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 50575ffd83dbSDimitry Andric bool IsRWPI = RelocationModel == llvm::Reloc::RWPI || 50585ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 50590b57cec5SDimitry Andric 50605ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_mcmse) && 50615ffd83dbSDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) { 50625ffd83dbSDimitry Andric if (IsROPI) 50635ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << IsROPI; 50645ffd83dbSDimitry Andric if (IsRWPI) 50655ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << !IsRWPI; 50665ffd83dbSDimitry Andric } 50675ffd83dbSDimitry Andric 50685ffd83dbSDimitry Andric if (IsROPI && types::isCXX(Input.getType()) && 50690b57cec5SDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) 50700b57cec5SDimitry Andric D.Diag(diag::err_drv_ropi_incompatible_with_cxx); 50710b57cec5SDimitry Andric 50725ffd83dbSDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 50730b57cec5SDimitry Andric if (RMName) { 50740b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 50750b57cec5SDimitry Andric CmdArgs.push_back(RMName); 50760b57cec5SDimitry Andric } 50770b57cec5SDimitry Andric if (PICLevel > 0) { 50780b57cec5SDimitry Andric CmdArgs.push_back("-pic-level"); 50790b57cec5SDimitry Andric CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); 50800b57cec5SDimitry Andric if (IsPIE) 50810b57cec5SDimitry Andric CmdArgs.push_back("-pic-is-pie"); 5082bdd1243dSDimitry Andric if (NoPICDataIsTextRelative) 5083bdd1243dSDimitry Andric CmdArgs.push_back("-mcmodel=medium"); 50840b57cec5SDimitry Andric } 50850b57cec5SDimitry Andric 50860b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::ROPI || 50870b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 50880b57cec5SDimitry Andric CmdArgs.push_back("-fropi"); 50890b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::RWPI || 50900b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 50910b57cec5SDimitry Andric CmdArgs.push_back("-frwpi"); 50920b57cec5SDimitry Andric 50930b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_meabi)) { 50940b57cec5SDimitry Andric CmdArgs.push_back("-meabi"); 50950b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 50960b57cec5SDimitry Andric } 50970b57cec5SDimitry Andric 5098e8d8bef9SDimitry Andric // -fsemantic-interposition is forwarded to CC1: set the 5099e8d8bef9SDimitry Andric // "SemanticInterposition" metadata to 1 (make some linkages interposable) and 5100e8d8bef9SDimitry Andric // make default visibility external linkage definitions dso_preemptable. 5101e8d8bef9SDimitry Andric // 5102e8d8bef9SDimitry Andric // -fno-semantic-interposition: if the target supports .Lfoo$local local 5103e8d8bef9SDimitry Andric // aliases (make default visibility external linkage definitions dso_local). 5104e8d8bef9SDimitry Andric // This is the CC1 default for ELF to match COFF/Mach-O. 5105e8d8bef9SDimitry Andric // 5106e8d8bef9SDimitry Andric // Otherwise use Clang's traditional behavior: like 5107e8d8bef9SDimitry Andric // -fno-semantic-interposition but local aliases are not used. So references 5108e8d8bef9SDimitry Andric // can be interposed if not optimized out. 5109e8d8bef9SDimitry Andric if (Triple.isOSBinFormatELF()) { 5110e8d8bef9SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fsemantic_interposition, 5111e8d8bef9SDimitry Andric options::OPT_fno_semantic_interposition); 5112e8d8bef9SDimitry Andric if (RelocationModel != llvm::Reloc::Static && !IsPIE) { 5113e8d8bef9SDimitry Andric // The supported targets need to call AsmPrinter::getSymbolPreferLocal. 5114fe6060f1SDimitry Andric bool SupportsLocalAlias = 5115fe6060f1SDimitry Andric Triple.isAArch64() || Triple.isRISCV() || Triple.isX86(); 5116e8d8bef9SDimitry Andric if (!A) 5117e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5118e8d8bef9SDimitry Andric else if (A->getOption().matches(options::OPT_fsemantic_interposition)) 51195ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5120e8d8bef9SDimitry Andric else if (!SupportsLocalAlias) 5121e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5122e8d8bef9SDimitry Andric } 5123e8d8bef9SDimitry Andric } 51245ffd83dbSDimitry Andric 51255ffd83dbSDimitry Andric { 51265ffd83dbSDimitry Andric std::string Model; 51270b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) { 51280b57cec5SDimitry Andric if (!TC.isThreadModelSupported(A->getValue())) 51290b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_thread_model_for_target) 51300b57cec5SDimitry Andric << A->getValue() << A->getAsString(Args); 51315ffd83dbSDimitry Andric Model = A->getValue(); 51325ffd83dbSDimitry Andric } else 51335ffd83dbSDimitry Andric Model = TC.getThreadModel(); 51345ffd83dbSDimitry Andric if (Model != "posix") { 51355ffd83dbSDimitry Andric CmdArgs.push_back("-mthread-model"); 51365ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Model)); 51370b57cec5SDimitry Andric } 51385ffd83dbSDimitry Andric } 51390b57cec5SDimitry Andric 5140bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fveclib)) { 5141bdd1243dSDimitry Andric StringRef Name = A->getValue(); 5142bdd1243dSDimitry Andric if (Name == "SVML") { 5143bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::x86 && 5144bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::x86_64) 5145bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5146bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5147bdd1243dSDimitry Andric } else if (Name == "LIBMVEC-X86") { 5148bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::x86 && 5149bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::x86_64) 5150bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5151bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5152bdd1243dSDimitry Andric } else if (Name == "SLEEF") { 5153bdd1243dSDimitry Andric if (Triple.getArch() != llvm::Triple::aarch64 && 5154bdd1243dSDimitry Andric Triple.getArch() != llvm::Triple::aarch64_be) 5155bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5156bdd1243dSDimitry Andric << Name << Triple.getArchName(); 5157bdd1243dSDimitry Andric } 5158bdd1243dSDimitry Andric A->render(Args, CmdArgs); 5159bdd1243dSDimitry Andric } 51600b57cec5SDimitry Andric 51610b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmerge_all_constants, 51620b57cec5SDimitry Andric options::OPT_fno_merge_all_constants, false)) 51630b57cec5SDimitry Andric CmdArgs.push_back("-fmerge-all-constants"); 51640b57cec5SDimitry Andric 5165bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdelete_null_pointer_checks, 5166bdd1243dSDimitry Andric options::OPT_fno_delete_null_pointer_checks); 51670b57cec5SDimitry Andric 51680b57cec5SDimitry Andric // LLVM Code Generator Options. 51690b57cec5SDimitry Andric 5170fe6060f1SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file_EQ)) { 51710b57cec5SDimitry Andric StringRef Map = A->getValue(); 51720b57cec5SDimitry Andric if (!llvm::sys::fs::exists(Map)) { 51730b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << Map; 51740b57cec5SDimitry Andric } else { 5175fe6060f1SDimitry Andric A->render(Args, CmdArgs); 51760b57cec5SDimitry Andric A->claim(); 51770b57cec5SDimitry Andric } 51780b57cec5SDimitry Andric } 51790b57cec5SDimitry Andric 5180e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_vec_extabi, 5181e8d8bef9SDimitry Andric options::OPT_mabi_EQ_vec_default)) { 5182e8d8bef9SDimitry Andric if (!Triple.isOSAIX()) 5183e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5184e8d8bef9SDimitry Andric << A->getSpelling() << RawTriple.str(); 5185d409305fSDimitry Andric if (A->getOption().getID() == options::OPT_mabi_EQ_vec_extabi) 5186d409305fSDimitry Andric CmdArgs.push_back("-mabi=vec-extabi"); 5187d409305fSDimitry Andric else 5188fe6060f1SDimitry Andric CmdArgs.push_back("-mabi=vec-default"); 5189fe6060f1SDimitry Andric } 5190fe6060f1SDimitry Andric 5191972a253aSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_quadword_atomics)) { 5192972a253aSDimitry Andric if (!Triple.isOSAIX() || Triple.isPPC32()) 5193972a253aSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5194972a253aSDimitry Andric << A->getSpelling() << RawTriple.str(); 5195972a253aSDimitry Andric CmdArgs.push_back("-mabi=quadword-atomics"); 5196972a253aSDimitry Andric } 5197972a253aSDimitry Andric 5198fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) { 5199fe6060f1SDimitry Andric // Emit the unsupported option error until the Clang's library integration 5200fe6060f1SDimitry Andric // support for 128-bit long double is available for AIX. 5201fe6060f1SDimitry Andric if (Triple.isOSAIX()) 5202fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5203fe6060f1SDimitry Andric << A->getSpelling() << RawTriple.str(); 5204e8d8bef9SDimitry Andric } 5205e8d8bef9SDimitry Andric 52060b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) { 52070b57cec5SDimitry Andric StringRef v = A->getValue(); 5208fe6060f1SDimitry Andric // FIXME: Validate the argument here so we don't produce meaningless errors 5209fe6060f1SDimitry Andric // about -fwarn-stack-size=. 5210fe6060f1SDimitry Andric if (v.empty()) 5211fe6060f1SDimitry Andric D.Diag(diag::err_drv_missing_argument) << A->getSpelling() << 1; 5212fe6060f1SDimitry Andric else 5213fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + v)); 52140b57cec5SDimitry Andric A->claim(); 52150b57cec5SDimitry Andric } 52160b57cec5SDimitry Andric 521781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fjump_tables, 521881ad6265SDimitry Andric options::OPT_fno_jump_tables); 521981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fprofile_sample_accurate, 522081ad6265SDimitry Andric options::OPT_fno_profile_sample_accurate); 522181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fpreserve_as_comments, 522281ad6265SDimitry Andric options::OPT_fno_preserve_as_comments); 52230b57cec5SDimitry Andric 52240b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 52250b57cec5SDimitry Andric CmdArgs.push_back("-mregparm"); 52260b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 52270b57cec5SDimitry Andric } 52280b57cec5SDimitry Andric 5229e86cf8adSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_maix_struct_return, 5230e86cf8adSDimitry Andric options::OPT_msvr4_struct_return)) { 5231e8d8bef9SDimitry Andric if (!TC.getTriple().isPPC32()) { 5232e86cf8adSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5233e86cf8adSDimitry Andric << A->getSpelling() << RawTriple.str(); 5234e86cf8adSDimitry Andric } else if (A->getOption().matches(options::OPT_maix_struct_return)) { 5235e86cf8adSDimitry Andric CmdArgs.push_back("-maix-struct-return"); 5236e86cf8adSDimitry Andric } else { 5237e86cf8adSDimitry Andric assert(A->getOption().matches(options::OPT_msvr4_struct_return)); 5238e86cf8adSDimitry Andric CmdArgs.push_back("-msvr4-struct-return"); 5239e86cf8adSDimitry Andric } 5240e86cf8adSDimitry Andric } 5241e86cf8adSDimitry Andric 52420b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 52430b57cec5SDimitry Andric options::OPT_freg_struct_return)) { 52440b57cec5SDimitry Andric if (TC.getArch() != llvm::Triple::x86) { 52450b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 52460b57cec5SDimitry Andric << A->getSpelling() << RawTriple.str(); 52470b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 52480b57cec5SDimitry Andric CmdArgs.push_back("-fpcc-struct-return"); 52490b57cec5SDimitry Andric } else { 52500b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_freg_struct_return)); 52510b57cec5SDimitry Andric CmdArgs.push_back("-freg-struct-return"); 52520b57cec5SDimitry Andric } 52530b57cec5SDimitry Andric } 52540b57cec5SDimitry Andric 52550b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) 52560b57cec5SDimitry Andric CmdArgs.push_back("-fdefault-calling-conv=stdcall"); 52570b57cec5SDimitry Andric 52585ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_fenable_matrix)) { 52595ffd83dbSDimitry Andric // enable-matrix is needed by both the LangOpts and by LLVM. 52605ffd83dbSDimitry Andric CmdArgs.push_back("-fenable-matrix"); 52615ffd83dbSDimitry Andric CmdArgs.push_back("-mllvm"); 52625ffd83dbSDimitry Andric CmdArgs.push_back("-enable-matrix"); 52635ffd83dbSDimitry Andric } 52645ffd83dbSDimitry Andric 5265a7dea167SDimitry Andric CodeGenOptions::FramePointerKind FPKeepKind = 5266a7dea167SDimitry Andric getFramePointerKind(Args, RawTriple); 5267a7dea167SDimitry Andric const char *FPKeepKindStr = nullptr; 5268a7dea167SDimitry Andric switch (FPKeepKind) { 5269a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::None: 5270a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=none"; 5271a7dea167SDimitry Andric break; 5272a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::NonLeaf: 5273a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=non-leaf"; 5274a7dea167SDimitry Andric break; 5275a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::All: 5276a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=all"; 5277a7dea167SDimitry Andric break; 5278a7dea167SDimitry Andric } 5279a7dea167SDimitry Andric assert(FPKeepKindStr && "unknown FramePointerKind"); 5280a7dea167SDimitry Andric CmdArgs.push_back(FPKeepKindStr); 5281a7dea167SDimitry Andric 528281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fzero_initialized_in_bss, 528381ad6265SDimitry Andric options::OPT_fno_zero_initialized_in_bss); 52840b57cec5SDimitry Andric 52850b57cec5SDimitry Andric bool OFastEnabled = isOptimizationLevelFast(Args); 52860b57cec5SDimitry Andric // If -Ofast is the optimization level, then -fstrict-aliasing should be 52870b57cec5SDimitry Andric // enabled. This alias option is being used to simplify the hasFlag logic. 52880b57cec5SDimitry Andric OptSpecifier StrictAliasingAliasOption = 52890b57cec5SDimitry Andric OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing; 52900b57cec5SDimitry Andric // We turn strict aliasing off by default if we're in CL mode, since MSVC 52910b57cec5SDimitry Andric // doesn't do any TBAA. 52920b57cec5SDimitry Andric bool TBAAOnByDefault = !D.IsCLMode(); 52930b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 52940b57cec5SDimitry Andric options::OPT_fno_strict_aliasing, TBAAOnByDefault)) 52950b57cec5SDimitry Andric CmdArgs.push_back("-relaxed-aliasing"); 52960b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, 529781ad6265SDimitry Andric options::OPT_fno_struct_path_tbaa, true)) 52980b57cec5SDimitry Andric CmdArgs.push_back("-no-struct-path-tbaa"); 529981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_enums, 530081ad6265SDimitry Andric options::OPT_fno_strict_enums); 530181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fstrict_return, 530281ad6265SDimitry Andric options::OPT_fno_strict_return); 530381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fallow_editor_placeholders, 530481ad6265SDimitry Andric options::OPT_fno_allow_editor_placeholders); 530581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_vtable_pointers, 530681ad6265SDimitry Andric options::OPT_fno_strict_vtable_pointers); 530781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_emit_vtables, 530881ad6265SDimitry Andric options::OPT_fno_force_emit_vtables); 530981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 531081ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 531181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fescaping_block_tail_calls, 531281ad6265SDimitry Andric options::OPT_fno_escaping_block_tail_calls); 53130b57cec5SDimitry Andric 53140b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses, 53150b57cec5SDimitry Andric options::OPT_fno_fine_grained_bitfield_accesses); 53160b57cec5SDimitry Andric 5317fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5318fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5319fe6060f1SDimitry Andric 53200b57cec5SDimitry Andric // Handle segmented stacks. 5321bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsplit_stack, 5322bdd1243dSDimitry Andric options::OPT_fno_split_stack); 5323fe6060f1SDimitry Andric 5324fe6060f1SDimitry Andric // -fprotect-parens=0 is default. 5325fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fprotect_parens, 5326fe6060f1SDimitry Andric options::OPT_fno_protect_parens, false)) 5327fe6060f1SDimitry Andric CmdArgs.push_back("-fprotect-parens"); 53280b57cec5SDimitry Andric 53295ffd83dbSDimitry Andric RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA); 53305ffd83dbSDimitry Andric 5331fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) { 5332fe6060f1SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 5333fe6060f1SDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5334fe6060f1SDimitry Andric StringRef V = A->getValue(); 5335fe6060f1SDimitry Andric if (V == "64") 5336fe6060f1SDimitry Andric CmdArgs.push_back("-fextend-arguments=64"); 5337fe6060f1SDimitry Andric else if (V != "32") 5338fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5339fe6060f1SDimitry Andric << A->getValue() << A->getOption().getName(); 5340fe6060f1SDimitry Andric } else 5341fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5342fe6060f1SDimitry Andric << A->getOption().getName() << TripleStr; 5343fe6060f1SDimitry Andric } 5344fe6060f1SDimitry Andric 53455ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) { 53465ffd83dbSDimitry Andric if (TC.getArch() == llvm::Triple::avr) 53475ffd83dbSDimitry Andric A->render(Args, CmdArgs); 53485ffd83dbSDimitry Andric else 53495ffd83dbSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 53505ffd83dbSDimitry Andric << A->getAsString(Args) << TripleStr; 53515ffd83dbSDimitry Andric } 53520b57cec5SDimitry Andric 5353a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) { 5354480093f4SDimitry Andric if (TC.getTriple().isX86()) 5355a7dea167SDimitry Andric A->render(Args, CmdArgs); 5356e8d8bef9SDimitry Andric else if (TC.getTriple().isPPC() && 5357a7dea167SDimitry Andric (A->getOption().getID() != options::OPT_mlong_double_80)) 53580b57cec5SDimitry Andric A->render(Args, CmdArgs); 53590b57cec5SDimitry Andric else 53600b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 53610b57cec5SDimitry Andric << A->getAsString(Args) << TripleStr; 53620b57cec5SDimitry Andric } 53630b57cec5SDimitry Andric 53640b57cec5SDimitry Andric // Decide whether to use verbose asm. Verbose assembly is the default on 53650b57cec5SDimitry Andric // toolchains which have the integrated assembler on by default. 53660b57cec5SDimitry Andric bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault(); 53675ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 5368a7dea167SDimitry Andric IsIntegratedAssemblerDefault)) 53695ffd83dbSDimitry Andric CmdArgs.push_back("-fno-verbose-asm"); 53700b57cec5SDimitry Andric 5371e8d8bef9SDimitry Andric // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we 5372e8d8bef9SDimitry Andric // use that to indicate the MC default in the backend. 5373e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbinutils_version_EQ)) { 5374e8d8bef9SDimitry Andric StringRef V = A->getValue(); 5375e8d8bef9SDimitry Andric unsigned Num; 5376e8d8bef9SDimitry Andric if (V == "none") 5377e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5378e8d8bef9SDimitry Andric else if (!V.consumeInteger(10, Num) && Num > 0 && 5379e8d8bef9SDimitry Andric (V.empty() || (V.consume_front(".") && 5380e8d8bef9SDimitry Andric !V.consumeInteger(10, Num) && V.empty()))) 5381e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5382e8d8bef9SDimitry Andric else 5383e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5384e8d8bef9SDimitry Andric << A->getValue() << A->getOption().getName(); 5385e8d8bef9SDimitry Andric } 5386e8d8bef9SDimitry Andric 5387fe6060f1SDimitry Andric // If toolchain choose to use MCAsmParser for inline asm don't pass the 5388fe6060f1SDimitry Andric // option to disable integrated-as explictly. 5389fe6060f1SDimitry Andric if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser()) 53900b57cec5SDimitry Andric CmdArgs.push_back("-no-integrated-as"); 53910b57cec5SDimitry Andric 53920b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 53930b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 53940b57cec5SDimitry Andric CmdArgs.push_back("Structure"); 53950b57cec5SDimitry Andric } 53960b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 53970b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 53980b57cec5SDimitry Andric CmdArgs.push_back("Arguments"); 53990b57cec5SDimitry Andric } 54000b57cec5SDimitry Andric 54010b57cec5SDimitry Andric // Enable -mconstructor-aliases except on darwin, where we have to work around 5402349cc55cSDimitry Andric // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where 5403349cc55cSDimitry Andric // aliases aren't supported. 5404349cc55cSDimitry Andric if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX()) 54050b57cec5SDimitry Andric CmdArgs.push_back("-mconstructor-aliases"); 54060b57cec5SDimitry Andric 54070b57cec5SDimitry Andric // Darwin's kernel doesn't support guard variables; just die if we 54080b57cec5SDimitry Andric // try to use them. 54090b57cec5SDimitry Andric if (KernelOrKext && RawTriple.isOSDarwin()) 54100b57cec5SDimitry Andric CmdArgs.push_back("-fforbid-guard-variables"); 54110b57cec5SDimitry Andric 54120b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields, 54135ffd83dbSDimitry Andric Triple.isWindowsGNUEnvironment())) { 54140b57cec5SDimitry Andric CmdArgs.push_back("-mms-bitfields"); 54150b57cec5SDimitry Andric } 54160b57cec5SDimitry Andric 5417e8d8bef9SDimitry Andric // Non-PIC code defaults to -fdirect-access-external-data while PIC code 5418e8d8bef9SDimitry Andric // defaults to -fno-direct-access-external-data. Pass the option if different 5419e8d8bef9SDimitry Andric // from the default. 5420e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, 5421e8d8bef9SDimitry Andric options::OPT_fno_direct_access_external_data)) 5422e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fdirect_access_external_data) != 5423e8d8bef9SDimitry Andric (PICLevel == 0)) 5424e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 54250b57cec5SDimitry Andric 54260b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { 54270b57cec5SDimitry Andric CmdArgs.push_back("-fno-plt"); 54280b57cec5SDimitry Andric } 54290b57cec5SDimitry Andric 54300b57cec5SDimitry Andric // -fhosted is default. 54310b57cec5SDimitry Andric // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to 54320b57cec5SDimitry Andric // use Freestanding. 54330b57cec5SDimitry Andric bool Freestanding = 54340b57cec5SDimitry Andric Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 54350b57cec5SDimitry Andric KernelOrKext; 54360b57cec5SDimitry Andric if (Freestanding) 54370b57cec5SDimitry Andric CmdArgs.push_back("-ffreestanding"); 54380b57cec5SDimitry Andric 543981ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_knr_functions); 544081ad6265SDimitry Andric 54410b57cec5SDimitry Andric // This is a coarse approximation of what llvm-gcc actually does, both 54420b57cec5SDimitry Andric // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 54430b57cec5SDimitry Andric // complicated ways. 5444349cc55cSDimitry Andric auto SanitizeArgs = TC.getSanitizerArgs(Args); 5445bdd1243dSDimitry Andric 5446bdd1243dSDimitry Andric bool IsAsyncUnwindTablesDefault = 5447bdd1243dSDimitry Andric TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Asynchronous; 5448bdd1243dSDimitry Andric bool IsSyncUnwindTablesDefault = 5449bdd1243dSDimitry Andric TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Synchronous; 5450bdd1243dSDimitry Andric 5451349cc55cSDimitry Andric bool AsyncUnwindTables = Args.hasFlag( 5452349cc55cSDimitry Andric options::OPT_fasynchronous_unwind_tables, 54530b57cec5SDimitry Andric options::OPT_fno_asynchronous_unwind_tables, 5454bdd1243dSDimitry Andric (IsAsyncUnwindTablesDefault || SanitizeArgs.needsUnwindTables()) && 54550b57cec5SDimitry Andric !Freestanding); 5456bdd1243dSDimitry Andric bool UnwindTables = 5457bdd1243dSDimitry Andric Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, 5458bdd1243dSDimitry Andric IsSyncUnwindTablesDefault && !Freestanding); 5459349cc55cSDimitry Andric if (AsyncUnwindTables) 5460349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=2"); 5461349cc55cSDimitry Andric else if (UnwindTables) 5462349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=1"); 54630b57cec5SDimitry Andric 54645ffd83dbSDimitry Andric // Prepare `-aux-target-cpu` and `-aux-target-feature` unless 54655ffd83dbSDimitry Andric // `--gpu-use-aux-triple-only` is specified. 54665ffd83dbSDimitry Andric if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) && 5467fe6060f1SDimitry Andric (IsCudaDevice || IsHIPDevice)) { 54685ffd83dbSDimitry Andric const ArgList &HostArgs = 54695ffd83dbSDimitry Andric C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None); 54705ffd83dbSDimitry Andric std::string HostCPU = 5471349cc55cSDimitry Andric getCPUName(D, HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); 54725ffd83dbSDimitry Andric if (!HostCPU.empty()) { 54735ffd83dbSDimitry Andric CmdArgs.push_back("-aux-target-cpu"); 54745ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(HostCPU)); 54755ffd83dbSDimitry Andric } 54765ffd83dbSDimitry Andric getTargetFeatures(D, *TC.getAuxTriple(), HostArgs, CmdArgs, 54775ffd83dbSDimitry Andric /*ForAS*/ false, /*IsAux*/ true); 54785ffd83dbSDimitry Andric } 54795ffd83dbSDimitry Andric 54800b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 54810b57cec5SDimitry Andric 54820b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 54835ffd83dbSDimitry Andric StringRef CM = A->getValue(); 54845ffd83dbSDimitry Andric if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" || 5485fe6060f1SDimitry Andric CM == "tiny") { 5486fe6060f1SDimitry Andric if (Triple.isOSAIX() && CM == "medium") 5487fe6060f1SDimitry Andric CmdArgs.push_back("-mcmodel=large"); 5488bdd1243dSDimitry Andric else if (Triple.isAArch64() && (CM == "kernel" || CM == "medium")) 5489bdd1243dSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5490bdd1243dSDimitry Andric << CM << A->getOption().getName(); 54915ffd83dbSDimitry Andric else 5492fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5493fe6060f1SDimitry Andric } else { 54945ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 54955ffd83dbSDimitry Andric << CM << A->getOption().getName(); 54960b57cec5SDimitry Andric } 5497fe6060f1SDimitry Andric } 54980b57cec5SDimitry Andric 5499480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { 5500480093f4SDimitry Andric StringRef Value = A->getValue(); 5501480093f4SDimitry Andric unsigned TLSSize = 0; 5502480093f4SDimitry Andric Value.getAsInteger(10, TLSSize); 5503480093f4SDimitry Andric if (!Triple.isAArch64() || !Triple.isOSBinFormatELF()) 5504480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5505480093f4SDimitry Andric << A->getOption().getName() << TripleStr; 5506480093f4SDimitry Andric if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48) 5507480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5508480093f4SDimitry Andric << A->getOption().getName() << Value; 5509480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ); 5510480093f4SDimitry Andric } 5511480093f4SDimitry Andric 55120b57cec5SDimitry Andric // Add the target cpu 5513349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 55140b57cec5SDimitry Andric if (!CPU.empty()) { 55150b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 55160b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 55170b57cec5SDimitry Andric } 55180b57cec5SDimitry Andric 55190b57cec5SDimitry Andric RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs); 55200b57cec5SDimitry Andric 5521fe6060f1SDimitry Andric // FIXME: For now we want to demote any errors to warnings, when they have 5522fe6060f1SDimitry Andric // been raised for asking the wrong question of scalable vectors, such as 5523fe6060f1SDimitry Andric // asking for the fixed number of elements. This may happen because code that 5524fe6060f1SDimitry Andric // is not yet ported to work for scalable vectors uses the wrong interfaces, 5525fe6060f1SDimitry Andric // whereas the behaviour is actually correct. Emitting a warning helps bring 5526fe6060f1SDimitry Andric // up scalable vector support in an incremental way. When scalable vector 5527fe6060f1SDimitry Andric // support is stable enough, all uses of wrong interfaces should be considered 5528fe6060f1SDimitry Andric // as errors, but until then, we can live with a warning being emitted by the 5529fe6060f1SDimitry Andric // compiler. This way, Clang can be used to compile code with scalable vectors 5530fe6060f1SDimitry Andric // and identify possible issues. 553104eeddc0SDimitry Andric if (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 553204eeddc0SDimitry Andric isa<BackendJobAction>(JA)) { 5533fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5534fe6060f1SDimitry Andric CmdArgs.push_back("-treat-scalable-fixed-error-as-warning"); 5535fe6060f1SDimitry Andric } 5536fe6060f1SDimitry Andric 55370b57cec5SDimitry Andric // These two are potentially updated by AddClangCLArgs. 55380b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; 55390b57cec5SDimitry Andric bool EmitCodeView = false; 55400b57cec5SDimitry Andric 55410b57cec5SDimitry Andric // Add clang-cl arguments. 55420b57cec5SDimitry Andric types::ID InputType = Input.getType(); 55430b57cec5SDimitry Andric if (D.IsCLMode()) 55440b57cec5SDimitry Andric AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView); 55450b57cec5SDimitry Andric 5546e8d8bef9SDimitry Andric DwarfFissionKind DwarfFission = DwarfFissionKind::None; 5547e8d8bef9SDimitry Andric renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, 5548e8d8bef9SDimitry Andric types::isLLVMIR(InputType), CmdArgs, DebugInfoKind, 5549e8d8bef9SDimitry Andric DwarfFission); 55500b57cec5SDimitry Andric 555181ad6265SDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 555281ad6265SDimitry Andric if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) { 555381ad6265SDimitry Andric if (TC.getTriple().isOSBinFormatELF()) { 555481ad6265SDimitry Andric if (DebugInfoKind >= codegenoptions::DebugInfoConstructor) 555581ad6265SDimitry Andric CmdArgs.push_back("-fjmc"); 555681ad6265SDimitry Andric else 555781ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc" 555881ad6265SDimitry Andric << "-g"; 555981ad6265SDimitry Andric } else { 556081ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_fjmc_for_elf_only); 556181ad6265SDimitry Andric } 556281ad6265SDimitry Andric } 556381ad6265SDimitry Andric 55640b57cec5SDimitry Andric // Add the split debug info name to the command lines here so we 55650b57cec5SDimitry Andric // can propagate it to the backend. 55660b57cec5SDimitry Andric bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) && 5567e8d8bef9SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 5568e8d8bef9SDimitry Andric TC.getTriple().isOSBinFormatWasm()) && 55690b57cec5SDimitry Andric (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 55700b57cec5SDimitry Andric isa<BackendJobAction>(JA)); 55710b57cec5SDimitry Andric if (SplitDWARF) { 5572e8d8bef9SDimitry Andric const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output); 55730b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-file"); 55740b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 55750b57cec5SDimitry Andric if (DwarfFission == DwarfFissionKind::Split) { 55760b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 55770b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 55780b57cec5SDimitry Andric } 55790b57cec5SDimitry Andric } 55800b57cec5SDimitry Andric 55810b57cec5SDimitry Andric // Pass the linker version in use. 55820b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 55830b57cec5SDimitry Andric CmdArgs.push_back("-target-linker-version"); 55840b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 55850b57cec5SDimitry Andric } 55860b57cec5SDimitry Andric 55870b57cec5SDimitry Andric // Explicitly error on some things we know we don't support and can't just 55880b57cec5SDimitry Andric // ignore. 55890b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fallow_unsupported)) { 55900b57cec5SDimitry Andric Arg *Unsupported; 55910b57cec5SDimitry Andric if (types::isCXX(InputType) && RawTriple.isOSDarwin() && 55920b57cec5SDimitry Andric TC.getArch() == llvm::Triple::x86) { 55930b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 55940b57cec5SDimitry Andric (Unsupported = Args.getLastArg(options::OPT_mkernel))) 55950b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 55960b57cec5SDimitry Andric << Unsupported->getOption().getName(); 55970b57cec5SDimitry Andric } 55980b57cec5SDimitry Andric // The faltivec option has been superseded by the maltivec option. 55990b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_faltivec))) 56000b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 56010b57cec5SDimitry Andric << Unsupported->getOption().getName() 56020b57cec5SDimitry Andric << "please use -maltivec and include altivec.h explicitly"; 56030b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec))) 56040b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 56050b57cec5SDimitry Andric << Unsupported->getOption().getName() << "please use -mno-altivec"; 56060b57cec5SDimitry Andric } 56070b57cec5SDimitry Andric 56080b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_v); 56095ffd83dbSDimitry Andric 56105ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_H)) { 56115ffd83dbSDimitry Andric CmdArgs.push_back("-H"); 56125ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 56135ffd83dbSDimitry Andric } 5614fe6060f1SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes); 56155ffd83dbSDimitry Andric 5616bdd1243dSDimitry Andric if (D.CCPrintHeadersFormat && !D.CCGenDiagnostics) { 56170b57cec5SDimitry Andric CmdArgs.push_back("-header-include-file"); 5618fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCPrintHeadersFilename.empty() 5619fe6060f1SDimitry Andric ? D.CCPrintHeadersFilename.c_str() 56200b57cec5SDimitry Andric : "-"); 56215ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 5622bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5623bdd1243dSDimitry Andric "-header-include-format=" + 5624bdd1243dSDimitry Andric std::string(headerIncludeFormatKindToString(D.CCPrintHeadersFormat)))); 5625bdd1243dSDimitry Andric CmdArgs.push_back( 5626bdd1243dSDimitry Andric Args.MakeArgString("-header-include-filtering=" + 5627bdd1243dSDimitry Andric std::string(headerIncludeFilteringKindToString( 5628bdd1243dSDimitry Andric D.CCPrintHeadersFiltering)))); 56290b57cec5SDimitry Andric } 56300b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_P); 56310b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 56320b57cec5SDimitry Andric 56330b57cec5SDimitry Andric if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 56340b57cec5SDimitry Andric CmdArgs.push_back("-diagnostic-log-file"); 5635fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty() 5636fe6060f1SDimitry Andric ? D.CCLogDiagnosticsFilename.c_str() 56370b57cec5SDimitry Andric : "-"); 56380b57cec5SDimitry Andric } 56390b57cec5SDimitry Andric 564013138422SDimitry Andric // Give the gen diagnostics more chances to succeed, by avoiding intentional 564113138422SDimitry Andric // crashes. 564213138422SDimitry Andric if (D.CCGenDiagnostics) 564313138422SDimitry Andric CmdArgs.push_back("-disable-pragma-debug-crash"); 564413138422SDimitry Andric 5645fe6060f1SDimitry Andric // Allow backend to put its diagnostic files in the same place as frontend 5646fe6060f1SDimitry Andric // crash diagnostics files. 5647fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) { 5648fe6060f1SDimitry Andric StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir); 5649fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5650fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir)); 5651fe6060f1SDimitry Andric } 5652fe6060f1SDimitry Andric 56530b57cec5SDimitry Andric bool UseSeparateSections = isUseSeparateSections(Triple); 56540b57cec5SDimitry Andric 56550b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffunction_sections, 56560b57cec5SDimitry Andric options::OPT_fno_function_sections, UseSeparateSections)) { 56570b57cec5SDimitry Andric CmdArgs.push_back("-ffunction-sections"); 56580b57cec5SDimitry Andric } 56590b57cec5SDimitry Andric 56605ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) { 56615ffd83dbSDimitry Andric StringRef Val = A->getValue(); 5662fe6060f1SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 56635ffd83dbSDimitry Andric if (Val != "all" && Val != "labels" && Val != "none" && 5664e8d8bef9SDimitry Andric !Val.startswith("list=")) 56655ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 56665ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 56675ffd83dbSDimitry Andric else 56685ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5669fe6060f1SDimitry Andric } else if (Triple.isNVPTX()) { 5670fe6060f1SDimitry Andric // Do not pass the option to the GPU compilation. We still want it enabled 5671fe6060f1SDimitry Andric // for the host-side compilation, so seeing it here is not an error. 5672fe6060f1SDimitry Andric } else if (Val != "none") { 5673fe6060f1SDimitry Andric // =none is allowed everywhere. It's useful for overriding the option 5674fe6060f1SDimitry Andric // and is the same as not specifying the option. 5675e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5676e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5677e8d8bef9SDimitry Andric } 56785ffd83dbSDimitry Andric } 56795ffd83dbSDimitry Andric 5680e8d8bef9SDimitry Andric bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF(); 56810b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, 5682e8d8bef9SDimitry Andric UseSeparateSections || HasDefaultDataSections)) { 56830b57cec5SDimitry Andric CmdArgs.push_back("-fdata-sections"); 56840b57cec5SDimitry Andric } 56850b57cec5SDimitry Andric 568681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_funique_section_names, 568781ad6265SDimitry Andric options::OPT_fno_unique_section_names); 568881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_internal_linkage_names, 568981ad6265SDimitry Andric options::OPT_fno_unique_internal_linkage_names); 569081ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_basic_block_section_names, 569181ad6265SDimitry Andric options::OPT_fno_unique_basic_block_section_names); 56925ffd83dbSDimitry Andric 5693e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, 5694e8d8bef9SDimitry Andric options::OPT_fno_split_machine_functions)) { 5695e8d8bef9SDimitry Andric // This codegen pass is only available on x86-elf targets. 5696e8d8bef9SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 5697e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fsplit_machine_functions)) 5698e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5699e8d8bef9SDimitry Andric } else { 5700e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5701e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5702e8d8bef9SDimitry Andric } 5703e8d8bef9SDimitry Andric } 5704e8d8bef9SDimitry Andric 57050b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, 57060b57cec5SDimitry Andric options::OPT_finstrument_functions_after_inlining, 57070b57cec5SDimitry Andric options::OPT_finstrument_function_entry_bare); 57080b57cec5SDimitry Andric 57095ffd83dbSDimitry Andric // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support 57105ffd83dbSDimitry Andric // for sampling, overhead of call arc collection is way too high and there's 57115ffd83dbSDimitry Andric // no way to collect the output. 57125ffd83dbSDimitry Andric if (!Triple.isNVPTX() && !Triple.isAMDGCN()) 5713349cc55cSDimitry Andric addPGOAndCoverageFlags(TC, C, D, Output, Args, SanitizeArgs, CmdArgs); 57140b57cec5SDimitry Andric 57150b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); 57160b57cec5SDimitry Andric 5717bdd1243dSDimitry Andric if (getLastProfileSampleUseArg(Args) && 5718bdd1243dSDimitry Andric Args.hasArg(options::OPT_fsample_profile_use_profi)) { 5719bdd1243dSDimitry Andric CmdArgs.push_back("-mllvm"); 5720bdd1243dSDimitry Andric CmdArgs.push_back("-sample-profile-use-profi"); 5721bdd1243dSDimitry Andric } 5722bdd1243dSDimitry Andric 572381ad6265SDimitry Andric // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled. 572481ad6265SDimitry Andric if (RawTriple.isPS() && 57250b57cec5SDimitry Andric !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 572681ad6265SDimitry Andric PScpu::addProfileRTArgs(TC, Args, CmdArgs); 572781ad6265SDimitry Andric PScpu::addSanitizerArgs(TC, Args, CmdArgs); 57280b57cec5SDimitry Andric } 57290b57cec5SDimitry Andric 57300b57cec5SDimitry Andric // Pass options for controlling the default header search paths. 57310b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdinc)) { 57320b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 57330b57cec5SDimitry Andric CmdArgs.push_back("-nobuiltininc"); 57340b57cec5SDimitry Andric } else { 57350b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdlibinc)) 57360b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 57370b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 57380b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 57390b57cec5SDimitry Andric } 57400b57cec5SDimitry Andric 57410b57cec5SDimitry Andric // Pass the path to compiler resource files. 57420b57cec5SDimitry Andric CmdArgs.push_back("-resource-dir"); 57430b57cec5SDimitry Andric CmdArgs.push_back(D.ResourceDir.c_str()); 57440b57cec5SDimitry Andric 57450b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_working_directory); 57460b57cec5SDimitry Andric 57470b57cec5SDimitry Andric RenderARCMigrateToolOptions(D, Args, CmdArgs); 57480b57cec5SDimitry Andric 57490b57cec5SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 57500b57cec5SDimitry Andric // preprocessor. 57510b57cec5SDimitry Andric // 57520b57cec5SDimitry Andric // FIXME: Support -fpreprocessed 57530b57cec5SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 57540b57cec5SDimitry Andric AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 57550b57cec5SDimitry Andric 57560b57cec5SDimitry Andric // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 57570b57cec5SDimitry Andric // that "The compiler can only warn and ignore the option if not recognized". 57580b57cec5SDimitry Andric // When building with ccache, it will pass -D options to clang even on 57590b57cec5SDimitry Andric // preprocessed inputs and configure concludes that -fPIC is not supported. 57600b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_D); 57610b57cec5SDimitry Andric 57620b57cec5SDimitry Andric // Manually translate -O4 to -O3; let clang reject others. 57630b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 57640b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 57650b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 57660b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 57670b57cec5SDimitry Andric } else { 57680b57cec5SDimitry Andric A->render(Args, CmdArgs); 57690b57cec5SDimitry Andric } 57700b57cec5SDimitry Andric } 57710b57cec5SDimitry Andric 57720b57cec5SDimitry Andric // Warn about ignored options to clang. 57730b57cec5SDimitry Andric for (const Arg *A : 57740b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) { 57750b57cec5SDimitry Andric D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args); 57760b57cec5SDimitry Andric A->claim(); 57770b57cec5SDimitry Andric } 57780b57cec5SDimitry Andric 57790b57cec5SDimitry Andric for (const Arg *A : 57800b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) { 57810b57cec5SDimitry Andric D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args); 57820b57cec5SDimitry Andric A->claim(); 57830b57cec5SDimitry Andric } 57840b57cec5SDimitry Andric 57850b57cec5SDimitry Andric claimNoWarnArgs(Args); 57860b57cec5SDimitry Andric 57870b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_R_Group); 57880b57cec5SDimitry Andric 5789349cc55cSDimitry Andric for (const Arg *A : 5790349cc55cSDimitry Andric Args.filtered(options::OPT_W_Group, options::OPT__SLASH_wd)) { 5791349cc55cSDimitry Andric A->claim(); 5792349cc55cSDimitry Andric if (A->getOption().getID() == options::OPT__SLASH_wd) { 5793349cc55cSDimitry Andric unsigned WarningNumber; 5794349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, WarningNumber)) { 5795349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5796349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5797349cc55cSDimitry Andric continue; 5798349cc55cSDimitry Andric } 5799349cc55cSDimitry Andric 5800349cc55cSDimitry Andric if (auto Group = diagGroupFromCLWarningID(WarningNumber)) { 5801349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5802349cc55cSDimitry Andric "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group))); 5803349cc55cSDimitry Andric } 5804349cc55cSDimitry Andric continue; 5805349cc55cSDimitry Andric } 5806349cc55cSDimitry Andric A->render(Args, CmdArgs); 5807349cc55cSDimitry Andric } 5808349cc55cSDimitry Andric 58090b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 58100b57cec5SDimitry Andric CmdArgs.push_back("-pedantic"); 58110b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 58120b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_w); 58130b57cec5SDimitry Andric 5814bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_ffixed_point, 5815bdd1243dSDimitry Andric options::OPT_fno_fixed_point); 58160b57cec5SDimitry Andric 5817fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ)) 5818fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5819fe6060f1SDimitry Andric 5820fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5821fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5822fe6060f1SDimitry Andric 5823349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) 5824349cc55cSDimitry Andric A->render(Args, CmdArgs); 5825349cc55cSDimitry Andric 58260b57cec5SDimitry Andric // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 58270b57cec5SDimitry Andric // (-ansi is equivalent to -std=c89 or -std=c++98). 58280b57cec5SDimitry Andric // 58290b57cec5SDimitry Andric // If a std is supplied, only add -trigraphs if it follows the 58300b57cec5SDimitry Andric // option. 5831fe6060f1SDimitry Andric bool ImplyVCPPCVer = false; 58320b57cec5SDimitry Andric bool ImplyVCPPCXXVer = false; 58330b57cec5SDimitry Andric const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); 58340b57cec5SDimitry Andric if (Std) { 58350b57cec5SDimitry Andric if (Std->getOption().matches(options::OPT_ansi)) 58360b57cec5SDimitry Andric if (types::isCXX(InputType)) 58370b57cec5SDimitry Andric CmdArgs.push_back("-std=c++98"); 58380b57cec5SDimitry Andric else 58390b57cec5SDimitry Andric CmdArgs.push_back("-std=c89"); 58400b57cec5SDimitry Andric else 58410b57cec5SDimitry Andric Std->render(Args, CmdArgs); 58420b57cec5SDimitry Andric 58430b57cec5SDimitry Andric // If -f(no-)trigraphs appears after the language standard flag, honor it. 58440b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 58450b57cec5SDimitry Andric options::OPT_ftrigraphs, 58460b57cec5SDimitry Andric options::OPT_fno_trigraphs)) 58470b57cec5SDimitry Andric if (A != Std) 58480b57cec5SDimitry Andric A->render(Args, CmdArgs); 58490b57cec5SDimitry Andric } else { 58500b57cec5SDimitry Andric // Honor -std-default. 58510b57cec5SDimitry Andric // 58520b57cec5SDimitry Andric // FIXME: Clang doesn't correctly handle -std= when the input language 58530b57cec5SDimitry Andric // doesn't match. For the time being just ignore this for C++ inputs; 58540b57cec5SDimitry Andric // eventually we want to do all the standard defaulting here instead of 58550b57cec5SDimitry Andric // splitting it between the driver and clang -cc1. 5856fe6060f1SDimitry Andric if (!types::isCXX(InputType)) { 5857fe6060f1SDimitry Andric if (!Args.hasArg(options::OPT__SLASH_std)) { 58580b57cec5SDimitry Andric Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", 58590b57cec5SDimitry Andric /*Joined=*/true); 5860fe6060f1SDimitry Andric } else 5861fe6060f1SDimitry Andric ImplyVCPPCVer = true; 5862fe6060f1SDimitry Andric } 58630b57cec5SDimitry Andric else if (IsWindowsMSVC) 58640b57cec5SDimitry Andric ImplyVCPPCXXVer = true; 58650b57cec5SDimitry Andric 58660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs, 58670b57cec5SDimitry Andric options::OPT_fno_trigraphs); 58680b57cec5SDimitry Andric } 58690b57cec5SDimitry Andric 58700b57cec5SDimitry Andric // GCC's behavior for -Wwrite-strings is a bit strange: 58710b57cec5SDimitry Andric // * In C, this "warning flag" changes the types of string literals from 58720b57cec5SDimitry Andric // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning 58730b57cec5SDimitry Andric // for the discarded qualifier. 58740b57cec5SDimitry Andric // * In C++, this is just a normal warning flag. 58750b57cec5SDimitry Andric // 58760b57cec5SDimitry Andric // Implementing this warning correctly in C is hard, so we follow GCC's 58770b57cec5SDimitry Andric // behavior for now. FIXME: Directly diagnose uses of a string literal as 58780b57cec5SDimitry Andric // a non-const char* in C, rather than using this crude hack. 58790b57cec5SDimitry Andric if (!types::isCXX(InputType)) { 58800b57cec5SDimitry Andric // FIXME: This should behave just like a warning flag, and thus should also 58810b57cec5SDimitry Andric // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on. 58820b57cec5SDimitry Andric Arg *WriteStrings = 58830b57cec5SDimitry Andric Args.getLastArg(options::OPT_Wwrite_strings, 58840b57cec5SDimitry Andric options::OPT_Wno_write_strings, options::OPT_w); 58850b57cec5SDimitry Andric if (WriteStrings && 58860b57cec5SDimitry Andric WriteStrings->getOption().matches(options::OPT_Wwrite_strings)) 58870b57cec5SDimitry Andric CmdArgs.push_back("-fconst-strings"); 58880b57cec5SDimitry Andric } 58890b57cec5SDimitry Andric 58900b57cec5SDimitry Andric // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 58910b57cec5SDimitry Andric // during C++ compilation, which it is by default. GCC keeps this define even 58920b57cec5SDimitry Andric // in the presence of '-w', match this behavior bug-for-bug. 58930b57cec5SDimitry Andric if (types::isCXX(InputType) && 58940b57cec5SDimitry Andric Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 58950b57cec5SDimitry Andric true)) { 58960b57cec5SDimitry Andric CmdArgs.push_back("-fdeprecated-macro"); 58970b57cec5SDimitry Andric } 58980b57cec5SDimitry Andric 58990b57cec5SDimitry Andric // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 59000b57cec5SDimitry Andric if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 59010b57cec5SDimitry Andric if (Asm->getOption().matches(options::OPT_fasm)) 59020b57cec5SDimitry Andric CmdArgs.push_back("-fgnu-keywords"); 59030b57cec5SDimitry Andric else 59040b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-keywords"); 59050b57cec5SDimitry Andric } 59060b57cec5SDimitry Andric 5907480093f4SDimitry Andric if (!ShouldEnableAutolink(Args, TC, JA)) 59080b57cec5SDimitry Andric CmdArgs.push_back("-fno-autolink"); 59090b57cec5SDimitry Andric 59100b57cec5SDimitry Andric // Add in -fdebug-compilation-dir if necessary. 59110eae32dcSDimitry Andric const char *DebugCompilationDir = 5912a7dea167SDimitry Andric addDebugCompDirArg(Args, CmdArgs, D.getVFS()); 59130b57cec5SDimitry Andric 591481ad6265SDimitry Andric addDebugPrefixMapArg(D, TC, Args, CmdArgs); 59150b57cec5SDimitry Andric 59160b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, 59170b57cec5SDimitry Andric options::OPT_ftemplate_depth_EQ)) { 59180b57cec5SDimitry Andric CmdArgs.push_back("-ftemplate-depth"); 59190b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59200b57cec5SDimitry Andric } 59210b57cec5SDimitry Andric 59220b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) { 59230b57cec5SDimitry Andric CmdArgs.push_back("-foperator-arrow-depth"); 59240b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59250b57cec5SDimitry Andric } 59260b57cec5SDimitry Andric 59270b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) { 59280b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-depth"); 59290b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59300b57cec5SDimitry Andric } 59310b57cec5SDimitry Andric 59320b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) { 59330b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-steps"); 59340b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59350b57cec5SDimitry Andric } 59360b57cec5SDimitry Andric 5937fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_library); 593881ad6265SDimitry Andric 5939a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter)) 5940a7dea167SDimitry Andric CmdArgs.push_back("-fexperimental-new-constant-interpreter"); 5941a7dea167SDimitry Andric 59420b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 59430b57cec5SDimitry Andric CmdArgs.push_back("-fbracket-depth"); 59440b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59450b57cec5SDimitry Andric } 59460b57cec5SDimitry Andric 59470b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 59480b57cec5SDimitry Andric options::OPT_Wlarge_by_value_copy_def)) { 59490b57cec5SDimitry Andric if (A->getNumValues()) { 59500b57cec5SDimitry Andric StringRef bytes = A->getValue(); 59510b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 59520b57cec5SDimitry Andric } else 59530b57cec5SDimitry Andric CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 59540b57cec5SDimitry Andric } 59550b57cec5SDimitry Andric 59560b57cec5SDimitry Andric if (Args.hasArg(options::OPT_relocatable_pch)) 59570b57cec5SDimitry Andric CmdArgs.push_back("-relocatable-pch"); 59580b57cec5SDimitry Andric 59590b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) { 59600b57cec5SDimitry Andric static const char *kCFABIs[] = { 59610b57cec5SDimitry Andric "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1", 59620b57cec5SDimitry Andric }; 59630b57cec5SDimitry Andric 596481ad6265SDimitry Andric if (!llvm::is_contained(kCFABIs, StringRef(A->getValue()))) 59650b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue(); 59660b57cec5SDimitry Andric else 59670b57cec5SDimitry Andric A->render(Args, CmdArgs); 59680b57cec5SDimitry Andric } 59690b57cec5SDimitry Andric 59700b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 59710b57cec5SDimitry Andric CmdArgs.push_back("-fconstant-string-class"); 59720b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59730b57cec5SDimitry Andric } 59740b57cec5SDimitry Andric 59750b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 59760b57cec5SDimitry Andric CmdArgs.push_back("-ftabstop"); 59770b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59780b57cec5SDimitry Andric } 59790b57cec5SDimitry Andric 5980bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_size_section, 5981bdd1243dSDimitry Andric options::OPT_fno_stack_size_section); 59820b57cec5SDimitry Andric 5983fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fstack_usage)) { 5984fe6060f1SDimitry Andric CmdArgs.push_back("-stack-usage-file"); 5985fe6060f1SDimitry Andric 5986fe6060f1SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 5987fe6060f1SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 5988fe6060f1SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "su"); 5989fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 5990fe6060f1SDimitry Andric } else 5991fe6060f1SDimitry Andric CmdArgs.push_back( 5992fe6060f1SDimitry Andric Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su")); 5993fe6060f1SDimitry Andric } 5994fe6060f1SDimitry Andric 59950b57cec5SDimitry Andric CmdArgs.push_back("-ferror-limit"); 59960b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 59970b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59980b57cec5SDimitry Andric else 59990b57cec5SDimitry Andric CmdArgs.push_back("19"); 60000b57cec5SDimitry Andric 60010b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) { 60020b57cec5SDimitry Andric CmdArgs.push_back("-fmacro-backtrace-limit"); 60030b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 60040b57cec5SDimitry Andric } 60050b57cec5SDimitry Andric 60060b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) { 60070b57cec5SDimitry Andric CmdArgs.push_back("-ftemplate-backtrace-limit"); 60080b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 60090b57cec5SDimitry Andric } 60100b57cec5SDimitry Andric 60110b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) { 60120b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-backtrace-limit"); 60130b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 60140b57cec5SDimitry Andric } 60150b57cec5SDimitry Andric 60160b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) { 60170b57cec5SDimitry Andric CmdArgs.push_back("-fspell-checking-limit"); 60180b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 60190b57cec5SDimitry Andric } 60200b57cec5SDimitry Andric 60210b57cec5SDimitry Andric // Pass -fmessage-length=. 60225ffd83dbSDimitry Andric unsigned MessageLength = 0; 60230b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 60245ffd83dbSDimitry Andric StringRef V(A->getValue()); 60255ffd83dbSDimitry Andric if (V.getAsInteger(0, MessageLength)) 60265ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 60275ffd83dbSDimitry Andric << V << A->getOption().getName(); 60280b57cec5SDimitry Andric } else { 60290b57cec5SDimitry Andric // If -fmessage-length=N was not specified, determine whether this is a 60300b57cec5SDimitry Andric // terminal and, if so, implicitly define -fmessage-length appropriately. 60315ffd83dbSDimitry Andric MessageLength = llvm::sys::Process::StandardErrColumns(); 60320b57cec5SDimitry Andric } 60335ffd83dbSDimitry Andric if (MessageLength != 0) 60345ffd83dbSDimitry Andric CmdArgs.push_back( 60355ffd83dbSDimitry Andric Args.MakeArgString("-fmessage-length=" + Twine(MessageLength))); 60360b57cec5SDimitry Andric 603781ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_EQ)) 603881ad6265SDimitry Andric CmdArgs.push_back( 603981ad6265SDimitry Andric Args.MakeArgString("-frandomize-layout-seed=" + Twine(A->getValue(0)))); 604081ad6265SDimitry Andric 604181ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_file_EQ)) 604281ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frandomize-layout-seed-file=" + 604381ad6265SDimitry Andric Twine(A->getValue(0)))); 604481ad6265SDimitry Andric 60450b57cec5SDimitry Andric // -fvisibility= and -fvisibility-ms-compat are of a piece. 60460b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 60470b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 60480b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 6049bdd1243dSDimitry Andric A->render(Args, CmdArgs); 60500b57cec5SDimitry Andric } else { 60510b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 6052bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=hidden"); 6053bdd1243dSDimitry Andric CmdArgs.push_back("-ftype-visibility=default"); 60540b57cec5SDimitry Andric } 605504eeddc0SDimitry Andric } else if (IsOpenMPDevice) { 605604eeddc0SDimitry Andric // When compiling for the OpenMP device we want protected visibility by 6057bdd1243dSDimitry Andric // default. This prevents the device from accidentally preempting code on 6058bdd1243dSDimitry Andric // the host, makes the system more robust, and improves performance. 6059bdd1243dSDimitry Andric CmdArgs.push_back("-fvisibility=protected"); 60600b57cec5SDimitry Andric } 60610b57cec5SDimitry Andric 6062bdd1243dSDimitry Andric // PS4/PS5 process these options in addClangTargetOptions. 6063bdd1243dSDimitry Andric if (!RawTriple.isPS()) { 6064e8d8bef9SDimitry Andric if (const Arg *A = 6065e8d8bef9SDimitry Andric Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass, 6066e8d8bef9SDimitry Andric options::OPT_fno_visibility_from_dllstorageclass)) { 6067e8d8bef9SDimitry Andric if (A->getOption().matches( 6068e8d8bef9SDimitry Andric options::OPT_fvisibility_from_dllstorageclass)) { 6069e8d8bef9SDimitry Andric CmdArgs.push_back("-fvisibility-from-dllstorageclass"); 6070e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_dllexport_EQ); 6071e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_nodllstorageclass_EQ); 6072e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_externs_dllimport_EQ); 6073e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, 6074e8d8bef9SDimitry Andric options::OPT_fvisibility_externs_nodllstorageclass_EQ); 6075e8d8bef9SDimitry Andric } 6076e8d8bef9SDimitry Andric } 6077bdd1243dSDimitry Andric } 6078e8d8bef9SDimitry Andric 6079e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mignore_xcoff_visibility)) { 6080e8d8bef9SDimitry Andric if (Triple.isOSAIX()) 6081e8d8bef9SDimitry Andric CmdArgs.push_back("-mignore-xcoff-visibility"); 6082e8d8bef9SDimitry Andric else 6083e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6084e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 6085e8d8bef9SDimitry Andric } 6086e8d8bef9SDimitry Andric 608781ad6265SDimitry Andric if (const Arg *A = 608881ad6265SDimitry Andric Args.getLastArg(options::OPT_mdefault_visibility_export_mapping_EQ)) { 608981ad6265SDimitry Andric if (Triple.isOSAIX()) 609081ad6265SDimitry Andric A->render(Args, CmdArgs); 609181ad6265SDimitry Andric else 609281ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 609381ad6265SDimitry Andric << A->getAsString(Args) << TripleStr; 609481ad6265SDimitry Andric } 6095fe6060f1SDimitry Andric 6096fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden, 6097fe6060f1SDimitry Andric options::OPT_fno_visibility_inlines_hidden, false)) 6098fe6060f1SDimitry Andric CmdArgs.push_back("-fvisibility-inlines-hidden"); 6099fe6060f1SDimitry Andric 6100e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, 6101e8d8bef9SDimitry Andric options::OPT_fno_visibility_inlines_hidden_static_local_var); 61020b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); 61030b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 61040b57cec5SDimitry Andric 61054824e7fdSDimitry Andric if (Args.hasFlag(options::OPT_fnew_infallible, 61064824e7fdSDimitry Andric options::OPT_fno_new_infallible, false)) 61074824e7fdSDimitry Andric CmdArgs.push_back("-fnew-infallible"); 61084824e7fdSDimitry Andric 6109fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fno_operator_names, 6110fe6060f1SDimitry Andric options::OPT_foperator_names, false)) 6111fe6060f1SDimitry Andric CmdArgs.push_back("-fno-operator-names"); 6112fe6060f1SDimitry Andric 61130b57cec5SDimitry Andric // Forward -f (flag) options which we can pass directly. 61140b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 61150b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 61160b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); 61170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femulated_tls, 61180b57cec5SDimitry Andric options::OPT_fno_emulated_tls); 611981ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); 612081ad6265SDimitry Andric 612181ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { 612281ad6265SDimitry Andric // FIXME: There's no reason for this to be restricted to X86. The backend 612381ad6265SDimitry Andric // code needs to be changed to include the appropriate function calls 612481ad6265SDimitry Andric // automatically. 612581ad6265SDimitry Andric if (!Triple.isX86() && !Triple.isAArch64()) 612681ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 612781ad6265SDimitry Andric << A->getAsString(Args) << TripleStr; 612881ad6265SDimitry Andric } 61290b57cec5SDimitry Andric 61300b57cec5SDimitry Andric // AltiVec-like language extensions aren't relevant for assembling. 61310b57cec5SDimitry Andric if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) 61320b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzvector); 61330b57cec5SDimitry Andric 61340b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 61350b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 61360b57cec5SDimitry Andric 61370b57cec5SDimitry Andric // Forward flags for OpenMP. We don't do this if the current action is an 61380b57cec5SDimitry Andric // device offloading action other than OpenMP. 61390b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, 61400b57cec5SDimitry Andric options::OPT_fno_openmp, false) && 61410b57cec5SDimitry Andric (JA.isDeviceOffloading(Action::OFK_None) || 61420b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_OpenMP))) { 61430b57cec5SDimitry Andric switch (D.getOpenMPRuntime(Args)) { 61440b57cec5SDimitry Andric case Driver::OMPRT_OMP: 61450b57cec5SDimitry Andric case Driver::OMPRT_IOMP5: 61460b57cec5SDimitry Andric // Clang can generate useful OpenMP code for these two runtime libraries. 61470b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp"); 61480b57cec5SDimitry Andric 61490b57cec5SDimitry Andric // If no option regarding the use of TLS in OpenMP codegeneration is 61500b57cec5SDimitry Andric // given, decide a default based on the target. Otherwise rely on the 61510b57cec5SDimitry Andric // options and pass the right information to the frontend. 61520b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_use_tls, 61530b57cec5SDimitry Andric options::OPT_fnoopenmp_use_tls, /*Default=*/true)) 61540b57cec5SDimitry Andric CmdArgs.push_back("-fnoopenmp-use-tls"); 61550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 61560b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 6157480093f4SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder); 61580b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 6159349cc55cSDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_extensions, 6160349cc55cSDimitry Andric options::OPT_fno_openmp_extensions, /*Default=*/true)) 6161349cc55cSDimitry Andric CmdArgs.push_back("-fno-openmp-extensions"); 61620b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ); 61630b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ); 61640b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 61650b57cec5SDimitry Andric options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ); 61660b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse, 61670b57cec5SDimitry Andric options::OPT_fno_openmp_optimistic_collapse, 61680b57cec5SDimitry Andric /*Default=*/false)) 61690b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-optimistic-collapse"); 61700b57cec5SDimitry Andric 61710b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, forward 61720b57cec5SDimitry Andric // cuda-mode flag 61730b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_mode, 61740b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_mode, /*Default=*/false)) 61750b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-mode"); 61760b57cec5SDimitry Andric 6177349cc55cSDimitry Andric // When in OpenMP offloading mode, enable debugging on the device. 6178349cc55cSDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ); 6179349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_debug, 6180349cc55cSDimitry Andric options::OPT_fno_openmp_target_debug, /*Default=*/false)) 6181349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-target-debug"); 6182349cc55cSDimitry Andric 6183349cc55cSDimitry Andric // When in OpenMP offloading mode, forward assumptions information about 6184349cc55cSDimitry Andric // thread and team counts in the device. 6185349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription, 6186349cc55cSDimitry Andric options::OPT_fno_openmp_assume_teams_oversubscription, 6187349cc55cSDimitry Andric /*Default=*/false)) 6188349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-teams-oversubscription"); 6189349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription, 6190349cc55cSDimitry Andric options::OPT_fno_openmp_assume_threads_oversubscription, 6191349cc55cSDimitry Andric /*Default=*/false)) 6192349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-threads-oversubscription"); 619381ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_thread_state)) 619481ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-thread-state"); 6195bdd1243dSDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_nested_parallelism)) 6196bdd1243dSDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-nested-parallelism"); 619781ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_offload_mandatory)) 619881ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-offload-mandatory"); 61990b57cec5SDimitry Andric break; 62000b57cec5SDimitry Andric default: 62010b57cec5SDimitry Andric // By default, if Clang doesn't know how to generate useful OpenMP code 62020b57cec5SDimitry Andric // for a specific runtime library, we just don't pass the '-fopenmp' flag 62030b57cec5SDimitry Andric // down to the actual compilation. 62040b57cec5SDimitry Andric // FIXME: It would be better to have a mode which *only* omits IR 62050b57cec5SDimitry Andric // generation based on the OpenMP support so that we get consistent 62060b57cec5SDimitry Andric // semantic analysis, etc. 62070b57cec5SDimitry Andric break; 62080b57cec5SDimitry Andric } 62090b57cec5SDimitry Andric } else { 62100b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 62110b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 62120b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 621381ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fopenmp_extensions, 621481ad6265SDimitry Andric options::OPT_fno_openmp_extensions); 62150b57cec5SDimitry Andric } 62160b57cec5SDimitry Andric 621781ad6265SDimitry Andric // Forward the new driver to change offloading code generation. 6218bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_offload_new_driver, 6219bdd1243dSDimitry Andric options::OPT_no_offload_new_driver, false)) 622081ad6265SDimitry Andric CmdArgs.push_back("--offload-new-driver"); 622181ad6265SDimitry Andric 6222349cc55cSDimitry Andric SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); 62230b57cec5SDimitry Andric 62240b57cec5SDimitry Andric const XRayArgs &XRay = TC.getXRayArgs(); 62250b57cec5SDimitry Andric XRay.addArgs(TC, Args, CmdArgs, InputType); 62260b57cec5SDimitry Andric 6227e8d8bef9SDimitry Andric for (const auto &Filename : 6228e8d8bef9SDimitry Andric Args.getAllArgValues(options::OPT_fprofile_list_EQ)) { 6229e8d8bef9SDimitry Andric if (D.getVFS().exists(Filename)) 6230e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-list=" + Filename)); 6231e8d8bef9SDimitry Andric else 6232e8d8bef9SDimitry Andric D.Diag(clang::diag::err_drv_no_such_file) << Filename; 6233e8d8bef9SDimitry Andric } 6234e8d8bef9SDimitry Andric 6235480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) { 6236480093f4SDimitry Andric StringRef S0 = A->getValue(), S = S0; 623755e4f9d5SDimitry Andric unsigned Size, Offset = 0; 6238fe6060f1SDimitry Andric if (!Triple.isAArch64() && !Triple.isRISCV() && !Triple.isX86()) 6239480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6240480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6241480093f4SDimitry Andric else if (S.consumeInteger(10, Size) || 6242480093f4SDimitry Andric (!S.empty() && (!S.consume_front(",") || 624355e4f9d5SDimitry Andric S.consumeInteger(10, Offset) || !S.empty()))) 6244480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 6245480093f4SDimitry Andric << S0 << A->getOption().getName(); 624655e4f9d5SDimitry Andric else if (Size < Offset) 6247480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument); 624855e4f9d5SDimitry Andric else { 6249480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size))); 625055e4f9d5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 625155e4f9d5SDimitry Andric "-fpatchable-function-entry-offset=" + Twine(Offset))); 625255e4f9d5SDimitry Andric } 6253480093f4SDimitry Andric } 6254480093f4SDimitry Andric 625504eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch); 625604eeddc0SDimitry Andric 6257480093f4SDimitry Andric if (TC.SupportsProfiling()) { 62580b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pg); 62590b57cec5SDimitry Andric 6260480093f4SDimitry Andric llvm::Triple::ArchType Arch = TC.getArch(); 6261480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfentry)) { 6262480093f4SDimitry Andric if (Arch == llvm::Triple::systemz || TC.getTriple().isX86()) 6263480093f4SDimitry Andric A->render(Args, CmdArgs); 6264480093f4SDimitry Andric else 6265480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6266480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6267480093f4SDimitry Andric } 6268480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) { 6269480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6270480093f4SDimitry Andric A->render(Args, CmdArgs); 6271480093f4SDimitry Andric else 6272480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6273480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6274480093f4SDimitry Andric } 6275480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) { 6276480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6277480093f4SDimitry Andric A->render(Args, CmdArgs); 6278480093f4SDimitry Andric else 6279480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6280480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6281480093f4SDimitry Andric } 6282480093f4SDimitry Andric } 6283bdd1243dSDimitry Andric if (Arg *A = Args.getLastArgNoClaim(options::OPT_p)) { 6284bdd1243dSDimitry Andric if (!TC.getTriple().isOSAIX() && !TC.getTriple().isOSOpenBSD()) { 6285bdd1243dSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6286bdd1243dSDimitry Andric << A->getAsString(Args) << TripleStr; 6287bdd1243dSDimitry Andric } 6288bdd1243dSDimitry Andric } 62890b57cec5SDimitry Andric 62900b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_fapple_kext) || 62910b57cec5SDimitry Andric (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) 62920b57cec5SDimitry Andric CmdArgs.push_back("-fapple-kext"); 62930b57cec5SDimitry Andric 6294fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat); 6295a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ); 62960b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 62970b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 62980b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 62990b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 6300e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ); 63010b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace); 6302a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); 6303fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_EQ); 63040b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 63050b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_malign_double); 6306480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file); 63070b57cec5SDimitry Andric 63080b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 63090b57cec5SDimitry Andric CmdArgs.push_back("-ftrapv-handler"); 63100b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 63110b57cec5SDimitry Andric } 63120b57cec5SDimitry Andric 63130b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 63140b57cec5SDimitry Andric 63150b57cec5SDimitry Andric // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 63160b57cec5SDimitry Andric // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 63170b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) { 63180b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fwrapv)) 63190b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 63200b57cec5SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 63210b57cec5SDimitry Andric options::OPT_fno_strict_overflow)) { 63220b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_strict_overflow)) 63230b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 63240b57cec5SDimitry Andric } 63250b57cec5SDimitry Andric 63260b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_freroll_loops, 63270b57cec5SDimitry Andric options::OPT_fno_reroll_loops)) 63280b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_freroll_loops)) 63290b57cec5SDimitry Andric CmdArgs.push_back("-freroll-loops"); 63300b57cec5SDimitry Andric 6331d409305fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops, 6332d409305fSDimitry Andric options::OPT_fno_finite_loops); 6333d409305fSDimitry Andric 63340b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 63350b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 63360b57cec5SDimitry Andric options::OPT_fno_unroll_loops); 63370b57cec5SDimitry Andric 6338fcaf7f86SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fstrict_flex_arrays_EQ); 6339fcaf7f86SDimitry Andric 63400b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pthread); 63410b57cec5SDimitry Andric 6342bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mspeculative_load_hardening, 6343bdd1243dSDimitry Andric options::OPT_mno_speculative_load_hardening); 63440b57cec5SDimitry Andric 6345e8d8bef9SDimitry Andric RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext); 63465ffd83dbSDimitry Andric RenderSCPOptions(TC, Args, CmdArgs); 63470b57cec5SDimitry Andric RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs); 63480b57cec5SDimitry Andric 6349349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fswift_async_fp_EQ); 6350349cc55cSDimitry Andric 6351bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_mstackrealign, 6352bdd1243dSDimitry Andric options::OPT_mno_stackrealign); 63530b57cec5SDimitry Andric 63540b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_alignment)) { 63550b57cec5SDimitry Andric StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 63560b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 63570b57cec5SDimitry Andric } 63580b57cec5SDimitry Andric 63590b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_probe_size)) { 63600b57cec5SDimitry Andric StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size); 63610b57cec5SDimitry Andric 63620b57cec5SDimitry Andric if (!Size.empty()) 63630b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size)); 63640b57cec5SDimitry Andric else 63650b57cec5SDimitry Andric CmdArgs.push_back("-mstack-probe-size=0"); 63660b57cec5SDimitry Andric } 63670b57cec5SDimitry Andric 636881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_mstack_arg_probe, 636981ad6265SDimitry Andric options::OPT_mno_stack_arg_probe); 63700b57cec5SDimitry Andric 63710b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it, 63720b57cec5SDimitry Andric options::OPT_mno_restrict_it)) { 63730b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mrestrict_it)) { 63740b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 63750b57cec5SDimitry Andric CmdArgs.push_back("-arm-restrict-it"); 63760b57cec5SDimitry Andric } else { 63770b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 637881ad6265SDimitry Andric CmdArgs.push_back("-arm-default-it"); 63790b57cec5SDimitry Andric } 63800b57cec5SDimitry Andric } 63810b57cec5SDimitry Andric 63820b57cec5SDimitry Andric // Forward -cl options to -cc1 6383fe6060f1SDimitry Andric RenderOpenCLOptions(Args, CmdArgs, InputType); 63840b57cec5SDimitry Andric 638581ad6265SDimitry Andric // Forward hlsl options to -cc1 638681ad6265SDimitry Andric RenderHLSLOptions(Args, CmdArgs, InputType); 638781ad6265SDimitry Andric 6388e8d8bef9SDimitry Andric if (IsHIP) { 6389e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fhip_new_launch_api, 63905ffd83dbSDimitry Andric options::OPT_fno_hip_new_launch_api, true)) 6391a7dea167SDimitry Andric CmdArgs.push_back("-fhip-new-launch-api"); 6392e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_allow_device_init, 6393e8d8bef9SDimitry Andric options::OPT_fno_gpu_allow_device_init, false)) 6394e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-allow-device-init"); 639581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fhip_kernel_arg_name, 639681ad6265SDimitry Andric options::OPT_fno_hip_kernel_arg_name); 6397e8d8bef9SDimitry Andric } 6398e8d8bef9SDimitry Andric 6399e8d8bef9SDimitry Andric if (IsCuda || IsHIP) { 6400753f127fSDimitry Andric if (IsRDCMode) 6401fe6060f1SDimitry Andric CmdArgs.push_back("-fgpu-rdc"); 6402e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_defer_diag, 6403e8d8bef9SDimitry Andric options::OPT_fno_gpu_defer_diag, false)) 6404e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6405e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads, 6406e8d8bef9SDimitry Andric options::OPT_fno_gpu_exclude_wrong_side_overloads, 6407e8d8bef9SDimitry Andric false)) { 6408e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-exclude-wrong-side-overloads"); 6409e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6410e8d8bef9SDimitry Andric } 6411e8d8bef9SDimitry Andric } 6412a7dea167SDimitry Andric 641381ad6265SDimitry Andric // Forward -nogpulib to -cc1. 641481ad6265SDimitry Andric if (Args.hasArg(options::OPT_nogpulib)) 641581ad6265SDimitry Andric CmdArgs.push_back("-nogpulib"); 641681ad6265SDimitry Andric 64170b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { 64180b57cec5SDimitry Andric CmdArgs.push_back( 64190b57cec5SDimitry Andric Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); 64200b57cec5SDimitry Andric } 64210b57cec5SDimitry Andric 6422753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ)) 6423753f127fSDimitry Andric CmdArgs.push_back( 6424753f127fSDimitry Andric Args.MakeArgString(Twine("-mfunction-return=") + A->getValue())); 6425753f127fSDimitry Andric 6426bdd1243dSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mindirect_branch_cs_prefix); 6427bdd1243dSDimitry Andric 6428fe6060f1SDimitry Andric // Forward -f options with positive and negative forms; we translate these by 6429fe6060f1SDimitry Andric // hand. Do not propagate PGO options to the GPU-side compilations as the 6430fe6060f1SDimitry Andric // profile info is for the host-side compilation only. 6431fe6060f1SDimitry Andric if (!(IsCudaDevice || IsHIPDevice)) { 64320b57cec5SDimitry Andric if (Arg *A = getLastProfileSampleUseArg(Args)) { 64330b57cec5SDimitry Andric auto *PGOArg = Args.getLastArg( 64340b57cec5SDimitry Andric options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, 6435fe6060f1SDimitry Andric options::OPT_fcs_profile_generate, 6436fe6060f1SDimitry Andric options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use, 6437fe6060f1SDimitry Andric options::OPT_fprofile_use_EQ); 64380b57cec5SDimitry Andric if (PGOArg) 64390b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 64400b57cec5SDimitry Andric << "SampleUse with PGO options"; 64410b57cec5SDimitry Andric 64420b57cec5SDimitry Andric StringRef fname = A->getValue(); 64430b57cec5SDimitry Andric if (!llvm::sys::fs::exists(fname)) 64440b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << fname; 64450b57cec5SDimitry Andric else 64460b57cec5SDimitry Andric A->render(Args, CmdArgs); 64470b57cec5SDimitry Andric } 64480b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); 64490b57cec5SDimitry Andric 6450e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, 6451fe6060f1SDimitry Andric options::OPT_fno_pseudo_probe_for_profiling, false)) { 6452e8d8bef9SDimitry Andric CmdArgs.push_back("-fpseudo-probe-for-profiling"); 6453fe6060f1SDimitry Andric // Enforce -funique-internal-linkage-names if it's not explicitly turned 6454fe6060f1SDimitry Andric // off. 6455fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_funique_internal_linkage_names, 6456fe6060f1SDimitry Andric options::OPT_fno_unique_internal_linkage_names, true)) 6457fe6060f1SDimitry Andric CmdArgs.push_back("-funique-internal-linkage-names"); 6458fe6060f1SDimitry Andric } 6459fe6060f1SDimitry Andric } 64600b57cec5SDimitry Andric RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs); 64610b57cec5SDimitry Andric 646281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 646381ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 64640b57cec5SDimitry Andric 64650b57cec5SDimitry Andric // -fblocks=0 is default. 64660b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 64670b57cec5SDimitry Andric TC.IsBlocksDefault()) || 64680b57cec5SDimitry Andric (Args.hasArg(options::OPT_fgnu_runtime) && 64690b57cec5SDimitry Andric Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 64700b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_blocks))) { 64710b57cec5SDimitry Andric CmdArgs.push_back("-fblocks"); 64720b57cec5SDimitry Andric 64730b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime()) 64740b57cec5SDimitry Andric CmdArgs.push_back("-fblocks-runtime-optional"); 64750b57cec5SDimitry Andric } 64760b57cec5SDimitry Andric 64770b57cec5SDimitry Andric // -fencode-extended-block-signature=1 is default. 64780b57cec5SDimitry Andric if (TC.IsEncodeExtendedBlockSignatureDefault()) 64790b57cec5SDimitry Andric CmdArgs.push_back("-fencode-extended-block-signature"); 64800b57cec5SDimitry Andric 64810b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts, 64820b57cec5SDimitry Andric false) && 64830b57cec5SDimitry Andric types::isCXX(InputType)) { 6484bdd1243dSDimitry Andric D.Diag(diag::warn_deperecated_fcoroutines_ts_flag); 64850b57cec5SDimitry Andric CmdArgs.push_back("-fcoroutines-ts"); 64860b57cec5SDimitry Andric } 64870b57cec5SDimitry Andric 6488bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_fcoro_aligned_allocation, 6489bdd1243dSDimitry Andric options::OPT_fno_coro_aligned_allocation, false) && 6490bdd1243dSDimitry Andric types::isCXX(InputType)) 6491bdd1243dSDimitry Andric CmdArgs.push_back("-fcoro-aligned-allocation"); 6492bdd1243dSDimitry Andric 64930b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes, 64940b57cec5SDimitry Andric options::OPT_fno_double_square_bracket_attributes); 64950b57cec5SDimitry Andric 649681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_faccess_control, 649781ad6265SDimitry Andric options::OPT_fno_access_control); 649881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_felide_constructors, 649981ad6265SDimitry Andric options::OPT_fno_elide_constructors); 65000b57cec5SDimitry Andric 65010b57cec5SDimitry Andric ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); 65020b57cec5SDimitry Andric 65030b57cec5SDimitry Andric if (KernelOrKext || (types::isCXX(InputType) && 65040b57cec5SDimitry Andric (RTTIMode == ToolChain::RM_Disabled))) 65050b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti"); 65060b57cec5SDimitry Andric 6507e8d8bef9SDimitry Andric // -fshort-enums=0 is default for all architectures except Hexagon and z/OS. 65080b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums, 6509e8d8bef9SDimitry Andric TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS())) 65100b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 65110b57cec5SDimitry Andric 65120b57cec5SDimitry Andric RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs); 65130b57cec5SDimitry Andric 65140b57cec5SDimitry Andric // -fuse-cxa-atexit is default. 65150b57cec5SDimitry Andric if (!Args.hasFlag( 65160b57cec5SDimitry Andric options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit, 65175ffd83dbSDimitry Andric !RawTriple.isOSAIX() && !RawTriple.isOSWindows() && 65180b57cec5SDimitry Andric ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) || 65190b57cec5SDimitry Andric RawTriple.hasEnvironment())) || 65200b57cec5SDimitry Andric KernelOrKext) 65210b57cec5SDimitry Andric CmdArgs.push_back("-fno-use-cxa-atexit"); 65220b57cec5SDimitry Andric 65230b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit, 65240b57cec5SDimitry Andric options::OPT_fno_register_global_dtors_with_atexit, 65250b57cec5SDimitry Andric RawTriple.isOSDarwin() && !KernelOrKext)) 65260b57cec5SDimitry Andric CmdArgs.push_back("-fregister-global-dtors-with-atexit"); 65270b57cec5SDimitry Andric 652881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fuse_line_directives, 652981ad6265SDimitry Andric options::OPT_fno_use_line_directives); 65300b57cec5SDimitry Andric 6531349cc55cSDimitry Andric // -fno-minimize-whitespace is default. 6532349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fminimize_whitespace, 6533349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace, false)) { 6534349cc55cSDimitry Andric types::ID InputType = Inputs[0].getType(); 6535349cc55cSDimitry Andric if (!isDerivedFromC(InputType)) 6536349cc55cSDimitry Andric D.Diag(diag::err_drv_minws_unsupported_input_type) 6537349cc55cSDimitry Andric << types::getTypeName(InputType); 6538349cc55cSDimitry Andric CmdArgs.push_back("-fminimize-whitespace"); 6539349cc55cSDimitry Andric } 6540349cc55cSDimitry Andric 65415ffd83dbSDimitry Andric // -fms-extensions=0 is default. 65425ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 65435ffd83dbSDimitry Andric IsWindowsMSVC)) 65445ffd83dbSDimitry Andric CmdArgs.push_back("-fms-extensions"); 65455ffd83dbSDimitry Andric 65460b57cec5SDimitry Andric // -fms-compatibility=0 is default. 6547a7dea167SDimitry Andric bool IsMSVCCompat = Args.hasFlag( 6548a7dea167SDimitry Andric options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, 6549a7dea167SDimitry Andric (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, 6550a7dea167SDimitry Andric options::OPT_fno_ms_extensions, true))); 6551a7dea167SDimitry Andric if (IsMSVCCompat) 65520b57cec5SDimitry Andric CmdArgs.push_back("-fms-compatibility"); 65530b57cec5SDimitry Andric 6554bdd1243dSDimitry Andric if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() && 6555bdd1243dSDimitry Andric Args.hasArg(options::OPT_fms_runtime_lib_EQ)) 6556bdd1243dSDimitry Andric ProcessVSRuntimeLibrary(Args, CmdArgs); 6557bdd1243dSDimitry Andric 6558a7dea167SDimitry Andric // Handle -fgcc-version, if present. 6559a7dea167SDimitry Andric VersionTuple GNUCVer; 6560a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) { 6561a7dea167SDimitry Andric // Check that the version has 1 to 3 components and the minor and patch 6562a7dea167SDimitry Andric // versions fit in two decimal digits. 6563a7dea167SDimitry Andric StringRef Val = A->getValue(); 6564a7dea167SDimitry Andric Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable. 6565a7dea167SDimitry Andric bool Invalid = GNUCVer.tryParse(Val); 656681ad6265SDimitry Andric unsigned Minor = GNUCVer.getMinor().value_or(0); 656781ad6265SDimitry Andric unsigned Patch = GNUCVer.getSubminor().value_or(0); 6568a7dea167SDimitry Andric if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) { 6569a7dea167SDimitry Andric D.Diag(diag::err_drv_invalid_value) 6570a7dea167SDimitry Andric << A->getAsString(Args) << A->getValue(); 6571a7dea167SDimitry Andric } 6572a7dea167SDimitry Andric } else if (!IsMSVCCompat) { 6573a7dea167SDimitry Andric // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect. 6574a7dea167SDimitry Andric GNUCVer = VersionTuple(4, 2, 1); 6575a7dea167SDimitry Andric } 6576a7dea167SDimitry Andric if (!GNUCVer.empty()) { 6577a7dea167SDimitry Andric CmdArgs.push_back( 6578a7dea167SDimitry Andric Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString())); 6579a7dea167SDimitry Andric } 6580a7dea167SDimitry Andric 65810b57cec5SDimitry Andric VersionTuple MSVT = TC.computeMSVCVersion(&D, Args); 65820b57cec5SDimitry Andric if (!MSVT.empty()) 65830b57cec5SDimitry Andric CmdArgs.push_back( 65840b57cec5SDimitry Andric Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString())); 65850b57cec5SDimitry Andric 65860b57cec5SDimitry Andric bool IsMSVC2015Compatible = MSVT.getMajor() >= 19; 6587fe6060f1SDimitry Andric if (ImplyVCPPCVer) { 6588fe6060f1SDimitry Andric StringRef LanguageStandard; 6589fe6060f1SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 6590fe6060f1SDimitry Andric Std = StdArg; 6591fe6060f1SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 6592fe6060f1SDimitry Andric .Case("c11", "-std=c11") 6593fe6060f1SDimitry Andric .Case("c17", "-std=c17") 6594fe6060f1SDimitry Andric .Default(""); 6595fe6060f1SDimitry Andric if (LanguageStandard.empty()) 6596fe6060f1SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 6597fe6060f1SDimitry Andric << StdArg->getAsString(Args); 6598fe6060f1SDimitry Andric } 6599fe6060f1SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 6600fe6060f1SDimitry Andric } 66010b57cec5SDimitry Andric if (ImplyVCPPCXXVer) { 66020b57cec5SDimitry Andric StringRef LanguageStandard; 66030b57cec5SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 66040b57cec5SDimitry Andric Std = StdArg; 66050b57cec5SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 66060b57cec5SDimitry Andric .Case("c++14", "-std=c++14") 66070b57cec5SDimitry Andric .Case("c++17", "-std=c++17") 6608fe6060f1SDimitry Andric .Case("c++20", "-std=c++20") 6609fe6060f1SDimitry Andric .Case("c++latest", "-std=c++2b") 66100b57cec5SDimitry Andric .Default(""); 66110b57cec5SDimitry Andric if (LanguageStandard.empty()) 66120b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 66130b57cec5SDimitry Andric << StdArg->getAsString(Args); 66140b57cec5SDimitry Andric } 66150b57cec5SDimitry Andric 66160b57cec5SDimitry Andric if (LanguageStandard.empty()) { 66170b57cec5SDimitry Andric if (IsMSVC2015Compatible) 66180b57cec5SDimitry Andric LanguageStandard = "-std=c++14"; 66190b57cec5SDimitry Andric else 66200b57cec5SDimitry Andric LanguageStandard = "-std=c++11"; 66210b57cec5SDimitry Andric } 66220b57cec5SDimitry Andric 66230b57cec5SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 66240b57cec5SDimitry Andric } 66250b57cec5SDimitry Andric 662681ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions, 662781ad6265SDimitry Andric options::OPT_fno_borland_extensions); 66280b57cec5SDimitry Andric 662981ad6265SDimitry Andric // -fno-declspec is default, except for PS4/PS5. 66300b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec, 663181ad6265SDimitry Andric RawTriple.isPS())) 66320b57cec5SDimitry Andric CmdArgs.push_back("-fdeclspec"); 66330b57cec5SDimitry Andric else if (Args.hasArg(options::OPT_fno_declspec)) 66340b57cec5SDimitry Andric CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec. 66350b57cec5SDimitry Andric 66360b57cec5SDimitry Andric // -fthreadsafe-static is default, except for MSVC compatibility versions less 66370b57cec5SDimitry Andric // than 19. 66380b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 66390b57cec5SDimitry Andric options::OPT_fno_threadsafe_statics, 6640349cc55cSDimitry Andric !types::isOpenCL(InputType) && 6641349cc55cSDimitry Andric (!IsWindowsMSVC || IsMSVC2015Compatible))) 66420b57cec5SDimitry Andric CmdArgs.push_back("-fno-threadsafe-statics"); 66430b57cec5SDimitry Andric 66440b57cec5SDimitry Andric // -fno-delayed-template-parsing is default, except when targeting MSVC. 66450b57cec5SDimitry Andric // Many old Windows SDK versions require this to parse. 66460b57cec5SDimitry Andric // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their 66470b57cec5SDimitry Andric // compiler. We should be able to disable this by default at some point. 66480b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 66490b57cec5SDimitry Andric options::OPT_fno_delayed_template_parsing, IsWindowsMSVC)) 66500b57cec5SDimitry Andric CmdArgs.push_back("-fdelayed-template-parsing"); 66510b57cec5SDimitry Andric 66520b57cec5SDimitry Andric // -fgnu-keywords default varies depending on language; only pass if 66530b57cec5SDimitry Andric // specified. 66540b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords, 66550b57cec5SDimitry Andric options::OPT_fno_gnu_keywords); 66560b57cec5SDimitry Andric 665781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fgnu89_inline, 665881ad6265SDimitry Andric options::OPT_fno_gnu89_inline); 66590b57cec5SDimitry Andric 666081ad6265SDimitry Andric const Arg *InlineArg = Args.getLastArg(options::OPT_finline_functions, 66610b57cec5SDimitry Andric options::OPT_finline_hint_functions, 66620b57cec5SDimitry Andric options::OPT_fno_inline_functions); 666381ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_finline, options::OPT_fno_inline)) { 666481ad6265SDimitry Andric if (A->getOption().matches(options::OPT_fno_inline)) 666581ad6265SDimitry Andric A->render(Args, CmdArgs); 666681ad6265SDimitry Andric } else if (InlineArg) { 666781ad6265SDimitry Andric InlineArg->render(Args, CmdArgs); 666881ad6265SDimitry Andric } 66690b57cec5SDimitry Andric 6670bdd1243dSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finline_max_stacksize_EQ); 6671bdd1243dSDimitry Andric 66720b57cec5SDimitry Andric bool HaveModules = 6673bdd1243dSDimitry Andric RenderModulesOptions(C, D, Args, Input, Output, Std, CmdArgs); 66740b57cec5SDimitry Andric 6675a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fpch_validate_input_files_content, 6676a7dea167SDimitry Andric options::OPT_fno_pch_validate_input_files_content, false)) 6677a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 66785ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 66795ffd83dbSDimitry Andric options::OPT_fno_pch_instantiate_templates, false)) 66805ffd83dbSDimitry Andric CmdArgs.push_back("-fpch-instantiate-templates"); 6681590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen, 6682590d96feSDimitry Andric false)) 6683590d96feSDimitry Andric CmdArgs.push_back("-fmodules-codegen"); 6684590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo, 6685590d96feSDimitry Andric false)) 6686590d96feSDimitry Andric CmdArgs.push_back("-fmodules-debuginfo"); 6687a7dea167SDimitry Andric 66885ffd83dbSDimitry Andric ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); 66890b57cec5SDimitry Andric RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, 66900b57cec5SDimitry Andric Input, CmdArgs); 66910b57cec5SDimitry Andric 6692fe6060f1SDimitry Andric if (types::isObjC(Input.getType()) && 6693fe6060f1SDimitry Andric Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec, 6694fe6060f1SDimitry Andric options::OPT_fno_objc_encode_cxx_class_template_spec, 6695fe6060f1SDimitry Andric !Runtime.isNeXTFamily())) 6696fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec"); 6697fe6060f1SDimitry Andric 66980b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fapplication_extension, 66990b57cec5SDimitry Andric options::OPT_fno_application_extension, false)) 67000b57cec5SDimitry Andric CmdArgs.push_back("-fapplication-extension"); 67010b57cec5SDimitry Andric 67020b57cec5SDimitry Andric // Handle GCC-style exception args. 6703fe6060f1SDimitry Andric bool EH = false; 67040b57cec5SDimitry Andric if (!C.getDriver().IsCLMode()) 6705fe6060f1SDimitry Andric EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); 67060b57cec5SDimitry Andric 67070b57cec5SDimitry Andric // Handle exception personalities 6708a7dea167SDimitry Andric Arg *A = Args.getLastArg( 6709a7dea167SDimitry Andric options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions, 6710a7dea167SDimitry Andric options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions); 67110b57cec5SDimitry Andric if (A) { 67120b57cec5SDimitry Andric const Option &Opt = A->getOption(); 67130b57cec5SDimitry Andric if (Opt.matches(options::OPT_fsjlj_exceptions)) 6714e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 67150b57cec5SDimitry Andric if (Opt.matches(options::OPT_fseh_exceptions)) 6716e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 67170b57cec5SDimitry Andric if (Opt.matches(options::OPT_fdwarf_exceptions)) 6718e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 6719a7dea167SDimitry Andric if (Opt.matches(options::OPT_fwasm_exceptions)) 6720e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=wasm"); 67210b57cec5SDimitry Andric } else { 67220b57cec5SDimitry Andric switch (TC.GetExceptionModel(Args)) { 67230b57cec5SDimitry Andric default: 67240b57cec5SDimitry Andric break; 67250b57cec5SDimitry Andric case llvm::ExceptionHandling::DwarfCFI: 6726e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 67270b57cec5SDimitry Andric break; 67280b57cec5SDimitry Andric case llvm::ExceptionHandling::SjLj: 6729e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 67300b57cec5SDimitry Andric break; 67310b57cec5SDimitry Andric case llvm::ExceptionHandling::WinEH: 6732e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 67330b57cec5SDimitry Andric break; 67340b57cec5SDimitry Andric } 67350b57cec5SDimitry Andric } 67360b57cec5SDimitry Andric 67370b57cec5SDimitry Andric // C++ "sane" operator new. 673881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 673981ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 67400b57cec5SDimitry Andric 67410b57cec5SDimitry Andric // -frelaxed-template-template-args is off by default, as it is a severe 67420b57cec5SDimitry Andric // breaking change until a corresponding change to template partial ordering 67430b57cec5SDimitry Andric // is provided. 674481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_frelaxed_template_template_args, 674581ad6265SDimitry Andric options::OPT_fno_relaxed_template_template_args); 67460b57cec5SDimitry Andric 67470b57cec5SDimitry Andric // -fsized-deallocation is off by default, as it is an ABI-breaking change for 67480b57cec5SDimitry Andric // most platforms. 674981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsized_deallocation, 675081ad6265SDimitry Andric options::OPT_fno_sized_deallocation); 67510b57cec5SDimitry Andric 67520b57cec5SDimitry Andric // -faligned-allocation is on by default in C++17 onwards and otherwise off 67530b57cec5SDimitry Andric // by default. 67540b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation, 67550b57cec5SDimitry Andric options::OPT_fno_aligned_allocation, 67560b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) { 67570b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_aligned_allocation)) 67580b57cec5SDimitry Andric CmdArgs.push_back("-fno-aligned-allocation"); 67590b57cec5SDimitry Andric else 67600b57cec5SDimitry Andric CmdArgs.push_back("-faligned-allocation"); 67610b57cec5SDimitry Andric } 67620b57cec5SDimitry Andric 67630b57cec5SDimitry Andric // The default new alignment can be specified using a dedicated option or via 67640b57cec5SDimitry Andric // a GCC-compatible option that also turns on aligned allocation. 67650b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ, 67660b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) 67670b57cec5SDimitry Andric CmdArgs.push_back( 67680b57cec5SDimitry Andric Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue())); 67690b57cec5SDimitry Andric 67700b57cec5SDimitry Andric // -fconstant-cfstrings is default, and may be subject to argument translation 67710b57cec5SDimitry Andric // on Darwin. 67720b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 677381ad6265SDimitry Andric options::OPT_fno_constant_cfstrings, true) || 67740b57cec5SDimitry Andric !Args.hasFlag(options::OPT_mconstant_cfstrings, 677581ad6265SDimitry Andric options::OPT_mno_constant_cfstrings, true)) 67760b57cec5SDimitry Andric CmdArgs.push_back("-fno-constant-cfstrings"); 67770b57cec5SDimitry Andric 677881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fpascal_strings, 677981ad6265SDimitry Andric options::OPT_fno_pascal_strings); 67800b57cec5SDimitry Andric 67810b57cec5SDimitry Andric // Honor -fpack-struct= and -fpack-struct, if given. Note that 67820b57cec5SDimitry Andric // -fno-pack-struct doesn't apply to -fpack-struct=. 67830b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 67840b57cec5SDimitry Andric std::string PackStructStr = "-fpack-struct="; 67850b57cec5SDimitry Andric PackStructStr += A->getValue(); 67860b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 67870b57cec5SDimitry Andric } else if (Args.hasFlag(options::OPT_fpack_struct, 67880b57cec5SDimitry Andric options::OPT_fno_pack_struct, false)) { 67890b57cec5SDimitry Andric CmdArgs.push_back("-fpack-struct=1"); 67900b57cec5SDimitry Andric } 67910b57cec5SDimitry Andric 67920b57cec5SDimitry Andric // Handle -fmax-type-align=N and -fno-type-align 67930b57cec5SDimitry Andric bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align); 67940b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) { 67950b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 67960b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align="; 67970b57cec5SDimitry Andric MaxTypeAlignStr += A->getValue(); 67980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 67990b57cec5SDimitry Andric } 68000b57cec5SDimitry Andric } else if (RawTriple.isOSDarwin()) { 68010b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 68020b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align=16"; 68030b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 68040b57cec5SDimitry Andric } 68050b57cec5SDimitry Andric } 68060b57cec5SDimitry Andric 68070b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true)) 68080b57cec5SDimitry Andric CmdArgs.push_back("-Qn"); 68090b57cec5SDimitry Andric 68105ffd83dbSDimitry Andric // -fno-common is the default, set -fcommon only when that flag is set. 681181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcommon, options::OPT_fno_common); 68120b57cec5SDimitry Andric 68130b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't yet support 68140b57cec5SDimitry Andric // -funsigned-bitfields. 68150b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fsigned_bitfields, 681681ad6265SDimitry Andric options::OPT_funsigned_bitfields, true)) 68170b57cec5SDimitry Andric D.Diag(diag::warn_drv_clang_unsupported) 68180b57cec5SDimitry Andric << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 68190b57cec5SDimitry Andric 68200b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 682181ad6265SDimitry Andric if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope, true)) 68220b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) 68230b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 68240b57cec5SDimitry Andric 68250b57cec5SDimitry Andric // -finput_charset=UTF-8 is default. Reject others 68260b57cec5SDimitry Andric if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) { 68270b57cec5SDimitry Andric StringRef value = inputCharset->getValue(); 6828fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 68290b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) 68300b57cec5SDimitry Andric << value; 68310b57cec5SDimitry Andric } 68320b57cec5SDimitry Andric 68330b57cec5SDimitry Andric // -fexec_charset=UTF-8 is default. Reject others 68340b57cec5SDimitry Andric if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) { 68350b57cec5SDimitry Andric StringRef value = execCharset->getValue(); 6836fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 68370b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) 68380b57cec5SDimitry Andric << value; 68390b57cec5SDimitry Andric } 68400b57cec5SDimitry Andric 68410b57cec5SDimitry Andric RenderDiagnosticsOptions(D, Args, CmdArgs); 68420b57cec5SDimitry Andric 684381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fasm_blocks, 684481ad6265SDimitry Andric options::OPT_fno_asm_blocks); 68450b57cec5SDimitry Andric 6846bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fgnu_inline_asm, 6847bdd1243dSDimitry Andric options::OPT_fno_gnu_inline_asm); 68480b57cec5SDimitry Andric 68490b57cec5SDimitry Andric // Enable vectorization per default according to the optimization level 68500b57cec5SDimitry Andric // selected. For optimization levels that want vectorization we use the alias 68510b57cec5SDimitry Andric // option to simplify the hasFlag logic. 68520b57cec5SDimitry Andric bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false); 68530b57cec5SDimitry Andric OptSpecifier VectorizeAliasOption = 68540b57cec5SDimitry Andric EnableVec ? options::OPT_O_Group : options::OPT_fvectorize; 68550b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 68560b57cec5SDimitry Andric options::OPT_fno_vectorize, EnableVec)) 68570b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-loops"); 68580b57cec5SDimitry Andric 68590b57cec5SDimitry Andric // -fslp-vectorize is enabled based on the optimization level selected. 68600b57cec5SDimitry Andric bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true); 68610b57cec5SDimitry Andric OptSpecifier SLPVectAliasOption = 68620b57cec5SDimitry Andric EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize; 68630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption, 68640b57cec5SDimitry Andric options::OPT_fno_slp_vectorize, EnableSLPVec)) 68650b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-slp"); 68660b57cec5SDimitry Andric 68670b57cec5SDimitry Andric ParseMPreferVectorWidth(D, Args, CmdArgs); 68680b57cec5SDimitry Andric 68690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ); 68700b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 68710b57cec5SDimitry Andric options::OPT_fsanitize_undefined_strip_path_components_EQ); 68720b57cec5SDimitry Andric 68730b57cec5SDimitry Andric // -fdollars-in-identifiers default varies depending on platform and 68740b57cec5SDimitry Andric // language; only pass if specified. 68750b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 68760b57cec5SDimitry Andric options::OPT_fno_dollars_in_identifiers)) { 68770b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 68780b57cec5SDimitry Andric CmdArgs.push_back("-fdollars-in-identifiers"); 68790b57cec5SDimitry Andric else 68800b57cec5SDimitry Andric CmdArgs.push_back("-fno-dollars-in-identifiers"); 68810b57cec5SDimitry Andric } 68820b57cec5SDimitry Andric 688381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fapple_pragma_pack, 688481ad6265SDimitry Andric options::OPT_fno_apple_pragma_pack); 68850b57cec5SDimitry Andric 6886e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fxl_pragma_pack, 6887e8d8bef9SDimitry Andric options::OPT_fno_xl_pragma_pack, RawTriple.isOSAIX())) 6888e8d8bef9SDimitry Andric CmdArgs.push_back("-fxl-pragma-pack"); 6889e8d8bef9SDimitry Andric 68900b57cec5SDimitry Andric // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. 6891480093f4SDimitry Andric if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple)) 6892480093f4SDimitry Andric renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA); 68930b57cec5SDimitry Andric 68940b57cec5SDimitry Andric bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports, 68950b57cec5SDimitry Andric options::OPT_fno_rewrite_imports, false); 68960b57cec5SDimitry Andric if (RewriteImports) 68970b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-imports"); 68980b57cec5SDimitry Andric 6899bdd1243dSDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdirectives_only, 6900bdd1243dSDimitry Andric options::OPT_fno_directives_only); 690181ad6265SDimitry Andric 69020b57cec5SDimitry Andric // Enable rewrite includes if the user's asked for it or if we're generating 69030b57cec5SDimitry Andric // diagnostics. 69040b57cec5SDimitry Andric // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be 69050b57cec5SDimitry Andric // nice to enable this when doing a crashdump for modules as well. 69060b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_frewrite_includes, 69070b57cec5SDimitry Andric options::OPT_fno_rewrite_includes, false) || 69080b57cec5SDimitry Andric (C.isForDiagnostics() && !HaveModules)) 69090b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-includes"); 69100b57cec5SDimitry Andric 69110b57cec5SDimitry Andric // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 69120b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_traditional, 69130b57cec5SDimitry Andric options::OPT_traditional_cpp)) { 69140b57cec5SDimitry Andric if (isa<PreprocessJobAction>(JA)) 69150b57cec5SDimitry Andric CmdArgs.push_back("-traditional-cpp"); 69160b57cec5SDimitry Andric else 69170b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 69180b57cec5SDimitry Andric } 69190b57cec5SDimitry Andric 69200b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dM); 69210b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dD); 692204eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dI); 69230b57cec5SDimitry Andric 69245ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ); 69255ffd83dbSDimitry Andric 69260b57cec5SDimitry Andric // Handle serialized diagnostics. 69270b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 69280b57cec5SDimitry Andric CmdArgs.push_back("-serialize-diagnostic-file"); 69290b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getValue())); 69300b57cec5SDimitry Andric } 69310b57cec5SDimitry Andric 69320b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 69330b57cec5SDimitry Andric CmdArgs.push_back("-fretain-comments-from-system-headers"); 69340b57cec5SDimitry Andric 69350b57cec5SDimitry Andric // Forward -fcomment-block-commands to -cc1. 69360b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 69370b57cec5SDimitry Andric // Forward -fparse-all-comments to -cc1. 69380b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 69390b57cec5SDimitry Andric 69400b57cec5SDimitry Andric // Turn -fplugin=name.so into -load name.so 69410b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) { 69420b57cec5SDimitry Andric CmdArgs.push_back("-load"); 69430b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 69440b57cec5SDimitry Andric A->claim(); 69450b57cec5SDimitry Andric } 69460b57cec5SDimitry Andric 69474824e7fdSDimitry Andric // Turn -fplugin-arg-pluginname-key=value into 69484824e7fdSDimitry Andric // -plugin-arg-pluginname key=value 69494824e7fdSDimitry Andric // GCC has an actual plugin_argument struct with key/value pairs that it 69504824e7fdSDimitry Andric // passes to its plugins, but we don't, so just pass it on as-is. 69514824e7fdSDimitry Andric // 69524824e7fdSDimitry Andric // The syntax for -fplugin-arg- is ambiguous if both plugin name and 69534824e7fdSDimitry Andric // argument key are allowed to contain dashes. GCC therefore only 69544824e7fdSDimitry Andric // allows dashes in the key. We do the same. 69554824e7fdSDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_arg)) { 69564824e7fdSDimitry Andric auto ArgValue = StringRef(A->getValue()); 69574824e7fdSDimitry Andric auto FirstDashIndex = ArgValue.find('-'); 69584824e7fdSDimitry Andric StringRef PluginName = ArgValue.substr(0, FirstDashIndex); 69594824e7fdSDimitry Andric StringRef Arg = ArgValue.substr(FirstDashIndex + 1); 69604824e7fdSDimitry Andric 69614824e7fdSDimitry Andric A->claim(); 69624824e7fdSDimitry Andric if (FirstDashIndex == StringRef::npos || Arg.empty()) { 69634824e7fdSDimitry Andric if (PluginName.empty()) { 69644824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_name) << A->getAsString(Args); 69654824e7fdSDimitry Andric } else { 69664824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_arg) 69674824e7fdSDimitry Andric << PluginName << A->getAsString(Args); 69684824e7fdSDimitry Andric } 69694824e7fdSDimitry Andric continue; 69704824e7fdSDimitry Andric } 69714824e7fdSDimitry Andric 69724824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-arg-") + PluginName)); 69734824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Arg)); 69744824e7fdSDimitry Andric } 69754824e7fdSDimitry Andric 69760b57cec5SDimitry Andric // Forward -fpass-plugin=name.so to -cc1. 69770b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { 69780b57cec5SDimitry Andric CmdArgs.push_back( 69790b57cec5SDimitry Andric Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); 69800b57cec5SDimitry Andric A->claim(); 69810b57cec5SDimitry Andric } 69820b57cec5SDimitry Andric 69830b57cec5SDimitry Andric // Setup statistics file output. 69840b57cec5SDimitry Andric SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); 69850b57cec5SDimitry Andric if (!StatsFile.empty()) 69860b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile)); 69870b57cec5SDimitry Andric 69880b57cec5SDimitry Andric // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 69890b57cec5SDimitry Andric // parser. 6990bdd1243dSDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) { 6991bdd1243dSDimitry Andric Arg->claim(); 69920b57cec5SDimitry Andric // -finclude-default-header flag is for preprocessor, 69930b57cec5SDimitry Andric // do not pass it to other cc1 commands when save-temps is enabled 69940b57cec5SDimitry Andric if (C.getDriver().isSaveTempsEnabled() && 69950b57cec5SDimitry Andric !isa<PreprocessJobAction>(JA)) { 6996bdd1243dSDimitry Andric if (StringRef(Arg->getValue()) == "-finclude-default-header") 6997bdd1243dSDimitry Andric continue; 6998bdd1243dSDimitry Andric } 69990b57cec5SDimitry Andric CmdArgs.push_back(Arg->getValue()); 70000b57cec5SDimitry Andric } 70010b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 70020b57cec5SDimitry Andric A->claim(); 70030b57cec5SDimitry Andric 70040b57cec5SDimitry Andric // We translate this by hand to the -cc1 argument, since nightly test uses 70050b57cec5SDimitry Andric // it and developers have been trained to spell it with -mllvm. Both 70060b57cec5SDimitry Andric // spellings are now deprecated and should be removed. 70070b57cec5SDimitry Andric if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") { 70080b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-optzns"); 70090b57cec5SDimitry Andric } else { 70100b57cec5SDimitry Andric A->render(Args, CmdArgs); 70110b57cec5SDimitry Andric } 70120b57cec5SDimitry Andric } 70130b57cec5SDimitry Andric 70140b57cec5SDimitry Andric // With -save-temps, we want to save the unoptimized bitcode output from the 70150b57cec5SDimitry Andric // CompileJobAction, use -disable-llvm-passes to get pristine IR generated 70160b57cec5SDimitry Andric // by the frontend. 70170b57cec5SDimitry Andric // When -fembed-bitcode is enabled, optimized bitcode is emitted because it 70180b57cec5SDimitry Andric // has slightly different breakdown between stages. 70190b57cec5SDimitry Andric // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of 70200b57cec5SDimitry Andric // pristine IR generated by the frontend. Ideally, a new compile action should 70210b57cec5SDimitry Andric // be added so both IR can be captured. 70225ffd83dbSDimitry Andric if ((C.getDriver().isSaveTempsEnabled() || 70235ffd83dbSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP)) && 7024fe6060f1SDimitry Andric !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) && 70250b57cec5SDimitry Andric isa<CompileJobAction>(JA)) 70260b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 70270b57cec5SDimitry Andric 70280b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_undef); 70290b57cec5SDimitry Andric 70300b57cec5SDimitry Andric const char *Exec = D.getClangProgramPath(); 70310b57cec5SDimitry Andric 70320b57cec5SDimitry Andric // Optionally embed the -cc1 level arguments into the debug info or a 70330b57cec5SDimitry Andric // section, for build analysis. 70340b57cec5SDimitry Andric // Also record command line arguments into the debug info if 70350b57cec5SDimitry Andric // -grecord-gcc-switches options is set on. 70360b57cec5SDimitry Andric // By default, -gno-record-gcc-switches is set on and no recording. 70370b57cec5SDimitry Andric auto GRecordSwitches = 70380b57cec5SDimitry Andric Args.hasFlag(options::OPT_grecord_command_line, 70390b57cec5SDimitry Andric options::OPT_gno_record_command_line, false); 70400b57cec5SDimitry Andric auto FRecordSwitches = 70410b57cec5SDimitry Andric Args.hasFlag(options::OPT_frecord_command_line, 70420b57cec5SDimitry Andric options::OPT_fno_record_command_line, false); 70430b57cec5SDimitry Andric if (FRecordSwitches && !Triple.isOSBinFormatELF()) 70440b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 70450b57cec5SDimitry Andric << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args) 70460b57cec5SDimitry Andric << TripleStr; 70470b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) { 70480b57cec5SDimitry Andric ArgStringList OriginalArgs; 70490b57cec5SDimitry Andric for (const auto &Arg : Args) 70500b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 70510b57cec5SDimitry Andric 70520b57cec5SDimitry Andric SmallString<256> Flags; 70535ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 70540b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 70550b57cec5SDimitry Andric SmallString<128> EscapedArg; 70560b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 70570b57cec5SDimitry Andric Flags += " "; 70580b57cec5SDimitry Andric Flags += EscapedArg; 70590b57cec5SDimitry Andric } 70600b57cec5SDimitry Andric auto FlagsArgString = Args.MakeArgString(Flags); 70610b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches) { 70620b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 70630b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 70640b57cec5SDimitry Andric } 70650b57cec5SDimitry Andric if (FRecordSwitches) { 70660b57cec5SDimitry Andric CmdArgs.push_back("-record-command-line"); 70670b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 70680b57cec5SDimitry Andric } 70690b57cec5SDimitry Andric } 70700b57cec5SDimitry Andric 7071753f127fSDimitry Andric // Host-side offloading compilation receives all device-side outputs. Include 7072753f127fSDimitry Andric // them in the host compilation depending on the target. If the host inputs 7073753f127fSDimitry Andric // are not empty we use the new-driver scheme, otherwise use the old scheme. 70740b57cec5SDimitry Andric if ((IsCuda || IsHIP) && CudaDeviceInput) { 70750b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 70760b57cec5SDimitry Andric CmdArgs.push_back(CudaDeviceInput->getFilename()); 7077753f127fSDimitry Andric } else if (!HostOffloadingInputs.empty()) { 7078fcaf7f86SDimitry Andric if ((IsCuda || IsHIP) && !IsRDCMode) { 7079753f127fSDimitry Andric assert(HostOffloadingInputs.size() == 1 && "Only one input expected"); 7080753f127fSDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 7081753f127fSDimitry Andric CmdArgs.push_back(HostOffloadingInputs.front().getFilename()); 7082753f127fSDimitry Andric } else { 7083753f127fSDimitry Andric for (const InputInfo Input : HostOffloadingInputs) 7084753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fembed-offload-object=" + 7085753f127fSDimitry Andric TC.getInputFilename(Input))); 7086753f127fSDimitry Andric } 70870b57cec5SDimitry Andric } 70880b57cec5SDimitry Andric 70890b57cec5SDimitry Andric if (IsCuda) { 70900b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcuda_short_ptr, 70910b57cec5SDimitry Andric options::OPT_fno_cuda_short_ptr, false)) 70920b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-short-ptr"); 70930b57cec5SDimitry Andric } 70940b57cec5SDimitry Andric 7095fe6060f1SDimitry Andric if (IsCuda || IsHIP) { 7096fe6060f1SDimitry Andric // Determine the original source input. 7097fe6060f1SDimitry Andric const Action *SourceAction = &JA; 7098fe6060f1SDimitry Andric while (SourceAction->getKind() != Action::InputClass) { 7099fe6060f1SDimitry Andric assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 7100fe6060f1SDimitry Andric SourceAction = SourceAction->getInputs()[0]; 7101fe6060f1SDimitry Andric } 7102fe6060f1SDimitry Andric auto CUID = cast<InputAction>(SourceAction)->getId(); 7103fe6060f1SDimitry Andric if (!CUID.empty()) 7104fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); 7105fe6060f1SDimitry Andric } 7106fe6060f1SDimitry Andric 710781ad6265SDimitry Andric if (IsHIP) { 7108480093f4SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 710981ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgpu_default_stream_EQ); 711081ad6265SDimitry Andric } 7111480093f4SDimitry Andric 7112fe6060f1SDimitry Andric if (IsCudaDevice || IsHIPDevice) { 7113fe6060f1SDimitry Andric StringRef InlineThresh = 7114fe6060f1SDimitry Andric Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ); 7115fe6060f1SDimitry Andric if (!InlineThresh.empty()) { 7116fe6060f1SDimitry Andric std::string ArgStr = 7117fe6060f1SDimitry Andric std::string("-inline-threshold=") + InlineThresh.str(); 7118fe6060f1SDimitry Andric CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)}); 7119fe6060f1SDimitry Andric } 7120fe6060f1SDimitry Andric } 7121fe6060f1SDimitry Andric 71220b57cec5SDimitry Andric // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path 71230b57cec5SDimitry Andric // to specify the result of the compile phase on the host, so the meaningful 71240b57cec5SDimitry Andric // device declarations can be identified. Also, -fopenmp-is-device is passed 71250b57cec5SDimitry Andric // along to tell the frontend that it is generating code for a device, so that 71260b57cec5SDimitry Andric // only the relevant declarations are emitted. 71270b57cec5SDimitry Andric if (IsOpenMPDevice) { 71280b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-is-device"); 71290b57cec5SDimitry Andric if (OpenMPDeviceInput) { 71300b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-host-ir-file-path"); 71310b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename())); 71320b57cec5SDimitry Andric } 71330b57cec5SDimitry Andric } 71340b57cec5SDimitry Andric 7135e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) { 7136e8d8bef9SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); 7137e8d8bef9SDimitry Andric 713881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_munsafe_fp_atomics, 713981ad6265SDimitry Andric options::OPT_mno_unsafe_fp_atomics); 7140e8d8bef9SDimitry Andric } 7141e8d8bef9SDimitry Andric 71420b57cec5SDimitry Andric // For all the host OpenMP offloading compile jobs we need to pass the targets 71430b57cec5SDimitry Andric // information using -fopenmp-targets= option. 71440b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 714581ad6265SDimitry Andric SmallString<128> Targets("-fopenmp-targets="); 71460b57cec5SDimitry Andric 714781ad6265SDimitry Andric SmallVector<std::string, 4> Triples; 714881ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); 714981ad6265SDimitry Andric std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples), 715081ad6265SDimitry Andric [](auto TC) { return TC.second->getTripleString(); }); 715181ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ","))); 71520b57cec5SDimitry Andric } 71530b57cec5SDimitry Andric 7154a7dea167SDimitry Andric bool VirtualFunctionElimination = 7155a7dea167SDimitry Andric Args.hasFlag(options::OPT_fvirtual_function_elimination, 7156a7dea167SDimitry Andric options::OPT_fno_virtual_function_elimination, false); 7157a7dea167SDimitry Andric if (VirtualFunctionElimination) { 7158a7dea167SDimitry Andric // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO 7159a7dea167SDimitry Andric // in the future). 7160fe6060f1SDimitry Andric if (LTOMode != LTOK_Full) 7161a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 7162a7dea167SDimitry Andric << "-fvirtual-function-elimination" 7163a7dea167SDimitry Andric << "-flto=full"; 7164a7dea167SDimitry Andric 7165a7dea167SDimitry Andric CmdArgs.push_back("-fvirtual-function-elimination"); 7166a7dea167SDimitry Andric } 7167a7dea167SDimitry Andric 7168a7dea167SDimitry Andric // VFE requires whole-program-vtables, and enables it by default. 7169a7dea167SDimitry Andric bool WholeProgramVTables = Args.hasFlag( 7170a7dea167SDimitry Andric options::OPT_fwhole_program_vtables, 7171a7dea167SDimitry Andric options::OPT_fno_whole_program_vtables, VirtualFunctionElimination); 7172a7dea167SDimitry Andric if (VirtualFunctionElimination && !WholeProgramVTables) { 7173a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7174a7dea167SDimitry Andric << "-fno-whole-program-vtables" 7175a7dea167SDimitry Andric << "-fvirtual-function-elimination"; 7176a7dea167SDimitry Andric } 7177a7dea167SDimitry Andric 71780b57cec5SDimitry Andric if (WholeProgramVTables) { 7179fe6060f1SDimitry Andric // Propagate -fwhole-program-vtables if this is an LTO compile. 7180fe6060f1SDimitry Andric if (IsUsingLTO) 7181fe6060f1SDimitry Andric CmdArgs.push_back("-fwhole-program-vtables"); 7182fe6060f1SDimitry Andric // Check if we passed LTO options but they were suppressed because this is a 7183fe6060f1SDimitry Andric // device offloading action, or we passed device offload LTO options which 7184fe6060f1SDimitry Andric // were suppressed because this is not the device offload action. 7185fe6060f1SDimitry Andric // Otherwise, issue an error. 7186fe6060f1SDimitry Andric else if (!D.isUsingLTO(!IsDeviceOffloadAction)) 71870b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 71880b57cec5SDimitry Andric << "-fwhole-program-vtables" 71890b57cec5SDimitry Andric << "-flto"; 71900b57cec5SDimitry Andric } 71910b57cec5SDimitry Andric 7192480093f4SDimitry Andric bool DefaultsSplitLTOUnit = 7193349cc55cSDimitry Andric (WholeProgramVTables || SanitizeArgs.needsLTO()) && 7194fe6060f1SDimitry Andric (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit()); 71950b57cec5SDimitry Andric bool SplitLTOUnit = 71960b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_lto_unit, 7197a7dea167SDimitry Andric options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); 7198349cc55cSDimitry Andric if (SanitizeArgs.needsLTO() && !SplitLTOUnit) 7199a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" 7200a7dea167SDimitry Andric << "-fsanitize=cfi"; 72010b57cec5SDimitry Andric if (SplitLTOUnit) 72020b57cec5SDimitry Andric CmdArgs.push_back("-fsplit-lto-unit"); 72030b57cec5SDimitry Andric 72045ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, 72055ffd83dbSDimitry Andric options::OPT_fno_global_isel)) { 72060b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 72075ffd83dbSDimitry Andric if (A->getOption().matches(options::OPT_fglobal_isel)) { 72080b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=1"); 72090b57cec5SDimitry Andric 72100b57cec5SDimitry Andric // GISel is on by default on AArch64 -O0, so don't bother adding 72110b57cec5SDimitry Andric // the fallback remarks for it. Other combinations will add a warning of 72120b57cec5SDimitry Andric // some kind. 72130b57cec5SDimitry Andric bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64; 72140b57cec5SDimitry Andric bool IsOptLevelSupported = false; 72150b57cec5SDimitry Andric 72160b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_O_Group); 72170b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::aarch64) { 72180b57cec5SDimitry Andric if (!A || A->getOption().matches(options::OPT_O0)) 72190b57cec5SDimitry Andric IsOptLevelSupported = true; 72200b57cec5SDimitry Andric } 72210b57cec5SDimitry Andric if (!IsArchSupported || !IsOptLevelSupported) { 72220b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 72230b57cec5SDimitry Andric CmdArgs.push_back("-global-isel-abort=2"); 72240b57cec5SDimitry Andric 72250b57cec5SDimitry Andric if (!IsArchSupported) 72265ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete) << Triple.getArchName(); 72270b57cec5SDimitry Andric else 72285ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete_opt); 72290b57cec5SDimitry Andric } 72300b57cec5SDimitry Andric } else { 72310b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=0"); 72320b57cec5SDimitry Andric } 72330b57cec5SDimitry Andric } 72340b57cec5SDimitry Andric 72350b57cec5SDimitry Andric if (Args.hasArg(options::OPT_forder_file_instrumentation)) { 72360b57cec5SDimitry Andric CmdArgs.push_back("-forder-file-instrumentation"); 72370b57cec5SDimitry Andric // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is 72380b57cec5SDimitry Andric // on, we need to pass these flags as linker flags and that will be handled 72390b57cec5SDimitry Andric // outside of the compiler. 7240fe6060f1SDimitry Andric if (!IsUsingLTO) { 72410b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 72420b57cec5SDimitry Andric CmdArgs.push_back("-enable-order-file-instrumentation"); 72430b57cec5SDimitry Andric } 72440b57cec5SDimitry Andric } 72450b57cec5SDimitry Andric 72460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128, 72470b57cec5SDimitry Andric options::OPT_fno_force_enable_int128)) { 72480b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fforce_enable_int128)) 72490b57cec5SDimitry Andric CmdArgs.push_back("-fforce-enable-int128"); 72500b57cec5SDimitry Andric } 72510b57cec5SDimitry Andric 725281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_consts, 725381ad6265SDimitry Andric options::OPT_fno_keep_static_consts); 725481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcomplete_member_pointers, 725581ad6265SDimitry Andric options::OPT_fno_complete_member_pointers); 7256bdd1243dSDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fcxx_static_destructors, 7257bdd1243dSDimitry Andric options::OPT_fno_cxx_static_destructors); 72580b57cec5SDimitry Andric 7259e8d8bef9SDimitry Andric addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false); 7260e8d8bef9SDimitry Andric 7261e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics, 7262e8d8bef9SDimitry Andric options::OPT_mno_outline_atomics)) { 7263e8d8bef9SDimitry Andric // Option -moutline-atomics supported for AArch64 target only. 7264e8d8bef9SDimitry Andric if (!Triple.isAArch64()) { 7265e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt) 72660eae32dcSDimitry Andric << Triple.getArchName() << A->getOption().getName(); 72670b57cec5SDimitry Andric } else { 72680eae32dcSDimitry Andric if (A->getOption().matches(options::OPT_moutline_atomics)) { 7269e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7270e8d8bef9SDimitry Andric CmdArgs.push_back("+outline-atomics"); 72710b57cec5SDimitry Andric } else { 7272e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7273e8d8bef9SDimitry Andric CmdArgs.push_back("-outline-atomics"); 72740b57cec5SDimitry Andric } 72750eae32dcSDimitry Andric } 7276fe6060f1SDimitry Andric } else if (Triple.isAArch64() && 7277fe6060f1SDimitry Andric getToolChain().IsAArch64OutlineAtomicsDefault(Args)) { 7278fe6060f1SDimitry Andric CmdArgs.push_back("-target-feature"); 7279fe6060f1SDimitry Andric CmdArgs.push_back("+outline-atomics"); 72800b57cec5SDimitry Andric } 72810b57cec5SDimitry Andric 7282bdd1243dSDimitry Andric if (Triple.isAArch64() && 7283bdd1243dSDimitry Andric (Args.hasArg(options::OPT_mno_fmv) || 7284bdd1243dSDimitry Andric getToolChain().GetRuntimeLibType(Args) != ToolChain::RLT_CompilerRT)) { 7285bdd1243dSDimitry Andric // Disable Function Multiversioning on AArch64 target. 7286bdd1243dSDimitry Andric CmdArgs.push_back("-target-feature"); 7287bdd1243dSDimitry Andric CmdArgs.push_back("-fmv"); 7288bdd1243dSDimitry Andric } 7289bdd1243dSDimitry Andric 72900b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig, 72910b57cec5SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 72920b57cec5SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 7293e8d8bef9SDimitry Andric !TC.getTriple().isPS4() && !TC.getTriple().isVE() && 72940b57cec5SDimitry Andric !TC.getTriple().isOSNetBSD() && 7295480093f4SDimitry Andric !Distro(D.getVFS(), TC.getTriple()).IsGentoo() && 7296e8d8bef9SDimitry Andric !TC.getTriple().isAndroid() && TC.useIntegratedAs())) 72970b57cec5SDimitry Andric CmdArgs.push_back("-faddrsig"); 72980b57cec5SDimitry Andric 7299fe6060f1SDimitry Andric if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && 7300bdd1243dSDimitry Andric (EH || UnwindTables || AsyncUnwindTables || 7301349cc55cSDimitry Andric DebugInfoKind != codegenoptions::NoDebugInfo)) 7302fe6060f1SDimitry Andric CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); 7303fe6060f1SDimitry Andric 73040b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { 73050b57cec5SDimitry Andric std::string Str = A->getAsString(Args); 73060b57cec5SDimitry Andric if (!TC.getTriple().isOSBinFormatELF()) 73070b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 73080b57cec5SDimitry Andric << Str << TC.getTripleString(); 73090b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str)); 73100b57cec5SDimitry Andric } 73110b57cec5SDimitry Andric 73120eae32dcSDimitry Andric // Add the output path to the object file for CodeView debug infos. 73130eae32dcSDimitry Andric if (EmitCodeView && Output.isFilename()) 73140eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 73150eae32dcSDimitry Andric Output.getFilename()); 73160eae32dcSDimitry Andric 73170b57cec5SDimitry Andric // Add the "-o out -x type src.c" flags last. This is done primarily to make 73180b57cec5SDimitry Andric // the -cc1 command easier to edit when reproducing compiler crashes. 73190b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 73200b57cec5SDimitry Andric // Handled with other dependency code. 73210b57cec5SDimitry Andric } else if (Output.isFilename()) { 7322480093f4SDimitry Andric if (Output.getType() == clang::driver::types::TY_IFS_CPP || 7323480093f4SDimitry Andric Output.getType() == clang::driver::types::TY_IFS) { 7324480093f4SDimitry Andric SmallString<128> OutputFilename(Output.getFilename()); 7325480093f4SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "ifs"); 7326480093f4SDimitry Andric CmdArgs.push_back("-o"); 7327480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 7328480093f4SDimitry Andric } else { 73290b57cec5SDimitry Andric CmdArgs.push_back("-o"); 73300b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 7331480093f4SDimitry Andric } 73320b57cec5SDimitry Andric } else { 73330b57cec5SDimitry Andric assert(Output.isNothing() && "Invalid output."); 73340b57cec5SDimitry Andric } 73350b57cec5SDimitry Andric 73360b57cec5SDimitry Andric addDashXForInput(Args, Input, CmdArgs); 73370b57cec5SDimitry Andric 73380b57cec5SDimitry Andric ArrayRef<InputInfo> FrontendInputs = Input; 7339bdd1243dSDimitry Andric if (IsExtractAPI) 734081ad6265SDimitry Andric FrontendInputs = ExtractAPIInputs; 73410b57cec5SDimitry Andric else if (Input.isNothing()) 73420b57cec5SDimitry Andric FrontendInputs = {}; 73430b57cec5SDimitry Andric 73440b57cec5SDimitry Andric for (const InputInfo &Input : FrontendInputs) { 73450b57cec5SDimitry Andric if (Input.isFilename()) 73460b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 73470b57cec5SDimitry Andric else 73480b57cec5SDimitry Andric Input.getInputArg().renderAsInput(Args, CmdArgs); 73490b57cec5SDimitry Andric } 73500b57cec5SDimitry Andric 7351fe6060f1SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 7352480093f4SDimitry Andric // Invoke the CC1 directly in this process 7353e8d8bef9SDimitry Andric C.addCommand(std::make_unique<CC1Command>(JA, *this, 7354e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7355e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 73560b57cec5SDimitry Andric } else { 7357e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 7358e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7359e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 73600b57cec5SDimitry Andric } 73610b57cec5SDimitry Andric 73620b57cec5SDimitry Andric // Make the compile command echo its inputs for /showFilenames. 73630b57cec5SDimitry Andric if (Output.getType() == types::TY_Object && 73640b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_showFilenames, 73650b57cec5SDimitry Andric options::OPT__SLASH_showFilenames_, false)) { 736613138422SDimitry Andric C.getJobs().getJobs().back()->PrintInputFilenames = true; 73670b57cec5SDimitry Andric } 73680b57cec5SDimitry Andric 73690b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_pg)) 73705ffd83dbSDimitry Andric if (FPKeepKind == CodeGenOptions::FramePointerKind::None && 73715ffd83dbSDimitry Andric !Args.hasArg(options::OPT_mfentry)) 73720b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer" 73730b57cec5SDimitry Andric << A->getAsString(Args); 73740b57cec5SDimitry Andric 73750b57cec5SDimitry Andric // Claim some arguments which clang supports automatically. 73760b57cec5SDimitry Andric 73770b57cec5SDimitry Andric // -fpch-preprocess is used with gcc to add a special marker in the output to 73780b57cec5SDimitry Andric // include the PCH file. 73790b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fpch_preprocess); 73800b57cec5SDimitry Andric 73810b57cec5SDimitry Andric // Claim some arguments which clang doesn't support, but we don't 73820b57cec5SDimitry Andric // care to warn the user about. 73830b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 73840b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 73850b57cec5SDimitry Andric 73860b57cec5SDimitry Andric // Disable warnings for clang -E -emit-llvm foo.c 73870b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 73880b57cec5SDimitry Andric } 73890b57cec5SDimitry Andric 73900eae32dcSDimitry Andric Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend) 73910b57cec5SDimitry Andric // CAUTION! The first constructor argument ("clang") is not arbitrary, 73920b57cec5SDimitry Andric // as it is for other tools. Some operations on a Tool actually test 73930b57cec5SDimitry Andric // whether that tool is Clang based on the Tool's Name as a string. 73940eae32dcSDimitry Andric : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {} 73950b57cec5SDimitry Andric 73960b57cec5SDimitry Andric Clang::~Clang() {} 73970b57cec5SDimitry Andric 73980b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI. 73990b57cec5SDimitry Andric /// 74000b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile. 74010b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 74025ffd83dbSDimitry Andric const InputInfoList &inputs, 74030b57cec5SDimitry Andric ArgStringList &cmdArgs, 74040b57cec5SDimitry Andric RewriteKind rewriteKind) const { 74050b57cec5SDimitry Andric // Look for the controlling runtime option. 74060b57cec5SDimitry Andric Arg *runtimeArg = 74070b57cec5SDimitry Andric args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime, 74080b57cec5SDimitry Andric options::OPT_fobjc_runtime_EQ); 74090b57cec5SDimitry Andric 74100b57cec5SDimitry Andric // Just forward -fobjc-runtime= to the frontend. This supercedes 74110b57cec5SDimitry Andric // options about fragility. 74120b57cec5SDimitry Andric if (runtimeArg && 74130b57cec5SDimitry Andric runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 74140b57cec5SDimitry Andric ObjCRuntime runtime; 74150b57cec5SDimitry Andric StringRef value = runtimeArg->getValue(); 74160b57cec5SDimitry Andric if (runtime.tryParse(value)) { 74170b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 74180b57cec5SDimitry Andric << value; 74190b57cec5SDimitry Andric } 74200b57cec5SDimitry Andric if ((runtime.getKind() == ObjCRuntime::GNUstep) && 74210b57cec5SDimitry Andric (runtime.getVersion() >= VersionTuple(2, 0))) 74220b57cec5SDimitry Andric if (!getToolChain().getTriple().isOSBinFormatELF() && 74230b57cec5SDimitry Andric !getToolChain().getTriple().isOSBinFormatCOFF()) { 74240b57cec5SDimitry Andric getToolChain().getDriver().Diag( 74250b57cec5SDimitry Andric diag::err_drv_gnustep_objc_runtime_incompatible_binary) 74260b57cec5SDimitry Andric << runtime.getVersion().getMajor(); 74270b57cec5SDimitry Andric } 74280b57cec5SDimitry Andric 74290b57cec5SDimitry Andric runtimeArg->render(args, cmdArgs); 74300b57cec5SDimitry Andric return runtime; 74310b57cec5SDimitry Andric } 74320b57cec5SDimitry Andric 74330b57cec5SDimitry Andric // Otherwise, we'll need the ABI "version". Version numbers are 74340b57cec5SDimitry Andric // slightly confusing for historical reasons: 74350b57cec5SDimitry Andric // 1 - Traditional "fragile" ABI 74360b57cec5SDimitry Andric // 2 - Non-fragile ABI, version 1 74370b57cec5SDimitry Andric // 3 - Non-fragile ABI, version 2 74380b57cec5SDimitry Andric unsigned objcABIVersion = 1; 74390b57cec5SDimitry Andric // If -fobjc-abi-version= is present, use that to set the version. 74400b57cec5SDimitry Andric if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 74410b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 74420b57cec5SDimitry Andric if (value == "1") 74430b57cec5SDimitry Andric objcABIVersion = 1; 74440b57cec5SDimitry Andric else if (value == "2") 74450b57cec5SDimitry Andric objcABIVersion = 2; 74460b57cec5SDimitry Andric else if (value == "3") 74470b57cec5SDimitry Andric objcABIVersion = 3; 74480b57cec5SDimitry Andric else 74490b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value; 74500b57cec5SDimitry Andric } else { 74510b57cec5SDimitry Andric // Otherwise, determine if we are using the non-fragile ABI. 74520b57cec5SDimitry Andric bool nonFragileABIIsDefault = 74530b57cec5SDimitry Andric (rewriteKind == RK_NonFragile || 74540b57cec5SDimitry Andric (rewriteKind == RK_None && 74550b57cec5SDimitry Andric getToolChain().IsObjCNonFragileABIDefault())); 74560b57cec5SDimitry Andric if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 74570b57cec5SDimitry Andric options::OPT_fno_objc_nonfragile_abi, 74580b57cec5SDimitry Andric nonFragileABIIsDefault)) { 74590b57cec5SDimitry Andric // Determine the non-fragile ABI version to use. 74600b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 74610b57cec5SDimitry Andric unsigned nonFragileABIVersion = 1; 74620b57cec5SDimitry Andric #else 74630b57cec5SDimitry Andric unsigned nonFragileABIVersion = 2; 74640b57cec5SDimitry Andric #endif 74650b57cec5SDimitry Andric 74660b57cec5SDimitry Andric if (Arg *abiArg = 74670b57cec5SDimitry Andric args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) { 74680b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 74690b57cec5SDimitry Andric if (value == "1") 74700b57cec5SDimitry Andric nonFragileABIVersion = 1; 74710b57cec5SDimitry Andric else if (value == "2") 74720b57cec5SDimitry Andric nonFragileABIVersion = 2; 74730b57cec5SDimitry Andric else 74740b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 74750b57cec5SDimitry Andric << value; 74760b57cec5SDimitry Andric } 74770b57cec5SDimitry Andric 74780b57cec5SDimitry Andric objcABIVersion = 1 + nonFragileABIVersion; 74790b57cec5SDimitry Andric } else { 74800b57cec5SDimitry Andric objcABIVersion = 1; 74810b57cec5SDimitry Andric } 74820b57cec5SDimitry Andric } 74830b57cec5SDimitry Andric 74840b57cec5SDimitry Andric // We don't actually care about the ABI version other than whether 74850b57cec5SDimitry Andric // it's non-fragile. 74860b57cec5SDimitry Andric bool isNonFragile = objcABIVersion != 1; 74870b57cec5SDimitry Andric 74880b57cec5SDimitry Andric // If we have no runtime argument, ask the toolchain for its default runtime. 74890b57cec5SDimitry Andric // However, the rewriter only really supports the Mac runtime, so assume that. 74900b57cec5SDimitry Andric ObjCRuntime runtime; 74910b57cec5SDimitry Andric if (!runtimeArg) { 74920b57cec5SDimitry Andric switch (rewriteKind) { 74930b57cec5SDimitry Andric case RK_None: 74940b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 74950b57cec5SDimitry Andric break; 74960b57cec5SDimitry Andric case RK_Fragile: 74970b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 74980b57cec5SDimitry Andric break; 74990b57cec5SDimitry Andric case RK_NonFragile: 75000b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 75010b57cec5SDimitry Andric break; 75020b57cec5SDimitry Andric } 75030b57cec5SDimitry Andric 75040b57cec5SDimitry Andric // -fnext-runtime 75050b57cec5SDimitry Andric } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 75060b57cec5SDimitry Andric // On Darwin, make this use the default behavior for the toolchain. 75070b57cec5SDimitry Andric if (getToolChain().getTriple().isOSDarwin()) { 75080b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 75090b57cec5SDimitry Andric 75100b57cec5SDimitry Andric // Otherwise, build for a generic macosx port. 75110b57cec5SDimitry Andric } else { 75120b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 75130b57cec5SDimitry Andric } 75140b57cec5SDimitry Andric 75150b57cec5SDimitry Andric // -fgnu-runtime 75160b57cec5SDimitry Andric } else { 75170b57cec5SDimitry Andric assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 75180b57cec5SDimitry Andric // Legacy behaviour is to target the gnustep runtime if we are in 75190b57cec5SDimitry Andric // non-fragile mode or the GCC runtime in fragile mode. 75200b57cec5SDimitry Andric if (isNonFragile) 75210b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0)); 75220b57cec5SDimitry Andric else 75230b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 75240b57cec5SDimitry Andric } 75250b57cec5SDimitry Andric 75265ffd83dbSDimitry Andric if (llvm::any_of(inputs, [](const InputInfo &input) { 75275ffd83dbSDimitry Andric return types::isObjC(input.getType()); 75285ffd83dbSDimitry Andric })) 75290b57cec5SDimitry Andric cmdArgs.push_back( 75300b57cec5SDimitry Andric args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); 75310b57cec5SDimitry Andric return runtime; 75320b57cec5SDimitry Andric } 75330b57cec5SDimitry Andric 75340b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) { 75350b57cec5SDimitry Andric bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-'); 75360b57cec5SDimitry Andric I += HaveDash; 75370b57cec5SDimitry Andric return !HaveDash; 75380b57cec5SDimitry Andric } 75390b57cec5SDimitry Andric 75400b57cec5SDimitry Andric namespace { 75410b57cec5SDimitry Andric struct EHFlags { 75420b57cec5SDimitry Andric bool Synch = false; 75430b57cec5SDimitry Andric bool Asynch = false; 75440b57cec5SDimitry Andric bool NoUnwindC = false; 75450b57cec5SDimitry Andric }; 75460b57cec5SDimitry Andric } // end anonymous namespace 75470b57cec5SDimitry Andric 75480b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are 75490b57cec5SDimitry Andric /// thrown. There are three modifiers: 75500b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions. 75510b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions. 75520b57cec5SDimitry Andric /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR. 75530b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind. 75540b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled. 75550b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { 75560b57cec5SDimitry Andric EHFlags EH; 75570b57cec5SDimitry Andric 75580b57cec5SDimitry Andric std::vector<std::string> EHArgs = 75590b57cec5SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_EH); 75600b57cec5SDimitry Andric for (auto EHVal : EHArgs) { 75610b57cec5SDimitry Andric for (size_t I = 0, E = EHVal.size(); I != E; ++I) { 75620b57cec5SDimitry Andric switch (EHVal[I]) { 75630b57cec5SDimitry Andric case 'a': 75640b57cec5SDimitry Andric EH.Asynch = maybeConsumeDash(EHVal, I); 75650b57cec5SDimitry Andric if (EH.Asynch) 75660b57cec5SDimitry Andric EH.Synch = false; 75670b57cec5SDimitry Andric continue; 75680b57cec5SDimitry Andric case 'c': 75690b57cec5SDimitry Andric EH.NoUnwindC = maybeConsumeDash(EHVal, I); 75700b57cec5SDimitry Andric continue; 75710b57cec5SDimitry Andric case 's': 75720b57cec5SDimitry Andric EH.Synch = maybeConsumeDash(EHVal, I); 75730b57cec5SDimitry Andric if (EH.Synch) 75740b57cec5SDimitry Andric EH.Asynch = false; 75750b57cec5SDimitry Andric continue; 75760b57cec5SDimitry Andric default: 75770b57cec5SDimitry Andric break; 75780b57cec5SDimitry Andric } 75790b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal; 75800b57cec5SDimitry Andric break; 75810b57cec5SDimitry Andric } 75820b57cec5SDimitry Andric } 75830b57cec5SDimitry Andric // The /GX, /GX- flags are only processed if there are not /EH flags. 75840b57cec5SDimitry Andric // The default is that /GX is not specified. 75850b57cec5SDimitry Andric if (EHArgs.empty() && 75860b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_, 75870b57cec5SDimitry Andric /*Default=*/false)) { 75880b57cec5SDimitry Andric EH.Synch = true; 75890b57cec5SDimitry Andric EH.NoUnwindC = true; 75900b57cec5SDimitry Andric } 75910b57cec5SDimitry Andric 759281ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 759381ad6265SDimitry Andric EH.Synch = false; 759481ad6265SDimitry Andric EH.NoUnwindC = false; 759581ad6265SDimitry Andric EH.Asynch = false; 759681ad6265SDimitry Andric } 759781ad6265SDimitry Andric 75980b57cec5SDimitry Andric return EH; 75990b57cec5SDimitry Andric } 76000b57cec5SDimitry Andric 76010b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, 76020b57cec5SDimitry Andric ArgStringList &CmdArgs, 76030b57cec5SDimitry Andric codegenoptions::DebugInfoKind *DebugInfoKind, 76040b57cec5SDimitry Andric bool *EmitCodeView) const { 76055ffd83dbSDimitry Andric bool isNVPTX = getToolChain().getTriple().isNVPTX(); 76060b57cec5SDimitry Andric 7607bdd1243dSDimitry Andric ProcessVSRuntimeLibrary(Args, CmdArgs); 76080b57cec5SDimitry Andric 76095ffd83dbSDimitry Andric if (Arg *ShowIncludes = 76105ffd83dbSDimitry Andric Args.getLastArg(options::OPT__SLASH_showIncludes, 76115ffd83dbSDimitry Andric options::OPT__SLASH_showIncludes_user)) { 76125ffd83dbSDimitry Andric CmdArgs.push_back("--show-includes"); 76135ffd83dbSDimitry Andric if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes)) 76145ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 76155ffd83dbSDimitry Andric } 76160b57cec5SDimitry Andric 76170b57cec5SDimitry Andric // This controls whether or not we emit RTTI data for polymorphic types. 76180b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR, 76190b57cec5SDimitry Andric /*Default=*/false)) 76200b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti-data"); 76210b57cec5SDimitry Andric 76220b57cec5SDimitry Andric // This controls whether or not we emit stack-protector instrumentation. 76230b57cec5SDimitry Andric // In MSVC, Buffer Security Check (/GS) is on by default. 76245ffd83dbSDimitry Andric if (!isNVPTX && Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_, 76250b57cec5SDimitry Andric /*Default=*/true)) { 76260b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 76270b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong))); 76280b57cec5SDimitry Andric } 76290b57cec5SDimitry Andric 7630e8d8bef9SDimitry Andric // Emit CodeView if -Z7 or -gline-tables-only are present. 7631e8d8bef9SDimitry Andric if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7, 76320b57cec5SDimitry Andric options::OPT_gline_tables_only)) { 76330b57cec5SDimitry Andric *EmitCodeView = true; 76340b57cec5SDimitry Andric if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7)) 7635fe6060f1SDimitry Andric *DebugInfoKind = codegenoptions::DebugInfoConstructor; 76360b57cec5SDimitry Andric else 76370b57cec5SDimitry Andric *DebugInfoKind = codegenoptions::DebugLineTablesOnly; 76380b57cec5SDimitry Andric } else { 76390b57cec5SDimitry Andric *EmitCodeView = false; 76400b57cec5SDimitry Andric } 76410b57cec5SDimitry Andric 76420b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 764381ad6265SDimitry Andric 764481ad6265SDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 764581ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_JMC, options::OPT__SLASH_JMC_, 764681ad6265SDimitry Andric /*Default=*/false)) { 764781ad6265SDimitry Andric if (*EmitCodeView && *DebugInfoKind >= codegenoptions::DebugInfoConstructor) 764881ad6265SDimitry Andric CmdArgs.push_back("-fjmc"); 764981ad6265SDimitry Andric else 765081ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC" 765181ad6265SDimitry Andric << "'/Zi', '/Z7'"; 765281ad6265SDimitry Andric } 765381ad6265SDimitry Andric 76540b57cec5SDimitry Andric EHFlags EH = parseClangCLEHFlags(D, Args); 76555ffd83dbSDimitry Andric if (!isNVPTX && (EH.Synch || EH.Asynch)) { 76560b57cec5SDimitry Andric if (types::isCXX(InputType)) 76570b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 76580b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 76590b57cec5SDimitry Andric } 76600b57cec5SDimitry Andric if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC) 76610b57cec5SDimitry Andric CmdArgs.push_back("-fexternc-nounwind"); 76620b57cec5SDimitry Andric 76630b57cec5SDimitry Andric // /EP should expand to -E -P. 76640b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_EP)) { 76650b57cec5SDimitry Andric CmdArgs.push_back("-E"); 76660b57cec5SDimitry Andric CmdArgs.push_back("-P"); 76670b57cec5SDimitry Andric } 76680b57cec5SDimitry Andric 76690b57cec5SDimitry Andric unsigned VolatileOptionID; 7670480093f4SDimitry Andric if (getToolChain().getTriple().isX86()) 76710b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_ms; 76720b57cec5SDimitry Andric else 76730b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_iso; 76740b57cec5SDimitry Andric 76750b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group)) 76760b57cec5SDimitry Andric VolatileOptionID = A->getOption().getID(); 76770b57cec5SDimitry Andric 76780b57cec5SDimitry Andric if (VolatileOptionID == options::OPT__SLASH_volatile_ms) 76790b57cec5SDimitry Andric CmdArgs.push_back("-fms-volatile"); 76800b57cec5SDimitry Andric 76810b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_, 76820b57cec5SDimitry Andric options::OPT__SLASH_Zc_dllexportInlines, 76830b57cec5SDimitry Andric false)) { 76840b57cec5SDimitry Andric CmdArgs.push_back("-fno-dllexport-inlines"); 76850b57cec5SDimitry Andric } 76860b57cec5SDimitry Andric 768781ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_wchar_t_, 768881ad6265SDimitry Andric options::OPT__SLASH_Zc_wchar_t, false)) { 768981ad6265SDimitry Andric CmdArgs.push_back("-fno-wchar"); 769081ad6265SDimitry Andric } 769181ad6265SDimitry Andric 769281ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 769381ad6265SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 769481ad6265SDimitry Andric std::vector<std::string> Values = 769581ad6265SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_arch); 769681ad6265SDimitry Andric if (!Values.empty()) { 769781ad6265SDimitry Andric llvm::SmallSet<std::string, 4> SupportedArches; 769881ad6265SDimitry Andric if (Arch == llvm::Triple::x86) 769981ad6265SDimitry Andric SupportedArches.insert("IA32"); 770081ad6265SDimitry Andric 770181ad6265SDimitry Andric for (auto &V : Values) 770281ad6265SDimitry Andric if (!SupportedArches.contains(V)) 770381ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 770481ad6265SDimitry Andric << std::string("/arch:").append(V) << "/kernel"; 770581ad6265SDimitry Andric } 770681ad6265SDimitry Andric 770781ad6265SDimitry Andric CmdArgs.push_back("-fno-rtti"); 770881ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR, options::OPT__SLASH_GR_, false)) 770981ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "/GR" 771081ad6265SDimitry Andric << "/kernel"; 771181ad6265SDimitry Andric } 771281ad6265SDimitry Andric 77130b57cec5SDimitry Andric Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); 77140b57cec5SDimitry Andric Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb); 77150b57cec5SDimitry Andric if (MostGeneralArg && BestCaseArg) 77160b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 77170b57cec5SDimitry Andric << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args); 77180b57cec5SDimitry Andric 77190b57cec5SDimitry Andric if (MostGeneralArg) { 77200b57cec5SDimitry Andric Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms); 77210b57cec5SDimitry Andric Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm); 77220b57cec5SDimitry Andric Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv); 77230b57cec5SDimitry Andric 77240b57cec5SDimitry Andric Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg; 77250b57cec5SDimitry Andric Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg; 77260b57cec5SDimitry Andric if (FirstConflict && SecondConflict && FirstConflict != SecondConflict) 77270b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 77280b57cec5SDimitry Andric << FirstConflict->getAsString(Args) 77290b57cec5SDimitry Andric << SecondConflict->getAsString(Args); 77300b57cec5SDimitry Andric 77310b57cec5SDimitry Andric if (SingleArg) 77320b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=single"); 77330b57cec5SDimitry Andric else if (MultipleArg) 77340b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=multiple"); 77350b57cec5SDimitry Andric else 77360b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=virtual"); 77370b57cec5SDimitry Andric } 77380b57cec5SDimitry Andric 77390b57cec5SDimitry Andric // Parse the default calling convention options. 77400b57cec5SDimitry Andric if (Arg *CCArg = 77410b57cec5SDimitry Andric Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr, 77420b57cec5SDimitry Andric options::OPT__SLASH_Gz, options::OPT__SLASH_Gv, 77430b57cec5SDimitry Andric options::OPT__SLASH_Gregcall)) { 77440b57cec5SDimitry Andric unsigned DCCOptId = CCArg->getOption().getID(); 77450b57cec5SDimitry Andric const char *DCCFlag = nullptr; 77465ffd83dbSDimitry Andric bool ArchSupported = !isNVPTX; 77470b57cec5SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 77480b57cec5SDimitry Andric switch (DCCOptId) { 77490b57cec5SDimitry Andric case options::OPT__SLASH_Gd: 77500b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=cdecl"; 77510b57cec5SDimitry Andric break; 77520b57cec5SDimitry Andric case options::OPT__SLASH_Gr: 77530b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 77540b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=fastcall"; 77550b57cec5SDimitry Andric break; 77560b57cec5SDimitry Andric case options::OPT__SLASH_Gz: 77570b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 77580b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=stdcall"; 77590b57cec5SDimitry Andric break; 77600b57cec5SDimitry Andric case options::OPT__SLASH_Gv: 77610b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 77620b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=vectorcall"; 77630b57cec5SDimitry Andric break; 77640b57cec5SDimitry Andric case options::OPT__SLASH_Gregcall: 77650b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 77660b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=regcall"; 77670b57cec5SDimitry Andric break; 77680b57cec5SDimitry Andric } 77690b57cec5SDimitry Andric 77700b57cec5SDimitry Andric // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either. 77710b57cec5SDimitry Andric if (ArchSupported && DCCFlag) 77720b57cec5SDimitry Andric CmdArgs.push_back(DCCFlag); 77730b57cec5SDimitry Andric } 77740b57cec5SDimitry Andric 77750b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ); 77760b57cec5SDimitry Andric 77770b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { 77780b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 77790b57cec5SDimitry Andric CmdArgs.push_back("msvc"); 77800b57cec5SDimitry Andric } 77810b57cec5SDimitry Andric 778281ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) 778381ad6265SDimitry Andric CmdArgs.push_back("-fms-kernel"); 778481ad6265SDimitry Andric 7785a324c340SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__SLASH_guard)) { 7786480093f4SDimitry Andric StringRef GuardArgs = A->getValue(); 7787fe6060f1SDimitry Andric // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and 7788fe6060f1SDimitry Andric // "ehcont-". 7789fe6060f1SDimitry Andric if (GuardArgs.equals_insensitive("cf")) { 7790480093f4SDimitry Andric // Emit CFG instrumentation and the table of address-taken functions. 77910b57cec5SDimitry Andric CmdArgs.push_back("-cfguard"); 7792fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf,nochecks")) { 7793480093f4SDimitry Andric // Emit only the table of address-taken functions. 7794480093f4SDimitry Andric CmdArgs.push_back("-cfguard-no-checks"); 7795fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("ehcont")) { 7796fe6060f1SDimitry Andric // Emit EH continuation table. 7797fe6060f1SDimitry Andric CmdArgs.push_back("-ehcontguard"); 7798fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf-") || 7799fe6060f1SDimitry Andric GuardArgs.equals_insensitive("ehcont-")) { 7800480093f4SDimitry Andric // Do nothing, but we might want to emit a security warning in future. 7801480093f4SDimitry Andric } else { 7802480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs; 7803480093f4SDimitry Andric } 78042efbaac7SDimitry Andric A->claim(); 78050b57cec5SDimitry Andric } 78060b57cec5SDimitry Andric } 78070b57cec5SDimitry Andric 78080b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args, 78090b57cec5SDimitry Andric const InputInfo &Input) { 78100b57cec5SDimitry Andric return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())); 78110b57cec5SDimitry Andric } 78120b57cec5SDimitry Andric 78130b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args, 78140b57cec5SDimitry Andric const InputInfoList &Inputs) { 78150b57cec5SDimitry Andric const char *Str = getBaseInputName(Args, Inputs[0]); 78160b57cec5SDimitry Andric 78170b57cec5SDimitry Andric if (const char *End = strrchr(Str, '.')) 78180b57cec5SDimitry Andric return Args.MakeArgString(std::string(Str, End)); 78190b57cec5SDimitry Andric 78200b57cec5SDimitry Andric return Str; 78210b57cec5SDimitry Andric } 78220b57cec5SDimitry Andric 78230b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args, 78240b57cec5SDimitry Andric const InputInfoList &Inputs) { 78250b57cec5SDimitry Andric // FIXME: Think about this more. 78260b57cec5SDimitry Andric 78270b57cec5SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 7828a7dea167SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 7829a7dea167SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d')); 7830a7dea167SDimitry Andric return Args.MakeArgString(OutputFilename); 78310b57cec5SDimitry Andric } 7832a7dea167SDimitry Andric 7833a7dea167SDimitry Andric return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d"); 78340b57cec5SDimitry Andric } 78350b57cec5SDimitry Andric 78360b57cec5SDimitry Andric // Begin ClangAs 78370b57cec5SDimitry Andric 78380b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args, 78390b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 78400b57cec5SDimitry Andric StringRef CPUName; 78410b57cec5SDimitry Andric StringRef ABIName; 78420b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 78430b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 78440b57cec5SDimitry Andric 78450b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 78460b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 78470b57cec5SDimitry Andric } 78480b57cec5SDimitry Andric 78490b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args, 78500b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 78515ffd83dbSDimitry Andric addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs, 78525ffd83dbSDimitry Andric /*IsLTO=*/false); 7853480093f4SDimitry Andric 78540b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 78550b57cec5SDimitry Andric StringRef Value = A->getValue(); 78560b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 78570b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 78580b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 78590b57cec5SDimitry Andric } else { 78600b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) 7861bdd1243dSDimitry Andric << A->getSpelling() << Value; 78620b57cec5SDimitry Andric } 78630b57cec5SDimitry Andric } 78640b57cec5SDimitry Andric } 78650b57cec5SDimitry Andric 78660b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args, 78670b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 78680b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 78690b57cec5SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 78700b57cec5SDimitry Andric 78710b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 78720b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 78730b57cec5SDimitry Andric } 78740b57cec5SDimitry Andric 78750b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 78760b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 78770b57cec5SDimitry Andric const ArgList &Args, 78780b57cec5SDimitry Andric const char *LinkingOutput) const { 78790b57cec5SDimitry Andric ArgStringList CmdArgs; 78800b57cec5SDimitry Andric 78810b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Unexpected number of inputs."); 78820b57cec5SDimitry Andric const InputInfo &Input = Inputs[0]; 78830b57cec5SDimitry Andric 78840b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 78850b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 78860b57cec5SDimitry Andric const auto &D = getToolChain().getDriver(); 78870b57cec5SDimitry Andric 78880b57cec5SDimitry Andric // Don't warn about "clang -w -c foo.s" 78890b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_w); 78900b57cec5SDimitry Andric // and "clang -emit-llvm -c foo.s" 78910b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 78920b57cec5SDimitry Andric 78930b57cec5SDimitry Andric claimNoWarnArgs(Args); 78940b57cec5SDimitry Andric 78950b57cec5SDimitry Andric // Invoke ourselves in -cc1as mode. 78960b57cec5SDimitry Andric // 78970b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 78980b57cec5SDimitry Andric CmdArgs.push_back("-cc1as"); 78990b57cec5SDimitry Andric 79000b57cec5SDimitry Andric // Add the "effective" target triple. 79010b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 79020b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 7903bdd1243dSDimitry Andric 7904bdd1243dSDimitry Andric getToolChain().addClangCC1ASTargetOptions(Args, CmdArgs); 79050b57cec5SDimitry Andric 79060b57cec5SDimitry Andric // Set the output mode, we currently only expect to be used as a real 79070b57cec5SDimitry Andric // assembler. 79080b57cec5SDimitry Andric CmdArgs.push_back("-filetype"); 79090b57cec5SDimitry Andric CmdArgs.push_back("obj"); 79100b57cec5SDimitry Andric 79110b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 79120b57cec5SDimitry Andric // -save-temps or preprocessed assembly. 79130b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 79140b57cec5SDimitry Andric CmdArgs.push_back(Clang::getBaseInputName(Args, Input)); 79150b57cec5SDimitry Andric 79160b57cec5SDimitry Andric // Add the target cpu 7917349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ true); 79180b57cec5SDimitry Andric if (!CPU.empty()) { 79190b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 79200b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 79210b57cec5SDimitry Andric } 79220b57cec5SDimitry Andric 79230b57cec5SDimitry Andric // Add the target features 79245ffd83dbSDimitry Andric getTargetFeatures(D, Triple, Args, CmdArgs, true); 79250b57cec5SDimitry Andric 79260b57cec5SDimitry Andric // Ignore explicit -force_cpusubtype_ALL option. 79270b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 79280b57cec5SDimitry Andric 79290b57cec5SDimitry Andric // Pass along any -I options so we get proper .include search paths. 79300b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I_Group); 79310b57cec5SDimitry Andric 79320b57cec5SDimitry Andric // Determine the original source input. 79330eae32dcSDimitry Andric auto FindSource = [](const Action *S) -> const Action * { 79340eae32dcSDimitry Andric while (S->getKind() != Action::InputClass) { 79350eae32dcSDimitry Andric assert(!S->getInputs().empty() && "unexpected root action!"); 79360eae32dcSDimitry Andric S = S->getInputs()[0]; 79370b57cec5SDimitry Andric } 79380eae32dcSDimitry Andric return S; 79390eae32dcSDimitry Andric }; 79400eae32dcSDimitry Andric const Action *SourceAction = FindSource(&JA); 79410b57cec5SDimitry Andric 79420b57cec5SDimitry Andric // Forward -g and handle debug info related flags, assuming we are dealing 79430b57cec5SDimitry Andric // with an actual assembly file. 79440b57cec5SDimitry Andric bool WantDebug = false; 79450b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_Group); 7946fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 79470b57cec5SDimitry Andric WantDebug = !A->getOption().matches(options::OPT_g0) && 79480b57cec5SDimitry Andric !A->getOption().matches(options::OPT_ggdb0); 7949480093f4SDimitry Andric 79500b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; 79510b57cec5SDimitry Andric 79520eae32dcSDimitry Andric // Add the -fdebug-compilation-dir flag if needed. 79530eae32dcSDimitry Andric const char *DebugCompilationDir = 79540eae32dcSDimitry Andric addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); 79550eae32dcSDimitry Andric 79560b57cec5SDimitry Andric if (SourceAction->getType() == types::TY_Asm || 79570b57cec5SDimitry Andric SourceAction->getType() == types::TY_PP_Asm) { 79580b57cec5SDimitry Andric // You might think that it would be ok to set DebugInfoKind outside of 79590b57cec5SDimitry Andric // the guard for source type, however there is a test which asserts 79600b57cec5SDimitry Andric // that some assembler invocation receives no -debug-info-kind, 79610b57cec5SDimitry Andric // and it's not clear whether that test is just overly restrictive. 7962fe6060f1SDimitry Andric DebugInfoKind = (WantDebug ? codegenoptions::DebugInfoConstructor 79630b57cec5SDimitry Andric : codegenoptions::NoDebugInfo); 79640b57cec5SDimitry Andric 796581ad6265SDimitry Andric addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args, 796681ad6265SDimitry Andric CmdArgs); 79670b57cec5SDimitry Andric 79680b57cec5SDimitry Andric // Set the AT_producer to the clang version when using the integrated 79690b57cec5SDimitry Andric // assembler on assembly source files. 79700b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-producer"); 79710b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 79720b57cec5SDimitry Andric 79730b57cec5SDimitry Andric // And pass along -I options 79740b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I); 79750b57cec5SDimitry Andric } 7976bdd1243dSDimitry Andric const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); 79770b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion, 79780b57cec5SDimitry Andric llvm::DebuggerKind::Default); 7979fe6060f1SDimitry Andric renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion); 79800b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain()); 79810b57cec5SDimitry Andric 79820b57cec5SDimitry Andric // Handle -fPIC et al -- the relocation-model affects the assembler 79830b57cec5SDimitry Andric // for some targets. 79840b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 79850b57cec5SDimitry Andric unsigned PICLevel; 79860b57cec5SDimitry Andric bool IsPIE; 79870b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 79880b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 79890b57cec5SDimitry Andric 79900b57cec5SDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 79910b57cec5SDimitry Andric if (RMName) { 79920b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 79930b57cec5SDimitry Andric CmdArgs.push_back(RMName); 79940b57cec5SDimitry Andric } 79950b57cec5SDimitry Andric 79960b57cec5SDimitry Andric // Optionally embed the -cc1as level arguments into the debug info, for build 79970b57cec5SDimitry Andric // analysis. 79980b57cec5SDimitry Andric if (getToolChain().UseDwarfDebugFlags()) { 79990b57cec5SDimitry Andric ArgStringList OriginalArgs; 80000b57cec5SDimitry Andric for (const auto &Arg : Args) 80010b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 80020b57cec5SDimitry Andric 80030b57cec5SDimitry Andric SmallString<256> Flags; 80040b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 80055ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 80060b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 80070b57cec5SDimitry Andric SmallString<128> EscapedArg; 80080b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 80090b57cec5SDimitry Andric Flags += " "; 80100b57cec5SDimitry Andric Flags += EscapedArg; 80110b57cec5SDimitry Andric } 80120b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 80130b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Flags)); 80140b57cec5SDimitry Andric } 80150b57cec5SDimitry Andric 80160b57cec5SDimitry Andric // FIXME: Add -static support, once we have it. 80170b57cec5SDimitry Andric 80180b57cec5SDimitry Andric // Add target specific flags. 80190b57cec5SDimitry Andric switch (getToolChain().getArch()) { 80200b57cec5SDimitry Andric default: 80210b57cec5SDimitry Andric break; 80220b57cec5SDimitry Andric 80230b57cec5SDimitry Andric case llvm::Triple::mips: 80240b57cec5SDimitry Andric case llvm::Triple::mipsel: 80250b57cec5SDimitry Andric case llvm::Triple::mips64: 80260b57cec5SDimitry Andric case llvm::Triple::mips64el: 80270b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 80280b57cec5SDimitry Andric break; 80290b57cec5SDimitry Andric 80300b57cec5SDimitry Andric case llvm::Triple::x86: 80310b57cec5SDimitry Andric case llvm::Triple::x86_64: 80320b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 80330b57cec5SDimitry Andric break; 80340b57cec5SDimitry Andric 80350b57cec5SDimitry Andric case llvm::Triple::arm: 80360b57cec5SDimitry Andric case llvm::Triple::armeb: 80370b57cec5SDimitry Andric case llvm::Triple::thumb: 80380b57cec5SDimitry Andric case llvm::Triple::thumbeb: 80390b57cec5SDimitry Andric // This isn't in AddARMTargetArgs because we want to do this for assembly 80400b57cec5SDimitry Andric // only, not C/C++. 80410b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 80420b57cec5SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 80430b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 80440b57cec5SDimitry Andric CmdArgs.push_back("-arm-add-build-attributes"); 80450b57cec5SDimitry Andric } 80460b57cec5SDimitry Andric break; 80470b57cec5SDimitry Andric 8048e8d8bef9SDimitry Andric case llvm::Triple::aarch64: 8049e8d8bef9SDimitry Andric case llvm::Triple::aarch64_32: 8050e8d8bef9SDimitry Andric case llvm::Triple::aarch64_be: 8051e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_mmark_bti_property)) { 8052e8d8bef9SDimitry Andric CmdArgs.push_back("-mllvm"); 8053e8d8bef9SDimitry Andric CmdArgs.push_back("-aarch64-mark-bti-property"); 8054e8d8bef9SDimitry Andric } 8055e8d8bef9SDimitry Andric break; 8056e8d8bef9SDimitry Andric 80570b57cec5SDimitry Andric case llvm::Triple::riscv32: 80580b57cec5SDimitry Andric case llvm::Triple::riscv64: 80590b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 80600b57cec5SDimitry Andric break; 80610b57cec5SDimitry Andric } 80620b57cec5SDimitry Andric 80630b57cec5SDimitry Andric // Consume all the warning flags. Usually this would be handled more 80640b57cec5SDimitry Andric // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as 80650b57cec5SDimitry Andric // doesn't handle that so rather than warning about unused flags that are 80660b57cec5SDimitry Andric // actually used, we'll lie by omission instead. 80670b57cec5SDimitry Andric // FIXME: Stop lying and consume only the appropriate driver flags 80680b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_W_Group); 80690b57cec5SDimitry Andric 80700b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 80710b57cec5SDimitry Andric getToolChain().getDriver()); 80720b57cec5SDimitry Andric 80730b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 80740b57cec5SDimitry Andric 80750eae32dcSDimitry Andric if (DebugInfoKind > codegenoptions::NoDebugInfo && Output.isFilename()) 80760eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 80770eae32dcSDimitry Andric Output.getFilename()); 80780eae32dcSDimitry Andric 80790eae32dcSDimitry Andric // Fixup any previous commands that use -object-file-name because when we 80800eae32dcSDimitry Andric // generated them, the final .obj name wasn't yet known. 80810eae32dcSDimitry Andric for (Command &J : C.getJobs()) { 80820eae32dcSDimitry Andric if (SourceAction != FindSource(&J.getSource())) 80830eae32dcSDimitry Andric continue; 80840eae32dcSDimitry Andric auto &JArgs = J.getArguments(); 80850eae32dcSDimitry Andric for (unsigned I = 0; I < JArgs.size(); ++I) { 80860eae32dcSDimitry Andric if (StringRef(JArgs[I]).startswith("-object-file-name=") && 80870eae32dcSDimitry Andric Output.isFilename()) { 80880eae32dcSDimitry Andric ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); 80890eae32dcSDimitry Andric addDebugObjectName(Args, NewArgs, DebugCompilationDir, 80900eae32dcSDimitry Andric Output.getFilename()); 80910eae32dcSDimitry Andric NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); 80920eae32dcSDimitry Andric J.replaceArguments(NewArgs); 80930eae32dcSDimitry Andric break; 80940eae32dcSDimitry Andric } 80950eae32dcSDimitry Andric } 80960eae32dcSDimitry Andric } 80970eae32dcSDimitry Andric 80980b57cec5SDimitry Andric assert(Output.isFilename() && "Unexpected lipo output."); 80990b57cec5SDimitry Andric CmdArgs.push_back("-o"); 81000b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 81010b57cec5SDimitry Andric 81020b57cec5SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 81030b57cec5SDimitry Andric Arg *A; 81040b57cec5SDimitry Andric if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split && 81050b57cec5SDimitry Andric T.isOSBinFormatELF()) { 81060b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 8107e8d8bef9SDimitry Andric CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output)); 81080b57cec5SDimitry Andric } 81090b57cec5SDimitry Andric 8110e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) 811181ad6265SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true); 8112e8d8bef9SDimitry Andric 81130b57cec5SDimitry Andric assert(Input.isFilename() && "Invalid input."); 81140b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 81150b57cec5SDimitry Andric 81160b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 8117e8d8bef9SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 8118e8d8bef9SDimitry Andric // Invoke cc1as directly in this process. 8119e8d8bef9SDimitry Andric C.addCommand(std::make_unique<CC1Command>(JA, *this, 8120e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 8121e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 8122e8d8bef9SDimitry Andric } else { 8123e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 8124e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 8125e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 8126e8d8bef9SDimitry Andric } 81270b57cec5SDimitry Andric } 81280b57cec5SDimitry Andric 81290b57cec5SDimitry Andric // Begin OffloadBundler 81300b57cec5SDimitry Andric 81310b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, 81320b57cec5SDimitry Andric const InputInfo &Output, 81330b57cec5SDimitry Andric const InputInfoList &Inputs, 81340b57cec5SDimitry Andric const llvm::opt::ArgList &TCArgs, 81350b57cec5SDimitry Andric const char *LinkingOutput) const { 81360b57cec5SDimitry Andric // The version with only one output is expected to refer to a bundling job. 81370b57cec5SDimitry Andric assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!"); 81380b57cec5SDimitry Andric 81390b57cec5SDimitry Andric // The bundling command looks like this: 81400b57cec5SDimitry Andric // clang-offload-bundler -type=bc 81410b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 814281ad6265SDimitry Andric // -output=output_file 814381ad6265SDimitry Andric // -input=unbundle_file_host 814481ad6265SDimitry Andric // -input=unbundle_file_tgt1 814581ad6265SDimitry Andric // -input=unbundle_file_tgt2 81460b57cec5SDimitry Andric 81470b57cec5SDimitry Andric ArgStringList CmdArgs; 81480b57cec5SDimitry Andric 81490b57cec5SDimitry Andric // Get the type. 81500b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 81510b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Output.getType()))); 81520b57cec5SDimitry Andric 81530b57cec5SDimitry Andric assert(JA.getInputs().size() == Inputs.size() && 81540b57cec5SDimitry Andric "Not have inputs for all dependence actions??"); 81550b57cec5SDimitry Andric 81560b57cec5SDimitry Andric // Get the targets. 81570b57cec5SDimitry Andric SmallString<128> Triples; 81580b57cec5SDimitry Andric Triples += "-targets="; 81590b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 81600b57cec5SDimitry Andric if (I) 81610b57cec5SDimitry Andric Triples += ','; 81620b57cec5SDimitry Andric 81630b57cec5SDimitry Andric // Find ToolChain for this input. 81640b57cec5SDimitry Andric Action::OffloadKind CurKind = Action::OFK_Host; 81650b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 81660b57cec5SDimitry Andric const Action *CurDep = JA.getInputs()[I]; 81670b57cec5SDimitry Andric 81680b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) { 81690b57cec5SDimitry Andric CurTC = nullptr; 81700b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) { 81710b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 81720b57cec5SDimitry Andric CurKind = A->getOffloadingDeviceKind(); 81730b57cec5SDimitry Andric CurTC = TC; 81740b57cec5SDimitry Andric }); 81750b57cec5SDimitry Andric } 81760b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(CurKind); 8177349cc55cSDimitry Andric Triples += '-'; 8178349cc55cSDimitry Andric Triples += CurTC->getTriple().normalize(); 8179349cc55cSDimitry Andric if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) && 81800eae32dcSDimitry Andric !StringRef(CurDep->getOffloadingArch()).empty()) { 8181349cc55cSDimitry Andric Triples += '-'; 81820b57cec5SDimitry Andric Triples += CurDep->getOffloadingArch(); 81830b57cec5SDimitry Andric } 8184349cc55cSDimitry Andric 8185349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8186349cc55cSDimitry Andric // with each toolchain. 8187349cc55cSDimitry Andric StringRef GPUArchName; 8188349cc55cSDimitry Andric if (CurKind == Action::OFK_OpenMP) { 8189349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8190349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8191349cc55cSDimitry Andric auto ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 8192349cc55cSDimitry Andric auto Arch = ArchStr.startswith_insensitive("-march="); 8193349cc55cSDimitry Andric if (Arch) { 8194349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8195349cc55cSDimitry Andric Triples += "-"; 8196349cc55cSDimitry Andric break; 8197349cc55cSDimitry Andric } 8198349cc55cSDimitry Andric } 8199349cc55cSDimitry Andric Triples += GPUArchName.str(); 8200349cc55cSDimitry Andric } 82010b57cec5SDimitry Andric } 82020b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 82030b57cec5SDimitry Andric 82040b57cec5SDimitry Andric // Get bundled file command. 82050b57cec5SDimitry Andric CmdArgs.push_back( 820681ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-output=") + Output.getFilename())); 82070b57cec5SDimitry Andric 82080b57cec5SDimitry Andric // Get unbundled files command. 82090b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 821081ad6265SDimitry Andric SmallString<128> UB; 821181ad6265SDimitry Andric UB += "-input="; 82120b57cec5SDimitry Andric 82130b57cec5SDimitry Andric // Find ToolChain for this input. 82140b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 82150b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) { 82160b57cec5SDimitry Andric CurTC = nullptr; 82170b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) { 82180b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 82190b57cec5SDimitry Andric CurTC = TC; 82200b57cec5SDimitry Andric }); 82216e75b2fbSDimitry Andric UB += C.addTempFile( 82226e75b2fbSDimitry Andric C.getArgs().MakeArgString(CurTC->getInputFilename(Inputs[I]))); 82236e75b2fbSDimitry Andric } else { 82240b57cec5SDimitry Andric UB += CurTC->getInputFilename(Inputs[I]); 82250b57cec5SDimitry Andric } 82260b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 822781ad6265SDimitry Andric } 82280b57cec5SDimitry Andric // All the inputs are encoded as commands. 8229a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 82305ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 82310b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8232bdd1243dSDimitry Andric CmdArgs, std::nullopt, Output)); 82330b57cec5SDimitry Andric } 82340b57cec5SDimitry Andric 82350b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs( 82360b57cec5SDimitry Andric Compilation &C, const JobAction &JA, const InputInfoList &Outputs, 82370b57cec5SDimitry Andric const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, 82380b57cec5SDimitry Andric const char *LinkingOutput) const { 82390b57cec5SDimitry Andric // The version with multiple outputs is expected to refer to a unbundling job. 82400b57cec5SDimitry Andric auto &UA = cast<OffloadUnbundlingJobAction>(JA); 82410b57cec5SDimitry Andric 82420b57cec5SDimitry Andric // The unbundling command looks like this: 82430b57cec5SDimitry Andric // clang-offload-bundler -type=bc 82440b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 824581ad6265SDimitry Andric // -input=input_file 824681ad6265SDimitry Andric // -output=unbundle_file_host 824781ad6265SDimitry Andric // -output=unbundle_file_tgt1 824881ad6265SDimitry Andric // -output=unbundle_file_tgt2 82490b57cec5SDimitry Andric // -unbundle 82500b57cec5SDimitry Andric 82510b57cec5SDimitry Andric ArgStringList CmdArgs; 82520b57cec5SDimitry Andric 82530b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Expecting to unbundle a single file!"); 82540b57cec5SDimitry Andric InputInfo Input = Inputs.front(); 82550b57cec5SDimitry Andric 82560b57cec5SDimitry Andric // Get the type. 82570b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 82580b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Input.getType()))); 82590b57cec5SDimitry Andric 82600b57cec5SDimitry Andric // Get the targets. 82610b57cec5SDimitry Andric SmallString<128> Triples; 82620b57cec5SDimitry Andric Triples += "-targets="; 82630b57cec5SDimitry Andric auto DepInfo = UA.getDependentActionsInfo(); 82640b57cec5SDimitry Andric for (unsigned I = 0; I < DepInfo.size(); ++I) { 82650b57cec5SDimitry Andric if (I) 82660b57cec5SDimitry Andric Triples += ','; 82670b57cec5SDimitry Andric 82680b57cec5SDimitry Andric auto &Dep = DepInfo[I]; 82690b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind); 8270349cc55cSDimitry Andric Triples += '-'; 8271349cc55cSDimitry Andric Triples += Dep.DependentToolChain->getTriple().normalize(); 8272349cc55cSDimitry Andric if ((Dep.DependentOffloadKind == Action::OFK_HIP || 8273349cc55cSDimitry Andric Dep.DependentOffloadKind == Action::OFK_Cuda) && 8274349cc55cSDimitry Andric !Dep.DependentBoundArch.empty()) { 8275349cc55cSDimitry Andric Triples += '-'; 82760b57cec5SDimitry Andric Triples += Dep.DependentBoundArch; 82770b57cec5SDimitry Andric } 8278349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8279349cc55cSDimitry Andric // with each toolchain. 8280349cc55cSDimitry Andric StringRef GPUArchName; 8281349cc55cSDimitry Andric if (Dep.DependentOffloadKind == Action::OFK_OpenMP) { 8282349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8283349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8284349cc55cSDimitry Andric StringRef ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 8285349cc55cSDimitry Andric auto Arch = ArchStr.startswith_insensitive("-march="); 8286349cc55cSDimitry Andric if (Arch) { 8287349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8288349cc55cSDimitry Andric Triples += "-"; 8289349cc55cSDimitry Andric break; 8290349cc55cSDimitry Andric } 8291349cc55cSDimitry Andric } 8292349cc55cSDimitry Andric Triples += GPUArchName.str(); 8293349cc55cSDimitry Andric } 82940b57cec5SDimitry Andric } 82950b57cec5SDimitry Andric 82960b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 82970b57cec5SDimitry Andric 82980b57cec5SDimitry Andric // Get bundled file command. 82990b57cec5SDimitry Andric CmdArgs.push_back( 830081ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-input=") + Input.getFilename())); 83010b57cec5SDimitry Andric 83020b57cec5SDimitry Andric // Get unbundled files command. 83030b57cec5SDimitry Andric for (unsigned I = 0; I < Outputs.size(); ++I) { 830481ad6265SDimitry Andric SmallString<128> UB; 830581ad6265SDimitry Andric UB += "-output="; 83060b57cec5SDimitry Andric UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]); 83070b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 830881ad6265SDimitry Andric } 83090b57cec5SDimitry Andric CmdArgs.push_back("-unbundle"); 8310e8d8bef9SDimitry Andric CmdArgs.push_back("-allow-missing-bundles"); 83110b57cec5SDimitry Andric 83120b57cec5SDimitry Andric // All the inputs are encoded as commands. 8313a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 83145ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 83150b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8316bdd1243dSDimitry Andric CmdArgs, std::nullopt, Outputs)); 8317a7dea167SDimitry Andric } 83181fd87a68SDimitry Andric 831981ad6265SDimitry Andric void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA, 832081ad6265SDimitry Andric const InputInfo &Output, 832181ad6265SDimitry Andric const InputInfoList &Inputs, 832281ad6265SDimitry Andric const llvm::opt::ArgList &Args, 832381ad6265SDimitry Andric const char *LinkingOutput) const { 832481ad6265SDimitry Andric ArgStringList CmdArgs; 832581ad6265SDimitry Andric 832681ad6265SDimitry Andric // Add the output file name. 832781ad6265SDimitry Andric assert(Output.isFilename() && "Invalid output."); 832881ad6265SDimitry Andric CmdArgs.push_back("-o"); 832981ad6265SDimitry Andric CmdArgs.push_back(Output.getFilename()); 833081ad6265SDimitry Andric 833181ad6265SDimitry Andric // Create the inputs to bundle the needed metadata. 833281ad6265SDimitry Andric for (const InputInfo &Input : Inputs) { 833381ad6265SDimitry Andric const Action *OffloadAction = Input.getAction(); 833481ad6265SDimitry Andric const ToolChain *TC = OffloadAction->getOffloadingToolChain(); 833581ad6265SDimitry Andric const ArgList &TCArgs = 833681ad6265SDimitry Andric C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(), 833781ad6265SDimitry Andric OffloadAction->getOffloadingDeviceKind()); 833881ad6265SDimitry Andric StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input)); 833981ad6265SDimitry Andric StringRef Arch = (OffloadAction->getOffloadingArch()) 834081ad6265SDimitry Andric ? OffloadAction->getOffloadingArch() 834181ad6265SDimitry Andric : TCArgs.getLastArgValue(options::OPT_march_EQ); 834281ad6265SDimitry Andric StringRef Kind = 834381ad6265SDimitry Andric Action::GetOffloadKindName(OffloadAction->getOffloadingDeviceKind()); 834481ad6265SDimitry Andric 834581ad6265SDimitry Andric ArgStringList Features; 834681ad6265SDimitry Andric SmallVector<StringRef> FeatureArgs; 8347753f127fSDimitry Andric getTargetFeatures(TC->getDriver(), TC->getTriple(), TCArgs, Features, 8348753f127fSDimitry Andric false); 834981ad6265SDimitry Andric llvm::copy_if(Features, std::back_inserter(FeatureArgs), 835081ad6265SDimitry Andric [](StringRef Arg) { return !Arg.startswith("-target"); }); 835181ad6265SDimitry Andric 835281ad6265SDimitry Andric SmallVector<std::string> Parts{ 835381ad6265SDimitry Andric "file=" + File.str(), 835481ad6265SDimitry Andric "triple=" + TC->getTripleString(), 835581ad6265SDimitry Andric "arch=" + Arch.str(), 835681ad6265SDimitry Andric "kind=" + Kind.str(), 835781ad6265SDimitry Andric }; 835881ad6265SDimitry Andric 835981ad6265SDimitry Andric if (TC->getDriver().isUsingLTO(/* IsOffload */ true)) 836081ad6265SDimitry Andric for (StringRef Feature : FeatureArgs) 836181ad6265SDimitry Andric Parts.emplace_back("feature=" + Feature.str()); 836281ad6265SDimitry Andric 836381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("--image=" + llvm::join(Parts, ","))); 836481ad6265SDimitry Andric } 836581ad6265SDimitry Andric 836681ad6265SDimitry Andric C.addCommand(std::make_unique<Command>( 836781ad6265SDimitry Andric JA, *this, ResponseFileSupport::None(), 836881ad6265SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), 836981ad6265SDimitry Andric CmdArgs, Inputs, Output)); 837081ad6265SDimitry Andric } 837181ad6265SDimitry Andric 83721fd87a68SDimitry Andric void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, 83731fd87a68SDimitry Andric const InputInfo &Output, 83741fd87a68SDimitry Andric const InputInfoList &Inputs, 83751fd87a68SDimitry Andric const ArgList &Args, 83761fd87a68SDimitry Andric const char *LinkingOutput) const { 8377d56accc7SDimitry Andric const Driver &D = getToolChain().getDriver(); 8378d56accc7SDimitry Andric const llvm::Triple TheTriple = getToolChain().getTriple(); 83791fd87a68SDimitry Andric ArgStringList CmdArgs; 83801fd87a68SDimitry Andric 8381d56accc7SDimitry Andric // Pass the CUDA path to the linker wrapper tool. 838281ad6265SDimitry Andric for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP}) { 838381ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains(Kind); 838481ad6265SDimitry Andric for (auto &I : llvm::make_range(TCRange.first, TCRange.second)) { 8385d56accc7SDimitry Andric const ToolChain *TC = I.second; 8386d56accc7SDimitry Andric if (TC->getTriple().isNVPTX()) { 8387d56accc7SDimitry Andric CudaInstallationDetector CudaInstallation(D, TheTriple, Args); 8388d56accc7SDimitry Andric if (CudaInstallation.isValid()) 8389d56accc7SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8390d56accc7SDimitry Andric "--cuda-path=" + CudaInstallation.getInstallPath())); 8391d56accc7SDimitry Andric break; 8392d56accc7SDimitry Andric } 8393d56accc7SDimitry Andric } 83941fd87a68SDimitry Andric } 83951fd87a68SDimitry Andric 839681ad6265SDimitry Andric if (D.isUsingLTO(/* IsOffload */ true)) { 83971fd87a68SDimitry Andric // Pass in the optimization level to use for LTO. 83981fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 83991fd87a68SDimitry Andric StringRef OOpt; 84001fd87a68SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 84011fd87a68SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 84021fd87a68SDimitry Andric OOpt = "3"; 84031fd87a68SDimitry Andric else if (A->getOption().matches(options::OPT_O)) { 84041fd87a68SDimitry Andric OOpt = A->getValue(); 84051fd87a68SDimitry Andric if (OOpt == "g") 84061fd87a68SDimitry Andric OOpt = "1"; 84071fd87a68SDimitry Andric else if (OOpt == "s" || OOpt == "z") 84081fd87a68SDimitry Andric OOpt = "2"; 84091fd87a68SDimitry Andric } else if (A->getOption().matches(options::OPT_O0)) 84101fd87a68SDimitry Andric OOpt = "0"; 84111fd87a68SDimitry Andric if (!OOpt.empty()) 8412753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--opt-level=O") + OOpt)); 84131fd87a68SDimitry Andric } 84141fd87a68SDimitry Andric } 84151fd87a68SDimitry Andric 8416753f127fSDimitry Andric CmdArgs.push_back( 8417753f127fSDimitry Andric Args.MakeArgString("--host-triple=" + TheTriple.getTriple())); 84181fd87a68SDimitry Andric if (Args.hasArg(options::OPT_v)) 8419bdd1243dSDimitry Andric CmdArgs.push_back("--wrapper-verbose"); 84201fd87a68SDimitry Andric 84211fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 8422753f127fSDimitry Andric if (!A->getOption().matches(options::OPT_g0)) 8423753f127fSDimitry Andric CmdArgs.push_back("--device-debug"); 84241fd87a68SDimitry Andric } 84251fd87a68SDimitry Andric 84261fd87a68SDimitry Andric for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 8427bdd1243dSDimitry Andric CmdArgs.push_back(Args.MakeArgString("--ptxas-arg=" + A)); 84281fd87a68SDimitry Andric 84291fd87a68SDimitry Andric // Forward remarks passes to the LLVM backend in the wrapper. 84301fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) 8431fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--offload-opt=-pass-remarks=") + 8432fcaf7f86SDimitry Andric A->getValue())); 84331fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) 8434fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8435fcaf7f86SDimitry Andric Twine("--offload-opt=-pass-remarks-missed=") + A->getValue())); 84361fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) 8437fcaf7f86SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8438fcaf7f86SDimitry Andric Twine("--offload-opt=-pass-remarks-analysis=") + A->getValue())); 84391fd87a68SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 8440753f127fSDimitry Andric CmdArgs.push_back("--save-temps"); 84411fd87a68SDimitry Andric 8442d56accc7SDimitry Andric // Construct the link job so we can wrap around it. 8443d56accc7SDimitry Andric Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); 8444d56accc7SDimitry Andric const auto &LinkCommand = C.getJobs().getJobs().back(); 8445d56accc7SDimitry Andric 844681ad6265SDimitry Andric // Forward -Xoffload-linker<-triple> arguments to the device link job. 8447753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_Xoffload_linker)) { 8448753f127fSDimitry Andric StringRef Val = A->getValue(0); 844981ad6265SDimitry Andric if (Val.empty()) 845081ad6265SDimitry Andric CmdArgs.push_back( 8451753f127fSDimitry Andric Args.MakeArgString(Twine("--device-linker=") + A->getValue(1))); 845281ad6265SDimitry Andric else 845381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8454753f127fSDimitry Andric "--device-linker=" + 845581ad6265SDimitry Andric ToolChain::getOpenMPTriple(Val.drop_front()).getTriple() + "=" + 8456753f127fSDimitry Andric A->getValue(1))); 845781ad6265SDimitry Andric } 845881ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_Xoffload_linker); 845981ad6265SDimitry Andric 8460bdd1243dSDimitry Andric // Embed bitcode instead of an object in JIT mode. 8461bdd1243dSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_jit, 8462bdd1243dSDimitry Andric options::OPT_fno_openmp_target_jit, false)) 8463bdd1243dSDimitry Andric CmdArgs.push_back("--embed-bitcode"); 8464bdd1243dSDimitry Andric 8465753f127fSDimitry Andric // Forward `-mllvm` arguments to the LLVM invocations if present. 8466753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_mllvm)) { 8467753f127fSDimitry Andric CmdArgs.push_back("-mllvm"); 8468753f127fSDimitry Andric CmdArgs.push_back(A->getValue()); 8469753f127fSDimitry Andric A->claim(); 8470753f127fSDimitry Andric } 8471753f127fSDimitry Andric 84721fd87a68SDimitry Andric // Add the linker arguments to be forwarded by the wrapper. 8473753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") + 8474753f127fSDimitry Andric LinkCommand->getExecutable())); 84751fd87a68SDimitry Andric CmdArgs.push_back("--"); 84761fd87a68SDimitry Andric for (const char *LinkArg : LinkCommand->getArguments()) 84771fd87a68SDimitry Andric CmdArgs.push_back(LinkArg); 84781fd87a68SDimitry Andric 84791fd87a68SDimitry Andric const char *Exec = 84801fd87a68SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper")); 84811fd87a68SDimitry Andric 84821fd87a68SDimitry Andric // Replace the executable and arguments of the link job with the 84831fd87a68SDimitry Andric // wrapper. 84841fd87a68SDimitry Andric LinkCommand->replaceExecutable(Exec); 84851fd87a68SDimitry Andric LinkCommand->replaceArguments(CmdArgs); 84861fd87a68SDimitry Andric } 8487