xref: /llvm-project/mlir/test/Dialect/Polynomial/ops_errors.mlir (revision 692ae5443b1778e138527ef55d799a4b535a36f9)
1// RUN: mlir-opt --split-input-file --verify-diagnostics %s
2
3#my_poly = #polynomial.int_polynomial<1 + x**1024>
4#ring = #polynomial.ring<coefficientType=i16>
5!ty = !polynomial.polynomial<ring=#ring>
6
7func.func @test_from_tensor_too_large_coeffs() {
8  %two = arith.constant 2 : i32
9  %coeffs1 = tensor.from_elements %two, %two : tensor<2xi32>
10  // expected-error@below {{is too large to fit in the coefficients}}
11  // expected-note@below {{rescaled to fit}}
12  %poly = polynomial.from_tensor %coeffs1 : tensor<2xi32> -> !ty
13  return
14}
15
16// -----
17
18#my_poly = #polynomial.int_polynomial<1 + x**4>
19#ring = #polynomial.ring<coefficientType=i32, coefficientModulus=256:i32, polynomialModulus=#my_poly>
20!ty = !polynomial.polynomial<ring=#ring>
21func.func @test_from_tensor_wrong_tensor_type() {
22  %two = arith.constant 2 : i32
23  %coeffs1 = tensor.from_elements %two, %two, %two, %two, %two : tensor<5xi32>
24  // expected-error@below {{input type 'tensor<5xi32>' does not match output type '!polynomial.polynomial<ring = <coefficientType = i32, coefficientModulus = 256 : i32, polynomialModulus = <1 + x**4>>>'}}
25  // expected-note@below {{at most the degree of the polynomialModulus of the output type's ring attribute}}
26  %poly = polynomial.from_tensor %coeffs1 : tensor<5xi32> -> !ty
27  return
28}
29
30// -----
31
32#my_poly = #polynomial.int_polynomial<1 + x**4>
33#ring = #polynomial.ring<coefficientType=i32, coefficientModulus=256:i32, polynomialModulus=#my_poly>
34!ty = !polynomial.polynomial<ring=#ring>
35func.func @test_to_tensor_wrong_output_tensor_type(%arg0 : !ty) {
36  // expected-error@below {{input type '!polynomial.polynomial<ring = <coefficientType = i32, coefficientModulus = 256 : i32, polynomialModulus = <1 + x**4>>>' does not match output type 'tensor<5xi32>'}}
37  // expected-note@below {{at most the degree of the polynomialModulus of the input type's ring attribute}}
38  %tensor = polynomial.to_tensor %arg0 : !ty -> tensor<5xi32>
39  return
40}
41
42// -----
43
44#my_poly = #polynomial.int_polynomial<1 + x**1024>
45#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i32, polynomialModulus=#my_poly>
46!ty = !polynomial.polynomial<ring=#ring>
47
48func.func @test_mul_scalar_wrong_type(%arg0: !ty) -> !ty {
49  %scalar = arith.constant 2 : i32  // should be i16
50  // expected-error@below {{polynomial coefficient type 'i16' does not match scalar type 'i32'}}
51  %poly = polynomial.mul_scalar %arg0, %scalar : !ty, i32
52  return %poly : !ty
53}
54
55// -----
56
57#my_poly = #polynomial.int_polynomial<-1 + x**1024>
58#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i16, polynomialModulus=#my_poly>
59!poly_ty = !polynomial.polynomial<ring=#ring>
60
61// CHECK-NOT: @test_invalid_ntt
62// CHECK-NOT: polynomial.ntt
63func.func @test_invalid_ntt(%0 : !poly_ty) {
64  // expected-error@below {{expects a ring encoding to be provided to the tensor}}
65  %1 = polynomial.ntt %0 {root=#polynomial.primitive_root<value=31:i32, degree=8:index>} : !poly_ty -> tensor<1024xi32>
66  return
67}
68
69// -----
70
71#my_poly = #polynomial.int_polynomial<-1 + x**1024>
72#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i16, polynomialModulus=#my_poly>
73!poly_ty = !polynomial.polynomial<ring=#ring>
74
75// CHECK-NOT: @test_invalid_ntt
76// CHECK-NOT: polynomial.ntt
77func.func @test_invalid_ntt(%0 : !poly_ty) {
78  // expected-error@below {{tensor encoding is not a ring attribute}}
79  %1 = polynomial.ntt %0 {root=#polynomial.primitive_root<value=31:i32, degree=8:index>} : !poly_ty -> tensor<1024xi32, #my_poly>
80  return
81}
82
83// -----
84
85#my_poly = #polynomial.int_polynomial<-1 + x**1024>
86#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i16, polynomialModulus=#my_poly>
87#ring1 = #polynomial.ring<coefficientType=i16, coefficientModulus=257:i16, polynomialModulus=#my_poly>
88!poly_ty = !polynomial.polynomial<ring=#ring>
89
90// CHECK-NOT: @test_invalid_intt
91// CHECK-NOT: polynomial.intt
92func.func @test_invalid_intt(%0 : tensor<1024xi32, #ring1>) {
93  // expected-error@below {{not equivalent to the polynomial ring}}
94  %1 = polynomial.intt %0 {root=#polynomial.primitive_root<value=31:i32, degree=8:index>} : tensor<1024xi32, #ring1> -> !poly_ty
95  return
96}
97
98// -----
99
100#my_poly = #polynomial.int_polynomial<-1 + x**1024>
101#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i16, polynomialModulus=#my_poly>
102!poly_ty = !polynomial.polynomial<ring=#ring>
103
104// CHECK-NOT: @test_invalid_intt
105// CHECK-NOT: polynomial.intt
106func.func @test_invalid_intt(%0 : tensor<1025xi32, #ring>) {
107  // expected-error@below {{does not match output type}}
108  // expected-note@below {{exactly the degree of the polynomialModulus of the polynomial type's ring attribute}}
109  %1 = polynomial.intt %0 {root=#polynomial.primitive_root<value=31:i32, degree=8:index>} : tensor<1025xi32, #ring> -> !poly_ty
110  return
111}
112
113// -----
114
115#my_poly = #polynomial.int_polynomial<-1 + x**8>
116// A valid root is 31
117#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i16, polynomialModulus=#my_poly>
118!poly_ty = !polynomial.polynomial<ring=#ring>
119
120// CHECK-NOT: @test_invalid_intt
121// CHECK-NOT: polynomial.intt
122func.func @test_invalid_intt(%0 : tensor<8xi32, #ring>) {
123  // expected-error@below {{provided root 32 is not a primitive root of unity mod 256, with the specified degree 8}}
124  %1 = polynomial.intt %0 {root=#polynomial.primitive_root<value=32:i16, degree=8:index>} : tensor<8xi32, #ring> -> !poly_ty
125  return
126}
127