[llvm-bugs] [Bug 39109] constexpr fibonacci is not computed at compile time

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jun 21 13:19:50 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=39109

Richard Smith <richard-llvm at metafoo.co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Richard Smith <richard-llvm at metafoo.co.uk> ---
This is not a bug; arbitrary calls to constexpr functions aren't required (and
shouldn't in general be expected) to be evaluated to constants.

If you want to ensure that a value is computed at compile time, there are
plenty of ways to request that (eg, use it to initialize a constexpr variable
or an enumerator, pass it as a template argument, use it as an array bound,
[since C++20] use a consteval function, ...). Use in a return statement is not
such a context.

Since C++20, the initializer of a local const variable of integer or unscoped
enumeration type joins the list (see http://eel.is/c++draft/expr.const#14.5),
which is why main2 now returns a constant.

I don't think we have any intent to do anything about this. Whether the call to
fib(7) in main is reduced to a constant or not is something we leave up to the
optimizer. We don't convey the fact that the function is declared 'constexpr'
as a stronger hint to optimize it.

(I think it would be entirely reasonable for the optimizer to attempt symbolic
evaluation of function calls to see it can reduce them to a small amount of
straight-line code or to a constant. But I don't think we have such an
optimization pass just yet.)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200621/9ed6f1f2/attachment.html>


More information about the llvm-bugs mailing list