Skip to content

Commit ce2bad5

Browse files
committed
[NFC][Cloning] Replace DIFinder usage in CloneFunctionInto with a MetadataPredicate
Summary: The new code should be functionally identical to the old one (but faster). The reasoning is as follows. In the old code when cloning within the module: 1. DIFinder traverses and collects *all* debug info reachable from a function, its instructions, and its owning compile unit. 2. Then "compile units, types, other subprograms, and lexical blocks of other subprograms" are saved in a set. 3. Then when we MapMetadata, we traverse the function's debug info _again_ and those nodes that are in the set from p.2 are identity mapped. This looks equivalent to just doing step 3 with identity mapping based on a predicate that says to identity map "compile units, types, other subprograms, and lexical blocks of other subprograms" (same as in step 2). This is what the new code does. Test Plan: ninja check-all There's a bunch of tests around cloning and all of them pass. stack-info: PR: #129148, branch: users/artempyanykh/fast-coro-upstream-part2-take2/6
1 parent 726ffd3 commit ce2bad5

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ void collectDebugInfoFromInstructions(const Function &F,
5050
DIFinder.processInstruction(*M, I);
5151
}
5252
}
53+
54+
// Create a predicate that matches the metadata that should be identity mapped
55+
// during function cloning.
56+
MetadataPredicate createIdentityMDPredicate(const Function &F,
57+
CloneFunctionChangeType Changes) {
58+
if (Changes >= CloneFunctionChangeType::DifferentModule)
59+
return [](const Metadata *MD) { return false; };
60+
61+
DISubprogram *SPClonedWithinModule = F.getSubprogram();
62+
return [=](const Metadata *MD) {
63+
// Avoid cloning types, compile units, and (other) subprograms.
64+
if (isa<DICompileUnit>(MD) || isa<DIType>(MD))
65+
return true;
66+
67+
if (auto *SP = dyn_cast<DISubprogram>(MD); SP)
68+
return SP != SPClonedWithinModule;
69+
70+
// If a subprogram isn't going to be cloned skip its lexical blocks as well.
71+
if (auto *LScope = dyn_cast<DILocalScope>(MD); LScope)
72+
return LScope->getSubprogram() != SPClonedWithinModule;
73+
74+
return false;
75+
};
76+
}
5377
} // namespace
5478

5579
/// See comments in Cloning.h.
@@ -325,13 +349,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
325349
}
326350
}
327351

328-
DISubprogram *SPClonedWithinModule =
329-
CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder);
330-
331-
MetadataPredicate IdentityMD =
332-
[MDSet =
333-
FindDebugInfoToIdentityMap(Changes, DIFinder, SPClonedWithinModule)](
334-
const Metadata *MD) { return MDSet.contains(MD); };
352+
MetadataPredicate IdentityMD = createIdentityMDPredicate(*OldFunc, Changes);
335353

336354
// Cloning is always a Module level operation, since Metadata needs to be
337355
// cloned.

0 commit comments

Comments
 (0)