Skip to content

Commit 0971d82

Browse files
committed
SILGen: Remaining fixes for @callee_guaranteed closures and enable it
- Fix block to func reabstraction thunks block argument handling - Forward cast ownership - Fix applyPartiallyAppliedSuperMethod ownership for @callee_guaranteed closures - Avoid a copy in buildBlockToFuncThunkBody - Update tests for callee_guaranteed closures SR-5441 rdar://33255593
1 parent 4096000 commit 0971d82

File tree

108 files changed

+1551
-1351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1551
-1351
lines changed

include/swift/AST/SILOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class SILOptions {
115115

116116
/// Emit captures and function contexts using +0 caller-guaranteed ARC
117117
/// conventions.
118-
bool EnableGuaranteedClosureContexts = false;
118+
bool EnableGuaranteedClosureContexts = true;
119119

120120
/// Don't generate code using partial_apply in SIL generation.
121121
bool DisableSILPartialApply = false;

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 385; // Last change: Optimization attribute
57+
const uint16_t VERSION_MINOR = 386; // Last change: @callee_guaranted closures
5858

5959
using DeclIDField = BCFixed<31>;
6060

lib/SILGen/SILGenApply.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
11011101
}
11021102
auto loweredResultTy = SGF.getLoweredLoadableType(resultTy);
11031103
if (loweredResultTy != selfValue.getType()) {
1104-
selfValue = SGF.B.createUpcast(ice, selfValue, loweredResultTy);
1104+
selfValue = SGF.emitManagedRValueWithCleanup(
1105+
SGF.B.createUpcast(ice, selfValue.forward(SGF), loweredResultTy));
11051106
}
11061107

11071108
selfArg = ice->getSubExpr();
@@ -4100,8 +4101,8 @@ CallEmission::applyPartiallyAppliedSuperMethod(SGFContext C) {
41004101
SILValue partialApply =
41014102
SGF.B.createPartialApply(loc, superMethod.getValue(), partialApplyTy,
41024103
subs, {upcastedSelf.forward(SGF)}, closureTy);
4103-
firstLevelResult.value = RValue(SGF, loc, formalApplyType.getResult(),
4104-
ManagedValue::forUnmanaged(partialApply));
4104+
ManagedValue pa = SGF.emitManagedRValueWithCleanup(partialApply);
4105+
firstLevelResult.value = RValue(SGF, loc, formalApplyType.getResult(), pa);
41054106
return firstLevelResult;
41064107
}
41074108

lib/SILGen/SILGenBridging.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,9 @@ static void buildBlockToFuncThunkBody(SILGenFunction &SGF,
811811
// Add the block argument.
812812
SILValue blockV =
813813
entry->createFunctionArgument(SILType::getPrimitiveObjectType(blockTy));
814-
ManagedValue block = SGF.emitManagedRValueWithCleanup(blockV);
814+
ManagedValue block = SGF.SGM.M.getOptions().EnableGuaranteedClosureContexts
815+
? ManagedValue::forUnmanaged(blockV)
816+
: SGF.emitManagedRValueWithCleanup(blockV);
815817

816818
CanType formalResultType = formalFuncTy.getResult();
817819

test/ClangImporter/serialization-sil.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ public func testPartialApply(_ obj: Test) {
1212
// CHECK: dynamic_method_br [[CURRIED1_OBJ:%.+]] : $@opened([[CURRIED1_EXISTENTIAL:.+]]) Test, #Test.normalObject!1.foreign, [[CURRIED1_TRUE:[^,]+]], [[CURRIED1_FALSE:[^,]+]]
1313
// CHECK: [[CURRIED1_FALSE]]:
1414
// CHECK: [[CURRIED1_TRUE]]([[CURRIED1_METHOD:%.+]] : $@convention(objc_method) (@opened([[CURRIED1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject):
15-
// CHECK: [[CURRIED1_PARTIAL:%.+]] = partial_apply [[CURRIED1_METHOD]]([[CURRIED1_OBJ]]) : $@convention(objc_method) (@opened([[CURRIED1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject
16-
// CHECK: [[CURRIED1_THUNK:%.+]] = function_ref @_T0yXlIexo_ypIexr_TR : $@convention(thin) (@owned @callee_owned () -> @owned AnyObject) -> @out Any
17-
// CHECK: = partial_apply [[CURRIED1_THUNK]]([[CURRIED1_PARTIAL]])
15+
// CHECK: [[CURRIED1_PARTIAL:%.+]] = partial_apply [callee_guaranteed] [[CURRIED1_METHOD]]([[CURRIED1_OBJ]]) : $@convention(objc_method) (@opened([[CURRIED1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject
16+
// CHECK: [[CURRIED1_THUNK:%.+]] = function_ref @_T0yXlIego_ypIegr_TR : $@convention(thin) (@guaranteed @callee_guaranteed () -> @owned AnyObject) -> @out Any
17+
// CHECK: = partial_apply [callee_guaranteed] [[CURRIED1_THUNK]]([[CURRIED1_PARTIAL]])
1818
curried1()
1919
}
2020
if let curried2 = obj.innerPointer {
2121
// CHECK: dynamic_method_br [[CURRIED2_OBJ:%.+]] : $@opened([[CURRIED2_EXISTENTIAL:.+]]) Test, #Test.innerPointer!1.foreign, [[CURRIED2_TRUE:[^,]+]], [[CURRIED2_FALSE:[^,]+]]
2222
// CHECK: [[CURRIED2_FALSE]]:
2323
// CHECK: [[CURRIED2_TRUE]]([[CURRIED2_METHOD:%.+]] : $@convention(objc_method) (@opened([[CURRIED2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutableRawPointer):
24-
// CHECK: [[CURRIED2_PARTIAL:%.+]] = partial_apply [[CURRIED2_METHOD]]([[CURRIED2_OBJ]]) : $@convention(objc_method) (@opened([[CURRIED2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutableRawPointer
24+
// CHECK: [[CURRIED2_PARTIAL:%.+]] = partial_apply [callee_guaranteed] [[CURRIED2_METHOD]]([[CURRIED2_OBJ]]) : $@convention(objc_method) (@opened([[CURRIED2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutableRawPointer
2525
curried2()
2626
}
2727
if let prop1 = obj.normalObjectProp {
2828
// CHECK: dynamic_method_br [[PROP1_OBJ:%.+]] : $@opened([[PROP1_EXISTENTIAL:.+]]) Test, #Test.normalObjectProp!getter.1.foreign, [[PROP1_TRUE:[^,]+]], [[PROP1_FALSE:[^,]+]]
2929
// CHECK: [[PROP1_FALSE]]:
3030
// CHECK: [[PROP1_TRUE]]([[PROP1_METHOD:%.+]] : $@convention(objc_method) (@opened([[PROP1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject):
31-
// CHECK: [[PROP1_PARTIAL:%.+]] = partial_apply [[PROP1_METHOD]]([[PROP1_OBJ]]) : $@convention(objc_method) (@opened([[PROP1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject
32-
// CHECK: = apply [[PROP1_PARTIAL]]() : $@callee_owned () -> @owned AnyObject
31+
// CHECK: [[PROP1_PARTIAL:%.+]] = partial_apply [callee_guaranteed] [[PROP1_METHOD]]([[PROP1_OBJ]]) : $@convention(objc_method) (@opened([[PROP1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject
32+
// CHECK: = apply [[PROP1_PARTIAL]]() : $@callee_guaranteed () -> @owned AnyObject
3333
_ = prop1
3434
}
3535
if let prop2 = obj.innerPointerProp {
3636
// CHECK: dynamic_method_br [[PROP2_OBJ:%.+]] : $@opened([[PROP2_EXISTENTIAL:.+]]) Test, #Test.innerPointerProp!getter.1.foreign, [[PROP2_TRUE:[^,]+]], [[PROP2_FALSE:[^,]+]]
3737
// CHECK: [[PROP2_FALSE]]:
3838
// CHECK: [[PROP2_TRUE]]([[PROP2_METHOD:%.+]] : $@convention(objc_method) (@opened([[PROP2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutableRawPointer):
39-
// CHECK: [[PROP2_PARTIAL:%.+]] = partial_apply [[PROP2_METHOD]]([[PROP2_OBJ]]) : $@convention(objc_method) (@opened([[PROP2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutableRawPointer
40-
// CHECK: = apply [[PROP2_PARTIAL]]() : $@callee_owned () -> UnsafeMutableRawPointer
39+
// CHECK: [[PROP2_PARTIAL:%.+]] = partial_apply [callee_guaranteed] [[PROP2_METHOD]]([[PROP2_OBJ]]) : $@convention(objc_method) (@opened([[PROP2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutableRawPointer
40+
// CHECK: = apply [[PROP2_PARTIAL]]() : $@callee_guaranteed () -> UnsafeMutableRawPointer
4141
_ = prop2
4242
}
4343
} // CHECK: // end sil function '_T04Test16testPartialApplyySoAA_pF'

test/Constraints/rdar35142121.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ func foo(_ a: (Int) -> (Int)) -> Int {
88
return 42
99
}
1010

11-
// CHECK: function_ref @_T012rdar351421213fooS3icF : $@convention(thin) (@owned @noescape @callee_owned (Int) -> Int) -> Int
11+
// CHECK: function_ref @_T012rdar351421213fooS3icF : $@convention(thin) (@owned @noescape @callee_guaranteed (Int) -> Int) -> Int
1212
let _ = foo({ (a: Int) -> Int in a + 1 })

test/DebugInfo/closure-multivalue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ demo()
3939
// CHECK-O0-NOT: DW_OP_bit_piece
4040
// CHECK-O0: !DILocalVariable(name: "b", arg: 2{{.*}} line: 17,
4141
// CHECK-O0-NOT: DW_OP_bit_piece
42-
// CHECK-O0: !DISubprogram(linkageName: "_T0S2SSbs5Error_pIxxxdzo_S2SSbsAA_pIxiidzo_TR",
42+
// CHECK-O0: !DISubprogram(linkageName: "_T0S2SSbs5Error_pIgxxdzo_S2SSbsAA_pIgiidzo_TR",

test/DebugInfo/fnptr.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func barz(_ i: Float, _ j: Float) -> Int64 { return 0; }
1212
func main() -> Int64 {
1313
// CHECK-DAG: !DILocalVariable(name: "bar_fnptr",{{.*}} line: [[@LINE+3]],{{.*}} type: ![[BARPT:[0-9]+]]
1414
// AST-DAG: !DILocalVariable(name: "bar_fnptr",{{.*}} line: [[@LINE+2]],{{.*}} type: ![[BAR_T:[0-9]+]]
15-
// AST-DAG: ![[BAR_T]] = !DICompositeType({{.*}}, identifier: "_T0Iex_D")
15+
// AST-DAG: ![[BAR_T]] = !DICompositeType({{.*}}, identifier: "_T0Ieg_D")
1616
var bar_fnptr = bar
1717
// CHECK-DAG: ![[BARPT]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}} elements: ![[BARMEMBERS:[0-9]+]]
1818
// CHECK-DAG: ![[BARMEMBERS]] = !{![[BARMEMBER:.*]], {{.*}}}
@@ -25,7 +25,7 @@ func main() -> Int64 {
2525

2626
// CHECK-DAG: !DILocalVariable(name: "baz_fnptr",{{.*}} type: ![[BAZPT:[0-9]+]]
2727
// AST-DAG: !DILocalVariable(name: "baz_fnptr",{{.*}} type: ![[BAZ_T:[0-9]+]]
28-
// AST-DAG: ![[BAZ_T]] = !DICompositeType({{.*}}, identifier: "_T0Sfs5Int64VIexyd_D")
28+
// AST-DAG: ![[BAZ_T]] = !DICompositeType({{.*}}, identifier: "_T0Sfs5Int64VIegyd_D")
2929
// CHECK-DAG: ![[BAZPT]] = !DICompositeType(tag: DW_TAG_structure_type, {{.*}} elements: ![[BAZMEMBERS:[0-9]+]]
3030
// CHECK-DAG: ![[BAZMEMBERS]] = !{![[BAZMEMBER:.*]], {{.*}}}
3131
// CHECK-DAG: ![[BAZMEMBER]] = !DIDerivedType(tag: DW_TAG_member,{{.*}} baseType: ![[BAZPTR:[0-9]+]]
@@ -39,7 +39,7 @@ func main() -> Int64 {
3939

4040
// CHECK-DAG: !DILocalVariable(name: "barz_fnptr",{{.*}} type: ![[BARZPT:[0-9]+]]
4141
// AST_DAG: !DILocalVariable(name: "barz_fnptr",{{.*}} type: ![[BARZ_T:[0-9]+]]
42-
// AST-DAG: ![[BARZ_T:[0-9]+]] = !DICompositeType({{.*}}, identifier: "_T0S2fs5Int64VIexyyd_D")
42+
// AST-DAG: ![[BARZ_T:[0-9]+]] = !DICompositeType({{.*}}, identifier: "_T0S2fs5Int64VIegyyd_D")
4343
// CHECK-DAG: ![[BARZPT]] = !DICompositeType(tag: DW_TAG_structure_type,{{.*}} elements: ![[BARZMEMBERS:[0-9]+]]
4444
// CHECK-DAG: ![[BARZMEMBERS]] = !{![[BARZMEMBER:.*]], {{.*}}}
4545
// CHECK-DAG: ![[BARZMEMBER]] = !DIDerivedType(tag: DW_TAG_member,{{.*}} baseType: ![[BARZPTR:[0-9]+]]

test/DebugInfo/generic_args.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Wrapper<T: AProtocol> {
4545
}
4646
}
4747

48-
// CHECK-DAG: ![[FNTY:.*]] = !DICompositeType({{.*}}identifier: "_T012generic_args5applyq_x_q_xc1ftr0_lFQq_AaBq_x_q_xcACtr0_lFQq0_Ixir_D"
48+
// CHECK-DAG: ![[FNTY:.*]] = !DICompositeType({{.*}}identifier: "_T012generic_args5applyq_x_q_xc1ftr0_lFQq_AaBq_x_q_xcACtr0_lFQq0_Igir_D"
4949
// CHECK-DAG: !DILocalVariable(name: "f", {{.*}}, line: [[@LINE+1]], type: ![[FNTY]])
5050
func apply<T, U> (_ x: T, f: (T) -> (U)) -> U {
5151
return f(x)

test/DebugInfo/linetable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main(_ x: Int64) -> Void
3636
// CHECK: call {{.*}} @swift_rt_swift_release {{.*}}
3737
// CHECK: bitcast
3838
// CHECK: llvm.lifetime.end
39-
// CHECK: call {{.*}} @swift_rt_swift_release {{.*}}, !dbg ![[CLOSURE_END:.*]]
39+
// CHECK: call {{.*}}llvm.lifetime.end{{.*}}, !dbg ![[CLOSURE_END:.*]]
4040
// CHECK-NEXT: ret void, !dbg ![[CLOSURE_END]]
4141
// CHECK: ![[CLOSURE_END]] = !DILocation(line: [[@LINE+1]],
4242
}

0 commit comments

Comments
 (0)