Skip to content

Commit 5725108

Browse files
committed
[SIL] Added lexical flag to move_value.
The new flag will be used to track whether a move_value corresponds to a source-level lexical scope. Here, the flag is just added to the instruction and represented in textual and serialized SIL.
1 parent 0bf1389 commit 5725108

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
@@ -5280,7 +5280,7 @@ move_value
52805280

52815281
::
52825282

5283-
sil-instruction ::= 'move_value' sil-operand
5283+
sil-instruction ::= 'move_value' '[lexical]'? sil-operand
52845284

52855285
%1 = move_value %0 : $@_moveOnly A
52865286

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

5306+
The ``lexical`` attribute specifies that the value corresponds to a local
5307+
variable in the Swift source.
5308+
53065309
release_value
53075310
`````````````
53085311

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
@@ -7457,13 +7457,17 @@ class MoveValueInst
74577457
/// set to false, we shouldn't emit such a diagnostic. This is a short term
74587458
/// addition until we get MoveOnly wrapper types into the SIL type system.
74597459
bool allowDiagnostics = false;
7460+
bool lexical = false;
74607461

7461-
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand)
7462-
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {}
7462+
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand, bool isLexical)
7463+
: UnaryInstructionBase(DebugLoc, operand, operand->getType()),
7464+
lexical(isLexical) {}
74637465

74647466
public:
74657467
bool getAllowDiagnostics() const { return allowDiagnostics; }
74667468
void setAllowsDiagnostics(bool newValue) { allowDiagnostics = newValue; }
7469+
7470+
bool isLexical() const { return lexical; };
74677471
};
74687472

74697473
/// 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
@@ -3275,21 +3275,28 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
32753275

32763276
case SILInstructionKind::MoveValueInst: {
32773277
bool allowsDiagnostics = false;
3278+
bool isLexical = false;
3279+
32783280
StringRef AttrName;
3279-
if (parseSILOptional(AttrName, *this)) {
3280-
if (!AttrName.equals("allows_diagnostics")) {
3281-
auto diag = diag::sil_movevalue_invalid_optional_attribute;
3282-
P.diagnose(InstLoc.getSourceLoc(), diag, AttrName);
3281+
SourceLoc AttrLoc;
3282+
while (parseSILOptional(AttrName, AttrLoc, *this)) {
3283+
if (AttrName == "allows_diagnostics")
3284+
allowsDiagnostics = true;
3285+
else if (AttrName == "lexical")
3286+
isLexical = true;
3287+
else {
3288+
P.diagnose(InstLoc.getSourceLoc(),
3289+
diag::sil_invalid_attribute_for_instruction, AttrName,
3290+
"move_value");
32833291
return true;
32843292
}
3285-
allowsDiagnostics = true;
32863293
}
32873294

32883295
if (parseTypedValueRef(Val, B))
32893296
return true;
32903297
if (parseSILDebugLocation(InstLoc, B))
32913298
return true;
3292-
auto *MVI = B.createMoveValue(InstLoc, Val);
3299+
auto *MVI = B.createMoveValue(InstLoc, Val, isLexical);
32933300
MVI->setAllowsDiagnostics(allowsDiagnostics);
32943301
ResultVal = MVI;
32953302
break;

lib/Serialization/DeserializeSIL.cpp

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

19851985
case SILInstructionKind::MoveValueInst: {
19861986
auto Ty = MF->getType(TyID);
1987-
auto AllowsDiagnostics = bool(Attr);
1987+
bool AllowsDiagnostics = Attr & 0x1;
1988+
bool IsLexical = (Attr >> 1) & 0x1;
19881989
auto *MVI = Builder.createMoveValue(
19891990
Loc,
1990-
getLocalValue(ValID, getSILType(Ty, (SILValueCategory)TyCategory, Fn)));
1991+
getLocalValue(ValID, getSILType(Ty, (SILValueCategory)TyCategory, Fn)),
1992+
IsLexical);
19911993
MVI->setAllowsDiagnostics(AllowsDiagnostics);
19921994
ResultInst = MVI;
19931995
break;

lib/Serialization/SerializeSIL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,8 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
14631463
} else if (auto *BBI = dyn_cast<BeginBorrowInst>(&SI)) {
14641464
Attr = BBI->isLexical();
14651465
} else if (auto *MVI = dyn_cast<MoveValueInst>(&SI)) {
1466-
Attr = MVI->getAllowDiagnostics();
1466+
Attr = unsigned(MVI->getAllowDiagnostics()) |
1467+
(unsigned(MVI->isLexical() << 1));
14671468
}
14681469
writeOneOperandLayout(SI.getKind(), Attr, SI.getOperand(0));
14691470
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)