Skip to content

Commit 8a69d95

Browse files
committed
fixup! Reference unboxing
1 parent f1dfc22 commit 8a69d95

File tree

6 files changed

+48
-38
lines changed

6 files changed

+48
-38
lines changed

compiler/lib/driver.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ let rec loop max name round i (p : 'a) : 'a =
216216
let round profile : 'a -> 'a =
217217
print
218218
+> tailcall
219-
+> (flow +> specialize +> eval +> fst)
220219
+> Ref_unboxing.f
220+
+> (flow +> specialize +> eval +> fst)
221221
+> inline profile
222222
+> phi
223223
+> deadcode

compiler/lib/phisimpl.ml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ let program_deps { blocks; _ } =
6969
(fun _pc block ->
7070
List.iter block.body ~f:(fun i ->
7171
match i with
72-
| Let (x, Prim (Extern "%identity", [ Pv y ])) ->
73-
(* This is introduced by the reference unboxing pass *)
74-
add_var vars x;
75-
add_dep deps x y;
76-
add_def vars defs x y
7772
| Let (x, e) ->
7873
add_var vars x;
7974
expr_deps blocks vars deps defs x e

compiler/lib/ref_unboxing.ml

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,48 @@ let times = Debug.find "times"
3737

3838
let stats = Debug.find "stats"
3939

40-
let rewrite refs block m =
41-
let m, l =
40+
let rewrite refs block m m' =
41+
let m, m', l =
4242
List.fold_left
43-
~f:(fun (m, acc) i ->
43+
~f:(fun (m, m', acc) i ->
4444
match i with
4545
| Let (x, Block (0, [| y |], (NotArray | Unknown), Maybe_mutable))
46-
when Var.Set.mem x refs -> Var.Map.add x y m, acc
46+
when Var.Set.mem x refs -> Var.Map.add x y m, m', acc
4747
| Let (y, Field (x, 0, Non_float)) when Var.Map.mem x m ->
4848
(* Optimized away by Phisimpl *)
49-
m, Let (y, Prim (Extern "%identity", [ Pv (Var.Map.find x m) ])) :: acc
49+
m, Var.Map.add y (Var.Map.find x m) m', acc
5050
| Offset_ref (x, n) when Var.Map.mem x m ->
5151
let y = Var.fresh () in
5252
( Var.Map.add x y m
53+
, m'
5354
, Let
5455
( y
5556
, Prim
5657
( Extern "%int_add"
5758
, [ Pv (Var.Map.find x m); Pc (Int (Targetint.of_int_exn n)) ] ) )
5859
:: acc )
59-
| Set_field (x, 0, Non_float, y) when Var.Map.mem x m -> Var.Map.add x y m, acc
60+
| Set_field (x, 0, Non_float, y) when Var.Map.mem x m ->
61+
Var.Map.add x y m, m', acc
6062
| Event _ -> (
6163
( m
64+
, m'
6265
, match acc with
6366
| Event _ :: prev ->
6467
(* Avoid consecutive events (keep just the last one) *)
6568
i :: prev
6669
| _ -> i :: acc ))
67-
| _ -> m, i :: acc)
70+
| _ -> m, m', i :: acc)
6871
block.body
69-
~init:(m, [])
72+
~init:(m, m', [])
7073
in
71-
m, List.rev l
74+
m, m', List.rev l
7275

7376
let rewrite_cont relevant_vars vars (pc', args) =
7477
let refs, _ = Int.Hashtbl.find relevant_vars pc' in
7578
let vars = Var.Map.filter (fun x _ -> Var.Set.mem x refs) vars in
7679
pc', List.map ~f:snd (Var.Map.bindings vars) @ args
7780

78-
let rewrite_function p variables pc =
81+
let rewrite_function p variables pc subst =
7982
let relevant_vars = Int.Hashtbl.create 16 in
8083
let g = Structure.(dominator_tree (build_graph p.blocks pc)) in
8184
let rec traverse_tree g pc refs =
@@ -94,14 +97,14 @@ let rewrite_function p variables pc =
9497
Addr.Set.iter (fun pc' -> traverse_tree g pc' refs') (Structure.get_edges g pc)
9598
in
9699
traverse_tree g pc Var.Set.empty;
97-
let rec traverse_tree' g pc blocks =
100+
let rec traverse_tree' g pc blocks subst =
98101
let block = Addr.Map.find pc p.blocks in
99102
let refs, refs' = Int.Hashtbl.find relevant_vars pc in
100103
let vars =
101104
Var.Set.fold (fun x m -> Var.Map.add x (Var.fork x) m) refs Var.Map.empty
102105
in
103106
let params = List.map ~f:snd (Var.Map.bindings vars) @ block.params in
104-
let vars, body = rewrite refs' block vars in
107+
let vars, subst, body = rewrite refs' block vars subst in
105108
let branch =
106109
match block.branch with
107110
| Return _ | Raise _ | Stop -> block.branch
@@ -122,12 +125,12 @@ let rewrite_function p variables pc =
122125
in
123126
let blocks = Addr.Map.add pc { params; body; branch } blocks in
124127
Addr.Set.fold
125-
(fun pc' blocks -> traverse_tree' g pc' blocks)
128+
(fun pc' (blocks, subst) -> traverse_tree' g pc' blocks subst)
126129
(Structure.get_edges g pc)
127-
blocks
130+
(blocks, subst)
128131
in
129-
let blocks = traverse_tree' g pc p.blocks in
130-
{ p with blocks }
132+
let blocks, subst = traverse_tree' g pc p.blocks subst in
133+
{ p with blocks }, subst
131134

132135
let f p =
133136
let t = Timer.make () in
@@ -205,7 +208,17 @@ let f p =
205208
let functions =
206209
Var.Hashtbl.fold (fun _ pc s -> Addr.Set.add pc s) candidates Addr.Set.empty
207210
in
208-
let p = Addr.Set.fold (fun pc p -> rewrite_function p candidates pc) functions p in
211+
let p, subst =
212+
Addr.Set.fold
213+
(fun pc (p, subst) -> rewrite_function p candidates pc subst)
214+
functions
215+
(p, Var.Map.empty)
216+
in
217+
let p =
218+
if Var.Map.is_empty subst
219+
then p
220+
else Subst.Excluding_Binders.program (Subst.from_map subst) p
221+
in
209222
if times () then Format.eprintf " reference unboxing: %a@." Timer.print t;
210223
if stats ()
211224
then Format.eprintf "Stats - reference unboxing: %d@." (Var.Hashtbl.length candidates);

compiler/tests-compiler/global_deadcode.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ let%expect_test "Bug fix in PR #1681" =
146146
x.a <- 1; (* This has to be handled after [x] is returned *)
147147
{a = 3; b = 4}
148148
)
149-
let g = ref (fun _ -> assert false)
149+
let g =
150+
(* Make sure the reference is not optimized away *)
151+
let rec h n = if n = 0 then ref else h (n - 1) in
152+
h 0 (fun _ -> assert false)
150153
let _ =
151154
(* We should not track that [f] is used below *)
152155
g := f; prerr_int ((!g true).b + (!g false).b)

compiler/tests-compiler/loops.ml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -582,23 +582,22 @@ let () = print_endline (trim " ")
582582
var
583583
s$0 = copy(caml_bytes_of_string(x)),
584584
len = caml_ml_bytes_length(s$0),
585-
ofs = 0;
585+
i = 0;
586586
for(;;){
587-
if(ofs >= len) break;
588-
if(! is_space(caml_bytes_unsafe_get(s$0, ofs))) break;
589-
ofs = ofs + 1 | 0;
587+
if(i >= len) break;
588+
if(! is_space(caml_bytes_unsafe_get(s$0, i))) break;
589+
i = i + 1 | 0;
590590
}
591591
var j = len - 1 | 0;
592592
for(;;){
593-
if(ofs <= j && is_space(caml_bytes_unsafe_get(s$0, j))){j = j - 1 | 0; continue;}
593+
if(i <= j && is_space(caml_bytes_unsafe_get(s$0, j))){j = j - 1 | 0; continue;}
594594
a:
595595
{
596-
if(ofs <= j){
597-
var len$0 = (j - ofs | 0) + 1 | 0;
598-
if
599-
(0 <= ofs && 0 <= len$0 && (caml_ml_bytes_length(s$0) - len$0 | 0) >= ofs){
596+
if(i <= j){
597+
var len$0 = (j - i | 0) + 1 | 0;
598+
if(0 <= i && 0 <= len$0 && (caml_ml_bytes_length(s$0) - len$0 | 0) >= i){
600599
var r = caml_create_bytes(len$0);
601-
caml_blit_bytes(s$0, ofs, r, 0, len$0);
600+
caml_blit_bytes(s$0, i, r, 0, len$0);
602601
var b = r;
603602
break a;
604603
}

compiler/tests-full/stdlib.cma.expected.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16757,7 +16757,7 @@
1675716757
/*<<camlinternalFormat.ml:1429:6>>*/ caml_ml_string_length(s) - 1 | 0,
1675816758
_aS_ = 0;
1675916759
if(_aQ_ < 0)
16760-
var digits = _aP_;
16760+
var n$1 = _aP_;
1676116761
else{
1676216762
var n = _aP_, i$0 = _aS_;
1676316763
for(;;){
@@ -16768,7 +16768,7 @@
1676816768
? n
1676916769
: n + 1 | 0,
1677016770
_aW_ = i$0 + 1 | 0;
16771-
if(_aQ_ === i$0){var digits = n$0; break;}
16771+
if(_aQ_ === i$0){var n$1 = n$0; break;}
1677216772
n = n$0;
1677316773
i$0 = _aW_;
1677416774
}
@@ -16777,7 +16777,7 @@
1677716777
buf =
1677816778
/*<<camlinternalFormat.ml:1436:4>>*/ /*<<camlinternalFormat.ml:1436:14>>*/ caml_create_bytes
1677916779
( /*<<camlinternalFormat.ml:1436:4>>*/ caml_ml_string_length(s)
16780-
+ ((digits - 1 | 0) / 3 | 0)
16780+
+ ((n$1 - 1 | 0) / 3 | 0)
1678116781
| 0),
1678216782
pos = /*<<camlinternalFormat.ml:1437:4>>*/ [0, 0];
1678316783
function put(c){
@@ -16787,7 +16787,7 @@
1678716787
var
1678816788
_aR_ =
1678916789
/*<<camlinternalFormat.ml:1440:4>>*/ caml_ml_string_length(s) - 1 | 0,
16790-
_aT_ = ((digits - 1 | 0) % 3 | 0) + 1 | 0,
16790+
_aT_ = ((n$1 - 1 | 0) % 3 | 0) + 1 | 0,
1679116791
_aU_ = 0;
1679216792
if(_aR_ >= 0){
1679316793
var left = _aT_, i = _aU_;

0 commit comments

Comments
 (0)