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

Compilation error about RISC-V H extension #111637

Open
KuangjuX opened this issue May 16, 2023 · 13 comments
Open

Compilation error about RISC-V H extension #111637

KuangjuX opened this issue May 16, 2023 · 13 comments
Labels
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

@KuangjuX
Copy link

KuangjuX commented May 16, 2023

I'm trying to use Rust to compile the riscv H extension instructions, but an error occurred during compilation, The Rust target I am using is riscv64gc-unknown-none-elf. When I use rustc --print target-features --target riscv64gc-unknown-none-elf command to check, I found information about the H extension:

    h                             - 'H' (Hypervisor).

Subsequently, I added the following instruction to the compile command:

LOG=debug cargo rustc --release --features "libax/platform-qemu-virt-riscv libax/log-level-debug libax/alloc libax/hv libax/paging libax/irq" --manifest-path=hv/Cargo.toml -- -Ctarget-feature=+h -Clink-arg=-Thvruntime/src/linker.ld -Cforce-frame-pointers=yes 

However, the compilation error remains unresolved:

error: instruction requires the following: 'H' (Hypervisor)
    hsv.b t3, (a0)

What is causing this issue? And my target is specified in .cargo/config.toml and my rustc version is rustc 1.71.0-nightly (2f2c438dc 2023-05-08).

Meta

rustc --version --verbose
rustc 1.71.0-nightly (2f2c438dc 2023-05-08)
binary: rustc
commit-hash: 2f2c438dce75d8cc532c3baa849eeddc0901802c
commit-date: 2023-05-08
host: aarch64-apple-darwin
release: 1.71.0-nightly
LLVM version: 16.0.2
@andropixels
Copy link

One possible solution is to ensure that your RISC-V target is configured properly to support the "H" extension instructions. You can verify this by checking the configuration files or documentation related to the target you are using. Make sure that the target specification you are using supports the "H" extension.

if you have confirmed that, It's worth considering whether there might be compatibility issues or bugs with that specific nightly version. You could try updating to a newer version of the Rust compiler or using a stable release to see if the issue persists

@KuangjuX
Copy link
Author

KuangjuX commented May 16, 2023

I tried rolling back the Rust version to 1.68.0-nightly and successfully compiled. I noticed in the change log of Rust 1.70.0 beta that LLVM was updated to version 16, so I suspect it's an issue with the LLVM version.

@KuangjuX
Copy link
Author

KuangjuX commented May 17, 2023

@rustbot label C-bug T-compiler

@rustbot rustbot added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 17, 2023
@Jules-Bertholet
Copy link
Contributor

@rustbot label O-riscv

@rustbot rustbot added the O-riscv Target: RISC-V architecture label May 19, 2023
@KuangjuX
Copy link
Author

This issue seems to be resolved in the newest nightly vetsion.

@cylindrical2002
Copy link

I found this error was still there, but the project can still be compiled.

error: instruction requires the following: 'H' (Hypervisor)
    hsv.b t3, (a0)

How to remove this error?

@KuangjuX KuangjuX reopened this May 26, 2023
@istankovic
Copy link
Contributor

@cylindrical2002 could you please provide a minimal example that reproduces this?
Could you also please check with the latest nightly?

@alexfanqi
Copy link

alexfanqi commented Dec 26, 2023

Seems only happen with global_asm! and opt-level>0, I managed to find a minimal example that fails with latest stable rustc version as well

#![no_std]
#![no_main]

use core::arch::global_asm;

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

global_asm!(
    r#"
    .global asm_func
    asm_func:
        hlv.h   t0, (t1)
    "#
);

extern {
    fn asm_func();
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    unsafe {
        asm_func();
    }
    loop {}
}

compiling with rustc gives

>  rustc -C opt-level=1  -Ctarget-feature=+h --target=riscv64gc-unknown-none-elf main.rs 
warning: unknown feature specified for `-Ctarget-feature`: `h`
  |
  = note: it is still passed through to the codegen backend
  = help: consider filing a feature request

error: instruction requires the following: 'H' (Hypervisor)
        hlv.h   t0, (t1)
        ^
error: instruction requires the following: 'H' (Hypervisor)
        hlv.h   t0, (t1)
        ^
warning: 1 warning emitted

> rustc --version -v
rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4

but if opt-level is set to 0, the error goes away

@alexfanqi
Copy link

alexfanqi commented Dec 27, 2023

Tried to dig into this. The error comes from LTO. rustc -C opt-level=3 -C lto=off -Ctarget-feature=+h --target=riscv64gc-unknown-none-elf main.rs gives no error.

LLVM 16 makes 'h' an extension name instead of privilege level that is always enabled, which uncovers the error from previous warning. llvm/llvm-project@f4c887c . If you try other extensions with 1.68 and llvm 15, it will be an error.

The root of the error is LTO uses an empty MCSubtargetInfo to extract global symbols because it doesn't know enabled features for global module asm. llvm/llvm-project#67698, https://discourse.llvm.org/t/rfc-target-cpu-and-features-for-module-level-inline-assembly

Perhaps, instead of passing target-feature, you can work around this by using special riscv asm directives

.option push
.option arch, +h
// add hypervisor instructions
.option pop

@workingjubilee
Copy link
Member

cc @kito-cheng @michaelmaitland @robin-randhawa-sifive Is this issue still an active concern?

@michaelmaitland
Copy link

I am only seeing a warning, not an error: https://godbolt.org/z/77T9598Wq.

Is there a reason this is not part of target_features.rs::RISCV_ALLOWED_FEATURES?

@workingjubilee
Copy link
Member

The first reason is usually "no one asked", and the second reason is usually questions about if it winds up somehow affecting the consequent ABI in LLVM (which many features do, though I somewhat expect this one doesn't).

@alexfanqi
Copy link

I am only seeing a warning, not an error: https://godbolt.org/z/77T9598Wq.

If you check the compile to binary object or link to binary under the output... dropdown menu in asm viewer, the error should show up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

9 participants