[CUDA][HIP] Fix constexpr variables for C++17
authorYaxun (Sam) Liu <yaxun.liu@amd.com>
Fri, 1 May 2020 15:30:24 +0000 (11:30 -0400)
committerYaxun (Sam) Liu <yaxun.liu@amd.com>
Thu, 4 Jun 2020 01:56:52 +0000 (21:56 -0400)
commit049d860707ef22978b9379fee6dce38c66a22671
tree8bd4686a0f42597f0424f9a916112435c9282f94
parentb6020c330d3826aa542164842d6ba71fdee3650b
[CUDA][HIP] Fix constexpr variables for C++17

constexpr variables are compile time constants and implicitly const, therefore
they are safe to emit on both device and host side. Besides, in many cases
they are intended for both device and host, therefore it makes sense
to emit them on both device and host sides if necessary.

In most cases constexpr variables are used as rvalue and the variables
themselves do not need to be emitted. However if their address is taken,
then they need to be emitted.

For C++14, clang is able to handle that since clang emits them with
available_externally linkage together with the initializer.

However for C++17, the constexpr static data member of a class or template class
become inline variables implicitly. Therefore they become definitions with
linkonce_odr or weak_odr linkages. As such, they can not have available_externally
linkage.

This patch fixes that by adding implicit constant attribute to
file scope constexpr variables and constexpr static data members
in device compilation.

Differential Revision: https://reviews.llvm.org/D79237
clang/include/clang/Sema/Sema.h
clang/lib/Sema/SemaCUDA.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
clang/test/CodeGenCUDA/constexpr-variables.cu [new file with mode: 0644]
clang/test/SemaCUDA/constexpr-variables.cu [new file with mode: 0644]