Skip to content

Commit 2efbeb8

Browse files
authored
Merge pull request #60665 from gottesmm/pr-946fc9a839dd236549c02b010887103537c3b833
[builtin] Remove "unsafeGuaranteed" and related code since Unmanaged now has an Ownership SSA based implementation that works completely in SILGen.
2 parents 64cfb11 + 3e52007 commit 2efbeb8

File tree

22 files changed

+2
-1237
lines changed

22 files changed

+2
-1237
lines changed

docs/SIL.rst

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,36 +4862,6 @@ into a ``copy_block`` and a ``is_escaping``/``cond_fail``/``destroy_value`` at
48624862
the end of the lifetime of the objective c closure parameter to check whether
48634863
the sentinel closure was escaped.
48644864

4865-
builtin "unsafeGuaranteed"
4866-
``````````````````````````
4867-
4868-
::
4869-
4870-
sil-instruction := 'builtin' '"unsafeGuaranteed"' '<' sil-type '>' '(' sil-operand')' ':' sil-type
4871-
4872-
%1 = builtin "unsafeGuaranteed"<T>(%0 : $T) : ($T, Builtin.Int1)
4873-
// $T must be of AnyObject type.
4874-
4875-
Asserts that there exists another reference of the value ``%0`` for the scope
4876-
delineated by the call of this builtin up to the first call of a ``builtin
4877-
"unsafeGuaranteedEnd"`` instruction that uses the second element ``%1.1`` of the
4878-
returned value. If no such instruction can be found nothing can be assumed. This
4879-
assertion holds for uses of the first tuple element of the returned value
4880-
``%1.0`` within this scope. The returned reference value equals the input
4881-
``%0``.
4882-
4883-
builtin "unsafeGuaranteedEnd"
4884-
`````````````````````````````
4885-
4886-
::
4887-
4888-
sil-instruction := 'builtin' '"unsafeGuaranteedEnd"' '(' sil-operand')'
4889-
4890-
%1 = builtin "unsafeGuaranteedEnd"(%0 : $Builtin.Int1)
4891-
// $T must be of AnyObject type.
4892-
4893-
Ends the scope for the ``builtin "unsafeGuaranteed"`` instruction.
4894-
48954865
Literals
48964866
~~~~~~~~
48974867

include/swift/AST/Builtins.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,6 @@ BUILTIN_MISC_OPERATION(AssignTakeArray, "assignTakeArray", "", Special)
723723
/// This builtin indicates to the optimizer that the buffer is not mutable.
724724
BUILTIN_MISC_OPERATION(COWBufferForReading, "COWBufferForReading", "n", Special)
725725

726-
// unsafeGuaranteed has type <T: AnyObject> T -> (T, Builtin.Int8)
727-
BUILTIN_MISC_OPERATION(UnsafeGuaranteed, "unsafeGuaranteed", "", Special)
728-
729-
// unsafeGuaranteedEnd has type (Builtin.Int8) -> ()
730-
BUILTIN_MISC_OPERATION(UnsafeGuaranteedEnd, "unsafeGuaranteedEnd", "", Special)
731-
732726
// getObjCTypeEncoding has type <T> T.Type -> RawPointer
733727
BUILTIN_MISC_OPERATION(GetObjCTypeEncoding, "getObjCTypeEncoding", "n", Special)
734728

include/swift/SILOptimizer/Analysis/ARCAnalysis.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -392,35 +392,6 @@ class ConsumedArgToEpilogueReleaseMatcher {
392392
/// Match a call to a trap BB with no ARC relevant side effects.
393393
bool isARCInertTrapBB(const SILBasicBlock *BB);
394394

395-
/// Get the two result values of the builtin "unsafeGuaranteed" instruction.
396-
///
397-
/// Gets the (GuaranteedValue, Token) tuple from a call to "unsafeGuaranteed"
398-
/// if the tuple elements are identified by a single tuple_extract use.
399-
/// Otherwise, returns a (nullptr, nullptr) tuple.
400-
std::pair<SingleValueInstruction *, SingleValueInstruction *>
401-
getSingleUnsafeGuaranteedValueResult(BuiltinInst *UnsafeGuaranteedInst);
402-
403-
/// Get the single builtin "unsafeGuaranteedEnd" user of a builtin
404-
/// "unsafeGuaranteed"'s token.
405-
BuiltinInst *getUnsafeGuaranteedEndUser(SILValue UnsafeGuaranteedToken);
406-
407-
/// Walk forwards from an unsafeGuaranteedEnd builtin instruction looking for a
408-
/// release on the reference returned by the matching unsafeGuaranteed builtin
409-
/// ignoring releases on the way.
410-
/// Return nullptr if no release is found.
411-
///
412-
/// %4 = builtin "unsafeGuaranteed"<Foo>(%0 : $Foo) : $(Foo, Builtin.Int8)
413-
/// %5 = tuple_extract %4 : $(Foo, Builtin.Int8), 0
414-
/// %6 = tuple_extract %4 : $(Foo, Builtin.Int8), 1
415-
/// %12 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
416-
/// strong_release %5 : $Foo // <-- Matching release.
417-
///
418-
/// Alternatively, look for the release before the unsafeGuaranteedEnd.
419-
SILInstruction *findReleaseToMatchUnsafeGuaranteedValue(
420-
SILInstruction *UnsafeGuaranteedEndI, SILInstruction *UnsafeGuaranteedI,
421-
SILValue UnsafeGuaranteedValue, SILBasicBlock &BB,
422-
RCIdentityFunctionInfo &RCFI);
423-
424395
} // end namespace swift
425396

426397
#endif

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ PASS(StringOptimization, "string-optimization",
410410
"Optimization for String operations")
411411
PASS(SwiftArrayPropertyOpt, "array-property-opt",
412412
"Loop Specialization for Array Properties")
413-
PASS(UnsafeGuaranteedPeephole, "unsafe-guaranteed-peephole",
414-
"SIL retain/release Peephole Removal for Builtin.unsafeGuaranteed")
415413
PASS(UsePrespecialized, "use-prespecialized",
416414
"Use Pre-Specialized Functions")
417415
PASS(OwnershipDumper, "ownership-dumper",

lib/AST/Builtins.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,12 +2804,6 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
28042804
case BuiltinValueKind::COWBufferForReading:
28052805
return getCOWBufferForReading(Context, Id);
28062806

2807-
case BuiltinValueKind::UnsafeGuaranteed:
2808-
return getUnsafeGuaranteed(Context, Id);
2809-
2810-
case BuiltinValueKind::UnsafeGuaranteedEnd:
2811-
return getUnsafeGuaranteedEnd(Context, Id);
2812-
28132807
case BuiltinValueKind::ApplyDerivative:
28142808
case BuiltinValueKind::ApplyTranspose:
28152809
llvm_unreachable("Handled above");

lib/IRGen/GenBuiltin.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,6 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
135135
return;
136136
}
137137

138-
if (Builtin.ID == BuiltinValueKind::UnsafeGuaranteedEnd) {
139-
// Just consume the incoming argument.
140-
assert(args.size() == 1 && "Expecting one incoming argument");
141-
(void)args.claimAll();
142-
return;
143-
}
144-
145-
if (Builtin.ID == BuiltinValueKind::UnsafeGuaranteed) {
146-
// Just forward the incoming argument.
147-
assert(args.size() == 1 && "Expecting one incoming argument");
148-
out = std::move(args);
149-
// This is a token.
150-
out.add(llvm::ConstantInt::get(IGF.IGM.Int8Ty, 0));
151-
return;
152-
}
153-
154138
if (Builtin.ID == BuiltinValueKind::OnFastPath) {
155139
// The onFastPath builtin has only an effect on SIL level, so we lower it
156140
// to a no-op.

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,6 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, USubOver)
784784
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, UToSCheckedTrunc)
785785
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, UToUCheckedTrunc)
786786
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Unreachable)
787-
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, UnsafeGuaranteedEnd)
788787
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Xor)
789788
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GenericXor)
790789
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, ZExt)
@@ -806,7 +805,6 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, CreateTaskGroup)
806805
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, DestroyTaskGroup)
807806

808807
BUILTIN_OPERAND_OWNERSHIP(ForwardingConsume, COWBufferForReading)
809-
BUILTIN_OPERAND_OWNERSHIP(ForwardingConsume, UnsafeGuaranteed)
810808

811809
const int PARAMETER_INDEX_CREATE_ASYNC_TASK_FUTURE_FUNCTION = 2;
812810
const int PARAMETER_INDEX_CREATE_ASYNC_TASK_GROUP_FUTURE_FUNCTION = 3;

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ struct ValueOwnershipKindBuiltinVisitor
386386
// This returns a value at +1 that is destroyed strictly /after/ the
387387
// UnsafeGuaranteedEnd. This provides the guarantee that we want.
388388
CONSTANT_OWNERSHIP_BUILTIN(Owned, COWBufferForReading)
389-
CONSTANT_OWNERSHIP_BUILTIN(Owned, UnsafeGuaranteed)
390389
CONSTANT_OWNERSHIP_BUILTIN(None, AShr)
391390
CONSTANT_OWNERSHIP_BUILTIN(None, GenericAShr)
392391
CONSTANT_OWNERSHIP_BUILTIN(None, Add)
@@ -507,7 +506,6 @@ CONSTANT_OWNERSHIP_BUILTIN(None, Unreachable)
507506
CONSTANT_OWNERSHIP_BUILTIN(None, AtomicRMW)
508507

509508
CONSTANT_OWNERSHIP_BUILTIN(None, CondUnreachable)
510-
CONSTANT_OWNERSHIP_BUILTIN(None, UnsafeGuaranteedEnd)
511509
CONSTANT_OWNERSHIP_BUILTIN(None, GetObjCTypeEncoding)
512510
CONSTANT_OWNERSHIP_BUILTIN(None, CanBeObjCClass)
513511
CONSTANT_OWNERSHIP_BUILTIN(None, WillThrow)

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,8 +2484,6 @@ static void visitBuiltinAddress(BuiltinInst *builtin,
24842484
case BuiltinValueKind::Unreachable:
24852485
case BuiltinValueKind::CondUnreachable:
24862486
case BuiltinValueKind::DestroyArray:
2487-
case BuiltinValueKind::UnsafeGuaranteed:
2488-
case BuiltinValueKind::UnsafeGuaranteedEnd:
24892487
case BuiltinValueKind::Swift3ImplicitObjCEntrypoint:
24902488
case BuiltinValueKind::PoundAssert:
24912489
case BuiltinValueKind::IntInstrprofIncrement:

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,127 +1059,3 @@ bool swift::isARCInertTrapBB(const SILBasicBlock *BB) {
10591059
// ARC perspective in an unreachable BB.
10601060
return true;
10611061
}
1062-
1063-
//===----------------------------------------------------------------------===//
1064-
// Analysis of builtin "unsafeGuaranteed" instructions
1065-
//===----------------------------------------------------------------------===//
1066-
std::pair<SingleValueInstruction *, SingleValueInstruction *>
1067-
swift::getSingleUnsafeGuaranteedValueResult(BuiltinInst *BI) {
1068-
assert(BI->getBuiltinKind() &&
1069-
*BI->getBuiltinKind() == BuiltinValueKind::UnsafeGuaranteed &&
1070-
"Expecting a unsafeGuaranteed builtin");
1071-
1072-
SingleValueInstruction *GuaranteedValue = nullptr;
1073-
SingleValueInstruction *Token = nullptr;
1074-
1075-
auto Failed = std::make_pair(nullptr, nullptr);
1076-
1077-
for (auto *Operand : getNonDebugUses(BI)) {
1078-
auto *Usr = Operand->getUser();
1079-
if (isa<ReleaseValueInst>(Usr) || isa<RetainValueInst>(Usr))
1080-
continue;
1081-
1082-
auto *TE = dyn_cast<TupleExtractInst>(Usr);
1083-
if (!TE || TE->getOperand() != BI)
1084-
return Failed;
1085-
1086-
if (TE->getFieldIndex() == 0 && !GuaranteedValue) {
1087-
GuaranteedValue = TE;
1088-
continue;
1089-
}
1090-
if (TE->getFieldIndex() == 1 && !Token) {
1091-
Token = TE;
1092-
continue;
1093-
}
1094-
return Failed;
1095-
}
1096-
1097-
if (!GuaranteedValue || !Token)
1098-
return Failed;
1099-
1100-
return std::make_pair(GuaranteedValue, Token);
1101-
}
1102-
1103-
BuiltinInst *swift::getUnsafeGuaranteedEndUser(SILValue UnsafeGuaranteedToken) {
1104-
BuiltinInst *UnsafeGuaranteedEndI = nullptr;
1105-
1106-
for (auto *Operand : getNonDebugUses(UnsafeGuaranteedToken)) {
1107-
if (UnsafeGuaranteedEndI) {
1108-
LLVM_DEBUG(llvm::dbgs() << " multiple unsafeGuaranteedEnd users\n");
1109-
UnsafeGuaranteedEndI = nullptr;
1110-
break;
1111-
}
1112-
auto *BI = dyn_cast<BuiltinInst>(Operand->getUser());
1113-
if (!BI || !BI->getBuiltinKind() ||
1114-
*BI->getBuiltinKind() != BuiltinValueKind::UnsafeGuaranteedEnd) {
1115-
LLVM_DEBUG(llvm::dbgs() << " wrong unsafeGuaranteed token user "
1116-
<< *Operand->getUser());
1117-
break;
1118-
}
1119-
1120-
UnsafeGuaranteedEndI = BI;
1121-
}
1122-
return UnsafeGuaranteedEndI;
1123-
}
1124-
1125-
static bool hasUnsafeGuaranteedOperand(SILValue UnsafeGuaranteedValue,
1126-
SILValue UnsafeGuaranteedValueOperand,
1127-
RCIdentityFunctionInfo &RCII,
1128-
SILInstruction &Release) {
1129-
assert(isa<StrongReleaseInst>(Release) ||
1130-
isa<ReleaseValueInst>(Release) && "Expecting a release");
1131-
1132-
auto RCRoot = RCII.getRCIdentityRoot(Release.getOperand(0));
1133-
1134-
return RCRoot == UnsafeGuaranteedValue ||
1135-
RCRoot == UnsafeGuaranteedValueOperand;
1136-
}
1137-
1138-
SILInstruction *swift::findReleaseToMatchUnsafeGuaranteedValue(
1139-
SILInstruction *UnsafeGuaranteedEndI, SILInstruction *UnsafeGuaranteedI,
1140-
SILValue UnsafeGuaranteedValue, SILBasicBlock &BB,
1141-
RCIdentityFunctionInfo &RCFI) {
1142-
1143-
auto UnsafeGuaranteedRoot = RCFI.getRCIdentityRoot(UnsafeGuaranteedValue);
1144-
auto UnsafeGuaranteedOpdRoot =
1145-
RCFI.getRCIdentityRoot(UnsafeGuaranteedI->getOperand(0));
1146-
1147-
// Try finding it after the "unsafeGuaranteedEnd".
1148-
for (auto ForwardIt = std::next(UnsafeGuaranteedEndI->getIterator()),
1149-
End = BB.end();
1150-
ForwardIt != End; ++ForwardIt) {
1151-
SILInstruction &CurInst = *ForwardIt;
1152-
1153-
// Is this a release?
1154-
if (isa<ReleaseValueInst>(CurInst) || isa<StrongReleaseInst>(CurInst)) {
1155-
if (hasUnsafeGuaranteedOperand(UnsafeGuaranteedRoot,
1156-
UnsafeGuaranteedOpdRoot, RCFI, CurInst))
1157-
return &CurInst;
1158-
continue;
1159-
}
1160-
1161-
if (CurInst.mayHaveSideEffects() && !DebugValueInst::hasAddrVal(&CurInst))
1162-
break;
1163-
}
1164-
1165-
// Otherwise, Look before the "unsafeGuaranteedEnd".
1166-
for (auto ReverseIt = ++UnsafeGuaranteedEndI->getIterator().getReverse(),
1167-
End = BB.rend();
1168-
ReverseIt != End; ++ReverseIt) {
1169-
SILInstruction &CurInst = *ReverseIt;
1170-
1171-
// Is this a release?
1172-
if (isa<ReleaseValueInst>(CurInst) || isa<StrongReleaseInst>(CurInst)) {
1173-
if (hasUnsafeGuaranteedOperand(UnsafeGuaranteedRoot,
1174-
UnsafeGuaranteedOpdRoot, RCFI, CurInst))
1175-
return &CurInst;
1176-
continue;
1177-
}
1178-
1179-
if (CurInst.mayHaveSideEffects() && !DebugValueInst::hasAddrVal(&CurInst))
1180-
break;
1181-
}
1182-
1183-
return nullptr;
1184-
}
1185-

0 commit comments

Comments
 (0)