Skip to content

Update canTriviallyDeleteOSSAEndScopeInst and a related assertion #72019

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
Mar 1, 2024
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
2 changes: 2 additions & 0 deletions include/swift/SILOptimizer/Utils/InstOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ bool isInstructionTriviallyDeletable(SILInstruction *inst);
/// This routine only examines the state of the instruction at hand.
bool isInstructionTriviallyDead(SILInstruction *inst);

bool canTriviallyDeleteOSSAEndScopeInst(SILInstruction *inst);

/// Return true if this is a release instruction that's not going to
/// free the object.
bool isIntermediateRelease(SILInstruction *inst, EpilogueARCFunctionInfo *erfi);
Expand Down
9 changes: 7 additions & 2 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,17 @@ swift::createDecrementBefore(SILValue ptr, SILInstruction *insertPt) {

/// Returns true if OSSA scope ending instructions end_borrow/destroy_value can
/// be deleted trivially
static bool canTriviallyDeleteOSSAEndScopeInst(SILInstruction *i) {
bool swift::canTriviallyDeleteOSSAEndScopeInst(SILInstruction *i) {
if (!isa<EndBorrowInst>(i) && !isa<DestroyValueInst>(i))
return false;
if (isa<StoreBorrowInst>(i->getOperand(0)))
return false;
return i->getOperand(0)->getOwnershipKind() == OwnershipKind::None;

auto opValue = i->getOperand(0);
// We can delete destroy_value with operands of none ownership unless
// they are move-only values, which can have custom deinit
return opValue->getOwnershipKind() == OwnershipKind::None &&
!opValue->getType().isMoveOnly();
}

/// Perform a fast local check to see if the instruction is dead.
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/Utils/InstructionDeleter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ bool InstructionDeleter::trackIfDead(SILInstruction *inst) {
bool fixLifetime = inst->getFunction()->hasOwnership();
if (isInstructionTriviallyDead(inst)
|| isScopeAffectingInstructionDead(inst, fixLifetime)) {
assert(!isIncidentalUse(inst) && !isa<DestroyValueInst>(inst) &&
assert(!isIncidentalUse(inst) &&
(!isa<DestroyValueInst>(inst) ||
canTriviallyDeleteOSSAEndScopeInst(inst)) &&
"Incidental uses cannot be removed in isolation. "
"They would be removed iff the operand is dead");
getCallbacks().notifyWillBeDeleted(inst);
Expand Down
43 changes: 43 additions & 0 deletions test/SILOptimizer/simplify_cfg_ossa_jump_threading.sil
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,46 @@ bb6:
%t = tuple ()
return %t : $()
}

sil @_swift_stdlib_isNSString : $@convention(c) (AnyObject) -> UInt8

// Ensure no crash in compiler
sil hidden [ossa] @test_clone_destroy_none : $@convention(thin) (@guaranteed AnyObject) -> Bool {
bb0(%0 : @guaranteed $AnyObject):
%3 = enum $Optional<(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)>, #Optional.none!enumelt
%4 = begin_borrow %3 : $Optional<(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)>
switch_enum %4 : $Optional<(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%6 : @guaranteed $(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)):
end_borrow %4 : $Optional<(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)>
%8 = integer_literal $Builtin.Int1, -1
br bb3(%8 : $Builtin.Int1)

bb2:
end_borrow %4 : $Optional<(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)>
%11 = integer_literal $Builtin.Int1, 0
br bb3(%11 : $Builtin.Int1)

bb3(%13 : $Builtin.Int1):
destroy_value %3 : $Optional<(utf16Length: Int, asciiContentsPointer: UnsafePointer<UInt8>, untaggedCocoa: AnyObject)>
cond_br %13, bb4, bb5

bb4:
%16 = integer_literal $Builtin.Int1, -1
%17 = struct $Bool (%16 : $Builtin.Int1)
br bb6(%17 : $Bool)

bb5:
%19 = function_ref @_swift_stdlib_isNSString : $@convention(c) (AnyObject) -> UInt8
%20 = apply %19(%0) : $@convention(c) (AnyObject) -> UInt8
%21 = integer_literal $Builtin.Int8, 0
%22 = struct_extract %20 : $UInt8, #UInt8._value
%23 = builtin "cmp_eq_Int8"(%22 : $Builtin.Int8, %21 : $Builtin.Int8) : $Builtin.Int1
%24 = integer_literal $Builtin.Int1, -1
%25 = builtin "xor_Int1"(%23 : $Builtin.Int1, %24 : $Builtin.Int1) : $Builtin.Int1
%26 = struct $Bool (%25 : $Builtin.Int1)
br bb6(%26 : $Bool)

bb6(%28 : $Bool):
return %28 : $Bool
}