[GHC] #8321: improve basic block layout on LLVM backend by predicting stack/heap checks

#8321: improve basic block layout on LLVM backend by predicting stack/heap checks
------------------------------------+-------------------------------------
Reporter: rwbarton | Owner:
Type: feature request | Status: new
Priority: normal | Milestone: 7.10.1
Component: Compiler (LLVM) | Version: 7.7
Keywords: | Operating System: Unknown/Multiple
Architecture: Unknown/Multiple | Type of failure: None/Unknown
Difficulty: Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: |
------------------------------------+-------------------------------------
Currently we don't give the LLVM optimizer any information about which
branch of an `if` is likely to be taken. As a result, the optimizer is
likely to produce a basic block layout which is not optimal. Improving the
layout can improve performance through better instruction cache usage and
better branch prediction by the hardware.
We can control LLVM's idea of what is likely with the `llvm.expect`
intrinsic function. Some obvious branches which we can predict accurately
are the stack and heap checks that appear near the entry of many
functions.
Here's a small example of some Cmm code and the output of the LLVM
optimizer/compiler.
{{{
block_c2Lc_entry() // [R1, R2]
{ info_tbl: [(c2Lc,
label: block_c2Lc_info
rep:StackRep [])]
stack_info: arg_space: 0 updfr_space: Nothing
}
{offset
c2Lc:
Hp = Hp + 24;
if (Hp > HpLim) goto c2Lm; else goto c2Ll;
c2Lm:
HpAlloc = 24;
R2 = R2;
R1 = R1;
call stg_gc_pp(R2, R1) args: 8, res: 8, upd: 24;
c2Ll:
I64[Hp - 16] = :_con_info;
P64[Hp - 8] = R1;
P64[Hp] = R2;
R1 = Hp - 14;
Sp = Sp + 8;
call (P64[Sp])(R1) args: 24, res: 0, upd: 24;
}
}]
}}}
{{{
00000000000002b8

#8321: improve basic block layout on LLVM backend by predicting stack/heap checks -------------------------------------+------------------------------------ Reporter: rwbarton | Owner: rwbarton Type: feature request | Status: new Priority: normal | Milestone: 7.10.1 Component: Compiler (LLVM) | Version: 7.7 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by rwbarton): * owner: => rwbarton Comment: I have a proof-of-concept patch that implements this. It gave about a 1.5% speedup overall on nofib, with around 10% speedup on a couple simple stack-heavy benchmarks and a worst-case slowdown of -1.5%. I plan to revisit it after the 7.8 release. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8321#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8321: improve basic block layout on LLVM backend by predicting stack/heap checks -------------------------------------+------------------------------------ Reporter: rwbarton | Owner: rwbarton Type: feature request | Status: new Priority: normal | Milestone: 7.10.1 Component: Compiler (LLVM) | Version: 7.7 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by carter): Awesome work Reid! this sort of perf boost is huge. (also is another bullet point for why a lot of 7.10 work should be giving the code gens some love.) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8321#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8321: improve basic block layout on LLVM backend by predicting stack/heap checks -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: rwbarton Type: feature | Status: new request | Milestone: 7.10.1 Priority: normal | Version: 7.7 Component: Compiler | Keywords: (LLVM) | Architecture: Unknown/Multiple Resolution: | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: Runtime | performance bug | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by thomie): * failure: None/Unknown => Runtime performance bug -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8321#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8321: improve basic block layout on LLVM backend by predicting stack/heap checks
-------------------------------------+-------------------------------------
Reporter: rwbarton | Owner: rwbarton
Type: feature request | Status: new
Priority: normal | Milestone:
Component: Compiler (LLVM) | Version: 7.7
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
Type of failure: Runtime | Unknown/Multiple
performance bug | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#8321: improve basic block layout on LLVM backend by predicting stack/heap checks -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: rwbarton Type: feature request | Status: closed Priority: normal | Milestone: 8.2.1 Component: Compiler (LLVM) | Version: 7.7 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: new => closed * resolution: => fixed * milestone: => 8.2.1 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8321#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC