Skip to content

FLambda particularly slow with large file consisting in top-level trivial aliases. #7630

Closed

Description

@vicuna

Original bug ID: 7630
Reporter: @ejgallego
Assigned to: @gasche
Status: resolved (set by @gasche on 2018-09-10T14:51:33Z)
Resolution: fixed
Priority: normal
Severity: minor
Platform: AMD64
OS: Linux
OS Version: Ubuntu 17.04
Version: 4.05.0
Fixed in version: 4.07.0
Category: back end (clambda to assembly)
Tags: flambda
Related to: #4074
Parent of: #5546 #6001 #7067 #7631

Bug description

Dear OCaml developers,

we have observed very slow compilation times with -flambda in the
context of the Coq project.

In particular, ocaml+flambda seems to struggle in a file that has a
few thousand top level trivial definitions of the form:

let a = b

where b is defined in a different module.

The file takes around 7 minutes and 10GiB of RAM, which is a problem.
Compilation time and memory seems significantly increased with regards
to the non-flambda OCaml compiler. The problem seems present in 4.03.0
and 4.05.0+trunk.

No command line option we have tried is able to improve this,
including -Oclassic and -rounds=0.

Steps to reproduce

I think that the best way to reproduce the bug is to compile Coq
master with a flambda-enabled compiler. You will reach the problematic
file, OrdersEx.v in a few minutes. The concrete bad '.ml' file that
Coq generates is available here https://ufile.io/kfcqi

Additional information

Pierre-Marie Pédrot profiled the compiler and he thinks that
ocamlopt is stuck at register allocation, in concrete at
Coloring.walk, but are lacking some more concrete data.

Thank you!

Activity

  1. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @gasche

    Good news: the upcoming 4.06 release will come with a linear-scan allocator to be used optionally (option -linscan) that may be helpful to avoid register-allocation-level bottlenecks (we had a couple such bottlenecks even with the non-flambda backend in the past, for example with code within Camlp4 (not machine-generated)).

  2. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Thanks Gabriel, note thou that we are not sure about the reg allocator causing the problem, given the characteristics of the file I found Pierre-Marie's finding surprising.

    Has linscan been already merged in 4.06 ?

  3. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Never mind, I found #375 , also Paul Steckler has reported the following in Coq's gitter:

    OK, the profile does have Coloring.walk pretty high, but also ....
    Set.mem and Set.bal are even higher
    and Interf.compare
    the highest item is the garbage-collector, not much to do about that
    and the caller of Set.mem is Flambda_invariants__declare_variable

  4. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @gasche

    That indeed looks like the graph-based register coloration algorithm having trouble -- I have seen similar memory profiles in the past. You should definitely try with -linscan the just-branched 4.06 release branch if you can (there should be an alpha/beta release soon-ish).

  5. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @gasche

    Another tool that can help, by the way, is the -dtimings option that displays pass-by-pass time (and, in recent versions, memory) usage for the compiler. It keeps improving over the OCaml version.

  6. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @psteckler

    I had created a smaller example to demonstrate the slowdown introduced by flambda. I wanted to see if the linear scan code would help, and installed OPAM 4.06.0+trunk+fp+flambda. I used the -linscan flag and got:

    Fatal error: exception Invalid_argument("index out of bounds")
    Raised by primitive operation at file "asmcomp/linscan.ml", line 111, characters 19-35
    Called from file "list.ml", line 100, characters 12-15
    Called from file "asmcomp/linscan.ml", line 115, characters 10-56
    Called from file "asmcomp/linscan.ml", line 169, characters 4-28
    Called from file "list.ml", line 100, characters 12-15
    Called from file "asmcomp/asmgen.ml", line 87, characters 4-32
    Called from file "utils/misc.ml", line 28, characters 20-27
    Re-raised at file "utils/misc.ml", line 28, characters 50-57

    I can file a separate bug report, if desired.

  7. vicuna commented on Sep 18, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Maybe the combination of flambda + linscan is not well tested. Anyways, Paul used -dtimings to get more data and indeed register allocation is taking a lot more time in the flambda case.

  8. vicuna commented on Sep 19, 2017

    @vicuna
    Author

    Comment author: @gasche

    Well, there goes my hope of -linscan fixing your issue :p At least we get a lot of testing by convincing Coq people to try our new options.

  9. vicuna commented on Sep 19, 2017

    @vicuna
    Author

    Comment author: @xclerc

    As noted in #7631, the linscan bug seems to be related to fp;
    could you try to build Coq with -linscan on a non-fp switch?

  10. vicuna commented on Sep 19, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Also a -dtimings for OrdersEx.v would be very useful, unfortunately I am away from my desktop so I'll take a while.

  11. vicuna commented on Sep 19, 2017

    @vicuna
    Author

    Comment author: @psteckler

    I assume it's not necessary to try a non-fp switch, since the bug with linscan has been fixed.

  12. vicuna commented on Sep 25, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Ok, so I am back to my desktop and for the example in #7631, I see:

    4.05.0+trunk+flambda -Oclassic:
    regalloc(sourcefile(b.ml)): 5.408s

    4.05.0:
    regalloc(sourcefile(b.ml)): 0.084s

    thus this is IMHO a bug.

  13. vicuna commented on Sep 25, 2017

    @vicuna
    Author

    Comment author: @gasche

    Again, you cannot conclude that there is a bug from these numbers, because how much work the register allocator has to do depends a lot on the size and shape of your basic blocks, which in turns depends a lot on how aggressively you inline in the previous passes.

    The compiler backend has changed its compilation strategies for structures (long series of "let" at the toplevel or between "struct .. end") several times in the past to avoid similar performance issues. However, I believe that these changes were performed at the bytecomp/translmod level, that is before the split between flambda and non-flambda backends (or bytecode), so it should apply to all backends. It may be the case that some flambda optimizations undo this work, and it might be the case that a not-too-invasive change could un-undo this.

    See related issues: MPR #4074, MPR #5546, MPR #6001, MPR #7067

  14. vicuna commented on Sep 25, 2017

    @vicuna
    Author

    Comment author: @alainfrisch

    flambda goes through Translmod.transl_implementation_flambda, which uses the "normal" compilation strategy for structures, not the "allocate then update slots" one. My understanding is that later passes of flambda extract back a structured representation of toplevel definitions and do specific specific with them, but I don't know whether it is supposed to avoid sending "monsters" to the register allocator.

  15. vicuna commented on Sep 25, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    My reasoning was based on the (likely wrong) assumption that flambda -Oclassic inlining should be the same that non-flambda. Quoting the manual:

    "-Oclassic: Make inlining decisions at the point of definition of a function rather than at the call site(s). This mirrors the behaviour of OCaml compilers not using Flambda"

    so I thought indeed some other reason different from inlining could be creating the slow behavior, but of course I am likely wrong :)

  16. 11 remaining items

  17. vicuna commented on Oct 12, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Alain, soon (TM) we will have a way to systematically test performance of Coq with different combinations of compiler flags, we'll make an announcement once the setup is ready.

  18. vicuna commented on Oct 12, 2017

    @vicuna
    Author

    Comment author: @alainfrisch

    we will have a way to systematically test performance of Coq with different combinations of compiler flags

    Looking forward to it!

    it seems to me that enabling -linscan by default is a bit too much.

    It depends on the numbers. If it makes compilation significantly faster without degrading runtime performance in any observable way, it's just "better", not "too much".

  19. vicuna commented on Oct 12, 2017

    @vicuna
    Author

    Comment author: @gasche

    Alain, I've already told Emilio that -linscan should be fine (not actually degrade performances in observable ways), but he's strong-willed to get the absolute best baseline performance. He is also, among the active Coq developers, the only one currently doing testing and benchmarking of cutting-edge OCaml-side changes, so be nice to him :-)

    (Remark: there is no problem compiling some files with -linscan but not others, so Coq could special-case some of its standard library to use it.)

  20. vicuna commented on Oct 12, 2017

    @vicuna
    Author

    Comment author: @alainfrisch

    Gabriel, I'm always nice to everyone :-)

    I'm just honestly interested in more real-world results about -linscan.

  21. vicuna commented on Oct 12, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    I think my view here is more of "things should work fine without any special options" than "let's get the best baseline performance".

    I am also very interested about the results of different flags!

  22. vicuna commented on Oct 13, 2017

    @vicuna
    Author

    Comment author: @xclerc

    Would it make sense to have a heuristics choosing
    the algorithm to be used for register allocation?

    (And then probably a -graphcoloring switch to
    enforce the use of graph coloring.)

  23. vicuna commented on Oct 27, 2017

    @vicuna
    Author

    Comment author: @chambart

    I made another PR #1455 that disables CSE
    for the toplevel function. This allows to compile that file in ~13 seconds on
    my laptop. -Oclassic isn't really faster on that file by the way. You can
    further reduce the compilation time by ~4 seconds using -dflambda-no-invariants
    If you do, please keep the invariants checks enabled in you CI builds as this
    helps uncover compiler bugs. Also there is no guarantee that this option will
    stay.

  24. vicuna commented on Oct 27, 2017

    @vicuna
    Author

    Comment author: @ejgallego

    Thank you very much Pierre, we will test the new PR ASAP.

  25. vicuna commented on Sep 10, 2018

    @vicuna
    Author

    Comment author: @ejgallego

    So far Coq has been working pretty well with FLambda and 4.07.0 for little more than a month; from our part this issue can be closed.

  26. vicuna commented on Sep 10, 2018

    @vicuna
    Author

    Comment author: @gasche

    Great! Marking this as resolved.

    (Can you give a representative (by which I mean "any you like among those you have seen in practice") number on the runtime speedup and compilation-time slowdown related to your particular preferred flambda settings?)

  27. vicuna commented on Sep 10, 2018

    @vicuna
    Author

    Comment author: @ejgallego

    (Can you give a representative (by which I mean "any you like among those you have seen in practice") number on the runtime speedup and compilation-time slowdown related to your particular preferred flambda settings?)

    We are still in the process of gathering more data (*), I think that
    typical speedups for Coq projects where Coq has been compiled with -O3
    -unbox-closures are in the order of 0-15%. Relevant projects such as
    Iris / LambdaRust and mathcomp are closer to the 15% IIRC.

    The speedup comes for free as there is no OCaml compiling involved.

    Compilation time in Gitlab CI is typically double, but a large part of
    it is due to the "native compute" overhead which could be saved for
    most users: https://gitlab.com/coq/coq/pipelines/29809722/builds

    Incidentally, a "bench" should appear here soon:
    https://ci.inria.fr/coq/job/benchmark-part-of-the-branch/512 ,
    however, it will run on 4.06.1 due to batteries not being available
    for 4.07 yet.

    (*) I'd like to have coq_dune ready before updating my little own
    bench scripts, so that's the bench timeline. I'll post the data ASAP
    as I get it, cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions