Skip to content

Commit 434a210

Browse files
authored
Merge pull request swiftlang#71360 from gottesmm/finished-all-insts
[region-isolation] Finish adding support for the rest of the SIL instructions
2 parents b67ace7 + 4bed671 commit 434a210

File tree

8 files changed

+764
-60
lines changed

8 files changed

+764
-60
lines changed

docs/SIL.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5151,7 +5151,7 @@ end_unpaired_access
51515151
sil-enforcement ::= static
51525152
sil-enforcement ::= dynamic
51535153
sil-enforcement ::= unsafe
5154-
%1 = end_unpaired_access [dynamic] %0 : $*Builtin.UnsafeValueBuffer
5154+
end_unpaired_access [dynamic] %0 : $*Builtin.UnsafeValueBuffer
51555155

51565156
Ends an access. This has the same semantics and constraints as ``end_access`` with the following exceptions:
51575157

include/swift/SIL/AddressWalker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ TransitiveAddressWalker<Impl>::walk(SILValue projectedAddress) && {
200200
isa<RetainValueAddrInst>(user) || isa<ReleaseValueAddrInst>(user) ||
201201
isa<PackElementSetInst>(user) || isa<PackElementGetInst>(user) ||
202202
isa<DeinitExistentialAddrInst>(user) || isa<LoadBorrowInst>(user) ||
203-
isa<TupleAddrConstructorInst>(user)) {
203+
isa<TupleAddrConstructorInst>(user) || isa<DeallocPackInst>(user)) {
204204
callVisitUse(op);
205205
continue;
206206
}

include/swift/SILOptimizer/Analysis/RegionAnalysis.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ inline bool isNonSendableType(SILType type, SILFunction *fn) {
4141
return true;
4242
}
4343

44+
// Treat Builtin.SILToken as Sendable. It cannot escape from the current
45+
// function. We should change isSendable to hardwire this.
46+
if (type.getASTType()->is<SILTokenType>()) {
47+
return false;
48+
}
49+
4450
// Otherwise, delegate to seeing if type conforms to the Sendable protocol.
4551
return !type.isSendable(fn);
4652
}

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16271627
requireAddressType(SILPackType, AI->getType(),
16281628
"result of alloc_pack must be an address of "
16291629
"lowered pack type");
1630+
checkAddressWalkerCanVisitAllTransitiveUses(AI);
16301631
}
16311632

16321633
void checkAllocRefBase(AllocRefInstBase *ARI) {

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

Lines changed: 138 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,13 @@ static bool isStaticallyLookThroughInst(SILInstruction *inst) {
196196
case SILInstructionKind::CopyableToMoveOnlyWrapperValueInst:
197197
case SILInstructionKind::DestructureStructInst:
198198
case SILInstructionKind::DestructureTupleInst:
199+
case SILInstructionKind::DifferentiableFunctionExtractInst:
199200
case SILInstructionKind::DropDeinitInst:
200201
case SILInstructionKind::EndCOWMutationInst:
201202
case SILInstructionKind::EndInitLetRefInst:
202203
case SILInstructionKind::ExplicitCopyValueInst:
203204
case SILInstructionKind::InitEnumDataAddrInst:
205+
case SILInstructionKind::LinearFunctionExtractInst:
204206
case SILInstructionKind::MarkDependenceInst:
205207
case SILInstructionKind::MarkUninitializedInst:
206208
case SILInstructionKind::MarkUnresolvedNonCopyableValueInst:
@@ -210,6 +212,7 @@ static bool isStaticallyLookThroughInst(SILInstruction *inst) {
210212
case SILInstructionKind::MoveOnlyWrapperToCopyableValueInst:
211213
case SILInstructionKind::MoveValueInst:
212214
case SILInstructionKind::OpenExistentialAddrInst:
215+
case SILInstructionKind::OpenExistentialValueInst:
213216
case SILInstructionKind::ProjectBlockStorageInst:
214217
case SILInstructionKind::ProjectBoxInst:
215218
case SILInstructionKind::RefToBridgeObjectInst:
@@ -220,6 +223,12 @@ static bool isStaticallyLookThroughInst(SILInstruction *inst) {
220223
case SILInstructionKind::UnownedToRefInst:
221224
case SILInstructionKind::UpcastInst:
222225
case SILInstructionKind::ValueToBridgeObjectInst:
226+
case SILInstructionKind::WeakCopyValueInst:
227+
case SILInstructionKind::StrongCopyWeakValueInst:
228+
case SILInstructionKind::StrongCopyUnmanagedValueInst:
229+
case SILInstructionKind::RefToUnmanagedInst:
230+
case SILInstructionKind::UnmanagedToRefInst:
231+
case SILInstructionKind::InitExistentialValueInst:
223232
return true;
224233
case SILInstructionKind::UnconditionalCheckedCastInst: {
225234
auto cast = SILDynamicCastInst::getAs(inst);
@@ -1157,9 +1166,12 @@ enum class TranslationSemantics {
11571166
/// handle every instruction to ensure we cover the IR.
11581167
Asserting,
11591168

1160-
/// An instruction that we do not handle yet. Just for now during bring
1161-
/// up. Will be removed.
1162-
Unhandled,
1169+
/// An instruction that the checker thinks it can ignore as long as all of its
1170+
/// operands are Sendable. If we see that such an instruction has a
1171+
/// non-Sendable parameter, then someone added an instruction to the compiler
1172+
/// without updating this code correctly. This is most likely driver error and
1173+
/// should be caught in testing when we assert.
1174+
AssertingIfNonSendable,
11631175
};
11641176

11651177
} // namespace
@@ -1199,8 +1211,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
11991211
case TranslationSemantics::Asserting:
12001212
os << "asserting";
12011213
return os;
1202-
case TranslationSemantics::Unhandled:
1203-
os << "unhandled";
1214+
case TranslationSemantics::AssertingIfNonSendable:
1215+
os << "asserting_if_nonsendable";
12041216
return os;
12051217
}
12061218

@@ -2101,8 +2113,15 @@ class PartitionOpTranslator {
21012113
"transfer-non-sendable: Found banned instruction?!");
21022114
return;
21032115

2104-
case TranslationSemantics::Unhandled:
2105-
LLVM_DEBUG(llvm::dbgs() << "Unhandled inst: " << *inst);
2116+
case TranslationSemantics::AssertingIfNonSendable:
2117+
// Do not error if all of our operands are sendable.
2118+
if (llvm::none_of(inst->getOperandValues(), [&](SILValue value) {
2119+
return ::isNonSendableType(value->getType(), inst->getFunction());
2120+
}))
2121+
return;
2122+
llvm::report_fatal_error(
2123+
"transfer-non-sendable: Found instruction that is not allowed to "
2124+
"have non-Sendable parameters with such parameters?!");
21062125
return;
21072126
}
21082127

@@ -2170,8 +2189,11 @@ void PartitionOpBuilder::print(llvm::raw_ostream &os) const {
21702189
assert(trackableValue);
21712190
llvm::dbgs() << "State: %%" << opArg << ". ";
21722191
trackableValue->getValueState().print(llvm::dbgs());
2173-
llvm::dbgs() << "\n Value: "
2192+
llvm::dbgs() << "\n Rep Value: "
21742193
<< trackableValue->getRepresentative();
2194+
if (auto value = trackableValue->getRepresentative().maybeGetValue()) {
2195+
llvm::dbgs() << " Type: " << value->getType() << '\n';
2196+
}
21752197
}
21762198
#endif
21772199
}
@@ -2218,6 +2240,9 @@ CONSTANT_TRANSLATION(WitnessMethodInst, AssignFresh)
22182240
CONSTANT_TRANSLATION(IntegerLiteralInst, AssignFresh)
22192241
CONSTANT_TRANSLATION(FloatLiteralInst, AssignFresh)
22202242
CONSTANT_TRANSLATION(StringLiteralInst, AssignFresh)
2243+
// Metatypes are Sendable, but AnyObject isn't
2244+
CONSTANT_TRANSLATION(ObjCMetatypeToObjectInst, AssignFresh)
2245+
CONSTANT_TRANSLATION(ObjCExistentialMetatypeToObjectInst, AssignFresh)
22212246

22222247
//===---
22232248
// Assign
@@ -2236,6 +2261,7 @@ CONSTANT_TRANSLATION(ClassMethodInst, Assign)
22362261
CONSTANT_TRANSLATION(ObjCMethodInst, Assign)
22372262
CONSTANT_TRANSLATION(SuperMethodInst, Assign)
22382263
CONSTANT_TRANSLATION(ObjCSuperMethodInst, Assign)
2264+
CONSTANT_TRANSLATION(LoadUnownedInst, Assign)
22392265

22402266
// These instructions are in between look through and a true assign. We should
22412267
// probably eventually treat them as look through but we haven't done the work
@@ -2253,14 +2279,12 @@ CONSTANT_TRANSLATION(InitExistentialAddrInst, Assign)
22532279
CONSTANT_TRANSLATION(InitExistentialRefInst, Assign)
22542280
CONSTANT_TRANSLATION(OpenExistentialBoxInst, Assign)
22552281
CONSTANT_TRANSLATION(OpenExistentialRefInst, Assign)
2256-
CONSTANT_TRANSLATION(RefToUnmanagedInst, Assign)
22572282
CONSTANT_TRANSLATION(TailAddrInst, Assign)
22582283
CONSTANT_TRANSLATION(ThickToObjCMetatypeInst, Assign)
22592284
CONSTANT_TRANSLATION(ThinToThickFunctionInst, Assign)
22602285
CONSTANT_TRANSLATION(UncheckedAddrCastInst, Assign)
22612286
CONSTANT_TRANSLATION(UncheckedEnumDataInst, Assign)
22622287
CONSTANT_TRANSLATION(UncheckedOwnershipConversionInst, Assign)
2263-
CONSTANT_TRANSLATION(UnmanagedToRefInst, Assign)
22642288
CONSTANT_TRANSLATION(IndexRawPointerInst, Assign)
22652289

22662290
// These are used by SIL to aggregate values together in a gep like way. We
@@ -2311,6 +2335,13 @@ CONSTANT_TRANSLATION(UnownedCopyValueInst, LookThrough)
23112335
CONSTANT_TRANSLATION(DropDeinitInst, LookThrough)
23122336
CONSTANT_TRANSLATION(ValueToBridgeObjectInst, LookThrough)
23132337
CONSTANT_TRANSLATION(BeginCOWMutationInst, LookThrough)
2338+
CONSTANT_TRANSLATION(OpenExistentialValueInst, LookThrough)
2339+
CONSTANT_TRANSLATION(WeakCopyValueInst, LookThrough)
2340+
CONSTANT_TRANSLATION(StrongCopyWeakValueInst, LookThrough)
2341+
CONSTANT_TRANSLATION(StrongCopyUnmanagedValueInst, LookThrough)
2342+
CONSTANT_TRANSLATION(RefToUnmanagedInst, LookThrough)
2343+
CONSTANT_TRANSLATION(UnmanagedToRefInst, LookThrough)
2344+
CONSTANT_TRANSLATION(InitExistentialValueInst, LookThrough)
23142345

23152346
//===---
23162347
// Store
@@ -2352,6 +2383,7 @@ CONSTANT_TRANSLATION(DestroyValueInst, Ignored)
23522383
CONSTANT_TRANSLATION(EndAccessInst, Ignored)
23532384
CONSTANT_TRANSLATION(EndBorrowInst, Ignored)
23542385
CONSTANT_TRANSLATION(EndLifetimeInst, Ignored)
2386+
CONSTANT_TRANSLATION(EndUnpairedAccessInst, Ignored)
23552387
CONSTANT_TRANSLATION(HopToExecutorInst, Ignored)
23562388
CONSTANT_TRANSLATION(InjectEnumAddrInst, Ignored)
23572389
CONSTANT_TRANSLATION(IsEscapingClosureInst, Ignored)
@@ -2371,6 +2403,26 @@ CONSTANT_TRANSLATION(FixLifetimeInst, Require)
23712403
CONSTANT_TRANSLATION(ClassifyBridgeObjectInst, Require)
23722404
CONSTANT_TRANSLATION(BridgeObjectToWordInst, Require)
23732405
CONSTANT_TRANSLATION(IsUniqueInst, Require)
2406+
CONSTANT_TRANSLATION(MarkFunctionEscapeInst, Require)
2407+
CONSTANT_TRANSLATION(UnmanagedRetainValueInst, Require)
2408+
CONSTANT_TRANSLATION(UnmanagedReleaseValueInst, Require)
2409+
CONSTANT_TRANSLATION(UnmanagedAutoreleaseValueInst, Require)
2410+
CONSTANT_TRANSLATION(RebindMemoryInst, Require)
2411+
CONSTANT_TRANSLATION(BindMemoryInst, Require)
2412+
CONSTANT_TRANSLATION(BeginUnpairedAccessInst, Require)
2413+
// Require of the value we extract the metatype from.
2414+
CONSTANT_TRANSLATION(ValueMetatypeInst, Require)
2415+
// Require of the value we extract the metatype from.
2416+
CONSTANT_TRANSLATION(ExistentialMetatypeInst, Require)
2417+
2418+
//===---
2419+
// Asserting If Non Sendable Parameter
2420+
//
2421+
2422+
// Takes metatypes as parameters and metatypes today are always sendable.
2423+
CONSTANT_TRANSLATION(InitExistentialMetatypeInst, AssertingIfNonSendable)
2424+
CONSTANT_TRANSLATION(OpenExistentialMetatypeInst, AssertingIfNonSendable)
2425+
CONSTANT_TRANSLATION(ObjCToThickMetatypeInst, AssertingIfNonSendable)
23742426

23752427
//===---
23762428
// Terminators
@@ -2383,6 +2435,8 @@ CONSTANT_TRANSLATION(CondFailInst, Ignored)
23832435
CONSTANT_TRANSLATION(SwitchValueInst, Ignored)
23842436
CONSTANT_TRANSLATION(UnreachableInst, Ignored)
23852437
CONSTANT_TRANSLATION(UnwindInst, Ignored)
2438+
// Doesn't take a parameter.
2439+
CONSTANT_TRANSLATION(ThrowAddrInst, Ignored)
23862440

23872441
// Terminators that only need require.
23882442
CONSTANT_TRANSLATION(ReturnInst, Require)
@@ -2396,6 +2450,13 @@ CONSTANT_TRANSLATION(CondBranchInst, TerminatorPhi)
23962450
CONSTANT_TRANSLATION(CheckedCastBranchInst, TerminatorPhi)
23972451
CONSTANT_TRANSLATION(DynamicMethodBranchInst, TerminatorPhi)
23982452

2453+
// Today, await_async_continuation just takes Sendable values
2454+
// (UnsafeContinuation and UnsafeThrowingContinuation).
2455+
CONSTANT_TRANSLATION(AwaitAsyncContinuationInst, AssertingIfNonSendable)
2456+
CONSTANT_TRANSLATION(GetAsyncContinuationInst, AssertingIfNonSendable)
2457+
CONSTANT_TRANSLATION(GetAsyncContinuationAddrInst, AssertingIfNonSendable)
2458+
CONSTANT_TRANSLATION(ExtractExecutorInst, AssertingIfNonSendable)
2459+
23992460
//===---
24002461
// Existential Box
24012462
//
@@ -2409,54 +2470,25 @@ CONSTANT_TRANSLATION(OpenExistentialBoxValueInst, Assign)
24092470
CONSTANT_TRANSLATION(DeallocExistentialBoxInst, Ignored)
24102471

24112472
//===---
2412-
// Unhandled Instructions
2473+
// Differentiable
2474+
//
2475+
2476+
CONSTANT_TRANSLATION(DifferentiabilityWitnessFunctionInst, AssignFresh)
2477+
CONSTANT_TRANSLATION(DifferentiableFunctionExtractInst, LookThrough)
2478+
CONSTANT_TRANSLATION(LinearFunctionExtractInst, LookThrough)
2479+
CONSTANT_TRANSLATION(LinearFunctionInst, Assign)
2480+
CONSTANT_TRANSLATION(DifferentiableFunctionInst, Assign)
2481+
2482+
//===---
2483+
// Packs
24132484
//
24142485

2415-
CONSTANT_TRANSLATION(ObjCToThickMetatypeInst, Unhandled)
2416-
CONSTANT_TRANSLATION(ObjCMetatypeToObjectInst, Unhandled)
2417-
CONSTANT_TRANSLATION(ObjCExistentialMetatypeToObjectInst, Unhandled)
2418-
CONSTANT_TRANSLATION(WeakCopyValueInst, Unhandled)
2419-
CONSTANT_TRANSLATION(StrongCopyWeakValueInst, Unhandled)
2420-
CONSTANT_TRANSLATION(StrongCopyUnmanagedValueInst, Unhandled)
2421-
CONSTANT_TRANSLATION(LoadUnownedInst, Unhandled)
2422-
CONSTANT_TRANSLATION(ValueMetatypeInst, Unhandled)
2423-
CONSTANT_TRANSLATION(ExistentialMetatypeInst, Unhandled)
2424-
CONSTANT_TRANSLATION(VectorInst, Unhandled)
2425-
CONSTANT_TRANSLATION(TuplePackElementAddrInst, Unhandled)
2426-
CONSTANT_TRANSLATION(TuplePackExtractInst, Unhandled)
2427-
CONSTANT_TRANSLATION(PackElementGetInst, Unhandled)
2428-
CONSTANT_TRANSLATION(InitExistentialValueInst, Unhandled)
2429-
CONSTANT_TRANSLATION(InitExistentialMetatypeInst, Unhandled)
2430-
CONSTANT_TRANSLATION(OpenExistentialMetatypeInst, Unhandled)
2431-
CONSTANT_TRANSLATION(OpenExistentialValueInst, Unhandled)
2432-
CONSTANT_TRANSLATION(OpenPackElementInst, Unhandled)
2433-
CONSTANT_TRANSLATION(PackLengthInst, Unhandled)
2434-
CONSTANT_TRANSLATION(DynamicPackIndexInst, Unhandled)
2435-
CONSTANT_TRANSLATION(PackPackIndexInst, Unhandled)
2436-
CONSTANT_TRANSLATION(ScalarPackIndexInst, Unhandled)
2437-
CONSTANT_TRANSLATION(DifferentiableFunctionInst, Unhandled)
2438-
CONSTANT_TRANSLATION(LinearFunctionInst, Unhandled)
2439-
CONSTANT_TRANSLATION(DifferentiableFunctionExtractInst, Unhandled)
2440-
CONSTANT_TRANSLATION(LinearFunctionExtractInst, Unhandled)
2441-
CONSTANT_TRANSLATION(DifferentiabilityWitnessFunctionInst, Unhandled)
2442-
CONSTANT_TRANSLATION(GetAsyncContinuationInst, Unhandled)
2443-
CONSTANT_TRANSLATION(GetAsyncContinuationAddrInst, Unhandled)
2444-
CONSTANT_TRANSLATION(ExtractExecutorInst, Unhandled)
2445-
CONSTANT_TRANSLATION(BindMemoryInst, Unhandled)
2446-
CONSTANT_TRANSLATION(RebindMemoryInst, Unhandled)
2447-
CONSTANT_TRANSLATION(ThrowAddrInst, Unhandled)
2448-
CONSTANT_TRANSLATION(AwaitAsyncContinuationInst, Unhandled)
2449-
CONSTANT_TRANSLATION(DeallocPackInst, Unhandled)
2450-
CONSTANT_TRANSLATION(UnmanagedRetainValueInst, Unhandled)
2451-
CONSTANT_TRANSLATION(UnmanagedReleaseValueInst, Unhandled)
2452-
CONSTANT_TRANSLATION(UnmanagedAutoreleaseValueInst, Unhandled)
2453-
CONSTANT_TRANSLATION(BeginUnpairedAccessInst, Unhandled)
2454-
CONSTANT_TRANSLATION(EndUnpairedAccessInst, Unhandled)
2455-
CONSTANT_TRANSLATION(AssignInst, Unhandled)
2456-
CONSTANT_TRANSLATION(AssignByWrapperInst, Unhandled)
2457-
CONSTANT_TRANSLATION(AssignOrInitInst, Unhandled)
2458-
CONSTANT_TRANSLATION(MarkFunctionEscapeInst, Unhandled)
2459-
CONSTANT_TRANSLATION(PackElementSetInst, Unhandled)
2486+
CONSTANT_TRANSLATION(DeallocPackInst, Ignored)
2487+
CONSTANT_TRANSLATION(DynamicPackIndexInst, Ignored)
2488+
CONSTANT_TRANSLATION(OpenPackElementInst, Ignored)
2489+
CONSTANT_TRANSLATION(PackLengthInst, Ignored)
2490+
CONSTANT_TRANSLATION(PackPackIndexInst, Ignored)
2491+
CONSTANT_TRANSLATION(ScalarPackIndexInst, Ignored)
24602492

24612493
//===---
24622494
// Apply
@@ -2488,6 +2520,16 @@ CONSTANT_TRANSLATION(UnownedRetainInst, Asserting)
24882520
CONSTANT_TRANSLATION(AllocPackMetadataInst, Asserting)
24892521
CONSTANT_TRANSLATION(DeallocPackMetadataInst, Asserting)
24902522

2523+
// All of these instructions should be removed by DI which runs before us in the
2524+
// pass pipeline.
2525+
CONSTANT_TRANSLATION(AssignInst, Asserting)
2526+
CONSTANT_TRANSLATION(AssignByWrapperInst, Asserting)
2527+
CONSTANT_TRANSLATION(AssignOrInitInst, Asserting)
2528+
2529+
// We should never hit this since it can only appear as a final instruction in a
2530+
// global variable static initializer list.
2531+
CONSTANT_TRANSLATION(VectorInst, Asserting)
2532+
24912533
#undef CONSTANT_TRANSLATION
24922534

24932535
#ifdef LOOKTHROUGH_IF_NONSENDABLE_RESULT_REQUIRE_OTHERWISE
@@ -2572,6 +2614,46 @@ CAST_WITH_MAYBE_SENDABLE_NONSENDABLE_OP_AND_RESULT(UncheckedValueCastInst)
25722614
// Custom Handling
25732615
//
25742616

2617+
TranslationSemantics
2618+
PartitionOpTranslator::visitPackElementGetInst(PackElementGetInst *r) {
2619+
if (!isNonSendableType(r->getType()))
2620+
return TranslationSemantics::Require;
2621+
translateSILAssign(SILValue(r), r->getPack());
2622+
return TranslationSemantics::Special;
2623+
}
2624+
2625+
TranslationSemantics PartitionOpTranslator::visitTuplePackElementAddrInst(
2626+
TuplePackElementAddrInst *r) {
2627+
if (!isNonSendableType(r->getType())) {
2628+
translateSILRequire(r->getTuple());
2629+
} else {
2630+
translateSILAssign(SILValue(r), r->getTuple());
2631+
}
2632+
return TranslationSemantics::Special;
2633+
}
2634+
2635+
TranslationSemantics
2636+
PartitionOpTranslator::visitTuplePackExtractInst(TuplePackExtractInst *r) {
2637+
if (!isNonSendableType(r->getType())) {
2638+
translateSILRequire(r->getTuple());
2639+
} else {
2640+
translateSILAssign(SILValue(r), r->getTuple());
2641+
}
2642+
return TranslationSemantics::Special;
2643+
}
2644+
2645+
TranslationSemantics
2646+
PartitionOpTranslator::visitPackElementSetInst(PackElementSetInst *r) {
2647+
// If the value we are storing is sendable, treat this as a require.
2648+
if (!isNonSendableType(r->getValue()->getType())) {
2649+
return TranslationSemantics::Require;
2650+
}
2651+
2652+
// Otherwise, this is a store.
2653+
translateSILStore(r->getPackOperand(), r->getValueOperand());
2654+
return TranslationSemantics::Special;
2655+
}
2656+
25752657
TranslationSemantics
25762658
PartitionOpTranslator::visitRawPointerToRefInst(RawPointerToRefInst *r) {
25772659
assert(isLookThroughIfResultNonSendable(r) && "Out of sync");

0 commit comments

Comments
 (0)