Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code is eliminated in @pstats #47

Closed
giordano opened this issue Dec 11, 2024 · 3 comments · Fixed by #48
Closed

Code is eliminated in @pstats #47

giordano opened this issue Dec 11, 2024 · 3 comments · Fixed by #48

Comments

@giordano
Copy link
Contributor

julia> using LinuxPerf

julia> function picalc(numsteps)
           slice = 1.0/numsteps
           sum = 0.0
           @simd for i = 1:numsteps
               x = (i - 0.5) * slice
               sum = sum + (4.0/(1.0 + x^2))
           end
           return sum * slice
       end
picalc (generic function with 1 method)

julia> @time picalc(10000000000)
  5.002960 seconds
3.1415926535897762

julia> @pstats picalc(10000000000)
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_frontend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_backend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
╶ cpu-cycles               1.44e+02  100.0%  #  0.0 cycles per ns
┌ instructions             1.70e+01  100.0%  #  0.1 insns per cycle
│ branch-instructions      5.00e+00  100.0%  # 29.4% of insns
└ branch-misses            2.00e+00  100.0%  # 40.0% of branch insns
┌ task-clock               2.59e+05  100.0%  # 258.8 μs
│ context-switches         0.00e+00  100.0%
│ cpu-migrations           0.00e+00  100.0%
└ page-faults              0.00e+00  100.0%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

julia> @pstats picalc(10000000000000)
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_frontend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_backend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
╶ cpu-cycles               1.57e+02  100.0%  #  0.0 cycles per ns
┌ instructions             1.70e+01  100.0%  #  0.1 insns per cycle
│ branch-instructions      5.00e+00  100.0%  # 29.4% of insns
└ branch-misses            2.00e+00  100.0%  # 40.0% of branch insns
┌ task-clock               3.09e+05  100.0%  # 309.2 μs
│ context-switches         0.00e+00  100.0%
│ cpu-migrations           0.00e+00  100.0%
└ page-faults              0.00e+00  100.0%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

julia> @pstats nothing
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_frontend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_backend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
╶ cpu-cycles               2.17e+02  100.0%  #  0.0 cycles per ns
┌ instructions             1.70e+01  100.0%  #  0.1 insns per cycle
│ branch-instructions      5.00e+00  100.0%  # 29.4% of insns
└ branch-misses            2.00e+00  100.0%  # 40.0% of branch insns
┌ task-clock               3.47e+05  100.0%  # 347.5 μs
│ context-switches         0.00e+00  100.0%
│ cpu-migrations           0.00e+00  100.0%
└ page-faults              0.00e+00  100.0%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

julia> versioninfo()
Julia Version 1.11.2
Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 22 × Intel(R) Core(TM) Ultra 7 155H
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 22 virtual cores)

(tmp) pkg> st
Status `/tmp/Project.toml`
  [b4c46c6c] LinuxPerf v0.4.0

picalc(10000000000000) should take 1.4 hours, but it takes ~300 μs inside @pstats, which is about the same time reported for nothing. I see there's some effort to try and avoid the code being eliminated

LinuxPerf.jl/src/LinuxPerf.jl

Lines 1144 to 1145 in eae2705

# trick the compiler not to eliminate the code
stats = rand() < 0 ? val : Stats(bench)

but it doesn't seem to be very effective anymore.

@Zentrik
Copy link
Collaborator

Zentrik commented Dec 11, 2024

Looks like llvm is able to figure out that rand() >= 0!

@topolarity
Copy link
Member

I think the ::Stats assertion on the next line is probably working against us, because it implies rand() >= 0

@Zentrik
Copy link
Collaborator

Zentrik commented Dec 11, 2024

I would guess LLVM can just see this gives non-negative results https://github.com/JuliaLang/julia/blob/8f5b7ca12ad48c6d740e058312fc8cf2bbe67848/stdlib/Random/src/Xoshiro.jl#L305-L306. Looks like the early CSE pass figured it out https://godbolt.org/z/qnqxrqM9a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants