Skip to content

Trace the JuliaC test and compile it with --trim=unsafe-warn - #3146

Open
blegat wants to merge 2 commits into
masterfrom
bl/juliac-trace-only
Open

blegat wants to merge 2 commits into
masterfrom
bl/juliac-trace-only

Conversation

@blegat

@blegat blegat commented Nov 16, 2026

Copy link
Copy Markdown
Member

I found an approach that makes it much easier to compile MOI with JuliaC. Take this simple program that writes a MOF file:

function @main(args::Vector{String})::Cint

JuliaC normally goes throw it and try to infer all the methods that it can reach and then give up when it reaches parts that are too type unstable so the number of methods is growing exponentially.
I tried a few approaches to make the MOF writer more type stable but some changes were starting to make the code quite complex and less extensible.
The idea to make it work with a code like MOI where we have a few type instability here and there is to run some Julia program with --trace-dispatch and record all the traces. Then, we give these traces to JuliaC and ask it to also consider them as new roots to start with to see all the new methods it can reach.
Thanks to this, if one type instability is a bottleneck that prevents JuliaC to see all the methods should compile with that bottleneck, if that bottleneck is easily reach with one trace, you only need one trace to be able to compile everything!

This is the method I am using now in JuMPy and in this PR, I am implementing in the test with it as well. Some changes in the MOF writer that allows JuliaC to work better would still make sense (let's see in follow-up PRs) even with this change but at least now there is a clear tradeoff.

@blegat blegat changed the title Bl/juliac trace only Trace the JuliaC test and compile it with --trim=unsafe-warn Nov 16, 2026
@blegat
blegat force-pushed the bl/juliac-trace-only branch from 211c0df to f2a36ab Compare November 16, 2026 07:50
@blegat
blegat added this pull request to stack #3148 November 16, 2026 08:02
With `--trim=no`, the JuliaC test does not check that the executable still
works once it is trimmed. With `--trim=unsafe-warn`, JuliaC only compiles the
methods it can reach from `main` through calls that it can infer, so a call
that is dynamically dispatched fails at run time unless its method happens to
be compiled for another reason.

Like JuMPy does for its compiled backends, we first run MyApp in ordinary Julia
with `--trace-dispatch` and every method that was dynamically dispatched to
becomes an entry point of the image, see `entry.jl`. With this, the test passes
without any change to the source code of MOI so the following PRs can focus on
the code paths that the tracing does not cover.
The tracing only covers the code paths that the traced run actually executes.
To test the code paths that it does not cover, we add commands to MyApp that are
not traced. Each of them writes the traced model with one difference (a
duplicate name, another objective type, ...) so that it tests exactly one code
path that the tracing does not contain, and the test compares the file written
by the executable with the one written by Julia.

`_model` builds the traced model, `_run` runs a command with the executable and
with Julia, and the outer testset makes sure that all commands are run even if
one of them fails.
@blegat
blegat force-pushed the bl/juliac-trace-only branch from f2a36ab to 4167509 Compare November 16, 2026 08:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant