Skip to content

Commit

Permalink
temporarily revert optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonch committed Jul 26, 2024
1 parent 7213297 commit ac533c4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 102 deletions.
22 changes: 1 addition & 21 deletions src/lib/backend/tofino/core/InlineEventVars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -569,29 +569,9 @@ let rec inline_decls ctx decls =
d::(inline_decls ctx ds)
;;

(* this checker determines whether there are any event variables to inline.
If there are not, we can skip this pass. *)
let has_event_vars ds =
let found = ref false in
let v = object
inherit [_] s_iter as super
method! visit_exp ctx exp =
match exp.e, exp.ety.raw_ty with
| EVar(_), TEvent -> found := true
| _ -> super#visit_exp ctx exp
end
in
v#visit_decls () ds;
!found
;;

let inline tds =
(* inline_locations tds |> eliminate_this |> inline_event_vars ;; *)
let res = inline_locations tds |> eliminate_this in
let res = if not (has_event_vars res)
then res
else inline_decls empty_ctx res
in
let res = inline_locations tds |> eliminate_this |> (inline_decls empty_ctx) in
(* print_endline ("after inline event variables");
print_endline (CorePrinting.decls_to_string res);
exit 1; *)
Expand Down
35 changes: 2 additions & 33 deletions src/lib/backend/tofino/tofinocore/AddHandlerTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let typed_generate_seqs (hdl_body:statement) : (id * gen_type) list list =
;;

(* find the generates in a statement *)
(* let rec find_generates (stmt : statement) : (gen_type * exp) list =
let rec find_generates (stmt : statement) : (gen_type * exp) list =
match stmt.s with
| SNoop | SUnit _ | SLocal _ | SAssign _ | SPrintf _ | SRet _ ->
[]
Expand All @@ -109,38 +109,7 @@ let typed_generate_seqs (hdl_body:statement) : (id * gen_type) list list =
find_generates stmt1 @ find_generates stmt2
| SMatch (_, branch_list) ->
List.concat (List.map (fun (_, stmt) -> find_generates stmt) branch_list)
;; *)

let extract_generate acc stmt =
match stmt.s with
| SGen (gen_type, exp) -> (gen_type, exp)::acc
| _ -> acc

let find_generates stmt =
List.fold_left extract_generate
[]
(flatten_stmt stmt)
|> List.rev


(*
let rec find_generates_aux stmt acc =
match stmt.s with
| SNoop | SUnit _ | SLocal _ | SAssign _ | SPrintf _ | SRet _ ->
acc
| STupleAssign _ ->
acc
| SIf (_, then_stmt, else_stmt) ->
find_generates_aux then_stmt (find_generates_aux else_stmt acc)
| SGen (gen_type, exp) ->
(gen_type, exp) :: acc
| SSeq (stmt1, stmt2) ->
find_generates_aux stmt1 (find_generates_aux stmt2 acc)
| SMatch (_, branch_list) ->
List.fold_left (fun acc (_, stmt) -> find_generates_aux stmt acc) acc branch_list
let find_generates stmt =
List.rev (find_generates_aux stmt []) *)
;;


(* derive the output event for this handler based on the generate statements *)
Expand Down
18 changes: 9 additions & 9 deletions src/lib/backend/tofino/tofinocore/RemoveLocalInits.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ let vars_in_exp exp : cid list =
inherit [_] s_iter as super
method vars = Hashtbl.create 128;
method! visit_EVar _ cid =
Hashtbl.replace (self#vars) cid true
Hashtbl.add (self#vars) cid true
end
in
vars_read#visit_exp () exp;
Hashtbl.to_seq_keys vars_read#vars |> List.of_seq
;;

let update_symbols (symbols : (cid,var_status) Hashtbl.t) exp =
let update_symbols symbols exp =
List.iter
(fun cid ->
match (find_opt symbols cid) with
| Some(Declared) ->
(* a variable is used after it is declared,
without an intermediate assignment. It is not
removable. *)
Hashtbl.replace symbols cid Read
add symbols cid Read
| _ -> ()
(* used without a declaration.
This can happen, e.g., params.
Expand All @@ -122,7 +122,6 @@ let update_symbols (symbols : (cid,var_status) Hashtbl.t) exp =
(vars_in_exp exp)
;;


(* find the variables that can be preallocated *)
let find_vars_to_prealloc stmt =
let symbols = create 128 in
Expand All @@ -137,13 +136,14 @@ let find_vars_to_prealloc stmt =
(* post-visit locals (so we hit exp first) *)
super#visit_SLocal symbols id ty exp;
(* now, update the symbols table for the newly declared var *)
Hashtbl.replace symbols (Cid.id id) Declared;
add symbols (Cid.id id) Declared;

method! visit_SAssign symbols cid exp =
super#visit_SAssign symbols cid exp;
match (find_opt symbols (cid)) with
| Some(Declared) ->
(* was declared, now is declared and assigned *)
Hashtbl.replace symbols (cid) Assigned;
add symbols (cid) Assigned;
| _ -> ()

(* we need to handle branches, ensuring to update the symbols table at the end *)
Expand All @@ -160,8 +160,8 @@ let find_vars_to_prealloc stmt =
let ss1 = ht_to_list symbols1 in
let ss2 = ht_to_list symbols2 in
let ss = ht_to_list symbols in
let ss = merge_symbols ss [ss1; ss2] in
Hashtbl.replace_seq symbols (List.to_seq ss)
let ss = merge_symbols ss [ss1; ss2] in
Hashtbl.add_seq symbols (List.to_seq ss)
method! visit_SMatch symbols exps branches =
List.iter (super#visit_exp symbols) exps;
let bstmts = List.split branches |> snd in
Expand All @@ -176,7 +176,7 @@ let find_vars_to_prealloc stmt =
in
let ss = ht_to_list symbols in
let ss = merge_symbols ss bstmt_ss in
Hashtbl.replace_seq symbols (List.to_seq ss)
Hashtbl.add_seq symbols (List.to_seq ss)
end
in
(* traverse the program to build up the symbols table *)
Expand Down
45 changes: 6 additions & 39 deletions src/lib/backend/tofino/tofinocore/TofinoCore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -597,20 +597,20 @@ let append_and_new seqs e =
| seq :: seqs -> (seq @ [e]) :: append_to_all seqs e
;;
(* find all paths of statements that match the filter_map *)
let rec find_statement_paths_not_tail_recursive paths_so_far stmt_filter stmt =
let rec find_statement_paths paths_so_far stmt_filter stmt =
match stmt.s with
| SSeq (s1, s2) ->
(* find all the statement paths through s1, then for each of those paths
find all the paths including s2 *)
let paths_including_s1 = find_statement_paths_not_tail_recursive paths_so_far stmt_filter s1 in
let paths_including_s2 = find_statement_paths_not_tail_recursive paths_including_s1 stmt_filter s2 in
let paths_including_s1 = find_statement_paths paths_so_far stmt_filter s1 in
let paths_including_s2 = find_statement_paths paths_including_s1 stmt_filter s2 in
(* printres "seq" res; *)
paths_including_s2
| SIf (_, s1, s2) ->
(* a branch with no inner paths creates a path *)
let s1_paths = find_statement_paths_not_tail_recursive paths_so_far stmt_filter s1 in
let s1_paths = find_statement_paths paths_so_far stmt_filter s1 in
let s1_paths = if (s1_paths = []) then [[]] else s1_paths in
let s2_paths = find_statement_paths_not_tail_recursive paths_so_far stmt_filter s2 in
let s2_paths = find_statement_paths paths_so_far stmt_filter s2 in
let s2_paths = if (s2_paths = []) then [[]] else s2_paths in


Expand Down Expand Up @@ -654,7 +654,7 @@ let rec find_statement_paths_not_tail_recursive paths_so_far stmt_filter stmt =
let res =
List.fold_left
(fun seqs (_, bstmt) ->
let bstmt_paths = find_statement_paths_not_tail_recursive paths_so_far stmt_filter bstmt in
let bstmt_paths = find_statement_paths paths_so_far stmt_filter bstmt in
let bstmt_paths = if (bstmt_paths = []) then [[]] else bstmt_paths in
seqs @ bstmt_paths)
[]
Expand All @@ -668,39 +668,6 @@ let rec find_statement_paths_not_tail_recursive paths_so_far stmt_filter stmt =
| None -> paths_so_far)
;;

let find_statement_paths _ (stmt_filter : statement -> 'a option) stmt : ('a list list) =
let rec helper stmt k = match stmt.s with
| SSeq (s1, s2) ->
helper
s1
(fun paths_s1 ->
helper s2
(fun paths_s2 ->
k
(paths_s1 @ paths_s2)))
| SIf (_, s1, s2) ->
helper s1 (fun paths_s1 ->
helper s2 (fun paths_s2 ->
let paths_s1 = if paths_s1 = [] then [[]] else paths_s1 in
let paths_s2 = if paths_s2 = [] then [[]] else paths_s2 in
k (paths_s1 @ paths_s2)))
| SMatch (_, ps) ->
let fold_fun acc (_, bstmt) =
helper bstmt (fun bstmt_paths ->
let bstmt_paths = if bstmt_paths = [] then [[]] else bstmt_paths in
k (acc @ bstmt_paths))
in
List.fold_left fold_fun [] ps
| _ ->
(match stmt_filter stmt with
| Some r -> k (append_and_new [] r)
| None -> k [])
in
helper stmt (fun x -> x)
;;




(* find all paths of statements that match stmt_filter and
transform matching statements according to stmt_transformer *)
Expand Down

0 comments on commit ac533c4

Please sign in to comment.