Skip to content

asm! can't let you use lateout("rbx") (LLVM needs it) #90083

Closed
@noncombatant

Description

@noncombatant

This is about an unstable feature, so maybe you don't consider that a regression, exactly. This seemed like the best bug template to me; I hope it's OK.

I'm reading the asm! document at https://doc.rust-lang.org/beta/unstable-book/library-features/asm.html. The cpuid example has this code:

#![allow(unused)]
#![feature(asm)]
fn main() {
let ebx: u32;
let ecx: u32;

unsafe {
    asm!(
        "cpuid",
        // EAX 4 selects the "Deterministic Cache Parameters" CPUID leaf
        inout("eax") 4 => _,
        // ECX 0 selects the L0 cache information.
        inout("ecx") 0 => ecx,
        lateout("ebx") ebx,
        lateout("edx") _,
    );
}

println!(
    "L1 Cache: {}",
    ((ebx >> 22) + 1) * (((ebx >> 12) & 0x3ff) + 1) * ((ebx & 0xfff) + 1) * (ecx + 1)
);
}

But when you press the Play button to try it out, you get:

   Compiling playground v0.0.1 (/playground)
error: invalid register `ebx`: rbx is used internally by LLVM and cannot be used as an operand for inline asm
  --> src/main.rs:15:9
   |
15 |         lateout("ebx") ebx,
   |         ^^^^^^^^^^^^^^^^^^

error: could not compile `playground` due to previous error

I hacked it up locally (building with Rust Nightly), and this seems to work:

    unsafe {
        asm!(
            "cpuid",
            // EAX 4 selects the "Deterministic Cache Parameters" CPUID leaf
            inout("eax") 4 => _,
            // ECX 0 selects the L0 cache information.
            inout("ecx") 0 => ecx,
            //lateout("ebx") ebx,
            lateout("edx") _,
        );
        asm!(
                "mov {:e}, ebx", out(reg) ebx
        );
    }

I.e. I comment out the lateout("ebx") line, and add a second macro invocation to get the value of ebx.

But I assume there is a better way. :) The docs should be updated to show the better way. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.F-asm`#![feature(asm)]` (not `llvm_asm`)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