[llvm] [MergeFunctions] Add debug locations to redirected calls (PR #225625)

Mian Miftah via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 30 21:00:14 PDT 2026


mmiftahx wrote:

Makes sense to leave it as the function scope then.

> Is there really no possibility of preserving any scope or source information for these merged call sites?

Not for the call's own scope. A call that already has a location keeps it, inlined-at chain included so only the callee changes. The calls this patch touches have no location, so nothing in the IR says which scope they belong to. If the call got there by inlining, the inliner was the last place that knew: with inline line tables (the default), when the inlined function has debug info, it leaves instructions without a location as they are. It could give such calls a line 0 location in the inlined function's scope, with the call site's inlined-at chain, which would keep the frame it knows about at that point but can't bring back anything lost earlier. That would be a separate patch to the inliner, though.

> Given that is merging functions, is this similar to linker ICF (identical code folding)?

Yes, it's ICF on IR. With MergeFunctions, a call to the function that was merged away shows up in a backtrace under the name and lines of the one that was kept. Linker ICF is similar, though a debugger can mix the two: after `ld.lld --icf=all` on two identical functions f and g, gdb showed a call to g as f with g's line.

One difference from the linker is that MergeFunctions runs before DWARF is emitted. After lld's ICF the DWARF still has a subprogram for each function, and the call site entry still names the original callee. Here the merged function's subprogram is dropped and the call site entry names the kept function. MergeFunctions does know the original callee when it redirects the call, but nothing in the IR can carry that to the DWARF, so expressing it would need something new.

The pass has a hidden `-mergefunc-preserve-debug-info` flag that keeps the merged function as a thunk and doesn't redirect its callers, but it's off by default and hits an assertion in `writeThunk` on weak and linkonce_odr pairs.

https://github.com/llvm/llvm-project/pull/225625


More information about the llvm-commits mailing list