Skip to content

LLVM Error with asm! #83495

Closed
Closed
@Soveu

Description

@Soveu

I tried this code:

#![feature(asm)]

#[inline(always)]
fn check_bsf() -> (u64, u8) {
    let flags: u8;
    let result: u64;

    unsafe {
        asm!(
            "bsf rbx, rbx",
            "lahf",
            inout("rbx") 0u64 => result,
            out("ah") flags,
            options(nostack, nomem),
        );
    }

    return (result, flags);
}

fn main() {
    let flags1: u8;
    unsafe {
        asm!("lahf", out("ah") flags1, options(nostack, nomem));
    }

    let (result, flags2) = check_bsf();
    let flagsdiff = flags2 ^ flags1;
    let zero_flag = 1 << 6;

    println!("result={}", result);
    assert_eq!(flagsdiff, zero_flag);
}

I expected to see this happen:
The code compiles, like with opt-level 0 or 1

Instead, this happened:
When compiling with opt-level 2 or 3, just this message appears

LLVM ERROR: Cannot encode high byte register in REX-prefixed instruction

Meta

rustc --version --verbose:

rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)
binary: rustc
commit-hash: 07e0e2ec268c140e607e1ac7f49f145612d0f597
commit-date: 2021-03-24
host: x86_64-unknown-linux-gnu
release: 1.53.0-nightly
LLVM version: 12.0.0

No backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.F-asm`#![feature(asm)]` (not `llvm_asm`)O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions