Skip to content

[5.2] Generic Specializer: Use getResilienceExpansion() throughout ReabstractionInfo #28773

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
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions include/swift/SILOptimizer/Utils/Generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ void trySpecializeApplyOfGeneric(
/// Specifically, it contains information which formal parameters and returns
/// are changed from indirect values to direct values.
class ReabstractionInfo {
/// A 1-bit means that this parameter/return value is converted from indirect
/// to direct.
/// A 1-bit means that this argument (= either indirect return value or
/// parameter) is converted from indirect to direct.
SmallBitVector Conversions;

/// For each bit set in Conversions, there is a bit set in TrivialArgs if the
/// argument has a trivial type.
SmallBitVector TrivialArgs;

/// If set, indirect to direct conversions should be performed by the generic
/// specializer.
bool ConvertIndirectToDirect;
Expand Down Expand Up @@ -121,6 +125,10 @@ class ReabstractionInfo {

// Is the generated specialization going to be serialized?
IsSerialized_t Serialized;

unsigned param2ArgIndex(unsigned ParamIdx) const {
return ParamIdx + NumFormalIndirectResults;
}

// Create a new substituted type with the updated signature.
CanSILFunctionType createSubstitutedType(SILFunction *OrigF,
Expand Down Expand Up @@ -171,8 +179,7 @@ class ReabstractionInfo {
/// Returns true if the \p ParamIdx'th (non-result) formal parameter is
/// converted from indirect to direct.
bool isParamConverted(unsigned ParamIdx) const {
return ConvertIndirectToDirect &&
Conversions.test(ParamIdx + NumFormalIndirectResults);
return ConvertIndirectToDirect && isArgConverted(param2ArgIndex(ParamIdx));
}

/// Returns true if the \p ResultIdx'th formal result is converted from
Expand Down
22 changes: 12 additions & 10 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,10 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
// Check which parameters and results can be converted from
// indirect to direct ones.
NumFormalIndirectResults = SubstitutedType->getNumIndirectFormalResults();
Conversions.resize(NumFormalIndirectResults +
SubstitutedType->getParameters().size());
unsigned NumArgs = NumFormalIndirectResults +
SubstitutedType->getParameters().size();
Conversions.resize(NumArgs);
TrivialArgs.resize(NumArgs);

CanGenericSignature CanSig;
if (SpecializedGenericSig)
Expand All @@ -696,6 +698,8 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
if (TL.isLoadable() && !RI.getReturnValueType(M, SubstitutedType)->isVoid() &&
shouldExpand(M, ResultTy)) {
Conversions.set(IdxForResult);
if (TL.isTrivial())
TrivialArgs.set(IdxForResult);
break;
}
++IdxForResult;
Expand All @@ -721,6 +725,8 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
case ParameterConvention::Indirect_In:
case ParameterConvention::Indirect_In_Guaranteed:
Conversions.set(IdxToInsert);
if (TL.isTrivial())
TrivialArgs.set(IdxToInsert);
break;
case ParameterConvention::Indirect_In_Constant:
case ParameterConvention::Indirect_Inout:
Expand Down Expand Up @@ -796,14 +802,12 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
unsigned IndirectResultIdx = 0;
for (SILResultInfo RI : SubstFTy->getResults()) {
if (RI.isFormalIndirect()) {
bool isTrivial = TrivialArgs.test(IndirectResultIdx);
if (isFormalResultConverted(IndirectResultIdx++)) {
// Convert the indirect result to a direct result.
SILType SILResTy =
SILType::getPrimitiveObjectType(RI.getReturnValueType(M, SubstFTy));

// Indirect results are passed as owned, so we also need to pass the
// direct result as owned (except it's a trivial type).
auto C = (SILResTy.isTrivial(*Callee)
auto C = (isTrivial
? ResultConvention::Unowned
: ResultConvention::Owned);
SpecializedResults.push_back(SILResultInfo(RI.getReturnValueType(M, SubstFTy), C));
Expand All @@ -815,21 +819,19 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
}
unsigned ParamIdx = 0;
for (SILParameterInfo PI : SubstFTy->getParameters()) {
bool isTrivial = TrivialArgs.test(param2ArgIndex(ParamIdx));
if (!isParamConverted(ParamIdx++)) {
// No conversion: re-use the original, substituted parameter info.
SpecializedParams.push_back(PI);
continue;
}

// Convert the indirect parameter to a direct parameter.
SILType SILParamTy =
SILType::getPrimitiveObjectType(PI.getArgumentType(M, SubstFTy));

// Indirect parameters are passed as owned/guaranteed, so we also
// need to pass the direct/guaranteed parameter as
// owned/guaranteed (except it's a trivial type).
auto C = ParameterConvention::Direct_Unowned;
if (!SILParamTy.isTrivial(*Callee)) {
if (!isTrivial) {
if (PI.isGuaranteed()) {
C = ParameterConvention::Direct_Guaranteed;
} else {
Expand Down
19 changes: 19 additions & 0 deletions test/SILOptimizer/specialization_and_resilience.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend -parse-as-library -O -module-name=test %s -enable-library-evolution -emit-sil | %FileCheck %s

public enum En {
case A
case B
}

@inlinable
@inline(never)
func genfunc<T>(_ t: T) -> T {
return t
}

// CHECK-LABEL: sil @$s4test11callGenFuncyyF : $@convention(thin) () -> () {
// CHECK: = function_ref @$s4test7genfuncyxxlFAA2EnO_Tg5 : $@convention(thin) (En) -> @out En
// CHECK: } // end sil function '$s4test11callGenFuncyyF'
public func callGenFunc() {
_ = genfunc(En.A)
}