llvm / llvm/llvm-project

Assertion "FPConstrained should be enabled on entire function" failed if -ftrapping-math differs from PCH

Open
#183,643 4 comments 0 reactions 0 assignees View on GitHub
clang:PCH crash-on-valid
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This is reduced from an error I saw on Windows while building LLVM itself with Clang. The reduced test can also demonstrate the issue on other platforms when cross-compiling.

`$ cat cmake_pch.hxx`

```c++
template
To *cast_convert_val(From Val);
template
To *doCast(From f) {
return cast_convert_val(const_cast(f));
}
template
To *cast(From Val) {
return doCast(Val);
}
class Value {
};
class Constant : public Value {
};
class ConstantExpr : public Constant {
};
```

`$ cat cmake_pch.hxx.cxx`
`$ cat ConstantFolding.cpp`

```c++
ConstantExpr *Foo(Constant *C) {
return cast(C);
}
```

```console
$ /home/harald/source/llvm-project/build/bin/clang++ --target=x86_64-pc-windows-msvc -Xclang -emit-pch -Xclang -include -Xclang cmake_pch.hxx -x c++-header -o cmake_pch.hxx.pch -c cmake_pch.hxx.cxx
$ /home/harald/source/llvm-project/build/bin/clang++ --target=x86_64-pc-windows-msvc -ftrapping-math -include-pch cmake_pch.hxx.pch -include cmake_pch.hxx -o ConstantFolding.cpp.obj -c ConstantFolding.cpp
clang++: /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:168: void clang::CodeGen::CodeGenFunction::CGFPOptionsRAII::ConstructorHelper(clang::FPOptions): Assertion `(CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() || isa(CGF.CurFuncDecl) || isa(CGF.CurFuncDecl) || (NewExceptionBehavior == llvm::fp::ebIgnore && NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) && "FPConstrained should be enabled on entire function"' failed.
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: /home/harald/source/llvm-project/build/bin/clang++ --target=x86_64-pc-windows-msvc -ftrapping-math -include-pch cmake_pch.hxx.pch -include cmake_pch.hxx -o ConstantFolding.cpp.obj -c ConstantFolding.cpp
1. parser at end of file
2. Per-file LLVM IR generation
3. /home/harald/bug/cmake_pch.hxx:4:7: Generating code for declaration 'doCast'
#0 0x00007f6a9306e988 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/harald/source/llvm-project/llvm/lib/Support/Unix/Signals.inc:884:3
#1 0x00007f6a9306bba4 llvm::sys::RunSignalHandlers() /home/harald/source/llvm-project/llvm/lib/Support/Signals.cpp:108:20
#2 0x00007f6a92f6f460 HandleCrash /home/harald/source/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:73:5
#3 0x00007f6a92f6f460 CrashRecoverySignalHandler(int) /home/harald/source/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:393:62
#4 0x00007f6a91c4aa70 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x40a70)
#5 0x00007f6a91ca13bc (/usr/lib/x86_64-linux-gnu/libc.so.6+0x973bc)
#6 0x00007f6a91c4a942 raise (/usr/lib/x86_64-linux-gnu/libc.so.6+0x40942)
#7 0x00007f6a91c324ac abort (/usr/lib/x86_64-linux-gnu/libc.so.6+0x284ac)
#8 0x00007f6a91c32420 __assert_perror_fail (/usr/lib/x86_64-linux-gnu/libc.so.6+0x28420)
#9 0x00007f6a9e036b4b llvm::IRBuilderBase::setDefaultConstrainedExcept(llvm::fp::ExceptionBehavior) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:168:3
#10 0x00007f6a9e036b4b clang::CodeGen::CodeGenFunction::CGFPOptionsRAII::ConstructorHelper(clang::FPOptions) (.cold) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:164:42
#11 0x00007f6a9fb76d32 clang::CastExpr::getCastKind() const /home/harald/source/llvm-project/clang/include/clang/AST/Expr.h:3723:65
#12 0x00007f6a9fb76d32 (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:2589:34
#13 0x00007f6a9fb74bf7 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:491:3
#14 0x00007f6a9fb76e57 (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:2893:17
#15 0x00007f6a9fb74c7f Visit /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:491:3
#16 0x00007f6a9fb74c7f clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:6099:13
#17 0x00007f6a9fae93ef clang::CodeGen::RValue::get(llvm::Value*) /home/harald/source/llvm-project/clang/lib/CodeGen/CGValue.h:103:19
#18 0x00007f6a9fae93ef clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExpr.cpp:278:23
#19 0x00007f6a9fae9847 clang::CodeGen::CodeGenFunction::EmitAnyExprToTemp(clang::Expr const*) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExpr.cpp:298:1
#20 0x00007f6a9fa36603 clang::CodeGen::CodeGenFunction::EmitCallArg(clang::CodeGen::CallArgList&, clang::Expr const*, clang::QualType) /home/harald/source/llvm-project/clang/lib/CodeGen/CGCall.cpp:5074:11
#21 0x00007f6a9fa37b30 clang::CodeGen::CodeGenFunction::EmitCallArgs(clang::CodeGen::CallArgList&, clang::CodeGen::CodeGenFunction::PrototypeWrapper, llvm::iterator_range>, clang::CodeGen::CodeGenFunction::AbstractCallee, unsigned int, clang::CodeGen::CodeGenFunction::EvaluationOrder) /home/harald/source/llvm-project/clang/lib/CodeGen/CGCall.cpp:4917:5
#22 0x00007f6a9fb1b2bd std::__uniq_ptr_impl>::_M_ptr() const /usr/include/c++/15/bits/unique_ptr.h:192:67
#23 0x00007f6a9fb1b2bd std::unique_ptr>::get() const /usr/include/c++/15/bits/unique_ptr.h:472:27
#24 0x00007f6a9fb1b2bd std::unique_ptr>::operator*() const /usr/include/c++/15/bits/unique_ptr.h:455:2
#25 0x00007f6a9fb1b2bd clang::CodeGen::CodeGenModule::getTypes() /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.h:878:38
#26 0x00007f6a9fb1b2bd clang::CodeGen::CodeGenFunction::EmitCall(clang::QualType, clang::CodeGen::CGCallee const&, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::Value*, llvm::CallBase**, clang::CodeGen::CGFunctionInfo const**) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExpr.cpp:7011:46
#27 0x00007f6a9fb1c87d clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExpr.cpp:6413:50
#28 0x00007f6a9fb7ab20 clang::CodeGen::RValue::getScalarVal() const /home/harald/source/llvm-project/clang/lib/CodeGen/CGValue.h:73:5
#29 0x00007f6a9fb7ab20 VisitCallExpr /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:679:48
#30 0x00007f6a9fb7ab20 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:675:10
#31 0x00007f6a9fb74c7f Visit /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:491:3
#32 0x00007f6a9fb74c7f clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) /home/harald/source/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:6099:13
#33 0x00007f6a9fce5df3 clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) /home/harald/source/llvm-project/clang/lib/CodeGen/CGStmt.cpp:1658:40
#34 0x00007f6a9fcf6963 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) /home/harald/source/llvm-project/clang/lib/CodeGen/CGStmt.cpp:587:10
#35 0x00007f6a9fd64057 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:1376:1
#36 0x00007f6a9fd78ab4 clang::CodeGen::CodeGenModule::getLangOpts() const /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.h:851:51
#37 0x00007f6a9fd78ab4 clang::CodeGen::CodeGenFunction::getLangOpts() const /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenFunction.h:2192:66
#38 0x00007f6a9fd78ab4 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:1632:18
#39 0x00007f6a9fdcb66e clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:6707:3
#40 0x00007f6a9fdc64c5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:4727:47
#41 0x00007f6a9fdd136a clang::CodeGen::CodeGenModule::EmitDeferred() /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:3770:34
#42 0x00007f6a9fdd1386 clang::CodeGen::CodeGenModule::EmitDeferred() /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:3772:7
#43 0x00007f6a9fdd350b clang::CodeGen::CodeGenModule::Release() /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:977:29
#44 0x00007f6a9fe50e9c (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) /home/harald/source/llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp:297:11
#45 0x00007f6a9fd5e353 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) /home/harald/source/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:238:31
#46 0x00007f6a9e3b6744 clang::ParseAST(clang::Sema&, bool, bool) /home/harald/source/llvm-project/clang/lib/Parse/ParseAST.cpp:183:34
#47 0x00007f6aa068dee1 clang::FrontendAction::Execute() /home/harald/source/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1326:38
#48 0x00007f6aa0618192 llvm::Error::getPtr() const /home/harald/source/llvm-project/llvm/include/llvm/Support/Error.h:278:51
#49 0x00007f6aa0618192 llvm::Error::operator bool() /home/harald/source/llvm-project/llvm/include/llvm/Support/Error.h:241:22
#50 0x00007f6aa0618192 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/harald/source/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1007:42
#51 0x00007f6aa072765a std::__shared_ptr::get() const /usr/include/c++/15/bits/shared_ptr_base.h:1673:16
#52 0x00007f6aa072765a std::__shared_ptr_access::_M_get() const /usr/include/c++/15/bits/shared_ptr_base.h:1370:69
#53 0x00007f6aa072765a std::__shared_ptr_access::operator*() const /usr/include/c++/15/bits/shared_ptr_base.h:1358:40
#54 0x00007f6aa072765a clang::CompilerInvocation::getFrontendOpts() /home/harald/source/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h:277:48
#55 0x00007f6aa072765a clang::CompilerInstance::getFrontendOpts() /home/harald/source/llvm-project/clang/include/clang/Frontend/CompilerInstance.h:301:39
#56 0x00007f6aa072765a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/harald/source/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:311:29
#57 0x000055f14e2a57f7 cc1_main(llvm::ArrayRef, char const*, void*) /home/harald/source/llvm-project/clang/tools/driver/cc1_main.cpp:304:40
#58 0x000055f14e29ddc8 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) /home/harald/source/llvm-project/clang/tools/driver/driver.cpp:229:20
#59 0x000055f14e29e07a std::__atomic_base::fetch_sub(int, std::memory_order) /usr/include/c++/15/bits/atomic_base.h:641:34
#60 0x000055f14e29e07a llvm::ThreadSafeRefCountedBase::Release() const /home/harald/source/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:134:41
#61 0x000055f14e29e07a llvm::IntrusiveRefCntPtrInfo::release(llvm::vfs::FileSystem*) /home/harald/source/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:164:45
#62 0x000055f14e29e07a llvm::IntrusiveRefCntPtr::release() /home/harald/source/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:233:41
#63 0x000055f14e29e07a llvm::IntrusiveRefCntPtr::~IntrusiveRefCntPtr() /home/harald/source/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:196:34
#64 0x000055f14e29e07a operator() /home/harald/source/llvm-project/clang/tools/driver/driver.cpp:379:26
#65 0x000055f14e29e07a int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) /home/harald/source/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:46:52
#66 0x00007f6aa023fa89 operator() /home/harald/source/llvm-project/clang/lib/Driver/Job.cpp:442:32
#67 0x00007f6aa023fa89 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) /home/harald/source/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:46:52
#68 0x00007f6a92f6f5c1 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) /home/harald/source/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:438:10
#69 0x00007f6aa0240354 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) /home/harald/source/llvm-project/clang/lib/Driver/Job.cpp:446:10
#70 0x00007f6aa01f9819 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const /home/harald/source/llvm-project/clang/lib/Driver/Compilation.cpp:196:22
#71 0x00007f6aa01f9e67 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const /home/harald/source/llvm-project/clang/lib/Driver/Compilation.cpp:246:5
#72 0x00007f6aa020ec85 llvm::SmallVectorBase::empty() const /home/harald/source/llvm-project/llvm/include/llvm/ADT/SmallVector.h:83:46
#73 0x00007f6aa020ec85 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) /home/harald/source/llvm-project/clang/lib/Driver/Driver.cpp:2330:28
#74 0x000055f14e2a1437 llvm::SmallVectorBase::size() const /home/harald/source/llvm-project/llvm/include/llvm/ADT/SmallVector.h:80:32
#75 0x000055f14e2a1437 llvm::SmallVectorTemplateCommon, void>::end() /home/harald/source/llvm-project/llvm/include/llvm/ADT/SmallVector.h:275:41
#76 0x000055f14e2a1437 clang_main(int, char**, llvm::ToolContext const&) /home/harald/source/llvm-project/clang/tools/driver/driver.cpp:421:26
#77 0x000055f14e29d0e5 main /home/harald/source/llvm-project/build/tools/clang/tools/driver/clang-driver.cpp:18:1
#78 0x00007f6a91c33f75 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29f75)
#79 0x00007f6a91c34027 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a027)
#80 0x000055f14e29d121 _start (/home/harald/source/llvm-project/build/bin/clang+++0xf121)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 23.0.0git (https://github.com/llvm/llvm-project 30969cc6441261a4281bdb9811c36a2348137d61)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: /home/harald/source/llvm-project/build/bin
Build config: +assertions
clang++: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /tmp/ConstantFolding-b6a6b3.cpp
clang++: note: diagnostic msg: /tmp/ConstantFolding-b6a6b3.sh
clang++: note: diagnostic msg:

********************
Aborted (core dumped)
```

The generated `/tmp/ConstantFolding-b6a6b3.cpp` and `/tmp/ConstantFolding-b6a6b3.sh` do not reproduce the issue.

There are two aspects to this:

- Clang should not crash. I do not know what the intended behaviour is though: I do not know whether it should manage to use the precompiled header, silently skip it, or reject it with a clean error message. I have marked this as crash-on-valid, but if an error message is expected, that would make it crash-on-invalid instead.
- LLVM's build system attempts to not use PCHs in those cases where they cannot be used (see [`llvm_update_pch`](https://github.com/llvm/llvm-project/blob/e55945556a1e99a7d6618735873d513a0239b2a0/llvm/cmake/modules/AddLLVM.cmake#L82)). Depending on the intended behaviour, if PCHs are not meant to work here, the build system should not use them.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.