Skip to content

RFC: Changes to target_feature attribute #3820

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

veluca93
Copy link

@veluca93 veluca93 commented May 25, 2025

tldr: make it easier/safer to use target_feature in traits.

Also see prior discussion in rust-lang/rust#139368.

Rendered

@veluca93
Copy link
Author

@hanna-kruppe @traviscross @RalfJung

Correct RFC link here :-)

@traviscross traviscross changed the title RFC: changes to target_feature attribute RFC: Changes to target_feature attribute May 25, 2025
@traviscross traviscross added the T-lang Relevant to the language team, which will review and decide on the RFC. label May 25, 2025

This `unsafe` attribute can be applied to free functions, trait method implementations or trait method definitions.

It comes with the following soundness requirement: the a function the signature of the function the attribute is applied to must _only be callable if the force-enabled features are guaranteed to be present_ (this can be done, for example, if the function takes arguments that carry appropriate safety invariants).
Copy link

@hanna-kruppe hanna-kruppe May 25, 2025

Choose a reason for hiding this comment

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

If the language had something like #3525 to make the "function takes argument that ensures the target feature is available" reasoning transparent to the compiler, would we still want this form of the attribute? Would the attribute still exist but no longer be unsafe? Or would we keep it unsafe but deprecate it in favor of the fully safe way with parameter types?

Copy link
Author

Choose a reason for hiding this comment

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

This likely depends on the shape #3525 would take (if any -- note that discussion on that RFC has been effectively stalled for quite some time).

The fundamental issue at hand here is that it's hard to define "function takes argument that ensures" at a language level in a way that is both ergonomic and doesn't run into recursive reference validity issues.

IMO, the attribute is likely to still be useful to bridge cases that are unlikely to be solved at language level, such as fn f(_: &Avx2).

There are also some other practical issues for #3525 (involving generics and the MIR inliner).

My understanding is that #3525 as-is is somewhat unlikely to actually be accepted, and the most likely way to achieve SIMD multiversioning functionality will be through some version of an effect system; however, those discussions are at an early enough stage that it would be hard for me to claim with any confidence whether an attribute like unsafe(target_feature) would still have an use then. If not, it can always be deprecated :-)

Choose a reason for hiding this comment

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

I get that the details of the compiler establishing safety of extra target features via arguments' types are very tricky. But it would be extremely unfortunate to first drag the ecosystem through the churn of a FCW, and then one edition later deprecate the style that the FCW promoted in favor of something new that solves the same use case better. (Of course, it would also be unfortunate to block any improvement here on some speculative far-future language evolution.)


This `unsafe` attribute can be applied to free functions, trait method implementations or trait method definitions.

It comes with the following soundness requirement: the a function the signature of the function the attribute is applied to must _only be callable if the force-enabled features are guaranteed to be present_ (this can be done, for example, if the function takes arguments that carry appropriate safety invariants).
Copy link
Member

@RalfJung RalfJung May 26, 2025

Choose a reason for hiding this comment

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

How does this account for what aho-corasick does? It seems to overfit specifically to TC's example, but while the example demonstrates the issue from rust-lang/rust#139368, it does not seem to capture every way in which existing code could soundly use target_feature in traits.

Someone needs to dig into aho-corasick and figure out the exact flow of proof obligations there. And, ideally, go hunt for some other examples across the ecosystem.

Copy link
Author

@veluca93 veluca93 May 26, 2025

Choose a reason for hiding this comment

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

From what I can see, the pattern in aho-corasick matches what would be possible with #[unsafe(target_feature(force=...))]. In particular, SearcherT is implemented for 4 (generic) structs named (Slim|Fat)(Avx2|Ssse3), which check for presence of the relevant CPU features upon construction and then declare in the trait method implementation...

impl SearcherT for FatAVX2<$len> {
    #[target_feature(enable = "avx2")]
    #[inline]
    unsafe fn find(
        &self,
        start: *const u8,
        end: *const u8,
    ) -> Option<Match> {
        // SAFETY: All obligations except for `target_feature` are
        // passed to the caller. Our use of `target_feature` is
        // safe because construction of this type requires that the
        // requisite target features are available.
        self.fat256.find(start, end)
    }
}

(admittedly the location of the safety comment wrt features is not quite correct, but alas)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants