<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141592>141592</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[HIP]: explicit instantiated constexpr functions calling non-constexpr functions fail to compile (`__host__ __device__` function references `__host__` function)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
MiroPalmu
</td>
</tr>
</table>
<pre>
Following *HIP* code will not compile (https://godbolt.org/z/9Y16zxv6a):
```cpp
void fail();
template <typename T>
constexpr void foo(bool x) {
if (x) fail();
}
template void foo<int>(bool);
```
```shell
<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).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVU-Po7gT_TTOpTQRmBDCgQOdnqj78JP6MNJPe4oMLoJXxmZtkz_96VcF6SSdzuzMYSIkIlyuelX1XpXwXu0MYsHSJ5Y-z8QQWuuK_yln34Tuhlll5anYWK3tQZkdMF6-vL4xXkJtJcJBaQ3GBqht1yuNwPiqDaH3LCkZ3zC-2VlZWR3m1u0Y37wzvsn_ipfvx_1SMJ6zpAQWlfQso-mp-55F5d4qCY1QmvHVaPc0m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