Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ jobs:
lake env lean --run Scripts/CheckCiFilters.lean --self-test
lake env lean --run Scripts/CheckCiFilters.lean
- name: Run primary IxTests
run: lake env .lake/build/bin/IxTests
run: lake env .lake/build/bin/IxTests -j 16
- name: Test Ixon contracts and primitives
run: |
lake env .lake/build/bin/ixon-primitives
Expand Down
15 changes: 13 additions & 2 deletions Ix/Common.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public import Lean.Expr
public import Lean.Declaration
public import Lean.Environment
public import Lean.Elab.Frontend
import Std.Sync.Mutex

public section

Expand Down Expand Up @@ -322,14 +323,24 @@ def checkToolchain : IO Unit := do
if version != expectedVersion then
IO.println s!"Warning: expected toolchain '{expectedVersion}' but got '{version}'"

private initialize leanImportMutex : Std.Mutex Unit ← Std.Mutex.new ()

/-- Serialize `Lean.enableInitializersExecution` + `importModules` / `processHeader`
across concurrent threads: `enableInitializersExecution` sets a process-global
flag that `importModules` resets on completion, and `initSearchPath` mutates
process-global `Lean.searchPathRef`. -/
def _root_.Ix.withImportLock (action : IO α) : IO α :=
leanImportMutex.atomically action

open Elab in
open System (FilePath) in
def runFrontend (input : String) (filePath : FilePath) : IO Environment := do
checkToolchain
let inputCtx := Parser.mkInputContext input filePath.toString
let (header, parserState, messages) ← Parser.parseHeader inputCtx
unsafe enableInitializersExecution -- required for `processHeader`'s `loadExts := true` import
let (env, messages) ← processHeader header default messages inputCtx 0
let (env, messages) ← _root_.Ix.withImportLock do
unsafe enableInitializersExecution -- required for `processHeader`'s `loadExts := true` import
processHeader header default messages inputCtx 0
let env := env.setMainModule default
let commandState := Command.mkState env messages default
let s ← IO.processCommands inputCtx parserState commandState
Expand Down
33 changes: 17 additions & 16 deletions Ix/Meta.lean
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,15 @@ isolation the mode exists for (TruthMines' `module-header-v1` patch) without
corrupting stored content. See `Ix.EnvScope.defaultConstList`. -/
def getFileEnvCore (path : FilePath) : IO FileEnv := do
let path ← IO.FS.realPath path
initLeanSearchPathFor path
let source ← IO.FS.readFile path
let inputCtx := Parser.mkInputContext source path.toString
let (header, parserState, messages) ← Parser.parseHeader inputCtx
-- Required for `processHeaderCore`'s `loadExts := true` import; a completed
-- import resets the flag, so it is re-armed before every import.
unsafe enableInitializersExecution
let (env, messages) ← processHeaderCore
(HeaderSyntax.startPos header) (HeaderSyntax.imports header)
(isModule := false) default messages inputCtx 0
let (env, messages) ← Ix.withImportLock do
initLeanSearchPathFor path
unsafe enableInitializersExecution
processHeaderCore
(HeaderSyntax.startPos header) (HeaderSyntax.imports header)
(isModule := false) default messages inputCtx 0
if messages.hasErrors then
throw $ IO.userError $ "\n\n".intercalate $
(← messages.toList.mapM (·.toString)).map (String.trimAscii · |>.toString)
Expand Down Expand Up @@ -139,15 +138,16 @@ callers union locally-elaborated names (`getModuleIdxFor? · |>.isNone`) from
the full-content env. -/
def moduleVisibleNames (path : FilePath) : IO (Option NameSet) := do
let path ← IO.FS.realPath path
initLeanSearchPathFor path
let source ← IO.FS.readFile path
let inputCtx := Parser.mkInputContext source path.toString
let (header, _, messages) ← Parser.parseHeader inputCtx
if !HeaderSyntax.isModule header then return none
unsafe enableInitializersExecution -- re-armed per import; see `getFileEnvCore`
let (env, messages) ← processHeaderCore
(HeaderSyntax.startPos header) (HeaderSyntax.imports header)
(isModule := true) default messages inputCtx 0
let (env, messages) ← Ix.withImportLock do
initLeanSearchPathFor path
unsafe enableInitializersExecution -- re-armed per import; see `getFileEnvCore`
processHeaderCore
(HeaderSyntax.startPos header) (HeaderSyntax.imports header)
(isModule := true) default messages inputCtx 0
if messages.hasErrors then
throw $ IO.userError $ "\n\n".intercalate $
(← messages.toList.mapM (·.toString)).map (String.trimAscii · |>.toString)
Expand All @@ -171,10 +171,11 @@ initial value — all imported entries sit in raw form but the computed state
σ is empty, which silently breaks any test that reads extension state via
`get_env!`. Matches `Lean.Elab.processHeaderCore`'s import path (used by
`getFileEnv`) and Lake's own `importModulesUsingCache`. -/
def getCompileEnv (imports : Array Name) : IO Environment := do
initLeanSearchPath
unsafe enableInitializersExecution -- required for `loadExts := true`
importModules (imports.map ({ module := · : Import })) default (loadExts := true)
def getCompileEnv (imports : Array Name) : IO Environment :=
Ix.withImportLock do
initLeanSearchPath
unsafe enableInitializersExecution -- required for `loadExts := true`
importModules (imports.map ({ module := · : Import })) default (loadExts := true)

macro "get_env!" : term =>
`(getCompileEnv this_file!)
Expand Down
68 changes: 43 additions & 25 deletions Ix/RoleBinding/Compile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public import Ix.Cert.Compile
public import Ix.EnvScope
public import Ix.LeanExport
public import Ix.Meta
public import Ix.Common
public import IxCK.Kernel.PinGen.Certs

/-! Artifact-specific support and evidence for the production compiler.
Expand Down Expand Up @@ -647,12 +648,14 @@ def elaborateNativeCertSource : IO Lean.Environment := do
let opts := Lean.Options.empty.setBool `linter.deprecated false
let inputCtx := Lean.Parser.mkInputContext nativeCertSourceText "IxCK/Kernel/PinGen/Certs.lean"
let (header, _, _) ← Lean.Parser.parseHeader inputCtx
unsafe Lean.enableInitializersExecution
let some certElabEnv ← Lean.Elab.runFrontend nativeCertSourceText opts
"IxCK/Kernel/PinGen/Certs.lean" `IxCK.Kernel.PinGen.Certs
| throw (IO.userError "nativeCertSource: failed to elaborate embedded IxCK.Kernel.PinGen.Certs")
unsafe Lean.enableInitializersExecution
let baseEnv ← Lean.importModules (Lean.Elab.HeaderSyntax.imports header) default (loadExts := false)
let (certElabEnv, baseEnv) ← _root_.Ix.withImportLock do
unsafe Lean.enableInitializersExecution
let some certElabEnv ← Lean.Elab.runFrontend nativeCertSourceText opts
"IxCK/Kernel/PinGen/Certs.lean" `IxCK.Kernel.PinGen.Certs
| throw (IO.userError "nativeCertSource: failed to elaborate embedded IxCK.Kernel.PinGen.Certs")
unsafe Lean.enableInitializersExecution
let baseEnv ← Lean.importModules (Lean.Elab.HeaderSyntax.imports header) default (loadExts := false)
pure (certElabEnv, baseEnv)
let baseConsts := baseEnv.constants.fold (init := #[]) fun acc n ci => acc.push (n, ci)
let certConsts := certElabEnv.constants.map₂.foldl (init := #[]) fun acc n ci => acc.push (n, ci)
Ix.LeanExport.environment (baseConsts ++ certConsts)
Expand All @@ -665,25 +668,40 @@ without invoking `lake`. -/
def nativeCertSource : IO Lean.Environment := do
if let some env ← cachedNativeCertSource.get then
return env
if (← (← Lean.searchPathRef.get).findWithExt "olean" `Init).isNone then
Lean.initSearchPath (← Lean.findSysroot)
if (← (← Lean.searchPathRef.get).findModuleWithExt "olean" `IxCK.Kernel.PinGen.Certs).isNone then
let mut localDirs : List System.FilePath := []
for dir in ([".lake/kernel/lib/lean", "../.lake/kernel/lib/lean",
"../../.lake/kernel/lib/lean"] : List System.FilePath) do
if ← dir.pathExists then
localDirs := localDirs ++ [dir]
unless localDirs.isEmpty do
let current ← Lean.searchPathRef.get
Lean.searchPathRef.set (localDirs ++ current)
let hasOlean ← match ← (← Lean.searchPathRef.get).findModuleWithExt "olean" `IxCK.Kernel.PinGen.Certs with
| some path => System.FilePath.pathExists (System.FilePath.mk (path.toString ++ ".private"))
| none => pure false
let env ← if hasOlean then
Lean.importModules #[{ module := `IxCK.Kernel.PinGen.Certs }] default (loadExts := false)
else
elaborateNativeCertSource
cachedNativeCertSource.set (some env)
let env ← _root_.Ix.withImportLock do
if let some env ← cachedNativeCertSource.get then
return env
if (← (← Lean.searchPathRef.get).findWithExt "olean" `Init).isNone then
Lean.initSearchPath (← Lean.findSysroot)
if (← (← Lean.searchPathRef.get).findModuleWithExt "olean" `IxCK.Kernel.PinGen.Certs).isNone then
let mut localDirs : List System.FilePath := []
for dir in ([".lake/kernel/lib/lean", "../.lake/kernel/lib/lean",
"../../.lake/kernel/lib/lean"] : List System.FilePath) do
if ← dir.pathExists then
localDirs := localDirs ++ [dir]
unless localDirs.isEmpty do
let current ← Lean.searchPathRef.get
Lean.searchPathRef.set (localDirs ++ current)
let hasOlean ← match ← (← Lean.searchPathRef.get).findModuleWithExt "olean" `IxCK.Kernel.PinGen.Certs with
| some path => System.FilePath.pathExists (System.FilePath.mk (path.toString ++ ".private"))
| none => pure false
let env ← if hasOlean then
Lean.importModules #[{ module := `IxCK.Kernel.PinGen.Certs }] default (loadExts := false)
else do
let opts := Lean.Options.empty.setBool `linter.deprecated false
let inputCtx := Lean.Parser.mkInputContext nativeCertSourceText "IxCK/Kernel/PinGen/Certs.lean"
let (header, _, _) ← Lean.Parser.parseHeader inputCtx
unsafe Lean.enableInitializersExecution
let some certElabEnv ← Lean.Elab.runFrontend nativeCertSourceText opts
"IxCK/Kernel/PinGen/Certs.lean" `IxCK.Kernel.PinGen.Certs
| throw (IO.userError "nativeCertSource: failed to elaborate embedded IxCK.Kernel.PinGen.Certs")
unsafe Lean.enableInitializersExecution
let baseEnv ← Lean.importModules (Lean.Elab.HeaderSyntax.imports header) default (loadExts := false)
let baseConsts := baseEnv.constants.fold (init := #[]) fun acc n ci => acc.push (n, ci)
let certConsts := certElabEnv.constants.map₂.foldl (init := #[]) fun acc n ci => acc.push (n, ci)
Ix.LeanExport.environment (baseConsts ++ certConsts)
cachedNativeCertSource.set (some env)
return env
return env

/-- Release a `CheckedStore`'s decoded record map on a background task after
Expand Down
7 changes: 5 additions & 2 deletions Tests/AggrBindingReuse.lean
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public section

namespace Tests.Aggr.BindingReuse

open LSpec

/-- The closures of `names` and of the constants the kernel fabricates
during reduction, with every inductive's recursors
(`IxVM.ClaimHarness.collectCertifiedClosure`: the binder-data annotator
Expand Down Expand Up @@ -180,8 +182,9 @@ def runWith (name : String) (aggregate : Bool) : IO UInt32 := do
bindingProof aggregate
let right ← claimSet exe home dir "right"
rightEnv 2 bindingProof aggregate (traceShards := true)
LSpec.lspecIO (.ofList [(name,
(left ++ right).toList.map fun (label, ok) => LSpec.test label ok)]) []
LSpec.lspecMain (describe name do
for (label, ok) in left ++ right do
itBool label ok) []
finally
IO.FS.removeDirAll home
IO.FS.removeDirAll dir
Expand Down
Loading
Loading