Skip to content

Commit 6e0e4dc

Browse files
authored
Merge pull request swiftlang#36174 from meg-gupta/fixcastoptimizer
Fix verifier error in CastOptimizer
2 parents 718af42 + b3615f6 commit 6e0e4dc

File tree

9 files changed

+25
-33
lines changed

9 files changed

+25
-33
lines changed

docs/SIL.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3620,6 +3620,20 @@ We require that ``%1`` and ``%0`` have the same type ignoring SILValueCategory.
36203620

36213621
This instruction is only valid in functions in Ownership SSA form.
36223622

3623+
end_lifetime
3624+
``````````
3625+
3626+
::
3627+
3628+
sil-instruction ::= 'end_lifetime' sil-operand
3629+
3630+
This instruction signifies the end of it's operand's lifetime to the ownership
3631+
verifier. It is inserted by the compiler in instances where it could be illegal
3632+
to insert a destroy operation. Ex: if the sil-operand had an undef value.
3633+
3634+
This instruction is valid only in OSSA and is lowered to a no-op when lowering
3635+
to non-OSSA.
3636+
36233637
assign
36243638
``````
36253639
::

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,14 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
20352035
"Inst with qualified ownership in a function that is not qualified");
20362036
}
20372037

2038+
void checkEndLifetimeInst(EndLifetimeInst *I) {
2039+
require(!I->getOperand()->getType().isTrivial(*I->getFunction()),
2040+
"Source value should be non-trivial");
2041+
require(!fnConv.useLoweredAddresses() || F.hasOwnership(),
2042+
"end_lifetime is only valid in functions with qualified "
2043+
"ownership");
2044+
}
2045+
20382046
void checkUncheckedValueCastInst(UncheckedValueCastInst *) {
20392047
require(
20402048
F.hasOwnership(),

lib/SILOptimizer/Transforms/PhiArgumentOptimizations.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ bool RedundantPhiEliminationPass::optimizeArgs(SILBasicBlock *block) {
165165
// arg.
166166
if (phi1->getOwnershipKind() == OwnershipKind::Owned &&
167167
phi2->getOwnershipKind() == OwnershipKind::Owned) {
168-
#ifndef NDEBUG
169168
assert(hasOnlyNoneOwnershipIncomingValues(phi1));
170169
assert(hasOnlyNoneOwnershipIncomingValues(phi2));
171-
#endif
172170
SILBuilderWithScope builder(&block->front());
173171
auto copy = builder.createCopyValue(
174172
RegularLocation::getAutoGeneratedLocation(), phi1);
@@ -178,18 +176,14 @@ bool RedundantPhiEliminationPass::optimizeArgs(SILBasicBlock *block) {
178176
// If arg2 has none ownership, replace arg1 with arg2
179177
else if (phi1->getOwnershipKind() == OwnershipKind::Owned &&
180178
phi2->getOwnershipKind() == OwnershipKind::None) {
181-
#ifndef NDEBUG
182179
assert(hasOnlyNoneOwnershipIncomingValues(phi1));
183-
#endif
184180
phi1->replaceAllUsesWith(phi2);
185181
eraseOwnedPhiArgument(block, arg1Idx);
186182
}
187183
// If arg1 has none ownership, replace arg2 with arg1
188184
else if (phi1->getOwnershipKind() == OwnershipKind::None &&
189185
phi2->getOwnershipKind() == OwnershipKind::Owned) {
190-
#ifndef NDEBUG
191186
assert(hasOnlyNoneOwnershipIncomingValues(phi2));
192-
#endif
193187
phi2->replaceAllUsesWith(phi1);
194188
eraseOwnedPhiArgument(block, arg2Idx);
195189
} else {

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)