Skip to content

Commit 12ea649

Browse files
Merge pull request #21637 from aschwaighofer/remove_escaped_on_convert_escape_to_noescape
SIL: Remove isEscapedByUser flag on convert_escape_to_noescape instruction
2 parents 95e0c66 + cb0c53a commit 12ea649

18 files changed

+25
-70
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,10 @@ class SILBuilder {
927927

928928
ConvertEscapeToNoEscapeInst *
929929
createConvertEscapeToNoEscape(SILLocation Loc, SILValue Op, SILType Ty,
930-
bool isEscapedByUser, bool lifetimeGuaranteed) {
930+
bool lifetimeGuaranteed) {
931931
return insert(ConvertEscapeToNoEscapeInst::create(
932932
getSILDebugLocation(Loc), Op, Ty, getFunction(), C.OpenedArchetypes,
933-
isEscapedByUser, lifetimeGuaranteed));
933+
lifetimeGuaranteed));
934934
}
935935

936936
ThinFunctionToPointerInst *

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,7 @@ void SILCloner<ImplClass>::visitConvertEscapeToNoEscapeInst(
12671267
recordClonedInstruction(
12681268
Inst, getBuilder().createConvertEscapeToNoEscape(
12691269
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
1270-
getOpType(Inst->getType()), Inst->isEscapedByUser(),
1271-
Inst->isLifetimeGuaranteed()));
1270+
getOpType(Inst->getType()), Inst->isLifetimeGuaranteed()));
12721271
}
12731272

12741273
template<typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,36 +4037,26 @@ class ConvertEscapeToNoEscapeInst final
40374037
friend SILBuilder;
40384038

40394039
bool lifetimeGuaranteed;
4040-
bool isEscaped; // Even if we can analyze this
4041-
// instruction the user might have
4042-
// escaped it.
40434040

40444041
ConvertEscapeToNoEscapeInst(SILDebugLocation DebugLoc, SILValue Operand,
40454042
ArrayRef<SILValue> TypeDependentOperands,
4046-
SILType Ty, bool isEscapedByUser,
4043+
SILType Ty,
40474044
bool isLifetimeGuaranteed)
40484045
: UnaryInstructionWithTypeDependentOperandsBase(
40494046
DebugLoc, Operand, TypeDependentOperands, Ty),
4050-
lifetimeGuaranteed(isLifetimeGuaranteed),
4051-
isEscaped(isEscapedByUser) {
4047+
lifetimeGuaranteed(isLifetimeGuaranteed) {
40524048
assert(!Operand->getType().castTo<SILFunctionType>()->isNoEscape());
40534049
assert(Ty.castTo<SILFunctionType>()->isNoEscape());
40544050
}
40554051

40564052
static ConvertEscapeToNoEscapeInst *
40574053
create(SILDebugLocation DebugLoc, SILValue Operand, SILType Ty,
40584054
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes,
4059-
bool isEscapedByUser, bool lifetimeGuaranteed);
4055+
bool lifetimeGuaranteed);
40604056
public:
40614057
bool isLifetimeGuaranteed() const {
40624058
return lifetimeGuaranteed;
40634059
}
4064-
4065-
bool isEscapedByUser() const {
4066-
return isEscaped;
4067-
}
4068-
4069-
void setEscapedByUser(bool isEscaped = true) { this->isEscaped = isEscaped; }
40704060
};
40714061

40724062
/// ThinFunctionToPointerInst - Convert a thin function pointer to a

lib/IRGen/LoadableByAddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ void LoadableByAddress::recreateConvInstrs() {
26322632
auto instr = cast<ConvertEscapeToNoEscapeInst>(convInstr);
26332633
newInstr = convBuilder.createConvertEscapeToNoEscape(
26342634
instr->getLoc(), instr->getOperand(), newType,
2635-
instr->isEscapedByUser(), instr->isLifetimeGuaranteed());
2635+
instr->isLifetimeGuaranteed());
26362636
break;
26372637
}
26382638
case SILInstructionKind::MarkDependenceInst: {

lib/ParseSIL/ParseSIL.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,20 +3120,15 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
31203120
Identifier ToToken;
31213121
SourceLoc ToLoc;
31223122
bool not_guaranteed = false;
3123-
bool escaped = false;
31243123
bool without_actually_escaping = false;
31253124
if (Opcode == SILInstructionKind::ConvertEscapeToNoEscapeInst) {
31263125
StringRef attrName;
31273126
if (parseSILOptional(attrName, *this)) {
3128-
if (attrName.equals("escaped"))
3129-
escaped = true;
3130-
else if (attrName.equals("not_guaranteed"))
3127+
if (attrName.equals("not_guaranteed"))
31313128
not_guaranteed = true;
31323129
else
31333130
return true;
31343131
}
3135-
if (parseSILOptional(escaped, *this, "escaped"))
3136-
return true;
31373132
}
31383133
if (parseTypedValueRef(Val, B)
31393134
|| parseSILIdentifier(ToToken, ToLoc, diag::expected_tok_in_sil_instr,
@@ -3178,8 +3173,8 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
31783173
B.createConvertFunction(InstLoc, Val, Ty, without_actually_escaping);
31793174
break;
31803175
case SILInstructionKind::ConvertEscapeToNoEscapeInst:
3181-
ResultVal = B.createConvertEscapeToNoEscape(InstLoc, Val, Ty, escaped,
3182-
!not_guaranteed);
3176+
ResultVal =
3177+
B.createConvertEscapeToNoEscape(InstLoc, Val, Ty, !not_guaranteed);
31833178
break;
31843179
case SILInstructionKind::AddressToPointerInst:
31853180
ResultVal = B.createAddressToPointer(InstLoc, Val, Ty);

lib/SIL/SILInstructions.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,18 +1993,16 @@ ConvertFunctionInst *ConvertFunctionInst::create(
19931993

19941994
ConvertEscapeToNoEscapeInst *ConvertEscapeToNoEscapeInst::create(
19951995
SILDebugLocation DebugLoc, SILValue Operand, SILType Ty, SILFunction &F,
1996-
SILOpenedArchetypesState &OpenedArchetypes, bool isEscapedByUser,
1997-
bool isLifetimeGuaranteed) {
1996+
SILOpenedArchetypesState &OpenedArchetypes, bool isLifetimeGuaranteed) {
19981997
SILModule &Mod = F.getModule();
19991998
SmallVector<SILValue, 8> TypeDependentOperands;
20001999
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, F,
20012000
Ty.getASTType());
20022001
unsigned size =
20032002
totalSizeToAlloc<swift::Operand>(1 + TypeDependentOperands.size());
20042003
void *Buffer = Mod.allocateInst(size, alignof(ConvertEscapeToNoEscapeInst));
2005-
auto *CFI = ::new (Buffer)
2006-
ConvertEscapeToNoEscapeInst(DebugLoc, Operand, TypeDependentOperands, Ty,
2007-
isEscapedByUser, isLifetimeGuaranteed);
2004+
auto *CFI = ::new (Buffer) ConvertEscapeToNoEscapeInst(
2005+
DebugLoc, Operand, TypeDependentOperands, Ty, isLifetimeGuaranteed);
20082006
// If we do not have lowered SIL, make sure that are not performing
20092007
// ABI-incompatible conversions.
20102008
//

lib/SIL/SILPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,6 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14141414
}
14151415
void visitConvertEscapeToNoEscapeInst(ConvertEscapeToNoEscapeInst *CI) {
14161416
*this << (CI->isLifetimeGuaranteed() ? "" : "[not_guaranteed] ")
1417-
<< (CI->isEscapedByUser() ? "[escaped] " : "")
14181417
<< getIDAndType(CI->getOperand()) << " to " << CI->getType();
14191418
}
14201419
void visitThinFunctionToPointerInst(ThinFunctionToPointerInst *CI) {

lib/SIL/SILVerifier.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,9 +3683,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
36833683
// '[not_guaranteed]' or '[escaped]' attributes.
36843684
if (!SkipConvertEscapeToNoescapeAttributes &&
36853685
F.getModule().getStage() != SILStage::Raw) {
3686-
require(!ICI->isEscapedByUser(),
3687-
"convert_escape_to_noescape [escaped] not "
3688-
"allowed after mandatory passes");
36893686
require(ICI->isLifetimeGuaranteed(),
36903687
"convert_escape_to_noescape [not_guaranteed] not "
36913688
"allowed after mandatory passes");

lib/SILGen/SILGenBridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
973973
// Handle the escaping to noescape conversion.
974974
assert(loweredFuncTy->isNoEscape());
975975
return B.createConvertEscapeToNoEscape(
976-
loc, thunkedFn, SILType::getPrimitiveObjectType(loweredFuncTy), false);
976+
loc, thunkedFn, SILType::getPrimitiveObjectType(loweredFuncTy));
977977
}
978978

979979
static ManagedValue emitCBridgedToNativeValue(SILGenFunction &SGF,

lib/SILGen/SILGenBuilder.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ SILGenBuilder::createConvertFunction(SILLocation loc, ManagedValue fn,
218218
}
219219

220220
ManagedValue SILGenBuilder::createConvertEscapeToNoEscape(
221-
SILLocation loc, ManagedValue fn, SILType resultTy,
222-
bool isEscapedByUser) {
221+
SILLocation loc, ManagedValue fn, SILType resultTy) {
223222

224223
auto fnType = fn.getType().castTo<SILFunctionType>();
225224
auto resultFnType = resultTy.castTo<SILFunctionType>();
@@ -234,8 +233,8 @@ ManagedValue SILGenBuilder::createConvertEscapeToNoEscape(
234233
(void)fnType;
235234
(void)resultFnType;
236235
SILValue fnValue = fn.getValue();
237-
SILValue result = createConvertEscapeToNoEscape(
238-
loc, fnValue, resultTy, isEscapedByUser, false);
236+
SILValue result =
237+
createConvertEscapeToNoEscape(loc, fnValue, resultTy, false);
239238
return ManagedValue::forTrivialObjectRValue(result);
240239
}
241240

lib/SILGen/SILGenBuilder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ class SILGenBuilder : public SILBuilder {
379379
using SILBuilder::createConvertEscapeToNoEscape;
380380
ManagedValue
381381
createConvertEscapeToNoEscape(SILLocation loc, ManagedValue fn,
382-
SILType resultTy,
383-
bool isEscapedByUser);
382+
SILType resultTy);
384383

385384
using SILBuilder::createStore;
386385
/// Forward \p value into \p address.

lib/SILGen/SILGenPoly.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,7 +3181,7 @@ static ManagedValue createThunk(SILGenFunction &SGF,
31813181
// Handle the escaping to noescape conversion.
31823182
assert(expectedType->isNoEscape());
31833183
return SGF.B.createConvertEscapeToNoEscape(
3184-
loc, thunkedFn, SILType::getPrimitiveObjectType(expectedType), false);
3184+
loc, thunkedFn, SILType::getPrimitiveObjectType(expectedType));
31853185
}
31863186

31873187
static CanSILFunctionType buildWithoutActuallyEscapingThunkType(
@@ -3355,7 +3355,7 @@ ManagedValue Transform::transformFunction(ManagedValue fn,
33553355
} else if (newFnType != expectedFnType) {
33563356
// Escaping to noescape conversion.
33573357
SILType resTy = SILType::getPrimitiveObjectType(expectedFnType);
3358-
fn = SGF.B.createConvertEscapeToNoEscape(Loc, fn, resTy, false);
3358+
fn = SGF.B.createConvertEscapeToNoEscape(Loc, fn, resTy);
33593359
}
33603360

33613361
return fn;

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ SILValue ClosureSpecCloner::cloneCalleeConversion(
708708
calleeValue = cloneCalleeConversion(Cvt->getOperand(), NewClosure, Builder,
709709
NeedsRelease);
710710
return Builder.createConvertEscapeToNoEscape(
711-
CallSiteDesc.getLoc(), calleeValue, Cvt->getType(), false, true);
711+
CallSiteDesc.getLoc(), calleeValue, Cvt->getType(), true);
712712
}
713713

714714
/// Populate the body of the cloned closure, modifying instructions as

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &Fn,
9898

9999
SILBuilderWithScope B(Cvt);
100100
auto NewCvt = B.createConvertEscapeToNoEscape(
101-
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), false, true);
101+
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
102102
Cvt->replaceAllUsesWith(NewCvt);
103103
Cvt->eraseFromParent();
104104
Cvt = NewCvt;
@@ -189,11 +189,6 @@ static SILInstruction *lookThroughRebastractionUsers(
189189
static bool tryExtendLifetimeToLastUse(
190190
ConvertEscapeToNoEscapeInst *Cvt,
191191
llvm::DenseMap<SILInstruction *, SILInstruction *> &Memoized) {
192-
// Don't optimize converts that might have been escaped by the function call
193-
// (materializeForSet 'escapes' its arguments into the writeback buffer).
194-
if (Cvt->isEscapedByUser())
195-
return false;
196-
197192
// If there is a single user that is an apply this is simple: extend the
198193
// lifetime of the operand until after the apply.
199194
auto SingleUser = lookThroughRebastractionUsers(Cvt, Memoized);
@@ -215,7 +210,7 @@ static bool tryExtendLifetimeToLastUse(
215210
{
216211
SILBuilderWithScope B(Cvt);
217212
auto NewCvt = B.createConvertEscapeToNoEscape(
218-
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), false, true);
213+
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
219214
Cvt->replaceAllUsesWith(NewCvt);
220215
Cvt->eraseFromParent();
221216
Cvt = NewCvt;
@@ -264,11 +259,6 @@ static bool tryExtendLifetimeToLastUse(
264259
/// diamonds. And a destroy of %closure at the last destroy of
265260
/// %convertOptionalBlock.
266261
static bool trySwitchEnumPeephole(ConvertEscapeToNoEscapeInst *Cvt) {
267-
// Don't optimize converts that might have been escaped by the function call
268-
// (materializeForSet 'escapes' its arguments into the writeback buffer).
269-
if (Cvt->isEscapedByUser())
270-
return false;
271-
272262
auto *blockArg = dyn_cast<SILArgument>(Cvt->getOperand());
273263
if (!blockArg)
274264
return false;
@@ -314,7 +304,7 @@ static bool trySwitchEnumPeephole(ConvertEscapeToNoEscapeInst *Cvt) {
314304
{
315305
SILBuilderWithScope B(Cvt);
316306
auto NewCvt = B.createConvertEscapeToNoEscape(
317-
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), false, true);
307+
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
318308
Cvt->replaceAllUsesWith(NewCvt);
319309
Cvt->eraseFromParent();
320310
}

lib/Serialization/DeserializeSIL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,11 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
11801180
assert(RecordKind == SIL_ONE_TYPE_ONE_OPERAND &&
11811181
"Layout should be OneTypeOneOperand.");
11821182
bool isLifetimeGuaranteed = Attr & 0x01;
1183-
bool isEscaped = Attr & 0x02;
11841183
ResultVal = Builder.createConvertEscapeToNoEscape(
11851184
Loc,
11861185
getLocalValue(ValID, getSILType(MF->getType(TyID2),
11871186
(SILValueCategory)TyCategory2)),
1188-
getSILType(MF->getType(TyID), (SILValueCategory)TyCategory), isEscaped,
1187+
getSILType(MF->getType(TyID), (SILValueCategory)TyCategory),
11891188
isLifetimeGuaranteed);
11901189
break;
11911190
}

lib/Serialization/SerializeSIL.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,8 +1489,6 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
14891489
if (SI.getKind() == SILInstructionKind::ConvertEscapeToNoEscapeInst) {
14901490
if (cast<ConvertEscapeToNoEscapeInst>(SI).isLifetimeGuaranteed())
14911491
attrs |= 0x01;
1492-
if (cast<ConvertEscapeToNoEscapeInst>(SI).isEscapedByUser())
1493-
attrs |= 0x02;
14941492
}
14951493
if (SI.getKind() == SILInstructionKind::ConvertFunctionInst) {
14961494
if (cast<ConvertFunctionInst>(SI).withoutActuallyEscaping())

test/SIL/Parser/basic.sil

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,15 +1344,11 @@ entry(%0 : $@convention(thin) (AnyObject) -> Optional<AnyObject>):
13441344
// CHECK-LABEL: sil @convert_function_trivial
13451345
// CHECK: %1 = convert_escape_to_noescape %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
13461346
// CHECK: %2 = convert_escape_to_noescape [not_guaranteed] %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
1347-
// CHECK: %3 = convert_escape_to_noescape [escaped] %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
1348-
// CHECK: %4 = convert_escape_to_noescape [not_guaranteed] [escaped] %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
13491347
// CHECK: return
13501348
sil @convert_function_trivial : $@convention(thin) (@owned @callee_guaranteed (AnyObject) -> Optional<AnyObject>) -> () {
13511349
entry(%0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject>):
13521350
%1 = convert_escape_to_noescape %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
13531351
%2 = convert_escape_to_noescape [not_guaranteed] %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
1354-
%3 = convert_escape_to_noescape [escaped] %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
1355-
%4 = convert_escape_to_noescape [not_guaranteed] [escaped] %0 : $@callee_guaranteed (AnyObject) -> Optional<AnyObject> to $@noescape @callee_guaranteed (Optional<AnyObject>) -> AnyObject
13561352
return undef : $()
13571353
}
13581354

test/Serialization/sil_partial_apply_ownership.sil

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ sil @use : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
4848
// CHECK-LABEL: sil @partial_apply_convert
4949
// CHECK: convert_escape_to_noescape %{{.*}} : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
5050
// CHECK: convert_escape_to_noescape [not_guaranteed] %
51-
// CHECK: convert_escape_to_noescape [escaped] %
52-
// CHECK: convert_escape_to_noescape [not_guaranteed] [escaped] %
5351
sil @partial_apply_convert : $@convention(thin) () -> () {
5452
entry:
5553
%f = function_ref @subject : $@convention(thin) (Builtin.Int64) -> ()
5654
%z = integer_literal $Builtin.Int64, 0
5755
%e = partial_apply [callee_guaranteed] %f(%z) : $@convention(thin) (Builtin.Int64) -> ()
5856
%n = convert_escape_to_noescape %e : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
5957
%x2 = convert_escape_to_noescape [not_guaranteed] %e : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
60-
%x3 = convert_escape_to_noescape [escaped] %e : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
61-
%x4 = convert_escape_to_noescape [not_guaranteed] [escaped] %e : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
6258
%n2 = mark_dependence %n : $@noescape @callee_guaranteed () -> () on %e : $@callee_guaranteed () -> ()
6359
%f2 = function_ref @use : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
6460
apply %f2(%n2) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()

0 commit comments

Comments
 (0)