Skip to content

Hoist Wosize_val from end conditions of loops #2301

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 1 commit into from
Feb 22, 2024
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
11 changes: 7 additions & 4 deletions ocaml/runtime/backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ CAMLprim value caml_convert_raw_backtrace(value bt)
if (!caml_debug_info_available())
caml_failwith("No debug information available");

for (i = 0, index = 0; i < Wosize_val(bt); ++i)
mlsize_t bt_size = Wosize_val(bt);

for (i = 0, index = 0; i < bt_size; ++i)
{
debuginfo dbg;
for (dbg = caml_debuginfo_extract(Backtrace_slot_val(Field(bt, i)));
Expand All @@ -289,7 +291,7 @@ CAMLprim value caml_convert_raw_backtrace(value bt)

array = caml_alloc(index, 0);

for (i = 0, index = 0; i < Wosize_val(bt); ++i)
for (i = 0, index = 0; i < bt_size; ++i)
{
debuginfo dbg;
for (dbg = caml_debuginfo_extract(Backtrace_slot_val(Field(bt, i)));
Expand Down Expand Up @@ -359,8 +361,9 @@ CAMLprim value caml_get_exception_backtrace(value unit)
} else {
backtrace = caml_get_exception_raw_backtrace(Val_unit);

arr = caml_alloc(Wosize_val(backtrace), 0);
for (i = 0; i < Wosize_val(backtrace); i++) {
mlsize_t backtrace_size = Wosize_val(backtrace);
arr = caml_alloc(backtrace_size, 0);
for (i = 0; i < backtrace_size; i++) {
backtrace_slot slot = Backtrace_slot_val(Field(backtrace, i));
debuginfo dbg = caml_debuginfo_extract(slot);
Store_field(arr, i, caml_convert_debuginfo(dbg));
Expand Down
6 changes: 6 additions & 0 deletions ocaml/runtime/caml/mlvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ where 0 <= R <= 31 is HEADER_RESERVED_BITS, set with the
#define Num_tags (1ull << HEADER_TAG_BITS)
#define Max_wosize ((1ull << HEADER_WOSIZE_BITS) - 1ull)

// Note that Wosize_val and the other macros that read headers will not
// be optimized by common subexpression elimination, because of the
// atomic header loads. It is best to bind the results of such macros
// to variables if they will be tested repeatedly, e.g. as the end condition
// in a for-loop.

#define Wosize_val(val) (Wosize_hd (Hd_val (val)))
#define Wosize_op(op) (Wosize_val (op))
#define Wosize_bp(bp) (Wosize_val (bp))
Expand Down
6 changes: 4 additions & 2 deletions ocaml/runtime/globroots.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ static void scan_native_globals(scanning_action f, void* fdata)
for(glob = caml_globals[i]; *glob != 0; glob++) {
glob_block = *glob;
compute_index_for_global_root_scan(&glob_block, &start);
for (j = start; j < Wosize_val(glob_block); j++) {
mlsize_t size = Wosize_val(glob_block);
for (j = start; j < size; j++) {
f(fdata, Field(glob_block, j), &Field(glob_block, j));
}
}
Expand All @@ -262,7 +263,8 @@ static void scan_native_globals(scanning_action f, void* fdata)
for(glob = (value *) lnk->data; *glob != 0; glob++) {
glob_block = *glob;
compute_index_for_global_root_scan(&glob_block, &start);
for (j = start; j < Wosize_val(glob_block); j++) {
mlsize_t size = Wosize_val(glob_block);
for (j = start; j < size; j++) {
f(fdata, Field(glob_block, j), &Field(glob_block, j));
}
}
Expand Down
3 changes: 2 additions & 1 deletion ocaml/runtime/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ CAMLexport void caml_set_fields (value obj, value v)
int i;
CAMLassert (Is_block(obj));

for (i = 0; i < Wosize_val(obj); i++) {
mlsize_t size = Wosize_val(obj);
for (i = 0; i < size; i++) {
caml_modify(&Field(obj, i), v);
}
}
Expand Down
5 changes: 3 additions & 2 deletions ocaml/runtime/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ static char* buffer_of_bytes_array(value ls, asize_t *len)
int i;

*len = 0;
for (i = 0; i < Wosize_val(ls); i++) {
mlsize_t ls_size = Wosize_val(ls);
for (i = 0; i < ls_size; i++) {
s = Field(ls, i);
*len += caml_string_length(s);
}

ret = caml_stat_alloc(*len);
off = 0;
for (i = 0; i < Wosize_val(ls); i++) {
for (i = 0; i < ls_size; i++) {
size_t s_len;
s = Field(ls, i);
s_len = caml_string_length(s);
Expand Down
3 changes: 2 additions & 1 deletion ocaml/runtime/minor_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ static void oldify_mopup (struct oldify_state* st, int do_ephemerons)
if (Is_block (f) && Is_young(f)) {
oldify_one (st, f, Op_val (new_v));
}
for (i = 1; i < Wosize_val (new_v); i++){
mlsize_t new_v_size = Wosize_val (new_v);
for (i = 1; i < new_v_size; i++){
f = Field(v, i);
CAMLassert (!Is_debug_tag(f));
if (Is_block (f) && Is_young(f)) {
Expand Down
3 changes: 2 additions & 1 deletion ocaml/runtime/printexc.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ CAMLexport char * caml_format_exception(value exn)
start = 1;
}
add_char(&buf, '(');
for (i = start; i < Wosize_val(bucket); i++) {
mlsize_t bucket_size = Wosize_val(bucket);
for (i = start; i < bucket_size; i++) {
if (i > start) add_string(&buf, ", ");
v = Field(bucket, i);
if (Is_long(v)) {
Expand Down
3 changes: 2 additions & 1 deletion ocaml/runtime/shared_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ static void verify_object(struct heap_verify_state* st, value v) {
if (Tag_val(v) == Closure_tag) {
i = Start_env_closinfo(Closinfo_val(v));
}
for (; i < Wosize_val(v); i++) {
mlsize_t size = Wosize_val(v);
for (; i < size; i++) {
value f = Field(v, i);
if (Is_block(f)) verify_push(st, f, Op_val(v)+i);
}
Expand Down
3 changes: 2 additions & 1 deletion ocaml/runtime/weak.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ static void ephe_copy_and_darken(value from, value to)

/* Copy and darken scannable fields */
caml_domain_state* domain_state = Caml_state;
while (i < Wosize_val(to)) {
mlsize_t to_size = Wosize_val(to);
while (i < to_size) {
value field = Field(from, i);
caml_darken (domain_state, field, 0);
Store_field(to, i, field);
Expand Down