What is the reason for emitting useless stack-work code?

I have compared two very short versions of the same code in C and Rust and found that Rust emits useless code around the stack.

C: https://godbolt.org/g/pc2u4J (gcc) https://godbolt.org/g/fiZM2h (clang)

Rust: https://godbolt.org/g/sTkQtU

Why rust always emit this?

push rbp
mov rbp, rsp

and this

pop rbp

For such an easy case?

I believe this is because of an extra compiler flag godbolt includes by default (see https://stackoverflow.com/a/45562380 and https://github.com/rust-lang/rust/issues/11906), which you can turn off by explicitly adding “-C debuginfo=0”. When I do that, these superfluous instructions go away and the Rust and C outputs are identical.


Awesome thanks! I thought how it can be possible that both clang and rust use libllvm and have different code… So I was wrong, Thanks! Though, I don’t understand why the godbolt adds any flags which are not visible.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.