2026-03-16 12:09:37 +00:00
|
|
|
module {
|
|
|
|
|
func.func @reduce(%buffer: memref<?*f32>, %lb: index, %ub: index, %step: index) -> f32 {
|
2026-01-19 13:59:43 +00:00
|
|
|
; Initial sum set to 0.
|
2026-03-16 12:09:37 +00:00
|
|
|
%sum.0 = arith.constant 0.0 : f32
|
2026-01-19 13:59:43 +00:00
|
|
|
; iter_args binds initial values to the loop's region arguments.
|
|
|
|
|
;%sum = "scf.for"(%lb, %ub, %step) -> (f32) {
|
2026-03-16 12:09:37 +00:00
|
|
|
cf.br ^for.entry(%lb: index, %sum.0: f32)
|
2026-01-19 13:59:43 +00:00
|
|
|
|
|
|
|
|
^for.entry(%iv: index, %sum.iter: f32):
|
2026-03-16 12:09:37 +00:00
|
|
|
%t = memref.load %buffer[%iv] : memref<?*f32>
|
|
|
|
|
%sum.next = arith.addf %sum.iter, %t : f32
|
2026-01-19 13:59:43 +00:00
|
|
|
|
2026-03-16 12:09:37 +00:00
|
|
|
cf.br ^for.cond(%iv: index, %sum.next: f32)
|
2026-01-19 13:59:43 +00:00
|
|
|
|
|
|
|
|
^for.cond(%iv2: index, %sum.next2: f32):
|
|
|
|
|
; Yield current iteration sum to next iteration %sum.iter or to %sum
|
|
|
|
|
; if final iteration.
|
2026-03-16 12:09:37 +00:00
|
|
|
%iv.next = arith.addi %iv, %step : index
|
|
|
|
|
%stop = arith.cmpi uge %iv.next, %ub : index
|
|
|
|
|
cf.br-cond %stop, ^for.end, ^for.entry(%iv.next: index, %sum.next: f32)
|
2026-01-19 13:59:43 +00:00
|
|
|
|
|
|
|
|
^for.end(%sum: f32):
|
2026-03-16 12:09:37 +00:00
|
|
|
func.return %sum : f32
|
|
|
|
|
}
|
|
|
|
|
}
|