116 lines · plain
1// RUN: mlir-opt %s --test-loop-unrolling="unroll-factor=3" -split-input-file -canonicalize | FileCheck %s2// RUN: mlir-opt %s --test-loop-unrolling="unroll-factor=1" -split-input-file -canonicalize | FileCheck %s --check-prefix UNROLL-BY-13// RUN: mlir-opt %s --test-loop-unrolling="unroll-factor=-1" -split-input-file -canonicalize | FileCheck %s --check-prefix UNROLL-FULL4 5// CHECK-LABEL: scf_loop_unroll_single6func.func @scf_loop_unroll_single(%arg0 : f32, %arg1 : f32) -> f32 {7 %from = arith.constant 0 : index8 %to = arith.constant 10 : index9 %step = arith.constant 1 : index10 %sum = scf.for %iv = %from to %to step %step iter_args(%sum_iter = %arg0) -> (f32) {11 %next = arith.addf %sum_iter, %arg1 : f3212 scf.yield %next : f3213 }14 // CHECK: %[[SUM:.*]] = scf.for %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[V0:.*]] =15 // CHECK-NEXT: %[[V1:.*]] = arith.addf %[[V0]]16 // CHECK-NEXT: %[[V2:.*]] = arith.addf %[[V1]]17 // CHECK-NEXT: %[[V3:.*]] = arith.addf %[[V2]]18 // CHECK-NEXT: scf.yield %[[V3]]19 // CHECK-NEXT: }20 // CHECK-NEXT: %[[RES:.*]] = arith.addf %[[SUM]],21 // CHECK-NEXT: return %[[RES]]22 return %sum : f3223}24 25// CHECK-LABEL: scf_loop_unroll_double_symbolic_ub26// CHECK-SAME: (%{{.*}}: f32, %{{.*}}: f32, %[[N:.*]]: index)27func.func @scf_loop_unroll_double_symbolic_ub(%arg0 : f32, %arg1 : f32, %n : index) -> (f32,f32) {28 %from = arith.constant 0 : index29 %step = arith.constant 1 : index30 %sum:2 = scf.for %iv = %from to %n step %step iter_args(%i0 = %arg0, %i1 = %arg1) -> (f32, f32) {31 %sum0 = arith.addf %i0, %arg0 : f3232 %sum1 = arith.addf %i1, %arg1 : f3233 scf.yield %sum0, %sum1 : f32, f3234 }35 return %sum#0, %sum#1 : f32, f3236 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index37 // CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index38 // CHECK-DAG: %[[C3:.*]] = arith.constant 3 : index39 // CHECK-NEXT: %[[REM:.*]] = arith.remsi %[[N]], %[[C3]]40 // CHECK-NEXT: %[[UB:.*]] = arith.subi %[[N]], %[[REM]]41 // CHECK-NEXT: %[[SUM:.*]]:2 = scf.for {{.*}} = %[[C0]] to %[[UB]] step %[[C3]] iter_args42 // CHECK: }43 // CHECK-NEXT: %[[SUM1:.*]]:2 = scf.for {{.*}} = %[[UB]] to %[[N]] step %[[C1]] iter_args(%[[V1:.*]] = %[[SUM]]#0, %[[V2:.*]] = %[[SUM]]#1)44 // CHECK: }45 // CHECK-NEXT: return %[[SUM1]]#0, %[[SUM1]]#146}47 48// UNROLL-BY-1-LABEL: scf_loop_unroll_factor_1_promote49func.func @scf_loop_unroll_factor_1_promote() -> () {50 %step = arith.constant 1 : index51 %lo = arith.constant 0 : index52 %hi = arith.constant 1 : index53 scf.for %i = %lo to %hi step %step {54 %x = "test.foo"(%i) : (index) -> i3255 }56 return57 // UNROLL-BY-1-NEXT: %[[C0:.*]] = arith.constant 0 : index58 // UNROLL-BY-1-NEXT: %{{.*}} = "test.foo"(%[[C0]]) : (index) -> i3259}60 61// UNROLL-FULL-LABEL: func @scf_loop_unroll_full_single62// UNROLL-FULL-SAME: %[[ARG:.*]]: index)63func.func @scf_loop_unroll_full_single(%arg : index) -> index {64 %0 = arith.constant 0 : index65 %1 = arith.constant 1 : index66 %2 = arith.constant 4 : index67 %4 = scf.for %iv = %0 to %2 step %1 iter_args(%arg1 = %1) -> index {68 %3 = arith.addi %arg1, %arg : index69 scf.yield %3 : index70 }71 return %4 : index72 // UNROLL-FULL: %[[C1:.*]] = arith.constant 1 : index73 // UNROLL-FULL: %[[V0:.*]] = arith.addi %[[ARG]], %[[C1]] : index74 // UNROLL-FULL: %[[V1:.*]] = arith.addi %[[V0]], %[[ARG]] : index75 // UNROLL-FULL: %[[V2:.*]] = arith.addi %[[V1]], %[[ARG]] : index76 // UNROLL-FULL: %[[V3:.*]] = arith.addi %[[V2]], %[[ARG]] : index77 // UNROLL-FULL: return %[[V3]] : index78}79 80// UNROLL-FULL-LABEL: func @scf_loop_unroll_full_outter_loops81// UNROLL-FULL-SAME: %[[ARG:.*]]: vector<4x4xindex>)82func.func @scf_loop_unroll_full_outter_loops(%arg0: vector<4x4xindex>) -> index {83 %0 = arith.constant 0 : index84 %1 = arith.constant 1 : index85 %2 = arith.constant 4 : index86 %6 = scf.for %arg1 = %0 to %2 step %1 iter_args(%it0 = %0) -> index {87 %5 = scf.for %arg2 = %0 to %2 step %1 iter_args(%it1 = %it0) -> index {88 %3 = vector.extract %arg0[%arg1, %arg2] : index from vector<4x4xindex>89 %4 = arith.addi %3, %it1 : index90 scf.yield %3 : index91 }92 scf.yield %5 : index93 }94 return %6 : index95 // UNROLL-FULL: %[[C0:.*]] = arith.constant 0 : index96 // UNROLL-FULL: %[[C1:.*]] = arith.constant 1 : index97 // UNROLL-FULL: %[[C4:.*]] = arith.constant 4 : index98 // UNROLL-FULL: %[[SUM0:.*]] = scf.for %[[IV:.*]] = %[[C0]] to %[[C4]] step %[[C1]] iter_args(%{{.*}} = %[[C0]])99 // UNROLL-FULL: %[[VAL:.*]] = vector.extract %[[ARG]][0, %[[IV]]] : index from vector<4x4xindex>100 // UNROLL-FULL: scf.yield %[[VAL]] : index101 // UNROLL-FULL: }102 // UNROLL-FULL: %[[SUM1:.*]] = scf.for %[[IV:.*]] = %[[C0]] to %[[C4]] step %[[C1]] iter_args(%{{.*}} = %[[SUM0]])103 // UNROLL-FULL: %[[VAL:.*]] = vector.extract %[[ARG]][1, %[[IV]]] : index from vector<4x4xindex>104 // UNROLL-FULL: scf.yield %[[VAL]] : index105 // UNROLL-FULL: }106 // UNROLL-FULL: %[[SUM2:.*]] = scf.for %[[IV:.*]] = %[[C0]] to %[[C4]] step %[[C1]] iter_args(%{{.*}} = %[[SUM1]])107 // UNROLL-FULL: %[[VAL:.*]] = vector.extract %[[ARG]][2, %[[IV]]] : index from vector<4x4xindex>108 // UNROLL-FULL: scf.yield %[[VAL]] : index109 // UNROLL-FULL: }110 // UNROLL-FULL: %[[SUM3:.*]] = scf.for %[[IV:.*]] = %[[C0]] to %[[C4]] step %[[C1]] iter_args(%{{.*}} = %[[SUM2]])111 // UNROLL-FULL: %[[VAL:.*]] = vector.extract %[[ARG]][3, %[[IV]]] : index from vector<4x4xindex>112 // UNROLL-FULL: scf.yield %[[VAL]] : index113 // UNROLL-FULL: }114 // UNROLL-FULL: return %[[SUM3]] : index115}116