Open ctessum opened 3 months ago
This is not a good test case for it. Or at least the title is not correct. Julia's dead code elimination is performed at the lock level. So if you want to determine what code is actually run you need to check the post optimization llvm. But llvm's dce is very robust so I would be surprised if there is a problem there.
Now your example would not dce because your code mutates a global and thus the compiler is not allowed to delete that as it would clearly change the code behavior. So your test case, if changed to not have a side effect, would get erased in the llvm layer
But finally, it isn't optimal to drop all of the observed in there and rely on dce. While it would make 0 difference in runtime, it could make a difference in compile time. It would be good to actually measure that and see if it's an noticable effect. Since dce is prior to the most expensive passes I would be surprised if that was the case though
Does this work as a test case:
function f(t)
@info "evaluating unnecessary observed function"
return sin(t)
end
It gives a similar result, printing 9 lines of log messages.
No, logging is again mutating a global so DCE is not allowed to delete it since it would change how code is run. It would have to be something like
julia> function f(t)
a = 1+1
return sin(t)
end
f (generic function with 1 method)
julia> @code_llvm(f(1.0))
; @ REPL[4]:1 within `f`
; Function Attrs: uwtable
define double @julia_f_1898(double %0) #0 {
top:
; @ REPL[4]:3 within `f`
%1 = call double @j_sin_1900(double %0)
ret double %1
}
You can see DCE removed the extraneous computation in the LLVM.
OK, I see the miscommunication here. The problem that I have is not that there is extraneous code running in the function, it's that that function is running at all. So, in other words, the desired behavior in this case would be that f(t)
is never called, because the variable on the right-hand-side of that equation, y
, is never used for anything.
That will be deleted by DCE though, so it won't be actually called. Can you show me the LLVM where it's called and it's not needed?
using ModelingToolkit, DifferentialEquations
using ModelingToolkit: t_nounits as t, D_nounits as D
using Test
using InteractiveUtils
@variables x(t)=1 y(t)=1
function f(t)
return sin(t)
end
@register_symbolic f(t)
@named sys = ODESystem([
D(x) ~ 1,
y ~ f(t)
], t)
sys = structural_simplify(sys)
prob = ODEProblem(sys, [], (0.0, 1.0), [])
@code_llvm(prob.f([0.0, 0.0], 0.0, 0.0))
; @ within `ODEFunction`
define nonnull {}* @julia_ODEFunction_3351({ [1 x i8], { { i64, {}*, { {}*, i8, {}*, {}*, {}*, {}*, {}*, i32 }, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, i8, {}*, {}*, {}*, {}*, {}* }, {}*, i8, i8, {}* }, { i64, {}*, { {}*, i8, {}*, {}*, {}*, {}*, {}*, i32 }, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, i8, {}*, {}*, {}*, {}*, {}* } }* nocapture noundef nonnull readonly align 8 dereferenceable(736) %0, {}* noundef nonnull align 16 dereferenceable(40) %1, double %2, double %3) #0 {
top:
; β @ .julia/packages/ModelingToolkit/3AmNp/src/systems/diffeqs/abstractodesystem.jl:335 within `f`
; ββ @/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150 within `RuntimeGeneratedFunction`
; βββ @ none within `generated_callfunc`
; ββββ @ none within `macro expansion` @ /RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:163 @ .julia/packages/SymbolicUtils/EGhOJ/src/code.jl:375
; βββββ @ /test.jl:8 within `f`
%4 = call double @j_sin_3353(double %2)
; βββββ
; ββββ @ none within `macro expansion` @ /.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:163 @ /.julia/packages/SymbolicUtils/EGhOJ/src/code.jl:468
; βββββ @ /.julia/packages/SymbolicUtils/EGhOJ/src/code.jl:498 within `create_array`
; ββββββ @ array.jl:163 within `vect`
; βββββββ @ boot.jl:477 within `Array`
%5 = call nonnull {}* inttoptr (i64 4315363904 to {}* ({}*, i64)*)({}* inttoptr (i64 4762507712 to {}*), i64 1)
; βββββββ
; ββββββ @ array.jl:165 within `vect`
; βββββββ @ array.jl:1026 within `__inbounds_setindex!`
%6 = bitcast {}* %5 to double**
%arrayptr4 = load double*, double** %6, align 8
store double 1.000000e+00, double* %arrayptr4, align 8
; βββββββ
; ββββ @ none within `macro expansion`
ret {}* %5
; ββββ
It's the %4 = call double @j_sin_3353(double %2)
, right?
Try @code_llvm optimize=true f(x)
Still there:
@code_llvm optimize=true prob.f([0.0, 0.0], 0.0, 0.0)
@ //.julia/packages/SciMLBase/hq1ku/src/scimlfunctions.jl:2299 within `ODEFunction`
define nonnull {}* @julia_ODEFunction_2895({ [1 x i8], { { i64, {}*, { {}*, i8, {}*, {}*, {}*, {}*, {}*, i32 }, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, i8, {}*, {}*, {}*, {}*, {}* }, {}*, i8, i8, {}* }, { i64, {}*, { {}*, i8, {}*, {}*, {}*, {}*, {}*, i32 }, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, {}*, i8, {}*, {}*, {}*, {}*, {}* } }* nocapture noundef nonnull readonly align 8 dereferenceable(736) %0, {}* noundef nonnull align 16 dereferenceable(40) %1, double %2, double %3) #0 {
top:
; β @ /.julia/packages/ModelingToolkit/3AmNp/src/systems/diffeqs/abstractodesystem.jl:335 within `f`
; ββ @ /.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150 within `RuntimeGeneratedFunction`
; βββ @ none within `generated_callfunc`
; ββββ @ none within `macro expansion` @ //.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:163 @ /.julia/packages/SymbolicUtils/EGhOJ/src/code.jl:375
; βββββ @ /test.jl:9 within `f`
%4 = call double @j_sin_2897(double %2)
; βββββ
; ββββ @ none within `macro expansion` @ /.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:163 @ /.julia/packages/SymbolicUtils/EGhOJ/src/code.jl:468
; βββββ @ /.julia/packages/SymbolicUtils/EGhOJ/src/code.jl:498 within `create_array`
; ββββββ @ array.jl:163 within `vect`
; βββββββ @ boot.jl:477 within `Array`
%5 = call nonnull {}* inttoptr (i64 4353620544 to {}* ({}*, i64)*)({}* inttoptr (i64 4800764352 to {}*), i64 1)
; βββββββ
; ββββββ @ array.jl:165 within `vect`
; βββββββ @ array.jl:1026 within `__inbounds_setindex!`
%6 = bitcast {}* %5 to double**
%arrayptr4 = load double*, double** %6, align 8
store double 1.000000e+00, double* %arrayptr4, align 8
; βββββββ
; ββββ @ none within `macro expansion`
ret {}* %5
; ββββ
}
@gbaraldi is this because we cannot prove sin
doesn't have side effects? I thought the effects system would have solved that?
Describe the bug π
I have a project that involves data loaders, basically reading large amounts of data from files and interpolating them into MTK models, for example here: https://data.earthsci.dev/stable/geosfp/. There are a lot of variables in the data files, and the system is set up to load all of them as equations, because it's hard to tell ahead of time which ones will be needed. However, when I run the model, it appears that all of the variables from all of the data files are being load and interpolated at each time step, rather than only the ones that need to be computed to calculate the state variables. This seems to be slowing things downβwhen I look at the CPU profile, almost all of the time is being spent on data loading.
Expected behavior
I would hope that the observed variables would be computed lazily, i.e. only when the value of the observed variable is required to compute the system state, or when it is queried by someone interating with the solution object.
Minimal Reproducible Example π
The code below creates an ODESystem with one state variable and one observed variable. The observed variable isn't related to the state variable at all, so it doesn't necessarily need to be evaluated to solve the problem.
There is also a counter
unnecessary_computations
, which gets incremented whenever the equation to calculatey
is evaluated. Since we don't need to knowy
to solve the problem, any evaluations of the equation leading toy
are unnecessary.Therefore, after solution,
unecessary_computations
should equal 0, but instead it equals 9 in this case. Insol.stats
, the number of function 1 evaluations is also 9, suggesting that the unnecessary computation is happening whenever the ODE function is being evaluated.Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
This command gives an error but I'm using Julia version 1.10.
Additional context:
There is a larger-scale reproducer here: https://github.com/EarthSciML/EnvironmentalTransport.jl/blob/28b0749bd7c6457cfa00ec305a0485accc6a07f5/docs/src/advection.md