Add options to enable/disable certain optimizations (#409)

* Add flags to enable/disable specific optimizations

* Fix typos

* Enable/disable scode optimization

* Update CHANGELOG.md

* Remove optimize_all option
This commit is contained in:
Gaith Hallak 2022-08-30 10:14:46 +03:00 committed by GitHub
parent ad4c341a4a
commit c1c169273c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 15 deletions

View File

@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added ### Added
- Options to enable/disable certain optimizations.
### Changed ### Changed
### Removed ### Removed
### Fixed ### Fixed

View File

@ -184,7 +184,7 @@ ast_to_fcode(Code, Options) ->
optimize(FCode1, Options) -> optimize(FCode1, Options) ->
Verbose = lists:member(pp_fcode, Options), Verbose = lists:member(pp_fcode, Options),
[io:format("-- Before lambda lifting --\n~s\n\n", [format_fcode(FCode1)]) || Verbose], [io:format("-- Before lambda lifting --\n~s\n\n", [format_fcode(FCode1)]) || Verbose],
FCode2 = optimize_fcode(FCode1), FCode2 = optimize_fcode(FCode1, Options),
[ io:format("-- After optimization --\n~s\n\n", [format_fcode(FCode2)]) || Verbose, FCode2 /= FCode1 ], [ io:format("-- After optimization --\n~s\n\n", [format_fcode(FCode2)]) || Verbose, FCode2 /= FCode1 ],
FCode3 = lambda_lift(FCode2), FCode3 = lambda_lift(FCode2),
[ io:format("-- After lambda lifting --\n~s\n\n", [format_fcode(FCode3)]) || Verbose, FCode3 /= FCode2 ], [ io:format("-- After lambda lifting --\n~s\n\n", [format_fcode(FCode3)]) || Verbose, FCode3 /= FCode2 ],
@ -1287,20 +1287,28 @@ lambda_lift_exprs(Layout, As) -> [lambda_lift_expr(Layout, A) || A <- As].
%% - Constant propagation %% - Constant propagation
%% - Inlining %% - Inlining
-spec optimize_fcode(fcode()) -> fcode(). -spec optimize_fcode(fcode(), [option()]) -> fcode().
optimize_fcode(Code = #{ functions := Funs }) -> optimize_fcode(Code = #{ functions := Funs }, Options) ->
Code1 = Code#{ functions := maps:map(fun(Name, Def) -> optimize_fun(Code, Name, Def) end, Funs) }, Code1 = Code#{ functions := maps:map(fun(Name, Def) -> optimize_fun(Code, Name, Def, Options) end, Funs) },
eliminate_dead_code(Code1). eliminate_dead_code(Code1).
-spec optimize_fun(fcode(), fun_name(), fun_def()) -> fun_def(). -spec optimize_fun(fcode(), fun_name(), fun_def(), [option()]) -> fun_def().
optimize_fun(Fcode, Fun, Def = #{ body := Body }) -> optimize_fun(Fcode, Fun, Def = #{ body := Body0 }, Options) ->
%% io:format("Optimizing ~p =\n~s\n", [_Fun, prettypr:format(pp_fexpr(_Body))]), Inliner = proplists:get_value(optimize_inliner, Options, true),
Def#{ body := drop_unused_lets( InlineLocalFunctions = proplists:get_value(optimize_inline_local_functions, Options, true),
simplifier( BindSubexpressions = proplists:get_value(optimize_bind_subexpressions, Options, true),
let_floating( LetFloating = proplists:get_value(optimize_let_floating, Options, true),
bind_subexpressions( Simplifier = proplists:get_value(optimize_simplifier, Options, true),
inline_local_functions( DropUnusedLets = proplists:get_value(optimize_drop_unused_lets, Options, true),
inliner(Fcode, Fun, Body)))))) }.
Body1 = if Inliner -> inliner (Fcode, Fun, Body0); true -> Body0 end,
Body2 = if InlineLocalFunctions -> inline_local_functions(Body1); true -> Body1 end,
Body3 = if BindSubexpressions -> bind_subexpressions (Body2); true -> Body2 end,
Body4 = if LetFloating -> let_floating (Body3); true -> Body3 end,
Body5 = if Simplifier -> simplifier (Body4); true -> Body4 end,
Body6 = if DropUnusedLets -> drop_unused_lets (Body5); true -> Body5 end,
Def#{ body := Body6 }.
%% --- Inlining --- %% --- Inlining ---

View File

@ -708,8 +708,13 @@ tuple(N) -> aeb_fate_ops:tuple(?a, N).
%% Optimize %% Optimize
optimize_scode(Funs, Options) -> optimize_scode(Funs, Options) ->
maps:map(fun(Name, Def) -> optimize_fun(Funs, Name, Def, Options) end, case proplists:get_value(optimize_scode, Options, true) of
Funs). true ->
maps:map(fun(Name, Def) -> optimize_fun(Funs, Name, Def, Options) end,
Funs);
false ->
Funs
end.
flatten(missing) -> missing; flatten(missing) -> missing;
flatten(Code) -> lists:map(fun flatten_s/1, lists:flatten(Code)). flatten(Code) -> lists:map(fun flatten_s/1, lists:flatten(Code)).