Skip to content

Commit fad2d5e

Browse files
committed
[NFC][Utils] Extract BuildDebugInfoMDMap from CloneFunctionInto
Summary: Extract the logic to build up a metadta map to use in metadata cloning into a separate function. Test Plan: ninja check-llvm-unit
1 parent 06c10b1 commit fad2d5e

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

llvm/include/llvm/IR/ValueMap.h

Lines changed: 3 additions & 1 deletion
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ DISubprogram *ProcessSubprogramAttachment(const Function &F,
209209
CloneFunctionChangeType Changes,
210210
DebugInfoFinder &DIFinder);
211211

212+
/// Build a map of debug info to use during Metadata cloning.
213+
/// Returns true if cloning would need module level changes and false if there
214+
/// would only be local changes.
215+
bool BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes,
216+
DebugInfoFinder &DIFinder,
217+
DISubprogram *SPClonedWithinModule);
218+
212219
/// This class captures the data input to the InlineFunction call, and records
213220
/// the auxiliary results produced by it.
214221
class InlineFunctionInfo {

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,53 @@ DISubprogram *llvm::ProcessSubprogramAttachment(const Function &F,
153153
return SPClonedWithinModule;
154154
}
155155

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

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

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

0 commit comments

Comments
 (0)