Skip to content

Commit 00d54eb

Browse files
committed
[reference-binding] Add mark_unresolved_reference_binding to signal from SILGen to the pass to check.
Just the SIL part of this.
1 parent 78d57ea commit 00d54eb

19 files changed

+141
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,5 +2080,11 @@ ERROR(parser_new_parser_errors,none,
20802080
"new Swift parser generated errors for code that C++ parser accepted",
20812081
())
20822082

2083+
// MARK: Reference Binding Diagnostics
2084+
ERROR(sil_markuncheckedreferencebinding_requires_attribute,none,
2085+
"mark_unchecked_reference_binding requires an attribute like [inout]", ())
2086+
ERROR(sil_markuncheckedreferencebinding_invalid_attribute,none,
2087+
"Attribute '[%0]' can not be applied to mark_unchecked_reference_binding", (StringRef))
2088+
20832089
#define UNDEFINE_DIAGNOSTIC_MACROS
20842090
#include "DefineDiagnosticMacros.h"

include/swift/SIL/MemAccessUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,7 @@ inline bool isAccessStorageIdentityCast(SingleValueInstruction *svi) {
16321632
// Simply pass-thru the incoming address.
16331633
case SILInstructionKind::MarkUninitializedInst:
16341634
case SILInstructionKind::MarkMustCheckInst:
1635+
case SILInstructionKind::MarkUnresolvedReferenceBindingInst:
16351636
case SILInstructionKind::MarkDependenceInst:
16361637
case SILInstructionKind::CopyValueInst:
16371638
return true;

include/swift/SIL/SILBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,13 @@ class SILBuilder {
13301330
MarkMustCheckInst(getSILDebugLocation(loc), src, kind));
13311331
}
13321332

1333+
MarkUnresolvedReferenceBindingInst *createMarkUnresolvedReferenceBindingInst(
1334+
SILLocation loc, SILValue src,
1335+
MarkUnresolvedReferenceBindingInst::Kind kind) {
1336+
return insert(new (getModule()) MarkUnresolvedReferenceBindingInst(
1337+
getSILDebugLocation(loc), src, kind));
1338+
}
1339+
13331340
CopyableToMoveOnlyWrapperValueInst *
13341341
createOwnedCopyableToMoveOnlyWrapperValue(SILLocation loc, SILValue src) {
13351342
return insert(new (getModule()) CopyableToMoveOnlyWrapperValueInst(

include/swift/SIL/SILCloner.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,16 @@ void SILCloner<ImplClass>::visitMarkMustCheckInst(MarkMustCheckInst *Inst) {
18891889
recordClonedInstruction(Inst, MVI);
18901890
}
18911891

1892+
template <typename ImplClass>
1893+
void SILCloner<ImplClass>::visitMarkUnresolvedReferenceBindingInst(
1894+
MarkUnresolvedReferenceBindingInst *Inst) {
1895+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1896+
auto *MVI = getBuilder().createMarkUnresolvedReferenceBindingInst(
1897+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1898+
Inst->getKind());
1899+
recordClonedInstruction(Inst, MVI);
1900+
}
1901+
18921902
template <typename ImplClass>
18931903
void SILCloner<ImplClass>::visitMoveOnlyWrapperToCopyableValueInst(
18941904
MoveOnlyWrapperToCopyableValueInst *inst) {

include/swift/SIL/SILInstruction.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8339,6 +8339,37 @@ class MarkMustCheckInst
83398339
}
83408340
};
83418341

8342+
/// A marker instruction that states a given alloc_box or alloc_stack is a
8343+
/// reference binding that must be transformed.
8344+
class MarkUnresolvedReferenceBindingInst
8345+
: public UnaryInstructionBase<
8346+
SILInstructionKind::MarkUnresolvedReferenceBindingInst,
8347+
SingleValueInstruction>,
8348+
public OwnershipForwardingMixin {
8349+
friend class SILBuilder;
8350+
8351+
public:
8352+
enum class Kind : unsigned {
8353+
Invalid = 0,
8354+
8355+
InOut = 1,
8356+
};
8357+
8358+
private:
8359+
Kind kind;
8360+
8361+
MarkUnresolvedReferenceBindingInst(SILDebugLocation debugLoc,
8362+
SILValue operand, Kind kind)
8363+
: UnaryInstructionBase(debugLoc, operand, operand->getType()),
8364+
OwnershipForwardingMixin(
8365+
SILInstructionKind::MarkUnresolvedReferenceBindingInst,
8366+
operand->getOwnershipKind()),
8367+
kind(kind) {}
8368+
8369+
public:
8370+
Kind getKind() const { return kind; }
8371+
};
8372+
83428373
/// Convert from a non-trivial copyable type to an `@moveOnly` wrapper type.
83438374
///
83448375
/// IMPORTANT: Unlike other forwarding instructions, the ownership of
@@ -10693,7 +10724,8 @@ inline bool OwnershipForwardingMixin::isa(SILInstructionKind kind) {
1069310724
OwnershipForwardingConversionInst::classof(kind) ||
1069410725
OwnershipForwardingSelectEnumInstBase::classof(kind) ||
1069510726
OwnershipForwardingMultipleValueInstruction::classof(kind) ||
10696-
kind == SILInstructionKind::MarkMustCheckInst;
10727+
kind == SILInstructionKind::MarkMustCheckInst ||
10728+
kind == SILInstructionKind::MarkUnresolvedReferenceBindingInst;
1069710729
}
1069810730

1069910731
inline OwnershipForwardingMixin *
@@ -10717,6 +10749,8 @@ OwnershipForwardingMixin::get(SILInstruction *inst) {
1071710749
return result;
1071810750
if (auto *result = dyn_cast<MarkMustCheckInst>(inst))
1071910751
return result;
10752+
if (auto *result = dyn_cast<MarkUnresolvedReferenceBindingInst>(inst))
10753+
return result;
1072010754
if (auto *result = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(inst))
1072110755
return result;
1072210756
if (auto *result = dyn_cast<CopyableToMoveOnlyWrapperValueInst>(inst))

include/swift/SIL/SILNodes.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
467467
// running the relevant diagnostic.
468468
SINGLE_VALUE_INST(MarkMustCheckInst, mark_must_check,
469469
SingleValueInstruction, None, DoesNotRelease)
470+
// A canary value inserted by a SIL generating frontend to signal to the
471+
// reference binding transform to check/transform a specific value. Valid
472+
// only in Raw SIL. The relevant checkers should remove the instruction after
473+
// successfully running the relevant diagnostic.
474+
SINGLE_VALUE_INST(MarkUnresolvedReferenceBindingInst, mark_unresolved_reference_binding,
475+
SingleValueInstruction, None, DoesNotRelease)
470476
// Convert a $T to $@moveOnly T. This is the method that one uses to convert a
471477
// trivial value to a non-trivial move only value. Ownership is fixed at construction by
472478
// frontend to express specific semantics: guaranteed for guaranteed function arguments

lib/IRGen/IRGenSIL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,10 @@ class IRGenSILFunction :
12251225
void visitMarkMustCheckInst(MarkMustCheckInst *i) {
12261226
llvm_unreachable("Invalid in Lowered SIL");
12271227
}
1228+
void visitMarkUnresolvedReferenceBindingInst(
1229+
MarkUnresolvedReferenceBindingInst *i) {
1230+
llvm_unreachable("Invalid in Lowered SIL");
1231+
}
12281232
void visitCopyableToMoveOnlyWrapperValueInst(
12291233
CopyableToMoveOnlyWrapperValueInst *i) {
12301234
auto e = getLoweredExplosion(i->getOperand());

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,9 @@ static bool canParsePattern(Parser &P) {
13911391
P.consumeToken();
13921392
return true;
13931393
case tok::kw_inout:
1394+
if (!P.Context.LangOpts.hasFeature(Feature::ReferenceBindings))
1395+
return false;
1396+
LLVM_FALLTHROUGH;
13941397
case tok::kw_let:
13951398
case tok::kw_var:
13961399
P.consumeToken();

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ FORWARDING_OWNERSHIP(InitExistentialRef)
367367
FORWARDING_OWNERSHIP(DifferentiableFunction)
368368
FORWARDING_OWNERSHIP(LinearFunction)
369369
FORWARDING_OWNERSHIP(MarkMustCheck)
370+
FORWARDING_OWNERSHIP(MarkUnresolvedReferenceBinding)
370371
FORWARDING_OWNERSHIP(MoveOnlyWrapperToCopyableValue)
371372
FORWARDING_OWNERSHIP(CopyableToMoveOnlyWrapperValue)
372373
#undef FORWARDING_OWNERSHIP

lib/SIL/IR/SILPrinter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,19 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
20072007
*this << getIDAndType(I->getOperand());
20082008
}
20092009

2010+
void visitMarkUnresolvedReferenceBindingInst(
2011+
MarkUnresolvedReferenceBindingInst *I) {
2012+
using Kind = MarkUnresolvedReferenceBindingInst::Kind;
2013+
switch (I->getKind()) {
2014+
case Kind::Invalid:
2015+
llvm_unreachable("Invalid?!");
2016+
case Kind::InOut:
2017+
*this << "[inout] ";
2018+
break;
2019+
}
2020+
*this << getIDAndType(I->getOperand());
2021+
}
2022+
20102023
void visitCopyableToMoveOnlyWrapperValueInst(
20112024
CopyableToMoveOnlyWrapperValueInst *I) {
20122025
switch (I->getInitialKind()) {

0 commit comments

Comments
 (0)