Skip to content

Miscompile for riscv64gc-unknown-none-elf using inout() in asm!() #126221

Closed as not planned
@kaoet

Description

@kaoet

I tried this code with flags --target riscv64gc-unknown-none-elf -C opt-level=1. And here is the compiler explorer link.

#![no_std]

use core::arch::asm;

#[no_mangle]
pub fn f1() {
    unsafe {
        asm!(
            "nop",
            inout("a1") "Hello".as_ptr() as usize => _,
        );
    }
}

#[no_mangle]
pub fn f2() {
    unsafe {
        asm!(
            "nop",
            in("a1") "Hello".as_ptr() as usize,
            lateout("a1") _,
        );
    }
}

The above code compiles to the following assembly:

f1:
        auipc   a1, %pcrel_hi(.L__unnamed_1)
        nop
        ret

f2:
.Lpcrel_hi1:
        auipc   a0, %pcrel_hi(.L__unnamed_1)
        addi    a1, a0, %pcrel_lo(.Lpcrel_hi1)
        nop
        ret

.L__unnamed_1:
        .ascii  "Hello"

I expect f1() and f2() compiles to the exact same assembly. However, in the assembly of f1(), the addi ..., %pcrel_lo(...) instruction is missing after the auipc instruction. That would make the value of a1 register wrong for my nop instruction.

Meta

rustc --version --verbose:

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.O-riscvTarget: RISC-V architectureT-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