Skip to content

Conversation

AlexVlx
Copy link
Contributor

@AlexVlx AlexVlx commented Aug 20, 2025

In Windows, on a MSVC environment (e.g. when linking against the UCRT), -nostdlib is used (for example, by CMake) to prevent linking in non-existent glibc. However, an unintended side-effect is that we end up never linking in the HIP RT in these circumstances, even when --hip-link is explicitly specified. This breaks hipstdpar, where we implicitly link in the HIP RT when --hipstdpar is passed as a link flag. To fix this, we relax the restriction on linking the HIP RT, for known MSVC environments.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Aug 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-clang

Author: Alex Voicu (AlexVlx)

Changes

In Windows, on a MSVC environment (e.g. when linking against the UCRT), -nostdlib is used (for example, by CMake) to prevent linking in non-existent glibc. However, an unintended side-effect is that we end up never linking in the HIP RT in these circumstances, even when --hip-link is explicitly specified. This breaks hipstdpar, where we implicitly link in the HIP RT when --hipstdpar is passed as a link flag. To fix this, we relax the restriction on linking the HIP RT, for known MSVC environments.


Full diff: https://github.com/llvm/llvm-project/pull/154630.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+2-1)
  • (modified) clang/test/Driver/hip-runtime-libs-msvc.hip (+7)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index a21f89da55009..055906a8d721d 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2986,7 +2986,8 @@ void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
                                  const llvm::opt::ArgList &Args,
                                  llvm::opt::ArgStringList &CmdArgs) {
   if ((C.getActiveOffloadKinds() & Action::OFK_HIP) &&
-      !Args.hasArg(options::OPT_nostdlib) &&
+      (!Args.hasArg(options::OPT_nostdlib) ||
+       TC.getTriple().isKnownWindowsMSVCEnvironment()) &&
       !Args.hasArg(options::OPT_no_hip_rt) && !Args.hasArg(options::OPT_r)) {
     TC.AddHIPRuntimeLibArgs(Args, CmdArgs);
   } else {
diff --git a/clang/test/Driver/hip-runtime-libs-msvc.hip b/clang/test/Driver/hip-runtime-libs-msvc.hip
index 943cd0569f4fd..d282a2646342a 100644
--- a/clang/test/Driver/hip-runtime-libs-msvc.hip
+++ b/clang/test/Driver/hip-runtime-libs-msvc.hip
@@ -10,4 +10,11 @@
 // RUN:   --rocm-path=%S/Inputs/rocm %s 2>&1 \
 // RUN:   | FileCheck %s
 
+// Test HIP runtime lib is linked even if -nostdlib is specified when the input
+// is a HIP file. This is important when composing with e.g. the UCRT or other
+// non glibc-like implementations of the C standard library.
+// RUN: %clang -### --target=x86_64-pc-windows-msvc -nogpuinc -nogpulib \
+// RUN:   -nostdlib --rocm-path=%S/Inputs/rocm %s 2>&1 \
+// RUN:   | FileCheck %s
+
 // CHECK: "-libpath:{{.*Inputs.*rocm.*lib}}" "amdhip64.lib"

@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-clang-driver

Author: Alex Voicu (AlexVlx)

Changes

In Windows, on a MSVC environment (e.g. when linking against the UCRT), -nostdlib is used (for example, by CMake) to prevent linking in non-existent glibc. However, an unintended side-effect is that we end up never linking in the HIP RT in these circumstances, even when --hip-link is explicitly specified. This breaks hipstdpar, where we implicitly link in the HIP RT when --hipstdpar is passed as a link flag. To fix this, we relax the restriction on linking the HIP RT, for known MSVC environments.


Full diff: https://github.com/llvm/llvm-project/pull/154630.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+2-1)
  • (modified) clang/test/Driver/hip-runtime-libs-msvc.hip (+7)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index a21f89da55009..055906a8d721d 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2986,7 +2986,8 @@ void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
                                  const llvm::opt::ArgList &Args,
                                  llvm::opt::ArgStringList &CmdArgs) {
   if ((C.getActiveOffloadKinds() & Action::OFK_HIP) &&
-      !Args.hasArg(options::OPT_nostdlib) &&
+      (!Args.hasArg(options::OPT_nostdlib) ||
+       TC.getTriple().isKnownWindowsMSVCEnvironment()) &&
       !Args.hasArg(options::OPT_no_hip_rt) && !Args.hasArg(options::OPT_r)) {
     TC.AddHIPRuntimeLibArgs(Args, CmdArgs);
   } else {
diff --git a/clang/test/Driver/hip-runtime-libs-msvc.hip b/clang/test/Driver/hip-runtime-libs-msvc.hip
index 943cd0569f4fd..d282a2646342a 100644
--- a/clang/test/Driver/hip-runtime-libs-msvc.hip
+++ b/clang/test/Driver/hip-runtime-libs-msvc.hip
@@ -10,4 +10,11 @@
 // RUN:   --rocm-path=%S/Inputs/rocm %s 2>&1 \
 // RUN:   | FileCheck %s
 
+// Test HIP runtime lib is linked even if -nostdlib is specified when the input
+// is a HIP file. This is important when composing with e.g. the UCRT or other
+// non glibc-like implementations of the C standard library.
+// RUN: %clang -### --target=x86_64-pc-windows-msvc -nogpuinc -nogpulib \
+// RUN:   -nostdlib --rocm-path=%S/Inputs/rocm %s 2>&1 \
+// RUN:   | FileCheck %s
+
 // CHECK: "-libpath:{{.*Inputs.*rocm.*lib}}" "amdhip64.lib"

@AlexVlx AlexVlx requested a review from yxsamliu August 20, 2025 21:58
@AlexVlx AlexVlx merged commit c984132 into llvm:main Aug 22, 2025
12 checks passed
@AlexVlx AlexVlx deleted the nostdlib_should_not_always_imply_no_hip_lib branch August 22, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants