[clang] 932c635 - [HIP] link HIP runtime library without --hip-link

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 4 15:30:07 PDT 2023


Author: Yaxun (Sam) Liu
Date: 2023-08-04T18:29:47-04:00
New Revision: 932c63550ad5752ba333a7a4e0045b559c65840b

URL: https://github.com/llvm/llvm-project/commit/932c63550ad5752ba333a7a4e0045b559c65840b
DIFF: https://github.com/llvm/llvm-project/commit/932c63550ad5752ba333a7a4e0045b559c65840b.diff

LOG: [HIP] link HIP runtime library without --hip-link

When doing combined compilation/link for HIP source
files, clang should link the HIP runtime library
automatically without --hip-link.

Reviewed by: Siu Chi Chan, Joseph Huber

Differential Revision: https://reviews.llvm.org/D156426

Added: 
    

Modified: 
    clang/docs/HIPSupport.rst
    clang/lib/Driver/ToolChains/CommonArgs.cpp
    clang/lib/Driver/ToolChains/CommonArgs.h
    clang/lib/Driver/ToolChains/Gnu.cpp
    clang/lib/Driver/ToolChains/MSVC.cpp
    clang/test/Driver/hip-runtime-libs-linux.hip
    clang/test/Driver/hip-runtime-libs-msvc.hip
    clang/test/Driver/rocm-detect.hip

Removed: 
    


################################################################################
diff  --git a/clang/docs/HIPSupport.rst b/clang/docs/HIPSupport.rst
index 4057e1f691593b..8b4649733a9c77 100644
--- a/clang/docs/HIPSupport.rst
+++ b/clang/docs/HIPSupport.rst
@@ -67,11 +67,14 @@ To link a HIP program, use this command:
 
    clang++ --hip-link --offload-arch=gfx906 sample.o -o sample
 
+In the above command, the ``--hip-link`` flag instructs Clang to link the HIP runtime library. However,
+the use of this flag is unnecessary if a HIP input file is already present in your program.
+
 For convenience, Clang also supports compiling and linking in a single step:
 
 .. code-block:: shell
 
-   clang++ --hip-link --offload-arch=gfx906 -xhip sample.cpp -o sample
+   clang++ --offload-arch=gfx906 -xhip sample.cpp -o sample
 
 In the above commands, ``gfx906`` is the GPU architecture that the code is being compiled for. The supported GPU
 architectures can be found in the `AMDGPU Processor Table <https://llvm.org/docs/AMDGPUUsage.html#processors>`_.
@@ -85,7 +88,7 @@ You can use ``--offload-arch=native`` to automatically detect the GPU architectu
 
 .. code-block:: shell
 
-   clang++ --hip-link --offload-arch=native -xhip sample.cpp -o sample
+   clang++ --offload-arch=native -xhip sample.cpp -o sample
 
 
 Path Setting for Dependencies

diff  --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 358d7565f47c2e..6d38cf2f33053f 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2428,10 +2428,10 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
           << LibOmpTargetName << ArchPrefix;
   }
 }
-void tools::addHIPRuntimeLibArgs(const ToolChain &TC,
+void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
                                  const llvm::opt::ArgList &Args,
                                  llvm::opt::ArgStringList &CmdArgs) {
-  if (Args.hasArg(options::OPT_hip_link) &&
+  if ((C.getActiveOffloadKinds() & Action::OFK_HIP) &&
       !Args.hasArg(options::OPT_nostdlib) &&
       !Args.hasArg(options::OPT_no_hip_rt)) {
     TC.AddHIPRuntimeLibArgs(Args, CmdArgs);

diff  --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index 6a8de0f1c36d1f..b1c8441226310c 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -138,7 +138,8 @@ void addFortranRuntimeLibraryPath(const ToolChain &TC,
                                   const llvm::opt::ArgList &Args,
                                   llvm::opt::ArgStringList &CmdArgs);
 
-void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
+void addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
+                          const llvm::opt::ArgList &Args,
                           llvm::opt::ArgStringList &CmdArgs);
 
 const char *getAsNeededOption(const ToolChain &TC, bool as_needed);

diff  --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 485fd67ca53d3e..c8fb05d238782e 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -549,7 +549,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
 
-  addHIPRuntimeLibArgs(ToolChain, Args, CmdArgs);
+  addHIPRuntimeLibArgs(ToolChain, C, Args, CmdArgs);
 
   // The profile runtime also needs access to system libraries.
   getToolChain().addProfileRTLibs(Args, CmdArgs);

diff  --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index a9fe9da4620f80..4966d102c51f1a 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -315,7 +315,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     A.renderAsInput(Args, CmdArgs);
   }
 
-  addHIPRuntimeLibArgs(TC, Args, CmdArgs);
+  addHIPRuntimeLibArgs(TC, C, Args, CmdArgs);
 
   TC.addProfileRTLibs(Args, CmdArgs);
 

diff  --git a/clang/test/Driver/hip-runtime-libs-linux.hip b/clang/test/Driver/hip-runtime-libs-linux.hip
index b0e9f3a460eb03..142582963c958f 100644
--- a/clang/test/Driver/hip-runtime-libs-linux.hip
+++ b/clang/test/Driver/hip-runtime-libs-linux.hip
@@ -43,6 +43,11 @@
 // RUN:   --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
 // RUN:   | FileCheck -check-prefixes=NOHIPRT %s
 
+// Test HIP runtime lib is linked without hip-link if there is HIP input file.
+// RUN: %clang -### --target=x86_64-linux-gnu -nogpuinc -nogpulib \
+// RUN:   --rocm-path=%S/Inputs/rocm %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=ROCM-PATH %s
+
 // ROCM-PATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-lamdhip64"
 // ROCM-RPATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64"
 // ROCM-REL: "-L[[HIPRT:.*/opt/rocm-3.10.0/lib]]" "-lamdhip64"

diff  --git a/clang/test/Driver/hip-runtime-libs-msvc.hip b/clang/test/Driver/hip-runtime-libs-msvc.hip
index 6d70fff023f9f9..34a7d36919c3b5 100644
--- a/clang/test/Driver/hip-runtime-libs-msvc.hip
+++ b/clang/test/Driver/hip-runtime-libs-msvc.hip
@@ -7,4 +7,9 @@
 // RUN:   --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
 // RUN:   | FileCheck %s
 
+// Test HIP runtime lib is linked without --hip-link when there is HIP input file.
+// RUN: %clang -### --target=x86_64-pc-windows-msvc \
+// RUN:   --rocm-path=%S/Inputs/rocm %s 2>&1 \
+// RUN:   | FileCheck %s
+
 // CHECK: "-libpath:{{.*Inputs.*rocm.*lib}}" "amdhip64.lib"

diff  --git a/clang/test/Driver/rocm-detect.hip b/clang/test/Driver/rocm-detect.hip
index 6c6c352dcd1235..68a4c201be9a8d 100644
--- a/clang/test/Driver/rocm-detect.hip
+++ b/clang/test/Driver/rocm-detect.hip
@@ -35,32 +35,32 @@
 
 // Test HIP_PATH overrides ROCM_PATH.
 // RUN: env ROCM_PATH=%S/Inputs/rocm HIP_PATH=%t/myhip \
-// RUN:   %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
+// RUN:   %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
 // RUN:   --print-rocm-search-dirs %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
 
 // Test --hip-path overrides ROCM_PATH.
 // RUN: env ROCM_PATH=%S/Inputs/rocm \
-// RUN:   %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
+// RUN:   %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
 // RUN:   --hip-path=%t/myhip \
 // RUN:   --print-rocm-search-dirs %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
 
 // Test --hip-path overrides --rocm-path.
-// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
+// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
 // RUN:   --hip-path=%t/myhip --rocm-path=%S/Inputs/rocm \
 // RUN:   --print-rocm-search-dirs %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
 
 // Test HIP_PATH overrides --rocm-path.
-// RUN: env HIP_PATH=%t/myhip %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
+// RUN: env HIP_PATH=%t/myhip %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
 // RUN:   --rocm-path=%S/Inputs/rocm \
 // RUN:   --print-rocm-search-dirs %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
 
 // Test empty HIP_PATH does not override --rocm-path.
 // RUN: env HIP_PATH= \
-// RUN:   %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
+// RUN:   %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
 // RUN:   --rocm-path=%S/Inputs/rocm --print-rocm-search-dirs %s 2>&1 \
 // RUN:   | FileCheck -check-prefixes=ROCM-PATH %s
 


        


More information about the cfe-commits mailing list