From 688c055709732dfc7b352096eed916e93d7ad18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 11 Nov 2026 12:57:09 +0100 Subject: [PATCH 1/2] Trace the JuliaC test and compile it with --trim=unsafe-warn 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. --- test/JuliaC/entry.jl | 28 ++++++++++++++++++++++++++++ test/JuliaC/test_juliac.jl | 19 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 test/JuliaC/entry.jl diff --git a/test/JuliaC/entry.jl b/test/JuliaC/entry.jl new file mode 100644 index 0000000000..37968a8155 --- /dev/null +++ b/test/JuliaC/entry.jl @@ -0,0 +1,28 @@ +# Copyright (c) 2017: Miles Lubin and contributors +# Copyright (c) 2017: Google Inc. +# +# Use of this source code is governed by an MIT-style license that can be found +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. + +# The entry point of the compiled MyApp. `test_juliac.jl` copies it next to +# `dispatch.trace`, the `--trace-dispatch` output of a run of MyApp in ordinary +# Julia. Each method reached through dynamic dispatch in that run becomes an +# entry point of the image, compiled with the methods it statically reaches. + +import MyApp.main + +# The signatures of the trace refer to the modules by name. +for M in values(Base.loaded_modules) + if !isdefined(@__MODULE__, nameof(M)) + Core.eval(@__MODULE__, :(const $(nameof(M)) = $M)) + end +end + +for line in eachline(joinpath(@__DIR__, "dispatch.trace")) + signature = try + Core.eval(@__MODULE__, Meta.parse(line).args[2]) + catch + continue # For instance, a closure that only exists in the traced run + end + Base.Experimental.entrypoint(signature) +end diff --git a/test/JuliaC/test_juliac.jl b/test/JuliaC/test_juliac.jl index 5130bb520d..900ed94c48 100644 --- a/test/JuliaC/test_juliac.jl +++ b/test/JuliaC/test_juliac.jl @@ -24,13 +24,28 @@ using Test import JSON import JuliaC +# Trace the methods that MyApp reaches through dynamic dispatch, so that +# `entry.jl` keeps them in the trimmed image. +function trace(output_dir) + project = joinpath(@__DIR__, "MyApp") + trace = joinpath(output_dir, "dispatch.trace") + output = joinpath(output_dir, "trace.mof.json") + run( + `$(Base.julia_cmd()) --project=$project --trace-dispatch=$trace -m MyApp --mathoptformat $output`, + ) + entry = joinpath(output_dir, "entry.jl") + cp(joinpath(@__DIR__, "entry.jl"), entry) + return entry +end + function compile() output_dir = mktempdir() outname = joinpath(output_dir, "MyApp") image_recipe = JuliaC.ImageRecipe( output_type = "--output-exe", - file = joinpath(@__DIR__, "MyApp"), - trim_mode = "no", + file = trace(output_dir), + project = joinpath(@__DIR__, "MyApp"), + trim_mode = "unsafe-warn", add_ccallables = false, verbose = true, ) From 4167509a0f34cb4557a3a63436f8959d15cc9462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 15 Nov 2026 09:47:24 +0100 Subject: [PATCH 2/2] Separate the traced command of the JuliaC test from the others 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. --- test/JuliaC/MyApp/src/MyApp.jl | 19 +++++++++++++++--- test/JuliaC/test_juliac.jl | 36 +++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/test/JuliaC/MyApp/src/MyApp.jl b/test/JuliaC/MyApp/src/MyApp.jl index 3d2c400a94..ac787d5e17 100644 --- a/test/JuliaC/MyApp/src/MyApp.jl +++ b/test/JuliaC/MyApp/src/MyApp.jl @@ -19,11 +19,14 @@ function @main(args::Vector{String})::Cint if head == "--mathoptformat" return run_mathoptformat(args) end + # The following commands are not traced by `test_juliac.jl`: the compiled + # binary must run them with the methods resolved at compile time. return 1 # Incorrect head end -function run_mathoptformat(args::Vector{String})::Cint - filename = only(args) +# The model of the traced command. Each command that is not traced writes this +# model with one difference, so that it tests one code path that is not traced. +function _model() model = MOI.FileFormats.MOF.Model() x = MOI.add_variable(model) MOI.set(model, MOI.VariableName(), x, "x") @@ -37,8 +40,18 @@ function run_mathoptformat(args::Vector{String})::Cint objective = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0) MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) MOI.set(model, MOI.ObjectiveFunction{typeof(objective)}(), objective) + return model, x +end + +function _write(model, args::Vector{String})::Cint + filename = only(args) open(io -> write(io, model), filename, "w") return 0 end -end # module MOFWriter +function run_mathoptformat(args::Vector{String})::Cint + model, _ = _model() + return _write(model, args) +end + +end # module MyApp diff --git a/test/JuliaC/test_juliac.jl b/test/JuliaC/test_juliac.jl index 900ed94c48..13bc9d5cbc 100644 --- a/test/JuliaC/test_juliac.jl +++ b/test/JuliaC/test_juliac.jl @@ -25,7 +25,8 @@ import JSON import JuliaC # Trace the methods that MyApp reaches through dynamic dispatch, so that -# `entry.jl` keeps them in the trimmed image. +# `entry.jl` keeps them in the trimmed image. Only `--mathoptformat` is traced, +# the other commands test the methods that are resolved at compile time. function trace(output_dir) project = joinpath(@__DIR__, "MyApp") trace = joinpath(output_dir, "dispatch.trace") @@ -60,25 +61,32 @@ end function runtests() output_dir = compile() is_test(name) = startswith("$name", "test_") - @testset "$name" for name in filter(is_test, names(@__MODULE__; all = true)) - getfield(@__MODULE__, name)(output_dir) + # The outer testset runs every command, even after one fails. + @testset "JuliaC" begin + @testset "$name" for name in + filter(is_test, names(@__MODULE__; all = true)) + getfield(@__MODULE__, name)(output_dir) + end end return end -function test_mathoptformat(output_dir) - # Run via the compiled binary - output = joinpath(output_dir, "compiled.mof.json") - run(`$(output_dir)/bin/MyApp --mathoptformat $output`) - # Compare against running as `-m` - reference = joinpath(output_dir, "reference.mof.json") +# Run the command `head` of MyApp with the compiled binary and with Julia, and +# return both outputs. +function _run(output_dir, head) + output = joinpath(output_dir, "compiled$head.mof.json") + run(`$(output_dir)/bin/MyApp $head $output`) + reference = joinpath(output_dir, "reference$head.mof.json") project = joinpath(@__DIR__, "MyApp") - run( - `$(Base.julia_cmd()) --project=$project -m MyApp --mathoptformat $reference`, - ) - @test read(output, String) == read(reference, String) + run(`$(Base.julia_cmd()) --project=$project -m MyApp $head $reference`) + return read(output, String), read(reference, String) +end + +function test_mathoptformat(output_dir) + compiled, reference = _run(output_dir, "--mathoptformat") + @test compiled == reference # Manually test the output - object = JSON.parsefile(output) + object = JSON.parse(compiled) @test object["variables"] == [Dict("name" => "x")] @test object["objective"] == Dict( "sense" => "min",