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

Different ways of writing Option::or codegen differently #124533

Open
scottmcm opened this issue Apr 29, 2024 · 0 comments
Open

Different ways of writing Option::or codegen differently #124533

scottmcm opened this issue Apr 29, 2024 · 0 comments
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Apr 29, 2024

Copying this here from #101210 (comment) cc @veber-alex

Demo: https://rust.godbolt.org/z/96W4h87Yn

The currently-optimized form isn't allowed to be transformed into the better one: https://alive2.llvm.org/ce/z/DXXRPX

A couple thoughts on things that might help in the codegen:

  %4 = load i8, ptr %opta, align 1, !range !3, !noundef !4
  %5 = trunc i8 %4 to i1
  %_3 = zext i1 %5 to i64
  switch i64 %_3, label %bb1 [
    i64 0, label %bb2
    i64 1, label %bb3
  ]

And LLVM forgetting both different ways that we told it that inputs like 0x00_05 (the one Alive found) are UB.

Maybe us emitting trunc nuw will be the eventual solution here? If the trunc nuw+zext could fold to just zext rather than and +zext maybe it'd be enough...


Adding some assumes does give LLVM enough information to be allowed to optimize again (https://alive2.llvm.org/ce/z/e6NWxA) though seemingly it doesn't from that starting point

  %discr0 = and i16 %0, 255
  %discr1 = and i16 %1, 255
  %discr0bound = icmp ult i16 %discr0, 2
  %discr1bound = icmp ult i16 %discr1, 2
  call void @llvm.assume(i1 %discr0bound)
  call void @llvm.assume(i1 %discr1bound)
@scottmcm scottmcm added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-codegen Area: Code generation labels Apr 29, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 29, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants