Skip to content

Commit cec0213

Browse files
committed
[NFC][Utils] Extract CloneFunctionMetadataInto from CloneFunctionInto
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 stack-info: PR: #118623, branch: users/artempyanykh/fast-coro-upstream/4
1 parent eadc0c9 commit cec0213

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

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

+12
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

+19-9
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ 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+
// 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,
@@ -262,15 +278,9 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
262278
BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule);
263279

264280
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-
}
281+
282+
CloneFunctionMetadataInto(NewFunc, OldFunc, VMap, RemapFlag, TypeMapper,
283+
Materializer);
274284

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

0 commit comments

Comments
 (0)