Skip to content

Commit ac38f00

Browse files
authored
Merge pull request #16082 from jrose-apple/4.2-inlinable-rawValue-optimizations
[4.2] Two @inlinable rawValue optimizations
2 parents ec2f4dc + f5667f4 commit ac38f00

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ static VarDecl *deriveRawRepresentable_raw(TypeChecker &tc,
145145
addGetterToReadOnlyDerivedProperty(tc, propDecl, rawType);
146146
getterDecl->setBodySynthesizer(&deriveBodyRawRepresentable_raw);
147147

148+
// If the containing module is not resilient, make sure clients can get at
149+
// the raw value without function call overhead.
150+
if (parentDC->getParentModule()->getResilienceStrategy() !=
151+
ResilienceStrategy::Resilient) {
152+
AccessScope access =
153+
enumDecl->getFormalAccessScope(nullptr,
154+
/*treatUsableFromInlineAsPublic*/true);
155+
if (access.isPublic())
156+
getterDecl->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
157+
}
158+
148159
auto dc = cast<IterableDeclContext>(parentDecl);
149160
dc->addMember(getterDecl);
150161
dc->addMember(propDecl);
@@ -343,6 +354,17 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
343354
initDecl->copyFormalAccessFrom(enumDecl, /*sourceIsParentContext*/true);
344355
initDecl->setValidationStarted();
345356

357+
// If the containing module is not resilient, make sure clients can construct
358+
// an instance without function call overhead.
359+
if (parentDC->getParentModule()->getResilienceStrategy() !=
360+
ResilienceStrategy::Resilient) {
361+
AccessScope access =
362+
enumDecl->getFormalAccessScope(nullptr,
363+
/*treatUsableFromInlineAsPublic*/true);
364+
if (access.isPublic())
365+
initDecl->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
366+
}
367+
346368
tc.Context.addSynthesizedDecl(initDecl);
347369

348370
cast<IterableDeclContext>(parentDecl)->addMember(initDecl);

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,10 @@ void AttributeChecker::visitUsableFromInlineAttr(UsableFromInlineAttr *attr) {
19841984

19851985
// On internal declarations, @inlinable implies @usableFromInline.
19861986
if (VD->getAttrs().hasAttribute<InlinableAttr>()) {
1987-
diagnoseAndRemoveAttr(attr, diag::inlinable_implies_usable_from_inline);
1987+
if (attr->isImplicit())
1988+
attr->setInvalid();
1989+
else
1990+
diagnoseAndRemoveAttr(attr, diag::inlinable_implies_usable_from_inline);
19881991
return;
19891992
}
19901993
}

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,31 @@ public enum FloatingPointSign: Int {
12251225

12261226
/// The sign for a negative value.
12271227
case minus
1228+
1229+
// Explicit declarations of otherwise-synthesized members to make them
1230+
// @inlinable, promising that we will never change the implementation.
1231+
1232+
@inlinable
1233+
public init?(rawValue: Int) {
1234+
switch rawValue {
1235+
case 0: self = .plus
1236+
case 1: self = .minus
1237+
default: return nil
1238+
}
1239+
}
1240+
1241+
@inlinable
1242+
public var rawValue: Int {
1243+
switch self {
1244+
case .plus: return 0
1245+
case .minus: return 1
1246+
}
1247+
}
1248+
1249+
@inlinable
1250+
public static func ==(a: FloatingPointSign, b: FloatingPointSign) -> Bool {
1251+
return a.rawValue == b.rawValue
1252+
}
12281253
}
12291254

12301255
/// The IEEE 754 floating-point classes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-silgen -enable-sil-ownership %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -emit-silgen -enable-sil-ownership -enable-resilience %s | %FileCheck -check-prefix=CHECK-RESILIENT %s
3+
4+
public enum E: Int {
5+
case a, b, c
6+
}
7+
8+
// CHECK-DAG: sil [serialized] @$S22enum_raw_representable1EO0B5ValueACSgSi_tcfC
9+
// CHECK-DAG: sil [serialized] @$S22enum_raw_representable1EO0B5ValueSivg
10+
11+
// CHECK-RESILIENT-DAG: sil @$S22enum_raw_representable1EO0B5ValueACSgSi_tcfC
12+
// CHECK-RESILIENT-DAG: sil @$S22enum_raw_representable1EO0B5ValueSivg

0 commit comments

Comments
 (0)