Skip to content

Disable a few mark_dependence sil combines in ossa #68395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 30 additions & 45 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1875,49 +1875,37 @@ static bool isLiteral(SILValue val) {
}

SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *mdi) {
auto base = lookThroughOwnershipInsts(mdi->getBase());

// Simplify the base operand of a MarkDependenceInst to eliminate unnecessary
// instructions that aren't adding value.
//
// Conversions to Optional.Some(x) often happen here, this isn't important
// for us, we can just depend on 'x' directly.
if (auto *eiBase = dyn_cast<EnumInst>(base)) {
if (eiBase->hasOperand()) {
auto *use = &mdi->getOperandRef(MarkDependenceInst::Base);
OwnershipReplaceSingleUseHelper helper(ownershipFixupContext,
use, eiBase->getOperand());
if (helper) {
helper.perform();
tryEliminateOnlyOwnershipUsedForwardingInst(eiBase,
getInstModCallbacks());
if (!mdi->getFunction()->hasOwnership()) {
// Simplify the base operand of a MarkDependenceInst to eliminate
// unnecessary instructions that aren't adding value.
//
// Conversions to Optional.Some(x) often happen here, this isn't important
// for us, we can just depend on 'x' directly.
if (auto *eiBase = dyn_cast<EnumInst>(mdi->getBase())) {
if (eiBase->hasOperand()) {
mdi->setBase(eiBase->getOperand());
if (eiBase->use_empty()) {
eraseInstFromFunction(*eiBase);
}
return mdi;
}
}
}

// Conversions from a class to AnyObject also happen a lot, we can just depend
// on the class reference.
if (auto *ier = dyn_cast<InitExistentialRefInst>(base)) {
auto *use = &mdi->getOperandRef(MarkDependenceInst::Base);
OwnershipReplaceSingleUseHelper helper(ownershipFixupContext,
use, ier->getOperand());
if (helper) {
helper.perform();
tryEliminateOnlyOwnershipUsedForwardingInst(ier, getInstModCallbacks());
// Conversions from a class to AnyObject also happen a lot, we can just
// depend on the class reference.
if (auto *ier = dyn_cast<InitExistentialRefInst>(mdi->getBase())) {
mdi->setBase(ier->getOperand());
if (ier->use_empty())
eraseInstFromFunction(*ier);
return mdi;
}
}

// Conversions from a class to AnyObject also happen a lot, we can just depend
// on the class reference.
if (auto *oeri = dyn_cast<OpenExistentialRefInst>(base)) {
auto *use = &mdi->getOperandRef(MarkDependenceInst::Base);
OwnershipReplaceSingleUseHelper helper(ownershipFixupContext,
use, oeri->getOperand());
if (helper) {
helper.perform();
tryEliminateOnlyOwnershipUsedForwardingInst(oeri, getInstModCallbacks());
// Conversions from a class to AnyObject also happen a lot, we can just
// depend on the class reference.
if (auto *oeri = dyn_cast<OpenExistentialRefInst>(mdi->getBase())) {
mdi->setBase(oeri->getOperand());
if (oeri->use_empty())
eraseInstFromFunction(*oeri);
return mdi;
}
}
Expand All @@ -1926,17 +1914,14 @@ SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *mdi) {
// whose base is a trivial typed object. In such a case, the mark_dependence
// does not have a meaning, so just eliminate it.
{
SILType baseType = base->getType();
if (baseType.isObject()) {
if ((hasOwnership() && base->getOwnershipKind() == OwnershipKind::None) ||
baseType.isTrivial(*mdi->getFunction())) {
SILValue value = mdi->getValue();
replaceInstUsesWith(*mdi, value);
return eraseInstFromFunction(*mdi);
}
SILType baseType = mdi->getBase()->getType();
if (baseType.isObject() && baseType.isTrivial(*mdi->getFunction())) {
SILValue value = mdi->getValue();
mdi->replaceAllUsesWith(value);
return eraseInstFromFunction(*mdi);
}
}

if (isLiteral(mdi->getValue())) {
// A literal lives forever, so no mark_dependence is needed.
// This pattern can occur after StringOptimization when a utf8CString of
Expand Down
85 changes: 65 additions & 20 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -4303,14 +4303,13 @@ bb2(%5 : @owned $MyErrorType):
throw %5 : $MyErrorType
}

// CHECK-LABEL: sil [ossa] @mark_dependence_base :
// CHECK: bb0(
// CHECK-NOT: init_existential_ref
// CHECK-NOT: enum
// CHECK: mark_dependence %0 : $*Builtin.Int64 on {{%.*}} : $B
// CHECK-NOT: init_existential_ref
// CHECK-NOT: enum
// CHECK: } // end sil function 'mark_dependence_base'
// CHECK-LABEL: sil [ossa] @mark_dependence_base
// XHECK: bb0(
// XHECK-NOT: init_existential_ref
// XHECK-NOT: enum
// XHECK-NEXT: mark_dependence
// XHECK-NEXT: load
// XHECK: return
sil [ossa] @mark_dependence_base : $@convention(thin) (@inout Builtin.Int64, @owned B) -> Builtin.Int64 {
bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
%x = init_existential_ref %1 : $B : $B, $AnyObject
Expand All @@ -4322,10 +4321,10 @@ bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
}

// CHECK-LABEL: sil [ossa] @mark_dependence_trivial_object_base :
// CHECK: bb0(
// CHECK-NEXT: copy_value
// CHECK-NEXT: return
// CHECK: } // end sil function 'mark_dependence_trivial_object_base'
// XHECK: bb0(
// XHECK-NEXT: copy_value
// XHECK-NEXT: return
// XHECK: } // end sil function 'mark_dependence_trivial_object_base'
sil [ossa] @mark_dependence_trivial_object_base : $@convention(thin) (@guaranteed B) -> @owned B {
bb0(%0 : @guaranteed $B):
%0a = copy_value %0 : $B
Expand Down Expand Up @@ -4357,14 +4356,13 @@ bb0(%addr : $*Builtin.Int64, %instance : @owned $B):

protocol _NSArrayCore {}

// CHECK-LABEL: sil [ossa] @mark_dependence_base2 :
// CHECK: bb0(
// CHECK-NOT: open_existential_ref
// CHECK-NOT: enum
// CHECK: mark_dependence %0 : $*Builtin.Int64 on {{%.*}} : $B
// CHECK-NOT: open_existential_ref
// CHECK-NOT: enum
// CHECK: } // end sil function 'mark_dependence_base2'
// CHECK-LABEL: sil [ossa] @mark_dependence_base2
// XHECK: bb0(
// XHECK-NOT: open_existential_ref
// XHECK-NOT: enum
// XHECK-NEXT: mark_dependence
// XHECK-NEXT: load
// XHECK: return
sil [ossa] @mark_dependence_base2 : $@convention(thin) (@inout Builtin.Int64, @owned B) -> Builtin.Int64 {
bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
%2 = init_existential_ref %1 : $B : $B, $AnyObject
Expand Down Expand Up @@ -5416,3 +5414,50 @@ bb0(%0 : $*MoveOnlyStruct):
return %16 : $()
}

sil @originalClosure : $@convention(thin) (@owned Klass) -> ()
sil @useNoEscapeClosure : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()

// CHECK-LABEL: sil [ossa] @test_mark_dependence_ossa_silcombine1 :
// [[ENUM:%.*]] = enum
// [[MDI:%.*]] = mark_dependence {{.*}} on [[ENUM]]
// CHECK-LABEL: } // end sil function 'test_mark_dependence_ossa_silcombine1'
sil [ossa] @test_mark_dependence_ossa_silcombine1 : $@convention(thin) (@owned Klass) -> () {
bb0(%0 : @owned $Klass):
%1 = function_ref @originalClosure : $@convention(thin) (@owned Klass) -> ()
%2 = partial_apply [callee_guaranteed] %1(%0) : $@convention(thin) (@owned Klass) -> ()
%3 = convert_escape_to_noescape %2 : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
%4 = enum $Optional<@callee_guaranteed () -> ()>, #Optional.some!enumelt, %2 : $@callee_guaranteed () -> ()
%5 = begin_borrow %4 : $Optional<@callee_guaranteed () -> ()>
%6 = mark_dependence %3 : $@noescape @callee_guaranteed () -> () on %5 : $Optional<@callee_guaranteed () -> ()>
%7 = function_ref @useNoEscapeClosure : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
%8 = apply %7(%6) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
destroy_value %6 : $@noescape @callee_guaranteed () -> ()
end_borrow %5 : $Optional<@callee_guaranteed () -> ()>
destroy_value %4 : $Optional<@callee_guaranteed () -> ()>
%12 = tuple ()
return %12 : $()
}

// CHECK-LABEL: sil [ossa] @test_mark_dependence_ossa_silcombine2 :
// [[ENUM:%.*]] = enum
// [[MDI:%.*]] = mark_dependence {{.*}} on [[ENUM]]
// CHECK-LABEL: } // end sil function 'test_mark_dependence_ossa_silcombine2'
sil [ossa] @test_mark_dependence_ossa_silcombine2 : $@convention(thin) (@owned Klass) -> () {
bb0(%0 : @owned $Klass):
%1 = function_ref @originalClosure : $@convention(thin) (@owned Klass) -> ()
%2 = partial_apply [callee_guaranteed] %1(%0) : $@convention(thin) (@owned Klass) -> ()
%3 = convert_escape_to_noescape %2 : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
%4 = enum $Optional<@callee_guaranteed () -> ()>, #Optional.some!enumelt, %2 : $@callee_guaranteed () -> ()
%5 = begin_borrow %4 : $Optional<@callee_guaranteed () -> ()>
%6 = mark_dependence %3 : $@noescape @callee_guaranteed () -> () on %5 : $Optional<@callee_guaranteed () -> ()>
%7 = function_ref @useNoEscapeClosure : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
%8 = apply %7(%6) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
destroy_value %6 : $@noescape @callee_guaranteed () -> ()
br bb1(%4 : $Optional<@callee_guaranteed () -> ()>, %5 : $Optional<@callee_guaranteed () -> ()>)

bb1(%10 : @owned $Optional<@callee_guaranteed () -> ()>, %11 : @guaranteed $Optional<@callee_guaranteed () -> ()>):
end_borrow %11 : $Optional<@callee_guaranteed () -> ()>
destroy_value %10 : $Optional<@callee_guaranteed () -> ()>
%12 = tuple ()
return %12 : $()
}