Skip to content

Commit 926bd6c

Browse files
authored
Merge pull request #75615 from eeckstein/fix-atomic-effects
SIL: allow atomic operations in `@_noLocks` functions
2 parents d3cfaa3 + 50e22f9 commit 926bd6c

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ extension BuiltinInst : OnoneSimplifyable {
3232
.Strideof,
3333
.Alignof:
3434
optimizeTargetTypeConst(context)
35-
case .DestroyArray,
36-
.CopyArray,
35+
case .DestroyArray:
36+
if let elementType = substitutionMap.replacementTypes[0],
37+
elementType.isTrivial(in: parentFunction)
38+
{
39+
context.erase(instruction: self)
40+
return
41+
}
42+
optimizeArgumentToThinMetatype(argument: 0, context)
43+
case .CopyArray,
3744
.TakeArrayNoAlias,
3845
.TakeArrayFrontToBack,
3946
.TakeArrayBackToFront,

lib/SIL/Utils/InstructionUtils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,7 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
728728
case SILInstructionKind::AllocStackInst:
729729
case SILInstructionKind::AllocVectorInst:
730730
case SILInstructionKind::ProjectBoxInst:
731-
if (!cast<SingleValueInstruction>(inst)->getType().
732-
isLoadable(*inst->getFunction())) {
731+
if (cast<SingleValueInstruction>(inst)->getType().hasArchetype()) {
733732
impactType = cast<SingleValueInstruction>(inst)->getType();
734733
return RuntimeEffect::MetaData;
735734
}
@@ -1028,7 +1027,7 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
10281027
case BuiltinValueKind::AtomicLoad:
10291028
case BuiltinValueKind::AtomicStore:
10301029
case BuiltinValueKind::AtomicRMW:
1031-
return RuntimeEffect::Locking;
1030+
return RuntimeEffect::NoEffect;
10321031
case BuiltinValueKind::DestroyArray:
10331032
return RuntimeEffect::Releasing;
10341033
case BuiltinValueKind::CopyArray:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-swift-frontend -parse-as-library -disable-availability-checking -emit-sil %s -o /dev/null
2+
3+
// REQUIRES: synchronization
4+
5+
import Synchronization
6+
7+
// Check that atomics work in no-locks mode.
8+
9+
@_noLocks
10+
func testFence() {
11+
atomicMemoryFence(ordering: .acquiring)
12+
atomicMemoryFence(ordering: .releasing)
13+
atomicMemoryFence(ordering: .acquiringAndReleasing)
14+
atomicMemoryFence(ordering: .sequentiallyConsistent)
15+
}
16+
17+
@_noLocks
18+
func testLoadStore() -> Int {
19+
let x = Atomic(0)
20+
x.store(27, ordering: .relaxed)
21+
x.compareExchange(expected: 27, desired: 42, successOrdering: .relaxed, failureOrdering: .relaxed)
22+
return x.load(ordering: .acquiring)
23+
}
24+
25+
@_noLocks
26+
func testRMW(_ b: Bool) -> (Bool, Bool) {
27+
let x = Atomic(false)
28+
return x.logicalOr(true, ordering: .relaxed)
29+
}
30+

test/SILOptimizer/simplify_builtin.sil

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,12 @@ bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
605605
return %0 : $Builtin.RawPointer
606606
}
607607

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

0 commit comments

Comments
 (0)