box and val (c2 crash)

Remi Forax forax at
Thu Jan 3 15:14:22 UTC 2019

Now, i've tried to play with the .box/.val trying to implement the clojure Cursor protocol,
which iterate like it's an abstract linked list, so at each step either you return a new Cursor (a value type) or null at the end (which seems to be a good test for a nullable value type).

The following code compiles !
I had to had the fix method because the compier refuse to find any methods on and boxed = ...
doesn't compile too.

If you try to execute the code, it crashes the VM (in the C2 compiler thread it seems)

Current CompileTask:
C2:    338  119             fr.umlv.valuetype.perf.TupleLoopBenchMark::sum (46 bytes)

Stack: [0x00007ff4c0a37000,0x00007ff4c0b38000],  sp=0x00007ff4c0b34700,  free space=1013k
Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)
V  []  VMError::report_and_die(int, char const*, char const*, __va_list_tag*, Thread*, unsigned char*, void*, void*, char const*, int, unsigned long)+0x186
V  []  VMError::report_and_die(Thread*, void*, char const*, int, char const*, char const*, __va_list_tag*)+0x47
V  []  report_vm_error(char const*, int, char const*, char const*, ...) [clone .constprop.51]+0xee
V  []  TypePtr::xmeet_helper(Type const*) const+0x5e
V  []  TypePtr::xmeet(Type const*) const+0x10
V  []  PhiNode::Value(PhaseGVN*) const+0x131
V  []  PhaseGVN::transform_no_reclaim(Node*)+0x86
V  []  Parse::do_exits()+0x4bd
V  []  Parse::Parse(JVMState*, ciMethod*, float)+0xab6
V  []  ParseGenerator::generate(JVMState*)+0x88
V  []  Parse::do_call()+0x2dd
V  []  Parse::do_one_bytecode()+0x32e8
V  []  Parse::do_one_block()+0x197
V  []  Parse::do_all_blocks()+0x102
V  []  Parse::Parse(JVMState*, ciMethod*, float)+0xa70
V  []  ParseGenerator::generate(JVMState*)+0x88
V  []  Compile::Compile(ciEnv*, C2Compiler*, ciMethod*, int, bool, bool, bool, DirectiveSet*)+0xe98
V  []  C2Compiler::compile_method(ciEnv*, ciMethod*, int, DirectiveSet*)+0xe6
V  []  CompileBroker::invoke_compiler_on_method(CompileTask*)+0x3a2
V  []  CompileBroker::compiler_thread_loop()+0x608
V  []  JavaThread::thread_main_inner()+0xf8
V  []  Thread::call_run()+0x171
V  []  thread_native_entry(Thread*)+0xf8


  static value class Tuple {
    private final int index;
    private final int element;
    private Tuple(int index, int element) {
      this.index = index;
      this.element = element;
  static value class Cursor {
    private final int[] array;
    private final int index;
    private Cursor(int[] array, int index) {
      this.array = array;
      this.index = index;
    Tuple current() {
      return new Tuple(index, array[index]);
    } next() {
      if (index + 1 == array.length) {
        return null;
      return new Cursor(array, index + 1);
  private static indexedElements(int[] array) {
    if (array.length == 0) {
      return null;
    return new Cursor(array, 0);
  private static Cursor fix( cursor) {
    return cursor;
  public int sum() {
    int sum = 0;
    for( cursor = indexedElements(ARRAY); cursor != null; cursor = fix(cursor).next()) {
      Tuple tuple = fix(cursor).current();
      sum += tuple.index + tuple.element;
    return sum;

More information about the valhalla-dev mailing list