xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

argmax crashes when compiled using Visual Studio compiler with O1/O2 optimizations

Open rusmozhaik opened this issue 3 years ago • 4 comments

This issue was observed when running cmake unit tests on Windows platform: in debug mode all tests were passed, in release mode it tests were failed with unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.

Here is sample code to reproduce this issue:

#include <xtensor.hpp>

int main() {
    xt::xarray<double> a = {0, 1, 0};
    auto max = xt::argmax(a);
    std::cout << max << std::endl;
    return 0;
}

cl /I xtensor-0.24.2\include /I xtl-0.7.4\include argmax.cpp produces exe which doesn't crash

cl /I xtensor-0.24.2\include /I xtl-0.7.4\include /O1 argmax.cpp produces exe which crashes

Compiler information:

where cl
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.33.31629\bin\Hostx86\x86\cl.exe
cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.33.31629 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

rusmozhaik avatar Aug 19 '22 07:08 rusmozhaik

Probably it hase the same root cause: #2551

rusmozhaik avatar Aug 19 '22 07:08 rusmozhaik

Thanks for reporting. I don't have easy access to Visual Studio. Would you be willing to debug this a bit? Could you tell us which part of the implementation works and which does not?

tdegeus avatar Aug 24 '22 11:08 tdegeus

This does have the same root at #2551.

This happens only with MSVC > v19.31

A second, minimal reproduction, found in return of argmax

#include <xtensor.hpp>

int main() {
    xt::xtensor<std::size_t, 0> a{5};
    return 0;
}

It looks like there is an issue with 0 dimension xtensors / std::array<size_t, 0>.

During construction, the call to xt::resize(shape, true) fails somewhere in the inlined computed_strides()

makeshift printf stacktrace
xt::xtensor_container<class xt::uvector<unsigned __int64,class std::allocator<unsigned __int64> >,0,1,struct xt::xtensor_expression_tag>::xtensor_container
  xt::shape
  R: class std::array<unsigned __int64,0>
  T: unsigned __int64
    xt::detail::initializer_shape
    R: class std::array<unsigned __int64,0>
    U: unsigned __int64
    size_type: unsigned __int64
xt::xtensor_container<class xt::uvector<unsigned __int64,class std::allocator<unsigned __int64> >,0,1,struct xt::xtensor_expression_tag>::xtensor_container
  xt::xstrided_container<class xt::xtensor_container<class xt::uvector<unsigned __int64,class std::allocator<unsigned __int64> >,0,1,struct xt::xtensor_expression_tag> >::resize
  D: class xt::xtensor_container<class xt::uvector<unsigned __int64,class std::allocator<unsigned __int64> >,0,1,struct xt::xtensor_expression_tag>
  S: class std::array<unsigned __int64,0>
  dim: 0
  before compute_strides
    xt::compute_strides
    L: 1
    shape_type: class std::array<unsigned __int64,0>
    layout_type: enum xt::layout_type l: 1
    strides_type: class std::array<__int64,0>
    backstrides_type: class std::array<__int64,0>
      xt::detail::compute_strides
      L: 1
      shape_type: class std::array<unsigned __int64,0>
      layout_type: enum xt::layout_type l: 1
      strides_type: class std::array<__int64,0>
      bs_ptr: class std::array<__int64,0> * __ptr64
      strides_value_type: __int64
      data_size: 1
      is_row_major
      size: 0

I copied the relevant xtensor functions out into a minimal reproduction on godbolt.

I'm leaning towards MSVC compiler bug at this point

sg-james avatar Aug 30 '22 15:08 sg-james

upstream bug report: https://developercommunity.visualstudio.com/t/Runtime-Crash-msvc--1931-with-optimiz/10134617

sg-james avatar Sep 13 '22 15:09 sg-james

Thanks @sg-james !! I guess would should consider adding the workaround proposed upstream for this specific compiler version?

tdegeus avatar Jan 06 '23 16:01 tdegeus

I took a stab at a PR for this. Still trying to find a place to add tests

sg-james avatar Jan 06 '23 17:01 sg-james