summaryrefslogtreecommitdiff
path: root/docs/users_guide
diff options
context:
space:
mode:
Diffstat (limited to 'docs/users_guide')
-rw-r--r--docs/users_guide/9.4.1-notes.rst27
-rw-r--r--docs/users_guide/editing-guide.rst33
-rw-r--r--docs/users_guide/using-optimisation.rst56
3 files changed, 97 insertions, 19 deletions
diff --git a/docs/users_guide/9.4.1-notes.rst b/docs/users_guide/9.4.1-notes.rst
index 31f09fa6bc..5cb2736547 100644
--- a/docs/users_guide/9.4.1-notes.rst
+++ b/docs/users_guide/9.4.1-notes.rst
@@ -38,6 +38,33 @@ Compiler
enabled the :extension:`UnboxedTuples` extension but didn't explicitly
enable :extension:`UnboxedSums` will continue to work without changes.
+- Constructed Product Result analysis (c.f. :ghc-flag:`-fcpr-anal`) has been
+ overhauled and will now unbox nestedly, if termination properties of the
+ function permit. This allows unboxing of constructed results returned by
+ ``IO`` actions. E.g.::
+
+ sumIO :: [Int] -> IO Int
+ sumIO [] = return 0
+ sumIO (x:xs) = do
+ r <- sumIO xs
+ return $! x + r
+
+ Note the use of ``$!``: Without it, GHC would be unable to see that evaluation
+ of ``r`` and ``x`` terminates (and rapidly, at that). An alternative would be to
+ evaluate both with a bang pattern or a ``seq``, but the ``return $! <res>``
+ idiom should work more reliably and needs less thinking.
+
+- Demand analysis (cf. :ghc-flag:`-fstrictness`) now integrates a
+ Boxity Analysis that tracks whether a function needs a parameter boxed. If
+ that is the case, the worker/wrapper transformation (cf.
+ :ghc-flag:`-fworker-wrapper`) will not unbox that parameter, leading to less
+ reboxing in many cases.
+
+ For reasons of backwards-compatible performance, you may find that the new
+ mechanism is too aggressive in a few cases (e.g., still unboxing a parameter
+ that is used boxed in a hot path). Do post a bug report with your example!
+ Then wrap the uses of the parameter in ``GHC.Exts.lazy`` for a short-term fix.
+
``base`` library
~~~~~~~~~~~~~~~~
diff --git a/docs/users_guide/editing-guide.rst b/docs/users_guide/editing-guide.rst
index 737abd3b2e..b4720e1a71 100644
--- a/docs/users_guide/editing-guide.rst
+++ b/docs/users_guide/editing-guide.rst
@@ -6,15 +6,15 @@ for the Glasgow Haskell Compiler. Even more than this, it at times serves (for
better or for worse) as a de-facto language standard, being the sole
non-academic reference for many widely used language extensions.
-Since GHC 8.0, the User's Guide is authored in `ReStructuredText
-<https://en.wikipedia.org/wiki/ReStructuredText>`__ (or ReST or RST, for short)
+Since GHC 8.0, the User's Guide is authored in `reStructuredText
+<https://en.wikipedia.org/wiki/ReStructuredText>`__ (or reST or RST, for short)
a rich but light-weight mark-up language aimed at producing documentation. The
-`Sphinx <http://sphinx-doc.org/>`__ tool is used to produce the final PDF and
+`Sphinx <https://www.sphinx-doc.org/>`__ tool is used to produce the final PDF and
HTML documentation.
-This document (also written in ReST) serves as a brief introduction to ReST and to
+This document (also written in reST) serves as a brief introduction to reST and to
document the conventions used in the User's Guide. This document is *not* intended
-to be a thorough guide to ReST. For this see the resources referenced
+to be a thorough guide to reST. For this see the resources referenced
`below <#references>`__.
Basics
@@ -91,7 +91,7 @@ The above would be rendered as,
Headings
~~~~~~~~
-While ReST can accommodate a wide range of heading styles, we have standardized
+While reST can accommodate a wide range of heading styles, we have standardized
on this convention in the User's Guide,
.. code-block:: rest
@@ -141,7 +141,7 @@ Other languages
Double-colon blocks are syntax-highlighted as Haskell by default. To avoid this
use a
``.. code-block`` `directive
-<http://sphinx-doc.org/markup/code.html#directive-code-block>`__ with explicit
+<https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block>`__ with explicit
language designation,
.. code-block:: rest
@@ -314,7 +314,7 @@ There are a number of admonitions types,
* warning
-.. _Admonitions: http://docutils.sourceforge.net/docs/ref/rst/directives.html#admonitions
+.. _Admonitions: https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions
Documenting command-line options and GHCi commands
--------------------------------------------------
@@ -420,16 +420,17 @@ user-substitutable tokens. In this document we use the convention, ``⟨subst⟩
(note that these are angle brackets, ``U+27E8`` and ``U+27E9``, not
less-than/greater-than signs).
+.. _references:
-ReST reference materials
+reST reference materials
------------------------
-* `Sphinx ReST Primer`_: A great place to start.
-* `Sphinx extensions`_: How Sphinx extends ReST
-* `ReST reference`_: When you really need the details.
+* `Sphinx reST Primer`_: A great place to start.
+* `Sphinx extensions`_: How Sphinx extends reST
+* `reST reference`_: When you really need the details.
* `Directives reference`_
-.. _Sphinx ReST Primer: http://sphinx-doc.org/rest.html
-.. _ReST reference: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
-.. _Sphinx extensions: http://sphinx-doc.org/markup/index.html
-.. _Directives reference: http://docutils.sourceforge.net/docs/ref/rst/directives.html#code
+.. _Sphinx reST Primer: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
+.. _reST reference: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html
+.. _Sphinx extensions: https://www.sphinx-doc.org/en/master/usage/extensions/index.html
+.. _Directives reference: https://docutils.sourceforge.io/docs/ref/rst/directives.html#code
diff --git a/docs/users_guide/using-optimisation.rst b/docs/users_guide/using-optimisation.rst
index 37e4a4aae6..139031ad1c 100644
--- a/docs/users_guide/using-optimisation.rst
+++ b/docs/users_guide/using-optimisation.rst
@@ -247,7 +247,7 @@ by saying ``-fno-wombat``.
:default: on
Enables some control flow optimisations in the Cmm code
- generator, merging basic blocks and avoiding jumps right after jumps.
+ generator, merging basic blocks and avoiding jumps right after jumps.
.. ghc-flag:: -fasm-shortcutting
:shortdesc: Enable shortcutting on assembly. Implied by :ghc-flag:`-O2`.
@@ -319,14 +319,64 @@ by saying ``-fno-wombat``.
block layout behaves the same as in 8.6 and earlier.
.. ghc-flag:: -fcpr-anal
- :shortdesc: Turn on CPR analysis in the demand analyser. Implied by :ghc-flag:`-O`.
+ :shortdesc: Turn on Constructed Product Result analysis. Implied by :ghc-flag:`-O`.
:type: dynamic
:reverse: -fno-cpr-anal
:category:
:default: on
- Turn on CPR analysis in the demand analyser.
+ Turn on CPR analysis, which enables the worker/wrapper transformation (cf.
+ :ghc-flag:`-fworker-wrapper`) to unbox the result of a function, such as ::
+
+ sum :: [Int] -> Int
+ sum [] = 0
+ sum (x:xs) = x + sum xs
+
+ CPR analysis will see that each code path produces a *constructed product*
+ such as ``I# 0#`` in the first branch (where ``GHC.Exts.I#`` is the data
+ constructor of ``Int``, boxing up the the primitive integer literal ``0#``
+ of type ``Int#``) and optimise to ::
+
+ sum xs = I# ($wsum xs)
+ $wsum [] = 0#
+ $wsum (I# x:xs) = x# +# $wsum xs
+
+ and then ``sum`` can inline to potentially cancel away the ``I#`` box.
+
+ Here's an example of the function that *does not* return a constructed product: ::
+
+ f :: [Int] -> (Int -> Int) -> Int
+ f [] g = g 0
+ f (x:xs) g = x + f xs g
+
+ The expression ``g 0`` is not a constructed product, because we don't know
+ anything about ``g``.
+
+ CPR analysis also works *nestedly*, for example ::
+
+ sumIO :: [Int] -> IO Int
+ sumIO [] = return 0
+ sumIO (x:xs) = do
+ r <- sumIO xs
+ return $! x + r
+
+ Note the use of ``$!``: Without it, GHC would be unable to see that evaluation
+ of ``r`` and ``x`` terminates (and rapidly, at that). An alternative would be to
+ evaluate both with a bang pattern or a ``seq``, but the ``return $! <res>``
+ idiom should work more reliably and needs less thinking. The above example
+ will be optimised to ::
+
+ sumIO :: [Int] -> IO Int
+ sumIO xs = IO $ \s -> case $wsum xs s of
+ (# s', r #) -> (# s', I# r #)
+ $wsumIO :: [Int] -> (# RealWorld#, Int# #)
+ $wsumIO [] s = (# s, 0# #)
+ $wsumIO (I# x:xs) s = case $wsumIO xs of
+ (# s', r #) -> (# s', x +# r#)
+
+ And the latter can inline ``sumIO`` and cancel away the ``I#`` constructor.
+ Unboxing the result of a ``State`` action should work similarly.
.. ghc-flag:: -fcse
:shortdesc: Enable common sub-expression elimination. Implied by :ghc-flag:`-O`.