Skip to content

Commit c5e0417

Browse files
authored
perf: eliminate cases with all branches unreachable (#11525)
This PR makes the LCNF simplifier eliminate cases where all alts are `.unreach` to just an `.unreach`. an `.unreach` We considered dropping a cases in a situation like this but decided against it because it might hinder reuse. ``` def test x : Bool := cases x : Bool | Except.error a.1 => ⊥ | Except.ok a.2 => let _x.3 := true; return _x.3 ```
1 parent 4b77e22 commit c5e0417

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Lean/Compiler/LCNF/Simp/Main.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,18 @@ partial def simp (code : Code) : SimpM Code := withIncRecDepth do
336336
params.forM (eraseParam ·)
337337
markSimplified
338338
return k
339+
if alts.all (·.getCode matches .unreach ..) then
340+
alts.forM (liftM <| ·.getParams.forM eraseParam)
341+
markSimplified
342+
return .unreach resultType
343+
/-
344+
We considered handling a case where we drop a cases if it only has one non-unreachable
345+
branch and doesn't rely on the params of that branch here. However, this has the potential
346+
to hinder reuse in later passes as we loose information about the shape of a variable. We
347+
might be able to reintroduce this at a later point if we track this information different
348+
from cases.
349+
-/
350+
339351
markUsedFVar discr
340352
return code.updateCases! resultType discr alts
341353
end

0 commit comments

Comments
 (0)