graalvm / simplelanguage

A simple example language built using the Truffle API.
http://openjdk.java.net/projects/graal/
Universal Permissive License v1.0
608 stars 198 forks source link

Specialize `SLDivNode`s for constant-at-runtime divisors #108

Closed CAFxX closed 3 years ago

CAFxX commented 3 years ago

With this change, that is really just meant as an example of an additional kind of use cases for @Cached, the following contrived example:

function main() {
  i = 0;
  j = 0;
  k = 1;
  while (j < 1000000000) {
    j = j + i/(100000000+k);
    i = i + 1;
    if (k == 1) {
      k = 3;
    } else {
      k = 1;
    }
  }
  return j;
}

runs on my machine in:

real    0m3.020s
user    0m2.241s
sys     0m0.107s

whereas before it took more than twice as long:

real    0m5.912s
user    0m5.175s
sys     0m0.082s

Incidentally, the resulting optimization capability (strength reduction of divisions with non-constant divisors at compile time) is AFAIK not available in any AOT compiler (gcc, clang, icc). There is a point to be made that such an optimization would be better implemented in graalvm itself, rather than sl.

graalvmbot commented 3 years ago

Hello Carlo Alberto Ferraris, thanks for contributing a PR to our project!

We use the Oracle Contributor Agreement to make the copyright of contributions clear. We don't have a record of you having signed this yet, based on your email address cafxx -(at)- strayorange -(dot)- com. You can sign it at that link.

If you think you've already signed it, please comment below and we'll check.

CAFxX commented 3 years ago

Signed

boris-spas commented 3 years ago

Hi @CAFxX , Please note that simplelanguage is not developed on this repository but rather as part of truffle on https://github.com/oracle/graal/tree/master/truffle, so PRs are to be provided to the main repository. This repo is just a static snapshot to be used as an example starting point for language developers.