Skip to content

Add SIL instructions for pack indexing #63124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ Types
#if SWIFT_RUNTIME_VERSION >= 5.5
type ::= 'Bj' // Builtin.Job
#endif
type ::= 'BP' // Builtin.PackIndex
type ::= 'BO' // Builtin.UnknownObject (no longer a distinct type, but still used for AnyObject)
type ::= 'Bo' // Builtin.NativeObject
type ::= 'Bp' // Builtin.RawPointer
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ ABSTRACT_TYPE(Builtin, Type)
BUILTIN_TYPE(BuiltinExecutor, BuiltinType)
BUILTIN_TYPE(BuiltinFloat, BuiltinType)
BUILTIN_TYPE(BuiltinJob, BuiltinType)
BUILTIN_TYPE(BuiltinPackIndex, BuiltinType)
BUILTIN_TYPE(BuiltinRawPointer, BuiltinType)
BUILTIN_TYPE(BuiltinRawUnsafeContinuation, BuiltinType)
BUILTIN_TYPE(BuiltinNativeObject, BuiltinType)
Expand Down Expand Up @@ -193,6 +194,7 @@ LAST_TYPE(Dictionary) // Sugared types are last to make isa<SugarType>() fast.
SINGLETON_TYPE(IntegerLiteral, BuiltinIntegerLiteral)
SINGLETON_TYPE(Job, BuiltinJob)
SINGLETON_TYPE(Executor, BuiltinExecutor)
SINGLETON_TYPE(PackIndex, BuiltinPackIndex)
SINGLETON_TYPE(RawPointer, BuiltinRawPointer)
SINGLETON_TYPE(RawUnsafeContinuation, BuiltinRawUnsafeContinuation)
SINGLETON_TYPE(NativeObject, BuiltinNativeObject)
Expand Down
15 changes: 15 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,21 @@ class BuiltinDefaultActorStorageType : public BuiltinType {
};
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinDefaultActorStorageType, BuiltinType)

/// BuiltinPackIndexType - The type of an (untyped) index into a pack
/// in SIL. Essentially a UInt32 with some structural restrictions
/// about how it can be produced that ensures SIL maintains well-typed
/// use-def relationships around packs.
class BuiltinPackIndexType : public BuiltinType {
friend class ASTContext;
BuiltinPackIndexType(const ASTContext &C)
: BuiltinType(TypeKind::BuiltinPackIndex, C) {}
public:
static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::BuiltinPackIndex;
}
};
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinPackIndexType, BuiltinType)

/// BuiltinNativeObjectType - The builtin opaque object-pointer type.
/// Useful for keeping an object alive when it is otherwise being
/// manipulated via an unsafe pointer type.
Expand Down
26 changes: 26 additions & 0 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,32 @@ class SILBuilder {
getSILDebugLocation(Loc), Existential));
}

DynamicPackIndexInst *
createDynamicPackIndex(SILLocation loc, SILValue indexValue,
CanPackType indexedPackType) {
return insert(DynamicPackIndexInst::create(getFunction(),
getSILDebugLocation(loc),
indexValue, indexedPackType));
}

PackPackIndexInst *
createPackPackIndex(SILLocation loc, unsigned sliceStartIndex,
SILValue indexWithinSlice,
CanPackType indexedPackType) {
return insert(PackPackIndexInst::create(getFunction(),
getSILDebugLocation(loc),
sliceStartIndex, indexWithinSlice,
indexedPackType));
}

ScalarPackIndexInst *
createScalarPackIndex(SILLocation loc, unsigned componentIndex,
CanPackType indexedPackType) {
return insert(ScalarPackIndexInst::create(getFunction(),
getSILDebugLocation(loc),
componentIndex, indexedPackType));
}

OpenPackElementInst *
createOpenPackElement(SILLocation loc, SILValue packIndex,
GenericEnvironment *openedElementEnvironment) {
Expand Down
67 changes: 67 additions & 0 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,25 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
return asImpl().remapASTType(ty);
}

/// Remap a structural index into a pack so that it will point to the
/// corresponding structural index in the remapped pack type.
unsigned getOpStructuralPackIndex(CanPackType origPackType,
unsigned origIndex) {
assert(origIndex < origPackType->getNumElements());
unsigned newIndex = 0;
for (unsigned i = 0; i != origIndex; ++i) {
auto origComponentType = origPackType.getElementType(i);
if (auto origExpansionType =
dyn_cast<PackExpansionType>(origComponentType)) {
auto newShapeClass = getOpASTType(origExpansionType.getCountType());
newIndex += cast<PackType>(newShapeClass)->getNumElements();
} else {
newIndex++;
}
}
return newIndex;
}

void remapRootOpenedType(CanOpenedArchetypeType archetypeTy) {
assert(archetypeTy->isRoot());

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

template <typename ImplClass>
void SILCloner<ImplClass>::visitDynamicPackIndexInst(
DynamicPackIndexInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

auto newIndexValue = getOpValue(Inst->getOperand());
auto loc = getOpLocation(Inst->getLoc());
auto newPackType = cast<PackType>(getOpASTType(Inst->getIndexedPackType()));

recordClonedInstruction(
Inst, getBuilder().createDynamicPackIndex(loc, newIndexValue,
newPackType));
}

template <typename ImplClass>
void SILCloner<ImplClass>::visitPackPackIndexInst(PackPackIndexInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

auto newIndexValue = getOpValue(Inst->getOperand());
auto loc = getOpLocation(Inst->getLoc());
auto newPackType = cast<PackType>(getOpASTType(Inst->getIndexedPackType()));

auto newComponentStartIndex =
getOpStructuralPackIndex(Inst->getIndexedPackType(),
Inst->getComponentStartIndex());

recordClonedInstruction(
Inst, getBuilder().createPackPackIndex(loc, newComponentStartIndex,
newIndexValue, newPackType));
}

template <typename ImplClass>
void SILCloner<ImplClass>::visitScalarPackIndexInst(
ScalarPackIndexInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

auto loc = getOpLocation(Inst->getLoc());
auto newPackType = cast<PackType>(getOpASTType(Inst->getIndexedPackType()));

auto newComponentIndex =
getOpStructuralPackIndex(Inst->getIndexedPackType(),
Inst->getComponentIndex());

recordClonedInstruction(
Inst, getBuilder().createScalarPackIndex(loc, newComponentIndex,
newPackType));
}

template <typename ImplClass>
void SILCloner<ImplClass>::visitOpenPackElementInst(
OpenPackElementInst *Inst) {
Expand Down
Loading