Skip to content

Commit

Permalink
SIL: allow atomic operations in @_noLocks functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eeckstein committed Aug 1, 2024
1 parent e3de408 commit 50e22f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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)
}

0 comments on commit 50e22f9

Please sign in to comment.