Open
Description
Following HIP code doesn't compile (https://godbolt.org/z/9Y16zxv6a):
void fail();
template <typename T>
constexpr void foo(bool x) {
if (x) fail();
}
template void foo<int>(bool);
<source>:5:12: error: reference to __host__ function 'fail' in __host__ __device__ function
5 | if (x) fail();
| ^
<source>:8:15: note: in instantiation of function template specialization 'foo<int>' requested here
8 | template void foo<int>(bool);
| ^
<source>:1:6: note: 'fail' declared here
1 | void fail();
| ^
1 error generated when compiling for gfx906.
Compiler returned: 1
In C++ calling non-constexpr functions from constexpr functions is allowed as long as they are not used during compile time.
If I understand correctly, in HIP constexpr functions are implicitly annotated with __host__ __device__
and
this explains the error: reference to __host__ function 'fail' in __host__ __device__ function
However there must be some additional logic which fails for the explicit instantiation as following is accepted (https://godbolt.org/z/7asfWPefs):
void fail();
constexpr void foo(bool x) {
if (x) fail();
}
This problem comes up when trying to use external library in host code as part of HIP project (e.g. spdlog with bundeled fmt).