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

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

Closed
kaoet opened this issue Jun 10, 2024 · 2 comments
Closed

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

kaoet opened this issue Jun 10, 2024 · 2 comments
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. O-riscv Target: RISC-V architecture T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kaoet
Copy link

kaoet commented Jun 10, 2024

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
@kaoet kaoet added the C-bug Category: This is a bug. label Jun 10, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 10, 2024
@jieyouxu jieyouxu added A-inline-assembly Area: Inline assembly (`asm!(…)`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. O-riscv Target: RISC-V architecture and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 10, 2024
@workingjubilee
Copy link
Member

workingjubilee commented Jun 23, 2024

cc embedded RISCV team: @almindor @dkhayes117 @romancardenas @MabezDev @jessebraham

Seems slightly relevant to you.

@tgross35
Copy link
Contributor

#128212 seems to be a duplicate but has some more details, so I will close this one. (appears to be an LLVM issue).

@tgross35 tgross35 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. O-riscv Target: RISC-V architecture 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