effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Segfault (139) when filling a list with 1M entries

Open b-studios opened this issue 10 months ago • 4 comments

The following program segfaults in our LLVM backend:

def main() = {
  val l1 = fill(1000000, 1)
  println(l1.sum)
}

with

terminated by signal SIGSEGV (Address boundary error)

The example can be further simplified to

def main() = {
  def go(i: Int, acc: List[Int]): List[Int] =
    if (i < 1000000) {
      go(i + 1, Cons(1, acc))
    } else acc

  val l = go(0, Nil())

  ()
}

which does not have any other stdlib function calls and results in the following machine:

  def main_123750() = {
    def go_123752(i_123749 : Int, acc_123751 : Positive) = {
      let longLiteral_123829 = 1000000;
      let pureApp_123828 = infixLt_16465(i_123749, longLiteral_123829);
      tmp_123827 := pureApp_123828;
      switch tmp_123827 
        0 : { () =>
          return acc_123751
        }
        1 : { () =>
          let longLiteral_123831 = 1;
          let pureApp_123830 = infixAdd_16383(i_123749, longLiteral_123831);
          tmp_123823 := pureApp_123830;
          let longLiteral_123833 = 1;
          let pureApp_123832 = boxInt_16591(longLiteral_123833);
          tmp_123824 := pureApp_123832;
          let make_123834 = 1(tmp_123824, acc_123751);
          tmp_123825 := make_123834;
          i_123749 := tmp_123823, acc_123751 := tmp_123825;
          jump go_123752
        }
    };
    let make_123835 = 0();
    tmp_123826 := make_123835;
    val (l_123753 : Positive) = {
      let longLiteral_123837 = 0;
      i_123749 := longLiteral_123837, acc_123751 := tmp_123826;
      jump go_123752
    };
    let unitLiteral_123836 = 0();
    return unitLiteral_123836
  };
  jump main_123750

b-studios avatar Feb 19 '25 10:02 b-studios

Maybe this is something for our LLVM bug hunters @marvinborner and @MattisKra ?

b-studios avatar Feb 19 '25 10:02 b-studios

Maybe useful: This is an eraser stack overflow that doesn't happen with larger stack limits (e.g. ulimit -s unlimited)

marvinborner avatar Feb 19 '25 11:02 marvinborner

This explains everything, thanks @marvinborner. 1-2 years ago I discussed with @phischu how the erasers should look like and I wished that for cons-lists they would happen to be tail-recursive so that they do not stack overflow....

Well 1-2 years later we are here :)

Seems like the refunctionalized approach also has its significant downsides compared to the "work list" approach, where the eraser is one loop.

b-studios avatar Feb 19 '25 11:02 b-studios

The same will also apply to sharers, btw.

b-studios avatar Feb 19 '25 11:02 b-studios