Skip to content

Commit 13bb6c6

Browse files
committed
SIL: Remove AllocBoxInst::getElementType().
There's no longer a single element type to speak of. Update uses to either iterate all box fields or to assert that they're working with a single-field box.
1 parent c46eb4c commit 13bb6c6

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,9 @@ class AllocBoxInst final
882882
Operands[i].~Operand();
883883
}
884884
}
885-
886-
SILType getElementType() const {
887-
return SILType::getPrimitiveObjectType(
888-
getType().castTo<SILBoxType>()->getBoxedType());
885+
886+
CanSILBoxType getBoxType() const {
887+
return getType().castTo<SILBoxType>();
889888
}
890889

891890
/// Return the underlying variable declaration associated with this

lib/IRGen/IRGenSIL.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,7 +3646,9 @@ void IRGenSILFunction::visitDeallocBoxInst(swift::DeallocBoxInst *i) {
36463646
}
36473647

36483648
void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
3649-
const TypeInfo &type = getTypeInfo(i->getElementType());
3649+
assert(i->getBoxType()->getLayout()->getFields().size() == 1
3650+
&& "multi field boxes not implemented yet");
3651+
const TypeInfo &type = getTypeInfo(i->getBoxType()->getFieldType(0));
36503652

36513653
// Derive name from SIL location.
36523654
VarDecl *Decl = i->getDecl();
@@ -3676,7 +3678,10 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
36763678
if (Name == IGM.Context.Id_self.str())
36773679
return;
36783680

3679-
DebugTypeInfo DbgTy(Decl, i->getElementType().getSwiftType(), type, false);
3681+
assert(i->getBoxType()->getLayout()->getFields().size() == 1
3682+
&& "box for a local variable should only have one field");
3683+
DebugTypeInfo DbgTy(Decl, i->getBoxType()->getFieldType(0).getSwiftType(),
3684+
type, false);
36803685
IGM.DebugInfo->emitVariableDeclaration(
36813686
Builder,
36823687
emitShadowCopy(boxWithAddr.getAddress(), i->getDebugScope(), Name, 0),

lib/SIL/SILVerifier.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
17281728

17291729
require(AI->getType().isObject(),
17301730
"result of alloc_box must be an object");
1731-
verifyOpenedArchetype(AI, AI->getElementType().getSwiftRValueType());
1731+
for (unsigned field : indices(AI->getBoxType()->getLayout()->getFields()))
1732+
verifyOpenedArchetype(AI, AI->getBoxType()->getFieldLoweredType(field));
17321733
}
17331734

17341735
void checkDeallocBoxInst(DeallocBoxInst *DI) {

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ static unsigned getElementCountRec(CanType T,
5757
DIMemoryObjectInfo::DIMemoryObjectInfo(SILInstruction *MI) {
5858
MemoryInst = MI;
5959
// Compute the type of the memory object.
60-
if (auto *ABI = dyn_cast<AllocBoxInst>(MemoryInst))
61-
MemorySILType = ABI->getElementType();
62-
else if (auto *ASI = dyn_cast<AllocStackInst>(MemoryInst))
60+
if (auto *ABI = dyn_cast<AllocBoxInst>(MemoryInst)) {
61+
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
62+
&& "analyzing multi-field boxes not implemented");
63+
MemorySILType = ABI->getBoxType()->getFieldType(0);
64+
} else if (auto *ASI = dyn_cast<AllocStackInst>(MemoryInst)) {
6365
MemorySILType = ASI->getElementType();
64-
else {
66+
} else {
6567
auto *MUI = cast<MarkUninitializedInst>(MemoryInst);
6668
MemorySILType = MUI->getType().getObjectType();
6769

@@ -562,13 +564,15 @@ void ElementUseCollector::collectContainerUses(AllocBoxInst *ABI) {
562564
if (isa<StrongReleaseInst>(User))
563565
continue;
564566

565-
if (isa<ProjectBoxInst>(User)) {
566-
collectUses(User, 0);
567+
if (auto project = dyn_cast<ProjectBoxInst>(User)) {
568+
collectUses(User, project->getFieldIndex());
567569
continue;
568570
}
569571

570-
// Other uses of the container are considered escapes of the value.
571-
addElementUses(0, ABI->getElementType(), User, DIUseKind::Escape);
572+
// Other uses of the container are considered escapes of the values.
573+
for (unsigned field : indices(ABI->getBoxType()->getLayout()->getFields()))
574+
addElementUses(field, ABI->getBoxType()->getFieldType(field),
575+
User, DIUseKind::Escape);
572576
}
573577
}
574578

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,11 @@ AllocOptimize::AllocOptimize(SILInstruction *TheMemory,
247247
Releases(Releases) {
248248

249249
// Compute the type of the memory object.
250-
if (auto *ABI = dyn_cast<AllocBoxInst>(TheMemory))
251-
MemoryType = ABI->getElementType();
252-
else {
250+
if (auto *ABI = dyn_cast<AllocBoxInst>(TheMemory)) {
251+
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
252+
&& "optimizing multi-field boxes not implemented");
253+
MemoryType = ABI->getBoxType()->getFieldType(0);
254+
} else {
253255
assert(isa<AllocStackInst>(TheMemory));
254256
MemoryType = cast<AllocStackInst>(TheMemory)->getElementType();
255257
}

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,10 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI,
406406
auto &Entry = ABI->getFunction()->front();
407407
SILBuilder BuildAlloc(&Entry, Entry.begin());
408408
BuildAlloc.setCurrentDebugScope(ABI->getDebugScope());
409-
auto *ASI = BuildAlloc.createAllocStack(ABI->getLoc(), ABI->getElementType(),
409+
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
410+
&& "rewriting multi-field box not implemented");
411+
auto *ASI = BuildAlloc.createAllocStack(ABI->getLoc(),
412+
ABI->getBoxType()->getFieldType(0),
410413
ABI->getVarInfo());
411414

412415
// Replace all uses of the address of the box's contained value with
@@ -429,7 +432,10 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI,
429432
break;
430433
}
431434

432-
auto &Lowering = ABI->getModule().getTypeLowering(ABI->getElementType());
435+
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
436+
&& "promoting multi-field box not implemented");
437+
auto &Lowering = ABI->getModule()
438+
.getTypeLowering(ABI->getBoxType()->getFieldType(0));
433439
auto Loc = CleanupLocation::get(ABI->getLoc());
434440

435441
// For non-trivial types, insert destroys for each final release-like

test/DebugInfo/byref-capture.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ func makeIncrementor(_ inc : Int64) -> () -> Int64
99
// CHECK-SAME: metadata ![[SUM_CAPTURE:[0-9]+]],
1010
// CHECK-SAME: metadata ![[EMPTY:.*]])
1111
// CHECK: ![[EMPTY]] = !DIExpression()
12-
// CHECK: ![[SUM_CAPTURE]] = !DILocalVariable(name: "sum", arg: 1,
13-
// CHECK-SAME: line: [[@LINE-8]], type: ![[INOUTTY:[0-9]+]]
14-
// CHECK: ![[INOUTTY]] = !DICompositeType({{.*}}identifier: "_TtRVs5Int64"
12+
// CHECK: ![[INOUTTY:[0-9]+]] = !DICompositeType({{.*}}identifier: "_TtRVs5Int64"
1513
// ^ inout type.
14+
// CHECK: ![[SUM_CAPTURE]] = !DILocalVariable(name: "sum", arg: 1,
15+
// CHECK-SAME: line: [[@LINE-10]], type: ![[INOUTTY]]
1616
sum += inc
1717
return sum
1818
}

0 commit comments

Comments
 (0)