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

Fix the clang targets for RISC-V Rust targets #993

Merged
merged 1 commit into from
Mar 6, 2024

Conversation

djkoloski
Copy link

Rust supports a variety of RISC-V targets which may include optional features in the first segment of the target triple. When invoking clang for these targets, we need to remove these features since clang doesn't use them.

Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but don't you also need to pass some flags to Clang to ensure it enables the correct features for the selected target?

@djkoloski
Copy link
Author

I'll take a second look tomorrow. I got it compiling but didn't test any feature functionality.

@djkoloski
Copy link
Author

It looks like all of the extensions are getting passed through -march by slicing them off of the target name, with the exceptions of 32-bit linux targets and 64-bit linux, freebsd, and netbsd targets. Those override -march=rv32/64gc exactly, as per this PR. In the default case though, these extensions are getting passed through generically. Here's the snippet in question:

if target.starts_with("riscv32") || target.starts_with("riscv64") {
    // get the 32i/32imac/32imc/64gc/64imac/... part
    let mut parts = target.split('-');
    if let Some(arch) = parts.next() {
        let arch = &arch[5..];
        if arch.starts_with("64") {
            if target.contains("linux")
                | target.contains("freebsd")
                | target.contains("netbsd")
                | target.contains("linux")
            {
                cmd.args.push(("-march=rv64gc").into());
                cmd.args.push("-mabi=lp64d".into());
            } else {
                cmd.args.push(("-march=rv".to_owned() + arch).into());
                cmd.args.push("-mabi=lp64".into());
            }
        } else if arch.starts_with("32") {
            if target.contains("linux") {
                cmd.args.push(("-march=rv32gc").into());
                cmd.args.push("-mabi=ilp32d".into());
            } else {
                cmd.args.push(("-march=rv".to_owned() + arch).into());
                cmd.args.push("-mabi=ilp32".into());
            }
        } else {
            cmd.args.push("-mcmodel=medany".into());
        }
    }
}

So this PR should be ready to merge. I was testing with riscv32imc-unknown-none-elf so I should be hitting those branches.

@Amanieu Amanieu merged commit e0ac1c3 into rust-lang:main Mar 6, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants