Skip to content

Commit 73c962f

Browse files
committed
Add pointer_escape flag to move_value/begin_borrow/alloc_box
1 parent 2c6139d commit 73c962f

File tree

14 files changed

+254
-110
lines changed

14 files changed

+254
-110
lines changed

include/swift/SIL/SILArgument.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class SILArgument : public ValueBase {
131131
sharedUInt8().SILArgument.reborrow = isReborrow;
132132
}
133133

134-
void setEscaping(bool hasPointerEscape) {
134+
void setHasPointerEscape(bool hasPointerEscape) {
135135
sharedUInt8().SILArgument.pointerEscape = hasPointerEscape;
136136
}
137137

include/swift/SIL/SILBuilder.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,12 @@ class SILBuilder {
468468
Optional<SILDebugVariable> Var = None,
469469
bool hasDynamicLifetime = false,
470470
bool reflection = false,
471-
bool usesMoveableValueDebugInfo = false) {
471+
bool usesMoveableValueDebugInfo = false,
472+
bool hasPointerEscape = false) {
472473
return createAllocBox(loc, SILBoxType::get(fieldType.getASTType()), Var,
473474
hasDynamicLifetime, reflection,
474-
usesMoveableValueDebugInfo);
475+
usesMoveableValueDebugInfo,
476+
/*skipVarDeclAssert*/ false, hasPointerEscape);
475477
}
476478

477479
AllocBoxInst *createAllocBox(SILLocation Loc, CanSILBoxType BoxType,
@@ -480,9 +482,10 @@ class SILBuilder {
480482
bool reflection = false,
481483
bool usesMoveableValueDebugInfo = false
482484
#ifndef NDEBUG
483-
, bool skipVarDeclAssert = false
485+
,
486+
bool skipVarDeclAssert = false,
484487
#endif
485-
) {
488+
bool hasPointerEscape = false) {
486489
llvm::SmallString<4> Name;
487490
Loc.markAsPrologue();
488491
assert((skipVarDeclAssert ||
@@ -491,7 +494,7 @@ class SILBuilder {
491494
return insert(AllocBoxInst::create(
492495
getSILDebugLocation(Loc, true), BoxType, *F,
493496
substituteAnonymousArgs(Name, Var, Loc), hasDynamicLifetime, reflection,
494-
usesMoveableValueDebugInfo));
497+
usesMoveableValueDebugInfo, hasPointerEscape));
495498
}
496499

497500
AllocExistentialBoxInst *
@@ -794,11 +797,12 @@ class SILBuilder {
794797
}
795798

796799
BeginBorrowInst *createBeginBorrow(SILLocation Loc, SILValue LV,
797-
bool isLexical = false) {
800+
bool isLexical = false,
801+
bool hasPointerEscape = false) {
798802
assert(getFunction().hasOwnership());
799803
assert(!LV->getType().isAddress());
800-
return insert(new (getModule())
801-
BeginBorrowInst(getSILDebugLocation(Loc), LV, isLexical));
804+
return insert(new (getModule()) BeginBorrowInst(getSILDebugLocation(Loc),
805+
LV, isLexical, hasPointerEscape));
802806
}
803807

804808
/// Convenience function for creating a load_borrow on non-trivial values and
@@ -1340,13 +1344,14 @@ class SILBuilder {
13401344
}
13411345

13421346
MoveValueInst *createMoveValue(SILLocation loc, SILValue operand,
1343-
bool isLexical = false) {
1347+
bool isLexical = false,
1348+
bool hasPointerEscape = false) {
13441349
assert(getFunction().hasOwnership());
13451350
assert(!operand->getType().isTrivial(getFunction()) &&
13461351
"Should not be passing trivial values to this api. Use instead "
13471352
"emitMoveValueOperation");
1348-
return insert(new (getModule()) MoveValueInst(getSILDebugLocation(loc),
1349-
operand, isLexical));
1353+
return insert(new (getModule()) MoveValueInst(
1354+
getSILDebugLocation(loc), operand, isLexical, hasPointerEscape));
13501355
}
13511356

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

include/swift/SIL/SILInstruction.h

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,13 +2461,14 @@ class AllocBoxInst final
24612461
AllocBoxInst(SILDebugLocation DebugLoc, CanSILBoxType BoxType,
24622462
ArrayRef<SILValue> TypeDependentOperands, SILFunction &F,
24632463
Optional<SILDebugVariable> Var, bool hasDynamicLifetime,
2464-
bool reflection = false,
2465-
bool usesMoveableValueDebugInfo = false);
2464+
bool reflection = false, bool usesMoveableValueDebugInfo = false,
2465+
bool hasPointerEscape = false);
24662466

24672467
static AllocBoxInst *create(SILDebugLocation Loc, CanSILBoxType boxType,
24682468
SILFunction &F, Optional<SILDebugVariable> Var,
24692469
bool hasDynamicLifetime, bool reflection = false,
2470-
bool usesMoveableValueDebugInfo = false);
2470+
bool usesMoveableValueDebugInfo = false,
2471+
bool hasPointerEscape = false);
24712472

24722473
public:
24732474
CanSILBoxType getBoxType() const {
@@ -2482,6 +2483,14 @@ class AllocBoxInst final
24822483
return sharedUInt8().AllocBoxInst.dynamicLifetime;
24832484
}
24842485

2486+
void setHasPointerEscape(bool pointerEscape) {
2487+
sharedUInt8().AllocBoxInst.pointerEscape = pointerEscape;
2488+
}
2489+
2490+
bool hasPointerEscape() const {
2491+
return sharedUInt8().AllocBoxInst.pointerEscape;
2492+
}
2493+
24852494
/// True if the box should be emitted with reflection metadata for its
24862495
/// contents.
24872496
bool emitReflectionMetadata() const {
@@ -4421,12 +4430,15 @@ class BeginBorrowInst
44214430
SingleValueInstruction> {
44224431
friend class SILBuilder;
44234432

4424-
bool lexical;
4433+
USE_SHARED_UINT8;
44254434

4426-
BeginBorrowInst(SILDebugLocation DebugLoc, SILValue LValue, bool isLexical)
4435+
BeginBorrowInst(SILDebugLocation DebugLoc, SILValue LValue, bool isLexical,
4436+
bool hasPointerEscape)
44274437
: UnaryInstructionBase(DebugLoc, LValue,
4428-
LValue->getType().getObjectType()),
4429-
lexical(isLexical) {}
4438+
LValue->getType().getObjectType()) {
4439+
sharedUInt8().BeginBorrowInst.lexical = isLexical;
4440+
sharedUInt8().BeginBorrowInst.pointerEscape = hasPointerEscape;
4441+
}
44304442

44314443
public:
44324444
// FIXME: this does not return all instructions that end a local borrow
@@ -4438,11 +4450,18 @@ class BeginBorrowInst
44384450

44394451
/// Whether the borrow scope introduced by this instruction corresponds to a
44404452
/// source-level lexical scope.
4441-
bool isLexical() const { return lexical; }
4453+
bool isLexical() const { return sharedUInt8().BeginBorrowInst.lexical; }
44424454

44434455
/// If this is a lexical borrow, eliminate the lexical bit. If this borrow
44444456
/// doesn't have a lexical bit, do not do anything.
4445-
void removeIsLexical() { lexical = false; }
4457+
void removeIsLexical() { sharedUInt8().BeginBorrowInst.lexical = false; }
4458+
4459+
bool hasPointerEscape() const {
4460+
return sharedUInt8().BeginBorrowInst.pointerEscape;
4461+
}
4462+
void setHasPointerEscape(bool pointerEscape) {
4463+
sharedUInt8().BeginBorrowInst.pointerEscape = pointerEscape;
4464+
}
44464465

44474466
/// Return a range over all EndBorrow instructions for this BeginBorrow.
44484467
EndBorrowRange getEndBorrows() const;
@@ -8260,22 +8279,35 @@ class MoveValueInst
82608279
SingleValueInstruction> {
82618280
friend class SILBuilder;
82628281

8282+
USE_SHARED_UINT8;
8283+
8284+
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand, bool isLexical,
8285+
bool hasPointerEscape)
8286+
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {
8287+
sharedUInt8().MoveValueInst.lexical = isLexical;
8288+
sharedUInt8().MoveValueInst.pointerEscape = hasPointerEscape;
8289+
}
8290+
8291+
public:
82638292
/// If set to true, we should emit the kill diagnostic for this move_value. If
82648293
/// set to false, we shouldn't emit such a diagnostic. This is a short term
82658294
/// addition until we get MoveOnly wrapper types into the SIL type system.
8266-
bool allowDiagnostics = false;
8267-
bool lexical = false;
8268-
8269-
MoveValueInst(SILDebugLocation DebugLoc, SILValue operand, bool isLexical)
8270-
: UnaryInstructionBase(DebugLoc, operand, operand->getType()),
8271-
lexical(isLexical) {}
8295+
bool getAllowDiagnostics() const {
8296+
return sharedUInt8().MoveValueInst.allowDiagnostics;
8297+
}
8298+
void setAllowsDiagnostics(bool newValue) {
8299+
sharedUInt8().MoveValueInst.allowDiagnostics = newValue;
8300+
}
82728301

8273-
public:
8274-
bool getAllowDiagnostics() const { return allowDiagnostics; }
8275-
void setAllowsDiagnostics(bool newValue) { allowDiagnostics = newValue; }
8302+
bool isLexical() const { return sharedUInt8().MoveValueInst.lexical; }
8303+
void removeIsLexical() { sharedUInt8().MoveValueInst.lexical = false; }
82768304

8277-
bool isLexical() const { return lexical; };
8278-
void removeIsLexical() { lexical = false; }
8305+
bool hasPointerEscape() const {
8306+
return sharedUInt8().MoveValueInst.pointerEscape;
8307+
}
8308+
void setHasPointerEscape(bool pointerEscape) {
8309+
sharedUInt8().MoveValueInst.pointerEscape = pointerEscape;
8310+
}
82798311
};
82808312

82818313
class DropDeinitInst

include/swift/SIL/SILNode.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,18 @@ class alignas(8) SILNode :
221221
SHARED_FIELD(AllocBoxInst, uint8_t
222222
dynamicLifetime : 1,
223223
reflection : 1,
224-
usesMoveableValueDebugInfo : 1);
224+
usesMoveableValueDebugInfo : 1,
225+
pointerEscape : 1);
225226

226227
SHARED_FIELD(AllocRefInstBase, uint8_t
227228
objC : 1,
228229
onStack : 1,
229230
numTailTypes: NumAllocRefTailTypesBits);
230231

232+
SHARED_FIELD(BeginBorrowInst, uint8_t
233+
lexical : 1,
234+
pointerEscape : 1);
235+
231236
SHARED_FIELD(CopyAddrInst, uint8_t
232237
isTakeOfSrc : 1,
233238
isInitializationOfDest : 1);
@@ -251,6 +256,11 @@ class alignas(8) SILNode :
251256
aborting : 1,
252257
fromBuiltin : 1);
253258

259+
SHARED_FIELD(MoveValueInst, uint8_t
260+
allowDiagnostics : 1,
261+
lexical : 1,
262+
pointerEscape : 1);
263+
254264
// Do not use `_sharedUInt8_private` outside of SILNode.
255265
} _sharedUInt8_private;
256266
// clang-format on

lib/SIL/IR/SILInstructions.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ AllocBoxInst::AllocBoxInst(SILDebugLocation Loc, CanSILBoxType BoxType,
376376
ArrayRef<SILValue> TypeDependentOperands,
377377
SILFunction &F, Optional<SILDebugVariable> Var,
378378
bool hasDynamicLifetime, bool reflection,
379-
bool usesMoveableValueDebugInfo)
379+
bool usesMoveableValueDebugInfo,
380+
bool hasPointerEscape)
380381
: NullaryInstructionWithTypeDependentOperandsBase(
381382
Loc, TypeDependentOperands, SILType::getPrimitiveObjectType(BoxType)),
382383
VarInfo(Var, getTrailingObjects<char>()) {
@@ -389,21 +390,24 @@ AllocBoxInst::AllocBoxInst(SILDebugLocation Loc, CanSILBoxType BoxType,
389390

390391
sharedUInt8().AllocBoxInst.usesMoveableValueDebugInfo =
391392
usesMoveableValueDebugInfo;
393+
394+
sharedUInt8().AllocBoxInst.pointerEscape = hasPointerEscape;
392395
}
393396

394397
AllocBoxInst *AllocBoxInst::create(SILDebugLocation Loc, CanSILBoxType BoxType,
395398
SILFunction &F,
396399
Optional<SILDebugVariable> Var,
397400
bool hasDynamicLifetime, bool reflection,
398-
bool usesMoveableValueDebugInfo) {
401+
bool usesMoveableValueDebugInfo,
402+
bool hasPointerEscape) {
399403
SmallVector<SILValue, 8> TypeDependentOperands;
400404
collectTypeDependentOperands(TypeDependentOperands, F, BoxType);
401405
auto Sz = totalSizeToAlloc<swift::Operand, char>(TypeDependentOperands.size(),
402406
Var ? Var->Name.size() : 0);
403407
auto Buf = F.getModule().allocateInst(Sz, alignof(AllocBoxInst));
404-
return ::new (Buf)
405-
AllocBoxInst(Loc, BoxType, TypeDependentOperands, F, Var,
406-
hasDynamicLifetime, reflection, usesMoveableValueDebugInfo);
408+
return ::new (Buf) AllocBoxInst(Loc, BoxType, TypeDependentOperands, F, Var,
409+
hasDynamicLifetime, reflection,
410+
usesMoveableValueDebugInfo, hasPointerEscape);
407411
}
408412

409413
SILType AllocBoxInst::getAddressType() const {

lib/SIL/IR/SILPrinter.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14571457
*this << "[reflection] ";
14581458
}
14591459

1460+
if (ABI->hasPointerEscape()) {
1461+
*this << "[pointer_escape] ";
1462+
}
1463+
14601464
if (ABI->getUsesMoveableValueDebugInfo() &&
14611465
!ABI->getAddressType().isMoveOnly()) {
14621466
*this << "[moveable_value_debuginfo] ";
@@ -1673,11 +1677,14 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
16731677
*this << getIDAndType(LBI->getOperand());
16741678
}
16751679

1676-
void visitBeginBorrowInst(BeginBorrowInst *LBI) {
1677-
if (LBI->isLexical()) {
1680+
void visitBeginBorrowInst(BeginBorrowInst *BBI) {
1681+
if (BBI->isLexical()) {
16781682
*this << "[lexical] ";
16791683
}
1680-
*this << getIDAndType(LBI->getOperand());
1684+
if (BBI->hasPointerEscape()) {
1685+
*this << "[pointer_escape] ";
1686+
}
1687+
*this << getIDAndType(BBI->getOperand());
16811688
}
16821689

16831690
void printStoreOwnershipQualifier(StoreOwnershipQualifier Qualifier) {
@@ -2015,6 +2022,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
20152022
*this << "[allows_diagnostics] ";
20162023
if (I->isLexical())
20172024
*this << "[lexical] ";
2025+
if (I->hasPointerEscape())
2026+
*this << "[pointer_escape] ";
20182027
*this << getIDAndType(I->getOperand());
20192028
}
20202029

0 commit comments

Comments
 (0)