Skip to content

Commit 17ed75c

Browse files
XXX: Implement histogram as showcase for AccumulateOp.
1 parent d49ed91 commit 17ed75c

File tree

1 file changed

+62
-2
lines changed
  • experimental/iterators/test/Integration/Dialect/Iterators/CPU

1 file changed

+62
-2
lines changed

experimental/iterators/test/Integration/Dialect/Iterators/CPU/accumulate.mlir

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
// RUN: -convert-iterators-to-llvm \
33
// RUN: -decompose-iterator-states \
44
// RUN: -decompose-tuples \
5+
// RUN: -inline -canonicalize \
6+
// RUN: -one-shot-bufferize="allow-return-allocs" \
7+
// RUN: -buffer-hoisting \
8+
// RUN: -buffer-deallocation \
9+
// RUN: -convert-bufferization-to-memref \
10+
// RUN: -expand-strided-metadata \
11+
// RUN: -finalize-memref-to-llvm \
12+
// RUN: -convert-scf-to-cf \
513
// RUN: -convert-func-to-llvm \
6-
// RUN: -convert-scf-to-cf -convert-cf-to-llvm \
7-
// RUN: | mlir-cpu-runner -e main -entry-point-result=void \
14+
// RUN: -canonicalize \
15+
// RUN: -convert-cf-to-llvm \
16+
// RUN: | mlir-cpu-runner \
17+
// RUN: -e main -entry-point-result=void \
18+
// RUN: -shared-libs=%mlir_lib_dir/libmlir_c_runner_utils%shlibext \
819
// RUN: | FileCheck %s
920

1021
func.func private @accumulate_sum_tuple(
@@ -72,8 +83,57 @@ func.func @test_accumulate_avg_tuple() {
7283
return
7384
}
7485

86+
func.func private @unpack_i32(%input : tuple<i32>) -> i32 {
87+
%i = tuple.to_elements %input : tuple<i32>
88+
return %i : i32
89+
}
90+
91+
func.func private @accumulate_histogram(
92+
%hist : tensor<4xi32>, %val : i32) -> tensor<4xi32> {
93+
%idx = arith.index_cast %val : i32 to index
94+
%oldCount = tensor.extract %hist[%idx] : tensor<4xi32>
95+
%one = arith.constant 1 : i32
96+
%newCount = arith.addi %oldCount, %one : i32
97+
%newHist = tensor.insert %newCount into %hist[%idx] : tensor<4xi32>
98+
return %newHist : tensor<4xi32>
99+
}
100+
101+
func.func private @tensor_to_struct(%input : tensor<4xi32>) -> tuple<i32, i32, i32, i32> {
102+
%idx0 = arith.constant 0 : index
103+
%idx1 = arith.constant 1 : index
104+
%idx2 = arith.constant 2 : index
105+
%idx3 = arith.constant 3 : index
106+
%i0 = tensor.extract %input[%idx0] : tensor<4xi32>
107+
%i1 = tensor.extract %input[%idx1] : tensor<4xi32>
108+
%i2 = tensor.extract %input[%idx2] : tensor<4xi32>
109+
%i3 = tensor.extract %input[%idx3] : tensor<4xi32>
110+
%tuple = tuple.from_elements %i0, %i1, %i2, %i3 : tuple<i32, i32, i32, i32>
111+
return %tuple : tuple<i32, i32, i32, i32>
112+
}
113+
114+
// CHECK-LABEL: test_accumulate_histogram
115+
// CHECK-NEXT: (1, 2, 1, 0)
116+
// CHECK-NEXT: -
117+
func.func @test_accumulate_histogram() {
118+
iterators.print("test_accumulate_histogram")
119+
%input = "iterators.constantstream"()
120+
{ value = [[0 : i32], [1 : i32], [1 : i32], [2 : i32]] }
121+
: () -> (!iterators.stream<tuple<i32>>)
122+
%unpacked = "iterators.map"(%input) {mapFuncRef = @unpack_i32}
123+
: (!iterators.stream<tuple<i32>>) -> (!iterators.stream<i32>)
124+
%init_value = arith.constant dense<[0, 0, 0, 0]> : tensor<4xi32>
125+
%accumulated = iterators.accumulate(%unpacked, %init_value)
126+
with @accumulate_histogram
127+
: (!iterators.stream<i32>) -> !iterators.stream<tensor<4xi32>>
128+
%transposed = "iterators.map"(%accumulated) {mapFuncRef = @tensor_to_struct}
129+
: (!iterators.stream<tensor<4xi32>>) -> (!iterators.stream<tuple<i32, i32, i32, i32>>)
130+
"iterators.sink"(%transposed) : (!iterators.stream<tuple<i32, i32, i32, i32>>) -> ()
131+
return
132+
}
133+
75134
func.func @main() {
76135
call @test_accumulate_sum_tuple() : () -> ()
77136
call @test_accumulate_avg_tuple() : () -> ()
137+
call @test_accumulate_histogram() : () -> ()
78138
return
79139
}

0 commit comments

Comments
 (0)