Cherry-picks the applicable hunks of upstream https://gitlab.com/libeigen/eigen/-/merge_requests/3623 (commit ebafb223a). The evaluator stored its operand evaluators only when it vectorized, so its layout depended on `EIGEN_VECTORIZE`. Code built with and without vectorization, such as host C++ and CUDA, then disagreed on the layout of the same type, which crashes ThinLTO builds with debug info. The evaluator now always stores both operand evaluators. PiperOrigin-RevId: 1034814610 Change-Id: I7ccdba491ccdb703509074dea79c70dc8ee52afe
diff --git a/Eigen/src/Core/ProductEvaluators.h b/Eigen/src/Core/ProductEvaluators.h index daf162d..ccff85f 100644 --- a/Eigen/src/Core/ProductEvaluators.h +++ b/Eigen/src/Core/ProductEvaluators.h
@@ -743,11 +743,6 @@ template <int StorageOrder, int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode> struct etor_product_packet_impl; -struct product_empty_packet_evaluator { - template <typename Xpr> - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit product_empty_packet_evaluator(const Xpr&) {} -}; - // Coefficient-based evaluator of lhs * rhs whose packets run along rows if EvalToRowMajor_, else along columns. template <typename Lhs, typename Rhs, bool EvalToRowMajor_> struct coeff_based_product_evaluator : evaluator_base<Product<Lhs, Rhs, LazyProduct>> { @@ -761,11 +756,7 @@ // Also construct directly from the product operands so packet-cascade callers // do not have to materialize a temporary Product expression first. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE coeff_based_product_evaluator(const Lhs& lhs, const Rhs& rhs) - : m_lhs(lhs), - m_rhs(rhs), - m_lhsImpl(m_lhs), // Real evaluator objects for packet products; empty placeholders for scalar-only products. - m_rhsImpl(m_rhs), - m_innerDim(lhs.cols()) { + : m_lhs(lhs), m_rhs(rhs), m_lhsImpl(m_lhs), m_rhsImpl(m_rhs), m_innerDim(lhs.cols()) { EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost); EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::AddCost); EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost); @@ -836,13 +827,8 @@ static constexpr int Flags = ((int(LhsFlags) | int(RhsFlags)) & HereditaryBits & ~RowMajorBit) | (EvalToRowMajor ? RowMajorBit : 0) | (Vectorize ? PacketAccessBit : 0) | (XprType::IsVectorAtCompileTime ? LinearAccessBit : 0); - using LhsPacketEtorType = - std::conditional_t<bool(int(Flags) & PacketAccessBit), LhsEtorType, product_empty_packet_evaluator>; - using RhsPacketEtorType = - std::conditional_t<bool(int(Flags) & PacketAccessBit), RhsEtorType, product_empty_packet_evaluator>; - static constexpr bool SingleTermCoeff = InnerSize == 1 && std::is_same<LhsPacketEtorType, LhsEtorType>::value && - std::is_same<RhsPacketEtorType, RhsEtorType>::value; + static constexpr bool SingleTermCoeff = InnerSize == 1; static constexpr int LhsOuterStrideBytes = int(LhsNestedCleaned::OuterStrideAtCompileTime) * int(sizeof(typename LhsNestedCleaned::Scalar)); @@ -923,8 +909,10 @@ add_const_on_value_type_t<LhsNested> m_lhs; add_const_on_value_type_t<RhsNested> m_rhs; - LhsPacketEtorType m_lhsImpl; - RhsPacketEtorType m_rhsImpl; + // Unconditionally store operand evaluators so layout is ODR-invariant + // across EIGEN_VECTORIZE settings. + LhsEtorType m_lhsImpl; + RhsEtorType m_rhsImpl; variable_if_dynamic<Index, InnerSize> m_innerDim; }; @@ -943,15 +931,14 @@ }; // A dense assignment reads its source only through the evaluator, so it may evaluate a coefficient-based product -// along the destination's storage order, which the destination's packets need. It does so only where that order -// vectorizes; otherwise the assignment is scalar in either order, and the expression's evaluator is kept. +// along the destination's storage order unconditionally, keeping the source evaluator type ODR-invariant across +// EIGEN_VECTORIZE settings while clearing PacketAccessBit when that order does not vectorize. template <typename DstEvaluatorType, typename Lhs, typename Rhs> struct dense_assignment_source_evaluator< DstEvaluatorType, Product<Lhs, Rhs, LazyProduct>, std::enable_if_t<std::is_same<typename evaluator_traits<Lhs>::Shape, DenseShape>::value && std::is_same<typename evaluator_traits<Rhs>::Shape, DenseShape>::value>> { - using DstOrdered = coeff_based_product_evaluator<Lhs, Rhs, (int(DstEvaluatorType::Flags) & RowMajorBit) != 0>; - using type = std::conditional_t<DstOrdered::Vectorize, DstOrdered, evaluator<Product<Lhs, Rhs, LazyProduct>>>; + using type = coeff_based_product_evaluator<Lhs, Rhs, (int(DstEvaluatorType::Flags) & RowMajorBit) != 0>; }; template <typename Lhs, typename Rhs>
diff --git a/test/product_evaluators.cpp b/test/product_evaluators.cpp index 7e0cc56..74df1af 100644 --- a/test/product_evaluators.cpp +++ b/test/product_evaluators.cpp
@@ -244,7 +244,38 @@ VERIFY_IS_EQUAL(aliased, expectedLeft); } +// The coefficient-based product evaluator must have the same layout in both storage orders, vectorized or not: +// whether it vectorizes also depends on the vectorization settings of the translation unit, and translation units +// that disagree on the layout of the same evaluator type violate the one-definition rule. +template <typename Scalar> +void coeff_based_evaluator_layout() { + using ColMat = Matrix<Scalar, 4, 4, ColMajor>; + using RowMat = Matrix<Scalar, 4, 4, RowMajor>; + using DynMat = Matrix<Scalar, Dynamic, Dynamic, ColMajor>; + using Vec = Matrix<Scalar, 4, 1>; + using ComplexVec = Matrix<std::complex<Scalar>, 4, 1>; + using Mat1 = Matrix<Scalar, 1, 1>; + VERIFY_IS_EQUAL(sizeof(internal::coeff_based_product_evaluator<ColMat, Vec, false>), + sizeof(internal::coeff_based_product_evaluator<ColMat, Vec, true>)); + VERIFY_IS_EQUAL(sizeof(internal::coeff_based_product_evaluator<RowMat, RowMat, false>), + sizeof(internal::coeff_based_product_evaluator<RowMat, RowMat, true>)); + VERIFY_IS_EQUAL(sizeof(internal::coeff_based_product_evaluator<DynMat, DynMat, false>), + sizeof(internal::coeff_based_product_evaluator<DynMat, DynMat, true>)); + VERIFY_IS_EQUAL(sizeof(internal::coeff_based_product_evaluator<ColMat, ComplexVec, false>), + sizeof(internal::coeff_based_product_evaluator<ColMat, ComplexVec, true>)); + VERIFY_IS_EQUAL(Mat1::Constant(Scalar(3)).lazyProduct(Mat1::Constant(Scalar(4))).coeff(0, 0), Scalar(12)); + Matrix<std::complex<Scalar>, 4, 1> clhs = Matrix<std::complex<Scalar>, 4, 1>::Constant(std::complex<Scalar>(2, 1)); + Matrix<Scalar, 1, 4> rrhs = Matrix<Scalar, 1, 4>::Constant(Scalar(3)); + VERIFY_IS_EQUAL(clhs.lazyProduct(rrhs).coeff(2, 1), std::complex<Scalar>(6, 3)); + // Dense assignments evaluate a lazy product along the destination's storage order, whether or not it vectorizes. + using SrcEvaluator = typename internal::dense_assignment_source_evaluator<internal::evaluator<RowMat>, + Product<ColMat, ColMat, LazyProduct>>::type; + STATIC_CHECK((std::is_same<SrcEvaluator, internal::coeff_based_product_evaluator<ColMat, ColMat, true>>::value)); +} + EIGEN_DECLARE_TEST(product_evaluators) { + CALL_SUBTEST_1(coeff_based_evaluator_layout<float>()); + CALL_SUBTEST_1(coeff_based_evaluator_layout<double>()); for (int repeat = 0; repeat < g_repeat; ++repeat) { CALL_SUBTEST_4((outer_product_scalar_types<double, double, ColMajor>())); CALL_SUBTEST_4((outer_product_scalar_types<double, double, RowMajor>()));