Skip to content

Cmm arithmetic optimisations#17

Closed
stedolan wants to merge 3 commits intoocaml:trunkfrom
stedolan:linear-constant-opts
Closed

Cmm arithmetic optimisations#17
stedolan wants to merge 3 commits intoocaml:trunkfrom
stedolan:linear-constant-opts

Conversation

@stedolan
Copy link
Contributor

Some optimisations on Cmm expressions involving tagging or constants. Here's a (best-case) example:

let int_of_digits a b c = 
  100 * (Char.code a - Char.code '0') + 
   10 * (Char.code b - Char.code '0') +
    1 * (Char.code c - Char.code '0')

Trunk compiles this to:

 (+
   (+ (+ (* 200 (>>s (+ a/1020 -96) 1)) (* 20 (>>s (+ b/1021 -96) 1)))
     (* 2 (>>s (+ c/1022 -96) 1)))
   1)

This branch compiles this to:

(+ (+ (+ ( * a/1020 100) ( * b/1021 10)) c/1022) -10766)

Floating all of the constant operations, tags, etc. out of arithmetic means they can be merged into one addition.

Constant additions and tagging operations are moved out of
subexpressions when possible. Often they can be merged.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the name of the function, it is not obvious that the result is the tagged result minus one. You should probably move the 'incr_int' here.

@stedolan
Copy link
Contributor Author

Thanks for the quick response, sorry it took me a while to get back. I've fixed the things you pointed out.

A couple of days ago, you said that it might have O(n^2) behaviour. I'm fairly sure it doesn't: add_int only recurses when it actually finds constants, which will only happen at the root of an expression generated by these functions.

In a test, compiling a function f x = x + x + ... + x, I did observe O(n^2) runtimes. But add_int was only called O(n) times, and the problem exists on older versions of ocamlopt.

@chambart
Copy link
Contributor

I know, I deleted the comment for that reason... The quadratic behaviour for a long addition is probably due to register allocation (or more precisely constraints generation).

Now this patch looks ok to me.

@vouillon
Copy link
Member

It would be great to optimize logical operations as well.

@bobot
Copy link
Contributor

bobot commented Nov 11, 2014

This PR have been discussed during the last developers' meeting, where I had the pleasure to be. To sum-up:

  • Style: add bar at the start of match
  • Overflow? seems good, no obvious mistake
  • Use Sys.word_size.
  • For the record: This optimization can break other optimization like let-introduction of shared terms but it should be a general win.
  • OK for integration when style and Sys.word_size will be fixed.

@DemiMarie
Copy link
Contributor

Would it be possible for me to write & submit a fixed patch?

@stedolan
Copy link
Contributor Author

@drbo no objection from me! Sorry I haven't had a chance to fix this up recently.

Add | on initial match case, and use Sys.word_size
@stedolan
Copy link
Contributor Author

Finally fixed this, sorry everyone for the delay.

@gasche
Copy link
Member

gasche commented Feb 7, 2015

Merged in trunk@15820, thanks!

@gasche gasche closed this Feb 7, 2015
bactrian pushed a commit that referenced this pull request Feb 7, 2015
Constant additions and tagging operations are moved out of
subexpressions when possible. Often they can be merged.

From Stephen Dolan:
  #17

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15820 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
nojb pushed a commit to nojb/ocaml that referenced this pull request Apr 12, 2015
Constant additions and tagging operations are moved out of
subexpressions when possible. Often they can be merged.

From Stephen Dolan:
  ocaml#17

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15820 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
bactrian pushed a commit that referenced this pull request Apr 18, 2015
Constant additions and tagging operations are moved out of
subexpressions when possible. Often they can be merged.

From Stephen Dolan:
  #17

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15820 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
stedolan added a commit to stedolan/ocaml that referenced this pull request Aug 18, 2015
mshinwell referenced this pull request in mshinwell/ocaml Jul 1, 2016
lpw25 pushed a commit to lpw25/ocaml that referenced this pull request Aug 22, 2016
@stedolan stedolan deleted the linear-constant-opts branch March 10, 2017 17:05
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Feb 20, 2020
…alizer_promote

minor gc promote all finalizers objects
poechsel pushed a commit to poechsel/ocaml that referenced this pull request Jul 2, 2021
stedolan pushed a commit to stedolan/ocaml that referenced this pull request May 24, 2022
ce88833 Merge flambda-backend changes
b7506bb Revert "Cherry-pick of ocaml/ocaml 1eeb0e7 (ocaml#12)"
183f688 Add config option to enable/disable stack allocation (ocaml#22)
ee7c849 If both the type and mode of an ident are wrong, complain about the type. (ocaml#19)
44bade0 Allow submoding during module inclusion checks (ocaml#21)
de3bec9 Add subtyping between arrows of related modes (ocaml#20)
93d8615 Enable the local keywords even when the local extension is off (ocaml#18)
81dd85e Documentation for local allocations
b05519f Fix a GC bug in local stack scanning (ocaml#17)
9f879de Fix __FUNCTION__ (ocaml#15)
a78975e Optimise "include struct ... end" in more cases (ocaml#11134)
b819c66 Cherry-pick of ocaml/ocaml 1eeb0e7 (ocaml#12)
bb363d4 Optimise the allocation of optional arguments (ocaml#11)

git-subtree-dir: ocaml
git-subtree-split: ce88833
lpw25 pushed a commit to lpw25/ocaml that referenced this pull request Jun 21, 2022
The stack pointer was computed relative to the wrong local arena
OlivierNicole pushed a commit to OlivierNicole/ocaml that referenced this pull request Oct 22, 2022
Fix crash when using lifted module that contains macros
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
* Add the preview templates

* Add a tutorials index page

* Add a no_trailing_slash middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants