Issue 141592
Summary [HIP]: explicit instantiated constexpr functions calling non-constexpr functions fail to compile (`__host__ __device__` function references `__host__` function)
Labels new issue
Assignees
Reporter MiroPalmu
    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).
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to