Skip to content

Commit 17cf136

Browse files
committed
Very minimal POC of tuple KP feature
1 parent aab138d commit 17cf136

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,10 +2397,10 @@ class KeyPathPatternComponent {
23972397
StoredProperty,
23982398
GettableProperty,
23992399
SettableProperty,
2400+
TupleElement,
24002401
OptionalChain,
24012402
OptionalForce,
24022403
OptionalWrap,
2403-
TupleElement,
24042404
};
24052405

24062406
// Description of a captured index value and its Hashable conformance for a
@@ -2439,7 +2439,10 @@ class KeyPathPatternComponent {
24392439
// Value is the VarDecl* for StoredProperty, the SILFunction* of the
24402440
// Getter for computed properties, or the Kind for other kinds
24412441
llvm::PointerIntPair<void *, KindPackingBits, unsigned> ValueAndKind;
2442-
llvm::PointerIntPair<SILFunction *, 2,
2442+
2443+
// Setter is the SILFunction* of the Setter for computed properties, or the
2444+
// tuple index for tuple elements
2445+
llvm::PointerIntPair<void *, 2,
24432446
ComputedPropertyId::KindType> SetterAndIdKind;
24442447
ComputedPropertyId::ValueType IdValue;
24452448
ArrayRef<Index> Indices;
@@ -2486,6 +2489,15 @@ class KeyPathPatternComponent {
24862489
&& "not an optional component");
24872490
}
24882491

2492+
/// Constructor for tuple element.
2493+
KeyPathPatternComponent(unsigned tupleIndex, CanType componentType)
2494+
: ValueAndKind((void*)((uintptr_t)Kind::TupleElement << KindPackingBits), PackedStored),
2495+
SetterAndIdKind((void*)((uintptr_t)tupleIndex << 2), (ComputedPropertyId::KindType)0),
2496+
ComponentType(componentType)
2497+
{
2498+
// fixme: [technicated] magic << 2 shift
2499+
}
2500+
24892501
public:
24902502
KeyPathPatternComponent() : ValueAndKind(nullptr, 0) {}
24912503

@@ -2497,7 +2509,8 @@ class KeyPathPatternComponent {
24972509
auto packedKind = ValueAndKind.getInt();
24982510
switch ((PackedKind)packedKind) {
24992511
case PackedStored:
2500-
return Kind::StoredProperty;
2512+
return SetterAndIdKind.getPointer()
2513+
? Kind::TupleElement : Kind::StoredProperty;
25012514
case PackedComputed:
25022515
return SetterAndIdKind.getPointer()
25032516
? Kind::SettableProperty : Kind::GettableProperty;
@@ -2567,7 +2580,7 @@ class KeyPathPatternComponent {
25672580
case Kind::TupleElement:
25682581
llvm_unreachable("not a settable computed property");
25692582
case Kind::SettableProperty:
2570-
return SetterAndIdKind.getPointer();
2583+
return static_cast<SILFunction*>(SetterAndIdKind.getPointer());
25712584
}
25722585
llvm_unreachable("unhandled kind");
25732586
}
@@ -2663,7 +2676,8 @@ class KeyPathPatternComponent {
26632676
case Kind::SettableProperty:
26642677
llvm_unreachable("not a tuple element");
26652678
case Kind::TupleElement:
2666-
llvm_unreachable("[technicated]");
2679+
// fixme: [technicated] magic >> 2 shift
2680+
return (uintptr_t)SetterAndIdKind.getPointer() >> 2;
26672681
}
26682682
llvm_unreachable("unhandled kind");
26692683
}
@@ -2719,6 +2733,11 @@ class KeyPathPatternComponent {
27192733
}
27202734
return KeyPathPatternComponent(kind, ty);
27212735
}
2736+
2737+
static KeyPathPatternComponent forTupleElement(unsigned tupleIndex,
2738+
CanType ty) {
2739+
return KeyPathPatternComponent(tupleIndex, ty);
2740+
}
27222741

27232742
void incrementRefCounts() const;
27242743
void decrementRefCounts() const;

lib/IRGen/GenKeyPath.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "GenMeta.h"
2525
#include "GenProto.h"
2626
#include "GenStruct.h"
27+
#include "GenTuple.h"
2728
#include "GenType.h"
2829
#include "GenericRequirement.h"
2930
#include "IRGenDebugInfo.h"
@@ -1106,8 +1107,22 @@ emitKeyPathComponent(IRGenModule &IGM,
11061107
fields.addInt32(KeyPathComponentHeader::forOptionalWrap().getData());
11071108
break;
11081109
case KeyPathPatternComponent::Kind::TupleElement:
1109-
llvm_unreachable("[technicated]");
1110-
break;
1110+
if (!baseTy->is<TupleType>()) {
1111+
llvm_unreachable("not a tuple");
1112+
}
1113+
1114+
SILType loweredTy = IGM.getSILTypes().getLoweredType(baseTy);
1115+
1116+
if (auto offset = getFixedTupleElementOffset(IGM, loweredTy, component.getTupleIndex())) {
1117+
auto header = KeyPathComponentHeader
1118+
::forStructComponentWithInlineOffset(/*isLet*/ false,
1119+
offset->getValue());
1120+
1121+
fields.addInt32(header.getData());
1122+
break;
1123+
}
1124+
1125+
llvm_unreachable("could not get element offset");
11111126
}
11121127
}
11131128

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ void verifyKeyPathComponent(SILModule &M,
385385
break;
386386
}
387387
case KeyPathPatternComponent::Kind::TupleElement: {
388-
llvm_unreachable("[technicated]");
388+
//llvm_unreachable("[technicated]");
389389
break;
390390
}
391391
}

lib/SILGen/SILGen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
360360
bool forPropertyDescriptor);
361361

362362
KeyPathPatternComponent
363-
emitKeyPathComponentForTupleElement();
363+
emitKeyPathComponentForTupleElement(unsigned tupleIndex, CanType baseTy);
364364

365365
/// Known functions for bridging.
366366
SILDeclRef getStringToNSStringFn();

lib/SILGen/SILGenExpr.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,8 +3575,17 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35753575
}
35763576

35773577
KeyPathPatternComponent
3578-
SILGenModule::emitKeyPathComponentForTupleElement() {
3579-
llvm_unreachable("technicated");
3578+
SILGenModule::emitKeyPathComponentForTupleElement(unsigned tupleIndex,
3579+
CanType baseTy) {
3580+
if (!baseTy->is<TupleType>()) {
3581+
llvm_unreachable("baseTy is expected to be a TupleType");
3582+
}
3583+
3584+
auto elementTy = baseTy->getAs<TupleType>()
3585+
->getElementType(tupleIndex)
3586+
->getCanonicalType();
3587+
3588+
return KeyPathPatternComponent::forTupleElement(tupleIndex, elementTy);
35803589
}
35813590

35823591
RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
@@ -3650,8 +3659,10 @@ RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
36503659
}
36513660

36523661
case KeyPathExpr::Component::Kind::TupleElement: {
3662+
auto tupleIndex = component.getTupleIndex();
36533663
loweredComponents.push_back(
3654-
SGF.SGM.emitKeyPathComponentForTupleElement());
3664+
SGF.SGM.emitKeyPathComponentForTupleElement(tupleIndex,
3665+
baseTy));
36553666

36563667
baseTy = loweredComponents.back().getComponentType();
36573668

0 commit comments

Comments
 (0)