JuliaLang / JuliaLang/PrecompileTools.jl

`@compile_workload` does not include compilation for macro expansion of body

Open
#65 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
249
Forks
18
PR merge metrics
No merged PRs in 30d

Description

Consider a package like:
```julia
module DroppedPrecompile

using PrecompileTools
macro fancy_macro()
f = Core.compilerbarrier(:type, length)
return f(Tuple{Int64,Int32,Int16,Int8}[]) # this does not end up pre-compiled
end

@compile_workload begin
@fancy_macro
end

end # module DroppedPrecompile
```

The dynamically called `Base.length(::Tuple{Int64,Int32,Int16,Int8})` does not end up pre-compiled:
```julia
julia> using DroppedPrecompile
julia> Base.method_instance(Base.length, (Vector{Tuple{Int64,Int32,Int16,Int8}},)).cache
ERROR: UndefRefError: access to undefined reference
```

Logically, this makes sense - the macro expansion happens before the `begin ... end` block actually starts to execute.

Nonetheless, I wanted to file an issue, since it is a practical issue for libraries like ModelingToolkit.jl, which hit this use case (https://github.com/SciML/ModelingToolkit.jl/blob/3f965be3d256412844f7d43262d75fb48657c2df/src/ModelingToolkit.jl#L245-L278) and probably do intend to pre-compile the macros themselves, so that these are fast for future users.

Is there any good option for MTK to "opt-in" to including macro expansion in the workload?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the DroppedPrecompile example and inspect how @compile_workload handles macro expansion before the workload begins. Read the @compile_workload entry point and the ModelingToolkit.jl usage linked in the issue. Done should be a decided and documented opt-in path, with coverage for the macro-expansion case.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.