Skip to content

Commit a1c0abf

Browse files
authored
Merge pull request #64275 from meg-gupta/fixdcelexicalphi
Don't DCE lexical phis with only destroy users
2 parents 347b0f5 + df90e2a commit a1c0abf

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,13 @@ void DCE::markLive() {
274274
addReverseDependency(beginAccess, &I);
275275
break;
276276
}
277-
case SILInstructionKind::DestroyValueInst:
277+
case SILInstructionKind::DestroyValueInst: {
278+
auto phi = PhiValue(I.getOperand(0));
279+
if (phi && phi->isLexical()) {
280+
markInstructionLive(&I);
281+
}
282+
break;
283+
}
278284
case SILInstructionKind::EndBorrowInst:
279285
case SILInstructionKind::EndLifetimeInst: {
280286
// The instruction is live only if it's operand value is also live

test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,28 @@ bb3(%phi3 : @guaranteed $Klass, %phi4 : @guaranteed $Wrapper1):
11501150
return %9999 : $()
11511151
}
11521152

1153+
1154+
class C {}
1155+
1156+
sil [ossa] @getC : $@convention(thin) () -> (@owned C)
1157+
1158+
// CHECK-LABEL: sil [ossa] @dont_dce_lexical_phi :
1159+
// CHECK: destroy_value
1160+
// CHECK-LABEL: } // end sil function 'dont_dce_lexical_phi'
1161+
sil [ossa] @dont_dce_lexical_phi : $() -> () {
1162+
entry:
1163+
%getC = function_ref @getC : $@convention(thin) () -> (@owned C)
1164+
cond_br undef, left, right
1165+
left:
1166+
%c1 = apply %getC() : $@convention(thin) () -> (@owned C)
1167+
%m1 = move_value [lexical] %c1 : $C
1168+
br exit(%m1 : $C)
1169+
right:
1170+
%c2 = apply %getC() : $@convention(thin) () -> (@owned C)
1171+
br exit(%c2 : $C)
1172+
exit(%cm : @owned $C):
1173+
destroy_value %cm : $C
1174+
%retval = tuple ()
1175+
return %retval : $()
1176+
}
1177+

0 commit comments

Comments
 (0)