Skip to content

Commit c2866b3

Browse files
committed
Sema: add a @_semantics attribute to synthesized enum comparison functions
So that optimizations can identify and deal with them.
1 parent cac594f commit c2866b3

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

include/swift/AST/SemanticAttrs.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_PARTIAL_NEVER,
7373
"optimize.sil.specialize.generic.partial.never")
7474
SEMANTICS_ATTR(OPTIMIZE_SIL_INLINE_CONSTANT_ARGUMENTS,
7575
"optimize.sil.inline.constant.arguments")
76+
SEMANTICS_ATTR(DERIVED_ENUM_EQUALS,
77+
"derived_enum_equals")
7678
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
7779
"optimize.sil.specialize.generic.size.never")
7880
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_OWNED2GUARANTEE_NEVER,

lib/Sema/DerivedConformance/DerivedConformanceEquatableHashable.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,12 @@ deriveEquatable_eq(
394394
auto boolTy = C.getBoolType();
395395

396396
Identifier generatedIdentifier;
397+
bool isDerivedEnumEquals = false;
397398
if (parentDC->getParentModule()->isResilient()) {
398399
generatedIdentifier = C.Id_EqualsOperator;
399400
} else if (selfIfaceTy->getEnumOrBoundGenericEnum()) {
400401
generatedIdentifier = C.Id_derived_enum_equals;
402+
isDerivedEnumEquals = true;
401403
} else {
402404
assert(selfIfaceTy->getStructOrBoundGenericStruct());
403405
generatedIdentifier = C.Id_derived_struct_equals;
@@ -411,6 +413,9 @@ deriveEquatable_eq(
411413
/*GenericParams=*/nullptr, params, boolTy, parentDC);
412414
eqDecl->setUserAccessible(false);
413415
eqDecl->setSynthesized();
416+
if (isDerivedEnumEquals) {
417+
eqDecl->getAttrs().add(new (C) SemanticsAttr("derived_enum_equals", SourceLoc(), SourceRange(), /*Implicit=*/true));
418+
}
414419

415420
// Add the @_implements(Equatable, ==(_:_:)) attribute
416421
if (generatedIdentifier != C.Id_EqualsOperator) {

test/SILGen/synthesized_conformance_class.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Nonfinal<T> {
5656

5757
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}11stringValueAFyx_GSgSS_tcfC : $@convention(method) <T> (@owned String, @thin Final<T>.CodingKeys.Type) -> Optional<Final<T>.CodingKeys> {
5858
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}8intValueAFyx_GSgSi_tcfC : $@convention(method) <T> (Int, @thin Final<T>.CodingKeys.Type) -> Optional<Final<T>.CodingKeys> {
59-
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}21__derived_enum_equalsySbAFyx_G_AHtFZ : $@convention(method) <T> (Final<T>.CodingKeys, Final<T>.CodingKeys, @thin Final<T>.CodingKeys.Type) -> Bool {
59+
// CHECK-LABEL: sil private [_semantics "derived_enum_equals"] [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}21__derived_enum_equalsySbAFyx_G_AHtFZ : $@convention(method) <T> (Final<T>.CodingKeys, Final<T>.CodingKeys, @thin Final<T>.CodingKeys.Type) -> Bool {
6060
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}4hash4intoys6HasherVz_tF : $@convention(method) <T> (@inout Hasher, Final<T>.CodingKeys) -> () {
6161
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}9hashValueSivg : $@convention(method) <T> (Final<T>.CodingKeys) -> Int {
6262
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}8intValueSiSgvg : $@convention(method) <T> (Final<T>.CodingKeys) -> Optional<Int> {

test/SILGen/synthesized_conformance_enum.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ enum NoValues {
3737
extension Enum: Equatable where T: Equatable {}
3838
// CHECK-FRAGILE-LABEL: // static Enum<A>.__derived_enum_equals(_:_:)
3939
// CHECK-FRAGILE-NEXT: // Isolation: unspecified
40-
// CHECK-FRAGILE-NEXT: sil hidden [ossa] @$s28synthesized_conformance_enum4EnumOAASQRzlE010__derived_C7_equalsySbACyxG_AEtFZ : $@convention(method) <T where T : Equatable> (@in_guaranteed Enum<T>, @in_guaranteed Enum<T>, @thin Enum<T>.Type) -> Bool {
40+
// CHECK-FRAGILE-NEXT: sil hidden [_semantics "derived_enum_equals"] [ossa] @$s28synthesized_conformance_enum4EnumOAASQRzlE010__derived_C7_equalsySbACyxG_AEtFZ : $@convention(method) <T where T : Equatable> (@in_guaranteed Enum<T>, @in_guaranteed Enum<T>, @thin Enum<T>.Type) -> Bool {
4141
// CHECK-RESILIENT-LABEL: // static Enum<A>.== infix(_:_:)
4242
// CHECK-RESILIENT-NEXT: // Isolation: unspecified
4343
// CHECK-RESILIENT-NEXT: sil hidden [ossa] @$s28synthesized_conformance_enum4EnumOAASQRzlE2eeoiySbACyxG_AEtFZ : $@convention(method) <T where T : Equatable> (@in_guaranteed Enum<T>, @in_guaranteed Enum<T>, @thin Enum<T>.Type) -> Bool {

0 commit comments

Comments
 (0)