1// RUN: mlir-opt -buffer-results-to-out-params -split-input-file -verify-diagnostics %s | FileCheck %s
2
3// CHECK-LABEL:   func @basic(
4// CHECK-SAME:                %[[ARG:.*]]: memref<f32>) {
5// CHECK:           %[[RESULT:.*]] = "test.source"() : () -> memref<f32>
6// CHECK:           linalg.copy(%[[RESULT]], %[[ARG]]) : memref<f32>, memref<f32>
7// CHECK:           return
8// CHECK:         }
9func @basic() -> (memref<f32>) {
10  %0 = "test.source"() : () -> (memref<f32>)
11  return %0 : memref<f32>
12}
13
14// CHECK-LABEL:   func @presence_of_existing_arguments(
15// CHECK-SAME:                                         %[[ARG0:.*]]: memref<1xf32>,
16// CHECK-SAME:                                         %[[ARG1:.*]]: memref<2xf32>) {
17// CHECK:           %[[RESULT:.*]] = "test.source"() : () -> memref<2xf32>
18// CHECK:           linalg.copy(%[[RESULT]], %[[ARG1]]) : memref<2xf32>, memref<2xf32>
19// CHECK:           return
20// CHECK:         }
21func @presence_of_existing_arguments(%arg0: memref<1xf32>) -> (memref<2xf32>) {
22  %0 = "test.source"() : () -> (memref<2xf32>)
23  return %0 : memref<2xf32>
24}
25
26// CHECK-LABEL:   func @multiple_results(
27// CHECK-SAME:                           %[[ARG0:.*]]: memref<1xf32>,
28// CHECK-SAME:                           %[[ARG1:.*]]: memref<2xf32>) {
29// CHECK:           %[[RESULTS:.*]]:2 = "test.source"() : () -> (memref<1xf32>, memref<2xf32>)
30// CHECK:           linalg.copy(%[[RESULTS]]#0, %[[ARG0]]) : memref<1xf32>, memref<1xf32>
31// CHECK:           linalg.copy(%[[RESULTS]]#1, %[[ARG1]]) : memref<2xf32>, memref<2xf32>
32// CHECK:           return
33// CHECK:         }
34func @multiple_results() -> (memref<1xf32>, memref<2xf32>) {
35  %0, %1 = "test.source"() : () -> (memref<1xf32>, memref<2xf32>)
36  return %0, %1 : memref<1xf32>, memref<2xf32>
37}
38
39// CHECK-LABEL:   func @non_memref_types(
40// CHECK-SAME:                           %[[OUTPARAM:.*]]: memref<f32>) -> (i1, i32) {
41// CHECK:           %[[RESULT1:.*]]:3 = "test.source"() : () -> (i1, memref<f32>, i32)
42// CHECK:           linalg.copy(%[[RESULT1]]#1, %[[OUTPARAM]]) : memref<f32>, memref<f32>
43// CHECK:           return %[[RESULT1]]#0, %[[RESULT1]]#2 : i1, i32
44// CHECK:         }
45func @non_memref_types() -> (i1, memref<f32>, i32) {
46  %0, %1, %2 = "test.source"() : () -> (i1, memref<f32>, i32)
47  return %0, %1, %2 : i1, memref<f32>, i32
48}
49
50// CHECK: func private @external_function(memref<f32>)
51func private @external_function() -> (memref<f32>)
52// CHECK: func private @result_attrs(memref<f32> {test.some_attr})
53func private @result_attrs() -> (memref<f32> {test.some_attr})
54// CHECK: func private @mixed_result_attrs(memref<1xf32>, memref<2xf32> {test.some_attr}, memref<3xf32>)
55func private @mixed_result_attrs() -> (memref<1xf32>, memref<2xf32> {test.some_attr}, memref<3xf32>)
56
57// -----
58
59// CHECK-LABEL: func private @callee(memref<1xf32>)
60func private @callee() -> memref<1xf32>
61
62// CHECK-LABEL:   func @call_basic() {
63// CHECK:           %[[OUTPARAM:.*]] = alloc() : memref<1xf32>
64// CHECK:           call @callee(%[[OUTPARAM]]) : (memref<1xf32>) -> ()
65// CHECK:           "test.sink"(%[[OUTPARAM]]) : (memref<1xf32>) -> ()
66// CHECK:           return
67// CHECK:         }
68func @call_basic() {
69  %0 = call @callee() : () -> memref<1xf32>
70  "test.sink"(%0) : (memref<1xf32>) -> ()
71  return
72}
73
74// -----
75
76// CHECK-LABEL: func private @callee(memref<1xf32>, memref<2xf32>)
77func private @callee() -> (memref<1xf32>, memref<2xf32>)
78
79// CHECK-LABEL:   func @call_multiple_result() {
80// CHECK:           %[[RESULT0:.*]] = alloc() : memref<1xf32>
81// CHECK:           %[[RESULT1:.*]] = alloc() : memref<2xf32>
82// CHECK:           call @callee(%[[RESULT0]], %[[RESULT1]]) : (memref<1xf32>, memref<2xf32>) -> ()
83// CHECK:           "test.sink"(%[[RESULT0]], %[[RESULT1]]) : (memref<1xf32>, memref<2xf32>) -> ()
84// CHECK:         }
85func @call_multiple_result() {
86  %0, %1 = call @callee() : () -> (memref<1xf32>, memref<2xf32>)
87  "test.sink"(%0, %1) : (memref<1xf32>, memref<2xf32>) -> ()
88}
89
90// -----
91
92// CHECK-LABEL: func private @callee(memref<1xf32>) -> (i1, i32)
93func private @callee() -> (i1, memref<1xf32>, i32)
94
95// CHECK-LABEL:   func @call_non_memref_result() {
96// CHECK:           %[[RESULT0:.*]] = alloc() : memref<1xf32>
97// CHECK:           %[[NON_MEMREF_RESULTS:.*]]:2 = call @callee(%[[RESULT0]]) : (memref<1xf32>) -> (i1, i32)
98// CHECK:           "test.sink"(%[[NON_MEMREF_RESULTS]]#0, %[[RESULT0]], %[[NON_MEMREF_RESULTS]]#1) : (i1, memref<1xf32>, i32) -> ()
99// CHECK:         }
100func @call_non_memref_result() {
101  %0, %1, %2 = call @callee() : () -> (i1, memref<1xf32>, i32)
102  "test.sink"(%0, %1, %2) : (i1, memref<1xf32>, i32) -> ()
103}
104
105// -----
106
107func private @callee() -> (memref<?xf32>)
108
109func @call_non_memref_result() {
110  // expected-error @+1 {{cannot create out param for dynamically shaped result}}
111  %0 = call @callee() : () -> (memref<?xf32>)
112  "test.sink"(%0) : (memref<?xf32>) -> ()
113}
114