<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/108548>108548</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            CUDA: Host-side virtual destructor of template class leaks into PTX as weak function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mkuron
      </td>
    </tr>
</table>

<pre>
    Consider the following bit of code:
```c++
void _Adjust_manually_vector_aligned()
{}

template <size_t>
constexpr void _Deallocate() {
 _Adjust_manually_vector_aligned();
}

struct basic_string {
 constexpr ~basic_string() {
        _Deallocate<8>();
 }
};

template <class Var_t>
class E {
  virtual ~E() = default;
  basic_string _name;
};

template class E<float>;
```
Including this into a CUDA file (you don't even have to use the class in a device-side function or variable) leads to errors like `ptxas fatal : Unresolved extern function '_Z31_Adjust_manually_vector_alignedv'` or, when compiling with `-fgpu-rdc`, corresponding errors from `nvlink`. The resulting PTX contains unreferenced `.weak .func`s of the defaulted virtual destructor. This bears some similarity to #98151, but the circumstances under which the extraneous symbol is emitted differ.

The code for `basic_string` and the functions it calls was reduced from https://github.com/microsoft/STL/blob/vs-2022-17.0/stl/inc/xstring and https://github.com/microsoft/STL/blob/vs-2022-17.0/stl/inc/xmemory. This makes it impossible to use `std::string` in template classes with defaulted virtual destructors when using CUDA on Windows in C++20 mode.

Godbolt: https://godbolt.org/z/7MTda4Kq4

NVCC does not show this issue, it never generates `.weak .func` symbols for host-side destructors. Clang appears to only be generating these extraneous `.weak .func` symbols for code that exactly follows the above pattern; eliminating the templates, the `virtual`, the `default`, or the `constexpr`s makes the problem go away.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk9v47YT_TT0ZRBDphzLOfiQOOvfr-gfFGh2W_RiUOTI4obiuBxKjvewn72gJP9r0G0PDYwkpsiZN_PePEox251HXIn7J3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