Skip to content

Commit fbbbf45

Browse files
committed
[NFC][Utils] Extract CloneFunctionMetadataInto from CloneFunctionInto
Summary: One potentially contentious point in the new function's API is that it expects the caller ot populate the VMap rather than doing metadata cloning wholesale inside it. We'll need it this way for a subsequent change. Test Plan: ninja check-llvm-unit
1 parent 63efec5 commit fbbbf45

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ void CloneFunctionAttributesInto(Function *NewFunc, const Function *OldFunc,
182182
ValueMapTypeRemapper *TypeMapper = nullptr,
183183
ValueMaterializer *Materializer = nullptr);
184184

185+
/// Clone OldFunc's metadata into NewFunc.
186+
///
187+
/// The caller is expected to populate \p VMap beforehand and set an appropriate
188+
/// \p RemapFlag.
189+
///
190+
/// NOTE: This function doesn't clone !llvm.dbg.cu when cloning into a different
191+
/// module. Use CloneFunctionInto for that behavior.
192+
void CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc,
193+
ValueToValueMapTy &VMap, RemapFlags RemapFlag,
194+
ValueMapTypeRemapper *TypeMapper = nullptr,
195+
ValueMaterializer *Materializer = nullptr);
196+
185197
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
186198
const Instruction *StartingInst,
187199
ValueToValueMapTy &VMap, bool ModuleLevelChanges,

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ bool llvm::BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes,
199199
return ModuleLevelChanges;
200200
}
201201

202+
void llvm::CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc,
203+
ValueToValueMapTy &VMap,
204+
RemapFlags RemapFlag,
205+
ValueMapTypeRemapper *TypeMapper,
206+
ValueMaterializer *Materializer) {
207+
// Duplicate the metadata that is attached to the cloned function.
208+
// Subprograms/CUs/types that were already mapped to themselves won't be
209+
// duplicated.
210+
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
211+
OldFunc->getAllMetadata(MDs);
212+
for (auto MD : MDs) {
213+
NewFunc->addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag,
214+
TypeMapper, Materializer));
215+
}
216+
}
217+
202218
// Clone OldFunc into NewFunc, transforming the old arguments into references to
203219
// VMap values.
204220
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
@@ -261,15 +277,9 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
261277
BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule);
262278

263279
const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges;
264-
// Duplicate the metadata that is attached to the cloned function.
265-
// Subprograms/CUs/types that were already mapped to themselves won't be
266-
// duplicated.
267-
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
268-
OldFunc->getAllMetadata(MDs);
269-
for (auto MD : MDs) {
270-
NewFunc->addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag,
271-
TypeMapper, Materializer));
272-
}
280+
281+
CloneFunctionMetadataInto(NewFunc, OldFunc, VMap, RemapFlag, TypeMapper,
282+
Materializer);
273283

274284
// Loop over all of the basic blocks in the function, cloning them as
275285
// appropriate. Note that we save BE this way in order to handle cloning of

0 commit comments

Comments
 (0)