tshort / StaticCompiler.jl

Compiles Julia code to a standalone library (experimental)
Other
500 stars 30 forks source link

Patch and relocate pointers #58

Closed MasonProtter closed 2 years ago

MasonProtter commented 2 years ago

This allows using the Julia runtime, and referencing global variables.

julia> using StaticCompiler

julia> f_comp, path = compile((Int,), "arrtest") do N
           v = Vector{Float64}(undef, N)
           for i ∈ eachindex(v)
               v[i] = i*i
           end
           v
       end
(f = #5(::Int64) :: Vector{Float64}, path = "/home/mason/arrtest")

julia> exit() # Quit this session and start a new one
[mason@mason-pc ~]$ julia -q
julia> using StaticCompiler

julia> f = load_function("arrtest")
#5(::Int64) :: Vector{Float64}

julia> f(8)
8-element Vector{Float64}:
  1.0
  4.0
  9.0
 16.0
 25.0
 36.0
 49.0
 64.0

And here are some globals

julia> const x = Ref(0);

julia> compile((Int,), "counter") do y
           x[] += y
       end
(f = #5(::Int64) :: Int64, path = "/home/mason/counter")

julia> exit() # Quit this session and start a new one
[mason@mason-pc ~]$ julia -q
julia> using StaticCompiler

julia> f = load_function("counter")
#5(::Int64) :: Int64

julia> f(1)
1

julia> f(1)
2

julia> f(2)
4

julia> @btime $f(1)
  2.329 ns (0 allocations: 0 bytes)
500507

This was done with a huge amount of help from @vchuravy and @jpsamaroo!

brenhinkeller commented 2 years ago

Pretty cool stuff!

MasonProtter commented 2 years ago

Is there an easy way to run tests on windows but allow failures like you used to be able to do with Travis?

brenhinkeller commented 2 years ago

There's https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idif but it doesn't look particularly elegant

brenhinkeller commented 2 years ago

Screen Shot 2022-02-10 at 12 12 03 PM

brenhinkeller commented 2 years ago

Oh, here's a slightly nicer-looking approach: https://github.com/JuliaArrays/StaticArrays.jl/pull/776

MasonProtter commented 2 years ago

Thanks!

brenhinkeller commented 2 years ago

Hooray!