Skip to content

Minor bug fixes in SILCombine, Canonicalization and LoadBorrowImmutabilityChecker #35609

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 3 commits into from
Jan 27, 2021
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
1 change: 1 addition & 0 deletions lib/SIL/Verifier/LoadBorrowImmutabilityChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ bool GatherWritesVisitor::visitUse(Operand *op, AccessUseType useTy) {
case SILInstructionKind::DeallocBoxInst:
case SILInstructionKind::WitnessMethodInst:
case SILInstructionKind::ExistentialMetatypeInst:
case SILInstructionKind::IsUniqueInst:
return true;

// Known writes...
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class SingleBlockOwnedForwardingInstFolder {
if (!hasOneNonDebugUse(next))
return false;

assert(getSingleNonDebugUser(rest.back()) == next);
assert(rest.empty() || getSingleNonDebugUser(rest.back()) == next);
rest.push_back(next);
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Debug.h"

Expand Down Expand Up @@ -521,7 +522,7 @@ eliminateUnneededForwardingUnarySingleValueInst(SingleValueInstruction *inst,
for (auto *use : getNonDebugUses(inst))
if (!isa<DestroyValueInst>(use->getUser()))
return next;
deleteAllDebugUses(inst);
deleteAllDebugUses(inst, pass.callbacks);
SILValue op = inst->getOperand(0);
inst->replaceAllUsesWith(op);
pass.notifyHasNewUsers(op);
Expand Down
14 changes: 14 additions & 0 deletions test/SIL/ownership-verifier/load_borrow_invalidation_test.sil
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,17 @@ bb0(%0 : @owned $Builtin.NativeObject):
%9999 = tuple()
return %9999 : $()
}

struct Bool {
var _value: Builtin.Int1
}

sil [ossa] @test_withisunique : $@convention(method) <Element> (@inout Builtin.NativeObject) -> Bool {
bb0(%0 : $*Builtin.NativeObject):
%1 = is_unique %0 : $*Builtin.NativeObject
%2 = struct $Bool (%1 : $Builtin.Int1)
%3 = load_borrow %0 : $*Builtin.NativeObject
end_borrow %3 : $Builtin.NativeObject
return %2 : $Bool
}

41 changes: 38 additions & 3 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ struct MyInt {
var value: Builtin.Int32
}

struct NativeObjectStruct {
var ele : Builtin.NativeObject
}

sil [global_init] @global_init_fun : $@convention(thin) () -> Builtin.RawPointer

sil [ossa] @user : $@convention(thin) (@owned Builtin.NativeObject) -> ()
Expand Down Expand Up @@ -1373,18 +1377,33 @@ bb0(%0 : @owned $B):
}

// (upcast X2->X (ref-to-object-pointer-inst (object-pointer-to-ref-inst x) typeof(x))) -> x
// CHECK-LABEL: sil [ossa] @upcast_unchecked_ref_cast_round_trip : $@convention(thin) (@owned E) -> @owned E {
// CHECK-LABEL: sil [ossa] @upcast_unchecked_ref_cast_round_trip1 : $@convention(thin) (@owned E) -> @owned E {
// CHECK-NOT: upcast
// CHECK-NOT: unchecked_ref_cast
// CHECK: } // end sil function 'upcast_unchecked_ref_cast_round_trip'
sil [ossa] @upcast_unchecked_ref_cast_round_trip : $@convention(thin) (@owned E) -> @owned E {
// CHECK: } // end sil function 'upcast_unchecked_ref_cast_round_trip1'
sil [ossa] @upcast_unchecked_ref_cast_round_trip1 : $@convention(thin) (@owned E) -> @owned E {
bb0(%0 : @owned $E):
%1 = upcast %0 : $E to $B
%2 = unchecked_ref_cast %1 : $B to $Builtin.NativeObject
%3 = unchecked_ref_cast %2 : $Builtin.NativeObject to $E
return %3 : $E
}

// CHECK-LABEL: sil [ossa] @upcast_unchecked_ref_cast_round_trip2 :
// CHECK-NOT: upcast
// CHECK-NOT: unchecked_ref_cast
// CHECK-LABEL: } // end sil function 'upcast_unchecked_ref_cast_round_trip2'
sil [ossa] @upcast_unchecked_ref_cast_round_trip2 : $@convention(method) (@owned E) -> () {
bb0(%0 : @owned $E):
%1 = upcast %0 : $E to $B
debug_value %1 : $B
%2 = unchecked_ref_cast %1 : $B to $Builtin.NativeObject
%3 = struct $NativeObjectStruct (%2 : $Builtin.NativeObject)
destroy_value %3 : $NativeObjectStruct
%res = tuple ()
return %res : $()
}

// (load (upcast-addr %x)) -> (upcast-ref (load %x))
//
// CHECK-LABEL: sil [ossa] @load_upcast_addr_to_upcast_ref_load_canonicalization : $@convention(thin) (@inout E) -> @owned B {
Expand Down Expand Up @@ -4561,3 +4580,19 @@ bb1(%2 : @owned $FakeOptional<Builtin.NativeObject>):
%9999 = tuple()
return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @test_useafterfree_debuginstdelete :
// CHECK-NOT: upcast
// CHECK-NOT: unchecked_ref_cast
// CHECK-LABEL: } // end sil function 'test_useafterfree_debuginstdelete'
sil [ossa] @test_useafterfree_debuginstdelete : $@convention(method) (@owned E) -> () {
bb0(%0 : @owned $E):
%1 = upcast %0 : $E to $B
debug_value %1 : $B
%2 = unchecked_ref_cast %1 : $B to $Builtin.NativeObject
debug_value %2 : $Builtin.NativeObject
destroy_value %2 : $Builtin.NativeObject
%res = tuple ()
return %res : $()
}