Bug type: crash
Component: GlobalOpt
Bug location: llvm/lib/Transforms/IPO/GlobalOpt.cpp: OptimizeNonTrivialIFuncs
Issue title: clang-21 crashed with ifunc attribute at O2 and above. Assertion `!Callees.empty() && "Expecting successful collection of versions"' failed.
Labels: ipo
crash
Reported by: iamanonymouscs

clang-21 crashed with `ifunc` attribute at `O2` and above.

Compiler explorer: https://godbolt.org/z/hbh1odEE6

```
$cat mutant.c
void a() __attribute__((ifunc("resolve_do_it_right_at_runtime")));
void *resolve_do_it_right_at_runtime() {
  for (;;)
    a();
}
```

```
$clang-21 -O2 mutant.c
clang: /root/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp:2722: bool OptimizeNonTrivialIFuncs(llvm::Module&, llvm::function_ref<llvm::TargetTransformInfo&(llvm::Function&)>): Assertion `!Callees.empty() && "Expecting successful collection of versions"' 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: /opt/compiler-explorer/clang-assertions-trunk/bin/clang -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -O2 <source>
1.	<eof> parser at end of file
2.	Optimizer
3.	Running pass "globalopt" on module "<source>"
 #0 0x0000000003e828c8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3e828c8)
 #1 0x0000000003e80554 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3e80554)
 #2 0x0000000003dc53e8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000071370b642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000071370b6969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x000071370b642476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x000071370b6287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000071370b62871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x000071370b639e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00000000057199d2 OptimizeNonTrivialIFuncs(llvm::Module&, llvm::function_ref<llvm::TargetTransformInfo& (llvm::Function&)>) (.constprop.0) GlobalOpt.cpp:0:0
#10 0x000000000572109e optimizeGlobalsInModule(llvm::Module&, llvm::DataLayout const&, llvm::function_ref<llvm::TargetLibraryInfo& (llvm::Function&)>, llvm::function_ref<llvm::TargetTransformInfo& (llvm::Function&)>, llvm::function_ref<llvm::BlockFrequencyInfo& (llvm::Function&)>, llvm::function_ref<llvm::DominatorTree& (llvm::Function&)>, llvm::function_ref<void (llvm::Function&)>, llvm::function_ref<void (llvm::Function&)>) (.constprop.0) GlobalOpt.cpp:0:0
#11 0x0000000005721d5f llvm::GlobalOptPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5721d5f)
#12 0x0000000005554c6e llvm::detail::PassModel<llvm::Module, llvm::GlobalOptPass, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5554c6e)
#13 0x0000000003821f90 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3821f90)
#14 0x0000000004134d24 (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>>&, clang::BackendConsumer*) BackendUtil.cpp:0:0
#15 0x000000000413856b clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x413856b)
#16 0x000000000483dd90 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x483dd90)
#17 0x000000000649d5ec clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x649d5ec)
#18 0x000000000483e198 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x483e198)
#19 0x0000000004b0dc75 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4b0dc75)
#20 0x0000000004a8fdbe clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4a8fdbe)
#21 0x0000000004bfd99e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4bfd99e)
#22 0x0000000000d4f95f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd4f95f)
#23 0x0000000000d4711a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#24 0x00000000048866f9 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
#25 0x0000000003dc5884 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3dc5884)
#26 0x0000000004886d0f 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
#27 0x000000000484977d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x484977d)
#28 0x000000000484a80e 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+0x484a80e)
#29 0x0000000004851ee5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4851ee5)
#30 0x0000000000d4c758 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd4c758)
#31 0x0000000000c13f14 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc13f14)
#32 0x000071370b629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#33 0x000071370b629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#34 0x0000000000d46bc5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd46bc5)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```


Fix patch:
commit cc052667b4b2f3bed12db68da876626e14f231f8
Author: Justin Riddell <arghnews@hotmail.co.uk>
Date:   Wed Sep 10 03:32:52 2025 +0100

    [GlobalOpt] Fix unreachable ifunc globalopt crash (#157332) (#157593)
    
    Also fixes (#131488)
    
    Unreachable case is triggering `Callees.empty()` assert. Since this was
    [originally
    ](https://github.com/llvm/llvm-project/pull/87939/commits/02bd5a7013c558f1e5220fc89bafa68f40276549#diff-06aba0dac2a263dc14297a15655291d5506b760f54a736385bcf3208f83df843R2524)
    a `continue` anyway, have applied that as a fix and added a test case.
    Please let me know if there's a better way.
    
    Not sure who/how to get folks to review, tagging a few people (apologies
    if you're not the right person/this is the wrong way to do it, please
    let me know what to do in future if so)
    
    @labrinea @dtcxzyw @nikic @fhahn

diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index d7edd1288309..f88d51f443bc 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2551,7 +2551,8 @@ static bool OptimizeNonTrivialIFuncs(
         }))
       continue;
 
-    assert(!Callees.empty() && "Expecting successful collection of versions");
+    if (Callees.empty())
+      continue;
 
     LLVM_DEBUG(dbgs() << "Statically resolving calls to function "
                       << Resolver->getName() << "\n");


Regression tests:
file: llvm/test/Transforms/GlobalOpt/resolve-indirect-ifunc.ll
commands: opt --passes=globalopt -o - -S < %s
tests: test_name: <module>
test_body: 
define ptr @f1() {
;
  unreachable
}

@i1 = ifunc void(), ptr @f1