Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -600,8 +600,9 @@ StringRef LTOName("full"); - const Arg *A = Args.getLastArg(OptEq); - if (A) + const Arg *A = Args.getLastArg(OptEq, OptPos); + // Use the OptEq only if it is after the -flto option. + if (A && A->getOption().matches(OptEq)) LTOName = A->getValue(); LTOMode = llvm::StringSwitch(LTOName) Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -4491,18 +4491,8 @@ 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 + // Only AMDGPU supports device-side LTO. + if (!IsDeviceOffloadAction || Triple.isAMDGPU()) { assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); CmdArgs.push_back(Args.MakeArgString( Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); 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,21 @@ // 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 2>&1 | \ +// RUN: FileCheck --check-prefix=FLTO-FULL %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