annotate mlir/test/mlir-cpu-runner/memref_reinterpret_cast.mlir @ 214:0cf2d4ade63d

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 13 Jul 2021 09:53:52 +0900 (2021-07-13)
parents 2e18cbf3894f
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 // RUN: mlir-opt %s -convert-scf-to-std -convert-std-to-llvm \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 // RUN: | mlir-cpu-runner -e main -entry-point-result=void \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // RUN: -shared-libs=%mlir_runner_utils_dir/libmlir_runner_utils%shlibext,%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // RUN: | FileCheck %s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 func private @print_memref_f32(memref<*xf32>) attributes { llvm.emit_c_interface }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 func @main() -> () {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 %c0 = constant 0 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 %c1 = constant 1 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 // Initialize input.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 %input = memref.alloc() : memref<2x3xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 %dim_x = memref.dim %input, %c0 : memref<2x3xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 %dim_y = memref.dim %input, %c1 : memref<2x3xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 scf.parallel (%i, %j) = (%c0, %c0) to (%dim_x, %dim_y) step (%c1, %c1) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 %prod = muli %i, %dim_y : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 %val = addi %prod, %j : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 %val_i64 = index_cast %val : index to i64
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 %val_f32 = sitofp %val_i64 : i64 to f32
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 memref.store %val_f32, %input[%i, %j] : memref<2x3xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 %unranked_input = memref.cast %input : memref<2x3xf32> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 call @print_memref_f32(%unranked_input) : (memref<*xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 // CHECK: rank = 2 offset = 0 sizes = [2, 3] strides = [3, 1]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 // CHECK-NEXT: [0, 1, 2]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 // CHECK-NEXT: [3, 4, 5]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 // Test cases.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 call @cast_ranked_memref_to_static_shape(%input) : (memref<2x3xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 call @cast_ranked_memref_to_dynamic_shape(%input) : (memref<2x3xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 call @cast_unranked_memref_to_static_shape(%input) : (memref<2x3xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 call @cast_unranked_memref_to_dynamic_shape(%input) : (memref<2x3xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 return
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 func @cast_ranked_memref_to_static_shape(%input : memref<2x3xf32>) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 %output = memref.reinterpret_cast %input to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 offset: [0], sizes: [6, 1], strides: [1, 1]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 : memref<2x3xf32> to memref<6x1xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 %unranked_output = memref.cast %output
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 : memref<6x1xf32> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 call @print_memref_f32(%unranked_output) : (memref<*xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 // CHECK: rank = 2 offset = 0 sizes = [6, 1] strides = [1, 1] data =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 // CHECK-NEXT: [0],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 // CHECK-NEXT: [1],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 // CHECK-NEXT: [2],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 // CHECK-NEXT: [3],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 // CHECK-NEXT: [4],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 // CHECK-NEXT: [5]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 return
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 func @cast_ranked_memref_to_dynamic_shape(%input : memref<2x3xf32>) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 %c0 = constant 0 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 %c1 = constant 1 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 %c6 = constant 6 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 %output = memref.reinterpret_cast %input to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 offset: [%c0], sizes: [%c1, %c6], strides: [%c6, %c1]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 : memref<2x3xf32> to memref<?x?xf32, offset: ?, strides: [?, ?]>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 %unranked_output = memref.cast %output
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 : memref<?x?xf32, offset: ?, strides: [?, ?]> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 call @print_memref_f32(%unranked_output) : (memref<*xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 // CHECK: rank = 2 offset = 0 sizes = [1, 6] strides = [6, 1] data =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 // CHECK-NEXT: [0, 1, 2, 3, 4, 5]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 return
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 func @cast_unranked_memref_to_static_shape(%input : memref<2x3xf32>) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 %unranked_input = memref.cast %input : memref<2x3xf32> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 %output = memref.reinterpret_cast %unranked_input to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 offset: [0], sizes: [6, 1], strides: [1, 1]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 : memref<*xf32> to memref<6x1xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 %unranked_output = memref.cast %output
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 : memref<6x1xf32> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 call @print_memref_f32(%unranked_output) : (memref<*xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 // CHECK: rank = 2 offset = 0 sizes = [6, 1] strides = [1, 1] data =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 // CHECK-NEXT: [0],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 // CHECK-NEXT: [1],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 // CHECK-NEXT: [2],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 // CHECK-NEXT: [3],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 // CHECK-NEXT: [4],
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 // CHECK-NEXT: [5]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 return
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 func @cast_unranked_memref_to_dynamic_shape(%input : memref<2x3xf32>) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 %unranked_input = memref.cast %input : memref<2x3xf32> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 %c0 = constant 0 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 %c1 = constant 1 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 %c6 = constant 6 : index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 %output = memref.reinterpret_cast %unranked_input to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 offset: [%c0], sizes: [%c1, %c6], strides: [%c6, %c1]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 : memref<*xf32> to memref<?x?xf32, offset: ?, strides: [?, ?]>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 %unranked_output = memref.cast %output
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 : memref<?x?xf32, offset: ?, strides: [?, ?]> to memref<*xf32>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 call @print_memref_f32(%unranked_output) : (memref<*xf32>) -> ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 // CHECK: rank = 2 offset = 0 sizes = [1, 6] strides = [6, 1] data =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 // CHECK-NEXT: [0, 1, 2, 3, 4, 5]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 return
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 }