Skip to content

Commit

Permalink
Emit safepoints at function entry (JuliaLang#41616)
Browse files Browse the repository at this point in the history
* Emit safepoints at function entry

* Make safepoint emission on function entry a codegen feature

* Hoist signal page lookup outside fence

* Update src/cgutils.cpp

* Fix rebase
  • Loading branch information
vchuravy authored and kpamnany committed Sep 28, 2023
1 parent cf32895 commit 11bb88d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ struct CodegenParams
prefer_specsig::Cint
gnu_pubnames::Cint
debug_info_kind::Cint
safepoint_on_entry::Cint

lookup::Ptr{Cvoid}

Expand All @@ -1100,12 +1101,14 @@ struct CodegenParams
function CodegenParams(; track_allocations::Bool=true, code_coverage::Bool=true,
prefer_specsig::Bool=false,
gnu_pubnames=true, debug_info_kind::Cint = default_debug_info_kind(),
safepoint_on_entry::Bool=true,
lookup::Ptr{Cvoid}=cglobal(:jl_rettype_inferred),
generic_context = nothing)
return new(
Cint(track_allocations), Cint(code_coverage),
Cint(prefer_specsig),
Cint(gnu_pubnames), debug_info_kind,
Cint(safepoint_on_entry),
lookup, generic_context)
end
end
Expand Down
1 change: 0 additions & 1 deletion src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3933,7 +3933,6 @@ static Value *emit_defer_signal(jl_codectx_t &ctx)
return ctx.builder.CreateInBoundsGEP(ctx.types().T_sigatomic, ptls, ArrayRef<Value*>(offset), "jl_defer_signal");
}


#ifndef JL_NDEBUG
static int compare_cgparams(const jl_cgparams_t *a, const jl_cgparams_t *b)
{
Expand Down
7 changes: 6 additions & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,7 @@ extern "C" {
1,
#endif
(int) DICompileUnit::DebugEmissionKind::FullDebug,
1,
jl_rettype_inferred, NULL };
}

Expand Down Expand Up @@ -7805,7 +7806,11 @@ static jl_llvm_functions_t
ctx.builder.CreateAlignedStore(load_world, world_age_field, Align(sizeof(size_t)));
}

// step 11b. Do codegen in control flow order
// step 11b. Emit the entry safepoint
if (JL_FEAT_TEST(ctx, safepoint_on_entry))
emit_gc_safepoint(ctx.builder, get_current_ptls(ctx), ctx.tbaa().tbaa_const);

// step 11c. Do codegen in control flow order
std::vector<int> workstack;
std::map<int, BasicBlock*> BB;
std::map<size_t, BasicBlock*> come_from_bb;
Expand Down
4 changes: 3 additions & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2247,9 +2247,11 @@ typedef struct {

// controls the emission of debug-info. mirrors the clang options
int gnu_pubnames; // can we emit the gnu pubnames debuginfo
int debug_info_kind; // Enum for line-table-only, line-directives-only,
int debug_info_kind; // Enum for line-table-only, line-directives-only,
// limited, standalone

int safepoint_on_entry; // Emit a safepoint on entry to each function

// Cache access. Default: jl_rettype_inferred.
jl_codeinstance_lookup_t lookup;

Expand Down
9 changes: 6 additions & 3 deletions test/compiler/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ function libjulia_codegen_name()
is_debug_build ? "libjulia-codegen-debug" : "libjulia-codegen"
end

# `_dump_function` might be more efficient but it doesn't really matter here...
get_llvm(@nospecialize(f), @nospecialize(t), raw=true, dump_module=false, optimize=true) =
sprint(code_llvm, f, t, raw, dump_module, optimize)
# The tests below assume a certain format and safepoint_on_entry=true breaks that.
function get_llvm(@nospecialize(f), @nospecialize(t), raw=true, dump_module=false, optimize=true)
params = Base.CodegenParams(safepoint_on_entry=false)
d = InteractiveUtils._dump_function(f, t, false, false, !raw, dump_module, :att, optimize, :none, false, params)
sprint(print, d)
end

if !is_debug_build && opt_level > 0
# Make sure getptls call is removed at IR level with optimization on
Expand Down

0 comments on commit 11bb88d

Please sign in to comment.