diku-dk / futhark

:boom::computer::boom: A data-parallel functional programming language
http://futhark-lang.org
ISC License
2.41k stars 166 forks source link

Internal compiler error: unknown variable #2106

Closed nqpz closed 9 months ago

nqpz commented 9 months ago

The program

def f'' 'a (n: i64) (f: a -> i64) (a: a): [n]i64 = replicate n (f a)

def f (s: {n: i64}): [s.n]i64 =
  let f' 'a (f: a -> i64) (a: a): [s.n]i64 = f'' s.n f a
  in f' id 0

entry main (n: i64) = f {n}

compiled with futhark c from git produces this error:

BuilderT.lookupType: unknown variable d<{s.n}>_5121
Known variables: 
f_4499 a_4500 map_5123

CallStack (from HasCallStack):
  error, called at src/Futhark/Builder.hs:104:9 in futhark-0.26.0-inplace:Futhark.Builder
athas commented 9 months ago

Monomorphisation is probably to blame, as it produces a function with this form:

def f ((s: {n: i64})) : ?[d<{s.n}>].[d<{s.n}>]i64 =
  let (d<{s.n}>: i64) = s.n
  let f' ((f: i64 -> i64)) ((a: i64)): ?[d<{s.n}>].[d<{s.n}>]i64 =
    f'' d<{s.n}> f a
  in f' id 0

Note how d<{s.n}> is shadowed in the return type of f. That probably confuses defunctionalisation, which ultimately produces something bad.