Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into jn/codeinfo-edges
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Oct 30, 2024
2 parents 05e6e1b + 2fe6562 commit 3cf3252
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
10 changes: 6 additions & 4 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,8 @@ const StaleCacheKey = Tuple{PkgId, UInt128, String, String}
function compilecache_path(pkg::PkgId;
ignore_loaded::Bool=false,
stale_cache::Dict{StaleCacheKey,Bool}=Dict{StaleCacheKey, Bool}(),
cachepaths::Vector{String}=Base.find_all_in_cache_path(pkg),
cachepath_cache::Dict{PkgId, Vector{String}}=Dict{PkgId, Vector{String}}(),
cachepaths::Vector{String}=get!(() -> find_all_in_cache_path(pkg), cachepath_cache, pkg),
sourcepath::Union{String,Nothing}=Base.locate_package(pkg),
flags::CacheFlags=CacheFlags())
path = nothing
Expand All @@ -1830,7 +1831,7 @@ function compilecache_path(pkg::PkgId;
for dep in staledeps
dep isa Module && continue
modpath, modkey, modbuild_id = dep::Tuple{String, PkgId, UInt128}
modpaths = find_all_in_cache_path(modkey)
modpaths = get!(() -> find_all_in_cache_path(modkey), cachepath_cache, modkey)
for modpath_to_try in modpaths::Vector{String}
stale_cache_key = (modkey, modbuild_id, modpath, modpath_to_try)::StaleCacheKey
if get!(() -> stale_cachefile(stale_cache_key...; ignore_loaded, requested_flags=flags) === true,
Expand Down Expand Up @@ -1872,10 +1873,11 @@ fresh julia session specify `ignore_loaded=true`.
function isprecompiled(pkg::PkgId;
ignore_loaded::Bool=false,
stale_cache::Dict{StaleCacheKey,Bool}=Dict{StaleCacheKey, Bool}(),
cachepaths::Vector{String}=Base.find_all_in_cache_path(pkg),
cachepath_cache::Dict{PkgId, Vector{String}}=Dict{PkgId, Vector{String}}(),
cachepaths::Vector{String}=get!(() -> find_all_in_cache_path(pkg), cachepath_cache, pkg),
sourcepath::Union{String,Nothing}=Base.locate_package(pkg),
flags::CacheFlags=CacheFlags())
path = compilecache_path(pkg; ignore_loaded, stale_cache, cachepaths, sourcepath, flags)
path = compilecache_path(pkg; ignore_loaded, stale_cache, cachepath_cache, cachepaths, sourcepath, flags)
return !isnothing(path)
end

Expand Down
57 changes: 34 additions & 23 deletions base/precompilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ function _precompilepkgs(pkgs::Vector{String},
color_string(cstr::String, col::Union{Int64, Symbol}) = _color_string(cstr, col, hascolor)

stale_cache = Dict{StaleCacheKey, Bool}()
cachepath_cache = Dict{PkgId, Vector{String}}()

exts = Dict{PkgId, String}() # ext -> parent
# make a flat map of each dep and its direct deps
depsmap = Dict{PkgId, Vector{PkgId}}()
Expand Down Expand Up @@ -641,7 +643,7 @@ function _precompilepkgs(pkgs::Vector{String},
return false
end
end
std_outputs = Dict{PkgConfig,String}()
std_outputs = Dict{PkgConfig,IOBuffer}()
taskwaiting = Set{PkgConfig}()
pkgspidlocked = Dict{PkgConfig,String}()
pkg_liveprinted = nothing
Expand All @@ -663,7 +665,7 @@ function _precompilepkgs(pkgs::Vector{String},
print(io, ansi_cleartoendofline, str)
end
end
std_outputs[pkg_config] = string(get(std_outputs, pkg_config, ""), str)
write(get!(IOBuffer, std_outputs, pkg_config), str)
if !in(pkg_config, taskwaiting) && occursin("waiting for IO to finish", str)
!fancyprint && lock(print_lock) do
println(io, pkg.name, color_string(" Waiting for background task / IO / timer.", Base.warn_color()))
Expand Down Expand Up @@ -785,7 +787,7 @@ function _precompilepkgs(pkgs::Vector{String},
## precompilation loop

for (pkg, deps) in depsmap
cachepaths = Base.find_all_in_cache_path(pkg)
cachepaths = get!(() -> Base.find_all_in_cache_path(pkg), cachepath_cache, pkg)
sourcepath = Base.locate_package(pkg)
single_requested_pkg = length(requested_pkgs) == 1 && only(requested_pkgs) == pkg.name
for config in configs
Expand All @@ -808,7 +810,7 @@ function _precompilepkgs(pkgs::Vector{String},
wait(was_processed[(dep,config)])
end
circular = pkg in circular_deps
is_stale = !Base.isprecompiled(pkg; ignore_loaded=true, stale_cache, cachepaths, sourcepath, flags=cacheflags)
is_stale = !Base.isprecompiled(pkg; ignore_loaded=true, stale_cache, cachepath_cache, cachepaths, sourcepath, flags=cacheflags)
if !circular && is_stale
Base.acquire(parallel_limiter)
is_direct_dep = pkg in direct_deps
Expand Down Expand Up @@ -857,8 +859,9 @@ function _precompilepkgs(pkgs::Vector{String},
close(std_pipe.in) # close pipe to end the std output monitor
wait(t_monitor)
if err isa ErrorException || (err isa ArgumentError && startswith(err.msg, "Invalid header in cache file"))
failed_deps[pkg_config] = (strict || is_direct_dep) ? string(sprint(showerror, err), "\n", strip(get(std_outputs, pkg_config, ""))) : ""
errmsg = String(take!(get(IOBuffer, std_outputs, pkg_config)))
delete!(std_outputs, pkg_config) # so it's not shown as warnings, given error report
failed_deps[pkg_config] = (strict || is_direct_dep) ? string(sprint(showerror, err), "\n", strip(errmsg)) : ""
!fancyprint && lock(print_lock) do
println(io, " "^9, color_string("", Base.error_color()), name)
end
Expand Down Expand Up @@ -936,20 +939,22 @@ function _precompilepkgs(pkgs::Vector{String},
end
# show any stderr output, even if Pkg.precompile has been interrupted (quick_exit=true), given user may be
# interrupting a hanging precompile job with stderr output. julia#48371
filter!(kv -> !isempty(strip(last(kv))), std_outputs) # remove empty output
if !isempty(std_outputs)
plural1 = length(std_outputs) == 1 ? "y" : "ies"
plural2 = length(std_outputs) == 1 ? "" : "s"
print(iostr, "\n ", color_string("$(length(std_outputs))", Base.warn_color()), " dependenc$(plural1) had output during precompilation:")
for (pkg_config, err) in std_outputs
pkg, config = pkg_config
err = if pkg == pkg_liveprinted
"[Output was shown above]"
else
join(split(strip(err), "\n"), color_string("\n", Base.warn_color()))
let std_outputs = Tuple{PkgConfig,SubString{String}}[(pkg_config, strip(String(take!(io)))) for (pkg_config,io) in std_outputs]
filter!(kv -> !isempty(last(kv)), std_outputs)
if !isempty(std_outputs)
plural1 = length(std_outputs) == 1 ? "y" : "ies"
plural2 = length(std_outputs) == 1 ? "" : "s"
print(iostr, "\n ", color_string("$(length(std_outputs))", Base.warn_color()), " dependenc$(plural1) had output during precompilation:")
for (pkg_config, err) in std_outputs
pkg, config = pkg_config
err = if pkg == pkg_liveprinted
"[Output was shown above]"
else
join(split(err, "\n"), color_string("\n", Base.warn_color()))
end
name = haskey(exts, pkg) ? string(exts[pkg], "", pkg.name) : pkg.name
print(iostr, color_string("\n", Base.warn_color()), name, color_string("\n", Base.warn_color()), err, color_string("\n", Base.warn_color()))
end
name = haskey(exts, pkg) ? string(exts[pkg], "", pkg.name) : pkg.name
print(iostr, color_string("\n", Base.warn_color()), name, color_string("\n", Base.warn_color()), err, color_string("\n", Base.warn_color()))
end
end
end
Expand All @@ -959,20 +964,26 @@ function _precompilepkgs(pkgs::Vector{String},
end
end
quick_exit && return
err_str = ""
err_str = IOBuffer()
n_direct_errs = 0
for (pkg_config, err) in failed_deps
dep, config = pkg_config
if strict || (dep in direct_deps)
config_str = isempty(config[1]) ? "" : "$(join(config[1], " ")) "
err_str = string(err_str, "\n$(dep.name) $config_str\n\n$err", (n_direct_errs > 0 ? "\n" : ""))
print(err_str, "\n", dep.name, " ")
for cfg in config[1]
print(err_str, cfg, " ")
end
print(err_str, "\n\n", err)
n_direct_errs > 0 && write(err_str, "\n")
n_direct_errs += 1
end
end
if err_str != ""
if position(err_str) > 0
skip(err_str, -1)
truncate(err_str, position(err_str))
pluralde = n_direct_errs == 1 ? "y" : "ies"
direct = strict ? "" : "direct "
err_msg = "The following $n_direct_errs $(direct)dependenc$(pluralde) failed to precompile:\n$(err_str[1:end-1])"
err_msg = "The following $n_direct_errs $(direct)dependenc$(pluralde) failed to precompile:\n$(String(take!(err_str)))"
if internal_call # aka. auto-precompilation
if isinteractive() && !get(ENV, "CI", false)
plural1 = length(failed_deps) == 1 ? "y" : "ies"
Expand Down
2 changes: 1 addition & 1 deletion doc/src/devdocs/llvm-passes.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ This pass is used to hoist Julia-specific intrinsics out of loops. Specifically,
3. Hoist allocations out of loops when they do not escape the loop
1. We use a very conservative definition of escape here, the same as the one used in `AllocOptPass`. This transformation can reduce the number of allocations in the IR, even when an allocation escapes the function altogether.

!!!note
!!! note

This pass is required to preserve LLVM's [MemorySSA](https://llvm.org/docs/MemorySSA.html) ([Short Video](https://www.youtube.com/watch?v=bdxWmryoHak), [Longer Video](https://www.youtube.com/watch?v=1e5y6WDbXCQ)) and [ScalarEvolution](https://baziotis.cs.illinois.edu/compilers/introduction-to-scalar-evolution.html) ([Newer Slides](https://llvm.org/devmtg/2018-04/slides/Absar-ScalarEvolution.pdf) [Older Slides](https://llvm.org/devmtg/2009-10/ScalarEvolutionAndLoopOptimization.pdf)) analyses.
5 changes: 3 additions & 2 deletions stdlib/LinearAlgebra/src/structuredbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ end
function Base.similar(bc::Broadcasted{StructuredMatrixStyle{T}}, ::Type{ElType}) where {T,ElType}
inds = axes(bc)
fzerobc = fzeropreserving(bc)
if isstructurepreserving(bc) || (fzerobc && !(T <: Union{SymTridiagonal,UnitLowerTriangular,UnitUpperTriangular}))
if isstructurepreserving(bc) || (fzerobc && !(T <: Union{UnitLowerTriangular,UnitUpperTriangular}))
return structured_broadcast_alloc(bc, T, ElType, length(inds[1]))
elseif fzerobc && T <: UnitLowerTriangular
return similar(convert(Broadcasted{StructuredMatrixStyle{LowerTriangular}}, bc), ElType)
Expand Down Expand Up @@ -240,7 +240,8 @@ function copyto!(dest::SymTridiagonal, bc::Broadcasted{<:StructuredMatrixStyle})
end
for i = 1:size(dest, 1)-1
v = @inbounds bc[BandIndex(1, i)]
v == (@inbounds bc[BandIndex(-1, i)]) || throw(ArgumentError(lazy"broadcasted assignment breaks symmetry between locations ($i, $(i+1)) and ($(i+1), $i)"))
v == transpose(@inbounds bc[BandIndex(-1, i)]) ||
throw(ArgumentError(lazy"broadcasted assignment breaks symmetry between locations ($i, $(i+1)) and ($(i+1), $i)"))
dest.ev[i] = v
end
return dest
Expand Down
15 changes: 13 additions & 2 deletions stdlib/LinearAlgebra/test/structuredbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module TestStructuredBroadcast
using Test, LinearAlgebra

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
using .Main.SizedArrays

@testset "broadcast[!] over combinations of scalars, structured matrices, and dense vectors/matrices" begin
N = 10
s = rand()
Expand All @@ -12,10 +16,11 @@ using Test, LinearAlgebra
D = Diagonal(rand(N))
B = Bidiagonal(rand(N), rand(N - 1), :U)
T = Tridiagonal(rand(N - 1), rand(N), rand(N - 1))
S = SymTridiagonal(rand(N), rand(N - 1))
U = UpperTriangular(rand(N,N))
L = LowerTriangular(rand(N,N))
M = Matrix(rand(N,N))
structuredarrays = (D, B, T, U, L, M)
structuredarrays = (D, B, T, U, L, M, S)
fstructuredarrays = map(Array, structuredarrays)
for (X, fX) in zip(structuredarrays, fstructuredarrays)
@test (Q = broadcast(sin, X); typeof(Q) == typeof(X) && Q == broadcast(sin, fX))
Expand Down Expand Up @@ -166,10 +171,11 @@ end
D = Diagonal(rand(N))
B = Bidiagonal(rand(N), rand(N - 1), :U)
T = Tridiagonal(rand(N - 1), rand(N), rand(N - 1))
S = SymTridiagonal(rand(N), rand(N - 1))
U = UpperTriangular(rand(N,N))
L = LowerTriangular(rand(N,N))
M = Matrix(rand(N,N))
structuredarrays = (M, D, B, T, U, L)
structuredarrays = (M, D, B, T, S, U, L)
fstructuredarrays = map(Array, structuredarrays)
for (X, fX) in zip(structuredarrays, fstructuredarrays)
@test (Q = map(sin, X); typeof(Q) == typeof(X) && Q == map(sin, fX))
Expand Down Expand Up @@ -363,6 +369,11 @@ end
U = UpperTriangular([(i+j)*A for i in 1:3, j in 1:3])
standardbroadcastingtests(U, UpperTriangular)
end
@testset "SymTridiagonal" begin
m = SizedArrays.SizedArray{(2,2)}([1 2; 3 4])
S = SymTridiagonal(fill(m,4), fill(m,3))
standardbroadcastingtests(S, SymTridiagonal)
end
end

end

0 comments on commit 3cf3252

Please sign in to comment.