<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Wrongsided function picked for overload when valid template function exists."
   href="https://bugs.llvm.org/show_bug.cgi?id=46922">46922</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrongsided function picked for overload when valid template function exists.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Keywords</th>
          <td>compile-fail, regression
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>CUDA
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>canlewi@sandia.gov
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code doesn’t compile with newer versions of clang:

template<class T>
__device__ __host__ int foo(T *x) {
    return 1;
}

__device__ int foo(int *x) {
    return 2;
}

__host__ int foo(long *x) {
    return 3;
}

__device__ __host__ int bar() {
  auto long_val = 1l;
    return foo(&long_val);
}


clang++ -O2 -g -x cuda --cuda-gpu-arch=sm_61 -std=c++14 -o main -c main.cpp
give me:
error: reference to __host__ function 'foo' in __host__ __device__ function
    return foo(&long_val);
           ^
main.cpp:10:14: note: 'foo' declared here
__host__ int foo(long *x) {

I believe that the issue is at
<a href="https://github.com/llvm/llvm-project/blob/8224c5047e9cef2db4b0e31427cdf90a2568a341/clang/lib/Sema/SemaOverload.cpp#L9860">https://github.com/llvm/llvm-project/blob/8224c5047e9cef2db4b0e31427cdf90a2568a341/clang/lib/Sema/SemaOverload.cpp#L9860</a>

It’s possible that IdentifyCUDAPreference will return CFP_HostDevice for valid
overloads, but this code doesn’t erase the wrong side candidates in that case. 
Then best overload selection picks the wrong side candidate.  

If I rewrite those lines as:

 bool ContainsSameSideCandidate =
     llvm::any_of(Candidates, [&](OverloadCandidate *Cand) {
       // Check viable function only.
       if (Cand->Viable && Cand->Function) {
         auto MatchType = S.IdentifyCUDAPreference(Caller, Cand->Function);
         return MatchType == Sema::CFP_HostDevice ||
                MatchType == Sema::CFP_SameSide;
       }
       return false;
     });

My code compiles again.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>