1// RUN: mlir-opt <%s -split-input-file -verify-diagnostics
2
3func @tensor.cast_mismatching_constants(%arg0: tensor<1xf32>) {
4  // expected-error@+1 {{operand type 'tensor<1xf32>' and result type 'tensor<2xf32>' are cast incompatible}}
5  %0 = tensor.cast %arg0 : tensor<1xf32> to tensor<2xf32>
6  return
7}
8
9// -----
10
11func @extract_too_many_indices(%arg0: tensor<?xf32>) {
12  // expected-error@+1 {{incorrect number of indices for extract_element}}
13  %0 = tensor.extract %arg0[] : tensor<?xf32>
14  return
15}
16
17// -----
18
19func @tensor.from_elements_wrong_result_type() {
20  // expected-error@+2 {{'result' must be 1D tensor of any type values, but got 'tensor<*xi32>'}}
21  %c0 = constant 0 : i32
22  %0 = tensor.from_elements %c0 : tensor<*xi32>
23  return
24}
25
26// -----
27
28func @tensor.from_elements_wrong_elements_count() {
29  // expected-error@+2 {{1 operands present, but expected 2}}
30  %c0 = constant 0 : index
31  %0 = tensor.from_elements %c0 : tensor<2xindex>
32  return
33}
34
35// -----
36
37func @tensor.generate(%m : index)
38    -> tensor<?x3x?xf32> {
39  // expected-error @+1 {{must have as many index operands as dynamic extents in the result type}}
40  %tnsr = tensor.generate %m {
41    ^bb0(%i : index, %j : index, %k : index):
42      %elem = constant 8.0 : f32
43      tensor.yield %elem : f32
44  } : tensor<?x3x?xf32>
45  return %tnsr : tensor<?x3x?xf32>
46}
47
48// -----
49
50func @tensor.generate(%m : index, %n : index)
51    -> tensor<?x3x?xf32> {
52  // expected-error @+1 {{must have one body argument per input dimension}}
53  %tnsr = tensor.generate %m, %n {
54    ^bb0(%i : index, %j : index):
55      %elem = constant 8.0 : f32
56      tensor.yield %elem : f32
57  } : tensor<?x3x?xf32>
58  return %tnsr : tensor<?x3x?xf32>
59}
60
61// -----
62
63func @tensor.generate(%m : index, %n : index)
64    -> tensor<?x3x?xf32> {
65  // expected-error @+1 {{all body arguments must be index}}
66  %tnsr = tensor.generate %m, %n {
67    ^bb0(%i : index, %j : index, %k : i64):
68      %elem = constant 8.0 : f32
69      tensor.yield %elem : f32
70  } : tensor<?x3x?xf32>
71  return %tnsr : tensor<?x3x?xf32>
72}
73
74// -----
75
76func @tensor.generate(%m : index, %n : index)
77    -> tensor<?x3x?xf32> {
78  // expected-error @+2 {{op expects regions to end with 'tensor.yield', found 'std.return'}}
79  // expected-note @+1 {{in custom textual format, the absence of terminator implies 'tensor.yield'}}
80  %tnsr = tensor.generate %m, %n {
81    ^bb0(%i : index, %j : index, %k : index):
82      %elem = constant 8.0 : f32
83      return %elem : f32
84  } : tensor<?x3x?xf32>
85  return %tnsr : tensor<?x3x?xf32>
86}
87
88// -----
89
90func @tensor.generate(%m : index, %n : index)
91    -> tensor<?x3x?xf32> {
92  // expected-error @+1 {{body must be terminated with a `yield` operation of the tensor element type}}
93  %tnsr = tensor.generate %m, %n {
94    ^bb0(%i : index, %j : index, %k : index):
95      %elem = constant 8 : i32
96      tensor.yield %elem : i32
97  } : tensor<?x3x?xf32>
98  return %tnsr : tensor<?x3x?xf32>
99}
100// -----
101
102func @tensor.reshape_element_type_mismatch(
103       %buf: tensor<*xf32>, %shape: tensor<1xi32>) {
104  // expected-error @+1 {{element types of source and destination tensor types should be the same}}
105  tensor.reshape %buf(%shape) : (tensor<*xf32>, tensor<1xi32>) -> tensor<?xi32>
106}
107
108// -----
109
110func @tensor.reshape_dst_ranked_shape_unranked(
111       %buf: tensor<*xf32>, %shape: tensor<?xi32>) {
112  // expected-error @+1 {{cannot use shape operand with dynamic length to reshape to statically-ranked tensor type}}
113  tensor.reshape %buf(%shape) : (tensor<*xf32>, tensor<?xi32>) -> tensor<?xf32>
114}
115
116// -----
117
118func @tensor.reshape_dst_shape_rank_mismatch(
119       %buf: tensor<*xf32>, %shape: tensor<1xi32>) {
120  // expected-error @+1 {{length of shape operand differs from the result's tensor rank}}
121  tensor.reshape %buf(%shape)
122    : (tensor<*xf32>, tensor<1xi32>) -> tensor<?x?xf32>
123}
124
125// -----
126
127func @tensor.reshape_num_elements_mismatch(
128       %buf: tensor<1xf32>, %shape: tensor<1xi32>) {
129  // expected-error @+1 {{source and destination tensor should have the same number of elements}}
130  tensor.reshape %buf(%shape)
131    : (tensor<1xf32>, tensor<1xi32>) -> tensor<10xf32>
132}
133