1// RUN: mlir-opt -allow-unregistered-dialect -split-input-file -verify-diagnostics %s | FileCheck %s
2
3// CHECK-LABEL: @source
4func.func @source(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
5  %0 = "quant.region"(%arg0, %arg1, %arg2) ({
6    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>):
7      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
8      %14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
9      "quant.return"(%14) : (tensor<4xf32>) -> ()
10  }) {input_specs = [f32, f32, f32], output_specs = [f32], logical_kernel = "xyz"}
11    : (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
12  return %0 : tensor<4xf32>
13}
14
15// CHECK-LABEL: @annotated
16func.func @annotated(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
17  %0 = "quant.region"(%arg0, %arg1, %arg2) ({
18    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>):
19      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
20      %14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
21      "quant.return"(%14) : (tensor<4xf32>) -> ()
22  }) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, f32],
23      output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"}
24    : (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
25  return %0 : tensor<4xf32>
26}
27
28// CHECK-LABEL: @quantized
29func.func @quantized(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
30  %0 = "quant.region"(%arg0, %arg1, %arg2) ({
31    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>):
32      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
33      %14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
34      "quant.return"(%14) : (tensor<4xf32>) -> ()
35  }) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, !quant.uniform<i32:f32, 2.0>],
36      output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"}
37    : (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
38  return %0 : tensor<4xf32>
39}
40
41// -----
42
43func.func @unmatched_quantize(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
44  // @expected-error @+1 {{'quant.region' op has incompatible specification !quant.uniform<i32:f16, 3.000000e+00> and input type 'tensor<4xf32>'}}
45  %0 = "quant.region"(%arg0, %arg1, %arg2) ({
46    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>):
47      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
48      %14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
49      "quant.return"(%14) : (tensor<4xf32>) -> ()
50  }) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, !quant.uniform<i32:f16, 3.0>],
51      output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"}
52    : (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
53  return %0 : tensor<4xf32>
54}
55
56// -----
57
58func.func @unmatched_primitive(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
59  // @expected-error @+1 {{'quant.region' op has incompatible specification i32 and input type 'tensor<4xf32>'}}
60  %0 = "quant.region"(%arg0, %arg1, %arg2) ({
61    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>):
62      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
63      %14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
64      "quant.return"(%14) : (tensor<4xf32>) -> ()
65  }) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>, i32],
66      output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"}
67    : (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
68  return %0 : tensor<4xf32>
69}
70
71// -----
72
73func.func @unmatched_number(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
74  // @expected-error @+1 {{'quant.region' op has unmatched operands/results number and spec attributes number}}
75  %0 = "quant.region"(%arg0, %arg1, %arg2) ({
76    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>, %12: tensor<4xf32>):
77      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
78      %14 = "bar"(%13, %12) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
79      "quant.return"(%14) : (tensor<4xf32>) -> ()
80  }) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>],
81      output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"}
82    : (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
83  return %0 : tensor<4xf32>
84}
85
86// -----
87
88func.func @isolated(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>, %arg2: tensor<4xf32>) -> (tensor<4xf32>) {
89  // @expected-note @+1 {{required by region isolation constraints}}
90  %0 = "quant.region"(%arg0, %arg1) ({
91    ^bb0(%10: tensor<4xf32>, %11: tensor<4xf32>):
92      %13 = "foo"(%10, %11) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
93      // @expected-error @+1 {{'bar' op using value defined outside the region}}
94      %14 = "bar"(%13, %arg2) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
95      "quant.return"(%14) : (tensor<4xf32>) -> ()
96  }) {input_specs = [!quant.uniform<i8:f32, 1.0>, !quant.uniform<i8:f32, 2.0>],
97      output_specs = [!quant.uniform<i8:f32, 4.0>], logical_kernel = "xyz"}
98    : (tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>)
99  return %0 : tensor<4xf32>
100}
101
102