Skip to content

SwiftMergeFunction: Rewrite the byval/structret attributes when we bitcast the function type #61571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/LLVMPasses/LLVMMergeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,26 @@ void SwiftMergeFunctions::writeThunk(Function *ToFunc, Function *Thunk,
++NumSwiftThunksWritten;
}

static llvm::AttributeList
fixUpTypesInByValAndStructRetAttributes(llvm::FunctionType *fnType,
llvm::AttributeList attrList) {
auto &context = fnType->getContext();
for (unsigned i = 0; i < fnType->getNumParams(); ++i) {
auto paramTy = fnType->getParamType(i);
auto attrListIndex = llvm::AttributeList::FirstArgIndex + i;
if (attrList.hasParamAttr(i, llvm::Attribute::StructRet) &&
paramTy->getPointerElementType() != attrList.getParamStructRetType(i))
attrList = attrList.replaceAttributeTypeAtIndex(
context, attrListIndex, llvm::Attribute::StructRet,
paramTy->getPointerElementType());
if (attrList.hasParamAttr(i, llvm::Attribute::ByVal) &&
paramTy->getPointerElementType() != attrList.getParamByValType(i))
attrList = attrList.replaceAttributeTypeAtIndex(
context, attrListIndex, llvm::Attribute::ByVal,
paramTy->getPointerElementType());
}
return attrList;
}
/// Replace direct callers of Old with New. Also add parameters to the call to
/// \p New, which are defined by the FuncIdx's value in \p Params.
bool SwiftMergeFunctions::replaceDirectCallers(Function *Old, Function *New,
Expand Down Expand Up @@ -1329,9 +1349,11 @@ bool SwiftMergeFunctions::replaceDirectCallers(Function *Old, Function *New,
NewCI->setCallingConv(CI->getCallingConv());
// Don't transfer attributes from the function to the callee. Function
// attributes typically aren't relevant to the calling convention or ABI.
NewCI->setAttributes(AttributeList::get(Context, /*FnAttrs=*/AttributeSet(),
auto newAttrList = AttributeList::get(Context, /*FnAttrs=*/AttributeSet(),
NewPAL.getRetAttrs(),
NewArgAttrs));
NewArgAttrs);
newAttrList = fixUpTypesInByValAndStructRetAttributes(FType, newAttrList);
NewCI->setAttributes(newAttrList);
CI->replaceAllUsesWith(NewCI);
CI->eraseFromParent();
}
Expand Down