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

SROA: generalize unswitchtupleunion optimization #50502

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

aviatesk
Copy link
Member

This commit improves SROA pass by extending the unswitchtupleunion optimization to handle the general parametric types, e.g.:

julia> struct A{T}
           x::T
       end;

julia> function foo(a1, a2, c)
           t = c ? A(a1) : A(a2)
           return getfield(t, :x)
       end;

julia> only(Base.code_ircode(foo, (Int,Float64,Bool); optimize_until="SROA"))

Before

2 1 ─      goto #3 if not _4                                          │
  2 ─ %2 = %new(A{Int64}, _2)::A{Int64}                               │╻ A
  └──      goto #4                                                    │
  3 ─ %4 = %new(A{Float64}, _3)::A{Float64}                           │╻ A
  4 ┄ %5 = φ (#2 => %2, #3 => %4)::Union{A{Float64}, A{Int64}}        │
3 │   %6 = Main.getfield(%5, :x)::Union{Float64, Int64}               │
  └──      return %6                                                  │
   => Union{Float64, Int64}

After

julia> only(Base.code_ircode(foo, (Int,Float64,Bool); optimize_until="SROA"))
2 1 ─      goto #3 if not _4                                           │
  2 ─      nothing::A{Int64}                                           │╻ A
  └──      goto #4                                                     │
  3 ─      nothing::A{Float64}                                         │╻ A
  4 ┄ %8 = φ (#2 => _2, #3 => _3)::Union{Float64, Int64}               │
  │        nothing::Union{A{Float64}, A{Int64}}
3 │   %6 = %8::Union{Float64, Int64}                                   │
  └──      return %6                                                   │
   => Union{Float64, Int64}

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

@aviatesk aviatesk requested a review from Keno July 11, 2023 05:18
@nanosoldier
Copy link
Collaborator

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

@aviatesk aviatesk force-pushed the avi/unswitchtypeunion branch from 59aad42 to d7bf34a Compare July 11, 2023 06:24
@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.

This commit improves SROA pass by extending the `unswitchtupleunion`
optimization to handle the general parametric types, e.g.:
```julia
julia> struct A{T}
           x::T
       end;

julia> function foo(a1, a2, c)
           t = c ? A(a1) : A(a2)
           return getfield(t, :x)
       end;

julia> only(Base.code_ircode(foo, (Int,Float64,Bool); optimize_until="SROA"))
```

> Before
```
2 1 ─      goto #3 if not _4                                          │
  2 ─ %2 = %new(A{Int64}, _2)::A{Int64}                               │╻ A
  └──      goto #4                                                    │
  3 ─ %4 = %new(A{Float64}, _3)::A{Float64}                           │╻ A
  4 ┄ %5 = φ (#2 => %2, #3 => %4)::Union{A{Float64}, A{Int64}}        │
3 │   %6 = Main.getfield(%5, :x)::Union{Float64, Int64}               │
  └──      return %6                                                  │
   => Union{Float64, Int64}
```

> After
```
julia> only(Base.code_ircode(foo, (Int,Float64,Bool); optimize_until="SROA"))
2 1 ─      goto #3 if not _4                                           │
  2 ─      nothing::A{Int64}                                           │╻ A
  └──      goto #4                                                     │
  3 ─      nothing::A{Float64}                                         │╻ A
  4 ┄ %8 = φ (#2 => _2, #3 => _3)::Union{Float64, Int64}               │
  │        nothing::Union{A{Float64}, A{Int64}}
3 │   %6 = %8::Union{Float64, Int64}                                   │
  └──      return %6                                                   │
   => Union{Float64, Int64}
```
@aviatesk aviatesk force-pushed the avi/unswitchtypeunion branch from d7bf34a to ab1a6d6 Compare July 11, 2023 07:26
@aviatesk
Copy link
Member Author

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

@nanosoldier
Copy link
Collaborator

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

Copy link
Member

@Keno Keno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, why not

@aviatesk aviatesk merged commit 3995278 into master Jul 11, 2023
@aviatesk aviatesk deleted the avi/unswitchtypeunion branch July 11, 2023 17:53
vtjnash added a commit that referenced this pull request Jul 11, 2023
@maleadt
Copy link
Member

maleadt commented Jul 12, 2023

This also caused many PkgEval regressions, so good that it will be reverted:

EDIT: this PR actually fixes them.

Internal error: encountered unexpected error in runtime:
BoundsError(a=svec(), i=1)
ijl_bounds_error_int at /cache/build/default-amdci4-3/julialang/julia-master/src/rtutils.c:187
jl_f__svec_ref at /cache/build/default-amdci4-3/julialang/julia-master/src/builtins.c:1668
getindex at ./essentials.jl:764 [inlined]
unswitchtypeunion at ./compiler/typeutils.jl:345
unswitchtupleunion at ./compiler/typeutils.jl:321 [inlined]
sroa_pass! at ./compiler/ssair/passes.jl:1111
run_passes at ./compiler/optimize.jl:486
run_passes at ./compiler/optimize.jl:501 [inlined]
optimize at ./compiler/optimize.jl:450
jfptr_optimize_37641.1 at /opt/julia/lib/julia/sys.so (unknown line)
_jl_invoke at /cache/build/default-amdci4-3/julialang/julia-master/src/gf.c:2890 [inlined]
ijl_apply_generic at /cache/build/default-amdci4-3/julialang/julia-master/src/gf.c:3072
_typeinf at ./compiler/typeinfer.jl:265
typeinf at ./compiler/typeinfer.jl:216

@KristofferC
Copy link
Member

(it hasnt been reverted)

@aviatesk
Copy link
Member Author

This also caused many PkgEval regressions, so good that it has been reverted:

@maleadt At which package does the regression happen?

@maleadt
Copy link
Member

maleadt commented Jul 12, 2023

See the latest daily, https://s3.amazonaws.com/julialang-reports/nanosoldier/pkgeval/by_date/2023-07/11/report.html, most of the crashes point to this PR. For example, StaticArrayInterface, VectorizationBase, StrideArraysCore, ImageMorphology, ...

@aviatesk
Copy link
Member Author

Thanks!

@aviatesk
Copy link
Member Author

The regressions are introduced by #50483, but then fixed by #50502 (this PR). So they don't reproduce on the latest master.

@maleadt
Copy link
Member

maleadt commented Jul 12, 2023

Ah, OK. I saw unswitchtupleunion and only thought of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants