Skip to content

Commit c7b6ec8

Browse files
authored
Merge pull request #35609 from meg-gupta/ossabugs3
Minor bug fixes in SILCombine, Canonicalization and LoadBorrowImmutabilityChecker
2 parents 11228e7 + 514cd9c commit c7b6ec8

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

lib/SIL/Verifier/LoadBorrowImmutabilityChecker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ bool GatherWritesVisitor::visitUse(Operand *op, AccessUseType useTy) {
9797
case SILInstructionKind::DeallocBoxInst:
9898
case SILInstructionKind::WitnessMethodInst:
9999
case SILInstructionKind::ExistentialMetatypeInst:
100+
case SILInstructionKind::IsUniqueInst:
100101
return true;
101102

102103
// Known writes...

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class SingleBlockOwnedForwardingInstFolder {
141141
if (!hasOneNonDebugUse(next))
142142
return false;
143143

144-
assert(getSingleNonDebugUser(rest.back()) == next);
144+
assert(rest.empty() || getSingleNonDebugUser(rest.back()) == next);
145145
rest.push_back(next);
146146
return true;
147147
}

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/SIL/SILFunction.h"
2727
#include "swift/SIL/SILInstruction.h"
2828
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
29+
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
2930
#include "llvm/ADT/Statistic.h"
3031
#include "llvm/Support/Debug.h"
3132

@@ -521,7 +522,7 @@ eliminateUnneededForwardingUnarySingleValueInst(SingleValueInstruction *inst,
521522
for (auto *use : getNonDebugUses(inst))
522523
if (!isa<DestroyValueInst>(use->getUser()))
523524
return next;
524-
deleteAllDebugUses(inst);
525+
deleteAllDebugUses(inst, pass.callbacks);
525526
SILValue op = inst->getOperand(0);
526527
inst->replaceAllUsesWith(op);
527528
pass.notifyHasNewUsers(op);

test/SIL/ownership-verifier/load_borrow_invalidation_test.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,17 @@ bb0(%0 : @owned $Builtin.NativeObject):
395395
%9999 = tuple()
396396
return %9999 : $()
397397
}
398+
399+
struct Bool {
400+
var _value: Builtin.Int1
401+
}
402+
403+
sil [ossa] @test_withisunique : $@convention(method) <Element> (@inout Builtin.NativeObject) -> Bool {
404+
bb0(%0 : $*Builtin.NativeObject):
405+
%1 = is_unique %0 : $*Builtin.NativeObject
406+
%2 = struct $Bool (%1 : $Builtin.Int1)
407+
%3 = load_borrow %0 : $*Builtin.NativeObject
408+
end_borrow %3 : $Builtin.NativeObject
409+
return %2 : $Bool
410+
}
411+

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ struct MyInt {
4848
var value: Builtin.Int32
4949
}
5050

51+
struct NativeObjectStruct {
52+
var ele : Builtin.NativeObject
53+
}
54+
5155
sil [global_init] @global_init_fun : $@convention(thin) () -> Builtin.RawPointer
5256

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

13751379
// (upcast X2->X (ref-to-object-pointer-inst (object-pointer-to-ref-inst x) typeof(x))) -> x
1376-
// CHECK-LABEL: sil [ossa] @upcast_unchecked_ref_cast_round_trip : $@convention(thin) (@owned E) -> @owned E {
1380+
// CHECK-LABEL: sil [ossa] @upcast_unchecked_ref_cast_round_trip1 : $@convention(thin) (@owned E) -> @owned E {
13771381
// CHECK-NOT: upcast
13781382
// CHECK-NOT: unchecked_ref_cast
1379-
// CHECK: } // end sil function 'upcast_unchecked_ref_cast_round_trip'
1380-
sil [ossa] @upcast_unchecked_ref_cast_round_trip : $@convention(thin) (@owned E) -> @owned E {
1383+
// CHECK: } // end sil function 'upcast_unchecked_ref_cast_round_trip1'
1384+
sil [ossa] @upcast_unchecked_ref_cast_round_trip1 : $@convention(thin) (@owned E) -> @owned E {
13811385
bb0(%0 : @owned $E):
13821386
%1 = upcast %0 : $E to $B
13831387
%2 = unchecked_ref_cast %1 : $B to $Builtin.NativeObject
13841388
%3 = unchecked_ref_cast %2 : $Builtin.NativeObject to $E
13851389
return %3 : $E
13861390
}
13871391

1392+
// CHECK-LABEL: sil [ossa] @upcast_unchecked_ref_cast_round_trip2 :
1393+
// CHECK-NOT: upcast
1394+
// CHECK-NOT: unchecked_ref_cast
1395+
// CHECK-LABEL: } // end sil function 'upcast_unchecked_ref_cast_round_trip2'
1396+
sil [ossa] @upcast_unchecked_ref_cast_round_trip2 : $@convention(method) (@owned E) -> () {
1397+
bb0(%0 : @owned $E):
1398+
%1 = upcast %0 : $E to $B
1399+
debug_value %1 : $B
1400+
%2 = unchecked_ref_cast %1 : $B to $Builtin.NativeObject
1401+
%3 = struct $NativeObjectStruct (%2 : $Builtin.NativeObject)
1402+
destroy_value %3 : $NativeObjectStruct
1403+
%res = tuple ()
1404+
return %res : $()
1405+
}
1406+
13881407
// (load (upcast-addr %x)) -> (upcast-ref (load %x))
13891408
//
13901409
// CHECK-LABEL: sil [ossa] @load_upcast_addr_to_upcast_ref_load_canonicalization : $@convention(thin) (@inout E) -> @owned B {
@@ -4561,3 +4580,19 @@ bb1(%2 : @owned $FakeOptional<Builtin.NativeObject>):
45614580
%9999 = tuple()
45624581
return %9999 : $()
45634582
}
4583+
4584+
// CHECK-LABEL: sil [ossa] @test_useafterfree_debuginstdelete :
4585+
// CHECK-NOT: upcast
4586+
// CHECK-NOT: unchecked_ref_cast
4587+
// CHECK-LABEL: } // end sil function 'test_useafterfree_debuginstdelete'
4588+
sil [ossa] @test_useafterfree_debuginstdelete : $@convention(method) (@owned E) -> () {
4589+
bb0(%0 : @owned $E):
4590+
%1 = upcast %0 : $E to $B
4591+
debug_value %1 : $B
4592+
%2 = unchecked_ref_cast %1 : $B to $Builtin.NativeObject
4593+
debug_value %2 : $Builtin.NativeObject
4594+
destroy_value %2 : $Builtin.NativeObject
4595+
%res = tuple ()
4596+
return %res : $()
4597+
}
4598+

0 commit comments

Comments
 (0)