Skip to content

Commit 8178eb0

Browse files
authored
Merge pull request #70514 from gottesmm/pr-763529d8ee9f4164b82d03087fec25439cc1d315
[region-isolation] Fix the dataflow and add support for project_block_storage
2 parents dfe3d2a + e7f6377 commit 8178eb0

20 files changed

+443
-110
lines changed

docs/SIL.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,6 +3844,8 @@ Notionally, ``alloc_pack_metadata`` is a stack allocation instruction. See the
38443844
section above on stack discipline. The corresponding stack deallocation
38453845
instruction is ``dealloc_pack_metadata``.
38463846

3847+
Only valid in Lowered SIL.
3848+
38473849
alloc_ref
38483850
`````````
38493851
::
@@ -4124,6 +4126,8 @@ instruction after its operand) must be cleaned up here.
41244126
on Stack Discipline above. The operand must be an ``alloc_pack_metadata``
41254127
instruction.
41264128

4129+
Only valid in Lowered SIL.
4130+
41274131
dealloc_box
41284132
```````````
41294133
::

include/swift/SIL/MemAccessUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,9 @@ inline bool isAccessStorageTypeCast(SingleValueInstruction *svi) {
15911591
switch (svi->getKind()) {
15921592
default:
15931593
return false;
1594+
// This extracts out the block storage from an alloc_stack. We do not want
1595+
// to treat it as any more than a cast of the underlying value.
1596+
case SILInstructionKind::ProjectBlockStorageInst:
15941597
// Simply pass-thru the incoming address. But change its type!
15951598
case SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst:
15961599
case SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst:

include/swift/SIL/Projection.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ enum class ProjectionKind : unsigned {
9494
RefCast = 1,
9595
BitwiseCast = 2,
9696
TailElems = 3,
97+
BlockStorageCast = 4,
9798
FirstPointerKind = Upcast,
98-
LastPointerKind = TailElems,
99+
LastPointerKind = BlockStorageCast,
99100

100101
// Index Projection Kinds
101102
FirstIndexKind = 7,
@@ -120,6 +121,7 @@ static inline bool isCastProjectionKind(ProjectionKind Kind) {
120121
case ProjectionKind::Upcast:
121122
case ProjectionKind::RefCast:
122123
case ProjectionKind::BitwiseCast:
124+
case ProjectionKind::BlockStorageCast:
123125
return true;
124126
case ProjectionKind::Struct:
125127
case ProjectionKind::Tuple:
@@ -318,15 +320,19 @@ class Projection {
318320
auto *Ty = getPointer();
319321
assert(Ty->isCanonical());
320322
switch (getKind()) {
321-
case ProjectionKind::Upcast:
322-
case ProjectionKind::RefCast:
323-
case ProjectionKind::BitwiseCast:
324-
return SILType::getPrimitiveType(Ty->getCanonicalType(),
325-
BaseType.getCategory());
326-
case ProjectionKind::TailElems:
327-
return SILType::getPrimitiveAddressType(Ty->getCanonicalType());
328-
default:
329-
llvm_unreachable("unknown cast projection type");
323+
case ProjectionKind::Upcast:
324+
case ProjectionKind::RefCast:
325+
case ProjectionKind::BitwiseCast:
326+
return SILType::getPrimitiveType(Ty->getCanonicalType(),
327+
BaseType.getCategory());
328+
case ProjectionKind::TailElems:
329+
return SILType::getPrimitiveAddressType(Ty->getCanonicalType());
330+
case ProjectionKind::BlockStorageCast: {
331+
auto blockStorageTy = Ty->getCanonicalType()->castTo<SILBlockStorageType>();
332+
return blockStorageTy->getCaptureAddressType();
333+
}
334+
default:
335+
llvm_unreachable("unknown cast projection type");
330336
}
331337
}
332338

@@ -421,6 +427,7 @@ class Projection {
421427
case ProjectionKind::BitwiseCast:
422428
return true;
423429
case ProjectionKind::Upcast:
430+
case ProjectionKind::BlockStorageCast:
424431
case ProjectionKind::Struct:
425432
case ProjectionKind::Tuple:
426433
case ProjectionKind::Index:
@@ -445,6 +452,7 @@ class Projection {
445452
case ProjectionKind::RefCast:
446453
case ProjectionKind::Tuple:
447454
case ProjectionKind::Upcast:
455+
case ProjectionKind::BlockStorageCast:
448456
case ProjectionKind::Box:
449457
case ProjectionKind::TailElems:
450458
return false;

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ class Partition {
577577
fstReduced.elementToRegionMap.find(Element(sndRegionNumber));
578578
if (iter != fstReduced.elementToRegionMap.end()) {
579579
fstReduced.elementToRegionMap.insert({sndEltNumber, iter->second});
580-
if (fstReduced.fresh_label < Region(sndEltNumber))
580+
// We want fresh_label to always be one element larger than our
581+
// maximum element.
582+
if (fstReduced.fresh_label <= Region(sndEltNumber))
581583
fstReduced.fresh_label = Region(sndEltNumber + 1);
582584
continue;
583585
}
@@ -597,15 +599,10 @@ class Partition {
597599
fstIter.first->getSecond() =
598600
fstIter.first->second->merge(sndIter->second);
599601
}
600-
if (fstReduced.fresh_label < sndRegionNumber)
602+
if (fstReduced.fresh_label <= sndRegionNumber)
601603
fstReduced.fresh_label = Region(sndEltNumber + 1);
602604
}
603605

604-
LLVM_DEBUG(llvm::dbgs() << "JOIN PEFORMED: \nFST: ";
605-
fst.print(llvm::dbgs()); llvm::dbgs() << "SND: ";
606-
snd.print(llvm::dbgs()); llvm::dbgs() << "RESULT: ";
607-
fstReduced.print(llvm::dbgs()););
608-
609606
assert(fstReduced.is_canonical_correct());
610607

611608
// fst_reduced is now the join

lib/SIL/Utils/Projection.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ Projection::Projection(SingleValueInstruction *I) : Value() {
154154
}
155155
break;
156156
}
157+
case SILInstructionKind::ProjectBlockStorageInst: {
158+
auto *Ty = I->getType().getASTType().getPointer();
159+
assert(Ty->isCanonical());
160+
Value = ValueTy(ProjectionKind::BlockStorageCast, Ty);
161+
assert(getKind() == ProjectionKind::BlockStorageCast);
162+
break;
163+
}
157164
case SILInstructionKind::UpcastInst: {
158165
auto *Ty = I->getType().getASTType().getPointer();
159166
assert(Ty->isCanonical());
@@ -199,6 +206,7 @@ SILType Projection::getType(SILType BaseType, SILModule &M,
199206
case ProjectionKind::Tuple:
200207
return BaseType.getTupleElementType(getIndex());
201208
case ProjectionKind::Upcast:
209+
case ProjectionKind::BlockStorageCast:
202210
case ProjectionKind::RefCast:
203211
case ProjectionKind::BitwiseCast:
204212
case ProjectionKind::TailElems:
@@ -238,6 +246,8 @@ Projection::createObjectProjection(SILBuilder &B, SILLocation Loc,
238246
return nullptr;
239247
case ProjectionKind::Box:
240248
return nullptr;
249+
case ProjectionKind::BlockStorageCast:
250+
return nullptr;
241251
case ProjectionKind::Upcast:
242252
return B.createUpcast(Loc, Base, getCastType(BaseTy));
243253
case ProjectionKind::RefCast:
@@ -285,6 +295,8 @@ Projection::createAddressProjection(SILBuilder &B, SILLocation Loc,
285295
return B.createRefTailAddr(Loc, Base, getCastType(BaseTy));
286296
case ProjectionKind::Box:
287297
return B.createProjectBox(Loc, Base, getIndex());
298+
case ProjectionKind::BlockStorageCast:
299+
return B.createProjectBlockStorage(Loc, Base);
288300
case ProjectionKind::Upcast:
289301
return B.createUpcast(Loc, Base, getCastType(BaseTy));
290302
case ProjectionKind::RefCast:
@@ -600,6 +612,10 @@ void Projection::print(raw_ostream &os, SILType baseType) const {
600612
os << " Box over";
601613
break;
602614
}
615+
case ProjectionKind::BlockStorageCast: {
616+
os << "BlockStorageCast";
617+
break;
618+
}
603619
case ProjectionKind::Upcast: {
604620
os << "UpCast";
605621
break;
@@ -872,6 +888,7 @@ SILValue Projection::getOperandForAggregate(SILInstruction *I) const {
872888
case ProjectionKind::Class:
873889
case ProjectionKind::TailElems:
874890
case ProjectionKind::Box:
891+
case ProjectionKind::BlockStorageCast:
875892
case ProjectionKind::Upcast:
876893
case ProjectionKind::RefCast:
877894
case ProjectionKind::BitwiseCast:
@@ -931,6 +948,7 @@ static bool isSupportedProjection(const Projection &p) {
931948
case ProjectionKind::Enum:
932949
case ProjectionKind::Box:
933950
case ProjectionKind::Upcast:
951+
case ProjectionKind::BlockStorageCast:
934952
case ProjectionKind::RefCast:
935953
case ProjectionKind::BitwiseCast:
936954
case ProjectionKind::TailElems:

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6273,13 +6273,17 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
62736273
require(apmi->getIntroducer()->mayRequirePackMetadata(),
62746274
"Introduces instruction of kind which cannot emit on-stack pack "
62756275
"metadata");
6276+
require(F.getModule().getStage() == SILStage::Lowered,
6277+
"Only supported in lowered SIL");
62766278
}
62776279

62786280
void checkDeallocPackMetadataInst(DeallocPackMetadataInst *dpmi) {
62796281
auto *apmi = dpmi->getOperand()->getDefiningInstruction();
62806282
require(apmi, "Must have instruction operand.");
62816283
require(isa<AllocPackMetadataInst>(apmi),
62826284
"Must have alloc_pack_metadata operand");
6285+
require(F.getModule().getStage() == SILStage::Lowered,
6286+
"Only supported in lowered SIL");
62836287
}
62846288

62856289
void checkMoveOnlyWrapperToCopyableAddrInst(

0 commit comments

Comments
 (0)