Skip to content

[4.2] Two @inlinable rawValue optimizations #16082

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
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
22 changes: 22 additions & 0 deletions lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ static VarDecl *deriveRawRepresentable_raw(TypeChecker &tc,
addGetterToReadOnlyDerivedProperty(tc, propDecl, rawType);
getterDecl->setBodySynthesizer(&deriveBodyRawRepresentable_raw);

// If the containing module is not resilient, make sure clients can get at
// the raw value without function call overhead.
if (parentDC->getParentModule()->getResilienceStrategy() !=
ResilienceStrategy::Resilient) {
AccessScope access =
enumDecl->getFormalAccessScope(nullptr,
/*treatUsableFromInlineAsPublic*/true);
if (access.isPublic())
getterDecl->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
}

auto dc = cast<IterableDeclContext>(parentDecl);
dc->addMember(getterDecl);
dc->addMember(propDecl);
Expand Down Expand Up @@ -343,6 +354,17 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
initDecl->copyFormalAccessFrom(enumDecl, /*sourceIsParentContext*/true);
initDecl->setValidationStarted();

// If the containing module is not resilient, make sure clients can construct
// an instance without function call overhead.
if (parentDC->getParentModule()->getResilienceStrategy() !=
ResilienceStrategy::Resilient) {
AccessScope access =
enumDecl->getFormalAccessScope(nullptr,
/*treatUsableFromInlineAsPublic*/true);
if (access.isPublic())
initDecl->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
}

tc.Context.addSynthesizedDecl(initDecl);

cast<IterableDeclContext>(parentDecl)->addMember(initDecl);
Expand Down
5 changes: 4 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,10 @@ void AttributeChecker::visitUsableFromInlineAttr(UsableFromInlineAttr *attr) {

// On internal declarations, @inlinable implies @usableFromInline.
if (VD->getAttrs().hasAttribute<InlinableAttr>()) {
diagnoseAndRemoveAttr(attr, diag::inlinable_implies_usable_from_inline);
if (attr->isImplicit())
attr->setInvalid();
else
diagnoseAndRemoveAttr(attr, diag::inlinable_implies_usable_from_inline);
return;
}
}
Expand Down
25 changes: 25 additions & 0 deletions stdlib/public/core/FloatingPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,31 @@ public enum FloatingPointSign: Int {

/// The sign for a negative value.
case minus

// Explicit declarations of otherwise-synthesized members to make them
// @inlinable, promising that we will never change the implementation.

@inlinable
public init?(rawValue: Int) {
switch rawValue {
case 0: self = .plus
case 1: self = .minus
default: return nil
}
}

@inlinable
public var rawValue: Int {
switch self {
case .plus: return 0
case .minus: return 1
}
}

@inlinable
public static func ==(a: FloatingPointSign, b: FloatingPointSign) -> Bool {
return a.rawValue == b.rawValue
}
}

/// The IEEE 754 floating-point classes.
Expand Down
12 changes: 12 additions & 0 deletions test/SILGen/enum_raw_representable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-frontend -emit-silgen -enable-sil-ownership %s | %FileCheck %s
// RUN: %target-swift-frontend -emit-silgen -enable-sil-ownership -enable-resilience %s | %FileCheck -check-prefix=CHECK-RESILIENT %s

public enum E: Int {
case a, b, c
}

// CHECK-DAG: sil [serialized] @$S22enum_raw_representable1EO0B5ValueACSgSi_tcfC
// CHECK-DAG: sil [serialized] @$S22enum_raw_representable1EO0B5ValueSivg

// CHECK-RESILIENT-DAG: sil @$S22enum_raw_representable1EO0B5ValueACSgSi_tcfC
// CHECK-RESILIENT-DAG: sil @$S22enum_raw_representable1EO0B5ValueSivg