STG register mapping

Ben Gamari ben at smart-cactus.org
Mon Dec 18 20:07:45 UTC 2023


DashiellWill <dashiellwill at 163.com> writes:

> Hi all,
>
>
> I'm newbie at GHC, trying to read the source code and understand the design of GHC.
> Now I'm learning about the Haskell Execution Model, but now I'm a bit confused about the STG-machine Registers.
>
Hello!
>
> First, as shown in the GHC wiki e.g. https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/haskell-execution/registers ,
> the STG-machine has some virtual registers that are mapped to real registers, such as Base, Sp, SpLim, Hp, Rn, Fn and Dn,
> but unfortunately the mapping logic of the STG registers is not explained in detail here.
>
>
> Then I tried to read the source files of various platforms in rts/include/stg/MachRegs.
> At first I thought they were mapped using the callee saved registers, such as aarch64, ppc, loongarch64 and riscv64 platforms,
> but this seems broken on s390x and x86_64 platforms.
>
>
> so now I have two questions:
> 1. What determines the amounts of Rn, Fn and Dn for different platforms?
> As for Rn, aarch64 has 6 registers, loongarch64 has 5 registers, ppc has 10 registers, and riscv64 has 7 registers.
> As for Fn, both aarch64 and loongarch64 have 4 registers, and both ppc and riscv64 have 6 registers.
>
When possible we do try to map STG global registers to callee-saved
platform registers. The reason for this is that foreign calls must save
the contents of callee-saved registers, increasing the overhead of
unsafe foreign calls.

Naturally, callee-saved registers tend to be a fairly scarce resource
(e.g. the System V x86-64 ABI only provides r12-15, rsp, rbp, and rbx).
Consequently, sometimes there are tricky trade-offs that need to be
made.

It is quite possible that the current trade-off
is not ideal; perhaps mapping global registers to caller-saved registers
would be worthwhile, even if this means that foreign calls require a bit
more overhead. Afterall, most Haskell programs have significantly more
Haskell calls than foreign calls. It would be great if an interested
party would reconsider this decision and do some benchmarks.

> 2. What is the basic rule to assign the actual processor registers to
> the STG machine registers?

There isn't much of a rule; most of these assignments are just whatever
the original port author specified; the actual assignment really doesn't
matter.

I'd be happy to answer any further questions.

Cheers,

- Ben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20231218/56d6cd9e/attachment.sig>


More information about the ghc-devs mailing list