Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++20: ICE trying to emit a call to an immediate function #65520

Closed
mpolacek opened this issue Sep 6, 2023 · 3 comments · Fixed by #66222
Closed

c++20: ICE trying to emit a call to an immediate function #65520

mpolacek opened this issue Sep 6, 2023 · 3 comments · Fixed by #66222
Labels
clang:codegen confirmed Verified by a second party consteval C++20 consteval crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@mpolacek
Copy link

mpolacek commented Sep 6, 2023

consteval int bar (int i) { if (i != 1) throw 1; return 0; }

void
g ()
{
  int a1[bar(3)];
}

Using

$ xclang++ -v
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 6258912880876edd44944e130aef01b33480168b)

results in:

$ xclang++ -c -std=c++20 ice1.C
xclang++: /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGExpr.cpp:5305: clang::CodeGen::RValue clang::CodeGen::CodeGenFunction::EmitCall(clang::QualType, const clang::CodeGen::CGCallee&, const clang::CallExpr*, clang::CodeGen::ReturnValueSlot, llvm::Value*): Assertion `(!isa_and_present<FunctionDecl>(TargetDecl) || !cast<FunctionDecl>(TargetDecl)->isImmediateFunction()) && "trying to emit a call to an immediate function"' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: xclang++ -c -std=c++20 ice1.C
1.	<eof> parser at end of file
2.	ice1.C:4:1: LLVM IR generation of declaration 'g'
3.	ice1.C:4:1: Generating code for declaration 'g'
 #0 0x0000000004529287 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/mpolacek/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:22
 #1 0x0000000004529381 PrintStackTraceSignalHandler(void*) /home/mpolacek/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x0000000004526e46 llvm::sys::RunSignalHandlers() /home/mpolacek/src/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #3 0x0000000004528b73 llvm::sys::CleanupOnSignal(unsigned long) /home/mpolacek/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:367:31
 #4 0x0000000004460bf8 (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) /home/mpolacek/src/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:73:5
 #5 0x0000000004461087 CrashRecoverySignalHandler(int) /home/mpolacek/src/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:391:1
 #6 0x00007ff6ba85fb20 __restore_rt (/lib64/libc.so.6+0x3cb20)
 #7 0x00007ff6ba8afe8c __pthread_kill_implementation /usr/src/debug/glibc-2.36-11.fc37.x86_64/nptl/pthread_kill.c:44:76
 #8 0x00007ff6ba85fa76 gsignal /usr/src/debug/glibc-2.36-11.fc37.x86_64/signal/../sysdeps/posix/raise.c:27:6
 #9 0x00007ff6ba8497fc abort /usr/src/debug/glibc-2.36-11.fc37.x86_64/stdlib/abort.c:81:7
#10 0x00007ff6ba84971b _nl_load_domain.cold /usr/src/debug/glibc-2.36-11.fc37.x86_64/intl/loadmsgcat.c:1177:9
#11 0x00007ff6ba858656 (/lib64/libc.so.6+0x35656)
#12 0x0000000004fd2591 clang::CodeGen::CodeGenFunction::EmitCall(clang::QualType, clang::CodeGen::CGCallee const&, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::Value*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGExpr.cpp:5309:26
#13 0x0000000004fd0624 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGExpr.cpp:4993:18
#14 0x000000000500ece0 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:580:32
#15 0x000000000502a452 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) /home/mpolacek/src/llvm-project/build/tools/clang/include/clang/AST/StmtNodes.inc:949:1
#16 0x000000000500de9c (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:407:57
#17 0x000000000502765e clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:5098:1
#18 0x0000000004abb2e6 clang::CodeGen::CodeGenFunction::EmitVariablyModifiedType(clang::QualType) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:2317:54
#19 0x0000000004f8a2e7 clang::CodeGen::CodeGenFunction::EmitAutoVarAlloca(clang::VarDecl const&) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGDecl.cpp:1458:26
#20 0x0000000004f8984b clang::CodeGen::CodeGenFunction::EmitAutoVarDecl(clang::VarDecl const&) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGDecl.cpp:1345:18
#21 0x0000000004f83ebb clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGDecl.cpp:218:27
#22 0x0000000004f83bb0 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGDecl.cpp:164:47
#23 0x00000000049f93a9 clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:1394:3
#24 0x00000000049f498e clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:453:5
#25 0x00000000049f3922 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:61:3
#26 0x00000000049f4f12 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:507:3
#27 0x0000000004ab732c clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:1262:33
#28 0x0000000004ab82ab clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:1481:21
#29 0x0000000004ae98d3 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:5657:3
#30 0x0000000004ae228c clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:3922:47
#31 0x0000000004ae1232 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:3663:27
#32 0x0000000004aedd0a clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:6561:37
#33 0x0000000005e67523 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp:189:7
#34 0x0000000005e5e580 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:237:11
#35 0x0000000007ec7cc4 clang::ParseAST(clang::Sema&, bool, bool) /home/mpolacek/src/llvm-project/clang/lib/Parse/ParseAST.cpp:167:20
#36 0x000000000543bda6 clang::ASTFrontendAction::ExecuteAction() /home/mpolacek/src/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1178:11
#37 0x0000000005e5bb09 clang::CodeGenAction::ExecuteAction() /home/mpolacek/src/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:1208:5
#38 0x000000000543b6f7 clang::FrontendAction::Execute() /home/mpolacek/src/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1068:38
#39 0x00000000053705ae clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/mpolacek/src/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1054:42
#40 0x00000000055ce71a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/mpolacek/src/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:272:38
#41 0x0000000000d41ca2 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /home/mpolacek/src/llvm-project/clang/tools/driver/cc1_main.cpp:254:40
#42 0x0000000000d32de7 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) /home/mpolacek/src/llvm-project/clang/tools/driver/driver.cpp:366:20
#43 0x0000000000d32fd1 clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)::operator()(llvm::SmallVectorImpl<char const*>&) const /home/mpolacek/src/llvm-project/clang/tools/driver/driver.cpp:507:5
#44 0x0000000000d345bb int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) /home/mpolacek/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:47:3
#45 0x00000000051f475d llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::operator()(llvm::SmallVectorImpl<char const*>&) const /home/mpolacek/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:69:3
#46 0x00000000051f35e8 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'()::operator()() const /home/mpolacek/src/llvm-project/clang/lib/Driver/Job.cpp:440:32
#47 0x00000000051f3a0a 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) /home/mpolacek/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:46:40
#48 0x000000000331120e llvm::function_ref<void ()>::operator()() const /home/mpolacek/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:62
#49 0x000000000446124f llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) /home/mpolacek/src/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:427:10
#50 0x00000000051f37d4 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const /home/mpolacek/src/llvm-project/clang/lib/Driver/Job.cpp:440:7
#51 0x0000000005196feb clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const /home/mpolacek/src/llvm-project/clang/lib/Driver/Compilation.cpp:199:22
#52 0x0000000005197317 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const /home/mpolacek/src/llvm-project/clang/lib/Driver/Compilation.cpp:253:62
#53 0x00000000051a95dc clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) /home/mpolacek/src/llvm-project/clang/lib/Driver/Driver.cpp:1883:28
#54 0x0000000000d34167 clang_main(int, char**, llvm::ToolContext const&) /home/mpolacek/src/llvm-project/clang/tools/driver/driver.cpp:542:39
#55 0x0000000000d653ee main /home/mpolacek/src/llvm-project/build/tools/clang/tools/driver/clang-driver.cpp:15:58
#56 0x00007ff6ba84a510 __libc_start_call_main /usr/src/debug/glibc-2.36-11.fc37.x86_64/csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#57 0x00007ff6ba84a5c9 call_init /usr/src/debug/glibc-2.36-11.fc37.x86_64/csu/../csu/libc-start.c:128:20
#58 0x00007ff6ba84a5c9 __libc_start_main@GLIBC_2.2.5 /usr/src/debug/glibc-2.36-11.fc37.x86_64/csu/../csu/libc-start.c:347:5
#59 0x0000000000d313a5 _start (/home/mpolacek/src/llvm-project/build/bin/clang-12+0xd313a5)
xclang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 6258912880876edd44944e130aef01b33480168b)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/mpolacek/bin
xclang++: note: diagnostic msg: 
@EugeneZelenko EugeneZelenko added clang:codegen crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Sep 6, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2023

@llvm/issue-subscribers-clang-codegen

@cor3ntin cor3ntin added the consteval C++20 consteval label Sep 6, 2023
@cor3ntin
Copy link
Contributor

cor3ntin commented Sep 6, 2023

@cor3ntin cor3ntin added the confirmed Verified by a second party label Sep 6, 2023
@tbaederr
Copy link
Contributor

tbaederr commented Sep 7, 2023

We try to emit this as VLA (because the consteval function isn't valid).

Not sure if there's an existing mechanism for a "fatal" error in this context.

cor3ntin added a commit to cor3ntin/llvm-project that referenced this issue Oct 5, 2023
The bounds of a c++ array is a _constant-expression_.
And in C++ it is also a constant expression.

But we also support VLAs, ie arrays with non-constant bounds.

We need to take care to handle the case of a consteval function
(which are specified to be only immediately called in
non-constant contexts) that appear in arrays bounds.

This introduces `Sema::isAlwayConstantEvaluatedContext`,
and a flag in ExpressionEvaluationContextRecord, such that
immediate functions in array bounds are always immediately invoked.

Sema had both `isConstantEvaluatedContext` and
`isConstantEvaluated`, so I took the opportunity to cleanup that.

The change in `TimeProfilerTest.cpp` is an unfortunate
manifestation of the problem that llvm#66203 seeks to address.

Fixes llvm#65520
cor3ntin added a commit that referenced this issue Oct 5, 2023
The bounds of a c++ array is a _constant-expression_. And in C++ it is
also a constant expression.

But we also support VLAs, ie arrays with non-constant bounds.

We need to take care to handle the case of a consteval function (which
are specified to be only immediately called in non-constant contexts)
that appear in arrays bounds.

This introduces `Sema::isAlwayConstantEvaluatedContext`, and a flag in
ExpressionEvaluationContextRecord, such that immediate functions in
array bounds are always immediately invoked.

Sema had both `isConstantEvaluatedContext` and
`isConstantEvaluated`, so I took the opportunity to cleanup that.

The change in `TimeProfilerTest.cpp` is an unfortunate manifestation of
the problem that #66203 seeks to address.

Fixes #65520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen confirmed Verified by a second party consteval C++20 consteval crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants