<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/134222>134222</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect float conversion via __bf16 at -O0
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
PenXLa
</td>
</tr>
</table>
<pre>
# Description
When compiling with clang++ at -O0, a float -> __bf16 -> float conversion using a templated static_cast chain produces an incorrect result (0.0f instead of the expected value). This issue disappears when optimization is enabled (-O1 or higher).
# Reproduction:
Compile the following code with: `clang++ filename.cpp`
```cpp
#include <iostream>
template<typename D, typename S> D fp_cast(S x) {
return static_cast<D>(x);
}
float do_cast(float x) {
return fp_cast<float>(fp_cast<__bf16>(x));
}
int main() {
float t0 = 3.f;
float t2 = do_cast(t0);
std::cout << t2 << std::endl;
return 0;
}
```
*Expected behavior*: `do_cast(3.f)` should return `3.0f`.
*Actual behavior*: It returns `0.0f`.
# Environment
```txt
Ubuntu clang version 20.1.0 (++20250221063304+dc1bd6a8fa6a-1~exp1~20250221183420.63)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-20/bin
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxsVMGO4ygQ_RpyKcXCYDvJwQd30pFaWmlWO7PavbUwlGNWGCzA6cwe5ttX2HF3tmekKDJQ9d6rR1EiBH2xiDUpn0h52ogp9s7Xv6P9-zexaZ36XhPG4YRBej1G7SyhDaHNXz1akG4YtdH2Am869iCNsBfCngh7AhFh-4USdgQBnXFpSfgzvL62XV4t38u2dPaKPmhnYQoJSkDEYTQiooIQRdTyVYoQQfZCWxi9U5PEAMKCttJ5jzKCxzCZCITtaUY70DZEFApcB7FHwNuIMsFdhZmQsEMG33odQIcwISgdxDii8AHeUlFujHrQ_4pUK-gAaEVrUCXw7ZccnIdeX3r0CWfxIhn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