Skip to content

[6.0 cherry-pick][embedded] Avoid a crash on location-less SIL functions in PerformanceDiagnostics #72921

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
25 changes: 16 additions & 9 deletions lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,15 +810,22 @@ class PerformanceDiagnosticsPass : public SILModuleTransform {
SmallVector<SILFunction *, 8> constructorsAndDestructors;

for (SILFunction &function : *module) {
auto func = function.getLocation().getAsASTNode<AbstractFunctionDecl>();
if (func) {
if (isa<DestructorDecl>(func) || isa<ConstructorDecl>(func)) {
constructorsAndDestructors.push_back(&function);
continue;
}
if (getMethodDispatch(func) == MethodDispatch::Class) {
vtableMembers.push_back(&function);
continue;
// There might be SILFunctions without a location, e.g.
// swift_readAtKeyPath generated by SILGen for keypaths. It's okay to
// skip the ctor/dtor/method detection logic for those, such functions
// still end up in the "others" list and are still visited.
if (function.hasLocation()) {
auto func =
function.getLocation().getAsASTNode<AbstractFunctionDecl>();
if (func) {
if (isa<DestructorDecl>(func) || isa<ConstructorDecl>(func)) {
constructorsAndDestructors.push_back(&function);
continue;
}
if (getMethodDispatch(func) == MethodDispatch::Class) {
vtableMembers.push_back(&function);
continue;
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions test/embedded/keypath-crash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: %target-swift-emit-ir %s -module-name=main -enable-experimental-feature Embedded | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: OS=macosx || OS=linux-gnu

@propertyWrapper
@dynamicMemberLookup
public struct Binding<Value> {
public var wrappedValue: Value

init(get: @escaping () -> Value, set: @escaping (Value) -> Void) {
self.wrappedValue = get()
}

subscript<Subject>(dynamicMember keyPath: WritableKeyPath<Value, Subject>) -> Binding<Subject> {
get { fatalError() }
}
}


public struct State<Wrapped> {
public var wrappedValue: Wrapped

public init(wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue
}
public var projectedValue: Projection {
Projection(wrappedValue: wrappedValue)
}

@dynamicMemberLookup
public struct Projection {
var wrappedValue: Wrapped
public subscript<Member>(dynamicMember keyPath: ReferenceWritableKeyPath<Wrapped, Member>) -> Binding<Member> where Wrapped: AnyObject {
Binding(get: {
wrappedValue[keyPath: keyPath]
}, set: { newValue in
wrappedValue[keyPath: keyPath] = newValue
})
}
}
}

// CHECK: define {{.*}}@main(