Index: clang/include/clang/Driver/ToolChain.h =================================================================== --- clang/include/clang/Driver/ToolChain.h +++ clang/include/clang/Driver/ToolChain.h @@ -162,7 +162,8 @@ Tool *getOffloadBundler() const; Tool *getOffloadWrapper() const; - mutable std::unique_ptr SanitizerArguments; + mutable std::map> + SanArgs; mutable std::unique_ptr XRayArguments; /// The effective clang triple for the current Job. @@ -266,7 +267,8 @@ const Multilib &getMultilib() const { return SelectedMultilib; } - const SanitizerArgs& getSanitizerArgs() const; + const SanitizerArgs & + getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const; const XRayArgs& getXRayArgs() const; @@ -485,15 +487,15 @@ virtual bool isPICDefault() const = 0; /// Test whether this toolchain defaults to PIE. - virtual bool isPIEDefault() const = 0; + virtual bool isPIEDefault(const llvm::opt::ArgList &Args) const = 0; /// Test whether this toolchaind defaults to non-executable stacks. virtual bool isNoExecStackDefault() const; /// Tests whether this toolchain forces its default for PIC, PIE or /// non-PIC. If this returns true, any PIC related flags should be ignored - /// and instead the results of \c isPICDefault() and \c isPIEDefault() are - /// used exclusively. + /// and instead the results of \c isPICDefault() and \c isPIEDefault(const + /// llvm::opt::ArgList &Args) are used exclusively. virtual bool isPICDefaultForced() const = 0; /// SupportsProfiling - Does this tool chain support -pg. Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -2909,7 +2909,6 @@ class HIPActionBuilder final : public CudaActionBuilderBase { /// The linker inputs obtained for each device arch. SmallVector DeviceLinkerInputs; - bool GPUSanitize; // The default bundling behavior depends on the type of output, therefore // BundleOutput needs to be tri-value: None, true, or false. // Bundle code objects except --no-gpu-output is specified for device @@ -2922,8 +2921,6 @@ const Driver::InputList &Inputs) : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) { DefaultCudaArch = CudaArch::GFX803; - GPUSanitize = Args.hasFlag(options::OPT_fgpu_sanitize, - options::OPT_fno_gpu_sanitize, false); if (Args.hasArg(options::OPT_gpu_bundle_output, options::OPT_no_gpu_bundle_output)) BundleOutput = Args.hasFlag(options::OPT_gpu_bundle_output, Index: clang/lib/Driver/ToolChain.cpp =================================================================== --- clang/lib/Driver/ToolChain.cpp +++ clang/lib/Driver/ToolChain.cpp @@ -114,10 +114,13 @@ return false; } -const SanitizerArgs& ToolChain::getSanitizerArgs() const { - if (!SanitizerArguments.get()) - SanitizerArguments.reset(new SanitizerArgs(*this, Args)); - return *SanitizerArguments.get(); +const SanitizerArgs & +ToolChain::getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const { + auto Loc = SanArgs.find(&JobArgs); + if (Loc != SanArgs.end()) + return *Loc->second; + SanArgs[&JobArgs] = std::make_unique(*this, JobArgs); + return *SanArgs[&JobArgs]; } const XRayArgs& ToolChain::getXRayArgs() const { Index: clang/lib/Driver/ToolChains/AIX.h =================================================================== --- clang/lib/Driver/ToolChains/AIX.h +++ clang/lib/Driver/ToolChains/AIX.h @@ -63,7 +63,9 @@ return ParseInlineAsmUsingAsmParser; } bool isPICDefault() const override { return true; } - bool isPIEDefault() const override { return false; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return false; + } bool isPICDefaultForced() const override { return true; } void Index: clang/lib/Driver/ToolChains/AMDGPU.h =================================================================== --- clang/lib/Driver/ToolChains/AMDGPU.h +++ clang/lib/Driver/ToolChains/AMDGPU.h @@ -67,7 +67,9 @@ bool useIntegratedAs() const override { return true; } bool isCrossCompiling() const override { return true; } bool isPICDefault() const override { return false; } - bool isPIEDefault() const override { return false; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return false; + } bool isPICDefaultForced() const override { return false; } bool SupportsProfiling() const override { return false; } Index: clang/lib/Driver/ToolChains/BareMetal.h =================================================================== --- clang/lib/Driver/ToolChains/BareMetal.h +++ clang/lib/Driver/ToolChains/BareMetal.h @@ -42,7 +42,9 @@ bool useIntegratedAs() const override { return true; } bool isCrossCompiling() const override { return true; } bool isPICDefault() const override { return false; } - bool isPIEDefault() const override { return false; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return false; + } bool isPICDefaultForced() const override { return false; } bool SupportsProfiling() const override { return false; } Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -748,7 +748,7 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, const Driver &D, const InputInfo &Output, - const ArgList &Args, + const ArgList &Args, SanitizerArgs &SanArgs, ArgStringList &CmdArgs) { auto *PGOGenerateArg = Args.getLastArg(options::OPT_fprofile_generate, @@ -927,7 +927,7 @@ else if (Val != "single") D.Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() << Val; - } else if (TC.getSanitizerArgs().needsTsanRt()) { + } else if (SanArgs.needsTsanRt()) { CmdArgs.push_back("-fprofile-update=atomic"); } @@ -5127,12 +5127,12 @@ // This is a coarse approximation of what llvm-gcc actually does, both // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more // complicated ways. - bool AsyncUnwindTables = - Args.hasFlag(options::OPT_fasynchronous_unwind_tables, - options::OPT_fno_asynchronous_unwind_tables, - (TC.IsUnwindTablesDefault(Args) || - TC.getSanitizerArgs().needsUnwindTables()) && - !Freestanding); + auto SanitizeArgs = TC.getSanitizerArgs(Args); + bool AsyncUnwindTables = Args.hasFlag( + options::OPT_fasynchronous_unwind_tables, + options::OPT_fno_asynchronous_unwind_tables, + (TC.IsUnwindTablesDefault(Args) || SanitizeArgs.needsUnwindTables()) && + !Freestanding); bool UnwindTables = Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, false); if (AsyncUnwindTables) @@ -5373,7 +5373,7 @@ // for sampling, overhead of call arc collection is way too high and there's // no way to collect the output. if (!Triple.isNVPTX() && !Triple.isAMDGCN()) - addPGOAndCoverageFlags(TC, C, D, Output, Args, CmdArgs); + addPGOAndCoverageFlags(TC, C, D, Output, Args, SanitizeArgs, CmdArgs); Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ); @@ -5381,7 +5381,7 @@ if (RawTriple.isPS4CPU() && !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { PS4cpu::addProfileRTArgs(TC, Args, CmdArgs); - PS4cpu::addSanitizerArgs(TC, CmdArgs); + PS4cpu::addSanitizerArgs(TC, Args, CmdArgs); } // Pass options for controlling the default header search paths. @@ -5819,8 +5819,7 @@ CmdArgs.push_back("-fno-openmp-extensions"); } - const SanitizerArgs &Sanitize = TC.getSanitizerArgs(); - Sanitize.addArgs(TC, Args, CmdArgs, InputType); + SanitizeArgs.addArgs(TC, Args, CmdArgs, InputType); const XRayArgs &XRay = TC.getXRayArgs(); XRay.addArgs(TC, Args, CmdArgs, InputType); @@ -6764,12 +6763,12 @@ } bool DefaultsSplitLTOUnit = - (WholeProgramVTables || Sanitize.needsLTO()) && + (WholeProgramVTables || SanitizeArgs.needsLTO()) && (LTOMode == LTOK_Full || TC.canSplitThinLTOUnit()); bool SplitLTOUnit = Args.hasFlag(options::OPT_fsplit_lto_unit, options::OPT_fno_split_lto_unit, DefaultsSplitLTOUnit); - if (Sanitize.needsLTO() && !SplitLTOUnit) + if (SanitizeArgs.needsLTO() && !SplitLTOUnit) D.Diag(diag::err_drv_argument_not_allowed_with) << "-fno-split-lto-unit" << "-fsanitize=cfi"; if (SplitLTOUnit) Index: clang/lib/Driver/ToolChains/CloudABI.h =================================================================== --- clang/lib/Driver/ToolChains/CloudABI.h +++ clang/lib/Driver/ToolChains/CloudABI.h @@ -55,7 +55,7 @@ void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; SanitizerMask getSupportedSanitizers() const override; SanitizerMask getDefaultSanitizers() const override; Index: clang/lib/Driver/ToolChains/CloudABI.cpp =================================================================== --- clang/lib/Driver/ToolChains/CloudABI.cpp +++ clang/lib/Driver/ToolChains/CloudABI.cpp @@ -47,7 +47,7 @@ CmdArgs.push_back("--no-dynamic-linker"); // Provide PIE linker flags in case PIE is default for the architecture. - if (ToolChain.isPIEDefault()) { + if (ToolChain.isPIEDefault(Args)) { CmdArgs.push_back("-pie"); CmdArgs.push_back("-zrelro"); } @@ -125,7 +125,7 @@ return new tools::cloudabi::Linker(*this); } -bool CloudABI::isPIEDefault() const { +bool CloudABI::isPIEDefault(const llvm::opt::ArgList &Args) const { // Only enable PIE on architectures that support PC-relative // addressing. PC-relative addressing is required, as the process // startup code must be able to relocate itself. Index: clang/lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- clang/lib/Driver/ToolChains/CommonArgs.cpp +++ clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -786,7 +786,7 @@ SmallVectorImpl &NonWholeStaticRuntimes, SmallVectorImpl &HelperStaticRuntimes, SmallVectorImpl &RequiredSymbols) { - const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); + const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); // Collect shared runtimes. if (SanArgs.needsSharedRt()) { if (SanArgs.needsAsanRt() && SanArgs.linkRuntimes()) { @@ -922,7 +922,7 @@ NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols); - const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); + const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); // Inject libfuzzer dependencies. if (SanArgs.needsFuzzer() && SanArgs.linkRuntimes() && !Args.hasArg(options::OPT_shared)) { @@ -1115,7 +1115,7 @@ const llvm::Triple &EffectiveTriple = ToolChain.getEffectiveTriple(); const llvm::Triple &Triple = ToolChain.getTriple(); - bool PIE = ToolChain.isPIEDefault(); + bool PIE = ToolChain.isPIEDefault(Args); bool PIC = PIE || ToolChain.isPICDefault(); // The Darwin/MachO default to use PIC does not apply when using -static. if (Triple.isOSBinFormatMachO() && Args.hasArg(options::OPT_static)) Index: clang/lib/Driver/ToolChains/CrossWindows.h =================================================================== --- clang/lib/Driver/ToolChains/CrossWindows.h +++ clang/lib/Driver/ToolChains/CrossWindows.h @@ -57,7 +57,7 @@ bool IsIntegratedAssemblerDefault() const override { return true; } bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; LangOptions::StackProtectorMode Index: clang/lib/Driver/ToolChains/CrossWindows.cpp =================================================================== --- clang/lib/Driver/ToolChains/CrossWindows.cpp +++ clang/lib/Driver/ToolChains/CrossWindows.cpp @@ -185,7 +185,7 @@ } } - if (TC.getSanitizerArgs().needsAsanRt()) { + if (TC.getSanitizerArgs(Args).needsAsanRt()) { // TODO handle /MT[d] /MD[d] if (Args.hasArg(options::OPT_shared)) { CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk")); @@ -223,7 +223,7 @@ return getArch() == llvm::Triple::x86_64; } -bool CrossWindowsToolChain::isPIEDefault() const { +bool CrossWindowsToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const { return getArch() == llvm::Triple::x86_64; } Index: clang/lib/Driver/ToolChains/Cuda.h =================================================================== --- clang/lib/Driver/ToolChains/Cuda.h +++ clang/lib/Driver/ToolChains/Cuda.h @@ -157,7 +157,9 @@ bool useIntegratedAs() const override { return false; } bool isCrossCompiling() const override { return true; } bool isPICDefault() const override { return false; } - bool isPIEDefault() const override { return false; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return false; + } bool isPICDefaultForced() const override { return false; } bool SupportsProfiling() const override { return false; } bool supportsDebugInfoOption(const llvm::opt::Arg *A) const override; Index: clang/lib/Driver/ToolChains/Darwin.h =================================================================== --- clang/lib/Driver/ToolChains/Darwin.h +++ clang/lib/Driver/ToolChains/Darwin.h @@ -254,7 +254,7 @@ } bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; bool SupportsProfiling() const override; Index: clang/lib/Driver/ToolChains/Darwin.cpp =================================================================== --- clang/lib/Driver/ToolChains/Darwin.cpp +++ clang/lib/Driver/ToolChains/Darwin.cpp @@ -1357,7 +1357,7 @@ return; } - const SanitizerArgs &Sanitize = getSanitizerArgs(); + const SanitizerArgs &Sanitize = getSanitizerArgs(Args); if (Sanitize.needsAsanRt()) AddLinkSanitizerLibArgs(Args, CmdArgs, "asan"); if (Sanitize.needsLsanRt()) @@ -2778,7 +2778,7 @@ bool MachO::isPICDefault() const { return true; } -bool MachO::isPIEDefault() const { return false; } +bool MachO::isPIEDefault(const llvm::opt::ArgList &Args) const { return false; } bool MachO::isPICDefaultForced() const { return (getArch() == llvm::Triple::x86_64 || Index: clang/lib/Driver/ToolChains/FreeBSD.h =================================================================== --- clang/lib/Driver/ToolChains/FreeBSD.h +++ clang/lib/Driver/ToolChains/FreeBSD.h @@ -74,7 +74,7 @@ llvm::ExceptionHandling GetExceptionModel(const llvm::opt::ArgList &Args) const override; bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; SanitizerMask getSupportedSanitizers() const override; unsigned GetDefaultDwarfVersion() const override; // Until dtrace (via CTF) and LLDB can deal with distributed debug info, Index: clang/lib/Driver/ToolChains/FreeBSD.cpp =================================================================== --- clang/lib/Driver/ToolChains/FreeBSD.cpp +++ clang/lib/Driver/ToolChains/FreeBSD.cpp @@ -145,7 +145,7 @@ const llvm::Triple::ArchType Arch = ToolChain.getArch(); const bool IsPIE = !Args.hasArg(options::OPT_shared) && - (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault()); + (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault(Args)); ArgStringList CmdArgs; // Silence warning for "clang -g foo.o -o foo" @@ -467,7 +467,9 @@ bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; } -bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); } +bool FreeBSD::isPIEDefault(const llvm::opt::ArgList &Args) const { + return getSanitizerArgs(Args).requiresPIE(); +} SanitizerMask FreeBSD::getSupportedSanitizers() const { const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64; Index: clang/lib/Driver/ToolChains/Fuchsia.h =================================================================== --- clang/lib/Driver/ToolChains/Fuchsia.h +++ clang/lib/Driver/ToolChains/Fuchsia.h @@ -54,7 +54,9 @@ return true; } bool isPICDefault() const override { return false; } - bool isPIEDefault() const override { return true; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return true; + } bool isPICDefaultForced() const override { return false; } llvm::DebuggerKind getDefaultDebuggerTuning() const override { return llvm::DebuggerKind::GDB; Index: clang/lib/Driver/ToolChains/Fuchsia.cpp =================================================================== --- clang/lib/Driver/ToolChains/Fuchsia.cpp +++ clang/lib/Driver/ToolChains/Fuchsia.cpp @@ -89,7 +89,7 @@ else if (Args.hasArg(options::OPT_shared)) CmdArgs.push_back("-shared"); - const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); if (!Args.hasArg(options::OPT_shared)) { std::string Dyld = D.DyldPrefix; @@ -256,8 +256,9 @@ addMultilibFlag( Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, true), "fexceptions", Flags); - addMultilibFlag(getSanitizerArgs().needsAsanRt(), "fsanitize=address", Flags); - addMultilibFlag(getSanitizerArgs().needsHwasanRt(), "fsanitize=hwaddress", + addMultilibFlag(getSanitizerArgs(Args).needsAsanRt(), "fsanitize=address", + Flags); + addMultilibFlag(getSanitizerArgs(Args).needsHwasanRt(), "fsanitize=hwaddress", Flags); addMultilibFlag( Index: clang/lib/Driver/ToolChains/Gnu.h =================================================================== --- clang/lib/Driver/ToolChains/Gnu.h +++ clang/lib/Driver/ToolChains/Gnu.h @@ -298,7 +298,7 @@ bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; bool IsIntegratedAssemblerDefault() const override; llvm::opt::DerivedArgList * Index: clang/lib/Driver/ToolChains/Gnu.cpp =================================================================== --- clang/lib/Driver/ToolChains/Gnu.cpp +++ clang/lib/Driver/ToolChains/Gnu.cpp @@ -312,7 +312,7 @@ Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie, options::OPT_nopie); if (!A) - return TC.isPIEDefault(); + return TC.isPIEDefault(Args); return A->getOption().matches(options::OPT_pie); } @@ -2733,7 +2733,9 @@ } } -bool Generic_GCC::isPIEDefault() const { return false; } +bool Generic_GCC::isPIEDefault(const llvm::opt::ArgList &Args) const { + return false; +} bool Generic_GCC::isPICDefaultForced() const { return getArch() == llvm::Triple::x86_64 && getTriple().isOSWindows(); Index: clang/lib/Driver/ToolChains/HIP.cpp =================================================================== --- clang/lib/Driver/ToolChains/HIP.cpp +++ clang/lib/Driver/ToolChains/HIP.cpp @@ -37,6 +37,38 @@ const unsigned HIPCodeObjectAlign = 4096; } // namespace +static bool shouldSkipSanitizeOption(const ToolChain &TC, + const llvm::opt::ArgList &DriverArgs, + StringRef TargetID, + const llvm::opt::Arg *A) { + // For actions without targetID, do nothing. + if (TargetID.empty()) + return false; + Option O = A->getOption(); + if (!O.matches(options::OPT_fsanitize_EQ)) + return false; + + if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize, + -options::OPT_fno_gpu_sanitize, false)) + return true; + + llvm::StringMap FeatureMap; + auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap); + + assert(OptionalGpuArch && "Invalid Target ID"); + auto Loc = FeatureMap.find("xnack"); + if (Loc == FeatureMap.end() || !Loc->second) { + auto &Diags = TC.getDriver().getDiags(); + auto DiagID = Diags.getCustomDiagID( + DiagnosticsEngine::Warning, + "'%0' is not supported for offload arch '%1' and ignored. " + "Use an offload arch with 'xnack+' instead"); + Diags.Report(DiagID) << A->getSpelling() << TargetID; + return true; + } + return false; +} + void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA, const InputInfoList &Inputs, const InputInfo &Output, @@ -86,12 +118,6 @@ for (auto Input : Inputs) LldArgs.push_back(Input.getFilename()); - if (Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize, - false)) - llvm::for_each(TC.getHIPDeviceLibs(Args), [&](auto BCFile) { - LldArgs.push_back(Args.MakeArgString(BCFile.Path)); - }); - const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld")); C.addCommand(std::make_unique(JA, *this, ResponseFileSupport::None(), Lld, LldArgs, Inputs, Output)); @@ -295,7 +321,8 @@ const OptTable &Opts = getDriver().getOpts(); for (Arg *A : Args) { - if (!shouldSkipArgument(A)) + if (!shouldSkipArgument(A) && + !shouldSkipSanitizeOption(*this, Args, BoundArch, A)) DAL->append(A); } @@ -439,22 +466,6 @@ if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) { getDriver().Diag(clang::diag::err_drv_bad_target_id) << PTID.OptionalTargetID.getValue(); - return; - } - - assert(PTID.OptionalFeatures && "Invalid return from getParsedTargetID"); - auto &FeatureMap = PTID.OptionalFeatures.getValue(); - // Sanitizer is not supported with xnack-. - if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize, - options::OPT_fno_gpu_sanitize, false)) { - auto Loc = FeatureMap.find("xnack"); - if (Loc != FeatureMap.end() && !Loc->second) { - auto &Diags = getDriver().getDiags(); - auto DiagID = Diags.getCustomDiagID( - DiagnosticsEngine::Error, - "'-fgpu-sanitize' is not compatible with offload arch '%0'. " - "Use an offload arch without 'xnack-' instead"); - Diags.Report(DiagID) << PTID.OptionalTargetID.getValue(); - } } + return; } Index: clang/lib/Driver/ToolChains/Haiku.h =================================================================== --- clang/lib/Driver/ToolChains/Haiku.h +++ clang/lib/Driver/ToolChains/Haiku.h @@ -22,7 +22,7 @@ Haiku(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args); - bool isPIEDefault() const override { + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { return getTriple().getArch() == llvm::Triple::x86_64; } Index: clang/lib/Driver/ToolChains/Linux.h =================================================================== --- clang/lib/Driver/ToolChains/Linux.h +++ clang/lib/Driver/ToolChains/Linux.h @@ -43,7 +43,7 @@ CXXStdlibType GetDefaultCXXStdlibType() const override; bool IsAArch64OutlineAtomicsDefault(const llvm::opt::ArgList &Args) const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isNoExecStackDefault() const override; bool IsMathErrnoDefault() const override; SanitizerMask getSupportedSanitizers() const override; Index: clang/lib/Driver/ToolChains/Linux.cpp =================================================================== --- clang/lib/Driver/ToolChains/Linux.cpp +++ clang/lib/Driver/ToolChains/Linux.cpp @@ -655,9 +655,9 @@ } } -bool Linux::isPIEDefault() const { +bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const { return (getTriple().isAndroid() && !getTriple().isAndroidVersionLT(16)) || - getTriple().isMusl() || getSanitizerArgs().requiresPIE(); + getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE(); } bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const { Index: clang/lib/Driver/ToolChains/MSP430.h =================================================================== --- clang/lib/Driver/ToolChains/MSP430.h +++ clang/lib/Driver/ToolChains/MSP430.h @@ -37,7 +37,9 @@ Action::OffloadKind) const override; bool isPICDefault() const override { return false; } - bool isPIEDefault() const override { return false; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return false; + } bool isPICDefaultForced() const override { return true; } UnwindLibType Index: clang/lib/Driver/ToolChains/MSVC.h =================================================================== --- clang/lib/Driver/ToolChains/MSVC.h +++ clang/lib/Driver/ToolChains/MSVC.h @@ -52,7 +52,7 @@ bool IsIntegratedAssemblerDefault() const override; bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; /// Set CodeView as the default debug info format for non-MachO binary Index: clang/lib/Driver/ToolChains/MSVC.cpp =================================================================== --- clang/lib/Driver/ToolChains/MSVC.cpp +++ clang/lib/Driver/ToolChains/MSVC.cpp @@ -530,7 +530,7 @@ CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") + ImplibName)); } - if (TC.getSanitizerArgs().needsFuzzer()) { + if (TC.getSanitizerArgs(Args).needsFuzzer()) { if (!Args.hasArg(options::OPT_shared)) CmdArgs.push_back( Args.MakeArgString(std::string("-wholearchive:") + @@ -541,10 +541,10 @@ CmdArgs.push_back(Args.MakeArgString("-incremental:no")); } - if (TC.getSanitizerArgs().needsAsanRt()) { + if (TC.getSanitizerArgs(Args).needsAsanRt()) { CmdArgs.push_back(Args.MakeArgString("-debug")); CmdArgs.push_back(Args.MakeArgString("-incremental:no")); - if (TC.getSanitizerArgs().needsSharedRt() || + if (TC.getSanitizerArgs(Args).needsSharedRt() || Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) { for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"}) CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib)); @@ -798,7 +798,7 @@ return getArch() == llvm::Triple::x86_64; } -bool MSVCToolChain::isPIEDefault() const { +bool MSVCToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const { return false; } Index: clang/lib/Driver/ToolChains/MinGW.h =================================================================== --- clang/lib/Driver/ToolChains/MinGW.h +++ clang/lib/Driver/ToolChains/MinGW.h @@ -65,7 +65,7 @@ bool IsIntegratedAssemblerDefault() const override; bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; SanitizerMask getSupportedSanitizers() const override; Index: clang/lib/Driver/ToolChains/MinGW.cpp =================================================================== --- clang/lib/Driver/ToolChains/MinGW.cpp +++ clang/lib/Driver/ToolChains/MinGW.cpp @@ -98,7 +98,7 @@ const char *LinkingOutput) const { const ToolChain &TC = getToolChain(); const Driver &D = TC.getDriver(); - const SanitizerArgs &Sanitize = TC.getSanitizerArgs(); + const SanitizerArgs &Sanitize = TC.getSanitizerArgs(Args); ArgStringList CmdArgs; @@ -477,7 +477,9 @@ return getArch() == llvm::Triple::x86_64; } -bool toolchains::MinGW::isPIEDefault() const { return false; } +bool toolchains::MinGW::isPIEDefault(const llvm::opt::ArgList &Args) const { + return false; +} bool toolchains::MinGW::isPICDefaultForced() const { return getArch() == llvm::Triple::x86_64; Index: clang/lib/Driver/ToolChains/NetBSD.cpp =================================================================== --- clang/lib/Driver/ToolChains/NetBSD.cpp +++ clang/lib/Driver/ToolChains/NetBSD.cpp @@ -262,7 +262,7 @@ bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); - const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); if (SanArgs.needsSharedRt()) { CmdArgs.push_back("-rpath"); CmdArgs.push_back(Args.MakeArgString(ToolChain.getCompilerRTPath())); @@ -499,7 +499,7 @@ void NetBSD::addClangTargetOptions(const ArgList &DriverArgs, ArgStringList &CC1Args, Action::OffloadKind) const { - const SanitizerArgs &SanArgs = getSanitizerArgs(); + const SanitizerArgs &SanArgs = getSanitizerArgs(DriverArgs); if (SanArgs.hasAnySanitizer()) CC1Args.push_back("-D_REENTRANT"); Index: clang/lib/Driver/ToolChains/OpenBSD.h =================================================================== --- clang/lib/Driver/ToolChains/OpenBSD.h +++ clang/lib/Driver/ToolChains/OpenBSD.h @@ -59,7 +59,9 @@ bool IsMathErrnoDefault() const override { return false; } bool IsObjCNonFragileABIDefault() const override { return true; } - bool isPIEDefault() const override { return true; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return true; + } RuntimeLibType GetDefaultRuntimeLibType() const override { return ToolChain::RLT_CompilerRT; Index: clang/lib/Driver/ToolChains/PS4CPU.h =================================================================== --- clang/lib/Driver/ToolChains/PS4CPU.h +++ clang/lib/Driver/ToolChains/PS4CPU.h @@ -23,7 +23,8 @@ void addProfileRTArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs); -void addSanitizerArgs(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs); +void addSanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs); class LLVM_LIBRARY_VISIBILITY Assemble : public Tool { public: Index: clang/lib/Driver/ToolChains/PS4CPU.cpp =================================================================== --- clang/lib/Driver/ToolChains/PS4CPU.cpp +++ clang/lib/Driver/ToolChains/PS4CPU.cpp @@ -71,8 +71,9 @@ Exec, CmdArgs, Inputs, Output)); } -static void AddPS4SanitizerArgs(const ToolChain &TC, ArgStringList &CmdArgs) { - const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); +static void AddPS4SanitizerArgs(const ToolChain &TC, const ArgList &Args, + ArgStringList &CmdArgs) { + const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); if (SanArgs.needsUbsanRt()) { CmdArgs.push_back("-lSceDbgUBSanitizer_stub_weak"); } @@ -81,9 +82,9 @@ } } -void tools::PS4cpu::addSanitizerArgs(const ToolChain &TC, +void tools::PS4cpu::addSanitizerArgs(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) { - const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); + const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); if (SanArgs.needsUbsanRt()) CmdArgs.push_back("--dependent-lib=libSceDbgUBSanitizer_stub_weak.a"); if (SanArgs.needsAsanRt()) @@ -127,7 +128,7 @@ } if(!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) - AddPS4SanitizerArgs(ToolChain, CmdArgs); + AddPS4SanitizerArgs(ToolChain, Args, CmdArgs); Args.AddAllArgs(CmdArgs, options::OPT_L); Args.AddAllArgs(CmdArgs, options::OPT_T_Group); Index: clang/lib/Driver/ToolChains/TCE.h =================================================================== --- clang/lib/Driver/ToolChains/TCE.h +++ clang/lib/Driver/ToolChains/TCE.h @@ -27,7 +27,7 @@ bool IsMathErrnoDefault() const override; bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; }; Index: clang/lib/Driver/ToolChains/TCE.cpp =================================================================== --- clang/lib/Driver/ToolChains/TCE.cpp +++ clang/lib/Driver/ToolChains/TCE.cpp @@ -34,7 +34,9 @@ bool TCEToolChain::isPICDefault() const { return false; } -bool TCEToolChain::isPIEDefault() const { return false; } +bool TCEToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const { + return false; +} bool TCEToolChain::isPICDefaultForced() const { return false; } Index: clang/lib/Driver/ToolChains/VEToolchain.h =================================================================== --- clang/lib/Driver/ToolChains/VEToolchain.h +++ clang/lib/Driver/ToolChains/VEToolchain.h @@ -28,7 +28,7 @@ public: bool IsIntegratedAssemblerDefault() const override { return true; } bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; bool SupportsProfiling() const override; bool hasBlocksRuntime() const override; Index: clang/lib/Driver/ToolChains/VEToolchain.cpp =================================================================== --- clang/lib/Driver/ToolChains/VEToolchain.cpp +++ clang/lib/Driver/ToolChains/VEToolchain.cpp @@ -53,7 +53,9 @@ bool VEToolChain::isPICDefault() const { return false; } -bool VEToolChain::isPIEDefault() const { return false; } +bool VEToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const { + return false; +} bool VEToolChain::isPICDefaultForced() const { return false; } Index: clang/lib/Driver/ToolChains/WebAssembly.h =================================================================== --- clang/lib/Driver/ToolChains/WebAssembly.h +++ clang/lib/Driver/ToolChains/WebAssembly.h @@ -45,7 +45,7 @@ bool IsObjCNonFragileABIDefault() const override; bool UseObjCMixedDispatch() const override; bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; bool IsIntegratedAssemblerDefault() const override; bool hasBlocksRuntime() const override; Index: clang/lib/Driver/ToolChains/WebAssembly.cpp =================================================================== --- clang/lib/Driver/ToolChains/WebAssembly.cpp +++ clang/lib/Driver/ToolChains/WebAssembly.cpp @@ -201,7 +201,9 @@ bool WebAssembly::isPICDefault() const { return false; } -bool WebAssembly::isPIEDefault() const { return false; } +bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const { + return false; +} bool WebAssembly::isPICDefaultForced() const { return false; } Index: clang/lib/Driver/ToolChains/XCore.h =================================================================== --- clang/lib/Driver/ToolChains/XCore.h +++ clang/lib/Driver/ToolChains/XCore.h @@ -58,7 +58,7 @@ public: bool isPICDefault() const override; - bool isPIEDefault() const override; + bool isPIEDefault(const llvm::opt::ArgList &Args) const override; bool isPICDefaultForced() const override; bool SupportsProfiling() const override; bool hasBlocksRuntime() const override; Index: clang/lib/Driver/ToolChains/XCore.cpp =================================================================== --- clang/lib/Driver/ToolChains/XCore.cpp +++ clang/lib/Driver/ToolChains/XCore.cpp @@ -102,7 +102,9 @@ bool XCoreToolChain::isPICDefault() const { return false; } -bool XCoreToolChain::isPIEDefault() const { return false; } +bool XCoreToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const { + return false; +} bool XCoreToolChain::isPICDefaultForced() const { return false; } Index: clang/lib/Driver/ToolChains/ZOS.h =================================================================== --- clang/lib/Driver/ToolChains/ZOS.h +++ clang/lib/Driver/ToolChains/ZOS.h @@ -23,7 +23,9 @@ ~ZOS() override; bool isPICDefault() const override { return false; } - bool isPIEDefault() const override { return false; } + bool isPIEDefault(const llvm::opt::ArgList &Args) const override { + return false; + } bool isPICDefaultForced() const override { return false; } bool IsIntegratedAssemblerDefault() const override { return true; } Index: clang/test/Driver/hip-sanitize-options.hip =================================================================== --- clang/test/Driver/hip-sanitize-options.hip +++ clang/test/Driver/hip-sanitize-options.hip @@ -1,47 +1,60 @@ // REQUIRES: clang-driver, x86-registered-target, amdgpu-registered-target -// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900 \ +// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \ // RUN: -fsanitize=address \ // RUN: -nogpuinc --rocm-path=%S/Inputs/rocm \ // RUN: %s 2>&1 | FileCheck %s -// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900 \ +// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \ // RUN: -fsanitize=address -fno-gpu-sanitize \ // RUN: -nogpuinc --rocm-path=%S/Inputs/rocm \ // RUN: %s 2>&1 | FileCheck %s -// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900 \ +// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \ // RUN: -fsanitize=address -fgpu-sanitize \ // RUN: -nogpuinc --rocm-path=%S/Inputs/rocm \ // RUN: %s 2>&1 | FileCheck -check-prefixes=NORDC %s -// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900 \ +// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \ // RUN: -fsanitize=address -fgpu-sanitize -fgpu-rdc \ // RUN: -nogpuinc --rocm-path=%S/Inputs/rocm \ // RUN: %s 2>&1 | FileCheck -check-prefixes=RDC %s -// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900 \ +// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \ // RUN: -fsanitize=address -fgpu-sanitize \ // RUN: -nogpuinc --rocm-path=%S/Inputs/rocm-invalid \ // RUN: %s 2>&1 | FileCheck -check-prefixes=FAIL %s // RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack- \ -// RUN: -fsanitize=address -fgpu-sanitize \ +// RUN: --offload-arch=gfx900:xnack+ --offload-arch=gfx906 -fsanitize=address -fgpu-sanitize \ +// RUN: -nogpuinc --rocm-path=%S/Inputs/rocm \ +// RUN: %s 2>&1 | FileCheck -check-prefixes=XNACK %s + +// RUN: %clang -### -target x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack- \ +// RUN: --offload-arch=gfx900:xnack+ --offload-arch=gfx906 -fsanitize=address -fgpu-sanitize \ // RUN: -nogpuinc --rocm-path=%S/Inputs/rocm \ -// RUN: %s 2>&1 | FileCheck -check-prefix=XNACK %s +// RUN: %s 2>&1 | FileCheck -check-prefixes=XNACKNEG %s // CHECK-NOT: {{"[^"]*clang[^"]*".* "-fcuda-is-device".* "-fsanitize=address"}} // CHECK-NOT: {{"[^"]*lld(\.exe){0,1}".* ".*hip.bc"}} // CHECK: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}} // NORDC: {{"[^"]*clang[^"]*".* "-emit-obj".* "-fcuda-is-device".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-mlink-builtin-bitcode" ".*hip.bc".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.o]]" -// NORDC: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}} +// NORDC-NOT: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}} // NORDC: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}} // RDC: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}} // RDC: {{"[^"]*clang[^"]*".* "-emit-llvm-bc".* "-fcuda-is-device".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.bc]]" -// RDC: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}} +// RDC-NOT: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}} // FAIL: AMDGPU address sanitizer runtime library (asanrtl) is not found. Please install ROCm device library which supports address sanitizer -// XNACK: error: '-fgpu-sanitize' is not compatible with offload arch 'gfx900:xnack-'. Use an offload arch without 'xnack-' instead +// XNACK-DAG: warning: '-fsanitize=' is not supported for offload arch 'gfx900:xnack-' and ignored. Use an offload arch with 'xnack+' instead +// XNACK-DAG: warning: '-fsanitize=' is not supported for offload arch 'gfx906' and ignored. Use an offload arch with 'xnack+' instead +// XNACK-DAG: {{"[^"]*clang[^"]*".* "-target-cpu" "gfx900".* "-target-feature" "\+xnack".* "-fsanitize=address"}} +// XNACK-DAG: {{"[^"]*clang[^"]*".* "-target-cpu" "gfx900".* "-target-feature" "-xnack"}} +// XNACK-DAG: {{"[^"]*clang[^"]*".* "-target-cpu" "gfx906"}} +// XNACK-DAG: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}} +// XNACKNEG-NOT: {{"[^"]*clang[^"]*".* "-target-cpu" "gfx900".* "-target-feature" "-xnack".* "-fsanitize=address"}} +// XNACKNEG-NOT: {{"[^"]*clang[^"]*".* "-target-cpu" "gfx906".* "-fsanitize=address"}} +// XNACKNEG-NOT: {{"[^"]*lld(\.exe){0,1}".* ".*hip.bc"}}