-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lowering for TensorToStreamOp to LLVM.
- Loading branch information
1 parent
2dcd56b
commit 94bf5be
Showing
4 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
experimental/iterators/test/Conversion/IteratorsToLLVM/tensor-to-stream.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// RUN: iterators-opt %s \ | ||
// RUN: -convert-iterators-to-llvm \ | ||
// RUN: | FileCheck --enable-var-scope %s | ||
|
||
// CHECK-LABEL: func private @iterators.tensor_to_stream.close.{{[0-9]+}}( | ||
// CHECK-SAME: %[[ARG0:.*]]: !iterators.state<index, [[tensorType:.*]]>) -> | ||
// CHECK-SAME: !iterators.state<index, [[tensorType]]> { | ||
// CHECK-NEXT: return %[[arg0:.*]] : !iterators.state<index, [[tensorType]]> | ||
// CHECK-NEXT: } | ||
|
||
// CHECK-LABEL: func private @iterators.tensor_to_stream.next.{{[0-9]+}}( | ||
// CHECK-SAME: %[[ARG0:.*]]: !iterators.state<index, [[tensorType:.*]]>) -> | ||
// CHECK-SAME: (!iterators.state<index, [[tensorType]]>, i1, [[tensorSliceType:.*]]>) { | ||
// CHECK-NEXT: %[[V0:.*]] = iterators.extractvalue %[[arg0:.*]][0] : !iterators.state<index, [[tensorType]]> | ||
// CHECK-NEXT: %[[V1:.*]] = iterators.extractvalue %[[arg0]][1] : !iterators.state<index, [[tensorType]]> | ||
// CHECK-NEXT: %[[Vx:.*]] = arith.constant 0 : index | ||
// CHECK-NEXT: %[[V2:.*]] = tensor.dim %[[V1]], %[[Vx]] : [[tensorType]] | ||
// CHECK-NEXT: %[[V3:.*]] = arith.cmpi slt, %[[V0]], %[[V2]] : index | ||
// CHECK-NEXT: %[[V4:.*]]:2 = scf.if %[[V3]] -> (!iterators.state<index, [[tensorType]]>, [[tensorSliceType]]>) { | ||
// CHECK-NEXT: %[[C1:.*]] = arith.constant 2 : index | ||
// CHECK-NEXT: %[[V5:.*]] = arith.addi %[[C1]], %[[V0]] : index | ||
// CHECK-NEXT: %[[V6:.*]] = iterators.insertvalue %[[V5]] into %[[arg0]][0] : !iterators.state<index, [[tensorType]]> | ||
// CHECK-NEXT: %[[V7:.*]] = tensor.extract_slice %[[V1]][%[[V0]]] [2] [1] : [[tensorType]] to [[tensorSliceType]]> | ||
// CHECK-NEXT: scf.yield %[[V6]], %[[V7]] : !iterators.state<index, [[tensorType]]>, [[tensorSliceType]]> | ||
// CHECK-NEXT: } else { | ||
// CHECK-NEXT: %[[V5:.*]] = tensor.extract_slice %[[V1]][0] [2] [1] : [[tensorType]] to [[tensorSliceType]]> | ||
// CHECK-NEXT: scf.yield %[[arg0]], %[[V5]] : !iterators.state<index, [[tensorType]]>, [[tensorSliceType]]> | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %[[V4]]#0, %[[V3]], %[[V4]]#1 : !iterators.state<index, [[tensorType]]>, i1, [[tensorSliceType]]> | ||
// CHECK-NEXT: } | ||
|
||
// CHECK-LABEL: func private @iterators.tensor_to_stream.open.{{[0-9]+}}( | ||
// CHECK-SAME: %[[ARG0:.*]]: !iterators.state<index, [[tensorType:.*]]>) -> | ||
// CHECK-SAME: !iterators.state<index, [[tensorType]]> { | ||
// CHECK-NEXT: %[[V0:.*]] = arith.constant 0 : index | ||
// CHECK-NEXT: %[[V1:.*]] = iterators.insertvalue %[[V0]] into %[[ARG0]][0] : !iterators.state<index, [[tensorType]]> | ||
// CHECK-NEXT: return %[[V1]] : !iterators.state<index, [[tensorType]]> | ||
// CHECK-NEXT: } | ||
|
||
func.func @main(%tensor : tensor<?xi32>) { | ||
// CHECK-LABEL: func.func @main( | ||
// CHECK-SAME: %[[arg0:.*]]: [[tensorType:.*]]) { | ||
%stream = iterators.tensor_to_stream %tensor : | ||
tensor<?xi32> to !iterators.stream<tensor<2xi32>> | ||
// CHECK-NEXT: %[[V1:.*]] = arith.constant 0 : index | ||
// CHECK-NEXT: %[[V2:.*]] = iterators.createstate(%[[V1]], %[[arg0]]) : !iterators.state<index, [[tensorType]]> | ||
return | ||
// CHECK-NEXT: return | ||
} | ||
// CHECK-NEXT: } |
60 changes: 60 additions & 0 deletions
60
experimental/iterators/test/Integration/Dialect/Iterators/CPU/tensor-to-stream.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// RUN: iterators-opt %s \ | ||
// RUN: -convert-iterators-to-llvm \ | ||
// RUN: -inline -decompose-iterator-states -canonicalize \ | ||
// RUN: -one-shot-bufferize=bufferize-function-boundaries \ | ||
// RUN: -expand-strided-metadata -finalize-memref-to-llvm \ | ||
// RUN: -lower-affine -canonicalize \ | ||
// RUN: -convert-scf-to-cf \ | ||
// RUN: -convert-func-to-llvm \ | ||
// RUN: -canonicalize \ | ||
// RUN: -convert-cf-to-llvm \ | ||
// RUN: | mlir-cpu-runner -e main -entry-point-result=void \ | ||
// RUN: | FileCheck %s | ||
|
||
!struct_i32i32 = !llvm.struct<(i32, i32)> | ||
|
||
func.func private @tensor2xi32_to_struct(%input : tensor<2xi32>) -> !struct_i32i32 { | ||
%zero = arith.constant 0 : index | ||
%one = arith.constant 1 : index | ||
%i0 = tensor.extract %input[%zero] : tensor<2xi32> | ||
%i1 = tensor.extract %input[%one] : tensor<2xi32> | ||
%undef = llvm.mlir.undef : !struct_i32i32 | ||
%inserted = llvm.insertvalue %i0, %undef[0] : !struct_i32i32 | ||
%result = llvm.insertvalue %i1, %inserted[1] : !struct_i32i32 | ||
return %result : !struct_i32i32 | ||
} | ||
|
||
func.func @test_tensor_to_stream_simple() { | ||
%tensor = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi32> | ||
%stream = iterators.tensor_to_stream %tensor : | ||
tensor<6xi32> to !iterators.stream<tensor<2xi32>> | ||
%mapped = "iterators.map"(%stream) {mapFuncRef = @tensor2xi32_to_struct} | ||
: (!iterators.stream<tensor<2xi32>>) -> (!iterators.stream<!struct_i32i32>) | ||
"iterators.sink"(%mapped) : (!iterators.stream<!struct_i32i32>) -> () | ||
// CHECK: (1, 2) | ||
// CHECK-NEXT: (3, 4) | ||
// CHECK-NEXT: (5, 6) | ||
return | ||
} | ||
|
||
func.func @test_tensor_to_stream_simple_dynamic(%tensor : tensor<?xi32>) { | ||
%stream = iterators.tensor_to_stream %tensor : | ||
tensor<?xi32> to !iterators.stream<tensor<2xi32>> | ||
%mapped = "iterators.map"(%stream) {mapFuncRef = @tensor2xi32_to_struct} | ||
: (!iterators.stream<tensor<2xi32>>) -> (!iterators.stream<!struct_i32i32>) | ||
"iterators.sink"(%mapped) : (!iterators.stream<!struct_i32i32>) -> () | ||
// CHECK-NEXT: (11, 12) | ||
// CHECK-NEXT: (13, 14) | ||
// CHECK-NEXT: (15, 16) | ||
return | ||
} | ||
|
||
func.func @main() { | ||
func.call @test_tensor_to_stream_simple() : () -> () | ||
|
||
%tensor = arith.constant dense<[11, 12, 13, 14, 15, 16]> : tensor<6xi32> | ||
%dtensor = tensor.cast %tensor : tensor<6xi32> to tensor<?xi32> | ||
func.call @test_tensor_to_stream_simple_dynamic(%dtensor) : (tensor<?xi32>) -> () | ||
|
||
return | ||
} |