From 59845dec545bed4a5680610b28fed8cb95b562cb Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Fri, 5 Apr 2019 10:53:21 +0200 Subject: [PATCH] Limit the number of iterations for the optimization loop Should finish in one iteration, but we shouldn't loop if there are bugs or corner cases where it doesn't. --- src/aeso_fcode_to_fate.erl | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index 63adc2a..f0c1487 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -187,22 +187,30 @@ flatten(Code) -> lists:map(fun flatten_s/1, lists:flatten(Code)). flatten_s({ifte, Then, Else}) -> {ifte, flatten(Then), flatten(Else)}; flatten_s(I) -> I. +-define(MAX_SIMPL_ITERATIONS, 10). + optimize_fun(_Funs, Name, {{Args, Res}, Code}, Options) -> Code0 = flatten(Code), debug(opt, Options, "Optimizing ~s\n", [Name]), - Code1 = simpl_loop(Code0, Options), + Code1 = simpl_loop(0, Code0, Options), Code2 = desugar(Code1), {{Args, Res}, Code2}. -simpl_loop(Code, Options) -> +simpl_loop(N, Code, Options) when N >= ?MAX_SIMPL_ITERATIONS -> + debug(opt, Options, " No simpl_loop fixed_point after ~p iterations.\n\n", [N]), + Code; +simpl_loop(N, Code, Options) -> ACode = annotate_code(Code), - debug(opt, Options, " annotated:\n~s\n", [pp_ann(" ", ACode)]), + [ debug(opt, Options, " annotated:\n~s\n", [pp_ann(" ", ACode)]) || N == 0 ], Code1 = simplify(ACode, Options), [ debug(opt, Options, " optimized:\n~s\n", [pp_ann(" ", Code1)]) || Code1 /= ACode ], Code2 = unannotate(Code1), case Code == Code2 of - true -> Code2; - false -> simpl_loop(Code2, Options) + true -> + debug(opt, Options, " Reached simpl_loop fixed point after ~p iteration~s.\n\n", + [N, if N /= 1 -> "s"; true -> "" end]), + Code2; + false -> simpl_loop(N + 1, Code2, Options) end. pp_ann(Ind, [{ifte, Then, Else} | Code]) ->