Separate Lean IR generation (`compiler.postponeCompile = true`) fails with `Unknown constant` when importing modules built with inline codegen (including `Init`/`Lean`)

Aperta
#16,872 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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-256 2b7ab1ed8b92d2e6abfaf41344823b510064dbc9c0260cac73eb496ab2dea6eb)
  • 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 = true in [leanOptions]: fails (exit 1) with the exact two leanir errors 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

  1. Inline-compiled upstream modules (including the shipped stdlib) write empty .ir.sig placeholders.
    In src/Lean/Environment.lean (writeModule, lines 2243–2257), when lean runs inline codegen (writeIR := true), it writes an empty default : ModuleData to .ir.sig ((fname.withExtension "ir.sig", default) -- to be filled by leanir instead). In the official v4.36.0 Linux package, lib/lean/Init/Data/Array/Basic.ir.sig and lib/lean/Lean/Environment.ir.sig are both 176-byte empty ModuleData files (entries = #[], extraConstNames = #[]), whereas .olean carries extraConstNames := getIRExtraConstNames env level (line 2225) and .ir carries the full IR data (336,816 B and 1,213,136 B respectively).

  2. leanir loads .ir.sig for non-import all imports, and finalizeImport ignores .olean's extraConstNames.

    • Lake's Module.recBuildIRArts (src/lake/Lake/Build/Module.lean:1300-1320) invokes leanir on irsetup.json once elaboration completes.
    • leanir (src/LeanIR.lean:93-100) imports the target module via importModulesCore (globalLevel := .exported) (loadIRSig := true) and finalizeImport ... (level := .exported) (loadIRSig := true).
    • Under loadIRSig := true, ImportedModule.irData? (src/Lean/Environment.lean:2410-2418) selects self.irParts[0]? (.ir.sig) for every module imported without import all (including Init.* and Lean.*). In contrast, inline lean (loadIRSig := false) selects self.mainModule? (.olean) for normal imports or self.irParts.back? (.ir) for meta / import all imports.
    • In finalizeImport (src/Lean/Environment.lean:2710-2712, 2749), const2ModIdx populates extraConstNames only from irData[modIdx]? — never from moduleData[modIdx] (.olean), even though .olean already contains extraConstNames. Because the stdlib .ir.sig is an empty 176-byte default, const2ModIdx never receives Init/Lean compiler-generated or private-auxiliary extraConstNames (such as _private.Init.Data.Array.Basic.0.Array.forIn'Unsafe.loop, Array.instInhabited._redArg, or Lean.PersistentEnvExtension.getState._redArg), which are not in .olean's kernel constNames.
  3. LCNF lookup falls back to getConstInfo and throws Unknown constant.
    When leanir compiles 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) calls env.getModuleIdxFor? declName (const2ModIdx[declName]?). Because declName is missing from const2ModIdx, getModuleIdxFor? returns none (even though the baseExt/monoExt entry is present in .olean's moduleData), so getBaseDecl?/getMonoDecl? returns none. LCNF.InferType.inferConstType (src/Lean/Compiler/LCNF/InferType.lean:101-108) then falls back to getOtherDeclType → getOtherDeclBaseType (src/Lean/Compiler/LCNF/BaseTypes.lean:22-23) → getConstInfo (src/Lean/MonadEnv.lean:88) → throwUnknownConstant.

  4. Control confirmation.
    In a two-module project (A defining an @[inline] function with let rec @[specialize], B doing public import A) where both A and B are compiled with compiler.postponeCompile = true, leanir writes a populated A.ir.sig (5,264 bytes via mkIRSigData in src/LeanIR.lean:35-39) and lake build B succeeds (exit 0). Any compiler.postponeCompile = true build importing a module compiled with inline codegen — which includes the shipped Init/Std/Lean stdlib itself — triggers the bug.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
16h 45m
PR unite (30g)
361

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.