Skip to content

Commit 6c1eec8

Browse files
authored
Merge pull request #7557 from mtake/enhance-assume-single-threaded
Enhance -assume-single-threaded option (SR-3945)
2 parents 958cea5 + 2741535 commit 6c1eec8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+702
-231
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ option(SWIFT_STDLIB_ENABLE_RESILIENCE
270270
"Build the standard libraries and overlays with resilience enabled; see docs/LibraryEvolution.rst"
271271
FALSE)
272272

273+
option(SWIFT_STDLIB_USE_NONATOMIC_RC
274+
"Build the standard libraries and overlays with nonatomic reference count operations enabled"
275+
FALSE)
276+
273277
option(SWIFT_STDLIB_ENABLE_SIL_OWNERSHIP
274278
"Build the standard libraries and overlays with sil ownership enabled."
275279
FALSE)

cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ function(_compile_swift_files
239239
list(APPEND swift_flags "-Xfrontend" "-enable-resilience")
240240
endif()
241241

242+
if(SWIFT_STDLIB_USE_NONATOMIC_RC)
243+
list(APPEND swift_flags "-Xfrontend" "-assume-single-threaded")
244+
endif()
245+
242246
if(SWIFT_STDLIB_ENABLE_SIL_OWNERSHIP AND SWIFTFILE_IS_STDLIB)
243247
list(APPEND swift_flags "-Xfrontend" "-enable-sil-ownership")
244248
endif()

include/swift/Runtime/HeapObject.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,16 @@ SWIFT_RT_ENTRY_VISIBILITY
510510
void swift_unownedRelease(HeapObject *value)
511511
SWIFT_CC(RegisterPreservingCC);
512512

513+
/// Increment the unowned retain count.
514+
SWIFT_RT_ENTRY_VISIBILITY
515+
void swift_nonatomic_unownedRetain(HeapObject *value)
516+
SWIFT_CC(RegisterPreservingCC);
517+
518+
/// Decrement the unowned retain count.
519+
SWIFT_RT_ENTRY_VISIBILITY
520+
void swift_nonatomic_unownedRelease(HeapObject *value)
521+
SWIFT_CC(RegisterPreservingCC);
522+
513523
/// Increment the unowned retain count by n.
514524
SWIFT_RT_ENTRY_VISIBILITY
515525
void swift_unownedRetain_n(HeapObject *value, int n)
@@ -520,19 +530,42 @@ SWIFT_RT_ENTRY_VISIBILITY
520530
void swift_unownedRelease_n(HeapObject *value, int n)
521531
SWIFT_CC(RegisterPreservingCC);
522532

533+
/// Increment the unowned retain count by n.
534+
SWIFT_RT_ENTRY_VISIBILITY
535+
void swift_nonatomic_unownedRetain_n(HeapObject *value, int n)
536+
SWIFT_CC(RegisterPreservingCC);
537+
538+
/// Decrement the unowned retain count by n.
539+
SWIFT_RT_ENTRY_VISIBILITY
540+
void swift_nonatomic_unownedRelease_n(HeapObject *value, int n)
541+
SWIFT_CC(RegisterPreservingCC);
542+
523543
/// Increment the strong retain count of an object, aborting if it has
524544
/// been deallocated.
525545
SWIFT_RT_ENTRY_VISIBILITY
526546
void swift_unownedRetainStrong(HeapObject *value)
527547
SWIFT_CC(RegisterPreservingCC);
528548

549+
/// Increment the strong retain count of an object, aborting if it has
550+
/// been deallocated.
551+
SWIFT_RT_ENTRY_VISIBILITY
552+
void swift_nonatomic_unownedRetainStrong(HeapObject *value)
553+
SWIFT_CC(RegisterPreservingCC);
554+
529555
/// Increment the strong retain count of an object which may have been
530556
/// deallocated, aborting if it has been deallocated, and decrement its
531557
/// unowned reference count.
532558
SWIFT_RT_ENTRY_VISIBILITY
533559
void swift_unownedRetainStrongAndRelease(HeapObject *value)
534560
SWIFT_CC(RegisterPreservingCC);
535561

562+
/// Increment the strong retain count of an object which may have been
563+
/// deallocated, aborting if it has been deallocated, and decrement its
564+
/// unowned reference count.
565+
SWIFT_RT_ENTRY_VISIBILITY
566+
void swift_nonatomic_unownedRetainStrongAndRelease(HeapObject *value)
567+
SWIFT_CC(RegisterPreservingCC);
568+
536569
/// Aborts if the object has been deallocated.
537570
SWIFT_RUNTIME_EXPORT
538571
void swift_unownedCheck(HeapObject *value);

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ FUNCTION(NativeUnownedRelease, swift_unownedRelease, RegisterPreservingCC,
369369
ARGS(RefCountedPtrTy),
370370
ATTRS(NoUnwind))
371371

372+
// void swift_nonatomic_unownedRetain(void *ptr);
373+
FUNCTION(NonAtomicNativeUnownedRetain, swift_nonatomic_unownedRetain,
374+
RegisterPreservingCC,
375+
RETURNS(VoidTy),
376+
ARGS(RefCountedPtrTy),
377+
ATTRS(NoUnwind))
378+
379+
// void swift_nonatomic_unownedRelease(void *ptr);
380+
FUNCTION(NonAtomicNativeUnownedRelease, swift_nonatomic_unownedRelease,
381+
RegisterPreservingCC,
382+
RETURNS(VoidTy),
383+
ARGS(RefCountedPtrTy),
384+
ATTRS(NoUnwind))
385+
372386
// void swift_unownedRetain_n(void *ptr, int32_t n);
373387
FUNCTION(UnownedRetainN, swift_unownedRetain_n,
374388
RegisterPreservingCC,
@@ -383,19 +397,47 @@ FUNCTION(UnownedReleaseN, swift_unownedRelease_n,
383397
ARGS(RefCountedPtrTy, Int32Ty),
384398
ATTRS(NoUnwind))
385399

400+
// void swift_nonatomic_unownedRetain_n(void *ptr, int32_t n);
401+
FUNCTION(NonAtomicUnownedRetainN, swift_nonatomic_unownedRetain_n,
402+
RegisterPreservingCC,
403+
RETURNS(VoidTy),
404+
ARGS(RefCountedPtrTy, Int32Ty),
405+
ATTRS(NoUnwind))
406+
407+
// void swift_nonatomic_unownedRelease_n(void *ptr, int32_t n);
408+
FUNCTION(NonAtomicUnownedReleaseN, swift_nonatomic_unownedRelease_n,
409+
RegisterPreservingCC,
410+
RETURNS(VoidTy),
411+
ARGS(RefCountedPtrTy, Int32Ty),
412+
ATTRS(NoUnwind))
413+
386414
// void swift_unownedRetainStrong(void *ptr);
387415
FUNCTION(NativeStrongRetainUnowned, swift_unownedRetainStrong, RegisterPreservingCC,
388416
RETURNS(VoidTy),
389417
ARGS(RefCountedPtrTy),
390418
ATTRS(NoUnwind))
391419

420+
// void swift_nonatomic_unownedRetainStrong(void *ptr);
421+
FUNCTION(NonAtomicNativeStrongRetainUnowned, swift_nonatomic_unownedRetainStrong,
422+
RegisterPreservingCC,
423+
RETURNS(VoidTy),
424+
ARGS(RefCountedPtrTy),
425+
ATTRS(NoUnwind))
426+
392427
// void swift_unownedRetainStrongAndRelease(void *ptr);
393428
FUNCTION(NativeStrongRetainAndUnownedRelease,
394429
swift_unownedRetainStrongAndRelease, RegisterPreservingCC,
395430
RETURNS(VoidTy),
396431
ARGS(RefCountedPtrTy),
397432
ATTRS(NoUnwind))
398433

434+
// void swift_nonatomic_unownedRetainStrongAndRelease(void *ptr);
435+
FUNCTION(NonAtomicNativeStrongRetainAndUnownedRelease,
436+
swift_nonatomic_unownedRetainStrongAndRelease, RegisterPreservingCC,
437+
RETURNS(VoidTy),
438+
ARGS(RefCountedPtrTy),
439+
ATTRS(NoUnwind))
440+
399441
// void swift_weakDestroy(WeakReference *object);
400442
FUNCTION(NativeWeakDestroy, swift_weakDestroy, DefaultCC,
401443
RETURNS(VoidTy),

include/swift/SIL/SILBuilder.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,17 +820,19 @@ class SILBuilder {
820820
}
821821

822822
UnmanagedRetainValueInst *createUnmanagedRetainValue(SILLocation Loc,
823-
SILValue operand) {
823+
SILValue operand,
824+
Atomicity atomicity) {
824825
assert(F.hasQualifiedOwnership());
825826
return insert(new (F.getModule()) UnmanagedRetainValueInst(
826-
getSILDebugLocation(Loc), operand));
827+
getSILDebugLocation(Loc), operand, atomicity));
827828
}
828829

829830
UnmanagedReleaseValueInst *createUnmanagedReleaseValue(SILLocation Loc,
830-
SILValue operand) {
831+
SILValue operand,
832+
Atomicity atomicity) {
831833
assert(F.hasQualifiedOwnership());
832834
return insert(new (F.getModule()) UnmanagedReleaseValueInst(
833-
getSILDebugLocation(Loc), operand));
835+
getSILDebugLocation(Loc), operand, atomicity));
834836
}
835837

836838
CopyValueInst *createCopyValue(SILLocation Loc, SILValue operand) {
@@ -857,9 +859,10 @@ class SILBuilder {
857859
}
858860

859861
UnmanagedAutoreleaseValueInst *
860-
createUnmanagedAutoreleaseValue(SILLocation Loc, SILValue operand) {
862+
createUnmanagedAutoreleaseValue(SILLocation Loc, SILValue operand,
863+
Atomicity atomicity) {
861864
return insert(new (F.getModule()) UnmanagedAutoreleaseValueInst(
862-
getSILDebugLocation(Loc), operand));
865+
getSILDebugLocation(Loc), operand, atomicity));
863866
}
864867

865868
SetDeallocatingInst *createSetDeallocating(SILLocation Loc,
@@ -1530,6 +1533,11 @@ class SILBuilder {
15301533
// Memory management helpers
15311534
//===--------------------------------------------------------------------===//
15321535

1536+
/// Returns the default atomicity of the module.
1537+
Atomicity getDefaultAtomicity() {
1538+
return getModule().isDefaultAtomic() ? Atomicity::Atomic : Atomicity::NonAtomic;
1539+
}
1540+
15331541
/// Try to fold a destroy_addr operation into the previous instructions, or
15341542
/// generate an explicit one if that fails. If this inserts a new
15351543
/// instruction, it returns it, otherwise it returns null.

include/swift/SIL/SILCloner.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,8 @@ void SILCloner<ImplClass>::visitUnmanagedRetainValueInst(
11761176
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
11771177
doPostProcess(
11781178
Inst, getBuilder().createUnmanagedRetainValue(
1179-
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
1179+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1180+
Inst->getAtomicity()));
11801181
}
11811182

11821183
template <typename ImplClass>
@@ -1211,7 +1212,8 @@ void SILCloner<ImplClass>::visitUnmanagedReleaseValueInst(
12111212
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
12121213
doPostProcess(
12131214
Inst, getBuilder().createUnmanagedReleaseValue(
1214-
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
1215+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1216+
Inst->getAtomicity()));
12151217
}
12161218

12171219
template <typename ImplClass>
@@ -1238,7 +1240,8 @@ void SILCloner<ImplClass>::visitUnmanagedAutoreleaseValueInst(
12381240
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
12391241
doPostProcess(
12401242
Inst, getBuilder().createUnmanagedAutoreleaseValue(
1241-
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
1243+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1244+
Inst->getAtomicity()));
12421245
}
12431246

12441247
template<typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,8 +3021,11 @@ class UnmanagedRetainValueInst
30213021
/*HasValue*/ false> {
30223022
friend SILBuilder;
30233023

3024-
UnmanagedRetainValueInst(SILDebugLocation DebugLoc, SILValue operand)
3025-
: UnaryInstructionBase(DebugLoc, operand) {}
3024+
UnmanagedRetainValueInst(SILDebugLocation DebugLoc, SILValue operand,
3025+
Atomicity atomicity)
3026+
: UnaryInstructionBase(DebugLoc, operand) {
3027+
setAtomicity(atomicity);
3028+
}
30263029
};
30273030

30283031
/// Destroys a loadable value in an unmanaged, unbalanced way. Only meant for
@@ -3034,8 +3037,11 @@ class UnmanagedReleaseValueInst
30343037
/*HasValue*/ false> {
30353038
friend SILBuilder;
30363039

3037-
UnmanagedReleaseValueInst(SILDebugLocation DebugLoc, SILValue operand)
3038-
: UnaryInstructionBase(DebugLoc, operand) {}
3040+
UnmanagedReleaseValueInst(SILDebugLocation DebugLoc, SILValue operand,
3041+
Atomicity atomicity)
3042+
: UnaryInstructionBase(DebugLoc, operand) {
3043+
setAtomicity(atomicity);
3044+
}
30393045
};
30403046

30413047
/// Transfers ownership of a loadable value to the current autorelease
@@ -3046,8 +3052,11 @@ class UnmanagedAutoreleaseValueInst
30463052
/*HasValue*/ false> {
30473053
friend SILBuilder;
30483054

3049-
UnmanagedAutoreleaseValueInst(SILDebugLocation DebugLoc, SILValue operand)
3050-
: UnaryInstructionBase(DebugLoc, operand) {}
3055+
UnmanagedAutoreleaseValueInst(SILDebugLocation DebugLoc, SILValue operand,
3056+
Atomicity atomicity)
3057+
: UnaryInstructionBase(DebugLoc, operand) {
3058+
setAtomicity(atomicity);
3059+
}
30513060
};
30523061

30533062
/// Transfers ownership of a loadable value to the current autorelease pool.

include/swift/SIL/SILModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,11 @@ class SILModule {
642642

643643
/// Returns true if the builtin or intrinsic is no-return.
644644
bool isNoReturnBuiltinOrIntrinsic(Identifier Name);
645+
646+
/// Returns true if the default atomicity of the module is Atomic.
647+
bool isDefaultAtomic() const {
648+
return ! getOptions().AssumeSingleThreaded;
649+
}
645650
};
646651

647652
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SILModule &M){

lib/IRGen/GenClass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ static bool getInstanceSizeByMethod(IRGenFunction &IGF,
795795

796796
// Retain 'self' if necessary.
797797
if (fnType->getParameters()[0].isConsumed()) {
798-
IGF.emitNativeStrongRetain(selfValue);
798+
IGF.emitNativeStrongRetain(selfValue, IGF.getDefaultAtomicity());
799799
}
800800

801801
// Adjust down to the defining subclass type if necessary.

0 commit comments

Comments
 (0)