Skip to content

Commit 0472c0d

Browse files
Merge pull request swiftlang#40926 from nate-chandler/sil/move_value_defined_flag
[SIL] Added lexical flag to move_value.
2 parents 3d93d8a + 5725108 commit 0472c0d

File tree

11 files changed

+84
-25
lines changed

11 files changed

+84
-25
lines changed

docs/SIL.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5304,7 +5304,7 @@ move_value
53045304

53055305
::
53065306

5307-
sil-instruction ::= 'move_value' sil-operand
5307+
sil-instruction ::= 'move_value' '[lexical]'? sil-operand
53085308

53095309
%1 = move_value %0 : $@_moveOnly A
53105310

@@ -5327,6 +5327,9 @@ NOTE: This instruction is used in an experimental feature called 'move only
53275327
values'. A move_value instruction is an instruction that introduces (or injects)
53285328
a type `T` into the move only value space.
53295329

5330+
The ``lexical`` attribute specifies that the value corresponds to a local
5331+
variable in the Swift source.
5332+
53305333
release_value
53315334
`````````````
53325335

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,12 +1263,13 @@ class SILBuilder {
12631263
operand, poisonRefs));
12641264
}
12651265

1266-
MoveValueInst *createMoveValue(SILLocation loc, SILValue operand) {
1266+
MoveValueInst *createMoveValue(SILLocation loc, SILValue operand,
1267+
bool isLexical = false) {
12671268
assert(!operand->getType().isTrivial(getFunction()) &&
12681269
"Should not be passing trivial values to this api. Use instead "
12691270
"emitMoveValueOperation");
1270-
return insert(new (getModule())
1271-
MoveValueInst(getSILDebugLocation(loc), operand));
1271+
return insert(new (getModule()) MoveValueInst(getSILDebugLocation(loc),
1272+
operand, isLexical));
12721273
}
12731274

12741275
MarkUnresolvedMoveAddrInst *createMarkUnresolvedMoveAddr(SILLocation loc,

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,8 @@ template <typename ImplClass>
17841784
void SILCloner<ImplClass>::visitMoveValueInst(MoveValueInst *Inst) {
17851785
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
17861786
auto *MVI = getBuilder().createMoveValue(getOpLocation(Inst->getLoc()),
1787-
getOpValue(Inst->getOperand()));
1787+
getOpValue(Inst->getOperand()),
1788+
Inst->isLexical());
17881789
MVI->setAllowsDiagnostics(Inst->getAllowDiagnostics());
17891790
recordClonedInstruction(Inst, MVI);
17901791
}

include/swift/SIL/SILInstruction.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7459,13 +7459,17 @@ class MoveValueInst
74597459
/// set to false, we shouldn't emit such a diagnostic. This is a short term
74607460
/// addition until we get MoveOnly wrapper types into the SIL type system.
74617461
bool allowDiagnostics = false;
7462+
bool lexical = false;
74627463

7463-
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand)
7464-
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {}
7464+
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand, bool isLexical)
7465+
: UnaryInstructionBase(DebugLoc, operand, operand->getType()),
7466+
lexical(isLexical) {}
74657467

74667468
public:
74677469
bool getAllowDiagnostics() const { return allowDiagnostics; }
74687470
void setAllowsDiagnostics(bool newValue) { allowDiagnostics = newValue; }
7471+
7472+
bool isLexical() const { return lexical; };
74697473
};
74707474

74717475
/// Equivalent to a copy_addr to [init] except that it is used for diagnostics

lib/SIL/IR/SILPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
18961896
void visitMoveValueInst(MoveValueInst *I) {
18971897
if (I->getAllowDiagnostics())
18981898
*this << "[allows_diagnostics] ";
1899+
if (I->isLexical())
1900+
*this << "[lexical] ";
18991901
*this << getIDAndType(I->getOperand());
19001902
}
19011903

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,21 +3295,28 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
32953295

32963296
case SILInstructionKind::MoveValueInst: {
32973297
bool allowsDiagnostics = false;
3298+
bool isLexical = false;
3299+
32983300
StringRef AttrName;
3299-
if (parseSILOptional(AttrName, *this)) {
3300-
if (!AttrName.equals("allows_diagnostics")) {
3301-
auto diag = diag::sil_movevalue_invalid_optional_attribute;
3302-
P.diagnose(InstLoc.getSourceLoc(), diag, AttrName);
3301+
SourceLoc AttrLoc;
3302+
while (parseSILOptional(AttrName, AttrLoc, *this)) {
3303+
if (AttrName == "allows_diagnostics")
3304+
allowsDiagnostics = true;
3305+
else if (AttrName == "lexical")
3306+
isLexical = true;
3307+
else {
3308+
P.diagnose(InstLoc.getSourceLoc(),
3309+
diag::sil_invalid_attribute_for_instruction, AttrName,
3310+
"move_value");
33033311
return true;
33043312
}
3305-
allowsDiagnostics = true;
33063313
}
33073314

33083315
if (parseTypedValueRef(Val, B))
33093316
return true;
33103317
if (parseSILDebugLocation(InstLoc, B))
33113318
return true;
3312-
auto *MVI = B.createMoveValue(InstLoc, Val);
3319+
auto *MVI = B.createMoveValue(InstLoc, Val, isLexical);
33133320
MVI->setAllowsDiagnostics(allowsDiagnostics);
33143321
ResultVal = MVI;
33153322
break;

lib/Serialization/DeserializeSIL.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,10 +1999,12 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
19991999

20002000
case SILInstructionKind::MoveValueInst: {
20012001
auto Ty = MF->getType(TyID);
2002-
auto AllowsDiagnostics = bool(Attr);
2002+
bool AllowsDiagnostics = Attr & 0x1;
2003+
bool IsLexical = (Attr >> 1) & 0x1;
20032004
auto *MVI = Builder.createMoveValue(
20042005
Loc,
2005-
getLocalValue(ValID, getSILType(Ty, (SILValueCategory)TyCategory, Fn)));
2006+
getLocalValue(ValID, getSILType(Ty, (SILValueCategory)TyCategory, Fn)),
2007+
IsLexical);
20062008
MVI->setAllowsDiagnostics(AllowsDiagnostics);
20072009
ResultInst = MVI;
20082010
break;

lib/Serialization/SerializeSIL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,8 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
14881488
} else if (auto *BBI = dyn_cast<BeginBorrowInst>(&SI)) {
14891489
Attr = BBI->isLexical();
14901490
} else if (auto *MVI = dyn_cast<MoveValueInst>(&SI)) {
1491-
Attr = MVI->getAllowDiagnostics();
1491+
Attr = unsigned(MVI->getAllowDiagnostics()) |
1492+
(unsigned(MVI->isLexical() << 1));
14921493
}
14931494
writeOneOperandLayout(SI.getKind(), Attr, SI.getOperand(0));
14941495
break;

test/SIL/Parser/basic2.sil

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,22 @@ bb0(%0 : @owned $Builtin.NativeObject):
4545
}
4646

4747
// CHECK-LABEL: sil [ossa] @test_movevalue_parsing : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
48-
// CHECK: bb0(%0 :
49-
// CHECK-NEXT: %1 = move_value %0 : $Builtin.NativeObject
50-
// CHECK-NEXT: return
48+
// CHECK: bb0([[REGISTER_0:%[^,]+]] :
49+
// CHECK-NEXT: [[REGISTER_1:%[^,]+]] = move_value [[REGISTER_0]]
50+
// CHECK-NEXT: [[REGISTER_2:%[^,]+]] = move_value [allows_diagnostics] [[REGISTER_1]]
51+
// CHECK-NEXT: [[REGISTER_3:%[^,]+]] = move_value [lexical] [[REGISTER_2]]
52+
// CHECK-NEXT: [[REGISTER_4:%[^,]+]] = move_value [allows_diagnostics] [lexical] [[REGISTER_3]]
53+
// CHECK-NEXT: [[REGISTER_5:%[^,]+]] = move_value [allows_diagnostics] [lexical] [[REGISTER_4]]
54+
// CHECK-NEXT: return [[REGISTER_5]]
5155
// CHECK-NEXT: } // end sil function 'test_movevalue_parsing'
5256
sil [ossa] @test_movevalue_parsing : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
5357
bb0(%0 : @owned $Builtin.NativeObject):
5458
%1 = move_value %0 : $Builtin.NativeObject
55-
return %1 : $Builtin.NativeObject
59+
%2 = move_value [allows_diagnostics] %1 : $Builtin.NativeObject
60+
%3 = move_value [lexical] %2 : $Builtin.NativeObject
61+
%4 = move_value [allows_diagnostics] [lexical] %3 : $Builtin.NativeObject
62+
%5 = move_value [lexical] [allows_diagnostics] %4 : $Builtin.NativeObject
63+
return %5 : $Builtin.NativeObject
5664
}
5765

5866
// CHECK-LABEL: sil @test_movevalue_parsing_non_ossa : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {

test/SIL/Serialization/basic.sil

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,22 @@ bb0:
6969
}
7070

7171
// CHECK-LABEL: sil [ossa] @test_movevalue_parsing : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
72-
// CHECK: bb0(%0 :
73-
// CHECK-NEXT: %1 = move_value %0 : $Builtin.NativeObject
74-
// CHECK-NEXT: %2 = move_value [allows_diagnostics] %1 : $Builtin.NativeObject
75-
// CHECK-NEXT: return
72+
// CHECK: bb0([[REGISTER_0:%[^,]+]] :
73+
// CHECK-NEXT: [[REGISTER_1:%[^,]+]] = move_value [[REGISTER_0]]
74+
// CHECK-NEXT: [[REGISTER_2:%[^,]+]] = move_value [allows_diagnostics] [[REGISTER_1]]
75+
// CHECK-NEXT: [[REGISTER_3:%[^,]+]] = move_value [lexical] [[REGISTER_2]]
76+
// CHECK-NEXT: [[REGISTER_4:%[^,]+]] = move_value [allows_diagnostics] [lexical] [[REGISTER_3]]
77+
// CHECK-NEXT: [[REGISTER_5:%[^,]+]] = move_value [allows_diagnostics] [lexical] [[REGISTER_4]]
78+
// CHECK-NEXT: return [[REGISTER_5]]
7679
// CHECK-NEXT: } // end sil function 'test_movevalue_parsing'
7780
sil [ossa] @test_movevalue_parsing : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
7881
bb0(%0 : @owned $Builtin.NativeObject):
7982
%1 = move_value %0 : $Builtin.NativeObject
8083
%2 = move_value [allows_diagnostics] %1 : $Builtin.NativeObject
81-
return %2 : $Builtin.NativeObject
84+
%3 = move_value [lexical] %2 : $Builtin.NativeObject
85+
%4 = move_value [allows_diagnostics] [lexical] %3 : $Builtin.NativeObject
86+
%5 = move_value [lexical] [allows_diagnostics] %4 : $Builtin.NativeObject
87+
return %5 : $Builtin.NativeObject
8288
}
8389

8490
// CHECK-LABEL: sil @test_movevalue_parsing_non_ossa : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {

0 commit comments

Comments
 (0)