[clang] [CUDA] make kernel stub ICF-proof (PR #90155)

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 29 16:12:11 PDT 2024


================
@@ -424,6 +424,34 @@ void CGNVCUDARuntime::emitDeviceStubBodyNew(CodeGenFunction &CGF,
       CGM.CreateRuntimeFunction(FTy, LaunchKernelName);
   CGF.EmitCall(FI, CGCallee::forDirect(cudaLaunchKernelFn), ReturnValueSlot(),
                LaunchKernelArgs);
+
+  // To prevent CUDA device stub functions from being merged by ICF in MSVC
+  // environment, create an unique global variable for each kernel and write to
+  // the variable in the device stub.
+  if (CGM.getContext().getTargetInfo().getCXXABI().isMicrosoft() &&
+      !CGF.getLangOpts().HIP) {
+    llvm::Function *KernelFunction = llvm::cast<llvm::Function>(Kernel);
+    if (KernelFunction->hasComdat()) {
----------------
rnk wrote:

ICF may apply to all functions under [`/Gy`](https://learn.microsoft.com/en-us/cpp/build/reference/gy-enable-function-level-linking?view=msvc-170), and those are not reflected in the IR, so I would just do this for all kernels.

https://github.com/llvm/llvm-project/pull/90155


More information about the cfe-commits mailing list