Skip to content

Commit 463e65a

Browse files
committed
Add profiling for cbn
Seems to be responsible for 23.0% of the cost of rewrite rules in mit-plv/fiat-crypto#1778, with a single call taking 168.429s.
1 parent a7f66c4 commit 463e65a

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/Rewriter/Language/PreCommon.v

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Module Export Pre.
2121
(** Change this with [Ltac2 Set reify_debug_level ::= 1.] to get
2222
more debugging. *)
2323
Ltac2 mutable reify_debug_level : int := 0.
24+
Ltac2 mutable reify_profile_cbn : bool := false.
2425

2526
Module ScrapedData.
2627
Local Set Primitive Projections.

src/Rewriter/Language/Reify.v

+19
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Module Compilers.
9696

9797
Module Reify.
9898
Ltac2 Notation debug_level := Pre.reify_debug_level.
99+
Ltac2 Notation should_profile_cbn := Pre.reify_profile_cbn.
99100

100101
Ltac2 mutable should_debug_enter_reify () := Int.le 3 debug_level.
101102
Ltac2 mutable should_debug_enter_reify_preprocess () := Int.le 3 debug_level.
@@ -125,6 +126,19 @@ Module Compilers.
125126
then tac ()
126127
else default.
127128

129+
Ltac2 debug_profile_if (descr : string) (pr_a : 'a -> message) (pr_b : 'b -> message) (cond : unit -> bool) (tac : 'a -> 'b) (val : 'a) :=
130+
if cond ()
131+
then (let c' := Control.time (Some descr) (fun () => tac val) in
132+
printf "Info: %s from %a to %a" descr (fun () => pr_a) val (fun () => pr_b) c';
133+
c')
134+
else tac val.
135+
136+
Ltac2 debug_profile_eval_cbn descr s c :=
137+
let descr := String.concat " " ["eval cbn"; descr] in
138+
debug_profile_if
139+
descr Message.of_constr Message.of_constr
140+
(fun () => should_profile_cbn) (Std.eval_cbn s) c.
141+
128142
Ltac2 debug_typing_failure (funname : string) (x : constr) (err : exn)
129143
:= debug_if should_debug_typing_failure (fun () => printf "Warning: %s: failure to typecheck %t: %a" funname x (fun () => Message.of_exn) err) ().
130144
Ltac2 debug_typing_failure_assume_well_typed (funname : string) (v : constr) (term : constr) (ctx_tys : binder list) (ty : constr)
@@ -285,6 +299,11 @@ Module Compilers.
285299
e.
286300
Ltac2 debug_Constr_check (funname : string) (descr : constr -> constr -> exn -> message) (var : constr) (cache : (unit -> binder) list) (var_ty_ctx : constr list) (e : constr)
287301
:= Constr.debug_assert_hole_free funname (debug_Constr_check_allow_holes funname descr var cache var_ty_ctx e).
302+
303+
Module Export Notations.
304+
Ltac2 Notation "debug" "(" descr(tactic) ")" "profile" "eval" "cbn" s(strategy) "in" c(tactic(6)) :=
305+
debug_profile_eval_cbn descr s c.
306+
End Notations.
288307
End Reify.
289308

290309
Module type.

src/Rewriter/Rewriter/Reify.v

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Module Compilers.
4949
Export IdentifiersBasicGenerate.Compilers.
5050
Import invert_expr.
5151
Export Rewriter.Compilers.
52+
Import Language.Reify.Compilers.Reify.Notations.
5253

5354
Module RewriteRules.
5455
Export Rewriter.Compilers.RewriteRules.
@@ -317,7 +318,7 @@ Module Compilers.
317318
(fun ()
318319
=> let s := strategy:([pattern.type.relax pattern.type.subst_default pattern.type.subst_default_relax pattern.type.unsubst_default_relax]) in
319320
let pat := Std.eval_cbv s pat in
320-
let pat := Std.eval_cbn s pat in
321+
let pat := Reify.debug_profile_eval_cbn "preadjust_pattern_type_variables" s pat in
321322
pat).
322323

323324
Ltac2 rec adjust_pattern_type_variables' (pat : constr) : constr :=
@@ -693,10 +694,10 @@ Module Compilers.
693694
Reify.should_debug_fine_grained Reify.should_debug_fine_grained (Some Message.of_constr)
694695
(fun ()
695696
=> let term := lazy_match! (eval pattern 'andb, '(andb true) in term) with
696-
| ?f _ _ => (eval cbn [andb] in constr:($f (fun x y => andb y x) (fun b => b)))
697+
| ?f _ _ => (debug ("remove_andb_true:1") profile eval cbn [andb] in constr:($f (fun x y => andb y x) (fun b => b)))
697698
end in
698699
let term := lazy_match! (eval pattern 'andb, '(andb true) in term) with
699-
| ?f _ _ => (eval cbn [andb] in constr:($f (fun x y => andb y x) (fun b => b)))
700+
| ?f _ _ => (debug ("remove_andb_true:2") profile eval cbn [andb] in constr:($f (fun x y => andb y x) (fun b => b)))
700701
end in
701702
term).
702703
Ltac2 rec adjust_if_negb (term : constr) : constr :=
@@ -781,7 +782,7 @@ Module Compilers.
781782
Reify.should_debug_fine_grained Reify.should_debug_fine_grained (Some Message.of_constr)
782783
(fun ()
783784
=> let base_interp_beq_head := head_reference base_interp_beq in
784-
let term := (eval cbn [Prod.prod_beq] in term) in
785+
let term := (debug ("clean_beq:Prod.prod_beq") profile eval cbn [Prod.prod_beq] in term) in
785786
let term := (eval cbv [ident.literal] in term) in
786787
let term := deep_substitute_beq base_interp_beq avoid only_eliminate_in_ctx term in
787788
let term := (eval cbv [base.interp_beq $base_interp_beq_head] in term) in
@@ -884,7 +885,7 @@ Module Compilers.
884885
let pident_type_of_list_arg_types_beq := head_reference pident_type_of_list_arg_types_beq in
885886
let pident_arg_types_of_typed_ident := head_reference pident_arg_types_of_typed_ident in
886887
(eval cbv [expr_to_pattern_and_replacement_unfolded_split $pident_arg_types $pident_of_typed_ident $pident_type_of_list_arg_types_beq $pident_arg_types_of_typed_ident (*reflect_ident_iota*)] in res) in
887-
let res := (eval cbn [fst snd andb pattern.base.relax pattern.base.subst_default pattern.base.subst_default_relax] in res) in
888+
let res := (debug ("reify_to_pattern_and_replacement_in_context:1") profile eval cbn [fst snd andb pattern.base.relax pattern.base.subst_default pattern.base.subst_default_relax] in res) in
888889
let res := change_pattern_base_subst_default_relax res in
889890
let (p, res) := lazy_match! res with
890891
| existT _ ?p ?res => (p, res)
@@ -913,7 +914,7 @@ Module Compilers.
913914
res)))) in
914915
let res := debug_Constr_check res in
915916
let res := (eval cbv [UnderLets.map UnderLets.flat_map reify_expr_beta_iota reflect_expr_beta_iota reify_to_UnderLets] in res) in
916-
let res := (eval cbn [reify reflect UnderLets.of_expr UnderLets.to_expr UnderLets.splice value' Base_value invert_Literal invert_ident_Literal splice_under_lets_with_value] in res) in
917+
let res := (debug ("reify_to_pattern_and_replacement_in_context:2") profile eval cbn [reify reflect UnderLets.of_expr UnderLets.to_expr UnderLets.splice value' Base_value invert_Literal invert_ident_Literal splice_under_lets_with_value] in res) in
917918
let res := strip_invalid_or_fail res in
918919
(* cbv here not strictly needed *)
919920
let res := (eval cbv [partial_lam_unif_rewrite_ruleTP_gen_unfolded]
@@ -922,7 +923,7 @@ Module Compilers.
922923
$p
923924
($cpartial_lam_unif_rewrite_ruleTP_gen _ $p $res))) in
924925
(* not strictly needed *)
925-
let res := (eval cbn [pattern.base.subst_default pattern.base.lookup_default PositiveMap.find type.interp base.interp $base_interp_head] in res) in
926+
let res := (debug ("reify_to_pattern_and_replacement_in_context:3") profile eval cbn [pattern.base.subst_default pattern.base.lookup_default PositiveMap.find type.interp base.interp $base_interp_head] in res) in
926927
let res := (eval cbv [projT1 projT2]
927928
in constr:(existT
928929
(@rewrite_ruleTP $base $ident $var $pident $pident_arg_types)

0 commit comments

Comments
 (0)