Skip to content

squash closure #175

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

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 29 additions & 0 deletions compiler/inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@

open Code

let squash_closure blocks args (pc,l) =
let rec aux pc acc =
let b = AddrMap.find pc blocks in
match b with
| { body = [Let (x, Closure (args,(pc,l)))];
branch = Return x';
handler = None;
params = [] } when x = x' ->
aux pc (args :: acc)
| {params=[]} -> begin
match acc with
| [] -> assert false
| [_] -> None
| acc ->
let args = List.fold_left (fun args args' ->
args' @ args
) [] acc in
Some (Closure (args, (pc,[])))
end
| _ -> None
in
aux pc [args]

let get_closures (_, blocks, _) =
AddrMap.fold
(fun _ block closures ->
Expand Down Expand Up @@ -126,6 +149,12 @@ let inline closures live_vars blocks free_pc pc =
in
([], (Branch (free_pc + 1, args), blocks, free_pc + 2))

| Let (x,Closure (l,cont)) when Option.Optim.squash () ->
begin
match squash_closure blocks l cont with
| None -> i::rem,state
| Some c -> Let (x,c)::rem,state
end
| _ ->
(i :: rem, state))
block.body ([], (block.branch, blocks, free_pc))
Expand Down
2 changes: 2 additions & 0 deletions compiler/option.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ module Optim = struct
let include_cmis = o ~name:"withcmi" ~default: true
let warn_unused = o ~name:"warn-unused" ~default: false

let squash = o ~name:"squash" ~default: true

let inline_callgen = o ~name:"callgen" ~default:false

(* this does not optimize properly *)
Expand Down
2 changes: 2 additions & 0 deletions compiler/option.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module Optim : sig
val warn_unused : unit -> bool
val inline_callgen : unit -> bool

val squash : unit -> bool

val enable : string -> unit
val disable : string -> unit
end
Expand Down