Skip to content

Enhance -assume-single-threaded option (SR-3945) #7557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ option(SWIFT_STDLIB_ENABLE_RESILIENCE
"Build the standard libraries and overlays with resilience enabled; see docs/LibraryEvolution.rst"
FALSE)

option(SWIFT_STDLIB_USE_NONATOMIC_RC
"Build the standard libraries and overlays with nonatomic reference count operations enabled"
FALSE)

option(SWIFT_STDLIB_ENABLE_SIL_OWNERSHIP
"Build the standard libraries and overlays with sil ownership enabled."
FALSE)
Expand Down
4 changes: 4 additions & 0 deletions cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ function(_compile_swift_files
list(APPEND swift_flags "-Xfrontend" "-enable-resilience")
endif()

if(SWIFT_STDLIB_USE_NONATOMIC_RC)
list(APPEND swift_flags "-Xfrontend" "-assume-single-threaded")
endif()

if(SWIFT_STDLIB_ENABLE_SIL_OWNERSHIP AND SWIFTFILE_IS_STDLIB)
list(APPEND swift_flags "-Xfrontend" "-enable-sil-ownership")
endif()
Expand Down
33 changes: 33 additions & 0 deletions include/swift/Runtime/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ SWIFT_RT_ENTRY_VISIBILITY
void swift_unownedRelease(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Increment the unowned retain count.
SWIFT_RT_ENTRY_VISIBILITY
void swift_nonatomic_unownedRetain(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Decrement the unowned retain count.
SWIFT_RT_ENTRY_VISIBILITY
void swift_nonatomic_unownedRelease(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Increment the unowned retain count by n.
SWIFT_RT_ENTRY_VISIBILITY
void swift_unownedRetain_n(HeapObject *value, int n)
Expand All @@ -520,19 +530,42 @@ SWIFT_RT_ENTRY_VISIBILITY
void swift_unownedRelease_n(HeapObject *value, int n)
SWIFT_CC(RegisterPreservingCC);

/// Increment the unowned retain count by n.
SWIFT_RT_ENTRY_VISIBILITY
void swift_nonatomic_unownedRetain_n(HeapObject *value, int n)
SWIFT_CC(RegisterPreservingCC);

/// Decrement the unowned retain count by n.
SWIFT_RT_ENTRY_VISIBILITY
void swift_nonatomic_unownedRelease_n(HeapObject *value, int n)
SWIFT_CC(RegisterPreservingCC);

/// Increment the strong retain count of an object, aborting if it has
/// been deallocated.
SWIFT_RT_ENTRY_VISIBILITY
void swift_unownedRetainStrong(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Increment the strong retain count of an object, aborting if it has
/// been deallocated.
SWIFT_RT_ENTRY_VISIBILITY
void swift_nonatomic_unownedRetainStrong(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Increment the strong retain count of an object which may have been
/// deallocated, aborting if it has been deallocated, and decrement its
/// unowned reference count.
SWIFT_RT_ENTRY_VISIBILITY
void swift_unownedRetainStrongAndRelease(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Increment the strong retain count of an object which may have been
/// deallocated, aborting if it has been deallocated, and decrement its
/// unowned reference count.
SWIFT_RT_ENTRY_VISIBILITY
void swift_nonatomic_unownedRetainStrongAndRelease(HeapObject *value)
SWIFT_CC(RegisterPreservingCC);

/// Aborts if the object has been deallocated.
SWIFT_RUNTIME_EXPORT
void swift_unownedCheck(HeapObject *value);
Expand Down
42 changes: 42 additions & 0 deletions include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@ FUNCTION(NativeUnownedRelease, swift_unownedRelease, RegisterPreservingCC,
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_nonatomic_unownedRetain(void *ptr);
FUNCTION(NonAtomicNativeUnownedRetain, swift_nonatomic_unownedRetain,
RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_nonatomic_unownedRelease(void *ptr);
FUNCTION(NonAtomicNativeUnownedRelease, swift_nonatomic_unownedRelease,
RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_unownedRetain_n(void *ptr, int32_t n);
FUNCTION(UnownedRetainN, swift_unownedRetain_n,
RegisterPreservingCC,
Expand All @@ -383,19 +397,47 @@ FUNCTION(UnownedReleaseN, swift_unownedRelease_n,
ARGS(RefCountedPtrTy, Int32Ty),
ATTRS(NoUnwind))

// void swift_nonatomic_unownedRetain_n(void *ptr, int32_t n);
FUNCTION(NonAtomicUnownedRetainN, swift_nonatomic_unownedRetain_n,
RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy, Int32Ty),
ATTRS(NoUnwind))

// void swift_nonatomic_unownedRelease_n(void *ptr, int32_t n);
FUNCTION(NonAtomicUnownedReleaseN, swift_nonatomic_unownedRelease_n,
RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy, Int32Ty),
ATTRS(NoUnwind))

// void swift_unownedRetainStrong(void *ptr);
FUNCTION(NativeStrongRetainUnowned, swift_unownedRetainStrong, RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_nonatomic_unownedRetainStrong(void *ptr);
FUNCTION(NonAtomicNativeStrongRetainUnowned, swift_nonatomic_unownedRetainStrong,
RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_unownedRetainStrongAndRelease(void *ptr);
FUNCTION(NativeStrongRetainAndUnownedRelease,
swift_unownedRetainStrongAndRelease, RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_nonatomic_unownedRetainStrongAndRelease(void *ptr);
FUNCTION(NonAtomicNativeStrongRetainAndUnownedRelease,
swift_nonatomic_unownedRetainStrongAndRelease, RegisterPreservingCC,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_weakDestroy(WeakReference *object);
FUNCTION(NativeWeakDestroy, swift_weakDestroy, DefaultCC,
RETURNS(VoidTy),
Expand Down
20 changes: 14 additions & 6 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,17 +819,19 @@ class SILBuilder {
}

UnmanagedRetainValueInst *createUnmanagedRetainValue(SILLocation Loc,
SILValue operand) {
SILValue operand,
Atomicity atomicity) {
assert(F.hasQualifiedOwnership());
return insert(new (F.getModule()) UnmanagedRetainValueInst(
getSILDebugLocation(Loc), operand));
getSILDebugLocation(Loc), operand, atomicity));
}

UnmanagedReleaseValueInst *createUnmanagedReleaseValue(SILLocation Loc,
SILValue operand) {
SILValue operand,
Atomicity atomicity) {
assert(F.hasQualifiedOwnership());
return insert(new (F.getModule()) UnmanagedReleaseValueInst(
getSILDebugLocation(Loc), operand));
getSILDebugLocation(Loc), operand, atomicity));
}

CopyValueInst *createCopyValue(SILLocation Loc, SILValue operand) {
Expand All @@ -856,9 +858,10 @@ class SILBuilder {
}

UnmanagedAutoreleaseValueInst *
createUnmanagedAutoreleaseValue(SILLocation Loc, SILValue operand) {
createUnmanagedAutoreleaseValue(SILLocation Loc, SILValue operand,
Atomicity atomicity) {
return insert(new (F.getModule()) UnmanagedAutoreleaseValueInst(
getSILDebugLocation(Loc), operand));
getSILDebugLocation(Loc), operand, atomicity));
}

SetDeallocatingInst *createSetDeallocating(SILLocation Loc,
Expand Down Expand Up @@ -1529,6 +1532,11 @@ class SILBuilder {
// Memory management helpers
//===--------------------------------------------------------------------===//

/// Returns the default atomicity of the module.
Atomicity getDefaultAtomicity() {
return getModule().isDefaultAtomic() ? Atomicity::Atomic : Atomicity::NonAtomic;
}

/// Try to fold a destroy_addr operation into the previous instructions, or
/// generate an explicit one if that fails. If this inserts a new
/// instruction, it returns it, otherwise it returns null.
Expand Down
9 changes: 6 additions & 3 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,8 @@ void SILCloner<ImplClass>::visitUnmanagedRetainValueInst(
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
doPostProcess(
Inst, getBuilder().createUnmanagedRetainValue(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
Inst->getAtomicity()));
}

template <typename ImplClass>
Expand Down Expand Up @@ -1211,7 +1212,8 @@ void SILCloner<ImplClass>::visitUnmanagedReleaseValueInst(
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
doPostProcess(
Inst, getBuilder().createUnmanagedReleaseValue(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
Inst->getAtomicity()));
}

template <typename ImplClass>
Expand All @@ -1238,7 +1240,8 @@ void SILCloner<ImplClass>::visitUnmanagedAutoreleaseValueInst(
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
doPostProcess(
Inst, getBuilder().createUnmanagedAutoreleaseValue(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
Inst->getAtomicity()));
}

template<typename ImplClass>
Expand Down
21 changes: 15 additions & 6 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3021,8 +3021,11 @@ class UnmanagedRetainValueInst
/*HasValue*/ false> {
friend SILBuilder;

UnmanagedRetainValueInst(SILDebugLocation DebugLoc, SILValue operand)
: UnaryInstructionBase(DebugLoc, operand) {}
UnmanagedRetainValueInst(SILDebugLocation DebugLoc, SILValue operand,
Atomicity atomicity)
: UnaryInstructionBase(DebugLoc, operand) {
setAtomicity(atomicity);
}
};

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

UnmanagedReleaseValueInst(SILDebugLocation DebugLoc, SILValue operand)
: UnaryInstructionBase(DebugLoc, operand) {}
UnmanagedReleaseValueInst(SILDebugLocation DebugLoc, SILValue operand,
Atomicity atomicity)
: UnaryInstructionBase(DebugLoc, operand) {
setAtomicity(atomicity);
}
};

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

UnmanagedAutoreleaseValueInst(SILDebugLocation DebugLoc, SILValue operand)
: UnaryInstructionBase(DebugLoc, operand) {}
UnmanagedAutoreleaseValueInst(SILDebugLocation DebugLoc, SILValue operand,
Atomicity atomicity)
: UnaryInstructionBase(DebugLoc, operand) {
setAtomicity(atomicity);
}
};

/// Transfers ownership of a loadable value to the current autorelease pool.
Expand Down
5 changes: 5 additions & 0 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ class SILModule {

/// Returns true if the builtin or intrinsic is no-return.
bool isNoReturnBuiltinOrIntrinsic(Identifier Name);

/// Returns true if the default atomicity of the module is Atomic.
bool isDefaultAtomic() const {
return ! getOptions().AssumeSingleThreaded;
}
};

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SILModule &M){
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ static bool getInstanceSizeByMethod(IRGenFunction &IGF,

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

// Adjust down to the defining subclass type if necessary.
Expand Down
Loading