Skip to content

Commit 1122050

Browse files
authored
Merge pull request #66187 from meg-gupta/escapeflags
Add pointer_escape flag to move_value/begin_borrow/alloc_box
2 parents c4b6710 + 73c962f commit 1122050

File tree

17 files changed

+283
-137
lines changed

17 files changed

+283
-137
lines changed

include/swift/SIL/SILArgument.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ class SILArgument : public ValueBase {
7676
SILArgument(ValueKind subClassKind, SILBasicBlock *inputParentBlock,
7777
SILType type, ValueOwnershipKind ownershipKind,
7878
const ValueDecl *inputDecl = nullptr, bool reborrow = false,
79-
bool escaping = false);
79+
bool pointerEscape = false);
8080

8181
// A special constructor, only intended for use in
8282
// SILBasicBlock::replacePHIArg and replaceFunctionArg.
8383
explicit SILArgument(ValueKind subClassKind, SILType type,
8484
ValueOwnershipKind ownershipKind,
8585
const ValueDecl *inputDecl = nullptr,
86-
bool reborrow = false, bool escaping = false)
86+
bool reborrow = false, bool pointerEscape = false)
8787
: ValueBase(subClassKind, type), parentBlock(nullptr), decl(inputDecl) {
8888
sharedUInt8().SILArgument.valueOwnershipKind = uint8_t(ownershipKind);
8989
// When the optimizer creates reborrows, reborrow flag needs to be set by
9090
// calling setReborrow.
9191
sharedUInt8().SILArgument.reborrow = false;
92-
sharedUInt8().SILArgument.escaping = false;
92+
sharedUInt8().SILArgument.pointerEscape = false;
9393
}
9494

9595
public:
@@ -119,8 +119,8 @@ class SILArgument : public ValueBase {
119119
return getOwnershipKind() == OwnershipKind::Guaranteed && !isReborrow();
120120
}
121121

122-
bool isEscaping() const {
123-
return ValueOwnershipKind(sharedUInt8().SILArgument.escaping);
122+
bool hasPointerEscape() const {
123+
return ValueOwnershipKind(sharedUInt8().SILArgument.pointerEscape);
124124
}
125125

126126
void setOwnershipKind(ValueOwnershipKind newKind) {
@@ -131,8 +131,8 @@ class SILArgument : public ValueBase {
131131
sharedUInt8().SILArgument.reborrow = isReborrow;
132132
}
133133

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

138138
SILBasicBlock *getParent() const { return parentBlock; }
@@ -260,17 +260,18 @@ class SILPhiArgument : public SILArgument {
260260
SILPhiArgument(SILBasicBlock *parentBlock, SILType type,
261261
ValueOwnershipKind ownershipKind,
262262
const ValueDecl *decl = nullptr, bool isReborrow = false,
263-
bool isEscaping = false)
263+
bool hasPointerEscape = false)
264264
: SILArgument(ValueKind::SILPhiArgument, parentBlock, type, ownershipKind,
265-
decl, isReborrow, isEscaping) {}
265+
decl, isReborrow, hasPointerEscape) {}
266266

267267
// A special constructor, only intended for use in
268268
// SILBasicBlock::replacePHIArg.
269269
explicit SILPhiArgument(SILType type, ValueOwnershipKind ownershipKind,
270270
const ValueDecl *decl = nullptr,
271-
bool isReborrow = false, bool isEscaping = false)
271+
bool isReborrow = false,
272+
bool hasPointerEscape = false)
272273
: SILArgument(ValueKind::SILPhiArgument, type, ownershipKind, decl,
273-
isReborrow, isEscaping) {}
274+
isReborrow, hasPointerEscape) {}
274275

275276
public:
276277
/// Return true if this is block argument is a phi, as opposed to a terminator

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/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ void SILCloner<ImplClass>::clonePhiArgs(SILBasicBlock *oldBB) {
735735
for (auto *Arg : oldBB->getSILPhiArguments()) {
736736
SILValue mappedArg = mappedBB->createPhiArgument(
737737
getOpType(Arg->getType()), Arg->getOwnershipKind(), Arg->getDecl(),
738-
Arg->isReborrow(), Arg->isEscaping());
738+
Arg->isReborrow(), Arg->hasPointerEscape());
739739

740740
asImpl().mapValue(Arg, mappedArg);
741741
}

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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class alignas(8) SILNode :
205205
SHARED_FIELD(SILArgument, uint8_t
206206
valueOwnershipKind : NumVOKindBits,
207207
reborrow : 1,
208-
escaping : 1);
208+
pointerEscape : 1);
209209

210210
SHARED_FIELD(DebugValueInst, uint8_t
211211
poisonRefs : 1,
@@ -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/SILArgument.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ SILArgument::SILArgument(ValueKind subClassKind,
2828
SILBasicBlock *inputParentBlock, SILType type,
2929
ValueOwnershipKind ownershipKind,
3030
const ValueDecl *inputDecl, bool reborrow,
31-
bool escaping)
31+
bool pointerEscape)
3232
: ValueBase(subClassKind, type), parentBlock(inputParentBlock),
3333
decl(inputDecl) {
3434
sharedUInt8().SILArgument.valueOwnershipKind = uint8_t(ownershipKind);
3535
sharedUInt8().SILArgument.reborrow = reborrow;
36-
sharedUInt8().SILArgument.escaping = escaping;
36+
sharedUInt8().SILArgument.pointerEscape = pointerEscape;
3737
inputParentBlock->insertArgument(inputParentBlock->args_end(), this);
3838
}
3939

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 {

0 commit comments

Comments
 (0)