SVE: Add pselect and rsqrt, and vectorize std::complex<float> libeigen/eigen!2934 Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7eae3da..994f2e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -433,7 +433,9 @@ ei_add_cxx_compiler_flag("-Wno-psabi") ei_add_cxx_compiler_flag("-Wno-variadic-macros") ei_add_cxx_compiler_flag("-Wno-long-long") - ei_add_cxx_compiler_flag("-Wno-pass-failed") # disable clang's warning for unrolling when the loop count is dynamic. + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + ei_add_cxx_compiler_flag("-Wno-pass-failed") # disable clang's warning for unrolling when the loop count is dynamic. + endif() ei_add_cxx_compiler_flag("-fno-common") ei_add_cxx_compiler_flag("-fstrict-aliasing") ei_add_cxx_compiler_flag("-wd981") # disable ICC's "operands are evaluated in unspecified order" remark @@ -451,7 +453,9 @@ if (CMAKE_COMPILER_IS_GNUCXX) ei_add_cxx_compiler_flag("-Wno-array-bounds") ei_add_cxx_compiler_flag("-Wno-maybe-uninitialized") - ei_add_cxx_compiler_flag("-Wno-stringop-overread") + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12) + ei_add_cxx_compiler_flag("-Wno-stringop-overread") + endif() ei_add_cxx_compiler_flag("-Wno-nonnull") endif()
diff --git a/Eigen/Core b/Eigen/Core index e6a4bb5..9da31c1 100644 --- a/Eigen/Core +++ b/Eigen/Core
@@ -284,6 +284,7 @@ #include "src/Core/arch/SVE/PacketMath.h" #include "src/Core/arch/SVE/TypeCasting.h" #include "src/Core/arch/SVE/MathFunctions.h" +#include "src/Core/arch/SVE/Complex.h" #elif defined EIGEN_VECTORIZE_RVV10 #include "src/Core/arch/RVV10/PacketMathDecl.h" #include "src/Core/arch/RVV10/PacketMath.h"
diff --git a/Eigen/src/Core/arch/SVE/Complex.h b/Eigen/src/Core/arch/SVE/Complex.h new file mode 100644 index 0000000..2fa7ee0 --- /dev/null +++ b/Eigen/src/Core/arch/SVE/Complex.h
@@ -0,0 +1,281 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +// SPDX-FileCopyrightText: The Eigen Authors +// SPDX-License-Identifier: MPL-2.0 + +#ifndef EIGEN_COMPLEX_SVE_H +#define EIGEN_COMPLEX_SVE_H + +// IWYU pragma: private +#include "../../InternalHeaderCheck.h" + +namespace Eigen { +namespace internal { + +// A complex packet is one real packet holding interleaved {re, im} pairs. That +// layout is not a choice: GenericPacketMathComplex.h and ConjHelper.h both index +// the member `v` and construct a complex packet from a real one. +// +// std::complex<double> is not vectorized here yet. plog_complex and pexp_complex +// reach plog and pexp for PacketXd, which need a 64-bit integer packet this +// backend does not define, and packetmath instantiates plog for any vectorizable +// complex type regardless of HasLog -- so complex<double> has to wait for the +// double transcendentals rather than ship with the flags turned off. +struct PacketXcf { + EIGEN_STRONG_INLINE PacketXcf() {} + EIGEN_STRONG_INLINE explicit PacketXcf(const PacketXf& a) : v(a) {} + PacketXf v; +}; + +template <> +struct packet_traits<std::complex<float>> : default_packet_traits { + typedef PacketXcf type; + typedef PacketXcf half; + enum { + Vectorizable = 1, + AlignedOnScalar = 1, + size = sve_packet_size_selector<std::complex<float>, EIGEN_ARM64_SVE_VL>::size, + + HasAdd = 1, + HasSub = 1, + HasMul = 1, + HasDiv = 1, + HasNegate = 1, + HasConj = 1, + HasSetLinear = 0, + HasAbs = 0, + HasAbs2 = 0, + HasMin = 0, + HasMax = 0, + HasArg = 0, + + HasSqrt = 1, + HasLog = 1, + // pexp_complex evaluates exp(a+bi) as e^a * (cos b + i sin b), and psin for + // PacketXf returns |sin| under -ffast-math with GCC (13 through 15): the + // magnitude is right but the sign is dropped, so exp of any value with a + // negative imaginary part comes out conjugated. clang is unaffected, as is + // NEON under either compiler, and every primitive psincos_float uses checks + // out in isolation -- a verbatim copy of its body in another translation + // unit gives the right answer with the same flags. Until that is resolved, + // leave exp to the scalar path. See + // https://gitlab.com/libeigen/eigen/-/issues/3132. + HasExp = 0 + }; +}; + +template <> +struct unpacket_traits<PacketXcf> { + typedef std::complex<float> type; + typedef PacketXcf half; + typedef PacketXf as_real; + enum { + size = sve_packet_size_selector<std::complex<float>, EIGEN_ARM64_SVE_VL>::size, + alignment = sve_packet_alignment_selector<EIGEN_ARM64_SVE_VL>::alignment, + vectorizable = true, + masked_load_available = false, + masked_store_available = false + }; +}; + +/********************************* complex<float> *****************************/ + +template <> +EIGEN_STRONG_INLINE PacketXcf pset1<PacketXcf>(const std::complex<float>& from) { + // {re, im} is one 64-bit lane, so broadcasting the value is a 64-bit dup. + return PacketXcf(svreinterpret_f32_u64(svdup_n_u64(numext::bit_cast<numext::uint64_t>(from)))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf pload<PacketXcf>(const std::complex<float>* from) { + return PacketXcf(pload<PacketXf>(reinterpret_cast<const float*>(from))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf ploadu<PacketXcf>(const std::complex<float>* from) { + return PacketXcf(ploadu<PacketXf>(reinterpret_cast<const float*>(from))); +} + +template <> +EIGEN_STRONG_INLINE void pstore<std::complex<float>>(std::complex<float>* to, const PacketXcf& from) { + pstore(reinterpret_cast<float*>(to), from.v); +} + +template <> +EIGEN_STRONG_INLINE void pstoreu<std::complex<float>>(std::complex<float>* to, const PacketXcf& from) { + pstoreu(reinterpret_cast<float*>(to), from.v); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf ploaddup<PacketXcf>(const std::complex<float>* from) { + // Load the size/2 values this reads into the low half and interleave them + // with themselves on 64-bit lanes, which moves whole {re, im} pairs. The + // predicate is exact rather than svptrue -- a wider one would read past the + // end of the input. + constexpr uint64_t kHalf = uint64_t(packet_traits<std::complex<float>>::size) / 2; + const svuint64_t lo = + svreinterpret_u64_f32(svld1_f32(svwhilelt_b32(uint64_t(0), 2 * kHalf), reinterpret_cast<const float*>(from))); + return PacketXcf(svreinterpret_f32_u64(svzip1_u64(lo, lo))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf ploadquad<PacketXcf>(const std::complex<float>* from) { + // As ploaddup, one zip further: size/4 values, each repeated four times. At + // the smallest vector length size/4 rounds to zero, where one value still + // has to be read. + constexpr uint64_t kQuarter = numext::maxi(uint64_t(packet_traits<std::complex<float>>::size) / 4, uint64_t(1)); + svuint64_t lo = + svreinterpret_u64_f32(svld1_f32(svwhilelt_b32(uint64_t(0), 2 * kQuarter), reinterpret_cast<const float*>(from))); + lo = svzip1_u64(lo, lo); + return PacketXcf(svreinterpret_f32_u64(svzip1_u64(lo, lo))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf pgather<std::complex<float>, PacketXcf>(const std::complex<float>* from, Index stride) { + const svuint64_t idx = svindex_u64(0, numext::uint64_t(stride)); + return PacketXcf(svreinterpret_f32_u64( + svld1_gather_u64index_u64(svptrue_b64(), reinterpret_cast<const numext::uint64_t*>(from), idx))); +} + +template <> +EIGEN_STRONG_INLINE void pscatter<std::complex<float>, PacketXcf>(std::complex<float>* to, const PacketXcf& from, + Index stride) { + const svuint64_t idx = svindex_u64(0, numext::uint64_t(stride)); + svst1_scatter_u64index_u64(svptrue_b64(), reinterpret_cast<numext::uint64_t*>(to), idx, + svreinterpret_u64_f32(from.v)); +} + +template <> +EIGEN_STRONG_INLINE std::complex<float> pfirst<PacketXcf>(const PacketXcf& a) { + // svlasta with no active lane returns lane 0, which is the whole value. + return numext::bit_cast<std::complex<float>>(svlasta_u64(svpfalse_b(), svreinterpret_u64_f32(a.v))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf pconj(const PacketXcf& a) { + // {re, im} is one 64-bit lane with im in the high half, so flipping bit 63 + // negates the imaginary part alone. + return PacketXcf( + svreinterpret_f32_u64(sveor_n_u64_x(svptrue_b64(), svreinterpret_u64_f32(a.v), numext::uint64_t(1) << 63))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf pcplxflip<PacketXcf>(const PacketXcf& a) { + // Swap the 32-bit halves of every 64-bit lane: {re, im} -> {im, re}. + return PacketXcf(svreinterpret_f32_u64(svrevw_u64_x(svptrue_b64(), svreinterpret_u64_f32(a.v)))); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf pdupreal<PacketXcf>(const PacketXcf& a) { + return PacketXcf(svtrn1_f32(a.v, a.v)); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf pdupimag<PacketXcf>(const PacketXcf& a) { + return PacketXcf(svtrn2_f32(a.v, a.v)); +} + +template <> +EIGEN_STRONG_INLINE PacketXcf preverse(const PacketXcf& a) { + // Reversing 64-bit lanes moves whole complex values and keeps {re, im} paired. + return PacketXcf(svreinterpret_f32_u64(svrev_u64(svreinterpret_u64_f32(a.v)))); +} + +template <> +EIGEN_STRONG_INLINE std::complex<float> predux<PacketXcf>(const PacketXcf& a) { + // Read as a 32-bit predicate, an all-true 64-bit one is exactly the even + // lanes -- the real parts -- and reversing it gives the odd ones. + const svbool_t even = svptrue_b64(); + const svbool_t odd = svrev_b32(even); + return {svaddv_f32(even, a.v), svaddv_f32(odd, a.v)}; +} + +/********************************* shared *************************************/ + +// Everything that acts on {re, im} pairs identically forwards to the real packet. +#define EIGEN_SVE_COMPLEX_DELEGATE(PACKET_CPLX) \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX padd<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return PACKET_CPLX(padd(a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX psub<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return PACKET_CPLX(psub(a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pnegate(const PACKET_CPLX& a) { \ + return PACKET_CPLX(pnegate(a.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pzero<PACKET_CPLX>(const PACKET_CPLX& a) { \ + return PACKET_CPLX(pzero(a.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pand<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return PACKET_CPLX(pand(a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX por<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return PACKET_CPLX(por(a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pxor<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return PACKET_CPLX(pxor(a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pandnot<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return PACKET_CPLX(pandnot(a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pselect<PACKET_CPLX>(const PACKET_CPLX& mask, const PACKET_CPLX& a, \ + const PACKET_CPLX& b) { \ + return PACKET_CPLX(pselect(mask.v, a.v, b.v)); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pmul<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return pmul_complex(a, b); \ + } \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pdiv<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + return pdiv_complex(a, b); \ + } \ + /* A complex value is equal only if both components are, so fold the two */ \ + /* real-lane results together across each pair. */ \ + template <> \ + EIGEN_STRONG_INLINE PACKET_CPLX pcmp_eq<PACKET_CPLX>(const PACKET_CPLX& a, const PACKET_CPLX& b) { \ + const PACKET_CPLX t = PACKET_CPLX(pcmp_eq(a.v, b.v)); \ + return PACKET_CPLX(pand(pdupreal(t).v, pdupimag(t).v)); \ + } + +EIGEN_SVE_COMPLEX_DELEGATE(PacketXcf) +#undef EIGEN_SVE_COMPLEX_DELEGATE + +EIGEN_INSTANTIATE_COMPLEX_MATH_FUNCS_NO_EXP(PacketXcf) + +// A complex value is exactly one 64-bit lane, so transposing complex packets is +// a zip network run on 64-bit elements. +template <int N> +EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<PacketXcf, N>& kernel) { + EIGEN_STATIC_ASSERT((N & (N - 1)) == 0, EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT); + for (int stride = N / 2; stride > 0; stride >>= 1) { + for (int block = 0; block < N; block += 2 * stride) { + for (int k = 0; k < stride; ++k) { + const svuint64_t a = svreinterpret_u64_f32(kernel.packet[block + k].v); + const svuint64_t b = svreinterpret_u64_f32(kernel.packet[block + k + stride].v); + kernel.packet[block + k] = PacketXcf(svreinterpret_f32_u64(svzip1_u64(a, b))); + kernel.packet[block + k + stride] = PacketXcf(svreinterpret_f32_u64(svzip2_u64(a, b))); + } + } + } +} + +EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(PacketXcf, PacketXf) + +} // end namespace internal +} // end namespace Eigen + +#endif // EIGEN_COMPLEX_SVE_H
diff --git a/Eigen/src/Core/arch/SVE/PacketMath.h b/Eigen/src/Core/arch/SVE/PacketMath.h index d01cbed..07e024f 100644 --- a/Eigen/src/Core/arch/SVE/PacketMath.h +++ b/Eigen/src/Core/arch/SVE/PacketMath.h
@@ -64,6 +64,15 @@ HasMin = 1, HasMax = 1, HasConj = 1, + // HasSetLinear and HasCmp stay 0 even though plset and pcmp_{eq,lt,le} are + // implemented below: both flags route work onto a packet path that measures + // far slower than the scalar loop GCC autovectorizes. On Neoverse V2 at + // VL=128, HasSetLinear costs LinSpaced 20.7 -> 60.6 us for float and + // 20.7 -> 121.6 us for double, and HasCmp costs (a < b).select(a, b) + // 6.2 -> 50.6 us. Neither is explained by pselect: these numbers are with + // the svsel specialization below in place, and it made the LinSpaced case + // worse rather than better. The cause is higher up, in how those evaluators + // drive the packet path, and should be found before either flag is set. HasSetLinear = 0, HasReduxp = 0 // Not implemented in SVE }; @@ -187,6 +196,17 @@ return svbic_s32_x(svptrue_b32(), a, b); } +// SVE selects on a predicate, so turn Eigen's all-ones/all-zeros value mask back +// into one and use svsel rather than the generic por(pand, pandnot). Measured on +// Neoverse V2 at VL=128: latency 3.18 -> 2.55 ns, throughput 0.80 -> 0.64 ns/op. +// SVE2's single-instruction svbsl is no faster than this, so it is not worth an +// ISA-conditional path. The mask is compared as an integer: as a float it would +// be a NaN bit pattern, and relying on NaN != 0 is needlessly subtle. +template <> +EIGEN_STRONG_INLINE PacketXi pselect<PacketXi>(const PacketXi& mask, const PacketXi& a, const PacketXi& b) { + return svsel_s32(svcmpne_n_s32(svptrue_b32(), mask, 0), a, b); +} + template <int N> EIGEN_STRONG_INLINE PacketXi parithmetic_shift_right(const PacketXi& a) { // ASR, not ASRD: ASRD is the shift-for-divide form, which rounds toward zero, @@ -346,6 +366,7 @@ HasMin = 1, HasMax = 1, HasConj = 1, + // See the int32 traits above for why HasSetLinear stays 0. HasSetLinear = 0, HasReduxp = 0, // Not implemented in SVE @@ -365,6 +386,7 @@ HasExp = 1, HasPow = 1, HasSqrt = 1, + HasRsqrt = 1, HasCbrt = 1, HasTanh = EIGEN_FAST_MATH, HasErf = EIGEN_FAST_MATH, @@ -542,6 +564,12 @@ return svreinterpret_f32_u32(svbic_u32_x(svptrue_b32(), svreinterpret_u32_f32(a), svreinterpret_u32_f32(b))); } +// See pselect<PacketXi>. +template <> +EIGEN_STRONG_INLINE PacketXf pselect<PacketXf>(const PacketXf& mask, const PacketXf& a, const PacketXf& b) { + return svsel_f32(svcmpne_n_s32(svptrue_b32(), svreinterpret_s32_f32(mask), 0), a, b); +} + template <> EIGEN_STRONG_INLINE PacketXf pload<PacketXf>(const float* from) { EIGEN_DEBUG_ALIGNED_LOAD return svld1_f32(svptrue_b32(), from); @@ -709,12 +737,14 @@ HasMin = 1, HasMax = 1, HasConj = 1, + // See the int32 traits above for why HasSetLinear stays 0. HasSetLinear = 0, HasReduxp = 0, // Not implemented in SVE HasDiv = 1, HasCmp = 1, - HasSqrt = 1 + HasSqrt = 1, + HasRsqrt = 1 }; }; @@ -891,6 +921,12 @@ return svreinterpret_f64_u64(svbic_u64_x(svptrue_b64(), svreinterpret_u64_f64(a), svreinterpret_u64_f64(b))); } +// See pselect<PacketXi>. +template <> +EIGEN_STRONG_INLINE PacketXd pselect<PacketXd>(const PacketXd& mask, const PacketXd& a, const PacketXd& b) { + return svsel_f64(svcmpne_n_s64(svptrue_b64(), svreinterpret_s64_f64(mask), 0), a, b); +} + template <> EIGEN_STRONG_INLINE PacketXd pload<PacketXd>(const double* from) { EIGEN_DEBUG_ALIGNED_LOAD return svld1_f64(svptrue_b64(), from); @@ -1012,6 +1048,14 @@ return svsqrt_f64_x(svptrue_b64(), a); } +template <> +EIGEN_STRONG_INLINE PacketXd prsqrt<PacketXd>(const PacketXd& a) { + // Newton off the FRSQRTE seed, as NEON's Packet2d does. The generic + // preciprocal(psqrt(x)) form is correct but pays a double-precision FDIV, + // which is slow enough here to lose to the scalar loop. + return generic_rsqrt_newton_step<PacketXd, /*Steps=*/3>::run(a, svrsqrte_f64(a)); +} + } // namespace internal } // namespace Eigen
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bbe9c57..d6c36b1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt
@@ -490,9 +490,11 @@ endif() # The fastmath test intentionally uses NaN/infinity under -ffast-math. # Suppress the clang warning about this being technically undefined. -check_cxx_compiler_flag("-Wno-nan-infinity-disabled" COMPILER_SUPPORT_WNO_NAN_INF) -if(COMPILER_SUPPORT_WNO_NAN_INF) - set(EIGEN_FASTMATH_FLAGS "${EIGEN_FASTMATH_FLAGS} -Wno-nan-infinity-disabled") +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + check_cxx_compiler_flag("-Wno-nan-infinity-disabled" COMPILER_SUPPORT_WNO_NAN_INF) + if(COMPILER_SUPPORT_WNO_NAN_INF) + set(EIGEN_FASTMATH_FLAGS "${EIGEN_FASTMATH_FLAGS} -Wno-nan-infinity-disabled") + endif() endif() if(COMPILER_SUPPORT_FASTMATH)
diff --git a/test/packetmath_fastmath.cpp b/test/packetmath_fastmath.cpp index 94d0ed6..0bdeac5 100644 --- a/test/packetmath_fastmath.cpp +++ b/test/packetmath_fastmath.cpp
@@ -88,7 +88,7 @@ struct packetmath_fastmath_runner<Scalar, true> { static void run() { typedef typename Eigen::internal::packet_traits<Scalar>::type Packet; - const int packet_size = Eigen::internal::unpacket_traits<Packet>::size; + const int packet_size = Eigen::internal::packet_traits<Scalar>::size; Scalar output[packet_size]; for (int i = 0; i < packet_size; ++i) { output[i] = Scalar(0); @@ -119,14 +119,14 @@ b[i] = Scalar(-(i + 1)); } - std::memset(mask, 0, sizeof(mask)); + std::memset(static_cast<void*>(mask), 0, sizeof(mask)); select_with_mask<Scalar, Packet>(mask, a, b, selected); for (int i = 0; i < packet_size; ++i) { VERIFY_IS_EQUAL(selected[i], b[i]); } VERIFY(!(mask_any<Scalar, Packet>(mask))); - std::memset(mask, 0xff, sizeof(mask)); + std::memset(static_cast<void*>(mask), 0xff, sizeof(mask)); select_with_mask<Scalar, Packet>(mask, a, b, selected); for (int i = 0; i < packet_size; ++i) { VERIFY_IS_EQUAL(selected[i], a[i]); @@ -140,8 +140,8 @@ VERIFY((ptrue_mask_any<Scalar, Packet>())); for (int lane = 0; lane < packet_size; ++lane) { - std::memset(mask, 0, sizeof(mask)); - std::memset(mask + lane, 0xff, sizeof(Scalar)); + std::memset(static_cast<void*>(mask), 0, sizeof(mask)); + std::memset(static_cast<void*>(mask + lane), 0xff, sizeof(Scalar)); select_with_mask<Scalar, Packet>(mask, a, b, selected); for (int i = 0; i < packet_size; ++i) { VERIFY_IS_EQUAL(selected[i], i == lane ? a[i] : b[i]);