Skip to content

Commit 594760b

Browse files
authored
Merge pull request #14143 from gottesmm/pr-e157f770a9c37f5e04723d39871a9898c4632a3c
2 parents ff104e4 + d14024a commit 594760b

File tree

3 files changed

+94
-62
lines changed

3 files changed

+94
-62
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,27 @@ class TypeLowering {
257257
virtual void emitDestroyRValue(SILBuilder &B, SILLocation loc,
258258
SILValue value) const = 0;
259259

260-
enum class LoweringStyle { Shallow, Deep };
260+
/// When using "Lowered" APIs on a type lowering, how far should type lowering
261+
/// expand a type into its subtypes when emitting an operation.
262+
///
263+
/// If we emit operations on a subtype of the type, we expand the type into
264+
/// the subtypes and perform the requested operation at that level of the
265+
/// type tree.
266+
enum class TypeExpansionKind {
267+
None, ///> Emit operations on the actual value.
268+
DirectChildren, ///> Expand the value into its direct children and place
269+
///> operations on the children.
270+
MostDerivedDescendents, ///> Expand the value into its most derived
271+
///> substypes and perform operations on these
272+
///> types.
273+
};
261274

262275
/// Given the result of the expansion heuristic,
263276
/// return appropriate lowering style.
264-
static LoweringStyle getLoweringStyle(bool shouldExpand) {
277+
static TypeExpansionKind getLoweringStyle(bool shouldExpand) {
265278
if (shouldExpand)
266-
return TypeLowering::LoweringStyle::Deep;
267-
return TypeLowering::LoweringStyle::Shallow;
279+
return TypeLowering::TypeExpansionKind::MostDerivedDescendents;
280+
return TypeLowering::TypeExpansionKind::DirectChildren;
268281
}
269282

270283
//===--------------------------------------------------------------------===//
@@ -274,32 +287,39 @@ class TypeLowering {
274287
/// Emit a lowered destroy value operation.
275288
///
276289
/// This type must be loadable.
277-
virtual void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc,
278-
SILValue value,
279-
LoweringStyle loweringStyle) const = 0;
290+
virtual void
291+
emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
292+
TypeExpansionKind loweringStyle) const = 0;
280293

281294
void emitLoweredDestroyChildValue(SILBuilder &B, SILLocation loc,
282295
SILValue value,
283-
LoweringStyle loweringStyle) const {
284-
if (loweringStyle != LoweringStyle::Shallow)
285-
return emitLoweredDestroyValue(B, loc, value, loweringStyle);
286-
return emitDestroyValue(B, loc, value);
296+
TypeExpansionKind loweringStyle) const {
297+
switch (loweringStyle) {
298+
case TypeExpansionKind::None:
299+
llvm_unreachable("This does not apply to children of aggregate types");
300+
case TypeExpansionKind::DirectChildren:
301+
return emitDestroyValue(B, loc, value);
302+
case TypeExpansionKind::MostDerivedDescendents:
303+
return emitLoweredDestroyValueMostDerivedDescendents(B, loc, value);
304+
}
287305
}
288306

289307
/// Emit a lowered destroy value operation.
290308
///
291309
/// This type must be loadable.
292-
void emitLoweredDestroyValueShallow(SILBuilder &B, SILLocation loc,
293-
SILValue value) const {
294-
emitLoweredDestroyValue(B, loc, value, LoweringStyle::Shallow);
310+
void emitLoweredDestroyValueDirectChildren(SILBuilder &B, SILLocation loc,
311+
SILValue value) const {
312+
emitLoweredDestroyValue(B, loc, value, TypeExpansionKind::DirectChildren);
295313
}
296314

297315
/// Emit a lowered destroy_value operation.
298316
///
299317
/// This type must be loadable.
300-
void emitLoweredDestroyValueDeep(SILBuilder &B, SILLocation loc,
301-
SILValue value) const {
302-
emitLoweredDestroyValue(B, loc, value, LoweringStyle::Deep);
318+
void emitLoweredDestroyValueMostDerivedDescendents(SILBuilder &B,
319+
SILLocation loc,
320+
SILValue value) const {
321+
emitLoweredDestroyValue(B, loc, value,
322+
TypeExpansionKind::MostDerivedDescendents);
303323
}
304324

305325
/// Given a primitively loaded value of this type (which must be
@@ -322,22 +342,25 @@ class TypeLowering {
322342
/// This type must be loadable.
323343
virtual SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
324344
SILValue value,
325-
LoweringStyle style) const = 0;
345+
TypeExpansionKind style) const = 0;
326346

327347
/// Emit a lowered copy value operation.
328348
///
329349
/// This type must be loadable.
330-
SILValue emitLoweredCopyValueShallow(SILBuilder &B, SILLocation loc,
331-
SILValue value) const {
332-
return emitLoweredCopyValue(B, loc, value, LoweringStyle::Shallow);
350+
SILValue emitLoweredCopyValueDirectChildren(SILBuilder &B, SILLocation loc,
351+
SILValue value) const {
352+
return emitLoweredCopyValue(B, loc, value,
353+
TypeExpansionKind::DirectChildren);
333354
}
334355

335356
/// Emit a lowered copy value operation.
336357
///
337358
/// This type must be loadable.
338-
SILValue emitLoweredCopyValueDeep(SILBuilder &B, SILLocation loc,
339-
SILValue value) const {
340-
return emitLoweredCopyValue(B, loc, value, LoweringStyle::Deep);
359+
SILValue emitLoweredCopyValueMostDerivedDescendents(SILBuilder &B,
360+
SILLocation loc,
361+
SILValue value) const {
362+
return emitLoweredCopyValue(B, loc, value,
363+
TypeExpansionKind::MostDerivedDescendents);
341364
}
342365

343366
/// Given a primitively loaded value of this type (which must be
@@ -353,11 +376,14 @@ class TypeLowering {
353376

354377
SILValue emitLoweredCopyChildValue(SILBuilder &B, SILLocation loc,
355378
SILValue value,
356-
LoweringStyle style) const {
357-
if (style != LoweringStyle::Shallow) {
358-
return emitLoweredCopyValue(B, loc, value, style);
359-
} else {
379+
TypeExpansionKind style) const {
380+
switch (style) {
381+
case TypeExpansionKind::None:
382+
llvm_unreachable("This does not apply to children of aggregate");
383+
case TypeExpansionKind::DirectChildren:
360384
return emitCopyValue(B, loc, value);
385+
case TypeExpansionKind::MostDerivedDescendents:
386+
return emitLoweredCopyValueMostDerivedDescendents(B, loc, value);
361387
}
362388
}
363389

lib/SIL/TypeLowering.cpp

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,15 @@ namespace {
599599
// Trivial
600600
}
601601

602-
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
603-
LoweringStyle loweringStyle) const override {
602+
void
603+
emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
604+
TypeExpansionKind loweringStyle) const override {
604605
// Trivial
605606
}
606607

607608
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
608609
SILValue value,
609-
LoweringStyle style) const override {
610+
TypeExpansionKind style) const override {
610611
// Trivial
611612
return value;
612613
}
@@ -778,7 +779,7 @@ namespace {
778779

779780
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
780781
SILValue aggValue,
781-
LoweringStyle style) const override {
782+
TypeExpansionKind style) const override {
782783
llvm::SmallVector<SILValue, 8> loweredChildValues;
783784
for (auto &child : getChildren(B.getModule())) {
784785
auto &childLowering = child.getLowering();
@@ -807,17 +808,19 @@ namespace {
807808
B.emitReleaseValueAndFold(loc, aggValue);
808809
}
809810

810-
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc,
811-
SILValue aggValue,
812-
LoweringStyle loweringStyle) const override {
811+
void
812+
emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue aggValue,
813+
TypeExpansionKind loweringStyle) const override {
813814
SimpleOperationTy Fn;
814815

815816
switch(loweringStyle) {
816-
case LoweringStyle::Shallow:
817+
case TypeExpansionKind::None:
818+
return emitDestroyValue(B, loc, aggValue);
819+
case TypeExpansionKind::DirectChildren:
817820
Fn = &TypeLowering::emitDestroyValue;
818821
break;
819-
case LoweringStyle::Deep:
820-
Fn = &TypeLowering::emitLoweredDestroyValueDeep;
822+
case TypeExpansionKind::MostDerivedDescendents:
823+
Fn = &TypeLowering::emitLoweredDestroyValueMostDerivedDescendents;
821824
break;
822825
}
823826

@@ -911,7 +914,7 @@ namespace {
911914

912915
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
913916
SILValue value,
914-
LoweringStyle style) const override {
917+
TypeExpansionKind style) const override {
915918
if (B.getFunction().hasQualifiedOwnership())
916919
return B.createCopyValue(loc, value);
917920
B.createRetainValue(loc, value, B.getDefaultAtomicity());
@@ -928,19 +931,23 @@ namespace {
928931
}
929932

930933
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
931-
LoweringStyle style) const override {
932-
if (style == LoweringStyle::Shallow) {
933-
emitDestroyValue(B, loc, value);
934-
return;
935-
}
936-
assert(style != LoweringStyle::Shallow &&
937-
"This method should never be called when performing a shallow "
938-
"destroy value.");
939-
if (B.getFunction().hasQualifiedOwnership()) {
940-
B.createDestroyValue(loc, value);
934+
TypeExpansionKind style) const override {
935+
// Enums, we never want to expand.
936+
//
937+
// TODO: This is a final class. I don't understand why we can't just
938+
// delegate to emitDestroyValue in all cases.
939+
switch (style) {
940+
case TypeExpansionKind::None:
941+
case TypeExpansionKind::DirectChildren:
942+
return emitDestroyValue(B, loc, value);
943+
case TypeExpansionKind::MostDerivedDescendents:
944+
if (B.getFunction().hasQualifiedOwnership()) {
945+
B.createDestroyValue(loc, value);
946+
return;
947+
}
948+
B.emitReleaseValueAndFold(loc, value);
941949
return;
942950
}
943-
B.emitReleaseValueAndFold(loc, value);
944951
}
945952
};
946953

@@ -953,12 +960,12 @@ namespace {
953960

954961
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
955962
SILValue value,
956-
LoweringStyle style) const override {
963+
TypeExpansionKind style) const override {
957964
return emitCopyValue(B, loc, value);
958965
}
959966

960967
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
961-
LoweringStyle style) const override {
968+
TypeExpansionKind style) const override {
962969
emitDestroyValue(B, loc, value);
963970
}
964971
};
@@ -1068,7 +1075,7 @@ namespace {
10681075

10691076
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
10701077
SILValue value,
1071-
LoweringStyle style) const override {
1078+
TypeExpansionKind style) const override {
10721079
llvm_unreachable("type is not loadable!");
10731080
}
10741081

@@ -1078,7 +1085,7 @@ namespace {
10781085
}
10791086

10801087
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
1081-
LoweringStyle style) const override {
1088+
TypeExpansionKind style) const override {
10821089
llvm_unreachable("type is not loadable!");
10831090
}
10841091
};
@@ -1127,12 +1134,12 @@ namespace {
11271134

11281135
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
11291136
SILValue value,
1130-
LoweringStyle style) const override {
1137+
TypeExpansionKind style) const override {
11311138
llvm_unreachable("lowered copy");
11321139
}
11331140

11341141
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
1135-
LoweringStyle style) const override {
1142+
TypeExpansionKind style) const override {
11361143
llvm_unreachable("destroy value");
11371144
}
11381145

@@ -1196,7 +1203,7 @@ namespace {
11961203

11971204
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
11981205
SILValue value,
1199-
LoweringStyle style) const override {
1206+
TypeExpansionKind style) const override {
12001207
llvm_unreachable("type is not loadable!");
12011208
}
12021209

@@ -1206,7 +1213,7 @@ namespace {
12061213
}
12071214

12081215
void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
1209-
LoweringStyle style) const override {
1216+
TypeExpansionKind style) const override {
12101217
llvm_unreachable("type is not loadable!");
12111218
}
12121219
};

lib/SILOptimizer/Transforms/SILLowerAggregateInstrs.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ static bool expandReleaseValue(ReleaseValueInst *DV) {
181181
return false;
182182

183183
auto &TL = Module.getTypeLowering(Type);
184-
TL.emitLoweredDestroyValue(Builder, DV->getLoc(), Value,
185-
TypeLowering::LoweringStyle::Deep);
184+
TL.emitLoweredDestroyValueMostDerivedDescendents(Builder, DV->getLoc(),
185+
Value);
186186

187187
DEBUG(llvm::dbgs() << " Expanding Destroy Value: " << *DV);
188188

@@ -208,8 +208,7 @@ static bool expandRetainValue(RetainValueInst *CV) {
208208
return false;
209209

210210
auto &TL = Module.getTypeLowering(Type);
211-
TL.emitLoweredCopyValue(Builder, CV->getLoc(), Value,
212-
TypeLowering::LoweringStyle::Deep);
211+
TL.emitLoweredCopyValueMostDerivedDescendents(Builder, CV->getLoc(), Value);
213212

214213
DEBUG(llvm::dbgs() << " Expanding Copy Value: " << *CV);
215214

0 commit comments

Comments
 (0)