Skip to content

Commit b968b21

Browse files
committed
[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: #118622, branch: users/artempyanykh/fast-coro-upstream/3
1 parent c2e1030 commit b968b21

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

llvm/include/llvm/IR/ValueMap.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ struct ValueMapConfig {
7979
static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; }
8080
};
8181

82+
/// This type stores Metadata. Used in ValueMap.
83+
using MDMapT = DenseMap<const Metadata *, TrackingMDRef>;
84+
8285
/// See the file comment.
8386
template<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT>>
8487
class ValueMap {
8588
friend class ValueMapCallbackVH<KeyT, ValueT, Config>;
8689

8790
using ValueMapCVH = ValueMapCallbackVH<KeyT, ValueT, Config>;
8891
using MapT = DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH>>;
89-
using MDMapT = DenseMap<const Metadata *, TrackingMDRef>;
9092
using ExtraData = typename Config::ExtraData;
9193

9294
MapT Map;

llvm/include/llvm/Transforms/Utils/Cloning.h

+7
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ DISubprogram *CollectDebugInfoForCloning(const Function &F,
220220
CloneFunctionChangeType Changes,
221221
DebugInfoFinder &DIFinder);
222222

223+
/// Build a map of debug info to use during Metadata cloning.
224+
/// Returns true if cloning would need module level changes and false if there
225+
/// would only be local changes.
226+
bool BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes,
227+
DebugInfoFinder &DIFinder,
228+
DISubprogram *SPClonedWithinModule);
229+
223230
/// This class captures the data input to the InlineFunction call, and records
224231
/// the auxiliary results produced by it.
225232
class InlineFunctionInfo {

llvm/lib/Transforms/Utils/CloneFunction.cpp

+49-39
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,53 @@ DISubprogram *llvm::CollectDebugInfoForCloning(const Function &F,
152152
return SPClonedWithinModule;
153153
}
154154

155+
bool llvm::BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes,
156+
DebugInfoFinder &DIFinder,
157+
DISubprogram *SPClonedWithinModule) {
158+
bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
159+
if (Changes < CloneFunctionChangeType::DifferentModule &&
160+
DIFinder.subprogram_count() > 0) {
161+
// Turn on module-level changes, since we need to clone (some of) the
162+
// debug info metadata.
163+
//
164+
// FIXME: Metadata effectively owned by a function should be made
165+
// local, and only that local metadata should be cloned.
166+
ModuleLevelChanges = true;
167+
168+
auto mapToSelfIfNew = [&MD](MDNode *N) {
169+
// Avoid clobbering an existing mapping.
170+
(void)MD.try_emplace(N, N);
171+
};
172+
173+
// Avoid cloning types, compile units, and (other) subprograms.
174+
SmallPtrSet<const DISubprogram *, 16> MappedToSelfSPs;
175+
for (DISubprogram *ISP : DIFinder.subprograms()) {
176+
if (ISP != SPClonedWithinModule) {
177+
mapToSelfIfNew(ISP);
178+
MappedToSelfSPs.insert(ISP);
179+
}
180+
}
181+
182+
// If a subprogram isn't going to be cloned skip its lexical blocks as well.
183+
for (DIScope *S : DIFinder.scopes()) {
184+
auto *LScope = dyn_cast<DILocalScope>(S);
185+
if (LScope && MappedToSelfSPs.count(LScope->getSubprogram()))
186+
mapToSelfIfNew(S);
187+
}
188+
189+
for (DICompileUnit *CU : DIFinder.compile_units())
190+
mapToSelfIfNew(CU);
191+
192+
for (DIType *Type : DIFinder.types())
193+
mapToSelfIfNew(Type);
194+
} else {
195+
assert(!SPClonedWithinModule &&
196+
"Subprogram should be in DIFinder->subprogram_count()...");
197+
}
198+
199+
return ModuleLevelChanges;
200+
}
201+
155202
// Clone OldFunc into NewFunc, transforming the old arguments into references to
156203
// VMap values.
157204
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
@@ -210,45 +257,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
210257
DISubprogram *SPClonedWithinModule =
211258
CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder);
212259

213-
if (Changes < CloneFunctionChangeType::DifferentModule &&
214-
DIFinder.subprogram_count() > 0) {
215-
// Turn on module-level changes, since we need to clone (some of) the
216-
// debug info metadata.
217-
//
218-
// FIXME: Metadata effectively owned by a function should be made
219-
// local, and only that local metadata should be cloned.
220-
ModuleLevelChanges = true;
221-
222-
auto mapToSelfIfNew = [&VMap](MDNode *N) {
223-
// Avoid clobbering an existing mapping.
224-
(void)VMap.MD().try_emplace(N, N);
225-
};
226-
227-
// Avoid cloning types, compile units, and (other) subprograms.
228-
SmallPtrSet<const DISubprogram *, 16> MappedToSelfSPs;
229-
for (DISubprogram *ISP : DIFinder.subprograms()) {
230-
if (ISP != SPClonedWithinModule) {
231-
mapToSelfIfNew(ISP);
232-
MappedToSelfSPs.insert(ISP);
233-
}
234-
}
235-
236-
// If a subprogram isn't going to be cloned skip its lexical blocks as well.
237-
for (DIScope *S : DIFinder.scopes()) {
238-
auto *LScope = dyn_cast<DILocalScope>(S);
239-
if (LScope && MappedToSelfSPs.count(LScope->getSubprogram()))
240-
mapToSelfIfNew(S);
241-
}
242-
243-
for (DICompileUnit *CU : DIFinder.compile_units())
244-
mapToSelfIfNew(CU);
245-
246-
for (DIType *Type : DIFinder.types())
247-
mapToSelfIfNew(Type);
248-
} else {
249-
assert(!SPClonedWithinModule &&
250-
"Subprogram should be in DIFinder->subprogram_count()...");
251-
}
260+
ModuleLevelChanges =
261+
BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule);
252262

253263
const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges;
254264
// Duplicate the metadata that is attached to the cloned function.

0 commit comments

Comments
 (0)