Skip to content

Commit 6d98f0e

Browse files
authored
Merge pull request #63124 from rjmccall/pack-indexing
Add SIL instructions for pack indexing
2 parents 32057c5 + 374c202 commit 6d98f0e

34 files changed

+816
-78
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ Types
570570
#if SWIFT_RUNTIME_VERSION >= 5.5
571571
type ::= 'Bj' // Builtin.Job
572572
#endif
573+
type ::= 'BP' // Builtin.PackIndex
573574
type ::= 'BO' // Builtin.UnknownObject (no longer a distinct type, but still used for AnyObject)
574575
type ::= 'Bo' // Builtin.NativeObject
575576
type ::= 'Bp' // Builtin.RawPointer

include/swift/AST/TypeNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ ABSTRACT_TYPE(Builtin, Type)
106106
BUILTIN_TYPE(BuiltinExecutor, BuiltinType)
107107
BUILTIN_TYPE(BuiltinFloat, BuiltinType)
108108
BUILTIN_TYPE(BuiltinJob, BuiltinType)
109+
BUILTIN_TYPE(BuiltinPackIndex, BuiltinType)
109110
BUILTIN_TYPE(BuiltinRawPointer, BuiltinType)
110111
BUILTIN_TYPE(BuiltinRawUnsafeContinuation, BuiltinType)
111112
BUILTIN_TYPE(BuiltinNativeObject, BuiltinType)
@@ -193,6 +194,7 @@ LAST_TYPE(Dictionary) // Sugared types are last to make isa<SugarType>() fast.
193194
SINGLETON_TYPE(IntegerLiteral, BuiltinIntegerLiteral)
194195
SINGLETON_TYPE(Job, BuiltinJob)
195196
SINGLETON_TYPE(Executor, BuiltinExecutor)
197+
SINGLETON_TYPE(PackIndex, BuiltinPackIndex)
196198
SINGLETON_TYPE(RawPointer, BuiltinRawPointer)
197199
SINGLETON_TYPE(RawUnsafeContinuation, BuiltinRawUnsafeContinuation)
198200
SINGLETON_TYPE(NativeObject, BuiltinNativeObject)

include/swift/AST/Types.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,21 @@ class BuiltinDefaultActorStorageType : public BuiltinType {
15981598
};
15991599
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinDefaultActorStorageType, BuiltinType)
16001600

1601+
/// BuiltinPackIndexType - The type of an (untyped) index into a pack
1602+
/// in SIL. Essentially a UInt32 with some structural restrictions
1603+
/// about how it can be produced that ensures SIL maintains well-typed
1604+
/// use-def relationships around packs.
1605+
class BuiltinPackIndexType : public BuiltinType {
1606+
friend class ASTContext;
1607+
BuiltinPackIndexType(const ASTContext &C)
1608+
: BuiltinType(TypeKind::BuiltinPackIndex, C) {}
1609+
public:
1610+
static bool classof(const TypeBase *T) {
1611+
return T->getKind() == TypeKind::BuiltinPackIndex;
1612+
}
1613+
};
1614+
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinPackIndexType, BuiltinType)
1615+
16011616
/// BuiltinNativeObjectType - The builtin opaque object-pointer type.
16021617
/// Useful for keeping an object alive when it is otherwise being
16031618
/// manipulated via an unsafe pointer type.

include/swift/SIL/SILBuilder.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,32 @@ class SILBuilder {
19361936
getSILDebugLocation(Loc), Existential));
19371937
}
19381938

1939+
DynamicPackIndexInst *
1940+
createDynamicPackIndex(SILLocation loc, SILValue indexValue,
1941+
CanPackType indexedPackType) {
1942+
return insert(DynamicPackIndexInst::create(getFunction(),
1943+
getSILDebugLocation(loc),
1944+
indexValue, indexedPackType));
1945+
}
1946+
1947+
PackPackIndexInst *
1948+
createPackPackIndex(SILLocation loc, unsigned sliceStartIndex,
1949+
SILValue indexWithinSlice,
1950+
CanPackType indexedPackType) {
1951+
return insert(PackPackIndexInst::create(getFunction(),
1952+
getSILDebugLocation(loc),
1953+
sliceStartIndex, indexWithinSlice,
1954+
indexedPackType));
1955+
}
1956+
1957+
ScalarPackIndexInst *
1958+
createScalarPackIndex(SILLocation loc, unsigned componentIndex,
1959+
CanPackType indexedPackType) {
1960+
return insert(ScalarPackIndexInst::create(getFunction(),
1961+
getSILDebugLocation(loc),
1962+
componentIndex, indexedPackType));
1963+
}
1964+
19391965
OpenPackElementInst *
19401966
createOpenPackElement(SILLocation loc, SILValue packIndex,
19411967
GenericEnvironment *openedElementEnvironment) {

include/swift/SIL/SILCloner.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
250250
return asImpl().remapASTType(ty);
251251
}
252252

253+
/// Remap a structural index into a pack so that it will point to the
254+
/// corresponding structural index in the remapped pack type.
255+
unsigned getOpStructuralPackIndex(CanPackType origPackType,
256+
unsigned origIndex) {
257+
assert(origIndex < origPackType->getNumElements());
258+
unsigned newIndex = 0;
259+
for (unsigned i = 0; i != origIndex; ++i) {
260+
auto origComponentType = origPackType.getElementType(i);
261+
if (auto origExpansionType =
262+
dyn_cast<PackExpansionType>(origComponentType)) {
263+
auto newShapeClass = getOpASTType(origExpansionType.getCountType());
264+
newIndex += cast<PackType>(newShapeClass)->getNumElements();
265+
} else {
266+
newIndex++;
267+
}
268+
}
269+
return newIndex;
270+
}
271+
253272
void remapRootOpenedType(CanOpenedArchetypeType archetypeTy) {
254273
assert(archetypeTy->isRoot());
255274

@@ -2392,6 +2411,54 @@ void SILCloner<ImplClass>::visitDeinitExistentialValueInst(
23922411
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
23932412
}
23942413

2414+
template <typename ImplClass>
2415+
void SILCloner<ImplClass>::visitDynamicPackIndexInst(
2416+
DynamicPackIndexInst *Inst) {
2417+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2418+
2419+
auto newIndexValue = getOpValue(Inst->getOperand());
2420+
auto loc = getOpLocation(Inst->getLoc());
2421+
auto newPackType = cast<PackType>(getOpASTType(Inst->getIndexedPackType()));
2422+
2423+
recordClonedInstruction(
2424+
Inst, getBuilder().createDynamicPackIndex(loc, newIndexValue,
2425+
newPackType));
2426+
}
2427+
2428+
template <typename ImplClass>
2429+
void SILCloner<ImplClass>::visitPackPackIndexInst(PackPackIndexInst *Inst) {
2430+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2431+
2432+
auto newIndexValue = getOpValue(Inst->getOperand());
2433+
auto loc = getOpLocation(Inst->getLoc());
2434+
auto newPackType = cast<PackType>(getOpASTType(Inst->getIndexedPackType()));
2435+
2436+
auto newComponentStartIndex =
2437+
getOpStructuralPackIndex(Inst->getIndexedPackType(),
2438+
Inst->getComponentStartIndex());
2439+
2440+
recordClonedInstruction(
2441+
Inst, getBuilder().createPackPackIndex(loc, newComponentStartIndex,
2442+
newIndexValue, newPackType));
2443+
}
2444+
2445+
template <typename ImplClass>
2446+
void SILCloner<ImplClass>::visitScalarPackIndexInst(
2447+
ScalarPackIndexInst *Inst) {
2448+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2449+
2450+
auto loc = getOpLocation(Inst->getLoc());
2451+
auto newPackType = cast<PackType>(getOpASTType(Inst->getIndexedPackType()));
2452+
2453+
auto newComponentIndex =
2454+
getOpStructuralPackIndex(Inst->getIndexedPackType(),
2455+
Inst->getComponentIndex());
2456+
2457+
recordClonedInstruction(
2458+
Inst, getBuilder().createScalarPackIndex(loc, newComponentIndex,
2459+
newPackType));
2460+
}
2461+
23952462
template <typename ImplClass>
23962463
void SILCloner<ImplClass>::visitOpenPackElementInst(
23972464
OpenPackElementInst *Inst) {

0 commit comments

Comments
 (0)