Skip to content

DeinitDevirtualizer: bail out for C++ move-only types #78941

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 2 commits into from
Jan 27, 2025
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
3 changes: 3 additions & 0 deletions SwiftCompilerSources/Sources/AST/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class Decl: CustomStringConvertible, Hashable {

public var description: String { String(taking: bridged.getDebugDescription()) }

// True if this declaration is imported from C/C++/ObjC.
public var hasClangNode: Bool { bridged.hasClangNode() }

public static func ==(lhs: Decl, rhs: Decl) -> Bool { lhs === rhs }

public func hash(into hasher: inout Hasher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ private func devirtualize(destroy: some DevirtualizableDestroy, _ context: some
return true
}

// We cannot de-virtualize C++ destructor calls of C++ move-only types because we cannot get
// its destructor (`nominal.valueTypeDestructor` is nil).
if nominal.hasClangNode {
return false
}

if nominal.valueTypeDestructor != nil && !destroy.shouldDropDeinit {
guard let deinitFunc = context.lookupDeinit(ofNominal: nominal) else {
return false
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ struct BridgedDeclObj {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Type_getName() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Value_getUserFacingName() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSourceLoc Value_getNameLoc() const;
BRIDGED_INLINE bool hasClangNode() const;
BRIDGED_INLINE bool Value_isObjC() const;
BRIDGED_INLINE bool GenericType_isGenericAtAnyLevel() const;
BRIDGED_INLINE bool NominalType_isGlobalActor() const;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/ASTBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ BridgedSourceLoc BridgedDeclObj::Value_getNameLoc() const {
return BridgedSourceLoc(getAs<swift::ValueDecl>()->getNameLoc().getOpaquePointerValue());
}

bool BridgedDeclObj::hasClangNode() const {
return unbridged()->hasClangNode();
}

bool BridgedDeclObj::Value_isObjC() const {
return getAs<swift::ValueDecl>()->isObjC();
}
Expand Down
11 changes: 11 additions & 0 deletions test/SILOptimizer/Inputs/CXXTypesWithUserProvidedDestructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ struct HasMemberWithUserProvidedDestructor {
~HasMemberWithUserProvidedDestructor() {}
};

void foo();

struct NonCopyable {
NonCopyable(int x) : x(x) {}
NonCopyable(const NonCopyable &) = delete;
NonCopyable(NonCopyable &&other) : x(other.x) { other.x = -123; }
~NonCopyable() { foo(); }

int x;
};

#endif // TEST_SIL_OPTIMIZER_CXX_WITH_CUSTOM_DESTRUCTOR_H
13 changes: 12 additions & 1 deletion test/SILOptimizer/devirt_deinits.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-sil-opt -sil-print-types %s -deinit-devirtualizer -module-name=test | %FileCheck %s
// RUN: %target-sil-opt -sil-print-types %s -deinit-devirtualizer -module-name=test -enable-experimental-cxx-interop -I %S/Inputs | %FileCheck %s

// REQUIRES: swift_in_compiler

Expand All @@ -7,6 +7,7 @@ sil_stage canonical
import Builtin
import Swift
import SwiftShims
import CXXTypesWithUserProvidedDestructor

@inline(never) func log(_ s: StaticString)

Expand Down Expand Up @@ -283,6 +284,16 @@ bb0(%0 : $*T):
return %r : $()
}

// CHECK-LABEL: sil [ossa] @nodevirt_of_cxx_type :
// CHECK: destroy_addr %0
// CHECK: } // end sil function 'nodevirt_of_cxx_type'
sil [ossa] @nodevirt_of_cxx_type : $@convention(thin) (@in NonCopyable) -> () {
bb0(%0 : $*NonCopyable):
destroy_addr %0
%5 = tuple ()
return %5
}

// CHECK-LABEL: sil @test_non_ossa :
// CHECK: destroy_addr %0
// CHECK: } // end sil function 'test_non_ossa'
Expand Down