Skip to content

Commit 99d87c3

Browse files
committed
[moveOnly] Allow for move_value to have an optional [allows_diagnostics] modifier.
This is a signal to the move value kill analysis that this is a move that should have diagnostics emitted for it. It is a temporary addition until we add MoveOnly to the SIL type system.
1 parent 6b4231b commit 99d87c3

File tree

12 files changed

+64
-11
lines changed

12 files changed

+64
-11
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ ERROR(sil_keypath_index_operand_type_conflict,none,
633633
ERROR(sil_keypath_no_use_of_operand_in_pattern,none,
634634
"operand %0 is not referenced by any component in the pattern",
635635
(unsigned))
636+
ERROR(sil_movevalue_invalid_optional_attribute,none,
637+
"Optional attribute '[%0]' can not be applied to move_value", (StringRef))
636638

637639
// SIL Basic Blocks
638640
ERROR(expected_sil_block_name,none,

include/swift/SIL/SILCloner.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,9 +1731,10 @@ void SILCloner<ImplClass>::visitExplicitCopyValueInst(
17311731
template <typename ImplClass>
17321732
void SILCloner<ImplClass>::visitMoveValueInst(MoveValueInst *Inst) {
17331733
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1734-
recordClonedInstruction(
1735-
Inst, getBuilder().createMoveValue(getOpLocation(Inst->getLoc()),
1736-
getOpValue(Inst->getOperand())));
1734+
auto *MVI = getBuilder().createMoveValue(getOpLocation(Inst->getLoc()),
1735+
getOpValue(Inst->getOperand()));
1736+
MVI->setAllowsDiagnostics(Inst->getAllowDiagnostics());
1737+
recordClonedInstruction(Inst, MVI);
17371738
}
17381739

17391740
template <typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7337,8 +7337,17 @@ class MoveValueInst
73377337
SingleValueInstruction> {
73387338
friend class SILBuilder;
73397339

7340+
/// If set to true, we should emit the kill diagnostic for this move_value. If
7341+
/// set to false, we shouldn't emit such a diagnostic. This is a short term
7342+
/// addition until we get MoveOnly wrapper types into the SIL type system.
7343+
bool allowDiagnostics = false;
7344+
73407345
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand)
73417346
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {}
7347+
7348+
public:
7349+
bool getAllowDiagnostics() const { return allowDiagnostics; }
7350+
void setAllowsDiagnostics(bool newValue) { allowDiagnostics = newValue; }
73427351
};
73437352

73447353
/// Given an object reference, return true iff it is non-nil and refers

lib/SIL/IR/SILPrinter.cpp

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

18861886
void visitMoveValueInst(MoveValueInst *I) {
1887+
if (I->getAllowDiagnostics())
1888+
*this << "[allows_diagnostics] ";
18871889
*this << getIDAndType(I->getOperand());
18881890
}
18891891

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3156,7 +3156,6 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
31563156
UNARY_INSTRUCTION(DestroyAddr)
31573157
UNARY_INSTRUCTION(CopyValue)
31583158
UNARY_INSTRUCTION(ExplicitCopyValue)
3159-
UNARY_INSTRUCTION(MoveValue)
31603159
UNARY_INSTRUCTION(EndBorrow)
31613160
UNARY_INSTRUCTION(DestructureStruct)
31623161
UNARY_INSTRUCTION(DestructureTuple)
@@ -3266,6 +3265,28 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
32663265
break;
32673266
}
32683267

3268+
case SILInstructionKind::MoveValueInst: {
3269+
bool allowsDiagnostics = false;
3270+
StringRef AttrName;
3271+
if (parseSILOptional(AttrName, *this)) {
3272+
if (!AttrName.equals("allows_diagnostics")) {
3273+
auto diag = diag::sil_movevalue_invalid_optional_attribute;
3274+
P.diagnose(InstLoc.getSourceLoc(), diag, AttrName);
3275+
return true;
3276+
}
3277+
allowsDiagnostics = true;
3278+
}
3279+
3280+
if (parseTypedValueRef(Val, B))
3281+
return true;
3282+
if (parseSILDebugLocation(InstLoc, B))
3283+
return true;
3284+
auto *MVI = B.createMoveValue(InstLoc, Val);
3285+
MVI->setAllowsDiagnostics(allowsDiagnostics);
3286+
ResultVal = MVI;
3287+
break;
3288+
}
3289+
32693290
case SILInstructionKind::LoadInst: {
32703291
Optional<LoadOwnershipQualifier> Qualifier;
32713292
SourceLoc AddrLoc;

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,10 @@ void SILInlineCloner::visitBuiltinInst(BuiltinInst *Inst) {
682682
SILValue otherValue = getBuilder().emitLoadValueOperation(
683683
opLoc, otherSrcAddr, LoadOwnershipQualifier::Take);
684684

685+
// Create a move_value and set that we want it to be used for diagnostic
686+
// emission.
685687
auto *mvi = getBuilder().createMoveValue(opLoc, otherValue);
686-
688+
mvi->setAllowsDiagnostics(true);
687689
getBuilder().emitStoreValueOperation(opLoc, mvi, otherResultAddr,
688690
StoreOwnershipQualifier::Init);
689691

lib/Serialization/DeserializeSIL.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,6 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
18671867
REFCOUNTING_INSTRUCTION(UnmanagedRetainValue)
18681868
UNARY_INSTRUCTION(CopyValue)
18691869
UNARY_INSTRUCTION(ExplicitCopyValue)
1870-
UNARY_INSTRUCTION(MoveValue)
18711870
REFCOUNTING_INSTRUCTION(ReleaseValue)
18721871
REFCOUNTING_INSTRUCTION(ReleaseValueAddr)
18731872
REFCOUNTING_INSTRUCTION(UnmanagedReleaseValue)
@@ -1984,6 +1983,17 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
19841983
break;
19851984
}
19861985

1986+
case SILInstructionKind::MoveValueInst: {
1987+
auto Ty = MF->getType(TyID);
1988+
auto AllowsDiagnostics = bool(Attr);
1989+
auto *MVI = Builder.createMoveValue(
1990+
Loc,
1991+
getLocalValue(ValID, getSILType(Ty, (SILValueCategory)TyCategory, Fn)));
1992+
MVI->setAllowsDiagnostics(AllowsDiagnostics);
1993+
ResultInst = MVI;
1994+
break;
1995+
}
1996+
19871997
case SILInstructionKind::LoadInst: {
19881998
auto Ty = MF->getType(TyID);
19891999
auto Qualifier = LoadOwnershipQualifier(Attr);

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5656
/// describe what change you made. The content of this comment isn't important;
5757
/// it just ensures a conflict if two people change the module format.
5858
/// Don't worry about adhering to the 80-column limit for this line.
59-
const uint16_t SWIFTMODULE_VERSION_MINOR = 640; // @_typeSequence
59+
const uint16_t SWIFTMODULE_VERSION_MINOR = 641; // diagnostic move only
6060

6161
/// A standard hash seed used for all string hashes in a serialized module.
6262
///

lib/Serialization/SerializeSIL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,8 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
14621462
Attr = ECMI->doKeepUnique();
14631463
} else if (auto *BBI = dyn_cast<BeginBorrowInst>(&SI)) {
14641464
Attr = BBI->isLexical();
1465+
} else if (auto *MVI = dyn_cast<MoveValueInst>(&SI)) {
1466+
Attr = MVI->getAllowDiagnostics();
14651467
}
14661468
writeOneOperandLayout(SI.getKind(), Attr, SI.getOperand(0));
14671469
break;

test/SIL/Parser/basic2.sil

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ bb0(%0 : @owned $Builtin.NativeObject):
5858
// CHECK-LABEL: sil @test_movevalue_parsing_non_ossa : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
5959
// CHECK: bb0(%0 :
6060
// CHECK-NEXT: %1 = move_value %0 : $Builtin.NativeObject
61+
// CHECK-NEXT: %2 = move_value [allows_diagnostics] %1 : $Builtin.NativeObject
6162
// CHECK-NEXT: return
6263
// CHECK-NEXT: } // end sil function 'test_movevalue_parsing_non_ossa'
6364
sil @test_movevalue_parsing_non_ossa : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
6465
bb0(%0 : $Builtin.NativeObject):
6566
%1 = move_value %0 : $Builtin.NativeObject
66-
return %1 : $Builtin.NativeObject
67+
%2 = move_value [allows_diagnostics] %1 : $Builtin.NativeObject
68+
return %2 : $Builtin.NativeObject
6769
}

0 commit comments

Comments
 (0)