perf: put mapMonoM where sensible in the compiler (#12610)

This commit is contained in:
Henrik Böving 2026-02-20 14:12:59 +01:00 committed by GitHub
parent 4fbc5d3c2a
commit 78df48bdf4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 21 additions and 13 deletions

View file

@ -642,7 +642,7 @@ where
eraseCode alt.getCode
return alt.updateCode <| .unreach typ
| .default body => return alt.updateCode (← go body)
return code.updateCases! cs.resultType cs.discr (← cs.alts.mapM <| processAlt cs.resultType)
return code.updateAlts! (← cs.alts.mapMonoM <| processAlt cs.resultType)
| .jmp .. | .return .. | .unreach .. => return code
end UnreachableBranches

View file

@ -311,7 +311,15 @@ where
| .pap .. => return object
| .uproj .. => return usize
| .erased => return tagged
| .fvar .. | .lit .. | .sproj .. | .oproj .. | .reset .. | .ctor .. | .reuse .. =>
| .lit (.nat n) =>
if n ≤ maxSmallNat then
return tagged
else
return currentType
| .lit (.str ..) =>
return object
| .ctor i _ => return i.type
| .fvar .. | .lit .. | .sproj .. | .oproj .. | .reset .. | .reuse .. =>
return currentType
| .box .. | .unbox .. => unreachable!

View file

@ -145,7 +145,7 @@ partial def visitCode (code : Code .pure) : M (Code .pure) := do
let decl ← decl.updateValue (← visitCode decl.value)
return code.updateFun! decl (← visitCode k)
| .cases cases =>
let alts ← cases.alts.mapM (fun alt => do return alt.updateCode (← visitCode alt.getCode))
let alts ← cases.alts.mapMonoM (fun alt => do return alt.updateCode (← visitCode alt.getCode))
return code.updateAlts! alts
| .jmp .. | .return _ | .unreach .. => return code

View file

@ -132,7 +132,7 @@ partial def Code.mapFVarM [MonadLiftT CompilerM m] [Monad m] (f : FVarId → m F
let decl ← decl.update (← Expr.mapFVarM f decl.type) params (← mapFVarM f decl.value)
return Code.updateFun! c decl (← mapFVarM f k)
| .cases cs =>
return Code.updateCases! c (← Expr.mapFVarM f cs.resultType) (← f cs.discr) (← cs.alts.mapM (·.mapCodeM (mapFVarM f)))
return Code.updateCases! c (← Expr.mapFVarM f cs.resultType) (← f cs.discr) (← cs.alts.mapMonoM (·.mapCodeM (mapFVarM f)))
| .jmp fn args =>
return Code.updateJmp! c (← f fn) (← args.mapM (Arg.mapFVarM f))
| .return var =>

View file

@ -320,7 +320,7 @@ where
}
let (_, res) ← goCases |>.run base
let remainders := res.newArms[Decision.dont]!
let newAlts ← cs.alts.mapM fun alt => do
let newAlts ← cs.alts.mapMonoM fun alt => do
let decision := Decision.ofAlt alt
let newCode := res.newArms[decision]!
trace[Compiler.floatLetIn] "Size of code that was pushed into arm: {repr decision} {newCode.length}"

View file

@ -118,7 +118,7 @@ where
let ps ← updateParams decl.params map[ParamMap.Key.jp declName decl.fvarId]!
let decl ← decl.update decl.type ps (← go declName decl.value)
return code.updateFun! decl (← go declName k)
| .cases cs => return code.updateAlts! <| ← cs.alts.mapM (·.mapCodeM (go declName))
| .cases cs => return code.updateAlts! <| ← cs.alts.mapMonoM (·.mapCodeM (go declName))
| .let _ k | .uset _ _ _ k _ | .sset _ _ _ _ _ k _ => return code.updateCont! (← go declName k)
| .return .. | .jmp .. | .unreach .. => return code
| .inc .. | .dec .. => unreachable!

View file

@ -240,7 +240,7 @@ where
let newDecl ← decl.updateValue (← go decl.value)
return Code.updateFun! code newDecl (← go k)
| .cases cs =>
return Code.updateCases! code cs.resultType cs.discr (← cs.alts.mapM (·.mapCodeM go))
return Code.updateCases! code cs.resultType cs.discr (← cs.alts.mapMonoM (·.mapCodeM go))
| .jmp .. | .return .. | .unreach .. =>
return code
@ -452,7 +452,7 @@ where
let visitor := fun alt => do
withNewAltScope alt do
alt.mapCodeM go
let alts ← cs.alts.mapM visitor
let alts ← cs.alts.mapMonoM visitor
return Code.updateCases! code cs.resultType discr alts
| .jmp fn args =>
let mut newArgs ← args.mapM (mapFVarM goFVar)
@ -624,7 +624,7 @@ where
let decl ← decl.updateValue (← goReduce decl.value)
return Code.updateFun! code decl (← goReduce k)
| .cases cs =>
let alts ← cs.alts.mapM (·.mapCodeM goReduce)
let alts ← cs.alts.mapMonoM (·.mapCodeM goReduce)
return Code.updateCases! code cs.resultType cs.discr alts
| .return .. | .unreach .. => return code

View file

@ -78,7 +78,7 @@ partial def Cases.pushProjs (c : Cases .impure) (decls : Array (CodeDecl .impure
let altsUsed := c.alts.map (·.getCode.collectUsed)
let ctxUsed := ({} : FVarIdHashSet) |>.insert c.discr
let (bs, alts) ← go decls c.alts altsUsed #[] ctxUsed
let alts ← alts.mapM (·.mapCodeM Code.pushProj)
let alts ← alts.mapMonoM (·.mapCodeM Code.pushProj)
let c := c.updateAlts alts
return attachCodeDecls bs (.cases c)
where

View file

@ -204,7 +204,7 @@ where
| .cases cs =>
if ← c.isFVarLiveIn x then
/- If `x` is live in `c`, we recursively process each branch. -/
let alts ← cs.alts.mapM (·.mapCodeM (D x info))
let alts ← cs.alts.mapMonoM (·.mapCodeM (D x info))
return (c.updateAlts! alts, true)
else
return (c, false)

View file

@ -101,7 +101,7 @@ partial def visitCode (code : Code .pure) : M (Code .pure) := do
-- a variable above while also destructuring an array doesn't work.
return code.updateCases! cases.resultType discr #[.alt ctorName params k]
else
let alts ← cases.alts.mapM (visitAlt ·)
let alts ← cases.alts.mapMonoM (visitAlt ·)
return code.updateCases! cases.resultType discr alts
| .return fvarId => return code.updateReturn! (← remapFVar fvarId)
| .unreach .. => return code

View file

@ -74,7 +74,7 @@ partial def bindCases (jpDecl : FunDecl .pure) (cases : Cases .pure) : CompilerM
return .jp jpDecl result
where
visitAlts (alts : Array (Alt .pure)) : BindCasesM (Array (Alt .pure)) :=
alts.mapM fun alt => return alt.updateCode (← go alt.getCode)
alts.mapMonoM (·.mapCodeM go)
findFun? (f : FVarId) : CompilerM (Option (FunDecl .pure)) := do
if let some funDecl ← findFunDecl? (pu := .pure) f then