Skip to content

Commit

Permalink
Add pspawn=true to @snoopl to allow staying in the same process!
Browse files Browse the repository at this point in the history
  • Loading branch information
NHDaly committed Jul 25, 2021
1 parent 1abf0a2 commit 74371aa
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions SnoopCompileCore/src/snoopl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Serialization

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

function snoopl(flags, func_file, llvm_file, commands)
println("Launching new julia process to run commands...")
function snoopl(func_file, llvm_file, commands, _module; pspawn=true, jlflags="")
if pspawn
println("Launching new julia process to run commands...")
end
# addprocs will run the unmodified version of julia, so we
# launch it as a command.
code_object = """
Expand All @@ -32,23 +42,31 @@ function snoopl(flags, func_file, llvm_file, commands)
Core.eval(Main, deserialize(stdin))
end
"""
process = open(`$(Base.julia_cmd()) $flags --eval $code_object`, stdout, write=true)
serialize(process, quote
record_and_run_quote = quote
let func_io = open($func_file, "w"), llvm_io = open($llvm_file, "w")
ccall(:jl_dump_emitted_mi_name, Nothing, (Ptr{Nothing},), func_io.handle)
ccall(:jl_dump_llvm_opt, Nothing, (Ptr{Nothing},), llvm_io.handle)
try
$commands
@eval $commands
finally
ccall(:jl_dump_emitted_mi_name, Nothing, (Ptr{Nothing},), C_NULL)
ccall(:jl_dump_llvm_opt, Nothing, (Ptr{Nothing},), C_NULL)
close(func_io)
close(llvm_io)
end
end
exit()
end)
wait(process)
println("done.")
end

if pspawn
process = open(`$(Base.julia_cmd()) $jlflags --eval $code_object`, stdout, write=true)
serialize(process, quote
$record_and_run_quote
exit()
end)
wait(process)
println("done.")
else
Core.eval(_module, record_and_run_quote)
end
nothing
end

0 comments on commit 74371aa

Please sign in to comment.