HomePhabricator

[CUDA][HIP] Fix capturing reference to host variable

Authored by yaxunl on Nov 3 2020, 12:24 PM.

Description

[CUDA][HIP] Fix capturing reference to host variable

In C++ when a reference variable is captured by copy, the lambda
is supposed to make a copy of the referenced variable in the captures
and refer to the copy in the lambda. Therefore, it is valid to capture
a reference to a host global variable in a device lambda since the
device lambda will refer to the copy of the host global variable instead
of access the host global variable directly.

However, clang tries to avoid capturing of reference to a host global variable
if it determines the use of the reference variable in the lambda function is
not odr-use. Clang also tries to emit load of the reference to a global variable
as load of the global variable if it determines that the reference variable is
a compile-time constant.

For a device lambda to capture a reference variable to host global variable
and use the captured value, clang needs to be taught that in such cases the use of the reference
variable is odr-use and the reference variable is not compile-time constant.

This patch fixes that.

Differential Revision: https://reviews.llvm.org/D91088

Details

Event Timeline

What is the expected behavior in the event of no capture clause? The no capture clause situation was connected to why lambdas behaved this way in the first place.

yaxunl added a comment.Dec 7 2020, 1:53 PM

What is the expected behavior in the event of no capture clause? The no capture clause situation was connected to why lambdas behaved this way in the first place.

If there is no capture clause and a device lambda references a host global variable, a diagnostic message will be emitted if the device lambda is actually emitted in device compilation, since a host global variable cannot be accessed by a device function.