Skip to content

Remove trivial branches in with_cond #13055

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

Closed
wants to merge 1 commit into from

Conversation

Aatch
Copy link
Contributor

@Aatch Aatch commented Mar 21, 2014

Removes branches on constant values when using with_cond, improving the
performance of non-optimized code and (hopefully) improving compile times.

This is a fairly simple front-end optimization.

@cmr if you could see if there is a measurable difference in compile times, that would be awesome

Removes branches on constant values when using with_cond, improving the
performance of non-optimized code and (hopefully) improving compile times.

This is a fairly simple front-end optimization.
@emberian
Copy link
Member

My setup right now doesn't usually provide reliable measures of time, but I can try (in a few hours)

@alexcrichton
Copy link
Member

Do you have any statistics for before/after codegen for this? Front-end optimizations are tricky and can easily go wrong, so it's good to make sure that they're worth it before committing.

@Aatch
Copy link
Contributor Author

Aatch commented Mar 24, 2014

@alexcrichton for what it's worth, we do a similar optimization in controlflow::trans_if.

@alexcrichton
Copy link
Member

Indeed, and it has caused bugs before (see #9484). That was also a proven win because all of our debug! calls could predominately be optimized out in the frontend with --cfg ndebug.

@dotdash
Copy link
Contributor

dotdash commented Mar 29, 2014

FWIW, in librustc.no-opt.ll we have these:

      1   br i1 false, label %cond17, label %next16
      1   br i1 false, label %cond19, label %next18
      1   br i1 false, label %cond22, label %next21
      1   br i1 false, label %cond25, label %next24
      1   br i1 false, label %cond28, label %next27
      2   br i1 false, label %cond2, label %next1
      2   br i1 false, label %cond3, label %next2
      1   br i1 false, label %cond44, label %next43
      1   br i1 false, label %cond4, label %next3
     18   br i1 false, label %cond, label %next

And the %cond* blocks are always just a single call or invoke followed by unreachable. So probably not a huge win.

@alexcrichton
Copy link
Member

@dotdash, is that saying that out of all of rustc there are only 29 constant branches? If so, this frontend optimization may not be worth it at this time.

@dotdash
Copy link
Contributor

dotdash commented Mar 30, 2014

@alexcrichton The above is the output from grep 'br i1 [^%]' librustc.no-opt.ll | sort | uniq -c | grep cond, so 29 constant branches that originate from with_cond. There are a few hundred that originate from || or &&.

@dotdash
Copy link
Contributor

dotdash commented Mar 30, 2014

Correction, they don't directly originate from && or || in the code(at least most of them don't), but from #[deriving(Eq)].

@huonw
Copy link
Member

huonw commented Mar 30, 2014

It would be possible (although annoying) to make the implementation of deriving(Eq) less neat by avoiding doing a straight fold, and so removing the base cases false || ... and true && ... from the output, if it's considered to be a large problem.

@dotdash
Copy link
Contributor

dotdash commented Mar 30, 2014

@huonw, I don't think it's worth the trouble. Better have the shortcut in trans_lazy_binop then. But I doubt that that makes much of a difference either.

@alexcrichton
Copy link
Member

While a plausible optimization, it sounds like we don't generate code that benefits much from this, so I'm going to close this for now.

We may want to implement this in the future, but it's likely more reliable to wait for a visible benefit in order to exercise the optimization.

flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 11, 2024
Only check for `automatically_derived` on impl blocks

This brings us in line with how rustc checks for the attribute. Also note that `unused_attributes` will trigger if it's placed anywhere else.

See:
https://github.com/rust-lang/rust/blob/9a21ac8e7efe9ea26a8065d0959a976ca32039bb/compiler/rustc_passes/src/dead.rs#L400-L403
https://github.com/rust-lang/rust/blob/9a21ac8e7efe9ea26a8065d0959a976ca32039bb/compiler/rustc_passes/src/liveness.rs#L143-L148

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants