feat: add phaseOut field to Pass

We need it for passes that move the code from one phase to another.

See `toMono` pass.

cc @hargoniX
This commit is contained in:
Leonardo de Moura 2022-10-06 03:28:20 -07:00
parent b172ba8a34
commit a0894dedbb
3 changed files with 30 additions and 23 deletions

View file

@ -73,7 +73,7 @@ def run (declNames : Array Name) : CompilerM (Array Decl) := withAtLeastMaxRecDe
for pass in manager.passes do
trace[Compiler] s!"Running pass: {pass.name}"
decls ← withPhase pass.phase <| pass.run decls
withPhase pass.phase <| checkpoint pass.name decls
withPhase pass.phaseOut <| checkpoint pass.name decls
if (← Lean.isTracingEnabledFor `Compiler.result) then
for decl in decls do
-- We display the declaration saved in the environment because the names have been normalized

View file

@ -10,6 +10,23 @@ import Lean.Compiler.LCNF.CompilerM
namespace Lean.Compiler.LCNF
def Phase.toNat : Phase → Nat
| .base => 0
| .mono => 1
| .impure => 2
instance : LT Phase where
lt l r := l.toNat < r.toNat
instance : LE Phase where
le l r := l.toNat ≤ r.toNat
instance {p1 p2 : Phase} : Decidable (p1 < p2) := Nat.decLt p1.toNat p2.toNat
instance {p1 p2 : Phase} : Decidable (p1 ≤ p2) := Nat.decLe p1.toNat p2.toNat
@[simp] theorem Phase.le_refl (p : Phase) : p ≤ p := by
cases p <;> decide
/--
A single compiler `Pass`, consisting of the actual pass function operating
on the `Decl`s as well as meta information.
@ -26,6 +43,11 @@ structure Pass where
-/
phase : Phase
/--
Resulting phase.
-/
phaseOut : Phase := phase
phaseInv : phaseOut ≥ phase := by simp
/--
The name of the `Pass`
-/
name : Name
@ -33,7 +55,9 @@ structure Pass where
The actual pass function, operating on the `Decl`s.
-/
run : Array Decl → CompilerM (Array Decl)
deriving Inhabited
instance : Inhabited Pass where
default := { phase := .base, name := default, run := fun decls => return decls }
/--
Can be used to install, remove, replace etc. passes by tagging a declaration
@ -56,30 +80,12 @@ structure PassManager where
passes : Array Pass
deriving Inhabited
namespace Phase
def toNat : Phase → Nat
| .base => 0
| .mono => 1
| .impure => 2
instance : LT Phase where
lt l r := l.toNat < r.toNat
instance : LE Phase where
le l r := l.toNat ≤ r.toNat
instance {p1 p2 : Phase} : Decidable (p1 < p2) := Nat.decLt p1.toNat p2.toNat
instance {p1 p2 : Phase} : Decidable (p1 ≤ p2) := Nat.decLe p1.toNat p2.toNat
instance : ToString Phase where
toString
| .base => "base"
| .mono => "mono"
| .impure => "impure"
end Phase
namespace Pass
def mkPerDeclaration (name : Name) (run : Decl → CompilerM Decl) (phase : Phase) (occurrence : Nat := 0) : Pass where

View file

@ -67,9 +67,10 @@ def Decl.toMono (decl : Decl) : CompilerM Decl := do
return decl
def toMono : Pass where
name := `toMono
run := fun decls => decls.mapM (·.toMono)
phase := .base
name := `toMono
run := fun decls => decls.mapM (·.toMono)
phase := .base
phaseOut := .mono
builtin_initialize
registerTraceClass `Compiler.toMono (inherited := true)