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

closures sometimes boxing closed variables #15502

Closed
adamkapor opened this issue Mar 14, 2016 · 2 comments
Closed

closures sometimes boxing closed variables #15502

adamkapor opened this issue Mar 14, 2016 · 2 comments

Comments

@adamkapor
Copy link

I ran into a case where a closure that I expected to be fast (closed variables of known type, read-only) was not. I think I reduced it to the example below. Should the closure returned by f1 have inferred the type of y?

function f1(x)
    y=x
    return z->y*z
end

function f2(x)
    return z->x*z
end

g1 = f1(1.)
g1.y  #Box(1.0)

g2 = f2(1.)
g2.x  #1.0

This is with a recent version:

Julia Version 0.5.0-dev+3149
Commit 3c72273 (2016-03-14 16:15 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin15.2.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, haswell)

@code_llvm shows that g2 is nice but g1 is not so nice:

julia> @code_llvm g1(1.)

define %jl_value_t* @"julia_#1_23995"(%jl_value_t*, double) #0 {
top:
  %2 = alloca [9 x %jl_value_t*], align 8
  %.sub = getelementptr inbounds [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 0
  %3 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 4
  %4 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 7
  %5 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 2
  store %jl_value_t* null, %jl_value_t** %5, align 8
  %6 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 3
  store %jl_value_t* null, %jl_value_t** %6, align 8
  store %jl_value_t* null, %jl_value_t** %3, align 8
  %7 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 5
  store %jl_value_t* null, %jl_value_t** %7, align 8
  %8 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 6
  store %jl_value_t* null, %jl_value_t** %8, align 8
  store %jl_value_t* null, %jl_value_t** %4, align 8
  %9 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 8
  store %jl_value_t* null, %jl_value_t** %9, align 8
  %10 = bitcast [9 x %jl_value_t*]* %2 to i64*
  store i64 14, i64* %10, align 8
  %11 = load i64, i64* bitcast (%jl_value_t*** @jl_tls_states to i64*), align 8
  %12 = getelementptr [9 x %jl_value_t*], [9 x %jl_value_t*]* %2, i64 0, i64 1
  %13 = bitcast %jl_value_t** %12 to i64*
  store i64 %11, i64* %13, align 8
  store %jl_value_t** %.sub, %jl_value_t*** @jl_tls_states, align 8
  store %jl_value_t* %0, %jl_value_t** %5, align 8
  %14 = bitcast %jl_value_t* %0 to i64*
  %15 = load i64, i64* %14, align 8
  %16 = bitcast %jl_value_t** %4 to i64*
  store i64 %15, i64* %16, align 8
  store %jl_value_t* inttoptr (i64 13063272696 to %jl_value_t*), %jl_value_t** %9, align 8
  %17 = call %jl_value_t* @jl_f_getfield(%jl_value_t* null, %jl_value_t** %4, i32 2)
  store %jl_value_t* %17, %jl_value_t** %7, align 8
  store %jl_value_t* inttoptr (i64 4458931072 to %jl_value_t*), %jl_value_t** %3, align 8
  %18 = call %jl_value_t* @jl_gc_alloc_1w()
  %19 = getelementptr inbounds %jl_value_t, %jl_value_t* %18, i64 -1, i32 0
  store %jl_value_t* inttoptr (i64 4456492784 to %jl_value_t*), %jl_value_t** %19, align 8
  %20 = bitcast %jl_value_t* %18 to double*
  store double %1, double* %20, align 8
  store %jl_value_t* %18, %jl_value_t** %8, align 8
  %21 = call %jl_value_t* @jl_apply_generic(%jl_value_t** %3, i32 3)
  store %jl_value_t* %21, %jl_value_t** %6, align 8
  %22 = load i64, i64* %13, align 8
  store i64 %22, i64* bitcast (%jl_value_t*** @jl_tls_states to i64*), align 8
  ret %jl_value_t* %21
}

julia> @code_llvm g2(1.)

define double @"julia_#3_23996"(%"##3#4"*, double) #0 {
top:
  %2 = getelementptr inbounds %"##3#4", %"##3#4"* %0, i64 0, i32 0
  %3 = load double, double* %2, align 8
  %4 = fmul double %3, %1
  ret double %4
}
@KristofferC
Copy link
Sponsor Member

See: #15276, #15358

@yuyichao
Copy link
Contributor

Close as dup.

This issue was closed.
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

3 participants