[llvm-branch-commits] [llvm] [NFC][Utils] Extract BuildDebugInfoMDMap from CloneFunctionInto (PR #118622)
Artem Pianykh via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Dec 4 05:16:33 PST 2024
https://github.com/artempyanykh updated https://github.com/llvm/llvm-project/pull/118622
>From b968b21c6283403e04a45acaafc506d9f26fb8ea Mon Sep 17 00:00:00 2001
From: Artem Pianykh <arr at fb.com>
Date: Thu, 12 Sep 2024 15:23:43 -0700
Subject: [PATCH] [NFC][Utils] Extract BuildDebugInfoMDMap from
CloneFunctionInto
Summary:
Extract the logic to build up a metadata map to use in metadata cloning
into a separate function.
Test Plan:
ninja check-llvm-unit check-llvm
stack-info: PR: https://github.com/llvm/llvm-project/pull/118622, branch: users/artempyanykh/fast-coro-upstream/3
---
llvm/include/llvm/IR/ValueMap.h | 4 +-
llvm/include/llvm/Transforms/Utils/Cloning.h | 7 ++
llvm/lib/Transforms/Utils/CloneFunction.cpp | 88 +++++++++++---------
3 files changed, 59 insertions(+), 40 deletions(-)
diff --git a/llvm/include/llvm/IR/ValueMap.h b/llvm/include/llvm/IR/ValueMap.h
index d12d639aaa8886..fd4c08492e0124 100644
--- a/llvm/include/llvm/IR/ValueMap.h
+++ b/llvm/include/llvm/IR/ValueMap.h
@@ -79,6 +79,9 @@ struct ValueMapConfig {
static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; }
};
+/// This type stores Metadata. Used in ValueMap.
+using MDMapT = DenseMap<const Metadata *, TrackingMDRef>;
+
/// See the file comment.
template<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT>>
class ValueMap {
@@ -86,7 +89,6 @@ class ValueMap {
using ValueMapCVH = ValueMapCallbackVH<KeyT, ValueT, Config>;
using MapT = DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH>>;
- using MDMapT = DenseMap<const Metadata *, TrackingMDRef>;
using ExtraData = typename Config::ExtraData;
MapT Map;
diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h
index 3c8f2cbfaa9b81..434089138bc521 100644
--- a/llvm/include/llvm/Transforms/Utils/Cloning.h
+++ b/llvm/include/llvm/Transforms/Utils/Cloning.h
@@ -220,6 +220,13 @@ DISubprogram *CollectDebugInfoForCloning(const Function &F,
CloneFunctionChangeType Changes,
DebugInfoFinder &DIFinder);
+/// Build a map of debug info to use during Metadata cloning.
+/// Returns true if cloning would need module level changes and false if there
+/// would only be local changes.
+bool BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes,
+ DebugInfoFinder &DIFinder,
+ DISubprogram *SPClonedWithinModule);
+
/// This class captures the data input to the InlineFunction call, and records
/// the auxiliary results produced by it.
class InlineFunctionInfo {
diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp
index d038117090e4cc..736c4a13045c14 100644
--- a/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -152,6 +152,53 @@ DISubprogram *llvm::CollectDebugInfoForCloning(const Function &F,
return SPClonedWithinModule;
}
+bool llvm::BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes,
+ DebugInfoFinder &DIFinder,
+ DISubprogram *SPClonedWithinModule) {
+ bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
+ if (Changes < CloneFunctionChangeType::DifferentModule &&
+ DIFinder.subprogram_count() > 0) {
+ // Turn on module-level changes, since we need to clone (some of) the
+ // debug info metadata.
+ //
+ // FIXME: Metadata effectively owned by a function should be made
+ // local, and only that local metadata should be cloned.
+ ModuleLevelChanges = true;
+
+ auto mapToSelfIfNew = [&MD](MDNode *N) {
+ // Avoid clobbering an existing mapping.
+ (void)MD.try_emplace(N, N);
+ };
+
+ // Avoid cloning types, compile units, and (other) subprograms.
+ SmallPtrSet<const DISubprogram *, 16> MappedToSelfSPs;
+ for (DISubprogram *ISP : DIFinder.subprograms()) {
+ if (ISP != SPClonedWithinModule) {
+ mapToSelfIfNew(ISP);
+ MappedToSelfSPs.insert(ISP);
+ }
+ }
+
+ // If a subprogram isn't going to be cloned skip its lexical blocks as well.
+ for (DIScope *S : DIFinder.scopes()) {
+ auto *LScope = dyn_cast<DILocalScope>(S);
+ if (LScope && MappedToSelfSPs.count(LScope->getSubprogram()))
+ mapToSelfIfNew(S);
+ }
+
+ for (DICompileUnit *CU : DIFinder.compile_units())
+ mapToSelfIfNew(CU);
+
+ for (DIType *Type : DIFinder.types())
+ mapToSelfIfNew(Type);
+ } else {
+ assert(!SPClonedWithinModule &&
+ "Subprogram should be in DIFinder->subprogram_count()...");
+ }
+
+ return ModuleLevelChanges;
+}
+
// Clone OldFunc into NewFunc, transforming the old arguments into references to
// VMap values.
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
@@ -210,45 +257,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
DISubprogram *SPClonedWithinModule =
CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder);
- if (Changes < CloneFunctionChangeType::DifferentModule &&
- DIFinder.subprogram_count() > 0) {
- // Turn on module-level changes, since we need to clone (some of) the
- // debug info metadata.
- //
- // FIXME: Metadata effectively owned by a function should be made
- // local, and only that local metadata should be cloned.
- ModuleLevelChanges = true;
-
- auto mapToSelfIfNew = [&VMap](MDNode *N) {
- // Avoid clobbering an existing mapping.
- (void)VMap.MD().try_emplace(N, N);
- };
-
- // Avoid cloning types, compile units, and (other) subprograms.
- SmallPtrSet<const DISubprogram *, 16> MappedToSelfSPs;
- for (DISubprogram *ISP : DIFinder.subprograms()) {
- if (ISP != SPClonedWithinModule) {
- mapToSelfIfNew(ISP);
- MappedToSelfSPs.insert(ISP);
- }
- }
-
- // If a subprogram isn't going to be cloned skip its lexical blocks as well.
- for (DIScope *S : DIFinder.scopes()) {
- auto *LScope = dyn_cast<DILocalScope>(S);
- if (LScope && MappedToSelfSPs.count(LScope->getSubprogram()))
- mapToSelfIfNew(S);
- }
-
- for (DICompileUnit *CU : DIFinder.compile_units())
- mapToSelfIfNew(CU);
-
- for (DIType *Type : DIFinder.types())
- mapToSelfIfNew(Type);
- } else {
- assert(!SPClonedWithinModule &&
- "Subprogram should be in DIFinder->subprogram_count()...");
- }
+ ModuleLevelChanges =
+ BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule);
const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges;
// Duplicate the metadata that is attached to the cloned function.
More information about the llvm-branch-commits
mailing list