<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>RefFlatten</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css">
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20201002</a>
</div>
</div>
<div id="header">
<h1>RefFlatten</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="RefFlatten">RefFlatten</a> is an optimization pass for the <a href="SSA2">SSA2</a>
<a href="IntermediateLanguage">IntermediateLanguage</a>, invoked from <a href="SSA2Simplify">SSA2Simplify</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
<div class="paragraph"><p>This pass flattens a <span class="monospaced">ref</span> cell into its containing object.
The idea is to replace, where possible, a type like</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>(int ref * real)</pre>
</div></div>
<div class="paragraph"><p>with a type like</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>(int[m] * real)</pre>
</div></div>
<div class="paragraph"><p>where the <span class="monospaced">[m]</span> indicates a mutable field of a tuple.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation">Implementation</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="https://github.com/MLton/mlton/blob/master/mlton/ssa/ref-flatten.fun"><span class="monospaced">ref-flatten.fun</span></a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_details_and_notes">Details and Notes</h2>
<div class="sectionbody">
<div class="paragraph"><p>The savings is obvious, I hope.  We avoid an extra heap-allocated
object for the <span class="monospaced">ref</span>, which in the above case saves two words.  We
also save the time and code for the extra indirection at each get and
set.  There are lots of useful data structures (singly-linked and
doubly-linked lists, union-find, Fibonacci heaps, &#8230;) that I believe
we are paying through the nose right now because of the absence of ref
flattening.</p></div>
<div class="paragraph"><p>The idea is to compute for each occurrence of a <span class="monospaced">ref</span> type in the
program whether or not that <span class="monospaced">ref</span> can be represented as an offset of
some object (constructor or tuple).  As before, a unification-based
whole-program with deep abstract values makes sure the analysis is
consistent.</p></div>
<div class="paragraph"><p>The only syntactic part of the analysis that remains is the part that
checks that for a variable bound to a value constructed by <span class="monospaced">Ref_ref</span>:</p></div>
<div class="ulist"><ul>
<li>
<p>
the object allocation is in the same block.  This is pretty
draconian, and it would be nice to generalize it some day to allow
flattening as long as the <span class="monospaced">ref</span> allocation and object allocation "line
up one-to-one" in the same loop-free chunk of code.
</p>
</li>
<li>
<p>
updates occur in the same block (and hence it is safe-for-space
because the containing object is still alive).  It would be nice to
relax this to allow updates as long as it can be provedthat the
container is live.
</p>
</li>
</ul></div>
<div class="paragraph"><p>Prevent flattening of <span class="monospaced">unit ref</span>-s.</p></div>
<div class="paragraph"><p><a href="RefFlatten">RefFlatten</a> is safe for space.  The idea is to prevent a <span class="monospaced">ref</span>
being flattened into an object that has a component of unbounded size
(other than possibly the <span class="monospaced">ref</span> itself) unless we can prove that at
each point the <span class="monospaced">ref</span> is live, then the containing object is live too.
I used a pretty simple approximation to liveness.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
