| /- | |
| Copyright (c) 2019 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.IR.Format | |
| public import Lean.Compiler.ExportAttr | |
| public import Lean.Compiler.LCNF.PhaseExt | |
| import Lean.Compiler.InitAttr | |
| import Init.Data.Format.Macro | |
| public section | |
| namespace Lean.IR | |
| inductive LogEntry where | |
| | step (cls : Name) (decls : Array Decl) | |
| | message (msg : Format) | |
| namespace LogEntry | |
| protected def fmt : LogEntry → Format | |
| | step cls decls => Format.bracket "[" (format cls) "]" ++ decls.foldl (fun fmt decl => fmt ++ Format.line ++ format decl) Format.nil | |
| | message msg => msg | |
| instance : ToFormat LogEntry := ⟨LogEntry.fmt⟩ | |
| end LogEntry | |
| abbrev Log := Array LogEntry | |
| def Log.format (log : Log) : Format := | |
| log.foldl (init := Format.nil) fun fmt entry => | |
| f!"{fmt}{Format.line}{entry}" | |
| def Log.toString (log : Log) : String := | |
| log.format.pretty | |
| abbrev CompilerM := CoreM | |
| def log (entry : LogEntry) : CompilerM Unit := | |
| addTrace `Compiler.IR m!"{entry}" | |
| def tracePrefixOptionName := `trace.compiler.ir | |
| private def isLogEnabledFor (opts : Options) (optName : Name) : Bool := | |
| match opts.get? optName with | |
| | some (DataValue.ofBool v) => v | |
| | _ => opts.getBool tracePrefixOptionName | |
| private def logDeclsAux (optName : Name) (cls : Name) (decls : Array Decl) : CompilerM Unit := do | |
| if isLogEnabledFor (← getOptions) optName then | |
| log (LogEntry.step cls decls) | |
| @[inline] def logDecls (cls : Name) (decl : Array Decl) : CompilerM Unit := | |
| logDeclsAux (tracePrefixOptionName ++ cls) cls decl | |
| private def logMessageIfAux {α : Type} [ToFormat α] (optName : Name) (a : α) : CompilerM Unit := do | |
| if isLogEnabledFor (← getOptions) optName then | |
| log (LogEntry.message (format a)) | |
| @[inline] def logMessageIf {α : Type} [ToFormat α] (cls : Name) (a : α) : CompilerM Unit := | |
| logMessageIfAux (tracePrefixOptionName ++ cls) a | |
| @[inline] def logMessage {α : Type} [ToFormat α] (a : α) : CompilerM Unit := | |
| logMessageIfAux tracePrefixOptionName a | |
| abbrev DeclMap := PHashMap Name Decl | |
| private abbrev declLt (a b : Decl) := | |
| Name.quickLt a.name b.name | |
| private abbrev sortDecls (decls : Array Decl) : Array Decl := | |
| decls.qsort declLt | |
| private abbrev findAtSorted? (decls : Array Decl) (declName : Name) : Option Decl := | |
| let tmpDecl := Decl.extern declName #[] default default | |
| decls.binSearch tmpDecl declLt | |
| builtin_initialize declMapExt : SimplePersistentEnvExtension Decl DeclMap ← | |
| registerSimplePersistentEnvExtension { | |
| addImportedFn := fun _ => {} | |
| addEntryFn := fun s d => s.insert d.name d | |
| -- Store `meta` closure only in `.olean`, turn all other decls into opaque externs. | |
| -- Leave storing the remainder for `meta import` and server `#eval` to `exportIREntries` below. | |
| exportEntriesFnEx? := some fun env s entries _ => | |
| let decls := entries.foldl (init := #[]) fun decls decl => decls.push decl | |
| let entries := sortDecls decls | |
| -- Do not save all IR even in .olean.private as it will be in .ir anyway | |
| if env.header.isModule then | |
| entries.filterMap fun d => do | |
| if isDeclMeta env d.name then | |
| return d | |
| guard <| Compiler.LCNF.isDeclPublic env d.name | |
| -- Bodies of imported IR decls are not relevant for codegen, only interpretation | |
| match d with | |
| | .fdecl f xs ty b info => | |
| if let some (.str _ s) := getExportNameFor? env f then | |
| return .extern f xs ty { entries := [.standard `all s] } | |
| else | |
| return .extern f xs ty { entries := [.opaque] } | |
| | d => some d | |
| else entries | |
| -- Written to on codegen environment branch but accessed from other elaboration branches when | |
| -- calling into the interpreter. We cannot use `async` as the IR declarations added may not | |
| -- share a name prefix with the top-level Lean declaration being compiled, e.g. from | |
| -- specialization. | |
| asyncMode := .sync | |
| replay? := some <| SimplePersistentEnvExtension.replayOfFilter (!·.contains ·.name) (fun s d => s.insert d.name d) | |
| } | |
| @[export lean_ir_export_entries] | |
| private def exportIREntries (env : Environment) : Array (Name × Array EnvExtensionEntry) := | |
| let irDecls := declMapExt.getEntries env |>.foldl (init := #[]) fun decls decl => decls.push decl | |
| -- safety: cast to erased type | |
| let irEntries : Array EnvExtensionEntry := unsafe unsafeCast <| sortDecls irDecls | |
| -- save all initializers independent of meta/private. Non-meta initializers will only be used when | |
| -- .ir is actually loaded, and private ones iff visible. | |
| let initDecls : Array (Name × Name) := | |
| regularInitAttr.ext.exportEntriesFn env (regularInitAttr.ext.getState env) .private | |
| -- safety: cast to erased type | |
| let initDecls : Array EnvExtensionEntry := unsafe unsafeCast initDecls | |
| #[(declMapExt.name, irEntries), | |
| (Lean.regularInitAttr.ext.name, initDecls)] | |
| def findEnvDecl (env : Environment) (declName : Name) (includeServer := false): Option Decl := | |
| match env.getModuleIdxFor? declName with | |
| | some modIdx => | |
| -- `meta import/import all` and, optionally, additional server-mode IR | |
| guard (includeServer || env.header.modules[modIdx]?.any (·.irPhases != .runtime)) *> | |
| findAtSorted? (declMapExt.getModuleIREntries env modIdx) declName <|> | |
| -- (closure of) `meta def`; will report `.extern`s for other `def`s so needs to come second | |
| findAtSorted? (declMapExt.getModuleEntries env modIdx) declName | |
| | none => declMapExt.getState env |>.find? declName | |
| @[export lean_ir_find_env_decl] | |
| private def findInterpDecl (env : Environment) (declName : Name) : Option Decl := | |
| findEnvDecl (includeServer := true) env declName | |
| namespace ExplicitBoxing | |
| def mkBoxedName (n : Name) : Name := | |
| Name.mkStr n "_boxed" | |
| def isBoxedName (name : Name) : Bool := | |
| name matches .str _ "_boxed" | |
| end ExplicitBoxing | |
| /-- Like ``findInterpDecl env (declName ++ `_boxed)`` but with optimized negative lookup. -/ | |
| @[export lean_ir_find_env_decl_boxed] | |
| private def findInterpDeclBoxed (env : Environment) (declName : Name) : Option Decl := | |
| let boxed := ExplicitBoxing.mkBoxedName declName | |
| -- Important: get module index of base name, not boxed version. Usually the interpreter never | |
| -- does negative lookups except in the case of `call_boxed` which must check whether a boxed | |
| -- version exists. If `declName` exists as an imported declaration but `declName'` doesn't, the | |
| -- latter's module index would be `none` and we may do an expensive blocking wait on the | |
| -- environment extension state even if in this situation we definitely know that `declName'` is | |
| -- not a local declaration. | |
| match env.getModuleIdxFor? declName with | |
| | some modIdx => | |
| findAtSorted? (declMapExt.getModuleIREntries env modIdx) boxed <|> | |
| findAtSorted? (declMapExt.getModuleEntries env modIdx) boxed | |
| | none => declMapExt.getState env |>.find? boxed | |
| @[export lean_has_compile_error] | |
| private def hasCompileError (env : Environment) (constName : Name) : Bool := | |
| match env.getModuleIdxFor? constName with | |
| | some _ => false -- Compile errors in imports would have stopped the build before this point | |
| -- TODO: do we need to store failures as a separate state? Not if we make sure to only ever | |
| -- evaluate constants previously called `compileDecl` on. | |
| | none => !(declMapExt.getState env |>.contains constName) | |
| def findDecl (n : Name) : CompilerM (Option Decl) := | |
| return findEnvDecl (← getEnv) n | |
| def containsDecl (n : Name) : CompilerM Bool := | |
| return (← findDecl n).isSome | |
| def getDecl (n : Name) : CompilerM Decl := do | |
| let (some decl) ← findDecl n | throwError s!"unknown declaration `{n}`" | |
| return decl | |
| def findLocalDecl (n : Name) : CompilerM (Option Decl) := | |
| return declMapExt.getState (← getEnv) |>.find? n | |
| /-- Returns the list of IR declarations in reverse declaration order. -/ | |
| def getDecls (env : Environment) : List Decl := | |
| declMapExt.getEntries env | |
| def addDecl (decl : Decl) : CompilerM Unit := do | |
| modifyEnv (declMapExt.addEntry · decl) | |
| def addDecls (decls : Array Decl) : CompilerM Unit := | |
| decls.forM addDecl | |
| def findEnvDecl' (env : Environment) (n : Name) (decls : Array Decl) : Option Decl := | |
| match decls.find? (fun decl => decl.name == n) with | |
| | some decl => some decl | |
| | none => findEnvDecl env n | |
| def findDecl' (n : Name) (decls : Array Decl) : CompilerM (Option Decl) := | |
| return findEnvDecl' (← getEnv) n decls | |
| def containsDecl' (n : Name) (decls : Array Decl) : CompilerM Bool := do | |
| if decls.any fun decl => decl.name == n then | |
| return true | |
| else | |
| containsDecl n | |
| def getDecl' (n : Name) (decls : Array Decl) : CompilerM Decl := do | |
| let (some decl) ← findDecl' n decls | throwError s!"unknown declaration `{n}`" | |
| return decl | |
| @[export lean_decl_get_sorry_dep] | |
| def getSorryDep (env : Environment) (declName : Name) : Option Name := | |
| match findEnvDecl env declName with | |
| | some (.fdecl (info := { sorryDep? := dep?, .. }) ..) => dep? | |
| | _ => none | |
| /-- Returns additional names that compiler env exts may want to call `getModuleIdxFor?` on. -/ | |
| @[export lean_get_ir_extra_const_names] | |
| private def getIRExtraConstNames (env : Environment) (level : OLeanLevel) (includeDecls := false) : Array Name := | |
| declMapExt.getEntries env |>.toArray.map (·.name) | |
| |>.filter fun n => (includeDecls || !env.contains n) && | |
| (level == .private || Compiler.LCNF.isDeclPublic env n || isDeclMeta env n) | |
| end IR | |
| end Lean |