llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[C++26] Crash when indexing a pack with a pack of indices

Open kaimfrai opened this issue 2 years ago • 4 comments

The following code crashes on the most recent clang trunk when compiling with -std=c++2c. The intent is to transform a pack of types into two sub-packs using the new pack indexing feature in C++26.

#include <utility>

template<typename... T>
struct TypeList{};

template<typename... T, std::size_t... left, std::size_t... right>
auto SplitImpl(TypeList<T...>, std::index_sequence<left...>, std::index_sequence<right...>)
{
    return TypeList<TypeList<T...[left]...>, TypeList<T...[sizeof...(left) + right]...>>{};
}

template<std::size_t N, typename... T>
auto Split(TypeList<T...> tl)
{
    return SplitImpl(tl, std::make_index_sequence<N>{}, std::make_index_sequence<sizeof...(T) - N>{});
}

auto tl1 = TypeList<char, bool, float, int, double>{};
auto tl2 = Split<2>(tl1);

https://godbolt.org/z/Px5c5971o

Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++2c <source>
1.	<eof> parser at end of file
2.	<source>:19:6: LLVM IR generation of declaration 'tl2'
3.	<source>:19:6: Generating code for declaration 'tl2'
 #0 0x0000000003695e68 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3695e68)
 #1 0x0000000003693fb4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3693fb4)
 #2 0x00000000035dd148 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f3b03e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00000000030aa562 llvm::Type::isIntegerTy(unsigned int) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x30aa562)
 #5 0x0000000003aaf7a5 clang::CodeGen::CodeGenTypes::ConvertTypeForMem(clang::QualType, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3aaf7a5)
 #6 0x000000000397a5dd clang::CodeGen::CodeGenModule::EmitNullConstant(clang::QualType) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x397a5dd)
 #7 0x0000000003a3f3fa clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3a3f3fa)
 #8 0x0000000003a62f45 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3a62f45)
 #9 0x0000000003a635f3 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3a635f3)
#10 0x0000000003a6bdd7 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#11 0x0000000003ef1e4e (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#12 0x0000000003ee2fb9 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ee2fb9)
#13 0x0000000005da18e4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5da18e4)
#14 0x0000000003eef935 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3eef935)
#15 0x0000000004186c01 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4186c01)
#16 0x000000000410c99b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x410c99b)
#17 0x0000000004269503 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4269503)
#18 0x0000000000c33a4c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc33a4c)
#19 0x0000000000c2d9fd ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#20 0x0000000003f38799 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#21 0x00000000035dd573 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35dd573)
#22 0x0000000003f389b9 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#23 0x0000000003efef77 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3efef77)
#24 0x0000000003eff92d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3eff92d)
#25 0x0000000003f0974c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3f0974c)
#26 0x0000000000c30eb1 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc30eb1)
#27 0x0000000000b28884 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xb28884)
#28 0x00007f3b03e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#29 0x00007f3b03e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#30 0x0000000000c2d55e _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc2d55e)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)

kaimfrai avatar Mar 22 '24 13:03 kaimfrai

@llvm/issue-subscribers-clang-codegen

Author: Kai Müller (kaimfrai)

The following code crashes on the most recent clang trunk when compiling with `-std=c++2c`. The intent is to transform a pack of types into two sub-packs using the new pack indexing feature in C++26.
#include &lt;utility&gt;

template&lt;typename... T&gt;
struct TypeList{};

template&lt;typename... T, std::size_t... left, std::size_t... right&gt;
auto SplitImpl(TypeList&lt;T...&gt;, std::index_sequence&lt;left...&gt;, std::index_sequence&lt;right...&gt;)
{
    return TypeList&lt;TypeList&lt;T...[left]...&gt;, TypeList&lt;T...[sizeof...(left) + right]...&gt;&gt;{};
}

template&lt;std::size_t N, typename... T&gt;
auto Split(TypeList&lt;T...&gt; tl)
{
    return SplitImpl(tl, std::make_index_sequence&lt;N&gt;{}, std::make_index_sequence&lt;sizeof...(T) - N&gt;{});
}

auto tl1 = TypeList&lt;char, bool, float, int, double&gt;{};
auto tl2 = Split&lt;2&gt;(tl1);

https://godbolt.org/z/Px5c5971o

Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++2c &lt;source&gt;
1.	&lt;eof&gt; parser at end of file
2.	&lt;source&gt;:19:6: LLVM IR generation of declaration 'tl2'
3.	&lt;source&gt;:19:6: Generating code for declaration 'tl2'
 #<!-- -->0 0x0000000003695e68 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3695e68)
 #<!-- -->1 0x0000000003693fb4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3693fb4)
 #<!-- -->2 0x00000000035dd148 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #<!-- -->3 0x00007f3b03e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->4 0x00000000030aa562 llvm::Type::isIntegerTy(unsigned int) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x30aa562)
 #<!-- -->5 0x0000000003aaf7a5 clang::CodeGen::CodeGenTypes::ConvertTypeForMem(clang::QualType, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3aaf7a5)
 #<!-- -->6 0x000000000397a5dd clang::CodeGen::CodeGenModule::EmitNullConstant(clang::QualType) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x397a5dd)
 #<!-- -->7 0x0000000003a3f3fa clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3a3f3fa)
 #<!-- -->8 0x0000000003a62f45 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3a62f45)
 #<!-- -->9 0x0000000003a635f3 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3a635f3)
#<!-- -->10 0x0000000003a6bdd7 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#<!-- -->11 0x0000000003ef1e4e (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#<!-- -->12 0x0000000003ee2fb9 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ee2fb9)
#<!-- -->13 0x0000000005da18e4 clang::ParseAST(clang::Sema&amp;, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5da18e4)
#<!-- -->14 0x0000000003eef935 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3eef935)
#<!-- -->15 0x0000000004186c01 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4186c01)
#<!-- -->16 0x000000000410c99b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x410c99b)
#<!-- -->17 0x0000000004269503 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4269503)
#<!-- -->18 0x0000000000c33a4c cc1_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc33a4c)
#<!-- -->19 0x0000000000c2d9fd ExecuteCC1Tool(llvm::SmallVectorImpl&lt;char const*&gt;&amp;, llvm::ToolContext const&amp;) driver.cpp:0:0
#<!-- -->20 0x0000000003f38799 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const::'lambda'()&gt;(long) Job.cpp:0:0
#<!-- -->21 0x00000000035dd573 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref&lt;void ()&gt;) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35dd573)
#<!-- -->22 0x0000000003f389b9 clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const (.part.0) Job.cpp:0:0
#<!-- -->23 0x0000000003efef77 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&amp;, clang::driver::Command const*&amp;, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3efef77)
#<!-- -->24 0x0000000003eff92d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3eff92d)
#<!-- -->25 0x0000000003f0974c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3f0974c)
#<!-- -->26 0x0000000000c30eb1 clang_main(int, char**, llvm::ToolContext const&amp;) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc30eb1)
#<!-- -->27 0x0000000000b28884 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xb28884)
#<!-- -->28 0x00007f3b03e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#<!-- -->29 0x00007f3b03e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#<!-- -->30 0x0000000000c2d55e _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc2d55e)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)

llvmbot avatar Mar 22 '24 13:03 llvmbot

Confirmed with assertion trunk: https://godbolt.org/z/Y6cMj81v8

The backtrace looks like: https://github.com/llvm/llvm-project/issues/76674

Assertion:

clang++: /root/llvm-project/clang/lib/AST/Decl.cpp:2561:
clang::APValue* clang::VarDecl::evaluateValueImpl(llvm::SmallVectorImpl<std::pair<clang::SourceLocation, clang::PartialDiagnostic> >&, bool) const:
Assertion `!Init->isValueDependent()' failed.

Backtrace:

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++2c <source>
1.	<eof> parser at end of file
2.	<source>:19:6: LLVM IR generation of declaration 'tl2'
3.	<source>:19:6: Generating code for declaration 'tl2'
 #0 0x000000000391d768 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x391d768)
 #1 0x000000000391b44c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x391b44c)
 #2 0x0000000003861d98 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f7714442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f77144969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007f7714442476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007f77144287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007f771442871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007f7714439e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00000000073357f3 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x73357f3)
#10 0x000000000733587b clang::VarDecl::evaluateValue() const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x733587b)
#11 0x0000000003c22b8b clang::CodeGen::ConstantEmitter::tryEmitPrivateForVarInit(clang::VarDecl const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3c22b8b)
#12 0x0000000003c22f21 clang::CodeGen::ConstantEmitter::tryEmitForInitializer(clang::VarDecl const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3c22f21)
#13 0x0000000003cf6a27 clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3cf6a27)
#14 0x0000000003d193e9 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3d193e9)
#15 0x0000000003d1a470 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3d1a470)
#16 0x0000000003d23a4b clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#17 0x00000000041bc306 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#18 0x00000000041acc28 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41acc28)
#19 0x00000000061b2564 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x61b2564)
#20 0x00000000041b9e48 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41b9e48)
#21 0x00000000044390a9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44390a9)
#22 0x00000000043c26de clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43c26de)
#23 0x000000000451c0ce clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x451c0ce)
#24 0x0000000000c28ecc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc28ecc)
#25 0x0000000000c2215a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#26 0x00000000041fcdb9 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#27 0x0000000003862244 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3862244)
#28 0x00000000041fd3af clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#29 0x00000000041c3745 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41c3745)
#30 0x00000000041c41ad clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41c41ad)
#31 0x00000000041cbf15 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41cbf15)
#32 0x0000000000c26365 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc26365)
#33 0x0000000000b1c824 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb1c824)
#34 0x00007f7714429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#35 0x00007f7714429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#36 0x0000000000c21c4e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc21c4e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

shafik avatar Mar 23 '24 02:03 shafik

CC @cor3ntin

shafik avatar Mar 23 '24 02:03 shafik

This was fixed!

cor3ntin avatar Jul 10 '24 14:07 cor3ntin