How to implement a loop with sum-reduce in the affine MLIR dialect

Hello.
Could you please tell me how can I implement (efficiently) a loop with sum-reduce in the affine dialect in MLIR.
I managed to do something like this:
%1 = memref.alloc() : memref<1000xf32> // the memref of the accumulator
affine.for %arg2 = 0 to 1000 {
%2 = affine.load %arg0[%arg2] : memref<1000xf32>
%3 = affine.load %1[%arg2] : memref<1000xf32>
%4 = arith.addf %3, %2 : f32
affine.store %4, %1[%arg2] : memref<1000xf32>
}
Note that I implemented the accumulator as a memref buffer.

  To be correct I need to change:
    affine.store %4, %1[%arg2] : memref<1000xf32>
  to:
    affine.store %4, %1[0] : memref<1000xf32>
 Also, I need to change 
    %3 = affine.load %1[%arg2] : memref<1000xf32>
  to:
    %3 = affine.load %1[0] : memref<1000xf32>

How can I make these changes? The code that generates the above (%3 = affine.load %1[%arg2] : memref<1000xf32>) is:
        auto loadedVal2 = nestedBuilder.create<mlir::AffineLoadOp>(loc, alloc, ivs);
 It seems I need to change the loop induction variables, ivs, to represent 0, if possible, instead of the induction variable. Is it possible? Can you help me here please?

Thank you very much,
Alex
PS: It seems the MLIR API has also an mlir::LoopReduction class. But I haven’t used this class because I don’t know how to use it - there are no examples on the Internet about it. Would you recommend using it?

You may want to use arith.constant so that we can put arbitrary constant values inside the loop.

The overall code may look like:

func.func @some_func(%arg0 : memref<1000xf32>) {
  %1 = memref.alloc() : memref<1000xf32> // the memref of the accumulato
  %zero = arith.constant 0 : index
  affine.for %arg2 = 0 to 1000 {
    %2 = affine.load %arg0[%arg2] : memref<1000xf32>
    %3 = affine.load %1[%zero] : memref<1000xf32>
    %4 = arith.addf %3, %2 : f32
    affine.store %4, %1[%zero] : memref<1000xf32>
  }
  return
}

If you just want to get the sum of the loop variables. using iter_args would be helpful. I’ve written about the way here before. I hope it depicts the solution to you as well.