Skip to content

[Regression] RuntimeGeneratedFunctions tagged under ModelingToolkit instead of precompiled user module #678

@dpad

Description

@dpad

I am writing a package that builds some ODEFunctions at precompile time dependent on some ModelingToolkit models.
Supporting this is what ultimately drove my previous PR #477.

It seems that since ModelingToolkit's v4.0 (I upgraded from v3.20.1), the above functionality is once again broken.

Using the first test from https://github.com/SciML/ModelingToolkit.jl/blob/master/test/function_registration.jl as an example, the regression I'm seeing is as follows:

julia> # NOTE: this is an illustrative example, the keys shown may not match exactly.
julia> # I have a package MyModule (as per the test case above).
julia> using MyModule  
[precompiling MyModule...]

julia> # As per the test, I expect an answer of [30.0]
julia> MyModule.fun([0.5], [5.0], 0.)  
ERROR: KeyError: key (0xd6493547, 0xaf3999e1, 0x66674bbf, 0xb716e034, 0xa6bb5c0f) not found
Stacktrace:
 [1] getindex(::Dict{Any,Any}, ::NTuple{5,UInt32}) at .\dict.jl:467
 [2] (::RuntimeGeneratedFunctions.var"#5#6"{DataType,NTuple{5,UInt32}})() at C:\Users\DPad\.julia\packages\RuntimeGeneratedFunctions\uvbuv\src\RuntimeGeneratedFunctions.jl:133
 [3] lock(::RuntimeGeneratedFunctions.var"#5#6"{DataType,NTuple{5,UInt32}}, ::Base.Threads.SpinLock) at .\lock.jl:161
 [4] _lookup_body(::Type{T} where T, ::NTuple{5,UInt32}) at C:\Users\DPad\.julia\packages\RuntimeGeneratedFunctions\uvbuv\src\RuntimeGeneratedFunctions.jl:131
 [5] generated_callfunc_body(::NTuple{4,Symbol}, ::Type{T} where T, ::NTuple{5,UInt32}, ::NTuple{4,DataType}) at C:\Users\DPad\.julia\packages\RuntimeGeneratedFunctions\uvbuv\src\RuntimeGeneratedFunctions.jl:76
 [6] #s12#1 at C:\Users\DPad\.julia\packages\RuntimeGeneratedFunctions\uvbuv\src\RuntimeGeneratedFunctions.jl:163 [inlined]     
 [7] #s12#1(::Any, ::Any, ::Any, ::Any, ::Any) at .\none:0
 [8] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at .\boot.jl:527
 [9] RuntimeGeneratedFunction at C:\Users\DPad\.julia\packages\RuntimeGeneratedFunctions\uvbuv\src\RuntimeGeneratedFunctions.jl:68 [inlined]
 [10] f at C:\Users\DPad\.julia\packages\ModelingToolkit\HTjKG\src\systems\diffeqs\abstractodesystem.jl:133 [inlined]
 [11] ODEFunction at C:\Users\DPad\.julia\packages\DiffEqBase\b38LC\src\diffeqfunction.jl:248 [inlined]
...

julia> # It seems my function is tagged as being in ModelingToolkit...
julia> typeof(MyModule.fun.f.f_oop).parameters[2:3] 
svec(ModelingToolkit.var"#_RGF_ModTag", (0xd6493547, 0xaf3999e1, 0x66674bbf, 0xb716e034, 0xa6bb5c0f))

julia>   # The ModelingToolkit's function cache is empty.
julia> using ModelingToolkit
julia> using RuntimeGeneratedFunctions
julia> getfield(ModelingToolkit, RuntimeGeneratedFunctions._cachename)
Dict{Any,Any}()

julia> # If I define the module at runtime, it works fine.
julia> module MyModule   
               ...
               fun = ODEFunction(sys)
       end
julia> MyModule.fun([0.5], [5.0], 0.)
1-element Array{Float64,1}:
 30.0
julia> getfield(ModelingToolkit, RuntimeGeneratedFunctions._cachename)
Dict{Any,Any} with 2 entries:
  (0x7091df0e, 0xaddd82b1, 0xaa32db83, 0x => WeakRef(quote
  (0x61321fab, 0x744b53c8, 0xe99e5a49, 0x => WeakRef(quote

Note that the regression does not seem to apply when a module is defined at runtime, as I assume is the case for the actual test cases above.

I would expect my function MyModule.fun to actually get tagged under MyModule instead of ModelingToolkit (and that I also should initialise my own cache using RuntimeGeneratedFunctions.init(MyModule) inside my module definition, at least if I understood correctly).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions