Skip to content

Commit 990c356

Browse files
authored
Use latestworld-if-toplevel (#60)
Fixes #58
1 parent 6b5623a commit 990c356

File tree

9 files changed

+115
-4
lines changed

9 files changed

+115
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "PrecompileTools"
22
uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
3-
version = "1.3.1"
3+
version = "1.3.2"
44
authors = ["Tim Holy <[email protected]>", "t-bltg <[email protected]>", "and contributors"]
55

66
[deps]

docs/src/index.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ end
5252
end
5353
```
5454

55+
!!! warning
56+
`@compile_workload` should go at "top level," not compiled into a function. A pattern like `withenv(...) do @compile_workload begin ... end end`,
57+
where `@compile_workload` appears in the anonymous function passed to `withenv`, may defeat some of the value of PrecompileTools.
58+
5559
When you build `MyPackage`, it will precompile the following, *including all their callees*:
5660

5761
- `Pair(::MyPackage.MyType, ::Vector{MyPackage.OtherType})`
@@ -270,14 +274,14 @@ You can also specify additional packages (e.g., dependencies of `MyPackage`) if
270274
from an external environment. This will also keep the `precompile_workload` setting independent and avoid needless recompilation
271275
of large environments.
272276

273-
Finally, it is possible to fully disable PrecompileTools.jl for all packages with
277+
Finally, it is possible to fully disable PrecompileTools.jl for all packages with
274278

275279
```julia
276280
using PrecompileTools, Preferences
277281
set_preferences!(PrecompileTools, "precompile_workloads" => false; force=true)
278282
```
279283

280-
This can be helpful to reduce the system image size generated when using PackageCompiler.jl by only compiling calls made in a precompilation script.
284+
This can be helpful to reduce the system image size generated when using PackageCompiler.jl by only compiling calls made in a precompilation script.
281285

282286
## Seeing what got precompiled
283287

src/workloads.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ end
1414

1515
@noinline is_generating_output() = ccall(:jl_generating_output, Cint, ()) == 1
1616

17+
macro latestworld_if_toplevel()
18+
Expr(Symbol("latestworld-if-toplevel"))
19+
end
20+
1721
function tag_newly_inferred_enable()
1822
ccall(:jl_tag_newly_inferred_enable, Cvoid, ())
1923
if !Base.generating_output() # for verbose[]
@@ -65,7 +69,7 @@ macro compile_workload(ex::Expr)
6569
local iscompiling = :($PrecompileTools.is_generating_output() && $PrecompileTools.workload_enabled(@__MODULE__))
6670
ex = quote
6771
begin
68-
Core.@latestworld # block inference from proceeding beyond this point (xref https://github.com/JuliaLang/julia/issues/57957)
72+
$PrecompileTools.@latestworld_if_toplevel # block inference from proceeding beyond this point (xref https://github.com/JuliaLang/julia/issues/57957)
6973
$(esc(ex))
7074
end
7175
end
@@ -113,6 +117,7 @@ macro setup_workload(ex::Expr)
113117
return quote
114118
if $iscompiling || $PrecompileTools.verbose[]
115119
let
120+
$PrecompileTools.@latestworld_if_toplevel # block inference from proceeding beyond this point (xref https://github.com/JuliaLang/julia/issues/57957)
116121
$(esc(ex))
117122
end
118123
end

test/NotPrecompiled/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "NotPrecompiled"
2+
uuid = "82bf3039-9474-4881-94bd-824dbad9b14d"
3+
version = "0.1.0"
4+
authors = ["Tim Holy <[email protected]>"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module NotPrecompiled
2+
3+
# This module can be a dependency of others to check
4+
# caching: call `call_isa_bool(x::T)` for some `T` and
5+
# then check for `isa_bool(::T)` specializations.
6+
7+
isa_bool(x) = isa(x, Bool)
8+
call_isa_bool(x) = isa_bool(Base.inferencebarrier(x))
9+
10+
const themethod = only(methods(isa_bool))
11+
12+
end # module NotPrecompiled

test/NotToplevel/Manifest.toml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
julia_version = "1.12.0-beta1"
4+
manifest_format = "2.0"
5+
project_hash = "e8122786ed34aaf26503bdeb405954ce2c258c3a"
6+
7+
[[deps.Dates]]
8+
deps = ["Printf"]
9+
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
10+
version = "1.11.0"
11+
12+
[[deps.NotPrecompiled]]
13+
path = "../NotPrecompiled"
14+
uuid = "82bf3039-9474-4881-94bd-824dbad9b14d"
15+
version = "0.1.0"
16+
17+
[[deps.NotToplevel]]
18+
deps = ["PrecompileTools"]
19+
path = "."
20+
uuid = "3879f6fb-0a74-4dcf-89ef-07d77fe6cb90"
21+
version = "0.1.0"
22+
23+
[[deps.PrecompileTools]]
24+
deps = ["Preferences"]
25+
path = "../.."
26+
uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
27+
version = "1.3.1"
28+
29+
[[deps.Preferences]]
30+
deps = ["TOML"]
31+
git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6"
32+
uuid = "21216c6a-2e73-6563-6e65-726566657250"
33+
version = "1.4.3"
34+
35+
[[deps.Printf]]
36+
deps = ["Unicode"]
37+
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
38+
version = "1.11.0"
39+
40+
[[deps.TOML]]
41+
deps = ["Dates"]
42+
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
43+
version = "1.0.3"
44+
45+
[[deps.Unicode]]
46+
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
47+
version = "1.11.0"

test/NotToplevel/Project.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name = "NotToplevel"
2+
uuid = "3879f6fb-0a74-4dcf-89ef-07d77fe6cb90"
3+
version = "0.1.0"
4+
authors = ["Tim Holy <[email protected]>"]
5+
6+
[deps]
7+
NotPrecompiled = "82bf3039-9474-4881-94bd-824dbad9b14d"
8+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"

test/NotToplevel/src/NotToplevel.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module NotToplevel
2+
3+
using NotPrecompiled
4+
using PrecompileTools: @setup_workload, @compile_workload
5+
6+
hello(who::AbstractString) = "Hello, $who"
7+
8+
NotPrecompiled.call_isa_bool(1.0)
9+
10+
@setup_workload begin
11+
withenv() do
12+
@compile_workload begin
13+
hello("x")
14+
NotPrecompiled.call_isa_bool('x')
15+
end
16+
end
17+
end
18+
19+
end # module NotToplevel

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ using Base: specializations
8787
end
8888
PrecompileTools.verbose[] = oldval
8989

90+
using NotToplevel
91+
# Check that calls inside @setup_workload are not precompiled
92+
m = NotToplevel.NotPrecompiled.themethod
93+
have_char = have_float = false
94+
for mi in specializations(m)
95+
mi === nothing && continue
96+
have_char |= mi.specTypes.parameters[2] === Char
97+
have_float |= mi.specTypes.parameters[2] === Float64
98+
end
99+
@test !have_float # not wrapped inside `@compile_workload`
100+
@test_broken have_char # is wrapped
101+
90102
## @recompile_invalidations
91103

92104
# Mimic the format written to `_jl_debug_method_invalidation`

0 commit comments

Comments
 (0)