This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Work around MSVC bug affecting `get(enumerator_result)`
ClosedPublic

Authored by kuhar on Mar 25 2023, 6:25 PM.

Details

Summary

This happened on a small number of MSVC releases (19.31.31xxx, Visual Studio 2022 17.1.x), and worked fine on everything else.

The issue seemed to be related to return type deduction on a function with and if constexpr; the compiler got confused and deduced different function return type from the type of the return statement.

The workaround is to split get into two functions using enable_if.

Diff Detail

Event Timeline

kuhar created this revision.Mar 25 2023, 6:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 25 2023, 6:25 PM
Herald added a subscriber: hanchung. · View Herald Transcript
kuhar requested review of this revision.Mar 25 2023, 6:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 25 2023, 6:25 PM
dstuttard accepted this revision.Mar 27 2023, 3:19 AM

Confirmed that this fixes the issue I was seeing for MSVC 17.1.

This revision is now accepted and ready to land.Mar 27 2023, 3:19 AM

Looks like there are still issues with MSVC 14.29.xxxx

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(492): error C2338: tuple index out of bounds
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(504): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(504): note: see reference to class template instantiation 'std::tuple_element<1,std::tuple<const mlir::utils::IteratorType &>>' being compiled
external/llvm-project/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp(1152): note: see reference to class template instantiation 'std::tuple_element<2,std::tuple<size_t,const mlir::utils::IteratorType &>>' being compiled
external/llvm-project/llvm/include\llvm/ADT/STLExtras.h(2323): note: see reference to alias template instantiation 'std::tuple_element_t<2,std::tuple<size_t,const mlir::utils::IteratorType &>>' being compiled
kuhar added a comment.Mar 28 2023, 8:14 AM

Looks like there are still issues with MSVC 14.29.xxxx

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(492): error C2338: tuple index out of bounds
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(504): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(504): note: see reference to class template instantiation 'std::tuple_element<1,std::tuple<const mlir::utils::IteratorType &>>' being compiled
external/llvm-project/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp(1152): note: see reference to class template instantiation 'std::tuple_element<2,std::tuple<size_t,const mlir::utils::IteratorType &>>' being compiled
external/llvm-project/llvm/include\llvm/ADT/STLExtras.h(2323): note: see reference to alias template instantiation 'std::tuple_element_t<2,std::tuple<size_t,const mlir::utils::IteratorType &>>' being compiled

Gah... Are you able to check if replacing std::tuple_element_t<I, value_reference_tuple> with decltype(auto) fixes that? The explicit return type was not necessary on 2022 17.1 but something I used to debug the previous failure and never bothered undoing.

jmorse added a subscriber: jmorse.Mar 28 2023, 8:35 AM

Note that I'm seeing this too with Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29915 for x64.

kuhar added a comment.Mar 28 2023, 8:40 AM

Note that I'm seeing this too with Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29915 for x64.

It'd be great if we had buildbots that cover a wider range of MSVC versions...

Gah... Are you able to check if replacing std::tuple_element_t<I, value_reference_tuple> with decltype(auto) fixes that? The explicit return type was not necessary on 2022 17.1 but something I used to debug the previous failure and never bothered undoing.

Either this or moving enable_if to the return type should fix this, I would guess. Could either of you, @jmorse or @jurahul, try that out? This would save me a couple of hours of work as I don't have these versions available to me.

probinson added inline comments.
llvm/include/llvm/ADT/STLExtras.h
2323

Tuple index should be decremented, yes?

kuhar marked an inline comment as done.Mar 28 2023, 8:46 AM
kuhar added inline comments.
llvm/include/llvm/ADT/STLExtras.h
2323

No, it's should be as-is. value_reference_tuple includes index as the first element, which is not the case for Storage.

Alternatively, we could have std::tuple_element_t<I - 1, range_reference_tuple>, but that shouldn't change anything.

kuhar marked an inline comment as done.Mar 28 2023, 8:49 AM

It'd be great if we had buildbots that cover a wider range of MSVC versions...

I agree, but my colleagues who pay more attention to this stuff claim there's only one bot actually using MSVC at all (the llvm-clang-x86_64-sie-win bot). They tell me all the other public Windows bots are using clang-cl, apparently, which doesn't help those of us downstream with big MSVC farms.