xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.38k stars 400 forks source link

[Bug] Fails to compile with TBB and XSIMD enabled #2736

Open spectre-ns opened 1 year ago

spectre-ns commented 1 year ago

Windows 10 MSVC 2019 Enterprise C++20

Fails to compile with both XSIMD and TBB enabled. Fails with the following syntax error: 'template' On line 1252 of xassign.hpp

spectre-ns commented 1 year ago

Example of failed build

https://github.com/spectre-ns/xtensor/actions/runs/6706077908/job/18221831529

JohanMabille commented 1 year ago

I guess we need to capture needs_cast here, but I wonder if it's a compiler issue. Do you have the same issue with MSVC 2022 ?

spectre-ns commented 1 year ago

@JohanMabille yes the same issue exists in MSVC 2022

spectre-ns commented 1 year ago

@JohanMabille looks like this is a bug as constexpr variables should be captured in lambda scope implicitly.

https://stackoverflow.com/questions/33873788/can-i-use-a-constexpr-value-in-a-lambda-without-capturing-it

Specifically MSVC:

https://stackoverflow.com/questions/71493298/why-cant-a-constexpr-variable-be-seen-only-by-a-capturing-lambda

spectre-ns commented 1 year ago

Also there are issues out of the box around tbb defining min and max that need to be mitigated using #define NOMINMAX

spectre-ns commented 1 year ago

@tdegeus this issue isn't C++20 specific. Not sure what that label means though.

PerretB commented 4 months ago

@spectre-ns @JohanMabille hello, did you find a workaround for this problem? Activating the flag /Zc:lambda does solve the failed capture of the constexpr var in the lambda https://learn.microsoft.com/en-us/cpp/build/reference/zc-lambda?view=msvc-170 . However the syntax error: 'template' remains on this line (and several others):

https://github.com/xtensor-stack/xtensor/blob/8c0a484f04eccd0dbc0e25eb58a97de000fb048b/include/xtensor/xassign.hpp#L1252

spectre-ns commented 4 months ago

Hi @PerretB I have not found a workaround. It works in clang and GCC so it is a compiler issue...

brccabral commented 2 months ago

This small example triggers the error

#include <xtensor/xarray.hpp>
#include <xtensor/xrandom.hpp>
#include <xtensor-blas/xlinalg.hpp>

int main()
{
    auto W1 = xt::random::rand<float>({10, 50}) - 0.5f;
    auto X = xt::random::rand<float>({10, 50}) - 0.5f;
    xt::xarray<float> d1 = xt::linalg::dot(W1, X);

    return 0;
}
cmake_minimum_required(VERSION 3.14)
project(xt_tbb)

set(XTENSOR_USE_XSIMD 1)
set(XTENSOR_USE_TBB 1)
find_package(xtensor REQUIRED)
find_package(xtensor-blas REQUIRED)

add_executable(xt_tbb main.cpp)
target_compile_options(xt_tbb PUBLIC "/Zc:lambda")
target_include_directories(xt_tbb PUBLIC ${xtensor-blas_INCLUDE_DIRS})
target_link_libraries(xt_tbb PUBLIC xtensor)
C:\Program Files (x86)\xtensor\include\xtensor\xassign.hpp(1252,41): error C2187: syntax error: 'template' was unexpected here