diku-dk / futhark

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

Internal compiler error using `reduce_by_index` with `copy dest` as values #2058

Closed spaarmann closed 11 months ago

spaarmann commented 11 months ago

This code

entry problem [n] (arr: *[n]i64) : [n]i64 =
  reduce_by_index arr (+) 0 (iota n) (copy arr)

produces the following output when compiled with futhark cuda or futhark opencl:

Internal compiler error.  Please report this:
  https://github.com/diku-dk/futhark/issues
Type error after pass 'simplify':
In function entry_problem
When checking function body
In expression of statement
  {defunc_0_reduce_by_index_res_5267 : ({}, [n_5216]i64)}
Type error:
Variable arr_5217 referenced after being consumed.

futhark c works fine.

Tested on a recent nightly (0.26.0 (prerelease), git: 07fa0b447debb1501c3bc9593a4f0975685bc8eb) as well as 0.25.9.

I originally came across this working on the 2nd weekly assignment for DPP'23, the example above is what I reduced it to. Cosmin proposed in the lab that the copy was incorrectly eliminated by some optimization and suggested using something like map (+ (opaque 0)) arr instead of copy arr as a workaround, which does work.

nhey commented 11 months ago

I suspected this might be my fault, but the following change makes it fail also on v0.22.6. (Just reporting this in case it's useful for pinpointing the problem.)

entry problem [n] (arr: *[n]i64) : [n]i64 =
  let arr2 = copy arr
  in reduce_by_index arr (+) 0 (iota n) arr2

A prettier workaround for you would be to copy first, I think.

entry problem [n] (arr: *[n]i64) : [n]i64 =
  reduce_by_index (copy arr) (+) 0 (iota n) arr
athas commented 11 months ago

I'm OK with blaming Nikolaj if no one else volunteers.