93 lines · plain
1// RUN: mlir-opt -convert-scf-to-spirv %s -o - | FileCheck %s2 3module attributes {4 spirv.target_env = #spirv.target_env<5 #spirv.vce<v1.0, [Shader], [SPV_KHR_storage_buffer_storage_class]>, #spirv.resource_limits<>>6} {7 8// CHECK-LABEL: @loop_kernel9func.func @loop_kernel(%arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg3 : memref<10xf32, #spirv.storage_class<StorageBuffer>>) {10 // CHECK: %[[LB:.*]] = spirv.Constant 4 : i3211 %lb = arith.constant 4 : index12 // CHECK: %[[UB:.*]] = spirv.Constant 42 : i3213 %ub = arith.constant 42 : index14 // CHECK: %[[STEP:.*]] = spirv.Constant 2 : i3215 %step = arith.constant 2 : index16 // CHECK: spirv.mlir.loop {17 // CHECK-NEXT: spirv.Branch ^[[HEADER:.*]](%[[LB]] : i32)18 // CHECK: ^[[HEADER]](%[[INDVAR:.*]]: i32):19 // CHECK: %[[CMP:.*]] = spirv.SLessThan %[[INDVAR]], %[[UB]] : i3220 // CHECK: spirv.BranchConditional %[[CMP]], ^[[BODY:.*]], ^[[MERGE:.*]]21 // CHECK: ^[[BODY]]:22 // CHECK: %[[ZERO1:.*]] = spirv.Constant 0 : i3223 // CHECK: spirv.AccessChain {{%.*}}{{\[}}%[[ZERO1]], %[[INDVAR]]{{\]}}24 // CHECK: %[[ZERO2:.*]] = spirv.Constant 0 : i3225 // CHECK: spirv.AccessChain {{%.*}}[%[[ZERO2]], %[[INDVAR]]]26 // CHECK: %[[INCREMENT:.*]] = spirv.IAdd %[[INDVAR]], %[[STEP]] : i3227 // CHECK: spirv.Branch ^[[HEADER]](%[[INCREMENT]] : i32)28 // CHECK: ^[[MERGE]]29 // CHECK: spirv.mlir.merge30 // CHECK: }31 scf.for %arg4 = %lb to %ub step %step {32 %1 = memref.load %arg2[%arg4] : memref<10xf32, #spirv.storage_class<StorageBuffer>>33 memref.store %1, %arg3[%arg4] : memref<10xf32, #spirv.storage_class<StorageBuffer>>34 }35 return36}37 38// CHECK-LABEL: @unsigned_loop39// CHECK: spirv.ULessThan40func.func @unsigned_loop(%arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg3 : memref<10xf32, #spirv.storage_class<StorageBuffer>>) {41 %lb = arith.constant 4 : index42 %ub = arith.constant 42 : index43 %step = arith.constant 2 : index44 scf.for unsigned %arg4 = %lb to %ub step %step {45 %1 = memref.load %arg2[%arg4] : memref<10xf32, #spirv.storage_class<StorageBuffer>>46 memref.store %1, %arg3[%arg4] : memref<10xf32, #spirv.storage_class<StorageBuffer>>47 }48 return49}50 51// CHECK-LABEL: @loop_yield52func.func @loop_yield(%arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg3 : memref<10xf32, #spirv.storage_class<StorageBuffer>>) {53 // CHECK: %[[LB:.*]] = spirv.Constant 4 : i3254 %lb = arith.constant 4 : index55 // CHECK: %[[UB:.*]] = spirv.Constant 42 : i3256 %ub = arith.constant 42 : index57 // CHECK: %[[STEP:.*]] = spirv.Constant 2 : i3258 %step = arith.constant 2 : index59 // CHECK: %[[INITVAR1:.*]] = spirv.Constant 0.000000e+00 : f3260 %s0 = arith.constant 0.0 : f3261 // CHECK: %[[INITVAR2:.*]] = spirv.Constant 1.000000e+00 : f3262 %s1 = arith.constant 1.0 : f3263 // CHECK: %[[VAR1:.*]] = spirv.Variable : !spirv.ptr<f32, Function>64 // CHECK: %[[VAR2:.*]] = spirv.Variable : !spirv.ptr<f32, Function>65 // CHECK: spirv.mlir.loop {66 // CHECK: spirv.Branch ^[[HEADER:.*]](%[[LB]], %[[INITVAR1]], %[[INITVAR2]] : i32, f32, f32)67 // CHECK: ^[[HEADER]](%[[INDVAR:.*]]: i32, %[[CARRIED1:.*]]: f32, %[[CARRIED2:.*]]: f32):68 // CHECK: %[[CMP:.*]] = spirv.SLessThan %[[INDVAR]], %[[UB]] : i3269 // CHECK: spirv.BranchConditional %[[CMP]], ^[[BODY:.*]], ^[[MERGE:.*]]70 // CHECK: ^[[BODY]]:71 // CHECK: %[[UPDATED:.*]] = spirv.FAdd %[[CARRIED1]], %[[CARRIED1]] : f3272 // CHECK-DAG: %[[INCREMENT:.*]] = spirv.IAdd %[[INDVAR]], %[[STEP]] : i3273 // CHECK-DAG: spirv.Store "Function" %[[VAR1]], %[[UPDATED]] : f3274 // CHECK-DAG: spirv.Store "Function" %[[VAR2]], %[[UPDATED]] : f3275 // CHECK: spirv.Branch ^[[HEADER]](%[[INCREMENT]], %[[UPDATED]], %[[UPDATED]] : i32, f32, f32)76 // CHECK: ^[[MERGE]]:77 // CHECK: spirv.mlir.merge78 // CHECK: }79 %result:2 = scf.for %i0 = %lb to %ub step %step iter_args(%si = %s0, %sj = %s1) -> (f32, f32) {80 %sn = arith.addf %si, %si : f3281 scf.yield %sn, %sn : f32, f3282 }83 // CHECK-DAG: %[[OUT1:.*]] = spirv.Load "Function" %[[VAR1]] : f3284 // CHECK-DAG: %[[OUT2:.*]] = spirv.Load "Function" %[[VAR2]] : f3285 // CHECK: spirv.Store "StorageBuffer" {{%.*}}, %[[OUT1]] : f3286 // CHECK: spirv.Store "StorageBuffer" {{%.*}}, %[[OUT2]] : f3287 memref.store %result#0, %arg3[%lb] : memref<10xf32, #spirv.storage_class<StorageBuffer>>88 memref.store %result#1, %arg3[%ub] : memref<10xf32, #spirv.storage_class<StorageBuffer>>89 return90}91 92} // end module93