@[inline]
Equations
Equations
- Lean.IR.Borrow.OwnedSet.beq x x = match x, x with | (f₁, x₁), (f₂, x₂) => f₁ == f₂ && x₁ == x₂
Equations
Equations
- Lean.IR.Borrow.OwnedSet.getHash x = match x with | (f, x) => mixHash (hash f) (hash x)
Equations
@[inline]
def
Lean.IR.Borrow.OwnedSet.insert
(s : Lean.IR.Borrow.OwnedSet)
(k : Lean.IR.Borrow.OwnedSet.Key)
:
Equations
def
Lean.IR.Borrow.OwnedSet.contains
(s : Lean.IR.Borrow.OwnedSet)
(k : Lean.IR.Borrow.OwnedSet.Key)
:
Equations
Equations
- Lean.IR.Borrow.ParamMap.instBEqKey = { beq := [anonymous] }
Equations
- Lean.IR.Borrow.ParamMap.getHash x = match x with | Lean.IR.Borrow.ParamMap.Key.decl n => hash n | Lean.IR.Borrow.ParamMap.Key.jp n id => mixHash (hash n) (hash id)
Equations
@[inline]
Equations
- Lean.IR.Borrow.ParamMap.fmt map = let fmts := Std.HashMap.fold (fun fmt k ps => let k := match k with | Lean.IR.Borrow.ParamMap.Key.decl n => Lean.format n | Lean.IR.Borrow.ParamMap.Key.jp n id => Lean.format n ++ Std.Format.text ":" ++ Lean.format id; fmt ++ Lean.Format.line ++ k ++ Std.Format.text " -> " ++ Lean.IR.formatParams ps) Lean.Format.nil map; Std.Format.text "{" ++ Lean.Format.nest 1 fmts ++ Std.Format.text "}"
Equations
- Lean.IR.Borrow.instToFormatParamMap = { format := Lean.IR.Borrow.ParamMap.fmt }
Equations
- Lean.IR.Borrow.instToStringParamMap = { toString := fun m => Lean.Format.pretty (Lean.format m) }
Equations
- Lean.IR.Borrow.InitParamMap.initBorrow ps = Array.map (fun p => { x := p.x, borrow := Lean.IR.IRType.isObj p.ty, ty := p.ty }) ps
def
Lean.IR.Borrow.InitParamMap.initBorrowIfNotExported
(exported : Bool)
(ps : Array Lean.IR.Param)
:
Equations
- Lean.IR.Borrow.InitParamMap.initBorrowIfNotExported exported ps = if exported = true then ps else Lean.IR.Borrow.InitParamMap.initBorrow ps
Equations
- Lean.IR.Borrow.InitParamMap.visitDecls env decls = Array.forM (fun decl => match decl with | Lean.IR.Decl.fdecl f xs x b x_1 => let exported := Lean.isExport env f; do modify fun m => Std.HashMap.insert m (Lean.IR.Borrow.ParamMap.Key.decl f) (Lean.IR.Borrow.InitParamMap.initBorrowIfNotExported exported xs) Lean.IR.Borrow.InitParamMap.visitFnBody f b | x => pure ()) decls 0 (Array.size decls)
Equations
- Lean.IR.Borrow.mkInitParamMap env decls = StateT.run' (SeqRight.seqRight (Lean.IR.Borrow.InitParamMap.visitDecls env decls) fun x => get) ∅
partial def
Lean.IR.Borrow.ApplyParamMap.visitFnBody
(fn : Lean.IR.FunId)
(paramMap : Lean.IR.Borrow.ParamMap)
:
def
Lean.IR.Borrow.ApplyParamMap.visitDecls
(decls : Array Lean.IR.Decl)
(paramMap : Lean.IR.Borrow.ParamMap)
:
Equations
- Lean.IR.Borrow.ApplyParamMap.visitDecls decls paramMap = Array.map (fun decl => match decl with | Lean.IR.Decl.fdecl f xs ty b info => let b := Lean.IR.Borrow.ApplyParamMap.visitFnBody f paramMap b; match Std.HashMap.find? paramMap (Lean.IR.Borrow.ParamMap.Key.decl f) with | some xs => Lean.IR.Decl.fdecl f xs ty b info | none => panicWithPosWithDecl "Lean.Compiler.IR.Borrow" "Lean.IR.Borrow.ApplyParamMap.visitDecls" 129 19 "unreachable code has been reached" | other => other) decls
Equations
- Lean.IR.Borrow.applyParamMap decls map = Lean.IR.Borrow.ApplyParamMap.visitDecls decls map
- env : Lean.Environment
- decls : Array Lean.IR.Decl
- currFn : Lean.IR.FunId
- paramSet : Lean.IR.IndexSet
- owned : Lean.IR.Borrow.OwnedSet
- modified : Bool
- paramMap : Lean.IR.Borrow.ParamMap
@[inline]
Equations
- Lean.IR.Borrow.getCurrFn = do let ctx ← read pure ctx.currFn
Equations
- Lean.IR.Borrow.markModified = modify fun s => { owned := s.owned, modified := true, paramMap := s.paramMap }
Equations
- Lean.IR.Borrow.ownVar x = do let currFn ← Lean.IR.Borrow.getCurrFn modify fun s => if Lean.IR.Borrow.OwnedSet.contains s.owned (currFn, x.idx) = true then s else { owned := Lean.IR.Borrow.OwnedSet.insert s.owned (currFn, x.idx), modified := true, paramMap := s.paramMap }
Equations
- Lean.IR.Borrow.ownArg x = match x with | Lean.IR.Arg.var x => Lean.IR.Borrow.ownVar x | x => pure ()
Equations
- Lean.IR.Borrow.ownArgs xs = Array.forM Lean.IR.Borrow.ownArg xs 0 (Array.size xs)
Equations
- Lean.IR.Borrow.isOwned x = do let currFn ← Lean.IR.Borrow.getCurrFn let s ← get pure (Lean.IR.Borrow.OwnedSet.contains s.owned (currFn, x.idx))
Equations
- Lean.IR.Borrow.updateParamMap k = do let _ ← Lean.IR.Borrow.getCurrFn let s ← get match Std.HashMap.find? s.paramMap k with | some ps => do let ps ← Array.mapM (fun p => if (!p.borrow) = true then pure p else do let a ← Lean.IR.Borrow.isOwned p.x if a = true then do Lean.IR.Borrow.markModified pure { x := p.x, borrow := false, ty := p.ty } else pure p) ps modify fun s => { owned := s.owned, modified := s.modified, paramMap := Std.HashMap.insert s.paramMap k ps } | none => pure ()
Equations
- Lean.IR.Borrow.getParamInfo k = do let s ← get match Std.HashMap.find? s.paramMap k with | some ps => pure ps | none => match k with | Lean.IR.Borrow.ParamMap.Key.decl fn => do let ctx ← read match Lean.IR.findEnvDecl ctx.env fn with | some decl => pure (Lean.IR.Decl.params decl) | none => panicWithPosWithDecl "Lean.Compiler.IR.Borrow" "Lean.IR.Borrow.getParamInfo" 203 21 "unreachable code has been reached" | x => panicWithPosWithDecl "Lean.Compiler.IR.Borrow" "Lean.IR.Borrow.getParamInfo" 204 11 "unreachable code has been reached"
Equations
- Lean.IR.Borrow.ownArgsUsingParams xs ps = Nat.forM (Array.size xs) fun i => let x := Array.getOp xs i; let p := Array.getOp ps i; if p.borrow = true then pure PUnit.unit else Lean.IR.Borrow.ownArg x
Equations
- Lean.IR.Borrow.ownParamsUsingArgs xs ps = Nat.forM (Array.size xs) fun i => let x := Array.getOp xs i; let p := Array.getOp ps i; match x with | Lean.IR.Arg.var x => do let a ← Lean.IR.Borrow.isOwned x if a = true then Lean.IR.Borrow.ownVar p.x else pure PUnit.unit | x => pure ()
Equations
- Lean.IR.Borrow.ownArgsIfParam xs = do let ctx ← read Array.forM (fun x => match x with | Lean.IR.Arg.var x => if Std.RBTree.contains ctx.paramSet x.idx = true then Lean.IR.Borrow.ownVar x else pure PUnit.unit | x => pure ()) xs 0 (Array.size xs)
Equations
- Lean.IR.Borrow.collectExpr z x = match x with | Lean.IR.Expr.reset x x_1 => SeqRight.seqRight (Lean.IR.Borrow.ownVar z) fun x => Lean.IR.Borrow.ownVar x_1 | Lean.IR.Expr.reuse x x_1 x_2 ys => SeqRight.seqRight (SeqRight.seqRight (Lean.IR.Borrow.ownVar z) fun x_3 => Lean.IR.Borrow.ownVar x) fun x => Lean.IR.Borrow.ownArgsIfParam ys | Lean.IR.Expr.ctor x xs => SeqRight.seqRight (Lean.IR.Borrow.ownVar z) fun x => Lean.IR.Borrow.ownArgsIfParam xs | Lean.IR.Expr.proj x x_1 => do let a ← Lean.IR.Borrow.isOwned x_1 let _do_jp : Unit → Lean.IR.Borrow.M Unit := fun y => do let a ← Lean.IR.Borrow.isOwned z if a = true then Lean.IR.Borrow.ownVar x_1 else pure PUnit.unit if a = true then do let y ← Lean.IR.Borrow.ownVar z _do_jp y else do let y ← pure PUnit.unit _do_jp y | Lean.IR.Expr.fap g xs => do let ps ← Lean.IR.Borrow.getParamInfo (Lean.IR.Borrow.ParamMap.Key.decl g) SeqRight.seqRight (Lean.IR.Borrow.ownVar z) fun x => Lean.IR.Borrow.ownArgsUsingParams xs ps | Lean.IR.Expr.ap x ys => SeqRight.seqRight (SeqRight.seqRight (Lean.IR.Borrow.ownVar z) fun x_1 => Lean.IR.Borrow.ownVar x) fun x => Lean.IR.Borrow.ownArgs ys | Lean.IR.Expr.pap x xs => SeqRight.seqRight (Lean.IR.Borrow.ownVar z) fun x => Lean.IR.Borrow.ownArgs xs | other => pure ()
Equations
- Lean.IR.Borrow.preserveTailCall x v b = do let ctx ← read match v, b with | Lean.IR.Expr.fap g ys, Lean.IR.FnBody.ret (Lean.IR.Arg.var z) => if (Array.any ctx.decls (fun a => Lean.IR.Decl.name a == g) 0 (Array.size ctx.decls) && x == z) = true then do let ps ← Lean.IR.Borrow.getParamInfo (Lean.IR.Borrow.ParamMap.Key.decl g) Lean.IR.Borrow.ownParamsUsingArgs ys ps else pure PUnit.unit | x, x_1 => pure ()
Equations
- Lean.IR.Borrow.updateParamSet ctx ps = { env := ctx.env, decls := ctx.decls, currFn := ctx.currFn, paramSet := Array.foldl (fun s p => Std.RBTree.insert s p.x.idx) ctx.paramSet ps 0 (Array.size ps) }
Equations
- Lean.IR.Borrow.collectDecl x = match x with | Lean.IR.Decl.fdecl f ys x b x_1 => withReader (fun ctx => let ctx := Lean.IR.Borrow.updateParamSet ctx ys; { env := ctx.env, decls := ctx.decls, currFn := f, paramSet := ctx.paramSet }) do Lean.IR.Borrow.collectFnBody b Lean.IR.Borrow.updateParamMap (Lean.IR.Borrow.ParamMap.Key.decl f) | x => pure ()
@[inline]
Equations
- Lean.IR.Borrow.collectDecls = do let a ← read Lean.IR.Borrow.whileModifing (Array.forM Lean.IR.Borrow.collectDecl a.decls 0 (Array.size a.decls)) let s ← get pure s.paramMap
Equations
- Lean.IR.Borrow.infer env decls = StateT.run' (Lean.IR.Borrow.collectDecls { env := env, decls := decls, currFn := default, paramSet := ∅ }) { owned := ∅, modified := false, paramMap := Lean.IR.Borrow.mkInitParamMap env decls }
Equations
- Lean.IR.inferBorrow decls = do let env ← Lean.IR.getEnv let paramMap : Lean.IR.Borrow.ParamMap := Lean.IR.Borrow.infer env decls pure (Lean.IR.Borrow.applyParamMap decls paramMap)