Skip to content

Commit 428ab47

Browse files
committed
[sil-combine] Turn off try_apply convert_function elimination on both ossa/non-ossa SIL.
In SILCombine, we do not want to add or delete edges. We are ok with swapping edges or replacing edges when the CFG structure is preserved. This becomes an issue since by performing this optimization, we are going to get rid of the error parameter but leave a try_apply, breaking SIL invariants. So to do perform this optimization, we would need to convert to an apply and eliminate the error edge, breaking the aforementioned SILCombine invariant. So, just do not perform this for now and leave it to other passes like SimplifyCFG.
1 parent d6a7e9b commit 428ab47

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,10 +1709,6 @@ SILInstruction *SILCombiner::visitTryApplyInst(TryApplyInst *AI) {
17091709
if (isa<PartialApplyInst>(AI->getCallee()))
17101710
return nullptr;
17111711

1712-
if (auto *CFI = dyn_cast<ConvertFunctionInst>(AI->getCallee())) {
1713-
return optimizeApplyOfConvertFunctionInst(AI, CFI);
1714-
}
1715-
17161712
// Optimize readonly functions with no meaningful users.
17171713
SILFunction *Fn = AI->getReferencedFunctionOrNull();
17181714
if (Fn && Fn->getEffectsKind() < EffectsKind::ReleaseNone) {

test/SILOptimizer/sil_combine.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4162,3 +4162,28 @@ bb0:
41624162
%1 = load %0 : $*Int64
41634163
return %1 : $Int64
41644164
}
4165+
4166+
sil @convert_raw_pointer_to_nativeobject : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject
4167+
4168+
// To eliminate the convert_function below we need to convert the try_apply to
4169+
// an apply. We do not delete edges like this in SILCombine with ownership and
4170+
// are trying to reduce it without ownership. So make sure we don't optimize
4171+
// this. This should be done in a different pass that handles CFG issues.
4172+
//
4173+
// CHECK-LABEL: sil @do_not_eliminate_error_adding_convert_function_used_by_try_apply_nonownership : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject {
4174+
// CHECK: convert_function
4175+
// CHECK: try_apply
4176+
// CHECK: } // end sil function 'do_not_eliminate_error_adding_convert_function_used_by_try_apply_nonownership'
4177+
sil @do_not_eliminate_error_adding_convert_function_used_by_try_apply_nonownership : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject {
4178+
bb0(%0 : $Builtin.RawPointer):
4179+
%f = function_ref @convert_raw_pointer_to_nativeobject : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject
4180+
%t = thin_to_thick_function %f : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject to $@noescape @callee_guaranteed (Builtin.RawPointer) -> @owned Builtin.NativeObject
4181+
%c = convert_function %t : $@noescape @callee_guaranteed (Builtin.RawPointer) -> @owned Builtin.NativeObject to $@noescape @callee_guaranteed (Builtin.RawPointer) -> (@owned Builtin.NativeObject, @error Error)
4182+
try_apply %c(%0) : $@noescape @callee_guaranteed (Builtin.RawPointer) -> (@owned Builtin.NativeObject, @error Error), normal bb1, error bb2
4183+
4184+
bb1(%result : $Builtin.NativeObject):
4185+
return %result : $Builtin.NativeObject
4186+
4187+
bb2(%error : $Error):
4188+
unreachable
4189+
}

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4901,3 +4901,28 @@ bb3(%4 : @owned $KlassNativeObjEither):
49014901
return %4 : $KlassNativeObjEither
49024902
}
49034903

4904+
4905+
sil @convert_raw_pointer_to_nativeobject : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject
4906+
4907+
// To eliminate the convert_function below we need to convert the try_apply to
4908+
// an apply. We do not delete edges like this in SILCombine with ownership and
4909+
// are trying to reduce it without ownership. So make sure we don't optimize
4910+
// this. This should be done in a different pass that handles CFG issues.
4911+
//
4912+
// CHECK-LABEL: sil [ossa] @do_not_eliminate_error_adding_convert_function_used_by_try_apply : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject {
4913+
// CHECK: convert_function
4914+
// CHECK: try_apply
4915+
// CHECK: } // end sil function 'do_not_eliminate_error_adding_convert_function_used_by_try_apply'
4916+
sil [ossa] @do_not_eliminate_error_adding_convert_function_used_by_try_apply : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject {
4917+
bb0(%0 : $Builtin.RawPointer):
4918+
%f = function_ref @convert_raw_pointer_to_nativeobject : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject
4919+
%t = thin_to_thick_function %f : $@convention(thin) (Builtin.RawPointer) -> @owned Builtin.NativeObject to $@noescape @callee_guaranteed (Builtin.RawPointer) -> @owned Builtin.NativeObject
4920+
%c = convert_function %t : $@noescape @callee_guaranteed (Builtin.RawPointer) -> @owned Builtin.NativeObject to $@noescape @callee_guaranteed (Builtin.RawPointer) -> (@owned Builtin.NativeObject, @error Error)
4921+
try_apply %c(%0) : $@noescape @callee_guaranteed (Builtin.RawPointer) -> (@owned Builtin.NativeObject, @error Error), normal bb1, error bb2
4922+
4923+
bb1(%result : @owned $Builtin.NativeObject):
4924+
return %result : $Builtin.NativeObject
4925+
4926+
bb2(%error : @owned $Error):
4927+
unreachable
4928+
}

0 commit comments

Comments
 (0)