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" 14fe6060f1SDimitry Andric #include "Arch/M68k.h" 150b57cec5SDimitry Andric #include "Arch/Mips.h" 160b57cec5SDimitry Andric #include "Arch/PPC.h" 170b57cec5SDimitry Andric #include "Arch/RISCV.h" 180b57cec5SDimitry Andric #include "Arch/Sparc.h" 190b57cec5SDimitry Andric #include "Arch/SystemZ.h" 205ffd83dbSDimitry Andric #include "Arch/VE.h" 210b57cec5SDimitry Andric #include "Arch/X86.h" 220b57cec5SDimitry Andric #include "CommonArgs.h" 230b57cec5SDimitry Andric #include "Hexagon.h" 245ffd83dbSDimitry Andric #include "MSP430.h" 250b57cec5SDimitry Andric #include "PS4CPU.h" 26349cc55cSDimitry Andric #include "clang/Basic/CLWarnings.h" 270b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h" 28a7dea167SDimitry Andric #include "clang/Basic/CodeGenOptions.h" 290b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 30753f127fSDimitry Andric #include "clang/Basic/MakeSupport.h" 310b57cec5SDimitry Andric #include "clang/Basic/ObjCRuntime.h" 320b57cec5SDimitry Andric #include "clang/Basic/Version.h" 3381ad6265SDimitry Andric #include "clang/Config/config.h" 3481ad6265SDimitry Andric #include "clang/Driver/Action.h" 350b57cec5SDimitry Andric #include "clang/Driver/Distro.h" 360b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h" 37fe6060f1SDimitry Andric #include "clang/Driver/InputInfo.h" 380b57cec5SDimitry Andric #include "clang/Driver/Options.h" 390b57cec5SDimitry Andric #include "clang/Driver/SanitizerArgs.h" 4004eeddc0SDimitry Andric #include "clang/Driver/Types.h" 410b57cec5SDimitry Andric #include "clang/Driver/XRayArgs.h" 4281ad6265SDimitry Andric #include "llvm/ADT/SmallSet.h" 430b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 440b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 450b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 460b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 475ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h" 480b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 490b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 50e8d8bef9SDimitry Andric #include "llvm/Support/Host.h" 510b57cec5SDimitry Andric #include "llvm/Support/Path.h" 520b57cec5SDimitry Andric #include "llvm/Support/Process.h" 530b57cec5SDimitry Andric #include "llvm/Support/TargetParser.h" 540b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h" 55753f127fSDimitry Andric #include <cctype> 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric using namespace clang::driver; 580b57cec5SDimitry Andric using namespace clang::driver::tools; 590b57cec5SDimitry Andric using namespace clang; 600b57cec5SDimitry Andric using namespace llvm::opt; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { 63349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC, 64349cc55cSDimitry Andric options::OPT_fminimize_whitespace, 65349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace)) { 660b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) && 670b57cec5SDimitry Andric !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) { 680b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 690b57cec5SDimitry Andric << A->getBaseArg().getAsString(Args) 700b57cec5SDimitry Andric << (D.IsCLMode() ? "/E, /P or /EP" : "-E"); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 760b57cec5SDimitry Andric // In gcc, only ARM checks this, but it seems reasonable to check universally. 770b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 780b57cec5SDimitry Andric if (const Arg *A = 790b57cec5SDimitry Andric Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic)) 800b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args) 810b57cec5SDimitry Andric << "-static"; 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // Add backslashes to escape spaces and other backslashes. 850b57cec5SDimitry Andric // This is used for the space-separated argument list specified with 860b57cec5SDimitry Andric // the -dwarf-debug-flags option. 870b57cec5SDimitry Andric static void EscapeSpacesAndBackslashes(const char *Arg, 880b57cec5SDimitry Andric SmallVectorImpl<char> &Res) { 890b57cec5SDimitry Andric for (; *Arg; ++Arg) { 900b57cec5SDimitry Andric switch (*Arg) { 910b57cec5SDimitry Andric default: 920b57cec5SDimitry Andric break; 930b57cec5SDimitry Andric case ' ': 940b57cec5SDimitry Andric case '\\': 950b57cec5SDimitry Andric Res.push_back('\\'); 960b57cec5SDimitry Andric break; 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric Res.push_back(*Arg); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric /// Apply \a Work on the current tool chain \a RegularToolChain and any other 1030b57cec5SDimitry Andric /// offloading tool chain that is associated with the current action \a JA. 1040b57cec5SDimitry Andric static void 1050b57cec5SDimitry Andric forAllAssociatedToolChains(Compilation &C, const JobAction &JA, 1060b57cec5SDimitry Andric const ToolChain &RegularToolChain, 1070b57cec5SDimitry Andric llvm::function_ref<void(const ToolChain &)> Work) { 1080b57cec5SDimitry Andric // Apply Work on the current/regular tool chain. 1090b57cec5SDimitry Andric Work(RegularToolChain); 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric // Apply Work on all the offloading tool chains associated with the current 1120b57cec5SDimitry Andric // action. 1130b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_Cuda)) 1140b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 1150b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_Cuda)) 1160b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1170b57cec5SDimitry Andric else if (JA.isHostOffloading(Action::OFK_HIP)) 1180b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_HIP>()); 1190b57cec5SDimitry Andric else if (JA.isDeviceOffloading(Action::OFK_HIP)) 1200b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 1230b57cec5SDimitry Andric auto TCs = C.getOffloadToolChains<Action::OFK_OpenMP>(); 1240b57cec5SDimitry Andric for (auto II = TCs.first, IE = TCs.second; II != IE; ++II) 1250b57cec5SDimitry Andric Work(*II->second); 1260b57cec5SDimitry Andric } else if (JA.isDeviceOffloading(Action::OFK_OpenMP)) 1270b57cec5SDimitry Andric Work(*C.getSingleOffloadToolChain<Action::OFK_Host>()); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric // 1300b57cec5SDimitry Andric // TODO: Add support for other offloading programming models here. 1310b57cec5SDimitry Andric // 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric /// This is a helper function for validating the optional refinement step 1350b57cec5SDimitry Andric /// parameter in reciprocal argument strings. Return false if there is an error 1360b57cec5SDimitry Andric /// parsing the refinement step. Otherwise, return true and set the Position 1370b57cec5SDimitry Andric /// of the refinement step in the input string. 1380b57cec5SDimitry Andric static bool getRefinementStep(StringRef In, const Driver &D, 1390b57cec5SDimitry Andric const Arg &A, size_t &Position) { 1400b57cec5SDimitry Andric const char RefinementStepToken = ':'; 1410b57cec5SDimitry Andric Position = In.find(RefinementStepToken); 1420b57cec5SDimitry Andric if (Position != StringRef::npos) { 1430b57cec5SDimitry Andric StringRef Option = A.getOption().getName(); 1440b57cec5SDimitry Andric StringRef RefStep = In.substr(Position + 1); 1450b57cec5SDimitry Andric // Allow exactly one numeric character for the additional refinement 1460b57cec5SDimitry Andric // step parameter. This is reasonable for all currently-supported 1470b57cec5SDimitry Andric // operations and architectures because we would expect that a larger value 1480b57cec5SDimitry Andric // of refinement steps would cause the estimate "optimization" to 1490b57cec5SDimitry Andric // under-perform the native operation. Also, if the estimate does not 1500b57cec5SDimitry Andric // converge quickly, it probably will not ever converge, so further 1510b57cec5SDimitry Andric // refinement steps will not produce a better answer. 1520b57cec5SDimitry Andric if (RefStep.size() != 1) { 1530b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1540b57cec5SDimitry Andric return false; 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric char RefStepChar = RefStep[0]; 1570b57cec5SDimitry Andric if (RefStepChar < '0' || RefStepChar > '9') { 1580b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << Option << RefStep; 1590b57cec5SDimitry Andric return false; 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric return true; 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric /// The -mrecip flag requires processing of many optional parameters. 1660b57cec5SDimitry Andric static void ParseMRecip(const Driver &D, const ArgList &Args, 1670b57cec5SDimitry Andric ArgStringList &OutStrings) { 1680b57cec5SDimitry Andric StringRef DisabledPrefixIn = "!"; 1690b57cec5SDimitry Andric StringRef DisabledPrefixOut = "!"; 1700b57cec5SDimitry Andric StringRef EnabledPrefixOut = ""; 1710b57cec5SDimitry Andric StringRef Out = "-mrecip="; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ); 1740b57cec5SDimitry Andric if (!A) 1750b57cec5SDimitry Andric return; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric unsigned NumOptions = A->getNumValues(); 1780b57cec5SDimitry Andric if (NumOptions == 0) { 1790b57cec5SDimitry Andric // No option is the same as "all". 1800b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + "all")); 1810b57cec5SDimitry Andric return; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric // Pass through "all", "none", or "default" with an optional refinement step. 1850b57cec5SDimitry Andric if (NumOptions == 1) { 1860b57cec5SDimitry Andric StringRef Val = A->getValue(0); 1870b57cec5SDimitry Andric size_t RefStepLoc; 1880b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStepLoc)) 1890b57cec5SDimitry Andric return; 1900b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStepLoc); 1910b57cec5SDimitry Andric if (ValBase == "all" || ValBase == "none" || ValBase == "default") { 1920b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out + Val)); 1930b57cec5SDimitry Andric return; 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric // Each reciprocal type may be enabled or disabled individually. 1980b57cec5SDimitry Andric // Check each input value for validity, concatenate them all back together, 1990b57cec5SDimitry Andric // and pass through. 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric llvm::StringMap<bool> OptionStrings; 2020b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divd", false)); 2030b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("divf", false)); 20481ad6265SDimitry Andric OptionStrings.insert(std::make_pair("divh", false)); 2050b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divd", false)); 2060b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-divf", false)); 20781ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-divh", false)); 2080b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtd", false)); 2090b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("sqrtf", false)); 21081ad6265SDimitry Andric OptionStrings.insert(std::make_pair("sqrth", false)); 2110b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtd", false)); 2120b57cec5SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrtf", false)); 21381ad6265SDimitry Andric OptionStrings.insert(std::make_pair("vec-sqrth", false)); 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric for (unsigned i = 0; i != NumOptions; ++i) { 2160b57cec5SDimitry Andric StringRef Val = A->getValue(i); 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric bool IsDisabled = Val.startswith(DisabledPrefixIn); 2190b57cec5SDimitry Andric // Ignore the disablement token for string matching. 2200b57cec5SDimitry Andric if (IsDisabled) 2210b57cec5SDimitry Andric Val = Val.substr(1); 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric size_t RefStep; 2240b57cec5SDimitry Andric if (!getRefinementStep(Val, D, *A, RefStep)) 2250b57cec5SDimitry Andric return; 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric StringRef ValBase = Val.slice(0, RefStep); 2280b57cec5SDimitry Andric llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase); 2290b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2300b57cec5SDimitry Andric // Try again specifying float suffix. 2310b57cec5SDimitry Andric OptionIter = OptionStrings.find(ValBase.str() + 'f'); 2320b57cec5SDimitry Andric if (OptionIter == OptionStrings.end()) { 2330b57cec5SDimitry Andric // The input name did not match any known option string. 2340b57cec5SDimitry Andric D.Diag(diag::err_drv_unknown_argument) << Val; 2350b57cec5SDimitry Andric return; 2360b57cec5SDimitry Andric } 23781ad6265SDimitry Andric // The option was specified without a half or float or double suffix. 23881ad6265SDimitry Andric // Make sure that the double or half entry was not already specified. 2390b57cec5SDimitry Andric // The float entry will be checked below. 24081ad6265SDimitry Andric if (OptionStrings[ValBase.str() + 'd'] || 24181ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h']) { 2420b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2430b57cec5SDimitry Andric return; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric if (OptionIter->second == true) { 2480b57cec5SDimitry Andric // Duplicate option specified. 2490b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val; 2500b57cec5SDimitry Andric return; 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // Mark the matched option as found. Do not allow duplicate specifiers. 2540b57cec5SDimitry Andric OptionIter->second = true; 2550b57cec5SDimitry Andric 25681ad6265SDimitry Andric // If the precision was not specified, also mark the double and half entry 25781ad6265SDimitry Andric // as found. 25881ad6265SDimitry Andric if (ValBase.back() != 'f' && ValBase.back() != 'd' && ValBase.back() != 'h') { 2590b57cec5SDimitry Andric OptionStrings[ValBase.str() + 'd'] = true; 26081ad6265SDimitry Andric OptionStrings[ValBase.str() + 'h'] = true; 26181ad6265SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // Build the output string. 2640b57cec5SDimitry Andric StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut; 2650b57cec5SDimitry Andric Out = Args.MakeArgString(Out + Prefix + Val); 2660b57cec5SDimitry Andric if (i != NumOptions - 1) 2670b57cec5SDimitry Andric Out = Args.MakeArgString(Out + ","); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric OutStrings.push_back(Args.MakeArgString(Out)); 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric /// The -mprefer-vector-width option accepts either a positive integer 2740b57cec5SDimitry Andric /// or the string "none". 2750b57cec5SDimitry Andric static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args, 2760b57cec5SDimitry Andric ArgStringList &CmdArgs) { 2770b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ); 2780b57cec5SDimitry Andric if (!A) 2790b57cec5SDimitry Andric return; 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric StringRef Value = A->getValue(); 2820b57cec5SDimitry Andric if (Value == "none") { 2830b57cec5SDimitry Andric CmdArgs.push_back("-mprefer-vector-width=none"); 2840b57cec5SDimitry Andric } else { 2850b57cec5SDimitry Andric unsigned Width; 2860b57cec5SDimitry Andric if (Value.getAsInteger(10, Width)) { 2870b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 2880b57cec5SDimitry Andric return; 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value)); 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 294480093f4SDimitry Andric static void getWebAssemblyTargetFeatures(const ArgList &Args, 295480093f4SDimitry Andric std::vector<StringRef> &Features) { 296480093f4SDimitry Andric handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group); 297480093f4SDimitry Andric } 298480093f4SDimitry Andric 2995ffd83dbSDimitry Andric static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, 300480093f4SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3015ffd83dbSDimitry Andric bool ForAS, bool IsAux = false) { 302480093f4SDimitry Andric std::vector<StringRef> Features; 303480093f4SDimitry Andric switch (Triple.getArch()) { 304480093f4SDimitry Andric default: 305480093f4SDimitry Andric break; 306480093f4SDimitry Andric case llvm::Triple::mips: 307480093f4SDimitry Andric case llvm::Triple::mipsel: 308480093f4SDimitry Andric case llvm::Triple::mips64: 309480093f4SDimitry Andric case llvm::Triple::mips64el: 310480093f4SDimitry Andric mips::getMIPSTargetFeatures(D, Triple, Args, Features); 311480093f4SDimitry Andric break; 312480093f4SDimitry Andric 313480093f4SDimitry Andric case llvm::Triple::arm: 314480093f4SDimitry Andric case llvm::Triple::armeb: 315480093f4SDimitry Andric case llvm::Triple::thumb: 316480093f4SDimitry Andric case llvm::Triple::thumbeb: 31781ad6265SDimitry Andric arm::getARMTargetFeatures(D, Triple, Args, Features, ForAS); 318480093f4SDimitry Andric break; 319480093f4SDimitry Andric 320480093f4SDimitry Andric case llvm::Triple::ppc: 321e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 322480093f4SDimitry Andric case llvm::Triple::ppc64: 323480093f4SDimitry Andric case llvm::Triple::ppc64le: 324480093f4SDimitry Andric ppc::getPPCTargetFeatures(D, Triple, Args, Features); 325480093f4SDimitry Andric break; 326480093f4SDimitry Andric case llvm::Triple::riscv32: 327480093f4SDimitry Andric case llvm::Triple::riscv64: 328480093f4SDimitry Andric riscv::getRISCVTargetFeatures(D, Triple, Args, Features); 329480093f4SDimitry Andric break; 330480093f4SDimitry Andric case llvm::Triple::systemz: 3315ffd83dbSDimitry Andric systemz::getSystemZTargetFeatures(D, Args, Features); 332480093f4SDimitry Andric break; 333480093f4SDimitry Andric case llvm::Triple::aarch64: 334480093f4SDimitry Andric case llvm::Triple::aarch64_32: 335480093f4SDimitry Andric case llvm::Triple::aarch64_be: 33681ad6265SDimitry Andric aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS); 337480093f4SDimitry Andric break; 338480093f4SDimitry Andric case llvm::Triple::x86: 339480093f4SDimitry Andric case llvm::Triple::x86_64: 340480093f4SDimitry Andric x86::getX86TargetFeatures(D, Triple, Args, Features); 341480093f4SDimitry Andric break; 342480093f4SDimitry Andric case llvm::Triple::hexagon: 343480093f4SDimitry Andric hexagon::getHexagonTargetFeatures(D, Args, Features); 344480093f4SDimitry Andric break; 345480093f4SDimitry Andric case llvm::Triple::wasm32: 346480093f4SDimitry Andric case llvm::Triple::wasm64: 347480093f4SDimitry Andric getWebAssemblyTargetFeatures(Args, Features); 348480093f4SDimitry Andric break; 349480093f4SDimitry Andric case llvm::Triple::sparc: 350480093f4SDimitry Andric case llvm::Triple::sparcel: 351480093f4SDimitry Andric case llvm::Triple::sparcv9: 352480093f4SDimitry Andric sparc::getSparcTargetFeatures(D, Args, Features); 353480093f4SDimitry Andric break; 354480093f4SDimitry Andric case llvm::Triple::r600: 355480093f4SDimitry Andric case llvm::Triple::amdgcn: 356e8d8bef9SDimitry Andric amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features); 357480093f4SDimitry Andric break; 35881ad6265SDimitry Andric case llvm::Triple::nvptx: 35981ad6265SDimitry Andric case llvm::Triple::nvptx64: 36081ad6265SDimitry Andric NVPTX::getNVPTXTargetFeatures(D, Triple, Args, Features); 36181ad6265SDimitry Andric break; 362fe6060f1SDimitry Andric case llvm::Triple::m68k: 363fe6060f1SDimitry Andric m68k::getM68kTargetFeatures(D, Triple, Args, Features); 364fe6060f1SDimitry Andric break; 365480093f4SDimitry Andric case llvm::Triple::msp430: 366480093f4SDimitry Andric msp430::getMSP430TargetFeatures(D, Args, Features); 3675ffd83dbSDimitry Andric break; 3685ffd83dbSDimitry Andric case llvm::Triple::ve: 3695ffd83dbSDimitry Andric ve::getVETargetFeatures(D, Args, Features); 370fe6060f1SDimitry Andric break; 37181ad6265SDimitry Andric case llvm::Triple::csky: 37281ad6265SDimitry Andric csky::getCSKYTargetFeatures(D, Triple, Args, CmdArgs, Features); 37381ad6265SDimitry Andric break; 374480093f4SDimitry Andric } 375480093f4SDimitry Andric 3765ffd83dbSDimitry Andric for (auto Feature : unifyTargetFeatures(Features)) { 3775ffd83dbSDimitry Andric CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature"); 3785ffd83dbSDimitry Andric CmdArgs.push_back(Feature.data()); 379480093f4SDimitry Andric } 380480093f4SDimitry Andric } 381480093f4SDimitry Andric 3820b57cec5SDimitry Andric static bool 3830b57cec5SDimitry Andric shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 3840b57cec5SDimitry Andric const llvm::Triple &Triple) { 3850b57cec5SDimitry Andric // We use the zero-cost exception tables for Objective-C if the non-fragile 3860b57cec5SDimitry Andric // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 3870b57cec5SDimitry Andric // later. 3880b57cec5SDimitry Andric if (runtime.isNonFragile()) 3890b57cec5SDimitry Andric return true; 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric if (!Triple.isMacOSX()) 3920b57cec5SDimitry Andric return false; 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric return (!Triple.isMacOSXVersionLT(10, 5) && 3950b57cec5SDimitry Andric (Triple.getArch() == llvm::Triple::x86_64 || 3960b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::arm)); 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric /// Adds exception related arguments to the driver command arguments. There's a 4004824e7fdSDimitry Andric /// main flag, -fexceptions and also language specific flags to enable/disable 4010b57cec5SDimitry Andric /// C++ and Objective-C exceptions. This makes it possible to for example 4020b57cec5SDimitry Andric /// disable C++ exceptions but enable Objective-C exceptions. 403fe6060f1SDimitry Andric static bool addExceptionArgs(const ArgList &Args, types::ID InputType, 4040b57cec5SDimitry Andric const ToolChain &TC, bool KernelOrKext, 4050b57cec5SDimitry Andric const ObjCRuntime &objcRuntime, 4060b57cec5SDimitry Andric ArgStringList &CmdArgs) { 4070b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric if (KernelOrKext) { 4100b57cec5SDimitry Andric // -mkernel and -fapple-kext imply no exceptions, so claim exception related 4110b57cec5SDimitry Andric // arguments now to avoid warnings about unused arguments. 4120b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fexceptions); 4130b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_exceptions); 4140b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 4150b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 4160b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 4170b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 418fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fasync_exceptions); 419fe6060f1SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_async_exceptions); 420fe6060f1SDimitry Andric return false; 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric // See if the user explicitly enabled exceptions. 4240b57cec5SDimitry Andric bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, 4250b57cec5SDimitry Andric false); 4260b57cec5SDimitry Andric 427fe6060f1SDimitry Andric bool EHa = Args.hasFlag(options::OPT_fasync_exceptions, 428fe6060f1SDimitry Andric options::OPT_fno_async_exceptions, false); 429fe6060f1SDimitry Andric if (EHa) { 430fe6060f1SDimitry Andric CmdArgs.push_back("-fasync-exceptions"); 431fe6060f1SDimitry Andric EH = true; 432fe6060f1SDimitry Andric } 433fe6060f1SDimitry Andric 4340b57cec5SDimitry Andric // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 4350b57cec5SDimitry Andric // is not necessarily sensible, but follows GCC. 4360b57cec5SDimitry Andric if (types::isObjC(InputType) && 4370b57cec5SDimitry Andric Args.hasFlag(options::OPT_fobjc_exceptions, 4380b57cec5SDimitry Andric options::OPT_fno_objc_exceptions, true)) { 4390b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-exceptions"); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric if (types::isCXX(InputType)) { 44581ad6265SDimitry Andric // Disable C++ EH by default on XCore and PS4/PS5. 44681ad6265SDimitry Andric bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore && 44781ad6265SDimitry Andric !Triple.isPS() && !Triple.isDriverKit(); 4480b57cec5SDimitry Andric Arg *ExceptionArg = Args.getLastArg( 4490b57cec5SDimitry Andric options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 4500b57cec5SDimitry Andric options::OPT_fexceptions, options::OPT_fno_exceptions); 4510b57cec5SDimitry Andric if (ExceptionArg) 4520b57cec5SDimitry Andric CXXExceptionsEnabled = 4530b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) || 4540b57cec5SDimitry Andric ExceptionArg->getOption().matches(options::OPT_fexceptions); 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric if (CXXExceptionsEnabled) { 4570b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric EH = true; 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric 4635ffd83dbSDimitry Andric // OPT_fignore_exceptions means exception could still be thrown, 4645ffd83dbSDimitry Andric // but no clean up or catch would happen in current module. 4655ffd83dbSDimitry Andric // So we do not set EH to false. 4665ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fignore_exceptions); 4675ffd83dbSDimitry Andric 4680b57cec5SDimitry Andric if (EH) 4690b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 470fe6060f1SDimitry Andric return EH; 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric 473480093f4SDimitry Andric static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, 474480093f4SDimitry Andric const JobAction &JA) { 4750b57cec5SDimitry Andric bool Default = true; 4760b57cec5SDimitry Andric if (TC.getTriple().isOSDarwin()) { 4770b57cec5SDimitry Andric // The native darwin assembler doesn't support the linker_option directives, 4780b57cec5SDimitry Andric // so we disable them if we think the .s file will be passed to it. 4790b57cec5SDimitry Andric Default = TC.useIntegratedAs(); 4800b57cec5SDimitry Andric } 481480093f4SDimitry Andric // The linker_option directives are intended for host compilation. 482480093f4SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 483480093f4SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 484480093f4SDimitry Andric Default = false; 485480093f4SDimitry Andric return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 4860b57cec5SDimitry Andric Default); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases 4900b57cec5SDimitry Andric // to the corresponding DebugInfoKind. 4910b57cec5SDimitry Andric static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) { 4920b57cec5SDimitry Andric assert(A.getOption().matches(options::OPT_gN_Group) && 4930b57cec5SDimitry Andric "Not a -g option that specifies a debug-info level"); 4940b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_g0) || 4950b57cec5SDimitry Andric A.getOption().matches(options::OPT_ggdb0)) 4960b57cec5SDimitry Andric return codegenoptions::NoDebugInfo; 4970b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_gline_tables_only) || 4980b57cec5SDimitry Andric A.getOption().matches(options::OPT_ggdb1)) 4990b57cec5SDimitry Andric return codegenoptions::DebugLineTablesOnly; 5000b57cec5SDimitry Andric if (A.getOption().matches(options::OPT_gline_directives_only)) 5010b57cec5SDimitry Andric return codegenoptions::DebugDirectivesOnly; 502fe6060f1SDimitry Andric return codegenoptions::DebugInfoConstructor; 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) { 5060b57cec5SDimitry Andric switch (Triple.getArch()){ 5070b57cec5SDimitry Andric default: 5080b57cec5SDimitry Andric return false; 5090b57cec5SDimitry Andric case llvm::Triple::arm: 5100b57cec5SDimitry Andric case llvm::Triple::thumb: 5110b57cec5SDimitry Andric // ARM Darwin targets require a frame pointer to be always present to aid 5120b57cec5SDimitry Andric // offline debugging via backtraces. 5130b57cec5SDimitry Andric return Triple.isOSDarwin(); 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric static bool useFramePointerForTargetByDefault(const ArgList &Args, 5180b57cec5SDimitry Andric const llvm::Triple &Triple) { 5195ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry)) 520a7dea167SDimitry Andric return true; 521a7dea167SDimitry Andric 5220b57cec5SDimitry Andric switch (Triple.getArch()) { 5230b57cec5SDimitry Andric case llvm::Triple::xcore: 5240b57cec5SDimitry Andric case llvm::Triple::wasm32: 5250b57cec5SDimitry Andric case llvm::Triple::wasm64: 5260b57cec5SDimitry Andric case llvm::Triple::msp430: 5270b57cec5SDimitry Andric // XCore never wants frame pointers, regardless of OS. 5280b57cec5SDimitry Andric // WebAssembly never wants frame pointers. 5290b57cec5SDimitry Andric return false; 5300b57cec5SDimitry Andric case llvm::Triple::ppc: 531e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 5320b57cec5SDimitry Andric case llvm::Triple::ppc64: 5330b57cec5SDimitry Andric case llvm::Triple::ppc64le: 5340b57cec5SDimitry Andric case llvm::Triple::riscv32: 5350b57cec5SDimitry Andric case llvm::Triple::riscv64: 536480093f4SDimitry Andric case llvm::Triple::amdgcn: 537480093f4SDimitry Andric case llvm::Triple::r600: 53881ad6265SDimitry Andric case llvm::Triple::csky: 5390b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5400b57cec5SDimitry Andric default: 5410b57cec5SDimitry Andric break; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 544753f127fSDimitry Andric if (Triple.isOSFuchsia() || Triple.isOSNetBSD()) { 5450b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric if (Triple.isOSLinux() || Triple.getOS() == llvm::Triple::CloudABI || 5490b57cec5SDimitry Andric Triple.isOSHurd()) { 5500b57cec5SDimitry Andric switch (Triple.getArch()) { 5510b57cec5SDimitry Andric // Don't use a frame pointer on linux if optimizing for certain targets. 5525ffd83dbSDimitry Andric case llvm::Triple::arm: 5535ffd83dbSDimitry Andric case llvm::Triple::armeb: 5545ffd83dbSDimitry Andric case llvm::Triple::thumb: 5555ffd83dbSDimitry Andric case llvm::Triple::thumbeb: 5565ffd83dbSDimitry Andric if (Triple.isAndroid()) 5575ffd83dbSDimitry Andric return true; 5585ffd83dbSDimitry Andric LLVM_FALLTHROUGH; 5590b57cec5SDimitry Andric case llvm::Triple::mips64: 5600b57cec5SDimitry Andric case llvm::Triple::mips64el: 5610b57cec5SDimitry Andric case llvm::Triple::mips: 5620b57cec5SDimitry Andric case llvm::Triple::mipsel: 5630b57cec5SDimitry Andric case llvm::Triple::systemz: 5640b57cec5SDimitry Andric case llvm::Triple::x86: 5650b57cec5SDimitry Andric case llvm::Triple::x86_64: 5660b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5670b57cec5SDimitry Andric default: 5680b57cec5SDimitry Andric return true; 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric } 5710b57cec5SDimitry Andric 5720b57cec5SDimitry Andric if (Triple.isOSWindows()) { 5730b57cec5SDimitry Andric switch (Triple.getArch()) { 5740b57cec5SDimitry Andric case llvm::Triple::x86: 5750b57cec5SDimitry Andric return !areOptimizationsEnabled(Args); 5760b57cec5SDimitry Andric case llvm::Triple::x86_64: 5770b57cec5SDimitry Andric return Triple.isOSBinFormatMachO(); 5780b57cec5SDimitry Andric case llvm::Triple::arm: 5790b57cec5SDimitry Andric case llvm::Triple::thumb: 5800b57cec5SDimitry Andric // Windows on ARM builds with FPO disabled to aid fast stack walking 5810b57cec5SDimitry Andric return true; 5820b57cec5SDimitry Andric default: 5830b57cec5SDimitry Andric // All other supported Windows ISAs use xdata unwind information, so frame 5840b57cec5SDimitry Andric // pointers are not generally useful. 5850b57cec5SDimitry Andric return false; 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric return true; 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 592a7dea167SDimitry Andric static CodeGenOptions::FramePointerKind 593a7dea167SDimitry Andric getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) { 594a7dea167SDimitry Andric // We have 4 states: 595a7dea167SDimitry Andric // 596a7dea167SDimitry Andric // 00) leaf retained, non-leaf retained 597a7dea167SDimitry Andric // 01) leaf retained, non-leaf omitted (this is invalid) 598a7dea167SDimitry Andric // 10) leaf omitted, non-leaf retained 599a7dea167SDimitry Andric // (what -momit-leaf-frame-pointer was designed for) 600a7dea167SDimitry Andric // 11) leaf omitted, non-leaf omitted 601a7dea167SDimitry Andric // 602a7dea167SDimitry Andric // "omit" options taking precedence over "no-omit" options is the only way 603a7dea167SDimitry Andric // to make 3 valid states representable 604a7dea167SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fomit_frame_pointer, 605a7dea167SDimitry Andric options::OPT_fno_omit_frame_pointer); 606a7dea167SDimitry Andric bool OmitFP = A && A->getOption().matches(options::OPT_fomit_frame_pointer); 607a7dea167SDimitry Andric bool NoOmitFP = 608a7dea167SDimitry Andric A && A->getOption().matches(options::OPT_fno_omit_frame_pointer); 60981ad6265SDimitry Andric bool OmitLeafFP = 61081ad6265SDimitry Andric Args.hasFlag(options::OPT_momit_leaf_frame_pointer, 611480093f4SDimitry Andric options::OPT_mno_omit_leaf_frame_pointer, 61281ad6265SDimitry Andric Triple.isAArch64() || Triple.isPS() || Triple.isVE()); 613a7dea167SDimitry Andric if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) || 614a7dea167SDimitry Andric (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) { 615e8d8bef9SDimitry Andric if (OmitLeafFP) 616a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::NonLeaf; 617a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::All; 6180b57cec5SDimitry Andric } 619a7dea167SDimitry Andric return CodeGenOptions::FramePointerKind::None; 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric /// Add a CC1 option to specify the debug compilation directory. 6230eae32dcSDimitry Andric static const char *addDebugCompDirArg(const ArgList &Args, 6240eae32dcSDimitry Andric ArgStringList &CmdArgs, 625a7dea167SDimitry Andric const llvm::vfs::FileSystem &VFS) { 626fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 627fe6060f1SDimitry Andric options::OPT_fdebug_compilation_dir_EQ)) { 628fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 629fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") + 630fe6060f1SDimitry Andric A->getValue())); 631fe6060f1SDimitry Andric else 632fe6060f1SDimitry Andric A->render(Args, CmdArgs); 633a7dea167SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 634a7dea167SDimitry Andric VFS.getCurrentWorkingDirectory()) { 635fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD)); 6360b57cec5SDimitry Andric } 6370eae32dcSDimitry Andric StringRef Path(CmdArgs.back()); 6380eae32dcSDimitry Andric return Path.substr(Path.find('=') + 1).data(); 6390eae32dcSDimitry Andric } 6400eae32dcSDimitry Andric 6410eae32dcSDimitry Andric static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs, 6420eae32dcSDimitry Andric const char *DebugCompilationDir, 6430eae32dcSDimitry Andric const char *OutputFileName) { 6440eae32dcSDimitry Andric // No need to generate a value for -object-file-name if it was provided. 6450eae32dcSDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xclang)) 6460eae32dcSDimitry Andric if (StringRef(Arg->getValue()).startswith("-object-file-name")) 6470eae32dcSDimitry Andric return; 6480eae32dcSDimitry Andric 6490eae32dcSDimitry Andric if (Args.hasArg(options::OPT_object_file_name_EQ)) 6500eae32dcSDimitry Andric return; 6510eae32dcSDimitry Andric 6520eae32dcSDimitry Andric SmallString<128> ObjFileNameForDebug(OutputFileName); 6530eae32dcSDimitry Andric if (ObjFileNameForDebug != "-" && 6540eae32dcSDimitry Andric !llvm::sys::path::is_absolute(ObjFileNameForDebug) && 6550eae32dcSDimitry Andric (!DebugCompilationDir || 6560eae32dcSDimitry Andric llvm::sys::path::is_absolute(DebugCompilationDir))) { 6570eae32dcSDimitry Andric // Make the path absolute in the debug infos like MSVC does. 6580eae32dcSDimitry Andric llvm::sys::fs::make_absolute(ObjFileNameForDebug); 6590eae32dcSDimitry Andric } 6600eae32dcSDimitry Andric CmdArgs.push_back( 6610eae32dcSDimitry Andric Args.MakeArgString(Twine("-object-file-name=") + ObjFileNameForDebug)); 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric /// Add a CC1 and CC1AS option to specify the debug file path prefix map. 66581ad6265SDimitry Andric static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC, 66681ad6265SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 66781ad6265SDimitry Andric auto AddOneArg = [&](StringRef Map, StringRef Name) { 668349cc55cSDimitry Andric if (!Map.contains('=')) 66981ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) << Map << Name; 6700b57cec5SDimitry Andric else 6710b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); 67281ad6265SDimitry Andric }; 67381ad6265SDimitry Andric 67481ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 67581ad6265SDimitry Andric options::OPT_fdebug_prefix_map_EQ)) { 67681ad6265SDimitry Andric AddOneArg(A->getValue(), A->getOption().getName()); 6770b57cec5SDimitry Andric A->claim(); 6780b57cec5SDimitry Andric } 67981ad6265SDimitry Andric std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping(); 68081ad6265SDimitry Andric if (GlobalRemapEntry.empty()) 68181ad6265SDimitry Andric return; 68281ad6265SDimitry Andric AddOneArg(GlobalRemapEntry, "environment"); 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric 685480093f4SDimitry Andric /// Add a CC1 and CC1AS option to specify the macro file path prefix map. 686480093f4SDimitry Andric static void addMacroPrefixMapArg(const Driver &D, const ArgList &Args, 687480093f4SDimitry Andric ArgStringList &CmdArgs) { 688480093f4SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 689480093f4SDimitry Andric options::OPT_fmacro_prefix_map_EQ)) { 690480093f4SDimitry Andric StringRef Map = A->getValue(); 691349cc55cSDimitry Andric if (!Map.contains('=')) 692480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 693480093f4SDimitry Andric << Map << A->getOption().getName(); 694480093f4SDimitry Andric else 695480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fmacro-prefix-map=" + Map)); 696480093f4SDimitry Andric A->claim(); 697480093f4SDimitry Andric } 698480093f4SDimitry Andric } 699480093f4SDimitry Andric 700e8d8bef9SDimitry Andric /// Add a CC1 and CC1AS option to specify the coverage file path prefix map. 701fe6060f1SDimitry Andric static void addCoveragePrefixMapArg(const Driver &D, const ArgList &Args, 702e8d8bef9SDimitry Andric ArgStringList &CmdArgs) { 703e8d8bef9SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 704fe6060f1SDimitry Andric options::OPT_fcoverage_prefix_map_EQ)) { 705e8d8bef9SDimitry Andric StringRef Map = A->getValue(); 706349cc55cSDimitry Andric if (!Map.contains('=')) 707e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 708e8d8bef9SDimitry Andric << Map << A->getOption().getName(); 709e8d8bef9SDimitry Andric else 710fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-prefix-map=" + Map)); 711e8d8bef9SDimitry Andric A->claim(); 712e8d8bef9SDimitry Andric } 713e8d8bef9SDimitry Andric } 714e8d8bef9SDimitry Andric 7150b57cec5SDimitry Andric /// Vectorize at all optimization levels greater than 1 except for -Oz. 7160b57cec5SDimitry Andric /// For -Oz the loop vectorizer is disabled, while the slp vectorizer is 7170b57cec5SDimitry Andric /// enabled. 7180b57cec5SDimitry Andric static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) { 7190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 7200b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 7210b57cec5SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 7220b57cec5SDimitry Andric return true; 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O0)) 7250b57cec5SDimitry Andric return false; 7260b57cec5SDimitry Andric 7270b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric // Vectorize -Os. 7300b57cec5SDimitry Andric StringRef S(A->getValue()); 7310b57cec5SDimitry Andric if (S == "s") 7320b57cec5SDimitry Andric return true; 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // Don't vectorize -Oz, unless it's the slp vectorizer. 7350b57cec5SDimitry Andric if (S == "z") 7360b57cec5SDimitry Andric return isSlpVec; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric unsigned OptLevel = 0; 7390b57cec5SDimitry Andric if (S.getAsInteger(10, OptLevel)) 7400b57cec5SDimitry Andric return false; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric return OptLevel > 1; 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric return false; 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric /// Add -x lang to \p CmdArgs for \p Input. 7490b57cec5SDimitry Andric static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 7500b57cec5SDimitry Andric ArgStringList &CmdArgs) { 7510b57cec5SDimitry Andric // When using -verify-pch, we don't want to provide the type 7520b57cec5SDimitry Andric // 'precompiled-header' if it was inferred from the file extension 7530b57cec5SDimitry Andric if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH) 7540b57cec5SDimitry Andric return; 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric CmdArgs.push_back("-x"); 7570b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc)) 7580b57cec5SDimitry Andric CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 7590b57cec5SDimitry Andric else { 7600b57cec5SDimitry Andric // Map the driver type to the frontend type. This is mostly an identity 7610b57cec5SDimitry Andric // mapping, except that the distinction between module interface units 7620b57cec5SDimitry Andric // and other source files does not exist at the frontend layer. 7630b57cec5SDimitry Andric const char *ClangType; 7640b57cec5SDimitry Andric switch (Input.getType()) { 7650b57cec5SDimitry Andric case types::TY_CXXModule: 7660b57cec5SDimitry Andric ClangType = "c++"; 7670b57cec5SDimitry Andric break; 7680b57cec5SDimitry Andric case types::TY_PP_CXXModule: 7690b57cec5SDimitry Andric ClangType = "c++-cpp-output"; 7700b57cec5SDimitry Andric break; 7710b57cec5SDimitry Andric default: 7720b57cec5SDimitry Andric ClangType = types::getTypeName(Input.getType()); 7730b57cec5SDimitry Andric break; 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric CmdArgs.push_back(ClangType); 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, 7800b57cec5SDimitry Andric const Driver &D, const InputInfo &Output, 781349cc55cSDimitry Andric const ArgList &Args, SanitizerArgs &SanArgs, 7820b57cec5SDimitry Andric ArgStringList &CmdArgs) { 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, 7850b57cec5SDimitry Andric options::OPT_fprofile_generate_EQ, 7860b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7870b57cec5SDimitry Andric if (PGOGenerateArg && 7880b57cec5SDimitry Andric PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7890b57cec5SDimitry Andric PGOGenerateArg = nullptr; 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate, 7920b57cec5SDimitry Andric options::OPT_fcs_profile_generate_EQ, 7930b57cec5SDimitry Andric options::OPT_fno_profile_generate); 7940b57cec5SDimitry Andric if (CSPGOGenerateArg && 7950b57cec5SDimitry Andric CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) 7960b57cec5SDimitry Andric CSPGOGenerateArg = nullptr; 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric auto *ProfileGenerateArg = Args.getLastArg( 7990b57cec5SDimitry Andric options::OPT_fprofile_instr_generate, 8000b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ, 8010b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate); 8020b57cec5SDimitry Andric if (ProfileGenerateArg && 8030b57cec5SDimitry Andric ProfileGenerateArg->getOption().matches( 8040b57cec5SDimitry Andric options::OPT_fno_profile_instr_generate)) 8050b57cec5SDimitry Andric ProfileGenerateArg = nullptr; 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric if (PGOGenerateArg && ProfileGenerateArg) 8080b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8090b57cec5SDimitry Andric << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling(); 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric auto *ProfileUseArg = getLastProfileUseArg(Args); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric if (PGOGenerateArg && ProfileUseArg) 8140b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8150b57cec5SDimitry Andric << ProfileUseArg->getSpelling() << PGOGenerateArg->getSpelling(); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric if (ProfileGenerateArg && ProfileUseArg) 8180b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8190b57cec5SDimitry Andric << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling(); 8200b57cec5SDimitry Andric 821e8d8bef9SDimitry Andric if (CSPGOGenerateArg && PGOGenerateArg) { 8220b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 8230b57cec5SDimitry Andric << CSPGOGenerateArg->getSpelling() << PGOGenerateArg->getSpelling(); 824e8d8bef9SDimitry Andric PGOGenerateArg = nullptr; 825e8d8bef9SDimitry Andric } 8260b57cec5SDimitry Andric 827fe6060f1SDimitry Andric if (TC.getTriple().isOSAIX()) { 828fe6060f1SDimitry Andric if (ProfileGenerateArg) 829fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 830fe6060f1SDimitry Andric << ProfileGenerateArg->getSpelling() << TC.getTriple().str(); 831fe6060f1SDimitry Andric if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args)) 832fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 833fe6060f1SDimitry Andric << ProfileSampleUseArg->getSpelling() << TC.getTriple().str(); 834fe6060f1SDimitry Andric } 835fe6060f1SDimitry Andric 8360b57cec5SDimitry Andric if (ProfileGenerateArg) { 8370b57cec5SDimitry Andric if (ProfileGenerateArg->getOption().matches( 8380b57cec5SDimitry Andric options::OPT_fprofile_instr_generate_EQ)) 8390b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instrument-path=") + 8400b57cec5SDimitry Andric ProfileGenerateArg->getValue())); 8410b57cec5SDimitry Andric // The default is to use Clang Instrumentation. 8420b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=clang"); 8430b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 8440b57cec5SDimitry Andric // Add dependent lib for clang_rt.profile 8455ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8465ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric } 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric Arg *PGOGenArg = nullptr; 8510b57cec5SDimitry Andric if (PGOGenerateArg) { 8520b57cec5SDimitry Andric assert(!CSPGOGenerateArg); 8530b57cec5SDimitry Andric PGOGenArg = PGOGenerateArg; 8540b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=llvm"); 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric if (CSPGOGenerateArg) { 8570b57cec5SDimitry Andric assert(!PGOGenerateArg); 8580b57cec5SDimitry Andric PGOGenArg = CSPGOGenerateArg; 8590b57cec5SDimitry Andric CmdArgs.push_back("-fprofile-instrument=csllvm"); 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric if (PGOGenArg) { 8620b57cec5SDimitry Andric if (TC.getTriple().isWindowsMSVCEnvironment()) { 8635ffd83dbSDimitry Andric // Add dependent lib for clang_rt.profile 8645ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8655ffd83dbSDimitry Andric "--dependent-lib=" + TC.getCompilerRTBasename(Args, "profile"))); 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric if (PGOGenArg->getOption().matches( 8680b57cec5SDimitry Andric PGOGenerateArg ? options::OPT_fprofile_generate_EQ 8690b57cec5SDimitry Andric : options::OPT_fcs_profile_generate_EQ)) { 8700b57cec5SDimitry Andric SmallString<128> Path(PGOGenArg->getValue()); 8710b57cec5SDimitry Andric llvm::sys::path::append(Path, "default_%m.profraw"); 8720b57cec5SDimitry Andric CmdArgs.push_back( 8730b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-path=") + Path)); 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric if (ProfileUseArg) { 8780b57cec5SDimitry Andric if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ)) 8790b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8800b57cec5SDimitry Andric Twine("-fprofile-instrument-use-path=") + ProfileUseArg->getValue())); 8810b57cec5SDimitry Andric else if ((ProfileUseArg->getOption().matches( 8820b57cec5SDimitry Andric options::OPT_fprofile_use_EQ) || 8830b57cec5SDimitry Andric ProfileUseArg->getOption().matches( 8840b57cec5SDimitry Andric options::OPT_fprofile_instr_use))) { 8850b57cec5SDimitry Andric SmallString<128> Path( 8860b57cec5SDimitry Andric ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); 8870b57cec5SDimitry Andric if (Path.empty() || llvm::sys::fs::is_directory(Path)) 8880b57cec5SDimitry Andric llvm::sys::path::append(Path, "default.profdata"); 8890b57cec5SDimitry Andric CmdArgs.push_back( 8900b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path)); 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric 8945ffd83dbSDimitry Andric bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage, 8955ffd83dbSDimitry Andric options::OPT_fno_test_coverage, false) || 896a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage); 8975ffd83dbSDimitry Andric bool EmitCovData = TC.needsGCovInstrumentation(Args); 898a7dea167SDimitry Andric if (EmitCovNotes) 899e8d8bef9SDimitry Andric CmdArgs.push_back("-ftest-coverage"); 900a7dea167SDimitry Andric if (EmitCovData) 901e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-arcs"); 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoverage_mapping, 9040b57cec5SDimitry Andric options::OPT_fno_coverage_mapping, false)) { 9050b57cec5SDimitry Andric if (!ProfileGenerateArg) 9060b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 9070b57cec5SDimitry Andric << "-fcoverage-mapping" 9080b57cec5SDimitry Andric << "-fprofile-instr-generate"; 9090b57cec5SDimitry Andric 9100b57cec5SDimitry Andric CmdArgs.push_back("-fcoverage-mapping"); 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric 913fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 914fe6060f1SDimitry Andric options::OPT_fcoverage_compilation_dir_EQ)) { 915fe6060f1SDimitry Andric if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ)) 916fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 917fe6060f1SDimitry Andric Twine("-fcoverage-compilation-dir=") + A->getValue())); 918fe6060f1SDimitry Andric else 919fe6060f1SDimitry Andric A->render(Args, CmdArgs); 920fe6060f1SDimitry Andric } else if (llvm::ErrorOr<std::string> CWD = 921fe6060f1SDimitry Andric D.getVFS().getCurrentWorkingDirectory()) { 922fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD)); 923fe6060f1SDimitry Andric } 924fe6060f1SDimitry Andric 9250b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) { 9260b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ); 9270b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 9280b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 9290b57cec5SDimitry Andric << "-fprofile-exclude-files=" 9300b57cec5SDimitry Andric << "--coverage"; 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric StringRef v = Arg->getValue(); 9330b57cec5SDimitry Andric CmdArgs.push_back( 9340b57cec5SDimitry Andric Args.MakeArgString(Twine("-fprofile-exclude-files=" + v))); 9350b57cec5SDimitry Andric } 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fprofile_filter_files_EQ)) { 9380b57cec5SDimitry Andric auto *Arg = Args.getLastArg(options::OPT_fprofile_filter_files_EQ); 9390b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_coverage)) 9400b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_only_allowed_with) 9410b57cec5SDimitry Andric << "-fprofile-filter-files=" 9420b57cec5SDimitry Andric << "--coverage"; 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric StringRef v = Arg->getValue(); 9450b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v))); 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric 948e8d8bef9SDimitry Andric if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) { 949e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 950e8d8bef9SDimitry Andric if (Val == "atomic" || Val == "prefer-atomic") 951e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 952e8d8bef9SDimitry Andric else if (Val != "single") 953e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 954e8d8bef9SDimitry Andric << A->getOption().getName() << Val; 955349cc55cSDimitry Andric } else if (SanArgs.needsTsanRt()) { 956e8d8bef9SDimitry Andric CmdArgs.push_back("-fprofile-update=atomic"); 957e8d8bef9SDimitry Andric } 958e8d8bef9SDimitry Andric 959a7dea167SDimitry Andric // Leave -fprofile-dir= an unused argument unless .gcda emission is 960a7dea167SDimitry Andric // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider 961a7dea167SDimitry Andric // the flag used. There is no -fno-profile-dir, so the user has no 962a7dea167SDimitry Andric // targeted way to suppress the warning. 963a7dea167SDimitry Andric Arg *FProfileDir = nullptr; 964a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fprofile_arcs) || 965a7dea167SDimitry Andric Args.hasArg(options::OPT_coverage)) 966a7dea167SDimitry Andric FProfileDir = Args.getLastArg(options::OPT_fprofile_dir); 967a7dea167SDimitry Andric 968a7dea167SDimitry Andric // Put the .gcno and .gcda files (if needed) next to the object file or 969a7dea167SDimitry Andric // bitcode file in the case of LTO. 970a7dea167SDimitry Andric // FIXME: There should be a simpler way to find the object file for this 971a7dea167SDimitry Andric // input, and this code probably does the wrong thing for commands that 972a7dea167SDimitry Andric // compile and link all at once. 973a7dea167SDimitry Andric if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) && 974a7dea167SDimitry Andric (EmitCovNotes || EmitCovData) && Output.isFilename()) { 9750b57cec5SDimitry Andric SmallString<128> OutputFilename; 976480093f4SDimitry Andric if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_Fo)) 977480093f4SDimitry Andric OutputFilename = FinalOutput->getValue(); 978480093f4SDimitry Andric else if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) 9790b57cec5SDimitry Andric OutputFilename = FinalOutput->getValue(); 9800b57cec5SDimitry Andric else 9810b57cec5SDimitry Andric OutputFilename = llvm::sys::path::filename(Output.getBaseInput()); 9820b57cec5SDimitry Andric SmallString<128> CoverageFilename = OutputFilename; 983a7dea167SDimitry Andric if (llvm::sys::path::is_relative(CoverageFilename)) 984a7dea167SDimitry Andric (void)D.getVFS().makeAbsolute(CoverageFilename); 9850b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcno"); 986a7dea167SDimitry Andric 987a7dea167SDimitry Andric CmdArgs.push_back("-coverage-notes-file"); 9880b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9890b57cec5SDimitry Andric 990a7dea167SDimitry Andric if (EmitCovData) { 991a7dea167SDimitry Andric if (FProfileDir) { 9920b57cec5SDimitry Andric CoverageFilename = FProfileDir->getValue(); 9930b57cec5SDimitry Andric llvm::sys::path::append(CoverageFilename, OutputFilename); 9940b57cec5SDimitry Andric } 9950b57cec5SDimitry Andric llvm::sys::path::replace_extension(CoverageFilename, "gcda"); 996a7dea167SDimitry Andric CmdArgs.push_back("-coverage-data-file"); 9970b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric } 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric /// Check whether the given input tree contains any compilation actions. 10030b57cec5SDimitry Andric static bool ContainsCompileAction(const Action *A) { 10040b57cec5SDimitry Andric if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A)) 10050b57cec5SDimitry Andric return true; 10060b57cec5SDimitry Andric 10070eae32dcSDimitry Andric return llvm::any_of(A->inputs(), ContainsCompileAction); 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric /// Check if -relax-all should be passed to the internal assembler. 10110b57cec5SDimitry Andric /// This is done by default when compiling non-assembler source with -O0. 10120b57cec5SDimitry Andric static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 10130b57cec5SDimitry Andric bool RelaxDefault = true; 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 10160b57cec5SDimitry Andric RelaxDefault = A->getOption().matches(options::OPT_O0); 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric if (RelaxDefault) { 10190b57cec5SDimitry Andric RelaxDefault = false; 10200b57cec5SDimitry Andric for (const auto &Act : C.getActions()) { 10210b57cec5SDimitry Andric if (ContainsCompileAction(Act)) { 10220b57cec5SDimitry Andric RelaxDefault = true; 10230b57cec5SDimitry Andric break; 10240b57cec5SDimitry Andric } 10250b57cec5SDimitry Andric } 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 10290b57cec5SDimitry Andric RelaxDefault); 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric // Extract the integer N from a string spelled "-dwarf-N", returning 0 10330b57cec5SDimitry Andric // on mismatch. The StringRef input (rather than an Arg) allows 10340b57cec5SDimitry Andric // for use by the "-Xassembler" option parser. 10350b57cec5SDimitry Andric static unsigned DwarfVersionNum(StringRef ArgValue) { 10360b57cec5SDimitry Andric return llvm::StringSwitch<unsigned>(ArgValue) 10370b57cec5SDimitry Andric .Case("-gdwarf-2", 2) 10380b57cec5SDimitry Andric .Case("-gdwarf-3", 3) 10390b57cec5SDimitry Andric .Case("-gdwarf-4", 4) 10400b57cec5SDimitry Andric .Case("-gdwarf-5", 5) 10410b57cec5SDimitry Andric .Default(0); 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric 1044fe6060f1SDimitry Andric // Find a DWARF format version option. 1045fe6060f1SDimitry Andric // This function is a complementary for DwarfVersionNum(). 1046fe6060f1SDimitry Andric static const Arg *getDwarfNArg(const ArgList &Args) { 1047fe6060f1SDimitry Andric return Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3, 1048fe6060f1SDimitry Andric options::OPT_gdwarf_4, options::OPT_gdwarf_5, 1049fe6060f1SDimitry Andric options::OPT_gdwarf); 1050fe6060f1SDimitry Andric } 1051fe6060f1SDimitry Andric 10520b57cec5SDimitry Andric static void RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs, 10530b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind, 10540b57cec5SDimitry Andric unsigned DwarfVersion, 10550b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning) { 10560b57cec5SDimitry Andric switch (DebugInfoKind) { 10570b57cec5SDimitry Andric case codegenoptions::DebugDirectivesOnly: 10580b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=line-directives-only"); 10590b57cec5SDimitry Andric break; 10600b57cec5SDimitry Andric case codegenoptions::DebugLineTablesOnly: 10610b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=line-tables-only"); 10620b57cec5SDimitry Andric break; 1063480093f4SDimitry Andric case codegenoptions::DebugInfoConstructor: 1064480093f4SDimitry Andric CmdArgs.push_back("-debug-info-kind=constructor"); 1065480093f4SDimitry Andric break; 10660b57cec5SDimitry Andric case codegenoptions::LimitedDebugInfo: 10670b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=limited"); 10680b57cec5SDimitry Andric break; 10690b57cec5SDimitry Andric case codegenoptions::FullDebugInfo: 10700b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-kind=standalone"); 10710b57cec5SDimitry Andric break; 1072e8d8bef9SDimitry Andric case codegenoptions::UnusedTypeInfo: 1073e8d8bef9SDimitry Andric CmdArgs.push_back("-debug-info-kind=unused-types"); 1074e8d8bef9SDimitry Andric break; 10750b57cec5SDimitry Andric default: 10760b57cec5SDimitry Andric break; 10770b57cec5SDimitry Andric } 10780b57cec5SDimitry Andric if (DwarfVersion > 0) 10790b57cec5SDimitry Andric CmdArgs.push_back( 10800b57cec5SDimitry Andric Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); 10810b57cec5SDimitry Andric switch (DebuggerTuning) { 10820b57cec5SDimitry Andric case llvm::DebuggerKind::GDB: 10830b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=gdb"); 10840b57cec5SDimitry Andric break; 10850b57cec5SDimitry Andric case llvm::DebuggerKind::LLDB: 10860b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=lldb"); 10870b57cec5SDimitry Andric break; 10880b57cec5SDimitry Andric case llvm::DebuggerKind::SCE: 10890b57cec5SDimitry Andric CmdArgs.push_back("-debugger-tuning=sce"); 10900b57cec5SDimitry Andric break; 1091fe6060f1SDimitry Andric case llvm::DebuggerKind::DBX: 1092fe6060f1SDimitry Andric CmdArgs.push_back("-debugger-tuning=dbx"); 1093fe6060f1SDimitry Andric break; 10940b57cec5SDimitry Andric default: 10950b57cec5SDimitry Andric break; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric static bool checkDebugInfoOption(const Arg *A, const ArgList &Args, 11000b57cec5SDimitry Andric const Driver &D, const ToolChain &TC) { 11010b57cec5SDimitry Andric assert(A && "Expected non-nullptr argument."); 11020b57cec5SDimitry Andric if (TC.supportsDebugInfoOption(A)) 11030b57cec5SDimitry Andric return true; 11040b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target) 11050b57cec5SDimitry Andric << A->getAsString(Args) << TC.getTripleString(); 11060b57cec5SDimitry Andric return false; 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric static void RenderDebugInfoCompressionArgs(const ArgList &Args, 11100b57cec5SDimitry Andric ArgStringList &CmdArgs, 11110b57cec5SDimitry Andric const Driver &D, 11120b57cec5SDimitry Andric const ToolChain &TC) { 1113e8d8bef9SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gz_EQ); 11140b57cec5SDimitry Andric if (!A) 11150b57cec5SDimitry Andric return; 11160b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 11170b57cec5SDimitry Andric StringRef Value = A->getValue(); 11180b57cec5SDimitry Andric if (Value == "none") { 11190b57cec5SDimitry Andric CmdArgs.push_back("--compress-debug-sections=none"); 112004eeddc0SDimitry Andric } else if (Value == "zlib") { 1121753f127fSDimitry Andric if (llvm::compression::zlib::isAvailable()) { 11220b57cec5SDimitry Andric CmdArgs.push_back( 11230b57cec5SDimitry Andric Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 11240b57cec5SDimitry Andric } else { 11250b57cec5SDimitry Andric D.Diag(diag::warn_debug_compression_unavailable); 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric } else { 11280b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 11290b57cec5SDimitry Andric << A->getOption().getName() << Value; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric } 11330b57cec5SDimitry Andric 11340b57cec5SDimitry Andric static const char *RelocationModelName(llvm::Reloc::Model Model) { 11350b57cec5SDimitry Andric switch (Model) { 11360b57cec5SDimitry Andric case llvm::Reloc::Static: 11370b57cec5SDimitry Andric return "static"; 11380b57cec5SDimitry Andric case llvm::Reloc::PIC_: 11390b57cec5SDimitry Andric return "pic"; 11400b57cec5SDimitry Andric case llvm::Reloc::DynamicNoPIC: 11410b57cec5SDimitry Andric return "dynamic-no-pic"; 11420b57cec5SDimitry Andric case llvm::Reloc::ROPI: 11430b57cec5SDimitry Andric return "ropi"; 11440b57cec5SDimitry Andric case llvm::Reloc::RWPI: 11450b57cec5SDimitry Andric return "rwpi"; 11460b57cec5SDimitry Andric case llvm::Reloc::ROPI_RWPI: 11470b57cec5SDimitry Andric return "ropi-rwpi"; 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric llvm_unreachable("Unknown Reloc::Model kind"); 11500b57cec5SDimitry Andric } 1151e8d8bef9SDimitry Andric static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, 1152e8d8bef9SDimitry Andric const ArgList &Args, 115381ad6265SDimitry Andric ArgStringList &CmdArgs, 115481ad6265SDimitry Andric bool IsCC1As = false) { 1155fe6060f1SDimitry Andric // If no version was requested by the user, use the default value from the 1156fe6060f1SDimitry Andric // back end. This is consistent with the value returned from 1157fe6060f1SDimitry Andric // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without 1158fe6060f1SDimitry Andric // requiring the corresponding llvm to have the AMDGPU target enabled, 1159fe6060f1SDimitry Andric // provided the user (e.g. front end tests) can use the default. 1160fe6060f1SDimitry Andric if (haveAMDGPUCodeObjectVersionArgument(D, Args)) { 1161fe6060f1SDimitry Andric unsigned CodeObjVer = getAMDGPUCodeObjectVersion(D, Args); 1162e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 1163e8d8bef9SDimitry Andric Args.MakeArgString(Twine("--amdhsa-code-object-version=") + 1164e8d8bef9SDimitry Andric Twine(CodeObjVer))); 1165e8d8bef9SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); 116681ad6265SDimitry Andric // -cc1as does not accept -mcode-object-version option. 116781ad6265SDimitry Andric if (!IsCC1As) 116881ad6265SDimitry Andric CmdArgs.insert(CmdArgs.begin() + 1, 116981ad6265SDimitry Andric Args.MakeArgString(Twine("-mcode-object-version=") + 117081ad6265SDimitry Andric Twine(CodeObjVer))); 1171e8d8bef9SDimitry Andric } 1172fe6060f1SDimitry Andric } 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, 11750b57cec5SDimitry Andric const Driver &D, const ArgList &Args, 11760b57cec5SDimitry Andric ArgStringList &CmdArgs, 11770b57cec5SDimitry Andric const InputInfo &Output, 11780b57cec5SDimitry Andric const InputInfoList &Inputs) const { 11790b57cec5SDimitry Andric const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU(); 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric CheckPreprocessingOptions(D, Args); 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_C); 11840b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_CC); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric // Handle dependency file generation. 1187a7dea167SDimitry Andric Arg *ArgM = Args.getLastArg(options::OPT_MM); 1188a7dea167SDimitry Andric if (!ArgM) 1189a7dea167SDimitry Andric ArgM = Args.getLastArg(options::OPT_M); 1190a7dea167SDimitry Andric Arg *ArgMD = Args.getLastArg(options::OPT_MMD); 1191a7dea167SDimitry Andric if (!ArgMD) 1192a7dea167SDimitry Andric ArgMD = Args.getLastArg(options::OPT_MD); 1193a7dea167SDimitry Andric 1194a7dea167SDimitry Andric // -M and -MM imply -w. 1195a7dea167SDimitry Andric if (ArgM) 1196a7dea167SDimitry Andric CmdArgs.push_back("-w"); 1197a7dea167SDimitry Andric else 1198a7dea167SDimitry Andric ArgM = ArgMD; 1199a7dea167SDimitry Andric 1200a7dea167SDimitry Andric if (ArgM) { 12010b57cec5SDimitry Andric // Determine the output location. 12020b57cec5SDimitry Andric const char *DepFile; 12030b57cec5SDimitry Andric if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 12040b57cec5SDimitry Andric DepFile = MF->getValue(); 12050b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 12060b57cec5SDimitry Andric } else if (Output.getType() == types::TY_Dependencies) { 12070b57cec5SDimitry Andric DepFile = Output.getFilename(); 1208a7dea167SDimitry Andric } else if (!ArgMD) { 12090b57cec5SDimitry Andric DepFile = "-"; 12100b57cec5SDimitry Andric } else { 12110b57cec5SDimitry Andric DepFile = getDependencyFileName(Args, Inputs); 12120b57cec5SDimitry Andric C.addFailureResultFile(DepFile, &JA); 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric CmdArgs.push_back("-dependency-file"); 12150b57cec5SDimitry Andric CmdArgs.push_back(DepFile); 12160b57cec5SDimitry Andric 1217a7dea167SDimitry Andric bool HasTarget = false; 1218a7dea167SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) { 1219a7dea167SDimitry Andric HasTarget = true; 1220a7dea167SDimitry Andric A->claim(); 1221a7dea167SDimitry Andric if (A->getOption().matches(options::OPT_MT)) { 1222a7dea167SDimitry Andric A->render(Args, CmdArgs); 1223a7dea167SDimitry Andric } else { 1224a7dea167SDimitry Andric CmdArgs.push_back("-MT"); 1225a7dea167SDimitry Andric SmallString<128> Quoted; 1226753f127fSDimitry Andric quoteMakeTarget(A->getValue(), Quoted); 1227a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 1228a7dea167SDimitry Andric } 1229a7dea167SDimitry Andric } 1230a7dea167SDimitry Andric 12310b57cec5SDimitry Andric // Add a default target if one wasn't specified. 1232a7dea167SDimitry Andric if (!HasTarget) { 12330b57cec5SDimitry Andric const char *DepTarget; 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric // If user provided -o, that is the dependency target, except 12360b57cec5SDimitry Andric // when we are only generating a dependency file. 12370b57cec5SDimitry Andric Arg *OutputOpt = Args.getLastArg(options::OPT_o); 12380b57cec5SDimitry Andric if (OutputOpt && Output.getType() != types::TY_Dependencies) { 12390b57cec5SDimitry Andric DepTarget = OutputOpt->getValue(); 12400b57cec5SDimitry Andric } else { 12410b57cec5SDimitry Andric // Otherwise derive from the base input. 12420b57cec5SDimitry Andric // 12430b57cec5SDimitry Andric // FIXME: This should use the computed output file location. 12440b57cec5SDimitry Andric SmallString<128> P(Inputs[0].getBaseInput()); 12450b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "o"); 12460b57cec5SDimitry Andric DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 12470b57cec5SDimitry Andric } 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andric CmdArgs.push_back("-MT"); 12500b57cec5SDimitry Andric SmallString<128> Quoted; 1251753f127fSDimitry Andric quoteMakeTarget(DepTarget, Quoted); 12520b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Quoted)); 12530b57cec5SDimitry Andric } 12540b57cec5SDimitry Andric 1255a7dea167SDimitry Andric if (ArgM->getOption().matches(options::OPT_M) || 1256a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MD)) 12570b57cec5SDimitry Andric CmdArgs.push_back("-sys-header-deps"); 12580b57cec5SDimitry Andric if ((isa<PrecompileJobAction>(JA) && 12590b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_module_file_deps)) || 12600b57cec5SDimitry Andric Args.hasArg(options::OPT_fmodule_file_deps)) 12610b57cec5SDimitry Andric CmdArgs.push_back("-module-file-deps"); 12620b57cec5SDimitry Andric } 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric if (Args.hasArg(options::OPT_MG)) { 1265a7dea167SDimitry Andric if (!ArgM || ArgM->getOption().matches(options::OPT_MD) || 1266a7dea167SDimitry Andric ArgM->getOption().matches(options::OPT_MMD)) 12670b57cec5SDimitry Andric D.Diag(diag::err_drv_mg_requires_m_or_mm); 12680b57cec5SDimitry Andric CmdArgs.push_back("-MG"); 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MP); 12720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_MV); 12730b57cec5SDimitry Andric 12745ffd83dbSDimitry Andric // Add offload include arguments specific for CUDA/HIP. This must happen 12755ffd83dbSDimitry Andric // before we -I or -include anything else, because we must pick up the 12765ffd83dbSDimitry Andric // CUDA/HIP headers from the particular CUDA/ROCm installation, rather than 12775ffd83dbSDimitry Andric // from e.g. /usr/local/include. 12780b57cec5SDimitry Andric if (JA.isOffloading(Action::OFK_Cuda)) 12790b57cec5SDimitry Andric getToolChain().AddCudaIncludeArgs(Args, CmdArgs); 12805ffd83dbSDimitry Andric if (JA.isOffloading(Action::OFK_HIP)) 12815ffd83dbSDimitry Andric getToolChain().AddHIPIncludeArgs(Args, CmdArgs); 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric // If we are offloading to a target via OpenMP we need to include the 12840b57cec5SDimitry Andric // openmp_wrappers folder which contains alternative system headers. 12850b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_OpenMP) && 128681ad6265SDimitry Andric !Args.hasArg(options::OPT_nostdinc) && 128769ade1e0SDimitry Andric (getToolChain().getTriple().isNVPTX() || 128869ade1e0SDimitry Andric getToolChain().getTriple().isAMDGCN())) { 12890b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_nobuiltininc)) { 12900b57cec5SDimitry Andric // Add openmp_wrappers/* to our system include path. This lets us wrap 12910b57cec5SDimitry Andric // standard library headers. 12920b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 12930b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 12940b57cec5SDimitry Andric llvm::sys::path::append(P, "openmp_wrappers"); 12950b57cec5SDimitry Andric CmdArgs.push_back("-internal-isystem"); 12960b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 12970b57cec5SDimitry Andric } 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric CmdArgs.push_back("-include"); 13005ffd83dbSDimitry Andric CmdArgs.push_back("__clang_openmp_device_functions.h"); 13010b57cec5SDimitry Andric } 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric // Add -i* options, and automatically translate to 13040b57cec5SDimitry Andric // -include-pch/-include-pth for transparent PCH support. It's 13050b57cec5SDimitry Andric // wonky, but we include looking for .gch so we can support seamless 13060b57cec5SDimitry Andric // replacement into a build system already set up to be generating 13070b57cec5SDimitry Andric // .gch files. 13080b57cec5SDimitry Andric 13090b57cec5SDimitry Andric if (getToolChain().getDriver().IsCLMode()) { 13100b57cec5SDimitry Andric const Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); 13110b57cec5SDimitry Andric const Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); 13120b57cec5SDimitry Andric if (YcArg && JA.getKind() >= Action::PrecompileJobClass && 13130b57cec5SDimitry Andric JA.getKind() <= Action::AssembleJobClass) { 13140b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-building-pch-with-obj")); 1315f91b0c1cSDimitry Andric // -fpch-instantiate-templates is the default when creating 1316f91b0c1cSDimitry Andric // precomp using /Yc 1317f91b0c1cSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 1318f91b0c1cSDimitry Andric options::OPT_fno_pch_instantiate_templates, true)) 13195ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates")); 13200b57cec5SDimitry Andric } 13210b57cec5SDimitry Andric if (YcArg || YuArg) { 13220b57cec5SDimitry Andric StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue(); 13230b57cec5SDimitry Andric if (!isa<PrecompileJobAction>(JA)) { 13240b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 13250b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath( 13260b57cec5SDimitry Andric C, !ThroughHeader.empty() 13270b57cec5SDimitry Andric ? ThroughHeader 13280b57cec5SDimitry Andric : llvm::sys::path::filename(Inputs[0].getBaseInput())))); 13290b57cec5SDimitry Andric } 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andric if (ThroughHeader.empty()) { 13320b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 13330b57cec5SDimitry Andric Twine("-pch-through-hdrstop-") + (YcArg ? "create" : "use"))); 13340b57cec5SDimitry Andric } else { 13350b57cec5SDimitry Andric CmdArgs.push_back( 13360b57cec5SDimitry Andric Args.MakeArgString(Twine("-pch-through-header=") + ThroughHeader)); 13370b57cec5SDimitry Andric } 13380b57cec5SDimitry Andric } 13390b57cec5SDimitry Andric } 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric bool RenderedImplicitInclude = false; 13420b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) { 134381ad6265SDimitry Andric if (A->getOption().matches(options::OPT_include) && 134481ad6265SDimitry Andric D.getProbePrecompiled()) { 13450b57cec5SDimitry Andric // Handling of gcc-style gch precompiled headers. 13460b57cec5SDimitry Andric bool IsFirstImplicitInclude = !RenderedImplicitInclude; 13470b57cec5SDimitry Andric RenderedImplicitInclude = true; 13480b57cec5SDimitry Andric 13490b57cec5SDimitry Andric bool FoundPCH = false; 13500b57cec5SDimitry Andric SmallString<128> P(A->getValue()); 13510b57cec5SDimitry Andric // We want the files to have a name like foo.h.pch. Add a dummy extension 13520b57cec5SDimitry Andric // so that replace_extension does the right thing. 13530b57cec5SDimitry Andric P += ".dummy"; 13540b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "pch"); 135581ad6265SDimitry Andric if (D.getVFS().exists(P)) 13560b57cec5SDimitry Andric FoundPCH = true; 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric if (!FoundPCH) { 13590b57cec5SDimitry Andric llvm::sys::path::replace_extension(P, "gch"); 136081ad6265SDimitry Andric if (D.getVFS().exists(P)) { 13610b57cec5SDimitry Andric FoundPCH = true; 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric } 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric if (FoundPCH) { 13660b57cec5SDimitry Andric if (IsFirstImplicitInclude) { 13670b57cec5SDimitry Andric A->claim(); 13680b57cec5SDimitry Andric CmdArgs.push_back("-include-pch"); 13690b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(P)); 13700b57cec5SDimitry Andric continue; 13710b57cec5SDimitry Andric } else { 13720b57cec5SDimitry Andric // Ignore the PCH if not first on command line and emit warning. 13730b57cec5SDimitry Andric D.Diag(diag::warn_drv_pch_not_first_include) << P 13740b57cec5SDimitry Andric << A->getAsString(Args); 13750b57cec5SDimitry Andric } 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_isystem_after)) { 13780b57cec5SDimitry Andric // Handling of paths which must come late. These entries are handled by 13790b57cec5SDimitry Andric // the toolchain itself after the resource dir is inserted in the right 13800b57cec5SDimitry Andric // search order. 13810b57cec5SDimitry Andric // Do not claim the argument so that the use of the argument does not 13820b57cec5SDimitry Andric // silently go unnoticed on toolchains which do not honour the option. 13830b57cec5SDimitry Andric continue; 1384a7dea167SDimitry Andric } else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) { 1385a7dea167SDimitry Andric // Translated to -internal-isystem by the driver, no need to pass to cc1. 1386a7dea167SDimitry Andric continue; 13870b57cec5SDimitry Andric } 13880b57cec5SDimitry Andric 13890b57cec5SDimitry Andric // Not translated, render as usual. 13900b57cec5SDimitry Andric A->claim(); 13910b57cec5SDimitry Andric A->render(Args, CmdArgs); 13920b57cec5SDimitry Andric } 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 13950b57cec5SDimitry Andric {options::OPT_D, options::OPT_U, options::OPT_I_Group, 13960b57cec5SDimitry Andric options::OPT_F, options::OPT_index_header_map}); 13970b57cec5SDimitry Andric 13980b57cec5SDimitry Andric // Add -Wp, and -Xpreprocessor if using the preprocessor. 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric // FIXME: There is a very unfortunate problem here, some troubled 14010b57cec5SDimitry Andric // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 14020b57cec5SDimitry Andric // really support that we would have to parse and then translate 14030b57cec5SDimitry Andric // those options. :( 14040b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 14050b57cec5SDimitry Andric options::OPT_Xpreprocessor); 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric // -I- is a deprecated GCC feature, reject it. 14080b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_I_)) 14090b57cec5SDimitry Andric D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 14120b57cec5SDimitry Andric // -isysroot to the CC1 invocation. 14130b57cec5SDimitry Andric StringRef sysroot = C.getSysRoot(); 14140b57cec5SDimitry Andric if (sysroot != "") { 14150b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_isysroot)) { 14160b57cec5SDimitry Andric CmdArgs.push_back("-isysroot"); 14170b57cec5SDimitry Andric CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 14180b57cec5SDimitry Andric } 14190b57cec5SDimitry Andric } 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andric // Parse additional include paths from environment variables. 14220b57cec5SDimitry Andric // FIXME: We should probably sink the logic for handling these from the 14230b57cec5SDimitry Andric // frontend into the driver. It will allow deleting 4 otherwise unused flags. 14240b57cec5SDimitry Andric // CPATH - included following the user specified includes (but prior to 14250b57cec5SDimitry Andric // builtin and standard includes). 14260b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 14270b57cec5SDimitry Andric // C_INCLUDE_PATH - system includes enabled when compiling C. 14280b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 14290b57cec5SDimitry Andric // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 14300b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 14310b57cec5SDimitry Andric // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 14320b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 14330b57cec5SDimitry Andric // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 14340b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 14350b57cec5SDimitry Andric 14360b57cec5SDimitry Andric // While adding the include arguments, we also attempt to retrieve the 14370b57cec5SDimitry Andric // arguments of related offloading toolchains or arguments that are specific 14380b57cec5SDimitry Andric // of an offloading programming model. 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric // Add C++ include arguments, if needed. 1441a7dea167SDimitry Andric if (types::isCXX(Inputs[0].getType())) { 1442a7dea167SDimitry Andric bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem); 1443a7dea167SDimitry Andric forAllAssociatedToolChains( 1444a7dea167SDimitry Andric C, JA, getToolChain(), 1445a7dea167SDimitry Andric [&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) { 1446a7dea167SDimitry Andric HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs) 1447a7dea167SDimitry Andric : TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 14480b57cec5SDimitry Andric }); 1449a7dea167SDimitry Andric } 14500b57cec5SDimitry Andric 14510b57cec5SDimitry Andric // Add system include arguments for all targets but IAMCU. 14520b57cec5SDimitry Andric if (!IsIAMCU) 14530b57cec5SDimitry Andric forAllAssociatedToolChains(C, JA, getToolChain(), 14540b57cec5SDimitry Andric [&Args, &CmdArgs](const ToolChain &TC) { 14550b57cec5SDimitry Andric TC.AddClangSystemIncludeArgs(Args, CmdArgs); 14560b57cec5SDimitry Andric }); 14570b57cec5SDimitry Andric else { 14580b57cec5SDimitry Andric // For IAMCU add special include arguments. 14590b57cec5SDimitry Andric getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs); 14600b57cec5SDimitry Andric } 1461480093f4SDimitry Andric 1462480093f4SDimitry Andric addMacroPrefixMapArg(D, Args, CmdArgs); 1463fe6060f1SDimitry Andric addCoveragePrefixMapArg(D, Args, CmdArgs); 146481ad6265SDimitry Andric 146581ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffile_reproducible, 146681ad6265SDimitry Andric options::OPT_fno_file_reproducible); 14670b57cec5SDimitry Andric } 14680b57cec5SDimitry Andric 14690b57cec5SDimitry Andric // FIXME: Move to target hook. 14700b57cec5SDimitry Andric static bool isSignedCharDefault(const llvm::Triple &Triple) { 14710b57cec5SDimitry Andric switch (Triple.getArch()) { 14720b57cec5SDimitry Andric default: 14730b57cec5SDimitry Andric return true; 14740b57cec5SDimitry Andric 14750b57cec5SDimitry Andric case llvm::Triple::aarch64: 1476480093f4SDimitry Andric case llvm::Triple::aarch64_32: 14770b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 14780b57cec5SDimitry Andric case llvm::Triple::arm: 14790b57cec5SDimitry Andric case llvm::Triple::armeb: 14800b57cec5SDimitry Andric case llvm::Triple::thumb: 14810b57cec5SDimitry Andric case llvm::Triple::thumbeb: 14820b57cec5SDimitry Andric if (Triple.isOSDarwin() || Triple.isOSWindows()) 14830b57cec5SDimitry Andric return true; 14840b57cec5SDimitry Andric return false; 14850b57cec5SDimitry Andric 14860b57cec5SDimitry Andric case llvm::Triple::ppc: 14870b57cec5SDimitry Andric case llvm::Triple::ppc64: 14880b57cec5SDimitry Andric if (Triple.isOSDarwin()) 14890b57cec5SDimitry Andric return true; 14900b57cec5SDimitry Andric return false; 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric case llvm::Triple::hexagon: 1493e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 14940b57cec5SDimitry Andric case llvm::Triple::ppc64le: 14950b57cec5SDimitry Andric case llvm::Triple::riscv32: 14960b57cec5SDimitry Andric case llvm::Triple::riscv64: 14970b57cec5SDimitry Andric case llvm::Triple::systemz: 14980b57cec5SDimitry Andric case llvm::Triple::xcore: 14990b57cec5SDimitry Andric return false; 15000b57cec5SDimitry Andric } 15010b57cec5SDimitry Andric } 15020b57cec5SDimitry Andric 1503480093f4SDimitry Andric static bool hasMultipleInvocations(const llvm::Triple &Triple, 1504480093f4SDimitry Andric const ArgList &Args) { 1505480093f4SDimitry Andric // Supported only on Darwin where we invoke the compiler multiple times 1506480093f4SDimitry Andric // followed by an invocation to lipo. 1507480093f4SDimitry Andric if (!Triple.isOSDarwin()) 1508480093f4SDimitry Andric return false; 1509480093f4SDimitry Andric // If more than one "-arch <arch>" is specified, we're targeting multiple 1510480093f4SDimitry Andric // architectures resulting in a fat binary. 1511480093f4SDimitry Andric return Args.getAllArgValues(options::OPT_arch).size() > 1; 1512480093f4SDimitry Andric } 1513480093f4SDimitry Andric 1514480093f4SDimitry Andric static bool checkRemarksOptions(const Driver &D, const ArgList &Args, 1515480093f4SDimitry Andric const llvm::Triple &Triple) { 1516480093f4SDimitry Andric // When enabling remarks, we need to error if: 1517480093f4SDimitry Andric // * The remark file is specified but we're targeting multiple architectures, 1518480093f4SDimitry Andric // which means more than one remark file is being generated. 1519480093f4SDimitry Andric bool hasMultipleInvocations = ::hasMultipleInvocations(Triple, Args); 1520480093f4SDimitry Andric bool hasExplicitOutputFile = 1521480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1522480093f4SDimitry Andric if (hasMultipleInvocations && hasExplicitOutputFile) { 1523480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_output_with_multiple_archs) 1524480093f4SDimitry Andric << "-foptimization-record-file"; 1525480093f4SDimitry Andric return false; 1526480093f4SDimitry Andric } 1527480093f4SDimitry Andric return true; 1528480093f4SDimitry Andric } 1529480093f4SDimitry Andric 1530480093f4SDimitry Andric static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, 1531480093f4SDimitry Andric const llvm::Triple &Triple, 1532480093f4SDimitry Andric const InputInfo &Input, 1533480093f4SDimitry Andric const InputInfo &Output, const JobAction &JA) { 1534480093f4SDimitry Andric StringRef Format = "yaml"; 1535480093f4SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) 1536480093f4SDimitry Andric Format = A->getValue(); 1537480093f4SDimitry Andric 1538480093f4SDimitry Andric CmdArgs.push_back("-opt-record-file"); 1539480093f4SDimitry Andric 1540480093f4SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); 1541480093f4SDimitry Andric if (A) { 1542480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1543480093f4SDimitry Andric } else { 1544480093f4SDimitry Andric bool hasMultipleArchs = 1545480093f4SDimitry Andric Triple.isOSDarwin() && // Only supported on Darwin platforms. 1546480093f4SDimitry Andric Args.getAllArgValues(options::OPT_arch).size() > 1; 1547480093f4SDimitry Andric 1548480093f4SDimitry Andric SmallString<128> F; 1549480093f4SDimitry Andric 1550480093f4SDimitry Andric if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) { 1551480093f4SDimitry Andric if (Arg *FinalOutput = Args.getLastArg(options::OPT_o)) 1552480093f4SDimitry Andric F = FinalOutput->getValue(); 1553480093f4SDimitry Andric } else { 1554480093f4SDimitry Andric if (Format != "yaml" && // For YAML, keep the original behavior. 1555480093f4SDimitry Andric Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles. 1556480093f4SDimitry Andric Output.isFilename()) 1557480093f4SDimitry Andric F = Output.getFilename(); 1558480093f4SDimitry Andric } 1559480093f4SDimitry Andric 1560480093f4SDimitry Andric if (F.empty()) { 1561480093f4SDimitry Andric // Use the input filename. 1562480093f4SDimitry Andric F = llvm::sys::path::stem(Input.getBaseInput()); 1563480093f4SDimitry Andric 1564480093f4SDimitry Andric // If we're compiling for an offload architecture (i.e. a CUDA device), 1565480093f4SDimitry Andric // we need to make the file name for the device compilation different 1566480093f4SDimitry Andric // from the host compilation. 1567480093f4SDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_None) && 1568480093f4SDimitry Andric !JA.isDeviceOffloading(Action::OFK_Host)) { 1569480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1570480093f4SDimitry Andric F += Action::GetOffloadingFileNamePrefix(JA.getOffloadingDeviceKind(), 1571480093f4SDimitry Andric Triple.normalize()); 1572480093f4SDimitry Andric F += "-"; 1573480093f4SDimitry Andric F += JA.getOffloadingArch(); 1574480093f4SDimitry Andric } 1575480093f4SDimitry Andric } 1576480093f4SDimitry Andric 1577480093f4SDimitry Andric // If we're having more than one "-arch", we should name the files 1578480093f4SDimitry Andric // differently so that every cc1 invocation writes to a different file. 1579480093f4SDimitry Andric // We're doing that by appending "-<arch>" with "<arch>" being the arch 1580480093f4SDimitry Andric // name from the triple. 1581480093f4SDimitry Andric if (hasMultipleArchs) { 1582480093f4SDimitry Andric // First, remember the extension. 1583480093f4SDimitry Andric SmallString<64> OldExtension = llvm::sys::path::extension(F); 1584480093f4SDimitry Andric // then, remove it. 1585480093f4SDimitry Andric llvm::sys::path::replace_extension(F, ""); 1586480093f4SDimitry Andric // attach -<arch> to it. 1587480093f4SDimitry Andric F += "-"; 1588480093f4SDimitry Andric F += Triple.getArchName(); 1589480093f4SDimitry Andric // put back the extension. 1590480093f4SDimitry Andric llvm::sys::path::replace_extension(F, OldExtension); 1591480093f4SDimitry Andric } 1592480093f4SDimitry Andric 1593480093f4SDimitry Andric SmallString<32> Extension; 1594480093f4SDimitry Andric Extension += "opt."; 1595480093f4SDimitry Andric Extension += Format; 1596480093f4SDimitry Andric 1597480093f4SDimitry Andric llvm::sys::path::replace_extension(F, Extension); 1598480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(F)); 1599480093f4SDimitry Andric } 1600480093f4SDimitry Andric 1601480093f4SDimitry Andric if (const Arg *A = 1602480093f4SDimitry Andric Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) { 1603480093f4SDimitry Andric CmdArgs.push_back("-opt-record-passes"); 1604480093f4SDimitry Andric CmdArgs.push_back(A->getValue()); 1605480093f4SDimitry Andric } 1606480093f4SDimitry Andric 1607480093f4SDimitry Andric if (!Format.empty()) { 1608480093f4SDimitry Andric CmdArgs.push_back("-opt-record-format"); 1609480093f4SDimitry Andric CmdArgs.push_back(Format.data()); 1610480093f4SDimitry Andric } 1611480093f4SDimitry Andric } 1612480093f4SDimitry Andric 1613fe6060f1SDimitry Andric void AddAAPCSVolatileBitfieldArgs(const ArgList &Args, ArgStringList &CmdArgs) { 1614fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_faapcs_bitfield_width, 1615fe6060f1SDimitry Andric options::OPT_fno_aapcs_bitfield_width, true)) 1616fe6060f1SDimitry Andric CmdArgs.push_back("-fno-aapcs-bitfield-width"); 1617fe6060f1SDimitry Andric 1618fe6060f1SDimitry Andric if (Args.getLastArg(options::OPT_ForceAAPCSBitfieldLoad)) 1619fe6060f1SDimitry Andric CmdArgs.push_back("-faapcs-bitfield-load"); 1620fe6060f1SDimitry Andric } 1621fe6060f1SDimitry Andric 16220b57cec5SDimitry Andric namespace { 1623349cc55cSDimitry Andric void RenderARMABI(const Driver &D, const llvm::Triple &Triple, 1624349cc55cSDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 16250b57cec5SDimitry Andric // Select the ABI to use. 16260b57cec5SDimitry Andric // FIXME: Support -meabi. 16270b57cec5SDimitry Andric // FIXME: Parts of this are duplicated in the backend, unify this somehow. 16280b57cec5SDimitry Andric const char *ABIName = nullptr; 16290b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 16300b57cec5SDimitry Andric ABIName = A->getValue(); 16310b57cec5SDimitry Andric } else { 1632349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 16330b57cec5SDimitry Andric ABIName = llvm::ARM::computeDefaultTargetABI(Triple, CPU).data(); 16340b57cec5SDimitry Andric } 16350b57cec5SDimitry Andric 16360b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 16370b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 16380b57cec5SDimitry Andric } 163981ad6265SDimitry Andric 164081ad6265SDimitry Andric void AddUnalignedAccessWarning(ArgStringList &CmdArgs) { 164181ad6265SDimitry Andric auto StrictAlignIter = 164281ad6265SDimitry Andric std::find_if(CmdArgs.rbegin(), CmdArgs.rend(), [](StringRef Arg) { 164381ad6265SDimitry Andric return Arg == "+strict-align" || Arg == "-strict-align"; 164481ad6265SDimitry Andric }); 164581ad6265SDimitry Andric if (StrictAlignIter != CmdArgs.rend() && 164681ad6265SDimitry Andric StringRef(*StrictAlignIter) == "+strict-align") 164781ad6265SDimitry Andric CmdArgs.push_back("-Wunaligned-access"); 164881ad6265SDimitry Andric } 16490b57cec5SDimitry Andric } 16500b57cec5SDimitry Andric 16511fd87a68SDimitry Andric static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, 16524824e7fdSDimitry Andric ArgStringList &CmdArgs, bool isAArch64) { 16534824e7fdSDimitry Andric const Arg *A = isAArch64 16544824e7fdSDimitry Andric ? Args.getLastArg(options::OPT_msign_return_address_EQ, 16554824e7fdSDimitry Andric options::OPT_mbranch_protection_EQ) 16564824e7fdSDimitry Andric : Args.getLastArg(options::OPT_mbranch_protection_EQ); 16574824e7fdSDimitry Andric if (!A) 16584824e7fdSDimitry Andric return; 16594824e7fdSDimitry Andric 16601fd87a68SDimitry Andric const Driver &D = TC.getDriver(); 16611fd87a68SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 16621fd87a68SDimitry Andric if (!(isAArch64 || (Triple.isArmT32() && Triple.isArmMClass()))) 1663d56accc7SDimitry Andric D.Diag(diag::warn_incompatible_branch_protection_option) 16641fd87a68SDimitry Andric << Triple.getArchName(); 16651fd87a68SDimitry Andric 16664824e7fdSDimitry Andric StringRef Scope, Key; 16674824e7fdSDimitry Andric bool IndirectBranches; 16684824e7fdSDimitry Andric 16694824e7fdSDimitry Andric if (A->getOption().matches(options::OPT_msign_return_address_EQ)) { 16704824e7fdSDimitry Andric Scope = A->getValue(); 167181ad6265SDimitry Andric if (Scope != "none" && Scope != "non-leaf" && Scope != "all") 167281ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 167381ad6265SDimitry Andric << A->getOption().getName() << Scope; 16744824e7fdSDimitry Andric Key = "a_key"; 16754824e7fdSDimitry Andric IndirectBranches = false; 16764824e7fdSDimitry Andric } else { 16774824e7fdSDimitry Andric StringRef DiagMsg; 16784824e7fdSDimitry Andric llvm::ARM::ParsedBranchProtection PBP; 16794824e7fdSDimitry Andric if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg)) 168081ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 168181ad6265SDimitry Andric << A->getOption().getName() << DiagMsg; 16824824e7fdSDimitry Andric if (!isAArch64 && PBP.Key == "b_key") 16834824e7fdSDimitry Andric D.Diag(diag::warn_unsupported_branch_protection) 16844824e7fdSDimitry Andric << "b-key" << A->getAsString(Args); 16854824e7fdSDimitry Andric Scope = PBP.Scope; 16864824e7fdSDimitry Andric Key = PBP.Key; 16874824e7fdSDimitry Andric IndirectBranches = PBP.BranchTargetEnforcement; 16884824e7fdSDimitry Andric } 16894824e7fdSDimitry Andric 16904824e7fdSDimitry Andric CmdArgs.push_back( 16914824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address=") + Scope)); 16924824e7fdSDimitry Andric if (!Scope.equals("none")) 16934824e7fdSDimitry Andric CmdArgs.push_back( 16944824e7fdSDimitry Andric Args.MakeArgString(Twine("-msign-return-address-key=") + Key)); 16954824e7fdSDimitry Andric if (IndirectBranches) 16964824e7fdSDimitry Andric CmdArgs.push_back("-mbranch-target-enforce"); 16974824e7fdSDimitry Andric } 16984824e7fdSDimitry Andric 16990b57cec5SDimitry Andric void Clang::AddARMTargetArgs(const llvm::Triple &Triple, const ArgList &Args, 17000b57cec5SDimitry Andric ArgStringList &CmdArgs, bool KernelOrKext) const { 1701349cc55cSDimitry Andric RenderARMABI(getToolChain().getDriver(), Triple, Args, CmdArgs); 17020b57cec5SDimitry Andric 17030b57cec5SDimitry Andric // Determine floating point ABI from the options & target defaults. 17040b57cec5SDimitry Andric arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); 17050b57cec5SDimitry Andric if (ABI == arm::FloatABI::Soft) { 17060b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 17070b57cec5SDimitry Andric // FIXME: This changes CPP defines, we need -target-soft-float. 17080b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 17090b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 17100b57cec5SDimitry Andric CmdArgs.push_back("soft"); 17110b57cec5SDimitry Andric } else if (ABI == arm::FloatABI::SoftFP) { 17120b57cec5SDimitry Andric // Floating point operations are hard, but argument passing is soft. 17130b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 17140b57cec5SDimitry Andric CmdArgs.push_back("soft"); 17150b57cec5SDimitry Andric } else { 17160b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 17170b57cec5SDimitry Andric assert(ABI == arm::FloatABI::Hard && "Invalid float abi!"); 17180b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 17190b57cec5SDimitry Andric CmdArgs.push_back("hard"); 17200b57cec5SDimitry Andric } 17210b57cec5SDimitry Andric 17220b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 17230b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 17240b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 17250b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 17260b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 17270b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=false"); 17280b57cec5SDimitry Andric else 17290b57cec5SDimitry Andric CmdArgs.push_back("-arm-global-merge=true"); 17300b57cec5SDimitry Andric } 17310b57cec5SDimitry Andric 17320b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 17330b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 17340b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 17350b57cec5SDimitry Andric 17360b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mcmse)) 17370b57cec5SDimitry Andric CmdArgs.push_back("-mcmse"); 1738fe6060f1SDimitry Andric 1739fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 17404824e7fdSDimitry Andric 17414824e7fdSDimitry Andric // Enable/disable return address signing and indirect branch targets. 17421fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); 174381ad6265SDimitry Andric 174481ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 17450b57cec5SDimitry Andric } 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, 17480b57cec5SDimitry Andric const ArgList &Args, bool KernelOrKext, 17490b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 17500b57cec5SDimitry Andric const ToolChain &TC = getToolChain(); 17510b57cec5SDimitry Andric 17520b57cec5SDimitry Andric // Add the target features 17535ffd83dbSDimitry Andric getTargetFeatures(TC.getDriver(), EffectiveTriple, Args, CmdArgs, false); 17540b57cec5SDimitry Andric 17550b57cec5SDimitry Andric // Add target specific flags. 17560b57cec5SDimitry Andric switch (TC.getArch()) { 17570b57cec5SDimitry Andric default: 17580b57cec5SDimitry Andric break; 17590b57cec5SDimitry Andric 17600b57cec5SDimitry Andric case llvm::Triple::arm: 17610b57cec5SDimitry Andric case llvm::Triple::armeb: 17620b57cec5SDimitry Andric case llvm::Triple::thumb: 17630b57cec5SDimitry Andric case llvm::Triple::thumbeb: 17640b57cec5SDimitry Andric // Use the effective triple, which takes into account the deployment target. 17650b57cec5SDimitry Andric AddARMTargetArgs(EffectiveTriple, Args, CmdArgs, KernelOrKext); 17660b57cec5SDimitry Andric CmdArgs.push_back("-fallow-half-arguments-and-returns"); 17670b57cec5SDimitry Andric break; 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric case llvm::Triple::aarch64: 1770480093f4SDimitry Andric case llvm::Triple::aarch64_32: 17710b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 17720b57cec5SDimitry Andric AddAArch64TargetArgs(Args, CmdArgs); 17730b57cec5SDimitry Andric CmdArgs.push_back("-fallow-half-arguments-and-returns"); 17740b57cec5SDimitry Andric break; 17750b57cec5SDimitry Andric 17760b57cec5SDimitry Andric case llvm::Triple::mips: 17770b57cec5SDimitry Andric case llvm::Triple::mipsel: 17780b57cec5SDimitry Andric case llvm::Triple::mips64: 17790b57cec5SDimitry Andric case llvm::Triple::mips64el: 17800b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 17810b57cec5SDimitry Andric break; 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric case llvm::Triple::ppc: 1784e8d8bef9SDimitry Andric case llvm::Triple::ppcle: 17850b57cec5SDimitry Andric case llvm::Triple::ppc64: 17860b57cec5SDimitry Andric case llvm::Triple::ppc64le: 17870b57cec5SDimitry Andric AddPPCTargetArgs(Args, CmdArgs); 17880b57cec5SDimitry Andric break; 17890b57cec5SDimitry Andric 17900b57cec5SDimitry Andric case llvm::Triple::riscv32: 17910b57cec5SDimitry Andric case llvm::Triple::riscv64: 17920b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 17930b57cec5SDimitry Andric break; 17940b57cec5SDimitry Andric 17950b57cec5SDimitry Andric case llvm::Triple::sparc: 17960b57cec5SDimitry Andric case llvm::Triple::sparcel: 17970b57cec5SDimitry Andric case llvm::Triple::sparcv9: 17980b57cec5SDimitry Andric AddSparcTargetArgs(Args, CmdArgs); 17990b57cec5SDimitry Andric break; 18000b57cec5SDimitry Andric 18010b57cec5SDimitry Andric case llvm::Triple::systemz: 18020b57cec5SDimitry Andric AddSystemZTargetArgs(Args, CmdArgs); 18030b57cec5SDimitry Andric break; 18040b57cec5SDimitry Andric 18050b57cec5SDimitry Andric case llvm::Triple::x86: 18060b57cec5SDimitry Andric case llvm::Triple::x86_64: 18070b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 18080b57cec5SDimitry Andric break; 18090b57cec5SDimitry Andric 18100b57cec5SDimitry Andric case llvm::Triple::lanai: 18110b57cec5SDimitry Andric AddLanaiTargetArgs(Args, CmdArgs); 18120b57cec5SDimitry Andric break; 18130b57cec5SDimitry Andric 18140b57cec5SDimitry Andric case llvm::Triple::hexagon: 18150b57cec5SDimitry Andric AddHexagonTargetArgs(Args, CmdArgs); 18160b57cec5SDimitry Andric break; 18170b57cec5SDimitry Andric 18180b57cec5SDimitry Andric case llvm::Triple::wasm32: 18190b57cec5SDimitry Andric case llvm::Triple::wasm64: 18200b57cec5SDimitry Andric AddWebAssemblyTargetArgs(Args, CmdArgs); 18210b57cec5SDimitry Andric break; 18225ffd83dbSDimitry Andric 18235ffd83dbSDimitry Andric case llvm::Triple::ve: 18245ffd83dbSDimitry Andric AddVETargetArgs(Args, CmdArgs); 18255ffd83dbSDimitry Andric break; 18260b57cec5SDimitry Andric } 18270b57cec5SDimitry Andric } 18280b57cec5SDimitry Andric 18290b57cec5SDimitry Andric namespace { 18300b57cec5SDimitry Andric void RenderAArch64ABI(const llvm::Triple &Triple, const ArgList &Args, 18310b57cec5SDimitry Andric ArgStringList &CmdArgs) { 18320b57cec5SDimitry Andric const char *ABIName = nullptr; 18330b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) 18340b57cec5SDimitry Andric ABIName = A->getValue(); 18350b57cec5SDimitry Andric else if (Triple.isOSDarwin()) 18360b57cec5SDimitry Andric ABIName = "darwinpcs"; 18370b57cec5SDimitry Andric else 18380b57cec5SDimitry Andric ABIName = "aapcs"; 18390b57cec5SDimitry Andric 18400b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 18410b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 18420b57cec5SDimitry Andric } 18430b57cec5SDimitry Andric } 18440b57cec5SDimitry Andric 18450b57cec5SDimitry Andric void Clang::AddAArch64TargetArgs(const ArgList &Args, 18460b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 18470b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 18480b57cec5SDimitry Andric 18490b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 18500b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 18510b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 18520b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 18530b57cec5SDimitry Andric 18540b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mimplicit_float, 18550b57cec5SDimitry Andric options::OPT_mno_implicit_float, true)) 18560b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 18570b57cec5SDimitry Andric 18580b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 18590b57cec5SDimitry Andric 18600b57cec5SDimitry Andric // Forward the -mglobal-merge option for explicit control over the pass. 18610b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 18620b57cec5SDimitry Andric options::OPT_mno_global_merge)) { 18630b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 18640b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_global_merge)) 18650b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=false"); 18660b57cec5SDimitry Andric else 18670b57cec5SDimitry Andric CmdArgs.push_back("-aarch64-enable-global-merge=true"); 18680b57cec5SDimitry Andric } 18690b57cec5SDimitry Andric 18700b57cec5SDimitry Andric // Enable/disable return address signing and indirect branch targets. 18711fd87a68SDimitry Andric CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, true /*isAArch64*/); 1872e8d8bef9SDimitry Andric 1873e8d8bef9SDimitry Andric // Handle -msve_vector_bits=<bits> 1874e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) { 1875e8d8bef9SDimitry Andric StringRef Val = A->getValue(); 1876e8d8bef9SDimitry Andric const Driver &D = getToolChain().getDriver(); 1877e8d8bef9SDimitry Andric if (Val.equals("128") || Val.equals("256") || Val.equals("512") || 1878349cc55cSDimitry Andric Val.equals("1024") || Val.equals("2048") || Val.equals("128+") || 1879349cc55cSDimitry Andric Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") || 1880349cc55cSDimitry Andric Val.equals("2048+")) { 1881349cc55cSDimitry Andric unsigned Bits = 0; 1882349cc55cSDimitry Andric if (Val.endswith("+")) 1883349cc55cSDimitry Andric Val = Val.substr(0, Val.size() - 1); 1884349cc55cSDimitry Andric else { 1885349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1886349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1887e8d8bef9SDimitry Andric CmdArgs.push_back( 1888349cc55cSDimitry Andric Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128))); 1889349cc55cSDimitry Andric } 1890349cc55cSDimitry Andric 1891349cc55cSDimitry Andric bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid; 1892349cc55cSDimitry Andric assert(!Invalid && "Failed to parse value"); 1893349cc55cSDimitry Andric CmdArgs.push_back( 1894349cc55cSDimitry Andric Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128))); 1895e8d8bef9SDimitry Andric // Silently drop requests for vector-length agnostic code as it's implied. 1896349cc55cSDimitry Andric } else if (!Val.equals("scalable")) 1897e8d8bef9SDimitry Andric // Handle the unsupported values passed to msve-vector-bits. 1898e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 1899e8d8bef9SDimitry Andric << A->getOption().getName() << Val; 1900e8d8bef9SDimitry Andric } 1901fe6060f1SDimitry Andric 1902fe6060f1SDimitry Andric AddAAPCSVolatileBitfieldArgs(Args, CmdArgs); 1903349cc55cSDimitry Andric 1904349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 1905349cc55cSDimitry Andric StringRef Name = A->getValue(); 1906349cc55cSDimitry Andric 1907349cc55cSDimitry Andric std::string TuneCPU; 1908349cc55cSDimitry Andric if (Name == "native") 1909349cc55cSDimitry Andric TuneCPU = std::string(llvm::sys::getHostCPUName()); 1910349cc55cSDimitry Andric else 1911349cc55cSDimitry Andric TuneCPU = std::string(Name); 1912349cc55cSDimitry Andric 1913349cc55cSDimitry Andric if (!TuneCPU.empty()) { 1914349cc55cSDimitry Andric CmdArgs.push_back("-tune-cpu"); 1915349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 1916349cc55cSDimitry Andric } 1917349cc55cSDimitry Andric } 191881ad6265SDimitry Andric 191981ad6265SDimitry Andric AddUnalignedAccessWarning(CmdArgs); 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric void Clang::AddMIPSTargetArgs(const ArgList &Args, 19230b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 19240b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 19250b57cec5SDimitry Andric StringRef CPUName; 19260b57cec5SDimitry Andric StringRef ABIName; 19270b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 19280b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 19290b57cec5SDimitry Andric 19300b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 19310b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric mips::FloatABI ABI = mips::getMipsFloatABI(D, Args, Triple); 19340b57cec5SDimitry Andric if (ABI == mips::FloatABI::Soft) { 19350b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 19360b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 19370b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 19380b57cec5SDimitry Andric CmdArgs.push_back("soft"); 19390b57cec5SDimitry Andric } else { 19400b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 19410b57cec5SDimitry Andric assert(ABI == mips::FloatABI::Hard && "Invalid float abi!"); 19420b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 19430b57cec5SDimitry Andric CmdArgs.push_back("hard"); 19440b57cec5SDimitry Andric } 19450b57cec5SDimitry Andric 19460b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 19470b57cec5SDimitry Andric options::OPT_mno_ldc1_sdc1)) { 19480b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 19490b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19500b57cec5SDimitry Andric CmdArgs.push_back("-mno-ldc1-sdc1"); 19510b57cec5SDimitry Andric } 19520b57cec5SDimitry Andric } 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 19550b57cec5SDimitry Andric options::OPT_mno_check_zero_division)) { 19560b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 19570b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19580b57cec5SDimitry Andric CmdArgs.push_back("-mno-check-zero-division"); 19590b57cec5SDimitry Andric } 19600b57cec5SDimitry Andric } 19610b57cec5SDimitry Andric 196204eeddc0SDimitry Andric if (Args.getLastArg(options::OPT_mfix4300)) { 196304eeddc0SDimitry Andric CmdArgs.push_back("-mllvm"); 196404eeddc0SDimitry Andric CmdArgs.push_back("-mfix4300"); 196504eeddc0SDimitry Andric } 196604eeddc0SDimitry Andric 19670b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_G)) { 19680b57cec5SDimitry Andric StringRef v = A->getValue(); 19690b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 19700b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 19710b57cec5SDimitry Andric A->claim(); 19720b57cec5SDimitry Andric } 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric Arg *GPOpt = Args.getLastArg(options::OPT_mgpopt, options::OPT_mno_gpopt); 19750b57cec5SDimitry Andric Arg *ABICalls = 19760b57cec5SDimitry Andric Args.getLastArg(options::OPT_mabicalls, options::OPT_mno_abicalls); 19770b57cec5SDimitry Andric 19780b57cec5SDimitry Andric // -mabicalls is the default for many MIPS environments, even with -fno-pic. 19790b57cec5SDimitry Andric // -mgpopt is the default for static, -fno-pic environments but these two 19800b57cec5SDimitry Andric // options conflict. We want to be certain that -mno-abicalls -mgpopt is 19810b57cec5SDimitry Andric // the only case where -mllvm -mgpopt is passed. 19820b57cec5SDimitry Andric // NOTE: We need a warning here or in the backend to warn when -mgpopt is 19830b57cec5SDimitry Andric // passed explicitly when compiling something with -mabicalls 19840b57cec5SDimitry Andric // (implictly) in affect. Currently the warning is in the backend. 19850b57cec5SDimitry Andric // 19860b57cec5SDimitry Andric // When the ABI in use is N64, we also need to determine the PIC mode that 19870b57cec5SDimitry Andric // is in use, as -fno-pic for N64 implies -mno-abicalls. 19880b57cec5SDimitry Andric bool NoABICalls = 19890b57cec5SDimitry Andric ABICalls && ABICalls->getOption().matches(options::OPT_mno_abicalls); 19900b57cec5SDimitry Andric 19910b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 19920b57cec5SDimitry Andric unsigned PICLevel; 19930b57cec5SDimitry Andric bool IsPIE; 19940b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 19950b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 19960b57cec5SDimitry Andric 19970b57cec5SDimitry Andric NoABICalls = NoABICalls || 19980b57cec5SDimitry Andric (RelocationModel == llvm::Reloc::Static && ABIName == "n64"); 19990b57cec5SDimitry Andric 20000b57cec5SDimitry Andric bool WantGPOpt = GPOpt && GPOpt->getOption().matches(options::OPT_mgpopt); 20010b57cec5SDimitry Andric // We quietly ignore -mno-gpopt as the backend defaults to -mno-gpopt. 20020b57cec5SDimitry Andric if (NoABICalls && (!GPOpt || WantGPOpt)) { 20030b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20040b57cec5SDimitry Andric CmdArgs.push_back("-mgpopt"); 20050b57cec5SDimitry Andric 20060b57cec5SDimitry Andric Arg *LocalSData = Args.getLastArg(options::OPT_mlocal_sdata, 20070b57cec5SDimitry Andric options::OPT_mno_local_sdata); 20080b57cec5SDimitry Andric Arg *ExternSData = Args.getLastArg(options::OPT_mextern_sdata, 20090b57cec5SDimitry Andric options::OPT_mno_extern_sdata); 20100b57cec5SDimitry Andric Arg *EmbeddedData = Args.getLastArg(options::OPT_membedded_data, 20110b57cec5SDimitry Andric options::OPT_mno_embedded_data); 20120b57cec5SDimitry Andric if (LocalSData) { 20130b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20140b57cec5SDimitry Andric if (LocalSData->getOption().matches(options::OPT_mlocal_sdata)) { 20150b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=1"); 20160b57cec5SDimitry Andric } else { 20170b57cec5SDimitry Andric CmdArgs.push_back("-mlocal-sdata=0"); 20180b57cec5SDimitry Andric } 20190b57cec5SDimitry Andric LocalSData->claim(); 20200b57cec5SDimitry Andric } 20210b57cec5SDimitry Andric 20220b57cec5SDimitry Andric if (ExternSData) { 20230b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20240b57cec5SDimitry Andric if (ExternSData->getOption().matches(options::OPT_mextern_sdata)) { 20250b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=1"); 20260b57cec5SDimitry Andric } else { 20270b57cec5SDimitry Andric CmdArgs.push_back("-mextern-sdata=0"); 20280b57cec5SDimitry Andric } 20290b57cec5SDimitry Andric ExternSData->claim(); 20300b57cec5SDimitry Andric } 20310b57cec5SDimitry Andric 20320b57cec5SDimitry Andric if (EmbeddedData) { 20330b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20340b57cec5SDimitry Andric if (EmbeddedData->getOption().matches(options::OPT_membedded_data)) { 20350b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=1"); 20360b57cec5SDimitry Andric } else { 20370b57cec5SDimitry Andric CmdArgs.push_back("-membedded-data=0"); 20380b57cec5SDimitry Andric } 20390b57cec5SDimitry Andric EmbeddedData->claim(); 20400b57cec5SDimitry Andric } 20410b57cec5SDimitry Andric 20420b57cec5SDimitry Andric } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt) 20430b57cec5SDimitry Andric D.Diag(diag::warn_drv_unsupported_gpopt) << (ABICalls ? 0 : 1); 20440b57cec5SDimitry Andric 20450b57cec5SDimitry Andric if (GPOpt) 20460b57cec5SDimitry Andric GPOpt->claim(); 20470b57cec5SDimitry Andric 20480b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcompact_branches_EQ)) { 20490b57cec5SDimitry Andric StringRef Val = StringRef(A->getValue()); 20500b57cec5SDimitry Andric if (mips::hasCompactBranches(CPUName)) { 20510b57cec5SDimitry Andric if (Val == "never" || Val == "always" || Val == "optimal") { 20520b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20530b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mips-compact-branches=" + Val)); 20540b57cec5SDimitry Andric } else 20550b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 20560b57cec5SDimitry Andric << A->getOption().getName() << Val; 20570b57cec5SDimitry Andric } else 20580b57cec5SDimitry Andric D.Diag(diag::warn_target_unsupported_compact_branches) << CPUName; 20590b57cec5SDimitry Andric } 20600b57cec5SDimitry Andric 20610b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrelax_pic_calls, 20620b57cec5SDimitry Andric options::OPT_mno_relax_pic_calls)) { 20630b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mno_relax_pic_calls)) { 20640b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 20650b57cec5SDimitry Andric CmdArgs.push_back("-mips-jalr-reloc=0"); 20660b57cec5SDimitry Andric } 20670b57cec5SDimitry Andric } 20680b57cec5SDimitry Andric } 20690b57cec5SDimitry Andric 20700b57cec5SDimitry Andric void Clang::AddPPCTargetArgs(const ArgList &Args, 20710b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 20720b57cec5SDimitry Andric // Select the ABI to use. 20730b57cec5SDimitry Andric const char *ABIName = nullptr; 2074480093f4SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 2075480093f4SDimitry Andric if (T.isOSBinFormatELF()) { 20760b57cec5SDimitry Andric switch (getToolChain().getArch()) { 20770b57cec5SDimitry Andric case llvm::Triple::ppc64: { 207875b4d546SDimitry Andric if ((T.isOSFreeBSD() && T.getOSMajorVersion() >= 13) || 207975b4d546SDimitry Andric T.isOSOpenBSD() || T.isMusl()) 2080480093f4SDimitry Andric ABIName = "elfv2"; 2081480093f4SDimitry Andric else 20820b57cec5SDimitry Andric ABIName = "elfv1"; 20830b57cec5SDimitry Andric break; 20840b57cec5SDimitry Andric } 20850b57cec5SDimitry Andric case llvm::Triple::ppc64le: 20860b57cec5SDimitry Andric ABIName = "elfv2"; 20870b57cec5SDimitry Andric break; 20880b57cec5SDimitry Andric default: 20890b57cec5SDimitry Andric break; 20900b57cec5SDimitry Andric } 2091480093f4SDimitry Andric } 20920b57cec5SDimitry Andric 209304eeddc0SDimitry Andric bool IEEELongDouble = getToolChain().defaultToIEEELongDouble(); 20940b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mabi_EQ)) { 20950b57cec5SDimitry Andric StringRef V = A->getValue(); 20960b57cec5SDimitry Andric if (V == "ieeelongdouble") 20970b57cec5SDimitry Andric IEEELongDouble = true; 20980b57cec5SDimitry Andric else if (V == "ibmlongdouble") 20990b57cec5SDimitry Andric IEEELongDouble = false; 21000b57cec5SDimitry Andric else if (V != "altivec") 21010b57cec5SDimitry Andric // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore 21020b57cec5SDimitry Andric // the option if given as we don't have backend support for any targets 21030b57cec5SDimitry Andric // that don't use the altivec abi. 21040b57cec5SDimitry Andric ABIName = A->getValue(); 21050b57cec5SDimitry Andric } 21060b57cec5SDimitry Andric if (IEEELongDouble) 21070b57cec5SDimitry Andric CmdArgs.push_back("-mabi=ieeelongdouble"); 21080b57cec5SDimitry Andric 21090b57cec5SDimitry Andric ppc::FloatABI FloatABI = 21100b57cec5SDimitry Andric ppc::getPPCFloatABI(getToolChain().getDriver(), Args); 21110b57cec5SDimitry Andric 21120b57cec5SDimitry Andric if (FloatABI == ppc::FloatABI::Soft) { 21130b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 21140b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 21150b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21160b57cec5SDimitry Andric CmdArgs.push_back("soft"); 21170b57cec5SDimitry Andric } else { 21180b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 21190b57cec5SDimitry Andric assert(FloatABI == ppc::FloatABI::Hard && "Invalid float abi!"); 21200b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21210b57cec5SDimitry Andric CmdArgs.push_back("hard"); 21220b57cec5SDimitry Andric } 21230b57cec5SDimitry Andric 21240b57cec5SDimitry Andric if (ABIName) { 21250b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 21260b57cec5SDimitry Andric CmdArgs.push_back(ABIName); 21270b57cec5SDimitry Andric } 21280b57cec5SDimitry Andric } 21290b57cec5SDimitry Andric 21305ffd83dbSDimitry Andric static void SetRISCVSmallDataLimit(const ToolChain &TC, const ArgList &Args, 21315ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 21325ffd83dbSDimitry Andric const Driver &D = TC.getDriver(); 21335ffd83dbSDimitry Andric const llvm::Triple &Triple = TC.getTriple(); 21345ffd83dbSDimitry Andric // Default small data limitation is eight. 21355ffd83dbSDimitry Andric const char *SmallDataLimit = "8"; 21365ffd83dbSDimitry Andric // Get small data limitation. 21375ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_shared, options::OPT_fpic, 21385ffd83dbSDimitry Andric options::OPT_fPIC)) { 21395ffd83dbSDimitry Andric // Not support linker relaxation for PIC. 21405ffd83dbSDimitry Andric SmallDataLimit = "0"; 21415ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 21425ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 21435ffd83dbSDimitry Andric } 21445ffd83dbSDimitry Andric } else if (Args.getLastArgValue(options::OPT_mcmodel_EQ) 2145fe6060f1SDimitry Andric .equals_insensitive("large") && 21465ffd83dbSDimitry Andric (Triple.getArch() == llvm::Triple::riscv64)) { 21475ffd83dbSDimitry Andric // Not support linker relaxation for RV64 with large code model. 21485ffd83dbSDimitry Andric SmallDataLimit = "0"; 21495ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_G)) { 21505ffd83dbSDimitry Andric D.Diag(diag::warn_drv_unsupported_sdata); 21515ffd83dbSDimitry Andric } 21525ffd83dbSDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_G)) { 21535ffd83dbSDimitry Andric SmallDataLimit = A->getValue(); 21545ffd83dbSDimitry Andric } 21555ffd83dbSDimitry Andric // Forward the -msmall-data-limit= option. 21565ffd83dbSDimitry Andric CmdArgs.push_back("-msmall-data-limit"); 21575ffd83dbSDimitry Andric CmdArgs.push_back(SmallDataLimit); 21585ffd83dbSDimitry Andric } 21595ffd83dbSDimitry Andric 21600b57cec5SDimitry Andric void Clang::AddRISCVTargetArgs(const ArgList &Args, 21610b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21620b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 2163a7dea167SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 21640b57cec5SDimitry Andric 21650b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 2166a7dea167SDimitry Andric CmdArgs.push_back(ABIName.data()); 21675ffd83dbSDimitry Andric 21685ffd83dbSDimitry Andric SetRISCVSmallDataLimit(getToolChain(), Args, CmdArgs); 2169e8d8bef9SDimitry Andric 2170e8d8bef9SDimitry Andric std::string TuneCPU; 2171e8d8bef9SDimitry Andric 2172e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2173e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2174e8d8bef9SDimitry Andric 2175e8d8bef9SDimitry Andric Name = llvm::RISCV::resolveTuneCPUAlias(Name, Triple.isArch64Bit()); 2176e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2177e8d8bef9SDimitry Andric } 2178e8d8bef9SDimitry Andric 2179e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2180e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2181e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2182e8d8bef9SDimitry Andric } 21830b57cec5SDimitry Andric } 21840b57cec5SDimitry Andric 21850b57cec5SDimitry Andric void Clang::AddSparcTargetArgs(const ArgList &Args, 21860b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 21870b57cec5SDimitry Andric sparc::FloatABI FloatABI = 21880b57cec5SDimitry Andric sparc::getSparcFloatABI(getToolChain().getDriver(), Args); 21890b57cec5SDimitry Andric 21900b57cec5SDimitry Andric if (FloatABI == sparc::FloatABI::Soft) { 21910b57cec5SDimitry Andric // Floating point operations and argument passing are soft. 21920b57cec5SDimitry Andric CmdArgs.push_back("-msoft-float"); 21930b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21940b57cec5SDimitry Andric CmdArgs.push_back("soft"); 21950b57cec5SDimitry Andric } else { 21960b57cec5SDimitry Andric // Floating point operations and argument passing are hard. 21970b57cec5SDimitry Andric assert(FloatABI == sparc::FloatABI::Hard && "Invalid float abi!"); 21980b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 21990b57cec5SDimitry Andric CmdArgs.push_back("hard"); 22000b57cec5SDimitry Andric } 22010b57cec5SDimitry Andric } 22020b57cec5SDimitry Andric 22030b57cec5SDimitry Andric void Clang::AddSystemZTargetArgs(const ArgList &Args, 22040b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2205753f127fSDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2206753f127fSDimitry Andric StringRef Name = A->getValue(); 2207753f127fSDimitry Andric 2208753f127fSDimitry Andric std::string TuneCPU; 2209753f127fSDimitry Andric if (Name == "native") 2210753f127fSDimitry Andric TuneCPU = std::string(llvm::sys::getHostCPUName()); 2211753f127fSDimitry Andric else 2212753f127fSDimitry Andric TuneCPU = std::string(Name); 2213753f127fSDimitry Andric 2214753f127fSDimitry Andric if (!TuneCPU.empty()) { 2215753f127fSDimitry Andric CmdArgs.push_back("-tune-cpu"); 2216753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2217753f127fSDimitry Andric } 2218753f127fSDimitry Andric } 2219753f127fSDimitry Andric 2220753f127fSDimitry Andric bool HasBackchain = 2221753f127fSDimitry Andric Args.hasFlag(options::OPT_mbackchain, options::OPT_mno_backchain, false); 2222480093f4SDimitry Andric bool HasPackedStack = Args.hasFlag(options::OPT_mpacked_stack, 2223480093f4SDimitry Andric options::OPT_mno_packed_stack, false); 22245ffd83dbSDimitry Andric systemz::FloatABI FloatABI = 22255ffd83dbSDimitry Andric systemz::getSystemZFloatABI(getToolChain().getDriver(), Args); 22265ffd83dbSDimitry Andric bool HasSoftFloat = (FloatABI == systemz::FloatABI::Soft); 22275ffd83dbSDimitry Andric if (HasBackchain && HasPackedStack && !HasSoftFloat) { 2228480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 2229480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt) 22305ffd83dbSDimitry Andric << "-mpacked-stack -mbackchain -mhard-float"; 2231480093f4SDimitry Andric } 2232480093f4SDimitry Andric if (HasBackchain) 22330b57cec5SDimitry Andric CmdArgs.push_back("-mbackchain"); 2234480093f4SDimitry Andric if (HasPackedStack) 2235480093f4SDimitry Andric CmdArgs.push_back("-mpacked-stack"); 22365ffd83dbSDimitry Andric if (HasSoftFloat) { 22375ffd83dbSDimitry Andric // Floating point operations and argument passing are soft. 22385ffd83dbSDimitry Andric CmdArgs.push_back("-msoft-float"); 22395ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22405ffd83dbSDimitry Andric CmdArgs.push_back("soft"); 2241480093f4SDimitry Andric } 22420b57cec5SDimitry Andric } 22430b57cec5SDimitry Andric 22440b57cec5SDimitry Andric void Clang::AddX86TargetArgs(const ArgList &Args, 22450b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 2246480093f4SDimitry Andric const Driver &D = getToolChain().getDriver(); 22475ffd83dbSDimitry Andric addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/false); 2248480093f4SDimitry Andric 22490b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) || 22500b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel) || 22510b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)) 22520b57cec5SDimitry Andric CmdArgs.push_back("-disable-red-zone"); 22530b57cec5SDimitry Andric 22540b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs, 22550b57cec5SDimitry Andric options::OPT_mno_tls_direct_seg_refs, true)) 22560b57cec5SDimitry Andric CmdArgs.push_back("-mno-tls-direct-seg-refs"); 22570b57cec5SDimitry Andric 22580b57cec5SDimitry Andric // Default to avoid implicit floating-point for kernel/kext code, but allow 22590b57cec5SDimitry Andric // that to be overridden with -mno-soft-float. 22600b57cec5SDimitry Andric bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 22610b57cec5SDimitry Andric Args.hasArg(options::OPT_fapple_kext)); 22620b57cec5SDimitry Andric if (Arg *A = Args.getLastArg( 22630b57cec5SDimitry Andric options::OPT_msoft_float, options::OPT_mno_soft_float, 22640b57cec5SDimitry Andric options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) { 22650b57cec5SDimitry Andric const Option &O = A->getOption(); 22660b57cec5SDimitry Andric NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 22670b57cec5SDimitry Andric O.matches(options::OPT_msoft_float)); 22680b57cec5SDimitry Andric } 22690b57cec5SDimitry Andric if (NoImplicitFloat) 22700b57cec5SDimitry Andric CmdArgs.push_back("-no-implicit-float"); 22710b57cec5SDimitry Andric 22720b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 22730b57cec5SDimitry Andric StringRef Value = A->getValue(); 22740b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 22750b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22760b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 2277349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-inline-asm=" + Value)); 22780b57cec5SDimitry Andric } else { 2279480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 22800b57cec5SDimitry Andric << A->getOption().getName() << Value; 22810b57cec5SDimitry Andric } 2282480093f4SDimitry Andric } else if (D.IsCLMode()) { 22830b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 22840b57cec5SDimitry Andric CmdArgs.push_back("-x86-asm-syntax=intel"); 22850b57cec5SDimitry Andric } 22860b57cec5SDimitry Andric 2287349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mskip_rax_setup, 2288349cc55cSDimitry Andric options::OPT_mno_skip_rax_setup)) 2289349cc55cSDimitry Andric if (A->getOption().matches(options::OPT_mskip_rax_setup)) 2290349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mskip-rax-setup")); 2291349cc55cSDimitry Andric 22920b57cec5SDimitry Andric // Set flags to support MCU ABI. 22930b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { 22940b57cec5SDimitry Andric CmdArgs.push_back("-mfloat-abi"); 22950b57cec5SDimitry Andric CmdArgs.push_back("soft"); 22960b57cec5SDimitry Andric CmdArgs.push_back("-mstack-alignment=4"); 22970b57cec5SDimitry Andric } 2298e8d8bef9SDimitry Andric 2299e8d8bef9SDimitry Andric // Handle -mtune. 2300e8d8bef9SDimitry Andric 230181ad6265SDimitry Andric // Default to "generic" unless -march is present or targetting the PS4/PS5. 2302e8d8bef9SDimitry Andric std::string TuneCPU; 2303e8d8bef9SDimitry Andric if (!Args.hasArg(clang::driver::options::OPT_march_EQ) && 230481ad6265SDimitry Andric !getToolChain().getTriple().isPS()) 2305e8d8bef9SDimitry Andric TuneCPU = "generic"; 2306e8d8bef9SDimitry Andric 2307e8d8bef9SDimitry Andric // Override based on -mtune. 2308e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) { 2309e8d8bef9SDimitry Andric StringRef Name = A->getValue(); 2310e8d8bef9SDimitry Andric 2311e8d8bef9SDimitry Andric if (Name == "native") { 2312e8d8bef9SDimitry Andric Name = llvm::sys::getHostCPUName(); 2313e8d8bef9SDimitry Andric if (!Name.empty()) 2314e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2315e8d8bef9SDimitry Andric } else 2316e8d8bef9SDimitry Andric TuneCPU = std::string(Name); 2317e8d8bef9SDimitry Andric } 2318e8d8bef9SDimitry Andric 2319e8d8bef9SDimitry Andric if (!TuneCPU.empty()) { 2320e8d8bef9SDimitry Andric CmdArgs.push_back("-tune-cpu"); 2321e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TuneCPU)); 2322e8d8bef9SDimitry Andric } 23230b57cec5SDimitry Andric } 23240b57cec5SDimitry Andric 23250b57cec5SDimitry Andric void Clang::AddHexagonTargetArgs(const ArgList &Args, 23260b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23270b57cec5SDimitry Andric CmdArgs.push_back("-mqdsp6-compat"); 23280b57cec5SDimitry Andric CmdArgs.push_back("-Wreturn-type"); 23290b57cec5SDimitry Andric 23300b57cec5SDimitry Andric if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) { 23310b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23320b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-hexagon-small-data-threshold=" + 2333753f127fSDimitry Andric Twine(G.value()))); 23340b57cec5SDimitry Andric } 23350b57cec5SDimitry Andric 23360b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_short_enums)) 23370b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 23380b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 23390b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23400b57cec5SDimitry Andric CmdArgs.push_back("-enable-hexagon-ieee-rnd-near"); 23410b57cec5SDimitry Andric } 23420b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 23430b57cec5SDimitry Andric CmdArgs.push_back("-machine-sink-split=0"); 23440b57cec5SDimitry Andric } 23450b57cec5SDimitry Andric 23460b57cec5SDimitry Andric void Clang::AddLanaiTargetArgs(const ArgList &Args, 23470b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23480b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 23490b57cec5SDimitry Andric StringRef CPUName = A->getValue(); 23500b57cec5SDimitry Andric 23510b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 23520b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPUName)); 23530b57cec5SDimitry Andric } 23540b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 23550b57cec5SDimitry Andric StringRef Value = A->getValue(); 23560b57cec5SDimitry Andric // Only support mregparm=4 to support old usage. Report error for all other 23570b57cec5SDimitry Andric // cases. 23580b57cec5SDimitry Andric int Mregparm; 23590b57cec5SDimitry Andric if (Value.getAsInteger(10, Mregparm)) { 23600b57cec5SDimitry Andric if (Mregparm != 4) { 23610b57cec5SDimitry Andric getToolChain().getDriver().Diag( 23620b57cec5SDimitry Andric diag::err_drv_unsupported_option_argument) 23630b57cec5SDimitry Andric << A->getOption().getName() << Value; 23640b57cec5SDimitry Andric } 23650b57cec5SDimitry Andric } 23660b57cec5SDimitry Andric } 23670b57cec5SDimitry Andric } 23680b57cec5SDimitry Andric 23690b57cec5SDimitry Andric void Clang::AddWebAssemblyTargetArgs(const ArgList &Args, 23700b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 23710b57cec5SDimitry Andric // Default to "hidden" visibility. 23720b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fvisibility_EQ, 23730b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 23740b57cec5SDimitry Andric CmdArgs.push_back("-fvisibility"); 23750b57cec5SDimitry Andric CmdArgs.push_back("hidden"); 23760b57cec5SDimitry Andric } 23770b57cec5SDimitry Andric } 23780b57cec5SDimitry Andric 23795ffd83dbSDimitry Andric void Clang::AddVETargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 23805ffd83dbSDimitry Andric // Floating point operations and argument passing are hard. 23815ffd83dbSDimitry Andric CmdArgs.push_back("-mfloat-abi"); 23825ffd83dbSDimitry Andric CmdArgs.push_back("hard"); 23835ffd83dbSDimitry Andric } 23845ffd83dbSDimitry Andric 23850b57cec5SDimitry Andric void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename, 23860b57cec5SDimitry Andric StringRef Target, const InputInfo &Output, 23870b57cec5SDimitry Andric const InputInfo &Input, const ArgList &Args) const { 23880b57cec5SDimitry Andric // If this is a dry run, do not create the compilation database file. 23890b57cec5SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 23900b57cec5SDimitry Andric return; 23910b57cec5SDimitry Andric 23920b57cec5SDimitry Andric using llvm::yaml::escape; 23930b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 23940b57cec5SDimitry Andric 23950b57cec5SDimitry Andric if (!CompilationDatabase) { 23960b57cec5SDimitry Andric std::error_code EC; 2397fe6060f1SDimitry Andric auto File = std::make_unique<llvm::raw_fd_ostream>( 239881ad6265SDimitry Andric Filename, EC, 239981ad6265SDimitry Andric llvm::sys::fs::OF_TextWithCRLF | llvm::sys::fs::OF_Append); 24000b57cec5SDimitry Andric if (EC) { 24010b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_compilationdatabase) << Filename 24020b57cec5SDimitry Andric << EC.message(); 24030b57cec5SDimitry Andric return; 24040b57cec5SDimitry Andric } 24050b57cec5SDimitry Andric CompilationDatabase = std::move(File); 24060b57cec5SDimitry Andric } 24070b57cec5SDimitry Andric auto &CDB = *CompilationDatabase; 2408a7dea167SDimitry Andric auto CWD = D.getVFS().getCurrentWorkingDirectory(); 2409a7dea167SDimitry Andric if (!CWD) 2410a7dea167SDimitry Andric CWD = "."; 2411a7dea167SDimitry Andric CDB << "{ \"directory\": \"" << escape(*CWD) << "\""; 24120b57cec5SDimitry Andric CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\""; 24130b57cec5SDimitry Andric CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\""; 24140b57cec5SDimitry Andric CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\""; 2415a7dea167SDimitry Andric SmallString<128> Buf; 24160b57cec5SDimitry Andric Buf = "-x"; 24170b57cec5SDimitry Andric Buf += types::getTypeName(Input.getType()); 24180b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 24190b57cec5SDimitry Andric if (!D.SysRoot.empty() && !Args.hasArg(options::OPT__sysroot_EQ)) { 24200b57cec5SDimitry Andric Buf = "--sysroot="; 24210b57cec5SDimitry Andric Buf += D.SysRoot; 24220b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\""; 24230b57cec5SDimitry Andric } 24240b57cec5SDimitry Andric CDB << ", \"" << escape(Input.getFilename()) << "\""; 242581ad6265SDimitry Andric CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\""; 24260b57cec5SDimitry Andric for (auto &A: Args) { 24270b57cec5SDimitry Andric auto &O = A->getOption(); 24280b57cec5SDimitry Andric // Skip language selection, which is positional. 24290b57cec5SDimitry Andric if (O.getID() == options::OPT_x) 24300b57cec5SDimitry Andric continue; 24310b57cec5SDimitry Andric // Skip writing dependency output and the compilation database itself. 24320b57cec5SDimitry Andric if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group) 24330b57cec5SDimitry Andric continue; 2434a7dea167SDimitry Andric if (O.getID() == options::OPT_gen_cdb_fragment_path) 2435a7dea167SDimitry Andric continue; 24360b57cec5SDimitry Andric // Skip inputs. 24370b57cec5SDimitry Andric if (O.getKind() == Option::InputClass) 24380b57cec5SDimitry Andric continue; 243981ad6265SDimitry Andric // Skip output. 244081ad6265SDimitry Andric if (O.getID() == options::OPT_o) 244181ad6265SDimitry Andric continue; 24420b57cec5SDimitry Andric // All other arguments are quoted and appended. 24430b57cec5SDimitry Andric ArgStringList ASL; 24440b57cec5SDimitry Andric A->render(Args, ASL); 24450b57cec5SDimitry Andric for (auto &it: ASL) 24460b57cec5SDimitry Andric CDB << ", \"" << escape(it) << "\""; 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric Buf = "--target="; 24490b57cec5SDimitry Andric Buf += Target; 24500b57cec5SDimitry Andric CDB << ", \"" << escape(Buf) << "\"]},\n"; 24510b57cec5SDimitry Andric } 24520b57cec5SDimitry Andric 2453a7dea167SDimitry Andric void Clang::DumpCompilationDatabaseFragmentToDir( 2454a7dea167SDimitry Andric StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output, 2455a7dea167SDimitry Andric const InputInfo &Input, const llvm::opt::ArgList &Args) const { 2456a7dea167SDimitry Andric // If this is a dry run, do not create the compilation database file. 2457a7dea167SDimitry Andric if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) 2458a7dea167SDimitry Andric return; 2459a7dea167SDimitry Andric 2460a7dea167SDimitry Andric if (CompilationDatabase) 2461a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2462a7dea167SDimitry Andric 2463a7dea167SDimitry Andric SmallString<256> Path = Dir; 2464a7dea167SDimitry Andric const auto &Driver = C.getDriver(); 2465a7dea167SDimitry Andric Driver.getVFS().makeAbsolute(Path); 2466a7dea167SDimitry Andric auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true); 2467a7dea167SDimitry Andric if (Err) { 2468a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message(); 2469a7dea167SDimitry Andric return; 2470a7dea167SDimitry Andric } 2471a7dea167SDimitry Andric 2472a7dea167SDimitry Andric llvm::sys::path::append( 2473a7dea167SDimitry Andric Path, 2474a7dea167SDimitry Andric Twine(llvm::sys::path::filename(Input.getFilename())) + ".%%%%.json"); 2475a7dea167SDimitry Andric int FD; 2476a7dea167SDimitry Andric SmallString<256> TempPath; 2477fe6060f1SDimitry Andric Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath, 2478fe6060f1SDimitry Andric llvm::sys::fs::OF_Text); 2479a7dea167SDimitry Andric if (Err) { 2480a7dea167SDimitry Andric Driver.Diag(diag::err_drv_compilationdatabase) << Path << Err.message(); 2481a7dea167SDimitry Andric return; 2482a7dea167SDimitry Andric } 2483a7dea167SDimitry Andric CompilationDatabase = 2484a7dea167SDimitry Andric std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true); 2485a7dea167SDimitry Andric DumpCompilationDatabase(C, "", Target, Output, Input, Args); 2486a7dea167SDimitry Andric } 2487a7dea167SDimitry Andric 2488fe6060f1SDimitry Andric static bool CheckARMImplicitITArg(StringRef Value) { 2489fe6060f1SDimitry Andric return Value == "always" || Value == "never" || Value == "arm" || 2490fe6060f1SDimitry Andric Value == "thumb"; 2491fe6060f1SDimitry Andric } 2492fe6060f1SDimitry Andric 2493fe6060f1SDimitry Andric static void AddARMImplicitITArgs(const ArgList &Args, ArgStringList &CmdArgs, 2494fe6060f1SDimitry Andric StringRef Value) { 2495fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 2496fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-arm-implicit-it=" + Value)); 2497fe6060f1SDimitry Andric } 2498fe6060f1SDimitry Andric 24990b57cec5SDimitry Andric static void CollectArgsForIntegratedAssembler(Compilation &C, 25000b57cec5SDimitry Andric const ArgList &Args, 25010b57cec5SDimitry Andric ArgStringList &CmdArgs, 25020b57cec5SDimitry Andric const Driver &D) { 25030b57cec5SDimitry Andric if (UseRelaxAll(C, Args)) 25040b57cec5SDimitry Andric CmdArgs.push_back("-mrelax-all"); 25050b57cec5SDimitry Andric 25060b57cec5SDimitry Andric // Only default to -mincremental-linker-compatible if we think we are 25070b57cec5SDimitry Andric // targeting the MSVC linker. 25080b57cec5SDimitry Andric bool DefaultIncrementalLinkerCompatible = 25090b57cec5SDimitry Andric C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(); 25100b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mincremental_linker_compatible, 25110b57cec5SDimitry Andric options::OPT_mno_incremental_linker_compatible, 25120b57cec5SDimitry Andric DefaultIncrementalLinkerCompatible)) 25130b57cec5SDimitry Andric CmdArgs.push_back("-mincremental-linker-compatible"); 25140b57cec5SDimitry Andric 251581ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_dwarf_unwind_EQ); 251681ad6265SDimitry Andric 2517a7dea167SDimitry Andric // If you add more args here, also add them to the block below that 2518a7dea167SDimitry Andric // starts with "// If CollectArgsForIntegratedAssembler() isn't called below". 2519a7dea167SDimitry Andric 25200b57cec5SDimitry Andric // When passing -I arguments to the assembler we sometimes need to 25210b57cec5SDimitry Andric // unconditionally take the next argument. For example, when parsing 25220b57cec5SDimitry Andric // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the 25230b57cec5SDimitry Andric // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo' 25240b57cec5SDimitry Andric // arg after parsing the '-I' arg. 25250b57cec5SDimitry Andric bool TakeNextArg = false; 25260b57cec5SDimitry Andric 25270b57cec5SDimitry Andric bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations(); 2528349cc55cSDimitry Andric bool UseNoExecStack = false; 25290b57cec5SDimitry Andric const char *MipsTargetFeature = nullptr; 2530fe6060f1SDimitry Andric StringRef ImplicitIt; 25310b57cec5SDimitry Andric for (const Arg *A : 2532fe6060f1SDimitry Andric Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler, 2533fe6060f1SDimitry Andric options::OPT_mimplicit_it_EQ)) { 25340b57cec5SDimitry Andric A->claim(); 25350b57cec5SDimitry Andric 2536fe6060f1SDimitry Andric if (A->getOption().getID() == options::OPT_mimplicit_it_EQ) { 2537fe6060f1SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 2538fe6060f1SDimitry Andric case llvm::Triple::arm: 2539fe6060f1SDimitry Andric case llvm::Triple::armeb: 2540fe6060f1SDimitry Andric case llvm::Triple::thumb: 2541fe6060f1SDimitry Andric case llvm::Triple::thumbeb: 2542fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2543fe6060f1SDimitry Andric ImplicitIt = A->getValue(); 2544fe6060f1SDimitry Andric if (!CheckARMImplicitITArg(ImplicitIt)) 2545fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2546fe6060f1SDimitry Andric << A->getOption().getName() << ImplicitIt; 2547fe6060f1SDimitry Andric continue; 2548fe6060f1SDimitry Andric default: 2549fe6060f1SDimitry Andric break; 2550fe6060f1SDimitry Andric } 2551fe6060f1SDimitry Andric } 2552fe6060f1SDimitry Andric 25530b57cec5SDimitry Andric for (StringRef Value : A->getValues()) { 25540b57cec5SDimitry Andric if (TakeNextArg) { 25550b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 25560b57cec5SDimitry Andric TakeNextArg = false; 25570b57cec5SDimitry Andric continue; 25580b57cec5SDimitry Andric } 25590b57cec5SDimitry Andric 25600b57cec5SDimitry Andric if (C.getDefaultToolChain().getTriple().isOSBinFormatCOFF() && 25610b57cec5SDimitry Andric Value == "-mbig-obj") 25620b57cec5SDimitry Andric continue; // LLVM handles bigobj automatically 25630b57cec5SDimitry Andric 25640b57cec5SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 25650b57cec5SDimitry Andric default: 25660b57cec5SDimitry Andric break; 25670b57cec5SDimitry Andric case llvm::Triple::thumb: 25680b57cec5SDimitry Andric case llvm::Triple::thumbeb: 25690b57cec5SDimitry Andric case llvm::Triple::arm: 25700b57cec5SDimitry Andric case llvm::Triple::armeb: 2571fe6060f1SDimitry Andric if (Value.startswith("-mimplicit-it=")) { 2572fe6060f1SDimitry Andric // Only store the value; the last value set takes effect. 2573fe6060f1SDimitry Andric ImplicitIt = Value.split("=").second; 2574fe6060f1SDimitry Andric if (CheckARMImplicitITArg(ImplicitIt)) 2575fe6060f1SDimitry Andric continue; 2576fe6060f1SDimitry Andric } 25770b57cec5SDimitry Andric if (Value == "-mthumb") 25780b57cec5SDimitry Andric // -mthumb has already been processed in ComputeLLVMTriple() 25790b57cec5SDimitry Andric // recognize but skip over here. 25800b57cec5SDimitry Andric continue; 25810b57cec5SDimitry Andric break; 25820b57cec5SDimitry Andric case llvm::Triple::mips: 25830b57cec5SDimitry Andric case llvm::Triple::mipsel: 25840b57cec5SDimitry Andric case llvm::Triple::mips64: 25850b57cec5SDimitry Andric case llvm::Triple::mips64el: 25860b57cec5SDimitry Andric if (Value == "--trap") { 25870b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25880b57cec5SDimitry Andric CmdArgs.push_back("+use-tcc-in-div"); 25890b57cec5SDimitry Andric continue; 25900b57cec5SDimitry Andric } 25910b57cec5SDimitry Andric if (Value == "--break") { 25920b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25930b57cec5SDimitry Andric CmdArgs.push_back("-use-tcc-in-div"); 25940b57cec5SDimitry Andric continue; 25950b57cec5SDimitry Andric } 25960b57cec5SDimitry Andric if (Value.startswith("-msoft-float")) { 25970b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 25980b57cec5SDimitry Andric CmdArgs.push_back("+soft-float"); 25990b57cec5SDimitry Andric continue; 26000b57cec5SDimitry Andric } 26010b57cec5SDimitry Andric if (Value.startswith("-mhard-float")) { 26020b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 26030b57cec5SDimitry Andric CmdArgs.push_back("-soft-float"); 26040b57cec5SDimitry Andric continue; 26050b57cec5SDimitry Andric } 26060b57cec5SDimitry Andric 26070b57cec5SDimitry Andric MipsTargetFeature = llvm::StringSwitch<const char *>(Value) 26080b57cec5SDimitry Andric .Case("-mips1", "+mips1") 26090b57cec5SDimitry Andric .Case("-mips2", "+mips2") 26100b57cec5SDimitry Andric .Case("-mips3", "+mips3") 26110b57cec5SDimitry Andric .Case("-mips4", "+mips4") 26120b57cec5SDimitry Andric .Case("-mips5", "+mips5") 26130b57cec5SDimitry Andric .Case("-mips32", "+mips32") 26140b57cec5SDimitry Andric .Case("-mips32r2", "+mips32r2") 26150b57cec5SDimitry Andric .Case("-mips32r3", "+mips32r3") 26160b57cec5SDimitry Andric .Case("-mips32r5", "+mips32r5") 26170b57cec5SDimitry Andric .Case("-mips32r6", "+mips32r6") 26180b57cec5SDimitry Andric .Case("-mips64", "+mips64") 26190b57cec5SDimitry Andric .Case("-mips64r2", "+mips64r2") 26200b57cec5SDimitry Andric .Case("-mips64r3", "+mips64r3") 26210b57cec5SDimitry Andric .Case("-mips64r5", "+mips64r5") 26220b57cec5SDimitry Andric .Case("-mips64r6", "+mips64r6") 26230b57cec5SDimitry Andric .Default(nullptr); 26240b57cec5SDimitry Andric if (MipsTargetFeature) 26250b57cec5SDimitry Andric continue; 26260b57cec5SDimitry Andric } 26270b57cec5SDimitry Andric 26280b57cec5SDimitry Andric if (Value == "-force_cpusubtype_ALL") { 26290b57cec5SDimitry Andric // Do nothing, this is the default and we don't support anything else. 26300b57cec5SDimitry Andric } else if (Value == "-L") { 26310b57cec5SDimitry Andric CmdArgs.push_back("-msave-temp-labels"); 26320b57cec5SDimitry Andric } else if (Value == "--fatal-warnings") { 26330b57cec5SDimitry Andric CmdArgs.push_back("-massembler-fatal-warnings"); 2634a7dea167SDimitry Andric } else if (Value == "--no-warn" || Value == "-W") { 2635a7dea167SDimitry Andric CmdArgs.push_back("-massembler-no-warn"); 26360b57cec5SDimitry Andric } else if (Value == "--noexecstack") { 26370b57cec5SDimitry Andric UseNoExecStack = true; 26380b57cec5SDimitry Andric } else if (Value.startswith("-compress-debug-sections") || 26390b57cec5SDimitry Andric Value.startswith("--compress-debug-sections") || 26400b57cec5SDimitry Andric Value == "-nocompress-debug-sections" || 26410b57cec5SDimitry Andric Value == "--nocompress-debug-sections") { 26420b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26430b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=yes" || 26440b57cec5SDimitry Andric Value == "--mrelax-relocations=yes") { 26450b57cec5SDimitry Andric UseRelaxRelocations = true; 26460b57cec5SDimitry Andric } else if (Value == "-mrelax-relocations=no" || 26470b57cec5SDimitry Andric Value == "--mrelax-relocations=no") { 26480b57cec5SDimitry Andric UseRelaxRelocations = false; 26490b57cec5SDimitry Andric } else if (Value.startswith("-I")) { 26500b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26510b57cec5SDimitry Andric // We need to consume the next argument if the current arg is a plain 26520b57cec5SDimitry Andric // -I. The next arg will be the include directory. 26530b57cec5SDimitry Andric if (Value == "-I") 26540b57cec5SDimitry Andric TakeNextArg = true; 26550b57cec5SDimitry Andric } else if (Value.startswith("-gdwarf-")) { 26560b57cec5SDimitry Andric // "-gdwarf-N" options are not cc1as options. 26570b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNum(Value); 26580b57cec5SDimitry Andric if (DwarfVersion == 0) { // Send it onward, and let cc1as complain. 26590b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26600b57cec5SDimitry Andric } else { 26610b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, 2662fe6060f1SDimitry Andric codegenoptions::DebugInfoConstructor, 26630b57cec5SDimitry Andric DwarfVersion, llvm::DebuggerKind::Default); 26640b57cec5SDimitry Andric } 26650b57cec5SDimitry Andric } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") || 26660b57cec5SDimitry Andric Value.startswith("-mhwdiv") || Value.startswith("-march")) { 26670b57cec5SDimitry Andric // Do nothing, we'll validate it later. 26680b57cec5SDimitry Andric } else if (Value == "-defsym") { 26690b57cec5SDimitry Andric if (A->getNumValues() != 2) { 26700b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << Value; 26710b57cec5SDimitry Andric break; 26720b57cec5SDimitry Andric } 26730b57cec5SDimitry Andric const char *S = A->getValue(1); 26740b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 26750b57cec5SDimitry Andric auto Sym = Pair.first; 26760b57cec5SDimitry Andric auto SVal = Pair.second; 26770b57cec5SDimitry Andric 26780b57cec5SDimitry Andric if (Sym.empty() || SVal.empty()) { 26790b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_format) << S; 26800b57cec5SDimitry Andric break; 26810b57cec5SDimitry Andric } 26820b57cec5SDimitry Andric int64_t IVal; 26830b57cec5SDimitry Andric if (SVal.getAsInteger(0, IVal)) { 26840b57cec5SDimitry Andric D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; 26850b57cec5SDimitry Andric break; 26860b57cec5SDimitry Andric } 26870b57cec5SDimitry Andric CmdArgs.push_back(Value.data()); 26880b57cec5SDimitry Andric TakeNextArg = true; 26890b57cec5SDimitry Andric } else if (Value == "-fdebug-compilation-dir") { 26900b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 26910b57cec5SDimitry Andric TakeNextArg = true; 2692480093f4SDimitry Andric } else if (Value.consume_front("-fdebug-compilation-dir=")) { 2693480093f4SDimitry Andric // The flag is a -Wa / -Xassembler argument and Options doesn't 2694480093f4SDimitry Andric // parse the argument, so this isn't automatically aliased to 2695480093f4SDimitry Andric // -fdebug-compilation-dir (without '=') here. 2696480093f4SDimitry Andric CmdArgs.push_back("-fdebug-compilation-dir"); 2697480093f4SDimitry Andric CmdArgs.push_back(Value.data()); 2698fe6060f1SDimitry Andric } else if (Value == "--version") { 2699fe6060f1SDimitry Andric D.PrintVersion(C, llvm::outs()); 27000b57cec5SDimitry Andric } else { 27010b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 27020b57cec5SDimitry Andric << A->getOption().getName() << Value; 27030b57cec5SDimitry Andric } 27040b57cec5SDimitry Andric } 27050b57cec5SDimitry Andric } 2706fe6060f1SDimitry Andric if (ImplicitIt.size()) 2707fe6060f1SDimitry Andric AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt); 27080b57cec5SDimitry Andric if (UseRelaxRelocations) 27090b57cec5SDimitry Andric CmdArgs.push_back("--mrelax-relocations"); 27100b57cec5SDimitry Andric if (UseNoExecStack) 27110b57cec5SDimitry Andric CmdArgs.push_back("-mnoexecstack"); 27120b57cec5SDimitry Andric if (MipsTargetFeature != nullptr) { 27130b57cec5SDimitry Andric CmdArgs.push_back("-target-feature"); 27140b57cec5SDimitry Andric CmdArgs.push_back(MipsTargetFeature); 27150b57cec5SDimitry Andric } 27160b57cec5SDimitry Andric 27170b57cec5SDimitry Andric // forward -fembed-bitcode to assmebler 27180b57cec5SDimitry Andric if (C.getDriver().embedBitcodeEnabled() || 27190b57cec5SDimitry Andric C.getDriver().embedBitcodeMarkerOnly()) 27200b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 27210b57cec5SDimitry Andric } 27220b57cec5SDimitry Andric 27230b57cec5SDimitry Andric static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D, 27240b57cec5SDimitry Andric bool OFastEnabled, const ArgList &Args, 27255ffd83dbSDimitry Andric ArgStringList &CmdArgs, 27265ffd83dbSDimitry Andric const JobAction &JA) { 27270b57cec5SDimitry Andric // Handle various floating point optimization flags, mapping them to the 27280b57cec5SDimitry Andric // appropriate LLVM code generation flags. This is complicated by several 27290b57cec5SDimitry Andric // "umbrella" flags, so we do this by stepping through the flags incrementally 27300b57cec5SDimitry Andric // adjusting what we think is enabled/disabled, then at the end setting the 27310b57cec5SDimitry Andric // LLVM flags based on the final state. 27320b57cec5SDimitry Andric bool HonorINFs = true; 27330b57cec5SDimitry Andric bool HonorNaNs = true; 2734349cc55cSDimitry Andric bool ApproxFunc = false; 27350b57cec5SDimitry Andric // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 27360b57cec5SDimitry Andric bool MathErrno = TC.IsMathErrnoDefault(); 27370b57cec5SDimitry Andric bool AssociativeMath = false; 27380b57cec5SDimitry Andric bool ReciprocalMath = false; 27390b57cec5SDimitry Andric bool SignedZeros = true; 2740480093f4SDimitry Andric bool TrappingMath = false; // Implemented via -ffp-exception-behavior 2741480093f4SDimitry Andric bool TrappingMathPresent = false; // Is trapping-math in args, and not 2742480093f4SDimitry Andric // overriden by ffp-exception-behavior? 2743480093f4SDimitry Andric bool RoundingFPMath = false; 2744480093f4SDimitry Andric bool RoundingMathPresent = false; // Is rounding-math in args? 2745480093f4SDimitry Andric // -ffp-model values: strict, fast, precise 2746480093f4SDimitry Andric StringRef FPModel = ""; 2747480093f4SDimitry Andric // -ffp-exception-behavior options: strict, maytrap, ignore 2748480093f4SDimitry Andric StringRef FPExceptionBehavior = ""; 274981ad6265SDimitry Andric // -ffp-eval-method options: double, extended, source 275081ad6265SDimitry Andric StringRef FPEvalMethod = ""; 27515ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFPMath = 27525ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA); 27535ffd83dbSDimitry Andric const llvm::DenormalMode DefaultDenormalFP32Math = 27545ffd83dbSDimitry Andric TC.getDefaultDenormalModeForType(Args, JA, &llvm::APFloat::IEEEsingle()); 27555ffd83dbSDimitry Andric 27565ffd83dbSDimitry Andric llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath; 27575ffd83dbSDimitry Andric llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math; 2758349cc55cSDimitry Andric // CUDA and HIP don't rely on the frontend to pass an ffp-contract option. 2759349cc55cSDimitry Andric // If one wasn't given by the user, don't pass it here. 2760349cc55cSDimitry Andric StringRef FPContract; 2761349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 2762349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 2763349cc55cSDimitry Andric FPContract = "on"; 2764480093f4SDimitry Andric bool StrictFPModel = false; 27650b57cec5SDimitry Andric 27660b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 27670b57cec5SDimitry Andric CmdArgs.push_back("-mlimit-float-precision"); 27680b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 27690b57cec5SDimitry Andric } 27700b57cec5SDimitry Andric 27710b57cec5SDimitry Andric for (const Arg *A : Args) { 2772480093f4SDimitry Andric auto optID = A->getOption().getID(); 2773480093f4SDimitry Andric bool PreciseFPModel = false; 2774480093f4SDimitry Andric switch (optID) { 2775480093f4SDimitry Andric default: 2776480093f4SDimitry Andric break; 2777480093f4SDimitry Andric case options::OPT_ffp_model_EQ: { 2778480093f4SDimitry Andric // If -ffp-model= is seen, reset to fno-fast-math 2779480093f4SDimitry Andric HonorINFs = true; 2780480093f4SDimitry Andric HonorNaNs = true; 2781480093f4SDimitry Andric // Turning *off* -ffast-math restores the toolchain default. 2782480093f4SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 2783480093f4SDimitry Andric AssociativeMath = false; 2784480093f4SDimitry Andric ReciprocalMath = false; 2785480093f4SDimitry Andric SignedZeros = true; 2786480093f4SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 2787349cc55cSDimitry Andric FPContract = "on"; 27885ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 27895ffd83dbSDimitry Andric 27905ffd83dbSDimitry Andric // FIXME: The target may have picked a non-IEEE default mode here based on 27915ffd83dbSDimitry Andric // -cl-denorms-are-zero. Should the target consider -fp-model interaction? 27925ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 27935ffd83dbSDimitry Andric 2794480093f4SDimitry Andric StringRef Val = A->getValue(); 2795480093f4SDimitry Andric if (OFastEnabled && !Val.equals("fast")) { 2796480093f4SDimitry Andric // Only -ffp-model=fast is compatible with OFast, ignore. 2797480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2798480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val) 2799480093f4SDimitry Andric << "-Ofast"; 2800480093f4SDimitry Andric break; 2801480093f4SDimitry Andric } 2802480093f4SDimitry Andric StrictFPModel = false; 2803480093f4SDimitry Andric PreciseFPModel = true; 2804480093f4SDimitry Andric // ffp-model= is a Driver option, it is entirely rewritten into more 2805480093f4SDimitry Andric // granular options before being passed into cc1. 2806480093f4SDimitry Andric // Use the gcc option in the switch below. 2807349cc55cSDimitry Andric if (!FPModel.empty() && !FPModel.equals(Val)) 2808480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2809480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + FPModel) 2810480093f4SDimitry Andric << Args.MakeArgString("-ffp-model=" + Val); 2811480093f4SDimitry Andric if (Val.equals("fast")) { 2812480093f4SDimitry Andric optID = options::OPT_ffast_math; 2813480093f4SDimitry Andric FPModel = Val; 28146e75b2fbSDimitry Andric FPContract = "fast"; 2815480093f4SDimitry Andric } else if (Val.equals("precise")) { 2816480093f4SDimitry Andric optID = options::OPT_ffp_contract; 2817480093f4SDimitry Andric FPModel = Val; 2818349cc55cSDimitry Andric FPContract = "on"; 2819480093f4SDimitry Andric PreciseFPModel = true; 2820480093f4SDimitry Andric } else if (Val.equals("strict")) { 2821480093f4SDimitry Andric StrictFPModel = true; 2822480093f4SDimitry Andric optID = options::OPT_frounding_math; 2823480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2824480093f4SDimitry Andric FPModel = Val; 28255ffd83dbSDimitry Andric FPContract = "off"; 2826480093f4SDimitry Andric TrappingMath = true; 2827480093f4SDimitry Andric } else 2828480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2829480093f4SDimitry Andric << A->getOption().getName() << Val; 2830480093f4SDimitry Andric break; 2831480093f4SDimitry Andric } 2832480093f4SDimitry Andric } 2833480093f4SDimitry Andric 2834480093f4SDimitry Andric switch (optID) { 28350b57cec5SDimitry Andric // If this isn't an FP option skip the claim below 28360b57cec5SDimitry Andric default: continue; 28370b57cec5SDimitry Andric 28380b57cec5SDimitry Andric // Options controlling individual features 28390b57cec5SDimitry Andric case options::OPT_fhonor_infinities: HonorINFs = true; break; 28400b57cec5SDimitry Andric case options::OPT_fno_honor_infinities: HonorINFs = false; break; 28410b57cec5SDimitry Andric case options::OPT_fhonor_nans: HonorNaNs = true; break; 28420b57cec5SDimitry Andric case options::OPT_fno_honor_nans: HonorNaNs = false; break; 2843349cc55cSDimitry Andric case options::OPT_fapprox_func: ApproxFunc = true; break; 2844349cc55cSDimitry Andric case options::OPT_fno_approx_func: ApproxFunc = false; break; 28450b57cec5SDimitry Andric case options::OPT_fmath_errno: MathErrno = true; break; 28460b57cec5SDimitry Andric case options::OPT_fno_math_errno: MathErrno = false; break; 28470b57cec5SDimitry Andric case options::OPT_fassociative_math: AssociativeMath = true; break; 28480b57cec5SDimitry Andric case options::OPT_fno_associative_math: AssociativeMath = false; break; 28490b57cec5SDimitry Andric case options::OPT_freciprocal_math: ReciprocalMath = true; break; 28500b57cec5SDimitry Andric case options::OPT_fno_reciprocal_math: ReciprocalMath = false; break; 28510b57cec5SDimitry Andric case options::OPT_fsigned_zeros: SignedZeros = true; break; 28520b57cec5SDimitry Andric case options::OPT_fno_signed_zeros: SignedZeros = false; break; 2853480093f4SDimitry Andric case options::OPT_ftrapping_math: 2854480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2855480093f4SDimitry Andric !FPExceptionBehavior.equals("strict")) 2856480093f4SDimitry Andric // Warn that previous value of option is overridden. 2857480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2858480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2859480093f4SDimitry Andric << "-ftrapping-math"; 2860480093f4SDimitry Andric TrappingMath = true; 2861480093f4SDimitry Andric TrappingMathPresent = true; 2862480093f4SDimitry Andric FPExceptionBehavior = "strict"; 2863480093f4SDimitry Andric break; 2864480093f4SDimitry Andric case options::OPT_fno_trapping_math: 2865480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2866480093f4SDimitry Andric !FPExceptionBehavior.equals("ignore")) 2867480093f4SDimitry Andric // Warn that previous value of option is overridden. 2868480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2869480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2870480093f4SDimitry Andric << "-fno-trapping-math"; 2871480093f4SDimitry Andric TrappingMath = false; 2872480093f4SDimitry Andric TrappingMathPresent = true; 2873480093f4SDimitry Andric FPExceptionBehavior = "ignore"; 2874480093f4SDimitry Andric break; 2875480093f4SDimitry Andric 2876480093f4SDimitry Andric case options::OPT_frounding_math: 2877480093f4SDimitry Andric RoundingFPMath = true; 2878480093f4SDimitry Andric RoundingMathPresent = true; 2879480093f4SDimitry Andric break; 2880480093f4SDimitry Andric 2881480093f4SDimitry Andric case options::OPT_fno_rounding_math: 2882480093f4SDimitry Andric RoundingFPMath = false; 2883480093f4SDimitry Andric RoundingMathPresent = false; 2884480093f4SDimitry Andric break; 28850b57cec5SDimitry Andric 28860b57cec5SDimitry Andric case options::OPT_fdenormal_fp_math_EQ: 28875ffd83dbSDimitry Andric DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue()); 288881ad6265SDimitry Andric DenormalFP32Math = DenormalFPMath; 28895ffd83dbSDimitry Andric if (!DenormalFPMath.isValid()) { 28905ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 28915ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 28925ffd83dbSDimitry Andric } 28935ffd83dbSDimitry Andric break; 28945ffd83dbSDimitry Andric 28955ffd83dbSDimitry Andric case options::OPT_fdenormal_fp_math_f32_EQ: 28965ffd83dbSDimitry Andric DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue()); 28975ffd83dbSDimitry Andric if (!DenormalFP32Math.isValid()) { 28985ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 28995ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 29005ffd83dbSDimitry Andric } 29010b57cec5SDimitry Andric break; 29020b57cec5SDimitry Andric 2903480093f4SDimitry Andric // Validate and pass through -ffp-contract option. 29040b57cec5SDimitry Andric case options::OPT_ffp_contract: { 29050b57cec5SDimitry Andric StringRef Val = A->getValue(); 2906480093f4SDimitry Andric if (PreciseFPModel) { 2907349cc55cSDimitry Andric // -ffp-model=precise enables ffp-contract=on. 2908349cc55cSDimitry Andric // -ffp-model=precise sets PreciseFPModel to on and Val to 2909349cc55cSDimitry Andric // "precise". FPContract is set. 2910480093f4SDimitry Andric ; 2911480093f4SDimitry Andric } else if (Val.equals("fast") || Val.equals("on") || Val.equals("off")) 29120b57cec5SDimitry Andric FPContract = Val; 29130b57cec5SDimitry Andric else 29140b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 29150b57cec5SDimitry Andric << A->getOption().getName() << Val; 29160b57cec5SDimitry Andric break; 29170b57cec5SDimitry Andric } 29180b57cec5SDimitry Andric 2919480093f4SDimitry Andric // Validate and pass through -ffp-model option. 2920480093f4SDimitry Andric case options::OPT_ffp_model_EQ: 2921480093f4SDimitry Andric // This should only occur in the error case 2922480093f4SDimitry Andric // since the optID has been replaced by a more granular 2923480093f4SDimitry Andric // floating point option. 2924480093f4SDimitry Andric break; 2925480093f4SDimitry Andric 2926480093f4SDimitry Andric // Validate and pass through -ffp-exception-behavior option. 2927480093f4SDimitry Andric case options::OPT_ffp_exception_behavior_EQ: { 2928480093f4SDimitry Andric StringRef Val = A->getValue(); 2929480093f4SDimitry Andric if (!TrappingMathPresent && !FPExceptionBehavior.empty() && 2930480093f4SDimitry Andric !FPExceptionBehavior.equals(Val)) 2931480093f4SDimitry Andric // Warn that previous value of option is overridden. 2932480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 2933480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + FPExceptionBehavior) 2934480093f4SDimitry Andric << Args.MakeArgString("-ffp-exception-behavior=" + Val); 2935480093f4SDimitry Andric TrappingMath = TrappingMathPresent = false; 2936480093f4SDimitry Andric if (Val.equals("ignore") || Val.equals("maytrap")) 2937480093f4SDimitry Andric FPExceptionBehavior = Val; 2938480093f4SDimitry Andric else if (Val.equals("strict")) { 2939480093f4SDimitry Andric FPExceptionBehavior = Val; 2940480093f4SDimitry Andric TrappingMath = TrappingMathPresent = true; 2941480093f4SDimitry Andric } else 2942480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 2943480093f4SDimitry Andric << A->getOption().getName() << Val; 2944480093f4SDimitry Andric break; 2945480093f4SDimitry Andric } 2946480093f4SDimitry Andric 294781ad6265SDimitry Andric // Validate and pass through -ffp-eval-method option. 294881ad6265SDimitry Andric case options::OPT_ffp_eval_method_EQ: { 294981ad6265SDimitry Andric StringRef Val = A->getValue(); 295081ad6265SDimitry Andric if (Val.equals("double") || Val.equals("extended") || 295181ad6265SDimitry Andric Val.equals("source")) 295281ad6265SDimitry Andric FPEvalMethod = Val; 295381ad6265SDimitry Andric else 295481ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 295581ad6265SDimitry Andric << A->getOption().getName() << Val; 295681ad6265SDimitry Andric break; 295781ad6265SDimitry Andric } 295881ad6265SDimitry Andric 29590b57cec5SDimitry Andric case options::OPT_ffinite_math_only: 29600b57cec5SDimitry Andric HonorINFs = false; 29610b57cec5SDimitry Andric HonorNaNs = false; 29620b57cec5SDimitry Andric break; 29630b57cec5SDimitry Andric case options::OPT_fno_finite_math_only: 29640b57cec5SDimitry Andric HonorINFs = true; 29650b57cec5SDimitry Andric HonorNaNs = true; 29660b57cec5SDimitry Andric break; 29670b57cec5SDimitry Andric 29680b57cec5SDimitry Andric case options::OPT_funsafe_math_optimizations: 29690b57cec5SDimitry Andric AssociativeMath = true; 29700b57cec5SDimitry Andric ReciprocalMath = true; 29710b57cec5SDimitry Andric SignedZeros = false; 297204eeddc0SDimitry Andric ApproxFunc = true; 29730b57cec5SDimitry Andric TrappingMath = false; 2974480093f4SDimitry Andric FPExceptionBehavior = ""; 29750b57cec5SDimitry Andric break; 29760b57cec5SDimitry Andric case options::OPT_fno_unsafe_math_optimizations: 29770b57cec5SDimitry Andric AssociativeMath = false; 29780b57cec5SDimitry Andric ReciprocalMath = false; 29790b57cec5SDimitry Andric SignedZeros = true; 298004eeddc0SDimitry Andric ApproxFunc = false; 29810b57cec5SDimitry Andric TrappingMath = true; 2982480093f4SDimitry Andric FPExceptionBehavior = "strict"; 29835ffd83dbSDimitry Andric 29845ffd83dbSDimitry Andric // The target may have opted to flush by default, so force IEEE. 29855ffd83dbSDimitry Andric DenormalFPMath = llvm::DenormalMode::getIEEE(); 29865ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 29870b57cec5SDimitry Andric break; 29880b57cec5SDimitry Andric 29890b57cec5SDimitry Andric case options::OPT_Ofast: 29900b57cec5SDimitry Andric // If -Ofast is the optimization level, then -ffast-math should be enabled 29910b57cec5SDimitry Andric if (!OFastEnabled) 29920b57cec5SDimitry Andric continue; 29930b57cec5SDimitry Andric LLVM_FALLTHROUGH; 29940b57cec5SDimitry Andric case options::OPT_ffast_math: 29950b57cec5SDimitry Andric HonorINFs = false; 29960b57cec5SDimitry Andric HonorNaNs = false; 29970b57cec5SDimitry Andric MathErrno = false; 29980b57cec5SDimitry Andric AssociativeMath = true; 29990b57cec5SDimitry Andric ReciprocalMath = true; 300004eeddc0SDimitry Andric ApproxFunc = true; 30010b57cec5SDimitry Andric SignedZeros = false; 30020b57cec5SDimitry Andric TrappingMath = false; 3003480093f4SDimitry Andric RoundingFPMath = false; 30040b57cec5SDimitry Andric // If fast-math is set then set the fp-contract mode to fast. 30050b57cec5SDimitry Andric FPContract = "fast"; 30060b57cec5SDimitry Andric break; 30070b57cec5SDimitry Andric case options::OPT_fno_fast_math: 30080b57cec5SDimitry Andric HonorINFs = true; 30090b57cec5SDimitry Andric HonorNaNs = true; 30100b57cec5SDimitry Andric // Turning on -ffast-math (with either flag) removes the need for 30110b57cec5SDimitry Andric // MathErrno. However, turning *off* -ffast-math merely restores the 30120b57cec5SDimitry Andric // toolchain default (which may be false). 30130b57cec5SDimitry Andric MathErrno = TC.IsMathErrnoDefault(); 30140b57cec5SDimitry Andric AssociativeMath = false; 30150b57cec5SDimitry Andric ReciprocalMath = false; 301604eeddc0SDimitry Andric ApproxFunc = false; 30170b57cec5SDimitry Andric SignedZeros = true; 30180b57cec5SDimitry Andric // -fno_fast_math restores default denormal and fpcontract handling 30195ffd83dbSDimitry Andric DenormalFPMath = DefaultDenormalFPMath; 30205ffd83dbSDimitry Andric DenormalFP32Math = llvm::DenormalMode::getIEEE(); 3021349cc55cSDimitry Andric if (!JA.isDeviceOffloading(Action::OFK_Cuda) && 3022349cc55cSDimitry Andric !JA.isOffloading(Action::OFK_HIP)) 3023349cc55cSDimitry Andric if (FPContract == "fast") { 3024349cc55cSDimitry Andric FPContract = "on"; 3025349cc55cSDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3026349cc55cSDimitry Andric << "-ffp-contract=fast" 3027349cc55cSDimitry Andric << "-ffp-contract=on"; 3028349cc55cSDimitry Andric } 30290b57cec5SDimitry Andric break; 30300b57cec5SDimitry Andric } 3031480093f4SDimitry Andric if (StrictFPModel) { 3032480093f4SDimitry Andric // If -ffp-model=strict has been specified on command line but 3033480093f4SDimitry Andric // subsequent options conflict then emit warning diagnostic. 3034349cc55cSDimitry Andric if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath && 303504eeddc0SDimitry Andric SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc && 30365ffd83dbSDimitry Andric DenormalFPMath == llvm::DenormalMode::getIEEE() && 3037349cc55cSDimitry Andric DenormalFP32Math == llvm::DenormalMode::getIEEE() && 3038349cc55cSDimitry Andric FPContract.equals("off")) 3039480093f4SDimitry Andric // OK: Current Arg doesn't conflict with -ffp-model=strict 3040480093f4SDimitry Andric ; 3041480093f4SDimitry Andric else { 3042480093f4SDimitry Andric StrictFPModel = false; 3043480093f4SDimitry Andric FPModel = ""; 3044480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3045480093f4SDimitry Andric << "-ffp-model=strict" << 3046480093f4SDimitry Andric ((A->getNumValues() == 0) ? A->getSpelling() 3047480093f4SDimitry Andric : Args.MakeArgString(A->getSpelling() + A->getValue())); 3048480093f4SDimitry Andric } 3049480093f4SDimitry Andric } 30500b57cec5SDimitry Andric 30510b57cec5SDimitry Andric // If we handled this option claim it 30520b57cec5SDimitry Andric A->claim(); 30530b57cec5SDimitry Andric } 30540b57cec5SDimitry Andric 30550b57cec5SDimitry Andric if (!HonorINFs) 30560b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-infs"); 30570b57cec5SDimitry Andric 30580b57cec5SDimitry Andric if (!HonorNaNs) 30590b57cec5SDimitry Andric CmdArgs.push_back("-menable-no-nans"); 30600b57cec5SDimitry Andric 3061349cc55cSDimitry Andric if (ApproxFunc) 3062349cc55cSDimitry Andric CmdArgs.push_back("-fapprox-func"); 3063349cc55cSDimitry Andric 30640b57cec5SDimitry Andric if (MathErrno) 30650b57cec5SDimitry Andric CmdArgs.push_back("-fmath-errno"); 30660b57cec5SDimitry Andric 30670b57cec5SDimitry Andric if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros && 306804eeddc0SDimitry Andric ApproxFunc && !TrappingMath) 30690b57cec5SDimitry Andric CmdArgs.push_back("-menable-unsafe-fp-math"); 30700b57cec5SDimitry Andric 30710b57cec5SDimitry Andric if (!SignedZeros) 30720b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-zeros"); 30730b57cec5SDimitry Andric 30740b57cec5SDimitry Andric if (AssociativeMath && !SignedZeros && !TrappingMath) 30750b57cec5SDimitry Andric CmdArgs.push_back("-mreassociate"); 30760b57cec5SDimitry Andric 30770b57cec5SDimitry Andric if (ReciprocalMath) 30780b57cec5SDimitry Andric CmdArgs.push_back("-freciprocal-math"); 30790b57cec5SDimitry Andric 3080480093f4SDimitry Andric if (TrappingMath) { 3081480093f4SDimitry Andric // FP Exception Behavior is also set to strict 3082480093f4SDimitry Andric assert(FPExceptionBehavior.equals("strict")); 3083e8d8bef9SDimitry Andric } 30840b57cec5SDimitry Andric 30855ffd83dbSDimitry Andric // The default is IEEE. 30865ffd83dbSDimitry Andric if (DenormalFPMath != llvm::DenormalMode::getIEEE()) { 30875ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 30885ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 30895ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math=" << DenormalFPMath; 30905ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 30915ffd83dbSDimitry Andric } 30925ffd83dbSDimitry Andric 30935ffd83dbSDimitry Andric // Add f32 specific denormal mode flag if it's different. 30945ffd83dbSDimitry Andric if (DenormalFP32Math != DenormalFPMath) { 30955ffd83dbSDimitry Andric llvm::SmallString<64> DenormFlag; 30965ffd83dbSDimitry Andric llvm::raw_svector_ostream ArgStr(DenormFlag); 30975ffd83dbSDimitry Andric ArgStr << "-fdenormal-fp-math-f32=" << DenormalFP32Math; 30985ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(ArgStr.str())); 30995ffd83dbSDimitry Andric } 31000b57cec5SDimitry Andric 31010b57cec5SDimitry Andric if (!FPContract.empty()) 31020b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract)); 31030b57cec5SDimitry Andric 3104480093f4SDimitry Andric if (!RoundingFPMath) 3105480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fno-rounding-math")); 3106480093f4SDimitry Andric 3107480093f4SDimitry Andric if (RoundingFPMath && RoundingMathPresent) 3108480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frounding-math")); 3109480093f4SDimitry Andric 3110480093f4SDimitry Andric if (!FPExceptionBehavior.empty()) 3111480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-exception-behavior=" + 3112480093f4SDimitry Andric FPExceptionBehavior)); 3113480093f4SDimitry Andric 311481ad6265SDimitry Andric if (!FPEvalMethod.empty()) 311581ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-eval-method=" + FPEvalMethod)); 311681ad6265SDimitry Andric 31170b57cec5SDimitry Andric ParseMRecip(D, Args, CmdArgs); 31180b57cec5SDimitry Andric 31190b57cec5SDimitry Andric // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for the 31200b57cec5SDimitry Andric // individual features enabled by -ffast-math instead of the option itself as 31210b57cec5SDimitry Andric // that's consistent with gcc's behaviour. 312204eeddc0SDimitry Andric if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc && 3123480093f4SDimitry Andric ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) { 31240b57cec5SDimitry Andric CmdArgs.push_back("-ffast-math"); 3125480093f4SDimitry Andric if (FPModel.equals("fast")) { 3126480093f4SDimitry Andric if (FPContract.equals("fast")) 3127480093f4SDimitry Andric // All set, do nothing. 3128480093f4SDimitry Andric ; 3129480093f4SDimitry Andric else if (FPContract.empty()) 3130480093f4SDimitry Andric // Enable -ffp-contract=fast 3131480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 3132480093f4SDimitry Andric else 3133480093f4SDimitry Andric D.Diag(clang::diag::warn_drv_overriding_flag_option) 3134480093f4SDimitry Andric << "-ffp-model=fast" 3135480093f4SDimitry Andric << Args.MakeArgString("-ffp-contract=" + FPContract); 3136480093f4SDimitry Andric } 3137480093f4SDimitry Andric } 31380b57cec5SDimitry Andric 31390b57cec5SDimitry Andric // Handle __FINITE_MATH_ONLY__ similarly. 31400b57cec5SDimitry Andric if (!HonorINFs && !HonorNaNs) 31410b57cec5SDimitry Andric CmdArgs.push_back("-ffinite-math-only"); 31420b57cec5SDimitry Andric 31430b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) { 31440b57cec5SDimitry Andric CmdArgs.push_back("-mfpmath"); 31450b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 31460b57cec5SDimitry Andric } 31470b57cec5SDimitry Andric 31480b57cec5SDimitry Andric // Disable a codegen optimization for floating-point casts. 31490b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_strict_float_cast_overflow, 31500b57cec5SDimitry Andric options::OPT_fstrict_float_cast_overflow, false)) 31510b57cec5SDimitry Andric CmdArgs.push_back("-fno-strict-float-cast-overflow"); 31520b57cec5SDimitry Andric } 31530b57cec5SDimitry Andric 31540b57cec5SDimitry Andric static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs, 31550b57cec5SDimitry Andric const llvm::Triple &Triple, 31560b57cec5SDimitry Andric const InputInfo &Input) { 31570b57cec5SDimitry Andric // Add default argument set. 31580b57cec5SDimitry Andric if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 31590b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=core"); 31600b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=apiModeling"); 31610b57cec5SDimitry Andric 31620b57cec5SDimitry Andric if (!Triple.isWindowsMSVCEnvironment()) { 31630b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix"); 31640b57cec5SDimitry Andric } else { 31650b57cec5SDimitry Andric // Enable "unix" checkers that also work on Windows. 31660b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.API"); 31670b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.Malloc"); 31680b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MallocSizeof"); 31690b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.MismatchedDeallocator"); 31700b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.BadSizeArg"); 31710b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=unix.cstring.NullArg"); 31720b57cec5SDimitry Andric } 31730b57cec5SDimitry Andric 317481ad6265SDimitry Andric // Disable some unix checkers for PS4/PS5. 317581ad6265SDimitry Andric if (Triple.isPS()) { 31760b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.API"); 31770b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-disable-checker=unix.Vfork"); 31780b57cec5SDimitry Andric } 31790b57cec5SDimitry Andric 3180480093f4SDimitry Andric if (Triple.isOSDarwin()) { 31810b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=osx"); 3182480093f4SDimitry Andric CmdArgs.push_back( 3183480093f4SDimitry Andric "-analyzer-checker=security.insecureAPI.decodeValueOfObjCType"); 3184480093f4SDimitry Andric } 3185480093f4SDimitry Andric else if (Triple.isOSFuchsia()) 3186480093f4SDimitry Andric CmdArgs.push_back("-analyzer-checker=fuchsia"); 31870b57cec5SDimitry Andric 31880b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=deadcode"); 31890b57cec5SDimitry Andric 31900b57cec5SDimitry Andric if (types::isCXX(Input.getType())) 31910b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=cplusplus"); 31920b57cec5SDimitry Andric 319381ad6265SDimitry Andric if (!Triple.isPS()) { 31940b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 31950b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 31960b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 31970b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 31980b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 31990b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 32000b57cec5SDimitry Andric } 32010b57cec5SDimitry Andric 32020b57cec5SDimitry Andric // Default nullability checks. 32030b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullPassedToNonnull"); 32040b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-checker=nullability.NullReturnedFromNonnull"); 32050b57cec5SDimitry Andric } 32060b57cec5SDimitry Andric 32070b57cec5SDimitry Andric // Set the output format. The default is plist, for (lame) historical reasons. 32080b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-output"); 32090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 32100b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 32110b57cec5SDimitry Andric else 32120b57cec5SDimitry Andric CmdArgs.push_back("plist"); 32130b57cec5SDimitry Andric 32140b57cec5SDimitry Andric // Disable the presentation of standard compiler warnings when using 32150b57cec5SDimitry Andric // --analyze. We only want to show static analyzer diagnostics or frontend 32160b57cec5SDimitry Andric // errors. 32170b57cec5SDimitry Andric CmdArgs.push_back("-w"); 32180b57cec5SDimitry Andric 32190b57cec5SDimitry Andric // Add -Xanalyzer arguments when running as analyzer. 32200b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 32210b57cec5SDimitry Andric } 32220b57cec5SDimitry Andric 3223753f127fSDimitry Andric static bool isValidSymbolName(StringRef S) { 3224753f127fSDimitry Andric if (S.empty()) 3225753f127fSDimitry Andric return false; 3226753f127fSDimitry Andric 3227753f127fSDimitry Andric if (std::isdigit(S[0])) 3228753f127fSDimitry Andric return false; 3229753f127fSDimitry Andric 3230753f127fSDimitry Andric return llvm::all_of(S, [](char C) { return std::isalnum(C) || C == '_'; }); 3231753f127fSDimitry Andric } 3232753f127fSDimitry Andric 3233e8d8bef9SDimitry Andric static void RenderSSPOptions(const Driver &D, const ToolChain &TC, 3234e8d8bef9SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 3235e8d8bef9SDimitry Andric bool KernelOrKext) { 32360b57cec5SDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 32370b57cec5SDimitry Andric 32380b57cec5SDimitry Andric // NVPTX doesn't support stack protectors; from the compiler's perspective, it 32390b57cec5SDimitry Andric // doesn't even have a stack! 32400b57cec5SDimitry Andric if (EffectiveTriple.isNVPTX()) 32410b57cec5SDimitry Andric return; 32420b57cec5SDimitry Andric 32430b57cec5SDimitry Andric // -stack-protector=0 is default. 3244e8d8bef9SDimitry Andric LangOptions::StackProtectorMode StackProtectorLevel = LangOptions::SSPOff; 3245e8d8bef9SDimitry Andric LangOptions::StackProtectorMode DefaultStackProtectorLevel = 32460b57cec5SDimitry Andric TC.GetDefaultStackProtectorLevel(KernelOrKext); 32470b57cec5SDimitry Andric 32480b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 32490b57cec5SDimitry Andric options::OPT_fstack_protector_all, 32500b57cec5SDimitry Andric options::OPT_fstack_protector_strong, 32510b57cec5SDimitry Andric options::OPT_fstack_protector)) { 32520b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fstack_protector)) 32530b57cec5SDimitry Andric StackProtectorLevel = 3254e8d8bef9SDimitry Andric std::max<>(LangOptions::SSPOn, DefaultStackProtectorLevel); 32550b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_strong)) 32560b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPStrong; 32570b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_fstack_protector_all)) 32580b57cec5SDimitry Andric StackProtectorLevel = LangOptions::SSPReq; 32590b57cec5SDimitry Andric } else { 32600b57cec5SDimitry Andric StackProtectorLevel = DefaultStackProtectorLevel; 32610b57cec5SDimitry Andric } 32620b57cec5SDimitry Andric 32630b57cec5SDimitry Andric if (StackProtectorLevel) { 32640b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 32650b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 32660b57cec5SDimitry Andric } 32670b57cec5SDimitry Andric 32680b57cec5SDimitry Andric // --param ssp-buffer-size= 32690b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT__param)) { 32700b57cec5SDimitry Andric StringRef Str(A->getValue()); 32710b57cec5SDimitry Andric if (Str.startswith("ssp-buffer-size=")) { 32720b57cec5SDimitry Andric if (StackProtectorLevel) { 32730b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector-buffer-size"); 32740b57cec5SDimitry Andric // FIXME: Verify the argument is a valid integer. 32750b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 32760b57cec5SDimitry Andric } 32770b57cec5SDimitry Andric A->claim(); 32780b57cec5SDimitry Andric } 32790b57cec5SDimitry Andric } 3280e8d8bef9SDimitry Andric 3281e8d8bef9SDimitry Andric const std::string &TripleStr = EffectiveTriple.getTriple(); 3282e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) { 3283e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3284349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3285349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3286e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3287e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3288349cc55cSDimitry Andric if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() || 3289349cc55cSDimitry Andric EffectiveTriple.isThumb()) && 3290349cc55cSDimitry Andric Value != "tls" && Value != "global") { 3291e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3292fe6060f1SDimitry Andric << A->getOption().getName() << Value << "tls global"; 3293fe6060f1SDimitry Andric return; 3294fe6060f1SDimitry Andric } 3295349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3296349cc55cSDimitry Andric Value == "tls") { 3297349cc55cSDimitry Andric if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3298349cc55cSDimitry Andric D.Diag(diag::err_drv_ssp_missing_offset_argument) 3299349cc55cSDimitry Andric << A->getAsString(Args); 3300349cc55cSDimitry Andric return; 3301349cc55cSDimitry Andric } 3302349cc55cSDimitry Andric // Check whether the target subarch supports the hardware TLS register 330304eeddc0SDimitry Andric if (!arm::isHardTPSupported(EffectiveTriple)) { 3304349cc55cSDimitry Andric D.Diag(diag::err_target_unsupported_tp_hard) 3305349cc55cSDimitry Andric << EffectiveTriple.getArchName(); 3306349cc55cSDimitry Andric return; 3307349cc55cSDimitry Andric } 3308349cc55cSDimitry Andric // Check whether the user asked for something other than -mtp=cp15 3309349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 3310349cc55cSDimitry Andric StringRef Value = A->getValue(); 3311349cc55cSDimitry Andric if (Value != "cp15") { 3312349cc55cSDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 3313349cc55cSDimitry Andric << A->getAsString(Args) << "-mstack-protector-guard=tls"; 3314349cc55cSDimitry Andric return; 3315349cc55cSDimitry Andric } 3316349cc55cSDimitry Andric } 3317349cc55cSDimitry Andric CmdArgs.push_back("-target-feature"); 3318349cc55cSDimitry Andric CmdArgs.push_back("+read-tp-hard"); 3319349cc55cSDimitry Andric } 3320fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sysreg" && Value != "global") { 3321fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3322fe6060f1SDimitry Andric << A->getOption().getName() << Value << "sysreg global"; 3323e8d8bef9SDimitry Andric return; 3324e8d8bef9SDimitry Andric } 3325e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3326e8d8bef9SDimitry Andric } 3327e8d8bef9SDimitry Andric 3328e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) { 3329e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3330349cc55cSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() && 3331349cc55cSDimitry Andric !EffectiveTriple.isARM() && !EffectiveTriple.isThumb()) 3332e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3333e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3334fe6060f1SDimitry Andric int Offset; 3335e8d8bef9SDimitry Andric if (Value.getAsInteger(10, Offset)) { 3336e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3337e8d8bef9SDimitry Andric return; 3338e8d8bef9SDimitry Andric } 3339349cc55cSDimitry Andric if ((EffectiveTriple.isARM() || EffectiveTriple.isThumb()) && 3340349cc55cSDimitry Andric (Offset < 0 || Offset > 0xfffff)) { 3341349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 3342349cc55cSDimitry Andric << A->getOption().getName() << Value; 3343349cc55cSDimitry Andric return; 3344349cc55cSDimitry Andric } 3345e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3346e8d8bef9SDimitry Andric } 3347e8d8bef9SDimitry Andric 3348e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) { 3349e8d8bef9SDimitry Andric StringRef Value = A->getValue(); 3350fe6060f1SDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64()) 3351e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 3352e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 3353e8d8bef9SDimitry Andric if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) { 3354e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_value_with_suggestion) 3355fe6060f1SDimitry Andric << A->getOption().getName() << Value << "fs gs"; 3356fe6060f1SDimitry Andric return; 3357fe6060f1SDimitry Andric } 3358fe6060f1SDimitry Andric if (EffectiveTriple.isAArch64() && Value != "sp_el0") { 3359fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value; 3360e8d8bef9SDimitry Andric return; 3361e8d8bef9SDimitry Andric } 3362e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 3363e8d8bef9SDimitry Andric } 3364753f127fSDimitry Andric 3365753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_symbol_EQ)) { 3366753f127fSDimitry Andric StringRef Value = A->getValue(); 3367753f127fSDimitry Andric if (!isValidSymbolName(Value)) { 3368753f127fSDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 3369753f127fSDimitry Andric << A->getOption().getName() << "legal symbol name"; 3370753f127fSDimitry Andric return; 3371753f127fSDimitry Andric } 3372753f127fSDimitry Andric A->render(Args, CmdArgs); 3373753f127fSDimitry Andric } 33740b57cec5SDimitry Andric } 33750b57cec5SDimitry Andric 33765ffd83dbSDimitry Andric static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, 33775ffd83dbSDimitry Andric ArgStringList &CmdArgs) { 33785ffd83dbSDimitry Andric const llvm::Triple &EffectiveTriple = TC.getEffectiveTriple(); 33795ffd83dbSDimitry Andric 33803063e1e5SEd Maste if (!EffectiveTriple.isOSFreeBSD() && !EffectiveTriple.isOSLinux()) 33815ffd83dbSDimitry Andric return; 33825ffd83dbSDimitry Andric 33835ffd83dbSDimitry Andric if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && 33845ffd83dbSDimitry Andric !EffectiveTriple.isPPC64()) 33855ffd83dbSDimitry Andric return; 33865ffd83dbSDimitry Andric 338781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection, 338881ad6265SDimitry Andric options::OPT_fno_stack_clash_protection); 33895ffd83dbSDimitry Andric } 33905ffd83dbSDimitry Andric 33910b57cec5SDimitry Andric static void RenderTrivialAutoVarInitOptions(const Driver &D, 33920b57cec5SDimitry Andric const ToolChain &TC, 33930b57cec5SDimitry Andric const ArgList &Args, 33940b57cec5SDimitry Andric ArgStringList &CmdArgs) { 33950b57cec5SDimitry Andric auto DefaultTrivialAutoVarInit = TC.GetDefaultTrivialAutoVarInit(); 33960b57cec5SDimitry Andric StringRef TrivialAutoVarInit = ""; 33970b57cec5SDimitry Andric 33980b57cec5SDimitry Andric for (const Arg *A : Args) { 33990b57cec5SDimitry Andric switch (A->getOption().getID()) { 34000b57cec5SDimitry Andric default: 34010b57cec5SDimitry Andric continue; 34020b57cec5SDimitry Andric case options::OPT_ftrivial_auto_var_init: { 34030b57cec5SDimitry Andric A->claim(); 34040b57cec5SDimitry Andric StringRef Val = A->getValue(); 34050b57cec5SDimitry Andric if (Val == "uninitialized" || Val == "zero" || Val == "pattern") 34060b57cec5SDimitry Andric TrivialAutoVarInit = Val; 34070b57cec5SDimitry Andric else 34080b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 34090b57cec5SDimitry Andric << A->getOption().getName() << Val; 34100b57cec5SDimitry Andric break; 34110b57cec5SDimitry Andric } 34120b57cec5SDimitry Andric } 34130b57cec5SDimitry Andric } 34140b57cec5SDimitry Andric 34150b57cec5SDimitry Andric if (TrivialAutoVarInit.empty()) 34160b57cec5SDimitry Andric switch (DefaultTrivialAutoVarInit) { 34170b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Uninitialized: 34180b57cec5SDimitry Andric break; 34190b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Pattern: 34200b57cec5SDimitry Andric TrivialAutoVarInit = "pattern"; 34210b57cec5SDimitry Andric break; 34220b57cec5SDimitry Andric case LangOptions::TrivialAutoVarInitKind::Zero: 34230b57cec5SDimitry Andric TrivialAutoVarInit = "zero"; 34240b57cec5SDimitry Andric break; 34250b57cec5SDimitry Andric } 34260b57cec5SDimitry Andric 34270b57cec5SDimitry Andric if (!TrivialAutoVarInit.empty()) { 34280b57cec5SDimitry Andric if (TrivialAutoVarInit == "zero" && !Args.hasArg(options::OPT_enable_trivial_var_init_zero)) 34290b57cec5SDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_zero_disabled); 34300b57cec5SDimitry Andric CmdArgs.push_back( 34310b57cec5SDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit)); 34320b57cec5SDimitry Andric } 34335ffd83dbSDimitry Andric 34345ffd83dbSDimitry Andric if (Arg *A = 34355ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) { 34365ffd83dbSDimitry Andric if (!Args.hasArg(options::OPT_ftrivial_auto_var_init) || 34375ffd83dbSDimitry Andric StringRef( 34385ffd83dbSDimitry Andric Args.getLastArg(options::OPT_ftrivial_auto_var_init)->getValue()) == 34395ffd83dbSDimitry Andric "uninitialized") 34405ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_missing_dependency); 34415ffd83dbSDimitry Andric A->claim(); 34425ffd83dbSDimitry Andric StringRef Val = A->getValue(); 34435ffd83dbSDimitry Andric if (std::stoi(Val.str()) <= 0) 34445ffd83dbSDimitry Andric D.Diag(diag::err_drv_trivial_auto_var_init_stop_after_invalid_value); 34455ffd83dbSDimitry Andric CmdArgs.push_back( 34465ffd83dbSDimitry Andric Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val)); 34475ffd83dbSDimitry Andric } 34480b57cec5SDimitry Andric } 34490b57cec5SDimitry Andric 3450fe6060f1SDimitry Andric static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, 3451fe6060f1SDimitry Andric types::ID InputType) { 34525ffd83dbSDimitry Andric // cl-denorms-are-zero is not forwarded. It is translated into a generic flag 34535ffd83dbSDimitry Andric // for denormal flushing handling based on the target. 34540b57cec5SDimitry Andric const unsigned ForwardedArguments[] = { 34550b57cec5SDimitry Andric options::OPT_cl_opt_disable, 34560b57cec5SDimitry Andric options::OPT_cl_strict_aliasing, 34570b57cec5SDimitry Andric options::OPT_cl_single_precision_constant, 34580b57cec5SDimitry Andric options::OPT_cl_finite_math_only, 34590b57cec5SDimitry Andric options::OPT_cl_kernel_arg_info, 34600b57cec5SDimitry Andric options::OPT_cl_unsafe_math_optimizations, 34610b57cec5SDimitry Andric options::OPT_cl_fast_relaxed_math, 34620b57cec5SDimitry Andric options::OPT_cl_mad_enable, 34630b57cec5SDimitry Andric options::OPT_cl_no_signed_zeros, 34640b57cec5SDimitry Andric options::OPT_cl_fp32_correctly_rounded_divide_sqrt, 34650b57cec5SDimitry Andric options::OPT_cl_uniform_work_group_size 34660b57cec5SDimitry Andric }; 34670b57cec5SDimitry Andric 34680b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_cl_std_EQ)) { 34690b57cec5SDimitry Andric std::string CLStdStr = std::string("-cl-std=") + A->getValue(); 34700b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLStdStr)); 347181ad6265SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_cl_ext_EQ)) { 347281ad6265SDimitry Andric std::string CLExtStr = std::string("-cl-ext=") + A->getValue(); 347381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CLExtStr)); 34740b57cec5SDimitry Andric } 34750b57cec5SDimitry Andric 34760b57cec5SDimitry Andric for (const auto &Arg : ForwardedArguments) 34770b57cec5SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 34780b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); 3479fe6060f1SDimitry Andric 3480fe6060f1SDimitry Andric // Only add the default headers if we are compiling OpenCL sources. 3481fe6060f1SDimitry Andric if ((types::isOpenCL(InputType) || 3482fe6060f1SDimitry Andric (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) && 3483fe6060f1SDimitry Andric !Args.hasArg(options::OPT_cl_no_stdinc)) { 3484fe6060f1SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 3485fe6060f1SDimitry Andric CmdArgs.push_back("-fdeclare-opencl-builtins"); 3486fe6060f1SDimitry Andric } 34870b57cec5SDimitry Andric } 34880b57cec5SDimitry Andric 348981ad6265SDimitry Andric static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs, 349081ad6265SDimitry Andric types::ID InputType) { 349181ad6265SDimitry Andric const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version, 349281ad6265SDimitry Andric options::OPT_D, 349381ad6265SDimitry Andric options::OPT_S, 349481ad6265SDimitry Andric options::OPT_emit_llvm, 349581ad6265SDimitry Andric options::OPT_disable_llvm_passes, 349681ad6265SDimitry Andric options::OPT_fnative_half_type}; 349781ad6265SDimitry Andric 349881ad6265SDimitry Andric for (const auto &Arg : ForwardedArguments) 349981ad6265SDimitry Andric if (const auto *A = Args.getLastArg(Arg)) 350081ad6265SDimitry Andric A->renderAsInput(Args, CmdArgs); 350181ad6265SDimitry Andric // Add the default headers if dxc_no_stdinc is not set. 350281ad6265SDimitry Andric if (!Args.hasArg(options::OPT_dxc_no_stdinc)) 350381ad6265SDimitry Andric CmdArgs.push_back("-finclude-default-header"); 350481ad6265SDimitry Andric CmdArgs.push_back("-fallow-half-arguments-and-returns"); 350581ad6265SDimitry Andric } 350681ad6265SDimitry Andric 35070b57cec5SDimitry Andric static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, 35080b57cec5SDimitry Andric ArgStringList &CmdArgs) { 35090b57cec5SDimitry Andric bool ARCMTEnabled = false; 35100b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) { 35110b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 35120b57cec5SDimitry Andric options::OPT_ccc_arcmt_modify, 35130b57cec5SDimitry Andric options::OPT_ccc_arcmt_migrate)) { 35140b57cec5SDimitry Andric ARCMTEnabled = true; 35150b57cec5SDimitry Andric switch (A->getOption().getID()) { 35160b57cec5SDimitry Andric default: llvm_unreachable("missed a case"); 35170b57cec5SDimitry Andric case options::OPT_ccc_arcmt_check: 3518e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=check"); 35190b57cec5SDimitry Andric break; 35200b57cec5SDimitry Andric case options::OPT_ccc_arcmt_modify: 3521e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=modify"); 35220b57cec5SDimitry Andric break; 35230b57cec5SDimitry Andric case options::OPT_ccc_arcmt_migrate: 3524e8d8bef9SDimitry Andric CmdArgs.push_back("-arcmt-action=migrate"); 35250b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 35260b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 35270b57cec5SDimitry Andric 35280b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 35290b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 35300b57cec5SDimitry Andric break; 35310b57cec5SDimitry Andric } 35320b57cec5SDimitry Andric } 35330b57cec5SDimitry Andric } else { 35340b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 35350b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 35360b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 35370b57cec5SDimitry Andric } 35380b57cec5SDimitry Andric 35390b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 35400b57cec5SDimitry Andric if (ARCMTEnabled) 35410b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 35420b57cec5SDimitry Andric << A->getAsString(Args) << "-ccc-arcmt-migrate"; 35430b57cec5SDimitry Andric 35440b57cec5SDimitry Andric CmdArgs.push_back("-mt-migrate-directory"); 35450b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 35460b57cec5SDimitry Andric 35470b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 35480b57cec5SDimitry Andric options::OPT_objcmt_migrate_subscripting, 35490b57cec5SDimitry Andric options::OPT_objcmt_migrate_property)) { 35500b57cec5SDimitry Andric // None specified, means enable them all. 35510b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-literals"); 35520b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-subscripting"); 35530b57cec5SDimitry Andric CmdArgs.push_back("-objcmt-migrate-property"); 35540b57cec5SDimitry Andric } else { 35550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 35560b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 35570b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 35580b57cec5SDimitry Andric } 35590b57cec5SDimitry Andric } else { 35600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 35610b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 35620b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 35630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all); 35640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property); 35650b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property); 35660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax); 35670b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation); 35680b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype); 35690b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros); 35700b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance); 35710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property); 35720b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property); 35730b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly); 35740b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init); 3575349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_objcmt_allowlist_dir_path); 35760b57cec5SDimitry Andric } 35770b57cec5SDimitry Andric } 35780b57cec5SDimitry Andric 35790b57cec5SDimitry Andric static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T, 35800b57cec5SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs) { 35810b57cec5SDimitry Andric // -fbuiltin is default unless -mkernel is used. 35820b57cec5SDimitry Andric bool UseBuiltins = 35830b57cec5SDimitry Andric Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin, 35840b57cec5SDimitry Andric !Args.hasArg(options::OPT_mkernel)); 35850b57cec5SDimitry Andric if (!UseBuiltins) 35860b57cec5SDimitry Andric CmdArgs.push_back("-fno-builtin"); 35870b57cec5SDimitry Andric 35880b57cec5SDimitry Andric // -ffreestanding implies -fno-builtin. 35890b57cec5SDimitry Andric if (Args.hasArg(options::OPT_ffreestanding)) 35900b57cec5SDimitry Andric UseBuiltins = false; 35910b57cec5SDimitry Andric 35920b57cec5SDimitry Andric // Process the -fno-builtin-* options. 359381ad6265SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fno_builtin_)) { 359481ad6265SDimitry Andric A->claim(); 35950b57cec5SDimitry Andric 35960b57cec5SDimitry Andric // If -fno-builtin is specified, then there's no need to pass the option to 35970b57cec5SDimitry Andric // the frontend. 359881ad6265SDimitry Andric if (UseBuiltins) 359981ad6265SDimitry Andric A->render(Args, CmdArgs); 36000b57cec5SDimitry Andric } 36010b57cec5SDimitry Andric 36020b57cec5SDimitry Andric // le32-specific flags: 36030b57cec5SDimitry Andric // -fno-math-builtin: clang should not convert math builtins to intrinsics 36040b57cec5SDimitry Andric // by default. 36050b57cec5SDimitry Andric if (TC.getArch() == llvm::Triple::le32) 36060b57cec5SDimitry Andric CmdArgs.push_back("-fno-math-builtin"); 36070b57cec5SDimitry Andric } 36080b57cec5SDimitry Andric 36095ffd83dbSDimitry Andric bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) { 361081ad6265SDimitry Andric if (const char *Str = std::getenv("CLANG_MODULE_CACHE_PATH")) { 361181ad6265SDimitry Andric Twine Path{Str}; 361281ad6265SDimitry Andric Path.toVector(Result); 361381ad6265SDimitry Andric return Path.getSingleStringRef() != ""; 361481ad6265SDimitry Andric } 36155ffd83dbSDimitry Andric if (llvm::sys::path::cache_directory(Result)) { 36165ffd83dbSDimitry Andric llvm::sys::path::append(Result, "clang"); 36170b57cec5SDimitry Andric llvm::sys::path::append(Result, "ModuleCache"); 36185ffd83dbSDimitry Andric return true; 36195ffd83dbSDimitry Andric } 36205ffd83dbSDimitry Andric return false; 36210b57cec5SDimitry Andric } 36220b57cec5SDimitry Andric 36230b57cec5SDimitry Andric static void RenderModulesOptions(Compilation &C, const Driver &D, 36240b57cec5SDimitry Andric const ArgList &Args, const InputInfo &Input, 36250b57cec5SDimitry Andric const InputInfo &Output, 36260b57cec5SDimitry Andric ArgStringList &CmdArgs, bool &HaveModules) { 36270b57cec5SDimitry Andric // -fmodules enables the use of precompiled modules (off by default). 36280b57cec5SDimitry Andric // Users can pass -fno-cxx-modules to turn off modules support for 36290b57cec5SDimitry Andric // C++/Objective-C++ programs. 36300b57cec5SDimitry Andric bool HaveClangModules = false; 36310b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 36320b57cec5SDimitry Andric bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 36330b57cec5SDimitry Andric options::OPT_fno_cxx_modules, true); 36340b57cec5SDimitry Andric if (AllowedInCXX || !types::isCXX(Input.getType())) { 36350b57cec5SDimitry Andric CmdArgs.push_back("-fmodules"); 36360b57cec5SDimitry Andric HaveClangModules = true; 36370b57cec5SDimitry Andric } 36380b57cec5SDimitry Andric } 36390b57cec5SDimitry Andric 36400b57cec5SDimitry Andric HaveModules |= HaveClangModules; 36410b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fmodules_ts)) { 36420b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-ts"); 36430b57cec5SDimitry Andric HaveModules = true; 36440b57cec5SDimitry Andric } 36450b57cec5SDimitry Andric 36460b57cec5SDimitry Andric // -fmodule-maps enables implicit reading of module map files. By default, 36470b57cec5SDimitry Andric // this is enabled if we are using Clang's flavor of precompiled modules. 36480b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fimplicit_module_maps, 36490b57cec5SDimitry Andric options::OPT_fno_implicit_module_maps, HaveClangModules)) 36500b57cec5SDimitry Andric CmdArgs.push_back("-fimplicit-module-maps"); 36510b57cec5SDimitry Andric 36520b57cec5SDimitry Andric // -fmodules-decluse checks that modules used are declared so (off by default) 365381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fmodules_decluse, 365481ad6265SDimitry Andric options::OPT_fno_modules_decluse); 36550b57cec5SDimitry Andric 36560b57cec5SDimitry Andric // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that 36570b57cec5SDimitry Andric // all #included headers are part of modules. 36580b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_strict_decluse, 36590b57cec5SDimitry Andric options::OPT_fno_modules_strict_decluse, false)) 36600b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-strict-decluse"); 36610b57cec5SDimitry Andric 36620b57cec5SDimitry Andric // -fno-implicit-modules turns off implicitly compiling modules on demand. 36630b57cec5SDimitry Andric bool ImplicitModules = false; 36640b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fimplicit_modules, 36650b57cec5SDimitry Andric options::OPT_fno_implicit_modules, HaveClangModules)) { 36660b57cec5SDimitry Andric if (HaveModules) 36670b57cec5SDimitry Andric CmdArgs.push_back("-fno-implicit-modules"); 36680b57cec5SDimitry Andric } else if (HaveModules) { 36690b57cec5SDimitry Andric ImplicitModules = true; 36700b57cec5SDimitry Andric // -fmodule-cache-path specifies where our implicitly-built module files 36710b57cec5SDimitry Andric // should be written. 36720b57cec5SDimitry Andric SmallString<128> Path; 36730b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) 36740b57cec5SDimitry Andric Path = A->getValue(); 36750b57cec5SDimitry Andric 36765ffd83dbSDimitry Andric bool HasPath = true; 36770b57cec5SDimitry Andric if (C.isForDiagnostics()) { 36780b57cec5SDimitry Andric // When generating crash reports, we want to emit the modules along with 36790b57cec5SDimitry Andric // the reproduction sources, so we ignore any provided module path. 36800b57cec5SDimitry Andric Path = Output.getFilename(); 36810b57cec5SDimitry Andric llvm::sys::path::replace_extension(Path, ".cache"); 36820b57cec5SDimitry Andric llvm::sys::path::append(Path, "modules"); 36830b57cec5SDimitry Andric } else if (Path.empty()) { 36840b57cec5SDimitry Andric // No module path was provided: use the default. 36855ffd83dbSDimitry Andric HasPath = Driver::getDefaultModuleCachePath(Path); 36860b57cec5SDimitry Andric } 36870b57cec5SDimitry Andric 36885ffd83dbSDimitry Andric // `HasPath` will only be false if getDefaultModuleCachePath() fails. 36895ffd83dbSDimitry Andric // That being said, that failure is unlikely and not caching is harmless. 36905ffd83dbSDimitry Andric if (HasPath) { 36910b57cec5SDimitry Andric const char Arg[] = "-fmodules-cache-path="; 36920b57cec5SDimitry Andric Path.insert(Path.begin(), Arg, Arg + strlen(Arg)); 36930b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Path)); 36940b57cec5SDimitry Andric } 36955ffd83dbSDimitry Andric } 36960b57cec5SDimitry Andric 36970b57cec5SDimitry Andric if (HaveModules) { 36980b57cec5SDimitry Andric // -fprebuilt-module-path specifies where to load the prebuilt module files. 36990b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) { 37000b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 37010b57cec5SDimitry Andric std::string("-fprebuilt-module-path=") + A->getValue())); 37020b57cec5SDimitry Andric A->claim(); 37030b57cec5SDimitry Andric } 3704e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules, 3705e8d8bef9SDimitry Andric options::OPT_fno_prebuilt_implicit_modules, false)) 3706e8d8bef9SDimitry Andric CmdArgs.push_back("-fprebuilt-implicit-modules"); 3707a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_input_files_content, 3708a7dea167SDimitry Andric options::OPT_fno_modules_validate_input_files_content, 3709a7dea167SDimitry Andric false)) 3710a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 37110b57cec5SDimitry Andric } 37120b57cec5SDimitry Andric 37130b57cec5SDimitry Andric // -fmodule-name specifies the module that is currently being built (or 37140b57cec5SDimitry Andric // used for header checking by -fmodule-maps). 37150b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ); 37160b57cec5SDimitry Andric 37170b57cec5SDimitry Andric // -fmodule-map-file can be used to specify files containing module 37180b57cec5SDimitry Andric // definitions. 37190b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file); 37200b57cec5SDimitry Andric 37210b57cec5SDimitry Andric // -fbuiltin-module-map can be used to load the clang 37220b57cec5SDimitry Andric // builtin headers modulemap file. 37230b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuiltin_module_map)) { 37240b57cec5SDimitry Andric SmallString<128> BuiltinModuleMap(D.ResourceDir); 37250b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "include"); 37260b57cec5SDimitry Andric llvm::sys::path::append(BuiltinModuleMap, "module.modulemap"); 37270b57cec5SDimitry Andric if (llvm::sys::fs::exists(BuiltinModuleMap)) 37280b57cec5SDimitry Andric CmdArgs.push_back( 37290b57cec5SDimitry Andric Args.MakeArgString("-fmodule-map-file=" + BuiltinModuleMap)); 37300b57cec5SDimitry Andric } 37310b57cec5SDimitry Andric 37320b57cec5SDimitry Andric // The -fmodule-file=<name>=<file> form specifies the mapping of module 37330b57cec5SDimitry Andric // names to precompiled module files (the module is loaded only if used). 37340b57cec5SDimitry Andric // The -fmodule-file=<file> form can be used to unconditionally load 37350b57cec5SDimitry Andric // precompiled module files (whether used or not). 37360b57cec5SDimitry Andric if (HaveModules) 37370b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file); 37380b57cec5SDimitry Andric else 37390b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fmodule_file); 37400b57cec5SDimitry Andric 37410b57cec5SDimitry Andric // When building modules and generating crashdumps, we need to dump a module 37420b57cec5SDimitry Andric // dependency VFS alongside the output. 37430b57cec5SDimitry Andric if (HaveClangModules && C.isForDiagnostics()) { 37440b57cec5SDimitry Andric SmallString<128> VFSDir(Output.getFilename()); 37450b57cec5SDimitry Andric llvm::sys::path::replace_extension(VFSDir, ".cache"); 37460b57cec5SDimitry Andric // Add the cache directory as a temp so the crash diagnostics pick it up. 37470b57cec5SDimitry Andric C.addTempFile(Args.MakeArgString(VFSDir)); 37480b57cec5SDimitry Andric 37490b57cec5SDimitry Andric llvm::sys::path::append(VFSDir, "vfs"); 37500b57cec5SDimitry Andric CmdArgs.push_back("-module-dependency-dir"); 37510b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(VFSDir)); 37520b57cec5SDimitry Andric } 37530b57cec5SDimitry Andric 37540b57cec5SDimitry Andric if (HaveClangModules) 37550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path); 37560b57cec5SDimitry Andric 37570b57cec5SDimitry Andric // Pass through all -fmodules-ignore-macro arguments. 37580b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 37590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 37600b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 37610b57cec5SDimitry Andric 3762753f127fSDimitry Andric if (HaveClangModules) { 37630b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp); 37640b57cec5SDimitry Andric 37650b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) { 37660b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fbuild_session_timestamp)) 37670b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 37680b57cec5SDimitry Andric << A->getAsString(Args) << "-fbuild-session-timestamp"; 37690b57cec5SDimitry Andric 37700b57cec5SDimitry Andric llvm::sys::fs::file_status Status; 37710b57cec5SDimitry Andric if (llvm::sys::fs::status(A->getValue(), Status)) 37720b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << A->getValue(); 3773349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 3774349cc55cSDimitry Andric "-fbuild-session-timestamp=" + 3775349cc55cSDimitry Andric Twine((uint64_t)std::chrono::duration_cast<std::chrono::seconds>( 3776349cc55cSDimitry Andric Status.getLastModificationTime().time_since_epoch()) 37770b57cec5SDimitry Andric .count()))); 37780b57cec5SDimitry Andric } 37790b57cec5SDimitry Andric 3780753f127fSDimitry Andric if (Args.getLastArg( 3781753f127fSDimitry Andric options::OPT_fmodules_validate_once_per_build_session)) { 37820b57cec5SDimitry Andric if (!Args.getLastArg(options::OPT_fbuild_session_timestamp, 37830b57cec5SDimitry Andric options::OPT_fbuild_session_file)) 37840b57cec5SDimitry Andric D.Diag(diag::err_drv_modules_validate_once_requires_timestamp); 37850b57cec5SDimitry Andric 37860b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 37870b57cec5SDimitry Andric options::OPT_fmodules_validate_once_per_build_session); 37880b57cec5SDimitry Andric } 37890b57cec5SDimitry Andric 37900b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmodules_validate_system_headers, 37910b57cec5SDimitry Andric options::OPT_fno_modules_validate_system_headers, 37920b57cec5SDimitry Andric ImplicitModules)) 37930b57cec5SDimitry Andric CmdArgs.push_back("-fmodules-validate-system-headers"); 37940b57cec5SDimitry Andric 3795753f127fSDimitry Andric Args.AddLastArg(CmdArgs, 3796753f127fSDimitry Andric options::OPT_fmodules_disable_diagnostic_validation); 3797753f127fSDimitry Andric } else { 3798753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_timestamp); 3799753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fbuild_session_file); 3800753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_once_per_build_session); 3801753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_validate_system_headers); 3802753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fno_modules_validate_system_headers); 3803753f127fSDimitry Andric Args.ClaimAllArgs(options::OPT_fmodules_disable_diagnostic_validation); 3804753f127fSDimitry Andric } 38050b57cec5SDimitry Andric } 38060b57cec5SDimitry Andric 38070b57cec5SDimitry Andric static void RenderCharacterOptions(const ArgList &Args, const llvm::Triple &T, 38080b57cec5SDimitry Andric ArgStringList &CmdArgs) { 38090b57cec5SDimitry Andric // -fsigned-char is default. 38100b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fsigned_char, 38110b57cec5SDimitry Andric options::OPT_fno_signed_char, 38120b57cec5SDimitry Andric options::OPT_funsigned_char, 38130b57cec5SDimitry Andric options::OPT_fno_unsigned_char)) { 38140b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_funsigned_char) || 38150b57cec5SDimitry Andric A->getOption().matches(options::OPT_fno_signed_char)) { 38160b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 38170b57cec5SDimitry Andric } 38180b57cec5SDimitry Andric } else if (!isSignedCharDefault(T)) { 38190b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-char"); 38200b57cec5SDimitry Andric } 38210b57cec5SDimitry Andric 38220b57cec5SDimitry Andric // The default depends on the language standard. 38230b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fchar8__t, options::OPT_fno_char8__t); 38240b57cec5SDimitry Andric 38250b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fshort_wchar, 38260b57cec5SDimitry Andric options::OPT_fno_short_wchar)) { 38270b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fshort_wchar)) { 38280b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=short"); 38290b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 38300b57cec5SDimitry Andric } else { 38310b57cec5SDimitry Andric bool IsARM = T.isARM() || T.isThumb() || T.isAArch64(); 38320b57cec5SDimitry Andric CmdArgs.push_back("-fwchar-type=int"); 3833e8d8bef9SDimitry Andric if (T.isOSzOS() || 3834e8d8bef9SDimitry Andric (IsARM && !(T.isOSWindows() || T.isOSNetBSD() || T.isOSOpenBSD()))) 38350b57cec5SDimitry Andric CmdArgs.push_back("-fno-signed-wchar"); 38360b57cec5SDimitry Andric else 38370b57cec5SDimitry Andric CmdArgs.push_back("-fsigned-wchar"); 38380b57cec5SDimitry Andric } 38390b57cec5SDimitry Andric } 38400b57cec5SDimitry Andric } 38410b57cec5SDimitry Andric 38420b57cec5SDimitry Andric static void RenderObjCOptions(const ToolChain &TC, const Driver &D, 38430b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 38440b57cec5SDimitry Andric ObjCRuntime &Runtime, bool InferCovariantReturns, 38450b57cec5SDimitry Andric const InputInfo &Input, ArgStringList &CmdArgs) { 38460b57cec5SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 38470b57cec5SDimitry Andric 38480b57cec5SDimitry Andric // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and legacy 38490b57cec5SDimitry Andric // is the default. Except for deployment target of 10.5, next runtime is 38500b57cec5SDimitry Andric // always legacy dispatch and -fno-objc-legacy-dispatch gets ignored silently. 38510b57cec5SDimitry Andric if (Runtime.isNonFragile()) { 38520b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 38530b57cec5SDimitry Andric options::OPT_fno_objc_legacy_dispatch, 38540b57cec5SDimitry Andric Runtime.isLegacyDispatchDefaultForArch(Arch))) { 38550b57cec5SDimitry Andric if (TC.UseObjCMixedDispatch()) 38560b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 38570b57cec5SDimitry Andric else 38580b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 38590b57cec5SDimitry Andric } 38600b57cec5SDimitry Andric } 38610b57cec5SDimitry Andric 38620b57cec5SDimitry Andric // When ObjectiveC legacy runtime is in effect on MacOSX, turn on the option 38630b57cec5SDimitry Andric // to do Array/Dictionary subscripting by default. 38640b57cec5SDimitry Andric if (Arch == llvm::Triple::x86 && T.isMacOSX() && 38650b57cec5SDimitry Andric Runtime.getKind() == ObjCRuntime::FragileMacOSX && Runtime.isNeXTFamily()) 38660b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-subscripting-legacy-runtime"); 38670b57cec5SDimitry Andric 38680b57cec5SDimitry Andric // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 38690b57cec5SDimitry Andric // NOTE: This logic is duplicated in ToolChains.cpp. 38700b57cec5SDimitry Andric if (isObjCAutoRefCount(Args)) { 38710b57cec5SDimitry Andric TC.CheckObjCARC(); 38720b57cec5SDimitry Andric 38730b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc"); 38740b57cec5SDimitry Andric 38750b57cec5SDimitry Andric // FIXME: It seems like this entire block, and several around it should be 38760b57cec5SDimitry Andric // wrapped in isObjC, but for now we just use it here as this is where it 38770b57cec5SDimitry Andric // was being used previously. 38780b57cec5SDimitry Andric if (types::isCXX(Input.getType()) && types::isObjC(Input.getType())) { 38790b57cec5SDimitry Andric if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 38800b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 38810b57cec5SDimitry Andric else 38820b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 38830b57cec5SDimitry Andric } 38840b57cec5SDimitry Andric 38850b57cec5SDimitry Andric // Allow the user to enable full exceptions code emission. 38860b57cec5SDimitry Andric // We default off for Objective-C, on for Objective-C++. 38870b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 38880b57cec5SDimitry Andric options::OPT_fno_objc_arc_exceptions, 38890b57cec5SDimitry Andric /*Default=*/types::isCXX(Input.getType()))) 38900b57cec5SDimitry Andric CmdArgs.push_back("-fobjc-arc-exceptions"); 38910b57cec5SDimitry Andric } 38920b57cec5SDimitry Andric 38930b57cec5SDimitry Andric // Silence warning for full exception code emission options when explicitly 38940b57cec5SDimitry Andric // set to use no ARC. 38950b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fno_objc_arc)) { 38960b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fobjc_arc_exceptions); 38970b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fno_objc_arc_exceptions); 38980b57cec5SDimitry Andric } 38990b57cec5SDimitry Andric 39000b57cec5SDimitry Andric // Allow the user to control whether messages can be converted to runtime 39010b57cec5SDimitry Andric // functions. 39020b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 39030b57cec5SDimitry Andric auto *Arg = Args.getLastArg( 39040b57cec5SDimitry Andric options::OPT_fobjc_convert_messages_to_runtime_calls, 39050b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls); 39060b57cec5SDimitry Andric if (Arg && 39070b57cec5SDimitry Andric Arg->getOption().matches( 39080b57cec5SDimitry Andric options::OPT_fno_objc_convert_messages_to_runtime_calls)) 39090b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls"); 39100b57cec5SDimitry Andric } 39110b57cec5SDimitry Andric 39120b57cec5SDimitry Andric // -fobjc-infer-related-result-type is the default, except in the Objective-C 39130b57cec5SDimitry Andric // rewriter. 39140b57cec5SDimitry Andric if (InferCovariantReturns) 39150b57cec5SDimitry Andric CmdArgs.push_back("-fno-objc-infer-related-result-type"); 39160b57cec5SDimitry Andric 39170b57cec5SDimitry Andric // Pass down -fobjc-weak or -fno-objc-weak if present. 39180b57cec5SDimitry Andric if (types::isObjC(Input.getType())) { 39190b57cec5SDimitry Andric auto WeakArg = 39200b57cec5SDimitry Andric Args.getLastArg(options::OPT_fobjc_weak, options::OPT_fno_objc_weak); 39210b57cec5SDimitry Andric if (!WeakArg) { 39220b57cec5SDimitry Andric // nothing to do 39230b57cec5SDimitry Andric } else if (!Runtime.allowsWeak()) { 39240b57cec5SDimitry Andric if (WeakArg->getOption().matches(options::OPT_fobjc_weak)) 39250b57cec5SDimitry Andric D.Diag(diag::err_objc_weak_unsupported); 39260b57cec5SDimitry Andric } else { 39270b57cec5SDimitry Andric WeakArg->render(Args, CmdArgs); 39280b57cec5SDimitry Andric } 39290b57cec5SDimitry Andric } 3930fe6060f1SDimitry Andric 3931fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fobjc_disable_direct_methods_for_testing)) 3932fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-disable-direct-methods-for-testing"); 39330b57cec5SDimitry Andric } 39340b57cec5SDimitry Andric 39350b57cec5SDimitry Andric static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args, 39360b57cec5SDimitry Andric ArgStringList &CmdArgs) { 39370b57cec5SDimitry Andric bool CaretDefault = true; 39380b57cec5SDimitry Andric bool ColumnDefault = true; 39390b57cec5SDimitry Andric 39400b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT__SLASH_diagnostics_classic, 39410b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_column, 39420b57cec5SDimitry Andric options::OPT__SLASH_diagnostics_caret)) { 39430b57cec5SDimitry Andric switch (A->getOption().getID()) { 39440b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_caret: 39450b57cec5SDimitry Andric CaretDefault = true; 39460b57cec5SDimitry Andric ColumnDefault = true; 39470b57cec5SDimitry Andric break; 39480b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_column: 39490b57cec5SDimitry Andric CaretDefault = false; 39500b57cec5SDimitry Andric ColumnDefault = true; 39510b57cec5SDimitry Andric break; 39520b57cec5SDimitry Andric case options::OPT__SLASH_diagnostics_classic: 39530b57cec5SDimitry Andric CaretDefault = false; 39540b57cec5SDimitry Andric ColumnDefault = false; 39550b57cec5SDimitry Andric break; 39560b57cec5SDimitry Andric } 39570b57cec5SDimitry Andric } 39580b57cec5SDimitry Andric 39590b57cec5SDimitry Andric // -fcaret-diagnostics is default. 39600b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 39610b57cec5SDimitry Andric options::OPT_fno_caret_diagnostics, CaretDefault)) 39620b57cec5SDimitry Andric CmdArgs.push_back("-fno-caret-diagnostics"); 39630b57cec5SDimitry Andric 396481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_fixit_info, 396581ad6265SDimitry Andric options::OPT_fno_diagnostics_fixit_info); 396681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_option, 396781ad6265SDimitry Andric options::OPT_fno_diagnostics_show_option); 39680b57cec5SDimitry Andric 39690b57cec5SDimitry Andric if (const Arg *A = 39700b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 39710b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-category"); 39720b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 39730b57cec5SDimitry Andric } 39740b57cec5SDimitry Andric 397581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fdiagnostics_show_hotness, 397681ad6265SDimitry Andric options::OPT_fno_diagnostics_show_hotness); 39770b57cec5SDimitry Andric 39780b57cec5SDimitry Andric if (const Arg *A = 39790b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) { 39800b57cec5SDimitry Andric std::string Opt = 39810b57cec5SDimitry Andric std::string("-fdiagnostics-hotness-threshold=") + A->getValue(); 39820b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Opt)); 39830b57cec5SDimitry Andric } 39840b57cec5SDimitry Andric 39850b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 39860b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 39870b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 39880b57cec5SDimitry Andric } 39890b57cec5SDimitry Andric 39900b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg( 39910b57cec5SDimitry Andric options::OPT_fdiagnostics_show_note_include_stack, 39920b57cec5SDimitry Andric options::OPT_fno_diagnostics_show_note_include_stack)) { 39930b57cec5SDimitry Andric const Option &O = A->getOption(); 39940b57cec5SDimitry Andric if (O.matches(options::OPT_fdiagnostics_show_note_include_stack)) 39950b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 39960b57cec5SDimitry Andric else 39970b57cec5SDimitry Andric CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 39980b57cec5SDimitry Andric } 39990b57cec5SDimitry Andric 40000b57cec5SDimitry Andric // Color diagnostics are parsed by the driver directly from argv and later 40010b57cec5SDimitry Andric // re-parsed to construct this job; claim any possible color diagnostic here 40020b57cec5SDimitry Andric // to avoid warn_drv_unused_argument and diagnose bad 40030b57cec5SDimitry Andric // OPT_fdiagnostics_color_EQ values. 400481ad6265SDimitry Andric Args.getLastArg(options::OPT_fcolor_diagnostics, 400581ad6265SDimitry Andric options::OPT_fno_color_diagnostics); 400681ad6265SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_color_EQ)) { 40070b57cec5SDimitry Andric StringRef Value(A->getValue()); 40080b57cec5SDimitry Andric if (Value != "always" && Value != "never" && Value != "auto") 400981ad6265SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 401081ad6265SDimitry Andric << Value << A->getOption().getName(); 40110b57cec5SDimitry Andric } 40120b57cec5SDimitry Andric 40130b57cec5SDimitry Andric if (D.getDiags().getDiagnosticOptions().ShowColors) 40140b57cec5SDimitry Andric CmdArgs.push_back("-fcolor-diagnostics"); 40150b57cec5SDimitry Andric 40160b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fansi_escape_codes)) 40170b57cec5SDimitry Andric CmdArgs.push_back("-fansi-escape-codes"); 40180b57cec5SDimitry Andric 401981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fshow_source_location, 402081ad6265SDimitry Andric options::OPT_fno_show_source_location); 40210b57cec5SDimitry Andric 40220b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdiagnostics_absolute_paths)) 40230b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-absolute-paths"); 40240b57cec5SDimitry Andric 40250b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column, 40260b57cec5SDimitry Andric ColumnDefault)) 40270b57cec5SDimitry Andric CmdArgs.push_back("-fno-show-column"); 40280b57cec5SDimitry Andric 402981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fspell_checking, 403081ad6265SDimitry Andric options::OPT_fno_spell_checking); 40310b57cec5SDimitry Andric } 40320b57cec5SDimitry Andric 40330b57cec5SDimitry Andric enum class DwarfFissionKind { None, Split, Single }; 40340b57cec5SDimitry Andric 40350b57cec5SDimitry Andric static DwarfFissionKind getDebugFissionKind(const Driver &D, 40360b57cec5SDimitry Andric const ArgList &Args, Arg *&Arg) { 4037e8d8bef9SDimitry Andric Arg = Args.getLastArg(options::OPT_gsplit_dwarf, options::OPT_gsplit_dwarf_EQ, 4038e8d8bef9SDimitry Andric options::OPT_gno_split_dwarf); 4039e8d8bef9SDimitry Andric if (!Arg || Arg->getOption().matches(options::OPT_gno_split_dwarf)) 40400b57cec5SDimitry Andric return DwarfFissionKind::None; 40410b57cec5SDimitry Andric 40420b57cec5SDimitry Andric if (Arg->getOption().matches(options::OPT_gsplit_dwarf)) 40430b57cec5SDimitry Andric return DwarfFissionKind::Split; 40440b57cec5SDimitry Andric 40450b57cec5SDimitry Andric StringRef Value = Arg->getValue(); 40460b57cec5SDimitry Andric if (Value == "split") 40470b57cec5SDimitry Andric return DwarfFissionKind::Split; 40480b57cec5SDimitry Andric if (Value == "single") 40490b57cec5SDimitry Andric return DwarfFissionKind::Single; 40500b57cec5SDimitry Andric 40510b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_option_argument) 40520b57cec5SDimitry Andric << Arg->getOption().getName() << Arg->getValue(); 40530b57cec5SDimitry Andric return DwarfFissionKind::None; 40540b57cec5SDimitry Andric } 40550b57cec5SDimitry Andric 4056fe6060f1SDimitry Andric static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, 4057fe6060f1SDimitry Andric const ArgList &Args, ArgStringList &CmdArgs, 4058fe6060f1SDimitry Andric unsigned DwarfVersion) { 4059fe6060f1SDimitry Andric auto *DwarfFormatArg = 4060fe6060f1SDimitry Andric Args.getLastArg(options::OPT_gdwarf64, options::OPT_gdwarf32); 4061fe6060f1SDimitry Andric if (!DwarfFormatArg) 4062fe6060f1SDimitry Andric return; 4063fe6060f1SDimitry Andric 4064fe6060f1SDimitry Andric if (DwarfFormatArg->getOption().matches(options::OPT_gdwarf64)) { 4065fe6060f1SDimitry Andric if (DwarfVersion < 3) 4066fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4067fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "DWARFv3 or greater"; 4068fe6060f1SDimitry Andric else if (!T.isArch64Bit()) 4069fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4070fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "64 bit architecture"; 4071fe6060f1SDimitry Andric else if (!T.isOSBinFormatELF()) 4072fe6060f1SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 4073fe6060f1SDimitry Andric << DwarfFormatArg->getAsString(Args) << "ELF platforms"; 4074fe6060f1SDimitry Andric } 4075fe6060f1SDimitry Andric 4076fe6060f1SDimitry Andric DwarfFormatArg->render(Args, CmdArgs); 4077fe6060f1SDimitry Andric } 4078fe6060f1SDimitry Andric 4079e8d8bef9SDimitry Andric static void renderDebugOptions(const ToolChain &TC, const Driver &D, 40800b57cec5SDimitry Andric const llvm::Triple &T, const ArgList &Args, 4081e8d8bef9SDimitry Andric bool EmitCodeView, bool IRInput, 4082e8d8bef9SDimitry Andric ArgStringList &CmdArgs, 40830b57cec5SDimitry Andric codegenoptions::DebugInfoKind &DebugInfoKind, 40840b57cec5SDimitry Andric DwarfFissionKind &DwarfFission) { 40850b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_info_for_profiling, 40860b57cec5SDimitry Andric options::OPT_fno_debug_info_for_profiling, false) && 40870b57cec5SDimitry Andric checkDebugInfoOption( 40880b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_info_for_profiling), Args, D, TC)) 40890b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-info-for-profiling"); 40900b57cec5SDimitry Andric 40910b57cec5SDimitry Andric // The 'g' groups options involve a somewhat intricate sequence of decisions 40920b57cec5SDimitry Andric // about what to pass from the driver to the frontend, but by the time they 40930b57cec5SDimitry Andric // reach cc1 they've been factored into three well-defined orthogonal choices: 40940b57cec5SDimitry Andric // * what level of debug info to generate 40950b57cec5SDimitry Andric // * what dwarf version to write 40960b57cec5SDimitry Andric // * what debugger tuning to use 40970b57cec5SDimitry Andric // This avoids having to monkey around further in cc1 other than to disable 40980b57cec5SDimitry Andric // codeview if not running in a Windows environment. Perhaps even that 40990b57cec5SDimitry Andric // decision should be made in the driver as well though. 41000b57cec5SDimitry Andric llvm::DebuggerKind DebuggerTuning = TC.getDefaultDebuggerTuning(); 41010b57cec5SDimitry Andric 41020b57cec5SDimitry Andric bool SplitDWARFInlining = 41030b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_dwarf_inlining, 4104480093f4SDimitry Andric options::OPT_fno_split_dwarf_inlining, false); 41050b57cec5SDimitry Andric 4106e8d8bef9SDimitry Andric // Normally -gsplit-dwarf is only useful with -gN. For IR input, Clang does 4107e8d8bef9SDimitry Andric // object file generation and no IR generation, -gN should not be needed. So 4108e8d8bef9SDimitry Andric // allow -gsplit-dwarf with either -gN or IR input. 4109e8d8bef9SDimitry Andric if (IRInput || Args.hasArg(options::OPT_g_Group)) { 41100b57cec5SDimitry Andric Arg *SplitDWARFArg; 41110b57cec5SDimitry Andric DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg); 41120b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None && 41130b57cec5SDimitry Andric !checkDebugInfoOption(SplitDWARFArg, Args, D, TC)) { 41140b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 41150b57cec5SDimitry Andric SplitDWARFInlining = false; 41160b57cec5SDimitry Andric } 4117e8d8bef9SDimitry Andric } 4118e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 4119fe6060f1SDimitry Andric DebugInfoKind = codegenoptions::DebugInfoConstructor; 41200b57cec5SDimitry Andric 41210b57cec5SDimitry Andric // If the last option explicitly specified a debug-info level, use it. 41220b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC) && 41230b57cec5SDimitry Andric A->getOption().matches(options::OPT_gN_Group)) { 41240b57cec5SDimitry Andric DebugInfoKind = DebugLevelToInfoKind(*A); 41250b57cec5SDimitry Andric // For -g0 or -gline-tables-only, drop -gsplit-dwarf. This gets a bit more 41260b57cec5SDimitry Andric // complicated if you've disabled inline info in the skeleton CUs 41270b57cec5SDimitry Andric // (SplitDWARFInlining) - then there's value in composing split-dwarf and 41280b57cec5SDimitry Andric // line-tables-only, so let those compose naturally in that case. 41290b57cec5SDimitry Andric if (DebugInfoKind == codegenoptions::NoDebugInfo || 41300b57cec5SDimitry Andric DebugInfoKind == codegenoptions::DebugDirectivesOnly || 41310b57cec5SDimitry Andric (DebugInfoKind == codegenoptions::DebugLineTablesOnly && 41320b57cec5SDimitry Andric SplitDWARFInlining)) 41330b57cec5SDimitry Andric DwarfFission = DwarfFissionKind::None; 41340b57cec5SDimitry Andric } 41350b57cec5SDimitry Andric } 41360b57cec5SDimitry Andric 41370b57cec5SDimitry Andric // If a debugger tuning argument appeared, remember it. 41380b57cec5SDimitry Andric if (const Arg *A = 41390b57cec5SDimitry Andric Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { 41400b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 41410b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_glldb)) 41420b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::LLDB; 41430b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_gsce)) 41440b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::SCE; 4145fe6060f1SDimitry Andric else if (A->getOption().matches(options::OPT_gdbx)) 4146fe6060f1SDimitry Andric DebuggerTuning = llvm::DebuggerKind::DBX; 41470b57cec5SDimitry Andric else 41480b57cec5SDimitry Andric DebuggerTuning = llvm::DebuggerKind::GDB; 41490b57cec5SDimitry Andric } 41500b57cec5SDimitry Andric } 41510b57cec5SDimitry Andric 41520b57cec5SDimitry Andric // If a -gdwarf argument appeared, remember it. 4153fe6060f1SDimitry Andric const Arg *GDwarfN = getDwarfNArg(Args); 4154480093f4SDimitry Andric bool EmitDwarf = false; 4155480093f4SDimitry Andric if (GDwarfN) { 4156480093f4SDimitry Andric if (checkDebugInfoOption(GDwarfN, Args, D, TC)) 4157480093f4SDimitry Andric EmitDwarf = true; 4158480093f4SDimitry Andric else 4159480093f4SDimitry Andric GDwarfN = nullptr; 4160480093f4SDimitry Andric } 41610b57cec5SDimitry Andric 41620b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcodeview)) { 41630b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) 41640b57cec5SDimitry Andric EmitCodeView = true; 41650b57cec5SDimitry Andric } 41660b57cec5SDimitry Andric 41670b57cec5SDimitry Andric // If the user asked for debug info but did not explicitly specify -gcodeview 41680b57cec5SDimitry Andric // or -gdwarf, ask the toolchain for the default format. 4169480093f4SDimitry Andric if (!EmitCodeView && !EmitDwarf && 41700b57cec5SDimitry Andric DebugInfoKind != codegenoptions::NoDebugInfo) { 41710b57cec5SDimitry Andric switch (TC.getDefaultDebugFormat()) { 41720b57cec5SDimitry Andric case codegenoptions::DIF_CodeView: 41730b57cec5SDimitry Andric EmitCodeView = true; 41740b57cec5SDimitry Andric break; 41750b57cec5SDimitry Andric case codegenoptions::DIF_DWARF: 4176480093f4SDimitry Andric EmitDwarf = true; 41770b57cec5SDimitry Andric break; 41780b57cec5SDimitry Andric } 41790b57cec5SDimitry Andric } 41800b57cec5SDimitry Andric 4181e8d8bef9SDimitry Andric unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user 4182e8d8bef9SDimitry Andric unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may 4183e8d8bef9SDimitry Andric // be lower than what the user wanted. 4184480093f4SDimitry Andric unsigned DefaultDWARFVersion = ParseDebugDefaultVersion(TC, Args); 4185480093f4SDimitry Andric if (EmitDwarf) { 4186480093f4SDimitry Andric // Start with the platform default DWARF version 4187e8d8bef9SDimitry Andric RequestedDWARFVersion = TC.GetDefaultDwarfVersion(); 4188e8d8bef9SDimitry Andric assert(RequestedDWARFVersion && 4189e8d8bef9SDimitry Andric "toolchain default DWARF version must be nonzero"); 4190480093f4SDimitry Andric 4191480093f4SDimitry Andric // If the user specified a default DWARF version, that takes precedence 4192480093f4SDimitry Andric // over the platform default. 4193480093f4SDimitry Andric if (DefaultDWARFVersion) 4194e8d8bef9SDimitry Andric RequestedDWARFVersion = DefaultDWARFVersion; 4195480093f4SDimitry Andric 4196480093f4SDimitry Andric // Override with a user-specified DWARF version 4197480093f4SDimitry Andric if (GDwarfN) 4198480093f4SDimitry Andric if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling())) 4199e8d8bef9SDimitry Andric RequestedDWARFVersion = ExplicitVersion; 4200e8d8bef9SDimitry Andric // Clamp effective DWARF version to the max supported by the toolchain. 4201e8d8bef9SDimitry Andric EffectiveDWARFVersion = 4202e8d8bef9SDimitry Andric std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion()); 4203480093f4SDimitry Andric } 4204480093f4SDimitry Andric 42050b57cec5SDimitry Andric // -gline-directives-only supported only for the DWARF debug info. 4206e8d8bef9SDimitry Andric if (RequestedDWARFVersion == 0 && 4207e8d8bef9SDimitry Andric DebugInfoKind == codegenoptions::DebugDirectivesOnly) 42080b57cec5SDimitry Andric DebugInfoKind = codegenoptions::NoDebugInfo; 42090b57cec5SDimitry Andric 4210fe6060f1SDimitry Andric // strict DWARF is set to false by default. But for DBX, we need it to be set 4211fe6060f1SDimitry Andric // as true by default. 4212fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gstrict_dwarf)) 4213fe6060f1SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4214fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_gstrict_dwarf, options::OPT_gno_strict_dwarf, 4215fe6060f1SDimitry Andric DebuggerTuning == llvm::DebuggerKind::DBX)) 4216fe6060f1SDimitry Andric CmdArgs.push_back("-gstrict-dwarf"); 4217fe6060f1SDimitry Andric 42180b57cec5SDimitry Andric // And we handle flag -grecord-gcc-switches later with DWARFDebugFlags. 42190b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_flags_Group); 42200b57cec5SDimitry Andric 42210b57cec5SDimitry Andric // Column info is included by default for everything except SCE and 42220b57cec5SDimitry Andric // CodeView. Clang doesn't track end columns, just starting columns, which, 42230b57cec5SDimitry Andric // in theory, is fine for CodeView (and PDB). In practice, however, the 4224fe6060f1SDimitry Andric // Microsoft debuggers don't handle missing end columns well, and the AIX 4225fe6060f1SDimitry Andric // debugger DBX also doesn't handle the columns well, so it's better not to 4226fe6060f1SDimitry Andric // include any column info. 42270b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gcolumn_info)) 42280b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 42295ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info, 4230fe6060f1SDimitry Andric !EmitCodeView && 4231fe6060f1SDimitry Andric (DebuggerTuning != llvm::DebuggerKind::SCE && 4232fe6060f1SDimitry Andric DebuggerTuning != llvm::DebuggerKind::DBX))) 42335ffd83dbSDimitry Andric CmdArgs.push_back("-gno-column-info"); 42340b57cec5SDimitry Andric 42350b57cec5SDimitry Andric // FIXME: Move backend command line options to the module. 42360b57cec5SDimitry Andric // If -gline-tables-only or -gline-directives-only is the last option it wins. 42370b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gmodules)) 42380b57cec5SDimitry Andric if (checkDebugInfoOption(A, Args, D, TC)) { 42390b57cec5SDimitry Andric if (DebugInfoKind != codegenoptions::DebugLineTablesOnly && 42400b57cec5SDimitry Andric DebugInfoKind != codegenoptions::DebugDirectivesOnly) { 4241fe6060f1SDimitry Andric DebugInfoKind = codegenoptions::DebugInfoConstructor; 42420b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-ext-refs"); 42430b57cec5SDimitry Andric CmdArgs.push_back("-fmodule-format=obj"); 42440b57cec5SDimitry Andric } 42450b57cec5SDimitry Andric } 42460b57cec5SDimitry Andric 4247fe6060f1SDimitry Andric if (T.isOSBinFormatELF() && SplitDWARFInlining) 4248fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-dwarf-inlining"); 42490b57cec5SDimitry Andric 42500b57cec5SDimitry Andric // After we've dealt with all combinations of things that could 42510b57cec5SDimitry Andric // make DebugInfoKind be other than None or DebugLineTablesOnly, 42520b57cec5SDimitry Andric // figure out if we need to "upgrade" it to standalone debug info. 42530b57cec5SDimitry Andric // We parse these two '-f' options whether or not they will be used, 42540b57cec5SDimitry Andric // to claim them even if you wrote "-fstandalone-debug -gline-tables-only" 42550b57cec5SDimitry Andric bool NeedFullDebug = Args.hasFlag( 42560b57cec5SDimitry Andric options::OPT_fstandalone_debug, options::OPT_fno_standalone_debug, 42570b57cec5SDimitry Andric DebuggerTuning == llvm::DebuggerKind::LLDB || 42580b57cec5SDimitry Andric TC.GetDefaultStandaloneDebug()); 42590b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fstandalone_debug)) 42600b57cec5SDimitry Andric (void)checkDebugInfoOption(A, Args, D, TC); 4261e8d8bef9SDimitry Andric 4262fe6060f1SDimitry Andric if (DebugInfoKind == codegenoptions::LimitedDebugInfo || 4263fe6060f1SDimitry Andric DebugInfoKind == codegenoptions::DebugInfoConstructor) { 4264e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fno_eliminate_unused_debug_types, 4265e8d8bef9SDimitry Andric options::OPT_feliminate_unused_debug_types, false)) 4266e8d8bef9SDimitry Andric DebugInfoKind = codegenoptions::UnusedTypeInfo; 4267e8d8bef9SDimitry Andric else if (NeedFullDebug) 42680b57cec5SDimitry Andric DebugInfoKind = codegenoptions::FullDebugInfo; 4269e8d8bef9SDimitry Andric } 42700b57cec5SDimitry Andric 42710b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gembed_source, options::OPT_gno_embed_source, 42720b57cec5SDimitry Andric false)) { 42730b57cec5SDimitry Andric // Source embedding is a vendor extension to DWARF v5. By now we have 42740b57cec5SDimitry Andric // checked if a DWARF version was stated explicitly, and have otherwise 42750b57cec5SDimitry Andric // fallen back to the target default, so if this is still not at least 5 42760b57cec5SDimitry Andric // we emit an error. 42770b57cec5SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_gembed_source); 4278e8d8bef9SDimitry Andric if (RequestedDWARFVersion < 5) 42790b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 42800b57cec5SDimitry Andric << A->getAsString(Args) << "-gdwarf-5"; 4281e8d8bef9SDimitry Andric else if (EffectiveDWARFVersion < 5) 4282e8d8bef9SDimitry Andric // The toolchain has reduced allowed dwarf version, so we can't enable 4283e8d8bef9SDimitry Andric // -gembed-source. 4284e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_dwarf_version_limited_by_target) 4285e8d8bef9SDimitry Andric << A->getAsString(Args) << TC.getTripleString() << 5 4286e8d8bef9SDimitry Andric << EffectiveDWARFVersion; 42870b57cec5SDimitry Andric else if (checkDebugInfoOption(A, Args, D, TC)) 42880b57cec5SDimitry Andric CmdArgs.push_back("-gembed-source"); 42890b57cec5SDimitry Andric } 42900b57cec5SDimitry Andric 42910b57cec5SDimitry Andric if (EmitCodeView) { 42920b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview"); 42930b57cec5SDimitry Andric 42940b57cec5SDimitry Andric // Emit codeview type hashes if requested. 42950b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_gcodeview_ghash, 42960b57cec5SDimitry Andric options::OPT_gno_codeview_ghash, false)) { 42970b57cec5SDimitry Andric CmdArgs.push_back("-gcodeview-ghash"); 42980b57cec5SDimitry Andric } 42990b57cec5SDimitry Andric } 43000b57cec5SDimitry Andric 4301480093f4SDimitry Andric // Omit inline line tables if requested. 4302480093f4SDimitry Andric if (Args.hasFlag(options::OPT_gno_inline_line_tables, 4303480093f4SDimitry Andric options::OPT_ginline_line_tables, false)) { 4304480093f4SDimitry Andric CmdArgs.push_back("-gno-inline-line-tables"); 4305480093f4SDimitry Andric } 4306480093f4SDimitry Andric 4307480093f4SDimitry Andric // When emitting remarks, we need at least debug lines in the output. 4308480093f4SDimitry Andric if (willEmitRemarks(Args) && 4309480093f4SDimitry Andric DebugInfoKind <= codegenoptions::DebugDirectivesOnly) 4310480093f4SDimitry Andric DebugInfoKind = codegenoptions::DebugLineTablesOnly; 4311480093f4SDimitry Andric 4312e8d8bef9SDimitry Andric // Adjust the debug info kind for the given toolchain. 4313e8d8bef9SDimitry Andric TC.adjustDebugInfoKind(DebugInfoKind, Args); 4314e8d8bef9SDimitry Andric 4315e8d8bef9SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion, 43160b57cec5SDimitry Andric DebuggerTuning); 43170b57cec5SDimitry Andric 43180b57cec5SDimitry Andric // -fdebug-macro turns on macro debug info generation. 43190b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_macro, options::OPT_fno_debug_macro, 43200b57cec5SDimitry Andric false)) 43210b57cec5SDimitry Andric if (checkDebugInfoOption(Args.getLastArg(options::OPT_fdebug_macro), Args, 43220b57cec5SDimitry Andric D, TC)) 43230b57cec5SDimitry Andric CmdArgs.push_back("-debug-info-macro"); 43240b57cec5SDimitry Andric 43250b57cec5SDimitry Andric // -ggnu-pubnames turns on gnu style pubnames in the backend. 43260b57cec5SDimitry Andric const auto *PubnamesArg = 43270b57cec5SDimitry Andric Args.getLastArg(options::OPT_ggnu_pubnames, options::OPT_gno_gnu_pubnames, 43280b57cec5SDimitry Andric options::OPT_gpubnames, options::OPT_gno_pubnames); 43290b57cec5SDimitry Andric if (DwarfFission != DwarfFissionKind::None || 43300b57cec5SDimitry Andric (PubnamesArg && checkDebugInfoOption(PubnamesArg, Args, D, TC))) 43310b57cec5SDimitry Andric if (!PubnamesArg || 43320b57cec5SDimitry Andric (!PubnamesArg->getOption().matches(options::OPT_gno_gnu_pubnames) && 43330b57cec5SDimitry Andric !PubnamesArg->getOption().matches(options::OPT_gno_pubnames))) 43340b57cec5SDimitry Andric CmdArgs.push_back(PubnamesArg && PubnamesArg->getOption().matches( 43350b57cec5SDimitry Andric options::OPT_gpubnames) 43360b57cec5SDimitry Andric ? "-gpubnames" 43370b57cec5SDimitry Andric : "-ggnu-pubnames"); 4338349cc55cSDimitry Andric const auto *SimpleTemplateNamesArg = 433981ad6265SDimitry Andric Args.getLastArg(options::OPT_gsimple_template_names, 434081ad6265SDimitry Andric options::OPT_gno_simple_template_names); 4341349cc55cSDimitry Andric bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; 4342349cc55cSDimitry Andric if (SimpleTemplateNamesArg && 4343349cc55cSDimitry Andric checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { 4344349cc55cSDimitry Andric const auto &Opt = SimpleTemplateNamesArg->getOption(); 4345349cc55cSDimitry Andric if (Opt.matches(options::OPT_gsimple_template_names)) { 4346349cc55cSDimitry Andric ForwardTemplateParams = true; 4347349cc55cSDimitry Andric CmdArgs.push_back("-gsimple-template-names=simple"); 4348349cc55cSDimitry Andric } 4349349cc55cSDimitry Andric } 43500b57cec5SDimitry Andric 43510b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_ranges_base_address, 43520b57cec5SDimitry Andric options::OPT_fno_debug_ranges_base_address, false)) { 43530b57cec5SDimitry Andric CmdArgs.push_back("-fdebug-ranges-base-address"); 43540b57cec5SDimitry Andric } 43550b57cec5SDimitry Andric 43560b57cec5SDimitry Andric // -gdwarf-aranges turns on the emission of the aranges section in the 43570b57cec5SDimitry Andric // backend. 43580b57cec5SDimitry Andric // Always enabled for SCE tuning. 43590b57cec5SDimitry Andric bool NeedAranges = DebuggerTuning == llvm::DebuggerKind::SCE; 43600b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_gdwarf_aranges)) 43610b57cec5SDimitry Andric NeedAranges = checkDebugInfoOption(A, Args, D, TC) || NeedAranges; 43620b57cec5SDimitry Andric if (NeedAranges) { 43630b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 43640b57cec5SDimitry Andric CmdArgs.push_back("-generate-arange-section"); 43650b57cec5SDimitry Andric } 43660b57cec5SDimitry Andric 4367480093f4SDimitry Andric if (Args.hasFlag(options::OPT_fforce_dwarf_frame, 4368480093f4SDimitry Andric options::OPT_fno_force_dwarf_frame, false)) 4369480093f4SDimitry Andric CmdArgs.push_back("-fforce-dwarf-frame"); 4370480093f4SDimitry Andric 43710b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdebug_types_section, 43720b57cec5SDimitry Andric options::OPT_fno_debug_types_section, false)) { 4373e8d8bef9SDimitry Andric if (!(T.isOSBinFormatELF() || T.isOSBinFormatWasm())) { 43740b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 43750b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fdebug_types_section) 43760b57cec5SDimitry Andric ->getAsString(Args) 43770b57cec5SDimitry Andric << T.getTriple(); 43780b57cec5SDimitry Andric } else if (checkDebugInfoOption( 43790b57cec5SDimitry Andric Args.getLastArg(options::OPT_fdebug_types_section), Args, D, 43800b57cec5SDimitry Andric TC)) { 43810b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 43820b57cec5SDimitry Andric CmdArgs.push_back("-generate-type-units"); 43830b57cec5SDimitry Andric } 43840b57cec5SDimitry Andric } 43850b57cec5SDimitry Andric 4386fe6060f1SDimitry Andric // To avoid join/split of directory+filename, the integrated assembler prefers 4387fe6060f1SDimitry Andric // the directory form of .file on all DWARF versions. GNU as doesn't allow the 4388fe6060f1SDimitry Andric // form before DWARF v5. 4389fe6060f1SDimitry Andric if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, 4390fe6060f1SDimitry Andric options::OPT_fno_dwarf_directory_asm, 4391fe6060f1SDimitry Andric TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) 4392fe6060f1SDimitry Andric CmdArgs.push_back("-fno-dwarf-directory-asm"); 4393fe6060f1SDimitry Andric 43940b57cec5SDimitry Andric // Decide how to render forward declarations of template instantiations. 43950b57cec5SDimitry Andric // SCE wants full descriptions, others just get them in the name. 4396349cc55cSDimitry Andric if (ForwardTemplateParams) 43970b57cec5SDimitry Andric CmdArgs.push_back("-debug-forward-template-params"); 43980b57cec5SDimitry Andric 43990b57cec5SDimitry Andric // Do we need to explicitly import anonymous namespaces into the parent 44000b57cec5SDimitry Andric // scope? 44010b57cec5SDimitry Andric if (DebuggerTuning == llvm::DebuggerKind::SCE) 44020b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-explicit-import"); 44030b57cec5SDimitry Andric 4404fe6060f1SDimitry Andric renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion); 44050b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC); 44060b57cec5SDimitry Andric } 44070b57cec5SDimitry Andric 44080b57cec5SDimitry Andric void Clang::ConstructJob(Compilation &C, const JobAction &JA, 44090b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 44100b57cec5SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 44110b57cec5SDimitry Andric const auto &TC = getToolChain(); 44120b57cec5SDimitry Andric const llvm::Triple &RawTriple = TC.getTriple(); 44130b57cec5SDimitry Andric const llvm::Triple &Triple = TC.getEffectiveTriple(); 44140b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 44150b57cec5SDimitry Andric 44160b57cec5SDimitry Andric bool KernelOrKext = 44170b57cec5SDimitry Andric Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 44180b57cec5SDimitry Andric const Driver &D = TC.getDriver(); 44190b57cec5SDimitry Andric ArgStringList CmdArgs; 44200b57cec5SDimitry Andric 44210b57cec5SDimitry Andric assert(Inputs.size() >= 1 && "Must have at least one input."); 44220b57cec5SDimitry Andric // CUDA/HIP compilation may have multiple inputs (source file + results of 44230b57cec5SDimitry Andric // device-side compilations). OpenMP device jobs also take the host IR as a 44240b57cec5SDimitry Andric // second input. Module precompilation accepts a list of header files to 442581ad6265SDimitry Andric // include as part of the module. API extraction accepts a list of header 442681ad6265SDimitry Andric // files whose API information is emitted in the output. All other jobs are 442781ad6265SDimitry Andric // expected to have exactly one input. 44280b57cec5SDimitry Andric bool IsCuda = JA.isOffloading(Action::OFK_Cuda); 4429fe6060f1SDimitry Andric bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda); 44300b57cec5SDimitry Andric bool IsHIP = JA.isOffloading(Action::OFK_HIP); 4431fe6060f1SDimitry Andric bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP); 44320b57cec5SDimitry Andric bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); 44330b57cec5SDimitry Andric bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA); 443481ad6265SDimitry Andric bool IsExtractAPI = isa<ExtractAPIJobAction>(JA); 4435fe6060f1SDimitry Andric bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) || 4436fe6060f1SDimitry Andric JA.isDeviceOffloading(Action::OFK_Host)); 443781ad6265SDimitry Andric bool IsHostOffloadingAction = 443881ad6265SDimitry Andric (JA.isHostOffloading(Action::OFK_OpenMP) && 443981ad6265SDimitry Andric Args.hasFlag(options::OPT_fopenmp_new_driver, 444081ad6265SDimitry Andric options::OPT_no_offload_new_driver, true)) || 444181ad6265SDimitry Andric (JA.isHostOffloading(C.getActiveOffloadKinds()) && 444281ad6265SDimitry Andric Args.hasFlag(options::OPT_offload_new_driver, 444381ad6265SDimitry Andric options::OPT_no_offload_new_driver, false)); 444481ad6265SDimitry Andric 4445753f127fSDimitry Andric bool IsRDCMode = 4446753f127fSDimitry Andric Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false); 4447fe6060f1SDimitry Andric bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction); 4448fe6060f1SDimitry Andric auto LTOMode = D.getLTOMode(IsDeviceOffloadAction); 44490b57cec5SDimitry Andric 44500b57cec5SDimitry Andric // A header module compilation doesn't have a main input file, so invent a 44510b57cec5SDimitry Andric // fake one as a placeholder. 44520b57cec5SDimitry Andric const char *ModuleName = [&] { 44530b57cec5SDimitry Andric auto *ModuleNameArg = Args.getLastArg(options::OPT_fmodule_name_EQ); 44540b57cec5SDimitry Andric return ModuleNameArg ? ModuleNameArg->getValue() : ""; 44550b57cec5SDimitry Andric }(); 44560b57cec5SDimitry Andric InputInfo HeaderModuleInput(Inputs[0].getType(), ModuleName, ModuleName); 44570b57cec5SDimitry Andric 445881ad6265SDimitry Andric // Extract API doesn't have a main input file, so invent a fake one as a 445981ad6265SDimitry Andric // placeholder. 446081ad6265SDimitry Andric InputInfo ExtractAPIPlaceholderInput(Inputs[0].getType(), "extract-api", 446181ad6265SDimitry Andric "extract-api"); 446281ad6265SDimitry Andric 446381ad6265SDimitry Andric const InputInfo &Input = [&]() -> const InputInfo & { 446481ad6265SDimitry Andric if (IsHeaderModulePrecompile) 446581ad6265SDimitry Andric return HeaderModuleInput; 446681ad6265SDimitry Andric if (IsExtractAPI) 446781ad6265SDimitry Andric return ExtractAPIPlaceholderInput; 446881ad6265SDimitry Andric return Inputs[0]; 446981ad6265SDimitry Andric }(); 44700b57cec5SDimitry Andric 44710b57cec5SDimitry Andric InputInfoList ModuleHeaderInputs; 447281ad6265SDimitry Andric InputInfoList ExtractAPIInputs; 447381ad6265SDimitry Andric InputInfoList HostOffloadingInputs; 44740b57cec5SDimitry Andric const InputInfo *CudaDeviceInput = nullptr; 44750b57cec5SDimitry Andric const InputInfo *OpenMPDeviceInput = nullptr; 44760b57cec5SDimitry Andric for (const InputInfo &I : Inputs) { 44770b57cec5SDimitry Andric if (&I == &Input) { 44780b57cec5SDimitry Andric // This is the primary input. 44790b57cec5SDimitry Andric } else if (IsHeaderModulePrecompile && 44800b57cec5SDimitry Andric types::getPrecompiledType(I.getType()) == types::TY_PCH) { 44810b57cec5SDimitry Andric types::ID Expected = HeaderModuleInput.getType(); 44820b57cec5SDimitry Andric if (I.getType() != Expected) { 44830b57cec5SDimitry Andric D.Diag(diag::err_drv_module_header_wrong_kind) 44840b57cec5SDimitry Andric << I.getFilename() << types::getTypeName(I.getType()) 44850b57cec5SDimitry Andric << types::getTypeName(Expected); 44860b57cec5SDimitry Andric } 44870b57cec5SDimitry Andric ModuleHeaderInputs.push_back(I); 448881ad6265SDimitry Andric } else if (IsExtractAPI) { 448981ad6265SDimitry Andric auto ExpectedInputType = ExtractAPIPlaceholderInput.getType(); 449081ad6265SDimitry Andric if (I.getType() != ExpectedInputType) { 449181ad6265SDimitry Andric D.Diag(diag::err_drv_extract_api_wrong_kind) 449281ad6265SDimitry Andric << I.getFilename() << types::getTypeName(I.getType()) 449381ad6265SDimitry Andric << types::getTypeName(ExpectedInputType); 449481ad6265SDimitry Andric } 449581ad6265SDimitry Andric ExtractAPIInputs.push_back(I); 449681ad6265SDimitry Andric } else if (IsHostOffloadingAction) { 449781ad6265SDimitry Andric HostOffloadingInputs.push_back(I); 44980b57cec5SDimitry Andric } else if ((IsCuda || IsHIP) && !CudaDeviceInput) { 44990b57cec5SDimitry Andric CudaDeviceInput = &I; 45000b57cec5SDimitry Andric } else if (IsOpenMPDevice && !OpenMPDeviceInput) { 45010b57cec5SDimitry Andric OpenMPDeviceInput = &I; 45020b57cec5SDimitry Andric } else { 45030b57cec5SDimitry Andric llvm_unreachable("unexpectedly given multiple inputs"); 45040b57cec5SDimitry Andric } 45050b57cec5SDimitry Andric } 45060b57cec5SDimitry Andric 45075ffd83dbSDimitry Andric const llvm::Triple *AuxTriple = 45085ffd83dbSDimitry Andric (IsCuda || IsHIP) ? TC.getAuxTriple() : nullptr; 45090b57cec5SDimitry Andric bool IsWindowsMSVC = RawTriple.isWindowsMSVCEnvironment(); 45100b57cec5SDimitry Andric bool IsIAMCU = RawTriple.isOSIAMCU(); 45110b57cec5SDimitry Andric 45120b57cec5SDimitry Andric // Adjust IsWindowsXYZ for CUDA/HIP compilations. Even when compiling in 45130b57cec5SDimitry Andric // device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not 45140b57cec5SDimitry Andric // Windows), we need to pass Windows-specific flags to cc1. 45150b57cec5SDimitry Andric if (IsCuda || IsHIP) 45160b57cec5SDimitry Andric IsWindowsMSVC |= AuxTriple && AuxTriple->isWindowsMSVCEnvironment(); 45170b57cec5SDimitry Andric 45180b57cec5SDimitry Andric // C++ is not supported for IAMCU. 45190b57cec5SDimitry Andric if (IsIAMCU && types::isCXX(Input.getType())) 45200b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU"; 45210b57cec5SDimitry Andric 45220b57cec5SDimitry Andric // Invoke ourselves in -cc1 mode. 45230b57cec5SDimitry Andric // 45240b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 45250b57cec5SDimitry Andric CmdArgs.push_back("-cc1"); 45260b57cec5SDimitry Andric 45270b57cec5SDimitry Andric // Add the "effective" target triple. 45280b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 45290b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 45300b57cec5SDimitry Andric 45310b57cec5SDimitry Andric if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) { 45320b57cec5SDimitry Andric DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args); 45330b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_MJ); 4534a7dea167SDimitry Andric } else if (const Arg *GenCDBFragment = 4535a7dea167SDimitry Andric Args.getLastArg(options::OPT_gen_cdb_fragment_path)) { 4536a7dea167SDimitry Andric DumpCompilationDatabaseFragmentToDir(GenCDBFragment->getValue(), C, 4537a7dea167SDimitry Andric TripleStr, Output, Input, Args); 4538a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_gen_cdb_fragment_path); 45390b57cec5SDimitry Andric } 45400b57cec5SDimitry Andric 45410b57cec5SDimitry Andric if (IsCuda || IsHIP) { 45420b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for a CUDA/HIP device 45430b57cec5SDimitry Andric // and vice-versa. 45440b57cec5SDimitry Andric std::string NormalizedTriple; 45450b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_Cuda) || 45460b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_HIP)) 45470b57cec5SDimitry Andric NormalizedTriple = C.getSingleOffloadToolChain<Action::OFK_Host>() 45480b57cec5SDimitry Andric ->getTriple() 45490b57cec5SDimitry Andric .normalize(); 45500b57cec5SDimitry Andric else { 45510b57cec5SDimitry Andric // Host-side compilation. 45520b57cec5SDimitry Andric NormalizedTriple = 45530b57cec5SDimitry Andric (IsCuda ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() 45540b57cec5SDimitry Andric : C.getSingleOffloadToolChain<Action::OFK_HIP>()) 45550b57cec5SDimitry Andric ->getTriple() 45560b57cec5SDimitry Andric .normalize(); 45570b57cec5SDimitry Andric if (IsCuda) { 45580b57cec5SDimitry Andric // We need to figure out which CUDA version we're compiling for, as that 45590b57cec5SDimitry Andric // determines how we load and launch GPU kernels. 45600b57cec5SDimitry Andric auto *CTC = static_cast<const toolchains::CudaToolChain *>( 45610b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Cuda>()); 45620b57cec5SDimitry Andric assert(CTC && "Expected valid CUDA Toolchain."); 45630b57cec5SDimitry Andric if (CTC && CTC->CudaInstallation.version() != CudaVersion::UNKNOWN) 45640b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 45650b57cec5SDimitry Andric Twine("-target-sdk-version=") + 45660b57cec5SDimitry Andric CudaVersionToString(CTC->CudaInstallation.version()))); 45670b57cec5SDimitry Andric } 45680b57cec5SDimitry Andric } 45690b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 45700b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 45710b57cec5SDimitry Andric } 45720b57cec5SDimitry Andric 45735ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { 45745ffd83dbSDimitry Andric CmdArgs.push_back("-fsycl-is-device"); 45755ffd83dbSDimitry Andric 45765ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { 45775ffd83dbSDimitry Andric A->render(Args, CmdArgs); 45785ffd83dbSDimitry Andric } else { 4579fe6060f1SDimitry Andric // Ensure the default version in SYCL mode is 2020. 4580fe6060f1SDimitry Andric CmdArgs.push_back("-sycl-std=2020"); 45815ffd83dbSDimitry Andric } 45825ffd83dbSDimitry Andric } 45835ffd83dbSDimitry Andric 45840b57cec5SDimitry Andric if (IsOpenMPDevice) { 45850b57cec5SDimitry Andric // We have to pass the triple of the host if compiling for an OpenMP device. 45860b57cec5SDimitry Andric std::string NormalizedTriple = 45870b57cec5SDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Host>() 45880b57cec5SDimitry Andric ->getTriple() 45890b57cec5SDimitry Andric .normalize(); 45900b57cec5SDimitry Andric CmdArgs.push_back("-aux-triple"); 45910b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); 45920b57cec5SDimitry Andric } 45930b57cec5SDimitry Andric 45940b57cec5SDimitry Andric if (Triple.isOSWindows() && (Triple.getArch() == llvm::Triple::arm || 45950b57cec5SDimitry Andric Triple.getArch() == llvm::Triple::thumb)) { 45960b57cec5SDimitry Andric unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6; 45975ffd83dbSDimitry Andric unsigned Version = 0; 45985ffd83dbSDimitry Andric bool Failure = 45995ffd83dbSDimitry Andric Triple.getArchName().substr(Offset).consumeInteger(10, Version); 46005ffd83dbSDimitry Andric if (Failure || Version < 7) 46010b57cec5SDimitry Andric D.Diag(diag::err_target_unsupported_arch) << Triple.getArchName() 46020b57cec5SDimitry Andric << TripleStr; 46030b57cec5SDimitry Andric } 46040b57cec5SDimitry Andric 46050b57cec5SDimitry Andric // Push all default warning arguments that are specific to 46060b57cec5SDimitry Andric // the given target. These come before user provided warning options 46070b57cec5SDimitry Andric // are provided. 46080b57cec5SDimitry Andric TC.addClangWarningOptions(CmdArgs); 46090b57cec5SDimitry Andric 4610fe6060f1SDimitry Andric // FIXME: Subclass ToolChain for SPIR and move this to addClangWarningOptions. 4611349cc55cSDimitry Andric if (Triple.isSPIR() || Triple.isSPIRV()) 4612fe6060f1SDimitry Andric CmdArgs.push_back("-Wspir-compat"); 4613fe6060f1SDimitry Andric 46140b57cec5SDimitry Andric // Select the appropriate action. 46150b57cec5SDimitry Andric RewriteKind rewriteKind = RK_None; 46160b57cec5SDimitry Andric 4617a7dea167SDimitry Andric // If CollectArgsForIntegratedAssembler() isn't called below, claim the args 4618a7dea167SDimitry Andric // it claims when not running an assembler. Otherwise, clang would emit 4619a7dea167SDimitry Andric // "argument unused" warnings for assembler flags when e.g. adding "-E" to 4620a7dea167SDimitry Andric // flags while debugging something. That'd be somewhat inconvenient, and it's 4621a7dea167SDimitry Andric // also inconsistent with most other flags -- we don't warn on 4622a7dea167SDimitry Andric // -ffunction-sections not being used in -E mode either for example, even 4623a7dea167SDimitry Andric // though it's not really used either. 4624a7dea167SDimitry Andric if (!isa<AssembleJobAction>(JA)) { 4625a7dea167SDimitry Andric // The args claimed here should match the args used in 4626a7dea167SDimitry Andric // CollectArgsForIntegratedAssembler(). 4627a7dea167SDimitry Andric if (TC.useIntegratedAs()) { 4628a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mrelax_all); 4629a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_relax_all); 4630a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mincremental_linker_compatible); 4631a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mno_incremental_linker_compatible); 4632a7dea167SDimitry Andric switch (C.getDefaultToolChain().getArch()) { 4633a7dea167SDimitry Andric case llvm::Triple::arm: 4634a7dea167SDimitry Andric case llvm::Triple::armeb: 4635a7dea167SDimitry Andric case llvm::Triple::thumb: 4636a7dea167SDimitry Andric case llvm::Triple::thumbeb: 4637a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_mimplicit_it_EQ); 4638a7dea167SDimitry Andric break; 4639a7dea167SDimitry Andric default: 4640a7dea167SDimitry Andric break; 4641a7dea167SDimitry Andric } 4642a7dea167SDimitry Andric } 4643a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Wa_COMMA); 4644a7dea167SDimitry Andric Args.ClaimAllArgs(options::OPT_Xassembler); 464581ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_femit_dwarf_unwind_EQ); 4646a7dea167SDimitry Andric } 4647a7dea167SDimitry Andric 46480b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) { 46490b57cec5SDimitry Andric assert(JA.getType() == types::TY_Plist && "Invalid output type."); 46500b57cec5SDimitry Andric CmdArgs.push_back("-analyze"); 46510b57cec5SDimitry Andric } else if (isa<MigrateJobAction>(JA)) { 46520b57cec5SDimitry Andric CmdArgs.push_back("-migrate"); 46530b57cec5SDimitry Andric } else if (isa<PreprocessJobAction>(JA)) { 46540b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) 46550b57cec5SDimitry Andric CmdArgs.push_back("-Eonly"); 46560b57cec5SDimitry Andric else { 46570b57cec5SDimitry Andric CmdArgs.push_back("-E"); 46580b57cec5SDimitry Andric if (Args.hasArg(options::OPT_rewrite_objc) && 46590b57cec5SDimitry Andric !Args.hasArg(options::OPT_g_Group)) 46600b57cec5SDimitry Andric CmdArgs.push_back("-P"); 466181ad6265SDimitry Andric else if (JA.getType() == types::TY_PP_CXXHeaderUnit) 466281ad6265SDimitry Andric CmdArgs.push_back("-fdirectives-only"); 46630b57cec5SDimitry Andric } 46640b57cec5SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 46650b57cec5SDimitry Andric CmdArgs.push_back("-emit-obj"); 46660b57cec5SDimitry Andric 46670b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 46680b57cec5SDimitry Andric 46690b57cec5SDimitry Andric // Also ignore explicit -force_cpusubtype_ALL option. 46700b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 46710b57cec5SDimitry Andric } else if (isa<PrecompileJobAction>(JA)) { 46720b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) 46730b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 46740b57cec5SDimitry Andric else if (JA.getType() == types::TY_ModuleFile) 46750b57cec5SDimitry Andric CmdArgs.push_back(IsHeaderModulePrecompile 46760b57cec5SDimitry Andric ? "-emit-header-module" 46770b57cec5SDimitry Andric : "-emit-module-interface"); 467881ad6265SDimitry Andric else if (JA.getType() == types::TY_HeaderUnit) 467981ad6265SDimitry Andric CmdArgs.push_back("-emit-header-unit"); 46800b57cec5SDimitry Andric else 46810b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 46820b57cec5SDimitry Andric } else if (isa<VerifyPCHJobAction>(JA)) { 46830b57cec5SDimitry Andric CmdArgs.push_back("-verify-pch"); 468481ad6265SDimitry Andric } else if (isa<ExtractAPIJobAction>(JA)) { 468581ad6265SDimitry Andric assert(JA.getType() == types::TY_API_INFO && 468681ad6265SDimitry Andric "Extract API actions must generate a API information."); 468781ad6265SDimitry Andric CmdArgs.push_back("-extract-api"); 468881ad6265SDimitry Andric if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ)) 468981ad6265SDimitry Andric ProductNameArg->render(Args, CmdArgs); 46900b57cec5SDimitry Andric } else { 46910b57cec5SDimitry Andric assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) && 46920b57cec5SDimitry Andric "Invalid action for clang tool."); 46930b57cec5SDimitry Andric if (JA.getType() == types::TY_Nothing) { 46940b57cec5SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 46950b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 46960b57cec5SDimitry Andric JA.getType() == types::TY_LTO_IR) { 46970b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm"); 46980b57cec5SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 46990b57cec5SDimitry Andric JA.getType() == types::TY_LTO_BC) { 4700fe6060f1SDimitry Andric // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S 4701fe6060f1SDimitry Andric if (Triple.isAMDGCN() && IsOpenMPDevice && Args.hasArg(options::OPT_S) && 4702fe6060f1SDimitry Andric Args.hasArg(options::OPT_emit_llvm)) { 4703fe6060f1SDimitry Andric CmdArgs.push_back("-emit-llvm"); 4704fe6060f1SDimitry Andric } else { 47050b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 4706fe6060f1SDimitry Andric } 4707a7dea167SDimitry Andric } else if (JA.getType() == types::TY_IFS || 4708a7dea167SDimitry Andric JA.getType() == types::TY_IFS_CPP) { 4709a7dea167SDimitry Andric StringRef ArgStr = 4710a7dea167SDimitry Andric Args.hasArg(options::OPT_interface_stub_version_EQ) 4711a7dea167SDimitry Andric ? Args.getLastArgValue(options::OPT_interface_stub_version_EQ) 4712fe6060f1SDimitry Andric : "ifs-v1"; 47130b57cec5SDimitry Andric CmdArgs.push_back("-emit-interface-stubs"); 47140b57cec5SDimitry Andric CmdArgs.push_back( 4715a7dea167SDimitry Andric Args.MakeArgString(Twine("-interface-stub-version=") + ArgStr.str())); 47160b57cec5SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 47170b57cec5SDimitry Andric CmdArgs.push_back("-S"); 47180b57cec5SDimitry Andric } else if (JA.getType() == types::TY_AST) { 47190b57cec5SDimitry Andric CmdArgs.push_back("-emit-pch"); 47200b57cec5SDimitry Andric } else if (JA.getType() == types::TY_ModuleFile) { 47210b57cec5SDimitry Andric CmdArgs.push_back("-module-file-info"); 47220b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenObjC) { 47230b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 47240b57cec5SDimitry Andric rewriteKind = RK_NonFragile; 47250b57cec5SDimitry Andric } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 47260b57cec5SDimitry Andric CmdArgs.push_back("-rewrite-objc"); 47270b57cec5SDimitry Andric rewriteKind = RK_Fragile; 47280b57cec5SDimitry Andric } else { 47290b57cec5SDimitry Andric assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!"); 47300b57cec5SDimitry Andric } 47310b57cec5SDimitry Andric 47320b57cec5SDimitry Andric // Preserve use-list order by default when emitting bitcode, so that 47330b57cec5SDimitry Andric // loading the bitcode up in 'opt' or 'llc' and running passes gives the 47340b57cec5SDimitry Andric // same result as running passes here. For LTO, we don't need to preserve 47350b57cec5SDimitry Andric // the use-list order, since serialization to bitcode is part of the flow. 47360b57cec5SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 47370b57cec5SDimitry Andric CmdArgs.push_back("-emit-llvm-uselists"); 47380b57cec5SDimitry Andric 473981ad6265SDimitry Andric if (IsUsingLTO) { 4740349cc55cSDimitry Andric // Only AMDGPU supports device-side LTO. 474181ad6265SDimitry Andric if (IsDeviceOffloadAction && 474281ad6265SDimitry Andric !Args.hasFlag(options::OPT_fopenmp_new_driver, 474381ad6265SDimitry Andric options::OPT_no_offload_new_driver, true) && 474481ad6265SDimitry Andric !Args.hasFlag(options::OPT_offload_new_driver, 474581ad6265SDimitry Andric options::OPT_no_offload_new_driver, false) && 474681ad6265SDimitry Andric !Triple.isAMDGPU()) { 4747fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 4748fe6060f1SDimitry Andric << Args.getLastArg(options::OPT_foffload_lto, 4749fe6060f1SDimitry Andric options::OPT_foffload_lto_EQ) 4750fe6060f1SDimitry Andric ->getAsString(Args) 4751fe6060f1SDimitry Andric << Triple.getTriple(); 4752349cc55cSDimitry Andric } else { 4753349cc55cSDimitry Andric assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); 4754349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 4755349cc55cSDimitry Andric Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 4756349cc55cSDimitry Andric CmdArgs.push_back("-flto-unit"); 4757fe6060f1SDimitry Andric } 47580b57cec5SDimitry Andric } 47590b57cec5SDimitry Andric } 47600b57cec5SDimitry Andric 47610b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_index_EQ)) { 47620b57cec5SDimitry Andric if (!types::isLLVMIR(Input.getType())) 47630b57cec5SDimitry Andric D.Diag(diag::err_drv_arg_requires_bitcode_input) << A->getAsString(Args); 47640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ); 47650b57cec5SDimitry Andric } 47660b57cec5SDimitry Andric 4767480093f4SDimitry Andric if (Args.getLastArg(options::OPT_fthin_link_bitcode_EQ)) 4768480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fthin_link_bitcode_EQ); 4769480093f4SDimitry Andric 47700b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 47710b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ); 47720b57cec5SDimitry Andric 4773e8d8bef9SDimitry Andric auto *MemProfArg = Args.getLastArg(options::OPT_fmemory_profile, 4774e8d8bef9SDimitry Andric options::OPT_fmemory_profile_EQ, 4775e8d8bef9SDimitry Andric options::OPT_fno_memory_profile); 4776e8d8bef9SDimitry Andric if (MemProfArg && 4777e8d8bef9SDimitry Andric !MemProfArg->getOption().matches(options::OPT_fno_memory_profile)) 4778e8d8bef9SDimitry Andric MemProfArg->render(Args, CmdArgs); 4779e8d8bef9SDimitry Andric 47800b57cec5SDimitry Andric // Embed-bitcode option. 47810b57cec5SDimitry Andric // Only white-listed flags below are allowed to be embedded. 4782fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO && 47830b57cec5SDimitry Andric (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) { 47840b57cec5SDimitry Andric // Add flags implied by -fembed-bitcode. 47850b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); 47860b57cec5SDimitry Andric // Disable all llvm IR level optimizations. 47870b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 47880b57cec5SDimitry Andric 4789480093f4SDimitry Andric // Render target options. 47900b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 47910b57cec5SDimitry Andric 47920b57cec5SDimitry Andric // reject options that shouldn't be supported in bitcode 47930b57cec5SDimitry Andric // also reject kernel/kext 4794349cc55cSDimitry Andric static const constexpr unsigned kBitcodeOptionIgnorelist[] = { 47950b57cec5SDimitry Andric options::OPT_mkernel, 47960b57cec5SDimitry Andric options::OPT_fapple_kext, 47970b57cec5SDimitry Andric options::OPT_ffunction_sections, 47980b57cec5SDimitry Andric options::OPT_fno_function_sections, 47990b57cec5SDimitry Andric options::OPT_fdata_sections, 48000b57cec5SDimitry Andric options::OPT_fno_data_sections, 48015ffd83dbSDimitry Andric options::OPT_fbasic_block_sections_EQ, 48025ffd83dbSDimitry Andric options::OPT_funique_internal_linkage_names, 48035ffd83dbSDimitry Andric options::OPT_fno_unique_internal_linkage_names, 48040b57cec5SDimitry Andric options::OPT_funique_section_names, 48050b57cec5SDimitry Andric options::OPT_fno_unique_section_names, 48065ffd83dbSDimitry Andric options::OPT_funique_basic_block_section_names, 48075ffd83dbSDimitry Andric options::OPT_fno_unique_basic_block_section_names, 48080b57cec5SDimitry Andric options::OPT_mrestrict_it, 48090b57cec5SDimitry Andric options::OPT_mno_restrict_it, 48100b57cec5SDimitry Andric options::OPT_mstackrealign, 48110b57cec5SDimitry Andric options::OPT_mno_stackrealign, 48120b57cec5SDimitry Andric options::OPT_mstack_alignment, 48130b57cec5SDimitry Andric options::OPT_mcmodel_EQ, 48140b57cec5SDimitry Andric options::OPT_mlong_calls, 48150b57cec5SDimitry Andric options::OPT_mno_long_calls, 48160b57cec5SDimitry Andric options::OPT_ggnu_pubnames, 48170b57cec5SDimitry Andric options::OPT_gdwarf_aranges, 48180b57cec5SDimitry Andric options::OPT_fdebug_types_section, 48190b57cec5SDimitry Andric options::OPT_fno_debug_types_section, 48200b57cec5SDimitry Andric options::OPT_fdwarf_directory_asm, 48210b57cec5SDimitry Andric options::OPT_fno_dwarf_directory_asm, 48220b57cec5SDimitry Andric options::OPT_mrelax_all, 48230b57cec5SDimitry Andric options::OPT_mno_relax_all, 48240b57cec5SDimitry Andric options::OPT_ftrap_function_EQ, 48250b57cec5SDimitry Andric options::OPT_ffixed_r9, 48260b57cec5SDimitry Andric options::OPT_mfix_cortex_a53_835769, 48270b57cec5SDimitry Andric options::OPT_mno_fix_cortex_a53_835769, 48280b57cec5SDimitry Andric options::OPT_ffixed_x18, 48290b57cec5SDimitry Andric options::OPT_mglobal_merge, 48300b57cec5SDimitry Andric options::OPT_mno_global_merge, 48310b57cec5SDimitry Andric options::OPT_mred_zone, 48320b57cec5SDimitry Andric options::OPT_mno_red_zone, 48330b57cec5SDimitry Andric options::OPT_Wa_COMMA, 48340b57cec5SDimitry Andric options::OPT_Xassembler, 48350b57cec5SDimitry Andric options::OPT_mllvm, 48360b57cec5SDimitry Andric }; 48370b57cec5SDimitry Andric for (const auto &A : Args) 4838349cc55cSDimitry Andric if (llvm::is_contained(kBitcodeOptionIgnorelist, A->getOption().getID())) 48390b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_embed_bitcode) << A->getSpelling(); 48400b57cec5SDimitry Andric 48410b57cec5SDimitry Andric // Render the CodeGen options that need to be passed. 484281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 484381ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 48440b57cec5SDimitry Andric 48450b57cec5SDimitry Andric RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args, 48465ffd83dbSDimitry Andric CmdArgs, JA); 48470b57cec5SDimitry Andric 48480b57cec5SDimitry Andric // Render ABI arguments 48490b57cec5SDimitry Andric switch (TC.getArch()) { 48500b57cec5SDimitry Andric default: break; 48510b57cec5SDimitry Andric case llvm::Triple::arm: 48520b57cec5SDimitry Andric case llvm::Triple::armeb: 48530b57cec5SDimitry Andric case llvm::Triple::thumbeb: 4854349cc55cSDimitry Andric RenderARMABI(D, Triple, Args, CmdArgs); 48550b57cec5SDimitry Andric break; 48560b57cec5SDimitry Andric case llvm::Triple::aarch64: 4857480093f4SDimitry Andric case llvm::Triple::aarch64_32: 48580b57cec5SDimitry Andric case llvm::Triple::aarch64_be: 48590b57cec5SDimitry Andric RenderAArch64ABI(Triple, Args, CmdArgs); 48600b57cec5SDimitry Andric break; 48610b57cec5SDimitry Andric } 48620b57cec5SDimitry Andric 48630b57cec5SDimitry Andric // Optimization level for CodeGen. 48640b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 48650b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 48660b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 48670b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 48680b57cec5SDimitry Andric } else { 48690b57cec5SDimitry Andric A->render(Args, CmdArgs); 48700b57cec5SDimitry Andric } 48710b57cec5SDimitry Andric } 48720b57cec5SDimitry Andric 48730b57cec5SDimitry Andric // Input/Output file. 48740b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 48750b57cec5SDimitry Andric // Handled with other dependency code. 48760b57cec5SDimitry Andric } else if (Output.isFilename()) { 48770b57cec5SDimitry Andric CmdArgs.push_back("-o"); 48780b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 48790b57cec5SDimitry Andric } else { 48800b57cec5SDimitry Andric assert(Output.isNothing() && "Input output."); 48810b57cec5SDimitry Andric } 48820b57cec5SDimitry Andric 48830b57cec5SDimitry Andric for (const auto &II : Inputs) { 48840b57cec5SDimitry Andric addDashXForInput(Args, II, CmdArgs); 48850b57cec5SDimitry Andric if (II.isFilename()) 48860b57cec5SDimitry Andric CmdArgs.push_back(II.getFilename()); 48870b57cec5SDimitry Andric else 48880b57cec5SDimitry Andric II.getInputArg().renderAsInput(Args, CmdArgs); 48890b57cec5SDimitry Andric } 48900b57cec5SDimitry Andric 4891e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>( 4892e8d8bef9SDimitry Andric JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(), 4893e8d8bef9SDimitry Andric CmdArgs, Inputs, Output)); 48940b57cec5SDimitry Andric return; 48950b57cec5SDimitry Andric } 48960b57cec5SDimitry Andric 4897fe6060f1SDimitry Andric if (C.getDriver().embedBitcodeMarkerOnly() && !IsUsingLTO) 48980b57cec5SDimitry Andric CmdArgs.push_back("-fembed-bitcode=marker"); 48990b57cec5SDimitry Andric 49000b57cec5SDimitry Andric // We normally speed up the clang process a bit by skipping destructors at 49010b57cec5SDimitry Andric // exit, but when we're generating diagnostics we can rely on some of the 49020b57cec5SDimitry Andric // cleanup. 49030b57cec5SDimitry Andric if (!C.isForDiagnostics()) 49040b57cec5SDimitry Andric CmdArgs.push_back("-disable-free"); 4905349cc55cSDimitry Andric CmdArgs.push_back("-clear-ast-before-backend"); 49060b57cec5SDimitry Andric 49070b57cec5SDimitry Andric #ifdef NDEBUG 49080b57cec5SDimitry Andric const bool IsAssertBuild = false; 49090b57cec5SDimitry Andric #else 49100b57cec5SDimitry Andric const bool IsAssertBuild = true; 49110b57cec5SDimitry Andric #endif 49120b57cec5SDimitry Andric 49130b57cec5SDimitry Andric // Disable the verification pass in -asserts builds. 49140b57cec5SDimitry Andric if (!IsAssertBuild) 49150b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-verifier"); 49160b57cec5SDimitry Andric 49170b57cec5SDimitry Andric // Discard value names in assert builds unless otherwise specified. 49180b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdiscard_value_names, 491947395794SDimitry Andric options::OPT_fno_discard_value_names, !IsAssertBuild)) { 492047395794SDimitry Andric if (Args.hasArg(options::OPT_fdiscard_value_names) && 4921349cc55cSDimitry Andric llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) { 492247395794SDimitry Andric return types::isLLVMIR(II.getType()); 4923349cc55cSDimitry Andric })) { 492447395794SDimitry Andric D.Diag(diag::warn_ignoring_fdiscard_for_bitcode); 492547395794SDimitry Andric } 49260b57cec5SDimitry Andric CmdArgs.push_back("-discard-value-names"); 492747395794SDimitry Andric } 49280b57cec5SDimitry Andric 49290b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 49300b57cec5SDimitry Andric // -save-temps. 49310b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 49320b57cec5SDimitry Andric CmdArgs.push_back(getBaseInputName(Args, Input)); 49330b57cec5SDimitry Andric 49340b57cec5SDimitry Andric // Some flags which affect the language (via preprocessor 49350b57cec5SDimitry Andric // defines). 49360b57cec5SDimitry Andric if (Args.hasArg(options::OPT_static)) 49370b57cec5SDimitry Andric CmdArgs.push_back("-static-define"); 49380b57cec5SDimitry Andric 49390b57cec5SDimitry Andric if (Args.hasArg(options::OPT_municode)) 49400b57cec5SDimitry Andric CmdArgs.push_back("-DUNICODE"); 49410b57cec5SDimitry Andric 49420b57cec5SDimitry Andric if (isa<AnalyzeJobAction>(JA)) 49430b57cec5SDimitry Andric RenderAnalyzerOptions(Args, CmdArgs, Triple, Input); 49440b57cec5SDimitry Andric 4945a7dea167SDimitry Andric if (isa<AnalyzeJobAction>(JA) || 4946a7dea167SDimitry Andric (isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze))) 4947a7dea167SDimitry Andric CmdArgs.push_back("-setup-static-analyzer"); 4948a7dea167SDimitry Andric 49490b57cec5SDimitry Andric // Enable compatilibily mode to avoid analyzer-config related errors. 49500b57cec5SDimitry Andric // Since we can't access frontend flags through hasArg, let's manually iterate 49510b57cec5SDimitry Andric // through them. 49520b57cec5SDimitry Andric bool FoundAnalyzerConfig = false; 49530b57cec5SDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) 49540b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 49550b57cec5SDimitry Andric FoundAnalyzerConfig = true; 49560b57cec5SDimitry Andric break; 49570b57cec5SDimitry Andric } 49580b57cec5SDimitry Andric if (!FoundAnalyzerConfig) 49590b57cec5SDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xanalyzer)) 49600b57cec5SDimitry Andric if (StringRef(Arg->getValue()) == "-analyzer-config") { 49610b57cec5SDimitry Andric FoundAnalyzerConfig = true; 49620b57cec5SDimitry Andric break; 49630b57cec5SDimitry Andric } 49640b57cec5SDimitry Andric if (FoundAnalyzerConfig) 49650b57cec5SDimitry Andric CmdArgs.push_back("-analyzer-config-compatibility-mode=true"); 49660b57cec5SDimitry Andric 49670b57cec5SDimitry Andric CheckCodeGenerationOptions(D, Args); 49680b57cec5SDimitry Andric 49690b57cec5SDimitry Andric unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args); 49700b57cec5SDimitry Andric assert(FunctionAlignment <= 31 && "function alignment will be truncated!"); 49710b57cec5SDimitry Andric if (FunctionAlignment) { 49720b57cec5SDimitry Andric CmdArgs.push_back("-function-alignment"); 49730b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment))); 49740b57cec5SDimitry Andric } 49750b57cec5SDimitry Andric 4976349cc55cSDimitry Andric // We support -falign-loops=N where N is a power of 2. GCC supports more 4977349cc55cSDimitry Andric // forms. 4978349cc55cSDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_falign_loops_EQ)) { 4979349cc55cSDimitry Andric unsigned Value = 0; 4980349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) 4981349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_invalid_int_value) 4982349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 4983349cc55cSDimitry Andric else if (Value & (Value - 1)) 4984349cc55cSDimitry Andric TC.getDriver().Diag(diag::err_drv_alignment_not_power_of_two) 4985349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 4986349cc55cSDimitry Andric // Treat =0 as unspecified (use the target preference). 4987349cc55cSDimitry Andric if (Value) 4988349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-falign-loops=" + 4989349cc55cSDimitry Andric Twine(std::min(Value, 65536u)))); 4990349cc55cSDimitry Andric } 4991349cc55cSDimitry Andric 49920b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 49930b57cec5SDimitry Andric unsigned PICLevel; 49940b57cec5SDimitry Andric bool IsPIE; 49950b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(TC, Args); 49960b57cec5SDimitry Andric 49975ffd83dbSDimitry Andric bool IsROPI = RelocationModel == llvm::Reloc::ROPI || 49985ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 49995ffd83dbSDimitry Andric bool IsRWPI = RelocationModel == llvm::Reloc::RWPI || 50005ffd83dbSDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI; 50010b57cec5SDimitry Andric 50025ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_mcmse) && 50035ffd83dbSDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) { 50045ffd83dbSDimitry Andric if (IsROPI) 50055ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << IsROPI; 50065ffd83dbSDimitry Andric if (IsRWPI) 50075ffd83dbSDimitry Andric D.Diag(diag::err_cmse_pi_are_incompatible) << !IsRWPI; 50085ffd83dbSDimitry Andric } 50095ffd83dbSDimitry Andric 50105ffd83dbSDimitry Andric if (IsROPI && types::isCXX(Input.getType()) && 50110b57cec5SDimitry Andric !Args.hasArg(options::OPT_fallow_unsupported)) 50120b57cec5SDimitry Andric D.Diag(diag::err_drv_ropi_incompatible_with_cxx); 50130b57cec5SDimitry Andric 50145ffd83dbSDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 50150b57cec5SDimitry Andric if (RMName) { 50160b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 50170b57cec5SDimitry Andric CmdArgs.push_back(RMName); 50180b57cec5SDimitry Andric } 50190b57cec5SDimitry Andric if (PICLevel > 0) { 50200b57cec5SDimitry Andric CmdArgs.push_back("-pic-level"); 50210b57cec5SDimitry Andric CmdArgs.push_back(PICLevel == 1 ? "1" : "2"); 50220b57cec5SDimitry Andric if (IsPIE) 50230b57cec5SDimitry Andric CmdArgs.push_back("-pic-is-pie"); 50240b57cec5SDimitry Andric } 50250b57cec5SDimitry Andric 50260b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::ROPI || 50270b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 50280b57cec5SDimitry Andric CmdArgs.push_back("-fropi"); 50290b57cec5SDimitry Andric if (RelocationModel == llvm::Reloc::RWPI || 50300b57cec5SDimitry Andric RelocationModel == llvm::Reloc::ROPI_RWPI) 50310b57cec5SDimitry Andric CmdArgs.push_back("-frwpi"); 50320b57cec5SDimitry Andric 50330b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_meabi)) { 50340b57cec5SDimitry Andric CmdArgs.push_back("-meabi"); 50350b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 50360b57cec5SDimitry Andric } 50370b57cec5SDimitry Andric 5038e8d8bef9SDimitry Andric // -fsemantic-interposition is forwarded to CC1: set the 5039e8d8bef9SDimitry Andric // "SemanticInterposition" metadata to 1 (make some linkages interposable) and 5040e8d8bef9SDimitry Andric // make default visibility external linkage definitions dso_preemptable. 5041e8d8bef9SDimitry Andric // 5042e8d8bef9SDimitry Andric // -fno-semantic-interposition: if the target supports .Lfoo$local local 5043e8d8bef9SDimitry Andric // aliases (make default visibility external linkage definitions dso_local). 5044e8d8bef9SDimitry Andric // This is the CC1 default for ELF to match COFF/Mach-O. 5045e8d8bef9SDimitry Andric // 5046e8d8bef9SDimitry Andric // Otherwise use Clang's traditional behavior: like 5047e8d8bef9SDimitry Andric // -fno-semantic-interposition but local aliases are not used. So references 5048e8d8bef9SDimitry Andric // can be interposed if not optimized out. 5049e8d8bef9SDimitry Andric if (Triple.isOSBinFormatELF()) { 5050e8d8bef9SDimitry Andric Arg *A = Args.getLastArg(options::OPT_fsemantic_interposition, 5051e8d8bef9SDimitry Andric options::OPT_fno_semantic_interposition); 5052e8d8bef9SDimitry Andric if (RelocationModel != llvm::Reloc::Static && !IsPIE) { 5053e8d8bef9SDimitry Andric // The supported targets need to call AsmPrinter::getSymbolPreferLocal. 5054fe6060f1SDimitry Andric bool SupportsLocalAlias = 5055fe6060f1SDimitry Andric Triple.isAArch64() || Triple.isRISCV() || Triple.isX86(); 5056e8d8bef9SDimitry Andric if (!A) 5057e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5058e8d8bef9SDimitry Andric else if (A->getOption().matches(options::OPT_fsemantic_interposition)) 50595ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5060e8d8bef9SDimitry Andric else if (!SupportsLocalAlias) 5061e8d8bef9SDimitry Andric CmdArgs.push_back("-fhalf-no-semantic-interposition"); 5062e8d8bef9SDimitry Andric } 5063e8d8bef9SDimitry Andric } 50645ffd83dbSDimitry Andric 50655ffd83dbSDimitry Andric { 50665ffd83dbSDimitry Andric std::string Model; 50670b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) { 50680b57cec5SDimitry Andric if (!TC.isThreadModelSupported(A->getValue())) 50690b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_thread_model_for_target) 50700b57cec5SDimitry Andric << A->getValue() << A->getAsString(Args); 50715ffd83dbSDimitry Andric Model = A->getValue(); 50725ffd83dbSDimitry Andric } else 50735ffd83dbSDimitry Andric Model = TC.getThreadModel(); 50745ffd83dbSDimitry Andric if (Model != "posix") { 50755ffd83dbSDimitry Andric CmdArgs.push_back("-mthread-model"); 50765ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Model)); 50770b57cec5SDimitry Andric } 50785ffd83dbSDimitry Andric } 50790b57cec5SDimitry Andric 50800b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fveclib); 50810b57cec5SDimitry Andric 50820b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fmerge_all_constants, 50830b57cec5SDimitry Andric options::OPT_fno_merge_all_constants, false)) 50840b57cec5SDimitry Andric CmdArgs.push_back("-fmerge-all-constants"); 50850b57cec5SDimitry Andric 50860b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_delete_null_pointer_checks, 50870b57cec5SDimitry Andric options::OPT_fdelete_null_pointer_checks, false)) 50880b57cec5SDimitry Andric CmdArgs.push_back("-fno-delete-null-pointer-checks"); 50890b57cec5SDimitry Andric 50900b57cec5SDimitry Andric // LLVM Code Generator Options. 50910b57cec5SDimitry Andric 5092fe6060f1SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file_EQ)) { 50930b57cec5SDimitry Andric StringRef Map = A->getValue(); 50940b57cec5SDimitry Andric if (!llvm::sys::fs::exists(Map)) { 50950b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << Map; 50960b57cec5SDimitry Andric } else { 5097fe6060f1SDimitry Andric A->render(Args, CmdArgs); 50980b57cec5SDimitry Andric A->claim(); 50990b57cec5SDimitry Andric } 51000b57cec5SDimitry Andric } 51010b57cec5SDimitry Andric 5102e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_vec_extabi, 5103e8d8bef9SDimitry Andric options::OPT_mabi_EQ_vec_default)) { 5104e8d8bef9SDimitry Andric if (!Triple.isOSAIX()) 5105e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5106e8d8bef9SDimitry Andric << A->getSpelling() << RawTriple.str(); 5107d409305fSDimitry Andric if (A->getOption().getID() == options::OPT_mabi_EQ_vec_extabi) 5108d409305fSDimitry Andric CmdArgs.push_back("-mabi=vec-extabi"); 5109d409305fSDimitry Andric else 5110fe6060f1SDimitry Andric CmdArgs.push_back("-mabi=vec-default"); 5111fe6060f1SDimitry Andric } 5112fe6060f1SDimitry Andric 5113fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) { 5114fe6060f1SDimitry Andric // Emit the unsupported option error until the Clang's library integration 5115fe6060f1SDimitry Andric // support for 128-bit long double is available for AIX. 5116fe6060f1SDimitry Andric if (Triple.isOSAIX()) 5117fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5118fe6060f1SDimitry Andric << A->getSpelling() << RawTriple.str(); 5119e8d8bef9SDimitry Andric } 5120e8d8bef9SDimitry Andric 51210b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) { 51220b57cec5SDimitry Andric StringRef v = A->getValue(); 5123fe6060f1SDimitry Andric // FIXME: Validate the argument here so we don't produce meaningless errors 5124fe6060f1SDimitry Andric // about -fwarn-stack-size=. 5125fe6060f1SDimitry Andric if (v.empty()) 5126fe6060f1SDimitry Andric D.Diag(diag::err_drv_missing_argument) << A->getSpelling() << 1; 5127fe6060f1SDimitry Andric else 5128fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fwarn-stack-size=" + v)); 51290b57cec5SDimitry Andric A->claim(); 51300b57cec5SDimitry Andric } 51310b57cec5SDimitry Andric 513281ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fjump_tables, 513381ad6265SDimitry Andric options::OPT_fno_jump_tables); 513481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fprofile_sample_accurate, 513581ad6265SDimitry Andric options::OPT_fno_profile_sample_accurate); 513681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fpreserve_as_comments, 513781ad6265SDimitry Andric options::OPT_fno_preserve_as_comments); 51380b57cec5SDimitry Andric 51390b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 51400b57cec5SDimitry Andric CmdArgs.push_back("-mregparm"); 51410b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 51420b57cec5SDimitry Andric } 51430b57cec5SDimitry Andric 5144e86cf8adSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_maix_struct_return, 5145e86cf8adSDimitry Andric options::OPT_msvr4_struct_return)) { 5146e8d8bef9SDimitry Andric if (!TC.getTriple().isPPC32()) { 5147e86cf8adSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5148e86cf8adSDimitry Andric << A->getSpelling() << RawTriple.str(); 5149e86cf8adSDimitry Andric } else if (A->getOption().matches(options::OPT_maix_struct_return)) { 5150e86cf8adSDimitry Andric CmdArgs.push_back("-maix-struct-return"); 5151e86cf8adSDimitry Andric } else { 5152e86cf8adSDimitry Andric assert(A->getOption().matches(options::OPT_msvr4_struct_return)); 5153e86cf8adSDimitry Andric CmdArgs.push_back("-msvr4-struct-return"); 5154e86cf8adSDimitry Andric } 5155e86cf8adSDimitry Andric } 5156e86cf8adSDimitry Andric 51570b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 51580b57cec5SDimitry Andric options::OPT_freg_struct_return)) { 51590b57cec5SDimitry Andric if (TC.getArch() != llvm::Triple::x86) { 51600b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 51610b57cec5SDimitry Andric << A->getSpelling() << RawTriple.str(); 51620b57cec5SDimitry Andric } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 51630b57cec5SDimitry Andric CmdArgs.push_back("-fpcc-struct-return"); 51640b57cec5SDimitry Andric } else { 51650b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_freg_struct_return)); 51660b57cec5SDimitry Andric CmdArgs.push_back("-freg-struct-return"); 51670b57cec5SDimitry Andric } 51680b57cec5SDimitry Andric } 51690b57cec5SDimitry Andric 51700b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) 51710b57cec5SDimitry Andric CmdArgs.push_back("-fdefault-calling-conv=stdcall"); 51720b57cec5SDimitry Andric 51735ffd83dbSDimitry Andric if (Args.hasArg(options::OPT_fenable_matrix)) { 51745ffd83dbSDimitry Andric // enable-matrix is needed by both the LangOpts and by LLVM. 51755ffd83dbSDimitry Andric CmdArgs.push_back("-fenable-matrix"); 51765ffd83dbSDimitry Andric CmdArgs.push_back("-mllvm"); 51775ffd83dbSDimitry Andric CmdArgs.push_back("-enable-matrix"); 51785ffd83dbSDimitry Andric } 51795ffd83dbSDimitry Andric 5180a7dea167SDimitry Andric CodeGenOptions::FramePointerKind FPKeepKind = 5181a7dea167SDimitry Andric getFramePointerKind(Args, RawTriple); 5182a7dea167SDimitry Andric const char *FPKeepKindStr = nullptr; 5183a7dea167SDimitry Andric switch (FPKeepKind) { 5184a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::None: 5185a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=none"; 5186a7dea167SDimitry Andric break; 5187a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::NonLeaf: 5188a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=non-leaf"; 5189a7dea167SDimitry Andric break; 5190a7dea167SDimitry Andric case CodeGenOptions::FramePointerKind::All: 5191a7dea167SDimitry Andric FPKeepKindStr = "-mframe-pointer=all"; 5192a7dea167SDimitry Andric break; 5193a7dea167SDimitry Andric } 5194a7dea167SDimitry Andric assert(FPKeepKindStr && "unknown FramePointerKind"); 5195a7dea167SDimitry Andric CmdArgs.push_back(FPKeepKindStr); 5196a7dea167SDimitry Andric 519781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fzero_initialized_in_bss, 519881ad6265SDimitry Andric options::OPT_fno_zero_initialized_in_bss); 51990b57cec5SDimitry Andric 52000b57cec5SDimitry Andric bool OFastEnabled = isOptimizationLevelFast(Args); 52010b57cec5SDimitry Andric // If -Ofast is the optimization level, then -fstrict-aliasing should be 52020b57cec5SDimitry Andric // enabled. This alias option is being used to simplify the hasFlag logic. 52030b57cec5SDimitry Andric OptSpecifier StrictAliasingAliasOption = 52040b57cec5SDimitry Andric OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing; 52050b57cec5SDimitry Andric // We turn strict aliasing off by default if we're in CL mode, since MSVC 52060b57cec5SDimitry Andric // doesn't do any TBAA. 52070b57cec5SDimitry Andric bool TBAAOnByDefault = !D.IsCLMode(); 52080b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 52090b57cec5SDimitry Andric options::OPT_fno_strict_aliasing, TBAAOnByDefault)) 52100b57cec5SDimitry Andric CmdArgs.push_back("-relaxed-aliasing"); 52110b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, 521281ad6265SDimitry Andric options::OPT_fno_struct_path_tbaa, true)) 52130b57cec5SDimitry Andric CmdArgs.push_back("-no-struct-path-tbaa"); 521481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_enums, 521581ad6265SDimitry Andric options::OPT_fno_strict_enums); 521681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fstrict_return, 521781ad6265SDimitry Andric options::OPT_fno_strict_return); 521881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fallow_editor_placeholders, 521981ad6265SDimitry Andric options::OPT_fno_allow_editor_placeholders); 522081ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fstrict_vtable_pointers, 522181ad6265SDimitry Andric options::OPT_fno_strict_vtable_pointers); 522281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fforce_emit_vtables, 522381ad6265SDimitry Andric options::OPT_fno_force_emit_vtables); 522481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_foptimize_sibling_calls, 522581ad6265SDimitry Andric options::OPT_fno_optimize_sibling_calls); 522681ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fescaping_block_tail_calls, 522781ad6265SDimitry Andric options::OPT_fno_escaping_block_tail_calls); 52280b57cec5SDimitry Andric 52290b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffine_grained_bitfield_accesses, 52300b57cec5SDimitry Andric options::OPT_fno_fine_grained_bitfield_accesses); 52310b57cec5SDimitry Andric 5232fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5233fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5234fe6060f1SDimitry Andric 52350b57cec5SDimitry Andric // Handle segmented stacks. 5236fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fsplit_stack, options::OPT_fno_split_stack, 5237fe6060f1SDimitry Andric false)) 5238fe6060f1SDimitry Andric CmdArgs.push_back("-fsplit-stack"); 5239fe6060f1SDimitry Andric 5240fe6060f1SDimitry Andric // -fprotect-parens=0 is default. 5241fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fprotect_parens, 5242fe6060f1SDimitry Andric options::OPT_fno_protect_parens, false)) 5243fe6060f1SDimitry Andric CmdArgs.push_back("-fprotect-parens"); 52440b57cec5SDimitry Andric 52455ffd83dbSDimitry Andric RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs, JA); 52465ffd83dbSDimitry Andric 5247fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fextend_args_EQ)) { 5248fe6060f1SDimitry Andric const llvm::Triple::ArchType Arch = TC.getArch(); 5249fe6060f1SDimitry Andric if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5250fe6060f1SDimitry Andric StringRef V = A->getValue(); 5251fe6060f1SDimitry Andric if (V == "64") 5252fe6060f1SDimitry Andric CmdArgs.push_back("-fextend-arguments=64"); 5253fe6060f1SDimitry Andric else if (V != "32") 5254fe6060f1SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5255fe6060f1SDimitry Andric << A->getValue() << A->getOption().getName(); 5256fe6060f1SDimitry Andric } else 5257fe6060f1SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5258fe6060f1SDimitry Andric << A->getOption().getName() << TripleStr; 5259fe6060f1SDimitry Andric } 5260fe6060f1SDimitry Andric 52615ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mdouble_EQ)) { 52625ffd83dbSDimitry Andric if (TC.getArch() == llvm::Triple::avr) 52635ffd83dbSDimitry Andric A->render(Args, CmdArgs); 52645ffd83dbSDimitry Andric else 52655ffd83dbSDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 52665ffd83dbSDimitry Andric << A->getAsString(Args) << TripleStr; 52675ffd83dbSDimitry Andric } 52680b57cec5SDimitry Andric 5269a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) { 5270480093f4SDimitry Andric if (TC.getTriple().isX86()) 5271a7dea167SDimitry Andric A->render(Args, CmdArgs); 5272e8d8bef9SDimitry Andric else if (TC.getTriple().isPPC() && 5273a7dea167SDimitry Andric (A->getOption().getID() != options::OPT_mlong_double_80)) 52740b57cec5SDimitry Andric A->render(Args, CmdArgs); 52750b57cec5SDimitry Andric else 52760b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 52770b57cec5SDimitry Andric << A->getAsString(Args) << TripleStr; 52780b57cec5SDimitry Andric } 52790b57cec5SDimitry Andric 52800b57cec5SDimitry Andric // Decide whether to use verbose asm. Verbose assembly is the default on 52810b57cec5SDimitry Andric // toolchains which have the integrated assembler on by default. 52820b57cec5SDimitry Andric bool IsIntegratedAssemblerDefault = TC.IsIntegratedAssemblerDefault(); 52835ffd83dbSDimitry Andric if (!Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 5284a7dea167SDimitry Andric IsIntegratedAssemblerDefault)) 52855ffd83dbSDimitry Andric CmdArgs.push_back("-fno-verbose-asm"); 52860b57cec5SDimitry Andric 5287e8d8bef9SDimitry Andric // Parse 'none' or '$major.$minor'. Disallow -fbinutils-version=0 because we 5288e8d8bef9SDimitry Andric // use that to indicate the MC default in the backend. 5289e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbinutils_version_EQ)) { 5290e8d8bef9SDimitry Andric StringRef V = A->getValue(); 5291e8d8bef9SDimitry Andric unsigned Num; 5292e8d8bef9SDimitry Andric if (V == "none") 5293e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5294e8d8bef9SDimitry Andric else if (!V.consumeInteger(10, Num) && Num > 0 && 5295e8d8bef9SDimitry Andric (V.empty() || (V.consume_front(".") && 5296e8d8bef9SDimitry Andric !V.consumeInteger(10, Num) && V.empty()))) 5297e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5298e8d8bef9SDimitry Andric else 5299e8d8bef9SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 5300e8d8bef9SDimitry Andric << A->getValue() << A->getOption().getName(); 5301e8d8bef9SDimitry Andric } 5302e8d8bef9SDimitry Andric 5303fe6060f1SDimitry Andric // If toolchain choose to use MCAsmParser for inline asm don't pass the 5304fe6060f1SDimitry Andric // option to disable integrated-as explictly. 5305fe6060f1SDimitry Andric if (!TC.useIntegratedAs() && !TC.parseInlineAsmUsingAsmParser()) 53060b57cec5SDimitry Andric CmdArgs.push_back("-no-integrated-as"); 53070b57cec5SDimitry Andric 53080b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 53090b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 53100b57cec5SDimitry Andric CmdArgs.push_back("Structure"); 53110b57cec5SDimitry Andric } 53120b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 53130b57cec5SDimitry Andric CmdArgs.push_back("-mdebug-pass"); 53140b57cec5SDimitry Andric CmdArgs.push_back("Arguments"); 53150b57cec5SDimitry Andric } 53160b57cec5SDimitry Andric 53170b57cec5SDimitry Andric // Enable -mconstructor-aliases except on darwin, where we have to work around 5318349cc55cSDimitry Andric // a linker bug (see <rdar://problem/7651567>), and CUDA device code, where 5319349cc55cSDimitry Andric // aliases aren't supported. 5320349cc55cSDimitry Andric if (!RawTriple.isOSDarwin() && !RawTriple.isNVPTX()) 53210b57cec5SDimitry Andric CmdArgs.push_back("-mconstructor-aliases"); 53220b57cec5SDimitry Andric 53230b57cec5SDimitry Andric // Darwin's kernel doesn't support guard variables; just die if we 53240b57cec5SDimitry Andric // try to use them. 53250b57cec5SDimitry Andric if (KernelOrKext && RawTriple.isOSDarwin()) 53260b57cec5SDimitry Andric CmdArgs.push_back("-fforbid-guard-variables"); 53270b57cec5SDimitry Andric 53280b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mms_bitfields, options::OPT_mno_ms_bitfields, 53295ffd83dbSDimitry Andric Triple.isWindowsGNUEnvironment())) { 53300b57cec5SDimitry Andric CmdArgs.push_back("-mms-bitfields"); 53310b57cec5SDimitry Andric } 53320b57cec5SDimitry Andric 5333e8d8bef9SDimitry Andric // Non-PIC code defaults to -fdirect-access-external-data while PIC code 5334e8d8bef9SDimitry Andric // defaults to -fno-direct-access-external-data. Pass the option if different 5335e8d8bef9SDimitry Andric // from the default. 5336e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data, 5337e8d8bef9SDimitry Andric options::OPT_fno_direct_access_external_data)) 5338e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fdirect_access_external_data) != 5339e8d8bef9SDimitry Andric (PICLevel == 0)) 5340e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 53410b57cec5SDimitry Andric 53420b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { 53430b57cec5SDimitry Andric CmdArgs.push_back("-fno-plt"); 53440b57cec5SDimitry Andric } 53450b57cec5SDimitry Andric 53460b57cec5SDimitry Andric // -fhosted is default. 53470b57cec5SDimitry Andric // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to 53480b57cec5SDimitry Andric // use Freestanding. 53490b57cec5SDimitry Andric bool Freestanding = 53500b57cec5SDimitry Andric Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 53510b57cec5SDimitry Andric KernelOrKext; 53520b57cec5SDimitry Andric if (Freestanding) 53530b57cec5SDimitry Andric CmdArgs.push_back("-ffreestanding"); 53540b57cec5SDimitry Andric 535581ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_knr_functions); 535681ad6265SDimitry Andric 53570b57cec5SDimitry Andric // This is a coarse approximation of what llvm-gcc actually does, both 53580b57cec5SDimitry Andric // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 53590b57cec5SDimitry Andric // complicated ways. 5360349cc55cSDimitry Andric auto SanitizeArgs = TC.getSanitizerArgs(Args); 5361349cc55cSDimitry Andric bool AsyncUnwindTables = Args.hasFlag( 5362349cc55cSDimitry Andric options::OPT_fasynchronous_unwind_tables, 53630b57cec5SDimitry Andric options::OPT_fno_asynchronous_unwind_tables, 5364349cc55cSDimitry Andric (TC.IsUnwindTablesDefault(Args) || SanitizeArgs.needsUnwindTables()) && 53650b57cec5SDimitry Andric !Freestanding); 5366349cc55cSDimitry Andric bool UnwindTables = Args.hasFlag(options::OPT_funwind_tables, 5367349cc55cSDimitry Andric options::OPT_fno_unwind_tables, false); 5368349cc55cSDimitry Andric if (AsyncUnwindTables) 5369349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=2"); 5370349cc55cSDimitry Andric else if (UnwindTables) 5371349cc55cSDimitry Andric CmdArgs.push_back("-funwind-tables=1"); 53720b57cec5SDimitry Andric 53735ffd83dbSDimitry Andric // Prepare `-aux-target-cpu` and `-aux-target-feature` unless 53745ffd83dbSDimitry Andric // `--gpu-use-aux-triple-only` is specified. 53755ffd83dbSDimitry Andric if (!Args.getLastArg(options::OPT_gpu_use_aux_triple_only) && 5376fe6060f1SDimitry Andric (IsCudaDevice || IsHIPDevice)) { 53775ffd83dbSDimitry Andric const ArgList &HostArgs = 53785ffd83dbSDimitry Andric C.getArgsForToolChain(nullptr, StringRef(), Action::OFK_None); 53795ffd83dbSDimitry Andric std::string HostCPU = 5380349cc55cSDimitry Andric getCPUName(D, HostArgs, *TC.getAuxTriple(), /*FromAs*/ false); 53815ffd83dbSDimitry Andric if (!HostCPU.empty()) { 53825ffd83dbSDimitry Andric CmdArgs.push_back("-aux-target-cpu"); 53835ffd83dbSDimitry Andric CmdArgs.push_back(Args.MakeArgString(HostCPU)); 53845ffd83dbSDimitry Andric } 53855ffd83dbSDimitry Andric getTargetFeatures(D, *TC.getAuxTriple(), HostArgs, CmdArgs, 53865ffd83dbSDimitry Andric /*ForAS*/ false, /*IsAux*/ true); 53875ffd83dbSDimitry Andric } 53885ffd83dbSDimitry Andric 53890b57cec5SDimitry Andric TC.addClangTargetOptions(Args, CmdArgs, JA.getOffloadingDeviceKind()); 53900b57cec5SDimitry Andric 53910b57cec5SDimitry Andric // FIXME: Handle -mtune=. 53920b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_mtune_EQ); 53930b57cec5SDimitry Andric 53940b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 53955ffd83dbSDimitry Andric StringRef CM = A->getValue(); 53965ffd83dbSDimitry Andric if (CM == "small" || CM == "kernel" || CM == "medium" || CM == "large" || 5397fe6060f1SDimitry Andric CM == "tiny") { 5398fe6060f1SDimitry Andric if (Triple.isOSAIX() && CM == "medium") 5399fe6060f1SDimitry Andric CmdArgs.push_back("-mcmodel=large"); 54005ffd83dbSDimitry Andric else 5401fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5402fe6060f1SDimitry Andric } else { 54035ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 54045ffd83dbSDimitry Andric << CM << A->getOption().getName(); 54050b57cec5SDimitry Andric } 5406fe6060f1SDimitry Andric } 54070b57cec5SDimitry Andric 5408480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mtls_size_EQ)) { 5409480093f4SDimitry Andric StringRef Value = A->getValue(); 5410480093f4SDimitry Andric unsigned TLSSize = 0; 5411480093f4SDimitry Andric Value.getAsInteger(10, TLSSize); 5412480093f4SDimitry Andric if (!Triple.isAArch64() || !Triple.isOSBinFormatELF()) 5413480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5414480093f4SDimitry Andric << A->getOption().getName() << TripleStr; 5415480093f4SDimitry Andric if (TLSSize != 12 && TLSSize != 24 && TLSSize != 32 && TLSSize != 48) 5416480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5417480093f4SDimitry Andric << A->getOption().getName() << Value; 5418480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ); 5419480093f4SDimitry Andric } 5420480093f4SDimitry Andric 54210b57cec5SDimitry Andric // Add the target cpu 5422349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false); 54230b57cec5SDimitry Andric if (!CPU.empty()) { 54240b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 54250b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 54260b57cec5SDimitry Andric } 54270b57cec5SDimitry Andric 54280b57cec5SDimitry Andric RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs); 54290b57cec5SDimitry Andric 5430fe6060f1SDimitry Andric // FIXME: For now we want to demote any errors to warnings, when they have 5431fe6060f1SDimitry Andric // been raised for asking the wrong question of scalable vectors, such as 5432fe6060f1SDimitry Andric // asking for the fixed number of elements. This may happen because code that 5433fe6060f1SDimitry Andric // is not yet ported to work for scalable vectors uses the wrong interfaces, 5434fe6060f1SDimitry Andric // whereas the behaviour is actually correct. Emitting a warning helps bring 5435fe6060f1SDimitry Andric // up scalable vector support in an incremental way. When scalable vector 5436fe6060f1SDimitry Andric // support is stable enough, all uses of wrong interfaces should be considered 5437fe6060f1SDimitry Andric // as errors, but until then, we can live with a warning being emitted by the 5438fe6060f1SDimitry Andric // compiler. This way, Clang can be used to compile code with scalable vectors 5439fe6060f1SDimitry Andric // and identify possible issues. 544004eeddc0SDimitry Andric if (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 544104eeddc0SDimitry Andric isa<BackendJobAction>(JA)) { 5442fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5443fe6060f1SDimitry Andric CmdArgs.push_back("-treat-scalable-fixed-error-as-warning"); 5444fe6060f1SDimitry Andric } 5445fe6060f1SDimitry Andric 54460b57cec5SDimitry Andric // These two are potentially updated by AddClangCLArgs. 54470b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; 54480b57cec5SDimitry Andric bool EmitCodeView = false; 54490b57cec5SDimitry Andric 54500b57cec5SDimitry Andric // Add clang-cl arguments. 54510b57cec5SDimitry Andric types::ID InputType = Input.getType(); 54520b57cec5SDimitry Andric if (D.IsCLMode()) 54530b57cec5SDimitry Andric AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView); 54540b57cec5SDimitry Andric 5455e8d8bef9SDimitry Andric DwarfFissionKind DwarfFission = DwarfFissionKind::None; 5456e8d8bef9SDimitry Andric renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView, 5457e8d8bef9SDimitry Andric types::isLLVMIR(InputType), CmdArgs, DebugInfoKind, 5458e8d8bef9SDimitry Andric DwarfFission); 54590b57cec5SDimitry Andric 546081ad6265SDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 546181ad6265SDimitry Andric if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) { 546281ad6265SDimitry Andric if (TC.getTriple().isOSBinFormatELF()) { 546381ad6265SDimitry Andric if (DebugInfoKind >= codegenoptions::DebugInfoConstructor) 546481ad6265SDimitry Andric CmdArgs.push_back("-fjmc"); 546581ad6265SDimitry Andric else 546681ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc" 546781ad6265SDimitry Andric << "-g"; 546881ad6265SDimitry Andric } else { 546981ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_fjmc_for_elf_only); 547081ad6265SDimitry Andric } 547181ad6265SDimitry Andric } 547281ad6265SDimitry Andric 54730b57cec5SDimitry Andric // Add the split debug info name to the command lines here so we 54740b57cec5SDimitry Andric // can propagate it to the backend. 54750b57cec5SDimitry Andric bool SplitDWARF = (DwarfFission != DwarfFissionKind::None) && 5476e8d8bef9SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 5477e8d8bef9SDimitry Andric TC.getTriple().isOSBinFormatWasm()) && 54780b57cec5SDimitry Andric (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) || 54790b57cec5SDimitry Andric isa<BackendJobAction>(JA)); 54800b57cec5SDimitry Andric if (SplitDWARF) { 5481e8d8bef9SDimitry Andric const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output); 54820b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-file"); 54830b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 54840b57cec5SDimitry Andric if (DwarfFission == DwarfFissionKind::Split) { 54850b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 54860b57cec5SDimitry Andric CmdArgs.push_back(SplitDWARFOut); 54870b57cec5SDimitry Andric } 54880b57cec5SDimitry Andric } 54890b57cec5SDimitry Andric 54900b57cec5SDimitry Andric // Pass the linker version in use. 54910b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 54920b57cec5SDimitry Andric CmdArgs.push_back("-target-linker-version"); 54930b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 54940b57cec5SDimitry Andric } 54950b57cec5SDimitry Andric 54960b57cec5SDimitry Andric // Explicitly error on some things we know we don't support and can't just 54970b57cec5SDimitry Andric // ignore. 54980b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fallow_unsupported)) { 54990b57cec5SDimitry Andric Arg *Unsupported; 55000b57cec5SDimitry Andric if (types::isCXX(InputType) && RawTriple.isOSDarwin() && 55010b57cec5SDimitry Andric TC.getArch() == llvm::Triple::x86) { 55020b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 55030b57cec5SDimitry Andric (Unsupported = Args.getLastArg(options::OPT_mkernel))) 55040b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 55050b57cec5SDimitry Andric << Unsupported->getOption().getName(); 55060b57cec5SDimitry Andric } 55070b57cec5SDimitry Andric // The faltivec option has been superseded by the maltivec option. 55080b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_faltivec))) 55090b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 55100b57cec5SDimitry Andric << Unsupported->getOption().getName() 55110b57cec5SDimitry Andric << "please use -maltivec and include altivec.h explicitly"; 55120b57cec5SDimitry Andric if ((Unsupported = Args.getLastArg(options::OPT_fno_altivec))) 55130b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported_opt_faltivec) 55140b57cec5SDimitry Andric << Unsupported->getOption().getName() << "please use -mno-altivec"; 55150b57cec5SDimitry Andric } 55160b57cec5SDimitry Andric 55170b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_v); 55185ffd83dbSDimitry Andric 55195ffd83dbSDimitry Andric if (Args.getLastArg(options::OPT_H)) { 55205ffd83dbSDimitry Andric CmdArgs.push_back("-H"); 55215ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 55225ffd83dbSDimitry Andric } 5523fe6060f1SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fshow_skipped_includes); 55245ffd83dbSDimitry Andric 55250b57cec5SDimitry Andric if (D.CCPrintHeaders && !D.CCGenDiagnostics) { 55260b57cec5SDimitry Andric CmdArgs.push_back("-header-include-file"); 5527fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCPrintHeadersFilename.empty() 5528fe6060f1SDimitry Andric ? D.CCPrintHeadersFilename.c_str() 55290b57cec5SDimitry Andric : "-"); 55305ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 55310b57cec5SDimitry Andric } 55320b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_P); 55330b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 55340b57cec5SDimitry Andric 55350b57cec5SDimitry Andric if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 55360b57cec5SDimitry Andric CmdArgs.push_back("-diagnostic-log-file"); 5537fe6060f1SDimitry Andric CmdArgs.push_back(!D.CCLogDiagnosticsFilename.empty() 5538fe6060f1SDimitry Andric ? D.CCLogDiagnosticsFilename.c_str() 55390b57cec5SDimitry Andric : "-"); 55400b57cec5SDimitry Andric } 55410b57cec5SDimitry Andric 554213138422SDimitry Andric // Give the gen diagnostics more chances to succeed, by avoiding intentional 554313138422SDimitry Andric // crashes. 554413138422SDimitry Andric if (D.CCGenDiagnostics) 554513138422SDimitry Andric CmdArgs.push_back("-disable-pragma-debug-crash"); 554613138422SDimitry Andric 5547fe6060f1SDimitry Andric // Allow backend to put its diagnostic files in the same place as frontend 5548fe6060f1SDimitry Andric // crash diagnostics files. 5549fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fcrash_diagnostics_dir)) { 5550fe6060f1SDimitry Andric StringRef Dir = Args.getLastArgValue(options::OPT_fcrash_diagnostics_dir); 5551fe6060f1SDimitry Andric CmdArgs.push_back("-mllvm"); 5552fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-crash-diagnostics-dir=" + Dir)); 5553fe6060f1SDimitry Andric } 5554fe6060f1SDimitry Andric 55550b57cec5SDimitry Andric bool UseSeparateSections = isUseSeparateSections(Triple); 55560b57cec5SDimitry Andric 55570b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffunction_sections, 55580b57cec5SDimitry Andric options::OPT_fno_function_sections, UseSeparateSections)) { 55590b57cec5SDimitry Andric CmdArgs.push_back("-ffunction-sections"); 55600b57cec5SDimitry Andric } 55610b57cec5SDimitry Andric 55625ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbasic_block_sections_EQ)) { 55635ffd83dbSDimitry Andric StringRef Val = A->getValue(); 5564fe6060f1SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 55655ffd83dbSDimitry Andric if (Val != "all" && Val != "labels" && Val != "none" && 5566e8d8bef9SDimitry Andric !Val.startswith("list=")) 55675ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_value) 55685ffd83dbSDimitry Andric << A->getAsString(Args) << A->getValue(); 55695ffd83dbSDimitry Andric else 55705ffd83dbSDimitry Andric A->render(Args, CmdArgs); 5571fe6060f1SDimitry Andric } else if (Triple.isNVPTX()) { 5572fe6060f1SDimitry Andric // Do not pass the option to the GPU compilation. We still want it enabled 5573fe6060f1SDimitry Andric // for the host-side compilation, so seeing it here is not an error. 5574fe6060f1SDimitry Andric } else if (Val != "none") { 5575fe6060f1SDimitry Andric // =none is allowed everywhere. It's useful for overriding the option 5576fe6060f1SDimitry Andric // and is the same as not specifying the option. 5577e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5578e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5579e8d8bef9SDimitry Andric } 55805ffd83dbSDimitry Andric } 55815ffd83dbSDimitry Andric 5582e8d8bef9SDimitry Andric bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF(); 55830b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, 5584e8d8bef9SDimitry Andric UseSeparateSections || HasDefaultDataSections)) { 55850b57cec5SDimitry Andric CmdArgs.push_back("-fdata-sections"); 55860b57cec5SDimitry Andric } 55870b57cec5SDimitry Andric 558881ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_funique_section_names, 558981ad6265SDimitry Andric options::OPT_fno_unique_section_names); 559081ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_internal_linkage_names, 559181ad6265SDimitry Andric options::OPT_fno_unique_internal_linkage_names); 559281ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_funique_basic_block_section_names, 559381ad6265SDimitry Andric options::OPT_fno_unique_basic_block_section_names); 55945ffd83dbSDimitry Andric 5595e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, 5596e8d8bef9SDimitry Andric options::OPT_fno_split_machine_functions)) { 5597e8d8bef9SDimitry Andric // This codegen pass is only available on x86-elf targets. 5598e8d8bef9SDimitry Andric if (Triple.isX86() && Triple.isOSBinFormatELF()) { 5599e8d8bef9SDimitry Andric if (A->getOption().matches(options::OPT_fsplit_machine_functions)) 5600e8d8bef9SDimitry Andric A->render(Args, CmdArgs); 5601e8d8bef9SDimitry Andric } else { 5602e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5603e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5604e8d8bef9SDimitry Andric } 5605e8d8bef9SDimitry Andric } 5606e8d8bef9SDimitry Andric 56070b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, 56080b57cec5SDimitry Andric options::OPT_finstrument_functions_after_inlining, 56090b57cec5SDimitry Andric options::OPT_finstrument_function_entry_bare); 56100b57cec5SDimitry Andric 56115ffd83dbSDimitry Andric // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support 56125ffd83dbSDimitry Andric // for sampling, overhead of call arc collection is way too high and there's 56135ffd83dbSDimitry Andric // no way to collect the output. 56145ffd83dbSDimitry Andric if (!Triple.isNVPTX() && !Triple.isAMDGCN()) 5615349cc55cSDimitry Andric addPGOAndCoverageFlags(TC, C, D, Output, Args, SanitizeArgs, CmdArgs); 56160b57cec5SDimitry Andric 56170b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); 56180b57cec5SDimitry Andric 561981ad6265SDimitry Andric // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled. 562081ad6265SDimitry Andric if (RawTriple.isPS() && 56210b57cec5SDimitry Andric !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 562281ad6265SDimitry Andric PScpu::addProfileRTArgs(TC, Args, CmdArgs); 562381ad6265SDimitry Andric PScpu::addSanitizerArgs(TC, Args, CmdArgs); 56240b57cec5SDimitry Andric } 56250b57cec5SDimitry Andric 56260b57cec5SDimitry Andric // Pass options for controlling the default header search paths. 56270b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdinc)) { 56280b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 56290b57cec5SDimitry Andric CmdArgs.push_back("-nobuiltininc"); 56300b57cec5SDimitry Andric } else { 56310b57cec5SDimitry Andric if (Args.hasArg(options::OPT_nostdlibinc)) 56320b57cec5SDimitry Andric CmdArgs.push_back("-nostdsysteminc"); 56330b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 56340b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 56350b57cec5SDimitry Andric } 56360b57cec5SDimitry Andric 56370b57cec5SDimitry Andric // Pass the path to compiler resource files. 56380b57cec5SDimitry Andric CmdArgs.push_back("-resource-dir"); 56390b57cec5SDimitry Andric CmdArgs.push_back(D.ResourceDir.c_str()); 56400b57cec5SDimitry Andric 56410b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_working_directory); 56420b57cec5SDimitry Andric 56430b57cec5SDimitry Andric RenderARCMigrateToolOptions(D, Args, CmdArgs); 56440b57cec5SDimitry Andric 56450b57cec5SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 56460b57cec5SDimitry Andric // preprocessor. 56470b57cec5SDimitry Andric // 56480b57cec5SDimitry Andric // FIXME: Support -fpreprocessed 56490b57cec5SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 56500b57cec5SDimitry Andric AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 56510b57cec5SDimitry Andric 56520b57cec5SDimitry Andric // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 56530b57cec5SDimitry Andric // that "The compiler can only warn and ignore the option if not recognized". 56540b57cec5SDimitry Andric // When building with ccache, it will pass -D options to clang even on 56550b57cec5SDimitry Andric // preprocessed inputs and configure concludes that -fPIC is not supported. 56560b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_D); 56570b57cec5SDimitry Andric 56580b57cec5SDimitry Andric // Manually translate -O4 to -O3; let clang reject others. 56590b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 56600b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4)) { 56610b57cec5SDimitry Andric CmdArgs.push_back("-O3"); 56620b57cec5SDimitry Andric D.Diag(diag::warn_O4_is_O3); 56630b57cec5SDimitry Andric } else { 56640b57cec5SDimitry Andric A->render(Args, CmdArgs); 56650b57cec5SDimitry Andric } 56660b57cec5SDimitry Andric } 56670b57cec5SDimitry Andric 56680b57cec5SDimitry Andric // Warn about ignored options to clang. 56690b57cec5SDimitry Andric for (const Arg *A : 56700b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) { 56710b57cec5SDimitry Andric D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args); 56720b57cec5SDimitry Andric A->claim(); 56730b57cec5SDimitry Andric } 56740b57cec5SDimitry Andric 56750b57cec5SDimitry Andric for (const Arg *A : 56760b57cec5SDimitry Andric Args.filtered(options::OPT_clang_ignored_legacy_options_Group)) { 56770b57cec5SDimitry Andric D.Diag(diag::warn_ignored_clang_option) << A->getAsString(Args); 56780b57cec5SDimitry Andric A->claim(); 56790b57cec5SDimitry Andric } 56800b57cec5SDimitry Andric 56810b57cec5SDimitry Andric claimNoWarnArgs(Args); 56820b57cec5SDimitry Andric 56830b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_R_Group); 56840b57cec5SDimitry Andric 5685349cc55cSDimitry Andric for (const Arg *A : 5686349cc55cSDimitry Andric Args.filtered(options::OPT_W_Group, options::OPT__SLASH_wd)) { 5687349cc55cSDimitry Andric A->claim(); 5688349cc55cSDimitry Andric if (A->getOption().getID() == options::OPT__SLASH_wd) { 5689349cc55cSDimitry Andric unsigned WarningNumber; 5690349cc55cSDimitry Andric if (StringRef(A->getValue()).getAsInteger(10, WarningNumber)) { 5691349cc55cSDimitry Andric D.Diag(diag::err_drv_invalid_int_value) 5692349cc55cSDimitry Andric << A->getAsString(Args) << A->getValue(); 5693349cc55cSDimitry Andric continue; 5694349cc55cSDimitry Andric } 5695349cc55cSDimitry Andric 5696349cc55cSDimitry Andric if (auto Group = diagGroupFromCLWarningID(WarningNumber)) { 5697349cc55cSDimitry Andric CmdArgs.push_back(Args.MakeArgString( 5698349cc55cSDimitry Andric "-Wno-" + DiagnosticIDs::getWarningOptionForGroup(*Group))); 5699349cc55cSDimitry Andric } 5700349cc55cSDimitry Andric continue; 5701349cc55cSDimitry Andric } 5702349cc55cSDimitry Andric A->render(Args, CmdArgs); 5703349cc55cSDimitry Andric } 5704349cc55cSDimitry Andric 57050b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 57060b57cec5SDimitry Andric CmdArgs.push_back("-pedantic"); 57070b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 57080b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_w); 57090b57cec5SDimitry Andric 57100b57cec5SDimitry Andric // Fixed point flags 57110b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_ffixed_point, options::OPT_fno_fixed_point, 57120b57cec5SDimitry Andric /*Default=*/false)) 57130b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffixed_point); 57140b57cec5SDimitry Andric 5715fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcxx_abi_EQ)) 5716fe6060f1SDimitry Andric A->render(Args, CmdArgs); 5717fe6060f1SDimitry Andric 5718fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fexperimental_relative_cxx_abi_vtables, 5719fe6060f1SDimitry Andric options::OPT_fno_experimental_relative_cxx_abi_vtables); 5720fe6060f1SDimitry Andric 5721349cc55cSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ffuchsia_api_level_EQ)) 5722349cc55cSDimitry Andric A->render(Args, CmdArgs); 5723349cc55cSDimitry Andric 57240b57cec5SDimitry Andric // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 57250b57cec5SDimitry Andric // (-ansi is equivalent to -std=c89 or -std=c++98). 57260b57cec5SDimitry Andric // 57270b57cec5SDimitry Andric // If a std is supplied, only add -trigraphs if it follows the 57280b57cec5SDimitry Andric // option. 5729fe6060f1SDimitry Andric bool ImplyVCPPCVer = false; 57300b57cec5SDimitry Andric bool ImplyVCPPCXXVer = false; 57310b57cec5SDimitry Andric const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); 57320b57cec5SDimitry Andric if (Std) { 57330b57cec5SDimitry Andric if (Std->getOption().matches(options::OPT_ansi)) 57340b57cec5SDimitry Andric if (types::isCXX(InputType)) 57350b57cec5SDimitry Andric CmdArgs.push_back("-std=c++98"); 57360b57cec5SDimitry Andric else 57370b57cec5SDimitry Andric CmdArgs.push_back("-std=c89"); 57380b57cec5SDimitry Andric else 57390b57cec5SDimitry Andric Std->render(Args, CmdArgs); 57400b57cec5SDimitry Andric 57410b57cec5SDimitry Andric // If -f(no-)trigraphs appears after the language standard flag, honor it. 57420b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 57430b57cec5SDimitry Andric options::OPT_ftrigraphs, 57440b57cec5SDimitry Andric options::OPT_fno_trigraphs)) 57450b57cec5SDimitry Andric if (A != Std) 57460b57cec5SDimitry Andric A->render(Args, CmdArgs); 57470b57cec5SDimitry Andric } else { 57480b57cec5SDimitry Andric // Honor -std-default. 57490b57cec5SDimitry Andric // 57500b57cec5SDimitry Andric // FIXME: Clang doesn't correctly handle -std= when the input language 57510b57cec5SDimitry Andric // doesn't match. For the time being just ignore this for C++ inputs; 57520b57cec5SDimitry Andric // eventually we want to do all the standard defaulting here instead of 57530b57cec5SDimitry Andric // splitting it between the driver and clang -cc1. 5754fe6060f1SDimitry Andric if (!types::isCXX(InputType)) { 5755fe6060f1SDimitry Andric if (!Args.hasArg(options::OPT__SLASH_std)) { 57560b57cec5SDimitry Andric Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=", 57570b57cec5SDimitry Andric /*Joined=*/true); 5758fe6060f1SDimitry Andric } else 5759fe6060f1SDimitry Andric ImplyVCPPCVer = true; 5760fe6060f1SDimitry Andric } 57610b57cec5SDimitry Andric else if (IsWindowsMSVC) 57620b57cec5SDimitry Andric ImplyVCPPCXXVer = true; 57630b57cec5SDimitry Andric 57640b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs, 57650b57cec5SDimitry Andric options::OPT_fno_trigraphs); 57665ffd83dbSDimitry Andric 57675ffd83dbSDimitry Andric // HIP headers has minimum C++ standard requirements. Therefore set the 57685ffd83dbSDimitry Andric // default language standard. 57695ffd83dbSDimitry Andric if (IsHIP) 57705ffd83dbSDimitry Andric CmdArgs.push_back(IsWindowsMSVC ? "-std=c++14" : "-std=c++11"); 57710b57cec5SDimitry Andric } 57720b57cec5SDimitry Andric 57730b57cec5SDimitry Andric // GCC's behavior for -Wwrite-strings is a bit strange: 57740b57cec5SDimitry Andric // * In C, this "warning flag" changes the types of string literals from 57750b57cec5SDimitry Andric // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning 57760b57cec5SDimitry Andric // for the discarded qualifier. 57770b57cec5SDimitry Andric // * In C++, this is just a normal warning flag. 57780b57cec5SDimitry Andric // 57790b57cec5SDimitry Andric // Implementing this warning correctly in C is hard, so we follow GCC's 57800b57cec5SDimitry Andric // behavior for now. FIXME: Directly diagnose uses of a string literal as 57810b57cec5SDimitry Andric // a non-const char* in C, rather than using this crude hack. 57820b57cec5SDimitry Andric if (!types::isCXX(InputType)) { 57830b57cec5SDimitry Andric // FIXME: This should behave just like a warning flag, and thus should also 57840b57cec5SDimitry Andric // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on. 57850b57cec5SDimitry Andric Arg *WriteStrings = 57860b57cec5SDimitry Andric Args.getLastArg(options::OPT_Wwrite_strings, 57870b57cec5SDimitry Andric options::OPT_Wno_write_strings, options::OPT_w); 57880b57cec5SDimitry Andric if (WriteStrings && 57890b57cec5SDimitry Andric WriteStrings->getOption().matches(options::OPT_Wwrite_strings)) 57900b57cec5SDimitry Andric CmdArgs.push_back("-fconst-strings"); 57910b57cec5SDimitry Andric } 57920b57cec5SDimitry Andric 57930b57cec5SDimitry Andric // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 57940b57cec5SDimitry Andric // during C++ compilation, which it is by default. GCC keeps this define even 57950b57cec5SDimitry Andric // in the presence of '-w', match this behavior bug-for-bug. 57960b57cec5SDimitry Andric if (types::isCXX(InputType) && 57970b57cec5SDimitry Andric Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 57980b57cec5SDimitry Andric true)) { 57990b57cec5SDimitry Andric CmdArgs.push_back("-fdeprecated-macro"); 58000b57cec5SDimitry Andric } 58010b57cec5SDimitry Andric 58020b57cec5SDimitry Andric // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 58030b57cec5SDimitry Andric if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 58040b57cec5SDimitry Andric if (Asm->getOption().matches(options::OPT_fasm)) 58050b57cec5SDimitry Andric CmdArgs.push_back("-fgnu-keywords"); 58060b57cec5SDimitry Andric else 58070b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-keywords"); 58080b57cec5SDimitry Andric } 58090b57cec5SDimitry Andric 5810480093f4SDimitry Andric if (!ShouldEnableAutolink(Args, TC, JA)) 58110b57cec5SDimitry Andric CmdArgs.push_back("-fno-autolink"); 58120b57cec5SDimitry Andric 58130b57cec5SDimitry Andric // Add in -fdebug-compilation-dir if necessary. 58140eae32dcSDimitry Andric const char *DebugCompilationDir = 5815a7dea167SDimitry Andric addDebugCompDirArg(Args, CmdArgs, D.getVFS()); 58160b57cec5SDimitry Andric 581781ad6265SDimitry Andric addDebugPrefixMapArg(D, TC, Args, CmdArgs); 58180b57cec5SDimitry Andric 58190b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, 58200b57cec5SDimitry Andric options::OPT_ftemplate_depth_EQ)) { 58210b57cec5SDimitry Andric CmdArgs.push_back("-ftemplate-depth"); 58220b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58230b57cec5SDimitry Andric } 58240b57cec5SDimitry Andric 58250b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) { 58260b57cec5SDimitry Andric CmdArgs.push_back("-foperator-arrow-depth"); 58270b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58280b57cec5SDimitry Andric } 58290b57cec5SDimitry Andric 58300b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) { 58310b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-depth"); 58320b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58330b57cec5SDimitry Andric } 58340b57cec5SDimitry Andric 58350b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) { 58360b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-steps"); 58370b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58380b57cec5SDimitry Andric } 58390b57cec5SDimitry Andric 584081ad6265SDimitry Andric if (Args.hasArg(options::OPT_funstable)) { 584181ad6265SDimitry Andric CmdArgs.push_back("-funstable"); 584281ad6265SDimitry Andric if (!Args.hasArg(options::OPT_fno_coroutines_ts)) 584381ad6265SDimitry Andric CmdArgs.push_back("-fcoroutines-ts"); 584481ad6265SDimitry Andric CmdArgs.push_back("-fmodules-ts"); 584581ad6265SDimitry Andric } 584681ad6265SDimitry Andric 5847a7dea167SDimitry Andric if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter)) 5848a7dea167SDimitry Andric CmdArgs.push_back("-fexperimental-new-constant-interpreter"); 5849a7dea167SDimitry Andric 58500b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 58510b57cec5SDimitry Andric CmdArgs.push_back("-fbracket-depth"); 58520b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58530b57cec5SDimitry Andric } 58540b57cec5SDimitry Andric 58550b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 58560b57cec5SDimitry Andric options::OPT_Wlarge_by_value_copy_def)) { 58570b57cec5SDimitry Andric if (A->getNumValues()) { 58580b57cec5SDimitry Andric StringRef bytes = A->getValue(); 58590b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 58600b57cec5SDimitry Andric } else 58610b57cec5SDimitry Andric CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 58620b57cec5SDimitry Andric } 58630b57cec5SDimitry Andric 58640b57cec5SDimitry Andric if (Args.hasArg(options::OPT_relocatable_pch)) 58650b57cec5SDimitry Andric CmdArgs.push_back("-relocatable-pch"); 58660b57cec5SDimitry Andric 58670b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fcf_runtime_abi_EQ)) { 58680b57cec5SDimitry Andric static const char *kCFABIs[] = { 58690b57cec5SDimitry Andric "standalone", "objc", "swift", "swift-5.0", "swift-4.2", "swift-4.1", 58700b57cec5SDimitry Andric }; 58710b57cec5SDimitry Andric 587281ad6265SDimitry Andric if (!llvm::is_contained(kCFABIs, StringRef(A->getValue()))) 58730b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_cf_runtime_abi) << A->getValue(); 58740b57cec5SDimitry Andric else 58750b57cec5SDimitry Andric A->render(Args, CmdArgs); 58760b57cec5SDimitry Andric } 58770b57cec5SDimitry Andric 58780b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 58790b57cec5SDimitry Andric CmdArgs.push_back("-fconstant-string-class"); 58800b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58810b57cec5SDimitry Andric } 58820b57cec5SDimitry Andric 58830b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 58840b57cec5SDimitry Andric CmdArgs.push_back("-ftabstop"); 58850b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 58860b57cec5SDimitry Andric } 58870b57cec5SDimitry Andric 58880b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fstack_size_section, 58890b57cec5SDimitry Andric options::OPT_fno_stack_size_section, RawTriple.isPS4())) 58900b57cec5SDimitry Andric CmdArgs.push_back("-fstack-size-section"); 58910b57cec5SDimitry Andric 5892fe6060f1SDimitry Andric if (Args.hasArg(options::OPT_fstack_usage)) { 5893fe6060f1SDimitry Andric CmdArgs.push_back("-stack-usage-file"); 5894fe6060f1SDimitry Andric 5895fe6060f1SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 5896fe6060f1SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 5897fe6060f1SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "su"); 5898fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 5899fe6060f1SDimitry Andric } else 5900fe6060f1SDimitry Andric CmdArgs.push_back( 5901fe6060f1SDimitry Andric Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".su")); 5902fe6060f1SDimitry Andric } 5903fe6060f1SDimitry Andric 59040b57cec5SDimitry Andric CmdArgs.push_back("-ferror-limit"); 59050b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 59060b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59070b57cec5SDimitry Andric else 59080b57cec5SDimitry Andric CmdArgs.push_back("19"); 59090b57cec5SDimitry Andric 59100b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) { 59110b57cec5SDimitry Andric CmdArgs.push_back("-fmacro-backtrace-limit"); 59120b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59130b57cec5SDimitry Andric } 59140b57cec5SDimitry Andric 59150b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) { 59160b57cec5SDimitry Andric CmdArgs.push_back("-ftemplate-backtrace-limit"); 59170b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59180b57cec5SDimitry Andric } 59190b57cec5SDimitry Andric 59200b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) { 59210b57cec5SDimitry Andric CmdArgs.push_back("-fconstexpr-backtrace-limit"); 59220b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59230b57cec5SDimitry Andric } 59240b57cec5SDimitry Andric 59250b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) { 59260b57cec5SDimitry Andric CmdArgs.push_back("-fspell-checking-limit"); 59270b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59280b57cec5SDimitry Andric } 59290b57cec5SDimitry Andric 59300b57cec5SDimitry Andric // Pass -fmessage-length=. 59315ffd83dbSDimitry Andric unsigned MessageLength = 0; 59320b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 59335ffd83dbSDimitry Andric StringRef V(A->getValue()); 59345ffd83dbSDimitry Andric if (V.getAsInteger(0, MessageLength)) 59355ffd83dbSDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 59365ffd83dbSDimitry Andric << V << A->getOption().getName(); 59370b57cec5SDimitry Andric } else { 59380b57cec5SDimitry Andric // If -fmessage-length=N was not specified, determine whether this is a 59390b57cec5SDimitry Andric // terminal and, if so, implicitly define -fmessage-length appropriately. 59405ffd83dbSDimitry Andric MessageLength = llvm::sys::Process::StandardErrColumns(); 59410b57cec5SDimitry Andric } 59425ffd83dbSDimitry Andric if (MessageLength != 0) 59435ffd83dbSDimitry Andric CmdArgs.push_back( 59445ffd83dbSDimitry Andric Args.MakeArgString("-fmessage-length=" + Twine(MessageLength))); 59450b57cec5SDimitry Andric 594681ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_EQ)) 594781ad6265SDimitry Andric CmdArgs.push_back( 594881ad6265SDimitry Andric Args.MakeArgString("-frandomize-layout-seed=" + Twine(A->getValue(0)))); 594981ad6265SDimitry Andric 595081ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_frandomize_layout_seed_file_EQ)) 595181ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-frandomize-layout-seed-file=" + 595281ad6265SDimitry Andric Twine(A->getValue(0)))); 595381ad6265SDimitry Andric 59540b57cec5SDimitry Andric // -fvisibility= and -fvisibility-ms-compat are of a piece. 59550b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 59560b57cec5SDimitry Andric options::OPT_fvisibility_ms_compat)) { 59570b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 59580b57cec5SDimitry Andric CmdArgs.push_back("-fvisibility"); 59590b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 59600b57cec5SDimitry Andric } else { 59610b57cec5SDimitry Andric assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 59620b57cec5SDimitry Andric CmdArgs.push_back("-fvisibility"); 59630b57cec5SDimitry Andric CmdArgs.push_back("hidden"); 59640b57cec5SDimitry Andric CmdArgs.push_back("-ftype-visibility"); 59650b57cec5SDimitry Andric CmdArgs.push_back("default"); 59660b57cec5SDimitry Andric } 596704eeddc0SDimitry Andric } else if (IsOpenMPDevice) { 596804eeddc0SDimitry Andric // When compiling for the OpenMP device we want protected visibility by 596904eeddc0SDimitry Andric // default. This prevents the device from accidenally preempting code on the 597004eeddc0SDimitry Andric // host, makes the system more robust, and improves performance. 597104eeddc0SDimitry Andric CmdArgs.push_back("-fvisibility"); 597204eeddc0SDimitry Andric CmdArgs.push_back("protected"); 59730b57cec5SDimitry Andric } 59740b57cec5SDimitry Andric 5975e8d8bef9SDimitry Andric if (!RawTriple.isPS4()) 5976e8d8bef9SDimitry Andric if (const Arg *A = 5977e8d8bef9SDimitry Andric Args.getLastArg(options::OPT_fvisibility_from_dllstorageclass, 5978e8d8bef9SDimitry Andric options::OPT_fno_visibility_from_dllstorageclass)) { 5979e8d8bef9SDimitry Andric if (A->getOption().matches( 5980e8d8bef9SDimitry Andric options::OPT_fvisibility_from_dllstorageclass)) { 5981e8d8bef9SDimitry Andric CmdArgs.push_back("-fvisibility-from-dllstorageclass"); 5982e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_dllexport_EQ); 5983e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_nodllstorageclass_EQ); 5984e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_externs_dllimport_EQ); 5985e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, 5986e8d8bef9SDimitry Andric options::OPT_fvisibility_externs_nodllstorageclass_EQ); 5987e8d8bef9SDimitry Andric } 5988e8d8bef9SDimitry Andric } 5989e8d8bef9SDimitry Andric 5990e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_mignore_xcoff_visibility)) { 5991e8d8bef9SDimitry Andric if (Triple.isOSAIX()) 5992e8d8bef9SDimitry Andric CmdArgs.push_back("-mignore-xcoff-visibility"); 5993e8d8bef9SDimitry Andric else 5994e8d8bef9SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 5995e8d8bef9SDimitry Andric << A->getAsString(Args) << TripleStr; 5996e8d8bef9SDimitry Andric } 5997e8d8bef9SDimitry Andric 599881ad6265SDimitry Andric if (const Arg *A = 599981ad6265SDimitry Andric Args.getLastArg(options::OPT_mdefault_visibility_export_mapping_EQ)) { 600081ad6265SDimitry Andric if (Triple.isOSAIX()) 600181ad6265SDimitry Andric A->render(Args, CmdArgs); 600281ad6265SDimitry Andric else 600381ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 600481ad6265SDimitry Andric << A->getAsString(Args) << TripleStr; 600581ad6265SDimitry Andric } 6006fe6060f1SDimitry Andric 6007fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden, 6008fe6060f1SDimitry Andric options::OPT_fno_visibility_inlines_hidden, false)) 6009fe6060f1SDimitry Andric CmdArgs.push_back("-fvisibility-inlines-hidden"); 6010fe6060f1SDimitry Andric 6011e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var, 6012e8d8bef9SDimitry Andric options::OPT_fno_visibility_inlines_hidden_static_local_var); 60130b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden); 60140b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 60150b57cec5SDimitry Andric 60164824e7fdSDimitry Andric if (Args.hasFlag(options::OPT_fnew_infallible, 60174824e7fdSDimitry Andric options::OPT_fno_new_infallible, false)) 60184824e7fdSDimitry Andric CmdArgs.push_back("-fnew-infallible"); 60194824e7fdSDimitry Andric 6020fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_fno_operator_names, 6021fe6060f1SDimitry Andric options::OPT_foperator_names, false)) 6022fe6060f1SDimitry Andric CmdArgs.push_back("-fno-operator-names"); 6023fe6060f1SDimitry Andric 60240b57cec5SDimitry Andric // Forward -f (flag) options which we can pass directly. 60250b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 60260b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 60270b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); 60280b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_femulated_tls, 60290b57cec5SDimitry Andric options::OPT_fno_emulated_tls); 603081ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); 603181ad6265SDimitry Andric 603281ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { 603381ad6265SDimitry Andric // FIXME: There's no reason for this to be restricted to X86. The backend 603481ad6265SDimitry Andric // code needs to be changed to include the appropriate function calls 603581ad6265SDimitry Andric // automatically. 603681ad6265SDimitry Andric if (!Triple.isX86() && !Triple.isAArch64()) 603781ad6265SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 603881ad6265SDimitry Andric << A->getAsString(Args) << TripleStr; 603981ad6265SDimitry Andric } 60400b57cec5SDimitry Andric 60410b57cec5SDimitry Andric // AltiVec-like language extensions aren't relevant for assembling. 60420b57cec5SDimitry Andric if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) 60430b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fzvector); 60440b57cec5SDimitry Andric 60450b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 60460b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 60470b57cec5SDimitry Andric 60480b57cec5SDimitry Andric // Forward flags for OpenMP. We don't do this if the current action is an 60490b57cec5SDimitry Andric // device offloading action other than OpenMP. 60500b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, 60510b57cec5SDimitry Andric options::OPT_fno_openmp, false) && 60520b57cec5SDimitry Andric (JA.isDeviceOffloading(Action::OFK_None) || 60530b57cec5SDimitry Andric JA.isDeviceOffloading(Action::OFK_OpenMP))) { 60540b57cec5SDimitry Andric switch (D.getOpenMPRuntime(Args)) { 60550b57cec5SDimitry Andric case Driver::OMPRT_OMP: 60560b57cec5SDimitry Andric case Driver::OMPRT_IOMP5: 60570b57cec5SDimitry Andric // Clang can generate useful OpenMP code for these two runtime libraries. 60580b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp"); 60590b57cec5SDimitry Andric 60600b57cec5SDimitry Andric // If no option regarding the use of TLS in OpenMP codegeneration is 60610b57cec5SDimitry Andric // given, decide a default based on the target. Otherwise rely on the 60620b57cec5SDimitry Andric // options and pass the right information to the frontend. 60630b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_use_tls, 60640b57cec5SDimitry Andric options::OPT_fnoopenmp_use_tls, /*Default=*/true)) 60650b57cec5SDimitry Andric CmdArgs.push_back("-fnoopenmp-use-tls"); 60660b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 60670b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 6068480093f4SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_enable_irbuilder); 60690b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 6070349cc55cSDimitry Andric if (!Args.hasFlag(options::OPT_fopenmp_extensions, 6071349cc55cSDimitry Andric options::OPT_fno_openmp_extensions, /*Default=*/true)) 6072349cc55cSDimitry Andric CmdArgs.push_back("-fno-openmp-extensions"); 60730b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_number_of_sm_EQ); 60740b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_cuda_blocks_per_sm_EQ); 60750b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, 60760b57cec5SDimitry Andric options::OPT_fopenmp_cuda_teams_reduction_recs_num_EQ); 60770b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_optimistic_collapse, 60780b57cec5SDimitry Andric options::OPT_fno_openmp_optimistic_collapse, 60790b57cec5SDimitry Andric /*Default=*/false)) 60800b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-optimistic-collapse"); 60810b57cec5SDimitry Andric 60820b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, forward 60830b57cec5SDimitry Andric // cuda-mode flag 60840b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_mode, 60850b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_mode, /*Default=*/false)) 60860b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-mode"); 60870b57cec5SDimitry Andric 6088349cc55cSDimitry Andric // When in OpenMP offloading mode, enable debugging on the device. 6089349cc55cSDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ); 6090349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_target_debug, 6091349cc55cSDimitry Andric options::OPT_fno_openmp_target_debug, /*Default=*/false)) 6092349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-target-debug"); 6093349cc55cSDimitry Andric 60940b57cec5SDimitry Andric // When in OpenMP offloading mode with NVPTX target, check if full runtime 60950b57cec5SDimitry Andric // is required. 60960b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_cuda_force_full_runtime, 60970b57cec5SDimitry Andric options::OPT_fno_openmp_cuda_force_full_runtime, 60980b57cec5SDimitry Andric /*Default=*/false)) 60990b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-cuda-force-full-runtime"); 6100349cc55cSDimitry Andric 6101349cc55cSDimitry Andric // When in OpenMP offloading mode, forward assumptions information about 6102349cc55cSDimitry Andric // thread and team counts in the device. 6103349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription, 6104349cc55cSDimitry Andric options::OPT_fno_openmp_assume_teams_oversubscription, 6105349cc55cSDimitry Andric /*Default=*/false)) 6106349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-teams-oversubscription"); 6107349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription, 6108349cc55cSDimitry Andric options::OPT_fno_openmp_assume_threads_oversubscription, 6109349cc55cSDimitry Andric /*Default=*/false)) 6110349cc55cSDimitry Andric CmdArgs.push_back("-fopenmp-assume-threads-oversubscription"); 611181ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_assume_no_thread_state)) 611281ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-assume-no-thread-state"); 611381ad6265SDimitry Andric if (Args.hasArg(options::OPT_fopenmp_offload_mandatory)) 611481ad6265SDimitry Andric CmdArgs.push_back("-fopenmp-offload-mandatory"); 61150b57cec5SDimitry Andric break; 61160b57cec5SDimitry Andric default: 61170b57cec5SDimitry Andric // By default, if Clang doesn't know how to generate useful OpenMP code 61180b57cec5SDimitry Andric // for a specific runtime library, we just don't pass the '-fopenmp' flag 61190b57cec5SDimitry Andric // down to the actual compilation. 61200b57cec5SDimitry Andric // FIXME: It would be better to have a mode which *only* omits IR 61210b57cec5SDimitry Andric // generation based on the OpenMP support so that we get consistent 61220b57cec5SDimitry Andric // semantic analysis, etc. 61230b57cec5SDimitry Andric break; 61240b57cec5SDimitry Andric } 61250b57cec5SDimitry Andric } else { 61260b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd, 61270b57cec5SDimitry Andric options::OPT_fno_openmp_simd); 61280b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ); 612981ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fopenmp_extensions, 613081ad6265SDimitry Andric options::OPT_fno_openmp_extensions); 61310b57cec5SDimitry Andric } 61320b57cec5SDimitry Andric 613381ad6265SDimitry Andric // Forward the new driver to change offloading code generation. 613481ad6265SDimitry Andric if (Args.hasArg(options::OPT_offload_new_driver)) 613581ad6265SDimitry Andric CmdArgs.push_back("--offload-new-driver"); 613681ad6265SDimitry Andric 6137349cc55cSDimitry Andric SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); 61380b57cec5SDimitry Andric 61390b57cec5SDimitry Andric const XRayArgs &XRay = TC.getXRayArgs(); 61400b57cec5SDimitry Andric XRay.addArgs(TC, Args, CmdArgs, InputType); 61410b57cec5SDimitry Andric 6142e8d8bef9SDimitry Andric for (const auto &Filename : 6143e8d8bef9SDimitry Andric Args.getAllArgValues(options::OPT_fprofile_list_EQ)) { 6144e8d8bef9SDimitry Andric if (D.getVFS().exists(Filename)) 6145e8d8bef9SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fprofile-list=" + Filename)); 6146e8d8bef9SDimitry Andric else 6147e8d8bef9SDimitry Andric D.Diag(clang::diag::err_drv_no_such_file) << Filename; 6148e8d8bef9SDimitry Andric } 6149e8d8bef9SDimitry Andric 6150480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) { 6151480093f4SDimitry Andric StringRef S0 = A->getValue(), S = S0; 615255e4f9d5SDimitry Andric unsigned Size, Offset = 0; 6153fe6060f1SDimitry Andric if (!Triple.isAArch64() && !Triple.isRISCV() && !Triple.isX86()) 6154480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6155480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6156480093f4SDimitry Andric else if (S.consumeInteger(10, Size) || 6157480093f4SDimitry Andric (!S.empty() && (!S.consume_front(",") || 615855e4f9d5SDimitry Andric S.consumeInteger(10, Offset) || !S.empty()))) 6159480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_argument_to_option) 6160480093f4SDimitry Andric << S0 << A->getOption().getName(); 616155e4f9d5SDimitry Andric else if (Size < Offset) 6162480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument); 616355e4f9d5SDimitry Andric else { 6164480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size))); 616555e4f9d5SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 616655e4f9d5SDimitry Andric "-fpatchable-function-entry-offset=" + Twine(Offset))); 616755e4f9d5SDimitry Andric } 6168480093f4SDimitry Andric } 6169480093f4SDimitry Andric 617004eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch); 617104eeddc0SDimitry Andric 6172480093f4SDimitry Andric if (TC.SupportsProfiling()) { 61730b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pg); 61740b57cec5SDimitry Andric 6175480093f4SDimitry Andric llvm::Triple::ArchType Arch = TC.getArch(); 6176480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfentry)) { 6177480093f4SDimitry Andric if (Arch == llvm::Triple::systemz || TC.getTriple().isX86()) 6178480093f4SDimitry Andric A->render(Args, CmdArgs); 6179480093f4SDimitry Andric else 6180480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6181480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6182480093f4SDimitry Andric } 6183480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) { 6184480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6185480093f4SDimitry Andric A->render(Args, CmdArgs); 6186480093f4SDimitry Andric else 6187480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6188480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6189480093f4SDimitry Andric } 6190480093f4SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) { 6191480093f4SDimitry Andric if (Arch == llvm::Triple::systemz) 6192480093f4SDimitry Andric A->render(Args, CmdArgs); 6193480093f4SDimitry Andric else 6194480093f4SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 6195480093f4SDimitry Andric << A->getAsString(Args) << TripleStr; 6196480093f4SDimitry Andric } 6197480093f4SDimitry Andric } 61980b57cec5SDimitry Andric 61990b57cec5SDimitry Andric if (Args.getLastArg(options::OPT_fapple_kext) || 62000b57cec5SDimitry Andric (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) 62010b57cec5SDimitry Andric CmdArgs.push_back("-fapple-kext"); 62020b57cec5SDimitry Andric 6203fe6060f1SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_altivec_src_compat); 6204a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions_EQ); 62050b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 62060b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 62070b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 62080b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 6209e8d8bef9SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ); 62100b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace); 6211a7dea167SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ); 62120b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 62130b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_malign_double); 6214480093f4SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file); 62150b57cec5SDimitry Andric 62160b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 62170b57cec5SDimitry Andric CmdArgs.push_back("-ftrapv-handler"); 62180b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 62190b57cec5SDimitry Andric } 62200b57cec5SDimitry Andric 62210b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 62220b57cec5SDimitry Andric 62230b57cec5SDimitry Andric // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 62240b57cec5SDimitry Andric // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 62250b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) { 62260b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fwrapv)) 62270b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 62280b57cec5SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 62290b57cec5SDimitry Andric options::OPT_fno_strict_overflow)) { 62300b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_strict_overflow)) 62310b57cec5SDimitry Andric CmdArgs.push_back("-fwrapv"); 62320b57cec5SDimitry Andric } 62330b57cec5SDimitry Andric 62340b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_freroll_loops, 62350b57cec5SDimitry Andric options::OPT_fno_reroll_loops)) 62360b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_freroll_loops)) 62370b57cec5SDimitry Andric CmdArgs.push_back("-freroll-loops"); 62380b57cec5SDimitry Andric 6239d409305fSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops, 6240d409305fSDimitry Andric options::OPT_fno_finite_loops); 6241d409305fSDimitry Andric 62420b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 62430b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 62440b57cec5SDimitry Andric options::OPT_fno_unroll_loops); 62450b57cec5SDimitry Andric 62460b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_pthread); 62470b57cec5SDimitry Andric 62485ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_mspeculative_load_hardening, 62495ffd83dbSDimitry Andric options::OPT_mno_speculative_load_hardening, false)) 62500b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mspeculative-load-hardening")); 62510b57cec5SDimitry Andric 6252e8d8bef9SDimitry Andric RenderSSPOptions(D, TC, Args, CmdArgs, KernelOrKext); 62535ffd83dbSDimitry Andric RenderSCPOptions(TC, Args, CmdArgs); 62540b57cec5SDimitry Andric RenderTrivialAutoVarInitOptions(D, TC, Args, CmdArgs); 62550b57cec5SDimitry Andric 6256349cc55cSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fswift_async_fp_EQ); 6257349cc55cSDimitry Andric 62580b57cec5SDimitry Andric // Translate -mstackrealign 62590b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign, 62600b57cec5SDimitry Andric false)) 62610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstackrealign")); 62620b57cec5SDimitry Andric 62630b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_alignment)) { 62640b57cec5SDimitry Andric StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 62650b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 62660b57cec5SDimitry Andric } 62670b57cec5SDimitry Andric 62680b57cec5SDimitry Andric if (Args.hasArg(options::OPT_mstack_probe_size)) { 62690b57cec5SDimitry Andric StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size); 62700b57cec5SDimitry Andric 62710b57cec5SDimitry Andric if (!Size.empty()) 62720b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size)); 62730b57cec5SDimitry Andric else 62740b57cec5SDimitry Andric CmdArgs.push_back("-mstack-probe-size=0"); 62750b57cec5SDimitry Andric } 62760b57cec5SDimitry Andric 627781ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_mstack_arg_probe, 627881ad6265SDimitry Andric options::OPT_mno_stack_arg_probe); 62790b57cec5SDimitry Andric 62800b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it, 62810b57cec5SDimitry Andric options::OPT_mno_restrict_it)) { 62820b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_mrestrict_it)) { 62830b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 62840b57cec5SDimitry Andric CmdArgs.push_back("-arm-restrict-it"); 62850b57cec5SDimitry Andric } else { 62860b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 628781ad6265SDimitry Andric CmdArgs.push_back("-arm-default-it"); 62880b57cec5SDimitry Andric } 62890b57cec5SDimitry Andric } 62900b57cec5SDimitry Andric 62910b57cec5SDimitry Andric // Forward -cl options to -cc1 6292fe6060f1SDimitry Andric RenderOpenCLOptions(Args, CmdArgs, InputType); 62930b57cec5SDimitry Andric 629481ad6265SDimitry Andric // Forward hlsl options to -cc1 629581ad6265SDimitry Andric if (C.getDriver().IsDXCMode()) 629681ad6265SDimitry Andric RenderHLSLOptions(Args, CmdArgs, InputType); 629781ad6265SDimitry Andric 6298e8d8bef9SDimitry Andric if (IsHIP) { 6299e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fhip_new_launch_api, 63005ffd83dbSDimitry Andric options::OPT_fno_hip_new_launch_api, true)) 6301a7dea167SDimitry Andric CmdArgs.push_back("-fhip-new-launch-api"); 6302e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_allow_device_init, 6303e8d8bef9SDimitry Andric options::OPT_fno_gpu_allow_device_init, false)) 6304e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-allow-device-init"); 630581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fhip_kernel_arg_name, 630681ad6265SDimitry Andric options::OPT_fno_hip_kernel_arg_name); 6307e8d8bef9SDimitry Andric } 6308e8d8bef9SDimitry Andric 6309e8d8bef9SDimitry Andric if (IsCuda || IsHIP) { 6310753f127fSDimitry Andric if (IsRDCMode) 6311fe6060f1SDimitry Andric CmdArgs.push_back("-fgpu-rdc"); 6312e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_defer_diag, 6313e8d8bef9SDimitry Andric options::OPT_fno_gpu_defer_diag, false)) 6314e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6315e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads, 6316e8d8bef9SDimitry Andric options::OPT_fno_gpu_exclude_wrong_side_overloads, 6317e8d8bef9SDimitry Andric false)) { 6318e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-exclude-wrong-side-overloads"); 6319e8d8bef9SDimitry Andric CmdArgs.push_back("-fgpu-defer-diag"); 6320e8d8bef9SDimitry Andric } 6321e8d8bef9SDimitry Andric } 6322a7dea167SDimitry Andric 632381ad6265SDimitry Andric // Forward -nogpulib to -cc1. 632481ad6265SDimitry Andric if (Args.hasArg(options::OPT_nogpulib)) 632581ad6265SDimitry Andric CmdArgs.push_back("-nogpulib"); 632681ad6265SDimitry Andric 63270b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { 63280b57cec5SDimitry Andric CmdArgs.push_back( 63290b57cec5SDimitry Andric Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); 63300b57cec5SDimitry Andric } 63310b57cec5SDimitry Andric 633204eeddc0SDimitry Andric if (IsUsingLTO) 633304eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_mibt_seal); 633404eeddc0SDimitry Andric 6335753f127fSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ)) 6336753f127fSDimitry Andric CmdArgs.push_back( 6337753f127fSDimitry Andric Args.MakeArgString(Twine("-mfunction-return=") + A->getValue())); 6338753f127fSDimitry Andric 6339fe6060f1SDimitry Andric // Forward -f options with positive and negative forms; we translate these by 6340fe6060f1SDimitry Andric // hand. Do not propagate PGO options to the GPU-side compilations as the 6341fe6060f1SDimitry Andric // profile info is for the host-side compilation only. 6342fe6060f1SDimitry Andric if (!(IsCudaDevice || IsHIPDevice)) { 63430b57cec5SDimitry Andric if (Arg *A = getLastProfileSampleUseArg(Args)) { 63440b57cec5SDimitry Andric auto *PGOArg = Args.getLastArg( 63450b57cec5SDimitry Andric options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ, 6346fe6060f1SDimitry Andric options::OPT_fcs_profile_generate, 6347fe6060f1SDimitry Andric options::OPT_fcs_profile_generate_EQ, options::OPT_fprofile_use, 6348fe6060f1SDimitry Andric options::OPT_fprofile_use_EQ); 63490b57cec5SDimitry Andric if (PGOArg) 63500b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 63510b57cec5SDimitry Andric << "SampleUse with PGO options"; 63520b57cec5SDimitry Andric 63530b57cec5SDimitry Andric StringRef fname = A->getValue(); 63540b57cec5SDimitry Andric if (!llvm::sys::fs::exists(fname)) 63550b57cec5SDimitry Andric D.Diag(diag::err_drv_no_such_file) << fname; 63560b57cec5SDimitry Andric else 63570b57cec5SDimitry Andric A->render(Args, CmdArgs); 63580b57cec5SDimitry Andric } 63590b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fprofile_remapping_file_EQ); 63600b57cec5SDimitry Andric 6361e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling, 6362fe6060f1SDimitry Andric options::OPT_fno_pseudo_probe_for_profiling, false)) { 6363e8d8bef9SDimitry Andric CmdArgs.push_back("-fpseudo-probe-for-profiling"); 6364fe6060f1SDimitry Andric // Enforce -funique-internal-linkage-names if it's not explicitly turned 6365fe6060f1SDimitry Andric // off. 6366fe6060f1SDimitry Andric if (Args.hasFlag(options::OPT_funique_internal_linkage_names, 6367fe6060f1SDimitry Andric options::OPT_fno_unique_internal_linkage_names, true)) 6368fe6060f1SDimitry Andric CmdArgs.push_back("-funique-internal-linkage-names"); 6369fe6060f1SDimitry Andric } 6370fe6060f1SDimitry Andric } 63710b57cec5SDimitry Andric RenderBuiltinOptions(TC, RawTriple, Args, CmdArgs); 63720b57cec5SDimitry Andric 637381ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 637481ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 63750b57cec5SDimitry Andric 63760b57cec5SDimitry Andric // -fblocks=0 is default. 63770b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 63780b57cec5SDimitry Andric TC.IsBlocksDefault()) || 63790b57cec5SDimitry Andric (Args.hasArg(options::OPT_fgnu_runtime) && 63800b57cec5SDimitry Andric Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 63810b57cec5SDimitry Andric !Args.hasArg(options::OPT_fno_blocks))) { 63820b57cec5SDimitry Andric CmdArgs.push_back("-fblocks"); 63830b57cec5SDimitry Andric 63840b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fgnu_runtime) && !TC.hasBlocksRuntime()) 63850b57cec5SDimitry Andric CmdArgs.push_back("-fblocks-runtime-optional"); 63860b57cec5SDimitry Andric } 63870b57cec5SDimitry Andric 63880b57cec5SDimitry Andric // -fencode-extended-block-signature=1 is default. 63890b57cec5SDimitry Andric if (TC.IsEncodeExtendedBlockSignatureDefault()) 63900b57cec5SDimitry Andric CmdArgs.push_back("-fencode-extended-block-signature"); 63910b57cec5SDimitry Andric 63920b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcoroutines_ts, options::OPT_fno_coroutines_ts, 63930b57cec5SDimitry Andric false) && 63940b57cec5SDimitry Andric types::isCXX(InputType)) { 63950b57cec5SDimitry Andric CmdArgs.push_back("-fcoroutines-ts"); 63960b57cec5SDimitry Andric } 63970b57cec5SDimitry Andric 63980b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fdouble_square_bracket_attributes, 63990b57cec5SDimitry Andric options::OPT_fno_double_square_bracket_attributes); 64000b57cec5SDimitry Andric 640181ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_faccess_control, 640281ad6265SDimitry Andric options::OPT_fno_access_control); 640381ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_felide_constructors, 640481ad6265SDimitry Andric options::OPT_fno_elide_constructors); 64050b57cec5SDimitry Andric 64060b57cec5SDimitry Andric ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); 64070b57cec5SDimitry Andric 64080b57cec5SDimitry Andric if (KernelOrKext || (types::isCXX(InputType) && 64090b57cec5SDimitry Andric (RTTIMode == ToolChain::RM_Disabled))) 64100b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti"); 64110b57cec5SDimitry Andric 6412e8d8bef9SDimitry Andric // -fshort-enums=0 is default for all architectures except Hexagon and z/OS. 64130b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums, 6414e8d8bef9SDimitry Andric TC.getArch() == llvm::Triple::hexagon || Triple.isOSzOS())) 64150b57cec5SDimitry Andric CmdArgs.push_back("-fshort-enums"); 64160b57cec5SDimitry Andric 64170b57cec5SDimitry Andric RenderCharacterOptions(Args, AuxTriple ? *AuxTriple : RawTriple, CmdArgs); 64180b57cec5SDimitry Andric 64190b57cec5SDimitry Andric // -fuse-cxa-atexit is default. 64200b57cec5SDimitry Andric if (!Args.hasFlag( 64210b57cec5SDimitry Andric options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit, 64225ffd83dbSDimitry Andric !RawTriple.isOSAIX() && !RawTriple.isOSWindows() && 64230b57cec5SDimitry Andric ((RawTriple.getVendor() != llvm::Triple::MipsTechnologies) || 64240b57cec5SDimitry Andric RawTriple.hasEnvironment())) || 64250b57cec5SDimitry Andric KernelOrKext) 64260b57cec5SDimitry Andric CmdArgs.push_back("-fno-use-cxa-atexit"); 64270b57cec5SDimitry Andric 64280b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fregister_global_dtors_with_atexit, 64290b57cec5SDimitry Andric options::OPT_fno_register_global_dtors_with_atexit, 64300b57cec5SDimitry Andric RawTriple.isOSDarwin() && !KernelOrKext)) 64310b57cec5SDimitry Andric CmdArgs.push_back("-fregister-global-dtors-with-atexit"); 64320b57cec5SDimitry Andric 643381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fuse_line_directives, 643481ad6265SDimitry Andric options::OPT_fno_use_line_directives); 64350b57cec5SDimitry Andric 6436349cc55cSDimitry Andric // -fno-minimize-whitespace is default. 6437349cc55cSDimitry Andric if (Args.hasFlag(options::OPT_fminimize_whitespace, 6438349cc55cSDimitry Andric options::OPT_fno_minimize_whitespace, false)) { 6439349cc55cSDimitry Andric types::ID InputType = Inputs[0].getType(); 6440349cc55cSDimitry Andric if (!isDerivedFromC(InputType)) 6441349cc55cSDimitry Andric D.Diag(diag::err_drv_minws_unsupported_input_type) 6442349cc55cSDimitry Andric << types::getTypeName(InputType); 6443349cc55cSDimitry Andric CmdArgs.push_back("-fminimize-whitespace"); 6444349cc55cSDimitry Andric } 6445349cc55cSDimitry Andric 64465ffd83dbSDimitry Andric // -fms-extensions=0 is default. 64475ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 64485ffd83dbSDimitry Andric IsWindowsMSVC)) 64495ffd83dbSDimitry Andric CmdArgs.push_back("-fms-extensions"); 64505ffd83dbSDimitry Andric 64510b57cec5SDimitry Andric // -fms-compatibility=0 is default. 6452a7dea167SDimitry Andric bool IsMSVCCompat = Args.hasFlag( 6453a7dea167SDimitry Andric options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, 6454a7dea167SDimitry Andric (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, 6455a7dea167SDimitry Andric options::OPT_fno_ms_extensions, true))); 6456a7dea167SDimitry Andric if (IsMSVCCompat) 64570b57cec5SDimitry Andric CmdArgs.push_back("-fms-compatibility"); 64580b57cec5SDimitry Andric 6459a7dea167SDimitry Andric // Handle -fgcc-version, if present. 6460a7dea167SDimitry Andric VersionTuple GNUCVer; 6461a7dea167SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) { 6462a7dea167SDimitry Andric // Check that the version has 1 to 3 components and the minor and patch 6463a7dea167SDimitry Andric // versions fit in two decimal digits. 6464a7dea167SDimitry Andric StringRef Val = A->getValue(); 6465a7dea167SDimitry Andric Val = Val.empty() ? "0" : Val; // Treat "" as 0 or disable. 6466a7dea167SDimitry Andric bool Invalid = GNUCVer.tryParse(Val); 646781ad6265SDimitry Andric unsigned Minor = GNUCVer.getMinor().value_or(0); 646881ad6265SDimitry Andric unsigned Patch = GNUCVer.getSubminor().value_or(0); 6469a7dea167SDimitry Andric if (Invalid || GNUCVer.getBuild() || Minor >= 100 || Patch >= 100) { 6470a7dea167SDimitry Andric D.Diag(diag::err_drv_invalid_value) 6471a7dea167SDimitry Andric << A->getAsString(Args) << A->getValue(); 6472a7dea167SDimitry Andric } 6473a7dea167SDimitry Andric } else if (!IsMSVCCompat) { 6474a7dea167SDimitry Andric // Imitate GCC 4.2.1 by default if -fms-compatibility is not in effect. 6475a7dea167SDimitry Andric GNUCVer = VersionTuple(4, 2, 1); 6476a7dea167SDimitry Andric } 6477a7dea167SDimitry Andric if (!GNUCVer.empty()) { 6478a7dea167SDimitry Andric CmdArgs.push_back( 6479a7dea167SDimitry Andric Args.MakeArgString("-fgnuc-version=" + GNUCVer.getAsString())); 6480a7dea167SDimitry Andric } 6481a7dea167SDimitry Andric 64820b57cec5SDimitry Andric VersionTuple MSVT = TC.computeMSVCVersion(&D, Args); 64830b57cec5SDimitry Andric if (!MSVT.empty()) 64840b57cec5SDimitry Andric CmdArgs.push_back( 64850b57cec5SDimitry Andric Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString())); 64860b57cec5SDimitry Andric 64870b57cec5SDimitry Andric bool IsMSVC2015Compatible = MSVT.getMajor() >= 19; 6488fe6060f1SDimitry Andric if (ImplyVCPPCVer) { 6489fe6060f1SDimitry Andric StringRef LanguageStandard; 6490fe6060f1SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 6491fe6060f1SDimitry Andric Std = StdArg; 6492fe6060f1SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 6493fe6060f1SDimitry Andric .Case("c11", "-std=c11") 6494fe6060f1SDimitry Andric .Case("c17", "-std=c17") 6495fe6060f1SDimitry Andric .Default(""); 6496fe6060f1SDimitry Andric if (LanguageStandard.empty()) 6497fe6060f1SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 6498fe6060f1SDimitry Andric << StdArg->getAsString(Args); 6499fe6060f1SDimitry Andric } 6500fe6060f1SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 6501fe6060f1SDimitry Andric } 65020b57cec5SDimitry Andric if (ImplyVCPPCXXVer) { 65030b57cec5SDimitry Andric StringRef LanguageStandard; 65040b57cec5SDimitry Andric if (const Arg *StdArg = Args.getLastArg(options::OPT__SLASH_std)) { 65050b57cec5SDimitry Andric Std = StdArg; 65060b57cec5SDimitry Andric LanguageStandard = llvm::StringSwitch<StringRef>(StdArg->getValue()) 65070b57cec5SDimitry Andric .Case("c++14", "-std=c++14") 65080b57cec5SDimitry Andric .Case("c++17", "-std=c++17") 6509fe6060f1SDimitry Andric .Case("c++20", "-std=c++20") 6510fe6060f1SDimitry Andric .Case("c++latest", "-std=c++2b") 65110b57cec5SDimitry Andric .Default(""); 65120b57cec5SDimitry Andric if (LanguageStandard.empty()) 65130b57cec5SDimitry Andric D.Diag(clang::diag::warn_drv_unused_argument) 65140b57cec5SDimitry Andric << StdArg->getAsString(Args); 65150b57cec5SDimitry Andric } 65160b57cec5SDimitry Andric 65170b57cec5SDimitry Andric if (LanguageStandard.empty()) { 65180b57cec5SDimitry Andric if (IsMSVC2015Compatible) 65190b57cec5SDimitry Andric LanguageStandard = "-std=c++14"; 65200b57cec5SDimitry Andric else 65210b57cec5SDimitry Andric LanguageStandard = "-std=c++11"; 65220b57cec5SDimitry Andric } 65230b57cec5SDimitry Andric 65240b57cec5SDimitry Andric CmdArgs.push_back(LanguageStandard.data()); 65250b57cec5SDimitry Andric } 65260b57cec5SDimitry Andric 652781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fborland_extensions, 652881ad6265SDimitry Andric options::OPT_fno_borland_extensions); 65290b57cec5SDimitry Andric 653081ad6265SDimitry Andric // -fno-declspec is default, except for PS4/PS5. 65310b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdeclspec, options::OPT_fno_declspec, 653281ad6265SDimitry Andric RawTriple.isPS())) 65330b57cec5SDimitry Andric CmdArgs.push_back("-fdeclspec"); 65340b57cec5SDimitry Andric else if (Args.hasArg(options::OPT_fno_declspec)) 65350b57cec5SDimitry Andric CmdArgs.push_back("-fno-declspec"); // Explicitly disabling __declspec. 65360b57cec5SDimitry Andric 65370b57cec5SDimitry Andric // -fthreadsafe-static is default, except for MSVC compatibility versions less 65380b57cec5SDimitry Andric // than 19. 65390b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 65400b57cec5SDimitry Andric options::OPT_fno_threadsafe_statics, 6541349cc55cSDimitry Andric !types::isOpenCL(InputType) && 6542349cc55cSDimitry Andric (!IsWindowsMSVC || IsMSVC2015Compatible))) 65430b57cec5SDimitry Andric CmdArgs.push_back("-fno-threadsafe-statics"); 65440b57cec5SDimitry Andric 65450b57cec5SDimitry Andric // -fno-delayed-template-parsing is default, except when targeting MSVC. 65460b57cec5SDimitry Andric // Many old Windows SDK versions require this to parse. 65470b57cec5SDimitry Andric // FIXME: MSVC introduced /Zc:twoPhase- to disable this behavior in their 65480b57cec5SDimitry Andric // compiler. We should be able to disable this by default at some point. 65490b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 65500b57cec5SDimitry Andric options::OPT_fno_delayed_template_parsing, IsWindowsMSVC)) 65510b57cec5SDimitry Andric CmdArgs.push_back("-fdelayed-template-parsing"); 65520b57cec5SDimitry Andric 65530b57cec5SDimitry Andric // -fgnu-keywords default varies depending on language; only pass if 65540b57cec5SDimitry Andric // specified. 65550b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgnu_keywords, 65560b57cec5SDimitry Andric options::OPT_fno_gnu_keywords); 65570b57cec5SDimitry Andric 655881ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fgnu89_inline, 655981ad6265SDimitry Andric options::OPT_fno_gnu89_inline); 65600b57cec5SDimitry Andric 656181ad6265SDimitry Andric const Arg *InlineArg = Args.getLastArg(options::OPT_finline_functions, 65620b57cec5SDimitry Andric options::OPT_finline_hint_functions, 65630b57cec5SDimitry Andric options::OPT_fno_inline_functions); 656481ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_finline, options::OPT_fno_inline)) { 656581ad6265SDimitry Andric if (A->getOption().matches(options::OPT_fno_inline)) 656681ad6265SDimitry Andric A->render(Args, CmdArgs); 656781ad6265SDimitry Andric } else if (InlineArg) { 656881ad6265SDimitry Andric InlineArg->render(Args, CmdArgs); 656981ad6265SDimitry Andric } 65700b57cec5SDimitry Andric 65710b57cec5SDimitry Andric // FIXME: Find a better way to determine whether the language has modules 65720b57cec5SDimitry Andric // support by default, or just assume that all languages do. 65730b57cec5SDimitry Andric bool HaveModules = 6574e8d8bef9SDimitry Andric Std && (Std->containsValue("c++2a") || Std->containsValue("c++20") || 6575e8d8bef9SDimitry Andric Std->containsValue("c++latest")); 65760b57cec5SDimitry Andric RenderModulesOptions(C, D, Args, Input, Output, CmdArgs, HaveModules); 65770b57cec5SDimitry Andric 6578a7dea167SDimitry Andric if (Args.hasFlag(options::OPT_fpch_validate_input_files_content, 6579a7dea167SDimitry Andric options::OPT_fno_pch_validate_input_files_content, false)) 6580a7dea167SDimitry Andric CmdArgs.push_back("-fvalidate-ast-input-files-content"); 65815ffd83dbSDimitry Andric if (Args.hasFlag(options::OPT_fpch_instantiate_templates, 65825ffd83dbSDimitry Andric options::OPT_fno_pch_instantiate_templates, false)) 65835ffd83dbSDimitry Andric CmdArgs.push_back("-fpch-instantiate-templates"); 6584590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen, 6585590d96feSDimitry Andric false)) 6586590d96feSDimitry Andric CmdArgs.push_back("-fmodules-codegen"); 6587590d96feSDimitry Andric if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo, 6588590d96feSDimitry Andric false)) 6589590d96feSDimitry Andric CmdArgs.push_back("-fmodules-debuginfo"); 6590a7dea167SDimitry Andric 659181ad6265SDimitry Andric if (!CLANG_ENABLE_OPAQUE_POINTERS_INTERNAL) 659281ad6265SDimitry Andric CmdArgs.push_back("-no-opaque-pointers"); 65930b57cec5SDimitry Andric 65945ffd83dbSDimitry Andric ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); 65950b57cec5SDimitry Andric RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, 65960b57cec5SDimitry Andric Input, CmdArgs); 65970b57cec5SDimitry Andric 6598fe6060f1SDimitry Andric if (types::isObjC(Input.getType()) && 6599fe6060f1SDimitry Andric Args.hasFlag(options::OPT_fobjc_encode_cxx_class_template_spec, 6600fe6060f1SDimitry Andric options::OPT_fno_objc_encode_cxx_class_template_spec, 6601fe6060f1SDimitry Andric !Runtime.isNeXTFamily())) 6602fe6060f1SDimitry Andric CmdArgs.push_back("-fobjc-encode-cxx-class-template-spec"); 6603fe6060f1SDimitry Andric 66040b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fapplication_extension, 66050b57cec5SDimitry Andric options::OPT_fno_application_extension, false)) 66060b57cec5SDimitry Andric CmdArgs.push_back("-fapplication-extension"); 66070b57cec5SDimitry Andric 66080b57cec5SDimitry Andric // Handle GCC-style exception args. 6609fe6060f1SDimitry Andric bool EH = false; 66100b57cec5SDimitry Andric if (!C.getDriver().IsCLMode()) 6611fe6060f1SDimitry Andric EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); 66120b57cec5SDimitry Andric 66130b57cec5SDimitry Andric // Handle exception personalities 6614a7dea167SDimitry Andric Arg *A = Args.getLastArg( 6615a7dea167SDimitry Andric options::OPT_fsjlj_exceptions, options::OPT_fseh_exceptions, 6616a7dea167SDimitry Andric options::OPT_fdwarf_exceptions, options::OPT_fwasm_exceptions); 66170b57cec5SDimitry Andric if (A) { 66180b57cec5SDimitry Andric const Option &Opt = A->getOption(); 66190b57cec5SDimitry Andric if (Opt.matches(options::OPT_fsjlj_exceptions)) 6620e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 66210b57cec5SDimitry Andric if (Opt.matches(options::OPT_fseh_exceptions)) 6622e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 66230b57cec5SDimitry Andric if (Opt.matches(options::OPT_fdwarf_exceptions)) 6624e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 6625a7dea167SDimitry Andric if (Opt.matches(options::OPT_fwasm_exceptions)) 6626e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=wasm"); 66270b57cec5SDimitry Andric } else { 66280b57cec5SDimitry Andric switch (TC.GetExceptionModel(Args)) { 66290b57cec5SDimitry Andric default: 66300b57cec5SDimitry Andric break; 66310b57cec5SDimitry Andric case llvm::ExceptionHandling::DwarfCFI: 6632e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=dwarf"); 66330b57cec5SDimitry Andric break; 66340b57cec5SDimitry Andric case llvm::ExceptionHandling::SjLj: 6635e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=sjlj"); 66360b57cec5SDimitry Andric break; 66370b57cec5SDimitry Andric case llvm::ExceptionHandling::WinEH: 6638e8d8bef9SDimitry Andric CmdArgs.push_back("-exception-model=seh"); 66390b57cec5SDimitry Andric break; 66400b57cec5SDimitry Andric } 66410b57cec5SDimitry Andric } 66420b57cec5SDimitry Andric 66430b57cec5SDimitry Andric // C++ "sane" operator new. 664481ad6265SDimitry Andric Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new, 664581ad6265SDimitry Andric options::OPT_fno_assume_sane_operator_new); 66460b57cec5SDimitry Andric 66470b57cec5SDimitry Andric // -frelaxed-template-template-args is off by default, as it is a severe 66480b57cec5SDimitry Andric // breaking change until a corresponding change to template partial ordering 66490b57cec5SDimitry Andric // is provided. 665081ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_frelaxed_template_template_args, 665181ad6265SDimitry Andric options::OPT_fno_relaxed_template_template_args); 66520b57cec5SDimitry Andric 66530b57cec5SDimitry Andric // -fsized-deallocation is off by default, as it is an ABI-breaking change for 66540b57cec5SDimitry Andric // most platforms. 665581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fsized_deallocation, 665681ad6265SDimitry Andric options::OPT_fno_sized_deallocation); 66570b57cec5SDimitry Andric 66580b57cec5SDimitry Andric // -faligned-allocation is on by default in C++17 onwards and otherwise off 66590b57cec5SDimitry Andric // by default. 66600b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_faligned_allocation, 66610b57cec5SDimitry Andric options::OPT_fno_aligned_allocation, 66620b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) { 66630b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fno_aligned_allocation)) 66640b57cec5SDimitry Andric CmdArgs.push_back("-fno-aligned-allocation"); 66650b57cec5SDimitry Andric else 66660b57cec5SDimitry Andric CmdArgs.push_back("-faligned-allocation"); 66670b57cec5SDimitry Andric } 66680b57cec5SDimitry Andric 66690b57cec5SDimitry Andric // The default new alignment can be specified using a dedicated option or via 66700b57cec5SDimitry Andric // a GCC-compatible option that also turns on aligned allocation. 66710b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fnew_alignment_EQ, 66720b57cec5SDimitry Andric options::OPT_faligned_new_EQ)) 66730b57cec5SDimitry Andric CmdArgs.push_back( 66740b57cec5SDimitry Andric Args.MakeArgString(Twine("-fnew-alignment=") + A->getValue())); 66750b57cec5SDimitry Andric 66760b57cec5SDimitry Andric // -fconstant-cfstrings is default, and may be subject to argument translation 66770b57cec5SDimitry Andric // on Darwin. 66780b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 667981ad6265SDimitry Andric options::OPT_fno_constant_cfstrings, true) || 66800b57cec5SDimitry Andric !Args.hasFlag(options::OPT_mconstant_cfstrings, 668181ad6265SDimitry Andric options::OPT_mno_constant_cfstrings, true)) 66820b57cec5SDimitry Andric CmdArgs.push_back("-fno-constant-cfstrings"); 66830b57cec5SDimitry Andric 668481ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fpascal_strings, 668581ad6265SDimitry Andric options::OPT_fno_pascal_strings); 66860b57cec5SDimitry Andric 66870b57cec5SDimitry Andric // Honor -fpack-struct= and -fpack-struct, if given. Note that 66880b57cec5SDimitry Andric // -fno-pack-struct doesn't apply to -fpack-struct=. 66890b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 66900b57cec5SDimitry Andric std::string PackStructStr = "-fpack-struct="; 66910b57cec5SDimitry Andric PackStructStr += A->getValue(); 66920b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 66930b57cec5SDimitry Andric } else if (Args.hasFlag(options::OPT_fpack_struct, 66940b57cec5SDimitry Andric options::OPT_fno_pack_struct, false)) { 66950b57cec5SDimitry Andric CmdArgs.push_back("-fpack-struct=1"); 66960b57cec5SDimitry Andric } 66970b57cec5SDimitry Andric 66980b57cec5SDimitry Andric // Handle -fmax-type-align=N and -fno-type-align 66990b57cec5SDimitry Andric bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align); 67000b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) { 67010b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 67020b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align="; 67030b57cec5SDimitry Andric MaxTypeAlignStr += A->getValue(); 67040b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 67050b57cec5SDimitry Andric } 67060b57cec5SDimitry Andric } else if (RawTriple.isOSDarwin()) { 67070b57cec5SDimitry Andric if (!SkipMaxTypeAlign) { 67080b57cec5SDimitry Andric std::string MaxTypeAlignStr = "-fmax-type-align=16"; 67090b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr)); 67100b57cec5SDimitry Andric } 67110b57cec5SDimitry Andric } 67120b57cec5SDimitry Andric 67130b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true)) 67140b57cec5SDimitry Andric CmdArgs.push_back("-Qn"); 67150b57cec5SDimitry Andric 67165ffd83dbSDimitry Andric // -fno-common is the default, set -fcommon only when that flag is set. 671781ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcommon, options::OPT_fno_common); 67180b57cec5SDimitry Andric 67190b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't yet support 67200b57cec5SDimitry Andric // -funsigned-bitfields. 67210b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fsigned_bitfields, 672281ad6265SDimitry Andric options::OPT_funsigned_bitfields, true)) 67230b57cec5SDimitry Andric D.Diag(diag::warn_drv_clang_unsupported) 67240b57cec5SDimitry Andric << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 67250b57cec5SDimitry Andric 67260b57cec5SDimitry Andric // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 672781ad6265SDimitry Andric if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope, true)) 67280b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) 67290b57cec5SDimitry Andric << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 67300b57cec5SDimitry Andric 67310b57cec5SDimitry Andric // -finput_charset=UTF-8 is default. Reject others 67320b57cec5SDimitry Andric if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) { 67330b57cec5SDimitry Andric StringRef value = inputCharset->getValue(); 6734fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 67350b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) 67360b57cec5SDimitry Andric << value; 67370b57cec5SDimitry Andric } 67380b57cec5SDimitry Andric 67390b57cec5SDimitry Andric // -fexec_charset=UTF-8 is default. Reject others 67400b57cec5SDimitry Andric if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) { 67410b57cec5SDimitry Andric StringRef value = execCharset->getValue(); 6742fe6060f1SDimitry Andric if (!value.equals_insensitive("utf-8")) 67430b57cec5SDimitry Andric D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) 67440b57cec5SDimitry Andric << value; 67450b57cec5SDimitry Andric } 67460b57cec5SDimitry Andric 67470b57cec5SDimitry Andric RenderDiagnosticsOptions(D, Args, CmdArgs); 67480b57cec5SDimitry Andric 674981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fasm_blocks, 675081ad6265SDimitry Andric options::OPT_fno_asm_blocks); 67510b57cec5SDimitry Andric 67520b57cec5SDimitry Andric // -fgnu-inline-asm is default. 67530b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fgnu_inline_asm, 67540b57cec5SDimitry Andric options::OPT_fno_gnu_inline_asm, true)) 67550b57cec5SDimitry Andric CmdArgs.push_back("-fno-gnu-inline-asm"); 67560b57cec5SDimitry Andric 67570b57cec5SDimitry Andric // Enable vectorization per default according to the optimization level 67580b57cec5SDimitry Andric // selected. For optimization levels that want vectorization we use the alias 67590b57cec5SDimitry Andric // option to simplify the hasFlag logic. 67600b57cec5SDimitry Andric bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false); 67610b57cec5SDimitry Andric OptSpecifier VectorizeAliasOption = 67620b57cec5SDimitry Andric EnableVec ? options::OPT_O_Group : options::OPT_fvectorize; 67630b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 67640b57cec5SDimitry Andric options::OPT_fno_vectorize, EnableVec)) 67650b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-loops"); 67660b57cec5SDimitry Andric 67670b57cec5SDimitry Andric // -fslp-vectorize is enabled based on the optimization level selected. 67680b57cec5SDimitry Andric bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true); 67690b57cec5SDimitry Andric OptSpecifier SLPVectAliasOption = 67700b57cec5SDimitry Andric EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize; 67710b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption, 67720b57cec5SDimitry Andric options::OPT_fno_slp_vectorize, EnableSLPVec)) 67730b57cec5SDimitry Andric CmdArgs.push_back("-vectorize-slp"); 67740b57cec5SDimitry Andric 67750b57cec5SDimitry Andric ParseMPreferVectorWidth(D, Args, CmdArgs); 67760b57cec5SDimitry Andric 67770b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ); 67780b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, 67790b57cec5SDimitry Andric options::OPT_fsanitize_undefined_strip_path_components_EQ); 67800b57cec5SDimitry Andric 67810b57cec5SDimitry Andric // -fdollars-in-identifiers default varies depending on platform and 67820b57cec5SDimitry Andric // language; only pass if specified. 67830b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 67840b57cec5SDimitry Andric options::OPT_fno_dollars_in_identifiers)) { 67850b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 67860b57cec5SDimitry Andric CmdArgs.push_back("-fdollars-in-identifiers"); 67870b57cec5SDimitry Andric else 67880b57cec5SDimitry Andric CmdArgs.push_back("-fno-dollars-in-identifiers"); 67890b57cec5SDimitry Andric } 67900b57cec5SDimitry Andric 679181ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fapple_pragma_pack, 679281ad6265SDimitry Andric options::OPT_fno_apple_pragma_pack); 67930b57cec5SDimitry Andric 6794e8d8bef9SDimitry Andric if (Args.hasFlag(options::OPT_fxl_pragma_pack, 6795e8d8bef9SDimitry Andric options::OPT_fno_xl_pragma_pack, RawTriple.isOSAIX())) 6796e8d8bef9SDimitry Andric CmdArgs.push_back("-fxl-pragma-pack"); 6797e8d8bef9SDimitry Andric 67980b57cec5SDimitry Andric // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. 6799480093f4SDimitry Andric if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple)) 6800480093f4SDimitry Andric renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA); 68010b57cec5SDimitry Andric 68020b57cec5SDimitry Andric bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports, 68030b57cec5SDimitry Andric options::OPT_fno_rewrite_imports, false); 68040b57cec5SDimitry Andric if (RewriteImports) 68050b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-imports"); 68060b57cec5SDimitry Andric 680781ad6265SDimitry Andric if (Args.hasFlag(options::OPT_fdirectives_only, 680881ad6265SDimitry Andric options::OPT_fno_directives_only, false)) 680981ad6265SDimitry Andric CmdArgs.push_back("-fdirectives-only"); 681081ad6265SDimitry Andric 68110b57cec5SDimitry Andric // Enable rewrite includes if the user's asked for it or if we're generating 68120b57cec5SDimitry Andric // diagnostics. 68130b57cec5SDimitry Andric // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be 68140b57cec5SDimitry Andric // nice to enable this when doing a crashdump for modules as well. 68150b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_frewrite_includes, 68160b57cec5SDimitry Andric options::OPT_fno_rewrite_includes, false) || 68170b57cec5SDimitry Andric (C.isForDiagnostics() && !HaveModules)) 68180b57cec5SDimitry Andric CmdArgs.push_back("-frewrite-includes"); 68190b57cec5SDimitry Andric 68200b57cec5SDimitry Andric // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 68210b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_traditional, 68220b57cec5SDimitry Andric options::OPT_traditional_cpp)) { 68230b57cec5SDimitry Andric if (isa<PreprocessJobAction>(JA)) 68240b57cec5SDimitry Andric CmdArgs.push_back("-traditional-cpp"); 68250b57cec5SDimitry Andric else 68260b57cec5SDimitry Andric D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 68270b57cec5SDimitry Andric } 68280b57cec5SDimitry Andric 68290b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dM); 68300b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dD); 683104eeddc0SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_dI); 68320b57cec5SDimitry Andric 68335ffd83dbSDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fmax_tokens_EQ); 68345ffd83dbSDimitry Andric 68350b57cec5SDimitry Andric // Handle serialized diagnostics. 68360b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 68370b57cec5SDimitry Andric CmdArgs.push_back("-serialize-diagnostic-file"); 68380b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A->getValue())); 68390b57cec5SDimitry Andric } 68400b57cec5SDimitry Andric 68410b57cec5SDimitry Andric if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 68420b57cec5SDimitry Andric CmdArgs.push_back("-fretain-comments-from-system-headers"); 68430b57cec5SDimitry Andric 68440b57cec5SDimitry Andric // Forward -fcomment-block-commands to -cc1. 68450b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 68460b57cec5SDimitry Andric // Forward -fparse-all-comments to -cc1. 68470b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 68480b57cec5SDimitry Andric 68490b57cec5SDimitry Andric // Turn -fplugin=name.so into -load name.so 68500b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_EQ)) { 68510b57cec5SDimitry Andric CmdArgs.push_back("-load"); 68520b57cec5SDimitry Andric CmdArgs.push_back(A->getValue()); 68530b57cec5SDimitry Andric A->claim(); 68540b57cec5SDimitry Andric } 68550b57cec5SDimitry Andric 68564824e7fdSDimitry Andric // Turn -fplugin-arg-pluginname-key=value into 68574824e7fdSDimitry Andric // -plugin-arg-pluginname key=value 68584824e7fdSDimitry Andric // GCC has an actual plugin_argument struct with key/value pairs that it 68594824e7fdSDimitry Andric // passes to its plugins, but we don't, so just pass it on as-is. 68604824e7fdSDimitry Andric // 68614824e7fdSDimitry Andric // The syntax for -fplugin-arg- is ambiguous if both plugin name and 68624824e7fdSDimitry Andric // argument key are allowed to contain dashes. GCC therefore only 68634824e7fdSDimitry Andric // allows dashes in the key. We do the same. 68644824e7fdSDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fplugin_arg)) { 68654824e7fdSDimitry Andric auto ArgValue = StringRef(A->getValue()); 68664824e7fdSDimitry Andric auto FirstDashIndex = ArgValue.find('-'); 68674824e7fdSDimitry Andric StringRef PluginName = ArgValue.substr(0, FirstDashIndex); 68684824e7fdSDimitry Andric StringRef Arg = ArgValue.substr(FirstDashIndex + 1); 68694824e7fdSDimitry Andric 68704824e7fdSDimitry Andric A->claim(); 68714824e7fdSDimitry Andric if (FirstDashIndex == StringRef::npos || Arg.empty()) { 68724824e7fdSDimitry Andric if (PluginName.empty()) { 68734824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_name) << A->getAsString(Args); 68744824e7fdSDimitry Andric } else { 68754824e7fdSDimitry Andric D.Diag(diag::warn_drv_missing_plugin_arg) 68764824e7fdSDimitry Andric << PluginName << A->getAsString(Args); 68774824e7fdSDimitry Andric } 68784824e7fdSDimitry Andric continue; 68794824e7fdSDimitry Andric } 68804824e7fdSDimitry Andric 68814824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-arg-") + PluginName)); 68824824e7fdSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Arg)); 68834824e7fdSDimitry Andric } 68844824e7fdSDimitry Andric 68850b57cec5SDimitry Andric // Forward -fpass-plugin=name.so to -cc1. 68860b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { 68870b57cec5SDimitry Andric CmdArgs.push_back( 68880b57cec5SDimitry Andric Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); 68890b57cec5SDimitry Andric A->claim(); 68900b57cec5SDimitry Andric } 68910b57cec5SDimitry Andric 68920b57cec5SDimitry Andric // Setup statistics file output. 68930b57cec5SDimitry Andric SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); 68940b57cec5SDimitry Andric if (!StatsFile.empty()) 68950b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile)); 68960b57cec5SDimitry Andric 68970b57cec5SDimitry Andric // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 68980b57cec5SDimitry Andric // parser. 68990b57cec5SDimitry Andric // -finclude-default-header flag is for preprocessor, 69000b57cec5SDimitry Andric // do not pass it to other cc1 commands when save-temps is enabled 69010b57cec5SDimitry Andric if (C.getDriver().isSaveTempsEnabled() && 69020b57cec5SDimitry Andric !isa<PreprocessJobAction>(JA)) { 69030b57cec5SDimitry Andric for (auto Arg : Args.filtered(options::OPT_Xclang)) { 69040b57cec5SDimitry Andric Arg->claim(); 69050b57cec5SDimitry Andric if (StringRef(Arg->getValue()) != "-finclude-default-header") 69060b57cec5SDimitry Andric CmdArgs.push_back(Arg->getValue()); 69070b57cec5SDimitry Andric } 69080b57cec5SDimitry Andric } 69090b57cec5SDimitry Andric else { 69100b57cec5SDimitry Andric Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); 69110b57cec5SDimitry Andric } 69120b57cec5SDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 69130b57cec5SDimitry Andric A->claim(); 69140b57cec5SDimitry Andric 69150b57cec5SDimitry Andric // We translate this by hand to the -cc1 argument, since nightly test uses 69160b57cec5SDimitry Andric // it and developers have been trained to spell it with -mllvm. Both 69170b57cec5SDimitry Andric // spellings are now deprecated and should be removed. 69180b57cec5SDimitry Andric if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") { 69190b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-optzns"); 69200b57cec5SDimitry Andric } else { 69210b57cec5SDimitry Andric A->render(Args, CmdArgs); 69220b57cec5SDimitry Andric } 69230b57cec5SDimitry Andric } 69240b57cec5SDimitry Andric 69250b57cec5SDimitry Andric // With -save-temps, we want to save the unoptimized bitcode output from the 69260b57cec5SDimitry Andric // CompileJobAction, use -disable-llvm-passes to get pristine IR generated 69270b57cec5SDimitry Andric // by the frontend. 69280b57cec5SDimitry Andric // When -fembed-bitcode is enabled, optimized bitcode is emitted because it 69290b57cec5SDimitry Andric // has slightly different breakdown between stages. 69300b57cec5SDimitry Andric // FIXME: -fembed-bitcode -save-temps will save optimized bitcode instead of 69310b57cec5SDimitry Andric // pristine IR generated by the frontend. Ideally, a new compile action should 69320b57cec5SDimitry Andric // be added so both IR can be captured. 69335ffd83dbSDimitry Andric if ((C.getDriver().isSaveTempsEnabled() || 69345ffd83dbSDimitry Andric JA.isHostOffloading(Action::OFK_OpenMP)) && 6935fe6060f1SDimitry Andric !(C.getDriver().embedBitcodeInObject() && !IsUsingLTO) && 69360b57cec5SDimitry Andric isa<CompileJobAction>(JA)) 69370b57cec5SDimitry Andric CmdArgs.push_back("-disable-llvm-passes"); 69380b57cec5SDimitry Andric 69390b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_undef); 69400b57cec5SDimitry Andric 69410b57cec5SDimitry Andric const char *Exec = D.getClangProgramPath(); 69420b57cec5SDimitry Andric 69430b57cec5SDimitry Andric // Optionally embed the -cc1 level arguments into the debug info or a 69440b57cec5SDimitry Andric // section, for build analysis. 69450b57cec5SDimitry Andric // Also record command line arguments into the debug info if 69460b57cec5SDimitry Andric // -grecord-gcc-switches options is set on. 69470b57cec5SDimitry Andric // By default, -gno-record-gcc-switches is set on and no recording. 69480b57cec5SDimitry Andric auto GRecordSwitches = 69490b57cec5SDimitry Andric Args.hasFlag(options::OPT_grecord_command_line, 69500b57cec5SDimitry Andric options::OPT_gno_record_command_line, false); 69510b57cec5SDimitry Andric auto FRecordSwitches = 69520b57cec5SDimitry Andric Args.hasFlag(options::OPT_frecord_command_line, 69530b57cec5SDimitry Andric options::OPT_fno_record_command_line, false); 69540b57cec5SDimitry Andric if (FRecordSwitches && !Triple.isOSBinFormatELF()) 69550b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 69560b57cec5SDimitry Andric << Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args) 69570b57cec5SDimitry Andric << TripleStr; 69580b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) { 69590b57cec5SDimitry Andric ArgStringList OriginalArgs; 69600b57cec5SDimitry Andric for (const auto &Arg : Args) 69610b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 69620b57cec5SDimitry Andric 69630b57cec5SDimitry Andric SmallString<256> Flags; 69645ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 69650b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 69660b57cec5SDimitry Andric SmallString<128> EscapedArg; 69670b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 69680b57cec5SDimitry Andric Flags += " "; 69690b57cec5SDimitry Andric Flags += EscapedArg; 69700b57cec5SDimitry Andric } 69710b57cec5SDimitry Andric auto FlagsArgString = Args.MakeArgString(Flags); 69720b57cec5SDimitry Andric if (TC.UseDwarfDebugFlags() || GRecordSwitches) { 69730b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 69740b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 69750b57cec5SDimitry Andric } 69760b57cec5SDimitry Andric if (FRecordSwitches) { 69770b57cec5SDimitry Andric CmdArgs.push_back("-record-command-line"); 69780b57cec5SDimitry Andric CmdArgs.push_back(FlagsArgString); 69790b57cec5SDimitry Andric } 69800b57cec5SDimitry Andric } 69810b57cec5SDimitry Andric 6982753f127fSDimitry Andric // Host-side offloading compilation receives all device-side outputs. Include 6983753f127fSDimitry Andric // them in the host compilation depending on the target. If the host inputs 6984753f127fSDimitry Andric // are not empty we use the new-driver scheme, otherwise use the old scheme. 69850b57cec5SDimitry Andric if ((IsCuda || IsHIP) && CudaDeviceInput) { 69860b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 69870b57cec5SDimitry Andric CmdArgs.push_back(CudaDeviceInput->getFilename()); 6988753f127fSDimitry Andric } else if (!HostOffloadingInputs.empty()) { 6989753f127fSDimitry Andric if (IsCuda && !IsRDCMode) { 6990753f127fSDimitry Andric assert(HostOffloadingInputs.size() == 1 && "Only one input expected"); 6991753f127fSDimitry Andric CmdArgs.push_back("-fcuda-include-gpubinary"); 6992753f127fSDimitry Andric CmdArgs.push_back(HostOffloadingInputs.front().getFilename()); 6993753f127fSDimitry Andric } else { 6994753f127fSDimitry Andric for (const InputInfo Input : HostOffloadingInputs) 6995753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("-fembed-offload-object=" + 6996753f127fSDimitry Andric TC.getInputFilename(Input))); 6997753f127fSDimitry Andric } 69980b57cec5SDimitry Andric } 69990b57cec5SDimitry Andric 70000b57cec5SDimitry Andric if (IsCuda) { 70010b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_fcuda_short_ptr, 70020b57cec5SDimitry Andric options::OPT_fno_cuda_short_ptr, false)) 70030b57cec5SDimitry Andric CmdArgs.push_back("-fcuda-short-ptr"); 70040b57cec5SDimitry Andric } 70050b57cec5SDimitry Andric 7006fe6060f1SDimitry Andric if (IsCuda || IsHIP) { 7007fe6060f1SDimitry Andric // Determine the original source input. 7008fe6060f1SDimitry Andric const Action *SourceAction = &JA; 7009fe6060f1SDimitry Andric while (SourceAction->getKind() != Action::InputClass) { 7010fe6060f1SDimitry Andric assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 7011fe6060f1SDimitry Andric SourceAction = SourceAction->getInputs()[0]; 7012fe6060f1SDimitry Andric } 7013fe6060f1SDimitry Andric auto CUID = cast<InputAction>(SourceAction)->getId(); 7014fe6060f1SDimitry Andric if (!CUID.empty()) 7015fe6060f1SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); 7016fe6060f1SDimitry Andric } 7017fe6060f1SDimitry Andric 701881ad6265SDimitry Andric if (IsHIP) { 7019480093f4SDimitry Andric CmdArgs.push_back("-fcuda-allow-variadic-functions"); 702081ad6265SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_fgpu_default_stream_EQ); 702181ad6265SDimitry Andric } 7022480093f4SDimitry Andric 7023fe6060f1SDimitry Andric if (IsCudaDevice || IsHIPDevice) { 7024fe6060f1SDimitry Andric StringRef InlineThresh = 7025fe6060f1SDimitry Andric Args.getLastArgValue(options::OPT_fgpu_inline_threshold_EQ); 7026fe6060f1SDimitry Andric if (!InlineThresh.empty()) { 7027fe6060f1SDimitry Andric std::string ArgStr = 7028fe6060f1SDimitry Andric std::string("-inline-threshold=") + InlineThresh.str(); 7029fe6060f1SDimitry Andric CmdArgs.append({"-mllvm", Args.MakeArgStringRef(ArgStr)}); 7030fe6060f1SDimitry Andric } 7031fe6060f1SDimitry Andric } 7032fe6060f1SDimitry Andric 70330b57cec5SDimitry Andric // OpenMP offloading device jobs take the argument -fopenmp-host-ir-file-path 70340b57cec5SDimitry Andric // to specify the result of the compile phase on the host, so the meaningful 70350b57cec5SDimitry Andric // device declarations can be identified. Also, -fopenmp-is-device is passed 70360b57cec5SDimitry Andric // along to tell the frontend that it is generating code for a device, so that 70370b57cec5SDimitry Andric // only the relevant declarations are emitted. 70380b57cec5SDimitry Andric if (IsOpenMPDevice) { 70390b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-is-device"); 70400b57cec5SDimitry Andric if (OpenMPDeviceInput) { 70410b57cec5SDimitry Andric CmdArgs.push_back("-fopenmp-host-ir-file-path"); 70420b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OpenMPDeviceInput->getFilename())); 70430b57cec5SDimitry Andric } 70440b57cec5SDimitry Andric } 70450b57cec5SDimitry Andric 7046e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) { 7047e8d8bef9SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); 7048e8d8bef9SDimitry Andric 704981ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_munsafe_fp_atomics, 705081ad6265SDimitry Andric options::OPT_mno_unsafe_fp_atomics); 7051e8d8bef9SDimitry Andric } 7052e8d8bef9SDimitry Andric 70530b57cec5SDimitry Andric // For all the host OpenMP offloading compile jobs we need to pass the targets 70540b57cec5SDimitry Andric // information using -fopenmp-targets= option. 70550b57cec5SDimitry Andric if (JA.isHostOffloading(Action::OFK_OpenMP)) { 705681ad6265SDimitry Andric SmallString<128> Targets("-fopenmp-targets="); 70570b57cec5SDimitry Andric 705881ad6265SDimitry Andric SmallVector<std::string, 4> Triples; 705981ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); 706081ad6265SDimitry Andric std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples), 706181ad6265SDimitry Andric [](auto TC) { return TC.second->getTripleString(); }); 706281ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ","))); 70630b57cec5SDimitry Andric } 70640b57cec5SDimitry Andric 7065a7dea167SDimitry Andric bool VirtualFunctionElimination = 7066a7dea167SDimitry Andric Args.hasFlag(options::OPT_fvirtual_function_elimination, 7067a7dea167SDimitry Andric options::OPT_fno_virtual_function_elimination, false); 7068a7dea167SDimitry Andric if (VirtualFunctionElimination) { 7069a7dea167SDimitry Andric // VFE requires full LTO (currently, this might be relaxed to allow ThinLTO 7070a7dea167SDimitry Andric // in the future). 7071fe6060f1SDimitry Andric if (LTOMode != LTOK_Full) 7072a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 7073a7dea167SDimitry Andric << "-fvirtual-function-elimination" 7074a7dea167SDimitry Andric << "-flto=full"; 7075a7dea167SDimitry Andric 7076a7dea167SDimitry Andric CmdArgs.push_back("-fvirtual-function-elimination"); 7077a7dea167SDimitry Andric } 7078a7dea167SDimitry Andric 7079a7dea167SDimitry Andric // VFE requires whole-program-vtables, and enables it by default. 7080a7dea167SDimitry Andric bool WholeProgramVTables = Args.hasFlag( 7081a7dea167SDimitry Andric options::OPT_fwhole_program_vtables, 7082a7dea167SDimitry Andric options::OPT_fno_whole_program_vtables, VirtualFunctionElimination); 7083a7dea167SDimitry Andric if (VirtualFunctionElimination && !WholeProgramVTables) { 7084a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 7085a7dea167SDimitry Andric << "-fno-whole-program-vtables" 7086a7dea167SDimitry Andric << "-fvirtual-function-elimination"; 7087a7dea167SDimitry Andric } 7088a7dea167SDimitry Andric 70890b57cec5SDimitry Andric if (WholeProgramVTables) { 7090fe6060f1SDimitry Andric // Propagate -fwhole-program-vtables if this is an LTO compile. 7091fe6060f1SDimitry Andric if (IsUsingLTO) 7092fe6060f1SDimitry Andric CmdArgs.push_back("-fwhole-program-vtables"); 7093fe6060f1SDimitry Andric // Check if we passed LTO options but they were suppressed because this is a 7094fe6060f1SDimitry Andric // device offloading action, or we passed device offload LTO options which 7095fe6060f1SDimitry Andric // were suppressed because this is not the device offload action. 7096fe6060f1SDimitry Andric // Otherwise, issue an error. 7097fe6060f1SDimitry Andric else if (!D.isUsingLTO(!IsDeviceOffloadAction)) 70980b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_only_allowed_with) 70990b57cec5SDimitry Andric << "-fwhole-program-vtables" 71000b57cec5SDimitry Andric << "-flto"; 71010b57cec5SDimitry Andric } 71020b57cec5SDimitry Andric 7103480093f4SDimitry Andric bool DefaultsSplitLTOUnit = 7104349cc55cSDimitry Andric (WholeProgramVTables || SanitizeArgs.needsLTO()) && 7105fe6060f1SDimitry Andric (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit()); 71060b57cec5SDimitry Andric bool SplitLTOUnit = 71070b57cec5SDimitry Andric Args.hasFlag(options::OPT_fsplit_lto_unit, 7108a7dea167SDimitry Andric options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); 7109349cc55cSDimitry Andric if (SanitizeArgs.needsLTO() && !SplitLTOUnit) 7110a7dea167SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" 7111a7dea167SDimitry Andric << "-fsanitize=cfi"; 71120b57cec5SDimitry Andric if (SplitLTOUnit) 71130b57cec5SDimitry Andric CmdArgs.push_back("-fsplit-lto-unit"); 71140b57cec5SDimitry Andric 71155ffd83dbSDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, 71165ffd83dbSDimitry Andric options::OPT_fno_global_isel)) { 71170b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 71185ffd83dbSDimitry Andric if (A->getOption().matches(options::OPT_fglobal_isel)) { 71190b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=1"); 71200b57cec5SDimitry Andric 71210b57cec5SDimitry Andric // GISel is on by default on AArch64 -O0, so don't bother adding 71220b57cec5SDimitry Andric // the fallback remarks for it. Other combinations will add a warning of 71230b57cec5SDimitry Andric // some kind. 71240b57cec5SDimitry Andric bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64; 71250b57cec5SDimitry Andric bool IsOptLevelSupported = false; 71260b57cec5SDimitry Andric 71270b57cec5SDimitry Andric Arg *A = Args.getLastArg(options::OPT_O_Group); 71280b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::aarch64) { 71290b57cec5SDimitry Andric if (!A || A->getOption().matches(options::OPT_O0)) 71300b57cec5SDimitry Andric IsOptLevelSupported = true; 71310b57cec5SDimitry Andric } 71320b57cec5SDimitry Andric if (!IsArchSupported || !IsOptLevelSupported) { 71330b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 71340b57cec5SDimitry Andric CmdArgs.push_back("-global-isel-abort=2"); 71350b57cec5SDimitry Andric 71360b57cec5SDimitry Andric if (!IsArchSupported) 71375ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete) << Triple.getArchName(); 71380b57cec5SDimitry Andric else 71395ffd83dbSDimitry Andric D.Diag(diag::warn_drv_global_isel_incomplete_opt); 71400b57cec5SDimitry Andric } 71410b57cec5SDimitry Andric } else { 71420b57cec5SDimitry Andric CmdArgs.push_back("-global-isel=0"); 71430b57cec5SDimitry Andric } 71440b57cec5SDimitry Andric } 71450b57cec5SDimitry Andric 71460b57cec5SDimitry Andric if (Args.hasArg(options::OPT_forder_file_instrumentation)) { 71470b57cec5SDimitry Andric CmdArgs.push_back("-forder-file-instrumentation"); 71480b57cec5SDimitry Andric // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is 71490b57cec5SDimitry Andric // on, we need to pass these flags as linker flags and that will be handled 71500b57cec5SDimitry Andric // outside of the compiler. 7151fe6060f1SDimitry Andric if (!IsUsingLTO) { 71520b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 71530b57cec5SDimitry Andric CmdArgs.push_back("-enable-order-file-instrumentation"); 71540b57cec5SDimitry Andric } 71550b57cec5SDimitry Andric } 71560b57cec5SDimitry Andric 71570b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fforce_enable_int128, 71580b57cec5SDimitry Andric options::OPT_fno_force_enable_int128)) { 71590b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_fforce_enable_int128)) 71600b57cec5SDimitry Andric CmdArgs.push_back("-fforce-enable-int128"); 71610b57cec5SDimitry Andric } 71620b57cec5SDimitry Andric 716381ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_consts, 716481ad6265SDimitry Andric options::OPT_fno_keep_static_consts); 716581ad6265SDimitry Andric Args.addOptInFlag(CmdArgs, options::OPT_fcomplete_member_pointers, 716681ad6265SDimitry Andric options::OPT_fno_complete_member_pointers); 71670b57cec5SDimitry Andric 71680b57cec5SDimitry Andric if (!Args.hasFlag(options::OPT_fcxx_static_destructors, 71690b57cec5SDimitry Andric options::OPT_fno_cxx_static_destructors, true)) 71700b57cec5SDimitry Andric CmdArgs.push_back("-fno-c++-static-destructors"); 71710b57cec5SDimitry Andric 7172e8d8bef9SDimitry Andric addMachineOutlinerArgs(D, Args, CmdArgs, Triple, /*IsLTO=*/false); 7173e8d8bef9SDimitry Andric 7174e8d8bef9SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics, 7175e8d8bef9SDimitry Andric options::OPT_mno_outline_atomics)) { 7176e8d8bef9SDimitry Andric // Option -moutline-atomics supported for AArch64 target only. 7177e8d8bef9SDimitry Andric if (!Triple.isAArch64()) { 7178e8d8bef9SDimitry Andric D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt) 71790eae32dcSDimitry Andric << Triple.getArchName() << A->getOption().getName(); 71800b57cec5SDimitry Andric } else { 71810eae32dcSDimitry Andric if (A->getOption().matches(options::OPT_moutline_atomics)) { 7182e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7183e8d8bef9SDimitry Andric CmdArgs.push_back("+outline-atomics"); 71840b57cec5SDimitry Andric } else { 7185e8d8bef9SDimitry Andric CmdArgs.push_back("-target-feature"); 7186e8d8bef9SDimitry Andric CmdArgs.push_back("-outline-atomics"); 71870b57cec5SDimitry Andric } 71880eae32dcSDimitry Andric } 7189fe6060f1SDimitry Andric } else if (Triple.isAArch64() && 7190fe6060f1SDimitry Andric getToolChain().IsAArch64OutlineAtomicsDefault(Args)) { 7191fe6060f1SDimitry Andric CmdArgs.push_back("-target-feature"); 7192fe6060f1SDimitry Andric CmdArgs.push_back("+outline-atomics"); 71930b57cec5SDimitry Andric } 71940b57cec5SDimitry Andric 71950b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_faddrsig, options::OPT_fno_addrsig, 71960b57cec5SDimitry Andric (TC.getTriple().isOSBinFormatELF() || 71970b57cec5SDimitry Andric TC.getTriple().isOSBinFormatCOFF()) && 7198e8d8bef9SDimitry Andric !TC.getTriple().isPS4() && !TC.getTriple().isVE() && 71990b57cec5SDimitry Andric !TC.getTriple().isOSNetBSD() && 7200480093f4SDimitry Andric !Distro(D.getVFS(), TC.getTriple()).IsGentoo() && 7201e8d8bef9SDimitry Andric !TC.getTriple().isAndroid() && TC.useIntegratedAs())) 72020b57cec5SDimitry Andric CmdArgs.push_back("-faddrsig"); 72030b57cec5SDimitry Andric 7204fe6060f1SDimitry Andric if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && 7205349cc55cSDimitry Andric (EH || AsyncUnwindTables || UnwindTables || 7206349cc55cSDimitry Andric DebugInfoKind != codegenoptions::NoDebugInfo)) 7207fe6060f1SDimitry Andric CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); 7208fe6060f1SDimitry Andric 72090b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { 72100b57cec5SDimitry Andric std::string Str = A->getAsString(Args); 72110b57cec5SDimitry Andric if (!TC.getTriple().isOSBinFormatELF()) 72120b57cec5SDimitry Andric D.Diag(diag::err_drv_unsupported_opt_for_target) 72130b57cec5SDimitry Andric << Str << TC.getTripleString(); 72140b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Str)); 72150b57cec5SDimitry Andric } 72160b57cec5SDimitry Andric 72170eae32dcSDimitry Andric // Add the output path to the object file for CodeView debug infos. 72180eae32dcSDimitry Andric if (EmitCodeView && Output.isFilename()) 72190eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 72200eae32dcSDimitry Andric Output.getFilename()); 72210eae32dcSDimitry Andric 72220b57cec5SDimitry Andric // Add the "-o out -x type src.c" flags last. This is done primarily to make 72230b57cec5SDimitry Andric // the -cc1 command easier to edit when reproducing compiler crashes. 72240b57cec5SDimitry Andric if (Output.getType() == types::TY_Dependencies) { 72250b57cec5SDimitry Andric // Handled with other dependency code. 72260b57cec5SDimitry Andric } else if (Output.isFilename()) { 7227480093f4SDimitry Andric if (Output.getType() == clang::driver::types::TY_IFS_CPP || 7228480093f4SDimitry Andric Output.getType() == clang::driver::types::TY_IFS) { 7229480093f4SDimitry Andric SmallString<128> OutputFilename(Output.getFilename()); 7230480093f4SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, "ifs"); 7231480093f4SDimitry Andric CmdArgs.push_back("-o"); 7232480093f4SDimitry Andric CmdArgs.push_back(Args.MakeArgString(OutputFilename)); 7233480093f4SDimitry Andric } else { 72340b57cec5SDimitry Andric CmdArgs.push_back("-o"); 72350b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 7236480093f4SDimitry Andric } 72370b57cec5SDimitry Andric } else { 72380b57cec5SDimitry Andric assert(Output.isNothing() && "Invalid output."); 72390b57cec5SDimitry Andric } 72400b57cec5SDimitry Andric 72410b57cec5SDimitry Andric addDashXForInput(Args, Input, CmdArgs); 72420b57cec5SDimitry Andric 72430b57cec5SDimitry Andric ArrayRef<InputInfo> FrontendInputs = Input; 72440b57cec5SDimitry Andric if (IsHeaderModulePrecompile) 72450b57cec5SDimitry Andric FrontendInputs = ModuleHeaderInputs; 724681ad6265SDimitry Andric else if (IsExtractAPI) 724781ad6265SDimitry Andric FrontendInputs = ExtractAPIInputs; 72480b57cec5SDimitry Andric else if (Input.isNothing()) 72490b57cec5SDimitry Andric FrontendInputs = {}; 72500b57cec5SDimitry Andric 72510b57cec5SDimitry Andric for (const InputInfo &Input : FrontendInputs) { 72520b57cec5SDimitry Andric if (Input.isFilename()) 72530b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 72540b57cec5SDimitry Andric else 72550b57cec5SDimitry Andric Input.getInputArg().renderAsInput(Args, CmdArgs); 72560b57cec5SDimitry Andric } 72570b57cec5SDimitry Andric 7258fe6060f1SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 7259480093f4SDimitry Andric // Invoke the CC1 directly in this process 7260e8d8bef9SDimitry Andric C.addCommand(std::make_unique<CC1Command>(JA, *this, 7261e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7262e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 72630b57cec5SDimitry Andric } else { 7264e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 7265e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 7266e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 72670b57cec5SDimitry Andric } 72680b57cec5SDimitry Andric 72690b57cec5SDimitry Andric // Make the compile command echo its inputs for /showFilenames. 72700b57cec5SDimitry Andric if (Output.getType() == types::TY_Object && 72710b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_showFilenames, 72720b57cec5SDimitry Andric options::OPT__SLASH_showFilenames_, false)) { 727313138422SDimitry Andric C.getJobs().getJobs().back()->PrintInputFilenames = true; 72740b57cec5SDimitry Andric } 72750b57cec5SDimitry Andric 72760b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_pg)) 72775ffd83dbSDimitry Andric if (FPKeepKind == CodeGenOptions::FramePointerKind::None && 72785ffd83dbSDimitry Andric !Args.hasArg(options::OPT_mfentry)) 72790b57cec5SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer" 72800b57cec5SDimitry Andric << A->getAsString(Args); 72810b57cec5SDimitry Andric 72820b57cec5SDimitry Andric // Claim some arguments which clang supports automatically. 72830b57cec5SDimitry Andric 72840b57cec5SDimitry Andric // -fpch-preprocess is used with gcc to add a special marker in the output to 72850b57cec5SDimitry Andric // include the PCH file. 72860b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_fpch_preprocess); 72870b57cec5SDimitry Andric 72880b57cec5SDimitry Andric // Claim some arguments which clang doesn't support, but we don't 72890b57cec5SDimitry Andric // care to warn the user about. 72900b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 72910b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 72920b57cec5SDimitry Andric 72930b57cec5SDimitry Andric // Disable warnings for clang -E -emit-llvm foo.c 72940b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 72950b57cec5SDimitry Andric } 72960b57cec5SDimitry Andric 72970eae32dcSDimitry Andric Clang::Clang(const ToolChain &TC, bool HasIntegratedBackend) 72980b57cec5SDimitry Andric // CAUTION! The first constructor argument ("clang") is not arbitrary, 72990b57cec5SDimitry Andric // as it is for other tools. Some operations on a Tool actually test 73000b57cec5SDimitry Andric // whether that tool is Clang based on the Tool's Name as a string. 73010eae32dcSDimitry Andric : Tool("clang", "clang frontend", TC), HasBackend(HasIntegratedBackend) {} 73020b57cec5SDimitry Andric 73030b57cec5SDimitry Andric Clang::~Clang() {} 73040b57cec5SDimitry Andric 73050b57cec5SDimitry Andric /// Add options related to the Objective-C runtime/ABI. 73060b57cec5SDimitry Andric /// 73070b57cec5SDimitry Andric /// Returns true if the runtime is non-fragile. 73080b57cec5SDimitry Andric ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 73095ffd83dbSDimitry Andric const InputInfoList &inputs, 73100b57cec5SDimitry Andric ArgStringList &cmdArgs, 73110b57cec5SDimitry Andric RewriteKind rewriteKind) const { 73120b57cec5SDimitry Andric // Look for the controlling runtime option. 73130b57cec5SDimitry Andric Arg *runtimeArg = 73140b57cec5SDimitry Andric args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime, 73150b57cec5SDimitry Andric options::OPT_fobjc_runtime_EQ); 73160b57cec5SDimitry Andric 73170b57cec5SDimitry Andric // Just forward -fobjc-runtime= to the frontend. This supercedes 73180b57cec5SDimitry Andric // options about fragility. 73190b57cec5SDimitry Andric if (runtimeArg && 73200b57cec5SDimitry Andric runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 73210b57cec5SDimitry Andric ObjCRuntime runtime; 73220b57cec5SDimitry Andric StringRef value = runtimeArg->getValue(); 73230b57cec5SDimitry Andric if (runtime.tryParse(value)) { 73240b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 73250b57cec5SDimitry Andric << value; 73260b57cec5SDimitry Andric } 73270b57cec5SDimitry Andric if ((runtime.getKind() == ObjCRuntime::GNUstep) && 73280b57cec5SDimitry Andric (runtime.getVersion() >= VersionTuple(2, 0))) 73290b57cec5SDimitry Andric if (!getToolChain().getTriple().isOSBinFormatELF() && 73300b57cec5SDimitry Andric !getToolChain().getTriple().isOSBinFormatCOFF()) { 73310b57cec5SDimitry Andric getToolChain().getDriver().Diag( 73320b57cec5SDimitry Andric diag::err_drv_gnustep_objc_runtime_incompatible_binary) 73330b57cec5SDimitry Andric << runtime.getVersion().getMajor(); 73340b57cec5SDimitry Andric } 73350b57cec5SDimitry Andric 73360b57cec5SDimitry Andric runtimeArg->render(args, cmdArgs); 73370b57cec5SDimitry Andric return runtime; 73380b57cec5SDimitry Andric } 73390b57cec5SDimitry Andric 73400b57cec5SDimitry Andric // Otherwise, we'll need the ABI "version". Version numbers are 73410b57cec5SDimitry Andric // slightly confusing for historical reasons: 73420b57cec5SDimitry Andric // 1 - Traditional "fragile" ABI 73430b57cec5SDimitry Andric // 2 - Non-fragile ABI, version 1 73440b57cec5SDimitry Andric // 3 - Non-fragile ABI, version 2 73450b57cec5SDimitry Andric unsigned objcABIVersion = 1; 73460b57cec5SDimitry Andric // If -fobjc-abi-version= is present, use that to set the version. 73470b57cec5SDimitry Andric if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 73480b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 73490b57cec5SDimitry Andric if (value == "1") 73500b57cec5SDimitry Andric objcABIVersion = 1; 73510b57cec5SDimitry Andric else if (value == "2") 73520b57cec5SDimitry Andric objcABIVersion = 2; 73530b57cec5SDimitry Andric else if (value == "3") 73540b57cec5SDimitry Andric objcABIVersion = 3; 73550b57cec5SDimitry Andric else 73560b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value; 73570b57cec5SDimitry Andric } else { 73580b57cec5SDimitry Andric // Otherwise, determine if we are using the non-fragile ABI. 73590b57cec5SDimitry Andric bool nonFragileABIIsDefault = 73600b57cec5SDimitry Andric (rewriteKind == RK_NonFragile || 73610b57cec5SDimitry Andric (rewriteKind == RK_None && 73620b57cec5SDimitry Andric getToolChain().IsObjCNonFragileABIDefault())); 73630b57cec5SDimitry Andric if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 73640b57cec5SDimitry Andric options::OPT_fno_objc_nonfragile_abi, 73650b57cec5SDimitry Andric nonFragileABIIsDefault)) { 73660b57cec5SDimitry Andric // Determine the non-fragile ABI version to use. 73670b57cec5SDimitry Andric #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 73680b57cec5SDimitry Andric unsigned nonFragileABIVersion = 1; 73690b57cec5SDimitry Andric #else 73700b57cec5SDimitry Andric unsigned nonFragileABIVersion = 2; 73710b57cec5SDimitry Andric #endif 73720b57cec5SDimitry Andric 73730b57cec5SDimitry Andric if (Arg *abiArg = 73740b57cec5SDimitry Andric args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) { 73750b57cec5SDimitry Andric StringRef value = abiArg->getValue(); 73760b57cec5SDimitry Andric if (value == "1") 73770b57cec5SDimitry Andric nonFragileABIVersion = 1; 73780b57cec5SDimitry Andric else if (value == "2") 73790b57cec5SDimitry Andric nonFragileABIVersion = 2; 73800b57cec5SDimitry Andric else 73810b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 73820b57cec5SDimitry Andric << value; 73830b57cec5SDimitry Andric } 73840b57cec5SDimitry Andric 73850b57cec5SDimitry Andric objcABIVersion = 1 + nonFragileABIVersion; 73860b57cec5SDimitry Andric } else { 73870b57cec5SDimitry Andric objcABIVersion = 1; 73880b57cec5SDimitry Andric } 73890b57cec5SDimitry Andric } 73900b57cec5SDimitry Andric 73910b57cec5SDimitry Andric // We don't actually care about the ABI version other than whether 73920b57cec5SDimitry Andric // it's non-fragile. 73930b57cec5SDimitry Andric bool isNonFragile = objcABIVersion != 1; 73940b57cec5SDimitry Andric 73950b57cec5SDimitry Andric // If we have no runtime argument, ask the toolchain for its default runtime. 73960b57cec5SDimitry Andric // However, the rewriter only really supports the Mac runtime, so assume that. 73970b57cec5SDimitry Andric ObjCRuntime runtime; 73980b57cec5SDimitry Andric if (!runtimeArg) { 73990b57cec5SDimitry Andric switch (rewriteKind) { 74000b57cec5SDimitry Andric case RK_None: 74010b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 74020b57cec5SDimitry Andric break; 74030b57cec5SDimitry Andric case RK_Fragile: 74040b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 74050b57cec5SDimitry Andric break; 74060b57cec5SDimitry Andric case RK_NonFragile: 74070b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 74080b57cec5SDimitry Andric break; 74090b57cec5SDimitry Andric } 74100b57cec5SDimitry Andric 74110b57cec5SDimitry Andric // -fnext-runtime 74120b57cec5SDimitry Andric } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 74130b57cec5SDimitry Andric // On Darwin, make this use the default behavior for the toolchain. 74140b57cec5SDimitry Andric if (getToolChain().getTriple().isOSDarwin()) { 74150b57cec5SDimitry Andric runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 74160b57cec5SDimitry Andric 74170b57cec5SDimitry Andric // Otherwise, build for a generic macosx port. 74180b57cec5SDimitry Andric } else { 74190b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 74200b57cec5SDimitry Andric } 74210b57cec5SDimitry Andric 74220b57cec5SDimitry Andric // -fgnu-runtime 74230b57cec5SDimitry Andric } else { 74240b57cec5SDimitry Andric assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 74250b57cec5SDimitry Andric // Legacy behaviour is to target the gnustep runtime if we are in 74260b57cec5SDimitry Andric // non-fragile mode or the GCC runtime in fragile mode. 74270b57cec5SDimitry Andric if (isNonFragile) 74280b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(2, 0)); 74290b57cec5SDimitry Andric else 74300b57cec5SDimitry Andric runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 74310b57cec5SDimitry Andric } 74320b57cec5SDimitry Andric 74335ffd83dbSDimitry Andric if (llvm::any_of(inputs, [](const InputInfo &input) { 74345ffd83dbSDimitry Andric return types::isObjC(input.getType()); 74355ffd83dbSDimitry Andric })) 74360b57cec5SDimitry Andric cmdArgs.push_back( 74370b57cec5SDimitry Andric args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); 74380b57cec5SDimitry Andric return runtime; 74390b57cec5SDimitry Andric } 74400b57cec5SDimitry Andric 74410b57cec5SDimitry Andric static bool maybeConsumeDash(const std::string &EH, size_t &I) { 74420b57cec5SDimitry Andric bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-'); 74430b57cec5SDimitry Andric I += HaveDash; 74440b57cec5SDimitry Andric return !HaveDash; 74450b57cec5SDimitry Andric } 74460b57cec5SDimitry Andric 74470b57cec5SDimitry Andric namespace { 74480b57cec5SDimitry Andric struct EHFlags { 74490b57cec5SDimitry Andric bool Synch = false; 74500b57cec5SDimitry Andric bool Asynch = false; 74510b57cec5SDimitry Andric bool NoUnwindC = false; 74520b57cec5SDimitry Andric }; 74530b57cec5SDimitry Andric } // end anonymous namespace 74540b57cec5SDimitry Andric 74550b57cec5SDimitry Andric /// /EH controls whether to run destructor cleanups when exceptions are 74560b57cec5SDimitry Andric /// thrown. There are three modifiers: 74570b57cec5SDimitry Andric /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions. 74580b57cec5SDimitry Andric /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions. 74590b57cec5SDimitry Andric /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR. 74600b57cec5SDimitry Andric /// - c: Assume that extern "C" functions are implicitly nounwind. 74610b57cec5SDimitry Andric /// The default is /EHs-c-, meaning cleanups are disabled. 74620b57cec5SDimitry Andric static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) { 74630b57cec5SDimitry Andric EHFlags EH; 74640b57cec5SDimitry Andric 74650b57cec5SDimitry Andric std::vector<std::string> EHArgs = 74660b57cec5SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_EH); 74670b57cec5SDimitry Andric for (auto EHVal : EHArgs) { 74680b57cec5SDimitry Andric for (size_t I = 0, E = EHVal.size(); I != E; ++I) { 74690b57cec5SDimitry Andric switch (EHVal[I]) { 74700b57cec5SDimitry Andric case 'a': 74710b57cec5SDimitry Andric EH.Asynch = maybeConsumeDash(EHVal, I); 74720b57cec5SDimitry Andric if (EH.Asynch) 74730b57cec5SDimitry Andric EH.Synch = false; 74740b57cec5SDimitry Andric continue; 74750b57cec5SDimitry Andric case 'c': 74760b57cec5SDimitry Andric EH.NoUnwindC = maybeConsumeDash(EHVal, I); 74770b57cec5SDimitry Andric continue; 74780b57cec5SDimitry Andric case 's': 74790b57cec5SDimitry Andric EH.Synch = maybeConsumeDash(EHVal, I); 74800b57cec5SDimitry Andric if (EH.Synch) 74810b57cec5SDimitry Andric EH.Asynch = false; 74820b57cec5SDimitry Andric continue; 74830b57cec5SDimitry Andric default: 74840b57cec5SDimitry Andric break; 74850b57cec5SDimitry Andric } 74860b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal; 74870b57cec5SDimitry Andric break; 74880b57cec5SDimitry Andric } 74890b57cec5SDimitry Andric } 74900b57cec5SDimitry Andric // The /GX, /GX- flags are only processed if there are not /EH flags. 74910b57cec5SDimitry Andric // The default is that /GX is not specified. 74920b57cec5SDimitry Andric if (EHArgs.empty() && 74930b57cec5SDimitry Andric Args.hasFlag(options::OPT__SLASH_GX, options::OPT__SLASH_GX_, 74940b57cec5SDimitry Andric /*Default=*/false)) { 74950b57cec5SDimitry Andric EH.Synch = true; 74960b57cec5SDimitry Andric EH.NoUnwindC = true; 74970b57cec5SDimitry Andric } 74980b57cec5SDimitry Andric 749981ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 750081ad6265SDimitry Andric EH.Synch = false; 750181ad6265SDimitry Andric EH.NoUnwindC = false; 750281ad6265SDimitry Andric EH.Asynch = false; 750381ad6265SDimitry Andric } 750481ad6265SDimitry Andric 75050b57cec5SDimitry Andric return EH; 75060b57cec5SDimitry Andric } 75070b57cec5SDimitry Andric 75080b57cec5SDimitry Andric void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, 75090b57cec5SDimitry Andric ArgStringList &CmdArgs, 75100b57cec5SDimitry Andric codegenoptions::DebugInfoKind *DebugInfoKind, 75110b57cec5SDimitry Andric bool *EmitCodeView) const { 75120b57cec5SDimitry Andric unsigned RTOptionID = options::OPT__SLASH_MT; 75135ffd83dbSDimitry Andric bool isNVPTX = getToolChain().getTriple().isNVPTX(); 75140b57cec5SDimitry Andric 75150b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 75160b57cec5SDimitry Andric // The /LDd option implies /MTd. The dependent lib part can be overridden, 75170b57cec5SDimitry Andric // but defining _DEBUG is sticky. 75180b57cec5SDimitry Andric RTOptionID = options::OPT__SLASH_MTd; 75190b57cec5SDimitry Andric 75200b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) 75210b57cec5SDimitry Andric RTOptionID = A->getOption().getID(); 75220b57cec5SDimitry Andric 75230b57cec5SDimitry Andric StringRef FlagForCRT; 75240b57cec5SDimitry Andric switch (RTOptionID) { 75250b57cec5SDimitry Andric case options::OPT__SLASH_MD: 75260b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 75270b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 75280b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 75290b57cec5SDimitry Andric CmdArgs.push_back("-D_DLL"); 75300b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=msvcrt"; 75310b57cec5SDimitry Andric break; 75320b57cec5SDimitry Andric case options::OPT__SLASH_MDd: 75330b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 75340b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 75350b57cec5SDimitry Andric CmdArgs.push_back("-D_DLL"); 75360b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=msvcrtd"; 75370b57cec5SDimitry Andric break; 75380b57cec5SDimitry Andric case options::OPT__SLASH_MT: 75390b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_LDd)) 75400b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 75410b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 75420b57cec5SDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 75430b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=libcmt"; 75440b57cec5SDimitry Andric break; 75450b57cec5SDimitry Andric case options::OPT__SLASH_MTd: 75460b57cec5SDimitry Andric CmdArgs.push_back("-D_DEBUG"); 75470b57cec5SDimitry Andric CmdArgs.push_back("-D_MT"); 75480b57cec5SDimitry Andric CmdArgs.push_back("-flto-visibility-public-std"); 75490b57cec5SDimitry Andric FlagForCRT = "--dependent-lib=libcmtd"; 75500b57cec5SDimitry Andric break; 75510b57cec5SDimitry Andric default: 75520b57cec5SDimitry Andric llvm_unreachable("Unexpected option ID."); 75530b57cec5SDimitry Andric } 75540b57cec5SDimitry Andric 75550b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_Zl)) { 75560b57cec5SDimitry Andric CmdArgs.push_back("-D_VC_NODEFAULTLIB"); 75570b57cec5SDimitry Andric } else { 75580b57cec5SDimitry Andric CmdArgs.push_back(FlagForCRT.data()); 75590b57cec5SDimitry Andric 75600b57cec5SDimitry Andric // This provides POSIX compatibility (maps 'open' to '_open'), which most 75610b57cec5SDimitry Andric // users want. The /Za flag to cl.exe turns this off, but it's not 75620b57cec5SDimitry Andric // implemented in clang. 75630b57cec5SDimitry Andric CmdArgs.push_back("--dependent-lib=oldnames"); 75640b57cec5SDimitry Andric } 75650b57cec5SDimitry Andric 75665ffd83dbSDimitry Andric if (Arg *ShowIncludes = 75675ffd83dbSDimitry Andric Args.getLastArg(options::OPT__SLASH_showIncludes, 75685ffd83dbSDimitry Andric options::OPT__SLASH_showIncludes_user)) { 75695ffd83dbSDimitry Andric CmdArgs.push_back("--show-includes"); 75705ffd83dbSDimitry Andric if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes)) 75715ffd83dbSDimitry Andric CmdArgs.push_back("-sys-header-deps"); 75725ffd83dbSDimitry Andric } 75730b57cec5SDimitry Andric 75740b57cec5SDimitry Andric // This controls whether or not we emit RTTI data for polymorphic types. 75750b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR, 75760b57cec5SDimitry Andric /*Default=*/false)) 75770b57cec5SDimitry Andric CmdArgs.push_back("-fno-rtti-data"); 75780b57cec5SDimitry Andric 75790b57cec5SDimitry Andric // This controls whether or not we emit stack-protector instrumentation. 75800b57cec5SDimitry Andric // In MSVC, Buffer Security Check (/GS) is on by default. 75815ffd83dbSDimitry Andric if (!isNVPTX && Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_, 75820b57cec5SDimitry Andric /*Default=*/true)) { 75830b57cec5SDimitry Andric CmdArgs.push_back("-stack-protector"); 75840b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong))); 75850b57cec5SDimitry Andric } 75860b57cec5SDimitry Andric 7587e8d8bef9SDimitry Andric // Emit CodeView if -Z7 or -gline-tables-only are present. 7588e8d8bef9SDimitry Andric if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7, 75890b57cec5SDimitry Andric options::OPT_gline_tables_only)) { 75900b57cec5SDimitry Andric *EmitCodeView = true; 75910b57cec5SDimitry Andric if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7)) 7592fe6060f1SDimitry Andric *DebugInfoKind = codegenoptions::DebugInfoConstructor; 75930b57cec5SDimitry Andric else 75940b57cec5SDimitry Andric *DebugInfoKind = codegenoptions::DebugLineTablesOnly; 75950b57cec5SDimitry Andric } else { 75960b57cec5SDimitry Andric *EmitCodeView = false; 75970b57cec5SDimitry Andric } 75980b57cec5SDimitry Andric 75990b57cec5SDimitry Andric const Driver &D = getToolChain().getDriver(); 760081ad6265SDimitry Andric 760181ad6265SDimitry Andric // This controls whether or not we perform JustMyCode instrumentation. 760281ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_JMC, options::OPT__SLASH_JMC_, 760381ad6265SDimitry Andric /*Default=*/false)) { 760481ad6265SDimitry Andric if (*EmitCodeView && *DebugInfoKind >= codegenoptions::DebugInfoConstructor) 760581ad6265SDimitry Andric CmdArgs.push_back("-fjmc"); 760681ad6265SDimitry Andric else 760781ad6265SDimitry Andric D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC" 760881ad6265SDimitry Andric << "'/Zi', '/Z7'"; 760981ad6265SDimitry Andric } 761081ad6265SDimitry Andric 76110b57cec5SDimitry Andric EHFlags EH = parseClangCLEHFlags(D, Args); 76125ffd83dbSDimitry Andric if (!isNVPTX && (EH.Synch || EH.Asynch)) { 76130b57cec5SDimitry Andric if (types::isCXX(InputType)) 76140b57cec5SDimitry Andric CmdArgs.push_back("-fcxx-exceptions"); 76150b57cec5SDimitry Andric CmdArgs.push_back("-fexceptions"); 76160b57cec5SDimitry Andric } 76170b57cec5SDimitry Andric if (types::isCXX(InputType) && EH.Synch && EH.NoUnwindC) 76180b57cec5SDimitry Andric CmdArgs.push_back("-fexternc-nounwind"); 76190b57cec5SDimitry Andric 76200b57cec5SDimitry Andric // /EP should expand to -E -P. 76210b57cec5SDimitry Andric if (Args.hasArg(options::OPT__SLASH_EP)) { 76220b57cec5SDimitry Andric CmdArgs.push_back("-E"); 76230b57cec5SDimitry Andric CmdArgs.push_back("-P"); 76240b57cec5SDimitry Andric } 76250b57cec5SDimitry Andric 76260b57cec5SDimitry Andric unsigned VolatileOptionID; 7627480093f4SDimitry Andric if (getToolChain().getTriple().isX86()) 76280b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_ms; 76290b57cec5SDimitry Andric else 76300b57cec5SDimitry Andric VolatileOptionID = options::OPT__SLASH_volatile_iso; 76310b57cec5SDimitry Andric 76320b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group)) 76330b57cec5SDimitry Andric VolatileOptionID = A->getOption().getID(); 76340b57cec5SDimitry Andric 76350b57cec5SDimitry Andric if (VolatileOptionID == options::OPT__SLASH_volatile_ms) 76360b57cec5SDimitry Andric CmdArgs.push_back("-fms-volatile"); 76370b57cec5SDimitry Andric 76380b57cec5SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_dllexportInlines_, 76390b57cec5SDimitry Andric options::OPT__SLASH_Zc_dllexportInlines, 76400b57cec5SDimitry Andric false)) { 76410b57cec5SDimitry Andric CmdArgs.push_back("-fno-dllexport-inlines"); 76420b57cec5SDimitry Andric } 76430b57cec5SDimitry Andric 764481ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_Zc_wchar_t_, 764581ad6265SDimitry Andric options::OPT__SLASH_Zc_wchar_t, false)) { 764681ad6265SDimitry Andric CmdArgs.push_back("-fno-wchar"); 764781ad6265SDimitry Andric } 764881ad6265SDimitry Andric 764981ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) { 765081ad6265SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 765181ad6265SDimitry Andric std::vector<std::string> Values = 765281ad6265SDimitry Andric Args.getAllArgValues(options::OPT__SLASH_arch); 765381ad6265SDimitry Andric if (!Values.empty()) { 765481ad6265SDimitry Andric llvm::SmallSet<std::string, 4> SupportedArches; 765581ad6265SDimitry Andric if (Arch == llvm::Triple::x86) 765681ad6265SDimitry Andric SupportedArches.insert("IA32"); 765781ad6265SDimitry Andric 765881ad6265SDimitry Andric for (auto &V : Values) 765981ad6265SDimitry Andric if (!SupportedArches.contains(V)) 766081ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) 766181ad6265SDimitry Andric << std::string("/arch:").append(V) << "/kernel"; 766281ad6265SDimitry Andric } 766381ad6265SDimitry Andric 766481ad6265SDimitry Andric CmdArgs.push_back("-fno-rtti"); 766581ad6265SDimitry Andric if (Args.hasFlag(options::OPT__SLASH_GR, options::OPT__SLASH_GR_, false)) 766681ad6265SDimitry Andric D.Diag(diag::err_drv_argument_not_allowed_with) << "/GR" 766781ad6265SDimitry Andric << "/kernel"; 766881ad6265SDimitry Andric } 766981ad6265SDimitry Andric 76700b57cec5SDimitry Andric Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); 76710b57cec5SDimitry Andric Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb); 76720b57cec5SDimitry Andric if (MostGeneralArg && BestCaseArg) 76730b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 76740b57cec5SDimitry Andric << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args); 76750b57cec5SDimitry Andric 76760b57cec5SDimitry Andric if (MostGeneralArg) { 76770b57cec5SDimitry Andric Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms); 76780b57cec5SDimitry Andric Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm); 76790b57cec5SDimitry Andric Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv); 76800b57cec5SDimitry Andric 76810b57cec5SDimitry Andric Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg; 76820b57cec5SDimitry Andric Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg; 76830b57cec5SDimitry Andric if (FirstConflict && SecondConflict && FirstConflict != SecondConflict) 76840b57cec5SDimitry Andric D.Diag(clang::diag::err_drv_argument_not_allowed_with) 76850b57cec5SDimitry Andric << FirstConflict->getAsString(Args) 76860b57cec5SDimitry Andric << SecondConflict->getAsString(Args); 76870b57cec5SDimitry Andric 76880b57cec5SDimitry Andric if (SingleArg) 76890b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=single"); 76900b57cec5SDimitry Andric else if (MultipleArg) 76910b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=multiple"); 76920b57cec5SDimitry Andric else 76930b57cec5SDimitry Andric CmdArgs.push_back("-fms-memptr-rep=virtual"); 76940b57cec5SDimitry Andric } 76950b57cec5SDimitry Andric 76960b57cec5SDimitry Andric // Parse the default calling convention options. 76970b57cec5SDimitry Andric if (Arg *CCArg = 76980b57cec5SDimitry Andric Args.getLastArg(options::OPT__SLASH_Gd, options::OPT__SLASH_Gr, 76990b57cec5SDimitry Andric options::OPT__SLASH_Gz, options::OPT__SLASH_Gv, 77000b57cec5SDimitry Andric options::OPT__SLASH_Gregcall)) { 77010b57cec5SDimitry Andric unsigned DCCOptId = CCArg->getOption().getID(); 77020b57cec5SDimitry Andric const char *DCCFlag = nullptr; 77035ffd83dbSDimitry Andric bool ArchSupported = !isNVPTX; 77040b57cec5SDimitry Andric llvm::Triple::ArchType Arch = getToolChain().getArch(); 77050b57cec5SDimitry Andric switch (DCCOptId) { 77060b57cec5SDimitry Andric case options::OPT__SLASH_Gd: 77070b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=cdecl"; 77080b57cec5SDimitry Andric break; 77090b57cec5SDimitry Andric case options::OPT__SLASH_Gr: 77100b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 77110b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=fastcall"; 77120b57cec5SDimitry Andric break; 77130b57cec5SDimitry Andric case options::OPT__SLASH_Gz: 77140b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86; 77150b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=stdcall"; 77160b57cec5SDimitry Andric break; 77170b57cec5SDimitry Andric case options::OPT__SLASH_Gv: 77180b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 77190b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=vectorcall"; 77200b57cec5SDimitry Andric break; 77210b57cec5SDimitry Andric case options::OPT__SLASH_Gregcall: 77220b57cec5SDimitry Andric ArchSupported = Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64; 77230b57cec5SDimitry Andric DCCFlag = "-fdefault-calling-conv=regcall"; 77240b57cec5SDimitry Andric break; 77250b57cec5SDimitry Andric } 77260b57cec5SDimitry Andric 77270b57cec5SDimitry Andric // MSVC doesn't warn if /Gr or /Gz is used on x64, so we don't either. 77280b57cec5SDimitry Andric if (ArchSupported && DCCFlag) 77290b57cec5SDimitry Andric CmdArgs.push_back(DCCFlag); 77300b57cec5SDimitry Andric } 77310b57cec5SDimitry Andric 77320b57cec5SDimitry Andric Args.AddLastArg(CmdArgs, options::OPT_vtordisp_mode_EQ); 77330b57cec5SDimitry Andric 77340b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { 77350b57cec5SDimitry Andric CmdArgs.push_back("-fdiagnostics-format"); 77360b57cec5SDimitry Andric CmdArgs.push_back("msvc"); 77370b57cec5SDimitry Andric } 77380b57cec5SDimitry Andric 773981ad6265SDimitry Andric if (Args.hasArg(options::OPT__SLASH_kernel)) 774081ad6265SDimitry Andric CmdArgs.push_back("-fms-kernel"); 774181ad6265SDimitry Andric 77420b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) { 7743480093f4SDimitry Andric StringRef GuardArgs = A->getValue(); 7744fe6060f1SDimitry Andric // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and 7745fe6060f1SDimitry Andric // "ehcont-". 7746fe6060f1SDimitry Andric if (GuardArgs.equals_insensitive("cf")) { 7747480093f4SDimitry Andric // Emit CFG instrumentation and the table of address-taken functions. 77480b57cec5SDimitry Andric CmdArgs.push_back("-cfguard"); 7749fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf,nochecks")) { 7750480093f4SDimitry Andric // Emit only the table of address-taken functions. 7751480093f4SDimitry Andric CmdArgs.push_back("-cfguard-no-checks"); 7752fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("ehcont")) { 7753fe6060f1SDimitry Andric // Emit EH continuation table. 7754fe6060f1SDimitry Andric CmdArgs.push_back("-ehcontguard"); 7755fe6060f1SDimitry Andric } else if (GuardArgs.equals_insensitive("cf-") || 7756fe6060f1SDimitry Andric GuardArgs.equals_insensitive("ehcont-")) { 7757480093f4SDimitry Andric // Do nothing, but we might want to emit a security warning in future. 7758480093f4SDimitry Andric } else { 7759480093f4SDimitry Andric D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs; 7760480093f4SDimitry Andric } 77610b57cec5SDimitry Andric } 77620b57cec5SDimitry Andric } 77630b57cec5SDimitry Andric 77640b57cec5SDimitry Andric const char *Clang::getBaseInputName(const ArgList &Args, 77650b57cec5SDimitry Andric const InputInfo &Input) { 77660b57cec5SDimitry Andric return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())); 77670b57cec5SDimitry Andric } 77680b57cec5SDimitry Andric 77690b57cec5SDimitry Andric const char *Clang::getBaseInputStem(const ArgList &Args, 77700b57cec5SDimitry Andric const InputInfoList &Inputs) { 77710b57cec5SDimitry Andric const char *Str = getBaseInputName(Args, Inputs[0]); 77720b57cec5SDimitry Andric 77730b57cec5SDimitry Andric if (const char *End = strrchr(Str, '.')) 77740b57cec5SDimitry Andric return Args.MakeArgString(std::string(Str, End)); 77750b57cec5SDimitry Andric 77760b57cec5SDimitry Andric return Str; 77770b57cec5SDimitry Andric } 77780b57cec5SDimitry Andric 77790b57cec5SDimitry Andric const char *Clang::getDependencyFileName(const ArgList &Args, 77800b57cec5SDimitry Andric const InputInfoList &Inputs) { 77810b57cec5SDimitry Andric // FIXME: Think about this more. 77820b57cec5SDimitry Andric 77830b57cec5SDimitry Andric if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 7784a7dea167SDimitry Andric SmallString<128> OutputFilename(OutputOpt->getValue()); 7785a7dea167SDimitry Andric llvm::sys::path::replace_extension(OutputFilename, llvm::Twine('d')); 7786a7dea167SDimitry Andric return Args.MakeArgString(OutputFilename); 77870b57cec5SDimitry Andric } 7788a7dea167SDimitry Andric 7789a7dea167SDimitry Andric return Args.MakeArgString(Twine(getBaseInputStem(Args, Inputs)) + ".d"); 77900b57cec5SDimitry Andric } 77910b57cec5SDimitry Andric 77920b57cec5SDimitry Andric // Begin ClangAs 77930b57cec5SDimitry Andric 77940b57cec5SDimitry Andric void ClangAs::AddMIPSTargetArgs(const ArgList &Args, 77950b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 77960b57cec5SDimitry Andric StringRef CPUName; 77970b57cec5SDimitry Andric StringRef ABIName; 77980b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 77990b57cec5SDimitry Andric mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 78000b57cec5SDimitry Andric 78010b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 78020b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 78030b57cec5SDimitry Andric } 78040b57cec5SDimitry Andric 78050b57cec5SDimitry Andric void ClangAs::AddX86TargetArgs(const ArgList &Args, 78060b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 78075ffd83dbSDimitry Andric addX86AlignBranchArgs(getToolChain().getDriver(), Args, CmdArgs, 78085ffd83dbSDimitry Andric /*IsLTO=*/false); 7809480093f4SDimitry Andric 78100b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) { 78110b57cec5SDimitry Andric StringRef Value = A->getValue(); 78120b57cec5SDimitry Andric if (Value == "intel" || Value == "att") { 78130b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 78140b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value)); 78150b57cec5SDimitry Andric } else { 78160b57cec5SDimitry Andric getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) 78170b57cec5SDimitry Andric << A->getOption().getName() << Value; 78180b57cec5SDimitry Andric } 78190b57cec5SDimitry Andric } 78200b57cec5SDimitry Andric } 78210b57cec5SDimitry Andric 78220b57cec5SDimitry Andric void ClangAs::AddRISCVTargetArgs(const ArgList &Args, 78230b57cec5SDimitry Andric ArgStringList &CmdArgs) const { 78240b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getTriple(); 78250b57cec5SDimitry Andric StringRef ABIName = riscv::getRISCVABI(Args, Triple); 78260b57cec5SDimitry Andric 78270b57cec5SDimitry Andric CmdArgs.push_back("-target-abi"); 78280b57cec5SDimitry Andric CmdArgs.push_back(ABIName.data()); 78290b57cec5SDimitry Andric } 78300b57cec5SDimitry Andric 78310b57cec5SDimitry Andric void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 78320b57cec5SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 78330b57cec5SDimitry Andric const ArgList &Args, 78340b57cec5SDimitry Andric const char *LinkingOutput) const { 78350b57cec5SDimitry Andric ArgStringList CmdArgs; 78360b57cec5SDimitry Andric 78370b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Unexpected number of inputs."); 78380b57cec5SDimitry Andric const InputInfo &Input = Inputs[0]; 78390b57cec5SDimitry Andric 78400b57cec5SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 78410b57cec5SDimitry Andric const std::string &TripleStr = Triple.getTriple(); 784281ad6265SDimitry Andric const Optional<llvm::Triple> TargetVariantTriple = 784381ad6265SDimitry Andric getToolChain().getTargetVariantTriple(); 78440b57cec5SDimitry Andric const auto &D = getToolChain().getDriver(); 78450b57cec5SDimitry Andric 78460b57cec5SDimitry Andric // Don't warn about "clang -w -c foo.s" 78470b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_w); 78480b57cec5SDimitry Andric // and "clang -emit-llvm -c foo.s" 78490b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_emit_llvm); 78500b57cec5SDimitry Andric 78510b57cec5SDimitry Andric claimNoWarnArgs(Args); 78520b57cec5SDimitry Andric 78530b57cec5SDimitry Andric // Invoke ourselves in -cc1as mode. 78540b57cec5SDimitry Andric // 78550b57cec5SDimitry Andric // FIXME: Implement custom jobs for internal actions. 78560b57cec5SDimitry Andric CmdArgs.push_back("-cc1as"); 78570b57cec5SDimitry Andric 78580b57cec5SDimitry Andric // Add the "effective" target triple. 78590b57cec5SDimitry Andric CmdArgs.push_back("-triple"); 78600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TripleStr)); 786181ad6265SDimitry Andric if (TargetVariantTriple) { 786281ad6265SDimitry Andric CmdArgs.push_back("-darwin-target-variant-triple"); 786381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TargetVariantTriple->getTriple())); 786481ad6265SDimitry Andric } 78650b57cec5SDimitry Andric 78660b57cec5SDimitry Andric // Set the output mode, we currently only expect to be used as a real 78670b57cec5SDimitry Andric // assembler. 78680b57cec5SDimitry Andric CmdArgs.push_back("-filetype"); 78690b57cec5SDimitry Andric CmdArgs.push_back("obj"); 78700b57cec5SDimitry Andric 78710b57cec5SDimitry Andric // Set the main file name, so that debug info works even with 78720b57cec5SDimitry Andric // -save-temps or preprocessed assembly. 78730b57cec5SDimitry Andric CmdArgs.push_back("-main-file-name"); 78740b57cec5SDimitry Andric CmdArgs.push_back(Clang::getBaseInputName(Args, Input)); 78750b57cec5SDimitry Andric 78760b57cec5SDimitry Andric // Add the target cpu 7877349cc55cSDimitry Andric std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ true); 78780b57cec5SDimitry Andric if (!CPU.empty()) { 78790b57cec5SDimitry Andric CmdArgs.push_back("-target-cpu"); 78800b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CPU)); 78810b57cec5SDimitry Andric } 78820b57cec5SDimitry Andric 78830b57cec5SDimitry Andric // Add the target features 78845ffd83dbSDimitry Andric getTargetFeatures(D, Triple, Args, CmdArgs, true); 78850b57cec5SDimitry Andric 78860b57cec5SDimitry Andric // Ignore explicit -force_cpusubtype_ALL option. 78870b57cec5SDimitry Andric (void)Args.hasArg(options::OPT_force__cpusubtype__ALL); 78880b57cec5SDimitry Andric 78890b57cec5SDimitry Andric // Pass along any -I options so we get proper .include search paths. 78900b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I_Group); 78910b57cec5SDimitry Andric 78920b57cec5SDimitry Andric // Determine the original source input. 78930eae32dcSDimitry Andric auto FindSource = [](const Action *S) -> const Action * { 78940eae32dcSDimitry Andric while (S->getKind() != Action::InputClass) { 78950eae32dcSDimitry Andric assert(!S->getInputs().empty() && "unexpected root action!"); 78960eae32dcSDimitry Andric S = S->getInputs()[0]; 78970b57cec5SDimitry Andric } 78980eae32dcSDimitry Andric return S; 78990eae32dcSDimitry Andric }; 79000eae32dcSDimitry Andric const Action *SourceAction = FindSource(&JA); 79010b57cec5SDimitry Andric 79020b57cec5SDimitry Andric // Forward -g and handle debug info related flags, assuming we are dealing 79030b57cec5SDimitry Andric // with an actual assembly file. 79040b57cec5SDimitry Andric bool WantDebug = false; 79050b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_g_Group); 7906fe6060f1SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 79070b57cec5SDimitry Andric WantDebug = !A->getOption().matches(options::OPT_g0) && 79080b57cec5SDimitry Andric !A->getOption().matches(options::OPT_ggdb0); 7909480093f4SDimitry Andric 7910fe6060f1SDimitry Andric unsigned DwarfVersion = ParseDebugDefaultVersion(getToolChain(), Args); 7911fe6060f1SDimitry Andric if (const Arg *GDwarfN = getDwarfNArg(Args)) 7912fe6060f1SDimitry Andric DwarfVersion = DwarfVersionNum(GDwarfN->getSpelling()); 7913480093f4SDimitry Andric 79140b57cec5SDimitry Andric if (DwarfVersion == 0) 79150b57cec5SDimitry Andric DwarfVersion = getToolChain().GetDefaultDwarfVersion(); 79160b57cec5SDimitry Andric 79170b57cec5SDimitry Andric codegenoptions::DebugInfoKind DebugInfoKind = codegenoptions::NoDebugInfo; 79180b57cec5SDimitry Andric 79190eae32dcSDimitry Andric // Add the -fdebug-compilation-dir flag if needed. 79200eae32dcSDimitry Andric const char *DebugCompilationDir = 79210eae32dcSDimitry Andric addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS()); 79220eae32dcSDimitry Andric 79230b57cec5SDimitry Andric if (SourceAction->getType() == types::TY_Asm || 79240b57cec5SDimitry Andric SourceAction->getType() == types::TY_PP_Asm) { 79250b57cec5SDimitry Andric // You might think that it would be ok to set DebugInfoKind outside of 79260b57cec5SDimitry Andric // the guard for source type, however there is a test which asserts 79270b57cec5SDimitry Andric // that some assembler invocation receives no -debug-info-kind, 79280b57cec5SDimitry Andric // and it's not clear whether that test is just overly restrictive. 7929fe6060f1SDimitry Andric DebugInfoKind = (WantDebug ? codegenoptions::DebugInfoConstructor 79300b57cec5SDimitry Andric : codegenoptions::NoDebugInfo); 79310b57cec5SDimitry Andric 793281ad6265SDimitry Andric addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args, 793381ad6265SDimitry Andric CmdArgs); 79340b57cec5SDimitry Andric 79350b57cec5SDimitry Andric // Set the AT_producer to the clang version when using the integrated 79360b57cec5SDimitry Andric // assembler on assembly source files. 79370b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-producer"); 79380b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 79390b57cec5SDimitry Andric 79400b57cec5SDimitry Andric // And pass along -I options 79410b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_I); 79420b57cec5SDimitry Andric } 79430b57cec5SDimitry Andric RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DwarfVersion, 79440b57cec5SDimitry Andric llvm::DebuggerKind::Default); 7945fe6060f1SDimitry Andric renderDwarfFormat(D, Triple, Args, CmdArgs, DwarfVersion); 79460b57cec5SDimitry Andric RenderDebugInfoCompressionArgs(Args, CmdArgs, D, getToolChain()); 79470b57cec5SDimitry Andric 79480b57cec5SDimitry Andric // Handle -fPIC et al -- the relocation-model affects the assembler 79490b57cec5SDimitry Andric // for some targets. 79500b57cec5SDimitry Andric llvm::Reloc::Model RelocationModel; 79510b57cec5SDimitry Andric unsigned PICLevel; 79520b57cec5SDimitry Andric bool IsPIE; 79530b57cec5SDimitry Andric std::tie(RelocationModel, PICLevel, IsPIE) = 79540b57cec5SDimitry Andric ParsePICArgs(getToolChain(), Args); 79550b57cec5SDimitry Andric 79560b57cec5SDimitry Andric const char *RMName = RelocationModelName(RelocationModel); 79570b57cec5SDimitry Andric if (RMName) { 79580b57cec5SDimitry Andric CmdArgs.push_back("-mrelocation-model"); 79590b57cec5SDimitry Andric CmdArgs.push_back(RMName); 79600b57cec5SDimitry Andric } 79610b57cec5SDimitry Andric 79620b57cec5SDimitry Andric // Optionally embed the -cc1as level arguments into the debug info, for build 79630b57cec5SDimitry Andric // analysis. 79640b57cec5SDimitry Andric if (getToolChain().UseDwarfDebugFlags()) { 79650b57cec5SDimitry Andric ArgStringList OriginalArgs; 79660b57cec5SDimitry Andric for (const auto &Arg : Args) 79670b57cec5SDimitry Andric Arg->render(Args, OriginalArgs); 79680b57cec5SDimitry Andric 79690b57cec5SDimitry Andric SmallString<256> Flags; 79700b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 79715ffd83dbSDimitry Andric EscapeSpacesAndBackslashes(Exec, Flags); 79720b57cec5SDimitry Andric for (const char *OriginalArg : OriginalArgs) { 79730b57cec5SDimitry Andric SmallString<128> EscapedArg; 79740b57cec5SDimitry Andric EscapeSpacesAndBackslashes(OriginalArg, EscapedArg); 79750b57cec5SDimitry Andric Flags += " "; 79760b57cec5SDimitry Andric Flags += EscapedArg; 79770b57cec5SDimitry Andric } 79780b57cec5SDimitry Andric CmdArgs.push_back("-dwarf-debug-flags"); 79790b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Flags)); 79800b57cec5SDimitry Andric } 79810b57cec5SDimitry Andric 79820b57cec5SDimitry Andric // FIXME: Add -static support, once we have it. 79830b57cec5SDimitry Andric 79840b57cec5SDimitry Andric // Add target specific flags. 79850b57cec5SDimitry Andric switch (getToolChain().getArch()) { 79860b57cec5SDimitry Andric default: 79870b57cec5SDimitry Andric break; 79880b57cec5SDimitry Andric 79890b57cec5SDimitry Andric case llvm::Triple::mips: 79900b57cec5SDimitry Andric case llvm::Triple::mipsel: 79910b57cec5SDimitry Andric case llvm::Triple::mips64: 79920b57cec5SDimitry Andric case llvm::Triple::mips64el: 79930b57cec5SDimitry Andric AddMIPSTargetArgs(Args, CmdArgs); 79940b57cec5SDimitry Andric break; 79950b57cec5SDimitry Andric 79960b57cec5SDimitry Andric case llvm::Triple::x86: 79970b57cec5SDimitry Andric case llvm::Triple::x86_64: 79980b57cec5SDimitry Andric AddX86TargetArgs(Args, CmdArgs); 79990b57cec5SDimitry Andric break; 80000b57cec5SDimitry Andric 80010b57cec5SDimitry Andric case llvm::Triple::arm: 80020b57cec5SDimitry Andric case llvm::Triple::armeb: 80030b57cec5SDimitry Andric case llvm::Triple::thumb: 80040b57cec5SDimitry Andric case llvm::Triple::thumbeb: 80050b57cec5SDimitry Andric // This isn't in AddARMTargetArgs because we want to do this for assembly 80060b57cec5SDimitry Andric // only, not C/C++. 80070b57cec5SDimitry Andric if (Args.hasFlag(options::OPT_mdefault_build_attributes, 80080b57cec5SDimitry Andric options::OPT_mno_default_build_attributes, true)) { 80090b57cec5SDimitry Andric CmdArgs.push_back("-mllvm"); 80100b57cec5SDimitry Andric CmdArgs.push_back("-arm-add-build-attributes"); 80110b57cec5SDimitry Andric } 80120b57cec5SDimitry Andric break; 80130b57cec5SDimitry Andric 8014e8d8bef9SDimitry Andric case llvm::Triple::aarch64: 8015e8d8bef9SDimitry Andric case llvm::Triple::aarch64_32: 8016e8d8bef9SDimitry Andric case llvm::Triple::aarch64_be: 8017e8d8bef9SDimitry Andric if (Args.hasArg(options::OPT_mmark_bti_property)) { 8018e8d8bef9SDimitry Andric CmdArgs.push_back("-mllvm"); 8019e8d8bef9SDimitry Andric CmdArgs.push_back("-aarch64-mark-bti-property"); 8020e8d8bef9SDimitry Andric } 8021e8d8bef9SDimitry Andric break; 8022e8d8bef9SDimitry Andric 80230b57cec5SDimitry Andric case llvm::Triple::riscv32: 80240b57cec5SDimitry Andric case llvm::Triple::riscv64: 80250b57cec5SDimitry Andric AddRISCVTargetArgs(Args, CmdArgs); 80260b57cec5SDimitry Andric break; 80270b57cec5SDimitry Andric } 80280b57cec5SDimitry Andric 80290b57cec5SDimitry Andric // Consume all the warning flags. Usually this would be handled more 80300b57cec5SDimitry Andric // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as 80310b57cec5SDimitry Andric // doesn't handle that so rather than warning about unused flags that are 80320b57cec5SDimitry Andric // actually used, we'll lie by omission instead. 80330b57cec5SDimitry Andric // FIXME: Stop lying and consume only the appropriate driver flags 80340b57cec5SDimitry Andric Args.ClaimAllArgs(options::OPT_W_Group); 80350b57cec5SDimitry Andric 80360b57cec5SDimitry Andric CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 80370b57cec5SDimitry Andric getToolChain().getDriver()); 80380b57cec5SDimitry Andric 80390b57cec5SDimitry Andric Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 80400b57cec5SDimitry Andric 80410eae32dcSDimitry Andric if (DebugInfoKind > codegenoptions::NoDebugInfo && Output.isFilename()) 80420eae32dcSDimitry Andric addDebugObjectName(Args, CmdArgs, DebugCompilationDir, 80430eae32dcSDimitry Andric Output.getFilename()); 80440eae32dcSDimitry Andric 80450eae32dcSDimitry Andric // Fixup any previous commands that use -object-file-name because when we 80460eae32dcSDimitry Andric // generated them, the final .obj name wasn't yet known. 80470eae32dcSDimitry Andric for (Command &J : C.getJobs()) { 80480eae32dcSDimitry Andric if (SourceAction != FindSource(&J.getSource())) 80490eae32dcSDimitry Andric continue; 80500eae32dcSDimitry Andric auto &JArgs = J.getArguments(); 80510eae32dcSDimitry Andric for (unsigned I = 0; I < JArgs.size(); ++I) { 80520eae32dcSDimitry Andric if (StringRef(JArgs[I]).startswith("-object-file-name=") && 80530eae32dcSDimitry Andric Output.isFilename()) { 80540eae32dcSDimitry Andric ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); 80550eae32dcSDimitry Andric addDebugObjectName(Args, NewArgs, DebugCompilationDir, 80560eae32dcSDimitry Andric Output.getFilename()); 80570eae32dcSDimitry Andric NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); 80580eae32dcSDimitry Andric J.replaceArguments(NewArgs); 80590eae32dcSDimitry Andric break; 80600eae32dcSDimitry Andric } 80610eae32dcSDimitry Andric } 80620eae32dcSDimitry Andric } 80630eae32dcSDimitry Andric 80640b57cec5SDimitry Andric assert(Output.isFilename() && "Unexpected lipo output."); 80650b57cec5SDimitry Andric CmdArgs.push_back("-o"); 80660b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 80670b57cec5SDimitry Andric 80680b57cec5SDimitry Andric const llvm::Triple &T = getToolChain().getTriple(); 80690b57cec5SDimitry Andric Arg *A; 80700b57cec5SDimitry Andric if (getDebugFissionKind(D, Args, A) == DwarfFissionKind::Split && 80710b57cec5SDimitry Andric T.isOSBinFormatELF()) { 80720b57cec5SDimitry Andric CmdArgs.push_back("-split-dwarf-output"); 8073e8d8bef9SDimitry Andric CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output)); 80740b57cec5SDimitry Andric } 80750b57cec5SDimitry Andric 8076e8d8bef9SDimitry Andric if (Triple.isAMDGPU()) 807781ad6265SDimitry Andric handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true); 8078e8d8bef9SDimitry Andric 80790b57cec5SDimitry Andric assert(Input.isFilename() && "Invalid input."); 80800b57cec5SDimitry Andric CmdArgs.push_back(Input.getFilename()); 80810b57cec5SDimitry Andric 80820b57cec5SDimitry Andric const char *Exec = getToolChain().getDriver().getClangProgramPath(); 8083e8d8bef9SDimitry Andric if (D.CC1Main && !D.CCGenDiagnostics) { 8084e8d8bef9SDimitry Andric // Invoke cc1as directly in this process. 8085e8d8bef9SDimitry Andric C.addCommand(std::make_unique<CC1Command>(JA, *this, 8086e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 8087e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 8088e8d8bef9SDimitry Andric } else { 8089e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 8090e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 8091e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 8092e8d8bef9SDimitry Andric } 80930b57cec5SDimitry Andric } 80940b57cec5SDimitry Andric 80950b57cec5SDimitry Andric // Begin OffloadBundler 80960b57cec5SDimitry Andric 80970b57cec5SDimitry Andric void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA, 80980b57cec5SDimitry Andric const InputInfo &Output, 80990b57cec5SDimitry Andric const InputInfoList &Inputs, 81000b57cec5SDimitry Andric const llvm::opt::ArgList &TCArgs, 81010b57cec5SDimitry Andric const char *LinkingOutput) const { 81020b57cec5SDimitry Andric // The version with only one output is expected to refer to a bundling job. 81030b57cec5SDimitry Andric assert(isa<OffloadBundlingJobAction>(JA) && "Expecting bundling job!"); 81040b57cec5SDimitry Andric 81050b57cec5SDimitry Andric // The bundling command looks like this: 81060b57cec5SDimitry Andric // clang-offload-bundler -type=bc 81070b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 810881ad6265SDimitry Andric // -output=output_file 810981ad6265SDimitry Andric // -input=unbundle_file_host 811081ad6265SDimitry Andric // -input=unbundle_file_tgt1 811181ad6265SDimitry Andric // -input=unbundle_file_tgt2 81120b57cec5SDimitry Andric 81130b57cec5SDimitry Andric ArgStringList CmdArgs; 81140b57cec5SDimitry Andric 81150b57cec5SDimitry Andric // Get the type. 81160b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 81170b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Output.getType()))); 81180b57cec5SDimitry Andric 81190b57cec5SDimitry Andric assert(JA.getInputs().size() == Inputs.size() && 81200b57cec5SDimitry Andric "Not have inputs for all dependence actions??"); 81210b57cec5SDimitry Andric 81220b57cec5SDimitry Andric // Get the targets. 81230b57cec5SDimitry Andric SmallString<128> Triples; 81240b57cec5SDimitry Andric Triples += "-targets="; 81250b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 81260b57cec5SDimitry Andric if (I) 81270b57cec5SDimitry Andric Triples += ','; 81280b57cec5SDimitry Andric 81290b57cec5SDimitry Andric // Find ToolChain for this input. 81300b57cec5SDimitry Andric Action::OffloadKind CurKind = Action::OFK_Host; 81310b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 81320b57cec5SDimitry Andric const Action *CurDep = JA.getInputs()[I]; 81330b57cec5SDimitry Andric 81340b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(CurDep)) { 81350b57cec5SDimitry Andric CurTC = nullptr; 81360b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *A, const ToolChain *TC, const char *) { 81370b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 81380b57cec5SDimitry Andric CurKind = A->getOffloadingDeviceKind(); 81390b57cec5SDimitry Andric CurTC = TC; 81400b57cec5SDimitry Andric }); 81410b57cec5SDimitry Andric } 81420b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(CurKind); 8143349cc55cSDimitry Andric Triples += '-'; 8144349cc55cSDimitry Andric Triples += CurTC->getTriple().normalize(); 8145349cc55cSDimitry Andric if ((CurKind == Action::OFK_HIP || CurKind == Action::OFK_Cuda) && 81460eae32dcSDimitry Andric !StringRef(CurDep->getOffloadingArch()).empty()) { 8147349cc55cSDimitry Andric Triples += '-'; 81480b57cec5SDimitry Andric Triples += CurDep->getOffloadingArch(); 81490b57cec5SDimitry Andric } 8150349cc55cSDimitry Andric 8151349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8152349cc55cSDimitry Andric // with each toolchain. 8153349cc55cSDimitry Andric StringRef GPUArchName; 8154349cc55cSDimitry Andric if (CurKind == Action::OFK_OpenMP) { 8155349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8156349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8157349cc55cSDimitry Andric auto ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 8158349cc55cSDimitry Andric auto Arch = ArchStr.startswith_insensitive("-march="); 8159349cc55cSDimitry Andric if (Arch) { 8160349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8161349cc55cSDimitry Andric Triples += "-"; 8162349cc55cSDimitry Andric break; 8163349cc55cSDimitry Andric } 8164349cc55cSDimitry Andric } 8165349cc55cSDimitry Andric Triples += GPUArchName.str(); 8166349cc55cSDimitry Andric } 81670b57cec5SDimitry Andric } 81680b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 81690b57cec5SDimitry Andric 81700b57cec5SDimitry Andric // Get bundled file command. 81710b57cec5SDimitry Andric CmdArgs.push_back( 817281ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-output=") + Output.getFilename())); 81730b57cec5SDimitry Andric 81740b57cec5SDimitry Andric // Get unbundled files command. 81750b57cec5SDimitry Andric for (unsigned I = 0; I < Inputs.size(); ++I) { 817681ad6265SDimitry Andric SmallString<128> UB; 817781ad6265SDimitry Andric UB += "-input="; 81780b57cec5SDimitry Andric 81790b57cec5SDimitry Andric // Find ToolChain for this input. 81800b57cec5SDimitry Andric const ToolChain *CurTC = &getToolChain(); 81810b57cec5SDimitry Andric if (const auto *OA = dyn_cast<OffloadAction>(JA.getInputs()[I])) { 81820b57cec5SDimitry Andric CurTC = nullptr; 81830b57cec5SDimitry Andric OA->doOnEachDependence([&](Action *, const ToolChain *TC, const char *) { 81840b57cec5SDimitry Andric assert(CurTC == nullptr && "Expected one dependence!"); 81850b57cec5SDimitry Andric CurTC = TC; 81860b57cec5SDimitry Andric }); 81876e75b2fbSDimitry Andric UB += C.addTempFile( 81886e75b2fbSDimitry Andric C.getArgs().MakeArgString(CurTC->getInputFilename(Inputs[I]))); 81896e75b2fbSDimitry Andric } else { 81900b57cec5SDimitry Andric UB += CurTC->getInputFilename(Inputs[I]); 81910b57cec5SDimitry Andric } 81920b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 819381ad6265SDimitry Andric } 81940b57cec5SDimitry Andric // All the inputs are encoded as commands. 8195a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 81965ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 81970b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8198e8d8bef9SDimitry Andric CmdArgs, None, Output)); 81990b57cec5SDimitry Andric } 82000b57cec5SDimitry Andric 82010b57cec5SDimitry Andric void OffloadBundler::ConstructJobMultipleOutputs( 82020b57cec5SDimitry Andric Compilation &C, const JobAction &JA, const InputInfoList &Outputs, 82030b57cec5SDimitry Andric const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, 82040b57cec5SDimitry Andric const char *LinkingOutput) const { 82050b57cec5SDimitry Andric // The version with multiple outputs is expected to refer to a unbundling job. 82060b57cec5SDimitry Andric auto &UA = cast<OffloadUnbundlingJobAction>(JA); 82070b57cec5SDimitry Andric 82080b57cec5SDimitry Andric // The unbundling command looks like this: 82090b57cec5SDimitry Andric // clang-offload-bundler -type=bc 82100b57cec5SDimitry Andric // -targets=host-triple,openmp-triple1,openmp-triple2 821181ad6265SDimitry Andric // -input=input_file 821281ad6265SDimitry Andric // -output=unbundle_file_host 821381ad6265SDimitry Andric // -output=unbundle_file_tgt1 821481ad6265SDimitry Andric // -output=unbundle_file_tgt2 82150b57cec5SDimitry Andric // -unbundle 82160b57cec5SDimitry Andric 82170b57cec5SDimitry Andric ArgStringList CmdArgs; 82180b57cec5SDimitry Andric 82190b57cec5SDimitry Andric assert(Inputs.size() == 1 && "Expecting to unbundle a single file!"); 82200b57cec5SDimitry Andric InputInfo Input = Inputs.front(); 82210b57cec5SDimitry Andric 82220b57cec5SDimitry Andric // Get the type. 82230b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString( 82240b57cec5SDimitry Andric Twine("-type=") + types::getTypeTempSuffix(Input.getType()))); 82250b57cec5SDimitry Andric 82260b57cec5SDimitry Andric // Get the targets. 82270b57cec5SDimitry Andric SmallString<128> Triples; 82280b57cec5SDimitry Andric Triples += "-targets="; 82290b57cec5SDimitry Andric auto DepInfo = UA.getDependentActionsInfo(); 82300b57cec5SDimitry Andric for (unsigned I = 0; I < DepInfo.size(); ++I) { 82310b57cec5SDimitry Andric if (I) 82320b57cec5SDimitry Andric Triples += ','; 82330b57cec5SDimitry Andric 82340b57cec5SDimitry Andric auto &Dep = DepInfo[I]; 82350b57cec5SDimitry Andric Triples += Action::GetOffloadKindName(Dep.DependentOffloadKind); 8236349cc55cSDimitry Andric Triples += '-'; 8237349cc55cSDimitry Andric Triples += Dep.DependentToolChain->getTriple().normalize(); 8238349cc55cSDimitry Andric if ((Dep.DependentOffloadKind == Action::OFK_HIP || 8239349cc55cSDimitry Andric Dep.DependentOffloadKind == Action::OFK_Cuda) && 8240349cc55cSDimitry Andric !Dep.DependentBoundArch.empty()) { 8241349cc55cSDimitry Andric Triples += '-'; 82420b57cec5SDimitry Andric Triples += Dep.DependentBoundArch; 82430b57cec5SDimitry Andric } 8244349cc55cSDimitry Andric // TODO: Replace parsing of -march flag. Can be done by storing GPUArch 8245349cc55cSDimitry Andric // with each toolchain. 8246349cc55cSDimitry Andric StringRef GPUArchName; 8247349cc55cSDimitry Andric if (Dep.DependentOffloadKind == Action::OFK_OpenMP) { 8248349cc55cSDimitry Andric // Extract GPUArch from -march argument in TC argument list. 8249349cc55cSDimitry Andric for (unsigned ArgIndex = 0; ArgIndex < TCArgs.size(); ArgIndex++) { 8250349cc55cSDimitry Andric StringRef ArchStr = StringRef(TCArgs.getArgString(ArgIndex)); 8251349cc55cSDimitry Andric auto Arch = ArchStr.startswith_insensitive("-march="); 8252349cc55cSDimitry Andric if (Arch) { 8253349cc55cSDimitry Andric GPUArchName = ArchStr.substr(7); 8254349cc55cSDimitry Andric Triples += "-"; 8255349cc55cSDimitry Andric break; 8256349cc55cSDimitry Andric } 8257349cc55cSDimitry Andric } 8258349cc55cSDimitry Andric Triples += GPUArchName.str(); 8259349cc55cSDimitry Andric } 82600b57cec5SDimitry Andric } 82610b57cec5SDimitry Andric 82620b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(Triples)); 82630b57cec5SDimitry Andric 82640b57cec5SDimitry Andric // Get bundled file command. 82650b57cec5SDimitry Andric CmdArgs.push_back( 826681ad6265SDimitry Andric TCArgs.MakeArgString(Twine("-input=") + Input.getFilename())); 82670b57cec5SDimitry Andric 82680b57cec5SDimitry Andric // Get unbundled files command. 82690b57cec5SDimitry Andric for (unsigned I = 0; I < Outputs.size(); ++I) { 827081ad6265SDimitry Andric SmallString<128> UB; 827181ad6265SDimitry Andric UB += "-output="; 82720b57cec5SDimitry Andric UB += DepInfo[I].DependentToolChain->getInputFilename(Outputs[I]); 82730b57cec5SDimitry Andric CmdArgs.push_back(TCArgs.MakeArgString(UB)); 827481ad6265SDimitry Andric } 82750b57cec5SDimitry Andric CmdArgs.push_back("-unbundle"); 8276e8d8bef9SDimitry Andric CmdArgs.push_back("-allow-missing-bundles"); 82770b57cec5SDimitry Andric 82780b57cec5SDimitry Andric // All the inputs are encoded as commands. 8279a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 82805ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 82810b57cec5SDimitry Andric TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8282e8d8bef9SDimitry Andric CmdArgs, None, Outputs)); 82830b57cec5SDimitry Andric } 8284a7dea167SDimitry Andric 8285a7dea167SDimitry Andric void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA, 8286a7dea167SDimitry Andric const InputInfo &Output, 8287a7dea167SDimitry Andric const InputInfoList &Inputs, 8288a7dea167SDimitry Andric const ArgList &Args, 8289a7dea167SDimitry Andric const char *LinkingOutput) const { 8290a7dea167SDimitry Andric ArgStringList CmdArgs; 8291a7dea167SDimitry Andric 8292a7dea167SDimitry Andric const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 8293a7dea167SDimitry Andric 8294a7dea167SDimitry Andric // Add the "effective" target triple. 8295a7dea167SDimitry Andric CmdArgs.push_back("-target"); 8296a7dea167SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Triple.getTriple())); 8297a7dea167SDimitry Andric 8298a7dea167SDimitry Andric // Add the output file name. 8299a7dea167SDimitry Andric assert(Output.isFilename() && "Invalid output."); 8300a7dea167SDimitry Andric CmdArgs.push_back("-o"); 8301a7dea167SDimitry Andric CmdArgs.push_back(Output.getFilename()); 8302a7dea167SDimitry Andric 8303a7dea167SDimitry Andric // Add inputs. 8304a7dea167SDimitry Andric for (const InputInfo &I : Inputs) { 8305a7dea167SDimitry Andric assert(I.isFilename() && "Invalid input."); 8306a7dea167SDimitry Andric CmdArgs.push_back(I.getFilename()); 8307a7dea167SDimitry Andric } 8308a7dea167SDimitry Andric 8309a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>( 83105ffd83dbSDimitry Andric JA, *this, ResponseFileSupport::None(), 8311a7dea167SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), 8312e8d8bef9SDimitry Andric CmdArgs, Inputs, Output)); 8313a7dea167SDimitry Andric } 83141fd87a68SDimitry Andric 831581ad6265SDimitry Andric void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA, 831681ad6265SDimitry Andric const InputInfo &Output, 831781ad6265SDimitry Andric const InputInfoList &Inputs, 831881ad6265SDimitry Andric const llvm::opt::ArgList &Args, 831981ad6265SDimitry Andric const char *LinkingOutput) const { 832081ad6265SDimitry Andric ArgStringList CmdArgs; 832181ad6265SDimitry Andric 832281ad6265SDimitry Andric // Add the output file name. 832381ad6265SDimitry Andric assert(Output.isFilename() && "Invalid output."); 832481ad6265SDimitry Andric CmdArgs.push_back("-o"); 832581ad6265SDimitry Andric CmdArgs.push_back(Output.getFilename()); 832681ad6265SDimitry Andric 832781ad6265SDimitry Andric // Create the inputs to bundle the needed metadata. 832881ad6265SDimitry Andric for (const InputInfo &Input : Inputs) { 832981ad6265SDimitry Andric const Action *OffloadAction = Input.getAction(); 833081ad6265SDimitry Andric const ToolChain *TC = OffloadAction->getOffloadingToolChain(); 833181ad6265SDimitry Andric const ArgList &TCArgs = 833281ad6265SDimitry Andric C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(), 833381ad6265SDimitry Andric OffloadAction->getOffloadingDeviceKind()); 833481ad6265SDimitry Andric StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input)); 833581ad6265SDimitry Andric StringRef Arch = (OffloadAction->getOffloadingArch()) 833681ad6265SDimitry Andric ? OffloadAction->getOffloadingArch() 833781ad6265SDimitry Andric : TCArgs.getLastArgValue(options::OPT_march_EQ); 833881ad6265SDimitry Andric StringRef Kind = 833981ad6265SDimitry Andric Action::GetOffloadKindName(OffloadAction->getOffloadingDeviceKind()); 834081ad6265SDimitry Andric 834181ad6265SDimitry Andric ArgStringList Features; 834281ad6265SDimitry Andric SmallVector<StringRef> FeatureArgs; 8343753f127fSDimitry Andric getTargetFeatures(TC->getDriver(), TC->getTriple(), TCArgs, Features, 8344753f127fSDimitry Andric false); 834581ad6265SDimitry Andric llvm::copy_if(Features, std::back_inserter(FeatureArgs), 834681ad6265SDimitry Andric [](StringRef Arg) { return !Arg.startswith("-target"); }); 834781ad6265SDimitry Andric 834881ad6265SDimitry Andric SmallVector<std::string> Parts{ 834981ad6265SDimitry Andric "file=" + File.str(), 835081ad6265SDimitry Andric "triple=" + TC->getTripleString(), 835181ad6265SDimitry Andric "arch=" + Arch.str(), 835281ad6265SDimitry Andric "kind=" + Kind.str(), 835381ad6265SDimitry Andric }; 835481ad6265SDimitry Andric 835581ad6265SDimitry Andric if (TC->getDriver().isUsingLTO(/* IsOffload */ true)) 835681ad6265SDimitry Andric for (StringRef Feature : FeatureArgs) 835781ad6265SDimitry Andric Parts.emplace_back("feature=" + Feature.str()); 835881ad6265SDimitry Andric 835981ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString("--image=" + llvm::join(Parts, ","))); 836081ad6265SDimitry Andric } 836181ad6265SDimitry Andric 836281ad6265SDimitry Andric C.addCommand(std::make_unique<Command>( 836381ad6265SDimitry Andric JA, *this, ResponseFileSupport::None(), 836481ad6265SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath(getShortName())), 836581ad6265SDimitry Andric CmdArgs, Inputs, Output)); 836681ad6265SDimitry Andric } 836781ad6265SDimitry Andric 83681fd87a68SDimitry Andric void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, 83691fd87a68SDimitry Andric const InputInfo &Output, 83701fd87a68SDimitry Andric const InputInfoList &Inputs, 83711fd87a68SDimitry Andric const ArgList &Args, 83721fd87a68SDimitry Andric const char *LinkingOutput) const { 8373d56accc7SDimitry Andric const Driver &D = getToolChain().getDriver(); 8374d56accc7SDimitry Andric const llvm::Triple TheTriple = getToolChain().getTriple(); 8375d56accc7SDimitry Andric auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); 83761fd87a68SDimitry Andric ArgStringList CmdArgs; 83771fd87a68SDimitry Andric 8378d56accc7SDimitry Andric // Pass the CUDA path to the linker wrapper tool. 837981ad6265SDimitry Andric for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP}) { 838081ad6265SDimitry Andric auto TCRange = C.getOffloadToolChains(Kind); 838181ad6265SDimitry Andric for (auto &I : llvm::make_range(TCRange.first, TCRange.second)) { 8382d56accc7SDimitry Andric const ToolChain *TC = I.second; 8383d56accc7SDimitry Andric if (TC->getTriple().isNVPTX()) { 8384d56accc7SDimitry Andric CudaInstallationDetector CudaInstallation(D, TheTriple, Args); 8385d56accc7SDimitry Andric if (CudaInstallation.isValid()) 8386d56accc7SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8387d56accc7SDimitry Andric "--cuda-path=" + CudaInstallation.getInstallPath())); 8388d56accc7SDimitry Andric break; 8389d56accc7SDimitry Andric } 8390d56accc7SDimitry Andric } 83911fd87a68SDimitry Andric } 83921fd87a68SDimitry Andric 839381ad6265SDimitry Andric // Get the AMDGPU math libraries. 839481ad6265SDimitry Andric // FIXME: This method is bad, remove once AMDGPU has a proper math library 839581ad6265SDimitry Andric // (see AMDGCN::OpenMPLinker::constructLLVMLinkCommand). 839681ad6265SDimitry Andric for (auto &I : llvm::make_range(OpenMPTCRange.first, OpenMPTCRange.second)) { 83971fd87a68SDimitry Andric const ToolChain *TC = I.second; 839881ad6265SDimitry Andric 839981ad6265SDimitry Andric if (!TC->getTriple().isAMDGPU() || Args.hasArg(options::OPT_nogpulib)) 840081ad6265SDimitry Andric continue; 840181ad6265SDimitry Andric 84021fd87a68SDimitry Andric const ArgList &TCArgs = C.getArgsForToolChain(TC, "", Action::OFK_OpenMP); 84031fd87a68SDimitry Andric StringRef Arch = TCArgs.getLastArgValue(options::OPT_march_EQ); 840481ad6265SDimitry Andric const toolchains::ROCMToolChain RocmTC(TC->getDriver(), TC->getTriple(), 840581ad6265SDimitry Andric TCArgs); 84061fd87a68SDimitry Andric 840781ad6265SDimitry Andric SmallVector<std::string, 12> BCLibs = 840881ad6265SDimitry Andric RocmTC.getCommonDeviceLibNames(TCArgs, Arch.str()); 84091fd87a68SDimitry Andric 841081ad6265SDimitry Andric for (StringRef LibName : BCLibs) 841181ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8412753f127fSDimitry Andric "--bitcode-library=" + Action::GetOffloadKindName(Action::OFK_OpenMP) + 841381ad6265SDimitry Andric "-" + TC->getTripleString() + "-" + Arch + "=" + LibName)); 84141fd87a68SDimitry Andric } 84151fd87a68SDimitry Andric 841681ad6265SDimitry Andric if (D.isUsingLTO(/* IsOffload */ true)) { 84171fd87a68SDimitry Andric // Pass in the optimization level to use for LTO. 84181fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 84191fd87a68SDimitry Andric StringRef OOpt; 84201fd87a68SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 84211fd87a68SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 84221fd87a68SDimitry Andric OOpt = "3"; 84231fd87a68SDimitry Andric else if (A->getOption().matches(options::OPT_O)) { 84241fd87a68SDimitry Andric OOpt = A->getValue(); 84251fd87a68SDimitry Andric if (OOpt == "g") 84261fd87a68SDimitry Andric OOpt = "1"; 84271fd87a68SDimitry Andric else if (OOpt == "s" || OOpt == "z") 84281fd87a68SDimitry Andric OOpt = "2"; 84291fd87a68SDimitry Andric } else if (A->getOption().matches(options::OPT_O0)) 84301fd87a68SDimitry Andric OOpt = "0"; 84311fd87a68SDimitry Andric if (!OOpt.empty()) 8432753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--opt-level=O") + OOpt)); 84331fd87a68SDimitry Andric } 84341fd87a68SDimitry Andric } 84351fd87a68SDimitry Andric 8436753f127fSDimitry Andric CmdArgs.push_back( 8437753f127fSDimitry Andric Args.MakeArgString("--host-triple=" + TheTriple.getTriple())); 84381fd87a68SDimitry Andric if (Args.hasArg(options::OPT_v)) 8439753f127fSDimitry Andric CmdArgs.push_back("--verbose"); 84401fd87a68SDimitry Andric 84411fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 8442753f127fSDimitry Andric if (!A->getOption().matches(options::OPT_g0)) 8443753f127fSDimitry Andric CmdArgs.push_back("--device-debug"); 84441fd87a68SDimitry Andric } 84451fd87a68SDimitry Andric 84461fd87a68SDimitry Andric for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 8447753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString("--ptxas-args=" + A)); 84481fd87a68SDimitry Andric 84491fd87a68SDimitry Andric // Forward remarks passes to the LLVM backend in the wrapper. 84501fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) 84511fd87a68SDimitry Andric CmdArgs.push_back( 8452753f127fSDimitry Andric Args.MakeArgString(Twine("--pass-remarks=") + A->getValue())); 84531fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) 84541fd87a68SDimitry Andric CmdArgs.push_back( 8455753f127fSDimitry Andric Args.MakeArgString(Twine("--pass-remarks-missed=") + A->getValue())); 84561fd87a68SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) 84571fd87a68SDimitry Andric CmdArgs.push_back( 8458753f127fSDimitry Andric Args.MakeArgString(Twine("--pass-remarks-analysis=") + A->getValue())); 84591fd87a68SDimitry Andric if (Args.getLastArg(options::OPT_save_temps_EQ)) 8460753f127fSDimitry Andric CmdArgs.push_back("--save-temps"); 84611fd87a68SDimitry Andric 8462d56accc7SDimitry Andric // Construct the link job so we can wrap around it. 8463d56accc7SDimitry Andric Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); 8464d56accc7SDimitry Andric const auto &LinkCommand = C.getJobs().getJobs().back(); 8465d56accc7SDimitry Andric 846681ad6265SDimitry Andric // Forward -Xoffload-linker<-triple> arguments to the device link job. 8467753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_Xoffload_linker)) { 8468753f127fSDimitry Andric StringRef Val = A->getValue(0); 846981ad6265SDimitry Andric if (Val.empty()) 847081ad6265SDimitry Andric CmdArgs.push_back( 8471753f127fSDimitry Andric Args.MakeArgString(Twine("--device-linker=") + A->getValue(1))); 847281ad6265SDimitry Andric else 847381ad6265SDimitry Andric CmdArgs.push_back(Args.MakeArgString( 8474753f127fSDimitry Andric "--device-linker=" + 847581ad6265SDimitry Andric ToolChain::getOpenMPTriple(Val.drop_front()).getTriple() + "=" + 8476753f127fSDimitry Andric A->getValue(1))); 847781ad6265SDimitry Andric } 847881ad6265SDimitry Andric Args.ClaimAllArgs(options::OPT_Xoffload_linker); 847981ad6265SDimitry Andric 8480753f127fSDimitry Andric // Forward `-mllvm` arguments to the LLVM invocations if present. 8481753f127fSDimitry Andric for (Arg *A : Args.filtered(options::OPT_mllvm)) { 8482753f127fSDimitry Andric CmdArgs.push_back("-mllvm"); 8483753f127fSDimitry Andric CmdArgs.push_back(A->getValue()); 8484753f127fSDimitry Andric A->claim(); 8485753f127fSDimitry Andric } 8486753f127fSDimitry Andric 84871fd87a68SDimitry Andric // Add the linker arguments to be forwarded by the wrapper. 8488753f127fSDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("--linker-path=") + 8489753f127fSDimitry Andric LinkCommand->getExecutable())); 84901fd87a68SDimitry Andric CmdArgs.push_back("--"); 84911fd87a68SDimitry Andric for (const char *LinkArg : LinkCommand->getArguments()) 84921fd87a68SDimitry Andric CmdArgs.push_back(LinkArg); 84931fd87a68SDimitry Andric 84941fd87a68SDimitry Andric const char *Exec = 84951fd87a68SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper")); 84961fd87a68SDimitry Andric 84971fd87a68SDimitry Andric // Replace the executable and arguments of the link job with the 84981fd87a68SDimitry Andric // wrapper. 84991fd87a68SDimitry Andric LinkCommand->replaceExecutable(Exec); 85001fd87a68SDimitry Andric LinkCommand->replaceArguments(CmdArgs); 85011fd87a68SDimitry Andric } 8502