Skip to content

Commit 7534c06

Browse files
authored
Merge pull request #58861 from meg-gupta/silgenfixes
Some SILGen fixes for opaque values
2 parents 0b20f21 + 3480eec commit 7534c06

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,15 +3962,20 @@ SILGenFunction::emitBeginApply(SILLocation loc, ManagedValue fn,
39623962
// Manage all the yielded values.
39633963
auto yieldInfos = substFnType->getYields();
39643964
assert(yieldValues.size() == yieldInfos.size());
3965+
bool useLoweredAddresses = silConv.useLoweredAddresses();
39653966
for (auto i : indices(yieldValues)) {
39663967
auto value = yieldValues[i];
39673968
auto info = yieldInfos[i];
39683969
if (info.isIndirectInOut()) {
39693970
yields.push_back(ManagedValue::forLValue(value));
39703971
} else if (info.isConsumed()) {
3971-
yields.push_back(emitManagedRValueWithCleanup(value));
3972+
!useLoweredAddresses && value->getType().isTrivial(getFunction())
3973+
? yields.push_back(ManagedValue::forTrivialRValue(value))
3974+
: yields.push_back(emitManagedRValueWithCleanup(value));
39723975
} else if (info.isGuaranteed()) {
3973-
yields.push_back(ManagedValue::forBorrowedRValue(value));
3976+
!useLoweredAddresses && value->getType().isTrivial(getFunction())
3977+
? yields.push_back(ManagedValue::forTrivialRValue(value))
3978+
: yields.push_back(ManagedValue::forBorrowedRValue(value));
39743979
} else {
39753980
yields.push_back(ManagedValue::forTrivialRValue(value));
39763981
}

lib/SILGen/SILGenExpr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,11 +1941,14 @@ RValue RValueEmitter::visitAnyHashableErasureExpr(AnyHashableErasureExpr *E,
19411941
SGFContext C) {
19421942
// Emit the source value into a temporary.
19431943
auto sourceOrigType = AbstractionPattern::getOpaque();
1944+
auto subExpr = E->getSubExpr();
1945+
auto &sourceOrigTL = SGF.getTypeLowering(sourceOrigType, subExpr->getType());
19441946
auto source =
1945-
SGF.emitMaterializedRValueAsOrig(E->getSubExpr(), sourceOrigType);
1947+
SGF.silConv.useLoweredAddresses()
1948+
? SGF.emitMaterializedRValueAsOrig(subExpr, sourceOrigType)
1949+
: SGF.emitRValueAsOrig(subExpr, sourceOrigType, sourceOrigTL, C);
19461950

1947-
return SGF.emitAnyHashableErasure(E, source,
1948-
E->getSubExpr()->getType(),
1951+
return SGF.emitAnyHashableErasure(E, source, subExpr->getType(),
19491952
E->getConformance(), C);
19501953
}
19511954

lib/SILGen/SILGenPattern.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,7 +2850,11 @@ void SILGenFunction::emitSwitchStmt(SwitchStmt *S) {
28502850
}
28512851

28522852
// If then we have an object, return it at +0.
2853+
// For opaque values, return at +1
28532854
if (subjectMV.getType().isObject()) {
2855+
if (subjectMV.getType().isAddressOnly(F)) {
2856+
return {subjectMV.copy(*this, S), CastConsumptionKind::TakeAlways};
2857+
}
28542858
return {subjectMV, CastConsumptionKind::BorrowAlways};
28552859
}
28562860

test/SILGen/opaque_values_silgen.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,20 @@ class TestGeneric<T> {
461461
// CHECK: unwind
462462
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen11TestGenericC08borrowedE0xvr'
463463

464+
// CHECK-LABEL: sil hidden [ossa] @$s20opaque_values_silgen8TestEnumO5_codeSivg :
465+
// CHECK: bb0(%0 : @guaranteed $TestEnum<T>):
466+
// CHECK: [[COPY:%.*]] = copy_value %0 : $TestEnum<T>
467+
// CHECK: switch_enum [[COPY]] : $TestEnum<T>, case #TestEnum.invalidValue!enumelt: bb1
468+
// CHECK: bb1([[VAL:%.*]] : @owned $Any):
469+
// CHECK: destroy_value [[VAL]] : $Any
470+
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen8TestEnumO5_codeSivg'
471+
enum TestEnum<T> {
472+
case invalidValue(Any)
473+
474+
var _code: Int {
475+
switch self {
476+
case .invalidValue: return 4866
477+
}
478+
}
479+
}
480+

0 commit comments

Comments
 (0)