Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[CloneFunction][DebugInfo] Ensure DILocalVariables of inlined functions are not cloned (NFC) #138590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dzhidzhoev
Copy link
Member

@dzhidzhoev dzhidzhoev commented May 5, 2025

This change was separated from #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 #119001, where a DILocalVariable may refer to a local type of a DISubprogram that is being cloned. In that case, ValueMapper will clone DILocalVariable even if it belongs to an inlined DISubprogram that is not cloned, which should be avoided.
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.

…ns are not cloned (NFC)

This change was separated from llvm#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 llvm#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.
@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-transforms

Author: Vladislav Dzhidzhoev (dzhidzhoev)

Changes

This change was separated from #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 #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.


Full diff: https://github.com/llvm/llvm-project/pull/138590.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/CloneFunction.cpp (+13-2)
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;
   };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants