Skip to content

Commit 67abbd2

Browse files
Merge pull request #61571 from aschwaighofer/swift_merge_funcs_attr_fixup
SwiftMergeFunction: Rewrite the byval/structret attributes when we bitcast the function type
2 parents 05e3c55 + 1cb6b4a commit 67abbd2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

lib/LLVMPasses/LLVMMergeFunctions.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,26 @@ void SwiftMergeFunctions::writeThunk(Function *ToFunc, Function *Thunk,
12601260
++NumSwiftThunksWritten;
12611261
}
12621262

1263+
static llvm::AttributeList
1264+
fixUpTypesInByValAndStructRetAttributes(llvm::FunctionType *fnType,
1265+
llvm::AttributeList attrList) {
1266+
auto &context = fnType->getContext();
1267+
for (unsigned i = 0; i < fnType->getNumParams(); ++i) {
1268+
auto paramTy = fnType->getParamType(i);
1269+
auto attrListIndex = llvm::AttributeList::FirstArgIndex + i;
1270+
if (attrList.hasParamAttr(i, llvm::Attribute::StructRet) &&
1271+
paramTy->getPointerElementType() != attrList.getParamStructRetType(i))
1272+
attrList = attrList.replaceAttributeTypeAtIndex(
1273+
context, attrListIndex, llvm::Attribute::StructRet,
1274+
paramTy->getPointerElementType());
1275+
if (attrList.hasParamAttr(i, llvm::Attribute::ByVal) &&
1276+
paramTy->getPointerElementType() != attrList.getParamByValType(i))
1277+
attrList = attrList.replaceAttributeTypeAtIndex(
1278+
context, attrListIndex, llvm::Attribute::ByVal,
1279+
paramTy->getPointerElementType());
1280+
}
1281+
return attrList;
1282+
}
12631283
/// Replace direct callers of Old with New. Also add parameters to the call to
12641284
/// \p New, which are defined by the FuncIdx's value in \p Params.
12651285
bool SwiftMergeFunctions::replaceDirectCallers(Function *Old, Function *New,
@@ -1329,9 +1349,11 @@ bool SwiftMergeFunctions::replaceDirectCallers(Function *Old, Function *New,
13291349
NewCI->setCallingConv(CI->getCallingConv());
13301350
// Don't transfer attributes from the function to the callee. Function
13311351
// attributes typically aren't relevant to the calling convention or ABI.
1332-
NewCI->setAttributes(AttributeList::get(Context, /*FnAttrs=*/AttributeSet(),
1352+
auto newAttrList = AttributeList::get(Context, /*FnAttrs=*/AttributeSet(),
13331353
NewPAL.getRetAttrs(),
1334-
NewArgAttrs));
1354+
NewArgAttrs);
1355+
newAttrList = fixUpTypesInByValAndStructRetAttributes(FType, newAttrList);
1356+
NewCI->setAttributes(newAttrList);
13351357
CI->replaceAllUsesWith(NewCI);
13361358
CI->eraseFromParent();
13371359
}

0 commit comments

Comments
 (0)