Skip to content
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

[6.0] SIL: Consistently drop substitution map when forming apply instructions #74275

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
2 changes: 2 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,8 @@ class ApplyInstBase<Impl, Base, false> : public Base {
SpecializationInfo(specializationInfo), NumCallArguments(args.size()),
NumTypeDependentOperands(typeDependentOperands.size()),
Substitutions(subs) {
assert(!!subs == !!callee->getType().castTo<SILFunctionType>()
->getInvocationGenericSignature());

// Initialize the operands.
auto allOperands = getAllOperands();
Expand Down
10 changes: 10 additions & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,11 @@ static void emitRawApply(SILGenFunction &SGF,
SILValue indirectErrorAddr,
SmallVectorImpl<SILValue> &rawResults,
ExecutorBreadcrumb prevExecutor) {
// We completely drop the generic signature if all generic parameters were
// concrete.
if (subs && subs.getGenericSignature()->areAllParamsConcrete())
subs = SubstitutionMap();

SILFunctionConventions substFnConv(substFnType, SGF.SGM.M);
// Get the callee value.
bool isConsumed = substFnType->isCalleeConsumed();
Expand Down Expand Up @@ -5920,6 +5925,11 @@ SILValue SILGenFunction::emitApplyWithRethrow(SILLocation loc, SILValue fn,
SILType substFnType,
SubstitutionMap subs,
ArrayRef<SILValue> args) {
// We completely drop the generic signature if all generic parameters were
// concrete.
if (subs && subs.getGenericSignature()->areAllParamsConcrete())
subs = SubstitutionMap();

CanSILFunctionType silFnType = substFnType.castTo<SILFunctionType>();
SILFunctionConventions fnConv(silFnType, SGM.M);
SILType resultType = fnConv.getSILResultType(getTypeExpansionContext());
Expand Down
14 changes: 10 additions & 4 deletions lib/SILGen/SILGenDestructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,16 @@ void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
SILValue baseSelf = B.createUpcast(cleanupLoc, selfValue, baseSILTy);
ManagedValue dtorValue;
SILType dtorTy;

auto subMap
= superclassTy->getContextSubstitutionMap(SGM.M.getSwiftModule(),
superclass);

// We completely drop the generic signature if all generic parameters were
// concrete.
if (subMap && subMap.getGenericSignature()->areAllParamsConcrete())
subMap = SubstitutionMap();

std::tie(dtorValue, dtorTy)
= emitSiblingMethodRef(cleanupLoc, baseSelf, dtorConstant, subMap);

Expand Down Expand Up @@ -191,12 +198,11 @@ void SILGenFunction::emitDeallocatingClassDestructor(DestructorDecl *dd) {
// Form a reference to the destroying destructor.
SILDeclRef dtorConstant(dd, SILDeclRef::Kind::Destroyer);
auto classTy = initialSelfValue->getType();
auto classDecl = classTy.getASTType()->getAnyNominal();

auto subMap = F.getForwardingSubstitutionMap();

ManagedValue dtorValue;
SILType dtorTy;
auto subMap = classTy.getASTType()
->getContextSubstitutionMap(SGM.M.getSwiftModule(),
classDecl);
std::tie(dtorValue, dtorTy)
= emitSiblingMethodRef(loc, initialSelfValue, dtorConstant, subMap);

Expand Down
8 changes: 7 additions & 1 deletion lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,13 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
constant, loweredCaptureInfo, subs);
}

if (subs) {
// We completely drop the generic signature if all generic parameters were
// concrete.
if (!pft->isPolymorphic()) {
subs = SubstitutionMap();
} else {
assert(!subs.getGenericSignature()->areAllParamsConcrete());

auto specialized =
pft->substGenericArgs(F.getModule(), subs, getTypeExpansionContext());
functionTy = SILType::getPrimitiveObjectType(specialized);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-frontend -parse-as-library -disable-availability-checking -import-objc-header %S/Inputs/perf-annotations.h -emit-sil %s -o /dev/null -verify

// REQUIRES: swift_in_compiler
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib

@_noAllocation
func createEmptyArray() {
_ = [Int]() // expected-error {{ending the lifetime of a value of type}}
}
40 changes: 34 additions & 6 deletions test/SILOptimizer/performance-annotations.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %target-swift-frontend -parse-as-library -disable-availability-checking -import-objc-header %S/Inputs/perf-annotations.h -emit-sil %s -o /dev/null -verify
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib

// REQUIRES: swift_in_compiler
// REQUIRES: optimized_stdlib

protocol P {
func protoMethod(_ a: Int) -> Int
Expand Down Expand Up @@ -233,11 +234,6 @@ func closueWhichModifiesLocalVar() -> Int {
return x
}

@_noAllocation
func createEmptyArray() {
_ = [Int]() // expected-error {{ending the lifetime of a value of type}}
}

struct Buffer {
var p: UnsafeMutableRawBufferPointer

Expand Down Expand Up @@ -514,3 +510,35 @@ func testNonCopyable() {
let t = NonCopyableStruct()
t.foo()
}

func takesClosure(_: () -> ()) {}

@_noLocks
func testClosureExpression<T>(_ t: T) {
takesClosure {
// expected-error@-1 {{generic closures or local functions can cause metadata allocation or locks}}
_ = T.self
}
}

@_noLocks
func testLocalFunction<T>(_ t: T) {
func localFunc() {
_ = T.self
}

takesClosure(localFunc)
// expected-error@-1 {{generic closures or local functions can cause metadata allocation or locks}}
}

func takesGInt(_ x: G<Int>) {}

struct G<T> {}

extension G where T == Int {
@_noAllocation func method() {
takesClosure {
takesGInt(self)
}
}
}