Tiny fix in unsupported/Eigen/CXX11/src/Tensor/TensorContraction.h

What changes ?

In file unsupported/Eigen/CXX11/src/Tensor/TensorContraction.h:703-704

const Index lhs_packet_size = internal::unpacket_traits<typename LeftEvaluator::PacketReturnType>::size;
const Index rhs_packet_size = internal::unpacket_traits<typename RightEvaluator::PacketReturnType>::size;
/* ... */
typedef internal::TensorContractionInputMapper<LhsScalar, Index, internal::Lhs, LeftEvaluator, left_nocontract_t,
                                               contract_t, lhs_packet_size, lhs_inner_dim_contiguous, false,
                                               lhs_alignment>
    LhsMapper;
typedef internal::TensorContractionInputMapper<RhsScalar, Index, internal::Rhs, RightEvaluator, right_nocontract_t,
                                               contract_t, rhs_packet_size, rhs_inner_dim_contiguous,
                                               rhs_inner_dim_reordered, rhs_alignment>
    RhsMapper;

The const **Index** lhs_packet_size here should always be deduced into const **int** lhs_packet_size.

How to prove the changes to be correct?

Because:

  • Who provide lhs_packet_size: Eigen::internal::unpacket_traits<...>::size, which is always an enum { size = 1 /*or 2, 4, ...*/ }, which is always an int.
  • Who consume lhs_packet_size: Eigen::internal::TensorContractionInputMapper, which has declaration template <typename Scalar_, typename Index, int side, typename Tensor, typename nocontract_t, typename contract_t, **int packet_size**, bool inner_dim_contiguous, bool inner_dim_reordered, int Alignment, template <class> class MakePointer_ = MakePointer> class TensorContractionInputMapper, where packet_size is always required to be an int.

So, the provider provides an int and the consumer consumes an int here, so the lhs_packet_size should definitely be deduced into an int.

What does this change fix?

Potential C++20 Module compilation.

Recently I try to compile Eigen into a C++20 Module via g++, like:

#include <Eigen/Core>
export module Eigen;
export namespace Eigen
{
    using Eigen::Matrix;
}
g++ -std=c++26 -Wall -fmodules -fmodule-mapper=./bin/cache/module_mapper.txt -I./bin/debug/packages/eigen/install/include -c ./module/eigen.cpp -o ./bin/debug/module/eigen.o

There is only 1 compilation error, complaining

error: ‘template <...very.long...> Eigen::TensorContractionEvaluatorBase<Derived>::evalGemv(Scalar*) const’ exposes TU-local entity ‘class Eigen::internal::TensorContractionInputMapper<...very.long...>’

And once I changes Index into int here, the error disappears. (I guess it is because the Index is a dependent type and g++ cannot assume it to be always constexpr and module-exportable, so the exportation of Tensor exposes TU-local Index value in a deep place. If we replace const Index lhs_packet with constexpr Index lhs_packet here, g++-15.2 will accept it too, but it might break some CI in lower gcc version, so I suggest that we better replace it with definite-deduced const int lhs_packet.)

Thank you very much!!

Edited by anonymouspc

Merge request reports

Loading