Skip to content

[no-implicit-copy] A few small patches to simplify a larger patch. #60003

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
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: 2 additions & 2 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5004,7 +5004,7 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(SILBoxType, Type)

class SILMoveOnlyWrappedType;
class SILModule; // From SIL
typedef CanTypeWrapper<SILMoveOnlyWrappedType> CanMoveOnlyType;
typedef CanTypeWrapper<SILMoveOnlyWrappedType> CanSILMoveOnlyWrappedType;

/// A wrapper type that marks an inner type as being a move only value. Can not
/// be written directly at the Swift level, instead it is triggered by adding
Expand All @@ -5022,7 +5022,7 @@ class SILMoveOnlyWrappedType final : public TypeBase,
public:
CanType getInnerType() const { return innerType; }

static CanMoveOnlyType get(CanType innerType);
static CanSILMoveOnlyWrappedType get(CanType innerType);

static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::SILMoveOnlyWrapped;
Expand Down
5 changes: 5 additions & 0 deletions include/swift/SIL/ApplySite.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ class ApplySite {
getArgumentOperands()[i].set(V);
}

void setCallee(SILValue V) const {
unsigned calleeIndex = getCalleeOperand()->getOperandNumber();
getInstruction()->getAllOperands()[calleeIndex].set(V);
}

/// Return the operand index of the first applied argument.
unsigned getOperandIndexOfFirstArgument() const {
FOREACH_IMPL_RETURN(getArgumentOperandNumber());
Expand Down
5 changes: 4 additions & 1 deletion include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -7580,7 +7580,10 @@ class MarkMustCheckInst
: UnaryInstructionBase(DebugLoc, operand, operand->getType()),
OwnershipForwardingMixin(SILInstructionKind::MarkMustCheckInst,
operand->getOwnershipKind()),
kind(checkKind) {}
kind(checkKind) {
assert(operand->getType().isMoveOnlyWrapped() &&
"mark_must_check can only take a move only wrapped value");
}

public:
CheckKind getCheckKind() const { return kind; }
Expand Down
5 changes: 2 additions & 3 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ class SILParser;
/// In addition to the Swift type system, SIL adds "address" types that can
/// reference any Swift type (but cannot take the address of an address). *T
/// is the type of an address pointing at T.
///
class SILType {
public:
/// The unsigned is a SILValueCategory.
using ValueType = llvm::PointerIntPair<TypeBase *, 2, unsigned>;

private:
ValueType value;

Expand Down Expand Up @@ -151,7 +151,7 @@ class SILType {

/// Returns the \p Category variant of this type.
SILType getCategoryType(SILValueCategory Category) const {
return SILType(getASTType(), Category);
return SILType(getRawASTType(), Category);
}

/// Returns the variant of this type that matches \p Ty.getCategory()
Expand Down Expand Up @@ -194,7 +194,6 @@ class SILType {
return removingMoveOnlyWrapper().getRawASTType();
}

private:
/// Returns the canonical AST type references by this SIL type without looking
/// through move only. Should only be used by internal utilities of SILType.
CanType getRawASTType() const { return CanType(value.getPointer()); }
Expand Down
22 changes: 22 additions & 0 deletions lib/SILGen/SILGenBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,3 +921,25 @@ ManagedValue SILGenBuilder::createGuaranteedMoveOnlyWrapperToCopyableValue(
assert(mdi->getOperand()->getType().isObject() && "Expected an object?!");
return ManagedValue::forUnmanaged(mdi);
}

ManagedValue
SILGenBuilder::createCopyableToMoveOnlyWrapperValue(SILLocation loc,
ManagedValue value) {
assert(value.isPlusOne(SGF) && "Argument must be at +1!");
CleanupCloner cloner(*this, value);
SILValue v = value.forward(getSILGenFunction());
auto *mdi = SILBuilder::createCopyableToMoveOnlyWrapperValue(loc, v);
if (v->getType().isTrivial(getFunction()))
return SGF.emitManagedRValueWithCleanup(mdi);
return cloner.clone(mdi);
}

ManagedValue
SILGenBuilder::createMarkMustCheckInst(SILLocation loc, ManagedValue value,
MarkMustCheckInst::CheckKind kind) {
assert(value.isPlusOne(SGF) && "Argument must be at +1!");
CleanupCloner cloner(*this, value);
auto *mdi = SILBuilder::createMarkMustCheckInst(
loc, value.forward(getSILGenFunction()), kind);
return cloner.clone(mdi);
}
12 changes: 12 additions & 0 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "RValue.h"
#include "swift/Basic/ProfileCounter.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILLocation.h"

namespace swift {
Expand Down Expand Up @@ -414,6 +415,17 @@ class SILGenBuilder : public SILBuilder {
ManagedValue
createGuaranteedMoveOnlyWrapperToCopyableValue(SILLocation loc,
ManagedValue value);

using SILBuilder::createCopyableToMoveOnlyWrapperValue;
/// Create a copyable_to_moveonlywrapper. This expects a +1 value and returns
/// a +1 value. Semantically though the two things are different entities so
/// we are not forwarding ownership in the sense of the word.
ManagedValue createCopyableToMoveOnlyWrapperValue(SILLocation loc,
ManagedValue value);

using SILBuilder::createMarkMustCheckInst;
ManagedValue createMarkMustCheckInst(SILLocation loc, ManagedValue value,
MarkMustCheckInst::CheckKind kind);
};

} // namespace Lowering
Expand Down
17 changes: 9 additions & 8 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,29 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,

// This can happen if the value is resilient in the calling convention
// but not resilient locally.
if (argType.isLoadable(SGF.F)) {
bool argIsLoadable = argType.isLoadable(SGF.F);
if (argIsLoadable) {
if (argType.isAddress()) {
if (mv.isPlusOne(SGF))
mv = SGF.B.createLoadTake(loc, mv);
else
mv = SGF.B.createLoadBorrow(loc, mv);
argType = argType.getObjectType();
}
} else {
if (isNoImplicitCopy) {
// We do not support no implicit copy address only types. Emit an error.
auto diag = diag::noimplicitcopy_used_on_generic_or_existential;
diagnose(SGF.getASTContext(), mv.getValue().getLoc().getSourceLoc(),
diag);
}
}

if (argType.getASTType() != paramType.getASTType()) {
// Reabstract the value if necessary.
mv = SGF.emitOrigToSubstValue(loc, mv.ensurePlusOne(SGF, loc), orig, t);
}

if (isNoImplicitCopy && !argIsLoadable) {
// We do not support no implicit copy address only types. Emit an error.
auto diag = diag::noimplicitcopy_used_on_generic_or_existential;
diagnose(SGF.getASTContext(), mv.getValue().getLoc().getSourceLoc(),
diag);
}

// If the value is a (possibly optional) ObjC block passed into the entry
// point of the function, then copy it so we can treat the value reliably
// as a heap object. Escape analysis can eliminate this copy if it's
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Mandatory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ target_sources(swiftSILOptimizer PRIVATE
YieldOnceCheck.cpp
MandatoryCombine.cpp
OSLogOptimization.cpp
MoveOnlyTypeEliminator.cpp
MoveOnlyWrappedTypeEliminator.cpp
OwnershipModelEliminator.cpp)