|
| 1 | +// Make sure that addition with carry produces expected results |
| 2 | +// with and without expansion to primitive add/cmp ops for WebGPU. |
| 3 | + |
| 4 | +// RUN: mlir-vulkan-runner %s \ |
| 5 | +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ |
| 6 | +// RUN: --entry-point-result=void | FileCheck %s |
| 7 | + |
| 8 | +// RUN: mlir-vulkan-runner %s --vulkan-runner-spirv-webgpu-prepare \ |
| 9 | +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ |
| 10 | +// RUN: --entry-point-result=void | FileCheck %s |
| 11 | + |
| 12 | +// CHECK: [0, 42, 0, 42] |
| 13 | +// CHECK: [1, 0, 1, 1] |
| 14 | +module attributes { |
| 15 | + gpu.container_module, |
| 16 | + spirv.target_env = #spirv.target_env< |
| 17 | + #spirv.vce<v1.4, [Shader], [SPV_KHR_storage_buffer_storage_class]>, #spirv.resource_limits<>> |
| 18 | +} { |
| 19 | + gpu.module @kernels { |
| 20 | + gpu.func @kernel_add(%arg0 : memref<4xi32>, %arg1 : memref<4xi32>, %arg2 : memref<4xi32>, %arg3 : memref<4xi32>) |
| 21 | + kernel attributes { spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [1, 1, 1]>} { |
| 22 | + %0 = gpu.block_id x |
| 23 | + %lhs = memref.load %arg0[%0] : memref<4xi32> |
| 24 | + %rhs = memref.load %arg1[%0] : memref<4xi32> |
| 25 | + %sum, %carry = arith.addui_extended %lhs, %rhs : i32, i1 |
| 26 | + |
| 27 | + %carry_i32 = arith.extui %carry : i1 to i32 |
| 28 | + |
| 29 | + memref.store %sum, %arg2[%0] : memref<4xi32> memref.store %carry_i32, %arg3[%0] : memref<4xi32> |
| 30 | + gpu.return |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + func.func @main() { |
| 35 | + %buf0 = memref.alloc() : memref<4xi32> |
| 36 | + %buf1 = memref.alloc() : memref<4xi32> |
| 37 | + %buf2 = memref.alloc() : memref<4xi32> |
| 38 | + %buf3 = memref.alloc() : memref<4xi32> |
| 39 | + %i32_0 = arith.constant 0 : i32 |
| 40 | + |
| 41 | + // Initialize output buffers. |
| 42 | + %buf4 = memref.cast %buf2 : memref<4xi32> to memref<?xi32> |
| 43 | + %buf5 = memref.cast %buf3 : memref<4xi32> to memref<?xi32> |
| 44 | + call @fillResource1DInt(%buf4, %i32_0) : (memref<?xi32>, i32) -> () |
| 45 | + call @fillResource1DInt(%buf5, %i32_0) : (memref<?xi32>, i32) -> () |
| 46 | + |
| 47 | + %idx_0 = arith.constant 0 : index |
| 48 | + %idx_1 = arith.constant 1 : index |
| 49 | + %idx_4 = arith.constant 4 : index |
| 50 | + |
| 51 | + // Initialize input buffers. |
| 52 | + %lhs_vals = arith.constant dense<[-1, 24, 4294967295, 43]> : vector<4xi32> |
| 53 | + %rhs_vals = arith.constant dense<[1, 18, 1, 4294967295]> : vector<4xi32> |
| 54 | + vector.store %lhs_vals, %buf0[%idx_0] : memref<4xi32>, vector<4xi32> |
| 55 | + vector.store %rhs_vals, %buf1[%idx_0] : memref<4xi32>, vector<4xi32> |
| 56 | + |
| 57 | + gpu.launch_func @kernels::@kernel_add |
| 58 | + blocks in (%idx_4, %idx_1, %idx_1) threads in (%idx_1, %idx_1, %idx_1) |
| 59 | + args(%buf0 : memref<4xi32>, %buf1 : memref<4xi32>, %buf2 : memref<4xi32>, %buf3 : memref<4xi32>) |
| 60 | + %buf_sum = memref.cast %buf4 : memref<?xi32> to memref<*xi32> |
| 61 | + %buf_carry = memref.cast %buf5 : memref<?xi32> to memref<*xi32> |
| 62 | + call @printMemrefI32(%buf_sum) : (memref<*xi32>) -> () |
| 63 | + call @printMemrefI32(%buf_carry) : (memref<*xi32>) -> () |
| 64 | + return |
| 65 | + } |
| 66 | + func.func private @fillResource1DInt(%0 : memref<?xi32>, %1 : i32) |
| 67 | + func.func private @printMemrefI32(%ptr : memref<*xi32>) |
| 68 | +} |
0 commit comments