Separate Lean IR generation (`compiler.postponeCompile = true`) fails with `Unknown constant` when importing modules built with inline codegen (including `Init`/`Lean`)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- compilers
Direzione di ricerca
Start with src/Lean/Environment.lean and trace how finalizeImport populates const2ModIdx when loadIRSig is enabled; compare the imported .ir.sig data with the .olean extraConstNames. Then check src/LeanIR.lean and src/Lean/Compiler/LCNF/Basic.lean for the import and lookup paths. Done means the provided reproductions and Batteries leaf targets build with compiler.postponeCompile = true.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
On the official Lean v4.36.0 Linux distribution (611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9), enabling compiler.postponeCompile = true causes leanir (:irArts) to fail with Unknown constant on compiler-generated / private auxiliary declarations from Init and Lean — even on a 7-line zero-dependency module — while default inline code generation succeeds on the exact same code.
This reproduces both on Batteries (b4b25a3886a16daacaf6615c2eea7170a6e8f458) and on standalone 7-line modules with no external package dependencies.
Environment
- Official release archive:
lean-4.36.0-linux.tar.zst(SHA-2562b7ab1ed8b92d2e6abfaf41344823b510064dbc9c0260cac73eb496ab2dea6eb) lean --version:Lean (version 4.36.0, x86_64-unknown-linux-gnu, commit 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9, Release)lake --version:Lake version 5.0.0-src+611c117 (Lean version 4.36.0)
Minimal Standalone Reproduction (7 lines, zero dependencies)
lean-toolchain:
leanprover/lean4:v4.36.0
lakefile.toml:
name = "repro"
defaultTargets = ["Repro"]
[leanOptions]
compiler.postponeCompile = true
[[lean_lib]]
name = "Repro"
Repro.lean:
module
public def sumArray (xs : Array Nat) : Nat := Id.run do
let mut s := 0
for x in xs do
s := s + x
return s
Running lake -v build Repro builds Repro (elaboration) cleanly and then fails in Repro:irArts:
✖ [3/4] Building Repro:irArts (167ms)
trace: .> LEAN_PATH=.../min-repro-array/.lake/build/lib/lean .../bin/leanir .../Repro.irsetup.json .../Repro.ir .../Repro.c
info: stderr:
.../Repro.ir:0:0: error: Failed to compile `sumArray`
Unknown constant `_private.Init.Data.Array.Basic.0.Array.forIn'Unsafe.loop`
error: external command '.../bin/leanir' exited with code 1
Commenting out compiler.postponeCompile = true and rebuilding (rm -rf .lake && lake -v build Repro) completes cleanly (exit 0).
A second 7-line module importing only Lean.Environment:
module
public import Lean.Environment
public def getExtState (ext : Lean.PersistentEnvExtension Nat Nat (Array Nat))
(env : Lean.Environment) : Array Nat :=
ext.getState env
also succeeds in default inline mode (exit 0) and fails under compiler.postponeCompile = true (exit 1):
error: Failed to compile `getExtState`
Unknown constant `Array.instInhabited._redArg`
Official v4.36.0 Verification on Batteries (b4b25a3886a16daacaf6615c2eea7170a6e8f458)
Building the leaf targets Batteries.Lean.TagAttribute and Batteries.Lean.Position with the official v4.36.0 Linux binary:
- Default inline mode (
lake -v build Batteries.Lean.TagAttribute Batteries.Lean.Position): succeeds (exit 0, 5 jobs). - With
compiler.postponeCompile = truein[leanOptions]: fails (exit 1) with the exact twoleanirerrors previously observed on the full Batteries build:Batteries.Lean.TagAttribute:irArts error: Failed to compile `Lean.TagAttribute.getDecls.core` Unknown constant `_private.Init.Data.Array.Basic.0.Array.forIn'Unsafe.loop` Batteries.Lean.Position:irArts error: Failed to compile `Lean.Position.getDeclsAfter` Unknown constant `Lean.PersistentEnvExtension.getState._redArg`
Root Cause Analysis
-
Inline-compiled upstream modules (including the shipped stdlib) write empty
.ir.sigplaceholders.
Insrc/Lean/Environment.lean(writeModule, lines 2243–2257), whenleanruns inline codegen (writeIR := true), it writes an emptydefault : ModuleDatato.ir.sig((fname.withExtension "ir.sig", default) -- to be filled by leanir instead). In the officialv4.36.0Linux package,lib/lean/Init/Data/Array/Basic.ir.sigandlib/lean/Lean/Environment.ir.sigare both 176-byte emptyModuleDatafiles (entries = #[],extraConstNames = #[]), whereas.oleancarriesextraConstNames := getIRExtraConstNames env level(line 2225) and.ircarries the full IR data (336,816 B and 1,213,136 B respectively). -
leanirloads.ir.sigfor non-import allimports, andfinalizeImportignores.olean'sextraConstNames.- Lake's
Module.recBuildIRArts(src/lake/Lake/Build/Module.lean:1300-1320) invokesleanironirsetup.jsononce elaboration completes. leanir(src/LeanIR.lean:93-100) imports the target module viaimportModulesCore (globalLevel := .exported) (loadIRSig := true)andfinalizeImport ... (level := .exported) (loadIRSig := true).- Under
loadIRSig := true,ImportedModule.irData?(src/Lean/Environment.lean:2410-2418) selectsself.irParts[0]?(.ir.sig) for every module imported withoutimport all(includingInit.*andLean.*). In contrast, inlinelean(loadIRSig := false) selectsself.mainModule?(.olean) for normal imports orself.irParts.back?(.ir) formeta/import allimports. - In
finalizeImport(src/Lean/Environment.lean:2710-2712,2749),const2ModIdxpopulatesextraConstNamesonly fromirData[modIdx]?— never frommoduleData[modIdx](.olean), even though.oleanalready containsextraConstNames. Because the stdlib.ir.sigis an empty 176-bytedefault,const2ModIdxnever receivesInit/Leancompiler-generated or private-auxiliaryextraConstNames(such as_private.Init.Data.Array.Basic.0.Array.forIn'Unsafe.loop,Array.instInhabited._redArg, orLean.PersistentEnvExtension.getState._redArg), which are not in.olean's kernelconstNames.
- Lake's
-
LCNF lookup falls back to
getConstInfoand throwsUnknown constant.
Whenleanircompiles an inlined/specialized call (e.g.,Array.forIn'→Array.forIn'Unsafe→_private.Init.Data.Array.Basic.0.Array.forIn'Unsafe.loop),findExtEntry?(src/Lean/Compiler/LCNF/Basic.lean:1252-1261) callsenv.getModuleIdxFor? declName(const2ModIdx[declName]?). BecausedeclNameis missing fromconst2ModIdx,getModuleIdxFor?returnsnone(even though thebaseExt/monoExtentry is present in.olean'smoduleData), sogetBaseDecl?/getMonoDecl?returnsnone.LCNF.InferType.inferConstType(src/Lean/Compiler/LCNF/InferType.lean:101-108) then falls back togetOtherDeclType→getOtherDeclBaseType(src/Lean/Compiler/LCNF/BaseTypes.lean:22-23) →getConstInfo(src/Lean/MonadEnv.lean:88) →throwUnknownConstant. -
Control confirmation.
In a two-module project (Adefining an@[inline]function withlet rec @[specialize],Bdoingpublic import A) where bothAandBare compiled withcompiler.postponeCompile = true,leanirwrites a populatedA.ir.sig(5,264 bytes viamkIRSigDatainsrc/LeanIR.lean:35-39) andlake build Bsucceeds (exit 0). Anycompiler.postponeCompile = truebuild importing a module compiled with inline codegen — which includes the shippedInit/Std/Leanstdlib itself — triggers the bug.
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 16h 45m
- PR unite (30g)
- 361
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
leanprover/lean4#16876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16842 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16841 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
leanprover/lean4#16840 ·
I maintainer di solito rispondono entro 1 giorno
-
doc: stray "public" in Bool and HEq documentationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
leanprover/lean4#16825 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
bug core-lang
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentenceAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
mw00/project-maya#1368 ·
I maintainer di solito rispondono entro 2 giorni
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
foundry-rs/foundry#18828 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno