1 // RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s
3 // test a wsloop reduction with a cleanup region
5 omp.declare_reduction @add_reduction_i_32 : !llvm.ptr init {
6 ^bb0(%arg0: !llvm.ptr):
7 %0 = llvm.mlir.constant(0 : i32) : i32
8 %c4 = llvm.mlir.constant(4 : i64) : i64
9 %2 = llvm.call @malloc(%c4) : (i64) -> !llvm.ptr
10 llvm.store %0, %2 : i32, !llvm.ptr
11 omp.yield(%2 : !llvm.ptr)
13 ^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
14 %0 = llvm.load %arg0 : !llvm.ptr -> i32
15 %1 = llvm.load %arg1 : !llvm.ptr -> i32
16 %2 = llvm.add %0, %1 : i32
17 llvm.store %2, %arg0 : i32, !llvm.ptr
18 omp.yield(%arg0 : !llvm.ptr)
20 ^bb0(%arg0: !llvm.ptr):
21 llvm.call @free(%arg0) : (!llvm.ptr) -> ()
27 %0 = llvm.mlir.constant(-1 : i32) : i32
28 %1 = llvm.mlir.addressof @i : !llvm.ptr
29 %2 = llvm.mlir.addressof @j : !llvm.ptr
30 %loop_ub = llvm.mlir.constant(9 : i32) : i32
31 %loop_lb = llvm.mlir.constant(0 : i32) : i32
32 %loop_step = llvm.mlir.constant(1 : i32) : i32
33 omp.wsloop reduction(byref @add_reduction_i_32 %1 -> %arg0, byref @add_reduction_i_32 %2 -> %arg1 : !llvm.ptr, !llvm.ptr) {
34 omp.loop_nest (%loop_cnt) : i32 = (%loop_lb) to (%loop_ub) inclusive step (%loop_step) {
35 llvm.store %0, %arg0 : i32, !llvm.ptr
36 llvm.store %0, %arg1 : i32, !llvm.ptr
42 llvm.mlir.global internal @i() {addr_space = 0 : i32} : i32 {
43 %0 = llvm.mlir.constant(0 : i32) : i32
46 llvm.mlir.global internal @j() {addr_space = 0 : i32} : i32 {
47 %0 = llvm.mlir.constant(0 : i32) : i32
50 llvm.func @malloc(%arg0 : i64) -> !llvm.ptr
51 llvm.func @free(%arg0 : !llvm.ptr) -> ()
53 // Private reduction variable and its initialization.
54 // CHECK: %[[PRIV_PTR_I:.+]] = alloca ptr
55 // CHECK: %[[PRIV_PTR_J:.+]] = alloca ptr
56 // CHECK: %[[MALLOC_I:.+]] = call ptr @malloc(i64 4)
57 // CHECK: store ptr %[[MALLOC_I]], ptr %[[PRIV_PTR_I]]
58 // CHECK: %[[MALLOC_J:.+]] = call ptr @malloc(i64 4)
59 // CHECK: store ptr %[[MALLOC_J]], ptr %[[PRIV_PTR_J]]
61 // Call to the reduction function.
62 // CHECK: call i32 @__kmpc_reduce
63 // CHECK-SAME: @[[REDFUNC:[A-Za-z_.][A-Za-z0-9_.]*]]
65 // Weirdly the finalization block is generated before the reduction blocks:
66 // CHECK: [[FINALIZE:.+]]:
67 // CHECK: call void @__kmpc_barrier
68 // CHECK: %[[PRIV_I:.+]] = load ptr, ptr %[[PRIV_PTR_I]], align 8
69 // CHECK: call void @free(ptr %[[PRIV_I]])
70 // CHECK: %[[PRIV_J:.+]] = load ptr, ptr %[[PRIV_PTR_J]], align 8
71 // CHECK: call void @free(ptr %[[PRIV_J]])
74 // Non-atomic reduction:
75 // CHECK: %[[PRIV_VAL_PTR_I:.+]] = load ptr, ptr %[[PRIV_PTR_I]]
76 // CHECK: %[[LOAD_I:.+]] = load i32, ptr @i
77 // CHECK: %[[PRIV_VAL_I:.+]] = load i32, ptr %[[PRIV_VAL_PTR_I]]
78 // CHECK: %[[SUM_I:.+]] = add i32 %[[LOAD_I]], %[[PRIV_VAL_I]]
79 // CHECK: store i32 %[[SUM_I]], ptr @i
80 // CHECK: %[[PRIV_VAL_PTR_J:.+]] = load ptr, ptr %[[PRIV_PTR_J]]
81 // CHECK: %[[LOAD_J:.+]] = load i32, ptr @j
82 // CHECK: %[[PRIV_VAL_J:.+]] = load i32, ptr %[[PRIV_VAL_PTR_J]]
83 // CHECK: %[[SUM_J:.+]] = add i32 %[[LOAD_J]], %[[PRIV_VAL_J]]
84 // CHECK: store i32 %[[SUM_J]], ptr @j
85 // CHECK: call void @__kmpc_end_reduce
86 // CHECK: br label %[[FINALIZE]]
88 // Reduction function.
89 // CHECK: define internal void @[[REDFUNC]]