Truffle: ImplicitCast and Type Specializations for TruffleSOM

Stefan Marr java at stefan-marr.de
Wed Dec 18 05:45:00 PST 2013


Hi:

In the attempt of avoiding boxing throughout TruffleSOM, I tried to push the type-based specialization a little further for ints, BigIntegers, doubles, and Strings.

Since implementing all possible combinations for all the type combinations of the primitive operations seems to be infeasible, I used @ImplicitCasts as in this example:

  @ImplicitCast
  public int castInteger(final SInteger i) {
    return i.getEmbeddedInteger();
  }

I restricted myself to introduce only conversion from boxed SOM types to Java types, because I thought that might be simpler to handle and avoids undesired conversions. Thus, the casts basically just unbox the SOM objects.

As a result, the primitives are rather nice now. See an example at the end. I really just need to implement specializations for the basic types, and SOM specific type promotions.

While I had the feeling that the overall code got nice, the result of this change is rather unexpected.

With the current code on the type-specialization branch (https://github.com/SOM-st/TruffleSOM/commits/type-specialization), I see a slowdown of 5x for the interpreted version. And the Graal-compiled version doesn’t actually run properly. It causes constant method invalidations.
I tried to use the following flags to make sense of it: -XX:+TraceDeoptimization -G:-TruffleBackgroundCompilation -G:+TraceTruffleCompilationDetails
However, I see only output that tells me about traps in OptimizedCallTarget::executeHelper, which doesn’t give me a lot of hints that I understand.

Any ideas what that could be the cause for such invalidations?

Thanks
Stefan

## MultiplicationPrim

public abstract class MultiplicationPrim extends ArithmeticPrim {
  public MultiplicationPrim(final SSymbol selector, final Universe universe) { super(selector, universe); }
  public MultiplicationPrim(final MultiplicationPrim node) { this(node.selector, node.universe); }


  @Specialization(order = 1, rewriteOn = ArithmeticException.class)
  public int doInteger(final int left, final int right) {
    return ExactMath.multiplyExact(left, right);
  }

  @Specialization(order = 2)
  public Object doBigInteger(final BigInteger left, final BigInteger right) {
    BigInteger result = left.multiply(right);
    return reduceToIntIfPossible(result);
  }

  @Specialization(order = 3)
  public double doDouble(final double left, final double right) {
    return left * right;
  }

  @Specialization(order = 10)
  public Object doInteger(final int left, final BigInteger right) {
    return doBigInteger(BigInteger.valueOf(left), right);
  }

  @Specialization(order = 11)
  public double doInteger(final int left, final double right) {
    return doDouble(left, right);
  }

  @Specialization(order = 12)
  public Object doBigInteger(final BigInteger left, final int right) {
    return doBigInteger(left, BigInteger.valueOf(right));
  }

  @Specialization(order = 13)
  public double doDouble(final double left, final int right) {
    return doDouble(left, (double) right);
  }
}

## Some Graal output

./mx.sh  --vm server vm -XX:+TraceDeoptimization -G:-TruffleBackgroundCompilation -G:+TraceTruffleCompilationDetails -Xbootclasspath/a:../som/build/classes:../som/libs/com.oracle.truffle.api.jar:../som/libs/com.oracle.truffle.api.dsl.jar som.vm.Universe -cp ../som/Smalltalk ../som/Examples/Benchmarks/BenchmarkHarness.som IntegerLoop 1 2 2000

Uncommon trap   bci=16 pc=153299396, relative_pc=1540, method=doInteger, speculation=0
Uncommon trap occurred in com.oracle.graal.truffle.OptimizedCallTarget::executeHelper (Graal: installedCodeName=HotSpotMethod<OptimizedCallTarget.executeHelper(PackedFrame, Arguments)>)  (@0x00000001092329c4) thread=6403 reason=null_assert|unreached0 action=reinterpret unloaded_class_index=-1 speculation=0
Uncommon trap   bci=0 pc=137952117, relative_pc=1397, method=executeHelper, speculation=0
Uncommon trap occurred in com.oracle.graal.truffle.OptimizedCallTarget::executeHelper (Graal: installedCodeName=HotSpotMethod<OptimizedCallTarget.executeHelper(PackedFrame, Arguments)>)  (@0x000000010838fb75) thread=6403 reason=null_assert|unreached0 action=none unloaded_class_index=-1 speculation=0
Uncommon trap   bci=61 pc=153208469, relative_pc=181, method=initializeFrame, speculation=0
Uncommon trap occurred in com.oracle.graal.truffle.OptimizedCallTarget::executeHelper (Graal: installedCodeName=HotSpotMethod<OptimizedCallTarget.executeHelper(PackedFrame, Arguments)>)  (@0x000000010921c695) thread=6403 reason=null_assert|unreached0 action=make_not_entrant unloaded_class_index=-1 speculation=0
[truffle] invalidated Method Integer>>#$block method:../som/Smalltalk//Integer.som:65 at 20e2cbe0 |Inv# 349                                     |Replace# 15
Uncommon trap   bci=0 pc=137952117, relative_pc=1397, method=executeHelper, speculation=0
Uncommon trap occurred in com.oracle.graal.truffle.OptimizedCallTarget::executeHelper (Graal: installedCodeName=HotSpotMethod<OptimizedCallTarget.executeHelper(PackedFrame, Arguments)>)  (@0x000000010838fb75) thread=6403 reason=null_assert|unreached0 action=none unloaded_class_index=-1 speculation=0
[truffle] invalidated Method IntegerLoop>>#$block method::../som/Examples/Benchmarks//IntegerLoop.som:30 at 10dba097 |Inv# 399                                     |Replace# 9
Uncommon trap   bci=0 pc=137952117, relative_pc=1397, method=executeHelper, speculation=0
Uncommon trap occurred in com.oracle.graal.truffle.OptimizedCallTarget::executeHelper (Graal: installedCodeName=HotSpotMethod<OptimizedCallTarget.executeHelper(PackedFrame, Arguments)>)  (@0x000000010838fb75) thread=6403 reason=null_assert|unreached0 action=none unloaded_class_index=-1 speculation=0







-- 
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax:   +32 2 629 3525



More information about the graal-dev mailing list