Skip to content

Commit 84ab0eb

Browse files
committed
[AddressLowering] Handle tuple_pack_extract.
By analogy with tuple_extract, transform the new instruction to tuple_pack_element_addr.
1 parent 7bddaf3 commit 84ab0eb

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,9 @@ static Operand *getProjectedDefOperand(SILValue value) {
974974
case ValueKind::OpenExistentialBoxValueInst:
975975
assert(value->getOwnershipKind() == OwnershipKind::Guaranteed);
976976
return &cast<SingleValueInstruction>(value)->getAllOperands()[0];
977+
case ValueKind::TuplePackExtractInst:
978+
assert(value->getOwnershipKind() == OwnershipKind::Guaranteed);
979+
return &cast<SingleValueInstruction>(value)->getAllOperands()[1];
977980
}
978981
}
979982

@@ -1703,6 +1706,8 @@ class AddressMaterialization {
17031706

17041707
SILValue materializeTupleExtract(SILInstruction *extractInst,
17051708
SILValue elementValue, unsigned fieldIdx);
1709+
SILValue materializeTuplePackExtract(SILInstruction *extractInst,
1710+
SILValue elementValue, SILValue index);
17061711

17071712
SILValue materializeProjectionIntoUse(Operand *operand, bool intoPhiOperand);
17081713
SILValue materializeProjectionIntoUseImpl(Operand *operand,
@@ -1859,6 +1864,11 @@ SILValue AddressMaterialization::materializeDefProjection(SILValue origValue) {
18591864
return materializeTupleExtract(extractInst, origValue,
18601865
extractInst->getFieldIndex());
18611866
}
1867+
case ValueKind::TuplePackExtractInst: {
1868+
auto *extractInst = cast<TuplePackExtractInst>(origValue);
1869+
return materializeTuplePackExtract(extractInst, origValue,
1870+
extractInst->getIndex());
1871+
}
18621872
case ValueKind::SILPhiArgument: {
18631873
// Handle this in the caller. unchecked_take_enum_data_addr is
18641874
// destructive. It cannot be materialized on demand.
@@ -1888,6 +1898,14 @@ SILValue AddressMaterialization::materializeTupleExtract(
18881898
elementValue->getType().getAddressType());
18891899
}
18901900

1901+
SILValue AddressMaterialization::materializeTuplePackExtract(
1902+
SILInstruction *extractInst, SILValue elementValue, SILValue fieldIdx) {
1903+
SILValue srcAddr = pass.getMaterializedAddress(extractInst->getOperand(1));
1904+
return projectionBuilder.createTuplePackElementAddr(
1905+
pass.genLoc(), fieldIdx, srcAddr,
1906+
elementValue->getType().getAddressType());
1907+
}
1908+
18911909
SILValue
18921910
AddressMaterialization::materializeProjectionIntoUse(Operand *operand,
18931911
bool intoPhiOperand) {
@@ -3528,6 +3546,9 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
35283546
// Extract from an opaque tuple.
35293547
void visitTupleExtractInst(TupleExtractInst *extractInst);
35303548

3549+
// Extract from an opaque pack tuple.
3550+
void visitTuplePackExtractInst(TuplePackExtractInst *extractInst);
3551+
35313552
void
35323553
visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *uncheckedCastInst) {
35333554
SILValue srcVal = uncheckedCastInst->getOperand();
@@ -3819,6 +3840,10 @@ void UseRewriter::visitTupleExtractInst(TupleExtractInst *extractInst) {
38193840
emitExtract(extractInst);
38203841
}
38213842

3843+
void UseRewriter::visitTuplePackExtractInst(TuplePackExtractInst *extractInst) {
3844+
emitExtract(extractInst);
3845+
}
3846+
38223847
// Rewrite switch_enum to switch_enum_addr. All associated block arguments are
38233848
// removed.
38243849
void UseRewriter::visitSwitchEnumInst(SwitchEnumInst * switchEnum) {

test/SILOptimizer/address_lowering.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,26 @@ entry:
26882688
return %retval : $()
26892689
}
26902690

2691+
// CHECK-LABEL: sil [ossa] @test_tuple_pack_extract : {{.*}} {
2692+
// CHECK: bb0([[TUPLE_PACK_ADDR:%[^,]+]] :
2693+
// CHECK: [[INDEX:%[^,]+]] = dynamic_pack_index
2694+
// CHECK: open_pack_element [[INDEX]] of <each U_1> at <Pack{repeat each T}>, shape $each U_1, uuid [[UUID:".*"]]
2695+
// CHECK: [[BORROW:%[^,]+]] = function_ref @borrowT
2696+
// CHECK: [[ELEMENT_ADDR:%[^,]+]] = tuple_pack_element_addr [[INDEX]] of [[TUPLE_PACK_ADDR]] : {{.*}} as $*@pack_element([[UUID]]) each U_1
2697+
// CHECK: apply [[BORROW]]<@pack_element([[UUID]]) each U_1>([[ELEMENT_ADDR]])
2698+
// CHECK-LABEL: } // end sil function 'test_tuple_pack_extract'
2699+
sil [ossa] @test_tuple_pack_extract : $@convention(thin) <each T> (@in_guaranteed (repeat each T)) -> () {
2700+
entry(%tuple : @guaranteed $(repeat each T)):
2701+
%zero = integer_literal $Builtin.Word, 0
2702+
%index = dynamic_pack_index %zero of $Pack{repeat each T}
2703+
%opened = open_pack_element %index of <each U_1> at <Pack{repeat each T}>, shape $U_1, uuid "00000000-0000-0000-0000-000000000002"
2704+
%elt = tuple_pack_extract %index of %tuple : $(repeat each T) as $@pack_element("00000000-0000-0000-0000-000000000002") U_1
2705+
%borrow = function_ref @borrowT : $@convention(thin) <T> (@in_guaranteed T) -> ()
2706+
apply %borrow<@pack_element("00000000-0000-0000-0000-000000000002") U_1>(%elt) : $@convention(thin) <T> (@in_guaranteed T) -> ()
2707+
%retval = tuple ()
2708+
return %retval : $()
2709+
}
2710+
26912711
// CHECK-LABEL: sil hidden [ossa] @test_unchecked_bitwise_cast :
26922712
// CHECK: bb0(%0 : $*U, %1 : $*T, %2 : $@thick U.Type):
26932713
// CHECK: [[STK:%.*]] = alloc_stack $T

0 commit comments

Comments
 (0)