1// RUN: mlir-opt %s \
2// RUN:   -gpu-kernel-outlining \
3// RUN:   -pass-pipeline='gpu.module(strip-debuginfo,convert-gpu-to-nvvm,gpu-to-cubin)' \
4// RUN:   -gpu-async-region -async-ref-counting -gpu-to-llvm \
5// RUN:   -async-to-async-runtime -convert-async-to-llvm -convert-std-to-llvm \
6// RUN: | mlir-cpu-runner \
7// RUN:   --shared-libs=%linalg_test_lib_dir/libmlir_cuda_runtime%shlibext \
8// RUN:   --shared-libs=%linalg_test_lib_dir/libmlir_async_runtime%shlibext \
9// RUN:   --shared-libs=%linalg_test_lib_dir/libmlir_runner_utils%shlibext \
10// RUN:   --entry-point-result=void -O0 \
11// RUN: | FileCheck %s
12
13func @main() {
14  %c0    = constant 0 : index
15  %c1    = constant 1 : index
16  %count = constant 2 : index
17
18  // initialize h0 on host
19  %h0 = memref.alloc(%count) : memref<?xi32>
20  %h0_unranked = memref.cast %h0 : memref<?xi32> to memref<*xi32>
21  gpu.host_register %h0_unranked : memref<*xi32>
22
23  %v0 = constant 42 : i32
24  memref.store %v0, %h0[%c0] : memref<?xi32>
25  memref.store %v0, %h0[%c1] : memref<?xi32>
26
27  // copy h0 to b0 on device.
28  %t0, %f0 = async.execute () -> !async.value<memref<?xi32>> {
29    %b0 = gpu.alloc(%count) : memref<?xi32>
30    gpu.memcpy %b0, %h0 : memref<?xi32>, memref<?xi32>
31    async.yield %b0 : memref<?xi32>
32  }
33
34  // copy h0 to b1 and b2 (fork)
35  %t1, %f1 = async.execute [%t0] (
36    %f0 as %b0 : !async.value<memref<?xi32>>
37  ) -> !async.value<memref<?xi32>> {
38    %b1 = gpu.alloc(%count) : memref<?xi32>
39    gpu.memcpy %b1, %b0 : memref<?xi32>, memref<?xi32>
40    async.yield %b1 : memref<?xi32>
41  }
42  %t2, %f2 = async.execute [%t0] (
43    %f0 as %b0 : !async.value<memref<?xi32>>
44  ) -> !async.value<memref<?xi32>> {
45    %b2 = gpu.alloc(%count) : memref<?xi32>
46    gpu.memcpy %b2, %b0 : memref<?xi32>, memref<?xi32>
47    async.yield %b2 : memref<?xi32>
48  }
49
50  // h0 = b1 + b2 (join).
51  %t3 = async.execute [%t1, %t2] (
52    %f1 as %b1 : !async.value<memref<?xi32>>,
53    %f2 as %b2 : !async.value<memref<?xi32>>
54  ) {
55    gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %c1, %grid_y = %c1, %grid_z = %c1)
56               threads(%tx, %ty, %tz) in (%block_x = %count, %block_y = %c1, %block_z = %c1) {
57      %v1 = memref.load %b1[%tx] : memref<?xi32>
58      %v2 = memref.load %b2[%tx] : memref<?xi32>
59      %sum = addi %v1, %v2 : i32
60      memref.store %sum, %h0[%tx] : memref<?xi32>
61      gpu.terminator
62    }
63    async.yield
64  }
65
66  async.await %t3 : !async.token
67  // CHECK: [84, 84]
68  call @print_memref_i32(%h0_unranked) : (memref<*xi32>) -> ()
69  return
70}
71
72func private @print_memref_i32(memref<*xi32>)
73