Skip to content

AVX Alignment Issue

Summary

  • Code in Eigen/src/Core/arch/AVX/TypeCasting.h uses _mm256_store_si256() (aligned AVX store requiring 32-byte alignment)
  • But stores to EIGEN_ALIGN16 int64_t aux[4] (only 16-byte aligned)
  • Results in segmentation fault when aux address is not 32-byte aligned

Environment

  • Operating System : Linux
  • Architecture : x64
  • Eigen Version : 5.0.0
  • Compiler Version : g++11.4.0
  • Compile Flags : -mavx2 -O0
  • Vector Extension : AVX

Minimal Example

#include <Eigen/Core>
#include <iostream>

void trigger_bug()
{
  Eigen::Array<double, -1, 1> azimuth = Eigen::Array<double, -1, 1>::LinSpaced(1024, 0.0, 6.28);
  Eigen::Array<double, -1, 1> elevation = Eigen::Array<double, -1, 1>::LinSpaced(1024, -0.5, 0.5);

  Eigen::Array<double, -1, 3> coords(1024, 3);

  coords.col(0) = (azimuth + elevation).cos() * elevation.cos();

  std::cout << "First coordinate: " << coords(0, 0) << std::endl;
}

int main()
{
  trigger_bug();
  return 0;
}

Steps to reproduce

  1. Eigen 5.0.0
  2. Compile example with with avx2 (e.g. g++ -I/tmp/eigen-5.0.0 -mavx2 -O0 alignment_bug_example.cpp -o alignment_bug_example && ./alignment_bug_example)
  3. Run into segfault

What is the current bug behavior?

It segfaults in:

Eigen::internal::pstore<long, Eigen::internal::eigen_packet_wrapper<long long __vector(4), 3> >(long*, Eigen::internal::eigen_packet_wrapper<long long __vector(4), 3> const&)

What is the expected correct behavior?

Correct calculations.

Relevant logs

Warning Messages

Benchmark scripts and results

Anything else that might help

--- Eigen/src/Core/arch/AVX/TypeCasting.h
+++ Eigen/src/Core/arch/AVX/TypeCasting.h
@@ -242,7 +242,7 @@ EIGEN_STRONG_INLINE Packet4d pcast<Packet4l, Packet4d>(const Packet4l& a) {
   return _mm256_cvtepi64_pd(a);
 #else
   EIGEN_ALIGN16 int64_t aux[4];
-  pstore(aux, a);
+  pstoreu(aux, a);
   return _mm256_set_pd(static_cast<double>(aux[3]), static_cast<double>(aux[2]), static_cast<double>(aux[1]),
                        static_cast<double>(aux[0]));
 #endif
  • Have a plan to fix this issue.
Edited by d-vo