Skip to content

SimplifyApply: don't do the raw-enum comparison optimization for custom RawRepresentable enums #81830

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
May 29, 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
4 changes: 3 additions & 1 deletion SwiftCompilerSources/Sources/AST/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public class NominalTypeDecl: GenericTypeDecl {
}
}

final public class EnumDecl: NominalTypeDecl {}
final public class EnumDecl: NominalTypeDecl {
public var hasRawType: Bool { bridged.Enum_hasRawType() }
}

final public class StructDecl: NominalTypeDecl {
public var hasUnreferenceableStorage: Bool { bridged.Struct_hasUnreferenceableStorage() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private func tryOptimizeEnumComparison(apply: ApplyInst, _ context: SimplifyCont
let lhs = apply.arguments[0]
let rhs = apply.arguments[1]
guard let enumDecl = lhs.type.nominal as? EnumDecl,
enumDecl.hasRawType,
!enumDecl.isResilient(in: apply.parentFunction),
!enumDecl.hasClangNode,
lhs.type.isAddress,
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 @@ -402,6 +402,7 @@ struct BridgedDeclObj {
BRIDGED_INLINE bool GenericType_isGenericAtAnyLevel() const;
BRIDGED_INLINE bool NominalType_isGlobalActor() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj NominalType_getValueTypeDestructor() const;
BRIDGED_INLINE bool Enum_hasRawType() const;
BRIDGED_INLINE bool Struct_hasUnreferenceableStorage() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType Class_getSuperclass() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj Class_getDestructor() 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 @@ -221,6 +221,10 @@ OptionalBridgedDeclObj BridgedDeclObj::NominalType_getValueTypeDestructor() cons
return {getAs<swift::NominalTypeDecl>()->getValueTypeDestructor()};
}

bool BridgedDeclObj::Enum_hasRawType() const {
return getAs<swift::EnumDecl>()->hasRawType();
}

bool BridgedDeclObj::Struct_hasUnreferenceableStorage() const {
return getAs<swift::StructDecl>()->hasUnreferenceableStorage();
}
Expand Down
25 changes: 25 additions & 0 deletions test/SILOptimizer/enum-comparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ func compare4(_ x: LargeEnum) -> Bool {
return x == .e3(28)
}

enum CustomRawValue: RawRepresentable {
case a, b, c

init(rawValue: Int) {
self = .a
}

@inline(never)
var rawValue: Int {
print(0)
return 0
}
}

// CHECK-LABEL: define {{.*}} i1 @"$s4test8compare5ySbAA14CustomRawValueOF"(i8 %0)
// CHECK: entry:
// CHECK-NEXT: call
@inline(never)
func compare5(_ x: CustomRawValue) -> Bool {
return x == .b
}

// OUT: 1: false
print("1: \(compareeq(.c, .long_case_name_for_testing))")

Expand Down Expand Up @@ -116,3 +138,6 @@ print("11: \(compare4(.e3(28)))")
// OUT: 12: false
print("12: \(compare4(.e3(27)))")

// OUT: 13: true
print("13: \(compare5(.a))")