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

Constant-propagation / inference #25

Open
mcabbott opened this issue Apr 16, 2021 · 0 comments
Open

Constant-propagation / inference #25

mcabbott opened this issue Apr 16, 2021 · 0 comments

Comments

@mcabbott
Copy link
Owner

mcabbott commented Apr 16, 2021

The example of JuliaLang/julia#33435:

julia> a = rand(2, 2)
2×2 Array{Float64,2}:
 0.159947  0.956223
 0.712618  0.864652

julia> @code_warntype (x -> PermutedDimsArray(x, (2, 1)))(a)
Variables
  #self#::Core.Compiler.Const(var"##28#29"(), false)
  x::Array{Float64,2}

Body::PermutedDimsArray{Float64,2,_A,_B,Array{Float64,2}} where _B where _A
1 ─ %1 = Core.tuple(2, 1)::Core.Compiler.Const((2, 1), false)
│   %2 = Main.PermutedDimsArray(x, %1)::PermutedDimsArray{Float64,2,_A,_B,Array{Float64,2}} where _B where _A
└──      return %2

... works cleanly here:

julia> using TransmuteDims

julia> @code_warntype (x -> transmute(x, (2, 1)))(a)
Variables
  #self#::Core.Const(var"#17#18"())
  x::Matrix{Float64}

Body::LinearAlgebra.Transpose{Float64, Matrix{Float64}}
1 ─ %1 = Core.tuple(2, 1)::Core.Const((2, 1))
│   %2 = Main.transmute(x, %1)::LinearAlgebra.Transpose{Float64, Matrix{Float64}}
└──      return %2

... but makes a Transpose. Ones that make a TransmutedDimsArray aren't quite so clean:

julia> b = rand(2,2,2);

julia> @code_warntype (x -> transmute(x, (2, 3, 0, 1)))(b)
Variables
  #self#::Core.Const(var"#27#28"())
  x::Array{Float64, 3}

Body::Union{Array{Float64, 4}, TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}}
1 ─ %1 = Core.tuple(2, 3, 0, 1)::Core.Const((2, 3, 0, 1))
│   %2 = Main.transmute(x, %1)::Union{Array{Float64, 4}, TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}}
└──      return %2

julia> @code_warntype (x -> transmute(x, Val((2, 3, 0, 1))))(b)
MethodInstance for (::var"#29#30")(::Array{Float64, 3})
  from (::var"#29#30")(x) in Main at REPL[200]:1
Arguments
  #self#::Core.Const(var"#29#30"())
  x::Array{Float64, 3}
Body::TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}
1 ─ %1 = Core.tuple(2, 3, 0, 1)::Core.Const((2, 3, 0, 1))
│   %2 = Main.Val(%1)::Core.Const(Val{(2, 3, 0, 1)}())
│   %3 = Main.transmute(x, %2)::TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}
└──      return %3

Although still quite fast:

julia> @btime (x -> PermutedDimsArray(x, (2, 1)))($a);
  315.051 ns (4 allocations: 176 bytes)

julia> @btime (x -> transmute(x, (2, 1)))($a);
  1.500 ns (0 allocations: 0 bytes)

julia> @btime (x -> transmute(x, (2, 3, 0, 1)))($b);
  11.845 ns (1 allocation: 16 bytes)

julia> @btime (x -> transmute(x, Val((2, 3, 0, 1))))($b);
  0.875 ns (0 allocations: 0 bytes)
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

No branches or pull requests

1 participant