1// RUN: mlir-opt -split-input-file -verify-diagnostics %s | mlir-opt | FileCheck %s
2// RUN: mlir-opt -split-input-file -verify-diagnostics -mlir-print-op-generic %s | FileCheck %s --check-prefix=GENERIC
3// RUN: mlir-opt -split-input-file -verify-diagnostics %s -mlir-print-debuginfo | mlir-opt -mlir-print-debuginfo | FileCheck %s --check-prefix=LOCINFO
4
5module {
6  // GENERIC: "llvm.func"
7  // GENERIC: function_type = !llvm.func<void ()>
8  // GENERIC-SAME: sym_name = "foo"
9  // GENERIC-SAME: () -> ()
10  // CHECK: llvm.func @foo()
11  "llvm.func"() ({
12  }) {sym_name = "foo", function_type = !llvm.func<void ()>} : () -> ()
13
14  // GENERIC: "llvm.func"
15  // GENERIC: function_type = !llvm.func<i64 (i64, i64)>
16  // GENERIC-SAME: sym_name = "bar"
17  // GENERIC-SAME: () -> ()
18  // CHECK: llvm.func @bar(i64, i64) -> i64
19  "llvm.func"() ({
20  }) {sym_name = "bar", function_type = !llvm.func<i64 (i64, i64)>} : () -> ()
21
22  // GENERIC: "llvm.func"
23  // CHECK: llvm.func @baz(%{{.*}}: i64) -> i64
24  "llvm.func"() ({
25  // GENERIC: ^bb0
26  ^bb0(%arg0: i64):
27    // GENERIC: llvm.return
28    llvm.return %arg0 : i64
29
30  // GENERIC: function_type = !llvm.func<i64 (i64)>
31  // GENERIC-SAME: sym_name = "baz"
32  // GENERIC-SAME: () -> ()
33  }) {sym_name = "baz", function_type = !llvm.func<i64 (i64)>} : () -> ()
34
35  // CHECK: llvm.func @qux(!llvm.ptr<i64> {llvm.noalias}, i64)
36  // CHECK: attributes {xxx = {yyy = 42 : i64}}
37  "llvm.func"() ({
38  }) {sym_name = "qux", function_type = !llvm.func<void (ptr<i64>, i64)>,
39      arg_attrs = [{llvm.noalias}, {}], xxx = {yyy = 42}} : () -> ()
40
41  // CHECK: llvm.func @roundtrip1()
42  llvm.func @roundtrip1()
43
44  // CHECK: llvm.func @roundtrip2(i64, f32) -> f64
45  llvm.func @roundtrip2(i64, f32) -> f64
46
47  // CHECK: llvm.func @roundtrip3(i32, i1)
48  llvm.func @roundtrip3(%a: i32, %b: i1)
49
50  // CHECK: llvm.func @roundtrip4(%{{.*}}: i32, %{{.*}}: i1) {
51  llvm.func @roundtrip4(%a: i32, %b: i1) {
52    llvm.return
53  }
54
55  // CHECK: llvm.func @roundtrip5()
56  // CHECK: attributes {baz = 42 : i64, foo = "bar"}
57  llvm.func @roundtrip5() attributes {foo = "bar", baz = 42}
58
59  // CHECK: llvm.func @roundtrip6()
60  // CHECK: attributes {baz = 42 : i64, foo = "bar"}
61  llvm.func @roundtrip6() attributes {foo = "bar", baz = 42} {
62    llvm.return
63  }
64
65  // CHECK: llvm.func @roundtrip7() {
66  llvm.func @roundtrip7() attributes {} {
67    llvm.return
68  }
69
70  // CHECK: llvm.func @roundtrip8() -> i32
71  llvm.func @roundtrip8() -> i32 attributes {}
72
73  // CHECK: llvm.func @roundtrip9(!llvm.ptr<i32> {llvm.noalias})
74  llvm.func @roundtrip9(!llvm.ptr<i32> {llvm.noalias})
75
76  // CHECK: llvm.func @roundtrip10(!llvm.ptr<i32> {llvm.noalias})
77  llvm.func @roundtrip10(%arg0: !llvm.ptr<i32> {llvm.noalias})
78
79  // CHECK: llvm.func @roundtrip11(%{{.*}}: !llvm.ptr<i32> {llvm.noalias}) {
80  llvm.func @roundtrip11(%arg0: !llvm.ptr<i32> {llvm.noalias}) {
81    llvm.return
82  }
83
84  // CHECK: llvm.func @roundtrip12(%{{.*}}: !llvm.ptr<i32> {llvm.noalias})
85  // CHECK: attributes {foo = 42 : i32}
86  llvm.func @roundtrip12(%arg0: !llvm.ptr<i32> {llvm.noalias})
87  attributes {foo = 42 : i32} {
88    llvm.return
89  }
90
91  // CHECK: llvm.func @byvalattr(%{{.*}}: !llvm.ptr<i32> {llvm.byval})
92  llvm.func @byvalattr(%arg0: !llvm.ptr<i32> {llvm.byval}) {
93    llvm.return
94  }
95
96  // CHECK: llvm.func @sretattr(%{{.*}}: !llvm.ptr<i32> {llvm.sret})
97  // LOCINFO: llvm.func @sretattr(%{{.*}}: !llvm.ptr<i32> {llvm.sret} loc("some_source_loc"))
98  llvm.func @sretattr(%arg0: !llvm.ptr<i32> {llvm.sret} loc("some_source_loc")) {
99    llvm.return
100  }
101
102  // CHECK: llvm.func @nestattr(%{{.*}}: !llvm.ptr<i32> {llvm.nest})
103  llvm.func @nestattr(%arg0: !llvm.ptr<i32> {llvm.nest}) {
104    llvm.return
105  }
106
107  // CHECK: llvm.func @variadic(...)
108  llvm.func @variadic(...)
109
110  // CHECK: llvm.func @variadic_args(i32, i32, ...)
111  llvm.func @variadic_args(i32, i32, ...)
112
113  //
114  // Check that functions can have linkage attributes.
115  //
116
117  // CHECK: llvm.func internal
118  llvm.func internal @internal_func() {
119    llvm.return
120  }
121
122  // CHECK: llvm.func weak
123  llvm.func weak @weak_linkage() {
124    llvm.return
125  }
126
127  // Omit the `external` linkage, which is the default, in the custom format.
128  // Check that it is present in the generic format using its numeric value.
129  //
130  // CHECK: llvm.func @external_func
131  // GENERIC: linkage = #llvm.linkage<external>
132  llvm.func external @external_func()
133
134  // CHECK-LABEL: llvm.func @arg_struct_attr(
135  // CHECK-SAME: %{{.*}}: !llvm.struct<(i32)> {llvm.struct_attrs = [{llvm.noalias}]}) {
136  llvm.func @arg_struct_attr(
137      %arg0 : !llvm.struct<(i32)> {llvm.struct_attrs = [{llvm.noalias}]}) {
138    llvm.return
139  }
140
141   // CHECK-LABEL: llvm.func @res_struct_attr(%{{.*}}: !llvm.struct<(i32)>)
142   // CHECK-SAME:-> (!llvm.struct<(i32)> {llvm.struct_attrs = [{llvm.noalias}]}) {
143  llvm.func @res_struct_attr(%arg0 : !llvm.struct<(i32)>)
144      -> (!llvm.struct<(i32)> {llvm.struct_attrs = [{llvm.noalias}]}) {
145    llvm.return %arg0 : !llvm.struct<(i32)>
146  }
147}
148
149// -----
150
151module {
152  // expected-error@+1 {{requires one region}}
153  "llvm.func"() {function_type = !llvm.func<void ()>, sym_name = "no_region"} : () -> ()
154}
155
156// -----
157
158module {
159  // expected-error@+1 {{requires attribute 'function_type'}}
160  "llvm.func"() ({}) {sym_name = "missing_type"} : () -> ()
161}
162
163// -----
164
165module {
166  // expected-error@+1 {{attribute 'function_type' failed to satisfy constraint: type attribute of LLVM function type}}
167  "llvm.func"() ({}) {sym_name = "non_llvm_type", function_type = i64} : () -> ()
168}
169
170// -----
171
172module {
173  // expected-error@+1 {{attribute 'function_type' failed to satisfy constraint: type attribute of LLVM function type}}
174  "llvm.func"() ({}) {sym_name = "non_function_type", function_type = i64} : () -> ()
175}
176
177// -----
178
179module {
180  // expected-error@+1 {{entry block must have 0 arguments}}
181  "llvm.func"() ({
182  ^bb0(%arg0: i64):
183    llvm.return
184  }) {function_type = !llvm.func<void ()>, sym_name = "wrong_arg_number"} : () -> ()
185}
186
187// -----
188
189module {
190  // expected-error@+1 {{entry block argument #0('tensor<*xf32>') must match the type of the corresponding argument in function signature('i64')}}
191  "llvm.func"() ({
192  ^bb0(%arg0: tensor<*xf32>):
193    llvm.return
194  }) {function_type = !llvm.func<void (i64)>, sym_name = "wrong_arg_number"} : () -> ()
195}
196
197// -----
198
199module {
200  // expected-error@+1 {{failed to construct function type: expected LLVM type for function arguments}}
201  llvm.func @foo(tensor<*xf32>)
202}
203
204// -----
205
206module {
207  // expected-error@+1 {{failed to construct function type: expected LLVM type for function results}}
208  llvm.func @foo() -> tensor<*xf32>
209}
210
211// -----
212
213module {
214  // expected-error@+1 {{failed to construct function type: expected zero or one function result}}
215  llvm.func @foo() -> (i64, i64)
216}
217
218// -----
219
220module {
221  // expected-error@+1 {{only external functions can be variadic}}
222  llvm.func @variadic_def(...) {
223    llvm.return
224  }
225}
226
227// -----
228
229module {
230  // expected-error@+1 {{cannot attach result attributes to functions with a void return}}
231  llvm.func @variadic_def() -> (!llvm.void {llvm.noalias})
232}
233
234// -----
235
236module {
237  // expected-error@+1 {{variadic arguments must be in the end of the argument list}}
238  llvm.func @variadic_inside(%arg0: i32, ..., %arg1: i32)
239}
240
241// -----
242
243module {
244  // expected-error@+1 {{external functions must have 'external' or 'extern_weak' linkage}}
245  llvm.func internal @internal_external_func()
246}
247
248// -----
249
250module {
251  // expected-error@+1 {{functions cannot have 'common' linkage}}
252  llvm.func common @common_linkage_func()
253}
254