Skip to content

Commit 4ec5faa

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 6cc0405 commit 4ec5faa

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
@@ -823,14 +823,17 @@ function versioninfo(io::IO=stdout)
823823
return nothing
824824
end
825825

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

@@ -842,6 +845,15 @@ function __init__()
842845
BLAS.set_num_threads(max(1, @ccall(jl_effective_threads()::Cint) ÷ 2))
843846
end
844847
end
848+
849+
return nothing
845850
end
846851

852+
function __init__()
853+
initialize_lbt()
854+
end
855+
856+
# Initialize eagerly, so that LinearAlgebra is available for sysimage builds (incl. `--trim`)
857+
initialize_lbt()
858+
847859
end # module LinearAlgebra

0 commit comments

Comments
 (0)