Skip to content

Commit c51d53b

Browse files
committed
[PackMetadataMarkerInserter] Insert fewer markers.
Rather than emitting markers for every single instruction of the relevant sorts, check whether the instructions' types involve packs. Only record them as potential on-stack pack metadata emitters if they do.
1 parent 2af114c commit c51d53b

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

lib/SIL/IR/SILInstruction.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,17 +1295,41 @@ bool SILInstruction::isDeallocatingStack() const {
12951295
bool SILInstruction::mayRequirePackMetadata() const {
12961296
switch (getKind()) {
12971297
case SILInstructionKind::AllocPackInst:
1298-
case SILInstructionKind::PartialApplyInst:
1299-
case SILInstructionKind::ApplyInst:
1300-
case SILInstructionKind::BeginApplyInst:
1301-
case SILInstructionKind::TryApplyInst:
1302-
case SILInstructionKind::DebugValueInst:
1303-
case SILInstructionKind::MetatypeInst:
13041298
case SILInstructionKind::TuplePackElementAddrInst:
13051299
case SILInstructionKind::OpenPackElementInst:
1306-
case SILInstructionKind::ClassMethodInst:
1307-
case SILInstructionKind::WitnessMethodInst:
13081300
return true;
1301+
case SILInstructionKind::PartialApplyInst:
1302+
case SILInstructionKind::ApplyInst:
1303+
case SILInstructionKind::BeginApplyInst:
1304+
case SILInstructionKind::TryApplyInst: {
1305+
// Check the function type for packs.
1306+
auto apply = ApplySite::isa(const_cast<SILInstruction *>(this));
1307+
if (apply.getCallee()->getType().hasPack())
1308+
return true;
1309+
// Check the substituted types for packs.
1310+
for (auto ty : apply.getSubstitutionMap().getReplacementTypes()) {
1311+
if (ty->hasPack())
1312+
return true;
1313+
}
1314+
return false;
1315+
}
1316+
case SILInstructionKind::DebugValueInst: {
1317+
auto *dvi = cast<DebugValueInst>(this);
1318+
return dvi->getOperand()->getType().hasPack();
1319+
}
1320+
case SILInstructionKind::MetatypeInst: {
1321+
auto *mi = cast<MetatypeInst>(this);
1322+
return mi->getType().hasPack();
1323+
}
1324+
case SILInstructionKind::ClassMethodInst: {
1325+
auto *cmi = cast<ClassMethodInst>(this);
1326+
return cmi->getOperand()->getType().hasPack();
1327+
}
1328+
case SILInstructionKind::WitnessMethodInst: {
1329+
auto *wmi = cast<WitnessMethodInst>(this);
1330+
auto ty = wmi->getLookupType();
1331+
return ty->hasPack();
1332+
}
13091333
default:
13101334
return false;
13111335
}

test/IRGen/pack_metadata_marker_inserter.sil

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ struct GV<each T> {
2626
var tu: (repeat each T)
2727
}
2828

29+
struct G<T> {
30+
var t: T
31+
}
32+
2933
sil @takeTypePack : $<each T>() -> ()
3034
sil @take : $<T>(@in T) -> ()
3135

@@ -56,6 +60,16 @@ entry:
5660
// BEGIN: Instructions: Apply {{
5761
// =============================================================================
5862

63+
// CHECK-SIL-LABEL: sil @no_markers_for_apply_without_pack : $@convention(thin) () -> () {
64+
// CHECK-SIL-NOT: alloc_pack_metadata
65+
// CHECK-SIL-LABEL: } // end sil function 'no_markers_for_apply_without_pack'
66+
sil @no_markers_for_apply_without_pack : $() -> () {
67+
entry:
68+
apply undef() : $@convention(thin) () -> ()
69+
%retval = tuple ()
70+
return %retval : $()
71+
}
72+
5973
// A pack directly in the signature results in markers.
6074
// CHECK-SIL-LABEL: sil @forward_type_pack {{.*}} {
6175
// CHECK-SIL: [[TAKE:%[^,]+]] = function_ref @takeTypePack
@@ -106,6 +120,33 @@ entry(%gv : $*GV<T, S2, S3>):
106120
%retval = tuple ()
107121
return %retval : $()
108122
}
123+
124+
// A _non_-variadic generic type within the signature doesn't entail a marker.
125+
// CHECK-SIL-LABEL: sil @apply_with_generic_instance : {{.*}} {
126+
// CHECK-SIL-NOT: alloc_pack_metadata
127+
// CHECK-SIL-LABEL: } // end sil function 'apply_with_generic_instance'
128+
sil @apply_with_generic_instance : $<T>(@in G<T>) -> () {
129+
entry(%g : $*G<T>):
130+
%take = function_ref @take : $@convention(thin) <T>(@in T) -> ()
131+
apply %take<G<T>>(%g) : $@convention(thin) <T>(@in T) -> ()
132+
%retval = tuple ()
133+
return %retval : $()
134+
}
135+
136+
// CHECK-SIL-LABEL: sil @apply_variadic_with_generic_instance : {{.*}} {
137+
// CHECK-SIL: [[TAKE:%[^,]+]] = function_ref @takeTypePack
138+
// CHECK-SIL: [[MARKER:%[^,]+]] = alloc_pack_metadata
139+
// CHECK-SIL: apply [[TAKE]]
140+
// CHECK-SIL: dealloc_pack_metadata [[MARKER]]
141+
// CHECK-SIL-LABEL: } // end sil function 'apply_variadic_with_generic_instance'
142+
sil @apply_variadic_with_generic_instance : $<T>() -> () {
143+
entry:
144+
%take = function_ref @takeTypePack : $@convention(thin) <each T>() -> ()
145+
apply %take<Pack{G<T>}>() : $@convention(thin) <each T>() -> ()
146+
%retval = tuple ()
147+
return %retval : $()
148+
}
149+
109150
// =============================================================================
110151
// FINISH: Instructions: Apply }}
111152
// =============================================================================

0 commit comments

Comments
 (0)