Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRMC, reloaded #9760

Merged
merged 32 commits into from Nov 2, 2021
Merged

TRMC, reloaded #9760

merged 32 commits into from Nov 2, 2021

Commits on Nov 2, 2021

  1. Configuration menu
    Copy the full SHA
    a84b25b View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    35dff8c View commit details
    Browse the repository at this point in the history
  3. TMC: product representation of choices

    Before this commit the representation of choices was
    
        type choice =
        | Return of lambda
        | Set of { direct: lambda; dps: Dps.t }
    
    the idea being that Return means that only the direct-style form is
    useful (there are no TMC calls, so never a need for a specific
    DPS implementation).
    
    The new representation has the same information encoded as a product
    rather than a sum:
    
      type choice = {
        direct: lambda;
        dps: dps.t;
        has_tmc_calls: bool;
      }
    
    When `has_tmc_calls` is false, this is the same as `Return`, otherwise
    it is a `Set`. This new representation makes some of the code
    easier (more homogeneous, no need for case distinctions), and it
    avoids some bugs we had when considering how to mix Return and Set
    values together.
    Elarnon authored and gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    e0df0a1 View commit details
    Browse the repository at this point in the history
  4. TMC: code-generation tests

    These tests are less robust than the other TMC tests, but they test
    more varied aspect of the transformation, and will be useful to assess
    the impact of upcoming code changes during the code review period, and
    further improvements to the TMC transformation.
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    90dd724 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    e9397f6 View commit details
    Browse the repository at this point in the history
  6. TMC: optimize constructor composition

    Elarnon authored and gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    4d2d0b0 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    e30d162 View commit details
    Browse the repository at this point in the history
  8. TMC: Constructor composition in direct style: [benefits_from_dps : bo…

    …ol].
    
    The direct-style version of the tailmap3 function
    
        let[@tail_mod_cons] rec tailmap3 = fun f li ->
          match li with
          | [] -> []
          | x :: xs -> f 0 x :: f 1 x :: f 2 x :: tailmap3 f xs
    
    looked as follows before this PR:
    
        (tailmap3/109
           (function f/110 li/111 tail_mod_cons
             (if li/111
               (let (xs/113 =a (field 1 li/111) x/112 =a (field 0 li/111))
                 (let (block/115 = (makemutable 0 (apply f/110 0 x/112) 0))
                   (seq
                     (let
                       (arg1/116 = (apply f/110 1 x/112)
                        block/117 = (makemutable 0 (apply f/110 2 x/112) 0))
                       (seq
                         (setfield_ptr(heap-init)_computed block/115 1
                           (makeblock 0 arg1/116 block/117))
                         (apply tailmap3_dps/114 block/117 1 f/110 xs/113)))
                     block/115)))
               0a))
    
    It now looks as follows:
    
        (tailmap3/109
           (function f/110 li/111 trmc
             (if li/111
               (let (xs/113 =a (field 1 li/111) x/112 =a (field 0 li/111))
                 (makeblock 0 (apply f/110 0 x/112)
                   (makeblock 0 (apply f/110 1 x/112)
                     (let (block/115 = (makemutable 0 (apply f/110 2 x/112) 0))
                       (seq (apply tailmap3_dps/114 block/115 1 f/110 xs/113)
                         block/115)))))
    
    The idea is that the deeply nested constructor (f 2 x :: ...) needs
    to be turned into a location for the tailmap3 call to go to the DPS
    version, but the code around this constructor itself does not need
    to be put in DPS form (as the constructor is already providing
    a location). Advertising that it "does not benefit from DPS" let
    us generate the much nicer version below, by not trying to create
    new locations on each constructor above it.
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    10d756b View commit details
    Browse the repository at this point in the history
  9. TMC: error if several different calls could be optimized

    One reason to error in these ambiguous situations, instead of making
    a choice for the user, is that the TMC transformation changes the
    evaluation order, delaying the call that is made tailrec to after the
    evaluation of all other arguments.
    
    Example:
    
    ```
    type 'a tree =
    | Leaf of 'a
    | Node of 'a tree * 'a tree
    
    let[@tail_mod_cons] rec correct_map f = function
    | Leaf v -> Leaf (f v)
    | Node (left, right) ->
      Node (correct_map f left, (correct_map[@tailcall]) f right)
    
    let[@tail_mod_cons] rec incorrect_map f = function
    | Leaf v -> Leaf (f v)
    | Node (left, right) ->
      Node (incorrect_map f left, incorrect_map f right)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Error: [@tail_mod_cons]: this constructor application may be optimized in several different ways, please disambiguate by adding a [@tailcall] attribute to the call that should be made tail-recursive.
    ```
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    7dc0d86 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    0e04aa1 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    1b8771e View commit details
    Browse the repository at this point in the history
  12. TMC: testsuite

    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    640f246 View commit details
    Browse the repository at this point in the history
  13. TMC: some semantic preservation tests

    Elarnon authored and gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    41b9afc View commit details
    Browse the repository at this point in the history
  14. [refactoring] move Simplif.exact_application to Lambda

    We need to use this logic in lambda/tmc.ml to support tupled functions.
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    1daea33 View commit details
    Browse the repository at this point in the history
  15. TMC: support Tupled functions and partial applications

    Partial applications are obviously not considered TMC calls, but this
    is also the case for direct calls to Tupled functions that do not take
    a direct tuple literal, but another tuple value.
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    9242408 View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    f881fc4 View commit details
    Browse the repository at this point in the history
  17. [review] TMC: rename 'return' to 'lambda'

    There is a naming conflict between using 'return v' to mean
    - "build a piece of code that returns the same result as `v`", and
    - the `return` function of the applicative functor
    
    (Note that the first operation is not polymorphic: it has type
      lambda -> lambda t
    and not
      'a -> 'a t
    )
    
    To avoid this ambiguity we rename our 'return' functions into
    'lambda'.
    
    (Issue spotted by Pierre Chambart during review.)
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    7c17ea6 View commit details
    Browse the repository at this point in the history
  18. Configuration menu
    Copy the full SHA
    9c99520 View commit details
    Browse the repository at this point in the history
  19. [review] tmc: use a placeholder value that is better for debugging

    (suggestion from Konstantin Romanov)
    
    After discussion with Pierre Chambart, we convinced ourselves that the
    tagged forms is what users will see in their debugger, and went for
    0xBBBB / 2, which gets tagged as 0xBBBB.
    
    Co-Authored-By: Pierre Chambart
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    d92ef98 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    9306f65 View commit details
    Browse the repository at this point in the history
  21. Configuration menu
    Copy the full SHA
    333806b View commit details
    Browse the repository at this point in the history
  22. [review] new TMC test

    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    67251d9 View commit details
    Browse the repository at this point in the history
  23. Configuration menu
    Copy the full SHA
    9725d90 View commit details
    Browse the repository at this point in the history
  24. Configuration menu
    Copy the full SHA
    2dcd818 View commit details
    Browse the repository at this point in the history
  25. Configuration menu
    Copy the full SHA
    955528b View commit details
    Browse the repository at this point in the history
  26. Configuration menu
    Copy the full SHA
    d2a2dc9 View commit details
    Browse the repository at this point in the history
  27. [WIP] TMC: Changes

    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    e6fbcc8 View commit details
    Browse the repository at this point in the history
  28. TMC: much thinking about which @tailcall annotations to preserve where

    Co-Authored-By: Pierre Chambart
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    1b10dd9 View commit details
    Browse the repository at this point in the history
  29. Configuration menu
    Copy the full SHA
    4a338a8 View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    a862307 View commit details
    Browse the repository at this point in the history
  31. [minor] complete the renaming of 'TRMC' into 'TMC'

    (suggestion from Konstantin Romanov)
    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    0d80ae3 View commit details
    Browse the repository at this point in the history
  32. bow to check-typo

    gasche committed Nov 2, 2021
    Configuration menu
    Copy the full SHA
    0891d8e View commit details
    Browse the repository at this point in the history