Skip to content

Commit ca02cff

Browse files
Merge pull request #67684 from nate-chandler/opaque-values/20230801/5/keypath-setter-trivial-rvalue
[OpaqueValues] Fixes for key-path setters.
2 parents 4c560c0 + 0ae2c07 commit ca02cff

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,8 +3071,9 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30713071
auto baseArg = entry->createFunctionArgument(baseArgTy);
30723072
SILValue indexPtrArg;
30733073
if (!indexes.empty()) {
3074-
auto indexArgTy = signature->getParameters()[1].getSILStorageType(
3075-
SGM.M, signature, subSGF.F.getTypeExpansionContext());
3074+
auto indexArgTy =
3075+
subSGF.silConv.getSILType(signature->getParameters()[1], signature,
3076+
subSGF.F.getTypeExpansionContext());
30763077
indexPtrArg = entry->createFunctionArgument(indexArgTy);
30773078
}
30783079

@@ -3234,10 +3235,12 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32343235
subSGF.F.getTypeExpansionContext());
32353236

32363237
auto entry = thunk->begin();
3237-
auto valueArgTy = signature->getParameters()[0].getSILStorageType(
3238-
SGM.M, signature, subSGF.getTypeExpansionContext());
3239-
auto baseArgTy = signature->getParameters()[1].getSILStorageType(
3240-
SGM.M, signature, subSGF.getTypeExpansionContext());
3238+
auto valueArgTy =
3239+
subSGF.silConv.getSILType(signature->getParameters()[0], signature,
3240+
subSGF.getTypeExpansionContext());
3241+
auto baseArgTy =
3242+
subSGF.silConv.getSILType(signature->getParameters()[1], signature,
3243+
subSGF.getTypeExpansionContext());
32413244
if (genericEnv) {
32423245
valueArgTy = genericEnv->mapTypeIntoContext(SGM.M, valueArgTy);
32433246
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -3247,8 +3250,9 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32473250
SILValue indexPtrArg;
32483251

32493252
if (!indexes.empty()) {
3250-
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
3251-
SGM.M, signature, subSGF.getTypeExpansionContext());
3253+
auto indexArgTy =
3254+
subSGF.silConv.getSILType(signature->getParameters()[2], signature,
3255+
subSGF.getTypeExpansionContext());
32523256
indexPtrArg = entry->createFunctionArgument(indexArgTy);
32533257
}
32543258

@@ -3257,9 +3261,11 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32573261
auto subscriptIndices =
32583262
loadIndexValuesForKeyPathComponent(subSGF, loc, property,
32593263
indexes, indexPtrArg);
3260-
3261-
auto valueOrig = ManagedValue::forBorrowedRValue(valueArg)
3262-
.copy(subSGF, loc);
3264+
3265+
auto valueOrig = valueArgTy.isTrivial(subSGF.F)
3266+
? ManagedValue::forTrivialRValue(valueArg)
3267+
: ManagedValue::forBorrowedRValue(valueArg);
3268+
valueOrig = valueOrig.copy(subSGF, loc);
32633269
auto valueSubst = subSGF.emitOrigToSubstValue(loc, valueOrig,
32643270
AbstractionPattern::getOpaque(),
32653271
propertyType);
@@ -3414,10 +3420,10 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
34143420
SILGenFunction subSGF(SGM, *equals, SGM.SwiftModule);
34153421
equals->setGenericEnvironment(genericEnv);
34163422
auto entry = equals->begin();
3417-
auto lhsPtr = entry->createFunctionArgument(params[0].getSILStorageType(
3418-
SGM.M, signature, subSGF.getTypeExpansionContext()));
3419-
auto rhsPtr = entry->createFunctionArgument(params[1].getSILStorageType(
3420-
SGM.M, signature, subSGF.getTypeExpansionContext()));
3423+
auto lhsPtr = entry->createFunctionArgument(subSGF.silConv.getSILType(
3424+
params[0], signature, subSGF.getTypeExpansionContext()));
3425+
auto rhsPtr = entry->createFunctionArgument(subSGF.silConv.getSILType(
3426+
params[1], signature, subSGF.getTypeExpansionContext()));
34213427

34223428
Scope scope(subSGF, loc);
34233429

@@ -3588,8 +3594,8 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
35883594
SILGenFunction subSGF(SGM, *hash, SGM.SwiftModule);
35893595
hash->setGenericEnvironment(genericEnv);
35903596
auto entry = hash->begin();
3591-
auto indexPtr = entry->createFunctionArgument(params[0].getSILStorageType(
3592-
SGM.M, signature, subSGF.getTypeExpansionContext()));
3597+
auto indexPtr = entry->createFunctionArgument(subSGF.silConv.getSILType(
3598+
params[0], signature, subSGF.getTypeExpansionContext()));
35933599

35943600
SILValue hashCode;
35953601

test/SILGen/opaque_values_silgen.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,19 @@ public func backDeployingReturningGeneric<T>(_ t: T) throws -> T { t }
662662
func set<Container, Field>(into container: inout Container, at keyPath: WritableKeyPath<Container, Field>, _ value: Field) {
663663
container[keyPath: keyPath] = value
664664
}
665+
666+
// CHECK-LABEL: sil {{.*}}[ossa] @$s20opaque_values_silgen16FormClassKeyPathyyF1QL_C1qSivpADTk : {{.*}} {
667+
// CHECK: bb0([[VALUE:%[^,]+]] :
668+
// CHECK-SAME: [[CONTAINER:%[^,]+]] :
669+
// CHECK: [[CONTAINER_COPY:%[^,]+]] = copy_value [[CONTAINER]]
670+
// CHECK: [[SETTER:%[^,]+]] = class_method [[CONTAINER_COPY]]
671+
// CHECK: [[REGISTER_4:%[^,]+]] = apply [[SETTER]]([[VALUE]], [[CONTAINER_COPY]])
672+
// CHECK: destroy_value [[CONTAINER_COPY]]
673+
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen16FormClassKeyPathyyF1QL_C1qSivpADTk'
674+
@_silgen_name("FormClassKeyPath")
675+
func FormClassKeyPath() {
676+
class Q {
677+
var q: Int = 0
678+
}
679+
_ = \Q.q
680+
}

0 commit comments

Comments
 (0)