Index: clang/lib/Driver/ToolChains/AMDGPU.h =================================================================== --- clang/lib/Driver/ToolChains/AMDGPU.h +++ clang/lib/Driver/ToolChains/AMDGPU.h @@ -107,7 +107,19 @@ protected: /// Check and diagnose invalid target ID specified by -mcpu. - void checkTargetID(const llvm::opt::ArgList &DriverArgs) const; + virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const; + + /// The struct type returned by getParsedTargetID. + struct ParsedTargetIDType { + Optional OptionalTargetID; + Optional OptionalGPUArch; + Optional> OptionalFeatures; + }; + + /// Get target ID, GPU arch, and target ID features if the target ID is + /// specified and valid. + ParsedTargetIDType + getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const; /// Get GPU arch from -mcpu without checking. StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const; Index: clang/lib/Driver/ToolChains/AMDGPU.cpp =================================================================== --- clang/lib/Driver/ToolChains/AMDGPU.cpp +++ clang/lib/Driver/ToolChains/AMDGPU.cpp @@ -707,16 +707,26 @@ getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ)); } -void AMDGPUToolChain::checkTargetID( - const llvm::opt::ArgList &DriverArgs) const { +AMDGPUToolChain::ParsedTargetIDType +AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const { StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ); if (TargetID.empty()) - return; + return {None, None, None}; llvm::StringMap FeatureMap; auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap); - if (!OptionalGpuArch) { - getDriver().Diag(clang::diag::err_drv_bad_target_id) << TargetID; + if (!OptionalGpuArch) + return {TargetID.str(), None, None}; + + return {TargetID.str(), OptionalGpuArch.getValue().str(), FeatureMap}; +} + +void AMDGPUToolChain::checkTargetID( + const llvm::opt::ArgList &DriverArgs) const { + auto PTID = getParsedTargetID(DriverArgs); + if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) { + getDriver().Diag(clang::diag::err_drv_bad_target_id) + << PTID.OptionalTargetID.getValue(); } } Index: clang/lib/Driver/ToolChains/HIP.h =================================================================== --- clang/lib/Driver/ToolChains/HIP.h +++ clang/lib/Driver/ToolChains/HIP.h @@ -95,6 +95,7 @@ unsigned GetDefaultDwarfVersion() const override { return 4; } const ToolChain &HostTC; + void checkTargetID(const llvm::opt::ArgList &DriverArgs) const override; protected: Tool *buildLinker() const override; Index: clang/lib/Driver/ToolChains/HIP.cpp =================================================================== --- clang/lib/Driver/ToolChains/HIP.cpp +++ clang/lib/Driver/ToolChains/HIP.cpp @@ -459,3 +459,28 @@ return BCLibs; } + +void HIPToolChain::checkTargetID(const llvm::opt::ArgList &DriverArgs) const { + auto PTID = getParsedTargetID(DriverArgs); + 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(); + } + } +} Index: clang/test/Driver/hip-sanitize-options.hip =================================================================== --- clang/test/Driver/hip-sanitize-options.hip +++ clang/test/Driver/hip-sanitize-options.hip @@ -25,6 +25,11 @@ // 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: -nogpuinc --rocm-path=%S/Inputs/rocm \ +// RUN: %s 2>&1 | FileCheck -check-prefix=XNACK %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"}} @@ -38,3 +43,5 @@ // RDC: {{"[^"]*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