Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2017,16 +2017,18 @@ HelpText<"Force linking the clang builtins runtime library">; def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group, HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">; -def flto_EQ_jobserver : Flag<["-"], "flto=jobserver">, Group; -def flto_EQ_auto : Flag<["-"], "flto=auto">, Group; +def flto_EQ_jobserver : Flag<["-"], "flto=jobserver">, Group, + Alias, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">; +def flto_EQ_auto : Flag<["-"], "flto=auto">, Group, + Alias, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">; def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group, - HelpText<"Enable LTO in 'full' mode">; + Alias, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">; def fno_lto : Flag<["-"], "fno-lto">, Flags<[CoreOption, CC1Option]>, Group, HelpText<"Disable LTO mode (default)">; def foffload_lto_EQ : Joined<["-"], "foffload-lto=">, Flags<[CoreOption]>, Group, HelpText<"Set LTO mode to either 'full' or 'thin' for offload compilation">, Values<"thin,full">; def foffload_lto : Flag<["-"], "foffload-lto">, Flags<[CoreOption]>, Group, - HelpText<"Enable LTO in 'full' mode for offload compilation">; + Alias, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode for offload compilation">; def fno_offload_lto : Flag<["-"], "fno-offload-lto">, Flags<[CoreOption]>, Group, HelpText<"Disable LTO mode (default) for offload compilation">; def flto_jobs_EQ : Joined<["-"], "flto-jobs=">, Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -585,53 +585,34 @@ // Parse the LTO options and record the type of LTO compilation // based on which -f(no-)?lto(=.*)? or -f(no-)?offload-lto(=.*)? // option occurs last. -static llvm::Optional -parseLTOMode(Driver &D, const llvm::opt::ArgList &Args, OptSpecifier OptPos, - OptSpecifier OptNeg, OptSpecifier OptEq, bool IsOffload) { - driver::LTOKind LTOMode = LTOK_None; - // Non-offload LTO allows -flto=auto and -flto=jobserver. Offload LTO does - // not support those options. - if (!Args.hasFlag(OptPos, OptEq, OptNeg, false) && - (IsOffload || - (!Args.hasFlag(options::OPT_flto_EQ_auto, options::OPT_fno_lto, false) && - !Args.hasFlag(options::OPT_flto_EQ_jobserver, options::OPT_fno_lto, - false)))) - return None; - - StringRef LTOName("full"); +static driver::LTOKind parseLTOMode(Driver &D, const llvm::opt::ArgList &Args, + OptSpecifier OptEq, OptSpecifier OptNeg) { + if (!Args.hasFlag(OptEq, OptNeg, false)) + return LTOK_None; const Arg *A = Args.getLastArg(OptEq); - if (A) - LTOName = A->getValue(); + StringRef LTOName = A->getValue(); - LTOMode = llvm::StringSwitch(LTOName) - .Case("full", LTOK_Full) - .Case("thin", LTOK_Thin) - .Default(LTOK_Unknown); + driver::LTOKind LTOMode = llvm::StringSwitch(LTOName) + .Case("full", LTOK_Full) + .Case("thin", LTOK_Thin) + .Default(LTOK_Unknown); if (LTOMode == LTOK_Unknown) { - assert(A); D.Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() << A->getValue(); - return None; + return LTOK_None; } return LTOMode; } // Parse the LTO options. void Driver::setLTOMode(const llvm::opt::ArgList &Args) { - LTOMode = LTOK_None; - if (auto M = parseLTOMode(*this, Args, options::OPT_flto, - options::OPT_fno_lto, options::OPT_flto_EQ, - /*IsOffload=*/false)) - LTOMode = M.getValue(); - - OffloadLTOMode = LTOK_None; - if (auto M = parseLTOMode(*this, Args, options::OPT_foffload_lto, - options::OPT_fno_offload_lto, - options::OPT_foffload_lto_EQ, - /*IsOffload=*/true)) - OffloadLTOMode = M.getValue(); + LTOMode = + parseLTOMode(*this, Args, options::OPT_flto_EQ, options::OPT_fno_lto); + + OffloadLTOMode = parseLTOMode(*this, Args, options::OPT_foffload_lto_EQ, + options::OPT_fno_offload_lto); } /// Compute the desired OpenMP runtime from the flags provided. Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -4474,28 +4474,18 @@ CmdArgs.push_back("-emit-llvm-uselists"); if (IsUsingLTO) { - if (!IsDeviceOffloadAction) { - if (Args.hasArg(options::OPT_flto)) - CmdArgs.push_back("-flto"); - else { - if (D.getLTOMode() == LTOK_Thin) - CmdArgs.push_back("-flto=thin"); - else - CmdArgs.push_back("-flto=full"); - } - CmdArgs.push_back("-flto-unit"); - } else if (Triple.isAMDGPU()) { - // Only AMDGPU supports device-side LTO - assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); - CmdArgs.push_back(Args.MakeArgString( - Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); - CmdArgs.push_back("-flto-unit"); - } else { + // Only AMDGPU supports device-side LTO. + if (IsDeviceOffloadAction && !Triple.isAMDGPU()) { D.Diag(diag::err_drv_unsupported_opt_for_target) << Args.getLastArg(options::OPT_foffload_lto, options::OPT_foffload_lto_EQ) ->getAsString(Args) << Triple.getTriple(); + } else { + assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); + CmdArgs.push_back(Args.MakeArgString( + Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); + CmdArgs.push_back("-flto-unit"); } } } Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -1419,7 +1419,7 @@ } if (Opts.PrepareForLTO && !Opts.PrepareForThinLTO) - GenerateArg(Args, OPT_flto, SA); + GenerateArg(Args, OPT_flto_EQ, "full", SA); if (Opts.PrepareForThinLTO) GenerateArg(Args, OPT_flto_EQ, "thin", SA); @@ -1706,9 +1706,10 @@ } } - Opts.PrepareForLTO = Args.hasArg(OPT_flto, OPT_flto_EQ); + Opts.PrepareForLTO = false; Opts.PrepareForThinLTO = false; if (Arg *A = Args.getLastArg(OPT_flto_EQ)) { + Opts.PrepareForLTO = true; StringRef S = A->getValue(); if (S == "thin") Opts.PrepareForThinLTO = true; Index: clang/test/Driver/amdgpu-toolchain.c =================================================================== --- clang/test/Driver/amdgpu-toolchain.c +++ clang/test/Driver/amdgpu-toolchain.c @@ -12,5 +12,5 @@ // RUN: %clang -### -target amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \ // RUN: -flto %s 2>&1 | FileCheck -check-prefix=LTO %s -// LTO: clang{{.*}} "-flto" +// LTO: clang{{.*}} "-flto=full" // LTO: ld.lld{{.*}} Index: clang/test/Driver/lto.c =================================================================== --- clang/test/Driver/lto.c +++ clang/test/Driver/lto.c @@ -85,3 +85,23 @@ // FLTO-AUTO: -flto=full // FLTO-JOBSERVER: -flto=full // + +// Pass the last -flto argument. +// RUN: %clang -target x86_64-unknown-linux -### %s -flto=thin -flto 2>&1 | \ +// RUN: FileCheck --check-prefix=FLTO-FULL %s +// RUN: %clang -target x86_64-unknown-linux -### %s -flto=thin -flto=full \ +// RUN: 2>&1 | FileCheck --check-prefix=FLTO-FULL %s +// RUN: %clang -target x86_64-unknown-linux -### %s -flto=full -flto=thin \ +// RUN: 2>&1 | FileCheck --check-prefix=FLTO-THIN %s +// RUN: %clang -target x86_64-unknown-linux -### %s -flto -flto=thin 2>&1 | \ +// RUN: FileCheck --check-prefix=FLTO-THIN %s +// +// FLTO-FULL-NOT: -flto=thin +// FLTO-FULL: -flto=full +// FLTO-FULL-NOT: -flto=thin +// +// FLTO-THIN-NOT: -flto=full +// FLTO-THIN-NOT: "-flto" +// FLTO-THIN: -flto=thin +// FLTO-THIN-NOT: "-flto" +// FLTO-THIN-NOT: -flto=full \ No newline at end of file