Skip to content

Commit

Permalink
RenameVars cleanup (#11176)
Browse files Browse the repository at this point in the history
* [timers] fix

* encapsulate overlaps

* change random things because I don't know how to fix the actual issue yet

* cache name lookups

* rework filter timers

* apparently this was important somehow

* lose some more tctx

* I don't think the JVM target cares about local names
  • Loading branch information
Simn authored Apr 24, 2023
1 parent b7f7f4f commit 8aa9cc4
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 161 deletions.
2 changes: 1 addition & 1 deletion src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ let finalize_typing ctx tctx =
let filter ctx tctx =
let t = Timer.timer ["filters"] in
DeprecationCheck.run ctx.com;
Filters.run ctx.com tctx ctx.com.main;
Filters.run tctx ctx.com.main;
t()

let call_light_init_macro com path =
Expand Down
11 changes: 7 additions & 4 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ let add_local_with_origin ctx origin n t p =
check_local_variable_name ctx n origin p;
add_local ctx (VUser origin) n t p

let gen_local_prefix = "`"
let gen_local_prefix = "_g"

let gen_local ctx t p =
add_local ctx VGenerated "`" t p
add_local ctx VGenerated gen_local_prefix t p

let is_gen_local v =
String.unsafe_get v.v_name 0 = String.unsafe_get gen_local_prefix 0
let is_gen_local v = match v.v_kind with
| VGenerated ->
true
| _ ->
false

let delay ctx p f =
let rec loop = function
Expand Down
6 changes: 4 additions & 2 deletions src/core/timer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ let close = close (get_time())

(* Printing *)

let timer_threshold = 0.01

type timer_node = {
name : string;
path : string;
Expand Down Expand Up @@ -168,7 +170,7 @@ let build_times_tree () =
) node.children;
node.children <- List.sort (fun node1 node2 -> compare node2.time node1.time) node.children;
if node.num_calls > !max_calls then max_calls := node.num_calls;
if node.time > 0.0009 && l > !max_name then max_name := l;
if node.time >= timer_threshold && l > !max_name then max_name := l;
in
loop 0 root;
!max_name,!max_calls,root
Expand All @@ -180,7 +182,7 @@ let report_times print =
let sep = String.make (max_name + max_calls + 27) '-' in
print sep;
let print_time name node =
if node.time > 0.0009 then
if node.time >= timer_threshold then
print (Printf.sprintf "%-*s | %7.3f | %3.0f | %3.0f | %*i | %s" max_name name node.time (node.time *. 100. /. root.time) (node.time *. 100. /. node.parent.time) max_calls node.num_calls node.info)
in
let rec loop depth node =
Expand Down
8 changes: 4 additions & 4 deletions src/filters/exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class catch ctx catch_local catch_pos =
let v =
match hx_exception_var with
| None ->
let v = alloc_var VGenerated "`" ctx.haxe_exception_type p in
let v = alloc_var VGenerated gen_local_prefix ctx.haxe_exception_type p in
hx_exception_var <- Some v;
v
| Some v -> v
Expand All @@ -228,7 +228,7 @@ class catch ctx catch_local catch_pos =
let v =
match unwrapped_var with
| None ->
let v = alloc_var VGenerated "`" t_dynamic p in
let v = alloc_var VGenerated gen_local_prefix t_dynamic p in
unwrapped_var <- Some v;
(* unwrapped_local <- Some e; *)
v
Expand Down Expand Up @@ -262,7 +262,7 @@ let catches_to_ifs ctx catches t p =
match catches with
| [] -> []
| ((first_v, first_body) :: _) as rest ->
let catch_var = alloc_var VGenerated "`" ctx.wildcard_catch_type first_v.v_pos in
let catch_var = alloc_var VGenerated gen_local_prefix ctx.wildcard_catch_type first_v.v_pos in
add_var_flag catch_var VCaught;
let catch_local = mk (TLocal catch_var) catch_var.v_type catch_var.v_pos in
let body =
Expand Down Expand Up @@ -396,7 +396,7 @@ let catches_as_value_exception ctx non_value_exception_catches value_exception_c
| Some (catch_var, _) ->
catch_var
| None ->
let catch_var = alloc_var VGenerated "`" ctx.value_exception_type first_v.v_pos in
let catch_var = alloc_var VGenerated gen_local_prefix ctx.value_exception_type first_v.v_pos in
add_var_flag catch_var VCaught;
catch_var
in
Expand Down
9 changes: 9 additions & 0 deletions src/filters/filterContext.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let with_timer detail_times label identifier f =
let label = match detail_times,identifier with
| 0,_ -> ["filters"]
| 1,_ -> "filters" :: label :: []
| _,Some identifier -> "filters" :: label :: identifier :: []
| _ -> ["filters"]
in
let timer = Timer.timer label in
Std.finally timer f ()
148 changes: 73 additions & 75 deletions src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,16 @@ let check_unification ctx e t =
end;
e

let rec fix_return_dynamic_from_void_function ctx return_is_void e =
let rec fix_return_dynamic_from_void_function return_is_void e =
match e.eexpr with
| TFunction fn ->
let is_void = ExtType.is_void (follow fn.tf_type) in
let body = fix_return_dynamic_from_void_function ctx is_void fn.tf_expr in
let body = fix_return_dynamic_from_void_function is_void fn.tf_expr in
{ e with eexpr = TFunction { fn with tf_expr = body } }
| TReturn (Some return_expr) when return_is_void && t_dynamic == follow return_expr.etype ->
let return_pos = { e.epos with pmax = return_expr.epos.pmin - 1 } in
let exprs = [
fix_return_dynamic_from_void_function ctx return_is_void return_expr;
fix_return_dynamic_from_void_function return_is_void return_expr;
{ e with eexpr = TReturn None; epos = return_pos };
] in
{ e with
Expand All @@ -338,7 +338,7 @@ let rec fix_return_dynamic_from_void_function ctx return_is_void e =
mk (TBlock exprs) e.etype e.epos
);
}
| _ -> Type.map_expr (fix_return_dynamic_from_void_function ctx return_is_void) e
| _ -> Type.map_expr (fix_return_dynamic_from_void_function return_is_void) e

let check_abstract_as_value e =
let rec loop e =
Expand Down Expand Up @@ -385,10 +385,10 @@ let remove_extern_fields com t = match t with
(* PASS 3 begin *)

(* Checks if a private class' path clashes with another path *)
let check_private_path ctx t = match t with
let check_private_path com t = match t with
| TClassDecl c when c.cl_private ->
let rpath = (fst c.cl_module.m_path,"_" ^ snd c.cl_module.m_path) in
if ctx.com.type_to_module#mem rpath then raise_typing_error ("This private class name will clash with " ^ s_type_path rpath) c.cl_pos;
if com.type_to_module#mem rpath then raise_typing_error ("This private class name will clash with " ^ s_type_path rpath) c.cl_pos;
| _ ->
()

Expand Down Expand Up @@ -653,10 +653,10 @@ let commit_features com t =
Common.add_feature com k;
) m.m_extra.m_features

let check_reserved_type_paths ctx t =
let check_reserved_type_paths com t =
let check path pos =
if List.mem path ctx.com.config.pf_reserved_type_paths then begin
warning ctx WReservedTypePath ("Type path " ^ (s_type_path path) ^ " is reserved on this target") pos
if List.mem path com.config.pf_reserved_type_paths then begin
com.warning WReservedTypePath [] ("Type path " ^ (s_type_path path) ^ " is reserved on this target") pos
end
in
match t with
Expand All @@ -671,7 +671,8 @@ let is_cached com t =
m.m_processed <> 0 && m.m_processed < com.compilation_step

let apply_filters_once ctx filters t =
if not (is_cached ctx.com t) then run_expression_filters None ctx filters t
let detail_times = (try int_of_string (Common.defined_value_safe ctx.com ~default:"0" Define.FilterTimes) with _ -> 0) in
if not (is_cached ctx.com t) then run_expression_filters ctx detail_times filters t

let iter_expressions fl mt =
match mt with
Expand All @@ -686,13 +687,6 @@ let iter_expressions fl mt =
| _ ->
()

let filter_timer detailed s =
Timer.timer (if detailed then "filters" :: s else ["filters"])

let timer_label detailed s =
if detailed then Some ("filters" :: s)
else None

module ForRemap = struct
let apply ctx e =
let rec loop e = match e.eexpr with
Expand All @@ -713,67 +707,69 @@ module ForRemap = struct
loop e
end

open FilterContext

let destruction tctx detail_times main locals =
let com = tctx.com in
let t = filter_timer detail_times ["type 2"] in
(* PASS 2: type filters pre-DCE *)
List.iter (fun t ->
remove_generic_base t;
remove_extern_fields com t;
(* check @:remove metadata before DCE so it is ignored there (issue #2923) *)
check_remove_metadata t;
) com.types;
t();
with_timer detail_times "type 2" None (fun () ->
(* PASS 2: type filters pre-DCE *)
List.iter (fun t ->
remove_generic_base t;
remove_extern_fields com t;
(* check @:remove metadata before DCE so it is ignored there (issue #2923) *)
check_remove_metadata t;
) com.types;
);
com.stage <- CDceStart;
let t = filter_timer detail_times ["dce"] in
(* DCE *)
let dce_mode = try Common.defined_value com Define.Dce with _ -> "no" in
let dce_mode = match dce_mode with
| "full" -> if Common.defined com Define.Interp then Dce.DceNo else DceFull
| "std" -> DceStd
| "no" -> DceNo
| _ -> failwith ("Unknown DCE mode " ^ dce_mode)
in
Dce.run com main dce_mode;
t();
with_timer detail_times "dce" None (fun () ->
(* DCE *)
let dce_mode = try Common.defined_value com Define.Dce with _ -> "no" in
let dce_mode = match dce_mode with
| "full" -> if Common.defined com Define.Interp then Dce.DceNo else DceFull
| "std" -> DceStd
| "no" -> DceNo
| _ -> failwith ("Unknown DCE mode " ^ dce_mode)
in
Dce.run com main dce_mode;
);
com.stage <- CDceDone;
(* PASS 3: type filters post-DCE *)
List.iter
(run_expression_filters
~ignore_processed_status:true
(timer_label detail_times [])
tctx
detail_times
(* This has to run after DCE, or otherwise its condition always holds. *)
["insert_save_stacks",Exceptions.insert_save_stacks tctx]
)
com.types;
let type_filters = [
Exceptions.patch_constructors tctx; (* TODO: I don't believe this should load_instance anything at this point... *)
check_private_path tctx;
check_private_path com;
apply_native_paths;
add_rtti com;
(match com.platform with | Java | Cs -> (fun _ -> ()) | _ -> (fun mt -> add_field_inits tctx.curclass.cl_path locals com mt));
(match com.platform with Hl -> (fun _ -> ()) | _ -> add_meta_field com);
check_void_field;
(match com.platform with | Cpp -> promote_first_interface_to_super | _ -> (fun _ -> ()));
commit_features com;
(if com.config.pf_reserved_type_paths <> [] then check_reserved_type_paths tctx else (fun _ -> ()));
(if com.config.pf_reserved_type_paths <> [] then check_reserved_type_paths com else (fun _ -> ()));
] in
let type_filters = match com.platform with
| Cs -> type_filters @ [ fun t -> InterfaceProps.run t ]
| _ -> type_filters
in
let t = filter_timer detail_times ["type 3"] in
List.iter (fun t ->
begin match t with
| TClassDecl c ->
tctx.curclass <- c
| _ ->
()
end;
List.iter (fun f -> f t) type_filters
) com.types;
t();
with_timer detail_times "type 3" None (fun () ->
List.iter (fun t ->
begin match t with
| TClassDecl c ->
tctx.curclass <- c
| _ ->
()
end;
List.iter (fun f -> f t) type_filters
) com.types;
);
com.callbacks#run com.callbacks#get_after_filters;
com.stage <- CFilteringDone

Expand Down Expand Up @@ -828,10 +824,10 @@ let update_cache_dependencies com t =
()

(* Saves a class state so it can be restored later, e.g. after DCE or native path rewrite *)
let save_class_state ctx t =
let save_class_state com t =
(* Update m_processed here. This means that nothing should add a dependency afterwards because
then the module is immediately considered uncached again *)
(t_infos t).mt_module.m_extra.m_processed <- ctx.com.compilation_step;
(t_infos t).mt_module.m_extra.m_processed <- com.compilation_step;
match t with
| TClassDecl c ->
let vars = ref [] in
Expand Down Expand Up @@ -915,8 +911,9 @@ let save_class_state ctx t =
a.a_meta <- List.filter (fun (m,_,_) -> m <> Meta.ValueUsed) a.a_meta
)

let run com tctx main =
let detail_times = Common.defined com DefineList.FilterTimes in
let run tctx main =
let com = tctx.com in
let detail_times = (try int_of_string (Common.defined_value_safe com ~default:"0" Define.FilterTimes) with _ -> 0) in
let new_types = List.filter (fun t ->
let cached = is_cached com t in
begin match t with
Expand Down Expand Up @@ -955,10 +952,10 @@ let run com tctx main =
"ForRemap",ForRemap.apply tctx;
"handle_abstract_casts",AbstractCast.handle_abstract_casts tctx;
] in
List.iter (run_expression_filters (timer_label detail_times ["expr 0"]) tctx filters) new_types;
List.iter (run_expression_filters tctx detail_times filters) new_types;
let filters = [
"local_statics",LocalStatic.run tctx;
"fix_return_dynamic_from_void_function",fix_return_dynamic_from_void_function tctx true;
"fix_return_dynamic_from_void_function",fix_return_dynamic_from_void_function true;
"check_local_vars_init",check_local_vars_init tctx;
"check_abstract_as_value",check_abstract_as_value;
"Tre",if defined com Define.AnalyzerOptimize then Tre.run tctx else (fun e -> e);
Expand All @@ -977,13 +974,13 @@ let run com tctx main =
filters
| _ -> filters
in
List.iter (run_expression_filters (timer_label detail_times ["expr 1"]) tctx filters) new_types;
List.iter (run_expression_filters tctx detail_times filters) new_types;
(* PASS 1.5: pre-analyzer type filters *)
let filters =
match com.platform with
| Cs ->
[
check_cs_events tctx.com;
check_cs_events com;
DefaultArguments.run com;
]
| Java ->
Expand All @@ -993,9 +990,9 @@ let run com tctx main =
| _ ->
[]
in
let t = filter_timer detail_times ["type 1"] in
List.iter (fun f -> List.iter f new_types) filters;
t();
with_timer detail_times "type 1" None (fun () ->
List.iter (fun f -> List.iter f new_types) filters;
);
com.stage <- CAnalyzerStart;
if com.platform <> Cross then Analyzer.Run.run_on_types com new_types;
com.stage <- CAnalyzerDone;
Expand All @@ -1005,22 +1002,23 @@ let run com tctx main =
"add_final_return",if com.config.pf_add_final_return then add_final_return else (fun e -> e);
"RenameVars",(match com.platform with
| Eval -> (fun e -> e)
| Java when defined com Jvm -> (fun e -> e)
| _ -> (fun e -> RenameVars.run tctx.curclass.cl_path locals e));
"mark_switch_break_loops",mark_switch_break_loops;
] in
List.iter (run_expression_filters (timer_label detail_times ["expr 2"]) tctx filters) new_types;
let t = filter_timer detail_times ["callbacks"] in
com.callbacks#run com.callbacks#get_before_save; (* macros onGenerate etc. *)
t();
List.iter (run_expression_filters tctx detail_times filters) new_types;
with_timer detail_times "callbacks" None (fun () ->
com.callbacks#run com.callbacks#get_before_save;
);
com.stage <- CSaveStart;
let t = filter_timer detail_times ["save state"] in
List.iter (fun mt ->
update_cache_dependencies com mt;
save_class_state tctx mt
) new_types;
t();
with_timer detail_times "save state" None (fun () ->
List.iter (fun mt ->
update_cache_dependencies com mt;
save_class_state com mt
) new_types;
);
com.stage <- CSaveDone;
let t = filter_timer detail_times ["callbacks"] in
com.callbacks#run com.callbacks#get_after_save; (* macros onGenerate etc. *)
t();
with_timer detail_times "callbacks" None (fun () ->
com.callbacks#run com.callbacks#get_after_save;
);
destruction tctx detail_times main locals
Loading

0 comments on commit 8aa9cc4

Please sign in to comment.