Skip to content

Commit 031037c

Browse files
committed
[SIL] Added var_decl flag to borrows/moves.
1 parent 952b086 commit 031037c

File tree

16 files changed

+89
-34
lines changed

16 files changed

+89
-34
lines changed

docs/SIL.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4500,12 +4500,16 @@ live. This makes sense semantically since ``%1`` is modeling a new value with a
45004500
dependent lifetime on ``%0``.
45014501

45024502
The optional ``lexical`` attribute specifies that the operand corresponds to a
4503-
local variable in the Swift source, so special care must be taken when moving
4504-
the end_borrow.
4503+
local variable with a lexical lifetime in the Swift source, so special care
4504+
must be taken when moving the end_borrow. Compare to the ``var_decl``
4505+
attribute.
45054506

45064507
The optional ``pointer_escape`` attribute specifies that a pointer to the
45074508
operand escapes within the borrow scope introduced by this begin_borrow.
45084509

4510+
The optional ``var_decl`` attribute specifies that the operand corresponds to a
4511+
local variable in the Swift source.
4512+
45094513
This instruction is only valid in functions in Ownership SSA form.
45104514

45114515
end_borrow
@@ -6259,11 +6263,15 @@ values'. A move_value instruction is an instruction that introduces (or injects)
62596263
a type `T` into the move only value space.
62606264

62616265
The ``lexical`` attribute specifies that the value corresponds to a local
6262-
variable in the Swift source.
6266+
variable with a lexical lifetime in the Swift source. Compare to the
6267+
``var_decl`` attribute.
62636268

62646269
The optional ``pointer_escape`` attribute specifies that a pointer to the
62656270
operand escapes within the scope introduced by this move_value.
62666271

6272+
The optional ``var_decl`` attribute specifies that the operand corresponds to a
6273+
local variable in the Swift source.
6274+
62676275

62686276
drop_deinit
62696277
```````````

include/swift/SIL/SILBuilder.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,13 @@ class SILBuilder {
817817

818818
BeginBorrowInst *createBeginBorrow(SILLocation Loc, SILValue LV,
819819
bool isLexical = false,
820-
bool hasPointerEscape = false) {
820+
bool hasPointerEscape = false,
821+
bool fromVarDecl = false) {
821822
assert(getFunction().hasOwnership());
822823
assert(!LV->getType().isAddress());
823-
return insert(new (getModule()) BeginBorrowInst(getSILDebugLocation(Loc),
824-
LV, isLexical, hasPointerEscape));
824+
return insert(new (getModule())
825+
BeginBorrowInst(getSILDebugLocation(Loc), LV, isLexical,
826+
hasPointerEscape, fromVarDecl));
825827
}
826828

827829
/// Convenience function for creating a load_borrow on non-trivial values and
@@ -1415,13 +1417,15 @@ class SILBuilder {
14151417

14161418
MoveValueInst *createMoveValue(SILLocation loc, SILValue operand,
14171419
bool isLexical = false,
1418-
bool hasPointerEscape = false) {
1420+
bool hasPointerEscape = false,
1421+
bool fromVarDecl = false) {
14191422
assert(getFunction().hasOwnership());
14201423
assert(!operand->getType().isTrivial(getFunction()) &&
14211424
"Should not be passing trivial values to this api. Use instead "
14221425
"emitMoveValueOperation");
1423-
return insert(new (getModule()) MoveValueInst(
1424-
getSILDebugLocation(loc), operand, isLexical, hasPointerEscape));
1426+
return insert(new (getModule())
1427+
MoveValueInst(getSILDebugLocation(loc), operand,
1428+
isLexical, hasPointerEscape, fromVarDecl));
14251429
}
14261430

14271431
DropDeinitInst *createDropDeinit(SILLocation loc, SILValue operand) {

include/swift/SIL/SILCloner.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,11 @@ void SILCloner<ImplClass>::visitBeginBorrowInst(BeginBorrowInst *Inst) {
12171217
return recordFoldedValue(Inst, getOpValue(Inst->getOperand()));
12181218
}
12191219

1220-
recordClonedInstruction(
1221-
Inst, getBuilder().createBeginBorrow(
1222-
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1223-
Inst->isLexical(), Inst->hasPointerEscape()));
1220+
recordClonedInstruction(Inst,
1221+
getBuilder().createBeginBorrow(
1222+
getOpLocation(Inst->getLoc()),
1223+
getOpValue(Inst->getOperand()), Inst->isLexical(),
1224+
Inst->hasPointerEscape(), Inst->isFromVarDecl()));
12241225
}
12251226

12261227
template <typename ImplClass>
@@ -1932,7 +1933,7 @@ void SILCloner<ImplClass>::visitMoveValueInst(MoveValueInst *Inst) {
19321933
}
19331934
auto *MVI = getBuilder().createMoveValue(
19341935
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1935-
Inst->isLexical(), Inst->hasPointerEscape());
1936+
Inst->isLexical(), Inst->hasPointerEscape(), Inst->isFromVarDecl());
19361937
MVI->setAllowsDiagnostics(Inst->getAllowDiagnostics());
19371938
recordClonedInstruction(Inst, MVI);
19381939
}

include/swift/SIL/SILInstruction.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,11 +4357,12 @@ class BeginBorrowInst
43574357
USE_SHARED_UINT8;
43584358

43594359
BeginBorrowInst(SILDebugLocation DebugLoc, SILValue LValue, bool isLexical,
4360-
bool hasPointerEscape)
4360+
bool hasPointerEscape, bool fromVarDecl)
43614361
: UnaryInstructionBase(DebugLoc, LValue,
43624362
LValue->getType().getObjectType()) {
43634363
sharedUInt8().BeginBorrowInst.lexical = isLexical;
43644364
sharedUInt8().BeginBorrowInst.pointerEscape = hasPointerEscape;
4365+
sharedUInt8().BeginBorrowInst.fromVarDecl = fromVarDecl;
43654366
}
43664367

43674368
public:
@@ -4387,6 +4388,10 @@ class BeginBorrowInst
43874388
sharedUInt8().BeginBorrowInst.pointerEscape = pointerEscape;
43884389
}
43894390

4391+
bool isFromVarDecl() const {
4392+
return sharedUInt8().BeginBorrowInst.fromVarDecl;
4393+
}
4394+
43904395
/// Return a range over all EndBorrow instructions for this BeginBorrow.
43914396
EndBorrowRange getEndBorrows() const;
43924397

@@ -8232,10 +8237,11 @@ class MoveValueInst
82328237
USE_SHARED_UINT8;
82338238

82348239
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand, bool isLexical,
8235-
bool hasPointerEscape)
8240+
bool hasPointerEscape, bool fromVarDecl)
82368241
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {
82378242
sharedUInt8().MoveValueInst.lexical = isLexical;
82388243
sharedUInt8().MoveValueInst.pointerEscape = hasPointerEscape;
8244+
sharedUInt8().MoveValueInst.fromVarDecl = fromVarDecl;
82398245
}
82408246

82418247
public:
@@ -8258,6 +8264,8 @@ class MoveValueInst
82588264
void setHasPointerEscape(bool pointerEscape) {
82598265
sharedUInt8().MoveValueInst.pointerEscape = pointerEscape;
82608266
}
8267+
8268+
bool isFromVarDecl() const { return sharedUInt8().MoveValueInst.fromVarDecl; }
82618269
};
82628270

82638271
class DropDeinitInst

include/swift/SIL/SILNode.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ class alignas(8) SILNode :
241241

242242
SHARED_FIELD(BeginBorrowInst, uint8_t
243243
lexical : 1,
244-
pointerEscape : 1);
244+
pointerEscape : 1,
245+
fromVarDecl : 1);
245246

246247
SHARED_FIELD(CopyAddrInst, uint8_t
247248
isTakeOfSrc : 1,
@@ -269,7 +270,8 @@ class alignas(8) SILNode :
269270
SHARED_FIELD(MoveValueInst, uint8_t
270271
allowDiagnostics : 1,
271272
lexical : 1,
272-
pointerEscape : 1);
273+
pointerEscape : 1,
274+
fromVarDecl : 1);
273275

274276
// Do not use `_sharedUInt8_private` outside of SILNode.
275277
} _sharedUInt8_private;

lib/SIL/IR/SILPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
17011701
if (BBI->hasPointerEscape()) {
17021702
*this << "[pointer_escape] ";
17031703
}
1704+
if (BBI->isFromVarDecl()) {
1705+
*this << "[var_decl] ";
1706+
}
17041707
*this << getIDAndType(BBI->getOperand());
17051708
}
17061709

@@ -2074,6 +2077,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
20742077
*this << "[lexical] ";
20752078
if (I->hasPointerEscape())
20762079
*this << "[pointer_escape] ";
2080+
if (I->isFromVarDecl())
2081+
*this << "[var_decl] ";
20772082
*this << getIDAndType(I->getOperand());
20782083
}
20792084

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,6 +3801,7 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38013801
bool allowsDiagnostics = false;
38023802
bool isLexical = false;
38033803
bool hasPointerEscape = false;
3804+
bool fromVarDecl = false;
38043805

38053806
StringRef AttrName;
38063807
SourceLoc AttrLoc;
@@ -3811,6 +3812,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38113812
isLexical = true;
38123813
else if (AttrName == "pointer_escape")
38133814
hasPointerEscape = true;
3815+
else if (AttrName == "var_decl")
3816+
fromVarDecl = true;
38143817
else {
38153818
P.diagnose(InstLoc.getSourceLoc(),
38163819
diag::sil_invalid_attribute_for_instruction, AttrName,
@@ -3823,7 +3826,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
38233826
return true;
38243827
if (parseSILDebugLocation(InstLoc, B))
38253828
return true;
3826-
auto *MVI = B.createMoveValue(InstLoc, Val, isLexical, hasPointerEscape);
3829+
auto *MVI = B.createMoveValue(InstLoc, Val, isLexical, hasPointerEscape,
3830+
fromVarDecl);
38273831
MVI->setAllowsDiagnostics(allowsDiagnostics);
38283832
ResultVal = MVI;
38293833
break;
@@ -4032,6 +4036,7 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
40324036

40334037
bool isLexical = false;
40344038
bool hasPointerEscape = false;
4039+
bool fromVarDecl = false;
40354040

40364041
StringRef AttrName;
40374042
SourceLoc AttrLoc;
@@ -4040,6 +4045,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
40404045
isLexical = true;
40414046
else if (AttrName == "pointer_escape")
40424047
hasPointerEscape = true;
4048+
else if (AttrName == "var_decl")
4049+
fromVarDecl = true;
40434050
else {
40444051
P.diagnose(InstLoc.getSourceLoc(),
40454052
diag::sil_invalid_attribute_for_instruction, AttrName,
@@ -4052,7 +4059,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
40524059
parseSILDebugLocation(InstLoc, B))
40534060
return true;
40544061

4055-
ResultVal = B.createBeginBorrow(InstLoc, Val, isLexical, hasPointerEscape);
4062+
ResultVal = B.createBeginBorrow(InstLoc, Val, isLexical, hasPointerEscape,
4063+
fromVarDecl);
40564064
break;
40574065
}
40584066

lib/Serialization/DeserializeSIL.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,11 +2142,12 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
21422142
assert(RecordKind == SIL_ONE_OPERAND && "Layout should be OneOperand.");
21432143
bool isLexical = Attr & 0x1;
21442144
bool hasPointerEscape = (Attr >> 1) & 0x1;
2145+
bool fromVarDecl = (Attr >> 2) & 0x1;
21452146
ResultInst = Builder.createBeginBorrow(
21462147
Loc,
21472148
getLocalValue(ValID, getSILType(MF->getType(TyID),
21482149
(SILValueCategory)TyCategory, Fn)),
2149-
isLexical, hasPointerEscape);
2150+
isLexical, hasPointerEscape, fromVarDecl);
21502151
break;
21512152
}
21522153

@@ -2233,10 +2234,11 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
22332234
bool AllowsDiagnostics = Attr & 0x1;
22342235
bool IsLexical = (Attr >> 1) & 0x1;
22352236
bool IsEscaping = (Attr >> 2) & 0x1;
2237+
bool IsFromVarDecl = (Attr >> 3) & 0x1;
22362238
auto *MVI = Builder.createMoveValue(
22372239
Loc,
22382240
getLocalValue(ValID, getSILType(Ty, (SILValueCategory)TyCategory, Fn)),
2239-
IsLexical, IsEscaping);
2241+
IsLexical, IsEscaping, IsFromVarDecl);
22402242
MVI->setAllowsDiagnostics(AllowsDiagnostics);
22412243
ResultInst = MVI;
22422244
break;

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 815; // nonisolated(unsafe)
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 816; // begin_borrow/move_value [var_decl]
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

lib/Serialization/SILFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ namespace sil_block {
419419
// SIL instructions with one typed valueref. (dealloc_stack, return)
420420
using SILOneOperandLayout =
421421
BCRecordLayout<SIL_ONE_OPERAND, SILInstOpCodeField,
422-
BCFixed<3>, // Optional attributes
422+
BCFixed<4>, // Optional attributes
423423
TypeIDField, SILTypeCategoryField, ValueIDField>;
424424

425425
using SILOneOperandExtraAttributeLayout = BCRecordLayout<

0 commit comments

Comments
 (0)