Skip to content

Commit 6908e9b

Browse files
committed
Annotate some Span and InlineArray's methods with semantics
1 parent 9ce8bb3 commit 6908e9b

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

include/swift/AST/SemanticAttrs.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,8 @@ SEMANTICS_ATTR(DELETE_IF_UNUSED, "sil.optimizer.delete_if_unused")
160160
// Force the use of the frame pointer for the specified function
161161
SEMANTICS_ATTR(USE_FRAME_POINTER, "use_frame_pointer")
162162

163+
SEMANTICS_ATTR(FIXED_STORAGE_CHECK_INDEX, "fixed_storage.check_index")
164+
SEMANTICS_ATTR(FIXED_STORAGE_GET_COUNT, "fixed_storage.get_count")
165+
163166
#undef SEMANTICS_ATTR
164167

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,8 @@ static std::optional<bool> shouldInlineGeneric(FullApplySite AI,
860860
// because they need to be preserved, so that the optimizer
861861
// can properly optimize a user code later.
862862
ModuleDecl *SwiftModule = Callee->getModule().getSwiftModule();
863-
if (Callee->hasSemanticsAttrThatStartsWith("array.") &&
863+
if ((Callee->hasSemanticsAttrThatStartsWith("array.") ||
864+
Callee->hasSemanticsAttrThatStartsWith("fixed_storage.")) &&
864865
(SwiftModule->isStdlibModule() || SwiftModule->isOnoneSupportModule()))
865866
return false;
866867

@@ -1167,8 +1168,9 @@ void SILPerformanceInliner::collectAppliesToInline(
11671168
Weight W(BlockWeight, WeightCorrections.lookup(AI));
11681169

11691170
if (decideInWarmBlock(AI, W, constTracker, NumCallerBlocks,
1170-
BBToWeightMap))
1171+
BBToWeightMap)) {
11711172
InitialCandidates.push_back(AI);
1173+
}
11721174
}
11731175
}
11741176

lib/SILOptimizer/Utils/PerformanceInlinerUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ SemanticFunctionLevel swift::getSemanticFunctionLevel(SILFunction *function) {
594594
//
595595
// Compiler "hints" and informational annotations (like remarks) should
596596
// ideally use a separate annotation rather than @_semantics.
597+
598+
if (isFixedStorageSemanticsCallKind(function)) {
599+
return SemanticFunctionLevel::Fundamental;
600+
}
601+
597602
switch (getArraySemanticsKind(function)) {
598603
case ArrayCallKind::kNone:
599604
return SemanticFunctionLevel::Transient;

stdlib/public/core/InlineArray.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ extension InlineArray where Element: ~Copyable {
236236
@available(SwiftStdlib 6.2, *)
237237
@_alwaysEmitIntoClient
238238
@_transparent
239+
@_semantics("fixed_storage.get_count")
240+
@inline(__always)
239241
public var count: Int {
240242
count
241243
}
@@ -320,6 +322,13 @@ extension InlineArray where Element: ~Copyable {
320322
i &- 1
321323
}
322324

325+
@_alwaysEmitIntoClient
326+
@_semantics("fixed_storage.check_index")
327+
@inline(__always)
328+
internal func _checkIndex(_ i: Int) {
329+
_precondition(indices.contains(i), "Index out of bounds")
330+
}
331+
323332
/// Accesses the element at the specified position.
324333
///
325334
/// The following example accesses an element of an array through its
@@ -345,15 +354,13 @@ extension InlineArray where Element: ~Copyable {
345354
public subscript(_ i: Int) -> Element {
346355
@_transparent
347356
unsafeAddress {
348-
_precondition(indices.contains(i), "Index out of bounds")
349-
357+
_checkIndex(i)
350358
return unsafe _address + i
351359
}
352360

353361
@_transparent
354362
unsafeMutableAddress {
355-
_precondition(indices.contains(i), "Index out of bounds")
356-
363+
_checkIndex(i)
357364
return unsafe _mutableAddress + i
358365
}
359366
}

stdlib/public/core/Span/Span.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ extension Span where Element: ~Copyable {
367367
///
368368
/// - Complexity: O(1)
369369
@_alwaysEmitIntoClient
370+
@_semantics("fixed_storage.get_count")
370371
public var count: Int { _count }
371372

372373
/// A Boolean value indicating whether the span is empty.
@@ -390,6 +391,12 @@ extension Span where Element: ~Copyable {
390391

391392
@available(SwiftStdlib 6.1, *)
392393
extension Span where Element: ~Copyable {
394+
@_semantics("fixed_storage.check_index")
395+
@inline(__always)
396+
@_alwaysEmitIntoClient
397+
internal func _checkIndex(_ position: Index) {
398+
_precondition(indices.contains(position), "Index out of bounds")
399+
}
393400

394401
/// Accesses the element at the specified position in the `Span`.
395402
///
@@ -401,7 +408,7 @@ extension Span where Element: ~Copyable {
401408
public subscript(_ position: Index) -> Element {
402409
//FIXME: change to unsafeRawAddress when ready
403410
unsafeAddress {
404-
_precondition(indices.contains(position), "Index out of bounds")
411+
_checkIndex(position)
405412
return unsafe _unsafeAddressOfElement(unchecked: position)
406413
}
407414
}
@@ -437,6 +444,15 @@ extension Span where Element: ~Copyable {
437444

438445
@available(SwiftStdlib 6.1, *)
439446
extension Span where Element: BitwiseCopyable {
447+
@_semantics("fixed_storage.check_index")
448+
@inline(__always)
449+
@_alwaysEmitIntoClient
450+
internal func _checkIndex(_ position: Index) {
451+
_precondition(
452+
UInt(bitPattern: position) < UInt(bitPattern: _count),
453+
"Index out of bounds"
454+
)
455+
}
440456

441457
/// Accesses the element at the specified position in the `Span`.
442458
///
@@ -447,10 +463,7 @@ extension Span where Element: BitwiseCopyable {
447463
@_alwaysEmitIntoClient
448464
public subscript(_ position: Index) -> Element {
449465
get {
450-
_precondition(
451-
UInt(bitPattern: position) < UInt(bitPattern: _count),
452-
"Index out of bounds"
453-
)
466+
_checkIndex(position)
454467
return unsafe self[unchecked: position]
455468
}
456469
}

0 commit comments

Comments
 (0)