Skip to content

Commit 9e8fdda

Browse files
authored
Merge pull request #61029 from kavon/assign-preconcurrency-followup-logicalAttempt
ABISafeConversionComponent should be a TranslationComponent
2 parents 62d99fb + bcc7541 commit 9e8fdda

File tree

4 files changed

+193
-19
lines changed

4 files changed

+193
-19
lines changed

lib/SILGen/LValue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class PathComponent {
105105
CoroutineAccessorKind, // coroutine accessor
106106
ValueKind, // random base pointer as an lvalue
107107
PhysicalKeyPathApplicationKind, // applying a key path
108-
ABISafeConversionKind, // unchecked_addr_cast
109108

110109
// Logical LValue kinds
111110
GetterSetterKind, // property or subscript getter/setter
@@ -118,6 +117,7 @@ class PathComponent {
118117
// Translation LValue kinds (a subtype of logical)
119118
OrigToSubstKind, // generic type substitution
120119
SubstToOrigKind, // generic type substitution
120+
UncheckedConversionKind, // unchecked_X_cast
121121

122122
FirstLogicalKind = GetterSetterKind,
123123
FirstTranslationKind = OrigToSubstKind,

lib/SILGen/SILGenBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ ManagedValue SILGenBuilder::createUncheckedBitCast(SILLocation loc,
625625
// updated.
626626
assert((isa<UncheckedTrivialBitCastInst>(cast) ||
627627
isa<UncheckedRefCastInst>(cast) ||
628-
isa<UncheckedBitwiseCastInst>(cast)) &&
628+
isa<UncheckedBitwiseCastInst>(cast) ||
629+
isa<ConvertFunctionInst>(cast)) &&
629630
"SILGenBuilder is out of sync with SILBuilder.");
630631

631632
// If we have a trivial inst, just return early.

lib/SILGen/SILGenLValue.cpp

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,26 +2198,82 @@ namespace {
21982198
}
21992199
};
22002200

2201-
/// A physical component which performs an unchecked_addr_cast
2202-
class ABISafeConversionComponent final : public PhysicalPathComponent {
2201+
/// A translation component that performs \c unchecked_*_cast 's as-needed.
2202+
class UncheckedConversionComponent final : public TranslationPathComponent {
2203+
private:
2204+
Type OrigType;
2205+
2206+
/// \returns the type this component is trying to convert \b to
2207+
CanType getTranslatedType() const {
2208+
return getTypeData().SubstFormalType->getCanonicalType();
2209+
}
2210+
2211+
/// \returns the type this component is trying to convert \b from
2212+
CanType getUntranslatedType() const {
2213+
return OrigType->getRValueType()->getCanonicalType();
2214+
}
2215+
2216+
/// perform a conversion of ManagedValue -> ManagedValue
2217+
ManagedValue doUncheckedConversion(SILGenFunction &SGF, SILLocation loc,
2218+
ManagedValue val, CanType toType) {
2219+
auto toTy = SGF.getLoweredType(toType);
2220+
auto fromTy = val.getType();
2221+
2222+
if (fromTy == toTy)
2223+
return val; // nothing to do.
2224+
2225+
// otherwise emit the right kind of cast based on whether it's an address.
2226+
assert(fromTy.isAddress() == toTy.isAddress());
2227+
2228+
if (toTy.isAddress())
2229+
return SGF.B.createUncheckedAddrCast(loc, val, toTy);
2230+
2231+
return SGF.B.createUncheckedBitCast(loc, val, toTy);
2232+
}
2233+
2234+
/// perform a conversion of RValue -> RValue
2235+
RValue doUncheckedConversion(SILGenFunction &SGF, SILLocation loc,
2236+
RValue &&rv, CanType toType) {
2237+
auto val = std::move(rv).getAsSingleValue(SGF, loc);
2238+
val = doUncheckedConversion(SGF, loc, val, toType);
2239+
return RValue(SGF, loc, toType, val);
2240+
}
2241+
22032242
public:
2204-
ABISafeConversionComponent(LValueTypeData typeData)
2205-
: PhysicalPathComponent(typeData, ABISafeConversionKind,
2206-
/*actorIsolation=*/None) {}
2243+
/// \param OrigType is the type we are converting \b from
2244+
/// \param typeData will contain the type we are converting \b to
2245+
UncheckedConversionComponent(LValueTypeData typeData, Type OrigType)
2246+
: TranslationPathComponent(typeData, UncheckedConversionKind),
2247+
OrigType(OrigType) {}
22072248

2208-
ManagedValue project(SILGenFunction &SGF, SILLocation loc,
2209-
ManagedValue base) && override {
2210-
auto toType = SGF.getLoweredType(getTypeData().SubstFormalType)
2211-
.getAddressType();
2249+
bool isLoadingPure() const override { return true; }
22122250

2213-
if (base.getType() == toType)
2214-
return base; // nothing to do
2251+
/// Used during write operations to convert the value prior to writing to
2252+
/// the base.
2253+
RValue untranslate(SILGenFunction &SGF, SILLocation loc,
2254+
RValue &&rv, SGFContext c) && override {
2255+
return doUncheckedConversion(SGF, loc, std::move(rv),
2256+
getUntranslatedType());
2257+
}
22152258

2216-
return SGF.B.createUncheckedAddrCast(loc, base, toType);
2259+
/// Used during read operations to convert the value after reading the base.
2260+
RValue translate(SILGenFunction &SGF, SILLocation loc,
2261+
RValue &&rv, SGFContext c) && override {
2262+
return doUncheckedConversion(SGF, loc, std::move(rv),
2263+
getTranslatedType());
2264+
}
2265+
2266+
std::unique_ptr<LogicalPathComponent>
2267+
clone(SILGenFunction &SGF, SILLocation loc) const override {
2268+
return std::make_unique<UncheckedConversionComponent>(getTypeData(),
2269+
OrigType);
22172270
}
22182271

22192272
void dump(raw_ostream &OS, unsigned indent) const override {
2220-
OS.indent(indent) << "ABISafeConversionComponent\n";
2273+
OS.indent(indent) << "UncheckedConversionComponent"
2274+
<< "\n\tfromType: " << getUntranslatedType()
2275+
<< "\n\ttoType: " << getTranslatedType()
2276+
<< "\n";
22212277
}
22222278
};
22232279
} // end anonymous namespace
@@ -3756,7 +3812,9 @@ LValue SILGenLValue::visitABISafeConversionExpr(ABISafeConversionExpr *e,
37563812
LValue lval = visitRec(e->getSubExpr(), accessKind, options);
37573813
auto typeData = getValueTypeData(SGF, accessKind, e);
37583814

3759-
lval.add<ABISafeConversionComponent>(typeData);
3815+
auto OrigType = e->getSubExpr()->getType();
3816+
3817+
lval.add<UncheckedConversionComponent>(typeData, OrigType);
37603818

37613819
return lval;
37623820
}

test/SILGen/objc_preconcurrency.swift

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
@preconcurrency var sendyHandler: @Sendable () -> Void { get set }
88
}
99

10+
@preconcurrency class OldWorld {
11+
@preconcurrency var handler: (@Sendable () -> Void)?
12+
@preconcurrency var mainHandler: (@MainActor () -> Void)?
13+
@preconcurrency var nonOptionalHandler: @Sendable () -> Void = {}
14+
@preconcurrency var nonOptionalMainHandler: @MainActor () -> Void = {}
15+
}
16+
1017
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency19testDynamicDispatch1p17completionHandleryAA1P_p_yyctF
1118
// CHECK: dynamic_method_br
1219
// CHECK: bb{{[0-9]+}}(%{{[0-9]+}} : $@convention(objc_method) (@convention(block) @Sendable () -> (), @opened
@@ -19,26 +26,134 @@ func testDynamicDispatch(p: P, completionHandler: @escaping () -> Void) {
1926
}
2027

2128
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency21testOptionalVarAccessyySo12NSTouchGrassCF
22-
// CHECK: unchecked_addr_cast {{.*}} : $*Optional<@Sendable @callee_guaranteed () -> ()> to $*Optional<@callee_guaranteed () -> ()>
29+
// CHECK: unchecked_bitwise_cast {{.*}} : $Optional<@Sendable @callee_guaranteed () -> ()> to $Optional<@callee_guaranteed () -> ()>
2330
// CHECK: } // end sil function '$s19objc_preconcurrency21testOptionalVarAccessyySo12NSTouchGrassCF'
2431
func testOptionalVarAccess(_ grass: NSTouchGrass) {
2532
grass.cancellationHandler?()
2633
}
2734

35+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency33testOptionalVarAccessPartialApplyyyycSgSo12NSTouchGrassCF
36+
// CHECK: unchecked_bitwise_cast {{.*}} : $Optional<@Sendable @callee_guaranteed () -> ()> to $Optional<@callee_guaranteed () -> ()>
37+
// CHECK: } // end sil function '$s19objc_preconcurrency33testOptionalVarAccessPartialApplyyyycSgSo12NSTouchGrassCF'
38+
func testOptionalVarAccessPartialApply(_ grass: NSTouchGrass) -> (() -> Void)? {
39+
let handler = grass.cancellationHandler
40+
if let unwrapped = handler {
41+
unwrapped()
42+
}
43+
return handler
44+
}
45+
46+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency16testObjCVarWriteyySo12NSTouchGrassCF
47+
// CHECK: unchecked_bitwise_cast {{.*}} : $Optional<@callee_guaranteed () -> ()> to $Optional<@Sendable @callee_guaranteed () -> ()>
48+
// CHECK: objc_method {{.*}} : $NSTouchGrass, #NSTouchGrass.cancellationHandler!setter.foreign : (NSTouchGrass) -> ((@Sendable () -> ())?) -> (), $@convention(objc_method) (Optional<@convention(block) @Sendable () -> ()>, NSTouchGrass) -> ()
49+
// CHECK: } // end sil function '$s19objc_preconcurrency16testObjCVarWriteyySo12NSTouchGrassCF'
50+
func testObjCVarWrite(_ grass: NSTouchGrass) {
51+
grass.cancellationHandler = {}
52+
}
53+
54+
// the below looks kinda long and wonky, but is expected. for a summary, the steps are:
55+
// 1. objc to native
56+
// 2. Sendable to non-Sendable (major part of this test)
57+
// 3. non-optional to optional
58+
// 4. from non-Sendable to Sendable (major part of this test)
59+
// 5. from native to objc (which involves unwrapping and rewrapping that optional; kinda silly but optimization will clean it up)
60+
//
61+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency22testObjCVarWriteAcrossyySo12NSTouchGrassCF
62+
// CHECK: [[GET_EXCEPTION:%[0-9]+]] = objc_method {{.*}} : $NSTouchGrass, #NSTouchGrass.exceptionHandler!getter.foreign
63+
// CHECK: [[SENDABLE_BLOCK:%[0-9]+]] = apply [[GET_EXCEPTION]]({{.*}}) : $@convention(objc_method) (NSTouchGrass) -> @autoreleased @convention(block) @Sendable () -> ()
64+
// << step 1 >>
65+
// CHECK: [[NATIVE_THUNK:%[0-9]+]] = function_ref @$sIeyBh_Iegh_TR : $@convention(thin) @Sendable (@guaranteed @convention(block) @Sendable () -> ()) -> ()
66+
// CHECK: [[NATIVE_SENDABLE_EXCEPTION:%[0-9]+]] = partial_apply [callee_guaranteed] [[NATIVE_THUNK]]([[SENDABLE_BLOCK]])
67+
// << step 2 >>
68+
// CHECK: [[NATIVE_EXCEPTION:%[0-9]+]] = convert_function [[NATIVE_SENDABLE_EXCEPTION]] : $@Sendable @callee_guaranteed () -> () to $@callee_guaranteed () -> ()
69+
// << step 3 >>
70+
// CHECK: [[OPTIONAL_NATIVE_EXCEPTION:%[0-9]+]] = enum $Optional<@callee_guaranteed () -> ()>, #Optional.some!enumelt, [[NATIVE_EXCEPTION]] : $@callee_guaranteed () -> ()
71+
// << step 4 >>
72+
// CHECK: = unchecked_bitwise_cast [[OPTIONAL_NATIVE_EXCEPTION]] : $Optional<@callee_guaranteed () -> ()> to $Optional<@Sendable @callee_guaranteed () -> ()>
73+
// << step 5 >>
74+
// CHECK: switch_enum {{.*}} : $Optional<@Sendable @callee_guaranteed () -> ()>
75+
//
76+
// CHECK: bb1({{.*}} : @owned $@Sendable @callee_guaranteed () -> ()):
77+
// CHECK: init_block_storage_header {{.*}} : $*@block_storage @Sendable @callee_guaranteed () -> ()
78+
// CHECK: } // end sil function '$s19objc_preconcurrency22testObjCVarWriteAcrossyySo12NSTouchGrassCF'
79+
func testObjCVarWriteAcross(_ grass: NSTouchGrass) {
80+
grass.cancellationHandler = grass.exceptionHandler // *slaps roof of assignment* this bad boy can fit so much conversion in it!
81+
}
82+
83+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency25testOptionalAssignSetter1yyAA8OldWorldCF
84+
// CHECK: unchecked_bitwise_cast {{.*}} : $Optional<@callee_guaranteed () -> ()> to $Optional<@Sendable @callee_guaranteed () -> ()>
85+
// CHECK: #OldWorld.handler!setter : (OldWorld) -> ((@Sendable () -> ())?) -> ()
86+
// CHECK: } // end sil function '$s19objc_preconcurrency25testOptionalAssignSetter1yyAA8OldWorldCF'
87+
func testOptionalAssignSetter1(_ oldWorld: OldWorld) {
88+
oldWorld.handler = {}
89+
}
90+
91+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency25testOptionalAssignSetter2yyAA8OldWorldCF
92+
// CHECK: convert_function {{.*}} : $@callee_guaranteed () -> () to $@Sendable @callee_guaranteed () -> ()
93+
// CHECK: $OldWorld, #OldWorld.nonOptionalHandler!setter : (OldWorld) -> (@escaping @Sendable () -> ()) -> ()
94+
// CHECK: } // end sil function '$s19objc_preconcurrency25testOptionalAssignSetter2yyAA8OldWorldCF'
95+
func testOptionalAssignSetter2(_ oldWorld: OldWorld) {
96+
oldWorld.nonOptionalHandler = {}
97+
}
98+
99+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency21testMainHandlerWritesyyAA8OldWorldCF
100+
// CHECK: = unchecked_bitwise_cast {{.*}} : $Optional<@callee_guaranteed () -> ()> to $Optional<@Sendable @callee_guaranteed () -> ()>
101+
// CHECK: = unchecked_bitwise_cast {{.*}} : $Optional<@Sendable @callee_guaranteed () -> ()> to $Optional<@callee_guaranteed () -> ()>
102+
// CHECK: } // end sil function '$s19objc_preconcurrency21testMainHandlerWritesyyAA8OldWorldCF'
103+
func testMainHandlerWrites(_ oldWorld: OldWorld) {
104+
oldWorld.handler = oldWorld.mainHandler
105+
oldWorld.mainHandler = oldWorld.handler
106+
}
107+
108+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency32testMainHandlerNonOptionalWritesyyAA8OldWorldCF
109+
// CHECK: = convert_function {{.*}} : $@callee_guaranteed () -> () to $@Sendable @callee_guaranteed () -> ()
110+
// CHECK: = convert_function {{.*}} : $@Sendable @callee_guaranteed () -> () to $@callee_guaranteed () -> ()
111+
// CHECK: } // end sil function '$s19objc_preconcurrency32testMainHandlerNonOptionalWritesyyAA8OldWorldCF'
112+
func testMainHandlerNonOptionalWrites(_ oldWorld: OldWorld) {
113+
oldWorld.nonOptionalHandler = oldWorld.nonOptionalMainHandler
114+
oldWorld.nonOptionalMainHandler = oldWorld.nonOptionalHandler
115+
}
116+
117+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency15testMixedWritesyyAA8OldWorldCF
118+
//
119+
// << sendable conversions should be here >>
120+
// CHECK: = unchecked_bitwise_cast {{.*}} : $Optional<@Sendable @callee_guaranteed () -> ()> to $Optional<@callee_guaranteed () -> ()>
121+
// CHECK: = convert_function {{.*}} : $@callee_guaranteed () -> () to $@Sendable @callee_guaranteed () -> ()
122+
//
123+
// << but main actor type mismatches are accepted by SIL >>
124+
// CHECK: [[NO_MAIN_ACTOR:%[0-9]+]] = partial_apply {{.*}} : $@convention(thin) (@guaranteed @callee_guaranteed () -> @out ()) -> ()
125+
// CHECK: [[SETTER:%[0-9]+]] = class_method {{.*}} : $OldWorld, #OldWorld.nonOptionalMainHandler!setter : (OldWorld) -> (@escaping @MainActor () -> ()) -> (), $@convention(method) (@owned @callee_guaranteed () -> (), @guaranteed OldWorld) -> ()
126+
// CHECK: apply [[SETTER]]([[NO_MAIN_ACTOR]]
127+
// CHECK: } // end sil function '$s19objc_preconcurrency15testMixedWritesyyAA8OldWorldCF'
128+
func testMixedWrites(_ oldWorld: OldWorld) {
129+
oldWorld.nonOptionalHandler = oldWorld.handler ?? {}
130+
oldWorld.nonOptionalMainHandler = oldWorld.mainHandler ?? {}
131+
}
132+
28133
func modify(_ v: inout () -> Void) {
29134
v = {}
30135
}
31136

32137
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency15testInoutAccessyySo12NSTouchGrassCF
33-
// CHECK: unchecked_addr_cast {{.*}} : $*@Sendable @callee_guaranteed () -> () to $*@callee_guaranteed () -> ()
138+
// CHECK: [[BEFORE_MODIFY:%[0-9]+]] = convert_function {{.*}} : $@Sendable @callee_guaranteed () -> () to $@callee_guaranteed () -> ()
139+
// CHECK: store [[BEFORE_MODIFY]] to [init] [[INOUT_ALLOC:%[0-9]+]] : $*@callee_guaranteed () -> ()
140+
// CHECK: [[MODIFY_FN:%[0-9]+]] = function_ref @$s19objc_preconcurrency6modifyyyyyczF : $@convention(thin) (@inout @callee_guaranteed () -> ()) -> ()
141+
// CHECK: = apply [[MODIFY_FN]]([[INOUT_ALLOC]])
142+
// CHECK: [[AFTER_MODIFY:%[0-9]+]] = load [take] [[INOUT_ALLOC]] : $*@callee_guaranteed () -> ()
143+
// CHECK: convert_function [[AFTER_MODIFY]] : $@callee_guaranteed () -> () to $@Sendable @callee_guaranteed () -> ()
34144
// CHECK: } // end sil function '$s19objc_preconcurrency15testInoutAccessyySo12NSTouchGrassCF'
35145
func testInoutAccess(_ grass: NSTouchGrass) {
36146
modify(&grass.exceptionHandler)
37147
}
38148

39149

40150
// CHECK-LABEL: sil hidden [ossa] @$s19objc_preconcurrency21testProtocolVarAccess1pyAA1P_p_tF
41-
// CHECK: unchecked_addr_cast {{.*}} : $*@Sendable @callee_guaranteed () -> () to $*@callee_guaranteed () -> ()
151+
// CHECK: [[BEFORE_MODIFY:%[0-9]+]] = convert_function {{.*}} : $@Sendable @callee_guaranteed () -> () to $@callee_guaranteed () -> ()
152+
// CHECK: store [[BEFORE_MODIFY]] to [init] [[INOUT_ALLOC:%[0-9]+]] : $*@callee_guaranteed () -> ()
153+
// CHECK: [[MODIFY_FN:%[0-9]+]] = function_ref @$s19objc_preconcurrency6modifyyyyyczF : $@convention(thin) (@inout @callee_guaranteed () -> ()) -> ()
154+
// CHECK: = apply [[MODIFY_FN]]([[INOUT_ALLOC]])
155+
// CHECK: [[AFTER_MODIFY:%[0-9]+]] = load [take] [[INOUT_ALLOC]] : $*@callee_guaranteed () -> ()
156+
// CHECK: convert_function [[AFTER_MODIFY]] : $@callee_guaranteed () -> () to $@Sendable @callee_guaranteed () -> ()
42157
// CHECK: } // end sil function '$s19objc_preconcurrency21testProtocolVarAccess1pyAA1P_p_tF'
43158
func testProtocolVarAccess(p: P) {
44159
modify(&p.sendyHandler)

0 commit comments

Comments
 (0)