[clang] 0a9d740 - [clang][Driver] Update/cleanup LTO logic to ensure that the last lto argument is honored
Usman Nadeem via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 8 15:54:14 PDT 2021
Author: Usman Nadeem
Date: 2021-09-08T15:53:49-07:00
New Revision: 0a9d740c232e1e4b71fcbbaf844211d70b06d873
URL: https://github.com/llvm/llvm-project/commit/0a9d740c232e1e4b71fcbbaf844211d70b06d873
DIFF: https://github.com/llvm/llvm-project/commit/0a9d740c232e1e4b71fcbbaf844211d70b06d873.diff
LOG: [clang][Driver] Update/cleanup LTO logic to ensure that the last lto argument is honored
- Make flto an alias of flto=full.
- Make foffload-lto an alias of foffload-lto=full.
- Make flto_EQ_jobserver, flto_EQ_auto aliases of flto=full,
since they are being treated as full lto right now.
- Clean up the code for parseLTOMode and setLTOMode.
- Replace uses of OPT_flto with OPT_flto_EQ since they alias now.
Differential Revision: https://reviews.llvm.org/D108881
Change-Id: I5d867db83a680434fba5c8d85c9a83135d3b81ee
Added:
Modified:
clang/include/clang/Driver/Options.td
clang/lib/Driver/Driver.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/test/Driver/amdgpu-toolchain.c
clang/test/Driver/lto.c
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 0b374fa4db410..8ab3a5156e075 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2017,16 +2017,18 @@ def fapple_link_rtlib : Flag<["-"], "fapple-link-rtlib">, Group<f_Group>,
HelpText<"Force linking the clang builtins runtime library">;
def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">;
-def flto_EQ_jobserver : Flag<["-"], "flto=jobserver">, Group<f_Group>;
-def flto_EQ_auto : Flag<["-"], "flto=auto">, Group<f_Group>;
+def flto_EQ_jobserver : Flag<["-"], "flto=jobserver">, Group<f_Group>,
+ Alias<flto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">;
+def flto_EQ_auto : Flag<["-"], "flto=auto">, Group<f_Group>,
+ Alias<flto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">;
def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
- HelpText<"Enable LTO in 'full' mode">;
+ Alias<flto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">;
def fno_lto : Flag<["-"], "fno-lto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
HelpText<"Disable LTO mode (default)">;
def foffload_lto_EQ : Joined<["-"], "foffload-lto=">, Flags<[CoreOption]>, Group<f_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<f_Group>,
- HelpText<"Enable LTO in 'full' mode for offload compilation">;
+ Alias<foffload_lto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode for offload compilation">;
def fno_offload_lto : Flag<["-"], "fno-offload-lto">, Flags<[CoreOption]>, Group<f_Group>,
HelpText<"Disable LTO mode (default) for offload compilation">;
def flto_jobs_EQ : Joined<["-"], "flto-jobs=">,
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a92b6e8c0908b..7dea484267dcd 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -585,53 +585,34 @@ static llvm::Triple computeTargetTriple(const Driver &D,
// 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<driver::LTOKind>
-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<LTOKind>(LTOName)
- .Case("full", LTOK_Full)
- .Case("thin", LTOK_Thin)
- .Default(LTOK_Unknown);
+ driver::LTOKind LTOMode = llvm::StringSwitch<LTOKind>(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.
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 5d817aa480bf4..be2bb90560cd8 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4474,28 +4474,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
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");
}
}
}
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 014595c65acc9..c23740fd72e95 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1419,7 +1419,7 @@ void CompilerInvocation::GenerateCodeGenArgs(
}
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 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
}
}
- 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;
diff --git a/clang/test/Driver/amdgpu-toolchain.c b/clang/test/Driver/amdgpu-toolchain.c
index cb92744eee6a3..bea7ef7dea034 100644
--- a/clang/test/Driver/amdgpu-toolchain.c
+++ b/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{{.*}}
diff --git a/clang/test/Driver/lto.c b/clang/test/Driver/lto.c
index 342dd25412740..b1d729a1ece70 100644
--- a/clang/test/Driver/lto.c
+++ b/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
More information about the cfe-commits
mailing list