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

inference×effects: improve the const-prop' heuristics #46471

Open
wants to merge 2 commits into
base: avi/inlining_cost
Choose a base branch
from

Conversation

aviatesk
Copy link
Member

This commit improves the heuristics to judge const-prop' profitability
with new effect property :const_prop_profitable_args. This is supposed
to supplement our primary const-prop' heuristic based on inlining cost
and is supposed to be a general fix for type stabilities issues discussed
at e.g. #45952 and #46430 (and eliminating the need for manual
@constprop :aggressive clutters in such situations).

The new effect property :const_prop_profitable_args tracks call
arguments that can be considered to shape up generated code if
their constant information is available. Currently this commit
exploits the following const-prop' profitabilities:

  • Val(x)-profitability: as Val generally encodes constant information
    into the type domain, it is generally profitable to constant prop' x
    if the constructed Val(x) is used later (e.g. for dispatch).
    This basically tries to exploit const-prop' profitability in the
    following kind of case:
    kernel(::Val{1}, args...) = ...
    kernel(::Val{2}, args...) = ...
    
    function profitable1(x::Int, args...)
        kernel(Val(x), args...)
    end
    This allows the compiler to perform const-prop' for case like Const prop failure leads to inference failure in [x;;] #45952
    even if the primary heuristics based on inlining cost gets confused.
  • branching-profitability: constant branch condition is generally very
    profitable as it can shape up generated code as well as narrow down
    the return type inference by cutting off the dead branch.
    function profitable2(raise::Bool, args...)
        v = op(args...)
        if v === nothing && raise
            return nothing
        end
        return v
    end

Currently this commit passes all the test cases and also actually
improves target type stabilities, but doesn't work very ideally as it
seems to be a bit too aggressive (this commit right now strictly
increases the chances of const-propagation). I'd like to further tweak
this heuristic to keep the latency in general cases.


fixes #45952, closes #46430.

/cc @Keno Do you have any opinion on this?

@aviatesk
Copy link
Member Author

@nanosoldier runbenchmarks(!"scalar", vs=":master")

@aviatesk aviatesk added compiler:inference Type inference compiler:effects effect analysis and removed compiler:effects effect analysis labels Aug 24, 2022
@Keno
Copy link
Member

Keno commented Aug 24, 2022

I'm not particularly opposed to it, but it does stretch the definition of effects a fair bit. This is more like reverse dataflow information, similar to escape analysis. That said, I don't think there's any big problem with this either.

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@antoine-levitt
Copy link
Contributor

Very cool! If I understand correctly it should also solve #44330

@oscardssmith
Copy link
Member

How will this work for functions with more than 32 inputs?

@aviatesk
Copy link
Member Author

Very cool! If I understand correctly it should also solve #44330

Good catch! I'd add a new test case based on the issue.

How will this work for functions with more than 32 inputs?

No, currently this only works for <=8 arguments. We may want to extend it though.

@aviatesk aviatesk force-pushed the avi/effects_constprop branch from 48b4957 to 6059860 Compare August 25, 2022 04:00
@aviatesk
Copy link
Member Author

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@aviatesk aviatesk force-pushed the avi/effects_constprop branch from 6059860 to db2a1e8 Compare August 30, 2022 11:57
@aviatesk aviatesk force-pushed the avi/effects_constprop branch 2 times, most recently from f8ebc9f to ab6a268 Compare October 3, 2022 03:14
@aviatesk
Copy link
Member Author

aviatesk commented Oct 3, 2022

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@aviatesk
Copy link
Member Author

@nanosoldier runbenchmarks(!"scalar", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@aviatesk aviatesk force-pushed the avi/effects_constprop branch from 36b9be3 to 5259e28 Compare April 1, 2023 16:06
@aviatesk
Copy link
Member Author

aviatesk commented Apr 1, 2023

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@aviatesk aviatesk force-pushed the avi/effects_constprop branch from 5259e28 to 2939699 Compare April 4, 2023 15:53
@aviatesk aviatesk changed the base branch from master to avi/inlining_cost April 4, 2023 15:54
@aviatesk aviatesk force-pushed the avi/effects_constprop branch from 2939699 to c34e6e0 Compare April 4, 2023 15:57
@aviatesk aviatesk force-pushed the avi/effects_constprop branch 3 times, most recently from 9c09477 to 0a59892 Compare April 4, 2023 16:27
@aviatesk
Copy link
Member Author

aviatesk commented Apr 4, 2023

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

aviatesk added 2 commits May 11, 2023 18:02
This commit improves the heuristics to judge const-prop' profitability
with new effect property `:const_prop_profitable_args`. This is supposed
to supplement our primary const-prop' heuristic based on inlining cost
and is supposed to be a general fix for type stabilities issues discussed
at e.g. #45952 and #46430 (and eliminating the need for manual
`@constprop :aggressive` clutters in such situations).

The new effect property `:const_prop_profitable_args` tracks call
arguments that can be considered to shape up generated code if
their constant information is available. Currently this commit
exploits the following const-prop' profitabilities:
- `Val(x)`-profitability: as `Val` generally encodes constant information
  into the type domain, it is generally profitable to constant prop' `x`
  if the constructed `Val(x)` is used later (e.g. for dispatch).
  This basically tries to exploit const-prop' profitability in the
  following kind of case:
  ```julia
  kernel(::Val{1}, args...) = ...
  kernel(::Val{2}, args...) = ...

  function profitable1(x::Int, args...)
      kernel(Val(x), args...)
  end
  ```
  This allows the compiler to perform const-prop' for case like #45952
  even if the primary heuristics based on inlining cost gets confused.
- branching-profitability: constant branch condition is generally very
  profitable as it can shape up generated code as well as narrow down
  the return type inference by cutting off the dead branch.
  ```julia
  function profitable2(raise::Bool, args...)
      v = op(args...)
      if v === nothing && raise
          return nothing
      end
      return v
  end
  ```

Currently this commit passes all the test cases and also actually
improves target type stabilities, but doesn't work very ideally as it
seems to be a bit too aggressive (this commit right now strictly
increases the chances of const-propagation). I'd like to further tweak
this heuristic to keep the latency in general cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Const prop failure leads to inference failure in [x;;]
5 participants