OpenCilk / opencilk-project

Monorepo for the OpenCilk compiler. Forked from llvm/llvm-project and based on Tapir/LLVM.
Other
90 stars 29 forks source link

Compiler crash with CSI, LTO, and optimization level >0 #149

Closed ailiop closed 11 months ago

ailiop commented 1 year ago

Describe the bug

The compiler crashes when compiling a C/C++ code with -fcsi -flto -O1 (or higher optimization level).

Expected behavior

No crash.

OpenCilk version

Built from source:

System information

Working example code

/* hello.c */
#include <stdio.h>
int main() {
    printf("hello\n");
    return 0;
}

Steps to reproduce (include relevant output)

$ /opt/opencilk-2/bin/clang -c hello.c -fcsi -flto -O1 -g -o hello.o
clang: /home/ailiop/opencilk/opencilk-project/llvm/include/llvm/ADT/StringRef.h:256: char llvm::StringRef::operator[](size_t) const: Assertion `Index < Length && "Invalid index!"' failed.
PLEASE submit a bug report to https://github.com/OpenCilk/opencilk-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/opencilk-2/bin/clang -c hello.c -fcsi -flto -O1 -g -o hello.o
1.      <eof> parser at end of file
2.      Optimizer
 #0 0x0000000003e2e6f3 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/opencilk-2/bin/clang+0x3e2e6f3)
 #1 0x0000000003e2c42e llvm::sys::RunSignalHandlers() (/opt/opencilk-2/bin/clang+0x3e2c42e)
 #2 0x0000000003e2da8d llvm::sys::CleanupOnSignal(unsigned long) (/opt/opencilk-2/bin/clang+0x3e2da8d)
 #3 0x0000000003da06a3 (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) CrashRecoveryContext.cpp:0:0
 #4 0x0000000003da084e CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #5 0x00007f4adfde6420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #6 0x00007f4adf87900b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #7 0x00007f4adf858859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #8 0x00007f4adf858729 get_sysdep_segment_value /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:509:8
 #9 0x00007f4adf858729 _nl_load_domain /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:970:34
#10 0x00007f4adf869fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
#11 0x000000000355432c llvm::GlobalValue::getGlobalIdentifier[abi:cxx11](llvm::StringRef, llvm::GlobalValue::LinkageTypes, llvm::StringRef) (/opt/opencilk-2/bin/clang+0x355432c)
#12 0x0000000003554375 llvm::GlobalValue::getGlobalIdentifier[abi:cxx11]() const (/opt/opencilk-2/bin/clang+0x3554375)
#13 0x0000000002eee12d llvm::ModuleSummaryIndex::getOrInsertValueInfo(llvm::GlobalValue const*) StackSafetyAnalysis.cpp:0:0
#14 0x0000000002f0ad91 findRefEdges(llvm::ModuleSummaryIndex&, llvm::User const*, llvm::SetVector<llvm::ValueInfo, std::vector<llvm::ValueInfo, std::allocator<llvm::ValueInfo> >, llvm::DenseSet<llvm::ValueInfo, llvm::DenseMapInfo<llvm::ValueInfo, void> > >&, llvm::SmallPtrSet<llvm::User const*, 8u>&) ModuleSummaryAnalysis.cpp:0:0
#15 0x0000000002f04e26 llvm::buildModuleSummaryIndex(llvm::Module const&, std::function<llvm::BlockFrequencyInfo* (llvm::Function const&)>, llvm::ProfileSummaryInfo*, std::function<llvm::StackSafetyInfo const* (llvm::Function const&)>) (/opt/opencilk-2/bin/clang+0x2f04e26)
#16 0x0000000002f09c19 llvm::ModuleSummaryIndexAnalysis::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/opencilk-2/bin/clang+0x2f09c19)
#17 0x0000000005189442 llvm::detail::AnalysisPassModel<llvm::Module, llvm::ModuleSummaryIndexAnalysis, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) PassBuilder.cpp:0:0
#18 0x00000000035d9320 llvm::AnalysisManager<llvm::Module>::getResultImpl(llvm::AnalysisKey*, llvm::Module&) (/opt/opencilk-2/bin/clang+0x35d9320)
#19 0x00000000052de45e llvm::BitcodeWriterPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/opencilk-2/bin/clang+0x52de45e)
#20 0x000000000419d64d llvm::detail::PassModel<llvm::Module, llvm::BitcodeWriterPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) BackendUtil.cpp:0:0
#21 0x00000000035d5fb4 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/opencilk-2/bin/clang+0x35d5fb4)
#22 0x0000000004194f86 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >&, std::unique_ptr<llvm::ToolOutputFile, std::default_delete<llvm::ToolOutputFile> >&) BackendUtil.cpp:0:0
#23 0x0000000004187c0a clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) (/opt/opencilk-2/bin/clang+0x4187c0a)
#24 0x0000000004ae59b4 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) CodeGenAction.cpp:0:0
#25 0x0000000005b1f913 clang::ParseAST(clang::Sema&, bool, bool) (/opt/opencilk-2/bin/clang+0x5b1f913)
#26 0x0000000004a23460 clang::FrontendAction::Execute() (/opt/opencilk-2/bin/clang+0x4a23460)
#27 0x0000000004997ab8 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/opencilk-2/bin/clang+0x4997ab8)
#28 0x0000000004adf0d3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/opencilk-2/bin/clang+0x4adf0d3)
#29 0x000000000270536f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/opencilk-2/bin/clang+0x270536f)
#30 0x0000000002703190 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#31 0x000000000480e1a2 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const::$_1>(long) Job.cpp:0:0
#32 0x0000000003da05c1 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/opencilk-2/bin/clang+0x3da05c1)
#33 0x000000000480dbb0 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const (/opt/opencilk-2/bin/clang+0x480dbb0)
#34 0x00000000047d2e44 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/opt/opencilk-2/bin/clang+0x47d2e44)
#35 0x00000000047d3387 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (/opt/opencilk-2/bin/clang+0x47d3387)
#36 0x00000000047ee6b8 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (/opt/opencilk-2/bin/clang+0x47ee6b8)
#37 0x0000000002702ab9 main (/opt/opencilk-2/bin/clang+0x2702ab9)
#38 0x00007f4adf85a083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#39 0x000000000270003e _start (/opt/opencilk-2/bin/clang+0x270003e)
clang-14: error: clang frontend command failed with exit code 134 (use -v to see invocation)
clang version 14.0.6 (git@github.com:OpenCilk/opencilk-project.git f54e98722992462e042f0686c74f710277c92cdb)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/opencilk-2/bin
clang-14: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-14: note: diagnostic msg: /tmp/hello-04ea94.c
clang-14: note: diagnostic msg: /tmp/hello-04ea94.sh
clang-14: note: diagnostic msg:

********************

(Attached: hello-04ea94.zip, which contains the crash diagnostic files produced by clang.)

Additional comments

This issue does not arise with optimization level -O0. It also does not arise with OpenCilk 1.0 at any optimization level.

neboat commented 1 year ago

I think I've encountered this issue before. If you have a source-build of OpenCilk around, can you please check if the following patch resolves the issue?

diff --git a/llvm/lib/Transforms/Instrumentation/ComprehensiveStaticInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/ComprehensiveStaticInstrumentation.cpp
index 9e0828dbb968..f847eed6f1fa 100644
--- a/llvm/lib/Transforms/Instrumentation/ComprehensiveStaticInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/ComprehensiveStaticInstrumentation.cpp
@@ -2002,7 +2002,7 @@ CallInst *CSIImpl::createRTUnitInitCall(IRBuilder<> &IRB) {
   // Insert call to __csirt_unit_init
   return IRB.CreateCall(
       RTUnitInit,
-      {IRB.CreateGlobalStringPtr(M.getName()),
+      {IRB.CreateGlobalStringPtr(M.getName(), "__csi_module_name"),
        ConstantExpr::getGetElementPtr(FEDGV->getValueType(), FEDGV, GepArgs),
        ConstantExpr::getGetElementPtr(SizeGV->getValueType(), SizeGV, GepArgs),
        InitCallsiteToFunction});

I just noticed the other day I had this change in a local copy of OpenCilk but had not pushed it yet.

ailiop commented 1 year ago

Thank you, that seems to fix the issue, indeed! I will let you know if I run into subsequent problems but I do not expect any.

neboat commented 1 year ago

Thanks for the confirmation. I'll work on pushing that change.