Skip to content

Commit 6bf9ac8

Browse files
committed
Add instructions to allocate and deallocate packs.
Having added these, I'm not entirely sure we couldn't just use alloc_stack and dealloc_stack. Well, if we find ourselves adding a lot of redundancy with those instructions (e.g. around DI), we can always go back and rip these out.
1 parent c491d25 commit 6bf9ac8

File tree

18 files changed

+174
-2
lines changed

18 files changed

+174
-2
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ class SILBuilder {
409409
wasMoved));
410410
}
411411

412+
AllocPackInst *createAllocPack(SILLocation loc, SILType packType) {
413+
return insert(AllocPackInst::create(getSILDebugLocation(loc), packType,
414+
getFunction()));
415+
}
416+
412417
AllocRefInst *createAllocRef(SILLocation Loc, SILType ObjectType,
413418
bool objc, bool canAllocOnStack,
414419
ArrayRef<SILType> ElementTypes,
@@ -2108,6 +2113,10 @@ class SILBuilder {
21082113
return insert(new (getModule())
21092114
DeallocStackInst(getSILDebugLocation(Loc), operand));
21102115
}
2116+
DeallocPackInst *createDeallocPack(SILLocation loc, SILValue operand) {
2117+
return insert(new (getModule())
2118+
DeallocPackInst(getSILDebugLocation(loc), operand));
2119+
}
21112120
DeallocStackRefInst *createDeallocStackRef(SILLocation Loc,
21122121
SILValue operand) {
21132122
return insert(new (getModule())

include/swift/SIL/SILCloner.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,16 @@ SILCloner<ImplClass>::visitAllocStackInst(AllocStackInst *Inst) {
829829
recordClonedInstruction(Inst, NewInst);
830830
}
831831

832+
template<typename ImplClass>
833+
void
834+
SILCloner<ImplClass>::visitAllocPackInst(AllocPackInst *Inst) {
835+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
836+
SILLocation Loc = getOpLocation(Inst->getLoc());
837+
auto *NewInst = getBuilder().createAllocPack(
838+
Loc, getOpType(Inst->getType().getObjectType()));
839+
recordClonedInstruction(Inst, NewInst);
840+
}
841+
832842
template<typename ImplClass>
833843
void
834844
SILCloner<ImplClass>::visitAllocRefInst(AllocRefInst *Inst) {
@@ -2647,6 +2657,15 @@ SILCloner<ImplClass>::visitDeallocStackInst(DeallocStackInst *Inst) {
26472657
getOpValue(Inst->getOperand())));
26482658
}
26492659

2660+
template<typename ImplClass>
2661+
void
2662+
SILCloner<ImplClass>::visitDeallocPackInst(DeallocPackInst *Inst) {
2663+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2664+
recordClonedInstruction(
2665+
Inst, getBuilder().createDeallocPack(getOpLocation(Inst->getLoc()),
2666+
getOpValue(Inst->getOperand())));
2667+
}
2668+
26502669
template<typename ImplClass>
26512670
void
26522671
SILCloner<ImplClass>::visitDeallocRefInst(DeallocRefInst *Inst) {

include/swift/SIL/SILInstruction.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,32 @@ class AllocStackInst final
22212221
DeallocStackInst *getSingleDeallocStack() const;
22222222
};
22232223

2224+
/// AllocPackInst - This represents the allocation of a value pack
2225+
/// in stack memory. The memory is provided uninitialized.
2226+
class AllocPackInst final
2227+
: public NullaryInstructionWithTypeDependentOperandsBase<
2228+
SILInstructionKind::AllocPackInst,
2229+
AllocPackInst,
2230+
AllocationInst> {
2231+
friend TrailingObjects;
2232+
friend SILBuilder;
2233+
2234+
AllocPackInst(SILDebugLocation loc, SILType resultType,
2235+
ArrayRef<SILValue> typeDependentOperands)
2236+
: NullaryInstructionWithTypeDependentOperandsBase(loc,
2237+
typeDependentOperands,
2238+
resultType) {}
2239+
2240+
static AllocPackInst *create(SILDebugLocation loc, SILType packType,
2241+
SILFunction &F);
2242+
public:
2243+
/// Return the allocated pack type. The result type of the instruction
2244+
/// is an address of this type.
2245+
CanSILPackType getPackType() const {
2246+
return getType().castTo<SILPackType>();
2247+
}
2248+
};
2249+
22242250
/// The base class for AllocRefInst and AllocRefDynamicInst.
22252251
///
22262252
/// The first NumTailTypes operands are counts for the tail allocated
@@ -8298,6 +8324,16 @@ class DeallocStackInst :
82988324
: UnaryInstructionBase(DebugLoc, operand) {}
82998325
};
83008326

8327+
/// DeallocPackInst - Deallocate stack memory allocated by alloc_pack.
8328+
class DeallocPackInst :
8329+
public UnaryInstructionBase<SILInstructionKind::DeallocPackInst,
8330+
DeallocationInst> {
8331+
friend SILBuilder;
8332+
8333+
DeallocPackInst(SILDebugLocation debugLoc, SILValue operand)
8334+
: UnaryInstructionBase(debugLoc, operand) {}
8335+
};
8336+
83018337
/// Like DeallocStackInst, but for `alloc_ref [stack]`.
83028338
class DeallocStackRefInst
83038339
: public UnaryInstructionBase<SILInstructionKind::DeallocStackRefInst,

include/swift/SIL/SILNodes.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
308308
ABSTRACT_SINGLE_VALUE_INST(AllocationInst, SingleValueInstruction)
309309
SINGLE_VALUE_INST(AllocStackInst, alloc_stack,
310310
AllocationInst, None, DoesNotRelease)
311+
SINGLE_VALUE_INST(AllocPackInst, alloc_pack,
312+
AllocationInst, None, DoesNotRelease)
311313
SINGLE_VALUE_INST(AllocRefInst, alloc_ref,
312314
AllocationInst, None, DoesNotRelease)
313315
SINGLE_VALUE_INST(AllocRefDynamicInst, alloc_ref_dynamic,
@@ -702,6 +704,8 @@ ABSTRACT_INST(TermInst, SILInstruction)
702704
ABSTRACT_INST(DeallocationInst, SILInstruction)
703705
NON_VALUE_INST(DeallocStackInst, dealloc_stack,
704706
DeallocationInst, MayHaveSideEffects, DoesNotRelease)
707+
NON_VALUE_INST(DeallocPackInst, dealloc_pack,
708+
DeallocationInst, MayHaveSideEffects, DoesNotRelease)
705709
NON_VALUE_INST(DeallocStackRefInst, dealloc_stack_ref,
706710
DeallocationInst, MayHaveSideEffects, DoesNotRelease)
707711
NON_VALUE_INST(DeallocRefInst, dealloc_ref,

lib/IRGen/IRGenSIL.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ class IRGenSILFunction :
11491149
void emitDebugInfoForAllocStack(AllocStackInst *i, const TypeInfo &type,
11501150
llvm::Value *addr);
11511151
void visitAllocStackInst(AllocStackInst *i);
1152+
void visitAllocPackInst(AllocPackInst *i);
11521153
void visitAllocRefInst(AllocRefInst *i);
11531154
void visitAllocRefDynamicInst(AllocRefDynamicInst *i);
11541155
void visitAllocBoxInst(AllocBoxInst *i);
@@ -1324,6 +1325,7 @@ class IRGenSILFunction :
13241325
void visitIsEscapingClosureInst(IsEscapingClosureInst *i);
13251326
void visitDeallocStackInst(DeallocStackInst *i);
13261327
void visitDeallocStackRefInst(DeallocStackRefInst *i);
1328+
void visitDeallocPackInst(DeallocPackInst *i);
13271329
void visitDeallocBoxInst(DeallocBoxInst *i);
13281330
void visitDeallocRefInst(DeallocRefInst *i);
13291331
void visitDeallocPartialRefInst(DeallocPartialRefInst *i);
@@ -5496,6 +5498,10 @@ void IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst *i) {
54965498
emitDebugInfoForAllocStack(i, type, addr.getAddress());
54975499
}
54985500

5501+
void IRGenSILFunction::visitAllocPackInst(swift::AllocPackInst *i) {
5502+
IGM.unimplemented(i->getLoc().getSourceLoc(), "alloc_pack");
5503+
}
5504+
54995505
static void
55005506
buildTailArrays(IRGenSILFunction &IGF,
55015507
SmallVectorImpl<std::pair<SILType, llvm::Value *>> &TailArrays,
@@ -5595,6 +5601,10 @@ void IRGenSILFunction::visitDeallocStackRefInst(DeallocStackRefInst *i) {
55955601
}
55965602
}
55975603

5604+
void IRGenSILFunction::visitDeallocPackInst(swift::DeallocPackInst *i) {
5605+
IGM.unimplemented(i->getLoc().getSourceLoc(), "dealloc_pack");
5606+
}
5607+
55985608
void IRGenSILFunction::visitDeallocRefInst(swift::DeallocRefInst *i) {
55995609
// Lower the operand.
56005610
Explosion self = getLoweredExplosion(i->getOperand());

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ SHOULD_NEVER_VISIT_INST(AllocBox)
9898
SHOULD_NEVER_VISIT_INST(AllocExistentialBox)
9999
SHOULD_NEVER_VISIT_INST(AllocGlobal)
100100
SHOULD_NEVER_VISIT_INST(AllocStack)
101+
SHOULD_NEVER_VISIT_INST(AllocPack)
101102
SHOULD_NEVER_VISIT_INST(DifferentiabilityWitnessFunction)
102103
SHOULD_NEVER_VISIT_INST(FloatLiteral)
103104
SHOULD_NEVER_VISIT_INST(FunctionRef)
@@ -154,6 +155,7 @@ OPERAND_OWNERSHIP(TrivialUse, CopyAddr)
154155
OPERAND_OWNERSHIP(TrivialUse, ExplicitCopyAddr)
155156
OPERAND_OWNERSHIP(TrivialUse, MarkUnresolvedMoveAddr)
156157
OPERAND_OWNERSHIP(TrivialUse, DeallocStack)
158+
OPERAND_OWNERSHIP(TrivialUse, DeallocPack)
157159
OPERAND_OWNERSHIP(TrivialUse, DeinitExistentialAddr)
158160
OPERAND_OWNERSHIP(TrivialUse, DestroyAddr)
159161
OPERAND_OWNERSHIP(TrivialUse, EndAccess)

lib/SIL/IR/SILInstruction.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,8 @@ namespace {
12301230
} // end anonymous namespace
12311231

12321232
bool SILInstruction::isAllocatingStack() const {
1233-
if (isa<AllocStackInst>(this))
1233+
if (isa<AllocStackInst>(this) ||
1234+
isa<AllocPackInst>(this))
12341235
return true;
12351236

12361237
if (auto *ARI = dyn_cast<AllocRefInstBase>(this)) {
@@ -1251,7 +1252,9 @@ bool SILInstruction::isAllocatingStack() const {
12511252
}
12521253

12531254
bool SILInstruction::isDeallocatingStack() const {
1254-
if (isa<DeallocStackInst>(this) || isa<DeallocStackRefInst>(this))
1255+
if (isa<DeallocStackInst>(this) ||
1256+
isa<DeallocStackRefInst>(this) ||
1257+
isa<DeallocPackInst>(this))
12551258
return true;
12561259

12571260
if (auto *BI = dyn_cast<BuiltinInst>(this)) {

lib/SIL/IR/SILInstructions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ DeallocStackInst *AllocStackInst::getSingleDeallocStack() const {
296296
return Dealloc;
297297
}
298298

299+
AllocPackInst *AllocPackInst::create(SILDebugLocation loc,
300+
SILType packType,
301+
SILFunction &F) {
302+
assert(packType.isObject());
303+
assert(packType.is<SILPackType>() && "pack type must be lowered");
304+
auto resultType = packType.getAddressType();
305+
306+
SmallVector<SILValue, 8> allOperands;
307+
collectTypeDependentOperands(allOperands, F, packType);
308+
309+
auto size = totalSizeToAlloc<swift::Operand>(allOperands.size());
310+
auto buffer = F.getModule().allocateInst(size, alignof(AllocPackInst));
311+
return ::new (buffer) AllocPackInst(loc, resultType, allOperands);
312+
}
313+
299314
AllocRefInstBase::AllocRefInstBase(SILInstructionKind Kind,
300315
SILDebugLocation Loc,
301316
SILType ObjectType,

lib/SIL/IR/SILPrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
13971397
printDebugVar(AVI->getVarInfo(),
13981398
&AVI->getModule().getASTContext().SourceMgr);
13991399
}
1400+
void visitAllocPackInst(AllocPackInst *API) {
1401+
*this << API->getType().getObjectType();
1402+
}
14001403

14011404
void printAllocRefInstBase(AllocRefInstBase *ARI) {
14021405
if (ARI->isObjC())
@@ -2374,6 +2377,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
23742377
void visitDeallocStackInst(DeallocStackInst *DI) {
23752378
*this << getIDAndType(DI->getOperand());
23762379
}
2380+
void visitDeallocPackInst(DeallocPackInst *DI) {
2381+
*this << getIDAndType(DI->getOperand());
2382+
}
23772383
void visitDeallocStackRefInst(DeallocStackRefInst *ESRL) {
23782384
*this << getIDAndType(ESRL->getOperand());
23792385
}

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ CONSTANT_OWNERSHIP_INST(Owned, ObjCMetatypeToObject)
9393
// not though.
9494
CONSTANT_OWNERSHIP_INST(None, AddressToPointer)
9595
CONSTANT_OWNERSHIP_INST(None, AllocStack)
96+
CONSTANT_OWNERSHIP_INST(None, AllocPack)
9697
CONSTANT_OWNERSHIP_INST(None, BeginAccess)
9798
CONSTANT_OWNERSHIP_INST(None, BindMemory)
9899
CONSTANT_OWNERSHIP_INST(None, RebindMemory)

0 commit comments

Comments
 (0)