Skip to content
Snippets Groups Projects
sum_sample.hir 1.32 KiB
fn sum<1>(a: array(f32, #0)) -> f32
  zero_idx = constant(u64, 0)
  one_idx = constant(u64, 1)
  zero_inc = constant(f32, 0)
  bound = dynamic_constant(#0)
  loop = region(start, if_true)
  idx = phi(loop, zero_idx, idx_inc)
  idx_inc = add(idx, one_idx)
  red = phi(loop, zero_inc, red_add)
  read = read(a, position(idx))
  red_add = add(red, read)
  in_bounds = lt(idx_inc, bound)
  if = if(loop, in_bounds)
  if_false = projection(if, 0)
  if_true = projection(if, 1)
  r = return(if_false, red_add)

fn alt_sum<1>(a: array(f32, #0)) -> f32
  zero_idx = constant(u64, 0)
  one_idx = constant(u64, 1)
  two_idx = constant(u64, 2)
  zero_inc = constant(f32, 0)
  bound = dynamic_constant(#0)
  loop = region(start, if_true)
  idx = phi(loop, zero_idx, idx_inc)
  idx_inc = add(idx, one_idx)
  red = phi(loop, zero_inc, red_add)
  rem = rem(idx, two_idx)
  odd = eq(rem, one_idx)
  negate_if = if(loop, odd)
  negate_if_false = projection(negate_if, 0)
  negate_if_true = projection(negate_if, 1)
  negate_bottom = region(negate_if_false, negate_if_true)
  read = read(a, position(idx))
  read_neg = neg(read)
  read_phi = phi(negate_bottom, read, read_neg)
  red_add = add(red, read_phi)
  in_bounds = lt(idx_inc, bound)
  if = if(negate_bottom, in_bounds)
  if_false = projection(if, 0)
  if_true = projection(if, 1)
  r = return(if_false, red_add)