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

Apply type optimizations across local function calls #2100



None yet
2 participants
Copy link

commented Jan 21, 2019

The current type pass works on a per-function level and assumes that every argument and return value could be anything, which means they have to be type-checked before they're used. This is a bit inefficient as most functions always return something of the same type, and their argument types seldom differ.

This PR makes the type pass track the return and argument types of local functions, letting us eliminate many redundant type checks.

example1() ->
    %% We no longer check that this is a 2-tuple before extracting A & B
    {A, B} = returns_2_tuple(), 
    A + B.

returns_2_tuple() ->
    {a:a(), b:b()}.

-record(a, {n=0}).

example2() ->
    loop(lists:seq(1, 20), #a{}).

%% Record type is no longer checked on each iteration
loop([I | Is], #a{n=N}=St) ->
    loop(Is, St#a{n=N+I});
loop([], St) ->

jhogberg added some commits Jan 14, 2019

beam_ssa_type: Fix type subtraction in #b_switch{}
A switch is equivalent to a series of '=:=', so we have to subtract
each value individually from the type. Subtracting a join risks
removing too much type information, and managed to narrow "number"
into "float" in the attached test case.
beam_ssa_opt: Don't ruin arguments of bs_match/skip
If the match instruction was already marked as a skip, we'd ruin
its argument list.

@jhogberg jhogberg self-assigned this Jan 21, 2019

@jhogberg jhogberg requested a review from bjorng Jan 21, 2019

@jhogberg jhogberg changed the title Apply type optimizations across local functions Apply type optimizations across local function calls Jan 21, 2019


This comment has been minimized.

Copy link
Contributor Author

commented Jan 22, 2019

9daffa0 revealed some funny side-effects from propagating the 'none' type across functions. I'm working on fixing them but it might take some time.

@jhogberg jhogberg force-pushed the jhogberg:john/compiler/module-type-optimization branch 3 times, most recently from 9c8b886 to 95a4452 Jan 23, 2019


bjorng approved these changes Jan 24, 2019

jhogberg added some commits Jan 10, 2019

beam_ssa_opt: Add a scaffold for module-level optimizations
This serves as a base for the upcoming module-level type
optimization, but may come in handy for other passes like
beam_ssa_funs and beam_ssa_bsm that have their own ad-hoc
compiler: Introduce module-level type optimization
This commit lets the type optimization pass work across functions,
tracking return and argument types to eliminate redundant tests.

@jhogberg jhogberg force-pushed the jhogberg:john/compiler/module-type-optimization branch from 95a4452 to 294d66a Jan 24, 2019

@jhogberg jhogberg merged commit a0104bc into erlang:master Jan 24, 2019

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
license/cla Contributor License Agreement is signed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.