Skip to content

Commit 3f0de57

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
1 parent ad00063 commit 3f0de57

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
@@ -360,7 +360,8 @@ static void addIndirectResultAttributes(IRGenModule &IGM,
360360
llvm::AttributeList &attrs,
361361
unsigned paramIndex, bool allowSRet,
362362
llvm::Type *storageType,
363-
const TypeInfo &typeInfo) {
363+
const TypeInfo &typeInfo,
364+
bool useInReg = false) {
364365
llvm::AttrBuilder b(IGM.getLLVMContext());
365366
b.addAttribute(llvm::Attribute::NoAlias);
366367
// Bitwise takable value types are guaranteed not to capture
@@ -370,6 +371,8 @@ static void addIndirectResultAttributes(IRGenModule &IGM,
370371
if (allowSRet) {
371372
assert(storageType);
372373
b.addStructRetAttr(storageType);
374+
if (useInReg)
375+
b.addAttribute(llvm::Attribute::InReg);
373376
}
374377
attrs = attrs.addParamAttributes(IGM.getLLVMContext(), paramIndex, b);
375378
}
@@ -552,7 +555,7 @@ namespace {
552555

553556
private:
554557
const TypeInfo &expand(SILParameterInfo param);
555-
llvm::Type *addIndirectResult(SILType resultType);
558+
llvm::Type *addIndirectResult(SILType resultType, bool useInReg = false);
556559

557560
SILFunctionConventions getSILFuncConventions() const {
558561
return SILFunctionConventions(FnType, IGM.getSILModule());
@@ -611,11 +614,12 @@ namespace {
611614
} // end namespace irgen
612615
} // end namespace swift
613616

614-
llvm::Type *SignatureExpansion::addIndirectResult(SILType resultType) {
617+
llvm::Type *SignatureExpansion::addIndirectResult(SILType resultType,
618+
bool useInReg) {
615619
const TypeInfo &resultTI = IGM.getTypeInfo(resultType);
616620
auto storageTy = resultTI.getStorageType();
617621
addIndirectResultAttributes(IGM, Attrs, ParamIRTypes.size(), claimSRet(),
618-
storageTy, resultTI);
622+
storageTy, resultTI, useInReg);
619623
addPointerParameter(storageTy);
620624
return IGM.VoidTy;
621625
}
@@ -1707,9 +1711,9 @@ void SignatureExpansion::expandExternalSignatureTypes() {
17071711
// returned indirect values.
17081712
emitArg(0);
17091713
firstParamToLowerNormally = 1;
1710-
addIndirectResult(resultType);
1714+
addIndirectResult(resultType, returnInfo.getInReg());
17111715
} else
1712-
addIndirectResult(resultType);
1716+
addIndirectResult(resultType, returnInfo.getInReg());
17131717
}
17141718

17151719
// 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,7 +12,8 @@ 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-x86_64: define {{.*}} void @"?nonConstPassThroughAsWrapper@HasMethods@@QEAA?AUNonTrivialInWrapper@@H@Z"(ptr {{.*}} %{{.*}}, ptr noalias sret(%struct.NonTrivialInWrapper) {{.*}} %{{.*}}, i32 noundef %{{.*}})
1819
// CHECK-aarch64: define {{.*}} void @"?nonConstPassThroughAsWrapper@HasMethods@@QEAA?AUNonTrivialInWrapper@@H@Z"(ptr {{.*}} %{{.*}}, ptr inreg noalias sret(%struct.NonTrivialInWrapper) {{.*}} %{{.*}}, i32 noundef %{{.*}})

0 commit comments

Comments
 (0)