New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Unexpected inability of compiler to optimize away moves #2410
Comments
Another place the compiler should be able to optimize away moves is for functions/methods that take their inputs as When the argument is taken as
When the input is taken as
This introduces hidden run time costs for programmers who prefer pure functions/methods. Also, if they want to use pure functions when working with non-movable types (in the very few cases where it can be done), it is currently not possible. Even if there is no mutation, borrowing does not call
...but passing through
Also see p2666 - Last use optimization:
|
Just FYI, Mojo is likely going to offer explicit return value optimization, which will be useful in cases where you really, really want to make sure that a return value isn't moved. Not that this means general improvements to code generation aren't valuable. Those are very nice as well. 🙂 |
No, this is great. I much prefer the syntactically explicit, guaranteed optimizations that Mojo offers. It removes the worries of relying on the compiler being "sufficiently smart" such that it can recognize the opportunity to perform these optimizations (parallelizing, vectorizing, RVO) in all cases. No more relying on "compiler magic". General codegen improvements like these just ensure that one doesn't have to perform a lot of syntactic ceremony to get good enough performance. |
Bug description
I was testing return value optimization in Mojo when I ran into some unexpected behaviour. For the first snippet linked below, the compiler is able to optimize away the move after returning from
add_10
as demonstrated by the output:But for the second snippet linked below, the compiler is unable to optimize away the move even though the behaviour of the function is still the same:
It's better that programmers shouldn't worry about reordering statements introducing hidden costs and affecting performance.
Steps to reproduce
Move optimized away: https://gist.github.com/modularbot/117c6679aea61ee5143ada73b3f2eb5d
Move not optimized away: https://gist.github.com/modularbot/c60ce97f28c5dcf04233aa601814c8e3
System information
The text was updated successfully, but these errors were encountered: