<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<meta content="text/html; charset=US-ASCII" http-equiv="Content-Type">
<title>
GitLab
</title>



<style>img {
max-width: 100%; height: auto;
}
</style>
</head>
<body>
<div class="content">

<h3>
Sebastian Graf pushed to branch wip/dmdanal-precise-exn-simpl
at <a href="https://gitlab.haskell.org/ghc/ghc">Glasgow Haskell Compiler / GHC</a>
</h3>
<h4>
Commits:
</h4>
<ul>
<li>
<strong><a href="https://gitlab.haskell.org/ghc/ghc/-/commit/a46287e3792426f2c07d2c96b1cc273267ae542f">a46287e3</a></strong>
<div>
<span>by Sebastian Graf</span>
<i>at 2020-04-29T18:35:52+02:00</i>
</div>
<pre class="commit-message" style="white-space: pre-wrap; margin: 0;">DmdAnal: Improve handling of precise exceptions

This patch does two things: Fix possible unsoundness in what was called
the "IO hack" and implement part 2.1 of the "fixing precise exceptions"
plan in
https://gitlab.haskell.org/ghc/ghc/wikis/fixing-precise-exceptions,
which, in combination with !2956, supersedes !3014 and !2525.

**IO hack**

The "IO hack" (which is a fallback to preserve precise exceptions
semantics and thus soundness, rather than some smart thing that
increases precision) is called `exprMayThrowPreciseException` now.
I came up with two testcases exemplifying possible unsoundness (if
twisted enough) in the old approach:

- `T13380d`: Demonstrating unsoundness of the "IO hack" when resorting
             to manual state token threading and direct use of primops.
             More details below.
- `T13380e`: Demonstrating unsoundness of the "IO hack" when we have
             Nested CPR. Not currently relevant, as we don't have Nested
             CPR yet.

Basically, the IO hack assumed that precise exceptions can only be
thrown from a case scrutinee of type `(# State# RealWorld, _ #)`. I
couldn't come up with a program using the `IO` abstraction that violates
this assumption. But it's easy to do so via manual state token threading
and direct use of primops, see `T13380d`. Also similar code might be
generated by Nested CPR in the (hopefully not too) distant future, see
`T13380e`. Hence, we now have a more careful test in `forcesRealWorld`
that passes `T13380{d,e}` (and will hopefully be robust to Nested CPR).

**Precise exceptions**

In #13380 and #17676 we saw that we didn't preserve precise exception
semantics in demand analysis. We fixed that with minimal changes in
!2956, but that was terribly unprincipled.

That unprincipledness resulted in a loss of precision, which is tracked
by these new test cases:

- `T13380b`: Regression in dead code elimination, because !2956 was too
             syntactic about `raiseIO#`
- `T13380c`: No need to apply the "IO hack" when the IO action may not
             throw a precise exception (and the existing IO hack doesn't
             detect that)

Fixing both issues in !3014 turned out to be too complicated and had
the potential to regress in the future. Hence we decided to only fix
`T13380b` and augment the `Divergence` lattice with a new middle-layer
element, `ExnOrDiv`, which means either `Diverges` (, throws an
imprecise exception) or throws a *precise* exception.

See the wiki page on Step 2.1 for more implementational details:
https://gitlab.haskell.org/ghc/ghc/wikis/fixing-precise-exceptions#dead-code-elimination-for-raiseio-with-isdeadenddiv-introducing-exnordiv-step-21
</pre>
</li>
</ul>
<h4>30 changed files:</h4>
<ul>
<li class="file-stats">
<a href="#d0d96a6d03668aeab20ebe05e2c4ccb798c7e64c">
compiler/GHC.hs
</a>
</li>
<li class="file-stats">
<a href="#451725cc4e5d443a3b7c2adcdf224840f953b7e2">
compiler/GHC/Builtin/primops.txt.pp
</a>
</li>
<li class="file-stats">
<a href="#8b1482a65a3092c268b853a00aa18286427d1dec">
compiler/GHC/Core/Arity.hs
</a>
</li>
<li class="file-stats">
<a href="#36a42448a83a9d1f6df8475f03ead2eed199dd8e">
compiler/GHC/Core/Lint.hs
</a>
</li>
<li class="file-stats">
<a href="#8e28acdbf4d8c043c7e5d55af312d10b2d8f16de">
compiler/GHC/Core/Opt/CallArity.hs
</a>
</li>
<li class="file-stats">
<a href="#8104fa1b71db6cfc4eb90cd769463d9eb9004619">
compiler/GHC/Core/Opt/DmdAnal.hs
</a>
</li>
<li class="file-stats">
<a href="#09072575aca2480de8c1966011972c529575ba5d">
compiler/GHC/Core/Opt/FloatIn.hs
</a>
</li>
<li class="file-stats">
<a href="#0debbb6150cac3b9f1027e430021269925103bb9">
compiler/GHC/Core/Opt/FloatOut.hs
</a>
</li>
<li class="file-stats">
<a href="#51fd81045296e6b42e5cf3cea9d36a47763c45e7">
compiler/GHC/Core/Opt/LiberateCase.hs
</a>
</li>
<li class="file-stats">
<a href="#cc763cdd1967f5d921161a32f64136cfcf0332c5">
compiler/GHC/Core/Opt/SetLevels.hs
</a>
</li>
<li class="file-stats">
<a href="#f168a93cde5e2aec2441d6331dfe500172df4af3">
compiler/GHC/Core/Opt/Simplify.hs
</a>
</li>
<li class="file-stats">
<a href="#48fbb5cdea308650de5756521feb28ec68819b9b">
compiler/GHC/Core/Opt/Simplify/Utils.hs
</a>
</li>
<li class="file-stats">
<a href="#b6a5ba32bafb8fbda933538b3007e755fef6f101">
compiler/GHC/Core/Opt/SpecConstr.hs
</a>
</li>
<li class="file-stats">
<a href="#f4421b4e35592648510c877ecf55b1af2b96dcee">
compiler/GHC/Core/Opt/WorkWrap/Utils.hs
</a>
</li>
<li class="file-stats">
<a href="#11ffe98a94d798427bc600e4fcfe899407536346">
compiler/GHC/Core/SimpleOpt.hs
</a>
</li>
<li class="file-stats">
<a href="#2811a7297b8aa206197ac1f5dabd0818e3c7ec5a">
compiler/GHC/Core/Unfold.hs
</a>
</li>
<li class="file-stats">
<a href="#1a7aba0daeafab195716dd25432479804a55ab60">
compiler/GHC/Core/Utils.hs
</a>
</li>
<li class="file-stats">
<a href="#d11203a9321005ba51a16d751c86f609d1f18c71">
compiler/GHC/Iface/Tidy.hs
</a>
</li>
<li class="file-stats">
<a href="#d09e98aae6ad2eb59d08eac7894b68c0ab820cda">
compiler/GHC/Types/Demand.hs
</a>
</li>
<li class="file-stats">
<a href="#20aaf8344f379f354fe31dd0c1c4db4ddc5b17aa">
compiler/GHC/Types/Id.hs
</a>
</li>
<li class="file-stats">
<a href="#261c0c5029eb34eb7436b087d77314f377ff8223">
compiler/GHC/Types/Id/Make.hs
</a>
</li>
<li class="file-stats">
<a href="#577323b744969d7ab63fbd7ff165a494b2d2d8a9">
testsuite/tests/stranal/should_compile/T10482a.stderr
</a>
</li>
<li class="file-stats">
<a href="#2d2548703c4a6e73bd0e6609702d5f6d8205d773">
testsuite/tests/stranal/should_compile/T10694.stderr
</a>
</li>
<li class="file-stats">
<a href="#42133bf5f411e5bde6cc4ac505b54dec83d9d359">
<span class="new-file">
+
testsuite/tests/stranal/should_compile/T13380b.hs
</span>
</a>
</li>
<li class="file-stats">
<a href="#ab93c58ebef7f2d807198123cbe82575c40b90f1">
testsuite/tests/stranal/should_compile/all.T
</a>
</li>
<li class="file-stats">
<a href="#c2c3737c7eb0ae6aee75ac72c35d1298307274f1">
<span class="new-file">
+
testsuite/tests/stranal/should_run/T13380d.hs
</span>
</a>
</li>
<li class="file-stats">
<a href="#e7dcdf58bca8dcb1e392216cb71d940363ddba18">
<span class="new-file">
+
testsuite/tests/stranal/should_run/T13380d.stderr
</span>
</a>
</li>
<li class="file-stats">
<a href="#23095d28504f0130b7374521e2b835ce4fcd59f5">
<span class="new-file">
+
testsuite/tests/stranal/should_run/T13380e.hs
</span>
</a>
</li>
<li class="file-stats">
<a href="#dec39370f254fbe290142670fd354260afce088e">
<span class="new-file">
+
testsuite/tests/stranal/should_run/T13380e.stderr
</span>
</a>
</li>
<li class="file-stats">
<a href="#fcabe0b768ce21a012d35e3434a69ded3f3024bd">
testsuite/tests/stranal/should_run/all.T
</a>
</li>
</ul>
<h5>The diff was not included because it is too large.</h5>

</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #777;">

<br>
<a href="https://gitlab.haskell.org/ghc/ghc/-/commit/a46287e3792426f2c07d2c96b1cc273267ae542f">View it on GitLab</a>.
<br>
You're receiving this email because of your account on gitlab.haskell.org.
If you'd like to receive fewer emails, you can
adjust your notification settings.
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Commit","url":"https://gitlab.haskell.org/ghc/ghc/-/commit/a46287e3792426f2c07d2c96b1cc273267ae542f"}}</script>


</p>
</div>
</body>
</html>