[clang] Remove Linux path names in ROCm search paths on Windows (PR #97668)

David Salinas via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 24 15:52:10 PDT 2024


https://github.com/david-salinas updated https://github.com/llvm/llvm-project/pull/97668

>From 803e3939a726bed2f1c43a3199850d580251c6d2 Mon Sep 17 00:00:00 2001
From: David Salinas <david.salinas at amd.com>
Date: Thu, 4 Jul 2024 03:32:13 +0000
Subject: [PATCH] Remove Linux path names in ROCm search paths on Windows

      When target triple indicates we are on Windows, do not add linux
      paths to search path in ROCm Installation Detection.

Change-Id: I18effb8c20252de3d84ea37ef562124695c5a570
---
 clang/lib/Driver/ToolChains/AMDGPU.cpp    | 24 +++++++++++++----------
 clang/lib/Driver/ToolChains/AMDGPU.h      |  2 +-
 clang/lib/Driver/ToolChains/HIPAMD.cpp    |  2 +-
 clang/lib/Driver/ToolChains/MSVC.cpp      |  4 ++++
 clang/lib/Driver/ToolChains/ROCm.h        |  9 ++++++++-
 clang/test/Driver/rocm-detect-windows.hip |  9 +++++++++
 6 files changed, 37 insertions(+), 13 deletions(-)
 create mode 100644 clang/test/Driver/rocm-detect-windows.hip

diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 453daed7cc7d5b..1bd891498ca0d4 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -306,23 +306,25 @@ RocmInstallationDetector::getInstallationPathCandidates() {
       LatestVer = Ver;
     }
   }
-  if (!LatestROCm.empty())
-    ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm,
-                                /*StrictChecking=*/true);
+  if (!isHostWindows()) {
+    if (!LatestROCm.empty())
+      ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm,
+                                  /*StrictChecking=*/true);
 
-  ROCmSearchDirs.emplace_back(D.SysRoot + "/usr/local",
-                              /*StrictChecking=*/true);
-  ROCmSearchDirs.emplace_back(D.SysRoot + "/usr",
-                              /*StrictChecking=*/true);
+    ROCmSearchDirs.emplace_back(D.SysRoot + "/usr/local",
+                                /*StrictChecking=*/true);
+    ROCmSearchDirs.emplace_back(D.SysRoot + "/usr",
+                                /*StrictChecking=*/true);
+  }
 
   DoPrintROCmSearchDirs();
   return ROCmSearchDirs;
 }
 
 RocmInstallationDetector::RocmInstallationDetector(
-    const Driver &D, const llvm::Triple &HostTriple,
+    const Driver &D, const llvm::Triple &TargetTriple,
     const llvm::opt::ArgList &Args, bool DetectHIPRuntime, bool DetectDeviceLib)
-    : D(D) {
+    : D(D), TargetTriple(TargetTriple) {
   Verbose = Args.hasArg(options::OPT_v);
   RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ);
   PrintROCmSearchDirs =
@@ -820,8 +822,10 @@ bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
 
 /// ROCM Toolchain
 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
-                             const ArgList &Args)
+                             const ArgList &Args, bool isHostTCMSVC)
     : AMDGPUToolChain(D, Triple, Args) {
+  RocmInstallation->setHostWindows(isHostTCMSVC);
+
   RocmInstallation->detectDeviceLibrary();
 }
 
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.h b/clang/lib/Driver/ToolChains/AMDGPU.h
index 7e70dae8ce152e..870230b3093df3 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.h
+++ b/clang/lib/Driver/ToolChains/AMDGPU.h
@@ -129,7 +129,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
 class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
 public:
   ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
-                const llvm::opt::ArgList &Args);
+                const llvm::opt::ArgList &Args, bool isHostTCMSVC=false);
   void
   addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
                         llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index c35b0febb262da..4fe7e75360726d 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -250,7 +250,7 @@ void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
 HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
                                  const ToolChain &HostTC, const ArgList &Args)
-    : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
+    : ROCMToolChain(D, Triple, Args, HostTC.getTriple().isWindowsMSVCEnvironment()), HostTC(HostTC) {
   // Lookup binaries into the driver directory, this is used to
   // discover the clang-offload-bundler executable.
   getProgramPaths().push_back(getDriver().Dir);
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index ca266e3e1d1d3c..7526c6613f4dd7 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -428,6 +428,9 @@ MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple &Triple,
                              const ArgList &Args)
     : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args),
       RocmInstallation(D, Triple, Args) {
+  
+  RocmInstallation->setHostWindows(true);
+
   getProgramPaths().push_back(getDriver().Dir);
 
   std::optional<llvm::StringRef> VCToolsDir, VCToolsVersion;
@@ -453,6 +456,7 @@ MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple &Triple,
       llvm::findVCToolChainViaSetupConfig(getVFS(), VCToolsVersion,
                                           VCToolChainPath, VSLayout) ||
       llvm::findVCToolChainViaRegistry(VCToolChainPath, VSLayout);
+
 }
 
 Tool *MSVCToolChain::buildLinker() const {
diff --git a/clang/lib/Driver/ToolChains/ROCm.h b/clang/lib/Driver/ToolChains/ROCm.h
index dceb0ab0366933..d766422396135d 100644
--- a/clang/lib/Driver/ToolChains/ROCm.h
+++ b/clang/lib/Driver/ToolChains/ROCm.h
@@ -80,6 +80,7 @@ class RocmInstallationDetector {
   bool HasHIPStdParLibrary = false;
   bool HasRocThrustLibrary = false;
   bool HasRocPrimLibrary = false;
+  bool IsHostMSVC = false;
 
   // Default version if not detected or specified.
   const unsigned DefaultVersionMajor = 3;
@@ -111,6 +112,8 @@ class RocmInstallationDetector {
   // Wheter -nogpulib is specified.
   bool NoBuiltinLibs = false;
 
+  llvm::Triple TargetTriple;
+
   // Paths
   SmallString<0> InstallPath;
   SmallString<0> BinPath;
@@ -166,7 +169,7 @@ class RocmInstallationDetector {
                                         StringRef PackageName);
 
 public:
-  RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
+  RocmInstallationDetector(const Driver &D, const llvm::Triple &TargetTriple,
                            const llvm::opt::ArgList &Args,
                            bool DetectHIPRuntime = true,
                            bool DetectDeviceLib = false);
@@ -193,6 +196,10 @@ class RocmInstallationDetector {
   /// Check whether we detected a valid HIP STDPAR Acceleration library.
   bool hasHIPStdParLibrary() const { return HasHIPStdParLibrary; }
 
+  /// Check whether the target triple is for Windows.
+  bool isHostWindows() const { return IsHostMSVC; }
+  void setHostWindows(bool val) { IsHostMSVC=val; }
+
   /// Print information about the detected ROCm installation.
   void print(raw_ostream &OS) const;
 
diff --git a/clang/test/Driver/rocm-detect-windows.hip b/clang/test/Driver/rocm-detect-windows.hip
new file mode 100644
index 00000000000000..d37a708e80b4fd
--- /dev/null
+++ b/clang/test/Driver/rocm-detect-windows.hip
@@ -0,0 +1,9 @@
+// REQUIRES: system-windows
+
+// Test to ensure that on Windows, we do not include linux sesrch paths
+// RUN: %clang -### -nogpulib -nogpuinc \
+// RUN:  --print-rocm-search-dirs %s 2>&1 \
+// RUN:   | FileCheck %s
+
+// CHECK-NOT: ROCm installation search path: {{/usr/local}}
+// CHECK-NOT: ROCm installation search path: {{/usr}}



More information about the cfe-commits mailing list