Skip to content

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.

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.

2 participants