Skip to content

Commit 84a78ab

Browse files
authored
[NFC][Utils] Extract CloneFunctionAttributesInto from CloneFunctionInto (#112976)
This patch is a part of step-by-step refactoring of CloneFunctionInto. The goal is to extract reusable pieces out of it that will be later used to optimize function cloning e.g. in coroutine processing. Extracted from #109032 (commit 2)
1 parent 1c2824e commit 84a78ab

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
175175
ValueMapTypeRemapper *TypeMapper = nullptr,
176176
ValueMaterializer *Materializer = nullptr);
177177

178+
/// Clone OldFunc's attributes into NewFunc, transforming values based on the
179+
/// mappings in VMap.
180+
void CloneFunctionAttributesInto(Function *NewFunc, const Function *OldFunc,
181+
ValueToValueMapTy &VMap,
182+
bool ModuleLevelChanges,
183+
ValueMapTypeRemapper *TypeMapper = nullptr,
184+
ValueMaterializer *Materializer = nullptr);
185+
178186
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
179187
const Instruction *StartingInst,
180188
ValueToValueMapTy &VMap, bool ModuleLevelChanges,

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,14 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
8787
return NewBB;
8888
}
8989

90-
// Clone OldFunc into NewFunc, transforming the old arguments into references to
91-
// VMap values.
92-
//
93-
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
94-
ValueToValueMapTy &VMap,
95-
CloneFunctionChangeType Changes,
96-
SmallVectorImpl<ReturnInst *> &Returns,
97-
const char *NameSuffix, ClonedCodeInfo *CodeInfo,
98-
ValueMapTypeRemapper *TypeMapper,
99-
ValueMaterializer *Materializer) {
100-
NewFunc->setIsNewDbgInfoFormat(OldFunc->IsNewDbgInfoFormat);
101-
assert(NameSuffix && "NameSuffix cannot be null!");
102-
103-
#ifndef NDEBUG
104-
for (const Argument &I : OldFunc->args())
105-
assert(VMap.count(&I) && "No mapping from source argument specified!");
106-
#endif
107-
108-
bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
109-
110-
// Copy all attributes other than those stored in the AttributeList. We need
111-
// to remap the parameter indices of the AttributeList.
90+
void llvm::CloneFunctionAttributesInto(Function *NewFunc,
91+
const Function *OldFunc,
92+
ValueToValueMapTy &VMap,
93+
bool ModuleLevelChanges,
94+
ValueMapTypeRemapper *TypeMapper,
95+
ValueMaterializer *Materializer) {
96+
// Copy all attributes other than those stored in Function's AttributeList
97+
// which holds e.g. parameters and return value attributes.
11298
AttributeList NewAttrs = NewFunc->getAttributes();
11399
NewFunc->copyAttributesFrom(OldFunc);
114100
NewFunc->setAttributes(NewAttrs);
@@ -140,6 +126,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
140126
// Clone any argument attributes that are present in the VMap.
141127
for (const Argument &OldArg : OldFunc->args()) {
142128
if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
129+
// Remap the parameter indices.
143130
NewArgAttrs[NewArg->getArgNo()] =
144131
OldAttrs.getParamAttrs(OldArg.getArgNo());
145132
}
@@ -148,6 +135,29 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
148135
NewFunc->setAttributes(
149136
AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttrs(),
150137
OldAttrs.getRetAttrs(), NewArgAttrs));
138+
}
139+
140+
// Clone OldFunc into NewFunc, transforming the old arguments into references to
141+
// VMap values.
142+
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
143+
ValueToValueMapTy &VMap,
144+
CloneFunctionChangeType Changes,
145+
SmallVectorImpl<ReturnInst *> &Returns,
146+
const char *NameSuffix, ClonedCodeInfo *CodeInfo,
147+
ValueMapTypeRemapper *TypeMapper,
148+
ValueMaterializer *Materializer) {
149+
NewFunc->setIsNewDbgInfoFormat(OldFunc->IsNewDbgInfoFormat);
150+
assert(NameSuffix && "NameSuffix cannot be null!");
151+
152+
#ifndef NDEBUG
153+
for (const Argument &I : OldFunc->args())
154+
assert(VMap.count(&I) && "No mapping from source argument specified!");
155+
#endif
156+
157+
bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
158+
159+
CloneFunctionAttributesInto(NewFunc, OldFunc, VMap, ModuleLevelChanges,
160+
TypeMapper, Materializer);
151161

152162
// Everything else beyond this point deals with function instructions,
153163
// so if we are dealing with a function declaration, we're done.

0 commit comments

Comments
 (0)