[llvm] eadc0c9 - [NFC][Utils] Extract BuildDebugInfoMDMap from CloneFunctionInto (#118622)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 10 00:10:26 PST 2024
Author: Artem Pianykh
Date: 2024-12-10T17:10:22+09:00
New Revision: eadc0c901ba3253ee3764f012c60de36e58cbf10
URL: https://github.com/llvm/llvm-project/commit/eadc0c901ba3253ee3764f012c60de36e58cbf10
DIFF: https://github.com/llvm/llvm-project/commit/eadc0c901ba3253ee3764f012c60de36e58cbf10.diff
LOG: [NFC][Utils] Extract BuildDebugInfoMDMap from CloneFunctionInto (#118622)
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
Added:
Modified:
llvm/include/llvm/Transforms/Utils/Cloning.h
llvm/lib/Transforms/Utils/CloneFunction.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h
index 3c8f2cbfaa9b81..7858c9d9def0da 100644
--- a/llvm/include/llvm/Transforms/Utils/Cloning.h
+++ b/llvm/include/llvm/Transforms/Utils/Cloning.h
@@ -220,6 +220,14 @@ 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(DenseMap<const Metadata *, TrackingMDRef> &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..6dc5f601b7fcaa 100644
--- a/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -152,6 +152,54 @@ DISubprogram *llvm::CollectDebugInfoForCloning(const Function &F,
return SPClonedWithinModule;
}
+bool llvm::BuildDebugInfoMDMap(DenseMap<const Metadata *, TrackingMDRef> &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 +258,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-commits
mailing list