Testing for type stability regressions

Is there a standard way of testing for regressions in type stability?

Currently I manually check there are no unions etc in @code_warntype, but it would be preferable if that was a test.

1 Like

Base.Test.@inferred? It returns the value of the expression, so you can sprinkle it in your runtests.jl file, like @test @inferred(f(12)) == 12

3 Likes

I’ve been using that, but it seems that it doesn’t really check that all the code generated is type stable, just that the result is?

@inferred might pass just fine but @code_warntype can still show a whole lot of Union types. Maybe I need to be more specific in how I’m using it. It just seemed like there could be a general test of stability for a whole block of code, not just the result.

1 Like

Perhaps https://github.com/MikeInnes/Traceur.jl can help? In its current form, it just prints out issues like type-instability, but I’m sure it could be adapted to act as a unit test.

1 Like

There is an open PR

https://github.com/JuliaLang/julia/pull/23426

and a package

https://github.com/afniedermayer/InferenceUtilities.jl

which I asked the author to register, but there was no response, so it may be abandonned.

Perhaps the PR could be dusted off.

1 Like

You can also use TraceCalls.jl to check for type-stability of all subcalls. Something like

trace = @trace YourModule f(x)
@test all(is_inferred, collect(trace))
1 Like

Given that the end-result of a type-instability is unexpected allocations, you can also test the behavior with @allocated to ensure Julia isn’t allocating more than is necessary.

Thanks for all the suggestions! I didn’t know about any of those modules, I’ll look into them. The allocated trick also sounds good, would simultaneously test allocation regressions, which is also useful