Skip to content

Commit b001b0b

Browse files
authored
Merge pull request #37898 from atrick/add-mandatory-hop
Add mandatory hop
2 parents 8687e61 + d3ac3d4 commit b001b0b

File tree

21 files changed

+178
-38
lines changed

21 files changed

+178
-38
lines changed

include/swift/AST/Builtins.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ BUILTIN_SIL_OPERATION(WithUnsafeContinuation, "withUnsafeContinuation", Special)
512512
/// the continuation is resumed.
513513
BUILTIN_SIL_OPERATION(WithUnsafeThrowingContinuation, "withUnsafeThrowingContinuation", Special)
514514

515+
/// Force the current task to be rescheduled on the specified actor.
516+
BUILTIN_SIL_OPERATION(HopToActor, "hopToActor", None)
517+
515518
#undef BUILTIN_SIL_OPERATION
516519

517520
// BUILTIN_RUNTIME_CALL - A call into a runtime function.

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,9 +2107,11 @@ class SILBuilder {
21072107
Throws));
21082108
}
21092109

2110-
HopToExecutorInst *createHopToExecutor(SILLocation Loc, SILValue Actor) {
2110+
HopToExecutorInst *createHopToExecutor(SILLocation Loc, SILValue Actor,
2111+
bool mandatory) {
21112112
return insert(new (getModule()) HopToExecutorInst(getSILDebugLocation(Loc),
2112-
Actor, hasOwnership()));
2113+
Actor, hasOwnership(),
2114+
mandatory));
21132115
}
21142116

21152117
ExtractExecutorInst *createExtractExecutor(SILLocation Loc, SILValue Actor) {

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3009,7 +3009,8 @@ ::visitHopToExecutorInst(HopToExecutorInst *Inst) {
30093009
recordClonedInstruction(Inst,
30103010
getBuilder().createHopToExecutor(
30113011
getOpLocation(Inst->getLoc()),
3012-
getOpValue(Inst->getTargetExecutor())));
3012+
getOpValue(Inst->getTargetExecutor()),
3013+
Inst->isMandatory()));
30133014
}
30143015

30153016
template <typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,11 +3477,15 @@ class HopToExecutorInst
34773477
friend SILBuilder;
34783478

34793479
HopToExecutorInst(SILDebugLocation debugLoc, SILValue executor,
3480-
bool hasOwnership)
3481-
: UnaryInstructionBase(debugLoc, executor) { }
3480+
bool hasOwnership, bool isMandatory)
3481+
: UnaryInstructionBase(debugLoc, executor) {
3482+
SILNode::Bits.HopToExecutorInst.mandatory = isMandatory;
3483+
}
34823484

34833485
public:
34843486
SILValue getTargetExecutor() const { return getOperand(); }
3487+
3488+
bool isMandatory() const { return SILNode::Bits.HopToExecutorInst.mandatory; }
34853489
};
34863490

34873491
/// Extract the ex that the code is executing on the operand executor already.

include/swift/SIL/SILNode.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ class alignas(8) SILNode :
342342
Immutable : 1
343343
);
344344

345+
SWIFT_INLINE_BITFIELD(HopToExecutorInst, NonValueInstruction, 1,
346+
mandatory : 1
347+
);
348+
345349
SWIFT_INLINE_BITFIELD(DestroyValueInst, NonValueInstruction, 1,
346350
PoisonRefs : 1);
347351

lib/AST/Builtins.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,17 @@ static ValueDecl *getWithUnsafeContinuation(ASTContext &ctx,
18451845
return builder.build(id);
18461846
}
18471847

1848+
static ValueDecl *getHopToActor(ASTContext &ctx, Identifier id) {
1849+
BuiltinFunctionBuilder builder(ctx);
1850+
auto *actorProto = ctx.getProtocol(KnownProtocolKind::Actor);
1851+
// Create type parameters and add conformance constraints.
1852+
auto actorParam = makeGenericParam();
1853+
builder.addParameter(actorParam);
1854+
builder.addConformanceRequirement(actorParam, actorProto);
1855+
builder.setResult(makeConcrete(TupleType::getEmpty(ctx)));
1856+
return builder.build(id);
1857+
}
1858+
18481859
/// An array of the overloaded builtin kinds.
18491860
static const OverloadedBuiltinKind OverloadedBuiltinKinds[] = {
18501861
OverloadedBuiltinKind::None,
@@ -2826,6 +2837,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
28262837
case BuiltinValueKind::WithUnsafeThrowingContinuation:
28272838
return getWithUnsafeContinuation(Context, Id, /*throws=*/true);
28282839

2840+
case BuiltinValueKind::HopToActor:
2841+
return getHopToActor(Context, Id);
2842+
28292843
case BuiltinValueKind::AutoDiffCreateLinearMapContext:
28302844
return getAutoDiffCreateLinearMapContext(Context, Id);
28312845

lib/SIL/IR/SILPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
21542154
}
21552155

21562156
void visitHopToExecutorInst(HopToExecutorInst *HTEI) {
2157+
if (HTEI->isMandatory())
2158+
*this << "[mandatory] ";
21572159
*this << getIDAndType(HTEI->getTargetExecutor());
21582160
}
21592161

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3024,7 +3024,6 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
30243024
UNARY_INSTRUCTION(EndBorrow)
30253025
UNARY_INSTRUCTION(DestructureStruct)
30263026
UNARY_INSTRUCTION(DestructureTuple)
3027-
UNARY_INSTRUCTION(HopToExecutor)
30283027
UNARY_INSTRUCTION(ExtractExecutor)
30293028
REFCOUNTING_INSTRUCTION(UnmanagedReleaseValue)
30303029
REFCOUNTING_INSTRUCTION(UnmanagedRetainValue)
@@ -3048,6 +3047,14 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
30483047
#undef UNARY_INSTRUCTION
30493048
#undef REFCOUNTING_INSTRUCTION
30503049

3050+
case SILInstructionKind::HopToExecutorInst: {
3051+
bool mandatory = false;
3052+
if (parseSILOptional(mandatory, *this, "mandatory")
3053+
|| parseTypedValueRef(Val, B) || parseSILDebugLocation(InstLoc, B))
3054+
return true;
3055+
ResultVal = B.createHopToExecutor(InstLoc, Val, mandatory);
3056+
break;
3057+
}
30513058
case SILInstructionKind::DestroyValueInst: {
30523059
bool poisonRefs = false;
30533060
if (parseSILOptional(poisonRefs, *this, "poison")

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,14 @@ static ManagedValue emitBuiltinWithUnsafeThrowingContinuation(
15701570
/*throws=*/true);
15711571
}
15721572

1573+
static ManagedValue emitBuiltinHopToActor(SILGenFunction &SGF, SILLocation loc,
1574+
SubstitutionMap subs,
1575+
ArrayRef<ManagedValue> args,
1576+
SGFContext C) {
1577+
SGF.emitHopToActorValue(loc, args[0]);
1578+
return ManagedValue::forUnmanaged(SGF.emitEmptyTuple(loc));
1579+
}
1580+
15731581
static ManagedValue emitBuiltinAutoDiffCreateLinearMapContext(
15741582
SILGenFunction &SGF, SILLocation loc, SubstitutionMap subs,
15751583
ArrayRef<ManagedValue> args, SGFContext C) {

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ void SILGenFunction::emitConstructorPrologActorHop(
692692

693693
if (auto executor = emitExecutor(loc, *maybeIso, None)) {
694694
ExpectedExecutor = *executor;
695-
B.createHopToExecutor(loc, *executor);
695+
B.createHopToExecutor(loc, *executor, /*mandatory*/ false);
696696
}
697697
}
698698

0 commit comments

Comments
 (0)