This is an archive of the discontinued LLVM Phabricator instance.

[CUDA][HIP] Fix template argument deduction
ClosedPublic

Authored by yaxunl on Jul 2 2023, 6:44 AM.

Details

Summary

nvcc allows using std::malloc and std::free in device code.
When std::malloc or std::free is passed as a template
function argument with template argument deduction,
there is no diagnostics. e.g.

#include <memory>

__global__ void kern() {
    void *p = std::malloc(1);
    std::free(p);
}
int main()
{

    std::shared_ptr<float> a;
    a = std::shared_ptr<float>(
      (float*)std::malloc(sizeof(float) * 100),
      std::free
    );
    return 0;
}

However, the same code fails to compile with clang
(https://godbolt.org/z/1roGvo6YY). The reason is
that clang does not have logic to choose a function
argument from an overloaded set of candidates
based on host/device attributes for template argument
deduction.

Currently, clang does have a logic to choose a candidate
based on the constraints of the candidates. This patch
extends that logic to account for the CUDA host/device-based
preference.

Diff Detail

Event Timeline

yaxunl created this revision.Jul 2 2023, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 2 2023, 6:44 AM
yaxunl requested review of this revision.Jul 2 2023, 6:44 AM
tra added inline comments.Jul 11 2023, 1:49 PM
clang/lib/Sema/SemaOverload.cpp
12758–12764

Maybe CheckCUDAPreference should return -1/0/1 or an enum. std::optional does not seem to be very readable here.

E.g. if(MorePreferableByCUDA) sounds like it's going to be satisfied when FD is a better choice than Result, but it's not the case.
I think this would be easier to follow:

if (CheckCUDAPreference(FD, Result) <= 0) // or `!= CP_BETTER`
     continue;
yaxunl marked an inline comment as done.Aug 7 2023, 12:06 PM
yaxunl added inline comments.
clang/lib/Sema/SemaOverload.cpp
12758–12764

will use an integer for that.

yaxunl updated this revision to Diff 547901.Aug 7 2023, 12:08 PM
yaxunl marked an inline comment as done.

revised by comments

tra accepted this revision.Aug 7 2023, 12:39 PM
This revision is now accepted and ready to land.Aug 7 2023, 12:39 PM
This revision was landed with ongoing or failed builds.Aug 8 2023, 2:40 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 8 2023, 2:40 PM