// RUN: mlir-opt -split-input-file -convert-memref-to-spirv -canonicalize -verify-diagnostics %s -o - | FileCheck %s module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { func.func @alloc_dealloc_workgroup_mem_shader_f32(%arg0 : index, %arg1 : index) { %0 = memref.alloc() : memref<4x5xf32, #spirv.storage_class> %1 = memref.load %0[%arg0, %arg1] : memref<4x5xf32, #spirv.storage_class> memref.store %1, %0[%arg0, %arg1] : memref<4x5xf32, #spirv.storage_class> memref.dealloc %0 : memref<4x5xf32, #spirv.storage_class> return } } // CHECK: spirv.GlobalVariable @[[$VAR:.+]] : !spirv.ptr)>, Workgroup> // CHECK-LABEL: func @alloc_dealloc_workgroup_mem_shader_f32 // CHECK-NOT: memref.alloc // CHECK: %[[PTR:.+]] = spirv.mlir.addressof @[[$VAR]] // CHECK: %[[LOADPTR:.+]] = spirv.AccessChain %[[PTR]] // CHECK: %[[VAL:.+]] = spirv.Load "Workgroup" %[[LOADPTR]] : f32 // CHECK: %[[STOREPTR:.+]] = spirv.AccessChain %[[PTR]] // CHECK: spirv.Store "Workgroup" %[[STOREPTR]], %[[VAL]] : f32 // CHECK-NOT: memref.dealloc // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { func.func @alloc_dealloc_workgroup_mem_shader_i16(%arg0 : index, %arg1 : index) { %0 = memref.alloc() : memref<4x5xi16, #spirv.storage_class> %1 = memref.load %0[%arg0, %arg1] : memref<4x5xi16, #spirv.storage_class> memref.store %1, %0[%arg0, %arg1] : memref<4x5xi16, #spirv.storage_class> memref.dealloc %0 : memref<4x5xi16, #spirv.storage_class> return } } // CHECK: spirv.GlobalVariable @__workgroup_mem__{{[0-9]+}} // CHECK-SAME: !spirv.ptr)>, Workgroup> // CHECK-LABEL: func @alloc_dealloc_workgroup_mem_shader_i16 // CHECK: %[[VAR:.+]] = spirv.mlir.addressof @__workgroup_mem__0 // CHECK: %[[LOC:.+]] = spirv.SDiv // CHECK: %[[PTR:.+]] = spirv.AccessChain %[[VAR]][%{{.+}}, %[[LOC]]] // CHECK: %{{.+}} = spirv.Load "Workgroup" %[[PTR]] : i32 // CHECK: %[[LOC:.+]] = spirv.SDiv // CHECK: %[[PTR:.+]] = spirv.AccessChain %[[VAR]][%{{.+}}, %[[LOC]]] // CHECK: %{{.+}} = spirv.AtomicAnd %[[PTR]], %{{.+}} : !spirv.ptr // CHECK: %{{.+}} = spirv.AtomicOr %[[PTR]], %{{.+}} : !spirv.ptr // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { func.func @two_allocs() { %0 = memref.alloc() : memref<4x5xf32, #spirv.storage_class> %1 = memref.alloc() : memref<2x3xi32, #spirv.storage_class> return } } // CHECK-DAG: spirv.GlobalVariable @__workgroup_mem__{{[0-9]+}} // CHECK-SAME: !spirv.ptr)>, Workgroup> // CHECK-DAG: spirv.GlobalVariable @__workgroup_mem__{{[0-9]+}} // CHECK-SAME: !spirv.ptr)>, Workgroup> // CHECK-LABEL: func @two_allocs() // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { func.func @two_allocs_vector() { %0 = memref.alloc() : memref<4xvector<4xf32>, #spirv.storage_class> %1 = memref.alloc() : memref<2xvector<2xi32>, #spirv.storage_class> return } } // CHECK-DAG: spirv.GlobalVariable @__workgroup_mem__{{[0-9]+}} // CHECK-SAME: !spirv.ptr>)>, Workgroup> // CHECK-DAG: spirv.GlobalVariable @__workgroup_mem__{{[0-9]+}} // CHECK-SAME: !spirv.ptr>)>, Workgroup> // CHECK-LABEL: func @two_allocs_vector() // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { // CHECK-LABEL: func @alloc_dynamic_size func.func @alloc_dynamic_size(%arg0 : index) -> f32 { // CHECK: memref.alloc %0 = memref.alloc(%arg0) : memref<4x?xf32, #spirv.storage_class> %1 = memref.load %0[%arg0, %arg0] : memref<4x?xf32, #spirv.storage_class> return %1: f32 } } // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { // CHECK-LABEL: func @alloc_unsupported_memory_space func.func @alloc_unsupported_memory_space(%arg0: index) -> f32 { // CHECK: memref.alloc %0 = memref.alloc() : memref<4x5xf32, #spirv.storage_class> %1 = memref.load %0[%arg0, %arg0] : memref<4x5xf32, #spirv.storage_class> return %1: f32 } } // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { // CHECK-LABEL: func @dealloc_dynamic_size func.func @dealloc_dynamic_size(%arg0 : memref<4x?xf32, #spirv.storage_class>) { // CHECK: memref.dealloc memref.dealloc %arg0 : memref<4x?xf32, #spirv.storage_class> return } } // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { // CHECK-LABEL: func @dealloc_unsupported_memory_space func.func @dealloc_unsupported_memory_space(%arg0 : memref<4x5xf32, #spirv.storage_class>) { // CHECK: memref.dealloc memref.dealloc %arg0 : memref<4x5xf32, #spirv.storage_class> return } } // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { func.func @alloc_dealloc_workgroup_mem_kernel(%arg0 : index, %arg1 : index) { %0 = memref.alloc() : memref<4x5xf32, #spirv.storage_class> %1 = memref.load %0[%arg0, %arg1] : memref<4x5xf32, #spirv.storage_class> memref.store %1, %0[%arg0, %arg1] : memref<4x5xf32, #spirv.storage_class> memref.dealloc %0 : memref<4x5xf32, #spirv.storage_class> return } } // CHECK: spirv.GlobalVariable @[[$VAR:.+]] : !spirv.ptr, Workgroup> // CHECK-LABEL: func @alloc_dealloc_workgroup_mem_kernel // CHECK-NOT: memref.alloc // CHECK: %[[PTR:.+]] = spirv.mlir.addressof @[[$VAR]] // CHECK: %[[LOADPTR:.+]] = spirv.AccessChain %[[PTR]] // CHECK: %[[VAL:.+]] = spirv.Load "Workgroup" %[[LOADPTR]] : f32 // CHECK: %[[STOREPTR:.+]] = spirv.AccessChain %[[PTR]] // CHECK: spirv.Store "Workgroup" %[[STOREPTR]], %[[VAL]] : f32 // CHECK-NOT: memref.dealloc // ----- module attributes { spirv.target_env = #spirv.target_env< #spirv.vce, #spirv.resource_limits<>> } { func.func @zero_size() { %0 = memref.alloc() : memref<0xf32, #spirv.storage_class> %1 = memref.alloc() : memref<0xi1, #spirv.storage_class> %2 = memref.alloc() : memref<0xi4, #spirv.storage_class> return } } // Zero-sized allocations are not handled yet. Just make sure we do not crash. // CHECK-LABEL: func @zero_size()