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

Slow compilation for long sequence of loops

Open dbakunevich opened this issue 4 years ago • 4 comments

This bug was found by Azul FuzzGen fuzzer for CPP.

Godbolt link: https://godbolt.org/z/c78bn3Tx9

Run clang -O3 (x86_64) on the test crash_2.cpp.txt

Passed on clang 14.0, so appears to be fresh.

Failure looks like:

fatal error: error in backend: Instruction Combining seems stuck in an infinite loop after 100 iterations.
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 -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -O3 <source>
1.	<eof> parser at end of file
2.	Optimizer
 #0 0x000055d7e88e3e2f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055d7e88e1b94 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3ae1b94)
 #2 0x000055d7e881793f llvm::CrashRecoveryContext::HandleExit(int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a1793f)
 #3 0x000055d7e88da28e llvm::sys::Process::Exit(int, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3ada28e)
 #4 0x000055d7e62325f3 LLVMErrorHandler(void*, char const*, bool) cc1_main.cpp:0:0
 #5 0x000055d7e882029f llvm::report_fatal_error(llvm::Twine const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a2029f)
 #6 0x000055d7e83047d2 combineInstructionsOverFunction(llvm::Function&, llvm::InstructionWorklist&, llvm::AAResults*, llvm::AssumptionCache&, llvm::TargetLibraryInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::OptimizationRemarkEmitter&, llvm::BlockFrequencyInfo*, llvm::ProfileSummaryInfo*, unsigned int, llvm::LoopInfo*) InstructionCombining.cpp:0:0
 #7 0x000055d7e8304e42 llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3504e42)
 #8 0x000055d7e99e4671 llvm::detail::PassModel<llvm::Function, llvm::InstCombinePass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4be4671)
 #9 0x000055d7e80a4dec llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x32a4dec)
#10 0x000055d7e64ff681 llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function> >, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x16ff681)
#11 0x000055d7e80a480a llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x32a480a)
#12 0x000055d7e64fffe1 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x16fffe1)
#13 0x000055d7e80a2fc4 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x32a2fc4)
#14 0x000055d7e8c2a6a7 (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> >&) (.constprop.777) BackendUtil.cpp:0:0
#15 0x000055d7e8c2d58a 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/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3e2d58a)
#16 0x000055d7e9965ecb clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b65ecb)
#17 0x000055d7ea9248f9 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5b248f9)
#18 0x000055d7e9964a00 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b64a00)
#19 0x000055d7e92e5e29 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44e5e29)
#20 0x000055d7e927f3ca clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x447f3ca)
#21 0x000055d7e93b3c83 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45b3c83)
#22 0x000055d7e6233981 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1433981)
#23 0x000055d7e622f819 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#24 0x000055d7e9115a25 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::'lambda'()>(long) Job.cpp:0:0
#25 0x000055d7e8817783 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a17783)
#26 0x000055d7e9117db6 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/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4317db6)
#27 0x000055d7e90e83da clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42e83da)
#28 0x000055d7e90e906f clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42e906f)
#29 0x000055d7e90f366a clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42f366a)
#30 0x000055d7e6177b7c main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1377b7c)
#31 0x00007f5b2d65c0b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b3)
#32 0x000055d7e622f38a _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x142f38a)
clang-15: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Compiler returned: 70

dbakunevich avatar May 01 '22 18:05 dbakunevich

maybe repeat to https://github.com/llvm/llvm-project/issues/51139

vfdff avatar May 03 '22 11:05 vfdff

Still hangs.

fhahn avatar Aug 22 '22 19:08 fhahn

The problem here seems related to a PHI optimization, similar to #55788. The following is the only change in iteration 100.

INSTCOMBINE ITERATION #100 on _ZN1a1gEh
...
IC: Visiting:   br label %for.body8.8.20
IC: Visiting:   %add27.8.20 = phi i32 [ %add.8.19, %for.cond5.for.cond.cleanup7_crit_edge.8.19 ], [ %add.8.20, %for.body8.8.20 ]
IC: Replacing   %add27.8.20 = phi i32 [ %add.8.19, %for.cond5.for.cond.cleanup7_crit_edge.8.19 ], [ %add.8.20, %for.body8.8.20 ]
    with i32 poison
IC: Mod =   %add27.8.20 = phi i32 [ %add.8.19, %for.cond5.for.cond.cleanup7_crit_edge.8.19 ], [ %add.8.20, %for.body8.8.20 ]
    New =   %add27.8.20 = phi i32 [ %add.8.19, %for.cond5.for.cond.cleanup7_crit_edge.8.19 ], [ %add.8.20, %for.body8.8.20 ]
IC: ERASE   %add27.8.20 = phi i32 [ %add.8.19, %for.cond5.for.cond.cleanup7_crit_edge.8.19 ], [ %add.8.20, %for.body8.8.20 ]
ADD DEFERRED:   %add.8.19 = add nsw i32 %add27.8.19, %3
ADD DEFERRED:   %add.8.20 = add nsw i32 poison, %3
IC: ERASE   %add.8.20 = add nsw i32 poison, %3
ADD DEFERRED:   %3 = load i32, ptr %this, align 4, !tbaa !5
ADD:   %3 = load i32, ptr %this, align 4, !tbaa !5
ADD:   %add.8.19 = add nsw i32 %add27.8.19, %3
IC: Visiting:   %add.8.19 = add nsw i32 %add27.8.19, %3

Iteration 99 just before it looks similar, just with another PHI (and likewise for iteration 98):

INSTCOMBINE ITERATION #99 on _ZN1a1gEh
...
IC: Visiting:   br label %for.body8.8.21
IC: Visiting:   %add27.8.21 = phi i32 [ %add.8.20, %for.cond5.for.cond.cleanup7_crit_edge.8.20 ], [ %add.8.21, %for.body8.8.21 ]
IC: Replacing   %add27.8.21 = phi i32 [ %add.8.20, %for.cond5.for.cond.cleanup7_crit_edge.8.20 ], [ %add.8.21, %for.body8.8.21 ]
    with i32 poison
IC: Mod =   %add27.8.21 = phi i32 [ %add.8.20, %for.cond5.for.cond.cleanup7_crit_edge.8.20 ], [ %add.8.21, %for.body8.8.21 ]
    New =   %add27.8.21 = phi i32 [ %add.8.20, %for.cond5.for.cond.cleanup7_crit_edge.8.20 ], [ %add.8.21, %for.body8.8.21 ]
IC: ERASE   %add27.8.21 = phi i32 [ %add.8.20, %for.cond5.for.cond.cleanup7_crit_edge.8.20 ], [ %add.8.21, %for.body8.8.21 ]
ADD DEFERRED:   %add.8.20 = add nsw i32 %add27.8.20, %3
ADD DEFERRED:   %add.8.21 = add nsw i32 poison, %3
IC: ERASE   %add.8.21 = add nsw i32 poison, %3
ADD DEFERRED:   %3 = load i32, ptr %this, align 4, !tbaa !5
ADD:   %3 = load i32, ptr %this, align 4, !tbaa !5
ADD:   %add.8.20 = add nsw i32 %add27.8.20, %3
IC: Visiting:   %add.8.20 = add nsw i32 %add27.8.20, %3

msebor avatar Aug 25 '22 17:08 msebor

This no longer reproduces even with fix-point verification force enabled, was fixed as part of single-iteration InstCombine work.

However, this sample takes 11s in the backend, attributed to IVUsers (input https://gist.github.com/nikic/47bf2c6abd93e70d5d924dd0dca7fe55). Looks like a long sequence of loops. Maybe there is optimization potential here.

nikic avatar Sep 26 '23 09:09 nikic