Skip to content

remove optimizaitons relying on int64 internals, which is fragile when switching to bigint #3906

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 2 commits into from
Oct 25, 2019
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
18 changes: 12 additions & 6 deletions jscomp/core/js_long.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ let make ~lo ~hi =
~comment:"int64" (E.zero_int_literal)
record_info [ hi; E.to_uint32 lo ]
Immutable
let get_lo x = E.array_index_by_int x 1l
let get_hi x = E.array_index_by_int x 0l




(* below should not depend on layout *)
Expand All @@ -66,7 +66,9 @@ let of_const (v : Int64.t) =
~hi:(Int64.to_int32 (Int64.shift_right v 32))

let to_int32 args =
E.to_int32 @@ get_lo (Ext_list.singleton_exn args)
int64_call "to_int32" args
(* let get_lo x = E.array_index_by_int x 1l *)
(* E.to_int32 @@ get_lo (Ext_list.singleton_exn args) *)


let of_int32 (args : J.expression list) =
Expand Down Expand Up @@ -108,15 +110,19 @@ let div args =
current pipe-line fall back to a function call
*)
let bit_op (op : E.t -> E.t -> E.t) runtime_call args =
match args with
int64_call runtime_call args
(*disable optimizations relying on int64 representations
this maybe outdated when we switch to bigint
*)
(* match args with
| [l;r] ->
(* Int64 is a block in ocaml, a little more conservative in inlining *)
if Js_analyzer.is_okay_to_duplicate l &&
Js_analyzer.is_okay_to_duplicate r then
make ~lo:(op (get_lo l) (get_lo r))
~hi:(op (get_hi l) (get_hi r))
else int64_call runtime_call args
| _ -> assert false
else
| _ -> assert false *)

let xor = bit_op E.int32_bxor "xor"
let or_ = bit_op E.int32_bor "or_"
Expand Down
230 changes: 134 additions & 96 deletions jscomp/test/int64_test.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion jscomp/test/int64_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ let suites : Mt.pair_suites = Mt.[
));
"mul simple", (fun _ -> Eq (6L, mul 3L 2L ));
"of_int32", (fun _ ->
Eq(Array.map Int64.of_int32 [|0l;(-2147483648l)|], [|0L; (-2147483648L)|]));
Eq(Array.map Int64.of_int32 [|0l;(-2147483648l)|], [|0L; (-2147483648L)|]);
);
"of_int32_singleton",(fun _ ->
Eq ( Int64.of_int32 (-3l), -3L )
) ;
__LOC__ , (fun _ ->
Eq (Int64.of_int32 3l , 3L)
);
"to_int32", (fun _ ->
Eq(Array.map Int64.to_int32
[|0L; 0x0000_0000_8000_0000L|], [|0l;-2147483648l|]));
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/swap_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ var suites_64 = List.map((function (param) {
])
]),
"swap64 %d"
]), a[1] | 0),
]), Caml_int64.to_int32(a)),
(function (param) {
return /* Eq */Block.__(0, [
Caml_int64.swap(a),
Expand Down
18 changes: 12 additions & 6 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117310,8 +117310,8 @@ let make ~lo ~hi =
~comment:"int64" (E.zero_int_literal)
record_info [ hi; E.to_uint32 lo ]
Immutable
let get_lo x = E.array_index_by_int x 1l
let get_hi x = E.array_index_by_int x 0l




(* below should not depend on layout *)
Expand All @@ -117323,7 +117323,9 @@ let of_const (v : Int64.t) =
~hi:(Int64.to_int32 (Int64.shift_right v 32))

let to_int32 args =
E.to_int32 @@ get_lo (Ext_list.singleton_exn args)
int64_call "to_int32" args
(* let get_lo x = E.array_index_by_int x 1l *)
(* E.to_int32 @@ get_lo (Ext_list.singleton_exn args) *)


let of_int32 (args : J.expression list) =
Expand Down Expand Up @@ -117365,15 +117367,19 @@ let div args =
current pipe-line fall back to a function call
*)
let bit_op (op : E.t -> E.t -> E.t) runtime_call args =
match args with
int64_call runtime_call args
(*disable optimizations relying on int64 representations
this maybe outdated when we switch to bigint
*)
(* match args with
| [l;r] ->
(* Int64 is a block in ocaml, a little more conservative in inlining *)
if Js_analyzer.is_okay_to_duplicate l &&
Js_analyzer.is_okay_to_duplicate r then
make ~lo:(op (get_lo l) (get_lo r))
~hi:(op (get_hi l) (get_hi r))
else int64_call runtime_call args
| _ -> assert false
else
| _ -> assert false *)

let xor = bit_op E.int32_bxor "xor"
let or_ = bit_op E.int32_bor "or_"
Expand Down
18 changes: 12 additions & 6 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108809,8 +108809,8 @@ let make ~lo ~hi =
~comment:"int64" (E.zero_int_literal)
record_info [ hi; E.to_uint32 lo ]
Immutable
let get_lo x = E.array_index_by_int x 1l
let get_hi x = E.array_index_by_int x 0l




(* below should not depend on layout *)
Expand All @@ -108822,7 +108822,9 @@ let of_const (v : Int64.t) =
~hi:(Int64.to_int32 (Int64.shift_right v 32))

let to_int32 args =
E.to_int32 @@ get_lo (Ext_list.singleton_exn args)
int64_call "to_int32" args
(* let get_lo x = E.array_index_by_int x 1l *)
(* E.to_int32 @@ get_lo (Ext_list.singleton_exn args) *)


let of_int32 (args : J.expression list) =
Expand Down Expand Up @@ -108864,15 +108866,19 @@ let div args =
current pipe-line fall back to a function call
*)
let bit_op (op : E.t -> E.t -> E.t) runtime_call args =
match args with
int64_call runtime_call args
(*disable optimizations relying on int64 representations
this maybe outdated when we switch to bigint
*)
(* match args with
| [l;r] ->
(* Int64 is a block in ocaml, a little more conservative in inlining *)
if Js_analyzer.is_okay_to_duplicate l &&
Js_analyzer.is_okay_to_duplicate r then
make ~lo:(op (get_lo l) (get_lo r))
~hi:(op (get_hi l) (get_hi r))
else int64_call runtime_call args
| _ -> assert false
else
| _ -> assert false *)

let xor = bit_op E.int32_bxor "xor"
let or_ = bit_op E.int32_bor "or_"
Expand Down
16 changes: 8 additions & 8 deletions lib/js/caml_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,29 +602,29 @@ function caml_int64_format(fmt, x) {
/* lo */0
], match$1[0]);
var modulus = match$1[1];
s = String.fromCharCode(cvtbl.charCodeAt(modulus[1] | 0)) + s;
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus))) + s;
while(Caml_int64.neq(quotient, /* int64 */[
/* hi */0,
/* lo */0
])) {
var match$2 = Caml_int64.div_mod(quotient, wbase);
quotient = match$2[0];
modulus = match$2[1];
s = String.fromCharCode(cvtbl.charCodeAt(modulus[1] | 0)) + s;
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus))) + s;
};
} else {
var match$3 = Caml_int64.div_mod(x$1, wbase);
var quotient$1 = match$3[0];
var modulus$1 = match$3[1];
s = String.fromCharCode(cvtbl.charCodeAt(modulus$1[1] | 0)) + s;
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus$1))) + s;
while(Caml_int64.neq(quotient$1, /* int64 */[
/* hi */0,
/* lo */0
])) {
var match$4 = Caml_int64.div_mod(quotient$1, wbase);
quotient$1 = match$4[0];
modulus$1 = match$4[1];
s = String.fromCharCode(cvtbl.charCodeAt(modulus$1[1] | 0)) + s;
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus$1))) + s;
};
}
break;
Expand Down Expand Up @@ -652,29 +652,29 @@ function caml_int64_format(fmt, x) {
/* lo */3435973836
], match$5[0]), match$6[0]);
var modulus$2 = match$6[1];
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$2[1] | 0)) + s;
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$2))) + s;
while(Caml_int64.neq(quotient$2, /* int64 */[
/* hi */0,
/* lo */0
])) {
var match$7 = Caml_int64.div_mod(quotient$2, wbase$1);
quotient$2 = match$7[0];
modulus$2 = match$7[1];
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$2[1] | 0)) + s;
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$2))) + s;
};
} else {
var match$8 = Caml_int64.div_mod(x$1, wbase$1);
var quotient$3 = match$8[0];
var modulus$3 = match$8[1];
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$3[1] | 0)) + s;
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$3))) + s;
while(Caml_int64.neq(quotient$3, /* int64 */[
/* hi */0,
/* lo */0
])) {
var match$9 = Caml_int64.div_mod(quotient$3, wbase$1);
quotient$3 = match$9[0];
modulus$3 = match$9[1];
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$3[1] | 0)) + s;
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$3))) + s;
};
}
break;
Expand Down
7 changes: 2 additions & 5 deletions lib/js/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ function int64(s, bound) {
var b1 = Caml_int64.of_int32(bits(s$1));
var b2 = Caml_int64.lsl_(Caml_int64.of_int32(bits(s$1)), 30);
var b3 = Caml_int64.lsl_(Caml_int64.of_int32(bits(s$1) & 7), 60);
var r = Caml_int64.or_(b1, /* int64 */[
/* hi */b2[0] | b3[0],
/* lo */((b2[1] | b3[1]) >>> 0)
]);
var r = Caml_int64.or_(b1, Caml_int64.or_(b2, b3));
var v = Caml_int64.mod_(r, n);
if (Caml_int64.gt(Caml_int64.sub(r, v), Caml_int64.add(Caml_int64.sub(Int64.max_int, n), /* int64 */[
/* hi */0,
Expand All @@ -149,7 +146,7 @@ function int64(s, bound) {
}

var nativeint = Nativeint.size === 32 ? int32 : (function (s, bound) {
return int64(s, Caml_int64.of_int32(bound))[1] | 0;
return Caml_int64.to_int32(int64(s, Caml_int64.of_int32(bound)));
});

function rawfloat(s) {
Expand Down