goretkin / FixArgs.jl

Other
19 stars 3 forks source link

inference with nested lambdas #29

Open goretkin opened 3 years ago

goretkin commented 3 years ago

This example came up on discord (also related to https://discourse.julialang.org/t/nested-lambda-functions-overhead-ideas-for-algebra-of-functions/47335 )

function foo(x, p)
    f1 = (x;p)->x^2+p.a
    f2 = (x;p)->1/(x^2+p.b^2)

    f3 = (x;p) -> p.c1 * f1(x;p) + p.c2 * f2(x;p)
    return f3(x; p)
end

#=
FixArgs.jl doesn't support do all the same transformations as lowering does, yet. It also does not support functions with keyword arguments (you can call functions with kwargs, but you cannot define functions with kwargs)
=#
using FixArgs: @xquote, xapply
function foo2(x, p)
    f1 = @xquote  (x, p) -> x^2 + getproperty(p, :a::::S)

    f2 = @xquote (x, p) -> 1/(x^2+(getproperty(p, :b::::S))^2)

    f3 = @xquote (x, p) -> getproperty(p, :c1::::S) * f1(x, p) + getproperty(p, :c2::::S) * f2(x, p)

    return xapply(f3, (x, p))
end

# seems fine
@code_llvm foo(1.0, (a=0, b=0, c1 = 0.5, c2 = 0.5))

# less fine
@code_llvm foo2(1.0, (a=0, b=0, c1 = 0.5, c2 = 0.5))

field access doesn't appear to be the problem

function foo3(nt)
      f = @xquote nt -> getproperty(nt, :a::::S)
      return xapply(f, (nt,))
end

julia> @code_llvm foo3((;a=1))
;  @ REPL[39]:1 within `foo3'
define i64 @julia_foo3_3334([1 x i64]* nocapture nonnull readonly align 8 dereferenceable(8) %0) {
top:
;  @ REPL[39]:3 within `foo3'
; ┌ @ /Users/goretkin/projects/FixArgs.jl/src/eval.jl:95 within `xapply' @ /Users/goretkin/projects/FixArgs.jl/src/eval.jl:96
; │┌ @ /Users/goretkin/projects/FixArgs.jl/src/eval.jl:44 within `xeval'
; ││┌ @ /Users/goretkin/projects/FixArgs.jl/src/eval.jl:104 within `_xapply'
; │││┌ @ /Users/goretkin/.julia/packages/FrankenTuples/LK9o9/src/FrankenTuples.jl:405 within `ftcall'
; ││││┌ @ Base.jl:33 within `getproperty'
       %1 = getelementptr inbounds [1 x i64], [1 x i64]* %0, i64 0, i64 0
; └└└└└
  %2 = load i64, i64* %1, align 8
  ret i64 %2
}