Skip to content

Commit

Permalink
XXX: Implement histogram as showcase for AccumulateOp.
Browse files Browse the repository at this point in the history
  • Loading branch information
ingomueller-net committed Apr 3, 2023
1 parent 4c770f2 commit e94ba26
Showing 1 changed file with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// RUN: iterators-opt %s \
// RUN: -convert-iterators-to-llvm \
// RUN: -convert-states-to-llvm \
// RUN: -inline -decompose-iterator-states -canonicalize \
// RUN: -one-shot-bufferize="bufferize-function-boundaries allow-return-allocs" \
// RUN: -buffer-hoisting -buffer-deallocation \
// RUN: -expand-strided-metadata \
// RUN: -finalize-memref-to-llvm \
// RUN: -convert-scf-to-cf \
// RUN: -convert-func-to-llvm \
// RUN: -convert-scf-to-cf -convert-cf-to-llvm \
// RUN: -canonicalize \
// RUN: -convert-cf-to-llvm \
// RUN: | mlir-cpu-runner -e main -entry-point-result=void \
// RUN: | FileCheck %s

!struct_i32 = !llvm.struct<(i32)>
!struct_i32i32 = !llvm.struct<(i32, i32)>
!struct_i32i32i32i32 = !llvm.struct<(i32, i32, i32, i32)>
!struct_f32 = !llvm.struct<(f32)>

func.func private @init_sum_struct() -> !struct_i32 {
Expand Down Expand Up @@ -97,8 +104,61 @@ func.func @test_accumulate_avg_struct() {
return
}

func.func private @unpack_i32(%input : !struct_i32) -> i32 {
%i = llvm.extractvalue %input[0] : !struct_i32
return %i : i32
}

func.func private @accumulate_histogram(
%hist : tensor<4xi32>, %val : i32) -> tensor<4xi32> {
%idx = arith.index_cast %val : i32 to index
%oldCount = tensor.extract %hist[%idx] : tensor<4xi32>
%one = arith.constant 1 : i32
%newCount = arith.addi %oldCount, %one : i32
%newHist = tensor.insert %newCount into %hist[%idx] : tensor<4xi32>
return %newHist : tensor<4xi32>
}

func.func private @tensor_to_struct(%input : tensor<4xi32>) -> !struct_i32i32i32i32 {
%idx0 = arith.constant 0 : index
%idx1 = arith.constant 1 : index
%idx2 = arith.constant 2 : index
%idx3 = arith.constant 3 : index
%i0 = tensor.extract %input[%idx0] : tensor<4xi32>
%i1 = tensor.extract %input[%idx1] : tensor<4xi32>
%i2 = tensor.extract %input[%idx2] : tensor<4xi32>
%i3 = tensor.extract %input[%idx3] : tensor<4xi32>
%structu = llvm.mlir.undef : !struct_i32i32i32i32
%struct0 = llvm.insertvalue %i0, %structu[0] : !struct_i32i32i32i32
%struct1 = llvm.insertvalue %i1, %struct0[1] : !struct_i32i32i32i32
%struct2 = llvm.insertvalue %i2, %struct1[2] : !struct_i32i32i32i32
%struct3 = llvm.insertvalue %i3, %struct2[3] : !struct_i32i32i32i32
return %struct3 : !struct_i32i32i32i32
}

// CHECK-LABEL: test_accumulate_histogram
// CHECK-NEXT: (1, 2, 1, 0)
// CHECK-NEXT: -
func.func @test_accumulate_histogram() {
iterators.print("test_accumulate_histogram")
%input = "iterators.constantstream"()
{ value = [[0 : i32], [1 : i32], [1 : i32], [2 : i32]] }
: () -> (!iterators.stream<!struct_i32>)
%unpacked = "iterators.map"(%input) {mapFuncRef = @unpack_i32}
: (!iterators.stream<!struct_i32>) -> (!iterators.stream<i32>)
%init_value = arith.constant dense<[0, 0, 0, 0]> : tensor<4xi32>
%accumulated = iterators.accumulate(%unpacked, %init_value)
with @accumulate_histogram
: (!iterators.stream<i32>) -> !iterators.stream<tensor<4xi32>>
%transposed = "iterators.map"(%accumulated) {mapFuncRef = @tensor_to_struct}
: (!iterators.stream<tensor<4xi32>>) -> (!iterators.stream<!struct_i32i32i32i32>)
"iterators.sink"(%transposed) : (!iterators.stream<!struct_i32i32i32i32>) -> ()
return
}

func.func @main() {
call @test_accumulate_sum_struct() : () -> ()
call @test_accumulate_avg_struct() : () -> ()
call @test_accumulate_histogram() : () -> ()
return
}

0 comments on commit e94ba26

Please sign in to comment.