blob: e86aba735d34040b67b442a826e2331cab91b15e [file]
/-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Compiler.LCNF.Simp.SimpM
public section
namespace Lean.Compiler.LCNF
namespace Simp
/--
Result of `inlineCandidate?`.
It contains information for inlining local and global functions.
-/
structure InlineCandidateInfo where
isLocal : Bool
params : Array (Param .pure)
/-- Value (lambda expression) of the function to be inlined. -/
value : Code .pure
fType : Expr
args : Array (Arg .pure)
/-- `ifReduce = true` if the declaration being inlined was tagged with `inline_if_reduce`. -/
ifReduce : Bool
/-- `recursive = true` if the declaration being inline is in a mutually recursive block. -/
recursive : Bool := false
/-- The arity (aka number of parameters) of the function to be inlined. -/
def InlineCandidateInfo.arity : InlineCandidateInfo → Nat
| { params, .. } => params.size
/--
Return `some info` if `e` should be inlined.
-/
def inlineCandidate? (e : LetValue .pure) : SimpM (Option InlineCandidateInfo) := do
let mut e := e
let mut mustInline := false
if let .const ``inline _ #[_, .fvar argFVarId] := e then
mustInline := true
if let some decl ← findFunDecl'? (pu := .pure) argFVarId then
e := .fvar decl.fvarId #[]
else if let some decl ← findLetDecl? argFVarId then
e := decl.value
if let .const declName _ _ := e then
if (← isCtor? declName).isSome then
throwError m!"`inline` applied to constructor '{declName}' is invalid"
else if (← getLocalDecl? declName).isNone then
throwError m!"`inline` applied to non-local declaration '{declName}' is invalid"
else
assert! (← findParam? (pu := .pure) argFVarId).isSome
throwError m!"`inline` applied to parameters is invalid"
if let .const declName us args := e then
unless (← read).config.inlineDefs do
return none
let some ⟨.pure, decl⟩ ← getDecl? declName | return none
let .code code := decl.value | return none
let shouldInline : SimpM Bool := do
if !decl.inlineIfReduceAttr && decl.recursive then return false
if mustInline then return true
/-
We don't inline instances tagged with `[inline]/[always_inline]/[inline_if_reduce]` at the base phase.
We assume that at the base phase these annotations are for the instance methods that will be lambda lifted during the base phase.
Reason: we eagerly lambda lift local functions occurring at instances before saving their code at
the end of the base phase. The goal is to make them cheap to inline in actual code.
By inlining their definitions we would be just generating extra work for the lambda lifter.
-/
if (← inBasePhase) then
/-
We claim it is correct to use `Meta.isInstance` because
1. `shouldInline` is called during LCNF compilation, which runs at `addDecl` time
2. Any instance referenced in the code was found by type class resolution during elaboration
3. For TC resolution to find it, the scope was active during elaboration
4. LCNF compilation happens before the scope changes
We use `Meta.isInstance` rather than `isInstanceReducible` here because we specifically
want the TC-scoped notion: declarations active as instances during elaboration. Manual
aux decls bearing `[instance_reducible]` without `[instance]` should not count.
-/
if (← Meta.isInstance decl.name) then
return false
-- This is done to avoid inlining `_override` implementations for computed fields in the
-- base phase, since `cases` constructs have not yet been replaced by their underlying
-- implementation, and thus inlining `_override` implementations for computed fields will
-- expose a constructor/`cases` mismatch.
-- TODO: Find a better solution for this problem.
if decl.name matches .str _ "_override" then return false
if decl.alwaysInlineAttr then return true
-- TODO: check inlining quota
if decl.inlineAttr || decl.inlineIfReduceAttr then return true
if decl.noinlineAttr then return false
isSmall code
unless (← shouldInline) do return none
/- check arity -/
let arity := decl.getArity
let inlinePartial := (← read).config.inlinePartial
if !mustInline && !inlinePartial && args.size < arity then return none
if decl.inlineIfReduceAttr then
let some paramIdx := decl.isCasesOnParam? | return none
unless paramIdx < args.size do return none
let arg := args[paramIdx]!
unless (← arg.isConstructorApp) do return none
let params := decl.instantiateParamsLevelParams us
let value := code.instantiateValueLevelParams decl.levelParams us
let type := decl.instantiateTypeLevelParams us
incInline
return some {
isLocal := false
fType := type
args := args
ifReduce := decl.inlineIfReduceAttr
recursive := decl.recursive
params, value
}
else if let .fvar f args := e then
let some decl ← findFunDecl'? f | return none
unless mustInline || args.size > 0 do return none -- It is not worth to inline a local function that does not take any arguments
unless mustInline || (← shouldInlineLocal decl) do return none
-- Remark: we inline local function declarations even if they are partial applied
incInlineLocal
modify fun s => { s with inlineLocal := s.inlineLocal + 1 }
return some {
isLocal := true
fType := (← getType f)
args := args
params := decl.params
value := decl.value
ifReduce := false
}
else
return none
builtin_initialize
registerTraceClass `Compiler.simp.inline