This is an archive of the discontinued LLVM Phabricator instance.

[CUDA][HIP] Allow function-scope static const variable
ClosedPublic

Authored by yaxunl on Jul 27 2018, 12:38 PM.

Details

Summary

CUDA 8.0 E.3.9.4 says: Within the body of a __device__ or __global__
function, only __shared__ variables or variables without any device
memory qualifiers may be declared with static storage class.

It is unclear how a function-scope non-const static variable
without device memory qualifier is implemented, therefore only static
const variable without device memory qualifier is allowed, which
can be emitted as a global variable in constant address space.

Currently clang only allows function-scope static variable with
__shared__ qualifier.

This patch also allows function-scope static const variable without
device memory qualifier and emits it as a global variable in constant
address space.

Diff Detail

Repository
rC Clang

Event Timeline

yaxunl updated this revision to Diff 157740.Jul 27 2018, 12:38 PM
yaxunl created this revision.
yaxunl edited the summary of this revision. (Show Details)

update diagnostic message.

tra added a comment.Jul 27 2018, 1:08 PM

This patch also allows function-scope static const variable without device memory qualifier and emits it as a global variable in constant address space.

What does NVCC do with local static const variables?

In D49931#1178720, @tra wrote:

This patch also allows function-scope static const variable without device memory qualifier and emits it as a global variable in constant address space.

What does NVCC do with local static const variables?

Both const and non-const static var without device memory qualifier are allowed. It emits it as a global variable in global address space.

tra added a comment.Jul 27 2018, 2:31 PM

Looks OK overall except for the huge if below.

lib/Sema/SemaDecl.cpp
11923–11930

This is rather convoluted. It would make it somewhat more readable if we could split CUDADiagIfDeviceCode into its own if().

Or, maybe use a lambda + early exit or, perhaps even goto to break down this huge if:

[&](){
   if (VD->hasAttr<CUDASharedAttr>()) return;
   if (VD->getType().isConstQualified() 
        && !(VD->hasAttr<CUDADeviceAttr>()||VD->hasAttr<CUDAConstantAttr>())
        return;
   if (CUDADiagIfDeviceCode(VD->getLocation(), diag::err_device_static_local_var)
              << CurrentCUDATarget()))
          VD->setInvalidDecl();
}()
yaxunl marked an inline comment as done.Jul 27 2018, 2:38 PM
yaxunl added inline comments.
lib/Sema/SemaDecl.cpp
11923–11930

The lambda looks good. Will use it. Thanks!

yaxunl updated this revision to Diff 157778.Jul 27 2018, 2:58 PM
yaxunl marked an inline comment as done.

Revised by Artem's comments.

tra accepted this revision.Jul 27 2018, 4:08 PM
This revision is now accepted and ready to land.Jul 27 2018, 4:08 PM
This revision was automatically updated to reflect the committed changes.