diff --git a/SnoopCompileCore/src/snoopl.jl b/SnoopCompileCore/src/snoopl.jl index 26e0832b..2e858d02 100644 --- a/SnoopCompileCore/src/snoopl.jl +++ b/SnoopCompileCore/src/snoopl.jl @@ -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 ``` @@ -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 = """ @@ -32,13 +42,12 @@ 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) @@ -46,9 +55,18 @@ function snoopl(flags, func_file, llvm_file, commands) 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