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

improve constant propagation of named tuples #33186

Closed
kcajf opened this issue Sep 6, 2019 · 1 comment
Closed

improve constant propagation of named tuples #33186

kcajf opened this issue Sep 6, 2019 · 1 comment
Labels
compiler:inference Type inference

Comments

@kcajf
Copy link
Contributor

kcajf commented Sep 6, 2019

This might already be known - there seem to be a few issues around keyword arguments and constant propagation / type inference, but I'm not really in the loop and it wasn't sure how out of date the issues are.

An attempt at a MWE:

f(; x) = g(x)
g(x) = x

z1() = g(3)
x1() = f(x=3)

z2() = g(Int64)
x2() = f(x=Int64)

julia> @code_warntype z1()
Variables
  #self#::Core.Compiler.Const(z1, false)

Body::Int64
1 ─ %1 = Main.g(3)::Core.Compiler.Const(3, false)
└──      return %1

julia> @code_warntype x1()
Variables
  #self#::Core.Compiler.Const(x1, false)

Body::Int64
1 ─ %1 = (:x,)::Core.Compiler.Const((:x,), false)
│   %2 = Core.apply_type(Core.NamedTuple, %1)::Core.Compiler.Const(NamedTuple{(:x,),T} where T<:Tuple, false)
│   %3 = Core.tuple(3)::Core.Compiler.Const((3,), false)
│   %4 = (%2)(%3)::NamedTuple{(:x,),Tuple{Int64}}
│   %5 = Core.kwfunc(Main.f)::Core.Compiler.Const(getfield(Main, Symbol("#kw##f"))(), false)
│   %6 = (%5)(%4, Main.f)::Int64
└──      return %6

julia> 

julia> @code_warntype z2()
Variables
  #self#::Core.Compiler.Const(z2, false)

Body::Type{Int64}
1 ─ %1 = Main.g(Main.Int64)::Core.Compiler.Const(Int64, false)
└──      return %1

julia> @code_warntype x2()
Variables
  #self#::Core.Compiler.Const(x2, false)

Body::DataType
1 ─ %1 = (:x,)::Core.Compiler.Const((:x,), false)
│   %2 = Core.apply_type(Core.NamedTuple, %1)::Core.Compiler.Const(NamedTuple{(:x,),T} where T<:Tuple, false)
│   %3 = Core.tuple(Main.Int64)::Core.Compiler.Const((Int64,), false)
│   %4 = (%2)(%3)::NamedTuple{(:x,),Tuple{DataType}}
│   %5 = Core.kwfunc(Main.f)::Core.Compiler.Const(getfield(Main, Symbol("#kw##f"))(), false)
│   %6 = (%5)(%4, Main.f)::DataType

For z1 and x1, inference seems OK. For x2, it seems to break (can only get DataType out), despite the fact that the non-kwargs version z2 infers something more precise (Type{Int64}).

The real usecase I met this in was trying to implement something like Base.dropdims(a::MyType; dims) = _dropdims(a, dims). Inference was completely failing, despite _dropdims(my_object, 3) inferring perfectly.

Ran on master:

julia> versioninfo()
Julia Version 1.4.0-DEV.103
Commit cb76f2a0e7 (2019-09-06 22:43 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
@JeffBezanson JeffBezanson added the compiler:inference Type inference label Sep 10, 2019
@JeffBezanson JeffBezanson changed the title Inference failure across simple kwargs wrapper function improve constant propagation of named tuples Sep 10, 2019
@JeffBezanson
Copy link
Member

Fixed by #35976

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

No branches or pull requests

2 participants