Skip to content
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

RangeInclusive and Range bits are not optimizes a way but also not used in a function #116861

Closed
vDorst opened this issue Oct 17, 2023 · 4 comments · Fixed by #117038
Closed

RangeInclusive and Range bits are not optimizes a way but also not used in a function #116861

vDorst opened this issue Oct 17, 2023 · 4 comments · Fixed by #117038
Labels
C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code.

Comments

@vDorst
Copy link

vDorst commented Oct 17, 2023

I tried this code: Also godbolt link https://rust.godbolt.org/z/9qs4cTP4v

pub struct MB {
    addr: u8,
}

pub fn new(addr: u8) -> Option<MB> {
    if !(1..=247).contains(&addr) {
        return None;
    }
    Some(MB { addr })
}

I expected to see this happen:

I was trying this code to see what kind of assemble the compilers makes.
I tried different optimize levels.
I see with optimize levels s and 1, that the RangeInclusive bits are still there but not used in the function.
function is self takes about 5 instructions and the RangeInclusive code about 40 instruction, which is dead code.

Instead, this happened:
RangeInclusive bits removed in all the optimize levels.
I was using a similar code on a embedded device.

FYI: It also happens with Range . Remove the = before 247.

Meta

I see this happen on stable and nightly both for x86_64 and thumbv6/v7.

@vDorst vDorst added the C-bug Category: This is a bug. label Oct 17, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 17, 2023
@vDorst vDorst changed the title RangeInclusive bits are not optimizes a way but also not used in a function RangeInclusive and Range bits are not optimizes a way but also not used in a function Oct 17, 2023
@saethlin saethlin added I-slow Issue: Problems and improvements with respect to performance of generated code. I-heavy Issue: Problems and improvements with respect to binary size of generated code. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed C-bug Category: This is a bug. labels Oct 18, 2023
@scottmcm
Copy link
Member

This is, unfortunately, well known. s and z aren't necessarily smaller https://internals.rust-lang.org/t/should-default-recommended-opt-level-for-minimizing-binary-size-be-s-or-z/19685/4?u=scottmcm

It'll be optimized out in -O (which is opt-level=2).

@saethlin saethlin removed I-slow Issue: Problems and improvements with respect to performance of generated code. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 22, 2023
@saethlin
Copy link
Member

Please open issues if you see any more problems like this. I think this general situation is more of a problem than our issue tracker suggests. If you report them we'll be more aware of the trend, and we can paper over individual situations like this one. I think that's what my linked PR does.

@vDorst
Copy link
Author

vDorst commented Oct 22, 2023

@scottmcm @saethlin maybe to clarify.
With opt_level s the assembly of the generated function is fine and optimized.

example::new:
        lea     eax, [rdi - 1]
        cmp     al, -9
        setb    al
        mov     edx, edi
        ret

But the RangeInclusive bits are not removed and not used.
@saethlin so I am not sure if inline will help in this case.

@saethlin
Copy link
Member

#[inline] does more than just apply an optimization hint. I disassembled the rlib files that come out of compiling that example code and confirmed that my PR does reduce the objdump output to just one function.

bors added a commit to rust-lang-ci/rust that referenced this issue Oct 22, 2023
Add #[inline] to some recalcitrant ops::range methods

Fixes rust-lang#116861
@bors bors closed this as completed in 7314873 Oct 28, 2023
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Nov 2, 2023
Add #[inline] to some recalcitrant ops::range methods

Fixes rust-lang/rust#116861
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Add #[inline] to some recalcitrant ops::range methods

Fixes rust-lang/rust#116861
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Add #[inline] to some recalcitrant ops::range methods

Fixes rust-lang/rust#116861
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants