brenhinkeller / StaticTools.jl

Enabling StaticCompiler.jl-based compilation of (some) Julia code to standalone native binaries by avoiding GC allocations and llvmcall-ing all the things!
MIT License
167 stars 12 forks source link

MallocArray of MallocMatrix #60

Closed Thomas008 closed 5 months ago

Thomas008 commented 5 months ago

I would like to use StaticTools to compile a MallocArray on MallocMatrix. StaticTools is very great e.g. to represent Strings in a MallocMatrix. When initializing a MallocArray on MallocMatrix, I get an AssertionError:

MallocVector{MallocMatrix}(undef,3)

Then I get

AssertionError: Base.allocatedinline(T)
Stacktrace:
 [1] MallocArray
   @ StaticTools C:\Users\T460\.julia\packages\StaticTools\BAWnD\src\mallocarray.jl:112 [inlined]
 [2] MallocVector{MallocArray}(x::UndefInitializer, dims::Int64)
   @ StaticTools C:\Users\T460\.julia\packages\StaticTools\BAWnD\src\mallocarray.jl:131
 [3] top-level scope
   @ REPL[47]:1

How can I initilize a MallocVector in MallocMatrix? There was already a similar issue. The solution was about redefining Base.allocatedinline(T), or Base.unsafe_load and Base.unsafe_store (?)

brenhinkeller commented 5 months ago

The problem is that MallocMatrix is an incomplete type; you need to fully specify the type.

julia> MallocMatrix
MallocMatrix (alias for MallocArray{T, 2} where T)

julia> MallocVector{MallocMatrix}(undef,3)
ERROR: AssertionError: Base.allocatedinline(T)
Stacktrace:
 [1] MallocArray
   @ ~/.julia/packages/StaticTools/PMqff/src/mallocarray.jl:112 [inlined]
 [2] MallocVector{MallocMatrix}(x::UndefInitializer, dims::Int64)
   @ StaticTools ~/.julia/packages/StaticTools/PMqff/src/mallocarray.jl:131
 [3] top-level scope
   @ REPL[3]:1

julia> MallocVector{MallocMatrix{Float64}}(undef,3)
3-element MallocVector{MallocMatrix{Float64}}:
 0×0 MallocMatrix{Float64}
 0×0 MallocMatrix{Float64}
 0×0 MallocMatrix{Float64}
Thomas008 commented 5 months ago

Great! I understand. Thank you a lot!