google-research / dex-lang

Research language for array processing in the Haskell/ML family
BSD 3-Clause "New" or "Revised" License
1.58k stars 107 forks source link

First inliner. #1170

Closed axch closed 1 year ago

axch commented 1 year ago

The inliner follows the architecture of the GHC inliner, as described in Secrets of the Glasgow Haskell Compiler Inliner, with a few notable differences:

This inliner also leaves at least one known Dex-specific opportunity on the table, one we might call "pointless-array elimination". To wit, consider an array whose body is an atom, xs = for i. <atom>. This array is not an atom itself, and will do work if materialized at runtime, allocating and filling the array. But, if it's inlined into positions where it is indexed, it becomes an atom, which we treat as zero work. Ergo, such an inlining is profitable in more situations than the current setup can identify, such as for j k. xs.k. (If the body of xs did work, inlining xs would duplicate that work. If xs wasn't indexed at all, then inlining would duplicate the work or array creation regardless of the body. And right now the inliner can't detect the situation where a binding does work if not beta-reduced but does no work if beta-reduced, partly because that situation does not arise in GHC core.)