[clang] Remove Linux search paths on Windows (PR #113628)

David Salinas via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 13 08:48:25 PST 2024


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

>From a5a84991fd605630e4c37cc92518fb8dee42ab61 Mon Sep 17 00:00:00 2001
From: David Salinas <david.salinas at amd.com>
Date: Tue, 22 Oct 2024 18:58:47 +0000
Subject: [PATCH] Remove Linux search paths on Windows

Change-Id: Ia0b44eb1069fa631a6d5156cf5881c978e23b62d
---
 clang/lib/Driver/Driver.cpp                  |  3 +-
 clang/lib/Driver/ToolChains/AMDGPU.cpp       | 32 +++++++++++---------
 clang/lib/Driver/ToolChains/AMDGPU.h         |  4 +--
 clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp |  3 +-
 clang/lib/Driver/ToolChains/Gnu.cpp          |  3 ++
 clang/lib/Driver/ToolChains/HIPAMD.cpp       |  9 +++++-
 clang/lib/Driver/ToolChains/MSVC.cpp         |  6 ++++
 clang/lib/Driver/ToolChains/ROCm.h           | 12 ++++++++
 clang/test/Driver/rocm-detect-linux.hip      |  9 ++++++
 clang/test/Driver/rocm-detect-windows.hip    |  9 ++++++
 clang/test/Driver/rocm-detect.hip            |  4 +--
 11 files changed, 72 insertions(+), 22 deletions(-)
 create mode 100644 clang/test/Driver/rocm-detect-linux.hip
 create mode 100644 clang/test/Driver/rocm-detect-windows.hip

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9878a9dad78d40..19f465b7dab08a 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6440,7 +6440,8 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
       TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
       break;
     case llvm::Triple::AMDHSA:
-      TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
+      TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args,
+                                                       Target.isOSWindows());
       break;
     case llvm::Triple::AMDPAL:
     case llvm::Triple::Mesa3D:
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 2c85d21ebd738c..8a746c3b1359b4 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -306,15 +306,17 @@ RocmInstallationDetector::getInstallationPathCandidates() {
       LatestVer = Ver;
     }
   }
-  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);
+  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);
+  }
   DoPrintROCmSearchDirs();
   return ROCmSearchDirs;
 }
@@ -375,11 +377,6 @@ RocmInstallationDetector::RocmInstallationDetector(
                        Twine(DefaultVersionMinor) + "." + VersionPatch)
                           .str();
   }
-
-  if (DetectHIPRuntime)
-    detectHIPRuntime();
-  if (DetectDeviceLib)
-    detectDeviceLibrary();
 }
 
 void RocmInstallationDetector::detectDeviceLibrary() {
@@ -699,10 +696,12 @@ void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
 
 /// AMDGPU Toolchain
 AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
-                                 const ArgList &Args)
+                                 const ArgList &Args, bool isHostTCMSVC)
     : Generic_ELF(D, Triple, Args),
       OptionsDefault(
           {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
+  if (!isHostTCMSVC)
+    RocmInstallation->init();
   // Check code object version options. Emit warnings for legacy options
   // and errors for the last invalid code object version options.
   // It is done here to avoid repeated warning or error messages for
@@ -835,8 +834,11 @@ bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
 
 /// ROCM Toolchain
 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
-                             const ArgList &Args)
-    : AMDGPUToolChain(D, Triple, Args) {
+                             const ArgList &Args, bool isHostTCMSVC)
+    : AMDGPUToolChain(D, Triple, Args, isHostTCMSVC) {
+  RocmInstallation->setHostWindows(isHostTCMSVC);
+  if (isHostTCMSVC)
+    RocmInstallation->init(true, false);
   RocmInstallation->detectDeviceLibrary();
 }
 
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.h b/clang/lib/Driver/ToolChains/AMDGPU.h
index a9b4552a1f91a4..c0e9727ebf56a2 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.h
+++ b/clang/lib/Driver/ToolChains/AMDGPU.h
@@ -59,7 +59,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
 
 public:
   AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
-                  const llvm::opt::ArgList &Args);
+                  const llvm::opt::ArgList &Args, bool isHostTCMSVC = false);
   unsigned GetDefaultDwarfVersion() const override { return 5; }
 
   bool IsMathErrnoDefault() const override { return false; }
@@ -135,7 +135,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);
   void
   addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
                         llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp b/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
index 3f0b3f2d86b3ed..33462b49196725 100644
--- a/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
@@ -33,7 +33,8 @@ AMDGPUOpenMPToolChain::AMDGPUOpenMPToolChain(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().isOSWindows()),
+      HostTC(HostTC) {
   // Lookup binaries into the driver directory, this is used to
   // discover the 'amdgpu-arch' executable.
   getProgramPaths().push_back(getDriver().Dir);
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 8397f1121ec2ce..8e489d3436b108 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -3060,6 +3060,9 @@ Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple &Triple,
     : ToolChain(D, Triple, Args), GCCInstallation(D),
       CudaInstallation(D, Triple, Args), RocmInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().Dir);
+  llvm::Triple TargetTriple(D.getTargetTriple());
+  if (!TargetTriple.isOSWindows())
+    RocmInstallation->init();
 }
 
 Generic_GCC::~Generic_GCC() {}
diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index bae05cc0bb7353..5a14c96b8484a1 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -213,7 +213,14 @@ 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().isOSWindows()),
+      HostTC(HostTC) {
+  if (HostTC.getTriple().isWindowsMSVCEnvironment()) {
+    RocmInstallation->setHostWindows(true);
+  }
+
+  RocmInstallation->init(true, false);
+
   // 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 80799d1e715f07..ba1130e650b975 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -425,6 +425,12 @@ MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple &Triple,
                              const ArgList &Args)
     : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args),
       RocmInstallation(D, Triple, Args) {
+
+  // Tell the ROCm installation detector that Host is Windows before trying to
+  // find HIPRT or Device Libs
+  RocmInstallation->setHostWindows(true);
+  RocmInstallation->init();
+
   getProgramPaths().push_back(getDriver().Dir);
 
   std::optional<llvm::StringRef> VCToolsDir, VCToolsVersion;
diff --git a/clang/lib/Driver/ToolChains/ROCm.h b/clang/lib/Driver/ToolChains/ROCm.h
index dceb0ab0366933..a851ecc30d0f43 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;
@@ -193,6 +194,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;
 
@@ -272,6 +277,13 @@ class RocmInstallationDetector {
     return Loc->second;
   }
 
+  void init(bool DetectHIPRuntime = true, bool DetectDeviceLib = false) {
+    if (DetectHIPRuntime)
+      detectHIPRuntime();
+    if (DetectDeviceLib)
+      detectDeviceLibrary();
+  }
+
   void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
                          llvm::opt::ArgStringList &CC1Args) const;
 
diff --git a/clang/test/Driver/rocm-detect-linux.hip b/clang/test/Driver/rocm-detect-linux.hip
new file mode 100644
index 00000000000000..26c10c488d9e4d
--- /dev/null
+++ b/clang/test/Driver/rocm-detect-linux.hip
@@ -0,0 +1,9 @@
+// REQUIRES: system-linux
+
+// 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: ROCm installation search path: {{/usr/local}}
+// CHECK: ROCm installation search path: {{/usr}}
diff --git a/clang/test/Driver/rocm-detect-windows.hip b/clang/test/Driver/rocm-detect-windows.hip
new file mode 100644
index 00000000000000..6642a78a3492f2
--- /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}}
diff --git a/clang/test/Driver/rocm-detect.hip b/clang/test/Driver/rocm-detect.hip
index 4aafeb97c00b5f..6f03c7690a9867 100644
--- a/clang/test/Driver/rocm-detect.hip
+++ b/clang/test/Driver/rocm-detect.hip
@@ -103,7 +103,7 @@
 // RUN: %t/rocm-spack/llvm-amdgpu-4.0.0-ieagcs7inf7runpyfvepqkurasoglq4z/bin/clang -### -no-canonical-prefixes -v \
 // RUN:   -resource-dir=%t/rocm-spack/llvm-amdgpu-4.0.0-ieagcs7inf7runpyfvepqkurasoglq4z/lib/clang \
 // RUN:   -target x86_64-linux-gnu --cuda-gpu-arch=gfx900 --print-rocm-search-dirs %s 2>&1 \
-// RUN:   | FileCheck -check-prefixes=SPACK %s
+// RUN:   | FileCheck -check-prefixes=SPACK %s --dump-input always --dump-input-context 100
 
 // Test SPACK installation with multiple hip and rocm-device-libs packages of the same
 // ROCm release. --hip-path and --rocm-device-lib-path can be used to specify them.
@@ -147,12 +147,12 @@
 // ROCM-REL: ROCm installation search path: {{.*}}/opt/rocm-3.10.0
 
 // SPACK: InstalledDir: [[DIR:.*]]/llvm-amdgpu-4.0.0-ieagcs7inf7runpyfvepqkurasoglq4z/bin
+// SPACK: Found HIP installation: [[DIR]]/hip-4.0.0-5f63slrursbrvfe2txrrjkynbsywsob5, version 4.0.20214-a2917cd
 // SPACK: ROCm installation search path (Spack 4.0.0): [[DIR]]
 // SPACK: ROCm installation search path: [[CLANG:.*]]
 // SPACK: ROCm installation search path: [[DIR]]/llvm-amdgpu-4.0.0-ieagcs7inf7runpyfvepqkurasoglq4z
 // SPACK: ROCm installation search path: [[DIR]]/llvm-amdgpu-4.0.0-ieagcs7inf7runpyfvepqkurasoglq4z/lib/clang
 // SPACK: ROCm installation search path: /opt/rocm
-// SPACK: Found HIP installation: [[DIR]]/hip-4.0.0-5f63slrursbrvfe2txrrjkynbsywsob5, version 4.0.20214-a2917cd
 // SPACK: "-triple" "amdgcn-amd-amdhsa"
 // SPACK-SAME: "-mlink-builtin-bitcode" "[[DIR]]/llvm-amdgpu-4.0.0-ieagcs7inf7runpyfvepqkurasoglq4z/amdgcn/bitcode/hip.bc"
 // SPACK-SAME: "-idirafter" "[[DIR]]/hip-4.0.0-5f63slrursbrvfe2txrrjkynbsywsob5/include"



More information about the cfe-commits mailing list