Skip to content

[move-only] Two small fixes #59303

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
40 changes: 24 additions & 16 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ enum class SILValueCategory : uint8_t {
Address,
};

class SILPrinter;

/// SILType - A Swift type that has been lowered to a SIL representation type.
/// 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
Expand Down Expand Up @@ -113,6 +115,8 @@ class SILType {

friend class Lowering::TypeConverter;
friend struct llvm::DenseMapInfo<SILType>;
friend class SILPrinter;

public:
SILType() = default;

Expand Down Expand Up @@ -184,8 +188,11 @@ class SILType {
/// type. This is done under the assumption that in all cases where we are
/// performing these AST queries on SILType, we are not interested in the
/// move only-ness of the value (which we can query separately anyways).
CanType getASTType() const { return withoutMoveOnly().getRawASTType(); }
CanType getASTType() const {
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 Expand Up @@ -597,33 +604,34 @@ class SILType {
///
/// Canonical way to check if a SILType is move only. Using is/getAs/castTo
/// will look through moveonly-ness.
bool isMoveOnly() const { return getRawASTType()->is<SILMoveOnlyType>(); }
bool isMoveOnlyWrapped() const {
return getRawASTType()->is<SILMoveOnlyType>();
}

/// Return *this if already move only... otherwise, wrap the current type
/// within a move only type wrapper and return that. Idempotent!
SILType asMoveOnly() const {
if (isMoveOnly())
/// If this is already a move only wrapped type, return *this. Otherwise, wrap
/// the copyable type in the mov eonly wrapper.
SILType addingMoveOnlyWrapper() const {
if (isMoveOnlyWrapped())
return *this;
auto newType = SILMoveOnlyType::get(getRawASTType());
return SILType::getPrimitiveType(newType, getCategory());
}

/// Return this SILType, removing moveonly-ness.
///
/// Is idempotent.
SILType withoutMoveOnly() const {
if (!isMoveOnly())
/// If this is already a copyable type, just return *this. Otherwise, if this
/// is a move only wrapped copyable type, return the inner type.
SILType removingMoveOnlyWrapper() const {
if (!isMoveOnlyWrapped())
return *this;
auto moveOnly = getRawASTType()->castTo<SILMoveOnlyType>();
return SILType::getPrimitiveType(moveOnly->getInnerType(), getCategory());
}

/// If \p otherType is move only, return this type that is move only as
/// well. Otherwise, returns self. Useful for propagating "move only"-ness
/// If \p otherType is move only wrapped, return this type that is move only
/// as well. Otherwise, returns self. Useful for propagating "move only"-ness
/// from a parent type to a subtype.
SILType copyMoveOnly(SILType otherType) const {
if (otherType.isMoveOnly()) {
return asMoveOnly();
SILType copyingMoveOnlyWrapper(SILType otherType) const {
if (otherType.isMoveOnlyWrapped()) {
return addingMoveOnlyWrapper();
}
return *this;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,6 @@ static void printSILFunctionNameAndType(llvm::raw_ostream &OS,
}

namespace {

class SILPrinter;

// 1. Accumulate opcode-specific comments in this stream.
// 2. Start emitting comments: lineComments.start()
Expand Down Expand Up @@ -598,6 +596,10 @@ class LineComments : public raw_ostream {
}
};

} // namespace

namespace swift {

/// SILPrinter class - This holds the internal implementation details of
/// printing SIL structures.
class SILPrinter : public SILInstructionVisitor<SILPrinter> {
Expand Down Expand Up @@ -2740,7 +2742,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
}
}
};
} // end anonymous namespace

} // namespace swift

static void printBlockID(raw_ostream &OS, SILBasicBlock *bb) {
SILPrintContext Ctx(OS);
Expand Down
14 changes: 7 additions & 7 deletions lib/SIL/IR/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ SILType SILType::getOptionalType(SILType type) {
auto optType = BoundGenericEnumType::get(ctx.getOptionalDecl(), Type(),
{ type.getASTType() });
return getPrimitiveType(CanType(optType), type.getCategory())
.copyMoveOnly(type);
.copyingMoveOnlyWrapper(type);
}

SILType SILType::getEmptyTupleType(const ASTContext &C) {
Expand Down Expand Up @@ -296,7 +296,7 @@ SILType SILType::getFieldType(VarDecl *field, TypeConverter &TC,

// If this type is not a class type, then we propagate "move only"-ness to the
// field. Example:
if (!getClassOrBoundGenericClass() && isMoveOnly())
if (!getClassOrBoundGenericClass() && isMoveOnlyWrapped())
loweredTy = SILMoveOnlyType::get(loweredTy);

if (isAddress() || getClassOrBoundGenericClass() != nullptr) {
Expand All @@ -318,7 +318,7 @@ SILType SILType::getEnumElementType(EnumElementDecl *elt, TypeConverter &TC,

if (auto objectType = getASTType().getOptionalObjectType()) {
assert(elt == TC.Context.getOptionalSomeDecl());
return SILType(objectType, getCategory()).copyMoveOnly(*this);
return SILType(objectType, getCategory()).copyingMoveOnlyWrapper(*this);
}

// If the case is indirect, then the payload is boxed.
Expand All @@ -333,7 +333,7 @@ SILType SILType::getEnumElementType(EnumElementDecl *elt, TypeConverter &TC,
auto loweredTy = TC.getLoweredRValueType(
context, TC.getAbstractionPattern(elt), substEltTy);

return SILType(loweredTy, getCategory()).copyMoveOnly(*this);
return SILType(loweredTy, getCategory()).copyingMoveOnlyWrapper(*this);
}

SILType SILType::getEnumElementType(EnumElementDecl *elt, SILModule &M,
Expand Down Expand Up @@ -438,15 +438,15 @@ bool SILType::aggregateHasUnreferenceableStorage() const {

SILType SILType::getOptionalObjectType() const {
if (auto objectTy = getASTType().getOptionalObjectType()) {
return SILType(objectTy, getCategory()).copyMoveOnly(*this);
return SILType(objectTy, getCategory()).copyingMoveOnlyWrapper(*this);
}

return SILType();
}

SILType SILType::unwrapOptionalType() const {
if (auto objectTy = withoutMoveOnly().getOptionalObjectType()) {
return objectTy.copyMoveOnly(*this);
if (auto objectTy = removingMoveOnlyWrapper().getOptionalObjectType()) {
return objectTy.copyingMoveOnlyWrapper(*this);
}

return *this;
Expand Down