[PATCH] D139045: [HIP] use detected GPU in --offload-arch

Yaxun Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 14:41:46 PST 2022


yaxunl created this revision.
yaxunl added a reviewer: tra.
Herald added subscribers: kosarev, kerbowa, jvesely.
Herald added a project: All.
yaxunl requested review of this revision.
Herald added a subscriber: MaskRay.

Currently HIP uses gfx803 as offload arch if not
specified. This is not convenient for command
line users.

This patch detects system GPU and use them
in --offload-arch if not specified. If system GPU
cannot be detected clang will fall back to gfx893.


https://reviews.llvm.org/D139045

Files:
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/AMDGPU.h


Index: clang/lib/Driver/ToolChains/AMDGPU.h
===================================================================
--- clang/lib/Driver/ToolChains/AMDGPU.h
+++ clang/lib/Driver/ToolChains/AMDGPU.h
@@ -107,6 +107,9 @@
   llvm::Error getSystemGPUArch(const llvm::opt::ArgList &Args,
                                std::string &GPUArch) const;
 
+  llvm::Error detectSystemGPUs(const llvm::opt::ArgList &Args,
+                               SmallVector<std::string, 1> &GPUArchs) const;
+
 protected:
   /// Check and diagnose invalid target ID specified by -mcpu.
   virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const;
@@ -126,8 +129,6 @@
   /// Get GPU arch from -mcpu without checking.
   StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const;
 
-  llvm::Error detectSystemGPUs(const llvm::opt::ArgList &Args,
-                               SmallVector<std::string, 1> &GPUArchs) const;
 };
 
 class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -3095,13 +3095,22 @@
       for (auto Arch : GpuArchs)
         GpuArchList.push_back(Arch.data());
 
-      // Default to sm_20 which is the lowest common denominator for
+      // CUDA defaults to sm_20 which is the lowest common denominator for
       // supported GPUs.  sm_20 code should work correctly, if
       // suboptimally, on all newer GPUs.
       if (GpuArchList.empty()) {
         if (ToolChains.front()->getTriple().isSPIRV())
           GpuArchList.push_back(CudaArch::Generic);
-        else
+        else if (ToolChains.front()->getTriple().isAMDGPU()) {
+          auto *TC = static_cast<const toolchains::HIPAMDToolChain *>(
+              ToolChains.front());
+          SmallVector<std::string, 1> GPUs;
+          if (!TC->detectSystemGPUs(Args, GPUs)) {
+            for (auto GPU : GPUs)
+              GpuArchList.push_back(Args.MakeArgString(GPU));
+          } else
+            GpuArchList.push_back(DefaultCudaArch);
+        } else
           GpuArchList.push_back(DefaultCudaArch);
       }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139045.479071.patch
Type: text/x-patch
Size: 2194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221130/4e904c97/attachment.bin>


More information about the cfe-commits mailing list