Skip to content

[sil][typelowering] Rename the enum LoweringStyle => TypeExpansionKin… #14143

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
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
82 changes: 54 additions & 28 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,27 @@ class TypeLowering {
virtual void emitDestroyRValue(SILBuilder &B, SILLocation loc,
SILValue value) const = 0;

enum class LoweringStyle { Shallow, Deep };
/// When using "Lowered" APIs on a type lowering, how far should type lowering
/// expand a type into its subtypes when emitting an operation.
///
/// If we emit operations on a subtype of the type, we expand the type into
/// the subtypes and perform the requested operation at that level of the
/// type tree.
enum class TypeExpansionKind {
None, ///> Emit operations on the actual value.
DirectChildren, ///> Expand the value into its direct children and place
///> operations on the children.
MostDerivedDescendents, ///> Expand the value into its most derived
///> substypes and perform operations on these
///> types.
};

/// Given the result of the expansion heuristic,
/// return appropriate lowering style.
static LoweringStyle getLoweringStyle(bool shouldExpand) {
static TypeExpansionKind getLoweringStyle(bool shouldExpand) {
if (shouldExpand)
return TypeLowering::LoweringStyle::Deep;
return TypeLowering::LoweringStyle::Shallow;
return TypeLowering::TypeExpansionKind::MostDerivedDescendents;
return TypeLowering::TypeExpansionKind::DirectChildren;
}

//===--------------------------------------------------------------------===//
Expand All @@ -274,32 +287,39 @@ class TypeLowering {
/// Emit a lowered destroy value operation.
///
/// This type must be loadable.
virtual void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle loweringStyle) const = 0;
virtual void
emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
TypeExpansionKind loweringStyle) const = 0;

void emitLoweredDestroyChildValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle loweringStyle) const {
if (loweringStyle != LoweringStyle::Shallow)
return emitLoweredDestroyValue(B, loc, value, loweringStyle);
return emitDestroyValue(B, loc, value);
TypeExpansionKind loweringStyle) const {
switch (loweringStyle) {
case TypeExpansionKind::None:
llvm_unreachable("This does not apply to children of aggregate types");
case TypeExpansionKind::DirectChildren:
return emitDestroyValue(B, loc, value);
case TypeExpansionKind::MostDerivedDescendents:
return emitLoweredDestroyValueMostDerivedDescendents(B, loc, value);
}
}

/// Emit a lowered destroy value operation.
///
/// This type must be loadable.
void emitLoweredDestroyValueShallow(SILBuilder &B, SILLocation loc,
SILValue value) const {
emitLoweredDestroyValue(B, loc, value, LoweringStyle::Shallow);
void emitLoweredDestroyValueDirectChildren(SILBuilder &B, SILLocation loc,
SILValue value) const {
emitLoweredDestroyValue(B, loc, value, TypeExpansionKind::DirectChildren);
}

/// Emit a lowered destroy_value operation.
///
/// This type must be loadable.
void emitLoweredDestroyValueDeep(SILBuilder &B, SILLocation loc,
SILValue value) const {
emitLoweredDestroyValue(B, loc, value, LoweringStyle::Deep);
void emitLoweredDestroyValueMostDerivedDescendents(SILBuilder &B,
SILLocation loc,
SILValue value) const {
emitLoweredDestroyValue(B, loc, value,
TypeExpansionKind::MostDerivedDescendents);
}

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

/// Emit a lowered copy value operation.
///
/// This type must be loadable.
SILValue emitLoweredCopyValueShallow(SILBuilder &B, SILLocation loc,
SILValue value) const {
return emitLoweredCopyValue(B, loc, value, LoweringStyle::Shallow);
SILValue emitLoweredCopyValueDirectChildren(SILBuilder &B, SILLocation loc,
SILValue value) const {
return emitLoweredCopyValue(B, loc, value,
TypeExpansionKind::DirectChildren);
}

/// Emit a lowered copy value operation.
///
/// This type must be loadable.
SILValue emitLoweredCopyValueDeep(SILBuilder &B, SILLocation loc,
SILValue value) const {
return emitLoweredCopyValue(B, loc, value, LoweringStyle::Deep);
SILValue emitLoweredCopyValueMostDerivedDescendents(SILBuilder &B,
SILLocation loc,
SILValue value) const {
return emitLoweredCopyValue(B, loc, value,
TypeExpansionKind::MostDerivedDescendents);
}

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

SILValue emitLoweredCopyChildValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const {
if (style != LoweringStyle::Shallow) {
return emitLoweredCopyValue(B, loc, value, style);
} else {
TypeExpansionKind style) const {
switch (style) {
case TypeExpansionKind::None:
llvm_unreachable("This does not apply to children of aggregate");
case TypeExpansionKind::DirectChildren:
return emitCopyValue(B, loc, value);
case TypeExpansionKind::MostDerivedDescendents:
return emitLoweredCopyValueMostDerivedDescendents(B, loc, value);
}
}

Expand Down
67 changes: 37 additions & 30 deletions lib/SIL/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,15 @@ namespace {
// Trivial
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle loweringStyle) const override {
void
emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
TypeExpansionKind loweringStyle) const override {
// Trivial
}

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
// Trivial
return value;
}
Expand Down Expand Up @@ -778,7 +779,7 @@ namespace {

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue aggValue,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm::SmallVector<SILValue, 8> loweredChildValues;
for (auto &child : getChildren(B.getModule())) {
auto &childLowering = child.getLowering();
Expand Down Expand Up @@ -807,17 +808,19 @@ namespace {
B.emitReleaseValueAndFold(loc, aggValue);
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc,
SILValue aggValue,
LoweringStyle loweringStyle) const override {
void
emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue aggValue,
TypeExpansionKind loweringStyle) const override {
SimpleOperationTy Fn;

switch(loweringStyle) {
case LoweringStyle::Shallow:
case TypeExpansionKind::None:
return emitDestroyValue(B, loc, aggValue);
case TypeExpansionKind::DirectChildren:
Fn = &TypeLowering::emitDestroyValue;
break;
case LoweringStyle::Deep:
Fn = &TypeLowering::emitLoweredDestroyValueDeep;
case TypeExpansionKind::MostDerivedDescendents:
Fn = &TypeLowering::emitLoweredDestroyValueMostDerivedDescendents;
break;
}

Expand Down Expand Up @@ -911,7 +914,7 @@ namespace {

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
if (B.getFunction().hasQualifiedOwnership())
return B.createCopyValue(loc, value);
B.createRetainValue(loc, value, B.getDefaultAtomicity());
Expand All @@ -928,19 +931,23 @@ namespace {
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
if (style == LoweringStyle::Shallow) {
emitDestroyValue(B, loc, value);
return;
}
assert(style != LoweringStyle::Shallow &&
"This method should never be called when performing a shallow "
"destroy value.");
if (B.getFunction().hasQualifiedOwnership()) {
B.createDestroyValue(loc, value);
TypeExpansionKind style) const override {
// Enums, we never want to expand.
//
// TODO: This is a final class. I don't understand why we can't just
// delegate to emitDestroyValue in all cases.
switch (style) {
case TypeExpansionKind::None:
case TypeExpansionKind::DirectChildren:
return emitDestroyValue(B, loc, value);
case TypeExpansionKind::MostDerivedDescendents:
if (B.getFunction().hasQualifiedOwnership()) {
B.createDestroyValue(loc, value);
return;
}
B.emitReleaseValueAndFold(loc, value);
return;
}
B.emitReleaseValueAndFold(loc, value);
}
};

Expand All @@ -953,12 +960,12 @@ namespace {

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
return emitCopyValue(B, loc, value);
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
emitDestroyValue(B, loc, value);
}
};
Expand Down Expand Up @@ -1068,7 +1075,7 @@ namespace {

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm_unreachable("type is not loadable!");
}

Expand All @@ -1078,7 +1085,7 @@ namespace {
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm_unreachable("type is not loadable!");
}
};
Expand Down Expand Up @@ -1127,12 +1134,12 @@ namespace {

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm_unreachable("lowered copy");
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm_unreachable("destroy value");
}

Expand Down Expand Up @@ -1196,7 +1203,7 @@ namespace {

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm_unreachable("type is not loadable!");
}

Expand All @@ -1206,7 +1213,7 @@ namespace {
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
TypeExpansionKind style) const override {
llvm_unreachable("type is not loadable!");
}
};
Expand Down
7 changes: 3 additions & 4 deletions lib/SILOptimizer/Transforms/SILLowerAggregateInstrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ static bool expandReleaseValue(ReleaseValueInst *DV) {
return false;

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

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

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

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

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

Expand Down