Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Nov 5, 2023
1 parent 5be9ff1 commit a085bf0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
31 changes: 27 additions & 4 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,21 @@ function compilecache(pkg::PkgId, path::String, internal_stderr::IO = stderr, in
# create a temporary file in `cachepath` directory, write the cache in it,
# write the checksum, _and then_ atomically move the file to `cachefile`.
mkpath(cachepath)
cache_objects = JLOptions().use_pkgimages != 0
if JLOptions().use_pkgimages == 0
cache_objects = false
else
if JLOptions().tracked_path == C_NULL
cache_objects = true
else
tracked_path = unsafe_string(JLOptions().tracked_path)
# disable pkgimages if srcpath falls within a code-coverage or allocation-tracking path
# TODO: disable if any includes fall within tracked_path, not just the srcpath
# harder because includes aren't currently known before cache generation
# or implement https://github.com/JuliaLang/julia/issues/51412
cache_objects = !startswith(path, tracked_path)
end
end

tmppath, tmpio = mktemp(cachepath)

if cache_objects
Expand Down Expand Up @@ -3159,6 +3173,7 @@ end
return stale_cachefile(PkgId(""), UInt128(0), modpath, cachefile; ignore_loaded)
end
@constprop :none function stale_cachefile(modkey::PkgId, build_id::UInt128, modpath::String, cachefile::String; ignore_loaded::Bool = false)
tracked_path = JLOptions().tracked_path == C_NULL ? "" : unsafe_string(JLOptions().tracked_path)
io = open(cachefile, "r")
try
checksum = isvalid_cache_header(io)
Expand All @@ -3170,10 +3185,19 @@ end
if isempty(modules)
return true # ignore empty file
end
if ccall(:jl_match_cache_flags, UInt8, (UInt8,), flags) == 0
current_flags = ccall(:jl_cache_flags, UInt8, ())
if !isempty(tracked_path)
for chi in includes
startswith(chi.filename, tracked_path) || continue
@debug "Allowing pkgimages=no for $modkey because it falls in coverage/allocation tracking path $tracked_path"
current_flags &= 0b11111110 # disable pkgimages flag
break
end
end
if ccall(:jl_match_cache_flags, UInt8, (UInt8, UInt8), flags, current_flags) == 0
@debug """
Rejecting cache file $cachefile for $modkey since the flags are mismatched
current session: $(CacheFlags())
current session: $(CacheFlags(current_flags))
cache file: $(CacheFlags(flags))
"""
return true
Expand Down Expand Up @@ -3275,7 +3299,6 @@ end
return true
end
end
tracked_path = JLOptions().tracked_path == C_NULL ? "" : unsafe_string(JLOptions().tracked_path)
for chi in includes
f, fsize_req, hash_req, ftime_req = chi.filename, chi.fsize, chi.hash, chi.mtime
if startswith(f, "@depot/")
Expand Down
2 changes: 1 addition & 1 deletion src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -3494,7 +3494,7 @@ static jl_value_t *jl_validate_cache_file(ios_t *f, jl_array_t *depmods, uint64_
"Precompile file header verification checks failed.");
}
uint8_t flags = read_uint8(f);
if (pkgimage && !jl_match_cache_flags(flags)) {
if (pkgimage && !jl_match_cache_flags(flags, jl_cache_flags())) {
return jl_get_exceptionf(jl_errorexception_type, "Pkgimage flags mismatch");
}
if (!pkgimage) {
Expand Down
14 changes: 7 additions & 7 deletions src/staticdata_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,20 +599,20 @@ JL_DLLEXPORT uint8_t jl_cache_flags(void)
{
// OOICCDDP
uint8_t flags = 0;
flags |= (jl_options.use_pkgimages & 1); // 0-bit
// don't use use_pkgimages here because no outputo overrides it
flags |= ((jl_options.outputo == NULL || jl_options.outputo[0] == '\0') | 1); // 0-bit
flags |= (jl_options.debug_level & 3) << 1; // 1-2 bit
flags |= (jl_options.check_bounds & 3) << 3; // 3-4 bit
flags |= (jl_options.can_inline & 1) << 5; // 5-bit
flags |= (jl_options.opt_level & 3) << OPT_LEVEL; // 6-7 bit
return flags;
}

JL_DLLEXPORT uint8_t jl_match_cache_flags(uint8_t flags)
JL_DLLEXPORT uint8_t jl_match_cache_flags(uint8_t cache_flags, uint8_t current_flags)
{
// 1. Check which flags are relevant
uint8_t current_flags = jl_cache_flags();
uint8_t supports_pkgimage = (current_flags & 1);
uint8_t is_pkgimage = (flags & 1);
uint8_t is_pkgimage = (cache_flags & 1);

// For .ji packages ignore other flags
if (!supports_pkgimage && !is_pkgimage) {
Expand All @@ -621,12 +621,12 @@ JL_DLLEXPORT uint8_t jl_match_cache_flags(uint8_t flags)

// 2. Check all flags, execept opt level must be exact
uint8_t mask = (1 << OPT_LEVEL)-1;
if ((flags & mask) != (current_flags & mask))
if ((cache_flags & mask) != (current_flags & mask))
return 0;
// 3. allow for higher optimization flags in cache
flags >>= OPT_LEVEL;
cache_flags >>= OPT_LEVEL;
current_flags >>= OPT_LEVEL;
return flags >= current_flags;
return cache_flags >= current_flags;
}

// "magic" string and version header of .ji file
Expand Down

0 comments on commit a085bf0

Please sign in to comment.