Skip to content

Commit d29da0a

Browse files
authored
Merge pull request swiftlang#81830 from eeckstein/fix-simplify-apply
SimplifyApply: don't do the raw-enum comparison optimization for custom RawRepresentable enums
2 parents 8b4740f + 1a5ea19 commit d29da0a

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public class NominalTypeDecl: GenericTypeDecl {
5858
}
5959
}
6060

61-
final public class EnumDecl: NominalTypeDecl {}
61+
final public class EnumDecl: NominalTypeDecl {
62+
public var hasRawType: Bool { bridged.Enum_hasRawType() }
63+
}
6264

6365
final public class StructDecl: NominalTypeDecl {
6466
public var hasUnreferenceableStorage: Bool { bridged.Struct_hasUnreferenceableStorage() }

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyApply.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ private func tryOptimizeEnumComparison(apply: ApplyInst, _ context: SimplifyCont
135135
let lhs = apply.arguments[0]
136136
let rhs = apply.arguments[1]
137137
guard let enumDecl = lhs.type.nominal as? EnumDecl,
138+
enumDecl.hasRawType,
138139
!enumDecl.isResilient(in: apply.parentFunction),
139140
!enumDecl.hasClangNode,
140141
lhs.type.isAddress,

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ struct BridgedDeclObj {
402402
BRIDGED_INLINE bool GenericType_isGenericAtAnyLevel() const;
403403
BRIDGED_INLINE bool NominalType_isGlobalActor() const;
404404
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj NominalType_getValueTypeDestructor() const;
405+
BRIDGED_INLINE bool Enum_hasRawType() const;
405406
BRIDGED_INLINE bool Struct_hasUnreferenceableStorage() const;
406407
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType Class_getSuperclass() const;
407408
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj Class_getDestructor() const;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ OptionalBridgedDeclObj BridgedDeclObj::NominalType_getValueTypeDestructor() cons
221221
return {getAs<swift::NominalTypeDecl>()->getValueTypeDestructor()};
222222
}
223223

224+
bool BridgedDeclObj::Enum_hasRawType() const {
225+
return getAs<swift::EnumDecl>()->hasRawType();
226+
}
227+
224228
bool BridgedDeclObj::Struct_hasUnreferenceableStorage() const {
225229
return getAs<swift::StructDecl>()->hasUnreferenceableStorage();
226230
}

test/SILOptimizer/enum-comparison.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ func compare4(_ x: LargeEnum) -> Bool {
8080
return x == .e3(28)
8181
}
8282

83+
enum CustomRawValue: RawRepresentable {
84+
case a, b, c
85+
86+
init(rawValue: Int) {
87+
self = .a
88+
}
89+
90+
@inline(never)
91+
var rawValue: Int {
92+
print(0)
93+
return 0
94+
}
95+
}
96+
97+
// CHECK-LABEL: define {{.*}} i1 @"$s4test8compare5ySbAA14CustomRawValueOF"(i8 %0)
98+
// CHECK: entry:
99+
// CHECK-NEXT: call
100+
@inline(never)
101+
func compare5(_ x: CustomRawValue) -> Bool {
102+
return x == .b
103+
}
104+
83105
// OUT: 1: false
84106
print("1: \(compareeq(.c, .long_case_name_for_testing))")
85107

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

141+
// OUT: 13: true
142+
print("13: \(compare5(.a))")
143+

0 commit comments

Comments
 (0)