1// RUN:   mlir-opt %s                                                          \
2// RUN:               -async-parallel-for                                      \
3// RUN:               -async-to-async-runtime                                  \
4// RUN:               -async-runtime-ref-counting                              \
5// RUN:               -async-runtime-ref-counting-opt                          \
6// RUN:               -convert-async-to-llvm                                   \
7// RUN:               -convert-linalg-to-loops                                 \
8// RUN:               -convert-scf-to-std                                      \
9// RUN:               -std-expand                                              \
10// RUN:               -convert-vector-to-llvm                                  \
11// RUN:               -convert-memref-to-llvm                                  \
12// RUN:               -convert-std-to-llvm                                     \
13// RUN:               -reconcile-unrealized-casts                              \
14// RUN: | mlir-cpu-runner                                                      \
15// RUN: -e entry -entry-point-result=void -O3                                  \
16// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_runner_utils%shlibext  \
17// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext\
18// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_async_runtime%shlibext \
19// RUN: | FileCheck %s --dump-input=always
20
21// RUN:   mlir-opt %s                                                          \
22// RUN:               -async-parallel-for=async-dispatch=false                 \
23// RUN:               -async-to-async-runtime                                  \
24// RUN:               -async-runtime-ref-counting                              \
25// RUN:               -async-runtime-ref-counting-opt                          \
26// RUN:               -convert-async-to-llvm                                   \
27// RUN:               -convert-linalg-to-loops                                 \
28// RUN:               -convert-scf-to-std                                      \
29// RUN:               -std-expand                                              \
30// RUN:               -convert-vector-to-llvm                                  \
31// RUN:               -convert-memref-to-llvm                                  \
32// RUN:               -convert-std-to-llvm                                     \
33// RUN:               -reconcile-unrealized-casts                              \
34// RUN: | mlir-cpu-runner                                                      \
35// RUN: -e entry -entry-point-result=void -O3                                  \
36// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_runner_utils%shlibext  \
37// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext\
38// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_async_runtime%shlibext \
39// RUN: | FileCheck %s --dump-input=always
40
41// RUN:   mlir-opt %s                                                          \
42// RUN:               -convert-linalg-to-loops                                 \
43// RUN:               -convert-scf-to-std                                      \
44// RUN:               -convert-vector-to-llvm                                  \
45// RUN:               -convert-memref-to-llvm                                  \
46// RUN:               -convert-std-to-llvm                                     \
47// RUN:               -reconcile-unrealized-casts                              \
48// RUN: | mlir-cpu-runner                                                      \
49// RUN: -e entry -entry-point-result=void -O3                                  \
50// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_runner_utils%shlibext  \
51// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext\
52// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_async_runtime%shlibext \
53// RUN: | FileCheck %s --dump-input=always
54
55#map0 = affine_map<(d0, d1) -> (d0, d1)>
56
57func @scf_parallel(%lhs: memref<?x?xf32>,
58                   %rhs: memref<?x?xf32>,
59                   %sum: memref<?x?xf32>) {
60  %c0 = arith.constant 0 : index
61  %c1 = arith.constant 1 : index
62
63  %d0 = memref.dim %lhs, %c0 : memref<?x?xf32>
64  %d1 = memref.dim %lhs, %c1 : memref<?x?xf32>
65
66  scf.parallel (%i, %j) = (%c0, %c0) to (%d0, %d1) step (%c1, %c1) {
67    %lv = memref.load %lhs[%i, %j] : memref<?x?xf32>
68    %rv = memref.load %lhs[%i, %j] : memref<?x?xf32>
69    %r = arith.addf %lv, %rv : f32
70    memref.store %r, %sum[%i, %j] : memref<?x?xf32>
71  }
72
73  return
74}
75
76func @entry() {
77  %f1 = arith.constant 1.0 : f32
78  %f4 = arith.constant 4.0 : f32
79  %c0 = arith.constant 0 : index
80  %c1 = arith.constant 1 : index
81  %cN = arith.constant 50 : index
82
83  //
84  // Sanity check for the function under test.
85  //
86
87  %LHS10 = memref.alloc() {alignment = 64} : memref<1x10xf32>
88  %RHS10 = memref.alloc() {alignment = 64} : memref<1x10xf32>
89  %DST10 = memref.alloc() {alignment = 64} : memref<1x10xf32>
90
91  linalg.fill(%f1, %LHS10) : f32, memref<1x10xf32>
92  linalg.fill(%f1, %RHS10) : f32, memref<1x10xf32>
93
94  %LHS = memref.cast %LHS10 : memref<1x10xf32> to memref<?x?xf32>
95  %RHS = memref.cast %RHS10 : memref<1x10xf32> to memref<?x?xf32>
96  %DST = memref.cast %DST10 : memref<1x10xf32> to memref<?x?xf32>
97
98  call @scf_parallel(%LHS, %RHS, %DST)
99    : (memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>) -> ()
100
101  // CHECK: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
102  %U = memref.cast %DST10 :  memref<1x10xf32> to memref<*xf32>
103  call @print_memref_f32(%U): (memref<*xf32>) -> ()
104
105  memref.dealloc %LHS10: memref<1x10xf32>
106  memref.dealloc %RHS10: memref<1x10xf32>
107  memref.dealloc %DST10: memref<1x10xf32>
108
109  //
110  // Allocate data for microbenchmarks.
111  //
112
113  %LHS1024 = memref.alloc() {alignment = 64} : memref<1024x1024xf32>
114  %RHS1024 = memref.alloc() {alignment = 64} : memref<1024x1024xf32>
115  %DST1024 = memref.alloc() {alignment = 64} : memref<1024x1024xf32>
116
117  %LHS0 = memref.cast %LHS1024 : memref<1024x1024xf32> to memref<?x?xf32>
118  %RHS0 = memref.cast %RHS1024 : memref<1024x1024xf32> to memref<?x?xf32>
119  %DST0 = memref.cast %DST1024 : memref<1024x1024xf32> to memref<?x?xf32>
120
121  //
122  // Warm up.
123  //
124
125  call @scf_parallel(%LHS0, %RHS0, %DST0)
126    : (memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>) -> ()
127
128  //
129  // Measure execution time.
130  //
131
132  %t0 = call @rtclock() : () -> f64
133  scf.for %i = %c0 to %cN step %c1 {
134    call @scf_parallel(%LHS0, %RHS0, %DST0)
135      : (memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>) -> ()
136  }
137  %t1 = call @rtclock() : () -> f64
138  %t1024 = arith.subf %t1, %t0 : f64
139
140  // Print timings.
141  vector.print %t1024 : f64
142
143  // Free.
144  memref.dealloc %LHS1024: memref<1024x1024xf32>
145  memref.dealloc %RHS1024: memref<1024x1024xf32>
146  memref.dealloc %DST1024: memref<1024x1024xf32>
147
148  return
149}
150
151func private @rtclock() -> f64
152
153func private @print_memref_f32(memref<*xf32>)
154  attributes { llvm.emit_c_interface }
155