Skip to content

Commit 32295f1

Browse files
committed
Run __init__() even in sysimage builds
This change makes the LinearAlgebra initialization code run during the sysimage build, similar to how it would run when using LinearAlgebra at top-level in a normal package. In this configuration, we can't rely on the serializer to isolate side-effects for us, so we have to manually ensure that any mutations that we perform here are not accidentally persisted to run-time. Accordingly, the `OncePerProcess` is here as a reminder that any code in this init have to think about phasing (and be careful not to leave behind changes from compile-time that would badly impact run-time).
1 parent 8cc4216 commit 32295f1

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/LinearAlgebra.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,14 +820,17 @@ function versioninfo(io::IO=stdout)
820820
return nothing
821821
end
822822

823-
function __init__()
823+
# This OncePerProcess does not yield a useful value, but it causes side-effects (configuring LBT / BLAS)
824+
# which should only last for the duration of the current process.
825+
const initialize_lbt = Base.OncePerProcess{Nothing} do
824826
try
825827
verbose = parse(Bool, get(ENV, "LBT_VERBOSE", "false"))
826828
BLAS.lbt_forward(OpenBLAS_jll.libopenblas_path; clear=true, verbose)
827829
BLAS.check()
828830
catch ex
829831
Base.showerror_nostdio(ex, "WARNING: Error during initialization of module LinearAlgebra")
830832
end
833+
831834
# register a hook to disable BLAS threading
832835
Base.at_disable_library_threading(() -> BLAS.set_num_threads(1))
833836

@@ -839,6 +842,15 @@ function __init__()
839842
BLAS.set_num_threads(max(1, @ccall(jl_effective_threads()::Cint) ÷ 2))
840843
end
841844
end
845+
846+
return nothing
842847
end
843848

849+
function __init__()
850+
initialize_lbt()
851+
end
852+
853+
# Initialize eagerly, so that LinearAlgebra is available for sysimage builds (incl. `--trim`)
854+
initialize_lbt()
855+
844856
end # module LinearAlgebra

0 commit comments

Comments
 (0)