Skip to content

Commit ca2e874

Browse files
authored
Merge pull request #17090 from slavapestov/inlinable-tweaks
Minor @inlinable improvements
2 parents 0ba027f + 4e0e462 commit ca2e874

File tree

12 files changed

+69
-28
lines changed

12 files changed

+69
-28
lines changed

docs/HighLevelSILOptimizations.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ Cloning code from the standard library
117117
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118118

119119
The Swift compiler can copy code from the standard library into the
120-
application for functions marked @_inlineable. This allows the optimizer to
120+
application for functions marked @inlinable. This allows the optimizer to
121121
inline calls from the stdlib and improve the performance of code that uses
122122
common operators such as '+=' or basic containers such as Array. However,
123123
importing code from the standard library can increase the binary size.
124124

125125
To prevent copying of functions from the standard library into the user
126-
program, make sure the function in question is not marked @_inlineable.
126+
program, make sure the function in question is not marked @inlinable.
127127

128128
Array
129129
~~~~~

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ class ASTContext final {
880880
/// This is usually the check you want; for example, when introducing
881881
/// a new language feature which is only visible in Swift 5, you would
882882
/// check for isSwiftVersionAtLeast(5).
883-
bool isSwiftVersionAtLeast(unsigned major) const {
884-
return LangOpts.isSwiftVersionAtLeast(major);
883+
bool isSwiftVersionAtLeast(unsigned major, unsigned minor = 0) const {
884+
return LangOpts.isSwiftVersionAtLeast(major, minor);
885885
}
886886

887887
private:

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ DECL_ATTR(_cdecl, CDecl,
320320
LongAttribute | UserInaccessible,
321321
63)
322322
SIMPLE_DECL_ATTR(usableFromInline, UsableFromInline,
323-
OnAbstractFunction | OnVar | OnSubscript | OnNominalType |
323+
OnAbstractFunction | OnVar | OnSubscript | OnNominalType | OnTypeAlias |
324324
LongAttribute,
325325
64)
326326
SIMPLE_DECL_ATTR(discardableResult, DiscardableResult,

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ namespace swift {
342342
/// This is usually the check you want; for example, when introducing
343343
/// a new language feature which is only visible in Swift 5, you would
344344
/// check for isSwiftVersionAtLeast(5).
345-
bool isSwiftVersionAtLeast(unsigned major) const {
346-
return EffectiveLanguageVersion.isVersionAtLeast(major);
345+
bool isSwiftVersionAtLeast(unsigned major, unsigned minor = 0) const {
346+
return EffectiveLanguageVersion.isVersionAtLeast(major, minor);
347347
}
348348

349349
/// Returns true if the given platform condition argument represents

include/swift/Basic/Version.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,17 @@ class Version {
112112

113113
/// Whether this version is greater than or equal to the given major version
114114
/// number.
115-
bool isVersionAtLeast(unsigned major) const {
116-
return !empty() && Components[0] >= major;
115+
bool isVersionAtLeast(unsigned major, unsigned minor = 0) const {
116+
switch (size()) {
117+
case 0:
118+
return false;
119+
case 1:
120+
return ((Components[0] == major && 0 == minor) ||
121+
(Components[0] > major));
122+
default:
123+
return ((Components[0] == major && Components[1] >= minor) ||
124+
(Components[0] > major));
125+
}
117126
}
118127

119128
/// Return this Version struct with minor and sub-minor components stripped

lib/Parse/ParseDecl.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,15 +1537,17 @@ bool Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc) {
15371537
// over to the alternate parsing path.
15381538
DeclAttrKind DK = DeclAttribute::getAttrKindFromString(Tok.getText());
15391539

1540-
auto checkInvalidAttrName = [&](StringRef invalidName, StringRef correctName,
1541-
DeclAttrKind kind, Diag<StringRef, StringRef> diag) {
1540+
auto checkInvalidAttrName = [&](StringRef invalidName,
1541+
StringRef correctName,
1542+
DeclAttrKind kind,
1543+
Optional<Diag<StringRef, StringRef>> diag = None) {
15421544
if (DK == DAK_Count && Tok.getText() == invalidName) {
1543-
// We renamed @availability to @available, so if we see the former,
1544-
// treat it as the latter and emit a Fix-It.
15451545
DK = kind;
1546-
1547-
diagnose(Tok, diag, invalidName, correctName)
1546+
1547+
if (diag) {
1548+
diagnose(Tok, *diag, invalidName, correctName)
15481549
.fixItReplace(Tok.getLoc(), correctName);
1550+
}
15491551
}
15501552
};
15511553

@@ -1556,14 +1558,17 @@ bool Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc) {
15561558
// Check if attr is inlineable, and suggest inlinable instead
15571559
checkInvalidAttrName("inlineable", "inlinable", DAK_Inlinable, diag::attr_name_close_match);
15581560

1559-
// In Swift 5 and above, these become hard errors. Otherwise, emit a
1560-
// warning for compatibility.
1561-
if (!Context.isSwiftVersionAtLeast(5)) {
1561+
// In Swift 5 and above, these become hard errors. In Swift 4.2, emit a
1562+
// warning for compatibility. Otherwise, don't diagnose at all.
1563+
if (Context.isSwiftVersionAtLeast(5)) {
1564+
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline, diag::attr_renamed);
1565+
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable, diag::attr_renamed);
1566+
} else if (Context.isSwiftVersionAtLeast(4, 2)) {
15621567
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline, diag::attr_renamed_warning);
15631568
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable, diag::attr_renamed_warning);
15641569
} else {
1565-
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline, diag::attr_renamed);
1566-
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable, diag::attr_renamed);
1570+
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline);
1571+
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable);
15671572
}
15681573

15691574
if (DK == DAK_Count && Tok.getText() == "warn_unused_result") {

stdlib/public/SwiftShims/Visibility.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
/// Note that @_silgen_name implementations must also be marked SWIFT_CC(swift).
119119
///
120120
/// SWIFT_RUNTIME_STDLIB_API functions are called by compiler-generated code
121-
/// or by @_inlineable Swift code.
121+
/// or by @inlinable Swift code.
122122
/// Such functions must be exported and must be supported forever as API.
123123
/// The function name should be prefixed with `swift_`.
124124
///
@@ -130,7 +130,7 @@
130130
/// SWIFT_RUNTIME_STDLIB_INTERNAL functions are called only by the stdlib.
131131
/// Such functions are internal and are not exported.
132132
/// FIXME(sil-serialize-all): _INTERNAL functions are also exported for now
133-
/// until the tide of @_inlineable is rolled back.
133+
/// until the tide of @inlinable is rolled back.
134134
/// They really should be LLVM_LIBRARY_VISIBILITY, not SWIFT_RUNTIME_EXPORT.
135135
#define SWIFT_RUNTIME_STDLIB_API SWIFT_RUNTIME_EXPORT
136136
#define SWIFT_RUNTIME_STDLIB_SPI SWIFT_RUNTIME_EXPORT

stdlib/public/core/BridgeObjectiveC.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ public func _getObjCTypeEncoding<T>(_ type: T.Type) -> UnsafePointer<Int8> {
649649
/// Convert `x` from its Objective-C representation to its Swift
650650
/// representation.
651651
/// COMPILER_INTRINSIC
652-
@_inlineable // FIXME(sil-serialize-all)
652+
@inlinable // FIXME(sil-serialize-all)
653653
public func _forceBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable> (
654654
_ x: T._ObjectiveCType,
655655
_: T.Type
@@ -662,7 +662,7 @@ public func _forceBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable> (
662662
/// Attempt to convert `x` from its Objective-C representation to its Swift
663663
/// representation.
664664
/// COMPILER_INTRINSIC
665-
@_inlineable // FIXME(sil-serialize-all)
665+
@inlinable // FIXME(sil-serialize-all)
666666
public func _conditionallyBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable>(
667667
_ x: T._ObjectiveCType,
668668
_: T.Type
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22
import ImportAsMember.Class
33

4-
@_versioned
4+
@usableFromInline
55
internal func foo(_ x: IncompleteImportTargetName) -> Any { return x }
66

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 3
2+
// RUN: %target-typecheck-verify-swift -swift-version 4
3+
4+
// In -swift-version 4 mode, the old spelling is silently parsed as the new spelling.
5+
6+
@_inlineable public func oldInlinableFunction() {}
7+
@_versioned func oldVersionedFunction() {}

test/Compatibility/attr_inlinable_old_spelling.swift renamed to test/Compatibility/attr_inlinable_old_spelling_42.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 3
2-
// RUN: %target-typecheck-verify-swift -swift-version 4
31
// RUN: %target-typecheck-verify-swift -swift-version 4.2
42

5-
// Until -swift-version 5 mode, the old spelling only produces a warning.
3+
// In -swift-version 4.2 mode, the old spelling produces a warning.
64

75
@_inlineable public func oldInlinableFunction() {}
86
// expected-warning@-1 {{'@_inlineable' has been renamed to '@inlinable'}}{{2-13=inlinable}}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// None of this is enforced for now, but make sure we don't crash or
4+
// do anything stupid when a typealias is annotated with @usableFromInline.
5+
6+
private typealias PrivateAlias = Int
7+
8+
internal typealias InternalAlias = Int
9+
10+
@usableFromInline typealias UsableFromInlineAlias = Int
11+
12+
public typealias PublicAlias = Int
13+
14+
@inlinable public func f() {
15+
_ = PrivateAlias.self
16+
17+
_ = InternalAlias.self
18+
19+
_ = UsableFromInlineAlias.self
20+
21+
_ = PublicAlias.self
22+
}

0 commit comments

Comments
 (0)