This code with an inner immediately invoked lambda function:
use std::num::NonZeroI32;
pub fn foo(x: NonZeroI32) -> i32 {
(|x: &NonZeroI32| 33 / x.get())(&x)
}
Compiled using:
rustc 1.65.0-nightly (f03ce3096 2022-08-08)
binary: rustc
commit-hash: f03ce30962cf1b2a5158667eabae8bf6e8d1cb03
commit-date: 2022-08-08
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6
With arguments:
--edition 2021 -C opt-level=3 -Z mir-opt-level=3
Gives a nice asm:
foo:
mov eax, 33
xor edx, edx
idiv edi
ret
But cranking opt up to the 4th level:
--edition 2021 -C opt-level=3 -Z mir-opt-level=4
Gives an asm similar to not using the lambda function at all:
foo:
test edi, edi
je .LBB0_2
mov eax, 33
xor edx, edx
idiv edi
ret
.LBB0_2:
push rax
lea rdi, [rip + str.0]
lea rdx, [rip + .L__unnamed_1]
mov esi, 25
call qword ptr [rip + core::panicking::panic@GOTPCREL]
ud2
(Inspired by: https://old.reddit.com/r/rust/comments/wkw55b/help_matching_c_codegen_for_a_small_function/ijr7o5x/ ).
This code with an inner immediately invoked lambda function:
Compiled using:
With arguments:
Gives a nice asm:
But cranking opt up to the 4th level:
Gives an asm similar to not using the lambda function at all:
(Inspired by: https://old.reddit.com/r/rust/comments/wkw55b/help_matching_c_codegen_for_a_small_function/ijr7o5x/ ).