Skip to content

Commit df93b5c

Browse files
committed
Fix verifier error in CastOptimizer
While removing an invalid cast and inserting traps, we are currently inserting a store of undef to the cast destination and delete all instructions after the cast except for dealloc_stack. If the cast destination was an alloc_stack, the verifier could raise an error saying the cast destination was initialized at the dealloc. This PR deletes the store to undef, destroy_addr of the cast src, and gets rid of the code that was retaining the dealloc_stack. None of this is necessary anymore and the SIL is going to be legal because we insert unreachable instruction.
1 parent b9f55fd commit df93b5c

File tree

6 files changed

+3
-27
lines changed

6 files changed

+3
-27
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,11 +1496,6 @@ void CastOptimizer::deleteInstructionsAfterUnreachable(
14961496
while (UnreachableInstIt != Block->end()) {
14971497
SILInstruction *CurInst = &*UnreachableInstIt;
14981498
++UnreachableInstIt;
1499-
if (auto *DeallocStack = dyn_cast<DeallocStackInst>(CurInst))
1500-
if (!isa<SILUndef>(DeallocStack->getOperand())) {
1501-
DeallocStack->moveBefore(TrapInst);
1502-
continue;
1503-
}
15041499
CurInst->replaceAllUsesOfAllResultsWithUndef();
15051500
eraseInstAction(CurInst);
15061501
}
@@ -1622,17 +1617,6 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
16221617
// Remove the cast and insert a trap, followed by an
16231618
// unreachable instruction.
16241619
SILBuilderWithScope Builder(Inst, builderContext);
1625-
// mem2reg's invariants get unhappy if we don't try to
1626-
// initialize a loadable result.
1627-
if (!dynamicCast.getTargetLoweredType().isAddressOnly(
1628-
Builder.getFunction())) {
1629-
auto undef = SILValue(
1630-
SILUndef::get(dynamicCast.getTargetLoweredType().getObjectType(),
1631-
Builder.getFunction()));
1632-
Builder.emitStoreValueOperation(Loc, undef, dynamicCast.getDest(),
1633-
StoreOwnershipQualifier::Init);
1634-
}
1635-
Builder.emitDestroyAddr(Loc, Inst->getSrc());
16361620
auto *TrapI = Builder.createBuiltinTrap(Loc);
16371621
eraseInstAction(Inst);
16381622
Builder.setInsertionPoint(std::next(TrapI->getIterator()));

test/SIL/Parser/basic.sil

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,10 +1583,10 @@ bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
15831583
return %28 : $()
15841584
}
15851585

1586-
// CHECK-LABEL: sil @test_end_lifetime : $@convention(thin) (@owned Builtin.NativeObject) -> () {
1586+
// CHECK-LABEL: sil [ossa] @test_end_lifetime : $@convention(thin) (@owned Builtin.NativeObject) -> () {
15871587
// CHECK: end_lifetime {{%.*}} : $Builtin.NativeObject
1588-
sil @test_end_lifetime : $@convention(thin) (@owned Builtin.NativeObject) -> () {
1589-
bb0(%0 : $Builtin.NativeObject):
1588+
sil [ossa] @test_end_lifetime : $@convention(thin) (@owned Builtin.NativeObject) -> () {
1589+
bb0(%0 : @owned $Builtin.NativeObject):
15901590
end_lifetime %0 : $Builtin.NativeObject
15911591
return undef : $()
15921592
}

test/SILOptimizer/constant_propagation.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,6 @@ class AnObject {}
700700
// CHECK-LABEL: sil @replace_unconditional_check_cast_failure
701701
// CHECK: bb2:
702702
// CHECK: alloc_stack $AnObject
703-
// CHECK: store undef to %0
704-
// CHECK: dealloc_stack
705703
// CHECK: builtin "int_trap"()
706704
// CHECK: unreachable
707705

test/SILOptimizer/constant_propagation_ownership.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,6 @@ class AnSubObject : AnObject {}
875875
// CHECK-LABEL: sil [ossa] @replace_unconditional_check_cast_failure
876876
// CHECK: bb2:
877877
// CHECK: alloc_stack $AnObject
878-
// CHECK: store undef to [trivial] %0
879-
// CHECK: dealloc_stack
880878
// CHECK: builtin "int_trap"()
881879
// CHECK: unreachable
882880

test/SILOptimizer/sil_combine_uncheck.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ bb0(%0 : $Builtin.Int1):
3838

3939
// CHECK-LABEL: sil @test_unconditional_checked_cast_addr_fail
4040
// CHECK: bb0
41-
// CHECK-NEXT: store undef
42-
// CHECK-NEXT: destroy_addr
4341
// CHECK-NEXT: builtin "int_trap"
4442
// CHECK: unreachable
4543
// CHECK: }

test/SILOptimizer/sil_combine_uncheck_ossa.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ bb0(%0 : $Builtin.Int1):
3838

3939
// CHECK-LABEL: sil [ossa] @test_unconditional_checked_cast_addr_fail
4040
// CHECK: bb0
41-
// CHECK-NEXT: store undef
42-
// CHECK-NEXT: destroy_addr
4341
// CHECK-NEXT: builtin "int_trap"
4442
// CHECK: unreachable
4543
// CHECK: }

0 commit comments

Comments
 (0)