[llvm] [CloneFunction][DebugInfo] Ensure DILocalVariables of inlined functions are not cloned (NFC) (PR #138590)
Vladislav Dzhidzhoev via llvm-commits
llvm-commits at lists.llvm.org
Mon May 5 14:31:17 PDT 2025
https://github.com/dzhidzhoev created https://github.com/llvm/llvm-project/pull/138590
This change was separated from https://github.com/llvm/llvm-project/pull/119001.
When cloning functions, use IdentityMDPredicate to ensure that if DISubprogram is not cloned, then its DILocalVariables are not cloned either.
This is currently expected to be an NFC, as DILocalVariables only reference their subprograms (via DILocalScopes) and types, and inlined DISubprograms and DITypes are not cloned. Thus, DILocalVariables are mapped to self in ValueMapper (in mapTopLevelUniquedNode).
However, it will be needed for the original PR https://github.com/llvm/llvm-project/pull/119001, where the type of a DILocalVariable may be cloned if it lies in the scope of a DISubprogram that is being cloned, whereas the DILocalVariable itself belongs to a different DISubprogram.
I'm making this change into a separate PR to make the original PR a bit smaller, and because this has more to do with variables than with types.
>From 285d3e9761faa7279fdd548424a58b7e77520604 Mon Sep 17 00:00:00 2001
From: Vladislav Dzhidzhoev <vdzhidzhoev at accesssoftek.com>
Date: Mon, 5 May 2025 22:28:21 +0200
Subject: [PATCH] [CloneFunction][DebugInfo] Ensure DILocalVariables of inlined
functions are not cloned (NFC)
This change was separated from https://github.com/llvm/llvm-project/pull/119001.
When cloning functions, use IdentityMDPredicate to ensure that
if DISubprogram is not cloned, then its DILocalVariables are not cloned
either.
This is expected to be an NFC currently, as DILocalVariables only reference
their subprograms (via DILocalScopes) and types.
Since inlined DISubprograms and DITypes are not cloned during the process,
DILocalVariables are mapped to self in ValueMapper (in mapTopLevelUniquedNode).
However, it will be needed for the original PR https://github.com/llvm/llvm-project/pull/119001,
where it is possible that the type of a DILocalVariable is cloned
if it lies in the scope of a DISubprogram that is being cloned,
whereas the DILocalVariable itself belongs to a different DISubprogram.
I'm making this change into a separate PR to make the original PR a bit smaller,
and since this has more to do with variables than with types.
---
llvm/lib/Transforms/Utils/CloneFunction.cpp | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp
index 35550642a4365..8f74ddc5d6652 100644
--- a/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -59,17 +59,28 @@ MetadataPredicate createIdentityMDPredicate(const Function &F,
return [](const Metadata *MD) { return false; };
DISubprogram *SPClonedWithinModule = F.getSubprogram();
+
+ // Don't clone inlined subprograms.
+ auto ShouldKeep = [SPClonedWithinModule](const DISubprogram *SP) -> bool {
+ return SP != SPClonedWithinModule;
+ };
+
return [=](const Metadata *MD) {
// Avoid cloning types, compile units, and (other) subprograms.
if (isa<DICompileUnit>(MD) || isa<DIType>(MD))
return true;
if (auto *SP = dyn_cast<DISubprogram>(MD))
- return SP != SPClonedWithinModule;
+ return ShouldKeep(SP);
// If a subprogram isn't going to be cloned skip its lexical blocks as well.
if (auto *LScope = dyn_cast<DILocalScope>(MD))
- return LScope->getSubprogram() != SPClonedWithinModule;
+ return ShouldKeep(LScope->getSubprogram());
+
+ // Avoid cloning local variables of subprograms that won't be cloned.
+ if (auto *DV = dyn_cast<DILocalVariable>(MD))
+ if (auto *S = dyn_cast_or_null<DILocalScope>(DV->getScope()))
+ return ShouldKeep(S->getSubprogram());
return false;
};
More information about the llvm-commits
mailing list