Skip to content

Commit 11a6aae

Browse files
committed
[SIL] DRY some code, to ensure method arguments can be __shared/@guaranteed too.
1 parent cbeacfd commit 11a6aae

File tree

2 files changed

+82
-34
lines changed

2 files changed

+82
-34
lines changed

lib/SIL/SILFunctionType.cpp

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -593,62 +593,55 @@ class DestructureInputs {
593593

594594
assert(numEltTypes > 0);
595595

596-
// If we don't have 'self', we don't need to do anything special.
597-
if (!extInfo.hasSelfParam() && !Foreign.Self.isImportAsMember()) {
598-
CanType ty = AnyFunctionType::composeInput(M.getASTContext(), params,
599-
/*canonicalVararg*/true)
600-
->getCanonicalType();
596+
auto handleParameter = [&](AbstractionPattern pattern,
597+
ParameterTypeFlags paramFlags, CanType ty) {
601598
CanTupleType tty = dyn_cast<TupleType>(ty);
602599
// If the abstraction pattern is opaque, and the tuple type is
603600
// materializable -- if it doesn't contain an l-value type -- then it's
604601
// a valid target for substitution and we should not expand it.
605-
if (!tty || (origType.isTypeParameter() && !tty->hasInOutElement())) {
606-
auto flags = (params.size() == 1)
607-
? params.front().getParameterFlags()
608-
: ParameterTypeFlags();
609-
if (flags.isShared()) {
610-
visitSharedType(origType, ty, extInfo.getSILRepresentation());
602+
auto silExtInfo = extInfo.getSILRepresentation();
603+
if (!tty || (pattern.isTypeParameter() && !tty->hasInOutElement())) {
604+
if (paramFlags.isShared()) {
605+
visitSharedType(pattern, ty, silExtInfo);
611606
} else {
612-
visit(origType, ty);
607+
visit(pattern, ty);
613608
}
614609
return;
615610
}
616611

617612
for (auto i : indices(tty.getElementTypes())) {
618-
if (tty->getElement(i).getParameterFlags().isShared()) {
619-
visitSharedType(origType.getTupleElementType(i),
620-
tty.getElementType(i),
621-
extInfo.getSILRepresentation());
613+
auto patternEltTy = pattern.getTupleElementType(i);
614+
auto trueEltTy = tty.getElementType(i);
615+
auto flags = tty->getElement(i).getParameterFlags();
616+
if (flags.isShared()) {
617+
visitSharedType(patternEltTy, trueEltTy, silExtInfo);
622618
} else {
623-
visit(origType.getTupleElementType(i), tty.getElementType(i));
619+
visit(patternEltTy, trueEltTy);
624620
}
625621
}
622+
};
623+
624+
// If we don't have 'self', we don't need to do anything special.
625+
if (!extInfo.hasSelfParam() && !Foreign.Self.isImportAsMember()) {
626+
CanType ty = AnyFunctionType::composeInput(M.getASTContext(), params,
627+
/*canonicalVararg*/true)
628+
->getCanonicalType();
629+
auto flags = (params.size() == 1) ? params.front().getParameterFlags()
630+
: ParameterTypeFlags();
631+
632+
handleParameter(origType, flags, ty);
626633
return;
627634
}
628635

629636
// Okay, handle 'self'.
630637

631638
// Process all the non-self parameters.
632639
for (unsigned i = 0; i != numNonSelfParams; ++i) {
633-
CanType ty = params[i].getType();
634-
CanTupleType tty = dyn_cast<TupleType>(ty);
640+
CanType ty = params[i].getType();
635641
AbstractionPattern eltPattern = origType.getTupleElementType(i);
636-
// If the abstraction pattern is opaque, and the tuple type is
637-
// materializable -- if it doesn't contain an l-value type -- then it's
638-
// a valid target for substitution and we should not expand it.
639-
if (!tty || (eltPattern.isTypeParameter() && !tty->hasInOutElement())) {
640-
if (params[i].isShared()) {
641-
visitSharedType(eltPattern, ty, extInfo.getSILRepresentation());
642-
} else {
643-
visit(eltPattern, ty);
644-
}
645-
continue;
646-
}
642+
auto flags = params[i].getParameterFlags();
647643

648-
assert(eltPattern.isTuple());
649-
for (unsigned j = 0; j < eltPattern.getNumTupleElements(); ++j) {
650-
visit(eltPattern.getTupleElementType(j), tty.getElementType(j));
651-
}
644+
handleParameter(eltPattern, flags, ty);
652645
}
653646

654647
// Process the self parameter. Note that we implicitly drop self

test/SILGen/shared.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,58 @@ func owned_to_shared_conversion(_ f : (Int, ValueAggregate, RefAggregate) -> Voi
124124

125125
// CHECK-LABEL: sil hidden @$S6shared0A17_closure_loweringyyySi_AA14ValueAggregateVAA03RefE0CtchF : $@convention(thin) (@guaranteed @callee_guaranteed (Int, @owned ValueAggregate, @owned RefAggregate) -> ()) -> ()
126126
func shared_closure_lowering(_ f : __shared (Int, ValueAggregate, RefAggregate) -> Void) {}
127+
128+
struct Foo {
129+
var x: ValueAggregate
130+
131+
// CHECK-LABEL: sil hidden @$S6shared3FooV21methodSharedArguments7trivial5value3refySih_AA14ValueAggregateVhAA03RefJ0ChtF : $@convention(method) (Int, @guaranteed ValueAggregate, @guaranteed RefAggregate, @guaranteed Foo) -> () {
132+
func methodSharedArguments(trivial : __shared Int, value : __shared ValueAggregate, ref : __shared RefAggregate) {
133+
// CHECK: bb0([[TRIVIAL_VAL:%[0-9]+]] : @trivial $Int, [[VALUE_VAL:%[0-9]+]] : @guaranteed $ValueAggregate, [[REF_VAL:%[0-9]+]] : @guaranteed $RefAggregate, [[SELF:%[0-9]+]] : @guaranteed $Foo):
134+
// CHECK: [[COPY_VALUE_VAL:%[0-9]+]] = copy_value [[VALUE_VAL]] : $ValueAggregate
135+
// CHECK: [[COPY_REF_VAL:%[0-9]+]] = copy_value [[REF_VAL]] : $RefAggregate
136+
// CHECK: [[OWNED_FUNC:%[0-9]+]] = function_ref @$S6shared3FooV20methodOwnedArguments7trivial5value3refySi_AA14ValueAggregateVAA03RefJ0CtF
137+
// CHECK: {{%.*}} = apply [[OWNED_FUNC]]([[TRIVIAL_VAL]], [[COPY_VALUE_VAL]], [[COPY_REF_VAL]], [[SELF]]) : $@convention(method) (Int, @owned ValueAggregate, @owned RefAggregate, @guaranteed Foo) -> ()
138+
// CHECK: } // end sil function '$S6shared3FooV21methodSharedArguments7trivial5value3refySih_AA14ValueAggregateVhAA03RefJ0ChtF'
139+
return methodOwnedArguments(trivial: trivial, value: value, ref: ref)
140+
}
141+
142+
// CHECK-LABEL: sil hidden @$S6shared3FooV20methodOwnedArguments7trivial5value3refySi_AA14ValueAggregateVAA03RefJ0CtF : $@convention(method) (Int, @owned ValueAggregate, @owned RefAggregate, @guaranteed Foo) -> () {
143+
func methodOwnedArguments(trivial : Int, value : ValueAggregate, ref : RefAggregate) {
144+
// CHECK: bb0([[TRIVIAL_VAL:%[0-9]+]] : @trivial $Int, [[VALUE_VAL:%[0-9]+]] : @owned $ValueAggregate, [[REF_VAL:%[0-9]+]] : @owned $RefAggregate, [[SELF:%[0-9]+]] : @guaranteed $Foo):
145+
// CHECK: [[BORROW_VALUE_VAL:%[0-9]+]] = begin_borrow [[VALUE_VAL]] : $ValueAggregate
146+
// CHECK: [[BORROW_REF_VAL:%[0-9]+]] = begin_borrow [[REF_VAL]] : $RefAggregate
147+
// CHECK: [[SHARED_FUNC:%[0-9]+]] = function_ref @$S6shared3FooV21methodSharedArguments7trivial5value3refySih_AA14ValueAggregateVhAA03RefJ0ChtF
148+
// CHECK: {{%.*}} = apply [[SHARED_FUNC]]([[TRIVIAL_VAL]], [[BORROW_VALUE_VAL]], [[BORROW_REF_VAL]], [[SELF]])
149+
// CHECK: end_borrow [[BORROW_REF_VAL]] from [[REF_VAL]] : $RefAggregate, $RefAggregate
150+
// CHECK: end_borrow [[BORROW_VALUE_VAL]] from [[VALUE_VAL]] : $ValueAggregate, $ValueAggregate
151+
// CHECK: destroy_value [[REF_VAL]] : $RefAggregate
152+
// CHECK: destroy_value [[VALUE_VAL]] : $ValueAggregate
153+
// CHECK: } // end sil function '$S6shared3FooV20methodOwnedArguments7trivial5value3refySi_AA14ValueAggregateVAA03RefJ0CtF'
154+
return methodSharedArguments(trivial: trivial, value: value, ref: ref)
155+
}
156+
157+
// CHECK-LABEL: sil hidden @$S6shared3FooV21staticSharedArguments7trivial5value3refySih_AA14ValueAggregateVhAA03RefJ0ChtFZ : $@convention(method) (Int, @guaranteed ValueAggregate, @guaranteed RefAggregate, @thin Foo.Type) -> () {
158+
static func staticSharedArguments(trivial : __shared Int, value : __shared ValueAggregate, ref : __shared RefAggregate) {
159+
// CHECK: bb0([[TRIVIAL_VAL:%[0-9]+]] : @trivial $Int, [[VALUE_VAL:%[0-9]+]] : @guaranteed $ValueAggregate, [[REF_VAL:%[0-9]+]] : @guaranteed $RefAggregate, [[SELF_METATYPE:%[0-9]+]] : @trivial $@thin Foo.Type):
160+
// CHECK: [[COPY_VALUE_VAL:%[0-9]+]] = copy_value [[VALUE_VAL]] : $ValueAggregate
161+
// CHECK: [[COPY_REF_VAL:%[0-9]+]] = copy_value [[REF_VAL]] : $RefAggregate
162+
// CHECK: [[OWNED_FUNC:%[0-9]+]] = function_ref @$S6shared3FooV20staticOwnedArguments7trivial5value3refySi_AA14ValueAggregateVAA03RefJ0CtFZ
163+
// CHECK: {{%.*}} = apply [[OWNED_FUNC]]([[TRIVIAL_VAL]], [[COPY_VALUE_VAL]], [[COPY_REF_VAL]], [[SELF_METATYPE]]) : $@convention(method) (Int, @owned ValueAggregate, @owned RefAggregate, @thin Foo.Type) -> ()
164+
// CHECK: } // end sil function '$S6shared3FooV21staticSharedArguments7trivial5value3refySih_AA14ValueAggregateVhAA03RefJ0ChtFZ'
165+
return staticOwnedArguments(trivial: trivial, value: value, ref: ref)
166+
}
167+
// CHECK-LABEL: sil hidden @$S6shared3FooV20staticOwnedArguments7trivial5value3refySi_AA14ValueAggregateVAA03RefJ0CtFZ : $@convention(method) (Int, @owned ValueAggregate, @owned RefAggregate, @thin Foo.Type) -> () {
168+
static func staticOwnedArguments(trivial : Int, value : ValueAggregate, ref : RefAggregate) {
169+
// CHECK: bb0([[TRIVIAL_VAL:%[0-9]+]] : @trivial $Int, [[VALUE_VAL:%[0-9]+]] : @owned $ValueAggregate, [[REF_VAL:%[0-9]+]] : @owned $RefAggregate, [[SELF_METATYPE:%[0-9]+]] : @trivial $@thin Foo.Type):
170+
// CHECK: [[BORROW_VALUE_VAL:%[0-9]+]] = begin_borrow [[VALUE_VAL]] : $ValueAggregate
171+
// CHECK: [[BORROW_REF_VAL:%[0-9]+]] = begin_borrow [[REF_VAL]] : $RefAggregate
172+
// CHECK: [[SHARED_FUNC:%[0-9]+]] = function_ref @$S6shared3FooV21staticSharedArguments7trivial5value3refySih_AA14ValueAggregateVhAA03RefJ0ChtFZ
173+
// CHECK: {{%.*}} = apply [[SHARED_FUNC]]([[TRIVIAL_VAL]], [[BORROW_VALUE_VAL]], [[BORROW_REF_VAL]], [[SELF_METATYPE]])
174+
// CHECK: end_borrow [[BORROW_REF_VAL]] from [[REF_VAL]] : $RefAggregate, $RefAggregate
175+
// CHECK: end_borrow [[BORROW_VALUE_VAL]] from [[VALUE_VAL]] : $ValueAggregate, $ValueAggregate
176+
// CHECK: destroy_value [[REF_VAL]] : $RefAggregate
177+
// CHECK: destroy_value [[VALUE_VAL]] : $ValueAggregate
178+
// CHECK: } // end sil function '$S6shared3FooV20staticOwnedArguments7trivial5value3refySi_AA14ValueAggregateVAA03RefJ0CtFZ'
179+
return staticSharedArguments(trivial: trivial, value: value, ref: ref)
180+
}
181+
}

0 commit comments

Comments
 (0)