Author: Aaron Puchert Date: 2024-12-12T15:38:37+01:00 New Revision: bae383ba6b53b0d8257c83f99ceecdd751d0a378
URL: https://github.com/llvm/llvm-project/commit/bae383ba6b53b0d8257c83f99ceecdd751d0a378 DIFF: https://github.com/llvm/llvm-project/commit/bae383ba6b53b0d8257c83f99ceecdd751d0a378.diff LOG: [Driver] Cache SanitizerArgs (NFC) (#119442) The name getSanitizerArgs seems to mislead callers that this is a cheap function, but it extracts the SanitizerArgs each time it is called. So we try to reuse it a bit more. Added: Modified: clang/lib/Driver/ToolChains/CommonArgs.cpp clang/lib/Driver/ToolChains/CommonArgs.h clang/lib/Driver/ToolChains/FreeBSD.cpp clang/lib/Driver/ToolChains/Fuchsia.cpp clang/lib/Driver/ToolChains/Gnu.cpp clang/lib/Driver/ToolChains/Hexagon.cpp clang/lib/Driver/ToolChains/NetBSD.cpp clang/lib/Driver/ToolChains/OpenBSD.cpp clang/lib/Driver/ToolChains/Solaris.cpp Removed: ################################################################################ diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 3c78b12b0741e0..0d851314a89539 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1417,6 +1417,7 @@ void tools::addAsNeededOption(const ToolChain &TC, void tools::linkSanitizerRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args, + const SanitizerArgs &SanArgs, ArgStringList &CmdArgs) { // Force linking against the system libraries sanitizers depends on // (see PR15823 why this is necessary). @@ -1443,18 +1444,18 @@ void tools::linkSanitizerRuntimeDeps(const ToolChain &TC, // libresolv.a, even if exists, is an empty archive to satisfy POSIX -lresolv // requirement. if (TC.getTriple().isOSLinux() && !TC.getTriple().isAndroid() && - !TC.getTriple().isMusl() && TC.getSanitizerArgs(Args).needsMsanRt()) + !TC.getTriple().isMusl() && SanArgs.needsMsanRt()) CmdArgs.push_back("-lresolv"); } static void collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, + const SanitizerArgs &SanArgs, SmallVectorImpl<StringRef> &SharedRuntimes, SmallVectorImpl<StringRef> &StaticRuntimes, SmallVectorImpl<StringRef> &NonWholeStaticRuntimes, SmallVectorImpl<StringRef> &HelperStaticRuntimes, SmallVectorImpl<StringRef> &RequiredSymbols) { - const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); // Collect shared runtimes. if (SanArgs.needsSharedRt()) { if (SanArgs.needsAsanRt()) { @@ -1588,12 +1589,12 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, // Should be called before we add system libraries (C++ ABI, libstdc++/libc++, // C runtime, etc). Returns true if sanitizer system deps need to be linked in. bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, + const SanitizerArgs &SanArgs, ArgStringList &CmdArgs) { - const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes, NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols; if (SanArgs.linkRuntimes()) { - collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes, + collectSanitizerRuntimes(TC, Args, SanArgs, SharedRuntimes, StaticRuntimes, NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols); } diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h index b6ddd99b872798..de2d143b904790 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.h +++ b/clang/lib/Driver/ToolChains/CommonArgs.h @@ -38,10 +38,12 @@ void addLinkerCompressDebugSectionsOption(const ToolChain &TC, void claimNoWarnArgs(const llvm::opt::ArgList &Args); bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, + const SanitizerArgs &SanArgs, llvm::opt::ArgStringList &CmdArgs); void linkSanitizerRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args, + const SanitizerArgs &SanArgs, llvm::opt::ArgStringList &CmdArgs); bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp index 3d744bc087f467..c78f4e26a2f107 100644 --- a/clang/lib/Driver/ToolChains/FreeBSD.cpp +++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp @@ -283,7 +283,9 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, D.getLTOMode() == LTOK_Thin); } - bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); + bool NeedsSanitizerDeps = + addSanitizerRuntimes(ToolChain, Args, SanArgs, CmdArgs); bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); @@ -324,7 +326,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, } if (NeedsSanitizerDeps) - linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs); + linkSanitizerRuntimeDeps(ToolChain, Args, SanArgs, CmdArgs); if (NeedsXRayDeps) linkXRayRuntimeDeps(ToolChain, Args, CmdArgs); // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding diff --git a/clang/lib/Driver/ToolChains/Fuchsia.cpp b/clang/lib/Driver/ToolChains/Fuchsia.cpp index c2badc80a7b452..52d58431d4ae12 100644 --- a/clang/lib/Driver/ToolChains/Fuchsia.cpp +++ b/clang/lib/Driver/ToolChains/Fuchsia.cpp @@ -184,7 +184,8 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA, // Note that Fuchsia never needs to link in sanitizer runtime deps. Any // sanitizer runtimes with system dependencies use the `.deplibs` feature // instead. - addSanitizerRuntimes(ToolChain, Args, CmdArgs); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); + addSanitizerRuntimes(ToolChain, Args, SanArgs, CmdArgs); addXRayRuntime(ToolChain, Args, CmdArgs); @@ -317,10 +318,9 @@ Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple, Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, true); addMultilibFlag(Exceptions, "-fexceptions", Flags); addMultilibFlag(!Exceptions, "-fno-exceptions", Flags); - addMultilibFlag(getSanitizerArgs(Args).needsAsanRt(), "-fsanitize=address", - Flags); - addMultilibFlag(getSanitizerArgs(Args).needsHwasanRt(), - "-fsanitize=hwaddress", Flags); + const SanitizerArgs &SanArgs = getSanitizerArgs(Args); + addMultilibFlag(SanArgs.needsAsanRt(), "-fsanitize=address", Flags); + addMultilibFlag(SanArgs.needsHwasanRt(), "-fsanitize=hwaddress", Flags); addMultilibFlag(Args.getLastArgValue(options::OPT_fcxx_abi_EQ) == "itanium", "-fc++-abi=itanium", Flags); diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 8397f1121ec2ce..9eb3d3125772cc 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -23,6 +23,7 @@ #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/MultilibBuilder.h" #include "clang/Driver/Options.h" +#include "clang/Driver/SanitizerArgs.h" #include "clang/Driver/Tool.h" #include "clang/Driver/ToolChain.h" #include "llvm/ADT/StringSet.h" @@ -538,7 +539,9 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) CmdArgs.push_back("--no-demangle"); - bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); + bool NeedsSanitizerDeps = + addSanitizerRuntimes(ToolChain, Args, SanArgs, CmdArgs); bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); @@ -583,7 +586,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("--start-group"); if (NeedsSanitizerDeps) - linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs); + linkSanitizerRuntimeDeps(ToolChain, Args, SanArgs, CmdArgs); if (NeedsXRayDeps) linkXRayRuntimeDeps(ToolChain, Args, CmdArgs); diff --git a/clang/lib/Driver/ToolChains/Hexagon.cpp b/clang/lib/Driver/ToolChains/Hexagon.cpp index 76cedf312d68a1..18900430313b59 100644 --- a/clang/lib/Driver/ToolChains/Hexagon.cpp +++ b/clang/lib/Driver/ToolChains/Hexagon.cpp @@ -13,6 +13,7 @@ #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/InputInfo.h" #include "clang/Driver/Options.h" +#include "clang/Driver/SanitizerArgs.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/FileSystem.h" @@ -215,7 +216,8 @@ void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA, "-mcpu=hexagon" + toolchains::HexagonToolChain::GetTargetCPUVersion(Args))); - addSanitizerRuntimes(HTC, Args, CmdArgs); + SanitizerArgs SanArgs = HTC.getSanitizerArgs(Args); + addSanitizerRuntimes(HTC, Args, SanArgs, CmdArgs); assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); if (Output.isFilename()) { @@ -301,7 +303,8 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA, bool UseShared = IsShared && !IsStatic; StringRef CpuVer = toolchains::HexagonToolChain::GetTargetCPUVersion(Args); - bool NeedsSanitizerDeps = addSanitizerRuntimes(HTC, Args, CmdArgs); + const SanitizerArgs &SanArgs = HTC.getSanitizerArgs(Args); + bool NeedsSanitizerDeps = addSanitizerRuntimes(HTC, Args, SanArgs, CmdArgs); bool NeedsXRayDeps = addXRayRuntime(HTC, Args, CmdArgs); //---------------------------------------------------------------------------- @@ -371,7 +374,7 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA, if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { if (NeedsSanitizerDeps) { - linkSanitizerRuntimeDeps(HTC, Args, CmdArgs); + linkSanitizerRuntimeDeps(HTC, Args, SanArgs, CmdArgs); if (UNW != ToolChain::UNW_None) CmdArgs.push_back("-lunwind"); diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp b/clang/lib/Driver/ToolChains/NetBSD.cpp index abd5e1aa003b38..0ead1c610ba0df 100644 --- a/clang/lib/Driver/ToolChains/NetBSD.cpp +++ b/clang/lib/Driver/ToolChains/NetBSD.cpp @@ -274,11 +274,12 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_s, options::OPT_t}); ToolChain.AddFilePathLibArgs(Args, CmdArgs); - bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); + bool NeedsSanitizerDeps = + addSanitizerRuntimes(ToolChain, Args, SanArgs, CmdArgs); bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); - const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); if (SanArgs.needsSharedRt()) { CmdArgs.push_back("-rpath"); CmdArgs.push_back(Args.MakeArgString(ToolChain.getCompilerRTPath())); @@ -334,7 +335,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, } if (NeedsSanitizerDeps) - linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs); + linkSanitizerRuntimeDeps(ToolChain, Args, SanArgs, CmdArgs); if (NeedsXRayDeps) linkXRayRuntimeDeps(ToolChain, Args, CmdArgs); if (Args.hasArg(options::OPT_pthread)) diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp index f668a11e78f81d..b7dcffb3f5ee0a 100644 --- a/clang/lib/Driver/ToolChains/OpenBSD.cpp +++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp @@ -213,7 +213,9 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, D.getLTOMode() == LTOK_Thin); } - bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); + bool NeedsSanitizerDeps = + addSanitizerRuntimes(ToolChain, Args, SanArgs, CmdArgs); bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); @@ -251,7 +253,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, if (NeedsSanitizerDeps) { CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins")); - linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs); + linkSanitizerRuntimeDeps(ToolChain, Args, SanArgs, CmdArgs); } if (NeedsXRayDeps) { CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins")); diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp index fd3232b7c1b06f..83e83835eb9153 100644 --- a/clang/lib/Driver/ToolChains/Solaris.cpp +++ b/clang/lib/Driver/ToolChains/Solaris.cpp @@ -203,7 +203,8 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA, Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group}); - bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); + const SanitizerArgs &SA = ToolChain.getSanitizerArgs(Args); + bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, SA, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, @@ -250,9 +251,8 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA, if (!Args.hasArg(options::OPT_shared)) { CmdArgs.push_back("-lgcc"); } - const SanitizerArgs &SA = ToolChain.getSanitizerArgs(Args); if (NeedsSanitizerDeps) { - linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs); + linkSanitizerRuntimeDeps(ToolChain, Args, SA, CmdArgs); // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly. // However, ld -z relax=transtls is available since Solaris 11.2, but not _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits