Skip to content

Commit

Permalink
Merge pull request #75615 from eeckstein/fix-atomic-effects
Browse files Browse the repository at this point in the history
SIL: allow atomic operations in `@_noLocks` functions
  • Loading branch information
eeckstein authored Aug 1, 2024
2 parents d3cfaa3 + 50e22f9 commit 926bd6c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ extension BuiltinInst : OnoneSimplifyable {
.Strideof,
.Alignof:
optimizeTargetTypeConst(context)
case .DestroyArray,
.CopyArray,
case .DestroyArray:
if let elementType = substitutionMap.replacementTypes[0],
elementType.isTrivial(in: parentFunction)
{
context.erase(instruction: self)
return
}
optimizeArgumentToThinMetatype(argument: 0, context)
case .CopyArray,
.TakeArrayNoAlias,
.TakeArrayFrontToBack,
.TakeArrayBackToFront,
Expand Down
5 changes: 2 additions & 3 deletions lib/SIL/Utils/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,7 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
case SILInstructionKind::AllocStackInst:
case SILInstructionKind::AllocVectorInst:
case SILInstructionKind::ProjectBoxInst:
if (!cast<SingleValueInstruction>(inst)->getType().
isLoadable(*inst->getFunction())) {
if (cast<SingleValueInstruction>(inst)->getType().hasArchetype()) {
impactType = cast<SingleValueInstruction>(inst)->getType();
return RuntimeEffect::MetaData;
}
Expand Down Expand Up @@ -1028,7 +1027,7 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
case BuiltinValueKind::AtomicLoad:
case BuiltinValueKind::AtomicStore:
case BuiltinValueKind::AtomicRMW:
return RuntimeEffect::Locking;
return RuntimeEffect::NoEffect;
case BuiltinValueKind::DestroyArray:
return RuntimeEffect::Releasing;
case BuiltinValueKind::CopyArray:
Expand Down
30 changes: 30 additions & 0 deletions test/SILOptimizer/performance-annotations-atomics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-swift-frontend -parse-as-library -disable-availability-checking -emit-sil %s -o /dev/null

// REQUIRES: synchronization

import Synchronization

// Check that atomics work in no-locks mode.

@_noLocks
func testFence() {
atomicMemoryFence(ordering: .acquiring)
atomicMemoryFence(ordering: .releasing)
atomicMemoryFence(ordering: .acquiringAndReleasing)
atomicMemoryFence(ordering: .sequentiallyConsistent)
}

@_noLocks
func testLoadStore() -> Int {
let x = Atomic(0)
x.store(27, ordering: .relaxed)
x.compareExchange(expected: 27, desired: 42, successOrdering: .relaxed, failureOrdering: .relaxed)
return x.load(ordering: .acquiring)
}

@_noLocks
func testRMW(_ b: Bool) -> (Bool, Bool) {
let x = Atomic(false)
return x.logicalOr(true, ordering: .relaxed)
}

9 changes: 9 additions & 0 deletions test/SILOptimizer/simplify_builtin.sil
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,12 @@ bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
return %0 : $Builtin.RawPointer
}

// CHECK-LABEL: sil @remove_trivial_destroy_array
// CHECK-NOT: builtin
// CHECK: } // end sil function 'remove_trivial_destroy_array'
sil @remove_trivial_destroy_array : $@convention(thin) (Builtin.RawPointer, Builtin.Word) -> Builtin.RawPointer {
bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
%2 = metatype $@thin Int.Type
%3 = builtin "destroyArray"<Int>(%2 : $@thin Int.Type, %0 : $Builtin.RawPointer, %1 : $Builtin.Word) : $()
return %0 : $Builtin.RawPointer
}

0 comments on commit 926bd6c

Please sign in to comment.