Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: Add zip generators for comprehensions #8926

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions erts/doc/guides/absform.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ An expression E is one of the following:
A qualifier Q is one of the following:

- If Q is a filter `E`, where `E` is an expression, then Rep(Q) = `Rep(E)`.
- If Q is a zip generator `Q_1 && ...&& Q_k]`, where each `Q_i` is
a non-zip generator, then Rep(E) = `{zip,ANNO,[Rep(Q_1), ..., Rep(Q_k)]}`.
For Rep(Q), see below.
- If Q is a list generator `P <- E`, where `P` is a pattern and `E` is an
expression, then Rep(Q) = `{generate,ANNO,Rep(P),Rep(E)}`.
- If Q is a bitstring generator `P <= E`, where `P` is a pattern and `E` is an
Expand Down
Binary file modified erts/preloaded/ebin/erts_internal.beam
Binary file not shown.
4 changes: 3 additions & 1 deletion erts/preloaded/src/erts_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ is_map_iter(Iter) ->
NI :: term().

mc_refill([Path | Map]) ->
erts_internal:map_next(Path, Map, iterator).
erts_internal:map_next(Path, Map, iterator);
mc_refill(NoRefillNeeded) when is_tuple(NoRefillNeeded) ->
NoRefillNeeded.

-spec erts_internal:flush_monitor_messages(Ref, Multi, Res) -> term() when
Ref :: reference(),
Expand Down
6 changes: 6 additions & 0 deletions lib/compiler/src/sys_coverage.erl
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ munge_qs([{m_generate,Anno,Pattern,Expr}|Qs], Vars0, MQs) ->
A = element(2, Expr),
{MExpr, Vars1} = munge_expr(Expr, Vars0),
munge_qs1(Qs, A, {m_generate,Anno,Pattern,MExpr}, Vars0, Vars1, MQs);
munge_qs([{zip,Anno,Gs0}|Qs], Vars0, MQs) ->
{Gs1, Vars1} = munge_qualifiers(Gs0, Vars0),
%% Get rid of dummy filters inserted by munge_qualifiers/2 --
%% they are not allowed in the zip construct.
Gs = [G || G <- Gs1, element(1, G) =/= block],
munge_qs1(Qs, Anno, {zip,Anno,Gs}, Vars0, Vars1, MQs);
munge_qs([Expr|Qs], Vars0, MQs) ->
A = element(2, Expr),
{MungedExpr, Vars1} = munge_expr(Expr, Vars0),
Expand Down
199 changes: 184 additions & 15 deletions lib/compiler/src/v3_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@

-export([module/2,format_error/1]).

-import(lists, [any/2,reverse/1,reverse/2,map/2,member/2,foldl/3,foldr/3,mapfoldl/3,
splitwith/2,keydelete/3,keyfind/3,keymember/3,sort/1,droplast/1,last/1,
duplicate/2]).
-import(lists, [all/2,any/2,append/1,droplast/1,duplicate/2,
foldl/3,foldr/3,
keydelete/3,keyfind/3,keymember/3,
last/1,map/2,member/2,mapfoldl/3,
reverse/1,reverse/2,
splitwith/2,sort/1, zip/2]).
-import(ordsets, [add_element/2,del_element/2,is_element/2,
union/1,union/2,intersection/2,subtract/2]).
-import(cerl, [ann_c_cons/3,ann_c_tuple/2,c_tuple/1,
Expand Down Expand Up @@ -123,6 +126,9 @@
-record(igen, {anno=#a{},acc_pat,acc_guard,
skip_pat,tail,tail_pat,arg,
refill={nomatch,ignore}}).
-record(izip, {anno=#a{},acc_pats,acc_guard,
skip_pats,tails,tail_pats,pres,args,
refill_pats,refill_as}).
-record(isimple, {anno=#a{},term :: cerl:cerl()}).

-type iapply() :: #iapply{}.
Expand All @@ -143,13 +149,14 @@
-type itry() :: #itry{}.
-type ifilter() :: #ifilter{}.
-type igen() :: #igen{}.
-type izip() :: #izip{}.
-type isimple() :: #isimple{}.

-type i() :: iapply() | ibinary() | icall() | icase() | icatch()
| iclause() | ifun() | iletrec() | imatch() | imap()
| iprimop() | iprotect() | ireceive1() | ireceive2()
| iset() | itry() | ifilter()
| igen() | isimple().
| igen() | izip() | isimple().

-type warning() :: {file:filename(), [{integer(), module(), term()}]}.

Expand Down Expand Up @@ -1601,7 +1608,20 @@ fun_tq(Cs0, L, St0, NameInfo) ->
%% lc_tq(Line, Exp, [Qualifier], Mc, State) -> {LetRec,[PreExp],State}.
%% This TQ from Simon PJ pp 127-138.

lc_tq(Line, E, [#igen{anno=#a{anno=GA}=GAnno,
lc_tq(Line, E, [#igen{}|_T] = Qs, Mc, St) ->
lc_tq1(Line, E, Qs, Mc, St);
lc_tq(Line, E, [#izip{}=Zip|Qs], Mc, St) ->
zip_tq(Line, E, Zip, Mc, St, Qs);
lc_tq(Line, E, [#ifilter{}=Filter|Qs], Mc, St) ->
filter_tq(Line, E, Filter, Mc, St, Qs, fun lc_tq/5);
lc_tq(Line, E0, [], Mc0, St0) ->
{H1,Hps,St1} = safe(E0, St0),
{T1,Tps,St} = force_safe(Mc0, St1),
Anno = lineno_anno(Line, St),
E = ann_c_cons(Anno, H1, T1),
{set_anno(E, [compiler_generated|Anno]),Hps ++ Tps,St}.

lc_tq1(Line, E, [#igen{anno=#a{anno=GA}=GAnno,
acc_pat=AccPat,acc_guard=AccGuard,
skip_pat=SkipPat,tail=Tail,tail_pat=TailPat,
refill={RefillPat,RefillAction},
Expand All @@ -1623,15 +1643,54 @@ lc_tq(Line, E, [#igen{anno=#a{anno=GA}=GAnno,
Fun = #ifun{anno=GAnno,id=[],vars=[Var],clauses=Cs,fc=Fc},
{#iletrec{anno=GAnno#a{anno=[list_comprehension|GA]},defs=[{{Name,1},Fun}],
body=Pre ++ [#iapply{anno=GAnno,op=F,args=[Arg]}]},
[],St3};
lc_tq(Line, E, [#ifilter{}=Filter|Qs], Mc, St) ->
filter_tq(Line, E, Filter, Mc, St, Qs, fun lc_tq/5);
lc_tq(Line, E0, [], Mc0, St0) ->
{H1,Hps,St1} = safe(E0, St0),
{T1,Tps,St} = force_safe(Mc0, St1),
Anno = lineno_anno(Line, St),
E = ann_c_cons(Anno, H1, T1),
{set_anno(E, [compiler_generated|Anno]),Hps ++ Tps,St}.
[],St3}.

%% zip_tq(Line, Exp, [Qualifier], Mc, State, TqFun) -> {LetRec,[PreExp],State}.

zip_tq(Line, E, #izip{anno=#a{anno=GA}=GAnno,
acc_pats=AccPats,acc_guard=AccGuard,
tails=TailVars,tail_pats=TailPats,
skip_pats=SkipPats,refill_pats=RefillPats0,
refill_as=RefillAs,pres=Pres,args=Args}, Mc, St0, Qs) ->
{Name,St1} = new_fun_name("zlc", St0),
LA = lineno_anno(Line, St1),
NumGenerators = length(AccPats),

%% Generate new vars for each generator, 1 for the regular call, and 1 for
%% the bad generator case.
{CallVars,St2} = new_vars(NumGenerators, St1),
{FcVars, St3} = new_vars(NumGenerators, St2),

%% Generate the name for the letrec.
F = #c_var{anno=LA,name={Name,NumGenerators}},

%% Generate the clauses for the letrec.
Nc = #iapply{anno=GAnno,op=F,args=TailVars},
{Lc,Lps,St4} = lc_tq(Line, E, Qs, Nc, St3),

AccClause = make_clause(LA, AccPats, AccGuard, Lps++[Lc]),
SkipClause = make_clause([skip_clause,compiler_generated|LA], SkipPats, [], [Nc]),
TailClause = make_clause(LA, TailPats, [], [Mc]),
RefillClause =
case all(fun(X) -> X =:= nomatch end, RefillPats0) of
true ->
%% There are no map generators.
nomatch;
false ->
RefillPats = make_ignored(RefillPats0, TailVars),
RefillBody = [C || C <- RefillAs, C =/= ignore],
make_clause(LA, RefillPats, [], RefillBody++[Nc])
end,

Cs0 = [AccClause, SkipClause, TailClause, RefillClause],
Cs = [C || C <- Cs0, C =/= nomatch],

Fc = bad_generators(FcVars, hd(Args), lc),
Fun = #ifun{anno=GAnno,id=[],vars=CallVars,clauses=Cs,fc=Fc},
{#iletrec{anno=GAnno#a{anno=[list_comprehension|GA]},
defs=[{{Name,NumGenerators},Fun}],
body=append(Pres) ++
[#iapply{anno=GAnno,op=F,args=Args}]},[],St4}.

%% bc_tq(Line, Exp, [Qualifier], More, State) -> {LetRec,[PreExp],State}.
%% This TQ from Gustafsson ERLANG'05.
Expand Down Expand Up @@ -1686,6 +1745,8 @@ bc_tq1(Line, E, [#igen{anno=GAnno,
defs=[{{Name,2},Fun}],
body=Pre ++ [#iapply{anno=LAnno,op=F,args=[Arg,Mc]}]},
[],St5};
bc_tq1(Line, E, [#izip{}=Zip|Qs], Mc, St) ->
bzip_tq1(Line, E, Zip, Mc, St, Qs);
bc_tq1(Line, E, [#ifilter{}=Filter|Qs], Mc, St) ->
filter_tq(Line, E, Filter, Mc, St, Qs, fun bc_tq1/5);
bc_tq1(_, {bin,Bl,Elements}, [], AccVar, St0) ->
Expand Down Expand Up @@ -1722,6 +1783,56 @@ bc_tq_build(Line, Pre0, #c_var{name=AccVar}, Elements0, St0) ->
Anno = Anno0#a{anno=[compiler_generated,single_use|A]},
{set_anno(E, Anno),Pre0++Pre,St}.

bzip_tq1(Line, E, #izip{anno=GAnno,
acc_pats=AccPats,acc_guard=AccGuard,
tails=TailVars,tail_pats=TailPats,
skip_pats=SkipPats,refill_pats=RefillPats0,
refill_as=RefillAs,pres=Pres,args=Args}, Mc, St0, Qs) ->
{Name,St1} = new_fun_name("bzip", St0),
LA = lineno_anno(Line, St1),
LAnno = #a{anno=LA},
Arity = length(AccPats) + 1,

%% Generate new vars for each generator, 1 for the regular call, and 1 for
%% the bad generator case. last(CallVars) is used as the accumulator var
%% when constructing the new binary.
{CallVars, St2} = new_vars(LA, Arity, St1),
{FcVars, St3} = new_vars(LA, Arity, St2),

%% Generate the name for the letrec.
F = #c_var{anno=LA,name={Name,Arity}},

%% Generate the clauses for the letrec.
BinAccVar = last(CallVars),
Nc = #iapply{anno=GAnno,op=F,args=TailVars++[BinAccVar]},
{Bc,Bps,St4} = bc_tq1(Line, E, Qs, BinAccVar, St3),
Body = Bps++[#iset{var=hd(CallVars), arg=Bc}, Nc],
AccClause = make_clause(LA, AccPats++[Mc], AccGuard, Body),
TailClause = make_clause(LA, TailPats++[Mc], [], [Mc]),
SkipClause = make_clause([skip_clause,compiler_generated|LA], SkipPats++[Mc], [], [Nc]),
RefillClause =
case all(fun(X) -> X =:= nomatch end, RefillPats0) of
true ->
%% There are no map generators.
nomatch;
false ->
RefillPats = make_ignored(RefillPats0, TailVars),
RefillBody = [C || C <- RefillAs, C =/= ignore],
make_clause(LA, RefillPats++[Mc], [], RefillBody++[Nc])
end,

Cs0 = [AccClause, SkipClause, TailClause, RefillClause],
Cs = [C || C <- Cs0, C =/= nomatch],

Fc = bad_generators(FcVars, hd(Args), bc),
Fun = #ifun{anno=GAnno,id=[],vars=CallVars,clauses=Cs,fc=Fc},
%% Inlining would disable the size calculation optimization for
%% bs_init_writable.
{#iletrec{anno=LAnno#a{anno=[list_comprehension,no_inline|LA]},
defs=[{{Name,Arity},Fun}],
body=append(Pres) ++
[#iapply{anno=LAnno,op=F,args=Args++[Mc]}]},[],St4}.

mc_tq(Line, {map_field_assoc,Lf,K,V}, Qs, Mc, St0) ->
E = {tuple,Lf,[K,V]},
{Lc,Pre0,St1} = lc_tq(Line, E, Qs, Mc, St0),
Expand All @@ -1732,6 +1843,15 @@ mc_tq(Line, {map_field_assoc,Lf,K,V}, Qs, Mc, St0) ->
args=[LcVar]},
{Call,Pre,St2}.

make_ignored(Ps, Vs) ->
[case P of
nomatch -> V;
_ -> P
end || {P, V} <- zip(Ps, Vs)].

make_clause(Anno, [Pat|PatExtra], Guard, Body) ->
make_clause(Anno, Pat, PatExtra, Guard, Body).

make_clause(_Anno, nomatch, _PatExtra, _Guard, _Body) ->
nomatch;
make_clause(Anno, Pat, PatExtra, Guard, Body) ->
Expand Down Expand Up @@ -1782,6 +1902,23 @@ filter_tq(Line, E, #ifilter{anno=#a{anno=LA}=LAnno,arg=Guard},
preprocess_quals(Line, Qs, St) ->
preprocess_quals(Line, Qs, St, []).

preprocess_quals(Line, [{zip,Anno,Gens}|Qs], St, Acc) ->
LAnno = #a{anno=lineno_anno(Anno, St)},
{Gens1, St1} = preprocess_quals(Line, Gens, St, []),
{AccPats, TailVars, TailPats, SkipPats, RefillPats, RefillAs, Pres, Args}
= preprocess_zip_1(Gens1),
[#igen{acc_guard=AccGuard}|_] = Gens1,
Zip = #izip{anno=LAnno,
acc_pats=AccPats,
acc_guard=AccGuard,
skip_pats=SkipPats,
tails=TailVars,
tail_pats=TailPats,
pres=Pres,
args=Args,
refill_pats=RefillPats,
refill_as=RefillAs},
preprocess_quals(Line, Qs, St1, [Zip|Acc]);
preprocess_quals(Line, [Q|Qs0], St0, Acc) ->
case is_generator(Q) of
true ->
Expand Down Expand Up @@ -1810,6 +1947,25 @@ preprocess_quals(Line, [Q|Qs0], St0, Acc) ->
preprocess_quals(_, [], St, Acc) ->
{reverse(Acc),St}.

preprocess_zip_1([#igen{arg={Pre,Arg},
tail=Tail,
acc_pat=AccPat,
tail_pat=TailPat,
refill={RefillPat, RefillArg},
skip_pat=SkipPat} | Rest]) ->
{AccPats, TailVars, TailPats, SkipPats, RefillPats, RefillAs, Pres, Args}
= preprocess_zip_1(Rest),
{[AccPat | AccPats],
[Tail | TailVars],
[TailPat | TailPats],
[SkipPat | SkipPats],
[RefillPat | RefillPats],
[RefillArg | RefillAs],
[Pre | Pres],
[Arg | Args]};
preprocess_zip_1([]) ->
{[], [], [], [], [], [], [], []}.

is_generator({generate,_,_,_}) -> true;
is_generator({b_generate,_,_,_}) -> true;
is_generator({m_generate,_,_,_}) -> true;
Expand Down Expand Up @@ -2406,8 +2562,21 @@ new_vars_1(N, Anno, St0, Vs) when N > 0 ->
new_vars_1(0, _, St, Vs) -> {Vs,St}.

bad_generator(Ps, Generator, Arg) ->
L = [#c_literal{val=bad_generator}, Generator],
bad_generator_common(L, Ps, Arg).

bad_generators(Ps, Arg, bc) ->
T1 = #c_tuple{es=droplast(Ps)},
L = [#c_literal{val=bad_generators}, T1],
bad_generator_common(L, Ps, Arg);
bad_generators(Ps, Arg, lc) ->
T = #c_tuple{es=Ps},
L = [#c_literal{val=bad_generators}, T],
bad_generator_common(L, Ps, Arg).

bad_generator_common(L, Ps, Arg) ->
Anno = get_anno(Arg),
Tuple = ann_c_tuple(Anno, [#c_literal{val=bad_generator},Generator]),
Tuple = ann_c_tuple(Anno, L),
Call = #icall{anno=#a{anno=Anno}, %Must have an #a{}
module=#c_literal{anno=Anno,val=erlang},
name=#c_literal{anno=Anno,val=error},
Expand Down
4 changes: 3 additions & 1 deletion lib/compiler/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ MODULES= \
trycatch_SUITE \
warnings_SUITE \
z_SUITE \
zlc_SUITE \
test_lib

NO_BOOL_OPT= \
Expand Down Expand Up @@ -87,7 +88,8 @@ NO_OPT= \
overridden_bif \
receive \
record \
trycatch
trycatch \
zlc

INLINE= \
andor \
Expand Down
Loading
Loading