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

likely/unlikely intrinsics fail to propagate through inline functions #45440

Open
gnzlbg opened this issue Oct 22, 2017 · 2 comments
Open

likely/unlikely intrinsics fail to propagate through inline functions #45440

gnzlbg opened this issue Oct 22, 2017 · 2 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Oct 22, 2017

See it live:

#![feature(core_intrinsics)]
extern crate core;
use core::intrinsics::unlikely;

#[inline(always)]
fn test(x: bool) -> bool {
    unsafe {
        unlikely(x)
    }
}

pub fn foo(x: u32) -> u32 {
  if test(x == 0) { 1 } else { x * 2 } 
}

pub fn foo2(x: u32) -> u32 {
  unsafe {
  let c = unlikely(x == 0);
  if c { 1 } else { x * 2 } 
  }
}

pub fn bar(x: u32) -> u32 {
  if x == 0 { 1 } else { x * 2 } 
}

foo2 generates:

        push    rbp
        mov     rbp, rsp
        test    edi, edi
        je      .LBB1_1
        add     edi, edi
.LBB1_3:
        mov     eax, edi
        pop     rbp
        ret
.LBB1_1:
        mov     edi, 1
        jmp     .LBB1_3

but foo generates the same assembly as bar instead of the same assembly as foo2:

        push    rbp
        mov     rbp, rsp
        lea     ecx, [rdi + rdi]
        test    edi, edi
        mov     eax, 1
        cmovne  eax, ecx
        pop     rbp
        ret
@oyvindln
Copy link
Contributor

Turning on MIR inlining using -Z mir-opt-level=3 makes foo and foo2 identical. I don't know what other effects that may have though (and at the moment the MIR opt-levels that enable MIR inlining also enables MIR copy-propagation which can be very slow).

@pietroalbini pietroalbini added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. labels Jan 23, 2018
@alecmocatta
Copy link
Contributor

In case this is useful, I had a quick play around with --emit=llvm-ir: in debug mode, the expect intrinsic appears regardless of inlining. In release mode, without inlining it's converted to branch_weights, with inlining it's absent.

@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. 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

5 participants