Skip to content

Commit a9237b1

Browse files
authored
[NFC][Utils] Extract CloneFunctionMetadataInto from CloneFunctionInto (#118623)
Summary: The new API expects the caller to populate the VMap. We need it this way for a subsequent change around coroutine cloning. Test Plan: ninja check-llvm-unit check-llvm
1 parent f9120dc commit a9237b1

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ 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. Subprograms/CUs/types that were already mapped to themselves
189+
/// won't be duplicated.
190+
///
191+
/// NOTE: This function doesn't clone !llvm.dbg.cu when cloning into a different
192+
/// module. Use CloneFunctionInto for that behavior.
193+
void CloneFunctionMetadataInto(Function &NewFunc, const Function &OldFunc,
194+
ValueToValueMapTy &VMap, RemapFlags RemapFlag,
195+
ValueMapTypeRemapper *TypeMapper = nullptr,
196+
ValueMaterializer *Materializer = nullptr);
197+
185198
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
186199
const Instruction *StartingInst,
187200
ValueToValueMapTy &VMap, bool ModuleLevelChanges,

llvm/lib/Transforms/Utils/CloneFunction.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ bool llvm::BuildDebugInfoMDMap(DenseMap<const Metadata *, TrackingMDRef> &MD,
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+
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
209+
OldFunc.getAllMetadata(MDs);
210+
for (auto MD : MDs) {
211+
NewFunc.addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag,
212+
TypeMapper, Materializer));
213+
}
214+
}
215+
203216
// Clone OldFunc into NewFunc, transforming the old arguments into references to
204217
// VMap values.
205218
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
@@ -262,15 +275,9 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
262275
BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule);
263276

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

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

0 commit comments

Comments
 (0)