Index: clang/docs/HIPSupport.rst =================================================================== --- clang/docs/HIPSupport.rst +++ clang/docs/HIPSupport.rst @@ -67,11 +67,14 @@ 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 `_. @@ -85,7 +88,7 @@ .. 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 Index: clang/include/clang/Driver/Driver.h =================================================================== --- clang/include/clang/Driver/Driver.h +++ clang/include/clang/Driver/Driver.h @@ -308,6 +308,9 @@ /// -include foo.h to -include-pch foo.h.pch. unsigned ProbePrecompiled : 1; + /// Whether there are HIP input files. + bool HasHIPInputs = false; + public: // getFinalPhase - Determine which compilation mode we are in and record // which option we used to determine the final phase. @@ -705,6 +708,9 @@ return IsOffload ? OffloadLTOMode : LTOMode; } + /// Whether there are HIP input files. + bool hasHIPInputs() const { return HasHIPInputs; } + private: /// Tries to load options from configuration files. Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -786,12 +786,7 @@ llvm::any_of(Inputs, [](std::pair &I) { return types::isCuda(I.first); }); - bool IsHIP = - llvm::any_of(Inputs, - [](std::pair &I) { - return types::isHIP(I.first); - }) || - C.getInputArgs().hasArg(options::OPT_hip_link); + bool IsHIP = hasHIPInputs() || C.getInputArgs().hasArg(options::OPT_hip_link); if (IsCuda && IsHIP) { Diag(clang::diag::err_drv_mix_cuda_hip); return; @@ -1488,6 +1483,11 @@ InputList Inputs; BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs); + HasHIPInputs = + llvm::any_of(Inputs, [](std::pair &I) { + return types::isHIP(I.first); + }); + // Populate the tool chains for the offloading devices, if any. CreateOffloadingDeviceToolChains(*C, Inputs); Index: clang/lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- clang/lib/Driver/ToolChains/CommonArgs.cpp +++ clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -2431,7 +2431,7 @@ void tools::addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_hip_link) && + if ((Args.hasArg(options::OPT_hip_link) || TC.getDriver().hasHIPInputs()) && !Args.hasArg(options::OPT_nostdlib) && !Args.hasArg(options::OPT_no_hip_rt)) { TC.AddHIPRuntimeLibArgs(Args, CmdArgs); Index: clang/test/Driver/hip-runtime-libs-linux.hip =================================================================== --- clang/test/Driver/hip-runtime-libs-linux.hip +++ 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 \ +// 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" Index: clang/test/Driver/hip-runtime-libs-msvc.hip =================================================================== --- clang/test/Driver/hip-runtime-libs-msvc.hip +++ 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" Index: clang/test/Driver/rocm-detect.hip =================================================================== --- clang/test/Driver/rocm-detect.hip +++ 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