Julia: Keyword arguments in code_typed/lowered/llvm/native

Created on 24 Dec 2013  ยท  6Comments  ยท  Source: JuliaLang/julia

I can't seem to find any documentation, discussion, etc. on this and didn't get any hits on the julia-users post. Is this possible? Can/should it be added? Is it just missing documentation?

keyword arguments

Most helpful comment

If/when keyword arguments are next worked on, it would be great to think through cases like this as well as #23548 (i.e. precompiling methods w/ keyword arguments). Not supporting functions w/ keyword arguments in these reflection apis and precompile really make them 2nd-class citizens, in addition to the known performance cost.

All 6 comments

This will be handled by #2758

Sounds great!

If/when keyword arguments are next worked on, it would be great to think through cases like this as well as #23548 (i.e. precompiling methods w/ keyword arguments). Not supporting functions w/ keyword arguments in these reflection apis and precompile really make them 2nd-class citizens, in addition to the known performance cost.

Now that keyword arguments have been changed, is what @code_warntype shows changed at all?

This issue is pretty stale at this point; I think improvements to inlining have mostly fixed this since you usually see the inlined "original" method body in @code_warntype. If the invoked keyword args are too complex, you can still use knowledge about keyword methods are lowered/called to still get to the original method body for inspection (clarified in https://github.com/JuliaLang/julia/pull/28358).

julia> versioninfo()
Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2683 v4 @ 2.10GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, broadwell)

# Using kwargs:

julia> function foo(;x::Int64)
           x + rand(1:10)
       end
foo (generic function with 1 method)

julia> @code_warntype foo(x=4)
Variables
  #unused#::Core.Compiler.Const(getfield(Main, Symbol("#kw##foo"))(), false)
  @_2::NamedTuple{(:x,),Tuple{Int64}}
  @_3::Core.Compiler.Const(foo, false)
  x::Int64
  @_5::Int64

Body::Int64
1 โ”€ %1  = Base.haskey(@_2, :x)::Core.Compiler.Const(true, false)
โ”‚         %1
โ”‚   %3  = Base.getindex(@_2, :x)::Int64
โ”‚   %4  = (%3 isa Main.Int64)::Core.Compiler.Const(true, false)
โ”‚         %4
โ””โ”€โ”€       goto #3
2 โ”€       Core.Compiler.Const(:(%new(Core.TypeError, Symbol("keyword argument"), :x, Main.Int64, %3)), false)
โ””โ”€โ”€       Core.Compiler.Const(:(Core.throw(%7)), false)
3 โ”„       (@_5 = %3)
โ””โ”€โ”€       goto #5
4 โ”€       Core.Compiler.Const(:(Core.UndefKeywordError(:x)), false)
โ””โ”€โ”€       Core.Compiler.Const(:(@_5 = Core.throw(%11)), false)
5 โ”„       (x = @_5)
โ”‚   %14 = (:x,)::Core.Compiler.Const((:x,), false)
โ”‚   %15 = Core.apply_type(Core.NamedTuple, %14)::Core.Compiler.Const(NamedTuple{(:x,),T} where T<:Tuple, false)
โ”‚   %16 = Base.structdiff(@_2, %15)::Core.Compiler.Const(NamedTuple(), false)
โ”‚   %17 = Base.pairs(%16)::Core.Compiler.Const(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}(), false)
โ”‚   %18 = Base.isempty(%17)::Core.Compiler.Const(true, false)
โ”‚         %18
โ””โ”€โ”€       goto #7
6 โ”€       Core.Compiler.Const(:(Base.kwerr(@_2, @_3)), false)
7 โ”„ %22 = Main.:(#foo#17)(x, @_3)::Int64
โ””โ”€โ”€       return %22

# Non-kwargs

julia> function bar(x::Int64)
           x + rand(1:10)
       end
bar (generic function with 1 method)

# much nicer output: we actually see the logic!
julia> @code_warntype bar(4)
Variables
  #self#::Core.Compiler.Const(bar, false)
  x::Int64

Body::Int64
1 โ”€ %1 = (1:10)::Core.Compiler.Const(1:10, false)
โ”‚   %2 = Main.rand(%1)::Int64
โ”‚   %3 = (x + %2)::Int64

On julia 1.2, I find this to still be an issue even for a very small function that should be "inlined" into the kwargs call. Makes it tricky to debug type instabilities, and is particularly confusing for first-time users.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbromberger picture sbromberger  ยท  3Comments

tkoolen picture tkoolen  ยท  3Comments

StefanKarpinski picture StefanKarpinski  ยท  3Comments

dpsanders picture dpsanders  ยท  3Comments

musm picture musm  ยท  3Comments