Skip to content

Meta LLVM codegen regression tracking issue #50422

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
2 of 3 tasks
nikomatsakis opened this issue May 3, 2018 · 11 comments
Closed
2 of 3 tasks

Meta LLVM codegen regression tracking issue #50422

nikomatsakis opened this issue May 3, 2018 · 11 comments
Assignees
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. metabug Issues about issues themselves ("bugs about bugs") P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented May 3, 2018

What is this bug?

From time to time, we encounter regressions that seem to be due to an LLVM misoptimization or other bug (though sometimes it's a case of invalid unsafe code, or rustc generating bad IR). In such cases, we try to narrow the problem down to some LLVM IR that shows the bug independent of Rust; this can then be filed with LLVM and tracked separately. This metabug serves to track those sorts of bugs, and specifically cases where we have not yet managed to create a reproducible test case.

Indications that a bug may fit this category

  • A common symptom is that enabling or disabling LTO causes the bug to appear or disappear.
  • Similarly, bugs that appear shortly after LLVM upgades are common candidates.

Guide to diagnosing

If you'd like to help out, @pnkfelix and @nagisa plan to write a guide for how to extract LLVM IR from rustc. It has sadly not yet been written.

Tracked issues

Priority

This bug is sometimes marked P-high when we find that there are a lot of such problems not yet diagnosed.

@nikomatsakis nikomatsakis added P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. E-help-wanted Call for participation: Help is requested to fix this issue. A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-codegen Area: Code generation I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness labels May 3, 2018
@rust-lang rust-lang deleted a comment from leonardo-m May 7, 2018
@rust-lang rust-lang deleted a comment from leonardo-m May 7, 2018
@jkordish jkordish added the C-bug Category: This is a bug. label May 10, 2018
@kennytm kennytm added the metabug Issues about issues themselves ("bugs about bugs") label May 31, 2018
@gnzlbg
Copy link
Contributor

gnzlbg commented Jul 3, 2018

If you'd like to help out, @pnkfelix and @nagisa plan to write a guide for how to extract LLVM IR from rustc. It has sadly not yet been written.

I end up doing this quite often, and what I do is the following:

  • create a new crate that reproduces the issue, e.g., by adding whatever crate is at fault as a dependency, and using it from there

  • minimize the crate by removing external dependencies (that is, moving everything relevant to the new crate)

  • further minimize the issue by making the code shorter (there are tools that help with this like creduce)

  • emit the LLVM-IR for all code involved (see below) and create a MWE in gcc.godbolt.org:

    • in godbolt choose LLVM-IR as programming language

    • use llc to compile the IR to a particular target as is:

      • -mattr enable target features, -march= selects target, -mcpu= the CPU, etc. You can go to your
      • if you have compiled rustc yourself somewhere, in the target directory you have binaries for llc, opt, etc. Commands like llc -march=help output all architectures available, which is useful because sometimes the Rust arch names and the LLVM names do not match
    • if you want to optimize the LLVM-IR, you can use opt to see how the LLVM optimizations transform it. I haven't used this much, so maybe others can give some tips here.

  • once you have a godbolt link demonstrating the issue, it is pretty easy to fill in an LLVM bug.

To generate LLVM-IR. There are a couple of things to keep in mind, and one typically needs to use them together:

  • RUSTFLAGS="--emit=llvm-ir" makes rustc spit llvm ir in the target directory
    • cargo llvm-ir [options] path spits the llvm-ir for a particular function at a path
      • cargo install cargo-asm installs cargo asm and cargo llvm-ir.
      • --build-type=debug emits code for debug builds, see other options there
    • debug info in LLVM IR can clutter the output a lot: RUSTFLAGS="-C debuginfo=0" is really useful
    • @eddyb mentioned a trick on IRC to prune a lot of LLVM-IR away by using unreachable_unchecked but I can't find the link to it anymore and I forgot to put it in my notes
  • RUSTFLAGS="-C save-temps" outputs llvm bitcode at different stages during compilation, which is sometimes useful. One just needs to convert the bitcode files to .ll files using llvm-dis which should be in the target local compilation of rustc.

Also, I recall reading somewhere that we are now shipping some of the llvm utilities, so one probably does not need to have a rustc compiled from source to use the appropriate versions of llc,opt, llvm-dis, etc. locally, but I haven't had a change to test that yet.

@nikomatsakis
Copy link
Contributor Author

@gnzlbg awesome, thanks! Maybe we can move these notes to the rustc-guide or forge or something, so that we can point at them.

@nagisa
Copy link
Member

nagisa commented Jul 5, 2018

mentioned a trick on IRC to prune a lot of LLVM-IR away by using unreachable_unchecked but I can't find the link to it anymore and I forgot to put it in my notes

unreachable_unchecked is a dangerous tool as while yes, using it will remove all the unreachable code coming after it, LLVM proving it to be reachable will also remove code coming before it. It may also invoke other UB, and I’ve been burnt by it enough times that I simply opt to just delete the code and fix it up to compile again when minimising.

@gnzlbg
Copy link
Contributor

gnzlbg commented Jul 6, 2018

@eddyb do you recall what it was?

@pnkfelix
Copy link
Member

visiting for triage.

These are some awesome notes from @gnzlbg . In order to get this bug out of my own issue queue, I'll try to recreate the steps listed and simultaneously port them into docs that we can put on the rustc-guide (or forge or something, as @nikomatsakis said...)

@eddyb
Copy link
Member

eddyb commented Jul 12, 2018

@gnzlbg Sorry, no. Do you mean something like this?

fn foo(x: Option<String>) -> String {
    match x {
        Some(s) => s,
        None => unsafe { unreachable_unchecked() }
    }
}

This should generate code that's effectively an unchecked variant field access, (x as Some).0, bypassing all the noise from unwrap or anything else that panics.
But it might be safer to use the abort intrinsic instead (which generates a trap).

@gnzlbg
Copy link
Contributor

gnzlbg commented Jul 12, 2018

Maybe, I can't recall any more :/ In any case when there is an issue in the rust book repo where people can submit their own tips and tricks for this: rust-lang/rustc-dev-guide#162

@pnkfelix
Copy link
Member

Visiting for triage: Given rust-lang/rustc-dev-guide#171, I am tempted to downgrade this to P-medium...

@pnkfelix
Copy link
Member

visiting for triage and downgrading to P-medium, as I threatened last week.

@pnkfelix pnkfelix added P-medium Medium priority and removed P-high High priority labels Jul 26, 2018
@Centril
Copy link
Contributor

Centril commented Apr 13, 2020

Removing I-unsound as this issue doesn't track a specific known soundness hole.

@Centril Centril removed the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Apr 13, 2020
@jyn514
Copy link
Member

jyn514 commented Apr 26, 2023

This meta-issue hasn't had subissues in a long time, even though we've found LLVM regressions since 2020. I'm going to close this for now and we can continue using individual issues to track miscompilations and regressions. Happy to be convinced otherwise if anyone feels strongly.

@jyn514 jyn514 closed this as completed Apr 26, 2023
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-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. metabug Issues about issues themselves ("bugs about bugs") P-medium Medium priority 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

9 participants