gmp disentangling patches
duncan at well-typed.com
Sat Jun 13 15:54:39 EDT 2009
As you know I've been working this week on the issue of moving gmp from
the rts into the integer-gmp package. Some preparatory patches went in
on Thursday. I will describe the remaining bulk in this email...
Summary of the patch sets:
* Compiler changes for "foreign import prim"
* Preparatory changes in rts and base
* Updated integer-gmp package with local cmm primop code (using
"foreign import prim" rather than importing gmp primops from
* Removal of gmp primops from compiler and rts
These are in dependency order. The whole system still validates between
each set of changes.
What is not yet complete is the build system changes to cope with using
the bundled gmp rather than the system gmp. More on that below.
Compiler changes for "foreign import prim"
This patch set contains all the changes to the compiler to implement the
"foreign import prim" feature. It's the same patches as I posted
previously with a few extra to make the changes people suggested. In
particular to switch the safe/unsafe attribute and to require a new
Thu Jun 11 12:52:43 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Reverse the safe/unsafe requirement on foreign import prim
The safe/unsafe annotation doesn't currently mean anything for prim.
Just in case we decide it means something later it's better to stick
to using one or the other consistently. We decided that using safe
is the better one to require (and it's also the default).
Thu Jun 11 13:27:27 BST 2009 Ian Lynagh <igloo at earth.li>
* Add missing StgPrimCallOp case to isSimpleOp
Thu Jun 11 17:59:05 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Add missing StgPrimCallOp case in repCCallConv
We don't handle "foreign import prim" in TH stuff.
Thu Jun 11 21:26:47 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Require GHCForeignImportPrim for "foreign import prim"
In practise currently you also need UnliftedFFITypes, however
the restriction to just unlifted types may be lifted in future.
Preparatory changes in rts and base
These are some more preparatory patches. They export a few things from
the rts that we will need for the integer-gmp package.
Sat Jun 13 18:06:22 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Exports a few rts things we need for cmm code in external packages
In particular we need alloc_blocks and alloc_blocks_lim for MAYBE_GC.
The gmp cmm primops also use stg_ARR_WORDS_info.
Fri Jun 12 12:41:56 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Add and export rts_unsafeGetMyCapability from rts
We need this, or something equivalent, to be able to implement
stgAllocForGMP outside of the rts. That's because we want to use
allocateLocal which allocates from the given capability without
having to take any locks. In the gmp primops we're basically in
an unsafe foreign call, that is a context where we hold a current
capability. So it's safe for us to use allocateLocal. We just
need a way to get the current capability. The method to get the
current capability varies depends on whether we're using the
threaded rts or not. When stgAllocForGMP is built inside the rts
that's ok because we can do it conditionally on THREADED_RTS.
Outside the rts we need a single api we can call without knowing
if we're talking to a threaded rts or not, hence this addition.
It is also necessary to change gcdInt about a bit because while it's
type is only Int, it's currently implemented via gmp.
Fri Jun 12 15:29:51 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Redefine gcdInt to use gcdInteger rather than gcdInt# primop
The gcdInt# primop uses gmp internally, even though the interface is
just Int#. Since we want to get gmp out of the rts we cannot keep
gcdInt#, however it's also a bit odd for the integer package to export
something that doesn't actually use Integer in its interface. Using
gcdInteger is still not terribly satisfactory aesthetically. However
in the short-term it works and it is no slower since gcdInteger calls
gcdInt# for the special case of two small Integers.
Sat Jun 13 12:03:18 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Adjust for the changed location of gcdInt
Updated integer-gmp package with local cmm primop code
This has the bulk of the changes to use local .cmm and .c files to
implement the gmp primops. This .c and .cmm code is basically just a
copy of the code from the rts. It also includes the code to set the gmp
Note that while this validates, it's a slightly funny intermediate state
where we've got essentially identical primops in the rts and in the
integer-gmp package. Also, both the rts and integer-gmp set their own
gmp memory functions (which are also essentially identical).
Sat Jun 13 14:37:50 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Implement the gmp primops in the integer-gmp package using cmm
Thu Jun 11 14:40:57 BST 2009 Ian Lynagh <igloo at earth.li>
* Add rules for building .cmm files in libraries
Thu Jun 11 17:20:38 BST 2009 Ian Lynagh <igloo at earth.li>
* Put the CMM objects in the GHCi library too
Removal of gmp primops from compiler and rts
This finally removes gmp from the rts.
Sat Jun 13 15:24:10 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Remove the gmp/Integer primops from the compiler
The implementations are still in the rts.
Sat Jun 13 17:58:41 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Stop setting the gmp memory functions in the rts
and remove the implementations of stg(Alloc|Realloc|Dealloc)ForGMP
Sat Jun 13 20:18:51 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Remove the implementation of gmp primops from the rts
Sat Jun 13 20:19:56 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Stop building the rts against gmp
Nothing from gmp is used in the rts anymore.
However we also then need to generate a header locally in the
integer-gmp package because the required constants are not exported from
the rts header files any more. We also add a configure script to detect
Sat Jun 13 15:46:10 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Add a configure script and rely on local definitions of derived
And an optimisation:
Sat Jun 13 14:40:49 BST 2009 Duncan Coutts <duncan at well-typed.com>
* Tweak the small integer case of gcdInteger for better optimisation
The gcdInt function in the base package now calls gcdInteger with
two small integers. With this weak, the optimiser generates a base
gcdInt that directly calls the gcdInt# primop from this package.
This means there should be no additional overhead compared to when
the base gcdInt called the gcdInt# primop directly.
With the above patches everything works so long as you've got gmp
installed on the system. What is left is that the top level configure
and makefile need to arrange to call the configure (via the Cabal
configure) for the integer-gmp package and, if necessary, tell it where
to look for the gmp header files and library. In particular if we are to
use the bundled gmp.
One thing that is slightly tricky about the bundled case is that the
location where we look for gmp is different between build time and when
we install everything. The rts handled this by generating its installed
package info differently for the build and install cases.
No doubt there will be some changes required in the bindist case.
Review and testing much appreciated.
More information about the Cvs-ghc