Skip to content

jit: do not compile methods with different optlevel in the same llvm module - #62034

Open
KristofferC wants to merge 3 commits into
masterfrom
kc/compile_level_sharding
Open

jit: do not compile methods with different optlevel in the same llvm module#62034
KristofferC wants to merge 3 commits into
masterfrom
kc/compile_level_sharding

Conversation

@KristofferC

Copy link
Copy Markdown
Member

Currently, a module running with non-default opt-level could "poison" the opt-level of a method in another module if they happened to get compiled together (due to the optimization level picked for the LLVM module being picked as the minimum of the methods involved).

The test shows a possible interaction where calling a method from a O0 module causes it to get compiled with O0, giving it a different result if it was compiled on its own (simd effect).

Iterated on with Claude

Comment thread Compiler/test/codegen.jl
a = rand(100_000)
expected = OptLevelNoLeakHeavy.ksum1(a) # compiled directly at the default level
OptLevelNoLeakSlow.call2(a) # ksum2 first reached (co-compiled) via an -O0 module
@test OptLevelNoLeakHeavy.ksum2(a) === expected

@KristofferC KristofferC Jun 8, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this won't test what it is supposed to test if bounds checking is forced on. Have to check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work even with --check-bounds=yes, but does require O2.

julia +nightly --check-bounds=yes /tmp/test_opt.jl
per-module optlevel does not leak across co-compiled modules: Test Failed at /tmp/test_opt.jl:29
  Expression: OptLevelNoLeakHeavy.ksum2(a) === expected
   Evaluated: 33348.18205471307 === 33348.18205471279
...

julia +nightly --check-bounds=yes -O0 /tmp/test_opt.jl 
Test Summary:                                                | Pass  Total  Time
per-module optlevel does not leak across co-compiled modules |    1      1  0.1s

julia +nightly --check-bounds=yes -O1 /tmp/test_opt.jl 
Test Summary:                                                | Pass  Total  Time
per-module optlevel does not leak across co-compiled modules |    1      1  0.1s

@KristofferC
KristofferC force-pushed the kc/compile_level_sharding branch from 54b072e to 6e40a4a Compare June 8, 2026 15:23
Comment thread src/jitlayers.cpp Outdated
auto &ES = jl_ExecutionEngine->getExecutionSession();
jl_emitted_output_t emitted =
out.finish(std::move(ctx), std::move(mod), *ES.getSymbolStringPool());
jl_ExecutionEngine->addOutput(std::move(emitted));

@xal-0 xal-0 Jun 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this reintroduces a more subtle version of the bug #61255 was intended to fix. In the current design, it is important that all of the CodeInfos passed to jl_emit_codeinsts_to_jit_impl be added to the JIT with a single addOutput call.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts about the updated revision?

…module

Currently, a module running with non-default opt-level could "poison" the opt-level of a method in another module if they happened to get compiled together (due to the optimization level picked for the LLVM module is picked as the minimum of the methods involved).
@KristofferC
KristofferC force-pushed the kc/compile_level_sharding branch from bd5c094 to 6e3a918 Compare July 22, 2026 13:20
@KristofferC

Copy link
Copy Markdown
Member Author

Bump (@xal-0). I know you are planning on rewriting this, but maybe there is some value in getting this in in the meantime.

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

Labels

compiler:codegen Generation of LLVM IR and native code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants