<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62479>62479</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Incorrect constant folding behavior of floating-point operations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:optimizations,
            floating-point
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          AaronBallman
      </td>
    </tr>
</table>

<pre>
    LLVM's constant folding appears to use the host system's math library to perform constant folding. e.g.,
https://github.com/llvm/llvm-project/blob/faa43a80f70c639f40021a175c60857bdcf7cc1f/llvm/lib/Analysis/ConstantFolding.cpp#L2407
(This is predominately a concern for cross-compilation scenarios where the host and target are different in interesting ways.)

What's more, it happily uses `double` to perform its operations, regardless of operand type. e.g.,
https://github.com/llvm/llvm-project/blob/faa43a80f70c639f40021a175c60857bdcf7cc1f/llvm/lib/Analysis/ConstantFolding.cpp#L1797
(This is predominately a concern about the use of `double` for all operations, regardless of source operand type, for native and cross compilation scenarios.)

This doesn't match conformance expectations for C. C2x 6.6p17 reads,
> The semantic rules for the evaluation of a constant expression are the same as for nonconstant expressions

while p5 reads,
> An expression that evaluates to a constant is required in several contexts. If a floating expression is evaluated in the translation environment, the arithmetic range and precision shall be at least as great as if the expression were being evaluated in the execution environment.

Use of `double` for performing the computation seems somewhat defensible per p5, but not for `long double` constant folding. It might be more defensible to use the host system's math library only when the host system floating-point model matches the target architecture's well enough to meet conformance requirements.

The requirements in C++ are a bit more relaxed than in C, but still recommend following the target architecture's semantics when folding at compile time: http://eel.is/c++draft/expr.const#14
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVsFu4zYQ_Rr6MlhBpmzLOvjgZGtgge1t255H1EhiQZEqZxTH_fqCkpPY2WyxvfWSBCFn9Oa9N09CZtt5ooPaPqjt5xVO0od4OGIM_gGdG9Cv6tBcDl-__v6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