Skip to content
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

Add small tweaks to juliac script and misc fixes to juliac #56119

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 15 additions & 7 deletions contrib/juliac-buildscript.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end
(f::Base.RedirectStdStream)(io::Core.CoreSTDOUT) = Base._redirect_io_global(io, f.unix_fd)

@eval Base begin
depwarn(msg, funcsym; force::Bool=false) = nothing
_assert_tostring(msg) = ""
reinit_stdio() = nothing
JuliaSyntax.enable_in_core!() = nothing
Expand Down Expand Up @@ -230,20 +231,15 @@ let loaded = Symbol.(Base.loaded_modules_array()) # TODO better way to do this
using Artifacts
@eval Artifacts begin
function _artifact_str(__module__, artifacts_toml, name, path_tail, artifact_dict, hash, platform, _::Val{lazyartifacts}) where lazyartifacts
moduleroot = Base.moduleroot(__module__)
if haskey(Base.module_keys, moduleroot)
# Process overrides for this UUID, if we know what it is
process_overrides(artifact_dict, Base.module_keys[moduleroot].uuid)
end

# If the artifact exists, we're in the happy path and we can immediately
# return the path to the artifact:
dirs = artifact_paths(hash; honor_overrides=true)
dirs = artifacts_dirs(bytes2hex(hash.bytes))
for dir in dirs
if isdir(dir)
return jointail(dir, path_tail)
end
end
error("Artifact not found")
end
end
end
Expand All @@ -259,6 +255,18 @@ let loaded = Symbol.(Base.loaded_modules_array()) # TODO better way to do this
__init__() = rand()
end
end
if :Markdown in loaded
using Markdown
@eval Markdown begin
__init__() = rand()
end
end
if :JuliaSyntaxHighlighting in loaded
using JuliaSyntaxHighlighting
@eval JuliaSyntaxHighlighting begin
__init__() = rand()
end
end
end

empty!(Core.ARGS)
Expand Down
6 changes: 4 additions & 2 deletions contrib/juliac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ trim = nothing
outname = nothing
file = nothing
add_ccallables = false
verbose = false

help = findfirst(x->x == "--help", ARGS)
if help !== nothing
Expand Down Expand Up @@ -39,6 +40,8 @@ let i = 1
end
elseif arg == "--compile-ccallable"
global add_ccallables = true
elseif arg == "--verbose"
global verbose = true
else
if arg[1] == '-' || !isnothing(file)
println("Unexpected argument `$arg`")
Expand Down Expand Up @@ -77,9 +80,8 @@ open(initsrc_path, "w") do io
end

static_call_graph_arg() = isnothing(trim) ? `` : `--trim=$(trim)`
is_verbose() = verbose ? `--verbose-compilation=yes` : ``
cmd = addenv(`$cmd --project=$(Base.active_project()) --output-o $img_path --output-incremental=no --strip-ir --strip-metadata $(static_call_graph_arg()) $(joinpath(@__DIR__,"juliac-buildscript.jl")) $absfile $output_type $add_ccallables`, "OPENBLAS_NUM_THREADS" => 1, "JULIA_NUM_THREADS" => 1)

verbose && println("Running: $cmd")
if !success(pipeline(cmd; stdout, stderr))
println(stderr, "\nFailed to compile $file")
exit(1)
Expand Down
39 changes: 34 additions & 5 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,7 @@ static jl_array_t* build_stack_crumbs(jl_codectx_t &ctx) JL_NOTSAFEPOINT
break;
}
if (caller) {
assert(ctx.emission_context.enqueuers.count(caller) == 1);
// assert(ctx.emission_context.enqueuers.count(caller) == 1);
topolarity marked this conversation as resolved.
Show resolved Hide resolved
if (jl_is_method_instance(caller)) {
//TODO: Use a subrange when C++20 is a thing
for (auto it2 = std::get<CallFrames>(it->second).begin(); it2 != (std::prev(std::get<CallFrames>(it->second).end())); ++it2) {
Expand Down Expand Up @@ -5717,10 +5717,34 @@ static jl_cgval_t emit_call(jl_codectx_t &ctx, jl_expr_t *ex, jl_value_t *rt, bo
// special case for some known builtin not handled by emit_builtin_call
auto it = builtin_func_map().find(builtin_fptr);
if (it != builtin_func_map().end()) {
if (trim_may_error(ctx.params->trim) && may_dispatch_builtins().count(builtin_fptr)) {
errs() << "ERROR: Dynamic call to builtin" << jl_symbol_name(((jl_datatype_t*)jl_typeof(f.constant))->name->name);
errs() << "In " << ctx.builder.getCurrentDebugLocation()->getFilename() << ":" << ctx.builder.getCurrentDebugLocation()->getLine() << "\n";
print_stacktrace(ctx, ctx.params->trim);
if (trim_may_error(ctx.params->trim)) {
bool may_dispatch = may_dispatch_builtins().count(builtin_fptr);
if (may_dispatch && f.constant == jl_builtin__apply_iterate && nargs >= 4) {
if (jl_subtype(argv[2].typ, (jl_value_t*)jl_builtin_type)) {
static jl_value_t *jl_dispatchfree_apply_iterate_type = NULL;
if (!jl_dispatchfree_apply_iterate_type) {
jl_value_t *types[5] = {
(jl_value_t *)jl_simplevector_type,
(jl_value_t *)jl_genericmemory_type,
(jl_value_t *)jl_array_type,
(jl_value_t *)jl_tuple_type,
(jl_value_t *)jl_namedtuple_type,
};
jl_dispatchfree_apply_iterate_type = jl_as_global_root(jl_type_union(types, 5), 1);
}
for (size_t i = 3; i < nargs; i++) {
auto ai = argv[i].typ;
if (!jl_subtype(ai, jl_dispatchfree_apply_iterate_type))
break;
}
may_dispatch = false;
}
}
if (may_dispatch) {
errs() << "ERROR: Dynamic call to builtin " << jl_symbol_name(((jl_datatype_t*)jl_typeof(f.constant))->name->name);
errs() << "In " << ctx.builder.getCurrentDebugLocation()->getFilename() << ":" << ctx.builder.getCurrentDebugLocation()->getLine() << "\n";
print_stacktrace(ctx, ctx.params->trim);
}
}
Value *ret = emit_jlcall(ctx, it->second, Constant::getNullValue(ctx.types().T_prjlvalue), ArrayRef<jl_cgval_t>(argv).drop_front(), nargs - 1, julia_call);
setName(ctx.emission_context, ret, it->second->name + "_ret");
Expand All @@ -5741,6 +5765,11 @@ static jl_cgval_t emit_call(jl_codectx_t &ctx, jl_expr_t *ex, jl_value_t *rt, bo
F = boxed(ctx, f);
cc = julia_call;
}
if (trim_may_error(ctx.params->trim)) {
errs() << "ERROR: Dynamic call to unknown builtin";
errs() << "In " << ctx.builder.getCurrentDebugLocation()->getFilename() << ":" << ctx.builder.getCurrentDebugLocation()->getLine() << "\n";
print_stacktrace(ctx, ctx.params->trim);
}
Value *ret = emit_jlcall(ctx, fptr, F, ArrayRef<jl_cgval_t>(argv).drop_front(), nargs - 1, cc);
setName(ctx.emission_context, ret, "Builtin_ret");
return mark_julia_type(ctx, ret, true, rt);
Expand Down