Skip to content

Support for locals in the 5.x runtime #2066

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 5 commits into from
Nov 24, 2023
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
3 changes: 2 additions & 1 deletion backend/cmm_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ let bind_list name args fn =

let caml_black = Nativeint.shift_left (Nativeint.of_int 3) 8

let caml_local = Nativeint.shift_left (Nativeint.of_int 2) 8
let caml_local =
Nativeint.shift_left (Nativeint.of_int (if Config.runtime5 then 3 else 2)) 8
(* cf. runtime/caml/gc.h *)

(* Loads *)
Expand Down
4 changes: 3 additions & 1 deletion ocaml/asmcomp/cmm_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ let bind_nonvar name arg fn =
| _ -> let id = V.create_local name in Clet(VP.create id, arg, fn (Cvar id))

let caml_black = Nativeint.shift_left (Nativeint.of_int 3) 8
let caml_local = Nativeint.shift_left (Nativeint.of_int 2) 8
let caml_local =
Nativeint.shift_left (Nativeint.of_int (if Config.runtime5 then 3 else 2)) 8
(* cf. runtime/caml/gc.h *)
(* cf. runtime/caml/gc.h *)

(* Loads *)
Expand Down
2 changes: 0 additions & 2 deletions ocaml/configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ocaml/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2120,8 +2120,6 @@ AS_CASE([$host],

if [[ x"$enable_runtime5" = x"yes" ]]; then
runtime_suffix=
# CR ocaml 5 runtime: forward port locals
enable_stack_allocation="no"
else
runtime_suffix=4
fi
Expand Down
9 changes: 8 additions & 1 deletion ocaml/otherlibs/systhreads/st_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct caml_thread_struct {
/* Note: we do not save Caml_state->stack_cache, because it can
safely be shared between all threads on the same domain. */
struct caml__roots_block *local_roots; /* saved value of local_roots */
struct caml_local_arenas *local_arenas;
int backtrace_pos; /* saved value of Caml_state->backtrace_pos */
backtrace_slot * backtrace_buffer;
/* saved value of Caml_state->backtrace_buffer */
Expand Down Expand Up @@ -173,7 +174,8 @@ static void caml_thread_scan_roots(
if (th != Active_thread) {
if (th->current_stack != NULL)
caml_do_local_roots(action, fflags, fdata,
th->local_roots, th->current_stack, th->gc_regs);
th->local_roots, th->current_stack, th->gc_regs,
th->local_arenas);
}
th = th->next;
} while (th != Active_thread);
Expand All @@ -196,6 +198,7 @@ static void save_runtime_state(void)
this_thread->gc_regs_buckets = Caml_state->gc_regs_buckets;
this_thread->exn_handler = Caml_state->exn_handler;
this_thread->local_roots = Caml_state->local_roots;
this_thread->local_arenas = caml_get_local_arenas(Caml_state);
this_thread->backtrace_pos = Caml_state->backtrace_pos;
this_thread->backtrace_buffer = Caml_state->backtrace_buffer;
this_thread->backtrace_last_exn = Caml_state->backtrace_last_exn;
Expand All @@ -216,6 +219,7 @@ static void restore_runtime_state(caml_thread_t th)
Caml_state->gc_regs_buckets = th->gc_regs_buckets;
Caml_state->exn_handler = th->exn_handler;
Caml_state->local_roots = th->local_roots;
caml_set_local_arenas(Caml_state, th->local_arenas);
Caml_state->backtrace_pos = th->backtrace_pos;
Caml_state->backtrace_buffer = th->backtrace_buffer;
Caml_state->backtrace_last_exn = th->backtrace_last_exn;
Expand Down Expand Up @@ -283,6 +287,7 @@ static caml_thread_t caml_thread_new_info(void)
}
th->c_stack = NULL;
th->local_roots = NULL;
th->local_arenas = NULL;
th->backtrace_pos = 0;
th->backtrace_buffer = NULL;
th->backtrace_last_exn = Val_unit;
Expand Down Expand Up @@ -319,6 +324,8 @@ void caml_thread_free_info(caml_thread_t th)
caml_free_stack(th->current_stack);
caml_free_backtrace_buffer(th->backtrace_buffer);

// CR sdolan: free local arenas

/* Remark: we could share gc_regs_buckets between threads on a same
domain, but this might break the invariant that it is always
non-empty at the point where we switch from OCaml to C, so we
Expand Down
14 changes: 14 additions & 0 deletions ocaml/runtime/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ CAMLexport value caml_alloc_string (mlsize_t len)
return result;
}

/* [len] is a number of bytes (chars) */
CAMLexport value caml_alloc_local_string (mlsize_t len)
{
mlsize_t offset_index;
mlsize_t wosize = (len + sizeof (value)) / sizeof (value);
value result;

result = caml_alloc_local(wosize, String_tag);
Field (result, wosize - 1) = 0;
offset_index = Bsize_wsize (wosize) - 1;
Byte (result, offset_index) = offset_index - len;
return result;
}

/* [len] is a number of bytes (chars) */
CAMLexport value caml_alloc_initialized_string (mlsize_t len, const char *p)
{
Expand Down
14 changes: 14 additions & 0 deletions ocaml/runtime/amd64.S
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,20 @@ CFI_STARTPROC
CFI_ENDPROC
ENDFUNCTION(G(caml_allocN))

FUNCTION(G(caml_call_local_realloc))
CFI_STARTPROC
CFI_SIGNAL_FRAME
ENTER_FUNCTION
SAVE_ALL_REGS
SWITCH_OCAML_TO_C
C_call (GCALL(caml_local_realloc))
SWITCH_C_TO_OCAML
RESTORE_ALL_REGS
LEAVE_FUNCTION
ret
CFI_ENDPROC
ENDFUNCTION(G(caml_call_local_realloc))

/******************************************************************************/
/* Call a C function from OCaml */
/******************************************************************************/
Expand Down
Loading