Skip to content

Commit 09ed0f1

Browse files
authored
Merge pull request #60003 from gottesmm/pr-fce4cec67c5827dcedf3ac25ff0f92b24f6ae193
[no-implicit-copy] A few small patches to simplify a larger patch.
2 parents bad59e6 + dc59f00 commit 09ed0f1

File tree

9 files changed

+57
-15
lines changed

9 files changed

+57
-15
lines changed

include/swift/AST/Types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,7 +5004,7 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(SILBoxType, Type)
50045004

50055005
class SILMoveOnlyWrappedType;
50065006
class SILModule; // From SIL
5007-
typedef CanTypeWrapper<SILMoveOnlyWrappedType> CanMoveOnlyType;
5007+
typedef CanTypeWrapper<SILMoveOnlyWrappedType> CanSILMoveOnlyWrappedType;
50085008

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

5025-
static CanMoveOnlyType get(CanType innerType);
5025+
static CanSILMoveOnlyWrappedType get(CanType innerType);
50265026

50275027
static bool classof(const TypeBase *T) {
50285028
return T->getKind() == TypeKind::SILMoveOnlyWrapped;

include/swift/SIL/ApplySite.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ class ApplySite {
297297
getArgumentOperands()[i].set(V);
298298
}
299299

300+
void setCallee(SILValue V) const {
301+
unsigned calleeIndex = getCalleeOperand()->getOperandNumber();
302+
getInstruction()->getAllOperands()[calleeIndex].set(V);
303+
}
304+
300305
/// Return the operand index of the first applied argument.
301306
unsigned getOperandIndexOfFirstArgument() const {
302307
FOREACH_IMPL_RETURN(getArgumentOperandNumber());

include/swift/SIL/SILInstruction.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7580,7 +7580,10 @@ class MarkMustCheckInst
75807580
: UnaryInstructionBase(DebugLoc, operand, operand->getType()),
75817581
OwnershipForwardingMixin(SILInstructionKind::MarkMustCheckInst,
75827582
operand->getOwnershipKind()),
7583-
kind(checkKind) {}
7583+
kind(checkKind) {
7584+
assert(operand->getType().isMoveOnlyWrapped() &&
7585+
"mark_must_check can only take a move only wrapped value");
7586+
}
75847587

75857588
public:
75867589
CheckKind getCheckKind() const { return kind; }

include/swift/SIL/SILType.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ class SILParser;
9393
/// In addition to the Swift type system, SIL adds "address" types that can
9494
/// reference any Swift type (but cannot take the address of an address). *T
9595
/// is the type of an address pointing at T.
96-
///
9796
class SILType {
9897
public:
9998
/// The unsigned is a SILValueCategory.
10099
using ValueType = llvm::PointerIntPair<TypeBase *, 2, unsigned>;
100+
101101
private:
102102
ValueType value;
103103

@@ -151,7 +151,7 @@ class SILType {
151151

152152
/// Returns the \p Category variant of this type.
153153
SILType getCategoryType(SILValueCategory Category) const {
154-
return SILType(getASTType(), Category);
154+
return SILType(getRawASTType(), Category);
155155
}
156156

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

197-
private:
198197
/// Returns the canonical AST type references by this SIL type without looking
199198
/// through move only. Should only be used by internal utilities of SILType.
200199
CanType getRawASTType() const { return CanType(value.getPointer()); }

lib/SILGen/SILGenBuilder.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,25 @@ ManagedValue SILGenBuilder::createGuaranteedMoveOnlyWrapperToCopyableValue(
921921
assert(mdi->getOperand()->getType().isObject() && "Expected an object?!");
922922
return ManagedValue::forUnmanaged(mdi);
923923
}
924+
925+
ManagedValue
926+
SILGenBuilder::createCopyableToMoveOnlyWrapperValue(SILLocation loc,
927+
ManagedValue value) {
928+
assert(value.isPlusOne(SGF) && "Argument must be at +1!");
929+
CleanupCloner cloner(*this, value);
930+
SILValue v = value.forward(getSILGenFunction());
931+
auto *mdi = SILBuilder::createCopyableToMoveOnlyWrapperValue(loc, v);
932+
if (v->getType().isTrivial(getFunction()))
933+
return SGF.emitManagedRValueWithCleanup(mdi);
934+
return cloner.clone(mdi);
935+
}
936+
937+
ManagedValue
938+
SILGenBuilder::createMarkMustCheckInst(SILLocation loc, ManagedValue value,
939+
MarkMustCheckInst::CheckKind kind) {
940+
assert(value.isPlusOne(SGF) && "Argument must be at +1!");
941+
CleanupCloner cloner(*this, value);
942+
auto *mdi = SILBuilder::createMarkMustCheckInst(
943+
loc, value.forward(getSILGenFunction()), kind);
944+
return cloner.clone(mdi);
945+
}

lib/SILGen/SILGenBuilder.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "RValue.h"
2929
#include "swift/Basic/ProfileCounter.h"
3030
#include "swift/SIL/SILBuilder.h"
31+
#include "swift/SIL/SILInstruction.h"
3132
#include "swift/SIL/SILLocation.h"
3233

3334
namespace swift {
@@ -414,6 +415,17 @@ class SILGenBuilder : public SILBuilder {
414415
ManagedValue
415416
createGuaranteedMoveOnlyWrapperToCopyableValue(SILLocation loc,
416417
ManagedValue value);
418+
419+
using SILBuilder::createCopyableToMoveOnlyWrapperValue;
420+
/// Create a copyable_to_moveonlywrapper. This expects a +1 value and returns
421+
/// a +1 value. Semantically though the two things are different entities so
422+
/// we are not forwarding ownership in the sense of the word.
423+
ManagedValue createCopyableToMoveOnlyWrapperValue(SILLocation loc,
424+
ManagedValue value);
425+
426+
using SILBuilder::createMarkMustCheckInst;
427+
ManagedValue createMarkMustCheckInst(SILLocation loc, ManagedValue value,
428+
MarkMustCheckInst::CheckKind kind);
417429
};
418430

419431
} // namespace Lowering

lib/SILGen/SILGenProlog.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,29 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
109109

110110
// This can happen if the value is resilient in the calling convention
111111
// but not resilient locally.
112-
if (argType.isLoadable(SGF.F)) {
112+
bool argIsLoadable = argType.isLoadable(SGF.F);
113+
if (argIsLoadable) {
113114
if (argType.isAddress()) {
114115
if (mv.isPlusOne(SGF))
115116
mv = SGF.B.createLoadTake(loc, mv);
116117
else
117118
mv = SGF.B.createLoadBorrow(loc, mv);
118119
argType = argType.getObjectType();
119120
}
120-
} else {
121-
if (isNoImplicitCopy) {
122-
// We do not support no implicit copy address only types. Emit an error.
123-
auto diag = diag::noimplicitcopy_used_on_generic_or_existential;
124-
diagnose(SGF.getASTContext(), mv.getValue().getLoc().getSourceLoc(),
125-
diag);
126-
}
127121
}
128122

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

128+
if (isNoImplicitCopy && !argIsLoadable) {
129+
// We do not support no implicit copy address only types. Emit an error.
130+
auto diag = diag::noimplicitcopy_used_on_generic_or_existential;
131+
diagnose(SGF.getASTContext(), mv.getValue().getLoc().getSourceLoc(),
132+
diag);
133+
}
134+
134135
// If the value is a (possibly optional) ObjC block passed into the entry
135136
// point of the function, then copy it so we can treat the value reliably
136137
// as a heap object. Escape analysis can eliminate this copy if it's

lib/SILOptimizer/Mandatory/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ target_sources(swiftSILOptimizer PRIVATE
3636
YieldOnceCheck.cpp
3737
MandatoryCombine.cpp
3838
OSLogOptimization.cpp
39-
MoveOnlyTypeEliminator.cpp
39+
MoveOnlyWrappedTypeEliminator.cpp
4040
OwnershipModelEliminator.cpp)

0 commit comments

Comments
 (0)