Skip to content

[moveOnly] Add a Builtin.move operator that is overloaded over Builtin types. #39286

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
16 changes: 16 additions & 0 deletions include/swift/AST/ASTSynthesis.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ Type synthesizeType(SynthesisContext &SC,
return ExistentialMetatypeType::get(synthesizeType(SC, M.Sub));
}

/// A synthesizer that generates a MoveOnly wrapper of a type.
template <class S>
struct MoveOnlyTypeSynthesizer {
S Sub;
};
template <class S>
constexpr MoveOnlyTypeSynthesizer<S> _moveOnly(S sub) {
return {sub};
}
template <class S>
Type synthesizeType(SynthesisContext &SC, const MoveOnlyTypeSynthesizer<S> &M) {
// Until we get the actual move only type, we just return the synthesized
// type.
return synthesizeType(SC, M.Sub);
}

/// Helper types for variadic synthesis.
template <class... Ss>
struct VariadicSynthesizerStorage;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ BUILTIN_SIL_OPERATION(WithUnsafeThrowingContinuation, "withUnsafeThrowingContinu
/// Force the current task to be rescheduled on the specified actor.
BUILTIN_SIL_OPERATION(HopToActor, "hopToActor", None)

/// Generate a move_value instruction to convert a T to a @_moveOnly T.
BUILTIN_SIL_OPERATION(Move, "move", Special)

#undef BUILTIN_SIL_OPERATION

// BUILTIN_RUNTIME_CALL - A call into a runtime function.
Expand Down
11 changes: 11 additions & 0 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,12 @@ static ValueDecl *getDestroyArrayOperation(ASTContext &ctx, Identifier id) {
_void);
}

static ValueDecl *getMoveOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin, _generics(_unrestricted),
_parameters(_owned(_typeparam(0))),
_moveOnly(_typeparam(0)));
}

static ValueDecl *getTransferArrayOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
Expand Down Expand Up @@ -2500,6 +2506,11 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
if (!Types.empty()) return nullptr;
return getEndUnpairedAccessOperation(Context, Id);

case BuiltinValueKind::Move:
if (!Types.empty())
return nullptr;
return getMoveOperation(Context, Id);

#define BUILTIN(id, name, Attrs)
#define BUILTIN_BINARY_OPERATION(id, name, attrs)
#define BUILTIN_BINARY_OPERATION_OVERLOADED_STATIC(id, name, attrs, overload) \
Expand Down
9 changes: 9 additions & 0 deletions lib/SILGen/SILGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,15 @@ static ManagedValue emitBuiltinHopToActor(SILGenFunction &SGF, SILLocation loc,
return ManagedValue::forUnmanaged(SGF.emitEmptyTuple(loc));
}

static ManagedValue emitBuiltinMove(SILGenFunction &SGF, SILLocation loc,
SubstitutionMap subs,
ArrayRef<ManagedValue> args, SGFContext C) {
assert(args.size() == 1 && "Move has a single argument");
auto firstArg = args[0].ensurePlusOne(SGF, loc);
CleanupCloner cloner(SGF, firstArg);
return cloner.clone(SGF.B.createMoveValue(loc, firstArg.forward(SGF)));
}

static ManagedValue emitBuiltinAutoDiffCreateLinearMapContext(
SILGenFunction &SGF, SILLocation loc, SubstitutionMap subs,
ArrayRef<ManagedValue> args, SGFContext C) {
Expand Down
27 changes: 27 additions & 0 deletions test/SILGen/moveonly.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-swift-emit-silgen -parse-stdlib %s -disable-access-control -disable-objc-attr-requires-foundation-module | %FileCheck %s

import Swift

class Klass {}

// CHECK-LABEL: sil hidden [ossa] @$s8moveonly7useMoveyAA5KlassCADF : $@convention(thin) (@guaranteed Klass) -> @owned Klass {
// CHECK: bb0([[ARG:%.*]] :
// CHECK-NEXT: debug_value
// CHECK-NEXT: copy_value
// CHECK-NEXT: move_value
// CHECK-NEXT: return
// CHECK: } // end sil function '$s8moveonly7useMoveyAA5KlassCADF'
func useMove(_ k: Klass) -> Klass {
Builtin.move(k)
}

// CHECK-LABEL: sil hidden [ossa] @$s8moveonly7useMoveyxxRlzClF : $@convention(thin) <T where T : AnyObject> (@guaranteed T) -> @owned T {
// CHECK: bb0([[ARG:%.*]] :
// CHECK-NEXT: debug_value
// CHECK-NEXT: copy_value
// CHECK-NEXT: move_value
// CHECK-NEXT: return
// CHECK: } // end sil function '$s8moveonly7useMoveyxxRlzClF'
func useMove<T : AnyObject>(_ k: T) -> T {
Builtin.move(k)
}
10 changes: 10 additions & 0 deletions test/SILGen/moveonly_generic_failure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: not --crash %target-swift-emit-silgen -parse-stdlib %s -disable-access-control -disable-objc-attr-requires-foundation-module

// REQUIRES: asserts

// This test makes sure that we do not accept using Builtin.move on address only
// types.

func addressOnlyMove<T>(t: T) -> T {
Builtin.move(t)
}