Skip to content

Commit 81c5655

Browse files
tydeuclaude
andcommitted
feat: lake: seperate leanir job
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 03cd78e commit 81c5655

8 files changed

Lines changed: 297 additions & 95 deletions

File tree

src/LeanIR.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import all Lean.Util.Path
1111
import all Lean.Environment
1212
import Lean.Compiler.Options
1313
import Lean.Compiler.IR.CompilerM
14+
import Lean.Compiler.ModPkgExt
1415

1516
import all Lean.Compiler.CSimpAttr
1617
import Lean.Compiler.LCNF.EmitC
@@ -90,6 +91,9 @@ public def main (args : List String) : IO UInt32 := do
9091
-- level exported because otherwise we would try to load the current module's `.ir`
9192
finalizeImport (leakEnv := true) (loadExts := false) (level := .exported) (loadIRSig := true) s imports opts
9293
let env := env.setMainModule modName
94+
-- `finalizeImport` above runs with `loadExts := false`, so the package recorded by `lean` in
95+
-- `modPkgExt` is not restored. Native symbols are prefixed by it, so take it from the setup.
96+
let env := env.setModulePackage setup.package?
9397

9498
let initExt {α β σ} [Inhabited σ] (ext : PersistentEnvExtension α β σ) (env : Environment) : IO Environment := do
9599
let s := ext.toEnvExtension.getState env

src/lake/Lake/Build/Actions.lean

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,26 @@ open Lean hiding SearchPath
2626

2727
namespace Lake
2828

29+
public def compileLeanIR
30+
(setupFile irFile cFile : FilePath)
31+
(leanPath : SearchPath := [])
32+
(leanir : FilePath := "leanir")
33+
: LogIO Unit := do
34+
createParentDirs irFile
35+
createParentDirs cFile
36+
proc {
37+
cmd := leanir.toString
38+
args := #[setupFile.toString, irFile.toString, cFile.toString]
39+
env := #[("LEAN_PATH", leanPath.toString)]
40+
}
41+
2942
public def compileLeanModule
3043
(leanFile relLeanFile : FilePath)
3144
(setup : ModuleSetup) (setupFile : FilePath)
3245
(arts : ModuleArtifacts)
3346
(leanArgs : Array String := #[])
3447
(leanPath : SearchPath := [])
3548
(lean : FilePath := "lean")
36-
(leanir : FilePath := "leanir")
3749
: LogIO Unit := do
3850
let mut args := leanArgs.push leanFile.toString
3951
if let some oleanFile := arts.olean? then
@@ -91,22 +103,6 @@ public def compileLeanModule
91103
failure
92104
else if out.exitCode ≠ 0 || hasErrors then
93105
error s!"Lean exited with code {out.exitCode}"
94-
if postponeCompile then
95-
if let (some irFile, some cFile) := (arts.ir?, arts.c?) then
96-
createParentDirs irFile
97-
createParentDirs cFile
98-
try
99-
proc {
100-
cmd := leanir.toString
101-
args := #[setupFile.toString, irFile.toString, cFile.toString]
102-
env := #[
103-
("LEAN_PATH", leanPath.toString)
104-
]
105-
}
106-
catch e =>
107-
if let some oleanFile := arts.olean? then
108-
removeFileIfExists oleanFile
109-
throw e
110106

111107
public def compileO
112108
(oFile srcFile : FilePath)

src/lake/Lake/Build/Facets.lean

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public structure ModuleImportInfo where
8080
allTransTrace : BuildTrace
8181
/-- Transitive import trace for an `import` of the module without the module system enabled. -/
8282
legacyTransTrace : BuildTrace
83+
/-- The trace produced by mixing the `leanir` traces of `directArts` with their transitive imports. -/
84+
irSigTrace : BuildTrace
85+
/-- Transitive import trace for a `leanir` run over an importer of the module. -/
86+
irSigTransTrace : BuildTrace
8387
deriving Inhabited
8488

8589
/-- **For internal use only.** Information about the imports of this module. -/
@@ -93,28 +97,39 @@ public structure ModuleExportInfo where
9397
arts : ImportArtifacts
9498
/-- The trace of the module's public olean. -/
9599
artsTrace : BuildTrace
96-
/-- The trace of the module's public olean and IR. -/
100+
/-- Transitive import trace for an `import` of the module with the module system enabled. -/
101+
transTrace : BuildTrace
102+
deriving Inhabited
103+
104+
/-- Information useful to importers of a module. Includes IR. -/
105+
public structure ModuleMetaExportInfo extends ModuleExportInfo where
106+
/-- The trace of the module's public olean and IR (i.e., what a `meta import` needs). -/
97107
metaArtsTrace : BuildTrace
108+
/-- The trace of the module's public olean and IR signature (i.e., what `leanir` needs). -/
109+
irSigArtsTrace : BuildTrace
98110
/--
99111
Artifacts directly needed for an `import` of the module from a module without the module
100112
system enabled or `import all` of the module from a module with it enabled.
101113
-/
102114
allArts : ImportArtifacts
103115
/-- The trace produced by mixing the traces of `allArts`. -/
104116
allArtsTrace : BuildTrace
105-
/-- Transitive import trace for an `import` of the module with the module system enabled. -/
106-
transTrace : BuildTrace
107117
/-- Transitive import trace for a `meta import` of the module. -/
108118
metaTransTrace : BuildTrace
109119
/-- Transitive import trace for an `import all` of the module. -/
110120
allTransTrace : BuildTrace
111121
/-- Transitive import trace for an `import` of the module without the module system enabled. -/
112122
legacyTransTrace : BuildTrace
123+
/-- Transitive import trace for a `leanir` run over an importer of the module. -/
124+
irSigTransTrace : BuildTrace
113125
deriving Inhabited
114126

115127
/-- **For internal use only.** Information useful to importers of this module. -/
116128
builtin_facet exportInfo : Module => ModuleExportInfo
117129

130+
/-- **For internal use only.** Information useful to `meta` importers of this module. -/
131+
builtin_facet metaExportInfo : Module => ModuleMetaExportInfo
132+
118133
/-- Artifacts directly needed for an `import` of this module with the module system enabled. -/
119134
builtin_facet importArts : Module => ImportArtifacts
120135

@@ -132,6 +147,9 @@ Its trace just includes its dependencies.
132147
-/
133148
builtin_facet leanArts : Module => ModuleOutputArtifacts
134149

150+
/-- The artifacts of a Lean module's code generation (e.g., `.ir.sig`, `.ir`, `.c`). -/
151+
builtin_facet irArts : Module => ModuleOutputArtifacts
152+
135153
/-- A compressed archive (produced via `leantar`) of the module's build artifacts. -/
136154
builtin_facet ltar : Module => FilePath
137155

src/lake/Lake/Build/Infos.lean

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ builtin_facet imports : Module => Array Module
8383
/-- Dynamic information computed about a module before building. -/
8484
public structure ModulePreSetup where
8585
trace : BuildTrace
86+
irSigTrace : BuildTrace
8687
srcMTime : MTime
8788
srcFile : FilePath
8889
isModule : Bool
@@ -181,6 +182,9 @@ namespace Module
181182
@[inherit_doc exportInfoFacet] public abbrev exportInfo (self : Module) :=
182183
self.facetCore exportInfoFacet
183184

185+
@[inherit_doc metaExportInfoFacet] public abbrev metaExportInfo (self : Module) :=
186+
self.facetCore metaExportInfoFacet
187+
184188
@[inherit_doc importArtsFacet] public abbrev importArts (self : Module) :=
185189
self.facetCore importArtsFacet
186190

@@ -190,6 +194,9 @@ namespace Module
190194
@[inherit_doc leanArtsFacet] public abbrev leanArts (self : Module) :=
191195
self.facetCore leanArtsFacet
192196

197+
@[inherit_doc irArtsFacet] public abbrev irArts (self : Module) :=
198+
self.facetCore irArtsFacet
199+
193200
@[inherit_doc oleanFacet] public abbrev olean (self : Module) :=
194201
self.facetCore oleanFacet
195202

0 commit comments

Comments
 (0)