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

Incorrect LLVM generated with mir-opt-level=2: cannot invoke intrinsic #69911

Closed
tmiasko opened this issue Mar 11, 2020 · 2 comments · Fixed by #70467
Closed

Incorrect LLVM generated with mir-opt-level=2: cannot invoke intrinsic #69911

tmiasko opened this issue Mar 11, 2020 · 2 comments · Fixed by #70467
Assignees
Labels
A-codegen Area: Code generation A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tmiasko
Copy link
Contributor

tmiasko commented Mar 11, 2020

struct A;

impl Drop for A {
    fn drop(&mut self) {
        println!("A");
    }
}

fn main() {
    let a = A;
    std::sync::atomic::spin_loop_hint();
    std::mem::forget(a);
}
$ rustc -Copt-level=0 -Zmir-opt-level=2 a.rs
Cannot invoke an intrinsic other than donothing, patchpoint, statepoint, coro_resume or coro_destroy
  invoke void @llvm.x86.sse2.pause()
          to label %15 unwind label %16
Cannot invoke an intrinsic other than donothing, patchpoint, statepoint, coro_resume or coro_destroy
  invoke void @llvm.x86.sse2.pause()
          to label %15 unwind label %16
in function _ZN1a4main17h5a7c080779719c70E
LLVM ERROR: Broken function found, compilation aborted!
@tmiasko tmiasko added the C-bug Category: This is a bug. label Mar 11, 2020
@jonas-schievink jonas-schievink added A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. labels Mar 11, 2020
@jonas-schievink
Copy link
Contributor

I guess intrinsics need to be called and not invoked? Maybe the MIR inliner is inlining the intrinsic's wrapper, but keeps the unwind target intact when it shouldn't. Or this should be handled in codegen, not sure.

@tmiasko
Copy link
Contributor Author

tmiasko commented Mar 12, 2020

The implementation ends up calling pause intrinsic declared as follows:

#![feature(link_llvm_intrinsics)]

extern "C" {
    #[link_name = "llvm.x86.sse2.pause"]
    fn pause();
}

The rustc codegen is completely unaware that it is dealing with an intrinsic,
it just changes the symbol name. Looks like the whole feature was introduced by
accident, and link_llvm_intrinsics feature gate was introduced after the fact
to make it unstable.

@jonas-schievink jonas-schievink added the A-codegen Area: Code generation label Mar 12, 2020
@jonas-schievink jonas-schievink added the A-mir-opt Area: MIR optimizations label Mar 29, 2020
@wesleywiser wesleywiser self-assigned this Mar 30, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 17, 2020
Use `call` instead of `invoke` for functions that cannot unwind

The `FnAbi` now knows if the function is allowed to unwind. If a
function isn't allowed to unwind, we can use a `call` instead of an
`invoke`.

This resolves an issue when calling LLVM intrinsics which cannot unwind
LLVM will generate an error if you attempt to invoke them so we need to
ignore cleanup blocks in codegen and generate a call instead.

Fixes rust-lang#69911

r? @eddyb
cc @rust-lang/wg-ffi-unwind
@bors bors closed this as completed in 43cf9d7 Apr 18, 2020
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-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants