linebender / linebender/fearless_simd

`#[simd]` should work on functions without an explicit SIMD token parameter

Open
#379 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
457
Forks
30
Avg merge
1d 10h
Merged PRs (30d)
25

Description

Right now (#347), `#[simd]` requires the first non-receiver argument to a SIMD token. However, it's not uncommon for SIMD functions to not have an explicit SIMD token parameter, because any SIMD type (e.g. `f32xN`, etc) already carries a SIMD token with them. For example, this is what the `sRGB` example does:

https://github.com/linebender/fearless_simd/blob/bd9f7c7be6b4a2c009558fffd5db67c54ff744f5/fearless_simd/examples/srgb.rs#L52-L67

Such functions should be supported to make the `#[simd]` macro easier to use.

## Suggested solution

Instead of requiring that the first parameter ***is*** a SIMD token, require that the first parameter ***carries*** a SIMD token.

This could be done by adding a new trait like so:

```rs
pub trait ExtractToken { // bikeshed names
type S: Simd;
fn simd(&self) -> Self::S;
}

// all SIMD token types return self
impl ExtractToken for Avx2 {
type S = Self;
fn simd(&self) -> Self::S {
*self
}
}

// all data type return their SIMD token
impl ExtractToken for f32x8 {
type S = S;
fn simd(&self) -> Self::S {
self.simd
}
}
```

and change the code gen of the `#[simd]` macro to use this trait to get a SIMD token from the first parameter.

By adding [`#[diagnostic::on_unimplemented]`](https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-diagnosticon_unimplemented-attribute) on the trait, we can also get nice compiler errors when users use `#[simd]` incorrectly. E.g.

```rs
#[simd]
fn foo(a: i32);
```
```
error[E0277]: `i32` does not carry a SIMD token
--> src\encode\bc4.rs:961:24
|
961 | ExtractToken::simd(&a)
| ------------------ ^^ the trait `fearless_simd::ExtractToken` is not implemented for `i32`
| |
| required by a bound introduced by this call
|
= note: If you are using the #[simd] macro, the first parameter must carry a SIMD token. See the docs of #[simd] for more information.
help: the following other types implement trait `fearless_simd::ExtractToken`
--> ...
```

---

Note: I think the `ExtractToken` trait should be part of the public API of the main crate. I want to implement it on the newtype wrappers around `fearless_simd` types I use.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the sRGB example at examples/srgb.rs lines 52–67 and compare the current #[simd] behavior described in issue #347. Trace the #[simd] macro implementation and determine how its first non-receiver argument is handled. Done means functions whose first parameter carries a SIMD token are accepted, while invalid parameters produce the intended diagnostic and the extraction trait is publicly usable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.