[llvm-bugs] [Bug 46922] New: Wrongsided function picked for overload when valid template function exists.

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jul 30 15:37:47 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=46922

            Bug ID: 46922
           Summary: Wrongsided function picked for overload when valid
                    template function exists.
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Keywords: compile-fail, regression
          Severity: normal
          Priority: P
         Component: CUDA
          Assignee: unassignedclangbugs at nondot.org
          Reporter: canlewi at sandia.gov
                CC: llvm-bugs at lists.llvm.org

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
https://github.com/llvm/llvm-project/blob/8224c5047e9cef2db4b0e31427cdf90a2568a341/clang/lib/Sema/SemaOverload.cpp#L9860

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200730/953fa7c7/attachment.html>


More information about the llvm-bugs mailing list