Skip to content

Commit

Permalink
Deploying to gh-pages from @ bb35155 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed Nov 12, 2024
1 parent ee25bfd commit 76932be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion search.js

Large diffs are not rendered by default.

55 changes: 42 additions & 13 deletions styx/ir/optimize.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,41 @@ <h1 class="modulename">

<label class="view-source-button" for="mod-optimize-view-source"><span>View Source</span></label>

<div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">import</span> <span class="nn">styx.ir.core</span> <span class="k">as</span> <span class="nn">ir</span>
<div class="pdoc-code codehilite"><pre><span></span><span id="L-1"><a href="#L-1"><span class="linenos"> 1</span></a><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Generator</span>
</span><span id="L-2"><a href="#L-2"><span class="linenos"> 2</span></a>
</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a>
</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a><span class="c1"># todo:</span>
</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a><span class="c1"># Likely optimizations:</span>
</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="c1"># - Find nested required=True repeated=False expressions and merge them</span>
</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="c1"># - Find neighbouring ConstantParameters in ExpressionSequences and merge them</span>
</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a><span class="c1"># - Find min 0 max 1 repetitions and convert them to required=False</span>
</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">:</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">:</span>
</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a> <span class="k">return</span> <span class="n">expr</span>
</span><span id="L-3"><a href="#L-3"><span class="linenos"> 3</span></a><span class="kn">import</span> <span class="nn">styx.ir.core</span> <span class="k">as</span> <span class="nn">ir</span>
</span><span id="L-4"><a href="#L-4"><span class="linenos"> 4</span></a>
</span><span id="L-5"><a href="#L-5"><span class="linenos"> 5</span></a>
</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="k">def</span> <span class="nf">_merge_string_tokens</span><span class="p">(</span><span class="n">interface</span><span class="p">:</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">:</span>
</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Merge neighbouring string literals in Carg tokens.&quot;&quot;&quot;</span>
</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a>
</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a> <span class="k">def</span> <span class="nf">_iter_cargs</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Generator</span><span class="p">[</span><span class="n">ir</span><span class="o">.</span><span class="n">Carg</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">interface</span><span class="o">.</span><span class="n">command</span><span class="o">.</span><span class="n">iter_params_recursively</span><span class="p">(</span><span class="kc">False</span><span class="p">):</span>
</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">body</span><span class="p">,</span> <span class="n">ir</span><span class="o">.</span><span class="n">Param</span><span class="o">.</span><span class="n">Struct</span><span class="p">):</span>
</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a> <span class="k">for</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">param</span><span class="o">.</span><span class="n">body</span><span class="o">.</span><span class="n">groups</span><span class="p">:</span>
</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a> <span class="k">yield from</span> <span class="n">group</span><span class="o">.</span><span class="n">cargs</span>
</span><span id="L-14"><a href="#L-14"><span class="linenos">14</span></a>
</span><span id="L-15"><a href="#L-15"><span class="linenos">15</span></a> <span class="k">for</span> <span class="n">carg</span> <span class="ow">in</span> <span class="n">_iter_cargs</span><span class="p">():</span>
</span><span id="L-16"><a href="#L-16"><span class="linenos">16</span></a> <span class="n">old_tokens</span> <span class="o">=</span> <span class="n">carg</span><span class="o">.</span><span class="n">tokens</span>
</span><span id="L-17"><a href="#L-17"><span class="linenos">17</span></a> <span class="n">new_tokens</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">ir</span><span class="o">.</span><span class="n">Param</span> <span class="o">|</span> <span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
</span><span id="L-18"><a href="#L-18"><span class="linenos">18</span></a> <span class="k">for</span> <span class="n">token</span> <span class="ow">in</span> <span class="n">old_tokens</span><span class="p">:</span>
</span><span id="L-19"><a href="#L-19"><span class="linenos">19</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_tokens</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span><span id="L-20"><a href="#L-20"><span class="linenos">20</span></a> <span class="n">new_tokens</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</span><span id="L-21"><a href="#L-21"><span class="linenos">21</span></a> <span class="k">continue</span>
</span><span id="L-22"><a href="#L-22"><span class="linenos">22</span></a> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">new_tokens</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="nb">str</span><span class="p">):</span>
</span><span id="L-23"><a href="#L-23"><span class="linenos">23</span></a> <span class="n">new_tokens</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="n">token</span>
</span><span id="L-24"><a href="#L-24"><span class="linenos">24</span></a> <span class="k">continue</span>
</span><span id="L-25"><a href="#L-25"><span class="linenos">25</span></a> <span class="n">new_tokens</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</span><span id="L-26"><a href="#L-26"><span class="linenos">26</span></a> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">old_tokens</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_tokens</span><span class="p">):</span>
</span><span id="L-27"><a href="#L-27"><span class="linenos">27</span></a> <span class="n">carg</span><span class="o">.</span><span class="n">tokens</span> <span class="o">=</span> <span class="n">new_tokens</span>
</span><span id="L-28"><a href="#L-28"><span class="linenos">28</span></a>
</span><span id="L-29"><a href="#L-29"><span class="linenos">29</span></a> <span class="k">return</span> <span class="n">interface</span>
</span><span id="L-30"><a href="#L-30"><span class="linenos">30</span></a>
</span><span id="L-31"><a href="#L-31"><span class="linenos">31</span></a>
</span><span id="L-32"><a href="#L-32"><span class="linenos">32</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">interface</span><span class="p">:</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">:</span>
</span><span id="L-33"><a href="#L-33"><span class="linenos">33</span></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Simplify IR without changing meaning.&quot;&quot;&quot;</span>
</span><span id="L-34"><a href="#L-34"><span class="linenos">34</span></a> <span class="n">interface</span> <span class="o">=</span> <span class="n">_merge_string_tokens</span><span class="p">(</span><span class="n">interface</span><span class="p">)</span>
</span><span id="L-35"><a href="#L-35"><span class="linenos">35</span></a> <span class="k">return</span> <span class="n">interface</span>
</span></pre></div>


Expand All @@ -73,18 +98,22 @@ <h1 class="modulename">
<div class="attr function">

<span class="def">def</span>
<span class="name">optimize</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">expr</span><span class="p">:</span> <span class="n"><a href="core.html#Interface">styx.ir.core.Interface</a></span></span><span class="return-annotation">) -> <span class="n"><a href="core.html#Interface">styx.ir.core.Interface</a></span>:</span></span>
<span class="name">optimize</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">interface</span><span class="p">:</span> <span class="n"><a href="core.html#Interface">styx.ir.core.Interface</a></span></span><span class="return-annotation">) -> <span class="n"><a href="core.html#Interface">styx.ir.core.Interface</a></span>:</span></span>

<label class="view-source-button" for="optimize-view-source"><span>View Source</span></label>

</div>
<a class="headerlink" href="#optimize"></a>
<div class="pdoc-code codehilite"><pre><span></span><span id="optimize-10"><a href="#optimize-10"><span class="linenos">10</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">expr</span><span class="p">:</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">:</span>
</span><span id="optimize-11"><a href="#optimize-11"><span class="linenos">11</span></a> <span class="k">return</span> <span class="n">expr</span>
<div class="pdoc-code codehilite"><pre><span></span><span id="optimize-33"><a href="#optimize-33"><span class="linenos">33</span></a><span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span><span class="n">interface</span><span class="p">:</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ir</span><span class="o">.</span><span class="n">Interface</span><span class="p">:</span>
</span><span id="optimize-34"><a href="#optimize-34"><span class="linenos">34</span></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Simplify IR without changing meaning.&quot;&quot;&quot;</span>
</span><span id="optimize-35"><a href="#optimize-35"><span class="linenos">35</span></a> <span class="n">interface</span> <span class="o">=</span> <span class="n">_merge_string_tokens</span><span class="p">(</span><span class="n">interface</span><span class="p">)</span>
</span><span id="optimize-36"><a href="#optimize-36"><span class="linenos">36</span></a> <span class="k">return</span> <span class="n">interface</span>
</span></pre></div>



<div class="docstring"><p>Simplify IR without changing meaning.</p>
</div>


</section>
</main>
Expand Down

0 comments on commit 76932be

Please sign in to comment.