Rustc ignoring target-feature when cross-compiling

It seems that rustc is ignoring my target-feature request.

I'm developing an application targeting aarch64-unknown-linux-musl (cross-compiling from a x86_64 host), and I'm trying to optimize my build for a particular CPU (ARM Cortex A53). The target-cpu option to rustc seemed like a good start, but I thought of trying with a single target-feature first.

Here's the code:

/* main.rs */
fn main() {
    if cfg!(target_feature = "a53") {
        println!("a53! :-)");
    } else {
        println!("nope... :-(");
    }
}

which I compile with

rustc --target aarch64-unknown-linux-musl -C linker=aarch64-linux-gnu-gcc -C target-feature=+a53 main.rs -o main

Since I explicitly enable the a53 feature, I'd expect the code to print the first text, but I instead get out a boring nope... :-(.

I have also made a similar test on a bare-metal (no_std) application with target armv7r-none-eabi (Cortex R5), and there also a target-feature=+r5 option seems to be ignored by rustc. If instead I build for the host machine (target x86_64-unknown-linux-gnu), then features seem to be recognized (e.g. target-feature=+xsave).

What am I doing wrong?

The a53 target feature is not in the list of allowed target features:

This means that while LLVM may accept it and codegen accordingly, rustc doesn't know about it and thus doesn't enable the cfg for the target feature. Rustc should probably give an error in this case.

2 Likes

Thank you bjorn3 for the reply!

I've swapped the a53 feature for the neon feature that does appear in the list you posted, but still got the same problem: the feature is ignored when compiling.

New code:

fn main() {
    if cfg!(target_feature = "neon") {
        println!("neon! :-)");
    } else {
        println!("nope... :-(");
    }
}

compiled with:

rustc --target aarch64-unknown-linux-musl -C linker=aarch64-linux-gnu-gcc -C target-feature=+neon main.rs -o main

prints:

# ./main 
nope... :-(

neon is still unstable (Some(sym::aarch64_target_feature) in the list) so you need to use nightly and use #![feature(aarch64_target_feature)] to use it. All stable target features are marked as having "None" feature gate in the list.

2 Likes

Thanks a lot, now I understand!

Switching to the nightly toolchain and adding the #![feature(aarch64_target_feature)] does the trick. It was not clear to me that I had to be on nightly.

You may want to open an issue for getting a warning or error when on stable.