From 174c1faf0bed1bc4f50ea967d0b37f43da380391 Mon Sep 17 00:00:00 2001 From: Zentrik Date: Wed, 11 Dec 2024 16:28:23 +0000 Subject: [PATCH 1/3] More robustly prevent compiler from eliminating code to be benchmarked --- src/LinuxPerf.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/LinuxPerf.jl b/src/LinuxPerf.jl index 3f37499..39e77fc 100644 --- a/src/LinuxPerf.jl +++ b/src/LinuxPerf.jl @@ -1140,9 +1140,12 @@ macro pstats(args...) try enable_all!() val = $(esc(expr)) + @static if isdefined(Base, :donotdelete) + Base.donotdelete(val) + end disable_all!() # trick the compiler not to eliminate the code - stats = rand() < 0 ? val : Stats(bench) + stats = @noinline rand() < 0 ? val : Stats(bench) return stats::Stats catch rethrow() From 624d8639663beadcf66e97d21b310067c66e24f3 Mon Sep 17 00:00:00 2001 From: Zentrik Date: Wed, 11 Dec 2024 16:42:51 +0000 Subject: [PATCH 2/3] Avoid calling `rand` if using `Base.donotdelete`. --- src/LinuxPerf.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/LinuxPerf.jl b/src/LinuxPerf.jl index 39e77fc..5e0cf9a 100644 --- a/src/LinuxPerf.jl +++ b/src/LinuxPerf.jl @@ -1140,12 +1140,14 @@ macro pstats(args...) try enable_all!() val = $(esc(expr)) + disable_all!() + # trick the compiler not to eliminate the code @static if isdefined(Base, :donotdelete) Base.donotdelete(val) + stats = Stats(bench) + else + stats = @noinline rand() < 0 ? val : Stats(bench) end - disable_all!() - # trick the compiler not to eliminate the code - stats = @noinline rand() < 0 ? val : Stats(bench) return stats::Stats catch rethrow() From 7c8e4d7e47ff0859650088669cfeb1edc5aad203 Mon Sep 17 00:00:00 2001 From: Zentrik Date: Wed, 11 Dec 2024 17:51:25 +0000 Subject: [PATCH 3/3] Only force `rand` to be outlined Doesn't seem necessary to outline the other function calls. Co-authored-by: Cody Tapscott <84105208+topolarity@users.noreply.github.com> --- src/LinuxPerf.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LinuxPerf.jl b/src/LinuxPerf.jl index 5e0cf9a..995574b 100644 --- a/src/LinuxPerf.jl +++ b/src/LinuxPerf.jl @@ -1146,7 +1146,7 @@ macro pstats(args...) Base.donotdelete(val) stats = Stats(bench) else - stats = @noinline rand() < 0 ? val : Stats(bench) + stats = (@noinline rand()) < 0 ? val : Stats(bench) end return stats::Stats catch