Skip to content

Commit 9d9f11e

Browse files
committed
More assertions for local alloc and letrec
1 parent 80d1383 commit 9d9f11e

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

backend/cmmgen.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,15 @@ type rhs_kind =
154154
| RHS_nonrec
155155
;;
156156

157+
let assert_not_local : Lambda.alloc_mode -> unit = function
158+
| Alloc_heap -> ()
159+
| Alloc_local -> Misc.fatal_error "Invalid stack allocation found"
160+
157161
let rec expr_size env = function
158162
| Uvar id ->
159163
begin try V.find_same id env with Not_found -> RHS_nonrec end
160164
| Uclosure { functions ; not_scanned_slots ; scanned_slots } ->
165+
List.iter (fun f -> assert_not_local f.mode) functions;
161166
RHS_block (fundecls_size functions + List.length not_scanned_slots
162167
+ List.length scanned_slots)
163168
| Ulet(_str, _kind, id, exp, body) ->
@@ -169,13 +174,17 @@ let rec expr_size env = function
169174
bindings env
170175
in
171176
expr_size env body
172-
| Uprim(Pmakeblock _, args, _) ->
177+
| Uprim(Pmakeblock (_, _, _, mode), args, _) ->
178+
assert_not_local mode;
173179
RHS_block (List.length args)
174-
| Uprim(Pmakearray((Paddrarray | Pintarray), _, _), args, _) ->
180+
| Uprim(Pmakearray((Paddrarray | Pintarray), _, mode), args, _) ->
181+
assert_not_local mode;
175182
RHS_block (List.length args)
176-
| Uprim(Pmakearray(Pfloatarray, _, _), args, _) ->
183+
| Uprim(Pmakearray(Pfloatarray, _, mode), args, _) ->
184+
assert_not_local mode;
177185
RHS_floatblock (List.length args)
178-
| Uprim(Pmakearray(Pgenarray, _, _), _, _) ->
186+
| Uprim(Pmakearray(Pgenarray, _, mode), _, _) ->
187+
assert_not_local mode;
179188
(* Pgenarray is excluded from recursive bindings by the
180189
check in Translcore.check_recursive_lambda *)
181190
RHS_nonrec

middle_end/flambda2/from_lambda/dissect_letrec.ml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ let is_simple (lam : Lambda.lambda) =
187187
match lam with Lvar _ | Lconst _ -> true | _ -> false
188188
[@@ocaml.warning "-fragile-match"]
189189

190+
let assert_not_local : Lambda.alloc_mode -> unit = function
191+
| Alloc_heap -> ()
192+
| Alloc_local -> Misc.fatal_error "Invalid stack allocation found"
193+
190194
let dead_code lam letrec =
191195
(* Some cases generate code without effects, and bound to nothing. We use this
192196
function to insert it as [Lsequence] in [effects], for documentation. It
@@ -203,6 +207,7 @@ let rec prepare_letrec (recursive_set : Ident.Set.t)
203207
(lam : Lambda.lambda) (letrec : letrec) =
204208
match lam with
205209
| Lfunction funct -> (
210+
assert_not_local funct.mode;
206211
match current_let with
207212
| Some current_let when Ident.Set.mem current_let.ident recursive_set ->
208213
{ letrec with functions = (current_let.ident, funct) :: letrec.functions }
@@ -221,6 +226,10 @@ let rec prepare_letrec (recursive_set : Ident.Set.t)
221226
| None -> dead_code lam letrec)
222227
| Lprim (((Pmakeblock _ | Pmakearray _ | Pduprecord _) as prim), args, dbg)
223228
when not (List.for_all is_simple args) ->
229+
(match prim with
230+
| Pmakeblock (_, _, _, mode) | Pmakearray (_, _, mode) ->
231+
assert_not_local mode
232+
| _ -> ());
224233
(* If there are some non-trivial expressions as arguments, we first extract
225234
the arguments (to let-bound variables) before deconstructing. Arguments
226235
could contain side effects and other blocks declarations. *)
@@ -246,15 +255,16 @@ let rec prepare_letrec (recursive_set : Ident.Set.t)
246255
defs
247256
in
248257
prepare_letrec recursive_set current_let lam letrec
249-
| Lprim (Pmakeblock _, args, _)
250-
| Lprim (Pmakearray ((Paddrarray | Pintarray), _, _), args, _) -> (
258+
| Lprim (Pmakeblock (_, _, _, mode), args, _)
259+
| Lprim (Pmakearray ((Paddrarray | Pintarray), _, mode), args, _) -> (
260+
assert_not_local mode;
251261
match current_let with
252262
| Some cl -> build_block cl (List.length args) (Normal 0) lam letrec
253-
| None ->
254-
dead_code lam letrec
255-
(* We know that [args] are all "simple" at this point, so no effects *))
256-
| Lprim (Pmakearray (Pfloatarray, _, _), args, _)
257-
| Lprim (Pmakefloatblock _, args, _) -> (
263+
| None -> dead_code lam letrec
264+
(* We know that [args] are all "simple" at this point, so no effects *))
265+
| Lprim (Pmakearray (Pfloatarray, _, mode), args, _)
266+
| Lprim (Pmakefloatblock (_, mode), args, _) -> (
267+
assert_not_local mode;
258268
match current_let with
259269
| Some cl -> build_block cl (List.length args) Boxed_float lam letrec
260270
| None -> dead_code lam letrec)

0 commit comments

Comments
 (0)