110 lines · plain
1// RUN: mlir-opt %s -acc-implicit-data=enable-implicit-reduction-copy=true -split-input-file | FileCheck %s --check-prefix=COPY2// RUN: mlir-opt %s -acc-implicit-data=enable-implicit-reduction-copy=false -split-input-file | FileCheck %s --check-prefix=FIRSTPRIVATE3 4// Test case: scalar reduction variable in parallel loop5// When enable-implicit-reduction-copy=true: expect copyin/copyout for reduction variable6// When enable-implicit-reduction-copy=false: expect firstprivate for reduction variable7 8acc.reduction.recipe @reduction_add_memref_i32 : memref<i32> reduction_operator <add> init {9^bb0(%arg0: memref<i32>):10 %c0_i32 = arith.constant 0 : i3211 %alloc = memref.alloca() : memref<i32>12 memref.store %c0_i32, %alloc[] : memref<i32>13 acc.yield %alloc : memref<i32>14} combiner {15^bb0(%arg0: memref<i32>, %arg1: memref<i32>):16 %0 = memref.load %arg0[] : memref<i32>17 %1 = memref.load %arg1[] : memref<i32>18 %2 = arith.addi %0, %1 : i3219 memref.store %2, %arg0[] : memref<i32>20 acc.yield %arg0 : memref<i32>21}22 23func.func @test_reduction_implicit_copy() {24 %c1_i32 = arith.constant 1 : i3225 %c100_i32 = arith.constant 100 : i3226 %c0_i32 = arith.constant 0 : i3227 %r = memref.alloca() : memref<i32>28 memref.store %c0_i32, %r[] : memref<i32>29 30 acc.parallel {31 %red_var = acc.reduction varPtr(%r : memref<i32>) recipe(@reduction_add_memref_i32) -> memref<i32> {name = "r"}32 acc.loop reduction(%red_var : memref<i32>) control(%iv : i32) = (%c1_i32 : i32) to (%c100_i32 : i32) step (%c1_i32 : i32) {33 %load = memref.load %red_var[] : memref<i32>34 %add = arith.addi %load, %c1_i32 : i3235 memref.store %add, %red_var[] : memref<i32>36 acc.yield37 } attributes {independent = [#acc.device_type<none>]}38 acc.yield39 }40 return41}42 43// When enable-implicit-reduction-copy=true: expect copyin/copyout for reduction variable44// COPY-LABEL: func.func @test_reduction_implicit_copy45// COPY: %[[COPYIN:.*]] = acc.copyin varPtr({{.*}} : memref<i32>) -> memref<i32> {dataClause = #acc<data_clause acc_reduction>, implicit = true, name = ""}46// COPY: acc.copyout accPtr(%[[COPYIN]] : memref<i32>) to varPtr({{.*}} : memref<i32>) {dataClause = #acc<data_clause acc_copy>, implicit = true, name = ""}47 48// When enable-implicit-reduction-copy=false: expect firstprivate for reduction variable 49// FIRSTPRIVATE-LABEL: func.func @test_reduction_implicit_copy50// FIRSTPRIVATE: acc.firstprivate varPtr({{.*}} : memref<i32>) recipe({{.*}}) -> memref<i32> {implicit = true, name = ""}51// FIRSTPRIVATE-NOT: acc.copyin52// FIRSTPRIVATE-NOT: acc.copyout53 54// -----55 56// Test case: reduction variable used both in loop and outside57// Should be firstprivate regardless of the flag setting58 59acc.reduction.recipe @reduction_add_memref_i32_2 : memref<i32> reduction_operator <add> init {60^bb0(%arg0: memref<i32>):61 %c0_i32 = arith.constant 0 : i3262 %alloc = memref.alloca() : memref<i32>63 memref.store %c0_i32, %alloc[] : memref<i32>64 acc.yield %alloc : memref<i32>65} combiner {66^bb0(%arg0: memref<i32>, %arg1: memref<i32>):67 %0 = memref.load %arg0[] : memref<i32>68 %1 = memref.load %arg1[] : memref<i32>69 %2 = arith.addi %0, %1 : i3270 memref.store %2, %arg0[] : memref<i32>71 acc.yield %arg0 : memref<i32>72}73 74func.func @test_reduction_with_usage_outside_loop() {75 %c1_i32 = arith.constant 1 : i3276 %c100_i32 = arith.constant 100 : i3277 %c0_i32 = arith.constant 0 : i3278 %r = memref.alloca() : memref<i32>79 %out = memref.alloca() : memref<i32>80 memref.store %c0_i32, %r[] : memref<i32>81 82 %out_create = acc.create varPtr(%out : memref<i32>) -> memref<i32> {dataClause = #acc<data_clause acc_copyout>, name = "out"}83 acc.parallel dataOperands(%out_create : memref<i32>) {84 %red_var = acc.reduction varPtr(%r : memref<i32>) recipe(@reduction_add_memref_i32_2) -> memref<i32> {name = "r"}85 acc.loop reduction(%red_var : memref<i32>) control(%iv : i32) = (%c1_i32 : i32) to (%c100_i32 : i32) step (%c1_i32 : i32) {86 %load = memref.load %red_var[] : memref<i32>87 %add = arith.addi %load, %c1_i32 : i3288 memref.store %add, %red_var[] : memref<i32>89 acc.yield90 } attributes {independent = [#acc.device_type<none>]}91 // out = r (usage of r outside the loop)92 %final_r = memref.load %r[] : memref<i32>93 memref.store %final_r, %out_create[] : memref<i32>94 acc.yield95 }96 acc.copyout accPtr(%out_create : memref<i32>) to varPtr(%out : memref<i32>) {dataClause = #acc<data_clause acc_copyout>, name = "out"}97 return98}99 100// In this case, r should be firstprivate regardless of the flag setting 101// because it's used outside the reduction context102// COPY-LABEL: func.func @test_reduction_with_usage_outside_loop103// COPY: acc.firstprivate varPtr({{.*}} : memref<i32>) recipe({{.*}}) -> memref<i32> {implicit = true, name = ""}104// COPY-NOT: acc.copyin varPtr({{.*}} : memref<i32>) -> memref<i32> {{.*}} name = ""105 106// FIRSTPRIVATE-LABEL: func.func @test_reduction_with_usage_outside_loop107// FIRSTPRIVATE: acc.firstprivate varPtr({{.*}} : memref<i32>) recipe({{.*}}) -> memref<i32> {implicit = true, name = ""}108// FIRSTPRIVATE-NOT: acc.copyin varPtr({{.*}} : memref<i32>) -> memref<i32> {{.*}} name = ""109 110