Skip to content

Commit eaf0e48

Browse files
committed
Add the inreg attribute to sreg when present.
On Windows/AArch64, a different register is used between when an arugment is both inreg and sret (X0 or X1) and when it is just sret (X8) as the following comment indicates: https://github.com/llvm/llvm-project/blob/46fe36a4295f05d5d3731762e31fc4e6e99863e9/llvm/lib/Target/AArch64/AArch64CallingConvention.td#L42 ``` // In AAPCS, an SRet is passed in X8, not X0 like a normal pointer parameter. // However, on windows, in some circumstances, the SRet is passed in X0 or X1 // instead. The presence of the inreg attribute indicates that SRet is // passed in the alternative register (X0 or X1), not X8: // - X0 for non-instance methods. // - X1 for instance methods. // The "sret" attribute identifies indirect returns. // The "inreg" attribute identifies non-aggregate types. // The position of the "sret" attribute identifies instance/non-instance // methods. // "sret" on argument 0 means non-instance methods. // "sret" on argument 1 means instance methods. CCIfInReg<CCIfType<[i64], CCIfSRet<CCIfType<[i64], CCAssignToReg<[X0, X1]>>>>>, CCIfSRet<CCIfType<[i64], CCAssignToReg<[X8]>>>, ``` So missing/dropping inreg can cause a codegen bug. This is a partial fix for #74866 Cherrypick commit 3f0de57 Cherrypick PR #76159
1 parent fd3ad8d commit eaf0e48

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ static void addIndirectResultAttributes(IRGenModule &IGM,
350350
llvm::AttributeList &attrs,
351351
unsigned paramIndex, bool allowSRet,
352352
llvm::Type *storageType,
353-
const TypeInfo &typeInfo) {
353+
const TypeInfo &typeInfo,
354+
bool useInReg = false) {
354355
llvm::AttrBuilder b(IGM.getLLVMContext());
355356
b.addAttribute(llvm::Attribute::NoAlias);
356357
// Bitwise takable value types are guaranteed not to capture
@@ -360,6 +361,8 @@ static void addIndirectResultAttributes(IRGenModule &IGM,
360361
if (allowSRet) {
361362
assert(storageType);
362363
b.addStructRetAttr(storageType);
364+
if (useInReg)
365+
b.addAttribute(llvm::Attribute::InReg);
363366
}
364367
attrs = attrs.addParamAttributes(IGM.getLLVMContext(), paramIndex, b);
365368
}
@@ -467,7 +470,7 @@ namespace {
467470

468471
private:
469472
const TypeInfo &expand(SILParameterInfo param);
470-
llvm::Type *addIndirectResult(SILType resultType);
473+
llvm::Type *addIndirectResult(SILType resultType, bool useInReg = false);
471474

472475
SILFunctionConventions getSILFuncConventions() const {
473476
return SILFunctionConventions(FnType, IGM.getSILModule());
@@ -521,11 +524,12 @@ namespace {
521524
} // end namespace irgen
522525
} // end namespace swift
523526

524-
llvm::Type *SignatureExpansion::addIndirectResult(SILType resultType) {
527+
llvm::Type *SignatureExpansion::addIndirectResult(SILType resultType,
528+
bool useInReg) {
525529
const TypeInfo &resultTI = IGM.getTypeInfo(resultType);
526530
auto storageTy = resultTI.getStorageType();
527531
addIndirectResultAttributes(IGM, Attrs, ParamIRTypes.size(), claimSRet(),
528-
storageTy, resultTI);
532+
storageTy, resultTI, useInReg);
529533
addPointerParameter(storageTy);
530534
return IGM.VoidTy;
531535
}
@@ -1560,9 +1564,9 @@ void SignatureExpansion::expandExternalSignatureTypes() {
15601564
// returned indirect values.
15611565
emitArg(0);
15621566
firstParamToLowerNormally = 1;
1563-
addIndirectResult(resultType);
1567+
addIndirectResult(resultType, returnInfo.getInReg());
15641568
} else
1565-
addIndirectResult(resultType);
1569+
addIndirectResult(resultType, returnInfo.getInReg());
15661570
}
15671571

15681572
// Use a special IR type for passing block pointers.

test/Interop/Cxx/class/method/methods-this-and-indirect-return-irgen-msvc.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public func use() -> CInt {
1212

1313
// CHECK: %[[instance:.*]] = alloca %TSo10HasMethodsV
1414
// CHECK: %[[result:.*]] = alloca %TSo19NonTrivialInWrapperV
15-
// CHECK: call void @"?nonConstPassThroughAsWrapper@HasMethods@@QEAA?AUNonTrivialInWrapper@@H@Z"(ptr %[[instance]], ptr noalias sret(%TSo19NonTrivialInWrapperV) %[[result]], i32 42)
15+
// CHECK-x86_64: call void @"?nonConstPassThroughAsWrapper@HasMethods@@QEAA?AUNonTrivialInWrapper@@H@Z"(ptr %[[instance]], ptr noalias sret(%TSo19NonTrivialInWrapperV) %[[result]], i32 42)
16+
// CHECK-aarch64: call void @"?nonConstPassThroughAsWrapper@HasMethods@@QEAA?AUNonTrivialInWrapper@@H@Z"(ptr %[[instance]], ptr inreg noalias sret(%TSo19NonTrivialInWrapperV) %[[result]], i32 42)
1617

1718
// CHECK: define {{.*}} void @"?nonConstPassThroughAsWrapper@HasMethods@@QEAA?AUNonTrivialInWrapper@@H@Z"(ptr {{.*}} %{{.*}}, ptr noalias sret(%struct.NonTrivialInWrapper) {{.*}} %{{.*}}, i32 noundef %{{.*}})

0 commit comments

Comments
 (0)