diff --git a/Eigen/src/Core/products/TriangularMatrixVector.h b/Eigen/src/Core/products/TriangularMatrixVector.h index 413f0ee18..0e0e6902f 100644 --- a/Eigen/src/Core/products/TriangularMatrixVector.h +++ b/Eigen/src/Core/products/TriangularMatrixVector.h @@ -287,21 +287,35 @@ struct trmv_selector { constexpr bool DirectlyUseRhs = ActualRhsTypeCleaned::InnerStrideAtCompileTime == 1; + const RhsScalar* actualRhsPtr = actualRhs.data(); + + // Potentially create a temporary buffer to copy RHS to contiguous memory. gemv_static_vector_if - static_rhs; - - ei_declare_aligned_stack_constructed_variable( - RhsScalar, actualRhsPtr, actualRhs.size(), - DirectlyUseRhs ? const_cast(actualRhs.data()) : static_rhs.data()); - + static_rhs; // Fixed-sized array. + RhsScalar* buffer = nullptr; if (!DirectlyUseRhs) { + // Maybe used fixed-sized buffer, otherwise allocate. + if (static_rhs.data() != nullptr) { + buffer = static_rhs.data(); + } else { + // Allocate either with alloca or malloc. + Eigen::internal::check_size_for_overflow(actualRhs.size()); + buffer = static_cast((sizeof(RhsScalar) * actualRhs.size() <= EIGEN_STACK_ALLOCATION_LIMIT) + ? EIGEN_ALIGNED_ALLOCA(sizeof(RhsScalar) * actualRhs.size()) + : Eigen::internal::aligned_malloc(sizeof(RhsScalar) * actualRhs.size())); + } #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN Index size = actualRhs.size(); EIGEN_DENSE_STORAGE_CTOR_PLUGIN #endif - Map(actualRhsPtr, actualRhs.size()) = actualRhs; + Map(buffer, actualRhs.size()) = actualRhs; + actualRhsPtr = buffer; } + // Deallocate only if malloced. + Eigen::internal::aligned_stack_memory_handler buffer_stack_memory_destructor( + buffer, actualRhs.size(), + !DirectlyUseRhs && static_rhs.data() == nullptr && actualRhs.size() > EIGEN_STACK_ALLOCATION_LIMIT); internal::triangular_matrix_vector_product::run(actualLhs.rows(),