Skip to content

Backports release 1.12 #57871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Compiler/src/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3283,6 +3283,7 @@ function abstract_eval_isdefinedglobal(interp::AbstractInterpreter, @nospecializ
exct = Union{exct, ConcurrencyViolationError}
end
end
⊑ = partialorder(typeinf_lattice(interp))
if M isa Const && s isa Const
M, s = M.val, s.val
if M isa Module && s isa Symbol
Expand Down
5 changes: 5 additions & 0 deletions Compiler/test/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1027,3 +1027,8 @@ module TurnedIntoExplicit

@test !occursin("jl_apply_generic", get_llvm(f, Tuple{UInt}))
end

# Test codegen for `isdefinedglobal` of constant (#57872)
const x57872 = "Hello"
f57872() = (Core.isdefinedglobal(@__MODULE__, Base.compilerbarrier(:const, :x57872)), x57872) # Extra globalref here to force world age bounds
@test f57872() == (true, "Hello")
2 changes: 1 addition & 1 deletion base/cmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ julia> run(cm)
Process(`echo 1`, ProcessExited(0))
```
"""
macro cmd(str)
macro cmd(str::String)
cmd_ex = shell_parse(str, special=shell_special, filename=String(__source__.file))[1]
return :(cmd_gen($(esc(cmd_ex))))
end
10 changes: 8 additions & 2 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,14 @@ function mod(x::T, y::T) where T<:Integer
y == -1 && return T(0) # avoid potential overflow in fld
return x - fld(x, y) * y
end
mod(x::BitSigned, y::Unsigned) = rem(y + unsigned(rem(x, y)), y)
mod(x::Unsigned, y::Signed) = rem(y + signed(rem(x, y)), y)
function mod(x::BitSigned, y::Unsigned)
remval = rem(x, y) # correct iff remval>=0
return unsigned(remval + (remval<zero(remval))*y)
end
function mod(x::Unsigned, y::Signed)
remval = signed(rem(x, y)) #remval>0 so correct iff y>0 or remval==0
return remval + (!iszero(remval) && y<zero(y))*y
end
mod(x::T, y::T) where {T<:Unsigned} = rem(x, y)

# Don't promote integers for div/rem/mod since there is no danger of overflow,
Expand Down
2 changes: 1 addition & 1 deletion base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ end
exec(re, subject::Union{String,SubString{String}}, offset, options, match_data) =
_exec(re, subject, offset, options, match_data)
exec(re, subject, offset, options, match_data) =
_exec(re, String(subject), offset, options, match_data)
_exec(re, String(subject)::String, offset, options, match_data)

function _exec(re, subject, offset, options, match_data)
rc = ccall((:pcre2_match_8, PCRE_LIB), Cint,
Expand Down
11 changes: 5 additions & 6 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,22 @@ struct CodegenParams
use_jlplt::Cint

"""
If enabled, only provably reachable code (from functions marked with `entrypoint`) is included
in the output system image. Errors or warnings can be given for call sites too dynamic to handle.
The option is disabled by default. (0=>disabled, 1=>safe (static errors), 2=>unsafe, 3=>unsafe plus warnings)
If enabled emit LLVM IR for all functions even if wouldn't be compiled
for some reason (i.e functions that return a constant value).
"""
trim::Cint
force_emit_all::Cint

function CodegenParams(; track_allocations::Bool=true, code_coverage::Bool=true,
prefer_specsig::Bool=false,
gnu_pubnames::Bool=true, debug_info_kind::Cint = default_debug_info_kind(),
debug_info_level::Cint = Cint(JLOptions().debug_level), safepoint_on_entry::Bool=true,
gcstack_arg::Bool=true, use_jlplt::Bool=true, trim::Cint=Cint(0))
gcstack_arg::Bool=true, use_jlplt::Bool=true, force_emit_all::Bool=false)
return new(
Cint(track_allocations), Cint(code_coverage),
Cint(prefer_specsig),
Cint(gnu_pubnames), debug_info_kind,
debug_info_level, Cint(safepoint_on_entry),
Cint(gcstack_arg), Cint(use_jlplt), Cint(trim))
Cint(gcstack_arg), Cint(use_jlplt), Cint(force_emit_all))
end
end

Expand Down
2 changes: 1 addition & 1 deletion base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ julia> v[2]
0x32
```
"""
macro b_str(s)
macro b_str(s::String)
v = codeunits(unescape_string(s))
QuoteNode(v)
end
Expand Down
2 changes: 1 addition & 1 deletion src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ void *jl_emit_native_impl(jl_array_t *codeinfos, LLVMOrcThreadSafeModuleRef llvm
params.tsctx, clone.getModuleUnlocked()->getDataLayout(),
Triple(clone.getModuleUnlocked()->getTargetTriple()));
jl_llvm_functions_t decls;
if (jl_atomic_load_relaxed(&codeinst->invoke) == jl_fptr_const_return_addr)
if (!(params.params->force_emit_all) && jl_atomic_load_relaxed(&codeinst->invoke) == jl_fptr_const_return_addr)
decls.functionObject = "jl_fptr_const_return";
else
decls = jl_emit_codeinst(result_m, codeinst, src, params);
Expand Down
3 changes: 2 additions & 1 deletion src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4410,7 +4410,8 @@ static int compare_cgparams(const jl_cgparams_t *a, const jl_cgparams_t *b)
(a->debug_info_kind == b->debug_info_kind) &&
(a->safepoint_on_entry == b->safepoint_on_entry) &&
(a->gcstack_arg == b->gcstack_arg) &&
(a->use_jlplt == b->use_jlplt);
(a->use_jlplt == b->use_jlplt) &&
(a->force_emit_all == b->force_emit_all);
}
#endif

Expand Down
13 changes: 6 additions & 7 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3824,30 +3824,29 @@ static bool emit_f_opfield(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,

static jl_cgval_t emit_isdefinedglobal(jl_codectx_t &ctx, jl_module_t *modu, jl_sym_t *name, int allow_import, enum jl_memory_order order)
{
Value *isnull = NULL;
jl_binding_t *bnd = allow_import ? jl_get_binding(modu, name) : jl_get_module_binding(modu, name, 0);
struct restriction_kind_pair rkp = { NULL, NULL, PARTITION_KIND_GUARD, 0 };
if (allow_import && jl_get_binding_leaf_partitions_restriction_kind(bnd, &rkp, ctx.min_world, ctx.max_world)) {
if (jl_bkind_is_some_constant(rkp.kind))
return mark_julia_const(ctx, rkp.restriction);
if (jl_bkind_is_some_constant(rkp.kind) && rkp.restriction)
return mark_julia_const(ctx, jl_true);
if (rkp.kind == PARTITION_KIND_GLOBAL) {
Value *bp = julia_binding_gv(ctx, rkp.binding_if_global);
bp = julia_binding_pvalue(ctx, bp);
LoadInst *v = ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, bp, Align(sizeof(void*)));
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_binding);
ai.decorateInst(v);
v->setOrdering(get_llvm_atomic_order(order));
isnull = ctx.builder.CreateICmpNE(v, Constant::getNullValue(ctx.types().T_prjlvalue));
Value *isnull = ctx.builder.CreateICmpNE(v, Constant::getNullValue(ctx.types().T_prjlvalue));
return mark_julia_type(ctx, isnull, false, jl_bool_type);
}
}
Value *v = ctx.builder.CreateCall(prepare_call(jlboundp_func), {
Value *isdef = ctx.builder.CreateCall(prepare_call(jlboundp_func), {
literal_pointer_val(ctx, (jl_value_t*)modu),
literal_pointer_val(ctx, (jl_value_t*)name),
ConstantInt::get(getInt32Ty(ctx.builder.getContext()), allow_import)
});
isnull = ctx.builder.CreateICmpNE(v, ConstantInt::get(getInt32Ty(ctx.builder.getContext()), 0));
return mark_julia_type(ctx, isnull, false, jl_bool_type);
isdef = ctx.builder.CreateTrunc(isdef, getInt1Ty(ctx.builder.getContext()));
return mark_julia_type(ctx, isdef, false, jl_bool_type);
}

static bool emit_f_opmemory(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
Expand Down
3 changes: 2 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ JL_DLLEXPORT jl_cgparams_t jl_default_cgparams = {
/* debug_info_level */ 0, // later jl_options.debug_level,
/* safepoint_on_entry */ 1,
/* gcstack_arg */ 1,
/* use_jlplt*/ 1 };
/* use_jlplt*/ 1 ,
/*force_emit_all=*/ 0};

static void init_global_mutexes(void) {
JL_MUTEX_INIT(&jl_modules_mutex, "jl_modules_mutex");
Expand Down
6 changes: 3 additions & 3 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4657,9 +4657,9 @@ f(x) = yt(x)
(let ((e1 (if (and arg-map (symbol? e))
(get arg-map e e)
e)))
(if (and value (or (underscore-symbol? e)
(and (pair? e) (eq? (car e) 'globalref)
(underscore-symbol? (cadr e)))))
(if (or (underscore-symbol? e)
(and (pair? e) (eq? (car e) 'globalref)
(underscore-symbol? (cadr e))))
(error (string "all-underscore identifiers are write-only and their values cannot be used in expressions" (format-loc current-loc))))
(cond (tail (emit-return tail e1))
(value e1)
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,7 @@ typedef struct {
int gcstack_arg; // Pass the ptls value as an argument with swiftself

int use_jlplt; // Whether to use the Julia PLT mechanism or emit symbols directly
int force_emit_all; // Force emission of code for const return functions
} jl_cgparams_t;
extern JL_DLLEXPORT int jl_default_debug_info_kind;
extern JL_DLLEXPORT jl_cgparams_t jl_default_cgparams;
Expand Down
3 changes: 2 additions & 1 deletion src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ static jl_value_t *resolve_definition_effects(jl_value_t *expr, jl_module_t *mod
int binding_effects, int eager_resolve)
{
if (jl_is_symbol(expr)) {
jl_error("Found raw symbol in code returned from lowering. Expected all symbols to have been resolved to GlobalRef or slots.");
jl_errorf("Found raw symbol %s in code returned from lowering. Expected all symbols to have been resolved to GlobalRef or slots.",
jl_symbol_name((jl_sym_t*)expr));
}

if (!jl_is_expr(expr)) {
Expand Down
5 changes: 5 additions & 0 deletions src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ static int thread0_exit_signo = 0;
static void JL_NORETURN jl_exit_thread0_cb(void)
{
CFI_NORETURN
jl_atomic_fetch_add(&jl_gc_disable_counter, -1);
jl_critical_error(thread0_exit_signo, 0, NULL, jl_current_task);
jl_atexit_hook(128);
jl_raise(thread0_exit_signo);
Expand Down Expand Up @@ -1089,6 +1090,10 @@ static void *signal_listener(void *arg)
//#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L && !HAVE_KEVENT
// si_code = info.si_code;
//#endif
// Let's forbid threads from running GC while we're trying to exit,
// also let's make sure we're not in the middle of GC.
jl_atomic_fetch_add(&jl_gc_disable_counter, 1);
jl_safepoint_wait_gc(NULL);
jl_exit_thread0(sig, signal_bt_data, signal_bt_size);
}
else if (critical) {
Expand Down
2 changes: 1 addition & 1 deletion src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ static void import_module(jl_task_t *ct, jl_module_t *JL_NONNULL m, jl_module_t
if (!jl_bkind_is_some_implicit(kind) && kind != PARTITION_KIND_DECLARED) {
// Unlike regular constant declaration, we allow this as long as we eventually end up at a constant.
jl_walk_binding_inplace(&b, &bpart, ct->world_age);
if (jl_binding_kind(bpart) == PARTITION_KIND_CONST || jl_binding_kind(bpart) == PARTITION_KIND_BACKDATED_CONST || jl_binding_kind(bpart) == PARTITION_KIND_CONST_IMPORT) {
if (jl_bkind_is_some_constant(jl_binding_kind(bpart))) {
// Already declared (e.g. on another thread) or imported.
if (bpart->restriction == (jl_value_t*)import)
return;
Expand Down
16 changes: 6 additions & 10 deletions stdlib/LibGit2/src/LibGit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1042,24 +1042,20 @@ function set_ssl_cert_locations(cert_loc)
else # files, /dev/null, non-existent paths, etc.
cert_file = cert_loc
end
ret = @ccall libgit2.git_libgit2_opts(
ret = @ccall libgit2.git_libgit2_opts(
Consts.SET_SSL_CERT_LOCATIONS::Cint;
cert_file::Cstring,
cert_dir::Cstring)::Cint
ret >= 0 && return ret
# On macOS and Windows LibGit2_jll is built without a TLS backend that supports
# certificate locations; don't throw on this expected error so we allow certificate
# location environment variables to be set for other purposes.
# We still try doing so to support other LibGit2 builds.
err = Error.GitError(ret)
err.class == Error.SSL &&
err.msg == "TLS backend doesn't support certificate locations" ||
throw(err)
var = nothing
for v in NetworkOptions.CA_ROOTS_VARS
haskey(ENV, v) && (var = v)
end
@assert var !== nothing # otherwise we shouldn't be here
msg = """
Your Julia is built with a SSL/TLS engine that libgit2 doesn't know how to configure to use a file or directory of certificate authority roots, but your environment specifies one via the $var variable. If you believe your system's root certificates are safe to use, you can `export JULIA_SSL_CA_ROOTS_PATH=""` in your environment to use those instead.
"""
throw(Error.GitError(err.class, err.code, chomp(msg)))
return ret
end

"""
Expand Down
26 changes: 15 additions & 11 deletions stdlib/LibGit2/test/bad_ca_roots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ const CAN_SET_CA_ROOTS_PATH = !Sys.isapple() && !Sys.iswindows()
# Given this is a sub-processed test file, not using @testsets avoids
# leaking the report print into the Base test runner report
begin # empty CA roots file
# these fail for different reasons on different platforms:
# - on Apple & Windows you cannot set the CA roots path location
# - on Linux & FreeBSD you you can but these are invalid files
# different behavior on different platforms:
# - on Apple & Windows you cannot set the CA roots path location; don't error
# - on Linux & FreeBSD you can but these are invalid files

ENV["JULIA_SSL_CA_ROOTS_PATH"] = "/dev/null"
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
if CAN_SET_CA_ROOTS_PATH
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
else
@test LibGit2.ensure_initialized() === nothing
end

ENV["JULIA_SSL_CA_ROOTS_PATH"] = tempname()
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
# test that it still fails if called a second time
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
if !CAN_SET_CA_ROOTS_PATH
# test that this doesn't work on macOS & Windows
ENV["JULIA_SSL_CA_ROOTS_PATH"] = NetworkOptions.bundled_ca_roots()
if CAN_SET_CA_ROOTS_PATH
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
# test that it still fails if called a second time
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
delete!(ENV, "JULIA_SSL_CA_ROOTS_PATH")
else
@test LibGit2.ensure_initialized() === nothing
@test LibGit2.ensure_initialized() === nothing
end
end
Expand Down
6 changes: 3 additions & 3 deletions stdlib/Profile/src/Allocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ end
function _prof_expr(expr, opts)
quote
$start(; $(esc(opts)))
try
Base.@__tryfinally(
$(esc(expr))
finally
,
$stop()
end
)
end
end

Expand Down
16 changes: 8 additions & 8 deletions stdlib/Profile/src/Profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ appended to an internal buffer of backtraces.
"""
macro profile(ex)
return quote
try
start_timer()
start_timer()
Base.@__tryfinally(
$(esc(ex))
finally
,
stop_timer()
end
)
end
end

Expand All @@ -78,12 +78,12 @@ it can be used to diagnose performance issues such as lock contention, IO bottle
"""
macro profile_walltime(ex)
return quote
try
start_timer(true)
start_timer(true);
Base.@__tryfinally(
$(esc(ex))
finally
,
stop_timer()
end
)
end
end

Expand Down
14 changes: 14 additions & 0 deletions stdlib/Profile/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ end
@test z == 10
end

@testset "@profile no scope" begin
@profile no_scope_57858_1 = 1
@test @isdefined no_scope_57858_1
Profile.clear()

@profile_walltime no_scope_57858_1 = 1
@test @isdefined no_scope_57858_1
Profile.clear()

Profile.Allocs.@profile no_scope_57858_2 = 1
@test @isdefined no_scope_57858_2
Profile.Allocs.clear()
end

@testset "setting sample count and delay in init" begin
n_, delay_ = Profile.init()
n_original = n_
Expand Down
Loading