Skip to content

Commit 74371aa

Browse files
committed
Add pspawn=true to @snoopl to allow staying in the same process!
1 parent 1abf0a2 commit 74371aa

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

SnoopCompileCore/src/snoopl.jl

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Serialization
44

55
"""
66
```
7-
@snoopl "func_names.csv" "llvm_timings.yaml" begin
7+
@snoopl [jlflags="..."] [pspawn=true] "func_names.csv" "llvm_timings.yaml" begin
88
# Commands to execute, in a new process
99
end
1010
```
@@ -14,16 +14,26 @@ be used for the input to `SnoopCompile.read_snoopl("func_names.csv", "llvm_timin
1414
1515
The logs contain the amount of time spent optimizing each "llvm module", and information
1616
about each module, where a module is a collection of functions being optimized together.
17+
18+
If `pspawn=false`, the commands will be run in the same julia process, via `eval()` in
19+
the current module. This will only report LLVM optimizations for _new_ compilations, that
20+
haven't already been cached in this process, which can be (carefully) used to pruned down
21+
the results to only the code you are interested in.
1722
"""
18-
macro snoopl(flags, func_file, llvm_file, commands)
19-
return :(snoopl($(esc(flags)), $(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands))))
23+
macro snoopl(args...)
24+
@assert length(args) >= 3 """Usage: @snoopl [args...] "snoopl.csv" "snoopl.yaml" commands"""
25+
flags, (func_file, llvm_file, commands) = args[1:end-3], args[end-2:end]
26+
flags = [esc(e) for e in flags]
27+
return :(snoopl($(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands)), $__module__; $(flags...)))
2028
end
2129
macro snoopl(func_file, llvm_file, commands)
22-
return :(snoopl(String[], $(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands))))
30+
return :(snoopl($(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands)), $__module__))
2331
end
2432

25-
function snoopl(flags, func_file, llvm_file, commands)
26-
println("Launching new julia process to run commands...")
33+
function snoopl(func_file, llvm_file, commands, _module; pspawn=true, jlflags="")
34+
if pspawn
35+
println("Launching new julia process to run commands...")
36+
end
2737
# addprocs will run the unmodified version of julia, so we
2838
# launch it as a command.
2939
code_object = """
@@ -32,23 +42,31 @@ function snoopl(flags, func_file, llvm_file, commands)
3242
Core.eval(Main, deserialize(stdin))
3343
end
3444
"""
35-
process = open(`$(Base.julia_cmd()) $flags --eval $code_object`, stdout, write=true)
36-
serialize(process, quote
45+
record_and_run_quote = quote
3746
let func_io = open($func_file, "w"), llvm_io = open($llvm_file, "w")
3847
ccall(:jl_dump_emitted_mi_name, Nothing, (Ptr{Nothing},), func_io.handle)
3948
ccall(:jl_dump_llvm_opt, Nothing, (Ptr{Nothing},), llvm_io.handle)
4049
try
41-
$commands
50+
@eval $commands
4251
finally
4352
ccall(:jl_dump_emitted_mi_name, Nothing, (Ptr{Nothing},), C_NULL)
4453
ccall(:jl_dump_llvm_opt, Nothing, (Ptr{Nothing},), C_NULL)
4554
close(func_io)
4655
close(llvm_io)
4756
end
4857
end
49-
exit()
50-
end)
51-
wait(process)
52-
println("done.")
58+
end
59+
60+
if pspawn
61+
process = open(`$(Base.julia_cmd()) $jlflags --eval $code_object`, stdout, write=true)
62+
serialize(process, quote
63+
$record_and_run_quote
64+
exit()
65+
end)
66+
wait(process)
67+
println("done.")
68+
else
69+
Core.eval(_module, record_and_run_quote)
70+
end
5371
nothing
5472
end

0 commit comments

Comments
 (0)