https://github.com/vogelsgesang updated
https://github.com/llvm/llvm-project/pull/141889
>From 700c1c465ff7f29dc980356f36844a12d580 Mon Sep 17 00:00:00 2001
From: Adrian Vogelsgesang
Date: Tue, 27 May 2025 23:50:18 +
Subject: [PATCH 1/3] [debuginfo][coro] Fix linkage name for clones of coro
functions
So far, the `DW_AT_linkage_name` of the coroutine `resume`, `destroy`,
`cleanup` and `noalloc` functions were incorrectly set to the original
function name instead of the updated function names.
With this commit, we now update the `DW_AT_linkage_name` to the correct
name. This has multiple benefits:
1. it's easier for me (and other developers) to understand the output of
`llvm-dwarf-dump` when coroutines are involved.
2. When hitting a breakpoint, both LLDB and GDB now tell you which clone
of the function you are in. E.g., GDB now prints
"Breakpoint 1.2, coro_func(int) [clone .resume] (v=43) at ..."
instead of
"Breakpoint 1.2, coro_func(int) (v=43) at ...".
3. GDB's `info line coro_func` command now allows you to distinguish the
multiple different clones of the function.
In Swift, the linkage names of the clones were already updated. The
comment right above the relevant code in `CoroSplit.cpp` already hinted
that the linkage name should probably also be updated in C++. This
commit was added in commit 6ce76ff7eb7640, and back then the
corresponding `DW_AT_specification` (i.e., `SP->getDeclaration()`) was
not updated, yet, which led to problems for C++. In the meantime, commit
ca1a5b37c7236d added code to also update `SP->getDeclaration`, as such
there is no reason anymore to not update the linkage name for C++.
Note that most test cases used inconsistent function names for the LLVM
function vs. the DISubprogram linkage name. clang would never emit such
LLVM IR. This confused me initially, and hence I fixed it while updating
the test case.
---
clang/lib/CodeGen/CGVTables.cpp | 2 +-
llvm/lib/Transforms/Coroutines/CoroSplit.cpp | 31 +--
...coro-debug-dbg.values-not_used_in_frame.ll | 6 ++--
.../Coroutines/coro-debug-dbg.values.ll | 10 +++---
.../Coroutines/coro-debug-frame-variable.ll | 10 +++---
llvm/test/Transforms/Coroutines/coro-debug.ll | 18 +--
6 files changed, 31 insertions(+), 46 deletions(-)
diff --git a/clang/lib/CodeGen/CGVTables.cpp b/clang/lib/CodeGen/CGVTables.cpp
index c7447273a42fa..2897ccdf88660 100644
--- a/clang/lib/CodeGen/CGVTables.cpp
+++ b/clang/lib/CodeGen/CGVTables.cpp
@@ -124,7 +124,7 @@ static void resolveTopLevelMetadata(llvm::Function *Fn,
auto *DIS = Fn->getSubprogram();
if (!DIS)
return;
- auto *NewDIS = DIS->replaceWithDistinct(DIS->clone());
+ auto *NewDIS = llvm::MDNode::replaceWithDistinct(DIS->clone());
VMap.MD()[DIS].reset(NewDIS);
// Find all llvm.dbg.declare intrinsics and resolve the DILocalVariable nodes
diff --git a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
index f9a6c70fedc2d..07d888a10eb7f 100644
--- a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
@@ -912,29 +912,14 @@ void coro::BaseCloner::create() {
assert(SP != OrigF.getSubprogram() && SP->isDistinct());
updateScopeLine(ActiveSuspend, *SP);
-// Update the linkage name to reflect the modified symbol name. It
-// is necessary to update the linkage name in Swift, since the
-// mangling changes for resume functions. It might also be the
-// right thing to do in C++, but due to a limitation in LLVM's
-// AsmPrinter we can only do this if the function doesn't have an
-// abstract specification, since the DWARF backend expects the
-// abstract specification to contain the linkage name and asserts
-// that they are identical.
-if (SP->getUnit() &&
-SP->getUnit()->getSourceLanguage() == dwarf::DW_LANG_Swift) {
- SP->replaceLinkageName(MDString::get(Context, NewF->getName()));
- if (auto *Decl = SP->getDeclaration()) {
-auto *NewDecl = DISubprogram::get(
-Decl->getContext(), Decl->getScope(), Decl->getName(),
-NewF->getName(), Decl->getFile(), Decl->getLine(), Decl->getType(),
-Decl->getScopeLine(), Decl->getContainingType(),
-Decl->getVirtualIndex(), Decl->getThisAdjustment(),
-Decl->getFlags(), Decl->getSPFlags(), Decl->getUnit(),
-Decl->getTemplateParams(), nullptr, Decl->getRetainedNodes(),
-Decl->getThrownTypes(), Decl->getAnnotations(),
-Decl->getTargetFuncName());
-SP->replaceDeclaration(NewDecl);
- }
+// Update the linkage name and the functaion name to reflect the modified
+// name.
+MDString *NewLinkageName = MDString::get(Context, NewF->getName());
+SP->replaceLinkageName(NewLinkageName);
+if (DISubprogram *Decl = SP->getDeclaration()) {
+ TempDISubprogram NewDecl = Decl->clone();
+ NewDecl->repla