xref: /llvm-project/mlir/test/Conversion/MemRefToSPIRV/alloca.mlir (revision 370a7eae352abb9646e8f86aae02930c38135e23)
1// RUN: mlir-opt -split-input-file -convert-memref-to-spirv -canonicalize -verify-diagnostics %s -o - | FileCheck %s
2
3module attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.3, [Shader], []>, #spirv.resource_limits<>>} {
4  func.func @alloc_function_variable(%arg0 : index, %arg1 : index) {
5    %0 = memref.alloca() : memref<4x5xf32, #spirv.storage_class<Function>>
6    %1 = memref.load %0[%arg0, %arg1] : memref<4x5xf32, #spirv.storage_class<Function>>
7    memref.store %1, %0[%arg0, %arg1] : memref<4x5xf32, #spirv.storage_class<Function>>
8    return
9  }
10}
11
12// CHECK-LABEL: func @alloc_function_variable
13//       CHECK:   %[[VAR:.+]] = spirv.Variable : !spirv.ptr<!spirv.struct<(!spirv.array<20 x f32>)>, Function>
14//       CHECK:   %[[LOADPTR:.+]] = spirv.AccessChain %[[VAR]]
15//       CHECK:   %[[VAL:.+]] = spirv.Load "Function" %[[LOADPTR]] : f32
16//       CHECK:   %[[STOREPTR:.+]] = spirv.AccessChain %[[VAR]]
17//       CHECK:   spirv.Store "Function" %[[STOREPTR]], %[[VAL]] : f32
18
19// -----
20
21module attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.3, [Shader], []>, #spirv.resource_limits<>>} {
22  func.func @two_allocs() {
23    %0 = memref.alloca() : memref<4x5xf32, #spirv.storage_class<Function>>
24    %1 = memref.alloca() : memref<2x3xi32, #spirv.storage_class<Function>>
25    return
26  }
27}
28
29// CHECK-LABEL: func @two_allocs
30//   CHECK-DAG: spirv.Variable : !spirv.ptr<!spirv.struct<(!spirv.array<6 x i32>)>, Function>
31//   CHECK-DAG: spirv.Variable : !spirv.ptr<!spirv.struct<(!spirv.array<20 x f32>)>, Function>
32
33// -----
34
35module attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.3, [Shader], []>, #spirv.resource_limits<>>} {
36  func.func @two_allocs_vector() {
37    %0 = memref.alloca() : memref<4xvector<4xf32>, #spirv.storage_class<Function>>
38    %1 = memref.alloca() : memref<2xvector<2xi32>, #spirv.storage_class<Function>>
39    return
40  }
41}
42
43// CHECK-LABEL: func @two_allocs_vector
44//   CHECK-DAG: spirv.Variable : !spirv.ptr<!spirv.struct<(!spirv.array<2 x vector<2xi32>>)>, Function>
45//   CHECK-DAG: spirv.Variable : !spirv.ptr<!spirv.struct<(!spirv.array<4 x vector<4xf32>>)>, Function>
46
47
48// -----
49
50module attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.3, [Shader], []>, #spirv.resource_limits<>>} {
51  // CHECK-LABEL: func @alloc_dynamic_size
52  func.func @alloc_dynamic_size(%arg0 : index) -> f32 {
53    // CHECK: memref.alloca
54    %0 = memref.alloca(%arg0) : memref<4x?xf32, #spirv.storage_class<Function>>
55    %1 = memref.load %0[%arg0, %arg0] : memref<4x?xf32, #spirv.storage_class<Function>>
56    return %1: f32
57  }
58}
59
60// -----
61
62module attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.3, [Shader], []>, #spirv.resource_limits<>>} {
63  // CHECK-LABEL: func @alloc_unsupported_memory_space
64  func.func @alloc_unsupported_memory_space(%arg0: index) -> f32 {
65    // CHECK: memref.alloca
66    %0 = memref.alloca() : memref<4x5xf32>
67    %1 = memref.load %0[%arg0, %arg0] : memref<4x5xf32>
68    return %1: f32
69  }
70}
71
72// -----
73
74module attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.3, [Shader], []>, #spirv.resource_limits<>>} {
75  func.func @zero_size() {
76    %0 = memref.alloca() : memref<0xf32, #spirv.storage_class<Function>>
77    return
78  }
79}
80
81// Zero-sized allocations are not handled yet. Just make sure we do not crash.
82// CHECK-LABEL: func @zero_size
83