Skip to content

Commit 53d8285

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 fad2d5e commit 53d8285

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
@@ -180,6 +180,18 @@ void CloneFunctionAttributesInto(Function *NewFunc, const Function *OldFunc,
180180
ValueMapTypeRemapper *TypeMapper = nullptr,
181181
ValueMaterializer *Materializer = nullptr);
182182

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

llvm/lib/Transforms/Utils/CloneFunction.cpp

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

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

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

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

0 commit comments

Comments
 (0)