forge
Explore
Log in

Don't you dare go Hollow

Put the AVX2 feature on the Lane methods themselves

Browse files at this commit

b848139b194dfda57e7cc4dca3554ecd9f68fcb7Ahmet Kaan GümüşParent: 1b7f343
  • perf
    • put the AVX2 feature on the Lane methods themselves
Show full message
perf(av1): put the AVX2 feature on the Lane methods themselves

The column pass measured -3.7% instructions when the network was
`#[inline(always)]`d into the `target_feature` wrapper -- and then measured
FLAT again after an unrelated refactor moved the size dispatch into its own
`target_feature` function: the generic network stopped landing inside an AVX2
context, and with it every `_mm256_*` went back to being an out-of-line call
(9.5% + 3.7% of the profile, 65 `vpmuldq` in the binary against 339 now).

Relying on inlining to carry the feature is what was fragile. `Lane`'s two
methods are `unsafe fn` now -- the only form rustc lets an impl carry
`#[target_feature]` -- so the AVX2 impl compiles its intrinsics inline no
matter how its callers are arranged. The scalar impls require nothing of
their callers, and `M256` values still exist only inside the feature-detected
`simd::column_pass_dct`.

seg4k at one thread: 523.15 -> 502.36 G instructions:u (-3.97%), stable across
runs. All 12 gate hashes unchanged.
crates/ec-av1/src/transform.rsmodified+58 -40
@@ -111,11 +111,26 @@ fn clamp_range(x: i32, r: usize) -> i32 {
111 /// output share the whole network). The two impls are elementwise-identical111 /// output share the whole network). The two impls are elementwise-identical
112 /// integer arithmetic, so the eight-wide pass is byte-exact with the scalar112 /// integer arithmetic, so the eight-wide pass is byte-exact with the scalar
113 /// one by construction -- the network itself is written once, above.113 /// one by construction -- the network itself is written once, above.
114+// `unsafe fn` only so the AVX2 impl can carry `#[target_feature]` (rustc
115+// rejects it on a SAFE trait method); the two scalar impls below require
116+// nothing of their caller.
117+#[allow(unsafe_code)]
114 trait Lane: Copy {118 trait Lane: Copy {
115 /// One butterfly rotation's two outputs, at pre-multiplied `cos`/`sin`.119 /// One butterfly rotation's two outputs, at pre-multiplied `cos`/`sin`.
116- fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self);120+ ///
121+ /// # Safety
122+ /// An impl may require CPU features: `M256`'s is `#[target_feature]` AVX2,
123+ /// which is the only way its intrinsics compile INLINE -- as plain safe
124+ /// methods every `_mm256_*` stayed an out-of-line call (13% of the 4K
125+ /// segment's profile). `M256` values exist only inside
126+ /// [`simd::column_pass_dct`], which its caller has feature-detected; the
127+ /// scalar impls require nothing.
128+ unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self);
117 /// One Hadamard rotation's two outputs, clamped to `r` bits.129 /// One Hadamard rotation's two outputs, clamped to `r` bits.
118- fn had(a: Self, b: Self, r: usize) -> (Self, Self);130+ ///
131+ /// # Safety
132+ /// As [`Lane::btf`].
133+ unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self);
119 }134 }
120 135
121 /// `Round2(x, COS_BITS)` on a butterfly's 64-bit product sum.136 /// `Round2(x, COS_BITS)` on a butterfly's 64-bit product sum.
@@ -123,21 +138,23 @@ fn round_cos(x: i64) -> i32 {
123 ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32138 ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32
124 }139 }
125 140
141+#[allow(unsafe_code)]
126 impl Lane for i32 {142 impl Lane for i32 {
127- fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) {143+ unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) {
128 let (ta, tb) = (i64::from(a), i64::from(b));144 let (ta, tb) = (i64::from(a), i64::from(b));
129 (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c))145 (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c))
130 }146 }
131 147
132- fn had(a: Self, b: Self, r: usize) -> (Self, Self) {148+ unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) {
133 (clamp_range(a.wrapping_add(b), r), clamp_range(a.wrapping_sub(b), r))149 (clamp_range(a.wrapping_add(b), r), clamp_range(a.wrapping_sub(b), r))
134 }150 }
135 }151 }
136 152
137 /// Eight columns at once. Every operation is the [`i32`] impl's, lane by lane,153 /// Eight columns at once. Every operation is the [`i32`] impl's, lane by lane,
138 /// which is what the optimiser turns into one vector op per line.154 /// which is what the optimiser turns into one vector op per line.
155+#[allow(unsafe_code)]
139 impl Lane for [i32; 8] {156 impl Lane for [i32; 8] {
140- fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) {157+ unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) {
141 let mut x = [0i32; 8];158 let mut x = [0i32; 8];
142 let mut y = [0i32; 8];159 let mut y = [0i32; 8];
143 for l in 0..8 {160 for l in 0..8 {
@@ -148,7 +165,7 @@ impl Lane for [i32; 8] {
148 (x, y)165 (x, y)
149 }166 }
150 167
151- fn had(a: Self, b: Self, r: usize) -> (Self, Self) {168+ unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) {
152 let mut x = [0i32; 8];169 let mut x = [0i32; 8];
153 let mut y = [0i32; 8];170 let mut y = [0i32; 8];
154 for l in 0..8 {171 for l in 0..8 {
@@ -182,42 +199,38 @@ mod simd {
182 /// bits (`|a| < 2^31`, `|c| <= 2^12`), and only the low 32 bits of the199 /// bits (`|a| < 2^31`, `|c| <= 2^12`), and only the low 32 bits of the
183 /// shifted sum are kept -- which is what the scalar `as i32` keeps, so200 /// shifted sum are kept -- which is what the scalar `as i32` keeps, so
184 /// the logical shift below stands in for the arithmetic one exactly.201 /// the logical shift below stands in for the arithmetic one exactly.
185- #[inline(always)]202+ #[target_feature(enable = "avx2")]
186- fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) {203+ unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) {
187- unsafe {204+ let cv = _mm256_set1_epi32(c as i32);
188- let cv = _mm256_set1_epi32(c as i32);205+ let sv = _mm256_set1_epi32(s as i32);
189- let sv = _mm256_set1_epi32(s as i32);206+ let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1));
190- let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1));207+ let (ae, be) = (a.0, b.0);
191- let (ae, be) = (a.0, b.0);208+ let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1));
192- let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1));209+ let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| {
193- let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| {210+ let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v));
194- let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v));211+ let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u));
195- let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u));
196- (
197- _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(p, rnd)),
198- _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(q, rnd)),
199- )
200- };
201- let (xe, ye) = half(ae, be, cv, sv);
202- let (xo, yo) = half(ao, bo, cv, sv);
203 (212 (
204- M256(_mm256_blend_epi32::<0xAA>(xe, _mm256_slli_epi64::<32>(xo))),213+ _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(p, rnd)),
205- M256(_mm256_blend_epi32::<0xAA>(ye, _mm256_slli_epi64::<32>(yo))),214+ _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(q, rnd)),
206 )215 )
207- }216+ };
217+ let (xe, ye) = half(ae, be, cv, sv);
218+ let (xo, yo) = half(ao, bo, cv, sv);
219+ (
220+ M256(_mm256_blend_epi32::<0xAA>(xe, _mm256_slli_epi64::<32>(xo))),
221+ M256(_mm256_blend_epi32::<0xAA>(ye, _mm256_slli_epi64::<32>(yo))),
222+ )
208 }223 }
209 224
210- #[inline(always)]225+ #[target_feature(enable = "avx2")]
211- fn had(a: Self, b: Self, r: usize) -> (Self, Self) {226+ unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) {
212- unsafe {227+ let hi = _mm256_set1_epi32(((1i64 << (r - 1)) - 1) as i32);
213- let hi = _mm256_set1_epi32(((1i64 << (r - 1)) - 1) as i32);228+ let lo = _mm256_set1_epi32((-(1i64 << (r - 1))) as i32);
214- let lo = _mm256_set1_epi32((-(1i64 << (r - 1))) as i32);229+ let clamp = |v| _mm256_min_epi32(_mm256_max_epi32(v, lo), hi);
215- let clamp = |v| _mm256_min_epi32(_mm256_max_epi32(v, lo), hi);230+ (
216- (231+ M256(clamp(_mm256_add_epi32(a.0, b.0))),
217- M256(clamp(_mm256_add_epi32(a.0, b.0))),232+ M256(clamp(_mm256_sub_epi32(a.0, b.0))),
218- M256(clamp(_mm256_sub_epi32(a.0, b.0))),233+ )
219- )
220- }
221 }234 }
222 }235 }
223 236
@@ -298,7 +311,10 @@ mod simd {
298 #[inline(always)]311 #[inline(always)]
299 fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) {312 fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) {
300 let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle)));313 let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle)));
301- let (x, y) = T::btf(t[a], t[b], c, s);314+ // SAFETY: [`Lane::btf`] -- an `M256` reaches here only from the
315+ // feature-detected [`simd::column_pass_dct`].
316+ #[allow(unsafe_code)]
317+ let (x, y) = unsafe { T::btf(t[a], t[b], c, s) };
302 t[a] = x;318 t[a] = x;
303 t[b] = y;319 t[b] = y;
304 if flip {320 if flip {
@@ -311,7 +327,9 @@ fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: boo
311 #[inline(always)]327 #[inline(always)]
312 fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) {328 fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) {
313 let (a, b) = if flip { (b, a) } else { (a, b) };329 let (a, b) = if flip { (b, a) } else { (a, b) };
314- let (x, y) = T::had(t[a], t[b], r);330+ // SAFETY: as [`butterfly`]'s.
331+ #[allow(unsafe_code)]
332+ let (x, y) = unsafe { T::had(t[a], t[b], r) };
315 t[a] = x;333 t[a] = x;
316 t[b] = y;334 t[b] = y;
317 }335 }